mpboot.S revision 60973
1/*
2 * Copyright (c) 1995, Jack F. Vogel
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Jack F. Vogel
16 * 4. The name of the developer may be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * mpboot.s:	FreeBSD machine support for the Intel MP Spec
32 *		multiprocessor systems.
33 *
34 * $FreeBSD: head/sys/amd64/amd64/mpboot.S 60973 2000-05-27 06:25:35Z jhb $
35 */
36
37#include <machine/asmacros.h>		/* miscellaneous asm macros */
38#include <machine/apic.h>
39#include <machine/specialreg.h>
40
41#include "assym.s"
42
43/*
44 * this code MUST be enabled here and in mp_machdep.c
45 * it follows the very early stages of AP boot by placing values in CMOS ram.
46 * it NORMALLY will never be needed and thus the primitive method for enabling.
47 *
48#define CHECK_POINTS
49 */
50
51#if defined(CHECK_POINTS) && !defined(PC98)
52
53#define CMOS_REG	(0x70)
54#define CMOS_DATA	(0x71)
55
56#define CHECKPOINT(A,D)		\
57	movb	$(A),%al ;	\
58	outb	%al,$CMOS_REG ;	\
59	movb	$(D),%al ;	\
60	outb	%al,$CMOS_DATA
61
62#else
63
64#define CHECKPOINT(A,D)
65
66#endif /* CHECK_POINTS */
67
68
69/*
70 * the APs enter here from their trampoline code (bootMP, below)
71 */
72	.p2align 4
73
74NON_GPROF_ENTRY(MPentry)
75	CHECKPOINT(0x36, 3)
76	/* Now enable paging mode */
77	movl	_IdlePTD-KERNBASE, %eax
78	movl	%eax,%cr3
79	movl	%cr0,%eax
80	orl	$CR0_PE|CR0_PG,%eax		/* enable paging */
81	movl	%eax,%cr0			/* let the games begin! */
82	movl	_bootSTK,%esp			/* boot stack end loc. */
83
84	pushl	$mp_begin			/* jump to high mem */
85	ret
86
87	/*
88	 * Wait for the booting CPU to signal startup
89	 */
90mp_begin:	/* now running relocated at KERNBASE */
91	CHECKPOINT(0x37, 4)
92	call	_init_secondary			/* load i386 tables */
93	CHECKPOINT(0x38, 5)
94
95	/*
96	 * If the [BSP] CPU has support for VME, turn it on.
97	 */
98	testl	$CPUID_VME, _cpu_feature	/* XXX WRONG! BSP! */
99	jz	1f
100	movl	%cr4, %eax
101	orl	$CR4_VME, %eax
102	movl	%eax, %cr4
1031:
104
105	/* disable the APIC, just to be SURE */
106	movl	lapic_svr, %eax			/* get spurious vector reg. */
107	andl	$~APIC_SVR_SWEN, %eax		/* clear software enable bit */
108	movl	%eax, lapic_svr
109
110	/* signal our startup to the BSP */
111	movl	lapic_ver, %eax			/* our version reg contents */
112	movl	%eax, _cpu_apic_versions	/* into [ 0 ] */
113	incl	_mp_ncpus			/* signal BSP */
114
115	CHECKPOINT(0x39, 6)
116
117	/* wait till we can get into the kernel */
118	call	_boot_get_mplock
119
120	/* Now, let's prepare for some REAL WORK :-) */
121	call	_ap_init
122
123	call	_rel_mplock
1242:
125	cmpl	$0, CNAME(smp_started)	/* Wait for last AP to be ready */
126	jz	2b
127	call _get_mplock
128
129	/* let her rip! (loads new stack) */
130	jmp 	_cpu_switch
131
132NON_GPROF_ENTRY(wait_ap)
133	pushl	%ebp
134	movl	%esp, %ebp
135	call	_rel_mplock
136	movl	%eax, 8(%ebp)
1371:
138	cmpl	$0, CNAME(smp_started)
139	jnz	2f
140	decl	%eax
141	cmpl	$0, %eax
142	jge	1b
1432:
144	call	_get_mplock
145	movl	%ebp, %esp
146	popl	%ebp
147	ret
148
149
150/*
151 * This is the embedded trampoline or bootstrap that is
152 * copied into 'real-mode' low memory, it is where the
153 * secondary processor "wakes up". When it is executed
154 * the processor will eventually jump into the routine
155 * MPentry, which resides in normal kernel text above
156 * 1Meg.		-jackv
157 */
158
159	.data
160	ALIGN_DATA				/* just to be sure */
161
162BOOTMP1:
163
164NON_GPROF_ENTRY(bootMP)
165	.code16
166	cli
167	CHECKPOINT(0x34, 1)
168	/* First guarantee a 'clean slate' */
169	xorl	%eax, %eax
170	movl	%eax, %ebx
171	movl	%eax, %ecx
172 	movl	%eax, %edx
173	movl	%eax, %esi
174	movl	%eax, %edi
175
176	/* set up data segments */
177	mov	%cs, %ax
178	mov	%ax, %ds
179	mov	%ax, %es
180	mov	%ax, %fs
181	mov	%ax, %gs
182	mov	%ax, %ss
183	mov	$(boot_stk-_bootMP), %esp
184
185	/* Now load the global descriptor table */
186	lgdt	MP_GDTptr-_bootMP
187
188	/* Enable protected mode */
189	movl	%cr0, %eax
190	orl	$CR0_PE, %eax
191	movl	%eax, %cr0
192
193	/*
194	 * make intrasegment jump to flush the processor pipeline and
195	 * reload CS register
196	 */
197	pushl	$0x18
198	pushl	$(protmode-_bootMP)
199	lretl
200
201       .code32
202protmode:
203	CHECKPOINT(0x35, 2)
204
205	/*
206	 * we are NOW running for the first time with %eip
207	 * having the full physical address, BUT we still
208	 * are using a segment descriptor with the origin
209	 * not matching the booting kernel.
210	 *
211 	 * SO NOW... for the BIG Jump into kernel's segment
212	 * and physical text above 1 Meg.
213	 */
214	mov	$0x10, %ebx
215	movw	%bx, %ds
216	movw	%bx, %es
217	movw	%bx, %fs
218	movw	%bx, %gs
219	movw	%bx, %ss
220
221	.globl	_bigJump
222_bigJump:
223	/* this will be modified by mpInstallTramp() */
224	ljmp	$0x08, $0			/* far jmp to MPentry() */
225
226dead:	hlt /* We should never get here */
227	jmp	dead
228
229/*
230 * MP boot strap Global Descriptor Table
231 */
232	.p2align 4
233	.globl	_MP_GDT
234	.globl	_bootCodeSeg
235	.globl	_bootDataSeg
236_MP_GDT:
237
238nulldesc:		/* offset = 0x0 */
239
240	.word	0x0
241	.word	0x0
242	.byte	0x0
243	.byte	0x0
244	.byte	0x0
245	.byte	0x0
246
247kernelcode:		/* offset = 0x08 */
248
249	.word	0xffff	/* segment limit 0..15 */
250	.word	0x0000	/* segment base 0..15 */
251	.byte	0x0	/* segment base 16..23; set for 0K */
252	.byte	0x9f	/* flags; Type	*/
253	.byte	0xcf	/* flags; Limit	*/
254	.byte	0x0	/* segment base 24..32 */
255
256kerneldata:		/* offset = 0x10 */
257
258	.word	0xffff	/* segment limit 0..15 */
259	.word	0x0000	/* segment base 0..15 */
260	.byte	0x0	/* segment base 16..23; set for 0k */
261	.byte	0x93	/* flags; Type  */
262	.byte	0xcf	/* flags; Limit */
263	.byte	0x0	/* segment base 24..32 */
264
265bootcode:		/* offset = 0x18 */
266
267	.word	0xffff	/* segment limit 0..15 */
268_bootCodeSeg:		/* this will be modified by mpInstallTramp() */
269	.word	0x0000	/* segment base 0..15 */
270	.byte	0x00	/* segment base 16...23; set for 0x000xx000 */
271	.byte	0x9e	/* flags; Type  */
272	.byte	0xcf	/* flags; Limit */
273	.byte	0x0	/*segment base 24..32 */
274
275bootdata:		/* offset = 0x20 */
276
277	.word	0xffff
278_bootDataSeg:		/* this will be modified by mpInstallTramp() */
279	.word	0x0000	/* segment base 0..15 */
280	.byte	0x00	/* segment base 16...23; set for 0x000xx000 */
281	.byte	0x92
282	.byte	0xcf
283	.byte	0x0
284
285/*
286 * GDT pointer for the lgdt call
287 */
288	.globl	_mp_gdtbase
289
290MP_GDTptr:
291_mp_gdtlimit:
292	.word	0x0028
293_mp_gdtbase:		/* this will be modified by mpInstallTramp() */
294	.long	0
295
296	.space	0x100	/* space for boot_stk - 1st temporary stack */
297boot_stk:
298
299BOOTMP2:
300	.globl	_bootMP_size
301_bootMP_size:
302	.long	BOOTMP2 - BOOTMP1
303