-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernel_functions.nas
119 lines (95 loc) · 1.64 KB
/
kernel_functions.nas
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
; For C calling conventions, refer to: https://www.csee.umbc.edu/~chang/cs313.s02/stack.shtml
GLOBAL _io_hlt
GLOBAL _io_cli
GLOBAL _io_out8
GLOBAL _io_out16
GLOBAL _io_out32
GLOBAL _io_in8
GLOBAL _io_in16
GLOBAL _io_in32
GLOBAL _get_eip
GLOBAL _enable_interrupt_and_halt
GLOBAL _set_tr
GLOBAL _switch_task
GLOBAL _panic
GLOBAL _putchar
GLOBAL _atom_inc
GLOBAL _load_page_table
GLOBAL _jump_usermode
section .text
_io_out8:
MOV edx, [esp+4]
MOV eax, [esp+8]
OUT dx, al
RET
_io_out16:
MOV edx, [esp+4]
MOV eax, [esp+8]
OUT dx, ax
RET
_io_out32:
MOV edx, [esp+4]
MOV eax, [esp+8]
OUT dx, eax
RET
_io_in8:
MOV edx, [esp+4]
MOV eax, 0x0
IN al, dx
RET
_io_in16:
MOV edx, [esp+4]
MOV eax, 0x0
IN ax, dx
RET
_io_in32:
MOV edx, [esp+4]
MOV eax, 0x0
IN eax, dx
RET
_get_eip:
MOV eax, [esp]
RET
_set_tr:
LTR [esp + 4]
RET
_switch_task:
JMP FAR [esp + 4]
RET
_panic:
.panic_loop:
HLT
JMP .panic_loop
_putchar:
MOV eax, [esp + 4]
MOV ecx, 0xB8000
MOV BYTE[ecx], al
INC ecx
MOV BYTE[ecx], 0x0B
RET
_load_page_table:
MOV eax, [esp + 4]
MOV cr3, eax
RET
_jump_usermode:
PUSH ebp
MOV ebp, esp
; Refer to https://wiki.osdev.org/Getting_to_Ring_3
; Set data segement register to point to the LDT data segment descriptor
XOR eax, eax
MOV ax, (1 << 3) | 0x7
MOV ds, ax
MOV es, ax
MOV fs, ax
MOV gs, ax
; Set up the stack frame iret expects
MOV eax, DWORD ss:[ebp + 0x0c]
push (1 << 3) | 0x7
push eax
pushf
pop eax
or eax, 0x200
push eax
push (0 << 3) | 0x7
push DWORD ss:[ebp + 0x08]
iret