3 #include "../port/lib.h"
13 /* Firmware compatibility */
14 #define Minfirmrev 326770
15 #define Minfirmdate "22 Jul 2012"
18 * Where configuration info is left for the loaded programme.
20 #define BOOTARGS ((char*)CONFADDR)
21 #define BOOTARGSLEN (MACHADDR-CONFADDR)
23 #define MAXCONFLINE 160
25 uintptr kseg0 = KZERO;
26 Mach* machaddr[MAXMACH];
28 ulong memsize = 128*1024*1024;
31 * Option arguments from the command line.
32 * oargv[0] is the boot file.
35 static char* oargv[20];
36 static char oargb[128];
39 static uintptr sp; /* XXX - must go - user stack of init proc */
41 /* store plan9.ini contents here at least until we stash them in #ec */
42 static char confname[MAXCONF][KNAMELEN];
43 static char confval[MAXCONF][MAXCONFLINE];
46 typedef struct Atag Atag;
48 u32int size; /* size of atag in words, including this header */
49 u32int tag; /* atag type */
51 u32int data[1]; /* actually [size-2] */
58 char cmdline[1]; /* actually [4*(size-2)] */
63 AtagNone = 0x00000000,
64 AtagCore = 0x54410001,
66 AtagCmdline = 0x54410009,
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);
103 strecpy(confval[i], confval[i]+sizeof(confval[i]), val);
119 /* convert to name=value\n format */
128 error("kernel configuration too large");
129 memmove(BOOTARGS, p, n);
130 memset(BOOTARGS + n, '\n', BOOTARGSLEN - n);
136 plan9iniinit(char *s, int cmdline)
142 if((c = *s) < ' ' || c >= 0x80)
145 n = tokenize(s, toks, MAXCONF);
147 n = getfields(s, toks, MAXCONF, 1, "\n");
148 for(i = 0; i < n; i++){
149 if(toks[i][0] == '#')
151 v = strchr(toks[i], '=');
164 if(a->tag != AtagCore){
165 plan9iniinit((char*)a, 0);
168 while(a->tag != AtagNone){
171 /* use only first bank */
172 if(conf.mem[0].limit == 0 && a->mem.size != 0){
173 memsize = a->mem.size;
174 conf.mem[0].base = a->mem.base;
175 conf.mem[0].limit = a->mem.base + memsize;
179 n = (a->size * sizeof(u32int)) - offsetof(Atag, cmdline[0]);
180 if(a->cmdline + n < BOOTARGS + BOOTARGSLEN)
183 BOOTARGS[BOOTARGSLEN-1] = 0;
184 plan9iniinit(a->cmdline, 1);
187 a = (Atag*)((u32int*)a + a->size);
195 machaddr[m->machno] = m;
211 strecpy(oargb, oargb+sizeof(oargb), s);
213 oargblen = strlen(oargb);
214 oargc = tokenize(oargb, oargv, nelem(oargv)-1);
222 memset(&seg, 0, sizeof seg);
223 seg.attr = SG_PHYSICAL;
225 seg.pa = (VIRTIO+0x200000);
234 extern char edata[], end[];
239 memset(edata, 0, end - edata); /* clear bss */
243 optionsinit("/boot/boot boot");
246 ataginit((Atag*)BOOTARGS);
247 confinit(); /* figures out amount of memory */
252 print("\nPlan 9 from Bell Labs\n");
254 print("firmware: rev %d\n", rev);
255 if(rev < Minfirmrev){
256 print("Sorry, firmware (start.elf) must be at least rev %d (%s)\n",
257 Minfirmrev, Minfirmdate);
271 chandevreset(); /* most devices are discovered here */
277 assert(0); /* shouldn't have returned */
281 * starting place for first process
287 char buf[2*KNAMELEN];
294 * These are o.k. because rootinit is null.
295 * Then early kproc's will have a root and dot.
297 up->slash = namec("#/", Atodir, 0, 0);
298 pathclose(up->slash->path);
299 up->slash->path = newpath("/");
300 up->dot = cclone(up->slash);
305 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
306 ksetenv("terminal", buf, 0);
307 ksetenv("cputype", "arm", 0);
309 ksetenv("service", "cpu", 0);
311 ksetenv("service", "terminal", 0);
312 snprint(buf, sizeof(buf), "-a %s", getethermac());
313 ksetenv("etherargs", buf, 0);
315 /* convert plan9.ini variables to #e and #ec */
316 for(i = 0; i < nconf; i++) {
317 ksetenv(confname[i], confval[i], 0);
318 ksetenv(confname[i], confval[i], 1);
322 kproc("alarm", alarmkproc, 0);
324 assert(0); /* shouldn't have returned */
328 bootargs(uintptr base)
335 * Push the boot args onto the stack.
336 * The initial value of the user stack must be such
337 * that the total used is larger than the maximum size
338 * of the argument list checked in syscall.
341 p = UINT2PTR(STACKALIGN(base + BY2PG - sizeof(Tos) - i));
342 memmove(p, oargb, i);
345 * Now push the argv pointers.
346 * The code jumped to by touser in lproc.s expects arguments
347 * main(char* argv0, ...)
349 * startboot("/boot/boot", &argv0)
350 * not the usual (int argc, char* argv[])
352 av = (char**)(p - (oargc+1)*sizeof(char*));
353 ssize = base + BY2PG - PTR2UINT(av);
354 for(i = 0; i < oargc; i++)
355 *av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
357 sp = USTKTOP - ssize;
361 * create the first process
371 /* no processes yet */
376 p->egrp = smalloc(sizeof(Egrp));
378 p->fgrp = dupfgrp(nil);
383 kstrdup(&p->text, "*init*");
384 kstrdup(&p->user, eve);
389 p->sched.pc = PTR2UINT(init0);
390 p->sched.sp = PTR2UINT(p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr));
391 p->sched.sp = STACKALIGN(p->sched.sp);
396 * Technically, newpage can't be called here because it
397 * should only be called when in a user context as it may
398 * try to sleep if there are no pages available, but that
399 * shouldn't be the case here.
401 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
404 pg = newpage(1, 0, USTKTOP-BY2PG);
413 s = newseg(SG_TEXT, UTZERO, 1);
415 pg = newpage(1, 0, UTZERO);
416 memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
418 k = kmap(s->map[0]->pages[0]);
419 memmove(UINT2PTR(VA(k)), initcode, sizeof initcode);
433 if(0 && (p = getconf("service")) != nil){
434 if(strcmp(p, "cpu") == 0)
436 else if(strcmp(p,"terminal") == 0)
439 if((p = getconf("*maxmem")) != nil){
440 memsize = strtoul(p, 0, 0) - PHYSDRAM;
441 if (memsize < 16*MB) /* sanity */
445 getramsize(&conf.mem[0]);
446 if(conf.mem[0].limit == 0){
447 conf.mem[0].base = PHYSDRAM;
448 conf.mem[0].limit = PHYSDRAM + memsize;
450 conf.mem[0].limit = conf.mem[0].base + memsize;
453 pa = PADDR(PGROUND(PTR2UINT(end)));
456 * we assume that the kernel is at the beginning of one of the
457 * contiguous chunks of memory and fits therein.
459 for(i=0; i<nelem(conf.mem); i++){
460 /* take kernel out of allocatable space */
461 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
462 conf.mem[i].base = pa;
464 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
465 conf.npage += conf.mem[i].npage;
468 conf.upages = (conf.npage*80)/100;
469 conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
471 /* only one processor */
474 /* set up other configuration parameters */
475 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
478 if(conf.nproc > 2000)
480 conf.nswap = conf.npage*3;
484 conf.copymode = 0; /* copy on write */
487 * Guess how much is taken by the large permanent
488 * datastructures. Mntcache and Mntrpc are not accounted for
491 kpages = conf.npage - conf.upages;
493 kpages -= conf.upages*sizeof(Page)
494 + conf.nproc*sizeof(Proc)
495 + conf.nimage*sizeof(Image)
497 + conf.nswppo*sizeof(Page);
498 mainmem->maxsize = kpages;
501 * give terminals lots of image memory, too; the dynamic
502 * allocation will balance the load properly, hopefully.
503 * be careful with 32-bit overflow.
505 imagmem->maxsize = kpages;
510 shutdown(int ispanic)
516 active.ispanic = ispanic;
517 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
519 once = active.machs & (1<<m->machno);
520 active.machs &= ~(1<<m->machno);
525 iprint("cpu%d: exiting\n", m->machno);
527 for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
529 if(active.machs == 0 && consactive() == 0)
536 * exit kernel either on a panic or user request
547 * stub for ../omap/devether.c
550 isaconfig(char *class, int ctlrno, ISAConf *isa)
554 return strcmp(class, "ether") == 0;
558 * the new kernel is already loaded at address `code'
559 * of size `size' and entry point `entry'.
562 reboot(void *entry, void *code, ulong size)
564 void (*f)(ulong, ulong, ulong);
566 print("starting reboot...");
571 * should be the only processor running now
574 print("reboot entry %#lux code %#lux size %ld\n",
575 PADDR(entry), PADDR(code), size);
578 /* turn off buffered serial console */
583 /* shutdown devices */
586 /* stop the clock (and watchdog if any) */
592 /* setup reboot trampoline function */
593 f = (void*)REBOOTADDR;
594 memmove(f, rebootcode, sizeof(rebootcode));
597 /* off we go - never to return */
598 (*f)(PADDR(entry), PADDR(code), size);
600 iprint("loaded kernel returned!\n");
606 cmpswap(long *addr, long old, long new)
608 return cas32(addr, old, new);