machdep.c revision c48ac12eb805b96b1b8d7582c4168b4e4fa56865
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/segments.h>
#include <sys/sysmacros.h>
#include <sys/bootconf.h>
#include <sys/ddidmareq.h>
#include <sys/privregs.h>
#include <vm/seg_kmem.h>
#include <sys/vm_machparam.h>
#include <sys/archsystm.h>
#include <sys/machsystm.h>
#include <sys/machlock.h>
#include <sys/instance.h>
#include <sys/smp_impldefs.h>
#include <sys/psm_types.h>
#include <sys/x86_archext.h>
#include <sys/pool_pset.h>
#include <sys/autoconf.h>
#include <sys/compress.h>
#if defined(__xpv)
#include <sys/hypervisor.h>
#include <sys/xpv_panic.h>
#endif
#ifdef TRAPTRACE
#include <sys/traptrace.h>
#endif /* TRAPTRACE */
extern void audit_enterprom(int);
extern void audit_exitprom(int);
/*
* Occassionally the kernel knows better whether to power-off or reboot.
*/
int force_shutdown_method = AD_UNKNOWN;
/*
* The panicbuf array is used to record messages and state:
*/
char panicbuf[PANICBUFSIZE];
/*
* maxphys - used during physio
* klustsize - used for klustering by swapfs and specfs
*/
/*
* defined here, though unused on x86,
* to make kstat_fr.c happy.
*/
int vac;
void stop_other_cpus();
void debug_enter(char *);
extern void pm_cfb_check_and_powerup(void);
extern void pm_cfb_rele(void);
/*
* Machine dependent code to reboot.
* "mdep" is interpreted as a character pointer; if non-null, it is a pointer
* to a string to be used as the argument string when rebooting.
*
* "invoke_cb" is a boolean. It is set to true when mdboot() can safely
* invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when
* we are in a normal shutdown sequence (interrupts are not blocked, the
* system is not panic'ing or being suspended).
*/
/*ARGSUSED*/
void
{
if (!panicstr) {
}
if (force_shutdown_method != AD_UNKNOWN)
/*
* XXX - rconsvp is set to NULL to ensure that output messages
* are sent to the underlying "hardware" device using the
* monitor's printf routine since we are in the process of
* either rebooting or halting the machine.
*/
/*
* Print the reboot message now, before pausing other cpus.
* There is a race condition in the printing support that
* can deadlock multiprocessor machines.
*/
prom_printf("rebooting...\n");
if (IN_XPV_PANIC())
reset();
/*
* We can't bring up the console from above lock level, so do it now
*/
/* make sure there are no more changes to the device tree */
if (invoke_cb)
/*
* Clear any unresolved UEs from memory.
*/
#if defined(__xpv)
/*
* XXPV Should probably think some more about how we deal
* with panicing before it's really safe to panic.
* On hypervisors, we reboot very quickly.. Perhaps panic
* should only attempt to recover by rebooting if,
* say, we were able to mount the root filesystem,
* or if we successfully launched init(1m).
*/
(void) HYPERVISOR_shutdown(SHUTDOWN_poweroff);
#endif
/*
* stop other cpus and raise our priority. since there is only
* one active cpu after this, and our priority will be too high
* for us to be preempted, we're essentially single threaded
* from here on out.
*/
(void) spl6();
if (!panicstr) {
}
/*
* try and reset leaf devices. reset_leaves() should only
* be called when there are no other threads that could be
* accessing devices
*/
reset_leaves();
(void) spl8();
else
prom_reboot("");
/*NOTREACHED*/
}
/* mdpreboot - may be called prior to mdboot while root fs still mounted */
/*ARGSUSED*/
void
{
}
void
{
}
void
{
}
void
{
/*
* xc_trycall will attempt to make all other CPUs execute mach_cpu_halt,
* and will return immediately regardless of whether or not it was
* able to make them do it.
*/
}
/*
* Machine dependent abort sequence handling
*/
void
abort_sequence_enter(char *msg)
{
if (abort_enable == 0) {
if (audit_active)
audit_enterprom(0);
return;
}
if (audit_active)
audit_enterprom(1);
if (audit_active)
audit_exitprom(1);
}
/*
* Enter debugger. Called when the user types ctrl-alt-d or whenever
* code wants to enter the debugger and possibly resume later.
*/
void
char *msg) /* message to print, possibly NULL */
{
if (dtrace_debugger_init != NULL)
(*dtrace_debugger_init)();
if (msg)
kmdb_enter();
if (dtrace_debugger_fini != NULL)
(*dtrace_debugger_fini)();
}
void
reset(void)
{
#if !defined(__xpv)
/*
* Can't use psm_map_phys before the hat is initialized.
*/
if (khat_running) {
if (bios_memchk)
}
efi_reset();
pc_reset();
#else
if (IN_XPV_PANIC())
pc_reset();
(void) HYPERVISOR_shutdown(SHUTDOWN_reboot);
panic("HYPERVISOR_shutdown() failed");
#endif
/*NOTREACHED*/
}
/*
* Halt the machine and return to the monitor
*/
void
halt(char *s)
{
stop_other_cpus(); /* send stop signal to other CPUs */
if (s)
prom_printf("(%s) \n", s);
/*NOTREACHED*/
}
/*
* Initiate interrupt redistribution.
*/
void
{
}
/*
* XXX These probably ought to live somewhere else
* XXX They are called from mem.c
*/
/*
* Convert page frame number to an OBMEM page frame number
* (i.e. put in the type bits -- zero for this implementation)
*/
{
return (pf);
}
#ifdef NM_DEBUG
int nmi_test = 0; /* checked in intentry.s during clock int */
int nmtest = -1;
int arg;
{
nmtest += 50;
return (1);
}
return (0);
}
#endif
#include <sys/bootsvcs.h>
/* Hacked up initialization for initial kernel check out is HERE. */
/* The basic steps are: */
/* kernel bootfuncs definition/initialization for KADB */
/* kadb bootfuncs pointer initialization */
/* kadb bootfuncs pointer initialization */
int
{
int i;
ulong_t s;
if (cons_polledio == NULL) {
/* Uh oh */
prom_printf("getchar called with no console\n");
for (;;)
/* LOOP FOREVER */;
}
s = clear_int_flag();
restore_int_flag(s);
return (i);
}
void
sysp_putchar(int c)
{
ulong_t s;
/*
* We have no alternative but to drop the output on the floor.
*/
if (cons_polledio == NULL ||
return;
s = clear_int_flag();
restore_int_flag(s);
}
int
{
int i;
ulong_t s;
if (cons_polledio == NULL ||
return (0);
s = clear_int_flag();
restore_int_flag(s);
return (i);
}
int
goany(void)
{
prom_printf("Type any key to continue ");
(void) prom_getchar();
prom_printf("\n");
return (1);
}
static struct boot_syscalls kern_sysp = {
sysp_getchar, /* unchar (*getchar)(); 7 */
sysp_putchar, /* int (*putchar)(); 8 */
sysp_ischar, /* int (*ischar)(); 9 */
};
#if defined(__xpv)
int using_kern_polledio;
#endif
void
{
/*
* This routine is now totally misnamed, since it does not in fact
* control kadb's I/O; it only controls the kernel's prom_* I/O.
*/
#if defined(__xpv)
using_kern_polledio = 1;
#endif
}
/*
* the interface to the outside world
*/
/*
* poll_port -- wait for a register to achieve a
* specific state. Arguments are a mask of bits we care about,
* and two sub-masks. To return normally, all the bits in the
* first sub-mask must be ON, all the bits in the second sub-
* mask must be OFF. If about seconds pass without the register
* achieving the desired bit configuration, we return 1, else
* 0.
*/
int
{
int i;
for (i = 500000; i; i--) {
return (0);
drv_usecwait(10);
}
return (1);
}
/*
* set_idle_cpu is called from idle() when a CPU becomes idle.
*/
/*LINTED: static unused */
static uint_t last_idle_cpu;
/*ARGSUSED*/
void
set_idle_cpu(int cpun)
{
(*psm_set_idle_cpuf)(cpun);
}
/*
* unset_idle_cpu is called from idle() when a CPU is no longer idle.
*/
/*ARGSUSED*/
void
unset_idle_cpu(int cpun)
{
(*psm_unset_idle_cpuf)(cpun);
}
/*
* This routine is almost correct now, but not quite. It still needs the
* equivalent concept of "hres_last_tick", just like on the sparc side.
* The idea is to take a snapshot of the hi-res timer while doing the
* hrestime_adj updates under hres_lock in locore, so that the small
* interval between interrupt assertion and interrupt processing is
* accounted for correctly. Once we have this, the code below should
* be modified to subtract off hres_last_tick rather than hrtime_base.
*
* I'd have done this myself, but I don't have source to all of the
* vendor-specific hi-res timer routines (grrr...). The generic hook I
* need is something like "gethrtime_unlocked()", which would be just like
* gethrtime() but would assume that you're already holding CLOCK_LOCK().
* This is what the GET_HRTIME() macro is for on sparc (although it also
* serves the function of making time available without a function call
* so you don't take a register window overflow while traps are disabled).
*/
void
{
int lock_prev;
int nslt; /* nsec since last tick */
int adj; /* amount of adjustment to apply */
loop:
if (nslt < 0) {
/*
* nslt < 0 means a tick came between sampling
* gethrtime() and hres_last_tick; restart the loop
*/
goto loop;
}
if (hrestime_adj != 0) {
if (hrestime_adj > 0) {
if (adj > hrestime_adj)
adj = (int)hrestime_adj;
} else {
if (adj < hrestime_adj)
adj = (int)hrestime_adj;
}
}
/*
* We might have a large adjustment or have been in the
* debugger for a long time; take care of (at most) four
* of those missed seconds (tv_nsec is 32 bits, so
* anything >4s will be wrapping around). However,
* anything more than 2 seconds out of sync will trigger
* timedelta from clock() to go correct the time anyway,
* so do what we can, and let the big crowbar do the
* rest. A similar correction while loop exists inside
* hres_tick(); in all cases we'd like tv_nsec to
* satisfy 0 <= tv_nsec < NANOSEC to avoid confusing
* user processes, but if tv_sec's a little behind for a
* little while, that's OK; time still monotonically
* increases.
*/
}
goto loop;
}
void
{
int s;
s = hr_clock_lock();
hr_clock_unlock(s);
}
gethrestime_sec(void)
{
gethrestime(&now);
}
/*
* Initialize a kernel thread's stack
*/
{
}
/*
* Initialize lwp's kernel stack.
*/
#ifdef TRAPTRACE
/*
* There's a tricky interdependency here between use of sysenter and
* TRAPTRACE which needs recording to avoid future confusion (this is
* about the third time I've re-figured this out ..)
*
* Here's how debugging lcall works with TRAPTRACE.
*
* 1 We're in userland with a breakpoint on the lcall instruction.
* 2 We execute the instruction - the instruction pushes the userland
* %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel
* via the call gate.
* 3 The hardware raises a debug trap in kernel mode, the hardware
* pushes %efl, %cs, %eip and gets to dbgtrap via the idt.
* 4 dbgtrap pushes the error code and trapno and calls cmntrap
* 5 cmntrap finishes building a trap frame
* 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk
* off the stack into the traptrace buffer.
*
* This means that the traptrace buffer contains the wrong values in
* %esp and %ss, but everything else in there is correct.
*
* Here's how debugging sysenter works with TRAPTRACE.
*
* a We're in userland with a breakpoint on the sysenter instruction.
* b We execute the instruction - the instruction pushes -nothing-
* on the stack, but sets %cs, %eip, %ss, %esp to prearranged
* values to take us to sys_sysenter, at the top of the lwp's
* stack.
* c goto 3
*
* At this point, because we got into the kernel without the requisite
* five pushes on the stack, if we didn't make extra room, we'd
* end up with the TRACE_REGS macro fetching the saved %ss and %esp
* values from negative (unmapped) stack addresses -- which really bites.
* That's why we do the '-= 8' below.
*
* XXX Note that reading "up" lwp0's stack works because t0 is declared
* right next to t0stack in locore.s
*/
#endif
{
#ifdef TRAPTRACE
#endif
/*
* Arrange that the virtualized %fs and %gs GDT descriptors
* have a well-defined initial state (present, ring 3
* and of type data).
*/
#if defined(__amd64)
else
#endif /* __i386 */
return (stk);
}
/*ARGSUSED*/
void
{}
/*
* If we're not the panic CPU, we wait in panic_idle for reboot.
*/
static void
panic_idle(void)
{
for (;;)
;
}
/*
* Stop the other CPUs by cross-calling them and forcing them to enter
* the panic_idle() loop above.
*/
/*ARGSUSED*/
void
{
/*
* In the case of a Xen panic, the hypervisor has already stopped
* all of the CPUs.
*/
if (!IN_XPV_PANIC()) {
(void) splzs();
}
for (i = 0; i < NCPU; i++) {
}
}
/*
* Platform callback following each entry to panicsys().
*/
/*ARGSUSED*/
void
panic_enter_hw(int spl)
{
/* Nothing to do here */
}
/*
* Platform-specific code to execute after panicstr is set: we invoke
* the PSM entry point to indicate that a panic has occurred.
*/
/*ARGSUSED*/
void
{
#ifdef TRAPTRACE
/*
* Turn off TRAPTRACE
*/
#endif /* TRAPTRACE */
}
/*
* Platform callback prior to writing crash dump.
*/
/*ARGSUSED*/
void
panic_dump_hw(int spl)
{
/* Nothing to do here */
}
void *
plat_traceback(void *fpreg)
{
#ifdef __xpv
if (IN_XPV_PANIC())
return (xpv_traceback(fpreg));
#endif
return (fpreg);
}
/*ARGSUSED*/
void
{}
/*ARGSUSED*/
int
{
return (ENOTSUP);
}
/*
* The underlying console output routines are protected by raising IPL in case
* we are still calling into the early boot services. Once we start calling
* the kernel console emulator, it will disable interrupts completely during
* character rendering (see sysp_putchar, for example). Refer to the comments
*/
/*ARGSUSED*/
int
console_enter(int busy)
{
return (splzs());
}
/*ARGSUSED*/
void
{
}
/*
* Allocate a region of virtual address space, unmapped.
* Stubbed out except on sparc, at least for now.
*/
/*ARGSUSED*/
void *
{
return (addr);
}
volatile unsigned long tenmicrodata;
void
tenmicrosec(void)
{
extern int gethrtime_hires;
if (gethrtime_hires) {
SMT_PAUSE();
}
} else {
#if defined(__xpv)
while (xpv_gethrtime() < newtime)
SMT_PAUSE();
#else /* __xpv */
int i;
/*
* Artificial loop to induce delay.
*/
for (i = 0; i < microdata; i++)
#endif /* __xpv */
}
}
/*
* get_cpu_mstate() is passed an array of timestamps, NCMSTATES
* long, and it fills in the array with the time spent on cpu in
* each of the mstates, where time is returned in nsec.
*
* No guarantee is made that the returned values in times[] will
* monotonically increase on sequential calls, although this will
* be true in the long run. Any such guarantee must be handled by
* the caller, if needed. This can happen if we fail to account
* for elapsed time due to a generation counter conflict, yet we
* did account for it on a prior call (see below).
*
* The complication is that the cpu in question may be updating
* its microstate at the same time that we are reading it.
* Because the microstate is only updated when the CPU's state
* changes, the values in cpu_intracct[] can be indefinitely out
* of date. To determine true current values, it is necessary to
* compare the current time with cpu_mstate_start, and add the
* difference to times[cpu_mstate].
*
* This can be a problem if those values are changing out from
* under us. Because the code path in new_cpu_mstate() is
* performance critical, we have not added a lock to it. Instead,
* we have added a generation counter. Before beginning
* modifications, the counter is set to 0. After modifications,
* it is set to the old value plus one.
*
* get_cpu_mstate() will not consider the values of cpu_mstate
* and cpu_mstate_start to be usable unless the value of
* cpu_mstate_gen is both non-zero and unchanged, both before and
* after reading the mstate information. Note that we must
* protect against out-of-order loads around accesses to the
* generation counter. Also, this is a best effort approach in
* that we do not retry should the counter be found to have
* changed.
*
* cpu_intracct[] is used to identify time spent in each CPU
* mstate while handling interrupts. Such time should be reported
* against system time, and so is subtracted out from its
* corresponding cpu_acct[] time and added to
* cpu_acct[CMS_SYSTEM].
*/
void
{
int i;
/*
* Load all volatile state under the protection of membar.
* cpu_acct[cpu_mstate] must be loaded to avoid double counting
* of (now - cpu_mstate_start) by a change in CPU mstate that
* arrives after we make our last check of cpu_mstate_gen.
*/
now = gethrtime_unscaled();
membar_consumer(); /* guarantee load ordering */
for (i = 0; i < NCMSTATES; i++) {
}
membar_consumer(); /* guarantee load ordering */
for (i = 0; i < NCMSTATES; i++) {
if (i == CMS_SYSTEM)
continue;
if (times[i] < 0) {
times[i] = 0;
}
scalehrtime(×[i]);
}
}
/*
* This is a version of the rdmsr instruction that allows
* an error code to be returned in the case of failure.
*/
int
{
if ((x86_feature & X86_MSR) == 0)
return (ENOTSUP);
return (0);
}
/*
* This is a version of the wrmsr instruction that allows
* an error code to be returned in the case of failure.
*/
int
{
if ((x86_feature & X86_MSR) == 0)
return (ENOTSUP);
return (0);
}
/*
* The mem driver's usual method of using hat_devload() to establish a
* temporary mapping will not work for foreign pages mapped into this
* domain or for the special hypervisor-provided pages. For the foreign
* pages, we often don't know which domain owns them, so we can't ask the
* hypervisor to set up a new mapping. For the other pages, we don't have
* a pfn, so we can't create a new PTE. For these special cases, we do a
* direct uiomove() from the existing kernel virtual address.
*/
/*ARGSUSED*/
int
{
#if defined(__xpv)
#endif
return (ENOTSUP);
}
{
#if defined(__xpv)
if (DOMAIN_IS_INITDOMAIN(xen_info)) {
if (HYPERVISOR_sysctl(&op) != 0)
panic("physinfo op refused");
}
#endif /* __xpv */
return (npages);
}
int
{
#ifdef __xpv
int cnt;
/*
* On the hypervisor, we want to dump the page with shared_info on it.
*/
if (!IN_XPV_PANIC()) {
cnt = 1;
} else {
cnt = dump_xpv_addr();
}
return (cnt);
#else
return (0);
#endif
}
void
{
#ifdef __xpv
if (!IN_XPV_PANIC())
else
dump_xpv_pfn();
#endif
}
/*ARGSUSED*/
int
dump_plat_data(void *dump_cbuf)
{
#ifdef __xpv
int cnt;
if (!IN_XPV_PANIC()) {
PAGESIZE);
cnt = 1;
} else {
}
return (cnt);
#else
return (0);
#endif
}
/*
* Calculates a linear address, given the CS selector and PC values
* by looking up the %cs selector process's LDT or the CPU's GDT.
* proc->p_ldtlock must be held across this call.
*/
int
{
/*
* Currently 64 bit processes cannot have private LDTs.
*/
return (-1);
/*
* Calculate the linear address (wraparound is not only ok,
* it's expected behavior). The cast to uint32_t is because
* LDT selectors are only allowed in 32-bit processes.
*/
} else {
#ifdef DEBUG
/* GDT-based descriptors' base addresses should always be 0 */
#endif
}
return (0);
}
/*
* The implementation of dtrace_linear_pc is similar to the that of
* linear_pc, above, but here we acquire p_ldtlock before accessing
* p_ldt. This implementation is used by the pid provider; we prefix
* it with "dtrace_" to avoid inducing spurious tracing events.
*/
int
{
/*
* Currently 64 bit processes cannot have private LDTs.
*/
mutex_enter(&p->p_ldtlock);
mutex_exit(&p->p_ldtlock);
return (-1);
}
mutex_exit(&p->p_ldtlock);
/*
* Calculate the linear address (wraparound is not only ok,
* it's expected behavior). The cast to uint32_t is because
* LDT selectors are only allowed in 32-bit processes.
*/
} else {
#ifdef DEBUG
/* GDT-based descriptors' base addresses should always be 0 */
#endif
}
return (0);
}