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 */
212 if (active.machs[cpu] == 0) { /* currently off? */
213 active.machs[cpu] = 1;
219 /* disable scheduling of this cpu */
224 if (active.machs[cpu]) { /* currently on? */
225 active.machs[cpu] = 0;
241 if(machaddr[m->machno] != m) {
247 if (canlock(&testlock)) {
250 panic("cpu%d: locks don't work", m->machno);
256 if (m->machno != 0) {
257 /* synchronise with cpu 0 */
258 m->ticks = m0->ticks;
259 m->fastclock = m0->fastclock;
260 m->cpuhz = m0->cpuhz;
261 m->delayloop = m0->delayloop;
263 if (m->machno != 0 &&
264 (m->fastclock == 0 || m->cpuhz == 0 || m->delayloop == 0))
265 panic("buggered cpu 0 Mach");
271 /* l.s has already zeroed Mach, which now contains our stack. */
284 lock(&testlock); /* hold this forever */
288 l1cache->wbse(&active, sizeof active);
293 * count CPU's, set up their mach structures and l1 ptes.
294 * we're running on cpu 0 and our data structures were
295 * statically allocated.
304 for(mach = 1; mach < MAXMACH; mach++){
305 machaddr[mach] = mm = mallocalign(MACHSIZE, MACHSIZE, 0, 0);
306 l1 = mallocalign(L1SIZE, L1SIZE, 0, 0);
307 if(mm == nil || l1 == nil)
309 memset(mm, 0, MACHSIZE);
312 memmove(l1, (void *)L1, L1SIZE); /* clone cpu0's l1 table */
313 l1cache->wbse(l1, L1SIZE);
316 l1cache->wbse(mm, MACHSIZE);
318 l1cache->wbse(machaddr, sizeof machaddr);
323 dump(void *vaddr, int words)
329 iprint("%.8lux%c", *addr++, words % 8 == 0? '\n': ' ');
335 allcacheinfo(cachel);
336 cacheconf = (Lowmemcache *)CACHECONF;
337 cacheconf->l1waysh = cachel[1].waysh;
338 cacheconf->l1setsh = cachel[1].setsh;
339 /* on the tegra 2, l2 is unarchitected */
340 cacheconf->l2waysh = cachel[2].waysh;
341 cacheconf->l2setsh = cachel[2].setsh;
351 l2pages = KADDR(PHYSDRAM + DRAMSIZE - RESRVDHIMEM);
355 * at entry, l.s has set m for cpu0 and printed "Plan 9 from Be"
356 * but has not zeroed bss.
362 static ulong vfy = 0xcafebabe;
365 if (vfy != 0xcafebabe) {
368 panic("data segment misaligned");
371 memset(edata, 0, end - edata);
374 * we can't lock until smpon has run, but we're supposed to wait
375 * until l1 & l2 are on. too bad. l1 is on, l2 will soon be.
382 * data segment is aligned, bss is zeroed, caches' characteristics
383 * are known. begin initialisation.
389 optionsinit("/boot/boot boot");
392 /* want plan9.ini to be able to affect memory sizing in confinit */
393 plan9iniinit(); /* before we step on plan9.ini in low memory */
395 /* l2 looks for *l2off= in plan9.ini */
396 l2cache->on(); /* l2->on requires locks to work, thus smpon */
397 l2cache->info(&cachel[2]);
402 trapinit(); /* so confinit can probe memory to size it */
403 confinit(); /* figures out amount of memory */
404 /* xinit prints (if it can), so finish up the banner here. */
406 navailcpus = getncpus();
407 iprint("(mp arm; %d cpus)\n\n", navailcpus);
410 for (cpu = 1; cpu < navailcpus; cpu++)
414 irqtooearly = 0; /* now that xinit and trapinit have run */
416 mainmem->flags |= POOL_ANTAGONISM /* | POOL_PARANOIA */ ;
419 * Printinit will cause the first malloc call.
420 * (printinit->qopen->malloc) unless any of the
421 * above (like clockinit) do an irqenable, which
423 * If the system dies here it's probably due
424 * to malloc(->xalloc) not being initialised
425 * correctly, or the data segment is misaligned
426 * (it's amazing how far you can get with
427 * things like that completely broken).
429 * (Should be) boilerplate from here on.
432 archreset(); /* cfg clock signals, print cache cfg */
433 clockinit(); /* start clocks */
436 delay(50); /* let uart catch up */
450 iprint("pcireset...");
451 pcireset(); /* this tends to hang after a reboot */
454 chandevreset(); /* most devices are discovered here */
455 // i8250console(); /* too early; see init0 */
457 pageinit(); /* prints "1020M memory: ⋯ */
462 * starting a cpu will eventually result in it calling schedinit,
463 * so everything necessary to run user processes should be set up
464 * before starting secondary cpus.
467 /* SMP & FW are already on when we get here; u-boot set them? */
468 for (cpu = 1; cpu < navailcpus; cpu++)
469 if (startcpu(cpu) < 0)
470 panic("cpu%d didn't start", cpu);
474 panic("cpu%d: schedinit returned", m->machno);
478 * exit kernel either on a panic or user request
496 isaconfig(char *class, int ctlrno, ISAConf *isa)
501 snprint(cc, sizeof cc, "%s%d", class, ctlrno);
507 isa->nopt = tokenize(p, isa->opt, NISAOPT);
508 for(i = 0; i < isa->nopt; i++){
510 if(cistrncmp(p, "type=", 5) == 0)
512 else if(cistrncmp(p, "port=", 5) == 0)
513 isa->port = strtoul(p+5, &p, 0);
514 else if(cistrncmp(p, "irq=", 4) == 0)
515 isa->irq = strtoul(p+4, &p, 0);
516 else if(cistrncmp(p, "dma=", 4) == 0)
517 isa->dma = strtoul(p+4, &p, 0);
518 else if(cistrncmp(p, "mem=", 4) == 0)
519 isa->mem = strtoul(p+4, &p, 0);
520 else if(cistrncmp(p, "size=", 5) == 0)
521 isa->size = strtoul(p+5, &p, 0);
522 else if(cistrncmp(p, "freq=", 5) == 0)
523 isa->freq = strtoul(p+5, &p, 0);
529 * the new kernel is already loaded at address `code'
530 * of size `size' and entry point `entry'.
533 reboot(void *entry, void *code, ulong size)
535 void (*f)(ulong, ulong, ulong);
540 * the boot processor is cpu0. execute this function on it
541 * so that the new kernel has the same cpu0.
543 if (m->machno != 0) {
550 * should be the only processor running now
554 /* turn off buffered serial console */
559 /* shutdown devices */
562 /* call off the dog */
568 /* setup reboot trampoline function */
569 f = (void*)REBOOTADDR;
570 memmove(f, rebootcode, sizeof(rebootcode));
576 /* off we go - never to return */
577 (*f)(PADDR(entry), PADDR(code), size);
581 * starting place for first process
587 char buf[2*KNAMELEN];
594 * These are o.k. because rootinit is null.
595 * Then early kproc's will have a root and dot.
597 up->slash = namec("#/", Atodir, 0, 0);
598 pathclose(up->slash->path);
599 up->slash->path = newpath("/");
600 up->dot = cclone(up->slash);
603 i8250console(); /* might be redundant, but harmless */
605 panic("init0: nil serialoq");
609 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
610 ksetenv("terminal", buf, 0);
611 ksetenv("cputype", "arm", 0);
613 ksetenv("service", "cpu", 0);
615 ksetenv("service", "terminal", 0);
617 /* convert plan9.ini variables to #e and #ec */
618 for(i = 0; i < nconf; i++) {
619 ksetenv(confname[i], confval[i], 0);
620 ksetenv(confname[i], confval[i], 1);
624 kproc("alarm", alarmkproc, 0);
630 bootargs(uintptr base)
637 * Push the boot args onto the stack.
638 * The initial value of the user stack must be such
639 * that the total used is larger than the maximum size
640 * of the argument list checked in syscall.
643 p = UINT2PTR(STACKALIGN(base + BY2PG - sizeof(up->s.args) - i));
644 memmove(p, oargb, i);
647 * Now push argc and the argv pointers.
648 * This isn't strictly correct as the code jumped to by
649 * touser in init9.s calls startboot (port/initcode.c) which
651 * startboot(char *argv0, char **argv)
652 * not the usual (int argc, char* argv[]), but argv0 is
653 * unused so it doesn't matter (at the moment...).
655 av = (char**)(p - (oargc+2)*sizeof(char*));
656 ssize = base + BY2PG - PTR2UINT(av);
657 *av++ = (char*)oargc;
658 for(i = 0; i < oargc; i++)
659 *av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
663 * Leave space for the return PC of the
664 * caller of initcode.
666 sp = USTKTOP - ssize - sizeof(void*);
670 * create the first process
680 /* no processes yet */
685 p->egrp = smalloc(sizeof(Egrp));
687 p->fgrp = dupfgrp(nil);
692 kstrdup(&p->text, "*init*");
693 kstrdup(&p->user, eve);
698 p->sched.pc = PTR2UINT(init0);
699 p->sched.sp = PTR2UINT(p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr));
700 p->sched.sp = STACKALIGN(p->sched.sp);
705 * Technically, newpage can't be called here because it
706 * should only be called when in a user context as it may
707 * try to sleep if there are no pages available, but that
708 * shouldn't be the case here.
710 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
713 pg = newpage(1, 0, USTKTOP-BY2PG);
722 s = newseg(SG_TEXT, UTZERO, 1);
724 pg = newpage(1, 0, UTZERO);
727 k = kmap(s->map[0]->pages[0]);
728 memmove(UINT2PTR(VA(k)), initcode, sizeof initcode);
734 Conf conf; /* XXX - must go - gag */
736 Confmem tsmem[nelem(conf.mem)] = {
738 * Memory available to Plan 9:
740 { .base = PHYSDRAM, .limit = PHYSDRAM + Minmem, },
742 ulong memsize = DRAMSIZE;
749 /* back off a little from the end */
750 addr = (uintptr)KADDR(PHYSDRAM + sz - BY2WD);
751 if (probeaddr(addr) >= 0) { /* didn't trap? memory present */
766 if(p = getconf("service")){
767 if(strcmp(p, "cpu") == 0)
769 else if(strcmp(p,"terminal") == 0)
774 * Copy the physical memory configuration to Conf.mem.
776 if(nelem(tsmem) > nelem(conf.mem)){
777 iprint("memory configuration botch\n");
780 if(0 && (p = getconf("*maxmem")) != nil) {
781 memsize = strtoul(p, 0, 0) - PHYSDRAM;
782 if (memsize < 16*MB) /* sanity */
787 * see if all that memory exists; if not, find out how much does.
788 * trapinit must have been called first.
790 if (gotmem(memsize - RESRVDHIMEM) < 0)
791 panic("can't find 1GB of memory");
793 tsmem[0].limit = PHYSDRAM + memsize;
794 memmove(conf.mem, tsmem, sizeof(tsmem));
797 pa = PADDR(PGROUND(PTR2UINT(end)));
800 * we assume that the kernel is at the beginning of one of the
801 * contiguous chunks of memory and fits therein.
803 for(i=0; i<nelem(conf.mem); i++){
804 /* take kernel out of allocatable space */
805 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
806 conf.mem[i].base = pa;
808 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
809 conf.npage += conf.mem[i].npage;
812 conf.upages = (conf.npage*80)/100;
813 conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
815 /* set up other configuration parameters */
816 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
819 if(conf.nproc > 2000)
821 conf.nswap = conf.npage*3;
826 * it's simpler on mp systems to take page-faults early,
827 * on reference, rather than later, on write, which might
828 * require tlb shootdowns.
830 conf.copymode = 1; /* copy on reference */
833 * Guess how much is taken by the large permanent
834 * datastructures. Mntcache and Mntrpc are not accounted for.
836 kpages = conf.npage - conf.upages;
838 kpages -= conf.upages*sizeof(Page)
839 + conf.nproc*sizeof(Proc)
840 + conf.nimage*sizeof(Image)
842 + conf.nswppo*sizeof(Page*);
843 mainmem->maxsize = kpages;
846 * give terminals lots of image memory, too; the dynamic
847 * allocation will balance the load properly, hopefully.
848 * be careful with 32-bit overflow.
850 imagmem->maxsize = kpages;
854 advertwfi(void) /* advertise my wfi status */
857 active.wfi |= 1 << m->machno;
862 unadvertwfi(void) /* do not advertise my wfi status */
865 active.wfi &= ~(1 << m->machno);
875 /* don't go into wfi until my local timer is ticking */
881 /* avoid recursion via ilock, advertise iff this cpu is initialised */
882 if (m->inidlehands == 1 && m->syscall > 0) {
902 * find any cpu other than me currently in wfi.
905 cpu = BI2BY*BY2WD - 1 - clz(active.wfi & ~(1 << m->machno));