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.
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.
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]
23 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Copyright (c) 2010, Intel Corporation.
27 * All rights reserved.
30 #include <sys/types.h>
31 #include <sys/t_lock.h>
32 #include <sys/param.h>
33 #include <sys/segments.h>
34 #include <sys/sysmacros.h>
35 #include <sys/signal.h>
36 #include <sys/systm.h>
42 #include <sys/class.h>
48 #include <sys/reboot.h>
49 #include <sys/uadmin.h>
50 #include <sys/callb.h>
53 #include <sys/vnode.h>
56 #include <sys/procfs.h>
62 #include <sys/cmn_err.h>
63 #include <sys/utsname.h>
64 #include <sys/debug.h>
66 #include <sys/dumphdr.h>
67 #include <sys/bootconf.h>
68 #include <sys/varargs.h>
69 #include <sys/promif.h>
70 #include <sys/modctl.h>
72 #include <sys/consdev.h>
73 #include <sys/frame.h>
75 #include <sys/sunddi.h>
76 #include <sys/ddidmareq.h>
78 #include <sys/regset.h>
79 #include <sys/privregs.h>
80 #include <sys/clock.h>
83 #include <sys/stack.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>
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>
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>
114 #include <sys/nvpair.h>
115 #include <sys/x86_archext.h>
116 #include <sys/pool_pset.h>
117 #include <sys/autoconf.h>
119 #include <sys/dumphdr.h>
120 #include <sys/compress.h>
121 #include <sys/cpu_module.h>
123 #include <sys/hypervisor.h>
124 #include <sys/xpv_panic.h>
127 #include <sys/fastboot.h>
128 #include <sys/machelf.h>
129 #include <sys/kobj.h>
130 #include <sys/multiboot.h>
133 #include <sys/traptrace.h>
134 #endif /* TRAPTRACE */
136 #include <c2/audit.h>
137 #include <sys/clock_impl.h>
139 extern void audit_enterprom(int);
140 extern void audit_exitprom(int);
143 * Tunable to enable apix PSM; if set to 0, pcplusmp PSM will be used.
147 int apic_nvidia_io_max
= 0; /* no. of NVIDIA i/o apics */
150 * Occassionally the kernel knows better whether to power-off or reboot.
152 int force_shutdown_method
= AD_UNKNOWN
;
155 * The panicbuf array is used to record messages and state:
157 char panicbuf
[PANICBUFSIZE
];
160 * Flags to control Dynamic Reconfiguration features.
162 uint64_t plat_dr_options
;
165 * Maximum physical address for memory DR operations.
167 uint64_t plat_dr_physmax
;
170 * maxphys - used during physio
171 * klustsize - used for klustering by swapfs and specfs
173 int maxphys
= 56 * 1024; /* XXX See vm_subr.c - max b_count in physio */
174 int klustsize
= 56 * 1024;
176 caddr_t p0_va
; /* Virtual address for accessing physical page 0 */
179 * defined here, though unused on x86,
180 * to make kstat_fr.c happy.
184 void debug_enter(char *);
186 extern void pm_cfb_check_and_powerup(void);
187 extern void pm_cfb_rele(void);
189 extern fastboot_info_t newkernel
;
192 * Machine dependent code to reboot.
193 * "mdep" is interpreted as a character pointer; if non-null, it is a pointer
194 * to a string to be used as the argument string when rebooting.
196 * "invoke_cb" is a boolean. It is set to true when mdboot() can safely
197 * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when
198 * we are in a normal shutdown sequence (interrupts are not blocked, the
199 * system is not panic'ing or being suspended).
203 mdboot(int cmd
, int fcn
, char *mdep
, boolean_t invoke_cb
)
205 processorid_t bootcpuid
= 0;
206 static int is_first_quiesce
= 1;
207 static int is_first_reset
= 1;
208 int reset_status
= 0;
209 static char fallback_str
[] = "Falling back to regular reboot.\n";
211 if (fcn
== AD_FASTREBOOT
&& !newkernel
.fi_valid
)
216 if (fcn
== AD_FASTREBOOT
) {
217 mutex_enter(&cpu_lock
);
218 if (CPU_ACTIVE(cpu_get(bootcpuid
))) {
219 affinity_set(bootcpuid
);
221 mutex_exit(&cpu_lock
);
223 affinity_set(CPU_CURRENT
);
227 if (force_shutdown_method
!= AD_UNKNOWN
)
228 fcn
= force_shutdown_method
;
231 * XXX - rconsvp is set to NULL to ensure that output messages
232 * are sent to the underlying "hardware" device using the
233 * monitor's printf routine since we are in the process of
234 * either rebooting or halting the machine.
239 * Print the reboot message now, before pausing other cpus.
240 * There is a race condition in the printing support that
241 * can deadlock multiprocessor machines.
243 if (!(fcn
== AD_HALT
|| fcn
== AD_POWEROFF
))
244 prom_printf("rebooting...\n");
250 * We can't bring up the console from above lock level, so do it now
252 pm_cfb_check_and_powerup();
254 /* make sure there are no more changes to the device tree */
258 (void) callb_execute_class(CB_CL_MDBOOT
, NULL
);
261 * Clear any unresolved UEs from memory.
263 page_retire_mdboot();
267 * XXPV Should probably think some more about how we deal
268 * with panicing before it's really safe to panic.
269 * On hypervisors, we reboot very quickly.. Perhaps panic
270 * should only attempt to recover by rebooting if,
271 * say, we were able to mount the root filesystem,
272 * or if we successfully launched init(1m).
274 if (panicstr
&& proc_init
== NULL
)
275 (void) HYPERVISOR_shutdown(SHUTDOWN_poweroff
);
278 * stop other cpus and raise our priority. since there is only
279 * one active cpu after this, and our priority will be too high
280 * for us to be preempted, we're essentially single threaded
285 mutex_enter(&cpu_lock
);
286 pause_cpus(NULL
, NULL
);
287 mutex_exit(&cpu_lock
);
291 * If the system is panicking, the preloaded kernel is valid, and
292 * fastreboot_onpanic has been set, and the system has been up for
293 * longer than fastreboot_onpanic_uptime (default to 10 minutes),
294 * choose Fast Reboot.
296 if (fcn
== AD_BOOT
&& panicstr
&& newkernel
.fi_valid
&&
297 fastreboot_onpanic
&&
298 (panic_lbolt
- lbolt_at_boot
) > fastreboot_onpanic_uptime
) {
303 * Try to quiesce devices.
305 if (is_first_quiesce
) {
307 * Clear is_first_quiesce before calling quiesce_devices()
308 * so that if quiesce_devices() causes panics, it will not
311 is_first_quiesce
= 0;
314 quiesce_devices(ddi_root_node(), &reset_status
);
315 if (reset_status
== -1) {
316 if (fcn
== AD_FASTREBOOT
&& !force_fastreboot
) {
317 prom_printf("Driver(s) not capable of fast "
319 prom_printf(fallback_str
);
320 fastreboot_capable
= 0;
322 } else if (fcn
!= AD_FASTREBOOT
)
323 fastreboot_capable
= 0;
329 * Try to reset devices. reset_leaves() should only be called
330 * a) when there are no other threads that could be accessing devices,
332 * b) on a system that's not capable of fast reboot (fastreboot_capable
333 * being 0), or on a system where quiesce_devices() failed to
334 * complete (quiesce_active being 1).
336 if (is_first_reset
&& (!fastreboot_capable
|| quiesce_active
)) {
338 * Clear is_first_reset before calling reset_devices()
339 * so that if reset_devices() causes panics, it will not
346 /* Verify newkernel checksum */
347 if (fastreboot_capable
&& fcn
== AD_FASTREBOOT
&&
348 fastboot_cksum_verify(&newkernel
) != 0) {
349 fastreboot_capable
= 0;
350 prom_printf("Fast reboot: checksum failed for the new "
352 prom_printf(fallback_str
);
357 if (fastreboot_capable
&& fcn
== AD_FASTREBOOT
) {
359 * psm_shutdown is called within fast_reboot()
363 (*psm_shutdownf
)(cmd
, fcn
);
365 if (fcn
== AD_HALT
|| fcn
== AD_POWEROFF
)
373 /* mdpreboot - may be called prior to mdboot while root fs still mounted */
376 mdpreboot(int cmd
, int fcn
, char *mdep
)
378 if (fcn
== AD_FASTREBOOT
&& !fastreboot_capable
) {
381 cmn_err(CE_WARN
, "Fast reboot is not supported on xVM");
384 "Fast reboot is not supported on this platform%s",
385 fastreboot_nosup_message());
389 if (fcn
== AD_FASTREBOOT
) {
390 fastboot_load_kernel(mdep
);
391 if (!newkernel
.fi_valid
)
395 (*psm_preshutdownf
)(cmd
, fcn
);
399 stop_other_cpus(void)
401 ulong_t s
= clear_int_flag(); /* fast way to keep CPU from changing */
404 CPUSET_ALL_BUT(xcset
, CPU
->cpu_id
);
405 xc_priority(0, 0, 0, CPUSET2BV(xcset
), (xc_func_t
)mach_cpu_halt
);
410 * Machine dependent abort sequence handling
413 abort_sequence_enter(char *msg
)
415 if (abort_enable
== 0) {
416 if (AU_ZONE_AUDITING(GET_KCTX_GZ
))
420 if (AU_ZONE_AUDITING(GET_KCTX_GZ
))
423 if (AU_ZONE_AUDITING(GET_KCTX_GZ
))
428 * Enter debugger. Called when the user types ctrl-alt-d or whenever
429 * code wants to enter the debugger and possibly resume later.
431 * msg: message to print, possibly NULL.
434 debug_enter(char *msg
)
436 if (dtrace_debugger_init
!= NULL
)
437 (*dtrace_debugger_init
)();
439 if (msg
!= NULL
|| (boothowto
& RB_DEBUG
))
443 prom_printf("%s\n", msg
);
445 if (boothowto
& RB_DEBUG
)
448 if (dtrace_debugger_fini
!= NULL
)
449 (*dtrace_debugger_fini
)();
455 extern void acpi_reset_system();
457 ushort_t
*bios_memchk
;
460 * Can't use psm_map_phys or acpi_reset_system before the hat is
464 bios_memchk
= (ushort_t
*)psm_map_phys(0x472,
465 sizeof (ushort_t
), PROT_READ
| PROT_WRITE
);
467 *bios_memchk
= 0x1234; /* bios memory check disable */
469 if (options_dip
!= NULL
&&
470 ddi_prop_exists(DDI_DEV_T_ANY
, ddi_root_node(), 0,
478 * The problem with using stubs is that we can call
479 * acpi_reset_system only after the kernel is up and running.
481 * We should create a global state to keep track of how far
482 * up the kernel is but for the time being we will depend on
483 * bootops. bootops cleared in startup_end().
491 if (IN_XPV_PANIC()) {
492 if (khat_running
&& bootops
== NULL
) {
499 (void) HYPERVISOR_shutdown(SHUTDOWN_reboot
);
500 panic("HYPERVISOR_shutdown() failed");
506 * Halt the machine and return to the monitor
511 stop_other_cpus(); /* send stop signal to other CPUs */
513 prom_printf("(%s) \n", s
);
519 * Initiate interrupt redistribution.
522 i_ddi_intr_redist_all_cpus()
527 * XXX These probably ought to live somewhere else
528 * XXX They are called from mem.c
532 * Convert page frame number to an OBMEM page frame number
533 * (i.e. put in the type bits -- zero for this implementation)
536 impl_obmem_pfnum(pfn_t pf
)
542 int nmi_test
= 0; /* checked in intentry.s during clock int */
544 nmfunc1(int arg
, struct regs
*rp
)
546 printf("nmi called with arg = %x, regs = %x\n", arg
, rp
);
549 printf("ip = %x\n", rp
->r_pc
);
557 #include <sys/bootsvcs.h>
559 /* Hacked up initialization for initial kernel check out is HERE. */
560 /* The basic steps are: */
561 /* kernel bootfuncs definition/initialization for KADB */
562 /* kadb bootfuncs pointer initialization */
563 /* putchar/getchar (interrupts disabled) */
565 /* kadb bootfuncs pointer initialization */
573 if (cons_polledio
== NULL
) {
575 prom_printf("getchar called with no console\n");
580 s
= clear_int_flag();
581 i
= cons_polledio
->cons_polledio_getchar(
582 cons_polledio
->cons_polledio_argument
);
593 * We have no alternative but to drop the output on the floor.
595 if (cons_polledio
== NULL
||
596 cons_polledio
->cons_polledio_putchar
== NULL
)
599 s
= clear_int_flag();
600 cons_polledio
->cons_polledio_putchar(
601 cons_polledio
->cons_polledio_argument
, c
);
611 if (cons_polledio
== NULL
||
612 cons_polledio
->cons_polledio_ischar
== NULL
)
615 s
= clear_int_flag();
616 i
= cons_polledio
->cons_polledio_ischar(
617 cons_polledio
->cons_polledio_argument
);
625 prom_printf("Type any key to continue ");
626 (void) prom_getchar();
631 static struct boot_syscalls kern_sysp
= {
632 sysp_getchar
, /* unchar (*getchar)(); 7 */
633 sysp_putchar
, /* int (*putchar)(); 8 */
634 sysp_ischar
, /* int (*ischar)(); 9 */
638 int using_kern_polledio
;
645 * This routine is now totally misnamed, since it does not in fact
646 * control kadb's I/O; it only controls the kernel's prom_* I/O.
650 using_kern_polledio
= 1;
655 * the interface to the outside world
659 * poll_port -- wait for a register to achieve a
660 * specific state. Arguments are a mask of bits we care about,
661 * and two sub-masks. To return normally, all the bits in the
662 * first sub-mask must be ON, all the bits in the second sub-
663 * mask must be OFF. If about seconds pass without the register
664 * achieving the desired bit configuration, we return 1, else
668 poll_port(ushort_t port
, ushort_t mask
, ushort_t onbits
, ushort_t offbits
)
673 for (i
= 500000; i
; i
--) {
674 maskval
= inb(port
) & mask
;
675 if (((maskval
& onbits
) == onbits
) &&
676 ((maskval
& offbits
) == 0))
684 * set_idle_cpu is called from idle() when a CPU becomes idle.
686 /*LINTED: static unused */
687 static uint_t last_idle_cpu
;
691 set_idle_cpu(int cpun
)
693 last_idle_cpu
= cpun
;
694 (*psm_set_idle_cpuf
)(cpun
);
698 * unset_idle_cpu is called from idle() when a CPU is no longer idle.
702 unset_idle_cpu(int cpun
)
704 (*psm_unset_idle_cpuf
)(cpun
);
708 * This routine is almost correct now, but not quite. It still needs the
709 * equivalent concept of "hres_last_tick", just like on the sparc side.
710 * The idea is to take a snapshot of the hi-res timer while doing the
711 * hrestime_adj updates under hres_lock in locore, so that the small
712 * interval between interrupt assertion and interrupt processing is
713 * accounted for correctly. Once we have this, the code below should
714 * be modified to subtract off hres_last_tick rather than hrtime_base.
716 * I'd have done this myself, but I don't have source to all of the
717 * vendor-specific hi-res timer routines (grrr...). The generic hook I
718 * need is something like "gethrtime_unlocked()", which would be just like
719 * gethrtime() but would assume that you're already holding CLOCK_LOCK().
720 * This is what the GET_HRTIME() macro is for on sparc (although it also
721 * serves the function of making time available without a function call
722 * so you don't take a register window overflow while traps are disabled).
725 pc_gethrestime(timestruc_t
*tp
)
729 int nslt
; /* nsec since last tick */
730 int adj
; /* amount of adjustment to apply */
733 lock_prev
= hres_lock
;
735 nslt
= (int)(gethrtime() - hres_last_tick
);
738 * nslt < 0 means a tick came between sampling
739 * gethrtime() and hres_last_tick; restart the loop
745 if (hrestime_adj
!= 0) {
746 if (hrestime_adj
> 0) {
747 adj
= (nslt
>> ADJ_SHIFT
);
748 if (adj
> hrestime_adj
)
749 adj
= (int)hrestime_adj
;
751 adj
= -(nslt
>> ADJ_SHIFT
);
752 if (adj
< hrestime_adj
)
753 adj
= (int)hrestime_adj
;
757 while ((unsigned long)now
.tv_nsec
>= NANOSEC
) {
760 * We might have a large adjustment or have been in the
761 * debugger for a long time; take care of (at most) four
762 * of those missed seconds (tv_nsec is 32 bits, so
763 * anything >4s will be wrapping around). However,
764 * anything more than 2 seconds out of sync will trigger
765 * timedelta from clock() to go correct the time anyway,
766 * so do what we can, and let the big crowbar do the
767 * rest. A similar correction while loop exists inside
768 * hres_tick(); in all cases we'd like tv_nsec to
769 * satisfy 0 <= tv_nsec < NANOSEC to avoid confusing
770 * user processes, but if tv_sec's a little behind for a
771 * little while, that's OK; time still monotonically
775 now
.tv_nsec
-= NANOSEC
;
778 if ((hres_lock
& ~1) != lock_prev
)
785 gethrestime_lasttick(timespec_t
*tp
)
795 gethrestime_sec(void)
804 * Initialize a kernel thread's stack
808 thread_stk_init(caddr_t stk
)
810 ASSERT(((uintptr_t)stk
& (STACK_ALIGN
- 1)) == 0);
811 return (stk
- SA(MINFRAME
));
815 * Initialize lwp's kernel stack.
820 * There's a tricky interdependency here between use of sysenter and
821 * TRAPTRACE which needs recording to avoid future confusion (this is
822 * about the third time I've re-figured this out ..)
824 * Here's how debugging lcall works with TRAPTRACE.
826 * 1 We're in userland with a breakpoint on the lcall instruction.
827 * 2 We execute the instruction - the instruction pushes the userland
828 * %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel
830 * 3 The hardware raises a debug trap in kernel mode, the hardware
831 * pushes %efl, %cs, %eip and gets to dbgtrap via the idt.
832 * 4 dbgtrap pushes the error code and trapno and calls cmntrap
833 * 5 cmntrap finishes building a trap frame
834 * 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk
835 * off the stack into the traptrace buffer.
837 * This means that the traptrace buffer contains the wrong values in
838 * %esp and %ss, but everything else in there is correct.
840 * Here's how debugging sysenter works with TRAPTRACE.
842 * a We're in userland with a breakpoint on the sysenter instruction.
843 * b We execute the instruction - the instruction pushes -nothing-
844 * on the stack, but sets %cs, %eip, %ss, %esp to prearranged
845 * values to take us to sys_sysenter, at the top of the lwp's
849 * At this point, because we got into the kernel without the requisite
850 * five pushes on the stack, if we didn't make extra room, we'd
851 * end up with the TRACE_REGS macro fetching the saved %ss and %esp
852 * values from negative (unmapped) stack addresses -- which really bites.
853 * That's why we do the '-= 8' below.
855 * XXX Note that reading "up" lwp0's stack works because t0 is declared
856 * right next to t0stack in locore.s
861 lwp_stk_init(klwp_t
*lwp
, caddr_t stk
)
864 struct pcb
*pcb
= &lwp
->lwp_pcb
;
867 stk
-= SA(sizeof (struct regs
) + SA(MINFRAME
));
869 stk
-= 2 * sizeof (greg_t
); /* space for phony %ss:%sp (see above) */
871 stk
= (caddr_t
)((uintptr_t)stk
& ~(STACK_ALIGN
- 1ul));
872 bzero(stk
, oldstk
- stk
);
873 lwp
->lwp_regs
= (void *)(stk
+ SA(MINFRAME
));
876 * Arrange that the virtualized %fs and %gs GDT descriptors
877 * have a well-defined initial state (present, ring 3
881 if (lwp_getdatamodel(lwp
) == DATAMODEL_NATIVE
)
882 pcb
->pcb_fsdesc
= pcb
->pcb_gsdesc
= zero_udesc
;
884 pcb
->pcb_fsdesc
= pcb
->pcb_gsdesc
= zero_u32desc
;
885 #elif defined(__i386)
886 pcb
->pcb_fsdesc
= pcb
->pcb_gsdesc
= zero_udesc
;
894 lwp_stk_fini(klwp_t
*lwp
)
898 * If we're not the panic CPU, we wait in panic_idle for reboot.
903 splx(ipltospl(CLOCK_LEVEL
));
904 (void) setjmp(&curthread
->t_pcb
);
918 * Stop the other CPUs by cross-calling them and forcing them to enter
919 * the panic_idle() loop above.
923 panic_stopcpus(cpu_t
*cp
, kthread_t
*t
, int spl
)
929 * In the case of a Xen panic, the hypervisor has already stopped
932 if (!IN_XPV_PANIC()) {
935 CPUSET_ALL_BUT(xcset
, cp
->cpu_id
);
936 xc_priority(0, 0, 0, CPUSET2BV(xcset
), (xc_func_t
)panic_idle
);
939 for (i
= 0; i
< NCPU
; i
++) {
940 if (i
!= cp
->cpu_id
&& cpu
[i
] != NULL
&&
941 (cpu
[i
]->cpu_flags
& CPU_EXISTS
))
942 cpu
[i
]->cpu_flags
|= CPU_QUIESCED
;
947 * Platform callback following each entry to panicsys().
951 panic_enter_hw(int spl
)
953 /* Nothing to do here */
957 * Platform-specific code to execute after panicstr is set: we invoke
958 * the PSM entry point to indicate that a panic has occurred.
962 panic_quiesce_hw(panic_data_t
*pdp
)
964 psm_notifyf(PSM_PANIC_ENTER
);
966 cmi_panic_callback();
973 #endif /* TRAPTRACE */
977 * Platform callback prior to writing crash dump.
981 panic_dump_hw(int spl
)
983 /* Nothing to do here */
987 plat_traceback(void *fpreg
)
991 return (xpv_traceback(fpreg
));
998 plat_tod_fault(enum tod_fault_type tod_bad
)
1003 blacklist(int cmd
, const char *scheme
, nvlist_t
*fmri
, const char *class)
1009 * The underlying console output routines are protected by raising IPL in case
1010 * we are still calling into the early boot services. Once we start calling
1011 * the kernel console emulator, it will disable interrupts completely during
1012 * character rendering (see sysp_putchar, for example). Refer to the comments
1013 * and code in common/os/console.c for more information on these callbacks.
1017 console_enter(int busy
)
1024 console_exit(int busy
, int spl
)
1030 * Allocate a region of virtual address space, unmapped.
1031 * Stubbed out except on sparc, at least for now.
1035 boot_virt_alloc(void *addr
, size_t size
)
1040 volatile unsigned long tenmicrodata
;
1045 extern int gethrtime_hires
;
1047 if (gethrtime_hires
) {
1048 hrtime_t start
, end
;
1049 start
= end
= gethrtime();
1050 while ((end
- start
) < (10 * (NANOSEC
/ MICROSEC
))) {
1058 newtime
= xpv_gethrtime() + 10000; /* now + 10 us */
1059 while (xpv_gethrtime() < newtime
)
1065 * Artificial loop to induce delay.
1067 for (i
= 0; i
< microdata
; i
++)
1068 tenmicrodata
= microdata
;
1074 * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
1075 * long, and it fills in the array with the time spent on cpu in
1076 * each of the mstates, where time is returned in nsec.
1078 * No guarantee is made that the returned values in times[] will
1079 * monotonically increase on sequential calls, although this will
1080 * be true in the long run. Any such guarantee must be handled by
1081 * the caller, if needed. This can happen if we fail to account
1082 * for elapsed time due to a generation counter conflict, yet we
1083 * did account for it on a prior call (see below).
1085 * The complication is that the cpu in question may be updating
1086 * its microstate at the same time that we are reading it.
1087 * Because the microstate is only updated when the CPU's state
1088 * changes, the values in cpu_intracct[] can be indefinitely out
1089 * of date. To determine true current values, it is necessary to
1090 * compare the current time with cpu_mstate_start, and add the
1091 * difference to times[cpu_mstate].
1093 * This can be a problem if those values are changing out from
1094 * under us. Because the code path in new_cpu_mstate() is
1095 * performance critical, we have not added a lock to it. Instead,
1096 * we have added a generation counter. Before beginning
1097 * modifications, the counter is set to 0. After modifications,
1098 * it is set to the old value plus one.
1100 * get_cpu_mstate() will not consider the values of cpu_mstate
1101 * and cpu_mstate_start to be usable unless the value of
1102 * cpu_mstate_gen is both non-zero and unchanged, both before and
1103 * after reading the mstate information. Note that we must
1104 * protect against out-of-order loads around accesses to the
1105 * generation counter. Also, this is a best effort approach in
1106 * that we do not retry should the counter be found to have
1109 * cpu_intracct[] is used to identify time spent in each CPU
1110 * mstate while handling interrupts. Such time should be reported
1111 * against system time, and so is subtracted out from its
1112 * corresponding cpu_acct[] time and added to
1113 * cpu_acct[CMS_SYSTEM].
1117 get_cpu_mstate(cpu_t
*cpu
, hrtime_t
*times
)
1120 hrtime_t now
, start
;
1123 hrtime_t intracct
[NCMSTATES
];
1126 * Load all volatile state under the protection of membar.
1127 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
1128 * of (now - cpu_mstate_start) by a change in CPU mstate that
1129 * arrives after we make our last check of cpu_mstate_gen.
1132 now
= gethrtime_unscaled();
1133 gen
= cpu
->cpu_mstate_gen
;
1135 membar_consumer(); /* guarantee load ordering */
1136 start
= cpu
->cpu_mstate_start
;
1137 state
= cpu
->cpu_mstate
;
1138 for (i
= 0; i
< NCMSTATES
; i
++) {
1139 intracct
[i
] = cpu
->cpu_intracct
[i
];
1140 times
[i
] = cpu
->cpu_acct
[i
];
1142 membar_consumer(); /* guarantee load ordering */
1144 if (gen
!= 0 && gen
== cpu
->cpu_mstate_gen
&& now
> start
)
1145 times
[state
] += now
- start
;
1147 for (i
= 0; i
< NCMSTATES
; i
++) {
1148 if (i
== CMS_SYSTEM
)
1150 times
[i
] -= intracct
[i
];
1152 intracct
[i
] += times
[i
];
1155 times
[CMS_SYSTEM
] += intracct
[i
];
1156 scalehrtime(×
[i
]);
1158 scalehrtime(×
[CMS_SYSTEM
]);
1162 * This is a version of the rdmsr instruction that allows
1163 * an error code to be returned in the case of failure.
1166 checked_rdmsr(uint_t msr
, uint64_t *value
)
1168 if (!is_x86_feature(x86_featureset
, X86FSET_MSR
))
1170 *value
= rdmsr(msr
);
1175 * This is a version of the wrmsr instruction that allows
1176 * an error code to be returned in the case of failure.
1179 checked_wrmsr(uint_t msr
, uint64_t value
)
1181 if (!is_x86_feature(x86_featureset
, X86FSET_MSR
))
1188 * The mem driver's usual method of using hat_devload() to establish a
1189 * temporary mapping will not work for foreign pages mapped into this
1190 * domain or for the special hypervisor-provided pages. For the foreign
1191 * pages, we often don't know which domain owns them, so we can't ask the
1192 * hypervisor to set up a new mapping. For the other pages, we don't have
1193 * a pfn, so we can't create a new PTE. For these special cases, we do a
1194 * direct uiomove() from the existing kernel virtual address.
1198 plat_mem_do_mmio(struct uio
*uio
, enum uio_rw rw
)
1201 void *va
= (void *)(uintptr_t)uio
->uio_loffset
;
1202 off_t pageoff
= uio
->uio_loffset
& PAGEOFFSET
;
1203 size_t nbytes
= MIN((size_t)(PAGESIZE
- pageoff
),
1204 (size_t)uio
->uio_iov
->iov_len
);
1206 if ((rw
== UIO_READ
&&
1207 (va
== HYPERVISOR_shared_info
|| va
== xen_info
)) ||
1208 (pfn_is_foreign(hat_getpfnum(kas
.a_hat
, va
))))
1209 return (uiomove(va
, nbytes
, rw
, uio
));
1221 if (DOMAIN_IS_INITDOMAIN(xen_info
))
1222 return (xpv_nr_phys_pages());
1225 for (mp
= phys_install
; mp
!= NULL
; mp
= mp
->ml_next
)
1226 npages
+= mp
->ml_size
>> PAGESHIFT
;
1231 /* cpu threshold for compressed dumps */
1233 uint_t dump_plat_mincpu_default
= DUMP_PLAT_X86_64_MINCPU
;
1235 uint_t dump_plat_mincpu_default
= DUMP_PLAT_X86_32_MINCPU
;
1242 pfn_t pfn
= mmu_btop(xen_info
->shared_info
) | PFN_IS_FOREIGN_MFN
;
1243 mem_vtop_t mem_vtop
;
1247 * On the hypervisor, we want to dump the page with shared_info on it.
1249 if (!IN_XPV_PANIC()) {
1250 mem_vtop
.m_as
= &kas
;
1251 mem_vtop
.m_va
= HYPERVISOR_shared_info
;
1252 mem_vtop
.m_pfn
= pfn
;
1253 dumpvp_write(&mem_vtop
, sizeof (mem_vtop_t
));
1256 cnt
= dump_xpv_addr();
1268 pfn_t pfn
= mmu_btop(xen_info
->shared_info
) | PFN_IS_FOREIGN_MFN
;
1270 if (!IN_XPV_PANIC())
1271 dumpvp_write(&pfn
, sizeof (pfn
));
1279 dump_plat_data(void *dump_cbuf
)
1285 if (!IN_XPV_PANIC()) {
1286 csize
= (uint32_t)compress(HYPERVISOR_shared_info
, dump_cbuf
,
1288 dumpvp_write(&csize
, sizeof (uint32_t));
1289 dumpvp_write(dump_cbuf
, csize
);
1292 cnt
= dump_xpv_data(dump_cbuf
);
1301 * Calculates a linear address, given the CS selector and PC values
1302 * by looking up the %cs selector process's LDT or the CPU's GDT.
1303 * proc->p_ldtlock must be held across this call.
1306 linear_pc(struct regs
*rp
, proc_t
*p
, caddr_t
*linearp
)
1308 user_desc_t
*descrp
;
1310 uint16_t idx
= SELTOIDX(rp
->r_cs
);
1312 ASSERT(rp
->r_cs
<= 0xFFFF);
1313 ASSERT(MUTEX_HELD(&p
->p_ldtlock
));
1315 if (SELISLDT(rp
->r_cs
)) {
1317 * Currently 64 bit processes cannot have private LDTs.
1319 ASSERT(p
->p_model
!= DATAMODEL_LP64
);
1321 if (p
->p_ldt
== NULL
)
1324 descrp
= &p
->p_ldt
[idx
];
1325 baseaddr
= (caddr_t
)(uintptr_t)USEGD_GETBASE(descrp
);
1328 * Calculate the linear address (wraparound is not only ok,
1329 * it's expected behavior). The cast to uint32_t is because
1330 * LDT selectors are only allowed in 32-bit processes.
1332 *linearp
= (caddr_t
)(uintptr_t)(uint32_t)((uintptr_t)baseaddr
+
1336 descrp
= &CPU
->cpu_gdt
[idx
];
1337 baseaddr
= (caddr_t
)(uintptr_t)USEGD_GETBASE(descrp
);
1338 /* GDT-based descriptors' base addresses should always be 0 */
1339 ASSERT(baseaddr
== 0);
1341 *linearp
= (caddr_t
)(uintptr_t)rp
->r_pc
;
1348 * The implementation of dtrace_linear_pc is similar to the that of
1349 * linear_pc, above, but here we acquire p_ldtlock before accessing
1350 * p_ldt. This implementation is used by the pid provider; we prefix
1351 * it with "dtrace_" to avoid inducing spurious tracing events.
1354 dtrace_linear_pc(struct regs
*rp
, proc_t
*p
, caddr_t
*linearp
)
1356 user_desc_t
*descrp
;
1358 uint16_t idx
= SELTOIDX(rp
->r_cs
);
1360 ASSERT(rp
->r_cs
<= 0xFFFF);
1362 if (SELISLDT(rp
->r_cs
)) {
1364 * Currently 64 bit processes cannot have private LDTs.
1366 ASSERT(p
->p_model
!= DATAMODEL_LP64
);
1368 mutex_enter(&p
->p_ldtlock
);
1369 if (p
->p_ldt
== NULL
) {
1370 mutex_exit(&p
->p_ldtlock
);
1373 descrp
= &p
->p_ldt
[idx
];
1374 baseaddr
= (caddr_t
)(uintptr_t)USEGD_GETBASE(descrp
);
1375 mutex_exit(&p
->p_ldtlock
);
1378 * Calculate the linear address (wraparound is not only ok,
1379 * it's expected behavior). The cast to uint32_t is because
1380 * LDT selectors are only allowed in 32-bit processes.
1382 *linearp
= (caddr_t
)(uintptr_t)(uint32_t)((uintptr_t)baseaddr
+
1386 descrp
= &CPU
->cpu_gdt
[idx
];
1387 baseaddr
= (caddr_t
)(uintptr_t)USEGD_GETBASE(descrp
);
1388 /* GDT-based descriptors' base addresses should always be 0 */
1389 ASSERT(baseaddr
== 0);
1391 *linearp
= (caddr_t
)(uintptr_t)rp
->r_pc
;
1398 * We need to post a soft interrupt to reprogram the lbolt cyclic when
1399 * switching from event to cyclic driven lbolt. The following code adds
1400 * and posts the softint for x86.
1402 static ddi_softint_hdl_impl_t lbolt_softint_hdl
=
1403 {0, NULL
, NULL
, NULL
, 0, NULL
, NULL
, NULL
};
1406 lbolt_softint_add(void)
1408 (void) add_avsoftintr((void *)&lbolt_softint_hdl
, LOCK_LEVEL
,
1409 (avfunc
)lbolt_ev_to_cyclic
, "lbolt_ev_to_cyclic", NULL
, NULL
);
1413 lbolt_softint_post(void)
1415 (*setsoftint
)(CBE_LOCK_PIL
, lbolt_softint_hdl
.ih_pending
);
1419 plat_dr_check_capability(uint64_t features
)
1421 return ((plat_dr_options
& features
) == features
);
1425 plat_dr_support_cpu(void)
1427 return (plat_dr_options
& PLAT_DR_FEATURE_CPU
);
1431 plat_dr_support_memory(void)
1433 return (plat_dr_options
& PLAT_DR_FEATURE_MEMORY
);
1437 plat_dr_enable_capability(uint64_t features
)
1439 atomic_or_64(&plat_dr_options
, features
);
1443 plat_dr_disable_capability(uint64_t features
)
1445 atomic_and_64(&plat_dr_options
, ~features
);