question about kernel stack and multiprocess
question about kernel stack and multiprocess
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.
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.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: question about kernel stack and multiprocess
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.
- xenos
- Member
- Posts: 1121
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: question about kernel stack and multiprocess
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
Thank you for answering! I'll try these technics in my implementation.
Re: question about kernel stack and multiprocess
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?
Could this solution work? Or there's some weakness, or fatal errors?
Last edited by serviper on Fri Aug 03, 2012 5:50 am, edited 1 time in total.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: question about kernel stack and multiprocess
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
Thanks a lot!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.
Re: question about kernel stack and multiprocess
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?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.
Re: question about kernel stack and multiprocess
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.
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
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.serviper wrote:It seems that every process/task should have its own 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
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.Dominator wrote: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?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.
Actually what you need is making the first stack manually and using a memory manager when creating processes.