section .rodata gdt_flat_slice: dw GDT_FLAT_LEN dd gdt_flat global gdt_flat_slice ; Segment descriptor layout ; | Range (bits) | Field | ; |--------------|---------------| ; | 0-16 | limit | ; | 16-32 | base | ; | 32-40 | base cont. | ; | 40-48 | access | ; | 48-52 | limit cont. | ; | 52-56 | flags | ; | 56-64 | base cont. | ; ; Flags ; - 0: reserved ; - 1: long-mode code segment ; - 2: size ; - unset: 16-bit ; - set: 32-bit ; - 3: granularity ; - unset: limit is measured in bytes ; - set: limit is measured in 4KiB pages ; ; Access ; - 0: accessed ; - unset: CPU will set it when the segment is accessed ; - 1: readable / writable ; - data segments: is segment writable (data segments are always readable) ; - code segments: is segment readable (code segments are never writable) ; - 2: direction / conforming ; - data segments: whether segment grows down ; - code segments: whether this can be executed from a lower-privilege ring ; - 3: executable ; - unset: this is a data segment ; - set: this is a code segment ; - 4: descriptor type ; - unset: this is a task state segment ; - set: this is a data or code segment ; - 5-6: privilege level (ring number) ; - 7: present (must be set) ; align 8 gdt_flat: ; First GDT entry must be 0. dq 0 ; 32-bit code segment. ; Bytes 0x0000 - 0xffff. .segment_code_32: db 0xff, 0xff, \ 0x00, 0x00, \ 0x00, \ 10011011b, \ 01000000b, \ 0x00 ; 16-bit code segment, to use if we want to switch back to real mode. ; Bytes 0x0000 - 0xffff. .segment_code_16: db 0xff, 0xff, \ 0x00, 0x00, \ 0x00, \ 10011011b, \ 00000000b, \ 0x00 ; Data segment. ; Pages 0x000000 - 0x0fffff, which covers the entire 32-bit address space (start of 0xfffff-th page ; is 0xfffff * 4096 = 0xfffff000, end of page exclusive is 0xfffff000 + 4096 = 0x100000000). .segment_data: db 0xff, 0xff, \ 0x00, 0x00, \ 0x00, \ 10010011b, \ 11001111b, \ 0x00 global gdt_flat GDT_FLAT_LEN equ ($ - gdt_flat) GDT_FLAT_IDX_CODE_32 equ (gdt_flat.segment_code_32 - gdt_flat) global GDT_FLAT_IDX_CODE_32 GDT_FLAT_IDX_CODE_16 equ (gdt_flat.segment_code_16 - gdt_flat) global GDT_FLAT_IDX_CODE_16 GDT_FLAT_IDX_DATA equ (gdt_flat.segment_data - gdt_flat) global GDT_FLAT_IDX_DATA