What is the reason for that? I have seen in MIPS context switching also there are some special registers are store into stack and restore from stack.Actually I want to figure it out how stack frame works when thread switching current thread to new thread in x86 assembly.I have read http://en.wikipedia.org/wiki/Setcontext and some other articles.Still I cant understand what is going on
-
http://stackoverflow.com/questions/4429398/why-does-windows64-use-a-different-calling-convention-from-all-other-oses-on-x86/4438515 has some pointers as to "why" things are done the way they are, in historical context. – FrankH. Mar 13 '14 at 11:49
-
Note that "stack frame" is optional with Microsoft tool sets. There's a compiler option to disable stack frame pointers, which allows ebp to be used for general purposes (it still needs to be saved by the callee). – rcgldr Mar 13 '14 at 16:03
2 Answers
The UNIX standard ABIs (Application Binary Interface) and as part of that, the C Programming binary interface implementations, refer to a processor-specific "supplement" section.
For 32bit x86, this is the Intel386 Architecture Processor Supplement document, and that (amongst many other things) specifies how registers are used when making function calls - in particular, the separation between caller-owned and callee-owned registers (i.e. which ones must be saved/restored if a function chooses to use them, and which are scratch).
Such a processor supplement document exists for all architectures using UN*X-style interfaces / ELF binaries; the Wikipedia page on ELF gives many pointers to the "processor supplement" for CPUs other than 32bit x86.
- 17,675
- 3
- 44
- 63
Stack frames and thread switching are unrelated. Each thread will have its own stack and each stack will have its own set of stack frames. The thread switching process goes along the lines of:-
thread 1 interrupted
cpu state is saved to thread 1 stack (cpu, fpu, sse, etc)
stack pointer is changed to point to thread 2's stack
cpu state is restored
return from interrupt (getting return address from thread 2's stack!)
That's just a basic outline, actual implementations will have a lot more in there, for example, determining what thread 2 actually is and so on.
The really important thing to remember is that absolutely everything is pushed to the stack when a thread switch occurs.
- 69,698
- 10
- 71
- 108