0

After reading this question on how context switches occur, I am still a little confused as to how exactly the switch is made from a thread's user mode stack to its kernel stack. I would assume this would mean changing the value of the stack pointer to point to the kernel mode stack, but if the thread's original stack pointer is to be saved on the kernel stack, how can it change the stack pointer to point to the kernel stack first and then save the old value of the stack pointer?

Community
  • 1
  • 1
mclaassen
  • 5,018
  • 4
  • 30
  • 52

1 Answers1

1
  1. Copy the stack pointer to another register;
  2. load the new stack pointer;
  3. push the register onto the new stack.
CL.
  • 173,858
  • 17
  • 217
  • 259
  • But if it's saving the thread context doesn't it need to also preserve all the registers? – mclaassen Aug 13 '14 at 11:34
  • A switch between threads saves everything. But your question is about the user/kernel mode switch, i.e., the system call mechanism. – CL. Aug 13 '14 at 11:45
  • Ok so how does it work when a hardware interrupt occurs and we need to save the current thread's context and switch to kernel mode (and kernel stack) in order to run the interrupt handling routine? – mclaassen Aug 13 '14 at 13:35
  • 1
    That is a different question. In most cases, it's done automatically by the hardware. – CL. Aug 13 '14 at 14:15