Repository navigation
Expand file tree
/
Copy pathboot.S
More file actions
265 lines (229 loc) · 4.16 KB
/
Copy pathboot.S
File metadata and controls
265 lines (229 loc) · 4.16 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
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
.set MAGIC,0x1BADB002
.set FLAGS,0x00000003
.set CHECKSUM, -(MAGIC + FLAGS)
.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM
# ── multiboot2 header for QEMU 9+ ──
.section .multiboot2, "ae"
.align 8
mb2_start:
.long 0xE85250D6
.long 0 # arch: i386
.long mb2_end - mb2_start
.long -(0xE85250D6 + 0 + (mb2_end - mb2_start))
.word 0 # end tag type
.word 0 # flags
.long 8 # size
mb2_end:
# ── PVH ELF note (QEMU 11 -kernel 唯一认的协议) ──
.section .note.Xen, "a"
.align 4
.long 4 # n_namesz = "Xen\0"
.long 4 # n_descsz = 1 dword
.long 18 # n_type = XEN_ELFNOTE_PHYS32_ENTRY
.asciz "Xen"
.long _start # entry point
.section .bss
.align 16
stack_bottom:
.skip 16384
stack_top:
.section .text
.global _start
_start:
mov $stack_top, %esp
call kernel_main
cli
1: hlt
jmp 1b
#=====gdt flush=====
.global gdt_flush
gdt_flush:
movl 4(%esp), %eax
lgdt (%eax)
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
ljmp $0x08, $flush
flush:
ret
#=====idt_flush=====
.global idt_flush
idt_flush:
movl 4(%esp), %eax
lidt (%eax)
ret
#=====IRQ STUBS=====
.macro IRQ num, handler
.global \handler
\handler:
cli
pushl $0
pushl $\num
jmp irq_common_stub
.endm
IRQ 0, irq0
IRQ 1, irq1
IRQ 2, irq2
IRQ 3, irq3
IRQ 4, irq4
IRQ 5, irq5
IRQ 6, irq6
IRQ 7, irq7
IRQ 8, irq8
IRQ 9, irq9
IRQ 10, irq10
IRQ 11, irq11
IRQ 12, irq12
IRQ 13, irq13
IRQ 14, irq14
IRQ 15, irq15
irq_common_stub:
pusha
pushl %ds
pushl %es
pushl %fs
pushl %gs
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movl %esp, %eax
pushl %eax
call irq_handler
popl %eax
popl %gs
popl %fs
popl %es
popl %ds
popa
addl $8, %esp
iret
#===== #DF special handler (must not touch stack) =====
.global exc8
exc8:
cli
movl $0xB8000, %edi
movl $0x0F200F44, (%edi) # "D "
movl $0x0F210F46, 4(%edi) # "F!"
movl $0x0F210F21, 8(%edi) # "!!"
movl $0x0F200F20, 12(%edi) # " "
1: hlt
jmp 1b
#=====EXCEPTION STUBS=====
.macro EXC num, has_err
.global exc\num
exc\num:
cli
.if !\has_err
pushl $0 # 无错误码的异常推 dummy
.endif
pushl $\num # 异常号
jmp exc_common_stub
.endm
EXC 0, 0
EXC 1, 0
EXC 2, 0
EXC 3, 0
EXC 4, 0
EXC 5, 0
EXC 6, 0
EXC 7, 0
# exc8 (#DF) has its own handler below — must not touch stack
EXC 9, 0
EXC 10, 1
EXC 11, 1
EXC 12, 1
EXC 13, 1
EXC 14, 1
EXC 15, 0
EXC 16, 0
EXC 17, 1
EXC 18, 0
EXC 19, 0
EXC 20, 0
EXC 21, 1
EXC 22, 0
EXC 23, 0
EXC 24, 0
EXC 25, 0
EXC 26, 0
EXC 27, 0
EXC 28, 0
EXC 29, 1
EXC 30, 1
EXC 31, 0
exc_common_stub:
pusha
pushl %ds
pushl %es
pushl %fs
pushl %gs
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movl %esp, %eax
pushl %eax
call exception_handler
popl %eax
popl %gs
popl %fs
popl %es
popl %ds
popa
addl $8, %esp
iret
#=====SYSCALL GATE=====
.global isr128
isr128:
cli
pushl $0
pushl $0x80
pusha
pushl %ds
pushl %es
pushl %fs
pushl %gs
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movl %esp, %eax
pushl %eax
call syscall_handler
popl %eax
popl %gs
popl %fs
popl %es
popl %ds
popa
addl $8, %esp
iret
#===== proc_start: pure asm ring0→ring3 =====
# void proc_start(uint32_t entry, uint32_t stack_top)
# C调用: proc_start(entry, stack_top)
# 参数: [ebp+8]=entry, [ebp+12]=stack_top
# 无返回——iret 跳 ring3
.global proc_start
proc_start:
pushl %ebp
movl %esp, %ebp
movl 0x8(%ebp), %eax
movl 0xc(%ebp), %ebx
pushl $0x23 # SS
pushl %ebx # ESP
pushfl # EFLAGS
pushl $0x1B # CS
pushl %eax # EIP
iret
# never returns