xref: /illumos-gate/usr/src/uts/i86pc/os/machdep.c (revision 55381082)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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 		return;
481 
482 	s = clear_int_flag();
483 	cons_polledio->cons_polledio_putchar(
484 		cons_polledio->cons_polledio_argument, c);
485 	restore_int_flag(s);
486 }
487 
488 int
489 sysp_ischar()
490 {
491 	int i;
492 	int s;
493 
494 	if (cons_polledio == NULL)
495 		return (0);
496 
497 	s = clear_int_flag();
498 	i = cons_polledio->cons_polledio_ischar(
499 		cons_polledio->cons_polledio_argument);
500 	restore_int_flag(s);
501 	return (i);
502 }
503 
504 int
505 goany(void)
506 {
507 	prom_printf("Type any key to continue ");
508 	(void) prom_getchar();
509 	prom_printf("\n");
510 	return (1);
511 }
512 
513 static struct boot_syscalls kern_sysp = {
514 	sysp_getchar,	/*	unchar	(*getchar)();	7  */
515 	sysp_putchar,	/*	int	(*putchar)();	8  */
516 	sysp_ischar,	/*	int	(*ischar)();	9  */
517 };
518 
519 void
520 kadb_uses_kernel()
521 {
522 	/*
523 	 * This routine is now totally misnamed, since it does not in fact
524 	 * control kadb's I/O; it only controls the kernel's prom_* I/O.
525 	 */
526 	sysp = &kern_sysp;
527 }
528 
529 /*
530  *	the interface to the outside world
531  */
532 
533 /*
534  * poll_port -- wait for a register to achieve a
535  *		specific state.  Arguments are a mask of bits we care about,
536  *		and two sub-masks.  To return normally, all the bits in the
537  *		first sub-mask must be ON, all the bits in the second sub-
538  *		mask must be OFF.  If about seconds pass without the register
539  *		achieving the desired bit configuration, we return 1, else
540  *		0.
541  */
542 int
543 poll_port(ushort_t port, ushort_t mask, ushort_t onbits, ushort_t offbits)
544 {
545 	int i;
546 	ushort_t maskval;
547 
548 	for (i = 500000; i; i--) {
549 		maskval = inb(port) & mask;
550 		if (((maskval & onbits) == onbits) &&
551 			((maskval & offbits) == 0))
552 			return (0);
553 		drv_usecwait(10);
554 	}
555 	return (1);
556 }
557 
558 /*
559  * set_idle_cpu is called from idle() when a CPU becomes idle.
560  */
561 /*LINTED: static unused */
562 static uint_t last_idle_cpu;
563 
564 /*ARGSUSED*/
565 void
566 set_idle_cpu(int cpun)
567 {
568 	last_idle_cpu = cpun;
569 	(*psm_set_idle_cpuf)(cpun);
570 }
571 
572 /*
573  * unset_idle_cpu is called from idle() when a CPU is no longer idle.
574  */
575 /*ARGSUSED*/
576 void
577 unset_idle_cpu(int cpun)
578 {
579 	(*psm_unset_idle_cpuf)(cpun);
580 }
581 
582 /*
583  * This routine is almost correct now, but not quite.  It still needs the
584  * equivalent concept of "hres_last_tick", just like on the sparc side.
585  * The idea is to take a snapshot of the hi-res timer while doing the
586  * hrestime_adj updates under hres_lock in locore, so that the small
587  * interval between interrupt assertion and interrupt processing is
588  * accounted for correctly.  Once we have this, the code below should
589  * be modified to subtract off hres_last_tick rather than hrtime_base.
590  *
591  * I'd have done this myself, but I don't have source to all of the
592  * vendor-specific hi-res timer routines (grrr...).  The generic hook I
593  * need is something like "gethrtime_unlocked()", which would be just like
594  * gethrtime() but would assume that you're already holding CLOCK_LOCK().
595  * This is what the GET_HRTIME() macro is for on sparc (although it also
596  * serves the function of making time available without a function call
597  * so you don't take a register window overflow while traps are disabled).
598  */
599 void
600 pc_gethrestime(timestruc_t *tp)
601 {
602 	int lock_prev;
603 	timestruc_t now;
604 	int nslt;		/* nsec since last tick */
605 	int adj;		/* amount of adjustment to apply */
606 
607 loop:
608 	lock_prev = hres_lock;
609 	now = hrestime;
610 	nslt = (int)(gethrtime() - hres_last_tick);
611 	if (nslt < 0) {
612 		/*
613 		 * nslt < 0 means a tick came between sampling
614 		 * gethrtime() and hres_last_tick; restart the loop
615 		 */
616 
617 		goto loop;
618 	}
619 	now.tv_nsec += nslt;
620 	if (hrestime_adj != 0) {
621 		if (hrestime_adj > 0) {
622 			adj = (nslt >> ADJ_SHIFT);
623 			if (adj > hrestime_adj)
624 				adj = (int)hrestime_adj;
625 		} else {
626 			adj = -(nslt >> ADJ_SHIFT);
627 			if (adj < hrestime_adj)
628 				adj = (int)hrestime_adj;
629 		}
630 		now.tv_nsec += adj;
631 	}
632 	while ((unsigned long)now.tv_nsec >= NANOSEC) {
633 
634 		/*
635 		 * We might have a large adjustment or have been in the
636 		 * debugger for a long time; take care of (at most) four
637 		 * of those missed seconds (tv_nsec is 32 bits, so
638 		 * anything >4s will be wrapping around).  However,
639 		 * anything more than 2 seconds out of sync will trigger
640 		 * timedelta from clock() to go correct the time anyway,
641 		 * so do what we can, and let the big crowbar do the
642 		 * rest.  A similar correction while loop exists inside
643 		 * hres_tick(); in all cases we'd like tv_nsec to
644 		 * satisfy 0 <= tv_nsec < NANOSEC to avoid confusing
645 		 * user processes, but if tv_sec's a little behind for a
646 		 * little while, that's OK; time still monotonically
647 		 * increases.
648 		 */
649 
650 		now.tv_nsec -= NANOSEC;
651 		now.tv_sec++;
652 	}
653 	if ((hres_lock & ~1) != lock_prev)
654 		goto loop;
655 
656 	*tp = now;
657 }
658 
659 void
660 gethrestime_lasttick(timespec_t *tp)
661 {
662 	int s;
663 
664 	s = hr_clock_lock();
665 	*tp = hrestime;
666 	hr_clock_unlock(s);
667 }
668 
669 time_t
670 gethrestime_sec(void)
671 {
672 	timestruc_t now;
673 
674 	gethrestime(&now);
675 	return (now.tv_sec);
676 }
677 
678 /*
679  * Initialize a kernel thread's stack
680  */
681 
682 caddr_t
683 thread_stk_init(caddr_t stk)
684 {
685 	ASSERT(((uintptr_t)stk & (STACK_ALIGN - 1)) == 0);
686 	return (stk - SA(MINFRAME));
687 }
688 
689 /*
690  * Initialize lwp's kernel stack.
691  */
692 
693 #ifdef TRAPTRACE
694 /*
695  * There's a tricky interdependency here between use of sysenter and
696  * TRAPTRACE which needs recording to avoid future confusion (this is
697  * about the third time I've re-figured this out ..)
698  *
699  * Here's how debugging lcall works with TRAPTRACE.
700  *
701  * 1 We're in userland with a breakpoint on the lcall instruction.
702  * 2 We execute the instruction - the instruction pushes the userland
703  *   %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel
704  *   via the call gate.
705  * 3 The hardware raises a debug trap in kernel mode, the hardware
706  *   pushes %efl, %cs, %eip and gets to dbgtrap via the idt.
707  * 4 dbgtrap pushes the error code and trapno and calls cmntrap
708  * 5 cmntrap finishes building a trap frame
709  * 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk
710  *   off the stack into the traptrace buffer.
711  *
712  * This means that the traptrace buffer contains the wrong values in
713  * %esp and %ss, but everything else in there is correct.
714  *
715  * Here's how debugging sysenter works with TRAPTRACE.
716  *
717  * a We're in userland with a breakpoint on the sysenter instruction.
718  * b We execute the instruction - the instruction pushes -nothing-
719  *   on the stack, but sets %cs, %eip, %ss, %esp to prearranged
720  *   values to take us to sys_sysenter, at the top of the lwp's
721  *   stack.
722  * c goto 3
723  *
724  * At this point, because we got into the kernel without the requisite
725  * five pushes on the stack, if we didn't make extra room, we'd
726  * end up with the TRACE_REGS macro fetching the saved %ss and %esp
727  * values from negative (unmapped) stack addresses -- which really bites.
728  * That's why we do the '-= 8' below.
729  *
730  * XXX	Note that reading "up" lwp0's stack works because t0 is declared
731  *	right next to t0stack in locore.s
732  */
733 #endif
734 
735 caddr_t
736 lwp_stk_init(klwp_t *lwp, caddr_t stk)
737 {
738 	caddr_t oldstk;
739 	struct pcb *pcb = &lwp->lwp_pcb;
740 
741 	oldstk = stk;
742 	stk -= SA(sizeof (struct regs) + SA(MINFRAME));
743 #ifdef TRAPTRACE
744 	stk -= 2 * sizeof (greg_t); /* space for phony %ss:%sp (see above) */
745 #endif
746 	stk = (caddr_t)((uintptr_t)stk & ~(STACK_ALIGN - 1ul));
747 	bzero(stk, oldstk - stk);
748 	lwp->lwp_regs = (void *)(stk + SA(MINFRAME));
749 
750 	/*
751 	 * Arrange that the virtualized %fs and %gs GDT descriptors
752 	 * have a well-defined initial state (present, ring 3
753 	 * and of type data).
754 	 */
755 #if defined(__amd64)
756 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
757 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
758 	else
759 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
760 #elif defined(__i386)
761 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
762 #endif	/* __i386 */
763 	lwp_installctx(lwp);
764 	return (stk);
765 }
766 
767 /*ARGSUSED*/
768 void
769 lwp_stk_fini(klwp_t *lwp)
770 {}
771 
772 /*
773  * If we're not the panic CPU, we wait in panic_idle for reboot.  If we're
774  * the boot CPU, then we are responsible for actually doing the reboot, so
775  * we watch for cpu_boot_cmd to be set.
776  */
777 static void
778 panic_idle(void)
779 {
780 	splx(ipltospl(CLOCK_LEVEL));
781 	(void) setjmp(&curthread->t_pcb);
782 
783 	if (CPU->cpu_id == getbootcpuid()) {
784 		while (cpu_boot_cmd == BOOT_WAIT || cpu_boot_fcn == BOOT_WAIT)
785 			drv_usecwait(10);
786 
787 		mdboot(cpu_boot_cmd, cpu_boot_fcn, NULL, B_FALSE);
788 	}
789 
790 	for (;;);
791 }
792 
793 /*
794  * Stop the other CPUs by cross-calling them and forcing them to enter
795  * the panic_idle() loop above.
796  */
797 /*ARGSUSED*/
798 void
799 panic_stopcpus(cpu_t *cp, kthread_t *t, int spl)
800 {
801 	processorid_t i;
802 	cpuset_t xcset;
803 
804 	(void) splzs();
805 
806 	CPUSET_ALL_BUT(xcset, cp->cpu_id);
807 	xc_trycall(NULL, NULL, NULL, xcset, (int (*)())panic_idle);
808 
809 	for (i = 0; i < NCPU; i++) {
810 		if (i != cp->cpu_id && cpu[i] != NULL &&
811 		    (cpu[i]->cpu_flags & CPU_EXISTS))
812 			cpu[i]->cpu_flags |= CPU_QUIESCED;
813 	}
814 }
815 
816 /*
817  * Platform callback following each entry to panicsys().
818  */
819 /*ARGSUSED*/
820 void
821 panic_enter_hw(int spl)
822 {
823 	/* Nothing to do here */
824 }
825 
826 /*
827  * Platform-specific code to execute after panicstr is set: we invoke
828  * the PSM entry point to indicate that a panic has occurred.
829  */
830 /*ARGSUSED*/
831 void
832 panic_quiesce_hw(panic_data_t *pdp)
833 {
834 	psm_notifyf(PSM_PANIC_ENTER);
835 
836 #ifdef	TRAPTRACE
837 	/*
838 	 * Turn off TRAPTRACE
839 	 */
840 	TRAPTRACE_FREEZE;
841 #endif	/* TRAPTRACE */
842 }
843 
844 /*
845  * Platform callback prior to writing crash dump.
846  */
847 /*ARGSUSED*/
848 void
849 panic_dump_hw(int spl)
850 {
851 	/* Nothing to do here */
852 }
853 
854 /*ARGSUSED*/
855 void
856 plat_tod_fault(enum tod_fault_type tod_bad)
857 {
858 }
859 
860 /*ARGSUSED*/
861 int
862 blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
863 {
864 	return (ENOTSUP);
865 }
866 
867 /*
868  * The underlying console output routines are protected by raising IPL in case
869  * we are still calling into the early boot services.  Once we start calling
870  * the kernel console emulator, it will disable interrupts completely during
871  * character rendering (see sysp_putchar, for example).  Refer to the comments
872  * and code in common/os/console.c for more information on these callbacks.
873  */
874 /*ARGSUSED*/
875 int
876 console_enter(int busy)
877 {
878 	return (splzs());
879 }
880 
881 /*ARGSUSED*/
882 void
883 console_exit(int busy, int spl)
884 {
885 	splx(spl);
886 }
887 
888 /*
889  * Allocate a region of virtual address space, unmapped.
890  * Stubbed out except on sparc, at least for now.
891  */
892 /*ARGSUSED*/
893 void *
894 boot_virt_alloc(void *addr, size_t size)
895 {
896 	return (addr);
897 }
898 
899 volatile unsigned long	tenmicrodata;
900 
901 void
902 tenmicrosec(void)
903 {
904 	extern int	tsc_gethrtime_initted;
905 	int		i;
906 
907 	if (tsc_gethrtime_initted) {
908 		hrtime_t start, end;
909 		start = end =  gethrtime();
910 		while ((end - start) < (10 * (NANOSEC / MICROSEC))) {
911 			SMT_PAUSE();
912 			end = gethrtime();
913 		}
914 	} else {
915 		/*
916 		 * Artificial loop to induce delay.
917 		 */
918 		for (i = 0; i < microdata; i++)
919 			tenmicrodata = microdata;
920 	}
921 }
922 
923 /*
924  * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
925  * long, and it fills in the array with the time spent on cpu in
926  * each of the mstates, where time is returned in nsec.
927  *
928  * No guarantee is made that the returned values in times[] will
929  * monotonically increase on sequential calls, although this will
930  * be true in the long run. Any such guarantee must be handled by
931  * the caller, if needed. This can happen if we fail to account
932  * for elapsed time due to a generation counter conflict, yet we
933  * did account for it on a prior call (see below).
934  *
935  * The complication is that the cpu in question may be updating
936  * its microstate at the same time that we are reading it.
937  * Because the microstate is only updated when the CPU's state
938  * changes, the values in cpu_intracct[] can be indefinitely out
939  * of date. To determine true current values, it is necessary to
940  * compare the current time with cpu_mstate_start, and add the
941  * difference to times[cpu_mstate].
942  *
943  * This can be a problem if those values are changing out from
944  * under us. Because the code path in new_cpu_mstate() is
945  * performance critical, we have not added a lock to it. Instead,
946  * we have added a generation counter. Before beginning
947  * modifications, the counter is set to 0. After modifications,
948  * it is set to the old value plus one.
949  *
950  * get_cpu_mstate() will not consider the values of cpu_mstate
951  * and cpu_mstate_start to be usable unless the value of
952  * cpu_mstate_gen is both non-zero and unchanged, both before and
953  * after reading the mstate information. Note that we must
954  * protect against out-of-order loads around accesses to the
955  * generation counter. Also, this is a best effort approach in
956  * that we do not retry should the counter be found to have
957  * changed.
958  *
959  * cpu_intracct[] is used to identify time spent in each CPU
960  * mstate while handling interrupts. Such time should be reported
961  * against system time, and so is subtracted out from its
962  * corresponding cpu_acct[] time and added to
963  * cpu_acct[CMS_SYSTEM].
964  */
965 
966 void
967 get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
968 {
969 	int i;
970 	hrtime_t now, start;
971 	uint16_t gen;
972 	uint16_t state;
973 	hrtime_t intracct[NCMSTATES];
974 
975 	/*
976 	 * Load all volatile state under the protection of membar.
977 	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
978 	 * of (now - cpu_mstate_start) by a change in CPU mstate that
979 	 * arrives after we make our last check of cpu_mstate_gen.
980 	 */
981 
982 	now = gethrtime_unscaled();
983 	gen = cpu->cpu_mstate_gen;
984 
985 	membar_consumer();	/* guarantee load ordering */
986 	start = cpu->cpu_mstate_start;
987 	state = cpu->cpu_mstate;
988 	for (i = 0; i < NCMSTATES; i++) {
989 		intracct[i] = cpu->cpu_intracct[i];
990 		times[i] = cpu->cpu_acct[i];
991 	}
992 	membar_consumer();	/* guarantee load ordering */
993 
994 	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
995 		times[state] += now - start;
996 
997 	for (i = 0; i < NCMSTATES; i++) {
998 		if (i == CMS_SYSTEM)
999 			continue;
1000 		times[i] -= intracct[i];
1001 		if (times[i] < 0) {
1002 			intracct[i] += times[i];
1003 			times[i] = 0;
1004 		}
1005 		times[CMS_SYSTEM] += intracct[i];
1006 		scalehrtime(&times[i]);
1007 	}
1008 	scalehrtime(&times[CMS_SYSTEM]);
1009 }
1010