Skip to content

Instantly share code, notes, and snippets.

@skyone-wzw
Created February 15, 2022 11:57
Show Gist options
  • Save skyone-wzw/3253615ab52e7d9ff049deba95bbefcf to your computer and use it in GitHub Desktop.
Save skyone-wzw/3253615ab52e7d9ff049deba95bbefcf to your computer and use it in GitHub Desktop.
A file learn how to write GDT (Global Descriptor Table) 带中文
; Segment Descriptor
;
; |------------------------------------------------------|----------------------------------------|
; | 63 56 | 55 52 | 51 48 | 47 40 | 39 32 |
; | Base (24~31) | Flags (3~0) | Limit (19~16) | Access Byte (7~0) | Base (23~16) |
; |------------------------------------------------------|----------------------------------------|
; | 31 16 | 15 0 |
; | Base (0~15) | Limit (0~15) |
; |------------------------------------------------------|----------------------------------------|
;
; Base: A 32-bit value containing the linear address where the segment begins.
;
; Limit: A 20-bit value, tells the maximum addressable unit. In 32-bit mode, max value is 0xfffff (4 GB)
;
; Access:
; ---------------------------------
; | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
; ---------------------------------
; | P | DPL | S | E | DC| RW| A |
; ---------------------------------
; P: Present bit
; Define whether to segment
; In this <Yes>, so it is 1
; DPL: Descriptor privilege level field
; Contains the CPU Privilege level of the segment
; 0 = highest privilege (kernel)
; 3 = lowest privilege (user applications)
; 定义 CPU 特权级别
; 0 = 最高权限 (内核)
; 3 = 最低权限 (用户APP)
; S: Descriptor type bit
; 0 = the descriptor defines a system segment (eg. a Task State Segment)
; 1 = it defines a code or data segment
; 0 = 系统段(例如:任务状态)
; 1 = 代码段或数据段
; E: Executable bit
; 0 = data segment, cannot execute
; 1 = code segment, can be executed
; 0 = 数据段,不可执行
; 1 = 代码段,可执行
; DC: Direction bit/Conforming bit
; * For Data segment
; 0 = The segment grows up
; 1 = The segment grows down
; 0 = 段从下到上
; 1 = 段从上到下
; * For Code segment
; 0 = Code in this segment can only be executed from an equal privilege level
; 1 = Code in this segment can be executed from an equal or lower privilege level
; 0 = 此段中的代码<只能>从相等的特权级别执行
; 1 = 此段中的代码可以从相等或更低的特权级别执行
; 简而言之,例如:如果为 1 ,则此段(假设为内核特权)中的代码可以被 用户特权级 的代码执行
; RW: Readable bit/Writable bit
; * For Data segment
; 0 = This segment is only readable, not writable
; 1 = This segment is readable and writable
; 0 = 此段只可读,不可写
; 1 = 此段可读也可写
; * For code segment
; 0 = This segment is neither readable nor writable
; 1 = This segment is readable (all code segment is not writable)
; 0 = 此段不可读也不可写
; 1 = 此段可读,不可写(代码段都不可写)
; A: Accessed bit
; Just keep it 0, CPU will set it when the segment is accessed
; 设为 0 即可,CPU在读取这个配置时会把它置 0
; Flags:
; -----------------
; | 3 | 2 | 1 | 0 |
; -----------------
; | G | DB| L | |
; -----------------
; G: Granularity flag
; 0 = The unit of the `Limit` is 1 bit (byte granularity)
; 1 = The unit of the `Limit` is 4 kB (page granularity)
; 0 = Limit 的单位是 1 bit
; 1 = Limit 的单位是 4 kB
; DB: Size flag
; 0 = This is a 16-bit protected mode segment
; 1 = This is a 32-bit protected mode segment
; L: Long-mode code flag
; 0 = Other mode
; 1 = This is a 64-bit protected mode segment
gdt_nulldesc:
dd 0, 0
gdt_codedesc:
dw 0xffff ; Limit (0~15)
dw 0x0000 ; Base (0~15)
db 0x00 ; Base (16~23)
db 1001_1010B ; Access Byte (0~7)
; 内核特权,
db 1100_1111B ; Flags(0~3) + Limit (16~19)
; 单位 1 bit, 32位的段
db 0x00 ; Base (24~31)
gdt_datadesc:
dw 0xffff
dw 0x0000
db 0x00
db 1001_0010B
db 1100_1111B
db 0x00
gdt_end:
gdt_descriptor:
gdt_size:
; The size of the table in bytes subtracted by 1
; 16 bit
dw gdt_end - gdt_nulldesc - 1
; The linear address of the GDT (not the physical address, paging applies)
; 32 bit (in 32-bit mode)
; 64 bit (in 64-bit mode)
dd gdt_nulldesc
codeseg euq gdt_codedesc - gdt_nulldesc
dataseg euq gdt_datadesc - gdt_nulldesc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment