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
25 Minmem = 256*MB, /* conservative default */
28 #define isascii(c) ((uchar)(c) > 0 && (uchar)(c) < 0177)
30 uintptr kseg0 = KZERO;
31 Mach* machaddr[MAXMACH];
34 * Option arguments from the command line.
35 * oargv[0] is the boot file.
36 * Optionsinit() is called from multiboot()
37 * or some other machine-dependent place
41 static char* oargv[20];
42 static char oargb[128];
44 static char oenv[4096];
46 static uintptr sp; /* XXX - must go - user stack of init proc */
52 /* store plan9.ini contents here at least until we stash them in #ec */
53 static char confname[MAXCONF][KNAMELEN];
54 static char confval[MAXCONF][MAXCONFLINE];
62 for(i = 0; i < nconf; i++)
63 if(cistrcmp(confname[i], name) == 0)
80 addconf(char *name, char *val)
86 if(val == nil || nconf >= MAXCONF)
89 strecpy(confname[i], confname[i]+sizeof(confname[i]), name);
92 strecpy(confval[i], confval[i]+sizeof(confval[i]), val);
108 /* convert to name=value\n format */
117 error("kernel configuration too large");
118 memmove(BOOTARGS, p, n);
119 memset(BOOTARGS + n, '\n', BOOTARGSLEN - n);
125 * assumes that we have loaded our /cfg/pxe/mac file at 0x1000 with
126 * tftp in u-boot. no longer uses malloc, so can be called early.
133 k = (char *)CONFADDR;
137 for(; k && *k != '\0'; k = next) {
138 if (!isascii(*k)) /* sanity check */
140 next = strchr(k, '\n');
144 if (*k == '\0' || *k == '\n' || *k == '#')
148 continue; /* mal-formed line */
161 o = strecpy(oargb, oargb+sizeof(oargb), s)+1;
162 if(getenv("bootargs", o, o - oargb) != nil)
165 oargblen = strlen(oargb);
166 oargc = tokenize(oargb, oargv, nelem(oargv)-1);
171 getenv(char* name, char* buf, int n)
177 if((e = strchr(p, '=')) == nil)
179 for(q = name; p < e; p++){
184 if(p == e && *q == 0){
185 strecpy(buf, buf+n, e+1);
198 extern char bdata[], edata[], end[], etext[];
199 static ulong vfy = 0xcafebabe;
201 /* l.s has already printed "Plan 9 from Be" */
202 // m = mach; /* now done in l.s */
204 /* realign data seg; apparently -H0 -R4096 does not pad the text seg */
205 if (vfy != 0xcafebabe) {
206 // wave('<'); wave('-');
207 memmove(bdata, etext, edata - bdata);
210 * once data segment is in place, always zero bss since we may
211 * have been loaded by another Plan 9 kernel.
213 memset(edata, 0, end - edata); /* zero BSS */
217 if (vfy != 0xcafebabe)
218 panic("data segment misaligned");
225 optionsinit("/boot/boot boot");
228 /* want plan9.ini to be able to affect memory sizing in confinit */
229 plan9iniinit(); /* before we step on plan9.ini in low memory */
231 trapinit(); /* so confinit can probe memory to size it */
232 confinit(); /* figures out amount of memory */
233 /* xinit prints (if it can), so finish up the banner here. */
235 iprint("l Labs\n\n");
239 mainmem->flags |= POOL_ANTAGONISM /* | POOL_PARANOIA */ ;
242 * Printinit will cause the first malloc call.
243 * (printinit->qopen->malloc) unless any of the
244 * above (like clockinit) do an irqenable, which
246 * If the system dies here it's probably due
247 * to malloc(->xalloc) not being initialised
248 * correctly, or the data segment is misaligned
249 * (it's amazing how far you can get with
250 * things like that completely broken).
252 * (Should be) boilerplate from here on.
255 archreset(); /* configure clock signals */
256 clockinit(); /* start clocks */
260 delay(250); /* let uart catch up */
274 chandevreset(); /* most devices are discovered here */
276 // i8250console(); /* too early; see init0 */
288 // memset(m, 0, sizeof(Mach)); /* done by l.s, now contains stack */
290 machaddr[m->machno] = m;
304 * exit kernel either on a panic or user request
315 isaconfig(char *class, int ctlrno, ISAConf *isa)
320 snprint(cc, sizeof cc, "%s%d", class, ctlrno);
330 isa->nopt = tokenize(p, isa->opt, NISAOPT);
331 for(i = 0; i < isa->nopt; i++){
333 if(cistrncmp(p, "type=", 5) == 0)
335 else if(cistrncmp(p, "port=", 5) == 0)
336 isa->port = strtoul(p+5, &p, 0);
337 else if(cistrncmp(p, "irq=", 4) == 0)
338 isa->irq = strtoul(p+4, &p, 0);
339 else if(cistrncmp(p, "dma=", 4) == 0)
340 isa->dma = strtoul(p+4, &p, 0);
341 else if(cistrncmp(p, "mem=", 4) == 0)
342 isa->mem = strtoul(p+4, &p, 0);
343 else if(cistrncmp(p, "size=", 5) == 0)
344 isa->size = strtoul(p+5, &p, 0);
345 else if(cistrncmp(p, "freq=", 5) == 0)
346 isa->freq = strtoul(p+5, &p, 0);
352 * the new kernel is already loaded at address `code'
353 * of size `size' and entry point `entry'.
356 reboot(void *entry, void *code, ulong size)
358 void (*f)(ulong, ulong, ulong);
363 /* turn off buffered serial console */
368 /* shutdown devices */
371 /* call off the dog */
377 /* setup reboot trampoline function */
378 f = (void*)REBOOTADDR;
379 memmove(f, rebootcode, sizeof(rebootcode));
383 /* off we go - never to return */
384 (*f)(PADDR(entry), PADDR(code), size);
388 * starting place for first process
394 char buf[2*KNAMELEN];
401 * These are o.k. because rootinit is null.
402 * Then early kproc's will have a root and dot.
404 up->slash = namec("#/", Atodir, 0, 0);
405 pathclose(up->slash->path);
406 up->slash->path = newpath("/");
407 up->dot = cclone(up->slash);
409 dmatest(); /* needs `up' set, so can't do it earlier */
411 i8250console(); /* might be redundant, but harmless */
413 panic("init0: nil serialoq");
417 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
418 ksetenv("terminal", buf, 0);
419 ksetenv("cputype", "arm", 0);
421 ksetenv("service", "cpu", 0);
423 ksetenv("service", "terminal", 0);
425 /* convert plan9.ini variables to #e and #ec */
426 for(i = 0; i < nconf; i++) {
427 ksetenv(confname[i], confval[i], 0);
428 ksetenv(confname[i], confval[i], 1);
432 kproc("alarm", alarmkproc, 0);
437 bootargs(uintptr base)
444 * Push the boot args onto the stack.
445 * The initial value of the user stack must be such
446 * that the total used is larger than the maximum size
447 * of the argument list checked in syscall.
450 p = (void*)(STACKALIGN(base + BY2PG - sizeof(up->s.args) - i));
451 memmove(p, oargb, i);
454 * Now push argc and the argv pointers.
455 * This isn't strictly correct as the code jumped to by
456 * touser in init9.s calls startboot (port/initcode.c) which
458 * startboot(char *argv0, char **argv)
459 * not the usual (int argc, char* argv[]), but argv0 is
460 * unused so it doesn't matter (at the moment...).
462 av = (char**)(p - (oargc+2)*sizeof(char*));
463 ssize = base + BY2PG - (uintptr)av;
464 *av++ = (char*)oargc;
465 for(i = 0; i < oargc; i++)
466 *av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
470 * Leave space for the return PC of the
471 * caller of initcode.
473 sp = USTKTOP - ssize - sizeof(void*);
477 * create the first process
487 /* no processes yet */
492 p->egrp = smalloc(sizeof(Egrp));
494 p->fgrp = dupfgrp(nil);
499 kstrdup(&p->text, "*init*");
500 kstrdup(&p->user, eve);
505 p->sched.pc = (uintptr)init0;
506 p->sched.sp = (uintptr)p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr);
507 p->sched.sp = STACKALIGN(p->sched.sp);
512 * Technically, newpage can't be called here because it
513 * should only be called when in a user context as it may
514 * try to sleep if there are no pages available, but that
515 * shouldn't be the case here.
517 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
520 pg = newpage(1, 0, USTKTOP-BY2PG);
529 s = newseg(SG_TEXT, UTZERO, 1);
531 pg = newpage(1, 0, UTZERO);
534 k = kmap(s->map[0]->pages[0]);
535 memmove((void*)VA(k), initcode, sizeof initcode);
541 Conf conf; /* XXX - must go - gag */
543 Confmem omapmem[nelem(conf.mem)] = {
545 * Memory available to Plan 9:
547 { .base = PHYSDRAM, .limit = PHYSDRAM + Minmem, },
549 ulong memsize = Minmem;
556 addr = PHYSDRAM + sz - BY2WD;
558 if (probeaddr(addr) >= 0) {
574 * Copy the physical memory configuration to Conf.mem.
576 if(nelem(omapmem) > nelem(conf.mem)){
577 iprint("memory configuration botch\n");
580 if((p = getconf("*maxmem")) != nil) {
581 memsize = strtoul(p, 0, 0) - PHYSDRAM;
582 if (memsize < 16*MB) /* sanity */
587 * see if all that memory exists; if not, find out how much does.
588 * trapinit must have been called first.
590 if (gotmem(memsize) < 0 && gotmem(256*MB) < 0 && gotmem(128*MB) < 0) {
591 iprint("can't find any memory, assuming %dMB\n", Minmem / MB);
595 omapmem[0].limit = PHYSDRAM + memsize;
596 memmove(conf.mem, omapmem, sizeof(omapmem));
599 pa = PADDR(PGROUND((uintptr)end));
602 * we assume that the kernel is at the beginning of one of the
603 * contiguous chunks of memory and fits therein.
605 for(i=0; i<nelem(conf.mem); i++){
606 /* take kernel out of allocatable space */
607 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
608 conf.mem[i].base = pa;
610 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
611 conf.npage += conf.mem[i].npage;
614 conf.upages = (conf.npage*80)/100;
615 conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
617 /* only one processor */
620 /* set up other configuration parameters */
621 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
624 if(conf.nproc > 2000)
626 conf.nswap = conf.npage*3;
630 conf.copymode = 0; /* copy on write */
633 * Guess how much is taken by the large permanent
634 * datastructures. Mntcache and Mntrpc are not accounted for.
636 kpages = conf.npage - conf.upages;
638 kpages -= conf.upages*sizeof(Page)
639 + conf.nproc*sizeof(Proc)
640 + conf.nimage*sizeof(Image)
642 + conf.nswppo*sizeof(Page*);
643 mainmem->maxsize = kpages;
646 * give terminals lots of image memory, too; the dynamic
647 * allocation will balance the load properly, hopefully.
648 * be careful with 32-bit overflow.
650 imagmem->maxsize = kpages;
656 cmpswap(long *addr, long old, long new)
658 return cas32(addr, old, new);
662 setupwatchpts(Proc *, Watchpt *, int n)
665 error("no watchpoints");