xref: /openbsd/sys/arch/amd64/amd64/mptramp.S (revision 891d7ab6)
1/*	$OpenBSD: mptramp.S,v 1.7 2010/11/13 04:16:42 guenther Exp $	*/
2/*	$NetBSD: mptramp.S,v 1.1 2003/04/26 18:39:30 fvdl Exp $	*/
3
4/*-
5 * Copyright (c) 2000 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by RedBack Networks Inc.
10 *
11 * Author: Bill Sommerfeld
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/*
36 * Copyright (c) 1999 Stefan Grefen
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgement:
48 *      This product includes software developed by the NetBSD
49 *      Foundation, Inc. and its contributors.
50 * 4. Neither the name of The NetBSD Foundation nor the names of its
51 *    contributors may be used to endorse or promote products derived
52 *    from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
55 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 */
66/*
67 * MP startup ...
68 * the stuff from cpu_spinup_trampoline to mp_startup
69 * is copied into the first 640 KB
70 *
71 * We startup the processors now when the kthreads become ready.
72 * The steps are:
73 *        1)   Get the processors running kernel-code from a special
74 *                  page-table and stack page, do chip identification.
75 *        2)   halt the processors waiting for them to be enabled
76 *              by a idle-thread
77 */
78
79#include "assym.h"
80#include <machine/param.h>
81#include <machine/asm.h>
82#include <machine/specialreg.h>
83#include <machine/segments.h>
84#include <machine/mpbiosvar.h>
85#include <machine/i82489reg.h>
86
87#define _RELOC(x)       ((x) - KERNBASE)
88#define RELOC(x)        _RELOC(_C_LABEL(x))
89
90#define _TRMP_LABEL(a)  a = . - _C_LABEL(cpu_spinup_trampoline) + MP_TRAMPOLINE
91
92	.globl	_C_LABEL(mpidle)
93	.global _C_LABEL(cpu_spinup_trampoline)
94	.global _C_LABEL(cpu_spinup_trampoline_end)
95	.global _C_LABEL(cpu_hatch)
96	.global _C_LABEL(local_apic)
97	.global _C_LABEL(mp_pdirpa)
98
99	.global	gdt64
100
101#define HALT	1: jmp 1b
102
103	.text
104	.align 4,0x0
105	.code16
106_C_LABEL(cpu_spinup_trampoline):
107	cli
108	xorw    %ax,%ax
109	movw    %ax, %ds
110	movw    %ax, %es
111	movw    %ax, %ss
112	data32 addr32 lgdt (mptramp_gdt32_desc)   # load flat descriptor table
113	movl    %cr0, %eax       # get cr0
114	orl     $0x1, %eax      # enable protected mode
115	movl    %eax, %cr0      # doit
116	ljmpl	$0x8, $mp_startup
117
118_TRMP_LABEL(mp_startup)
119	.code32
120
121	movl    $0x10, %eax     # data segment
122	movw    %ax, %ds
123	movw    %ax, %ss
124	movw    %ax, %es
125	movw    %ax, %fs
126	movw    %ax, %gs
127
128	movl    $ (MP_TRAMPOLINE+NBPG-16),%esp       # bootstrap stack end,
129						     # with scratch space..
130
131	/* First, reset the PSL. */
132	pushl   $PSL_MBO
133	popfl
134
135	movl	%cr4,%eax
136	orl	$(CR4_DEFAULT),%eax
137	movl	%eax,%cr4
138
139	movl	$MSR_EFER,%ecx
140	rdmsr
141	movl	%edx, %edi		# %edx is needed by wrmsr below
142
143	# Check if we need to enable NXE
144	movl	$0x80000001, %eax
145	cpuid
146	andl	$CPUID_NXE, %edx
147	xorl	%eax,%eax
148	testl	%edx, %edx
149	jz	1f
150	orl	$EFER_NXE, %eax
1511:
152	orl	$(EFER_LME|EFER_SCE), %eax
153
154	movl	%edi, %edx		# Restore saved %edx
155	movl	$MSR_EFER,%ecx
156	wrmsr
157
158	movl	$mp_tramp_pdirpa, %ecx
159	movl	(%ecx), %ecx
160        movl    %ecx,%cr3               # load ptd addr into mmu
161
162	movl    $GSEL(GDATA_SEL, SEL_KPL),%eax 	#switch to new segment
163	movl    %eax,%ds
164	movl    %eax,%es
165	movl    %eax,%ss
166
167	movl	$mptramp_gdt64_desc,%eax
168	lgdt	(%eax)
169	movl	$mptramp_jmp64,%eax
170
171	movl    %cr0,%ecx               # get control word
172	orl     $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP),%ecx
173	movl	%ecx, %cr0
174
175	ljmp	*(%eax)
176
177_TRMP_LABEL(mptramp_jmp64)
178	.long   mptramp_longmode
179	.word   GSEL(GCODE_SEL, SEL_KPL)
180
181_TRMP_LABEL(mptramp_gdt32)
182	.quad 0x0000000000000000
183	.quad 0x00cf9f000000ffff
184	.quad 0x00cf93000000ffff
185_TRMP_LABEL(mptramp_gdt32_desc)
186	.word 0x17
187	.long mptramp_gdt32
188
189_TRMP_LABEL(mptramp_gdt64)
190	.quad 0x0000000000000000
191	.quad 0x00af9a000000ffff
192	.quad 0x00cf92000000ffff
193_TRMP_LABEL(mptramp_gdt64_desc)
194	.word 0x17
195	.long mptramp_gdt64
196
197	.global mp_pdirpa
198_TRMP_LABEL(mp_pdirpa)
199_TRMP_LABEL(mp_tramp_pdirpa)
200	.long 0
201
202_TRMP_LABEL(mptramp_longmode)
203	.code64
204	movabsq	$_C_LABEL(cpu_spinup_trampoline_end),%rax
205	jmp	*%rax
206
207
208
209_C_LABEL(cpu_spinup_trampoline_end):	#end of code copied to MP_TRAMPOLINE
210	movl	_C_LABEL(local_apic)+LAPIC_ID,%eax
211	shrl	$LAPIC_ID_SHIFT,%eax
212	xorq	%rcx,%rcx
2131:
214	movq	_C_LABEL(cpu_info)(,%rcx,8),%rdi
215	incq	%rcx
216	movl	CPU_INFO_APICID(%rdi),%edx
217	cmpl	%eax,%edx
218	jne	1b
219
220	movq	CPU_INFO_IDLE_PCB(%rdi),%rsi
221
222	movq	PCB_RSP(%rsi),%rsp
223	movq	PCB_RBP(%rsi),%rbp
224
225	movq	CPU_INFO_GDT(%rdi),%rax
226	movw	$(GDT_SIZE-1),-10(%rsp)
227	movq	%rax,-8(%rsp)
228	lgdt	-10(%rsp)
229
230	/* Switch address space. */
231	movq	PCB_CR3(%rsi),%rax
232	movq	%rax,%cr3
233	movl    PCB_CR0(%rsi),%eax
234	movq    %rax,%cr0
235	call	_C_LABEL(cpu_hatch)
236	/* NOTREACHED */
237