I'm working on a small OS that will use a separate Local Descriptor Table for each process. I understand that I will need to use the lldt
instruction to load a LDT segment from my GDT. I already have my kernel running in protected mode with a valid GDT, but I cannot figure out what the GDT entry for my LDT should look like. I understand that its base address should point to my LDT, but I don't know what the privilege level and other attributes should be. Here is the NASM code that represents the LDT entry in my GDT:
localTable equ $-gdt ; GDT entry #5 (selector 20h)
dw 0x1FF ; limit to 64 descriptors
dw 0x8000 ; base address
db 0x0
db 0x89 ; probably incorrect...
db 0x1f ; possibly incorrect...
db 0x0
If you are not familiar with the NASM syntax, this table entry has a base address of 0x8000 and a limit of 511 (512 bytes total, or 64 entries). I have read the section about the GDT and LDT in the i486 programmer's reference manual, but I cannot fully understand what my GDT entry should look like.
Anyway, I load the LDT like so:
mov ax, 0x20
lldt ax
This code causes the processor to generate a general protection fault (I handle it with an interrupt). I would like to know two things:
1) Did I correctly describe my LDT in the GDT? If not, what needs to be changed?
2) Could the LLDT
instruction be failing because there are invalid selectors in my LDT itself? I read the LLDT instruction spec, and it seems to me that it doesn't even read the memory of the LDT, but I just want to be sure the LLDT isn't failing because I have a typo in my LDT's data.
Ok, I figured it out. The type that I was using (1001b
) was not what I needed. I found that type 2 (10b
) is used for LDT entries. For the record, this information is in chapter 6, page 4, of the i486 Microprocessor Programmer's Manual. My functional GDT entry looks as follows:
localTable equ $-gdt ; GDT entry #5 (selector 20h)
dw 0x1FF ; limit to 64 descriptors
dw 0x8000 ; base address
db 0x0
db 0x82 ; 10000010b (segment present set, WTM)
db 0x1f
db 0x0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With