summaryrefslogtreecommitdiff
path: root/arch/i386/boot/boot.s
blob: 535791026145099fbbf3fade4d57a50490f2625c (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
.set ALIGN,     1<<0
.set MEMINFO,   1<<1
.set FLAGS,     ALIGN | MEMINFO
.set MAGIC,     0x1BADB002
.set CHECKSUM,  -(MAGIC + FLAGS)

.section .multiboot.data, "aw"
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM

.section .bootstrap_stack, "aw", @nobits
stack_bottom:
.skip 16384
stack_top:

.section .bss, "aw", @nobits
        .align 4096
boot_page_directory:
        .skip 4096
boot_page_table0:
        .skip 4096
boot_page_table1:
        .skip 4096

.section .multiboot.text, "a"
.global _start
.type _start, @function
_start:
        movl $(boot_page_table0 - 0xC0000000), %edi
        movl $0, %esi
        movl $1023, %ecx

1:      cmpl $_kernel_start, %esi
        jl 2f
        cmpl $(_kernel_end - 0xC0000000), %esi
        jge 3f

        movl %esi, %edx
        orl $0x003, %edx
        movl %edx, (%edi)

2:      addl $4096, %esi
        addl $4, %edi
        loop 1b


3:      movl $boot_page_directory, %ecx
        subl $0xC0000000, %ecx
        orl $0x003, %ecx
        movl %ecx, boot_page_directory - 0xC0000000 + 1023 * 4

        movl $boot_page_table0, %ecx
        subl $0xC0000000, %ecx
        orl $0x003, %ecx
        movl %ecx, boot_page_directory - 0xC0000000
        movl %ecx, boot_page_directory - 0xC0000000 + 768 * 4

        movl $boot_page_table1, %ecx
        subl $0xC0000000, %ecx
        orl $0x003, %ecx
        movl %ecx, boot_page_directory - 0xC0000000 + 1022 * 4

        movl $boot_page_directory, %ecx
        subl $0xC0000000, %ecx
        movl %ecx, %cr3

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

        lea 4f, %ecx
        jmp *%ecx

.section .text

4:      movl $stack_top, %esp
        and $-16, %esp

        call setup_stack_guard

        xorl %ebp, %ebp
        pushl %ebx
        pushl %eax
        call i386_entry

        cli
1:      hlt
        jmp 1b

.global setup_stack_guard
.type setup_stack_guard, @function
setup_stack_guard:
        pushl %eax
        pushl %ebx
        pushl %ecx

        movl $1, %eax
        movl $0, %ecx
        cpuid
        shrl $30, %ecx
        andl $1, %ecx
        jnz start_loop
        jmp fail
start_loop:
        rdrand %eax
        jc done
        loop start_loop
fail:
        movl $-1, %eax
done:
        movl %eax, __stack_chk_guard
        popl %ecx
        popl %ebx
        popl %eax
        ret

.global enable_paging
.type enable_paging, @function
enable_paging:
        pushl %ebp
        movl %esp, %ebp

        movl 8(%esp), %eax
        movl %eax, %cr3

        movl %cr0, %eax
        orl $0x80000001, %eax
        movl %eax, %cr0

        movl $0, %eax
        popl %ebp
        ret

.global flush_gdt
.type flush_gdt, @function
flush_gdt:
        cli
        lgdt (gp)
        movw $0x10, %ax
        movw %ax, %ds
        movw %ax, %es
        movw %ax, %fs
        movw %ax, %gs
        movw %ax, %ss
        jmp $0x08, $.flush
.flush:
        ret