xref: /illumos-gate/usr/src/uts/i86pc/os/machdep.c (revision 737d277a27d4872543f597e35c470e7510f61f03)
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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/t_lock.h>
31 #include <sys/param.h>
32 #include <sys/sysmacros.h>
33 #include <sys/signal.h>
34 #include <sys/systm.h>
35 #include <sys/user.h>
36 #include <sys/mman.h>
37 #include <sys/vm.h>
38 
39 #include <sys/disp.h>
40 #include <sys/class.h>
41 
42 #include <sys/proc.h>
43 #include <sys/buf.h>
44 #include <sys/kmem.h>
45 
46 #include <sys/reboot.h>
47 #include <sys/uadmin.h>
48 #include <sys/callb.h>
49 
50 #include <sys/cred.h>
51 #include <sys/vnode.h>
52 #include <sys/file.h>
53 
54 #include <sys/procfs.h>
55 #include <sys/acct.h>
56 
57 #include <sys/vfs.h>
58 #include <sys/dnlc.h>
59 #include <sys/var.h>
60 #include <sys/cmn_err.h>
61 #include <sys/utsname.h>
62 #include <sys/debug.h>
63 #include <sys/kdi_impl.h>
64 
65 #include <sys/dumphdr.h>
66 #include <sys/bootconf.h>
67 #include <sys/varargs.h>
68 #include <sys/promif.h>
69 #include <sys/modctl.h>
70 
71 #include <sys/consdev.h>
72 #include <sys/frame.h>
73 
74 #include <sys/sunddi.h>
75 #include <sys/ddidmareq.h>
76 #include <sys/psw.h>
77 #include <sys/regset.h>
78 #include <sys/privregs.h>
79 #include <sys/clock.h>
80 #include <sys/tss.h>
81 #include <sys/cpu.h>
82 #include <sys/stack.h>
83 #include <sys/trap.h>
84 #include <sys/pic.h>
85 #include <sys/mmu.h>
86 #include <vm/hat.h>
87 #include <vm/anon.h>
88 #include <vm/as.h>
89 #include <vm/page.h>
90 #include <vm/seg.h>
91 #include <vm/seg_kmem.h>
92 #include <vm/seg_map.h>
93 #include <vm/seg_vn.h>
94 #include <vm/seg_kp.h>
95 #include <vm/hat_i86.h>
96 #include <sys/swap.h>
97 #include <sys/thread.h>
98 #include <sys/sysconf.h>
99 #include <sys/vm_machparam.h>
100 #include <sys/archsystm.h>
101 #include <sys/machsystm.h>
102 #include <sys/machlock.h>
103 #include <sys/x_call.h>
104 #include <sys/instance.h>
105 
106 #include <sys/time.h>
107 #include <sys/smp_impldefs.h>
108 #include <sys/psm_types.h>
109 #include <sys/atomic.h>
110 #include <sys/panic.h>
111 #include <sys/cpuvar.h>
112 #include <sys/dtrace.h>
113 #include <sys/bl.h>
114 #include <sys/nvpair.h>
115 #include <sys/x86_archext.h>
116 #include <sys/pool_pset.h>
117 #include <sys/autoconf.h>
118 #include <sys/kdi.h>
119 
120 #ifdef	TRAPTRACE
121 #include <sys/traptrace.h>
122 #endif	/* TRAPTRACE */
123 
124 #ifdef C2_AUDIT
125 extern void audit_enterprom(int);
126 extern void audit_exitprom(int);
127 #endif
128 
129 /*
130  * The panicbuf array is used to record messages and state:
131  */
132 char panicbuf[PANICBUFSIZE];
133 
134 /*
135  * maxphys - used during physio
136  * klustsize - used for klustering by swapfs and specfs
137  */
138 int maxphys = 56 * 1024;    /* XXX See vm_subr.c - max b_count in physio */
139 int klustsize = 56 * 1024;
140 
141 caddr_t	p0_va;		/* Virtual address for accessing physical page 0 */
142 
143 /*
144  * defined here, though unused on x86,
145  * to make kstat_fr.c happy.
146  */
147 int vac;
148 
149 void stop_other_cpus();
150 void debug_enter(char *);
151 
152 int	procset = 1;
153 
154 /*
155  * Flags set by mdboot if we're panicking and we invoke mdboot on a CPU which
156  * is not the boot CPU.  When set, panic_idle() on the boot CPU will invoke
157  * mdboot with the corresponding arguments.
158  */
159 
160 #define	BOOT_WAIT	-1		/* Flag indicating we should idle */
161 
162 volatile int cpu_boot_cmd = BOOT_WAIT;
163 volatile int cpu_boot_fcn = BOOT_WAIT;
164 
165 extern void pm_cfb_check_and_powerup(void);
166 extern void pm_cfb_rele(void);
167 
168 /*
169  * Machine dependent code to reboot.
170  * "mdep" is interpreted as a character pointer; if non-null, it is a pointer
171  * to a string to be used as the argument string when rebooting.
172  *
173  * "invoke_cb" is a boolean. It is set to true when mdboot() can safely
174  * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when
175  * we are in a normal shutdown sequence (interrupts are not blocked, the
176  * system is not panic'ing or being suspended).
177  */
178 /*ARGSUSED*/
179 void
180 mdboot(int cmd, int fcn, char *mdep, boolean_t invoke_cb)
181 {
182 	extern void mtrr_resync(void);
183 
184 	/*
185 	 * The PSMI guarantees the implementor of psm_shutdown that it will
186 	 * only be called on the boot CPU.  This was needed by Corollary
187 	 * because the hardware does not allow other CPUs to reset the
188 	 * boot CPU.  So before rebooting, we switch over to the boot CPU.
189 	 * If we are panicking, the other CPUs are at high spl spinning in
190 	 * panic_idle(), so we set the cpu_boot_* variables and wait for
191 	 * the boot CPU to re-invoke mdboot() for us.
192 	 */
193 	if (!panicstr) {
194 		kpreempt_disable();
195 		affinity_set(getbootcpuid());
196 	} else if (CPU->cpu_id != getbootcpuid()) {
197 		cpu_boot_cmd = cmd;
198 		cpu_boot_fcn = fcn;
199 		for (;;);
200 	}
201 
202 	/*
203 	 * XXX - rconsvp is set to NULL to ensure that output messages
204 	 * are sent to the underlying "hardware" device using the
205 	 * monitor's printf routine since we are in the process of
206 	 * either rebooting or halting the machine.
207 	 */
208 	rconsvp = NULL;
209 
210 	/*
211 	 * Print the reboot message now, before pausing other cpus.
212 	 * There is a race condition in the printing support that
213 	 * can deadlock multiprocessor machines.
214 	 */
215 	if (!(fcn == AD_HALT || fcn == AD_POWEROFF))
216 		prom_printf("rebooting...\n");
217 
218 	/*
219 	 * We can't bring up the console from above lock level, so do it now
220 	 */
221 	pm_cfb_check_and_powerup();
222 
223 	/* make sure there are no more changes to the device tree */
224 	devtree_freeze();
225 
226 	if (invoke_cb)
227 		(void) callb_execute_class(CB_CL_MDBOOT, NULL);
228 
229 	page_retire_hunt(page_retire_mdboot_cb);
230 
231 	/*
232 	 * stop other cpus and raise our priority.  since there is only
233 	 * one active cpu after this, and our priority will be too high
234 	 * for us to be preempted, we're essentially single threaded
235 	 * from here on out.
236 	 */
237 	(void) spl6();
238 	if (!panicstr) {
239 		mutex_enter(&cpu_lock);
240 		pause_cpus(NULL);
241 		mutex_exit(&cpu_lock);
242 	}
243 
244 	/*
245 	 * try and reset leaf devices.  reset_leaves() should only
246 	 * be called when there are no other threads that could be
247 	 * accessing devices
248 	 */
249 	reset_leaves();
250 
251 	(void) spl8();
252 	(*psm_shutdownf)(cmd, fcn);
253 
254 	mtrr_resync();
255 
256 	if (fcn == AD_HALT || fcn == AD_POWEROFF)
257 		halt((char *)NULL);
258 	else
259 		prom_reboot("");
260 	/*NOTREACHED*/
261 }
262 
263 /* mdpreboot - may be called prior to mdboot while root fs still mounted */
264 /*ARGSUSED*/
265 void
266 mdpreboot(int cmd, int fcn, char *mdep)
267 {
268 	(*psm_preshutdownf)(cmd, fcn);
269 }
270 
271 void
272 idle_other_cpus()
273 {
274 	int cpuid = CPU->cpu_id;
275 	cpuset_t xcset;
276 
277 	ASSERT(cpuid < NCPU);
278 	CPUSET_ALL_BUT(xcset, cpuid);
279 	xc_capture_cpus(xcset);
280 }
281 
282 void
283 resume_other_cpus()
284 {
285 	ASSERT(CPU->cpu_id < NCPU);
286 
287 	xc_release_cpus();
288 }
289 
290 extern void	mp_halt(char *);
291 
292 void
293 stop_other_cpus()
294 {
295 	int cpuid = CPU->cpu_id;
296 	cpuset_t xcset;
297 
298 	ASSERT(cpuid < NCPU);
299 
300 	/*
301 	 * xc_trycall will attempt to make all other CPUs execute mp_halt,
302 	 * and will return immediately regardless of whether or not it was
303 	 * able to make them do it.
304 	 */
305 	CPUSET_ALL_BUT(xcset, cpuid);
306 	xc_trycall(NULL, NULL, NULL, xcset, (int (*)())mp_halt);
307 }
308 
309 /*
310  *	Machine dependent abort sequence handling
311  */
312 void
313 abort_sequence_enter(char *msg)
314 {
315 	if (abort_enable == 0) {
316 #ifdef C2_AUDIT
317 		if (audit_active)
318 			audit_enterprom(0);
319 #endif /* C2_AUDIT */
320 		return;
321 	}
322 #ifdef C2_AUDIT
323 	if (audit_active)
324 		audit_enterprom(1);
325 #endif /* C2_AUDIT */
326 	debug_enter(msg);
327 #ifdef C2_AUDIT
328 	if (audit_active)
329 		audit_exitprom(1);
330 #endif /* C2_AUDIT */
331 }
332 
333 /*
334  * Enter debugger.  Called when the user types ctrl-alt-d or whenever
335  * code wants to enter the debugger and possibly resume later.
336  */
337 void
338 debug_enter(
339 	char	*msg)		/* message to print, possibly NULL */
340 {
341 	if (dtrace_debugger_init != NULL)
342 		(*dtrace_debugger_init)();
343 
344 	if (msg)
345 		prom_printf("%s\n", msg);
346 
347 	if (boothowto & RB_DEBUG)
348 		kdi_dvec_enter();
349 
350 	if (dtrace_debugger_fini != NULL)
351 		(*dtrace_debugger_fini)();
352 }
353 
354 void
355 reset(void)
356 {
357 	ushort_t *bios_memchk;
358 
359 	/*
360 	 * Can't use psm_map_phys before the hat is initialized.
361 	 */
362 	if (khat_running) {
363 		bios_memchk = (ushort_t *)psm_map_phys(0x472,
364 		    sizeof (ushort_t), PROT_READ | PROT_WRITE);
365 		if (bios_memchk)
366 			*bios_memchk = 0x1234;	/* bios memory check disable */
367 	}
368 
369 	pc_reset();
370 	/*NOTREACHED*/
371 }
372 
373 /*
374  * Halt the machine and return to the monitor
375  */
376 void
377 halt(char *s)
378 {
379 	stop_other_cpus();	/* send stop signal to other CPUs */
380 	if (s)
381 		prom_printf("(%s) \n", s);
382 	prom_exit_to_mon();
383 	/*NOTREACHED*/
384 }
385 
386 /*
387  * Enter monitor.  Called via cross-call from stop_other_cpus().
388  */
389 void
390 mp_halt(char *msg)
391 {
392 	if (msg)
393 		prom_printf("%s\n", msg);
394 
395 	/*CONSTANTCONDITION*/
396 	while (1)
397 		;
398 }
399 
400 /*
401  * Initiate interrupt redistribution.
402  */
403 void
404 i_ddi_intr_redist_all_cpus()
405 {
406 }
407 
408 /*
409  * XXX These probably ought to live somewhere else
410  * XXX They are called from mem.c
411  */
412 
413 /*
414  * Convert page frame number to an OBMEM page frame number
415  * (i.e. put in the type bits -- zero for this implementation)
416  */
417 pfn_t
418 impl_obmem_pfnum(pfn_t pf)
419 {
420 	return (pf);
421 }
422 
423 #ifdef	NM_DEBUG
424 int nmi_test = 0;	/* checked in intentry.s during clock int */
425 int nmtest = -1;
426 nmfunc1(arg, rp)
427 int	arg;
428 struct regs *rp;
429 {
430 	printf("nmi called with arg = %x, regs = %x\n", arg, rp);
431 	nmtest += 50;
432 	if (arg == nmtest) {
433 		printf("ip = %x\n", rp->r_pc);
434 		return (1);
435 	}
436 	return (0);
437 }
438 
439 #endif
440 
441 #include <sys/bootsvcs.h>
442 
443 /* Hacked up initialization for initial kernel check out is HERE. */
444 /* The basic steps are: */
445 /*	kernel bootfuncs definition/initialization for KADB */
446 /*	kadb bootfuncs pointer initialization */
447 /*	putchar/getchar (interrupts disabled) */
448 
449 /* kadb bootfuncs pointer initialization */
450 
451 int
452 sysp_getchar()
453 {
454 	int i;
455 	int s;
456 
457 	if (cons_polledio == NULL) {
458 		/* Uh oh */
459 		prom_printf("getchar called with no console\n");
460 		for (;;)
461 			/* LOOP FOREVER */;
462 	}
463 
464 	s = clear_int_flag();
465 	i = cons_polledio->cons_polledio_getchar(
466 		cons_polledio->cons_polledio_argument);
467 	restore_int_flag(s);
468 	return (i);
469 }
470 
471 void
472 sysp_putchar(int c)
473 {
474 	int s;
475 
476 	/*
477 	 * We have no alternative but to drop the output on the floor.
478 	 */
479 	if (cons_polledio == NULL ||
480 	    cons_polledio->cons_polledio_putchar == NULL)
481 		return;
482 
483 	s = clear_int_flag();
484 	cons_polledio->cons_polledio_putchar(
485 		cons_polledio->cons_polledio_argument, c);
486 	restore_int_flag(s);
487 }
488 
489 int
490 sysp_ischar()
491 {
492 	int i;
493 	int s;
494 
495 	if (cons_polledio == NULL ||
496 	    cons_polledio->cons_polledio_ischar == NULL)
497 		return (0);
498 
499 	s = clear_int_flag();
500 	i = cons_polledio->cons_polledio_ischar(
501 		cons_polledio->cons_polledio_argument);
502 	restore_int_flag(s);
503 	return (i);
504 }
505 
506 int
507 goany(void)
508 {
509 	prom_printf("Type any key to continue ");
510 	(void) prom_getchar();
511 	prom_printf("\n");
512 	return (1);
513 }
514 
515 static struct boot_syscalls kern_sysp = {
516 	sysp_getchar,	/*	unchar	(*getchar)();	7  */
517 	sysp_putchar,	/*	int	(*putchar)();	8  */
518 	sysp_ischar,	/*	int	(*ischar)();	9  */
519 };
520 
521 void
522 kadb_uses_kernel()
523 {
524 	/*
525 	 * This routine is now totally misnamed, since it does not in fact
526 	 * control kadb's I/O; it only controls the kernel's prom_* I/O.
527 	 */
528 	sysp = &kern_sysp;
529 }
530 
531 /*
532  *	the interface to the outside world
533  */
534 
535 /*
536  * poll_port -- wait for a register to achieve a
537  *		specific state.  Arguments are a mask of bits we care about,
538  *		and two sub-masks.  To return normally, all the bits in the
539  *		first sub-mask must be ON, all the bits in the second sub-
540  *		mask must be OFF.  If about seconds pass without the register
541  *		achieving the desired bit configuration, we return 1, else
542  *		0.
543  */
544 int
545 poll_port(ushort_t port, ushort_t mask, ushort_t onbits, ushort_t offbits)
546 {
547 	int i;
548 	ushort_t maskval;
549 
550 	for (i = 500000; i; i--) {
551 		maskval = inb(port) & mask;
552 		if (((maskval & onbits) == onbits) &&
553 			((maskval & offbits) == 0))
554 			return (0);
555 		drv_usecwait(10);
556 	}
557 	return (1);
558 }
559 
560 /*
561  * set_idle_cpu is called from idle() when a CPU becomes idle.
562  */
563 /*LINTED: static unused */
564 static uint_t last_idle_cpu;
565 
566 /*ARGSUSED*/
567 void
568 set_idle_cpu(int cpun)
569 {
570 	last_idle_cpu = cpun;
571 	(*psm_set_idle_cpuf)(cpun);
572 }
573 
574 /*
575  * unset_idle_cpu is called from idle() when a CPU is no longer idle.
576  */
577 /*ARGSUSED*/
578 void
579 unset_idle_cpu(int cpun)
580 {
581 	(*psm_unset_idle_cpuf)(cpun);
582 }
583 
584 /*
585  * This routine is almost correct now, but not quite.  It still needs the
586  * equivalent concept of "hres_last_tick", just like on the sparc side.
587  * The idea is to take a snapshot of the hi-res timer while doing the
588  * hrestime_adj updates under hres_lock in locore, so that the small
589  * interval between interrupt assertion and interrupt processing is
590  * accounted for correctly.  Once we have this, the code below should
591  * be modified to subtract off hres_last_tick rather than hrtime_base.
592  *
593  * I'd have done this myself, but I don't have source to all of the
594  * vendor-specific hi-res timer routines (grrr...).  The generic hook I
595  * need is something like "gethrtime_unlocked()", which would be just like
596  * gethrtime() but would assume that you're already holding CLOCK_LOCK().
597  * This is what the GET_HRTIME() macro is for on sparc (although it also
598  * serves the function of making time available without a function call
599  * so you don't take a register window overflow while traps are disabled).
600  */
601 void
602 pc_gethrestime(timestruc_t *tp)
603 {
604 	int lock_prev;
605 	timestruc_t now;
606 	int nslt;		/* nsec since last tick */
607 	int adj;		/* amount of adjustment to apply */
608 
609 loop:
610 	lock_prev = hres_lock;
611 	now = hrestime;
612 	nslt = (int)(gethrtime() - hres_last_tick);
613 	if (nslt < 0) {
614 		/*
615 		 * nslt < 0 means a tick came between sampling
616 		 * gethrtime() and hres_last_tick; restart the loop
617 		 */
618 
619 		goto loop;
620 	}
621 	now.tv_nsec += nslt;
622 	if (hrestime_adj != 0) {
623 		if (hrestime_adj > 0) {
624 			adj = (nslt >> ADJ_SHIFT);
625 			if (adj > hrestime_adj)
626 				adj = (int)hrestime_adj;
627 		} else {
628 			adj = -(nslt >> ADJ_SHIFT);
629 			if (adj < hrestime_adj)
630 				adj = (int)hrestime_adj;
631 		}
632 		now.tv_nsec += adj;
633 	}
634 	while ((unsigned long)now.tv_nsec >= NANOSEC) {
635 
636 		/*
637 		 * We might have a large adjustment or have been in the
638 		 * debugger for a long time; take care of (at most) four
639 		 * of those missed seconds (tv_nsec is 32 bits, so
640 		 * anything >4s will be wrapping around).  However,
641 		 * anything more than 2 seconds out of sync will trigger
642 		 * timedelta from clock() to go correct the time anyway,
643 		 * so do what we can, and let the big crowbar do the
644 		 * rest.  A similar correction while loop exists inside
645 		 * hres_tick(); in all cases we'd like tv_nsec to
646 		 * satisfy 0 <= tv_nsec < NANOSEC to avoid confusing
647 		 * user processes, but if tv_sec's a little behind for a
648 		 * little while, that's OK; time still monotonically
649 		 * increases.
650 		 */
651 
652 		now.tv_nsec -= NANOSEC;
653 		now.tv_sec++;
654 	}
655 	if ((hres_lock & ~1) != lock_prev)
656 		goto loop;
657 
658 	*tp = now;
659 }
660 
661 void
662 gethrestime_lasttick(timespec_t *tp)
663 {
664 	int s;
665 
666 	s = hr_clock_lock();
667 	*tp = hrestime;
668 	hr_clock_unlock(s);
669 }
670 
671 time_t
672 gethrestime_sec(void)
673 {
674 	timestruc_t now;
675 
676 	gethrestime(&now);
677 	return (now.tv_sec);
678 }
679 
680 /*
681  * Initialize a kernel thread's stack
682  */
683 
684 caddr_t
685 thread_stk_init(caddr_t stk)
686 {
687 	ASSERT(((uintptr_t)stk & (STACK_ALIGN - 1)) == 0);
688 	return (stk - SA(MINFRAME));
689 }
690 
691 /*
692  * Initialize lwp's kernel stack.
693  */
694 
695 #ifdef TRAPTRACE
696 /*
697  * There's a tricky interdependency here between use of sysenter and
698  * TRAPTRACE which needs recording to avoid future confusion (this is
699  * about the third time I've re-figured this out ..)
700  *
701  * Here's how debugging lcall works with TRAPTRACE.
702  *
703  * 1 We're in userland with a breakpoint on the lcall instruction.
704  * 2 We execute the instruction - the instruction pushes the userland
705  *   %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel
706  *   via the call gate.
707  * 3 The hardware raises a debug trap in kernel mode, the hardware
708  *   pushes %efl, %cs, %eip and gets to dbgtrap via the idt.
709  * 4 dbgtrap pushes the error code and trapno and calls cmntrap
710  * 5 cmntrap finishes building a trap frame
711  * 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk
712  *   off the stack into the traptrace buffer.
713  *
714  * This means that the traptrace buffer contains the wrong values in
715  * %esp and %ss, but everything else in there is correct.
716  *
717  * Here's how debugging sysenter works with TRAPTRACE.
718  *
719  * a We're in userland with a breakpoint on the sysenter instruction.
720  * b We execute the instruction - the instruction pushes -nothing-
721  *   on the stack, but sets %cs, %eip, %ss, %esp to prearranged
722  *   values to take us to sys_sysenter, at the top of the lwp's
723  *   stack.
724  * c goto 3
725  *
726  * At this point, because we got into the kernel without the requisite
727  * five pushes on the stack, if we didn't make extra room, we'd
728  * end up with the TRACE_REGS macro fetching the saved %ss and %esp
729  * values from negative (unmapped) stack addresses -- which really bites.
730  * That's why we do the '-= 8' below.
731  *
732  * XXX	Note that reading "up" lwp0's stack works because t0 is declared
733  *	right next to t0stack in locore.s
734  */
735 #endif
736 
737 caddr_t
738 lwp_stk_init(klwp_t *lwp, caddr_t stk)
739 {
740 	caddr_t oldstk;
741 	struct pcb *pcb = &lwp->lwp_pcb;
742 
743 	oldstk = stk;
744 	stk -= SA(sizeof (struct regs) + SA(MINFRAME));
745 #ifdef TRAPTRACE
746 	stk -= 2 * sizeof (greg_t); /* space for phony %ss:%sp (see above) */
747 #endif
748 	stk = (caddr_t)((uintptr_t)stk & ~(STACK_ALIGN - 1ul));
749 	bzero(stk, oldstk - stk);
750 	lwp->lwp_regs = (void *)(stk + SA(MINFRAME));
751 
752 	/*
753 	 * Arrange that the virtualized %fs and %gs GDT descriptors
754 	 * have a well-defined initial state (present, ring 3
755 	 * and of type data).
756 	 */
757 #if defined(__amd64)
758 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
759 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
760 	else
761 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
762 #elif defined(__i386)
763 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
764 #endif	/* __i386 */
765 	lwp_installctx(lwp);
766 	return (stk);
767 }
768 
769 /*ARGSUSED*/
770 void
771 lwp_stk_fini(klwp_t *lwp)
772 {}
773 
774 /*
775  * If we're not the panic CPU, we wait in panic_idle for reboot.  If we're
776  * the boot CPU, then we are responsible for actually doing the reboot, so
777  * we watch for cpu_boot_cmd to be set.
778  */
779 static void
780 panic_idle(void)
781 {
782 	splx(ipltospl(CLOCK_LEVEL));
783 	(void) setjmp(&curthread->t_pcb);
784 
785 	if (CPU->cpu_id == getbootcpuid()) {
786 		while (cpu_boot_cmd == BOOT_WAIT || cpu_boot_fcn == BOOT_WAIT)
787 			drv_usecwait(10);
788 
789 		mdboot(cpu_boot_cmd, cpu_boot_fcn, NULL, B_FALSE);
790 	}
791 
792 	for (;;);
793 }
794 
795 /*
796  * Stop the other CPUs by cross-calling them and forcing them to enter
797  * the panic_idle() loop above.
798  */
799 /*ARGSUSED*/
800 void
801 panic_stopcpus(cpu_t *cp, kthread_t *t, int spl)
802 {
803 	processorid_t i;
804 	cpuset_t xcset;
805 
806 	(void) splzs();
807 
808 	CPUSET_ALL_BUT(xcset, cp->cpu_id);
809 	xc_trycall(NULL, NULL, NULL, xcset, (int (*)())panic_idle);
810 
811 	for (i = 0; i < NCPU; i++) {
812 		if (i != cp->cpu_id && cpu[i] != NULL &&
813 		    (cpu[i]->cpu_flags & CPU_EXISTS))
814 			cpu[i]->cpu_flags |= CPU_QUIESCED;
815 	}
816 }
817 
818 /*
819  * Platform callback following each entry to panicsys().
820  */
821 /*ARGSUSED*/
822 void
823 panic_enter_hw(int spl)
824 {
825 	/* Nothing to do here */
826 }
827 
828 /*
829  * Platform-specific code to execute after panicstr is set: we invoke
830  * the PSM entry point to indicate that a panic has occurred.
831  */
832 /*ARGSUSED*/
833 void
834 panic_quiesce_hw(panic_data_t *pdp)
835 {
836 	psm_notifyf(PSM_PANIC_ENTER);
837 
838 #ifdef	TRAPTRACE
839 	/*
840 	 * Turn off TRAPTRACE
841 	 */
842 	TRAPTRACE_FREEZE;
843 #endif	/* TRAPTRACE */
844 }
845 
846 /*
847  * Platform callback prior to writing crash dump.
848  */
849 /*ARGSUSED*/
850 void
851 panic_dump_hw(int spl)
852 {
853 	/* Nothing to do here */
854 }
855 
856 /*ARGSUSED*/
857 void
858 plat_tod_fault(enum tod_fault_type tod_bad)
859 {
860 }
861 
862 /*ARGSUSED*/
863 int
864 blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
865 {
866 	return (ENOTSUP);
867 }
868 
869 /*
870  * The underlying console output routines are protected by raising IPL in case
871  * we are still calling into the early boot services.  Once we start calling
872  * the kernel console emulator, it will disable interrupts completely during
873  * character rendering (see sysp_putchar, for example).  Refer to the comments
874  * and code in common/os/console.c for more information on these callbacks.
875  */
876 /*ARGSUSED*/
877 int
878 console_enter(int busy)
879 {
880 	return (splzs());
881 }
882 
883 /*ARGSUSED*/
884 void
885 console_exit(int busy, int spl)
886 {
887 	splx(spl);
888 }
889 
890 /*
891  * Allocate a region of virtual address space, unmapped.
892  * Stubbed out except on sparc, at least for now.
893  */
894 /*ARGSUSED*/
895 void *
896 boot_virt_alloc(void *addr, size_t size)
897 {
898 	return (addr);
899 }
900 
901 volatile unsigned long	tenmicrodata;
902 
903 void
904 tenmicrosec(void)
905 {
906 	extern int	tsc_gethrtime_initted;
907 	int		i;
908 
909 	if (tsc_gethrtime_initted) {
910 		hrtime_t start, end;
911 		start = end =  gethrtime();
912 		while ((end - start) < (10 * (NANOSEC / MICROSEC))) {
913 			SMT_PAUSE();
914 			end = gethrtime();
915 		}
916 	} else {
917 		/*
918 		 * Artificial loop to induce delay.
919 		 */
920 		for (i = 0; i < microdata; i++)
921 			tenmicrodata = microdata;
922 	}
923 }
924 
925 /*
926  * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
927  * long, and it fills in the array with the time spent on cpu in
928  * each of the mstates, where time is returned in nsec.
929  *
930  * No guarantee is made that the returned values in times[] will
931  * monotonically increase on sequential calls, although this will
932  * be true in the long run. Any such guarantee must be handled by
933  * the caller, if needed. This can happen if we fail to account
934  * for elapsed time due to a generation counter conflict, yet we
935  * did account for it on a prior call (see below).
936  *
937  * The complication is that the cpu in question may be updating
938  * its microstate at the same time that we are reading it.
939  * Because the microstate is only updated when the CPU's state
940  * changes, the values in cpu_intracct[] can be indefinitely out
941  * of date. To determine true current values, it is necessary to
942  * compare the current time with cpu_mstate_start, and add the
943  * difference to times[cpu_mstate].
944  *
945  * This can be a problem if those values are changing out from
946  * under us. Because the code path in new_cpu_mstate() is
947  * performance critical, we have not added a lock to it. Instead,
948  * we have added a generation counter. Before beginning
949  * modifications, the counter is set to 0. After modifications,
950  * it is set to the old value plus one.
951  *
952  * get_cpu_mstate() will not consider the values of cpu_mstate
953  * and cpu_mstate_start to be usable unless the value of
954  * cpu_mstate_gen is both non-zero and unchanged, both before and
955  * after reading the mstate information. Note that we must
956  * protect against out-of-order loads around accesses to the
957  * generation counter. Also, this is a best effort approach in
958  * that we do not retry should the counter be found to have
959  * changed.
960  *
961  * cpu_intracct[] is used to identify time spent in each CPU
962  * mstate while handling interrupts. Such time should be reported
963  * against system time, and so is subtracted out from its
964  * corresponding cpu_acct[] time and added to
965  * cpu_acct[CMS_SYSTEM].
966  */
967 
968 void
969 get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
970 {
971 	int i;
972 	hrtime_t now, start;
973 	uint16_t gen;
974 	uint16_t state;
975 	hrtime_t intracct[NCMSTATES];
976 
977 	/*
978 	 * Load all volatile state under the protection of membar.
979 	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
980 	 * of (now - cpu_mstate_start) by a change in CPU mstate that
981 	 * arrives after we make our last check of cpu_mstate_gen.
982 	 */
983 
984 	now = gethrtime_unscaled();
985 	gen = cpu->cpu_mstate_gen;
986 
987 	membar_consumer();	/* guarantee load ordering */
988 	start = cpu->cpu_mstate_start;
989 	state = cpu->cpu_mstate;
990 	for (i = 0; i < NCMSTATES; i++) {
991 		intracct[i] = cpu->cpu_intracct[i];
992 		times[i] = cpu->cpu_acct[i];
993 	}
994 	membar_consumer();	/* guarantee load ordering */
995 
996 	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
997 		times[state] += now - start;
998 
999 	for (i = 0; i < NCMSTATES; i++) {
1000 		if (i == CMS_SYSTEM)
1001 			continue;
1002 		times[i] -= intracct[i];
1003 		if (times[i] < 0) {
1004 			intracct[i] += times[i];
1005 			times[i] = 0;
1006 		}
1007 		times[CMS_SYSTEM] += intracct[i];
1008 		scalehrtime(&times[i]);
1009 	}
1010 	scalehrtime(&times[CMS_SYSTEM]);
1011 }
1012