I am learning to write an OS, and was trying to update GDT in kernel code, therefore i defined some structs in C
typedef unsigned int u32;
typedef int s32;
typedef unsigned short u16;
typedef short s16;
typedef unsigned char u8;
typedef char s8;
typedef struct{
u16 low_limit;
u16 low_base;
u8 mid_base;
u8 type; // access type
u8 flags; // high 4 bits(flags) low 4 bits (limit 4 last bits)
u8 high_base;
} __attribute__((packed)) gdt_gate_t;
typedef struct{
u16 limit;
u32 base;
}__attribute__((packed)) gdt_register_t;
typedef struct{
u8 RPL : 2; // Request Privilege Level
u8 TI : 1; // Table Indicator (GDT or LDT)
u16 index : 13;
}__attribute__((packed)) gdt_selector_t;
gdt_gate_t gdt[128];
gdt_register_t gdt_reg;
As you can see, i am aming to create GDT with 128 entries.
After i initialized GDT entries, i told gdt_reg
where is the base of the new GDT and it's limit using
gdt_reg.base = (u32)&gdt;
gdt_reg.limit = sizeof(gdt) - 1;
__asm__ __volatile__("lgdt (%0)" : : "r"(&gdt_reg));
In my environment, memory address of variable gdt
beginns at 0x00004820
, so the value stored in gdt_reg.base
should be as expected 0x00004820
. But actually in the memory, the value is 00 00 20 48
(little endian), then the regester gdtr
has value 48200000(3ff)
, which means the gdtr
points to an illegal address.
So the question is, why the value of gdt_reg.base
was reversed.
Many thanks for any help