1

How is a thread's initial context (registers state) determined on thread creation?

For example: Current thread context eax=0x4, ebx=0x9000, ecx=0xfff, etc... Create a thread is called to begin executing at 0xbfbfbf. It seems obvious that the thread's eip will be set to 0xbfbfbf, but how about the other registers. Are they copied from the current thread? Are they set to 0x00000000? Is the stack pointer the same? I want to know what happens to the initial thread context at thread creation. I've checked google and OS internals books and haven't found what I'm looking for.

Also, is it the same process across other OS, in particular Linux and Windows?

1 Answers1

0

When a new thread begins executing, its registers are loaded from its stored context just like a thread that has been running already. The thread that creates the thread can fill in the new thread's context structure however it likes before it adds it to the operating system's table of running threads.

Basically it works like this:

  1. A new task structure is allocated.

  2. The fields in that new task structure are filled in however desired. This includes registers, the stack pointer, and so on.

  3. The task structure is marked ready-to-run and added to the scheduler's list of tasks.

  4. The scheduler decides to schedule the new thread, loads its context from the task structure, and switches to it.

  5. The thread now has whatever registers, stack, and instruction pointer the creating thread filled in for it.