Page 1 of 1

preemptive Multitasking - how does the task-switch work

Posted: Wed May 04, 2022 10:42 am
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?

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

Posted: Wed May 04, 2022 11:24 am
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.

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

Posted: Wed May 04, 2022 11:29 am
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?

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

Posted: Wed May 04, 2022 11:37 am
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).