Help with context switching.
Posted: Mon Dec 10, 2007 5:44 pm
Ok, from what I understand, this is what happens during a (software) context/task switch.
I can see this working fine for same PL context switches, but what if I want to switch back to a kernel task from a task executing at ring 3? Is there some way to detect that, or am I missing something?
(Of course, the above modal is a very basic scheduler, and doesn't deal with task priority, threads, or anything more complex than just switching tasks when the timer pulses, but it's for demonstration's sake.)
EDIT: I realized that each task/thread has it's own stack, so when returning using an iret, you need to push the EFLAGS, EIP, CS on the new task's stack.
- - Timer Pulses, CPU enters ISR in ring 0 (According to my IDT anyway). And registers/context info are stored in a task structure.
- (Additional task scheduling code).
- EFLAGS, EIP, CS are loaded from the new task's data struct, and are written over the existing values that currently reside within the stack.
- The rest of the new task's data is loaded from the struct, and placed in their according registers.
- (Send the interrupt clear flag to the timer PIC, so it can pulse again).
- iret occurs, popping EIP, EFLAGS, CS off the stack, and the program executes from EIP, at the PL described within EFLAGS until the timer pulses
I can see this working fine for same PL context switches, but what if I want to switch back to a kernel task from a task executing at ring 3? Is there some way to detect that, or am I missing something?
(Of course, the above modal is a very basic scheduler, and doesn't deal with task priority, threads, or anything more complex than just switching tasks when the timer pulses, but it's for demonstration's sake.)
EDIT: I realized that each task/thread has it's own stack, so when returning using an iret, you need to push the EFLAGS, EIP, CS on the new task's stack.