On Tue, Aug 09, 2005 at 18:17:56 +0200, Paul Duplys wrote: > I am now trying to define the "struct thread_struct" for my target > architecture. I as inspected the code for other architectures, I found there > two stack pointers. For intel (i386): esp0 and esp; for Motorola: ksp and > usp (the comment here says, these are the kernel stack pointer and user > stack pointer; so I guess, esp0 and esp play basically the same role). For security and general sanity of the system, kernel and user code can't use the same stack. So each process has to have 2 stacks -- one is in kernel memory, used by kernel code, and one is in user memory, used by the user code. When entry to the kernel is made (either due to syscall or interrupt), the stack pointer is saved in the esp resp. usp entry and the other, esp0 resp. ksp is loaded. When returning to userspace, the kernel is stored back in esp0/ksp and esp/usp restored to the register. Anytime in kernel, there can be a task switch, which will also change stack pointers, this time storing esp0/ksp of the process being put to sleep and loading the one of the process scheduled. The kernel stack, since it lives in kernel memory, which is unswappable, is generally very small. It is usually allocated two pages, which it shares with thread_struct itself (see definition of macro current). > Unfortunatelly, I couldn't find any explanations (in the books I have) about > these two different stacks/stack pointers. Does someone have an idea, what > it is all about? I mean, why do I need two separate stack pointers? ------------------------------------------------------------------------------- Jan 'Bulb' Hudec <bulb@xxxxxx>
Attachment:
signature.asc
Description: Digital signature