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