preemptive Multitasking - how does the task-switch work

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
TimonGaertner123
Posts: 6
Joined: Thu Sep 23, 2021 3:08 am

preemptive Multitasking - how does the task-switch work

Post by TimonGaertner123 »

So i have a working normal task switch for cooperative multitasking.
Now i want to switch tasks with help of the pit irq.

Normally the irq is called, i push all the registers to the stack, call the handler, the handler returns, i pop all the registers from the stack and return back to the state before the interrupt with iretq.
If i simply call the task-switch in the handler, the task switch stores the wrong registers, and returns to the spot where rip points -> to the task, there it pops all the registers stored last time from the stack for the task and proceeds to execute the task, but because it does return to the task the part after the handler call: popping all the registers from the stack and returning back via iretq never happens.
In theory, how would i switch the task if i wanted to use the pit for it?
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: preemptive Multitasking - how does the task-switch work

Post by Octocontrabass »

Your task switch function should return to its caller. That way, when you call it inside your PIT handler, it returns to your PIT handler, and when you call it somewhere else, it returns to that somewhere else.

Typical kernels handle this by switching between kernel stacks, so each task has its own kernel stack.
TimonGaertner123
Posts: 6
Joined: Thu Sep 23, 2021 3:08 am

Re: preemptive Multitasking - how does the task-switch work

Post by TimonGaertner123 »

Octocontrabass wrote:Your task switch function should return to its caller. That way, when you call it inside your PIT handler, it returns to your PIT handler, and when you call it somewhere else, it returns to that somewhere else.

Typical kernels handle this by switching between kernel stacks, so each task has its own kernel stack.
but how do i get the iretq at the end to switch to the new task then?
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: preemptive Multitasking - how does the task-switch work

Post by Octocontrabass »

You don't. The IRETQ at the end of your PIT handler is used to return to the original task.

If you're setting up a new task and you want that new task to be in ring 3, you set up the new task's ring 0 stack to point to code that will jump into ring 3 (using IRETQ or SYSRET or whatever you want).
Post Reply