-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot.S
140 lines (110 loc) · 2.13 KB
/
boot.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
#include "mm.h"
.global _start
.global _hang
.global kern_pdir
.global get_eip
.global kinit_usermode
.global kimg_end
.global vdso_page
.global kstack
.global do_syscall
#define MB_ALIGN (1<<0)
#define MB_INFO (1<<1)
#define MB_FLAGS (MB_ALIGN | MB_INFO)
#define MB_MAGIC (0x1BADB002)
#define MB_CHKSUM (-(MB_MAGIC + MB_FLAGS))
.text
/* multiboot header */
.align 4
multiboot:
.long MB_MAGIC
.long MB_FLAGS
.long MB_CHKSUM
_start:
/* save the multiboot magic number, we're gonna trash %eax */
mov %eax,%edx
/* initialize two tables of identity mappings, which will
* be unmapped when we set up paging for real */
mov $pg0 - KERNEL_OFFSET, %edi
mov $0x07, %eax
1:
stosl
add $0x1000, %eax
cmp $pg_stop - KERNEL_OFFSET, %edi
jne 1b
/* turn on paging */
mov $init_pdir - KERNEL_OFFSET, %eax
mov %eax, %cr3
mov %cr0, %eax
or $0x80000000, %eax
mov %eax, %cr0
/* jump to real startup */
lea 2f,%eax
jmp *%eax
2:
/* initialize the stack pointer */
mov $kstack,%esp
mov $kstack,%ebp
/* push multiboot magic number and header*/
push %edx
push %ebx
/* disable interrupts, start the kernel, and derp if it finishes */
cli
call kmain
_hang:
cli
hlt
do_syscall:
mov 4(%esp),%eax
/* provide address of the arguments */
lea 8(%esp),%ebx
int $0x80
ret
kinit_usermode:
cli
/* get and restore the next address to be executed */
pop %ecx
push %ecx
mov $0x23, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %esp, %eax
push $0x23
push %eax
pushf
pop %eax
or $0x200, %eax
push %eax
push $0x1b
push %ecx
iret
kimg_end:
.long _kimg_end
/* (virtual) pointer to the kernel page directory, used by almost everything */
kern_pdir:
.long init_pdir
.data
/* We need to set up a couple tables */
init_pdir:
.long pg0 - KERNEL_OFFSET + 0x07
.long pg1 - KERNEL_OFFSET + 0x07
.fill 766,4,0
.long pg0 - KERNEL_OFFSET + 0x07
.long pg1 - KERNEL_OFFSET + 0x07
.fill 254,4,0
pg0:
.fill 1024,4,0
pg1:
.fill 1024,4,0
pg_stop:
.fill 1024,4,0
/* claim one page for the vdso */
vdso_page:
.fill 1024,4,0
.bss
/* we also need a stack, multiboot won't give us one =( */
.align 32
kstack:
.fill 1024,4,0