GDT setup question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
sawdust
Member
Member
Posts: 51
Joined: Thu Dec 20, 2007 4:04 pm

GDT setup question

Post by sawdust »

Is this a valid gdt setup? The system did not crash though.

Code: Select all

unsigned long test_data[65536];
int gdt_setup(void) {
    unsigned long test_data_size = sizeof(test_data);
     
    gdata.limit = (sizeof(struct gdt_item) * 3) - 1;
    gdata.base = (unsigned long) &gdt;
 
    gdt_set(0,0,0,0,0);
    gdt_set(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); //0x08 - CS
    gdt_set(2, 0, 0xFFFFFFFF, 0x92, 0xCF); //0x10 - DS
    gdt_set(3, test_data, test_data_size, 0x42, 0xCF); //0x18 ..
     
    gdt_sync();
 
    return 0;
}

gdt_sync:
    cli
    lgdt (gdata)
    mov $0x10, %ax
    mov %ax, %ds
    mov %ax, %es
    mov %ax, %fs
    mov %ax, %gs
    mov %ax, %ss
    ljmp $8, $flush2
flush2:    ret 
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

To answer that properly, we would need to see your gdt_set function, but assuming that your last entries are the access and granularity bytes, that looks ok.

The best way to tell is:

a) Run some ring 0 protected mode instructions (even just cli; hlt;) If they execute without triple-faulting, you're OK.
b) Run in Bochs - when you stop emulation, you'll get a register dump with a summary of segment base / limit values.

Cheers,
Adam
Post Reply