xref: /openbsd/sys/arch/amd64/amd64/mptramp.S (revision 7b36286a)
1/*	$OpenBSD: mptramp.S,v 1.5 2008/06/26 05:42:09 ray 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#include <machine/gdt.h>
87
88#define _RELOC(x)       ((x) - KERNBASE)
89#define RELOC(x)        _RELOC(_C_LABEL(x))
90
91#define _TRMP_LABEL(a)  a = . - _C_LABEL(cpu_spinup_trampoline) + MP_TRAMPOLINE
92
93	.globl	_C_LABEL(mpidle)
94	.global _C_LABEL(cpu_spinup_trampoline)
95	.global _C_LABEL(cpu_spinup_trampoline_end)
96	.global _C_LABEL(cpu_hatch)
97	.global _C_LABEL(local_apic)
98	.global _C_LABEL(mp_pdirpa)
99
100	.global	gdt64
101
102#define HALT	1: jmp 1b
103
104	.text
105	.align 4,0x0
106	.code16
107_C_LABEL(cpu_spinup_trampoline):
108	cli
109	xorw    %ax,%ax
110	movw    %ax, %ds
111	movw    %ax, %es
112	movw    %ax, %ss
113	data32 addr32 lgdt (mptramp_gdt32_desc)   # load flat descriptor table
114	movl    %cr0, %eax       # get cr0
115	orl     $0x1, %eax      # enable protected mode
116	movl    %eax, %cr0      # doit
117	ljmpl	$0x8, $mp_startup
118
119_TRMP_LABEL(mp_startup)
120	.code32
121
122	movl    $0x10, %eax     # data segment
123	movw    %ax, %ds
124	movw    %ax, %ss
125	movw    %ax, %es
126	movw    %ax, %fs
127	movw    %ax, %gs
128
129	movl    $ (MP_TRAMPOLINE+NBPG-16),%esp       # bootstrap stack end,
130						     # with scratch space..
131
132	/* First, reset the PSL. */
133	pushl   $PSL_MBO
134	popfl
135
136	movl	%cr4,%eax
137	orl	$(CR4_DEFAULT),%eax
138	movl	%eax,%cr4
139
140	movl	$MSR_EFER,%ecx
141	rdmsr
142	movl	%edx, %edi		# %edx is needed by wrmsr below
143
144	# Check if we need to enable NXE
145	movl	$0x80000001, %eax
146	cpuid
147	andl	$CPUID_NXE, %edx
148	xorl	%eax,%eax
149	testl	%edx, %edx
150	jz	1f
151	orl	$EFER_NXE, %eax
1521:
153	orl	$(EFER_LME|EFER_SCE), %eax
154
155	movl	%edi, %edx		# Restore saved %edx
156	movl	$MSR_EFER,%ecx
157	wrmsr
158
159	movl	$mp_tramp_pdirpa, %ecx
160	movl	(%ecx), %ecx
161        movl    %ecx,%cr3               # load ptd addr into mmu
162
163	movl    $GSEL(GDATA_SEL, SEL_KPL),%eax 	#switch to new segment
164	movl    %eax,%ds
165	movl    %eax,%es
166	movl    %eax,%ss
167
168	movl	$mptramp_gdt64_desc,%eax
169	lgdt	(%eax)
170	movl	$mptramp_jmp64,%eax
171
172	movl    %cr0,%ecx               # get control word
173	orl     $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP),%ecx
174	movl	%ecx, %cr0
175
176	ljmp	*(%eax)
177
178_TRMP_LABEL(mptramp_jmp64)
179	.long   mptramp_longmode
180	.word   GSEL(GCODE_SEL, SEL_KPL)
181
182_TRMP_LABEL(mptramp_gdt32)
183	.quad 0x0000000000000000
184	.quad 0x00cf9f000000ffff
185	.quad 0x00cf93000000ffff
186_TRMP_LABEL(mptramp_gdt32_desc)
187	.word 0x17
188	.long mptramp_gdt32
189
190_TRMP_LABEL(mptramp_gdt64)
191	.quad 0x0000000000000000
192	.quad 0x00af9a000000ffff
193	.quad 0x00cf92000000ffff
194_TRMP_LABEL(mptramp_gdt64_desc)
195	.word 0x17
196	.long mptramp_gdt64
197
198	.global mp_pdirpa
199_TRMP_LABEL(mp_pdirpa)
200_TRMP_LABEL(mp_tramp_pdirpa)
201	.long 0
202
203_TRMP_LABEL(mptramp_longmode)
204	.code64
205	movabsq	$_C_LABEL(cpu_spinup_trampoline_end),%rax
206	jmp	*%rax
207
208
209
210_C_LABEL(cpu_spinup_trampoline_end):	#end of code copied to MP_TRAMPOLINE
211	movl	_C_LABEL(local_apic)+LAPIC_ID,%ecx
212	shrl	$LAPIC_ID_SHIFT,%ecx
213	movq	_C_LABEL(cpu_info)(,%rcx,8),%rdi
214
215	movq	CPU_INFO_IDLE_PCB(%rdi),%rsi
216
217	movq	PCB_RSP(%rsi),%rsp
218	movq	PCB_RBP(%rsi),%rbp
219
220	movq	CPU_INFO_GDT(%rdi),%rax
221	movw	$(MAXGDTSIZ-1),-10(%rsp)
222	movq	%rax,-8(%rsp)
223	lgdt	-10(%rsp)
224
225	/* Switch address space. */
226	movq	PCB_CR3(%rsi),%rax
227	movq	%rax,%cr3
228	movl    PCB_CR0(%rsi),%eax
229	movq    %rax,%cr0
230	call	_C_LABEL(cpu_hatch)
231	/* NOTREACHED */
232