summaryrefslogtreecommitdiff
path: root/arch/x86/machine/boot.S
blob: ece1bb892b6f0fc174ac274d21b8f6febf3b2ea0 (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
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
/*
 * Copyright (c) 2010, 2012 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define __ASSEMBLY__

#include <machine/asm.h>
#include <machine/cpu.h>
#include <machine/boot.h>
#include <machine/multiboot.h>

/*
 * Convert a physical address in the .boot section to its real address in
 * the MP trampoline code.
 */
#define BOOT_MP_ADDR_PTOT(addr) (BOOT_MP_TRAMPOLINE_ADDR + (addr) \
                                 - boot_ap_start)

.section .boothdr, "wax"

 /*
  * Multiboot header.
  */
.align 4
DATA(boot_header)
 .long MULTIBOOT_OS_MAGIC
 .long MULTIBOOT_OS_FLAGS
 .long -(MULTIBOOT_OS_FLAGS + MULTIBOOT_OS_MAGIC)
END(boot_header)

/*
 * Entry point.
 */
ENTRY(_start)
 .code32
 /*
  * Set up a simple GDT to conform to the multiboot specification.
  */
 lgdt boot_gdtr

 /*
  * Keep %eax and %ebx.
  */
 movw $0x10, %cx
 movw %cx, %ds
 movw %cx, %es
 movw %cx, %ss
 xorw %cx, %cx
 movw %cx, %fs
 movw %cx, %gs
 ljmp $8, $1f

1:

/* XXX For now */
#ifdef __LP64__
 hlt
#endif

 /*
  * Set up the init stack.
  */
 movl $(init_stack + BOOT_STACK_SIZE), %esp
 movl %esp, %ebp

 /*
  * Enable paging.
  */
 pushl %ebx
 pushl %eax
 call init_paging
 movl %eax, %cr3
 movl %cr0, %eax
 orl $CPU_CR0_PG, %eax
 movl %eax, %cr0
 ljmp $8, $1f

1:
#ifdef __LP64__
 hlt
#else /* __LP64__ */
 /*
  * Reset the stack, use high addresses.
  */
 movl $(init_stack + BOOT_STACK_SIZE + KERNEL_OFFSET), %esp
 movl %esp, %ebp

 /*
  * Prevent stack tracing from searching previous frames.
  */
 pushl $0
 jmp init
#endif /* __LP64__ */

 /*
  * Never reached.
  */
END(_start)

.section .boot, "wax"

DATA(boot_gdtr)
 .word (8 * 3)
 .long boot_gdt
END(boot_gdtr)

/*
 * This is where an AP runs after leaving the trampoline code.
 */
ENTRY(boot_ap_start32)
 /*
  * Set up the GDT again, because the current one is from the trampoline code
  * which isn't part of the identity mapping and won't be available once paging
  * is enabled.
  */
 lgdt boot_gdtr
 movw $0x10, %ax
 movw %ax, %ds
 movw %ax, %es
 movw %ax, %ss
 xorw %ax, %ax
 movw %ax, %fs
 movw %ax, %gs
 ljmp $8, $1f

1:
 /*
  * Set up the init stack.
  */
 movl $(init_ap_stack + BOOT_STACK_SIZE), %esp
 movl %esp, %ebp

 /*
  * Enable paging.
  */
 call init_ap_paging
 movl %eax, %cr3
 movl %cr0, %eax
 orl $CPU_CR0_PG, %eax
 movl %eax, %cr0
 ljmp $8, $1f

1:
 /*
  * Switch to the boot stack preallocated for this AP by the BSP.
  */
 movl init_ap_boot_stack_addr, %esp
 addl $BOOT_STACK_SIZE, %esp
 movl %esp, %ebp

 /*
  * Prevent stack tracing from searching previous frames.
  */
 pushl $0
 jmp init_ap

 /*
  * Never reached.
  */
END(boot_ap_start32)

/*
 * This section, including the GDT, is the MP trampoline code run by APs
 * on startup. It is copied at a fixed location in the first segment and
 * must enable protected mode to jump back into the kernel.
 */
.align 8
ENTRY(boot_ap_start)
 .code16
 cli
 xorw %ax, %ax
 movw %ax, %ds
 movw %ax, %es
 movw %ax, %fs
 movw %ax, %gs
 movw %ax, %ss
 lgdt BOOT_MP_ADDR_PTOT(boot_ap_gdtr)
 movl %cr0, %eax
 orl $CPU_CR0_PE, %eax
 movl %eax, %cr0
 ljmp $8, $BOOT_MP_ADDR_PTOT(1f)

.align 4
1:
 .code32
 movw $0x10, %ax
 movw %ax, %ds
 movw %ax, %es
 movw %ax, %ss
 xorw %ax, %ax
 movw %ax, %fs
 movw %ax, %gs
 ljmp $8, $boot_ap_start32
END(boot_ap_start)

DATA(boot_ap_gdtr)
 .word (8 * 3)
 .long BOOT_MP_ADDR_PTOT(boot_gdt)
END(boot_ap_gdtr)

.align 8
DATA(boot_gdt)
 /*
  * Null selector.
  */
 .word 0x0000
 .word 0x0000
 .word 0x0000
 .word 0x0000

 /*
  * Code segment selector.
  */
 .word 0xffff
 .word 0x0000
 .word 0x9a00
 .word 0x00cf

 /*
  * Data segment selector.
  */
 .word 0xffff
 .word 0x0000
 .word 0x9200
 .word 0x00cf
END(boot_gdt)

DATA(boot_ap_size)
 .long . - boot_ap_start
END(boot_ap_size)