/*
* Where configuration info is left for the loaded programme.
* This will turn into a structure as more is done by the boot loader
* (e.g. why parse the .ini file twice?).
* There are 3584 bytes available at CONFADDR.
*/
#define BOOTARGS ((char*)CONFADDR)
#define BOOTARGSLEN (16*KiB) /* limit in devenv.c */
#define MAXCONF 64
#define MAXCONFLINE 160
Memcache cachel[8]; /* arm arch v7 supports 1-7 */
/*
* these are used by the cache.v7.s routines.
*/
Lowmemcache *cacheconf;
int vflag;
int normalprint;
char debug[256];
static Lock testlock;
/* store plan9.ini contents here at least until we stash them in #ec */
static char confname[MAXCONF][KNAMELEN];
static char confval[MAXCONF][MAXCONFLINE];
static int nconf;
/* convert to name=value\n format */
for(q=p; *q; q++) {
q += strlen(q);
*q = '=';
q += strlen(q);
*q = '\n';
}
n = q - p + 1;
if(n >= BOOTARGSLEN)
error("kernel configuration too large");
memmove(BOOTARGS, p, n);
memset(BOOTARGS + n, '\n', BOOTARGSLEN - n);
poperror();
free(p);
}
/*
* assumes that we have loaded our /cfg/pxe/mac file at CONFADDR
* (usually 0x1000) with tftp in u-boot. no longer uses malloc, so
* can be called early.
*/
static void
plan9iniinit(void)
{
char *k, *v, *next;
k = (char *)CONFADDR;
if(!isascii(*k))
return;
for(; k && *k != '\0'; k = next) {
if (!isascii(*k)) /* sanity check */
break;
next = strchr(k, '\n');
if (next)
*next++ = '\0';
if (*k == '\0' || *k == '\n' || *k == '#')
continue;
v = strchr(k, '=');
if(v == nil)
continue; /* mal-formed line */
*v++ = '\0';
addconf(k, v);
}
}
/* enable scheduling of this cpu */
void
machon(uint cpu)
{
lock(&active);
if (active.machs[cpu] == 0) { /* currently off? */
active.machs[cpu] = 1;
conf.nmach++;
}
unlock(&active);
}
/* disable scheduling of this cpu */
void
machoff(uint cpu)
{
lock(&active);
if (active.machs[cpu]) { /* currently on? */
active.machs[cpu] = 0;
conf.nmach--;
}
unlock(&active);
}
void
machinit(void)
{
Mach *m0;
if (m == 0) {
serialputc('?');
serialputc('m');
serialputc('0');
}
if(machaddr[m->machno] != m) {
serialputc('?');
serialputc('m');
serialputc('m');
}
/* l.s has already zeroed Mach, which now contains our stack. */
void
mach0init(void)
{
if (m == 0) {
serialputc('?');
serialputc('m');
}
conf.nmach = 0;
m->machno = 0;
machaddr[0] = m;
lock(&testlock); /* hold this forever */
machinit();
active.exiting = 0;
l1cache->wbse(&active, sizeof active);
up = nil;
}
/*
* count CPU's, set up their mach structures and l1 ptes.
* we're running on cpu 0 and our data structures were
* statically allocated.
*/
void
launchinit(void)
{
int mach;
Mach *mm;
PTE *l1;
/*
* at entry, l.s has set m for cpu0 and printed "Plan 9 from Be"
* but has not zeroed bss.
*/
void
main(void)
{
int cpu;
static ulong vfy = 0xcafebabe;
up = nil;
if (vfy != 0xcafebabe) {
serialputc('?');
serialputc('d');
panic("data segment misaligned");
}
memset(edata, 0, end - edata);
/*
* we can't lock until smpon has run, but we're supposed to wait
* until l1 & l2 are on. too bad. l1 is on, l2 will soon be.
*/
smpon();
iprint("ll Labs ");
cacheinit();
/*
* data segment is aligned, bss is zeroed, caches' characteristics
* are known. begin initialisation.
*/
mach0init();
l2pageinit();
mmuinit();
quotefmtinstall();
/* want plan9.ini to be able to affect memory sizing in confinit */
plan9iniinit(); /* before we step on plan9.ini in low memory */
/* l2 looks for *l2off= in plan9.ini */
l2cache->on(); /* l2->on requires locks to work, thus smpon */
l2cache->info(&cachel[2]);
allcache->on();
cortexa9cachecfg();
trapinit(); /* so confinit can probe memory to size it */
confinit(); /* figures out amount of memory */
/* xinit prints (if it can), so finish up the banner here. */
delay(100);
navailcpus = getncpus();
iprint("(mp arm; %d cpus)\n\n", navailcpus);
delay(100);
for (cpu = 1; cpu < navailcpus; cpu++)
stopcpu(cpu);
xinit();
irqtooearly = 0; /* now that xinit and trapinit have run */
/*
* Printinit will cause the first malloc call.
* (printinit->qopen->malloc) unless any of the
* above (like clockinit) do an irqenable, which
* will call malloc.
* If the system dies here it's probably due
* to malloc(->xalloc) not being initialised
* correctly, or the data segment is misaligned
* (it's amazing how far you can get with
* things like that completely broken).
*
* (Should be) boilerplate from here on.
*/
iprint("pcireset...");
pcireset(); /* this tends to hang after a reboot */
iprint("ok\n");
chandevreset(); /* most devices are discovered here */
// i8250console(); /* too early; see init0 */
pageinit(); /* prints "1020M memory: ⋯ */
userinit();
/*
* starting a cpu will eventually result in it calling schedinit,
* so everything necessary to run user processes should be set up
* before starting secondary cpus.
*/
launchinit();
/* SMP & FW are already on when we get here; u-boot set them? */
for (cpu = 1; cpu < navailcpus; cpu++)
if (startcpu(cpu) < 0)
panic("cpu%d didn't start", cpu);
l1diag();
/*
* exit kernel either on a panic or user request
*/
void
exit(int)
{
cpushutdown();
splhi();
if (m->machno == 0)
archreboot();
else {
intrcpushutdown();
stopcpu(m->machno);
for (;;)
idlehands();
}
}
int
isaconfig(char *class, int ctlrno, ISAConf *isa)
{
char cc[32], *p, *x;
int i;
/*
* the new kernel is already loaded at address `code'
* of size `size' and entry point `entry'.
*/
void
reboot(void *entry, void *code, ulong size)
{
void (*f)(ulong, ulong, ulong);
writeconf();
/*
* the boot processor is cpu0. execute this function on it
* so that the new kernel has the same cpu0.
*/
if (m->machno != 0) {
procwired(up, 0);
sched();
}
cpushutdown();
/*
* should be the only processor running now
*/
pcireset();
/* turn off buffered serial console */
serialoq = nil;
kprintoq = nil;
screenputs = nil;
/* shutdown devices */
chandevshutdown();
/* call off the dog */
clockshutdown();
splhi();
intrshutdown();
/* setup reboot trampoline function */
f = (void*)REBOOTADDR;
memmove(f, rebootcode, sizeof(rebootcode));
cachedwb();
l2cache->wbinv();
l2cache->off();
cacheuwbinv();
/* off we go - never to return */
(*f)(PADDR(entry), PADDR(code), size);
}
/*
* starting place for first process
*/
void
init0(void)
{
char buf[2*KNAMELEN], **sp;
int i;
chandevinit();
i8250console(); /* might be redundant, but harmless */
if(serialoq == nil)
panic("init0: nil serialoq");
normalprint = 1;
Confmem tsmem[nelem(conf.mem)] = {
/*
* Memory available to Plan 9:
*/
{ .base = PHYSDRAM, .limit = PHYSDRAM + Minmem, },
};
ulong memsize = DRAMSIZE;
static int
gotmem(uintptr sz)
{
uintptr addr;
/* back off a little from the end */
addr = (uintptr)KADDR(PHYSDRAM + sz - BY2WD);
if (probeaddr(addr) >= 0) { /* didn't trap? memory present */
memsize = sz;
return 0;
}
return -1;
}
/*
* see if all that memory exists; if not, find out how much does.
* trapinit must have been called first.
*/
if (gotmem(memsize - RESRVDHIMEM) < 0)
panic("can't find 1GB of memory");
conf.npage = 0;
pa = PADDR(PGROUND((uintptr)end));
/*
* we assume that the kernel is at the beginning of one of the
* contiguous chunks of memory and fits therein.
*/
for(i=0; i<nelem(conf.mem); i++){
/* take kernel out of allocatable space */
if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
conf.mem[i].base = pa;
/*
* it's simpler on mp systems to take page-faults early,
* on reference, rather than later, on write, which might
* require tlb shootdowns.
*/
conf.copymode = 1; /* copy on reference */
/*
* Guess how much is taken by the large permanent
* datastructures. Mntcache and Mntrpc are not accounted for.
*/
kpages = conf.npage - conf.upages;
kpages *= BY2PG;
kpages -= conf.upages*sizeof(Page)
+ conf.nproc*sizeof(Proc)
+ conf.nimage*sizeof(Image)
+ conf.nswap
+ conf.nswppo*sizeof(Page*);
mainmem->maxsize = kpages;
if(!cpuserver)
/*
* give terminals lots of image memory, too; the dynamic
* allocation will balance the load properly, hopefully.
* be careful with 32-bit overflow.
*/
imagmem->maxsize = kpages;
}
void
advertwfi(void) /* advertise my wfi status */
{
ilock(&active);
active.wfi |= 1 << m->machno;
iunlock(&active);
}
void
unadvertwfi(void) /* do not advertise my wfi status */
{
ilock(&active);
active.wfi &= ~(1 << m->machno);
iunlock(&active);
}
void
idlehands(void)
{
#ifdef use_ipi
int advertised;
/* don't go into wfi until my local timer is ticking */
if (m->ticks <= 1)
return;
advertised = 0;
m->inidlehands++;
/* avoid recursion via ilock, advertise iff this cpu is initialised */
if (m->inidlehands == 1 && m->syscall > 0) {
advertwfi();
advertised = 1;
}
wfi();
if (advertised)
unadvertwfi();
m->inidlehands--;
#endif
}
void
wakewfi(void)
{
#ifdef use_ipi
uint cpu;
/*
* find any cpu other than me currently in wfi.
* need not be exact.
*/
cpu = BI2BY*BY2WD - 1 - clz(active.wfi & ~(1 << m->machno));
if (cpu < MAXMACH)
intrcpu(cpu);
#endif
}