Page 1 of 1
question about kernel stack and multiprocess
Posted: Mon Jul 18, 2011 10:38 pm
by serviper
Hi everyone.
I'm writing an OS for x86 architecture and I have some questions about the kernel stack. It seems that every process/task should have its own kernel stack, but I have no idea about 'split' the stack out of the identity mapped page tables.
How do you handle your kernel stack? Move it to a high address when initializing tasking, or just keep it out of the linked page table, which may contain kernel code and etc?
Feel free to correct any of my mistakes.
Re: question about kernel stack and multiprocess
Posted: Tue Jul 19, 2011 1:01 am
by Combuster
If you want a dedicated kernel stack per thread, all you really need to do is point TSS.ESP0 to a separate piece of physical memory and update both ESP (saved from last task switch) and TSS.ESP0 (always the same) on every task switch. You can draw kernel stacks from the kernel heap, you can map in pages in some other predefined part of the kernel half, or even into the user half. If you share pagetables, you always have access to all kernel stacks which makes process management a tad simpler.
Re: question about kernel stack and multiprocess
Posted: Tue Jul 19, 2011 1:07 am
by xenos
In my OS I have a "higher half" kernel residing above 0xC0000000 - so everything above 0xC0000000 is kernel space and shared by all processes, and everything below is unique to each process. The kernel space is divided further - for example, the page tables are mapped above 0xFFC00000. The area between 0xF0000000 and 0xFF000000 is reserved for kernel stacks. Whenever a new thread is created, the memory manager grabs a free physical page and maps it to a free virtual page in that area. The task manager then assigns this new page as the kernel stack for the new thread. (For my kernel, 4k are really enough for a kernel stack.) When the thread is destroyed, the page is unmapped again.
Re: question about kernel stack and multiprocess
Posted: Tue Jul 19, 2011 4:41 am
by serviper
Thank you for answering! I'll try these technics in my implementation.
Re: question about kernel stack and multiprocess
Posted: Tue Jul 19, 2011 5:40 am
by serviper
I just read James Molloy's tutorial, which uses the stack of GRUB at low address(<1MB, say, 0x60000 around) before initializing tasking. When doing identity mapping, the page table which contains both kernel code/data and the initial kernel stack is shared instead of copied. Later he has to move the stack to a higher address, say, kernel heap, and modify all ebps(may cause error?).
Could this solution work? Or there's some weakness, or fatal errors?
Re: question about kernel stack and multiprocess
Posted: Tue Jul 19, 2011 5:47 am
by Combuster
Technically, ESP is undefined and using it as is can cause any sorts of bugs you don't want. The usual approach is to enter higherhalf as soon as possible and then set up a stack there. That saves you from doing all sorts of hacks to move a stack that is in use later.
Re: question about kernel stack and multiprocess
Posted: Tue Jul 19, 2011 5:51 am
by serviper
Combuster wrote:Technically, ESP is undefined and using it as is can cause any sorts of bugs you don't want. The usual approach is to enter higherhalf as soon as possible and then set up a stack there. That saves you from doing all sorts of hacks to move a stack that is in use later.
Thanks a lot!
Re: question about kernel stack and multiprocess
Posted: Wed Jul 18, 2012 12:40 pm
by Dominator
Combuster wrote:Technically, ESP is undefined and using it as is can cause any sorts of bugs you don't want. The usual approach is to enter higherhalf as soon as possible and then set up a stack there. That saves you from doing all sorts of hacks to move a stack that is in use later.
If I start with a kernel stack in higher half, booting with VM enabled, when I switch to my own page directory, what should be done with the kernel stack? IOW, is there a clean way to re-establish the stack to make sure it's copied rather than linked in later processes?
Re: question about kernel stack and multiprocess
Posted: Fri Jul 20, 2012 3:36 am
by Nessphoro
Okay, Dominator lets get this straight.
After you established your main kernel stack in >0xC0000000 it will be linked every time you create a PD. But so what?
Just allocate a different page (Perhaps even at a predefined address or follow the XenOS' approach)
NO stack relocation is needed.
Re: question about kernel stack and multiprocess
Posted: Fri Jul 20, 2012 3:57 am
by bluemoon
serviper wrote:It seems that every process/task should have its own kernel stack.
One kernel stack per thread is one of the popular design. There is also other design like
one kernel stack per core, or anything hybrid. I highly recommend search Brendan's thread for kernel stack.
A quick search I found it here, if it's not you need to search it yourself.
http://forum.osdev.org/viewtopic.php?f= ... ck#p196844
Re: question about kernel stack and multiprocess
Posted: Tue Jul 31, 2012 7:25 am
by serviper
Dominator wrote:Combuster wrote:Technically, ESP is undefined and using it as is can cause any sorts of bugs you don't want. The usual approach is to enter higherhalf as soon as possible and then set up a stack there. That saves you from doing all sorts of hacks to move a stack that is in use later.
If I start with a kernel stack in higher half, booting with VM enabled, when I switch to my own page directory, what should be done with the kernel stack? IOW, is there a clean way to re-establish the stack to make sure it's copied rather than linked in later processes?
In my later solution, I start with the grub stack, boot with paging enabled and do some initialization such as GDT and IDT initialization. Then I grab a free page, return to the assembly code and set ESP and EBP at the end of the page, which is passed as a return value in EAX. Here I can safely call a C function for the next-phase initialization like multitasking.
Actually what you need is making the first stack manually and using a memory manager when creating processes.