/*
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1982, 1986, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* the Systems Programming Group of the University of Utah Computer
* Science Department.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: Utah $Hdr: machdep.c 1.74 92/12/20$
*
* @(#)machdep.c 8.10 (Berkeley) 4/20/94
*/
/* the following is used externally (sysctl_hw) */
char machine[] = MACHINE; /* from <machine/param.h> */
/* Our exported CPU info; we can have only one. */
struct cpu_info cpu_info_store;
struct vm_map *phys_map = NULL;
extern paddr_t avail_end;
/*
* bootinfo base (physical and virtual). The bootinfo is placed, by
* the boot loader, into the first page of kernel text, which is zero
* filled (see locore.s) and not mapped at 0. It is remapped to a
* different address in pmap_bootstrap().
*/
paddr_t bootinfo_pa;
vaddr_t bootinfo_va;
int maxmem; /* max memory per process */
extern u_int lowram;
extern short exframesize[];
/* prototypes for local functions */
static void parityenable(void);
static int parityerror(struct frame *);
static int parityerrorfind(void);
static void identifycpu(void);
static void initcpu(void);
static int cpu_dumpsize(void);
static int cpu_dump(int (*)(dev_t, daddr_t, void *, size_t), daddr_t *);
static void cpu_init_kcore_hdr(void);
/* functions called from locore.s */
void dumpsys(void);
void hp300_init(void);
void straytrap(int, u_short);
void nmihand(struct frame);
/*
* Note that the value of delay_divisor is roughly
* 2048 / cpuspeed (where cpuspeed is in MHz) on 68020
* and 68030 systems. See clock.c for the delay
* calibration algorithm.
*/
int cpuspeed; /* relative CPU speed; XXX skewed on 68040 */
int delay_divisor; /* delay constant */
/*
* Early initialization, before main() is called.
*/
void
hp300_init(void)
{
struct btinfo_magic *bt_mag;
int i;
extern paddr_t avail_start, avail_end;
#ifdef CACHE_HAVE_VAC
/*
* Determine VA aliasing distance if any
*/
switch (machineid) {
case HP_320:
pmap_aliasmask = 0x3fff; /* 16KB */
break;
case HP_350:
pmap_aliasmask = 0x7fff; /* 32KB */
break;
default:
break;
}
#endif
/*
* Tell the VM system about available physical memory. The
* hp300 only has one segment.
*/
uvm_page_physload(atop(avail_start), atop(avail_end),
atop(avail_start), atop(avail_end), VM_FREELIST_DEFAULT);
/* Calibrate the delay loop. */
hp300_calibrate_delay();
/*
* Initialize error message buffer (at end of core).
* avail_end was pre-decremented in pmap_bootstrap to compensate.
*/
for (i = 0; i < btoc(MSGBUFSIZE); i++)
pmap_kenter_pa((vaddr_t)msgbufaddr + i * PAGE_SIZE,
avail_end + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0);
pmap_update(pmap_kernel());
initmsgbuf(msgbufaddr, m68k_round_page(MSGBUFSIZE));
/*
* Map in the bootinfo page, and make sure the bootinfo
* exists by searching for the MAGIC record. If it's not
* there, disable bootinfo.
*/
bootinfo_va = virtual_avail;
virtual_avail += PAGE_SIZE;
pmap_enter(pmap_kernel(), bootinfo_va, bootinfo_pa,
VM_PROT_READ|VM_PROT_WRITE,
VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
pmap_update(pmap_kernel());
bt_mag = lookup_bootinfo(BTINFO_MAGIC);
if (bt_mag == NULL ||
bt_mag->magic1 != BOOTINFO_MAGIC1 ||
bt_mag->magic2 != BOOTINFO_MAGIC2) {
pmap_remove(pmap_kernel(), bootinfo_va,
bootinfo_va + PAGE_SIZE);
pmap_update(pmap_kernel());
virtual_avail -= PAGE_SIZE;
bootinfo_va = 0;
}
}
/*
* Console initialization: called early on from main,
* before vm init or startup. Do enough configuration
* to choose and initialize a console.
*/
void
consinit(void)
{
/*
* Initialize the external I/O extent map.
*/
iomap_init();
/*
* Initialize the console before we print anything out.
*/
hp300_cninit();
/*
* Issue a warning if the boot loader didn't provide bootinfo.
*/
if (bootinfo_va != 0)
printf("bootinfo found at 0x%08lx\n", bootinfo_pa);
else
printf("WARNING: boot loader did not provide bootinfo\n");
#if NKSYMS || defined(DDB) || defined(MODULAR)
{
extern int end;
extern int *esym;
/*
* Find the model number.
*/
for (t = s = NULL, i = 0; hp300_models[i].name != NULL; i++) {
if (hp300_models[i].id == machineid) {
if (hp300_models[i].mmuid != -1 &&
hp300_models[i].mmuid != mmuid)
continue;
t = hp300_models[i].name;
s = hp300_models[i].speed;
break;
}
}
if (t == NULL) {
printf("\nunknown machineid %d\n", machineid);
goto lose;
}
/*
* ...and the CPU type.
*/
switch (cputype) {
case CPU_68040:
cpu = "MC68040";
break;
case CPU_68030:
cpu = "MC68030";
break;
case CPU_68020:
cpu = "MC68020";
break;
default:
printf("\nunknown cputype %d\n", cputype);
goto lose;
}
/*
* ...and the MMU type.
*/
switch (mmutype) {
case MMU_68040:
case MMU_68030:
mmu = "+MMU";
break;
case MMU_68851:
mmu = ", MC68851 MMU";
break;
case MMU_HP:
mmu = ", HP MMU";
break;
default:
printf("%s\nunknown MMU type %d\n", cpu, mmutype);
panic("startup");
}
/*
* Now that we have told the user what they have,
* let them know if that machine type isn't configured.
*/
switch (machineid) {
case -1: /* keep compilers happy */
#if !defined(HP320)
case HP_320:
#endif
#if !defined(HP330)
case HP_330:
#endif
#if !defined(HP340)
case HP_340:
#endif
#if !defined(HP345)
case HP_345:
#endif
#if !defined(HP350)
case HP_350:
#endif
#if !defined(HP360)
case HP_360:
#endif
#if !defined(HP362)
case HP_362:
#endif
#if !defined(HP370)
case HP_370:
#endif
#if !defined(HP375)
case HP_375:
#endif
#if !defined(HP380)
case HP_380:
#endif
#if !defined(HP382)
case HP_382:
#endif
#if !defined(HP385)
case HP_385:
#endif
#if !defined(HP400)
case HP_400:
#endif
#if !defined(HP425)
case HP_425:
#endif
#if !defined(HP433)
case HP_433:
#endif
panic("SPU type not configured");
default:
break;
}
/* If rebooting and a dump is requested do it. */
if (howto & RB_DUMP)
dumpsys();
haltsys:
/* Run any shutdown hooks. */
doshutdownhooks();
pmf_system_shutdown(boothowto);
#if defined(PANICWAIT) && !defined(DDB)
if ((howto & RB_HALT) == 0 && panicstr) {
printf("hit any key to reboot...\n");
cnpollc(1);
(void)cngetc();
cnpollc(0);
printf("\n");
}
#endif
/* Finally, halt/reboot the system. */
if (howto & RB_HALT) {
printf("System halted. Hit any key to reboot.\n\n");
cnpollc(1);
(void)cngetc();
cnpollc(0);
}
/*
* Called by dumpsys() to dump the machine-dependent header.
*/
static int
cpu_dump(int (*dump)(dev_t, daddr_t, void *, size_t), daddr_t *blknop)
{
int buf[MDHDRSIZE / sizeof(int)];
cpu_kcore_hdr_t *chdr;
kcore_seg_t *kseg;
int error;
/*
* These variables are needed by /sbin/savecore
*/
uint32_t dumpmag = 0x8fca0101; /* magic number */
int dumpsize = 0; /* pages */
long dumplo = 0; /* blocks */
/*
* This is called by main to set dumplo and dumpsize.
* Dumps always skip the first PAGE_SIZE of disk space
* in case there might be a disk label stored there.
* If there is extra space, put dump at the end to
* reduce the chance that swapping trashes it.
*/
void
cpu_dumpconf(void)
{
int chdrsize; /* size of dump header */
int nblks; /* size of dump area */
/*
* Check do see if we will fit. Note we always skip the
* first PAGE_SIZE in case there is a disk label there.
*/
if (nblks < (ctod(dumpsize) + chdrsize + ctod(1))) {
dumpsize = 0;
dumplo = -1;
return;
}
/*
* Put dump at the end of the partition.
*/
dumplo = (nblks - 1) - ctod(dumpsize) - chdrsize;
}
/*
* Dump physical memory onto the dump device. Called by cpu_reboot().
*/
void
dumpsys(void)
{
const struct bdevsw *bdev;
daddr_t blkno; /* current block to write */
/* dump routine */
int (*dump)(dev_t, daddr_t, void *, size_t);
int pg; /* page being dumped */
paddr_t maddr; /* PA being dumped */
int error; /* error code from (*dump)() */
/* XXX initialized here because of gcc lossage */
maddr = lowram;
pg = 0;
/* Make sure dump device is valid. */
if (dumpdev == NODEV)
return;
bdev = bdevsw_lookup(dumpdev);
if (bdev == NULL)
return;
if (dumpsize == 0) {
cpu_dumpconf();
if (dumpsize == 0)
return;
}
if (dumplo <= 0) {
printf("\ndump to dev %u,%u not possible\n",
major(dumpdev), minor(dumpdev));
return;
}
dump = bdev->d_dump;
blkno = dumplo;
printf("\ndumping to dev %u,%u offset %ld\n",
major(dumpdev), minor(dumpdev), dumplo);
printf("dump ");
/* Write the dump header. */
error = cpu_dump(dump, &blkno);
if (error)
goto bad;
for (pg = 0; pg < dumpsize; pg++) {
#define NPGMB (1024*1024/PAGE_SIZE)
/* print out how many MBs we have dumped */
if (pg && (pg % NPGMB) == 0)
printf("%d ", pg / NPGMB);
#undef NPGMB
pmap_enter(pmap_kernel(), (vaddr_t)vmmap, maddr,
VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
/*
* lookup_bootinfo:
*
* Look up information in bootinfo from boot loader.
*/
void *
lookup_bootinfo(int type)
{
struct btinfo_common *bt;
char *help = (char *)bootinfo_va;
/* Check for a bootinfo record first. */
if (help == NULL)
return NULL;
do {
bt = (struct btinfo_common *)help;
if (bt->type == type)
return help;
help += bt->next;
} while (bt->next != 0 &&
(size_t)help < (size_t)bootinfo_va + BOOTINFO_SIZE);
return NULL;
}
#if defined(PANICBUTTON) && !defined(DDB)
/*
* Declare these so they can be patched.
*/
int panicbutton = 1; /* non-zero if panic buttons are enabled */
int candbdiv = 2; /* give em half a second (hz / candbdiv) */
/*
* Level 7 interrupts can be caused by HIL keyboards (in cooked mode only,
* but we run them in raw mode) or parity errors.
*/
void
nmihand(struct frame frame)
{
/*
* Determine if level 7 interrupt was caused by a parity error
* and deal with it if it was. Returns 1 if it was a parity error.
*/
static int
parityerror(struct frame *fp)
{
if (!gotparmem)
return 0;
*PARREG = 0;
DELAY(10);
*PARREG = 1;
if (panicstr) {
printf("parity error after panic ignored\n");
return 1;
}
if (!parityerrorfind())
printf("WARNING: transient parity error ignored\n");
else if (USERMODE(fp->f_sr)) {
printf("pid %d: parity error\n", curproc->p_pid);
uprintf("sorry, pid %d killed due to memory parity error\n",
curproc->p_pid);
psignal(curproc, SIGKILL);
#ifdef DEBUG
} else if (ignorekperr) {
printf("WARNING: kernel parity error ignored\n");
#endif
} else {
regdump((struct trapframe *)fp, 128);
panic("kernel parity error");
}
return 1;
}
/*
* Yuk! There has got to be a better way to do this!
* Searching all of memory with interrupts blocked can lead to disaster.
*/
static int
parityerrorfind(void)
{
static label_t parcatch;
static int looking = 0;
volatile int pg, o, s;
volatile int *ip;
int i;
int found;
/*
* If looking is true we are searching for a known parity error
* and it has just occurred. All we do is return to the higher
* level invocation.
*/
if (looking)
longjmp(&parcatch);
s = splhigh();
/*
* If setjmp returns true, the parity error we were searching
* for has just occurred (longjmp above) at the current pg+o
*/
if (setjmp(&parcatch)) {
printf("Parity error at 0x%x\n", ctob(pg)|o);
found = 1;
goto done;
}
/*
* If we get here, a parity error has occurred for the first time
* and we need to find it. We turn off any external caches and
* loop thru memory, testing every longword til a fault occurs and
* we regain control at setjmp above. Note that because of the
* setjmp, pg and o need to be volatile or their values will be lost.
*/
looking = 1;
ecacheoff();
for (pg = btoc(lowram); pg < btoc(lowram)+physmem; pg++) {
pmap_enter(pmap_kernel(), (vaddr_t)vmmap, ctob(pg),
VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
pmap_update(pmap_kernel());
ip = (int *)vmmap;
for (o = 0; o < PAGE_SIZE; o += sizeof(int))
i = *ip++;
}
__USE(i);
/*
* Getting here implies no fault was found. Should never happen.
*/
printf("Couldn't locate parity error\n");
found = 0;
done:
looking = 0;
pmap_remove(pmap_kernel(), (vaddr_t)vmmap, (vaddr_t)&vmmap[PAGE_SIZE]);
pmap_update(pmap_kernel());
ecacheon();
splx(s);
return found;
}
/*
* cpu_exec_aout_makecmds():
* CPU-dependent a.out format hook for execve().
*
* Determine of the given exec package refers to something which we
* understand and, if so, set up the vmcmds for it.
*
* XXX what are the special cases for the hp300?
* XXX why is this COMPAT_NOMID? was something generating
* hp300 binaries with an a_mid of 0? i thought that was only
* done on little-endian machines... -- cgd
*/
int
cpu_exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
{
#if defined(COMPAT_NOMID) || defined(COMPAT_44)
u_long midmag, magic;
u_short mid;
int error;
struct exec *execp = epp->ep_hdr;
/*
* On the hp300, physical RAM is always located at the end of
* the physical address space, i.e. from 0xffffffff to lowram.
*/
return (pa < lowram || pa >= 0xfffffffc) ? EFAULT : 0;
}
int
mm_md_kernacc(void *ptr, vm_prot_t prot, bool *handled)
{
/*
* Do not allow reading intio or dio device space. This could lead
* to corruption of device registers.
*/
*handled = false;
return (ISIIOVA(ptr) || ((uint8_t *)ptr >= extiobase &&
(uint8_t *)ptr < extiobase + (EIOMAPSIZE * PAGE_SIZE)))
? EFAULT : 0;
}
#ifdef MODULAR
/*
* Push any modules loaded by the bootloader etc.
*/
void
module_init_md(void)
{
}
#endif