2 #include "../port/lib.h"
15 * Where configuration info is left for the loaded programme.
16 * This will turn into a structure as more is done by the boot loader
17 * (e.g. why parse the .ini file twice?).
18 * There are 3584 bytes available at CONFADDR.
20 #define BOOTARGS ((char*)CONFADDR)
21 #define BOOTARGSLEN (16*KiB) /* limit in devenv.c */
23 #define MAXCONFLINE 160
26 Minmem = 256*MB, /* conservative default */
29 #define isascii(c) ((uchar)(c) > 0 && (uchar)(c) < 0177)
31 extern char bdata[], edata[], end[], etext[];
33 uintptr kseg0 = KZERO;
34 Mach* machaddr[MAXMACH];
37 Memcache cachel[8]; /* arm arch v7 supports 1-7 */
39 * these are used by the cache.v7.s routines.
41 Lowmemcache *cacheconf;
44 * Option arguments from the command line.
45 * oargv[0] is the boot file.
46 * Optionsinit() is called from multiboot()
47 * or some other machine-dependent place
51 static char* oargv[20];
52 static char oargb[128];
54 static char oenv[4096];
56 static uintptr sp; /* XXX - must go - user stack of init proc */
64 /* store plan9.ini contents here at least until we stash them in #ec */
65 static char confname[MAXCONF][KNAMELEN];
66 static char confval[MAXCONF][MAXCONFLINE];
74 for(i = 0; i < nconf; i++)
75 if(cistrcmp(confname[i], name) == 0)
92 addconf(char *name, char *val)
98 if(val == nil || nconf >= MAXCONF)
101 strecpy(confname[i], confname[i]+sizeof(confname[i]), name);
104 strecpy(confval[i], confval[i]+sizeof(confval[i]), val);
120 /* convert to name=value\n format */
129 error("kernel configuration too large");
130 memmove(BOOTARGS, p, n);
131 memset(BOOTARGS + n, '\n', BOOTARGSLEN - n);
137 * assumes that we have loaded our /cfg/pxe/mac file at CONFADDR
138 * (usually 0x1000) with tftp in u-boot. no longer uses malloc, so
139 * can be called early.
146 k = (char *)CONFADDR;
150 for(; k && *k != '\0'; k = next) {
151 if (!isascii(*k)) /* sanity check */
153 next = strchr(k, '\n');
157 if (*k == '\0' || *k == '\n' || *k == '#')
161 continue; /* mal-formed line */
174 o = strecpy(oargb, oargb+sizeof(oargb), s)+1;
175 if(getenv("bootargs", o, o - oargb) != nil)
178 oargblen = strlen(oargb);
179 oargc = tokenize(oargb, oargv, nelem(oargv)-1);
184 getenv(char* name, char* buf, int n)
190 if((e = strchr(p, '=')) == nil)
192 for(q = name; p < e; p++){
197 if(p == e && *q == 0){
198 strecpy(buf, buf+n, e+1);
207 /* enable scheduling of this cpu */
215 if ((active.machs & cpubit) == 0) { /* currently off? */
217 active.machs |= cpubit;
222 /* disable scheduling of this cpu */
230 if (active.machs & cpubit) { /* currently on? */
232 active.machs &= ~cpubit;
247 if(machaddr[m->machno] != m) {
253 if (canlock(&testlock)) {
256 panic("cpu%d: locks don't work", m->machno);
262 if (m->machno != 0) {
263 /* synchronise with cpu 0 */
264 m->ticks = m0->ticks;
265 m->fastclock = m0->fastclock;
266 m->cpuhz = m0->cpuhz;
267 m->delayloop = m0->delayloop;
269 if (m->machno != 0 &&
270 (m->fastclock == 0 || m->cpuhz == 0 || m->delayloop == 0))
271 panic("buggered cpu 0 Mach");
277 /* l.s has already zeroed Mach, which now contains our stack. */
290 lock(&testlock); /* hold this forever */
294 l1cache->wbse(&active, sizeof active);
299 * count CPU's, set up their mach structures and l1 ptes.
300 * we're running on cpu 0 and our data structures were
301 * statically allocated.
310 for(mach = 1; mach < MAXMACH; mach++){
311 machaddr[mach] = mm = mallocalign(MACHSIZE, MACHSIZE, 0, 0);
312 l1 = mallocalign(L1SIZE, L1SIZE, 0, 0);
313 if(mm == nil || l1 == nil)
315 memset(mm, 0, MACHSIZE);
318 memmove(l1, (void *)L1, L1SIZE); /* clone cpu0's l1 table */
319 l1cache->wbse(l1, L1SIZE);
322 l1cache->wbse(mm, MACHSIZE);
324 l1cache->wbse(machaddr, sizeof machaddr);
329 dump(void *vaddr, int words)
335 iprint("%.8lux%c", *addr++, words % 8 == 0? '\n': ' ');
341 allcacheinfo(cachel);
342 cacheconf = (Lowmemcache *)CACHECONF;
343 cacheconf->l1waysh = cachel[1].waysh;
344 cacheconf->l1setsh = cachel[1].setsh;
345 /* on the tegra 2, l2 is unarchitected */
346 cacheconf->l2waysh = cachel[2].waysh;
347 cacheconf->l2setsh = cachel[2].setsh;
357 l2pages = KADDR(PHYSDRAM + DRAMSIZE - RESRVDHIMEM);
361 * at entry, l.s has set m for cpu0 and printed "Plan 9 from Be"
362 * but has not zeroed bss.
368 static ulong vfy = 0xcafebabe;
371 if (vfy != 0xcafebabe) {
374 panic("data segment misaligned");
377 memset(edata, 0, end - edata);
380 * we can't lock until smpon has run, but we're supposed to wait
381 * until l1 & l2 are on. too bad. l1 is on, l2 will soon be.
388 * data segment is aligned, bss is zeroed, caches' characteristics
389 * are known. begin initialisation.
395 optionsinit("/boot/boot boot");
398 /* want plan9.ini to be able to affect memory sizing in confinit */
399 plan9iniinit(); /* before we step on plan9.ini in low memory */
401 /* l2 looks for *l2off= in plan9.ini */
402 l2cache->on(); /* l2->on requires locks to work, thus smpon */
403 l2cache->info(&cachel[2]);
408 trapinit(); /* so confinit can probe memory to size it */
409 confinit(); /* figures out amount of memory */
410 /* xinit prints (if it can), so finish up the banner here. */
412 navailcpus = getncpus();
413 iprint("(mp arm; %d cpus)\n\n", navailcpus);
416 for (cpu = 1; cpu < navailcpus; cpu++)
420 irqtooearly = 0; /* now that xinit and trapinit have run */
422 mainmem->flags |= POOL_ANTAGONISM /* | POOL_PARANOIA */ ;
425 * Printinit will cause the first malloc call.
426 * (printinit->qopen->malloc) unless any of the
427 * above (like clockinit) do an irqenable, which
429 * If the system dies here it's probably due
430 * to malloc(->xalloc) not being initialised
431 * correctly, or the data segment is misaligned
432 * (it's amazing how far you can get with
433 * things like that completely broken).
435 * (Should be) boilerplate from here on.
438 archreset(); /* cfg clock signals, print cache cfg */
439 clockinit(); /* start clocks */
442 delay(50); /* let uart catch up */
456 iprint("pcireset...");
457 pcireset(); /* this tends to hang after a reboot */
460 chandevreset(); /* most devices are discovered here */
461 // i8250console(); /* too early; see init0 */
463 pageinit(); /* prints "1020M memory: ⋯ */
468 * starting a cpu will eventually result in it calling schedinit,
469 * so everything necessary to run user processes should be set up
470 * before starting secondary cpus.
473 /* SMP & FW are already on when we get here; u-boot set them? */
474 for (cpu = 1; cpu < navailcpus; cpu++)
475 if (startcpu(cpu) < 0)
476 panic("cpu%d didn't start", cpu);
480 panic("cpu%d: schedinit returned", m->machno);
484 shutdown(int ispanic)
490 active.ispanic = ispanic;
491 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
493 once = active.machs & (1<<m->machno);
495 * setting exiting will make hzclock() on each processor call exit(0),
496 * which calls shutdown(0) and idles non-bootstrap cpus and returns
497 * on bootstrap processors (to permit a reboot). clearing our bit
498 * in machs avoids calling exit(0) from hzclock() on this processor.
500 active.machs &= ~(1<<m->machno);
505 delay(m->machno*1000); /* stagger them */
506 iprint("cpu%d: exiting\n", m->machno);
513 for(; ms > 0; ms -= TK2MS(2)){
515 if(active.machs == 0 && consactive() == 0)
522 * exit kernel either on a panic or user request
540 isaconfig(char *class, int ctlrno, ISAConf *isa)
545 snprint(cc, sizeof cc, "%s%d", class, ctlrno);
551 isa->nopt = tokenize(p, isa->opt, NISAOPT);
552 for(i = 0; i < isa->nopt; i++){
554 if(cistrncmp(p, "type=", 5) == 0)
556 else if(cistrncmp(p, "port=", 5) == 0)
557 isa->port = strtoul(p+5, &p, 0);
558 else if(cistrncmp(p, "irq=", 4) == 0)
559 isa->irq = strtoul(p+4, &p, 0);
560 else if(cistrncmp(p, "dma=", 4) == 0)
561 isa->dma = strtoul(p+4, &p, 0);
562 else if(cistrncmp(p, "mem=", 4) == 0)
563 isa->mem = strtoul(p+4, &p, 0);
564 else if(cistrncmp(p, "size=", 5) == 0)
565 isa->size = strtoul(p+5, &p, 0);
566 else if(cistrncmp(p, "freq=", 5) == 0)
567 isa->freq = strtoul(p+5, &p, 0);
573 * the new kernel is already loaded at address `code'
574 * of size `size' and entry point `entry'.
577 reboot(void *entry, void *code, ulong size)
579 int cpu, nmach, want, ms;
580 void (*f)(ulong, ulong, ulong);
586 * the boot processor is cpu0. execute this function on it
587 * so that the new kernel has the same cpu0.
589 if (m->machno != 0) {
594 print("on cpu%d (not 0)!\n", m->machno);
597 * the other cpus could be holding locks that will never get
598 * released (e.g., in the print path) if we put them into
599 * reset now, so force them to shutdown gracefully first.
601 for (want = 0, cpu = 1; cpu < navailcpus; cpu++)
605 for (ms = 15*1000; ms > 0 && active.stopped != want; ms -= 10)
608 if (active.stopped != want) {
609 for (cpu = 1; cpu < nmach; cpu++)
610 stopcpu(cpu); /* make really sure */
615 * should be the only processor running now
618 // print("reboot entry %#lux code %#lux size %ld\n",
619 // PADDR(entry), PADDR(code), size);
621 /* turn off buffered serial console */
626 /* shutdown devices */
629 /* call off the dog */
635 /* setup reboot trampoline function */
636 f = (void*)REBOOTADDR;
637 memmove(f, rebootcode, sizeof(rebootcode));
643 /* off we go - never to return */
644 (*f)(PADDR(entry), PADDR(code), size);
646 iprint("loaded kernel returned!\n");
651 * starting place for first process
657 char buf[2*KNAMELEN];
664 * These are o.k. because rootinit is null.
665 * Then early kproc's will have a root and dot.
667 up->slash = namec("#/", Atodir, 0, 0);
668 pathclose(up->slash->path);
669 up->slash->path = newpath("/");
670 up->dot = cclone(up->slash);
673 i8250console(); /* might be redundant, but harmless */
675 panic("init0: nil serialoq");
679 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
680 ksetenv("terminal", buf, 0);
681 ksetenv("cputype", "arm", 0);
683 ksetenv("service", "cpu", 0);
685 ksetenv("service", "terminal", 0);
687 /* convert plan9.ini variables to #e and #ec */
688 for(i = 0; i < nconf; i++) {
689 ksetenv(confname[i], confval[i], 0);
690 ksetenv(confname[i], confval[i], 1);
694 kproc("alarm", alarmkproc, 0);
700 bootargs(uintptr base)
707 * Push the boot args onto the stack.
708 * The initial value of the user stack must be such
709 * that the total used is larger than the maximum size
710 * of the argument list checked in syscall.
713 p = UINT2PTR(STACKALIGN(base + BY2PG - sizeof(up->s.args) - i));
714 memmove(p, oargb, i);
717 * Now push argc and the argv pointers.
718 * This isn't strictly correct as the code jumped to by
719 * touser in init9.s calls startboot (port/initcode.c) which
721 * startboot(char *argv0, char **argv)
722 * not the usual (int argc, char* argv[]), but argv0 is
723 * unused so it doesn't matter (at the moment...).
725 av = (char**)(p - (oargc+2)*sizeof(char*));
726 ssize = base + BY2PG - PTR2UINT(av);
727 *av++ = (char*)oargc;
728 for(i = 0; i < oargc; i++)
729 *av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
733 * Leave space for the return PC of the
734 * caller of initcode.
736 sp = USTKTOP - ssize - sizeof(void*);
740 * create the first process
750 /* no processes yet */
755 p->egrp = smalloc(sizeof(Egrp));
757 p->fgrp = dupfgrp(nil);
762 kstrdup(&p->text, "*init*");
763 kstrdup(&p->user, eve);
768 p->sched.pc = PTR2UINT(init0);
769 p->sched.sp = PTR2UINT(p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr));
770 p->sched.sp = STACKALIGN(p->sched.sp);
775 * Technically, newpage can't be called here because it
776 * should only be called when in a user context as it may
777 * try to sleep if there are no pages available, but that
778 * shouldn't be the case here.
780 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
783 pg = newpage(1, 0, USTKTOP-BY2PG);
792 s = newseg(SG_TEXT, UTZERO, 1);
794 pg = newpage(1, 0, UTZERO);
795 memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
797 k = kmap(s->map[0]->pages[0]);
798 memmove(UINT2PTR(VA(k)), initcode, sizeof initcode);
804 Conf conf; /* XXX - must go - gag */
806 Confmem tsmem[nelem(conf.mem)] = {
808 * Memory available to Plan 9:
810 { .base = PHYSDRAM, .limit = PHYSDRAM + Minmem, },
812 ulong memsize = DRAMSIZE;
819 /* back off a little from the end */
820 addr = (uintptr)KADDR(PHYSDRAM + sz - BY2WD);
821 if (probeaddr(addr) >= 0) { /* didn't trap? memory present */
836 if(p = getconf("service")){
837 if(strcmp(p, "cpu") == 0)
839 else if(strcmp(p,"terminal") == 0)
844 * Copy the physical memory configuration to Conf.mem.
846 if(nelem(tsmem) > nelem(conf.mem)){
847 iprint("memory configuration botch\n");
850 if(0 && (p = getconf("*maxmem")) != nil) {
851 memsize = strtoul(p, 0, 0) - PHYSDRAM;
852 if (memsize < 16*MB) /* sanity */
857 * see if all that memory exists; if not, find out how much does.
858 * trapinit must have been called first.
860 if (gotmem(memsize - RESRVDHIMEM) < 0)
861 panic("can't find 1GB of memory");
863 tsmem[0].limit = PHYSDRAM + memsize;
864 memmove(conf.mem, tsmem, sizeof(tsmem));
867 pa = PADDR(PGROUND(PTR2UINT(end)));
870 * we assume that the kernel is at the beginning of one of the
871 * contiguous chunks of memory and fits therein.
873 for(i=0; i<nelem(conf.mem); i++){
874 /* take kernel out of allocatable space */
875 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
876 conf.mem[i].base = pa;
878 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
879 conf.npage += conf.mem[i].npage;
882 conf.upages = (conf.npage*80)/100;
883 conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
885 /* set up other configuration parameters */
886 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
889 if(conf.nproc > 2000)
891 conf.nswap = conf.npage*3;
896 * it's simpler on mp systems to take page-faults early,
897 * on reference, rather than later, on write, which might
898 * require tlb shootdowns.
900 conf.copymode = 1; /* copy on reference */
903 * Guess how much is taken by the large permanent
904 * datastructures. Mntcache and Mntrpc are not accounted for.
906 kpages = conf.npage - conf.upages;
908 kpages -= conf.upages*sizeof(Page)
909 + conf.nproc*sizeof(Proc)
910 + conf.nimage*sizeof(Image)
912 + conf.nswppo*sizeof(Page);
913 mainmem->maxsize = kpages;
916 * give terminals lots of image memory, too; the dynamic
917 * allocation will balance the load properly, hopefully.
918 * be careful with 32-bit overflow.
920 imagmem->maxsize = kpages;
924 cmpswap(long *addr, long old, long new)
926 return cas((int *)addr, old, new);
930 advertwfi(void) /* advertise my wfi status */
933 active.wfi |= 1 << m->machno;
938 unadvertwfi(void) /* do not advertise my wfi status */
941 active.wfi &= ~(1 << m->machno);
951 /* don't go into wfi until my local timer is ticking */
957 /* avoid recursion via ilock, advertise iff this cpu is initialised */
958 if (m->inidlehands == 1 && m->syscall > 0) {
978 * find any cpu other than me currently in wfi.
981 cpu = BI2BY*BY2WD - 1 - clz(active.wfi & ~(1 << m->machno));