xref: /titanic_52/usr/src/uts/i86pc/os/mp_pc.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Welcome to the world of the "real mode platter".
30  * See also startup.c, mpcore.s and apic.c for related routines.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/cpuvar.h>
36 #include <sys/kmem.h>
37 #include <sys/archsystm.h>
38 #include <sys/machsystm.h>
39 #include <sys/controlregs.h>
40 #include <sys/x86_archext.h>
41 #include <sys/smp_impldefs.h>
42 #include <sys/sysmacros.h>
43 #include <sys/mach_mmu.h>
44 #include <sys/promif.h>
45 #include <sys/cpu.h>
46 #include <vm/hat_i86.h>
47 
48 extern void real_mode_start(void);
49 extern void real_mode_end(void);
50 extern void *(*cpu_pause_func)(void *);
51 
52 void rmp_gdt_init(rm_platter_t *);
53 
54 /*
55  * Fill up the real mode platter to make it easy for real mode code to
56  * kick it off. This area should really be one passed by boot to kernel
57  * and guaranteed to be below 1MB and aligned to 16 bytes. Should also
58  * have identical physical and virtual address in paged mode.
59  */
60 static ushort_t *warm_reset_vector = NULL;
61 
62 int
63 mach_cpucontext_init(void)
64 {
65 	ushort_t *vec;
66 
67 	if (!(vec = (ushort_t *)psm_map_phys(WARM_RESET_VECTOR,
68 	    sizeof (vec), PROT_READ | PROT_WRITE)))
69 		return (-1);
70 	/*
71 	 * setup secondary cpu bios boot up vector
72 	 */
73 	*vec = (ushort_t)((caddr_t)
74 	    ((struct rm_platter *)rm_platter_va)->rm_code - rm_platter_va
75 	    + ((ulong_t)rm_platter_va & 0xf));
76 	vec[1] = (ushort_t)(rm_platter_pa >> 4);
77 	warm_reset_vector = vec;
78 
79 	bcopy((caddr_t)real_mode_start,
80 	    (caddr_t)((rm_platter_t *)rm_platter_va)->rm_code,
81 	    (size_t)real_mode_end - (size_t)real_mode_start);
82 
83 	return (0);
84 }
85 
86 void
87 mach_cpucontext_fini(void)
88 {
89 	if (warm_reset_vector)
90 		psm_unmap_phys((caddr_t)warm_reset_vector,
91 		    sizeof (warm_reset_vector));
92 	hat_unload(kas.a_hat, (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
93 	    HAT_UNLOAD);
94 }
95 
96 #if defined(__amd64)
97 extern void *long_mode_64(void);
98 #endif	/* __amd64 */
99 
100 void *
101 mach_cpucontext_alloc(struct cpu *cp)
102 {
103 	rm_platter_t *rm = (rm_platter_t *)rm_platter_va;
104 	struct cpu_tables *ct;
105 	struct tss *ntss;
106 
107 	/*
108 	 * Allocate space for page directory, stack, tss, gdt and idt.
109 	 * The page directory has to be page aligned
110 	 */
111 	ct = kmem_zalloc(sizeof (*ct), KM_SLEEP);
112 	if ((uintptr_t)ct & ~MMU_STD_PAGEMASK)
113 		panic("mp_startup_init: cpu%d misaligned tables", cp->cpu_id);
114 
115 	ntss = cp->cpu_tss = &ct->ct_tss;
116 
117 #if defined(__amd64)
118 
119 	/*
120 	 * #DF (double fault).
121 	 */
122 	ntss->tss_ist1 = (uint64_t)&ct->ct_stack[sizeof (ct->ct_stack)];
123 
124 #elif defined(__i386)
125 
126 	ntss->tss_esp0 = ntss->tss_esp1 = ntss->tss_esp2 = ntss->tss_esp =
127 	    (uint32_t)&ct->ct_stack[sizeof (ct->ct_stack)];
128 
129 	ntss->tss_ss0 = ntss->tss_ss1 = ntss->tss_ss2 = ntss->tss_ss = KDS_SEL;
130 
131 	ntss->tss_eip = (uint32_t)cp->cpu_thread->t_pc;
132 
133 	ntss->tss_cs = KCS_SEL;
134 	ntss->tss_ds = ntss->tss_es = KDS_SEL;
135 	ntss->tss_fs = KFS_SEL;
136 	ntss->tss_gs = KGS_SEL;
137 
138 #endif	/* __i386 */
139 
140 	/*
141 	 * Set I/O bit map offset equal to size of TSS segment limit
142 	 * for no I/O permission map. This will cause all user I/O
143 	 * instructions to generate #gp fault.
144 	 */
145 	ntss->tss_bitmapbase = sizeof (*ntss);
146 
147 	/*
148 	 * Setup kernel tss.
149 	 */
150 	set_syssegd((system_desc_t *)&cp->cpu_gdt[GDT_KTSS], cp->cpu_tss,
151 	    sizeof (*cp->cpu_tss) -1, SDT_SYSTSS, SEL_KPL);
152 
153 	/*
154 	 * Now copy all that we've set up onto the real mode platter
155 	 * for the real mode code to digest as part of starting the cpu.
156 	 */
157 
158 	rm->rm_idt_base = cp->cpu_idt;
159 	rm->rm_idt_lim = sizeof (idt0) - 1;
160 	rm->rm_gdt_base = cp->cpu_gdt;
161 	rm->rm_gdt_lim = ((sizeof (*cp->cpu_gdt) * NGDT)) -1;
162 
163 	rm->rm_pdbr = getcr3();
164 	rm->rm_cpu = cp->cpu_id;
165 	rm->rm_x86feature = x86_feature;
166 	rm->rm_cr4 = getcr4();
167 
168 	rmp_gdt_init(rm);
169 
170 	return (ct);
171 }
172 
173 /*ARGSUSED*/
174 void
175 rmp_gdt_init(rm_platter_t *rm)
176 {
177 
178 #if defined(__amd64)
179 
180 	if (getcr3() > 0xffffffffUL)
181 		panic("Cannot initialize CPUs; kernel's 64-bit page tables\n"
182 		    "located above 4G in physical memory (@ 0x%lx)", getcr3());
183 
184 	/*
185 	 * Setup pseudo-descriptors for temporary GDT and IDT for use ONLY
186 	 * by code in real_mode_start():
187 	 *
188 	 * GDT[0]:  NULL selector
189 	 * GDT[1]:  64-bit CS: Long = 1, Present = 1, bits 12, 11 = 1
190 	 *
191 	 * Clear the IDT as interrupts will be off and a limit of 0 will cause
192 	 * the CPU to triple fault and reset on an NMI, seemingly as reasonable
193 	 * a course of action as any other, though it may cause the entire
194 	 * platform to reset in some cases...
195 	 */
196 	rm->rm_temp_gdt[0] = 0;
197 	rm->rm_temp_gdt[TEMPGDT_KCODE64] = 0x20980000000000ULL;
198 
199 	rm->rm_temp_gdt_lim = (ushort_t)(sizeof (rm->rm_temp_gdt) - 1);
200 	rm->rm_temp_gdt_base = rm_platter_pa +
201 	    (uint32_t)offsetof(rm_platter_t, rm_temp_gdt);
202 	rm->rm_temp_idt_lim = 0;
203 	rm->rm_temp_idt_base = 0;
204 
205 	/*
206 	 * Since the CPU needs to jump to protected mode using an identity
207 	 * mapped address, we need to calculate it here.
208 	 */
209 	rm->rm_longmode64_addr = rm_platter_pa +
210 	    ((uint32_t)long_mode_64 - (uint32_t)real_mode_start);
211 #endif	/* __amd64 */
212 
213 }
214 
215 /*ARGSUSED*/
216 void
217 mach_cpucontext_free(struct cpu *cp, void *arg, int err)
218 {
219 	struct cpu_tables *ct = arg;
220 
221 	ASSERT(&ct->ct_tss == cp->cpu_tss);
222 
223 	switch (err) {
224 	case 0:
225 		break;
226 	case ETIMEDOUT:
227 		/*
228 		 * The processor was poked, but failed to start before
229 		 * we gave up waiting for it.  In case it starts later,
230 		 * don't free anything.
231 		 */
232 		break;
233 	default:
234 		/*
235 		 * Some other, passive, error occurred.
236 		 */
237 		kmem_free(ct, sizeof (*ct));
238 		cp->cpu_tss = NULL;
239 		break;
240 	}
241 }
242 
243 /*
244  * "Enter monitor."  Called via cross-call from stop_other_cpus().
245  */
246 void
247 mach_cpu_halt(char *msg)
248 {
249 	if (msg)
250 		prom_printf("%s\n", msg);
251 
252 	/*CONSTANTCONDITION*/
253 	while (1)
254 		;
255 }
256 
257 void
258 mach_cpu_idle(void)
259 {
260 	tlb_going_idle();
261 	i86_halt();
262 	tlb_service();
263 }
264 
265 void
266 mach_cpu_pause(volatile char *safe)
267 {
268 	/*
269 	 * This cpu is now safe.
270 	 */
271 	*safe = PAUSE_WAIT;
272 	membar_enter(); /* make sure stores are flushed */
273 
274 	/*
275 	 * Now we wait.  When we are allowed to continue, safe
276 	 * will be set to PAUSE_IDLE.
277 	 */
278 	while (*safe != PAUSE_IDLE)
279 		SMT_PAUSE();
280 }
281 
282 /*
283  * Power on CPU.
284  */
285 /*ARGSUSED*/
286 int
287 mp_cpu_poweron(struct cpu *cp)
288 {
289 	ASSERT(MUTEX_HELD(&cpu_lock));
290 	return (ENOTSUP);		/* not supported */
291 }
292 
293 /*
294  * Power off CPU.
295  */
296 /*ARGSUSED*/
297 int
298 mp_cpu_poweroff(struct cpu *cp)
299 {
300 	ASSERT(MUTEX_HELD(&cpu_lock));
301 	return (ENOTSUP);		/* not supported */
302 }
303