Page 1 of 1

Some things that bother me: mainly multitasking & protection

Posted: Tue Mar 30, 2010 6:14 pm
by garob
Hi I'm just starting out, I've read a number of tutorials and I want to know if what I think I have learned is correct.

1. Firstly is setting up the GDT like in JamesM's tutorial enough if I'm going to use paging as a protection mechanism or when changing tasks do I need to modify it?

2. Secondly does each thread need a user stack and a kernel stack?

3. Thirdly how do I set a size to a stack and grow it when it runs out of room?

Thank you

Re: Some things that bother me: mainly multitasking & protec

Posted: Tue Mar 30, 2010 8:00 pm
by bewing
1a. Yes. You just set a flat memory space -- all bases = 0, all limits = 0xffffffff.
1b. No, you do not have to change the GDT, just the TSS.
2. Yes.
3. When using paging, there are generic sizes for pages. 4K is a typical generic size. You start with a stack sized to 4K, starting at the top of a 4k page. When you get a page fault because of the stack going off the BOTTOM of the page, you allocate a new page to the bottom of the stack.

Re: Some things that bother me: mainly multitasking & protec

Posted: Tue Mar 30, 2010 8:29 pm
by Gigasoft
The GDT usually only needs changing if one of the segments is to point to variables specific to the current thread.

Yes, each thread must have a kernel stack and an user stack. To implement a stack which grows, a maximum size should be specified and a virtual range reserved of that size. New pages can be allocated and mapped when page faults happen, just like with any other memory area. However, it's not recommended to have pageable kernel stacks. To implement pageable kernel stacks, the page fault vector should be a task gate. Each thread should then have an extra stack area for page faults, and the ESP in the TSS should be updated on each thread switch to point to the end of this area.

Re: Some things that bother me: mainly multitasking & protec

Posted: Tue Mar 30, 2010 11:05 pm
by Colonel Kernel
Technically, you don't need a separate kernel stack for each thread, it's just recommended. Some microkernel OSes like QNX have only one kernel stack per CPU. Other OSes can share kernel stacks between several user threads (e.g.: older versions of Solaris, the latest versions of Windows that include user-mode scheduling).