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