Okay I have a TSS, please verify that this is correct code. You really can't be sure when you're writing an OS. Sure it compiles, but is it working? Notice that I have set SS0 to my ring 0 data segment selector.
Code: Select all
global tss
tss:
dw 0, 0 ; back link
tss_esp0:
esp0 dd 0 ; ESP0
dw 10h, 0 ; SS0, reserved
dd 0 ; ESP1
dw 0, 0 ; SS1, reserved
dd 0 ; ESP2
dw 0, 0 ; SS2, reserved
dd 0 ; CR3
dd 0, 0 ; EIP, EFLAGS
dd 0, 0, 0, 0 ; EAX, ECX, EDX, EBX
dd 0, 0, 0, 0 ; ESP, EBP, ESI, EDI
dw 0, 0 ; ES, reserved
dw 0, 0 ; CS, reserved
dw 0, 0 ; SS, reserved
dw 0, 0 ; DS, reserved
dw 0, 0 ; FS, reserved
dw 0, 0 ; GS, reserved
dw 0, 0 ; LDT, reserved
dw 0, 103 ; debug, IO permission bitmap
Cut to where I'm loading my GDT. I am really unsure about this part. What this code does is sets the esp0 to the current (ring 0) esp value and loads the TSS selector.
Code: Select all
lgdt [gp]
mov [esp0], esp
mov ax, 0x28
ltr ax
I stuck in a few lines to store the ring 0 esp in my TSS and load the TSS selector. Is that all I need to do to have a working TSS? Do I need to stick code in my interrupt handler for anything TSS-related?
New GDT (should I just scrap the C gdt code and go to assembly?):
Code: Select all
gdt_set_gate(0, 0, 0, 0, 0);
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); // Ring 0 (kernel) CS and DS
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); // Ring 3 (user processes) CS and DS
gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);
gdt_set_gate(5, tss, tss+103, 0x89, 0xCF); // TSS descriptor
Okay, is this enough to have a working TSS? Doesn't seem like it because I'm still getting a GPF and Bochs CPU state dump is saying it's invalid:
Code: Select all
ldtr:s=0x0000, dl=0x00000000, dh=0x00000000, valid=0
tr:s=0x0028, dl=0x00000067, dh=0x00808900, valid=1
I noticed I forgot to set my EFLAGS for my user tasks to have a ring 3 IOPL. I now start out with an EFLAGS value of 0x3202.
However, with these new changes in place, I am STILL getting a GPF when I 'iret'. I probably am missing something obvious but necessary with the Intel architecture to have this working. Could anyone clue me in?
How much does it cost to order a printed version of the Intel manual? The "shopping cart" they have doesn't display the price.