xref: /titanic_41/usr/src/uts/i86pc/os/mlsetup.c (revision 2e02daeede04af58a9d4f18f8dfed1fda3ececa1)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/sysmacros.h>
28 #include <sys/disp.h>
29 #include <sys/promif.h>
30 #include <sys/clock.h>
31 #include <sys/cpuvar.h>
32 #include <sys/stack.h>
33 #include <vm/as.h>
34 #include <vm/hat.h>
35 #include <sys/reboot.h>
36 #include <sys/avintr.h>
37 #include <sys/vtrace.h>
38 #include <sys/proc.h>
39 #include <sys/thread.h>
40 #include <sys/cpupart.h>
41 #include <sys/pset.h>
42 #include <sys/copyops.h>
43 #include <sys/pg.h>
44 #include <sys/disp.h>
45 #include <sys/debug.h>
46 #include <sys/sunddi.h>
47 #include <sys/x86_archext.h>
48 #include <sys/privregs.h>
49 #include <sys/machsystm.h>
50 #include <sys/ontrap.h>
51 #include <sys/bootconf.h>
52 #include <sys/kdi_machimpl.h>
53 #include <sys/archsystm.h>
54 #include <sys/promif.h>
55 #include <sys/bootconf.h>
56 #include <sys/kobj.h>
57 #include <sys/kobj_lex.h>
58 #include <sys/pci_cfgspace.h>
59 #ifdef __xpv
60 #include <sys/hypervisor.h>
61 #endif
62 
63 /*
64  * some globals for patching the result of cpuid
65  * to solve problems w/ creative cpu vendors
66  */
67 
68 extern uint32_t cpuid_feature_ecx_include;
69 extern uint32_t cpuid_feature_ecx_exclude;
70 extern uint32_t cpuid_feature_edx_include;
71 extern uint32_t cpuid_feature_edx_exclude;
72 
73 /*
74  * Dummy spl priority masks
75  */
76 static unsigned char dummy_cpu_pri[MAXIPL + 1] = {
77 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
78 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf
79 };
80 
81 
82 static uint32_t
83 bootprop_getval(char *name)
84 {
85 	char prop[32];
86 	u_longlong_t ll;
87 	extern struct bootops *bootops;
88 	if ((BOP_GETPROPLEN(bootops, name) > sizeof (prop)) ||
89 	    (BOP_GETPROP(bootops, name, prop) < 0) ||
90 	    (kobj_getvalue(prop, &ll) == -1))
91 		return (0);
92 	return ((uint32_t)ll);
93 }
94 
95 /*
96  * Setup routine called right before main(). Interposing this function
97  * before main() allows us to call it in a machine-independent fashion.
98  */
99 void
100 mlsetup(struct regs *rp)
101 {
102 	extern struct classfuncs sys_classfuncs;
103 	extern disp_t cpu0_disp;
104 	extern char t0stack[];
105 #if !defined(__xpv)
106 	extern int xpv_is_hvm;
107 #endif
108 
109 	ASSERT_STACK_ALIGNED();
110 
111 	/*
112 	 * initialize cpu_self
113 	 */
114 	cpu[0]->cpu_self = cpu[0];
115 
116 #if defined(__xpv)
117 	/*
118 	 * Point at the hypervisor's virtual cpu structure
119 	 */
120 	cpu[0]->cpu_m.mcpu_vcpu_info = &HYPERVISOR_shared_info->vcpu_info[0];
121 #endif
122 
123 	/*
124 	 * Set up dummy cpu_pri_data values till psm spl code is
125 	 * installed.  This allows splx() to work on amd64.
126 	 */
127 
128 	cpu[0]->cpu_pri_data = dummy_cpu_pri;
129 
130 	/*
131 	 * check if we've got special bits to clear or set
132 	 * when checking cpu features
133 	 */
134 
135 	cpuid_feature_ecx_include =
136 	    bootprop_getval("cpuid_feature_ecx_include");
137 	cpuid_feature_ecx_exclude =
138 	    bootprop_getval("cpuid_feature_ecx_exclude");
139 	cpuid_feature_edx_include =
140 	    bootprop_getval("cpuid_feature_edx_include");
141 	cpuid_feature_edx_exclude =
142 	    bootprop_getval("cpuid_feature_edx_exclude");
143 
144 	/*
145 	 * The first lightweight pass (pass0) through the cpuid data
146 	 * was done in locore before mlsetup was called.  Do the next
147 	 * pass in C code.
148 	 *
149 	 * The x86_feature bits are set here on the basis of the capabilities
150 	 * of the boot CPU.  Note that if we choose to support CPUs that have
151 	 * different feature sets (at which point we would almost certainly
152 	 * want to set the feature bits to correspond to the feature
153 	 * minimum) this value may be altered.
154 	 */
155 	x86_feature = cpuid_pass1(cpu[0]);
156 
157 	/*
158 	 * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss.
159 	 */
160 	init_desctbls();
161 
162 #if !defined(__xpv)
163 
164 	/*
165 	 * Patch the tsc_read routine with appropriate set of instructions,
166 	 * depending on the processor family and architecure, to read the
167 	 * time-stamp counter while ensuring no out-of-order execution.
168 	 * Patch it while the kernel text is still writable.
169 	 *
170 	 * Note: tsc_read is not patched for intel processors whose family
171 	 * is >6 and for amd whose family >f (in case they don't support rdtscp
172 	 * instruction, unlikely). By default tsc_read will use cpuid for
173 	 * serialization in such cases. The following code needs to be
174 	 * revisited if intel processors of family >= f retains the
175 	 * instruction serialization nature of mfence instruction.
176 	 * Note: tsc_read is not patched for x86 processors which do
177 	 * not support "mfence". By default tsc_read will use cpuid for
178 	 * serialization in such cases.
179 	 *
180 	 * The Xen hypervisor does not correctly report whether rdtscp is
181 	 * supported or not, so we must assume that it is not.
182 	 */
183 	if (xpv_is_hvm == 0 && (x86_feature & X86_TSCP))
184 		patch_tsc_read(X86_HAVE_TSCP);
185 	else if (cpuid_getvendor(CPU) == X86_VENDOR_AMD &&
186 	    cpuid_getfamily(CPU) <= 0xf && (x86_feature & X86_SSE2) != 0)
187 		patch_tsc_read(X86_TSC_MFENCE);
188 	else if (cpuid_getvendor(CPU) == X86_VENDOR_Intel &&
189 	    cpuid_getfamily(CPU) <= 6 && (x86_feature & X86_SSE2) != 0)
190 		patch_tsc_read(X86_TSC_LFENCE);
191 
192 #endif	/* !__xpv */
193 
194 #if defined(__i386) && !defined(__xpv)
195 	/*
196 	 * Some i386 processors do not implement the rdtsc instruction,
197 	 * or at least they do not implement it correctly. Patch them to
198 	 * return 0.
199 	 */
200 	if ((x86_feature & X86_TSC) == 0)
201 		patch_tsc_read(X86_NO_TSC);
202 #endif	/* __i386 && !__xpv */
203 
204 #if defined(__amd64) && !defined(__xpv)
205 	patch_memops(cpuid_getvendor(CPU));
206 #endif	/* __amd64 && !__xpv */
207 
208 #if !defined(__xpv)
209 	/* XXPV	what, if anything, should be dorked with here under xen? */
210 
211 	/*
212 	 * While we're thinking about the TSC, let's set up %cr4 so that
213 	 * userland can issue rdtsc, and initialize the TSC_AUX value
214 	 * (the cpuid) for the rdtscp instruction on appropriately
215 	 * capable hardware.
216 	 */
217 	if (x86_feature & X86_TSC)
218 		setcr4(getcr4() & ~CR4_TSD);
219 
220 	if (x86_feature & X86_TSCP)
221 		(void) wrmsr(MSR_AMD_TSCAUX, 0);
222 
223 	if (x86_feature & X86_DE)
224 		setcr4(getcr4() | CR4_DE);
225 #endif /* __xpv */
226 
227 	/*
228 	 * initialize t0
229 	 */
230 	t0.t_stk = (caddr_t)rp - MINFRAME;
231 	t0.t_stkbase = t0stack;
232 	t0.t_pri = maxclsyspri - 3;
233 	t0.t_schedflag = TS_LOAD | TS_DONT_SWAP;
234 	t0.t_procp = &p0;
235 	t0.t_plockp = &p0lock.pl_lock;
236 	t0.t_lwp = &lwp0;
237 	t0.t_forw = &t0;
238 	t0.t_back = &t0;
239 	t0.t_next = &t0;
240 	t0.t_prev = &t0;
241 	t0.t_cpu = cpu[0];
242 	t0.t_disp_queue = &cpu0_disp;
243 	t0.t_bind_cpu = PBIND_NONE;
244 	t0.t_bind_pset = PS_NONE;
245 	t0.t_bindflag = (uchar_t)default_binding_mode;
246 	t0.t_cpupart = &cp_default;
247 	t0.t_clfuncs = &sys_classfuncs.thread;
248 	t0.t_copyops = NULL;
249 	THREAD_ONPROC(&t0, CPU);
250 
251 	lwp0.lwp_thread = &t0;
252 	lwp0.lwp_regs = (void *)rp;
253 	lwp0.lwp_procp = &p0;
254 	t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1;
255 
256 	p0.p_exec = NULL;
257 	p0.p_stat = SRUN;
258 	p0.p_flag = SSYS;
259 	p0.p_tlist = &t0;
260 	p0.p_stksize = 2*PAGESIZE;
261 	p0.p_stkpageszc = 0;
262 	p0.p_as = &kas;
263 	p0.p_lockp = &p0lock;
264 	p0.p_brkpageszc = 0;
265 	p0.p_t1_lgrpid = LGRP_NONE;
266 	p0.p_tr_lgrpid = LGRP_NONE;
267 	sigorset(&p0.p_ignore, &ignoredefault);
268 
269 	CPU->cpu_thread = &t0;
270 	bzero(&cpu0_disp, sizeof (disp_t));
271 	CPU->cpu_disp = &cpu0_disp;
272 	CPU->cpu_disp->disp_cpu = CPU;
273 	CPU->cpu_dispthread = &t0;
274 	CPU->cpu_idle_thread = &t0;
275 	CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE;
276 	CPU->cpu_dispatch_pri = t0.t_pri;
277 
278 	CPU->cpu_id = 0;
279 
280 	CPU->cpu_pri = 12;		/* initial PIL for the boot CPU */
281 
282 	/*
283 	 * The kernel doesn't use LDTs unless a process explicitly requests one.
284 	 */
285 	p0.p_ldt_desc = null_sdesc;
286 
287 	/*
288 	 * Initialize thread/cpu microstate accounting
289 	 */
290 	init_mstate(&t0, LMS_SYSTEM);
291 	init_cpu_mstate(CPU, CMS_SYSTEM);
292 
293 	/*
294 	 * Initialize lists of available and active CPUs.
295 	 */
296 	cpu_list_init(CPU);
297 
298 	/*
299 	 * Now that we have taken over the GDT, IDT and have initialized
300 	 * active CPU list it's time to inform kmdb if present.
301 	 */
302 	if (boothowto & RB_DEBUG)
303 		kdi_idt_sync();
304 
305 	/*
306 	 * If requested (boot -d) drop into kmdb.
307 	 *
308 	 * This must be done after cpu_list_init() on the 64-bit kernel
309 	 * since taking a trap requires that we re-compute gsbase based
310 	 * on the cpu list.
311 	 */
312 	if (boothowto & RB_DEBUGENTER)
313 		kmdb_enter();
314 
315 	cpu_vm_data_init(CPU);
316 
317 	/* lgrp_init() needs PCI config space access */
318 #if defined(__xpv)
319 	if (DOMAIN_IS_INITDOMAIN(xen_info))
320 		pci_cfgspace_init();
321 #else
322 	pci_cfgspace_init();
323 #endif
324 
325 	rp->r_fp = 0;	/* terminate kernel stack traces! */
326 
327 	prom_init("kernel", (void *)NULL);
328 
329 	boot_ncpus = bootprop_getval("boot-ncpus");
330 
331 	if (boot_ncpus <= 0 || boot_ncpus > NCPU)
332 		boot_ncpus = NCPU;
333 
334 	max_ncpus = boot_max_ncpus = boot_ncpus;
335 
336 	/*
337 	 * Initialize the lgrp framework
338 	 */
339 	lgrp_init();
340 
341 	if (boothowto & RB_HALT) {
342 		prom_printf("unix: kernel halted by -h flag\n");
343 		prom_enter_mon();
344 	}
345 
346 	ASSERT_STACK_ALIGNED();
347 
348 	/*
349 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
350 	 */
351 	ucode_check(CPU);
352 
353 	if (workaround_errata(CPU) != 0)
354 		panic("critical workaround(s) missing for boot cpu");
355 }
356 
357 
358 void
359 mach_modpath(char *path, const char *filename)
360 {
361 	/*
362 	 * Construct the directory path from the filename.
363 	 */
364 
365 	int len;
366 	char *p;
367 	const char isastr[] = "/amd64";
368 	size_t isalen = strlen(isastr);
369 
370 	if ((p = strrchr(filename, '/')) == NULL)
371 		return;
372 
373 	while (p > filename && *(p - 1) == '/')
374 		p--;	/* remove trailing '/' characters */
375 	if (p == filename)
376 		p++;	/* so "/" -is- the modpath in this case */
377 
378 	/*
379 	 * Remove optional isa-dependent directory name - the module
380 	 * subsystem will put this back again (!)
381 	 */
382 	len = p - filename;
383 	if (len > isalen &&
384 	    strncmp(&filename[len - isalen], isastr, isalen) == 0)
385 		p -= isalen;
386 
387 	/*
388 	 * "/platform/mumblefrotz" + " " + MOD_DEFPATH
389 	 */
390 	len += (p - filename) + 1 + strlen(MOD_DEFPATH) + 1;
391 	(void) strncpy(path, filename, p - filename);
392 }
393