even after reading many of tutorials and documents (but they are all almost identical, what doesn't help me)
What I understand
First I linked IRQ0 to an interrupt handler:
(note: I switched back to the state where everything is identity mapped, so there is no problem about paging)
Code: Select all
onIRQ0_ISR:
pusha
push ds
push es
push fs
push gs
mov eax, cr3
push eax
mov eax, cr0
and eax, 0x7FFFFFFF ; paging off
mov cr0, eax
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
push esp
call onIRQ0
mov esp, eax
mov al, 0x20
out 0x20, al
pop eax
mov cr3, eax
mov eax, cr0
or eax, 0x80000000 ; paging on
mov cr0, eax
pop gs
pop fs
pop es
pop ds
popa
iret
What is messy in my head
To switch to ring3, I simply have to use the ds, es, fs, gs, cs corresponding to ring3 (I'm talking about what is initially pushed in the process's stack)
But how do you specify ss?
When there is an IRQ0 (or any other interruption), the CPU can't directly switch to ring0, so we have to use a TSS (that's what I understood, tell me if I'm wrong)
In my IDT, should I use a task gate or an interrupt gate?
If it is a task gate, how do I specify the entry point of the interruption handler? In the TSS (eip register)? But should I reset the TSS'eip register after each interruption?
The TSS's esp0 and ss0 must correspond to the kernel's stack, so when there is an interruption, my asm code is executing with the kernel's stack: but how do you switch stacks?
Do you need to create another stack for each process? (and the esp0 and ss0 must match the next process's second stack?)
I also thought that on task switch the TSS's esp0 and ss0 could be set to the next process's esp, but the esp may probably change during the time the process is running (and so data will be erased on task switch)
Anyway, when there is an interruption, the current esp isn't stored anywhere (because no TSS is running, the TSR register is empty, what happens when it is empty is not precised in the Intel Manuel) so how do you switch back to the former esp?
I presume there is another TSS constantly running which would store it, but if so there would be no advantage for using software task-switching as it would be slower than hardware task switching