-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_environment.asm
More file actions
87 lines (75 loc) · 2.12 KB
/
_environment.asm
File metadata and controls
87 lines (75 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
;this code deals with several things
; 1 - setting up an execution stack for the kernel under the .bss section
; 2 - setting up a fixed, read only global descriptor table (GDT)
; 3 - calling kernel_main()
section .text
global environment
environment:
cli ;disable interrupts, just in case (the system should start with the interrupt flag cleared)
stack_setup:
mov esp, stack_bottom
gdt_setup:
lgdt [gdtr] ;load the gdt register
mov ax, 0x08 ;load the first gdt selector for our code segment
jmp 0x08:gdt_reload ;we're now using the code segment selector to jump
gdt_reload:
mov ax, 0x10 ;load the second gdt selector for the data segments and stack segment
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
idt_setup:
lidt [idtr]
finish: ;we're done setting up the environment, we can jump to kernel main
extern kmain
jmp kmain ;jmp to kernel_main, a function defined elsewhere (typically C code) to indicate the beginning of our kernel
section .rodata
gdtr: ;the data to load into the gdt register
gdt_limit dw 8*5 - 1 ;five entries
gdt_base dd gd_table ;pointer to the gd_table in rodata
align 8
gd_table:
.gdt_e_null: ;null decriptor with data about the location and size of the GDT (this data isn't really important)
dd gdt_limit
dd gdt_base
.gdt_e_code_0: ;ring 0 code segment
dw 0xffff
dw 0
db 0
db 0x9a
db 11001111b
db 0
.gdt_e_data_0: ;ring 0 data segment
dw 0xffff
dw 0
db 0
db 0x92
db 11001111b
db 0
.gdt_e_code_3: ;ring 3 code segment
dw 0xffff
dw 0
db 0
db 0xfa
db 11001111b
db 0
.gdt_e_data_3: ;ring 3 data segment
dw 0xffff
dw 0
db 0
db 0xf2
db 11001111b
db 0
section .data
idtr: ;the data to load into the gdt register
idt_limit dw 8*256 - 1 ;256 entries
idt_base dd id_table ;pointer to the idt in data
global _idt ;we want to export this symbol in order to later populate it elsewhere
align 8
id_table:
_idt times 256 dq 0 ;an interrupt table with empty 256 entries
section .bss
stack_top:
resb 16384 ;reserve 16 KiB
stack_bottom: ;stacks grow downwards in memory