Some things that bother me: mainly multitasking & protection

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
garob
Posts: 16
Joined: Wed Dec 30, 2009 3:26 am
Location: Melbourne, Australia

Some things that bother me: mainly multitasking & protection

Post 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
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

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

Post 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.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

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

Post 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.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

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

Post 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).
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
Post Reply