evmar / retrowin32

windows emulator
https://evmar.github.io/retrowin32/
Apache License 2.0
587 stars 26 forks source link

TLS not implemented #70

Closed evmar closed 1 month ago

evmar commented 2 months ago

Pocoman (#43) appears to depend on thread local storage, which is currently stubbed.

I pushed a new thread.exe demonstrating the tls problem in https://github.com/evmar/retrowin32/commit/769053b190b10c9a486c6b597291657177e3a3ee

Right now it prints

thread=0 name="main" tls=1 i=0
thread=1 name="i_am_thread" tls=2 i=0
thread=0 name="main" tls=2 i=1
thread=1 name="i_am_thread" tls=2 i=1
thread=0 name="main" tls=2 i=2
thread=0 name="main" tls=2 i=3
thread=1 name="i_am_thread" tls=2 i=2

But if you look at the code, the tls values it prints should flip between 1/2 depending on which thread is running.

evmar commented 2 months ago

The way this works in general in Windows is the fs register is supposed to point at the current thread's TEB, which then holds things like thread-local vars. You can see where retrowin32's TEB is set up here, and in particular how it doesn't set anything up per thread: https://github.com/evmar/retrowin32/blob/da79608b9d7b2e9c9ec1a9c518ffc145fe8c48cc/win32/src/winapi/kernel32/init.rs#L143

I think the way it should work is that instead whenever we create a thread we allocate a new TEB and set the fs_addr field in the new CPU appropriately. (And probably the place where the initial TEB is set up should use the same thing.)