2020-09-17 13:06:35 +03:00
|
|
|
%define DIV_ROUNDUP(a, b) (((a) + ((b) - 1)) / (b))
|
|
|
|
%define ALIGN_UP(x, a) (DIV_ROUNDUP((x), (a)) * (a))
|
|
|
|
|
|
|
|
; The GDT is copied to <start of EBDA> - gdt.size, which will also serve
|
|
|
|
; as the upper limit for balloc()
|
2020-09-06 04:35:32 +03:00
|
|
|
load_gdt:
|
|
|
|
pusha
|
|
|
|
push es
|
|
|
|
push ds
|
2020-09-17 13:06:35 +03:00
|
|
|
mov ax, word [0x40e] ; 0x40e contains the value of a segment pointing to the EBDA
|
|
|
|
sub ax, ALIGN_UP(gdt.size, 16) / 16
|
|
|
|
mov es, ax
|
|
|
|
mov word [gdt.ptr], ax
|
|
|
|
shl dword [gdt.ptr], 4
|
2020-09-06 04:35:32 +03:00
|
|
|
xor di, di
|
2020-09-17 13:06:35 +03:00
|
|
|
mov ds, di
|
|
|
|
mov si, gdt.start
|
|
|
|
mov cx, gdt.size
|
2020-09-06 04:35:32 +03:00
|
|
|
rep movsb
|
|
|
|
pop ds
|
2020-09-17 13:06:35 +03:00
|
|
|
lgdt [gdt]
|
2020-09-06 04:35:32 +03:00
|
|
|
pop es
|
|
|
|
popa
|
|
|
|
ret
|
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
gdt:
|
|
|
|
dw .size - 1 ; GDT size
|
|
|
|
.ptr:
|
|
|
|
dd 0 ; GDT start address (calculated at runtime)
|
2019-05-30 17:25:23 +03:00
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
.start:
|
|
|
|
; Null descriptor (required)
|
|
|
|
dw 0x0000 ; Limit
|
|
|
|
dw 0x0000 ; Base (low 16 bits)
|
|
|
|
db 0x00 ; Base (mid 8 bits)
|
|
|
|
db 00000000b ; Access
|
|
|
|
db 00000000b ; Granularity
|
|
|
|
db 0x00 ; Base (high 8 bits)
|
2019-05-30 17:25:23 +03:00
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
; 16-bit code
|
|
|
|
dw 0xffff ; Limit
|
|
|
|
dw 0x0000 ; Base (low 16 bits)
|
|
|
|
db 0x00 ; Base (mid 8 bits)
|
|
|
|
db 10011010b ; Access
|
|
|
|
db 00000000b ; Granularity
|
|
|
|
db 0x00 ; Base (high 8 bits)
|
2019-05-30 17:25:23 +03:00
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
; 16-bit data
|
|
|
|
dw 0xffff ; Limit
|
|
|
|
dw 0x0000 ; Base (low 16 bits)
|
|
|
|
db 0x00 ; Base (mid 8 bits)
|
|
|
|
db 10010010b ; Access
|
|
|
|
db 00000000b ; Granularity
|
|
|
|
db 0x00 ; Base (high 8 bits)
|
2019-05-30 17:25:23 +03:00
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
; 32-bit code
|
|
|
|
dw 0xffff ; Limit
|
|
|
|
dw 0x0000 ; Base (low 16 bits)
|
|
|
|
db 0x00 ; Base (mid 8 bits)
|
|
|
|
db 10011010b ; Access
|
|
|
|
db 11001111b ; Granularity
|
|
|
|
db 0x00 ; Base (high 8 bits)
|
2019-05-30 17:25:23 +03:00
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
; 32-bit data
|
|
|
|
dw 0xffff ; Limit
|
|
|
|
dw 0x0000 ; Base (low 16 bits)
|
|
|
|
db 0x00 ; Base (mid 8 bits)
|
|
|
|
db 10010010b ; Access
|
|
|
|
db 11001111b ; Granularity
|
|
|
|
db 0x00 ; Base (high 8 bits)
|
2019-05-30 17:25:23 +03:00
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
; 64-bit code
|
|
|
|
dw 0x0000 ; Limit
|
|
|
|
dw 0x0000 ; Base (low 16 bits)
|
|
|
|
db 0x00 ; Base (mid 8 bits)
|
|
|
|
db 10011010b ; Access
|
|
|
|
db 00100000b ; Granularity
|
|
|
|
db 0x00 ; Base (high 8 bits)
|
2019-05-30 17:25:23 +03:00
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
; 64-bit data
|
|
|
|
dw 0x0000 ; Limit
|
|
|
|
dw 0x0000 ; Base (low 16 bits)
|
|
|
|
db 0x00 ; Base (mid 8 bits)
|
|
|
|
db 10010010b ; Access
|
|
|
|
db 00000000b ; Granularity
|
|
|
|
db 0x00 ; Base (high 8 bits)
|
2020-03-25 06:38:23 +03:00
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
.end:
|
2020-03-25 06:38:23 +03:00
|
|
|
|
2020-09-17 13:06:35 +03:00
|
|
|
.size: equ .end - .start
|