2 #include "../port/lib.h"
14 * Where configuration info is left for the loaded programme.
15 * This will turn into a structure as more is done by the boot loader
16 * (e.g. why parse the .ini file twice?).
17 * There are 3584 bytes available at CONFADDR.
19 #define BOOTARGS ((char*)CONFADDR)
20 #define BOOTARGSLEN (16*KiB) /* limit in devenv.c */
22 #define MAXCONFLINE 160
24 #define isascii(c) ((uchar)(c) > 0 && (uchar)(c) < 0177)
26 uintptr kseg0 = KZERO;
27 Mach* machaddr[MAXMACH];
30 * Option arguments from the command line.
31 * oargv[0] is the boot file.
32 * Optionsinit() is called from multiboot()
33 * or some other machine-dependent place
37 static char* oargv[20];
38 static char oargb[128];
40 static char oenv[4096];
42 static uintptr sp; /* XXX - must go - user stack of init proc */
47 /* store plan9.ini contents here at least until we stash them in #ec */
48 static char confname[MAXCONF][KNAMELEN];
49 static char confval[MAXCONF][MAXCONFLINE];
53 uchar sandbox[64*1024+BY2PG];
61 for(i = 0; i < nconf; i++)
62 if(cistrcmp(confname[i], name) == 0)
79 addconf(char *name, char *val)
85 if(val == nil || nconf >= MAXCONF)
88 strecpy(confname[i], confname[i]+sizeof(confname[i]), name);
91 strecpy(confval[i], confval[i]+sizeof(confval[i]), val);
107 /* convert to name=value\n format */
116 error("kernel configuration too large");
117 memmove(BOOTARGS, p, n);
123 * assumes that we have loaded our /cfg/pxe/mac file at 0x1000 with
124 * tftp in u-boot. no longer uses malloc, so can be called early.
131 k = (char *)CONFADDR;
135 for(; k && *k != '\0'; k = next) {
136 if (!isascii(*k)) /* sanity check */
138 next = strchr(k, '\n');
142 if (*k == '\0' || *k == '\n' || *k == '#')
146 continue; /* mal-formed line */
158 o = strecpy(oargb, oargb+sizeof(oargb), s)+1;
159 if(getenv("bootargs", o, o - oargb) != nil)
162 oargblen = strlen(oargb);
163 oargc = tokenize(oargb, oargv, nelem(oargv)-1);
168 getenv(char* name, char* buf, int n)
174 if((e = strchr(p, '=')) == nil)
176 for(q = name; p < e; p++){
181 if(p == e && *q == 0){
182 strecpy(buf, buf+n, e+1);
193 typedef struct Spiregs Spiregs;
195 ulong ictl; /* interface ctl */
196 ulong icfg; /* interface config */
197 ulong out; /* data out */
198 ulong in; /* data in */
199 ulong ic; /* interrupt cause */
200 ulong im; /* interrupt mask */
202 ulong dwrcfg; /* direct write config */
203 ulong dwrhdr; /* direct write header */
208 Csnact = 1<<0, /* serial memory activated */
211 Bytelen = 1<<5, /* 2^(this_bit) bytes per transfer */
212 Dirrdcmd= 1<<10, /* flag: fast read */
216 dumpbytes(uchar *bp, long max)
219 for (; max > 0; max--)
220 iprint("%02.2ux ", *bp++);
227 Spiregs *rp = (Spiregs *)soc.spi;
231 rp->icfg |= Dirrdcmd | 3<<8; /* fast reads, 4-byte addresses */
232 rp->icfg &= ~Bytelen; /* one-byte reads */
235 // print("spi flash at %#ux: memory reads enabled\n", PHYSSPIFLASH);
238 void archconsole(void);
242 isaconfig(char *, int, ISAConf *)
248 * entered from l.s with mmu enabled.
250 * we may have to realign the data segment; apparently 5l -H0 -R4096
251 * does not pad the text segment. on the other hand, we may have been
252 * loaded by another kernel.
254 * be careful not to touch the data segment until we know it's aligned.
259 extern char bdata[], edata[], end[], etext[];
260 static ulong vfy = 0xcafebabe;
263 if (vfy != 0xcafebabe)
264 memmove(bdata, etext, edata - bdata);
265 if (vfy != 0xcafebabe) {
267 panic("misaligned data segment");
269 memset(edata, 0, end - edata); /* zero bss */
277 optionsinit("/boot/boot boot");
282 /* want plan9.ini to be able to affect memory sizing in confinit */
283 plan9iniinit(); /* before we step on plan9.ini in low memory */
286 /* xinit would print if it could */
290 * Printinit will cause the first malloc call.
291 * (printinit->qopen->malloc) unless any of the
292 * above (like clockintr) do an irqenable, which
294 * If the system dies here it's probably due
295 * to malloc(->xalloc) not being initialised
296 * correctly, or the data segment is misaligned
297 * (it's amazing how far you can get with
298 * things like that completely broken).
300 * (Should be) boilerplate from here on.
306 uartkirkwoodconsole();
307 /* only now can we print */
308 print("from Bell Labs\n\n");
311 print("sandbox: 64K at physical %#lux, mapped to 0xf10b0000\n",
312 PADDR((uintptr)sandbox & ~(BY2PG-1)));
322 chandevreset(); /* most devices are discovered here */
328 panic("schedinit returned");
336 cputype2name(name, sizeof name);
337 print("cpu%d: %lldMHz ARM %s\n", m->machno, m->cpuhz/1000000, name);
343 memset(m, 0, sizeof(Mach));
345 machaddr[m->machno] = m;
359 shutdown(int ispanic)
365 active.ispanic = ispanic;
366 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
368 once = active.machs & (1<<m->machno);
369 active.machs &= ~(1<<m->machno);
374 iprint("cpu%d: exiting\n", m->machno);
376 for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
378 if(active.machs == 0 && consactive() == 0)
385 * exit kernel either on a panic or user request
396 * the new kernel is already loaded at address `code'
397 * of size `size' and entry point `entry'.
400 reboot(void *entry, void *code, ulong size)
402 void (*f)(ulong, ulong, ulong);
404 iprint("starting reboot...");
410 * should be the only processor running now
413 print("shutting down...\n");
416 /* turn off buffered serial console */
419 /* shutdown devices */
422 /* call off the dog */
427 /* setup reboot trampoline function */
428 f = (void*)REBOOTADDR;
429 memmove(f, rebootcode, sizeof(rebootcode));
433 print("rebooting...");
434 iprint("entry %#lux code %#lux size %ld\n",
435 PADDR(entry), PADDR(code), size);
436 delay(100); /* wait for uart to quiesce */
438 /* off we go - never to return */
441 (*f)(PADDR(entry), PADDR(code), size);
443 iprint("loaded kernel returned!\n");
449 * starting place for first process
455 char buf[2*KNAMELEN];
463 * These are o.k. because rootinit is null.
464 * Then early kproc's will have a root and dot.
466 up->slash = namec("#/", Atodir, 0, 0);
467 pathclose(up->slash->path);
468 up->slash->path = newpath("/");
469 up->dot = cclone(up->slash);
474 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
475 ksetenv("terminal", buf, 0);
476 ksetenv("cputype", "arm", 0);
478 ksetenv("service", "cpu", 0);
480 ksetenv("service", "terminal", 0);
482 /* convert plan9.ini variables to #e and #ec */
483 for(i = 0; i < nconf; i++) {
484 ksetenv(confname[i], confval[i], 0);
485 ksetenv(confname[i], confval[i], 1);
489 kproc("alarm", alarmkproc, 0);
495 bootargs(uintptr base)
502 * Push the boot args onto the stack.
503 * The initial value of the user stack must be such
504 * that the total used is larger than the maximum size
505 * of the argument list checked in syscall.
508 p = UINT2PTR(STACKALIGN(base + BY2PG - sizeof(up->s.args) - i));
509 memmove(p, oargb, i);
512 * Now push argc and the argv pointers.
513 * This isn't strictly correct as the code jumped to by
514 * touser in init9.s calls startboot (port/initcode.c) which
516 * startboot(char *argv0, char **argv)
517 * not the usual (int argc, char* argv[]), but argv0 is
518 * unused so it doesn't matter (at the moment...).
520 av = (char**)(p - (oargc+2)*sizeof(char*));
521 ssize = base + BY2PG - PTR2UINT(av);
522 *av++ = (char*)oargc;
523 for(i = 0; i < oargc; i++)
524 *av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
528 * Leave space for the return PC of the
529 * caller of initcode.
531 sp = USTKTOP - ssize - sizeof(void*);
535 * create the first process
545 /* no processes yet */
550 p->egrp = smalloc(sizeof(Egrp));
552 p->fgrp = dupfgrp(nil);
557 kstrdup(&p->text, "*init*");
558 kstrdup(&p->user, eve);
563 p->sched.pc = PTR2UINT(init0);
564 p->sched.sp = PTR2UINT(p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr));
565 p->sched.sp = STACKALIGN(p->sched.sp);
570 * Technically, newpage can't be called here because it
571 * should only be called when in a user context as it may
572 * try to sleep if there are no pages available, but that
573 * shouldn't be the case here.
575 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
577 pg = newpage(1, 0, USTKTOP-BY2PG);
586 s = newseg(SG_TEXT, UTZERO, 1);
589 pg = newpage(1, 0, UTZERO);
592 k = kmap(s->map[0]->pages[0]);
593 memmove(UINT2PTR(VA(k)), initcode, sizeof initcode);
599 Conf conf; /* XXX - must go - gag */
601 Confmem sheevamem[] = {
603 * Memory available to Plan 9:
604 * the 8K is reserved for ethernet dma access violations to scribble on.
606 { .base = 0, .limit = 512*MB - 8*1024, },
617 * Copy the physical memory configuration to Conf.mem.
619 if(nelem(sheevamem) > nelem(conf.mem)){
620 iprint("memory configuration botch\n");
623 memmove(conf.mem, sheevamem, sizeof(sheevamem));
626 pa = PADDR(PGROUND(PTR2UINT(end)));
629 * we assume that the kernel is at the beginning of one of the
630 * contiguous chunks of memory and fits therein.
632 for(i=0; i<nelem(conf.mem); i++){
633 /* take kernel out of allocatable space */
634 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
635 conf.mem[i].base = pa;
637 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
638 conf.npage += conf.mem[i].npage;
641 conf.upages = (conf.npage*90)/100;
642 conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
644 /* only one processor */
647 /* set up other configuration parameters */
648 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
651 if(conf.nproc > 2000)
653 conf.nswap = conf.npage*3;
657 conf.copymode = 0; /* copy on write */
660 * Guess how much is taken by the large permanent
661 * datastructures. Mntcache and Mntrpc are not accounted for.
663 kpages = conf.npage - conf.upages;
665 kpages -= conf.upages*sizeof(Page)
666 + conf.nproc*sizeof(Proc)
667 + conf.nimage*sizeof(Image)
669 + conf.nswppo*sizeof(Page*);
670 mainmem->maxsize = kpages;
673 * give terminals lots of image memory, too; the dynamic
674 * allocation will balance the load properly, hopefully.
675 * be careful with 32-bit overflow.
677 imagmem->maxsize = kpages;
681 cmpswap(long *addr, long old, long new)
683 return cas32(addr, old, new);