OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:40 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Questions about kernel multitasking
PostPosted: Mon Apr 27, 2020 5:49 pm 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 293
Hi.
I want to implement multitasking in my core. To do this, I took the code from this article.
And I have some questions about the code in this article and so on.
There are 2 lines in the createTask() function that I don't understand
Code:
task->regs.cr3 = (uint32_t) pagedir;
task->regs.esp = (uint32_t) allocPage() + 0x1000;

What does the first line do? What is pagedir?
What does the second line do? As far as I understand, this is a thread stack.
How can I release a dedicated page when I don't need it? When do I call free to release a selected page(allocPage() as far as I understand allocates one page of memory)?
Explain how this code works.

I'm also wondering how I can automatically switch between threads, maybe use timers or something like that.

The article says that this is not quite "correct" multitasking, what should I use for my tasks?
Thanks.


Top
 Profile  
 
 Post subject: Re: Questions about kernel multitasking
PostPosted: Mon Apr 27, 2020 9:38 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
mrjbom wrote:
Explain how this code works.
It doesn't. Not as given, anyway. It is merely illustrative. The first of your quoted lines sets the in-memory version of CR3 to the last argument of the function. For a reason I have not figured out yet, many tutorials represent physical addresses as pointers (they aren't, since you cannot dereference them directly). That's why it is given in that form. It actually should just be called CR3 and have type uintptr_t. The reason you have to switch CR3 on task switch is that all user space processes have a different address space in the user space half. Optimization: If you are switching to a kernel task, you don't need to load CR3. Saves a TLB flush.

The second line... yeah, that won't work that way. The way I do it is: I allocate two contiguous pages, put the task struct right at the end of the allocation, and use everything before it as stack. I never deallocate this structure again; if the task finishes, I mark it as Condemned, and it can be resurrected by the task allocator.
mrjbom wrote:
I'm also wondering how I can automatically switch between threads, maybe use timers or something like that.
I tried to keep the structure of my task switcher simple, so here is how that works: Whenever something returns to user space (interrupt or syscall), I test the task flags of the current tasks for "signal pending" and "task timeout". If either is set, I reenter the kernel in a function that handles these states. The handler for "signal pending" is not relevant here. For "task timeout", it calls schedule() once and returns. schedule() will remove the "task timeout" flag, then pick the next task to run and switch there. Since the then-current task is still runnable, eventually execution will resume where it left off. Now the only thing left to do is install a timer interrupt handler that sets the "task timeout" flag.
mrjbom wrote:
The article says that this is not quite "correct" multitasking, what should I use for my tasks?
Well, lots of details were left out. And those details make the task switcher more complicated. You have to handle debug registers in it, and FPU, and SSE/AVX, etc. Or at the very least set the TS bit in CR0. There are optimizations like I alluded to above: Kernel tasks can use any address space and don't need CR3 reloaded. And the detail of how to pick the next task is entirely left out.

The code in that article is meant as illustration of the principles involved, not as cut-n-paste solution.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: Questions about kernel multitasking
PostPosted: Tue Apr 28, 2020 5:30 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 293
nullplan wrote:
mrjbom wrote:
Explain how this code works.
It doesn't. Not as given, anyway. It is merely illustrative. The first of...


I didn't understand much, but thank you for your answer.


Top
 Profile  
 
 Post subject: Re: Questions about kernel multitasking
PostPosted: Thu Jul 30, 2020 7:30 am 
Offline

Joined: Fri Jun 12, 2020 7:53 am
Posts: 17
Reading this:
Quote:
Optimization: If you are switching to a kernel task, you don't need to load CR3. Saves a TLB flush.


and this:
Quote:
Kernel tasks can use any address space and don't need CR3 reloaded


I'm guessing this is based on the kernel being mapped into each process, so kernel addresses are always valid no matter what the address space?


Top
 Profile  
 
 Post subject: Re: Questions about kernel multitasking
PostPosted: Thu Jul 30, 2020 9:32 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
LukeyTheKid wrote:
I'm guessing this is based on the kernel being mapped into each process, so kernel addresses are always valid no matter what the address space?
Yes. This is normal for a higher-half kernel, which is what most people are familiar with. The only other design I know is to forego virtual memory at all (which, for example, OS-9 uses), and only use the mechanism for memory protection (essentially, everything is identity mapped in those systems). For those OSes the same strategy applies. I am not aware of any other design that is still being used.

_________________
Carpe diem!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 32 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group