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: ⋯ */
461 * starting a cpu will eventually result in it calling schedinit,
462 * so everything necessary to run user processes should be set up
463 * before starting secondary cpus.
466 /* SMP & FW are already on when we get here; u-boot set them? */
467 for (cpu = 1; cpu < navailcpus; cpu++)
468 if (startcpu(cpu) < 0)
469 panic("cpu%d didn't start", cpu);
473 panic("cpu%d: schedinit returned", m->machno);
477 * exit kernel either on a panic or user request
495 isaconfig(char *class, int ctlrno, ISAConf *isa)
500 snprint(cc, sizeof cc, "%s%d", class, ctlrno);
510 isa->nopt = tokenize(p, isa->opt, NISAOPT);
511 for(i = 0; i < isa->nopt; i++){
513 if(cistrncmp(p, "type=", 5) == 0)
515 else if(cistrncmp(p, "port=", 5) == 0)
516 isa->port = strtoul(p+5, &p, 0);
517 else if(cistrncmp(p, "irq=", 4) == 0)
518 isa->irq = strtoul(p+4, &p, 0);
519 else if(cistrncmp(p, "dma=", 4) == 0)
520 isa->dma = strtoul(p+4, &p, 0);
521 else if(cistrncmp(p, "mem=", 4) == 0)
522 isa->mem = strtoul(p+4, &p, 0);
523 else if(cistrncmp(p, "size=", 5) == 0)
524 isa->size = strtoul(p+5, &p, 0);
525 else if(cistrncmp(p, "freq=", 5) == 0)
526 isa->freq = strtoul(p+5, &p, 0);
532 * the new kernel is already loaded at address `code'
533 * of size `size' and entry point `entry'.
536 reboot(void *entry, void *code, ulong size)
538 void (*f)(ulong, ulong, ulong);
543 * the boot processor is cpu0. execute this function on it
544 * so that the new kernel has the same cpu0.
546 if (m->machno != 0) {
553 * should be the only processor running now
557 /* turn off buffered serial console */
562 /* shutdown devices */
565 /* call off the dog */
571 /* setup reboot trampoline function */
572 f = (void*)REBOOTADDR;
573 memmove(f, rebootcode, sizeof(rebootcode));
579 /* off we go - never to return */
580 (*f)(PADDR(entry), PADDR(code), size);
584 * starting place for first process
590 char buf[2*KNAMELEN];
597 * These are o.k. because rootinit is null.
598 * Then early kproc's will have a root and dot.
600 up->slash = namec("#/", Atodir, 0, 0);
601 pathclose(up->slash->path);
602 up->slash->path = newpath("/");
603 up->dot = cclone(up->slash);
606 i8250console(); /* might be redundant, but harmless */
608 panic("init0: nil serialoq");
612 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
613 ksetenv("terminal", buf, 0);
614 ksetenv("cputype", "arm", 0);
616 ksetenv("service", "cpu", 0);
618 ksetenv("service", "terminal", 0);
620 /* convert plan9.ini variables to #e and #ec */
621 for(i = 0; i < nconf; i++) {
622 ksetenv(confname[i], confval[i], 0);
623 ksetenv(confname[i], confval[i], 1);
627 kproc("alarm", alarmkproc, 0);
633 bootargs(uintptr base)
640 * Push the boot args onto the stack.
641 * The initial value of the user stack must be such
642 * that the total used is larger than the maximum size
643 * of the argument list checked in syscall.
646 p = (void*)(STACKALIGN(base + BY2PG - sizeof(up->s.args) - i));
647 memmove(p, oargb, i);
650 * Now push argc and the argv pointers.
651 * This isn't strictly correct as the code jumped to by
652 * touser in init9.s calls startboot (port/initcode.c) which
654 * startboot(char *argv0, char **argv)
655 * not the usual (int argc, char* argv[]), but argv0 is
656 * unused so it doesn't matter (at the moment...).
658 av = (char**)(p - (oargc+2)*sizeof(char*));
659 ssize = base + BY2PG - (uintptr)av;
660 *av++ = (char*)oargc;
661 for(i = 0; i < oargc; i++)
662 *av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
666 * Leave space for the return PC of the
667 * caller of initcode.
669 sp = USTKTOP - ssize - sizeof(void*);
673 * create the first process
683 /* no processes yet */
688 p->egrp = smalloc(sizeof(Egrp));
690 p->fgrp = dupfgrp(nil);
695 kstrdup(&p->text, "*init*");
696 kstrdup(&p->user, eve);
701 p->sched.pc = (uintptr)init0;
702 p->sched.sp = (uintptr)p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr);
703 p->sched.sp = STACKALIGN(p->sched.sp);
708 * Technically, newpage can't be called here because it
709 * should only be called when in a user context as it may
710 * try to sleep if there are no pages available, but that
711 * shouldn't be the case here.
713 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
716 pg = newpage(1, 0, USTKTOP-BY2PG);
725 s = newseg(SG_TEXT, UTZERO, 1);
727 pg = newpage(1, 0, UTZERO);
730 k = kmap(s->map[0]->pages[0]);
731 memmove((void*)VA(k), initcode, sizeof initcode);
737 Conf conf; /* XXX - must go - gag */
739 Confmem tsmem[nelem(conf.mem)] = {
741 * Memory available to Plan 9:
743 { .base = PHYSDRAM, .limit = PHYSDRAM + Minmem, },
745 ulong memsize = DRAMSIZE;
752 /* back off a little from the end */
753 addr = (uintptr)KADDR(PHYSDRAM + sz - BY2WD);
754 if (probeaddr(addr) >= 0) { /* didn't trap? memory present */
769 if(p = getconf("service")){
770 if(strcmp(p, "cpu") == 0)
772 else if(strcmp(p,"terminal") == 0)
777 * Copy the physical memory configuration to Conf.mem.
779 if(nelem(tsmem) > nelem(conf.mem)){
780 iprint("memory configuration botch\n");
783 if(0 && (p = getconf("*maxmem")) != nil) {
784 memsize = strtoul(p, 0, 0) - PHYSDRAM;
785 if (memsize < 16*MB) /* sanity */
790 * see if all that memory exists; if not, find out how much does.
791 * trapinit must have been called first.
793 if (gotmem(memsize - RESRVDHIMEM) < 0)
794 panic("can't find 1GB of memory");
796 tsmem[0].limit = PHYSDRAM + memsize;
797 memmove(conf.mem, tsmem, sizeof(tsmem));
800 pa = PADDR(PGROUND((uintptr)end));
803 * we assume that the kernel is at the beginning of one of the
804 * contiguous chunks of memory and fits therein.
806 for(i=0; i<nelem(conf.mem); i++){
807 /* take kernel out of allocatable space */
808 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
809 conf.mem[i].base = pa;
811 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
812 conf.npage += conf.mem[i].npage;
815 conf.upages = (conf.npage*80)/100;
816 conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
818 /* set up other configuration parameters */
819 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
822 if(conf.nproc > 2000)
824 conf.nswap = conf.npage*3;
829 * it's simpler on mp systems to take page-faults early,
830 * on reference, rather than later, on write, which might
831 * require tlb shootdowns.
833 conf.copymode = 1; /* copy on reference */
836 * Guess how much is taken by the large permanent
837 * datastructures. Mntcache and Mntrpc are not accounted for.
839 kpages = conf.npage - conf.upages;
841 kpages -= conf.upages*sizeof(Page)
842 + conf.nproc*sizeof(Proc)
843 + conf.nimage*sizeof(Image)
845 + conf.nswppo*sizeof(Page*);
846 mainmem->maxsize = kpages;
849 * give terminals lots of image memory, too; the dynamic
850 * allocation will balance the load properly, hopefully.
851 * be careful with 32-bit overflow.
853 imagmem->maxsize = kpages;
857 advertwfi(void) /* advertise my wfi status */
860 active.wfi |= 1 << m->machno;
865 unadvertwfi(void) /* do not advertise my wfi status */
868 active.wfi &= ~(1 << m->machno);
878 /* don't go into wfi until my local timer is ticking */
884 /* avoid recursion via ilock, advertise iff this cpu is initialised */
885 if (m->inidlehands == 1 && m->syscall > 0) {
905 * find any cpu other than me currently in wfi.
908 cpu = BI2BY*BY2WD - 1 - clz(active.wfi & ~(1 << m->machno));
915 setupwatchpts(Proc *, Watchpt *, int n)
918 error("no watchpoints");