-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenos-x86_64.asm
More file actions
365 lines (277 loc) · 12.4 KB
/
benos-x86_64.asm
File metadata and controls
365 lines (277 loc) · 12.4 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
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
; ==================================================================
; benOS Bootloader
; Copyright (C) 2018 Bench Computer, Inc. -- see ~/LICENSE
;
; The official bootloader for benOS and BenchX desktop/laptop products.
; The first bootloader built for a decentralized operating system.
; Many bootloaders were looked at and utilized in the creation of
; benOS's BenchX bootloader.
;
; Bootloaders we utilized: Ubuntu, MikeOS, Debian, Linux Mint, ReOS
; ==================================================================
; =================================================================================
; benOS Bootloader - BenBoot Stage
; =================================================================================
; ------------------------------------------------------------------
; benOS BenBoot Stage - Step By Step
; 1. Check Readiness of benOS Bootloader
; 2. Get CPU ID
; 3. Initialize Table
; 4. Start benOS Stack
; 5. Exit benOS Stack
; 6. Exit Code
; ------------------------------------------------------------------
benboot:
.ready: dq 0
.cpu_id: dq 0
.page_table: dq 0
.stack_start: dq 0
.stack_end: dq 0
.code: dq 0
times 512 - ($ - benboot) db 0
; =================================================================================
; benOS Bootloader - 'startup_ap' - Initialize benOS Stack
; =================================================================================
startup_ap:
cli
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
; ==================================================================
; benOS Bootloader - Initialize benOS Stack, FPU and SSE
; ==================================================================
mov sp, 0x7C00
call initialize.fpu
call initialize.sse
; ==================================================================
; benOS Bootloader - CR3 Point to PML4
; ==================================================================
mov edi, 0x70000
mov cr3, edi
; =============================================================================
; benOS Bootloader - Enable FXSAVE/FXRSTOR/PG_GLOBAL/PG_ADDR_EXT/PG_SIZE_EXT
; =============================================================================
mov eax, cr4
or eax, 1 << 9 | 1 << 7 | 1 << 5 | 1 << 4
mov cr4, eax
; ======================================================================
; benOS Bootloader - Load Global Descriptor Tables In Protected Mode
; ======================================================================
lgdt [gdtr]
mov ecx, 0xC0000080 ; Read via the Extended Feature Enable Register Model Specific Registers.
rdmsr
or eax, 1 << 11 | 1 << 8 ; Enable Long-Mode-Enable and NXE bit for the USE64 Variable later.
wrmsr
; ======================================================================
; benOS Bootloader - Enable Simulataneous Paging and Protection
; ======================================================================
mov ebx, cr0
or ebx, 1 << 31 | 1 << 16 | 1 ;Bit 31: benOS Paging, Bit 16: Write-Protected benOS Microkernel, Bit 0: Protected Mode
mov cr0, ebx
; ================================================================================================
; benOS Bootloader - Enable Long Mode By Far Jumping & Load Microkernel Code Syntax Using 64 Bit
; ================================================================================================
jmp gdt.kernel_code:long_mode_ap
; ======================================================================
; benOS Bootloader - Import benOS Common Loaders To x86_64 Boot Process
; ======================================================================
%include "ben_common.asm"
; ======================================================================
; benOS Bootloader - Initialize benOS Arch
; ======================================================================
startup_benos_arch:
cli
; =============================================================================
; benOS Bootloader - Setup Page Tables and Identity Mapping on First Gigabyte
; ============================================================================
mov ax, 0x7000
mov es, ax
xor edi, edi
xor eax, eax
mov ecx, 6 * 4096 / 4
cld
rep stosd
xor edi, edi
; =============================================================================
; benOS Bootloader - Connect 1st PML4 and 2nd to Last PML4 to PDP
; ============================================================================
mov DWORD [es:edi], 0x71000 | 1 << 1 | 1
mov DWORD [es:edi + 510*8], 0x71000 | 1 << 1 | 1
add edi, 0x1000
; =============================================================================
; benOS Bootloader - Connect Last PML4 to PDP
; ============================================================================
mov DWORD [es:edi - 8], 0x70000 | 1 << 1 | 1
; =============================================================================
; benOS Bootloader - Connect All Four PDPs to PD
; ============================================================================
mov DWORD [es:edi], 0x72000 | 1 << 1 | 1
mov DWORD [es:edi + 8], 0x73000 | 1 << 1 | 1
mov DWORD [es:edi + 16], 0x74000 | 1 << 1 | 1
mov DWORD [es:edi + 24], 0x75000 | 1 << 1 | 1
add edi, 0x1000
; =============================================================================
; benOS Bootloader - Connect All PD's (Max 512 per PDP and 2 megabytes each)
; ============================================================================
mov ebx, 1 << 7 | 1 << 1 | 1
mov ecx, 4*512
; ======================================================================
; benOS Bootloader - Process Data Stage
; ======================================================================
.setpd:
mov [es:edi], ebx
add ebx, 0x200000
add edi, 8
loop .setpd
xor ax, ax
mov es, ax
; ======================================================================
; benOS Bootloader - CR3 Pointer To PML4
; ======================================================================
mov edi, 0x70000
mov cr3, edi
; =================================================================================
; benOS Bootloader - Enable FXSAVE/FXRSTOR, PG Global, PG Address Ext, PG Size Ext
; =================================================================================
mov eax, cr4
or eax, 1 << 9 | 1 << 7 | 1 << 5 | 1 << 4
mov cr4, eax
; =================================================================================
; benOS Bootloader - Load Protected Mode For Global Descriptor Tables
; =================================================================================
lgdt [gdtr]
mov ecx, 0xC0000080 ; Read via the Extended Feature Enable Register Model Specific Registers.
rdmsr
or eax, 1 << 11 | 1 << 8 ; Enable Long-Mode-Enable and NXE bit for the USE64 Variable later.
wrmsr
; =======================================================================================
; benOS Bootloader - Enable Simultaneous Paging / Protection To Use With 64 Bit Hardware
; =======================================================================================
mov ebx, cr0
or ebx, 1 << 31 | 1 << 16 | 1 ;Bit 31: benOS Paging, Bit 16: Write-Protect benOS Microkernel, Bit 0: Protected Mode
mov cr0, ebx
; ====================================================================================================
; benOS Bootloader - Far Jump-Based Enablement Of benOS Long Mode While Loading Code Syntax + 64 Bit
; ====================================================================================================
jmp gdt.kernel_code:long_mode
; ======================================================================
; benOS Bootloader - Load USE64 Global Bootloader Function
; ======================================================================
USE64
; =================================================================================
; benOS Bootloader - Long Mode Loader + Other 64 Bit Data Segments
; =================================================================================
long_mode:
mov rax, gdt.kernel_data
mov ds, rax
mov es, rax
mov fs, rax
mov gs, rax
mov ss, rax
; =================================================================================
; benOS Bootloader - benOS Stack Base
; =================================================================================
mov rsi, 0xFFFFFF0000080000
mov [args.stack_base], rsi
; =================================================================================
; benOS Bootloader - benOS Stack Size
; =================================================================================
mov rcx, 0x1F000
mov [args.stack_size], rcx
; =================================================================================
; benOS Bootloader - benOS Stack Pointer
; =================================================================================
mov rsp, rsi
add rsp, rcx
; =================================================================================
; benOS Bootloader - Copy Bootloader Environment Variables To benOS Stack
; =================================================================================
%ifdef KERNEL
mov rsi, 0
mov rcx, 0
%else
mov rsi, benfs.env
mov rcx, benfs.env.end - benfs.env
%endif
mov [args.env_size], rcx
; =================================================================================
; benOS Bootloader - Copy Environment Variables Function
; =================================================================================
.copy_env:
cmp rcx, 0
je .no_env
dec rcx
mov al, [rsi + rcx]
dec rsp
mov [rsp], al
jmp .copy_env
; =================================================================================
; benOS Bootloader - Function When Environment Variables Are Non-Existent
; =================================================================================
.no_env:
mov [args.env_base], rsp
; =================================================================================
; benOS Bootloader - benOS Stack Alignment
; =================================================================================
and rsp, 0xFFFFFFFFFFFFFFF0
; =================================================================================
; benOS Bootloader - Set Arguments From Environment Variables and Microkernel
; =================================================================================
mov rdi, args
; =================================================================================
; benOS Bootloader - benOS Microkernel Entry Point
; =================================================================================
mov rax, [args.kernel_base]
call [rax + 0x18]
; =================================================================================
; benOS Bootloader - Halt Stage
; =================================================================================
.halt:
cli
hlt
jmp .halt
; =================================================================================
; benOS Bootloader - Long Mode AP
; =================================================================================
long_mode_ap:
mov rax, gdt.kernel_data
mov ds, rax
mov es, rax
mov fs, rax
mov gs, rax
mov ss, rax
mov rcx, [benboot.stack_end]
lea rsp, [rcx - 256]
mov rdi, benboot.cpu_id
mov rax, [benboot.code]
mov qword [benboot.ready], 1
jmp rax
; =================================================================================
; benOS Bootloader - Finalization
; =================================================================================
gdtr:
dw gdt.end + 1 ; size
dq gdt ; offset
gdt:
.null equ $ - gdt
dq 0
.kernel_code equ $ - gdt
istruc GDTEntry
at GDTEntry.limitl, dw 0
at GDTEntry.basel, dw 0
at GDTEntry.basem, db 0
at GDTEntry.attribute, db attrib.present | attrib.user | attrib.code
at GDTEntry.flags__limith, db flags.long_mode
at GDTEntry.baseh, db 0
iend
.kernel_data equ $ - gdt
istruc GDTEntry
at GDTEntry.limitl, dw 0
at GDTEntry.basel, dw 0
at GDTEntry.basem, db 0
at GDTEntry.attribute, db attrib.present | attrib.user | attrib.writable
at GDTEntry.flags__limith, db 0
at GDTEntry.baseh, db 0
iend
.end equ $ - gdt