/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* The main CPU-control loops, used to control masters and slaves.
*/
#include <kmdb/kaif_start.h>
#include <kmdb/kmdb_asmutil.h>
#include <kmdb/kmdb_dpi_impl.h>
#include <kmdb/kmdb_kdi.h>
#define KAIF_SLAVE_CMD_SPIN 0
#if defined(__sparc)
#endif
/*
* Used to synchronize attempts to set kaif_master_cpuid. kaif_master_cpuid may
* be read without kaif_master_lock, and may be written by the current master
* CPU.
*/
/*
* Used to ensure that all CPUs leave the debugger together. kaif_loop_lock must
* be held to write kaif_looping, but need not be held to read it.
*/
static volatile int kaif_slave_cmd;
static void
{
continue;
}
static void
{
*lock = 0;
}
static void
{
}
static int
{
int notflushed, i;
#if defined(__sparc)
#endif
/*
* If we re-entered due to a ::switch, we need to tell the slave CPUs
* to sleep again.
*/
switch (kmdb_dpi_reenter()) {
case KMDB_DPI_CMD_SWITCH_CPU:
/*
* We assume that the target CPU is a valid slave. There's no
* easy way to complain here, so we'll assume that the caller
* has done the proper checking.
*/
break;
/*
* Switch back to the saved trap table before we switch CPUs --
* we need to make sure that only one CPU is on the debugger's
* table at a time.
*/
/* The new master is now awake */
return (KAIF_CPU_CMD_SWITCH);
case KMDB_DPI_CMD_RESUME_ALL:
/*
* Resume everyone, clean up for next entry.
*/
if (kmdb_dpi_work_required())
return (KAIF_CPU_CMD_RESUME);
/*
* Single-CPU resume, which is performed on the debugger's
* trap table (so no need to switch back).
*/
return (KAIF_CPU_CMD_RESUME_MASTER);
/*
* Wait for the other cpus to finish flushing their caches.
*/
do {
notflushed = 0;
for (i = 0; i < kaif_ncpusave; i++) {
if (save->krs_cpu_state ==
!save->krs_cpu_flushed) {
notflushed++;
break;
}
}
} while (notflushed > 0);
break;
case KMDB_DPI_CMD_REBOOT:
/*
* Reboot must be initiated by CPU 0. I could ask why, but I'm
* afraid that I don't want to know the answer.
*/
if (cpusave->krs_cpu_id == 0)
/*
* Spin forever, waiting for CPU 0 (apparently a slave) to
* reboot the system.
*/
for (;;)
continue;
/*NOTREACHED*/
break;
#endif
}
goto master_loop;
}
static int
{
#if defined(__sparc)
/*
* If the user elects to drop to OBP from the debugger, some OBP
* implementations will cross-call the slaves. We have to turn
* IE back on so we can receive the cross-calls. If we don't,
* some OBP implementations will wait forever.
*/
#endif
/* Wait for duty to call */
for (;;) {
if (slavecmd == KAIF_SLAVE_CMD_SWITCH &&
break;
} else if (slavecmd == KAIF_SLAVE_CMD_FLUSH) {
continue;
} else if (slavecmd == KAIF_SLAVE_CMD_REBOOT &&
cpusave->krs_cpu_id == 0) {
rv = 0;
break;
#endif
} else if (slavecmd == KAIF_SLAVE_CMD_RESUME) {
break;
#if defined(__sparc)
} else if (slavecmd == KAIF_SLAVE_CMD_ACK) {
} else if (cpusave->krs_cpu_acked &&
slavecmd == KAIF_SLAVE_CMD_SPIN) {
cpusave->krs_cpu_acked = 0;
#endif
}
}
#if defined(__sparc)
#endif
return (rv);
}
static void
{
if (kaif_master_cpuid == KAIF_MASTER_CPUID_UNSET) {
/* This is the master. */
} else {
/* The master was already chosen - go be a slave */
}
}
int
{
int cmd;
if (kaif_master_cpuid == KAIF_MASTER_CPUID_UNSET) {
if (!kmdb_dpi_resume_requested &&
/*
* Special case: Unload requested before first debugger
* entry. Don't stop the world, as there's nothing to
* clean up that can't be handled by the running kernel.
*/
return (KAIF_CPU_CMD_RESUME);
}
#ifdef __sparc
/*
* Everyone has arrived, so we can disarm the post-PROM
* entry point.
*/
*kaif_promexitarmp = 0;
}
#endif
} else {
}
cpusave->krs_cpu_flushed = 0;
kaif_looping++;
/*
* We know who the master and slaves are, so now they can go off
* to their respective loops.
*/
do {
else
} while (cmd == KAIF_CPU_CMD_SWITCH);
kaif_looping--;
if (cmd == KAIF_CPU_CMD_RESUME) {
/*
* By this point, the master has directed the slaves to resume,
* and everyone is making their way to this point. We're going
* to block here until all CPUs leave the master and slave
* loops. When all have arrived, we'll turn them all loose.
* This barrier is required for two reasons:
*
* 1. There exists a race condition whereby a CPU could reenter
* the debugger while another CPU is still in the slave loop
* from this debugger entry. This usually happens when the
* current master releases the slaves, and makes it back to
* the world before the slaves notice the release. The
* former master then triggers a debugger entry, and attempts
* to stop the slaves for this entry before they've even
* resumed from the last one. When the slaves arrive here,
* they'll have re-disabled interrupts, and will thus ignore
* cross-calls until they finish resuming.
*
* 2. At the time of this writing, there exists a SPARC bug that
* causes an apparently unsolicited interrupt vector trap
* from OBP to one of the slaves. This wouldn't normally be
* a problem but for the fact that the cross-called CPU
* encounters some sort of failure while in OBP. OBP
* recovers by executing the debugger-hook word, which sends
* the slave back into the debugger, triggering a debugger
* fault. This problem seems to only happen during resume,
* the result being that all CPUs save for the cross-called
* one make it back into the world, while the cross-called
* one is stuck at the debugger fault prompt. Leave the
* world in that state too long, and you'll get a mondo
* timeout panic. If we hold everyone here, we can give the
* the user a chance to trigger a panic for further analysis.
* To trigger the bug, "pool_unlock:b :c" and "while : ; do
* psrset -p ; done".
*
* When the second item is fixed, the barrier can move into
* kaif_select_master(), immediately prior to the setting of
* kaif_master_cpuid.
*/
while (kaif_looping != 0)
continue;
}
return (cmd);
}
#if defined(__sparc)
/*
* There exist a race condition observed by some
* platforms where the kmdb master cpu exits to OBP via
* prom_enter_mon (e.g. "$q" command) and then later re-enter
* kmdb (typing "go") while the slaves are still proceeding
* from the OBP idle-loop back to the kmdb slave loop. The
* problem arises when the master cpu now back in kmdb proceed
* to re-enter OBP (e.g. doing a prom_read() from the kmdb main
* loop) while the slaves are still trying to get out of (the
* previous trip in) OBP into the safety of the kmdb slave loop.
* This routine forces the slaves to explicitly acknowledge
* that they are back in the slave loop. The master cpu can
* call this routine to ensure that all slave cpus are back
* in the slave loop before proceeding.
*/
void
kaif_slave_loop_barrier(void)
{
extern void kdi_usecwait(clock_t);
int i;
int not_acked;
int timeout_count = 0;
/*
* Wait for slave cpus to explicitly acknowledge
* that they are spinning in the slave loop.
*/
do {
not_acked = 0;
for (i = 0; i < kaif_ncpusave; i++) {
if (save->krs_cpu_state ==
!save->krs_cpu_acked) {
not_acked++;
break;
}
}
if (not_acked == 0)
break;
/*
* Play it safe and do a timeout delay.
* We will do at most kaif_ncpusave delays before
* bailing out of this barrier.
*/
kdi_usecwait(200);
} while (++timeout_count < kaif_ncpusave);
if (not_acked > 0)
/*
* we cannot establish a barrier with all
* the slave cpus coming back from OBP
* Record this fact for future debugging
*/
}
#endif