-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot2.s
365 lines (338 loc) · 5.54 KB
/
boot2.s
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
.include "apicreg.def"
.include "regs.def"
.include "misc.def"
.include "mem.inc"
.global start
.intel_syntax noprefix
.code64
.text
.code64
start:
.long 0x55aa66bb
.long boot2_end - start
.quad offset ap_entry #offset ap_entry
.quad boot2_end
start.1:
mov rdi,kernel_virt_data_base
mov ecx,(8*1024*1024)/8
xor rax,rax
rep stosq
mov rdi,kernel_data_begin
mov r8,rdi
movabs rsi,offset gdt
mov ecx,temp_data0 - gdt
rep movsb
lea rsp,[r8+kernel_stk0]
lea rsi,[r8+kernel_gdtr]
mov [r8+0x48+2],r8 #gdt table address
lgdt [rsi]
push SEL_SCODE
movabs rax,offset start64
push rax
retfq
start64:
xor eax,eax
mov ds,ax
mov es,ax
mov ss,ax
mov fs,ax
mov gs,ax
movabs rdi,kernel_idt
mov edx,0x188e
movabs rax,offset intx00
mov r8,rax
mov ebx,0x1fffff
mov ecx,20
s.2:
shr ebx,1
jnc s.3
mov [rdi],ax
shr rax,16
mov [rdi+int_gate_offset_31_16],ax
shr rax,16
mov [rdi+int_gate_offset_63_32],eax
mov [rdi+int_gate_seg_sel],dh
mov [rdi+int_gate_p_dpl_type],dl
add r8,4
mov rax,r8
s.3:
lea rdi,[rdi+16]
loop s.2
movabs rsi,offset idtr
lidt [rsi]
push rdx
# mov dh,SEL_UCODE
mov dl,0xee
set_gate 0x40,int0x40
pop rdx
.if 0
set_gate 0x20,timer
set_gate 0x21,keyb
set_gate 0xf0,apic_timer
set_gate 0xf1,apic_err
set_gate 0xf2,apic_ici
set_gate 0xff,apic_svr
.endif
#
#set TSS
#
mov rsi,kernel_data_begin+kernel_gdt+SEL_TSS
mov rdi,kernel_data_begin+kernel_tss
mov rax,rdi
mov [rsi+tss_desc_base_15_0],ax
shr rax,16
mov [rsi+tss_desc_base_23_16],al
mov [rsi+tss_desc_base_31_24],ah
shr rax,16
mov [rsi+tss_desc_base_63_32],eax
mov [rdi+tss_rsp0],rsp
mov al,-1
mov [rdi+tss_iomap_base],al
mov ax,SEL_TSS
ltr ax
mov rax,kernel_data_begin
shld rdx,rax,32
mov ecx,IA32_FSBASE
wrmsr
call set_gs
call set_syscall
.if 1
#
#set int 0x15 vector
#
movabs rsi,offset user_task
mov edi,0x9e000
cld
mov ecx,1024
rep movsb
mov r8,0x15*4 #int 0x15 vector address
mov ebx,[r8] #orgi
mov [0x9e002],ebx
mov ax,0x9e00
mov [r8+2],ax
xor eax,eax
mov [r8],ax
mov eax,ebx
mov edi,0xb8000+160*8 + 64
call hex64
# jmp $
#
#复制VMX 和 测试代码到指定位置
#
mov edi,0x600
push rdi
xor eax,eax
cld
mov ecx,4096/8
rep stosq
pop rdi
movabs rsi,offset guest_bin
mov ecx,[rsi+8]
rep movsb
mov rdi,vmx_begin
push rdi
xor eax,eax
mov ecx,(32*1024*1024)/8
rep stosq
pop rdi
movabs rsi,offset vmx_i
mov ecx,[rsi+4]
rep movsb
movabs eax,[vmx_i+8]
mov rbp,rsp
call rax
vmlaunch
ud2
.endif
wait.0:
rep nop
jmp wait.0
jmp_ring3:
push SEL_UDATA
mov rax,kernel_data_begin + kernel_stk3
push rax
push 0x002
push SEL_UCODE
movabs rax,offset ring3
push rax
iretq
ring3:
nop
jmp $
set_gate1:
mov [rdi],ax
shr rax,16
mov [rdi+int_gate_offset_31_16],ax
shr rax,16
mov [rdi+int_gate_offset_63_32],eax
mov [rdi+int_gate_seg_sel],dh
mov [rdi+int_gate_p_dpl_type],dl
ret
#
#APIC ID 作为索引 计算AP CPU的核数据地址
#
ap_entry:
call get_lapic_id
mov rsi,kernel_data_begin
mov edi,kernel_data_size
imul edi,eax
add rdi,rsi
mov r15,rdi
#
#FS 基地址设置成CPU 数据起始地址
#
mov rax,r15
shld rdx,rax,32
mov ecx,IA32_FSBASE
wrmsr
mov ecx,kernel_tss - kernel_gdt
rep movsb #复制GDT 和 GDTR
lea rax,[r15 + kernel_gdt]
mov [r15 +kernel_gdtr +2 ],rax
lea rsp,[r15 +kernel_stk0]
#
#设置TSS描述符 和TSS段
#
lea rdx,[r15 + kernel_gdt + SEL_TSS]
lea r14,[r15+kernel_tss]
mov rax,r14
mov byte ptr [rdx+tss_desc_limit_15_0],0x67
mov byte ptr [rdx+tss_desc_p_dpl_type],0x89
mov [rdx+tss_desc_base_15_0],ax
shr rax,16
mov [rdx+tss_desc_base_23_16],al
mov [rdx+tss_desc_base_31_24],ah
shr rax,16
mov [rdx+tss_desc_base_63_32],eax
mov [r14+tss_rsp0],rsp
mov word ptr [r14+tss_iomap_base],0xffff
lea rsi,[r15+kernel_gdtr]
lgdt [rsi]
push SEL_SCODE
movabs rax,offset flush_ap
push rax
retfq
flush_ap:
movabs rsi,offset idtr
lidt [rsi]
mov ax,SEL_TSS
ltr ax
movabs rsi,offset idtr
lidt [rsi]
call enable_apic
call set_gs
call set_syscall
movabs rsi,offset ap_cpu
inc byte ptr [rsi]
sti
1:
pause
jmp 1b
set_gs:
call get_lapic_id
mov rbx,kernel_data_begin + kernel_pcb
mov edx,kernel_data_size
imul rax,rdx
add rax,rbx
shld rdx,rax,32
mov ecx,IA32_KERNEL_GSBASE
wrmsr
xor eax,eax
xor edx,edx
mov ecx,IA32_GSBASE
wrmsr
ret
set_syscall:
mov ecx,IA32_STAR
xor eax,eax
mov edx,0x00230018
wrmsr
mov eax,0x202
mov ecx,IA32_FMASK
wrmsr
movabs rax,offset sys_call
shld rdx,rax,32
mov ecx,IA32_LSTAR
wrmsr
ret
.global hex64,hex32
hex64:
push rax
shr rax,32
call hex32
pop rax
hex32:
push rax
shr rax,16
call hex16
pop rax
hex16:
push rax
shr ax,8
call hex8
pop rax
hex8:
push rax
shr al,4
call hex8.1
pop rax
hex8.1:
and al,0xf
add al,0x30
cmp al,0x39
jbe hex8.2
add al,0x7
hex8.2:
mov ah,2
stosw
ret
idtr:
.word 256*16
.quad kernel_idt
cpu_count:
.byte 0x0
.global t_lock
t_lock:
.byte 0x0
t_lock1:
.byte 0x1
.p2align 4
gdt:
.quad 0x0 #0
.quad 0x00cf9a000000ffff #0x8
.quad 0x00cf92000000ffff #0x10
.quad 0x00209a0000000000 #0x18
.quad 0x0000920000000000 #0x20
.quad 0x0000f20000000000 #0x28
.quad 0x0020fa0000000000 #0x30
#
#TSS
#
.word 0x67 #limit 0 -15
.word 0x0 #base address
.byte 0x0 #base address 16 -23
.byte 0x89 #p_dpl_type
.byte 0x00 #g_avl_limit-16-19
.byte 0x0 #byte 24 - 31
.long 0x0 #base 32 - 63
.long 0x0 #RSV
gdtr: .word gdtr - gdt
.quad gdt
temp_data0:
.word 0x0
.quad 0x0
ap_cpu:
.byte 0x0
.global cpu_flag
cpu_flag:
.quad 0x0
.global msg1
msg1: .asciz "dadsadsdadsaads\n"
.global bound_test
bound_test:
.quad 0x1
.quad 0x0
int0x15:
hlt
int15_vector: #real mode
.long 0x0