-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.asm
171 lines (152 loc) · 2.18 KB
/
lib.asm
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
PAGE_DIR_TBL_ADDR equ 0x100000
extern ready_task
extern itss
extern disp_pos
[BITS 32]
; void apply_paging
global apply_paging
apply_paging:
push eax
mov eax, PAGE_DIR_TBL_ADDR
mov cr3, eax
mov eax, cr0
or eax, 0x80000000
mov cr0, eax
pop eax
ret
; void out_byte(u16 port, u8 value);
global out_byte
out_byte:
mov edx, [esp + 4] ; port
mov al, [esp + 4 + 4] ; value
out dx, al
nop
nop
ret
; u8 in_byte(u16 port);
global in_byte
in_byte:
mov edx, [esp + 4] ; port
xor eax, eax
in al, dx
nop
nop
ret
; void load_idtr(struct dtr idtr);
global load_idtr
load_idtr:
lidt [esp + 4]
nop
nop
ret
; void load_gdtr(struct dtr gdtr);
global load_gdtr
load_gdtr:
lgdt [esp + 4]
nop
nop
ret
; void save_gdtr();
extern gdtr
global save_gdtr
save_gdtr:
sgdt [gdtr]
nop
nop
ret
; void load_ldtr(struct dtr ldtr);
global load_ldtr
load_ldtr:
lldt [esp + 4]
nop
nop
ret
; void save_ldtr();
extern ldtr
global save_ldtr
save_ldtr:
sldt [ldtr]
nop
nop
ret
; void set_interupt()
global set_interupt
set_interupt:
sti
ret
; void clear_interupt()
global clear_interupt
clear_interupt:
cli
ret
; void load_tss()
global load_tss
load_tss:
xor eax, eax
mov ax, 0x28 ; TSS Selector
ltr ax
ret
; void start_task()
global start_task
start_task:
mov esp, [ready_task]
lldt [esp + (18*4)] ; ldt_sel offset in struct tcb
lea eax, [esp + (18*4)] ; stack offset in struct tcb
mov dword [itss + 4], eax ; esp0 offset in tss, set it to be
; the bottom of regs in struct tcp
; prepare for next 'real' interupt
pop gs
pop fs
pop es
pop ds
popad
add esp, 4 ;skip retaddr
iretd
; void inc_char2()
global inc_char2
inc_char2:
inc byte [gs:2]
ret
; void inc_char4()
global inc_char4
inc_char4:
inc byte [gs:4]
ret
; void inc_char6()
global inc_char6
inc_char6:
inc byte [gs:6]
ret
; void disp_str(char *string)
global disp_str
disp_str:
push ebp
mov ebp, esp
mov esi, [ebp + 8]
mov edi, [disp_pos]
mov ah, 0Fh
.1:
lodsb
test al, al
jz .2
cmp al, 0Ah ; `enter`?
jnz .3
push eax
mov eax, edi
mov bl, 160
div bl
and eax, 0FFh
inc eax
mov bl, 160
mul bl
mov edi, eax
pop eax
jmp .1
.3:
mov [gs:edi], ax
add edi, 2
jmp .1
.2:
mov [disp_pos], edi
pop ebp
ret