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