aboutsummaryrefslogtreecommitdiff
path: root/src/arch/x86/boot/boot.S
blob: 02f6e9195603a71f28f5d27ef2b7916f83cffb56 (plain)
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
.section .bootstrap_stack, "aw", @nobits
stack_bottom:
.skip 16384
stack_top:

.section .bss, "aw", @nobits
	.align 4096
page_directory:
	.skip 4096
page_table1:
	.skip 4096
page_table2:
	.skip 4096


.set P,		1<<0
.set RW,	1<<1
.set FLAGS,	P | RW
.set KERNEL_VM,	0xC0000000


.section .multiboot.text, "a"
.global _start
.type _start, @function
_start:
_start:
	cli
	mov $stack_top - KERNEL_VM, %esp
	pushl %eax
	pushl %ebx

	movl $page_table1 - KERNEL_VM + FLAGS, page_directory - KERNEL_VM + 0 * 4
	movl $page_table2 - KERNEL_VM + FLAGS, page_directory - KERNEL_VM + 1 * 4
	movl $page_table1 - KERNEL_VM + FLAGS, page_directory - KERNEL_VM + 768 * 4
	movl $page_table2 - KERNEL_VM + FLAGS, page_directory - KERNEL_VM + 769 * 4

	mov $0, %ecx
not_done:
	mov $0x1000, %eax
	mul %ecx
	orl $FLAGS, %eax
	mov %eax, %edi

	// page_table1[%ecx] = 0x1000 * %ecx | 0x3
	mov $4, %eax
	mul %ecx
	add $page_table1 - KERNEL_VM, %eax
	movl %edi, (%eax)

	// page_table2[%ecx] = 0x1000 * %ecx | 0x3
	mov $4, %eax
	mul %ecx
	add $1024 * 0x1000, %edi
	add $page_table2 - KERNEL_VM, %eax
	movl %edi, (%eax)

	inc %ecx
	cmp $1024, %ecx
	je done
	jmp not_done
done:

	movl $(page_directory - KERNEL_VM), %ecx
	movl %ecx, %cr3

	movl %cr0, %ecx
	orl $0x80001000, %ecx
	movl %ecx, %cr0

	lea 7f, %ecx
	jmp *%ecx


.section .boot32.rodata

gdt:
gdt_null = . - gdt
	.quad 0
kernel_code = . - gdt
	.long 0xFFFF
	.byte 0
	.byte 0x9A
	.byte 0xCF
	.byte 0
kernel_data = . - gdt
	.long 0xFFFF
	.byte 0
	.byte 0x92
	.byte 0xCF
	.byte 0
user_code = . - gdt
	.long 0xFFFF
	.byte 0
	.byte 0xFA
	.byte 0xCF
	.byte 0
user_data = . - gdt
	.long 0xFFFF
	.byte 0
	.byte 0xF2
	.byte 0xCF
	.byte 0

.global gdtp
gdtp:
	.word . - gdt - 1
	.long gdt


.section .text

7:
	addl $KERNEL_VM, %esp
	lgdtl gdtp
	ljmp $0x08, $code

code:
	movw $0x10, %ax
	movw %ax, %ds
	movw %ax, %es
	movw %ax, %fs
	movw %ax, %gs
	movw %ax, %ss

	call kernel_main
	hlt