question about kernel stack and multiprocess

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

question about kernel stack and multiprocess

Post 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.
User avatar
Combuster
Member
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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
xenos
Member
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

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

Re: question about kernel stack and multiprocess

Post by serviper »

Thank you for answering! I'll try these technics in my implementation.
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

Re: question about kernel stack and multiprocess

Post 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?
Last edited by serviper on Fri Aug 03, 2012 5:50 am, edited 1 time in total.
User avatar
Combuster
Member
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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

Re: question about kernel stack and multiprocess

Post 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!
Dominator
Member
Member
Posts: 25
Joined: Fri Jul 06, 2012 10:52 am

Re: question about kernel stack and multiprocess

Post 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?
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: question about kernel stack and multiprocess

Post 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.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: question about kernel stack and multiprocess

Post 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
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

Re: question about kernel stack and multiprocess

Post 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.
Post Reply