/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ #include "u.h"#include "../port/lib.h"#include "mem.h"#include "dat.h"#include "fns.h"#include "io.h" #include "init.h"#include <pool.h>#include <tos.h> #include "reboot.h" /* * Where configuration info is left for the loaded programme. * This will turn into a structure as more is done by the boot loader * (e.g. why parse the .ini file twice?). * There are 3584 bytes available at CONFADDR. */#define BOOTARGS ((char*)CONFADDR)#define BOOTARGSLEN (16*KiB) /* limit in devenv.c */#define MAXCONF 64#define MAXCONFLINE 160 enum { Minmem = 256*MB, /* conservative default */ /* space for syscall args, return PC, top-of-stack struct */ Ustkheadroom = sizeof(Sargs) + sizeof(uintptr) + sizeof(Tos),}; #define isascii(c) ((uchar)(c) > 0 && (uchar)(c) < 0177) uintptr kseg0 = KZERO;Mach* machaddr[MAXMACH]; /* * Option arguments from the command line. * oargv[0] is the boot file. * Optionsinit() is called from multiboot() * or some other machine-dependent place * to set it all up. */static int oargc;static char* oargv[20];static char oargb[128];static int oargblen;static char oenv[4096]; static uintptr sp; /* XXX - must go - user stack of init proc */ int vflag;int normalprint;char debug[256]; /* store plan9.ini contents here at least until we stash them in #ec */static char confname[MAXCONF][KNAMELEN];static char confval[MAXCONF][MAXCONFLINE];static int nconf; static intfindconf(char *name){ int i; for(i = 0; i < nconf; i++) if(cistrcmp(confname[i], name) == 0) return i; return -1;} char*getconf(char *name){ int i; i = findconf(name); if(i >= 0) return confval[i]; return nil;} voidaddconf(char *name, char *val){ int i; i = findconf(name); if(i < 0){ if(val == nil || nconf >= MAXCONF) return; i = nconf++; strecpy(confname[i], confname[i]+sizeof(confname[i]), name); }// confval[i] = val; strecpy(confval[i], confval[i]+sizeof(confval[i]), val);} static voidwriteconf(void){ char *p, *q; int n; p = getconfenv(); if(waserror()) { free(p); nexterror(); } /* convert to name=value\n format */ for(q=p; *q; q++) { q += strlen(q); *q = '='; q += strlen(q); *q = '\n'; } n = q - p + 1; if(n >= BOOTARGSLEN) error("kernel configuration too large"); memmove(BOOTARGS, p, n); memset(BOOTARGS + n, '\n', BOOTARGSLEN - n); poperror(); free(p);} /* * assumes that we have loaded our /cfg/pxe/mac file at 0x1000 with * tftp in u-boot. no longer uses malloc, so can be called early. */static voidplan9iniinit(void){ char *k, *v, *next; k = (char *)CONFADDR; if(!isascii(*k)) return; for(; k && *k != '\0'; k = next) { if (!isascii(*k)) /* sanity check */ break; next = strchr(k, '\n'); if (next) *next++ = '\0'; if (*k == '\0' || *k == '\n' || *k == '#') continue; v = strchr(k, '='); if(v == nil) continue; /* mal-formed line */ *v++ = '\0'; addconf(k, v); }} static voidoptionsinit(char* s){ char *o; strcpy(oenv, ""); o = strecpy(oargb, oargb+sizeof(oargb), s)+1; if(getenv("bootargs", o, o - oargb) != nil) *(o-1) = ' '; oargblen = strlen(oargb); oargc = tokenize(oargb, oargv, nelem(oargv)-1); oargv[oargc] = nil;} char*getenv(char* name, char* buf, int n){ char *e, *p, *q; p = oenv; while(*p != 0){ if((e = strchr(p, '=')) == nil) break; for(q = name; p < e; p++){ if(*p != *q) break; q++; } if(p == e && *q == 0){ strecpy(buf, buf+n, e+1); return buf; } p += strlen(p)+1; } return nil;} voidmain(void){// int i; extern char bdata[], edata[], end[], etext[]; static ulong vfy = 0xcafebabe; /* l.s has already printed "Plan 9 from Be" */// m = mach; /* now done in l.s */ /* realign data seg; apparently -H0 -R4096 does not pad the text seg */ if (vfy != 0xcafebabe) {// wave('<'); wave('-'); memmove(bdata, etext, edata - bdata); } /* * once data segment is in place, always zero bss since we may * have been loaded by another Plan 9 kernel. */ memset(edata, 0, end - edata); /* zero BSS */ cacheuwbinv(); l2cacheuwbinv(); if (vfy != 0xcafebabe) panic("data segment misaligned"); vfy = 0; wave('l'); machinit(); mmuinit(); optionsinit("/boot/boot boot"); quotefmtinstall(); /* want plan9.ini to be able to affect memory sizing in confinit */ plan9iniinit(); /* before we step on plan9.ini in low memory */ trapinit(); /* so confinit can probe memory to size it */ confinit(); /* figures out amount of memory */ /* xinit prints (if it can), so finish up the banner here. */ delay(500); iprint("l Labs\n\n"); delay(500); xinit(); mainmem->flags |= POOL_ANTAGONISM /* | POOL_PARANOIA */ ; /* * Printinit will cause the first malloc call. * (printinit->qopen->malloc) unless any of the * above (like clockinit) do an irqenable, which * will call malloc. * If the system dies here it's probably due * to malloc(->xalloc) not being initialised * correctly, or the data segment is misaligned * (it's amazing how far you can get with * things like that completely broken). * * (Should be) boilerplate from here on. */ archreset(); /* configure clock signals */ clockinit(); /* start clocks */ timersinit(); watchdoginit(); delay(250); /* let uart catch up */ printinit(); kbdenable(); cpuidprint();// chkmissing(); procinit0(); initseg(); dmainit(); links(); conf.monitor = 1; screeninit(); chandevreset(); /* most devices are discovered here */ // i8250console(); /* too early; see init0 */ pageinit(); swapinit(); userinit(); schedinit();} voidmachinit(void){ if (m == 0) wave('?');// memset(m, 0, sizeof(Mach)); /* done by l.s, now contains stack */ m->machno = 0; machaddr[m->machno] = m; m->ticks = 1; m->perf.period = 1; conf.nmach = 1; active.machs = 1; active.exiting = 0; up = nil;} static voidshutdown(int ispanic){ int ms, once; lock(&active); if(ispanic) active.ispanic = ispanic; else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0) active.ispanic = 0; once = active.machs & (1<<m->machno); active.machs &= ~(1<<m->machno); active.exiting = 1; unlock(&active); if(once) iprint("cpu%d: exiting\n", m->machno); spllo(); for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){ delay(TK2MS(2)); if(active.machs == 0 && consactive() == 0) break; } delay(1000);} /* * exit kernel either on a panic or user request */voidexit(int code){ shutdown(code); splhi(); archreboot();} intisaconfig(char *class, int ctlrno, ISAConf *isa){ char cc[32], *p; int i; snprint(cc, sizeof cc, "%s%d", class, ctlrno); p = getconf(cc); if(p == nil) return 0; isa->type = ""; isa->nopt = tokenize(p, isa->opt, NISAOPT); for(i = 0; i < isa->nopt; i++){ p = isa->opt[i]; if(cistrncmp(p, "type=", 5) == 0) isa->type = p + 5; else if(cistrncmp(p, "port=", 5) == 0) isa->port = strtoul(p+5, &p, 0); else if(cistrncmp(p, "irq=", 4) == 0) isa->irq = strtoul(p+4, &p, 0); else if(cistrncmp(p, "dma=", 4) == 0) isa->dma = strtoul(p+4, &p, 0); else if(cistrncmp(p, "mem=", 4) == 0) isa->mem = strtoul(p+4, &p, 0); else if(cistrncmp(p, "size=", 5) == 0) isa->size = strtoul(p+5, &p, 0); else if(cistrncmp(p, "freq=", 5) == 0) isa->freq = strtoul(p+5, &p, 0); } return 1;} /* * the new kernel is already loaded at address `code' * of size `size' and entry point `entry'. */voidreboot(void *entry, void *code, ulong size){ void (*f)(ulong, ulong, ulong); print("starting reboot..."); writeconf(); shutdown(0); /* * should be the only processor running now */ print("reboot entry %#lux code %#lux size %ld\n", PADDR(entry), PADDR(code), size); delay(100); /* turn off buffered serial console */ serialoq = nil; kprintoq = nil; screenputs = nil; /* shutdown devices */ chandevshutdown(); /* call off the dog */ clockshutdown(); splhi(); intrsoff(); /* setup reboot trampoline function */ f = (void*)REBOOTADDR; memmove(f, rebootcode, sizeof(rebootcode)); cacheuwbinv(); l2cacheuwbinv(); /* off we go - never to return */ (*f)(PADDR(entry), PADDR(code), size); iprint("loaded kernel returned!\n"); delay(1000); archreboot();} /* * starting place for first process */voidinit0(void){ int i; char buf[2*KNAMELEN]; up->nerrlab = 0; coherence(); spllo(); /* * These are o.k. because rootinit is null. * Then early kproc's will have a root and dot. */ up->slash = namec("#/", Atodir, 0, 0); pathclose(up->slash->path); up->slash->path = newpath("/"); up->dot = cclone(up->slash); dmatest(); /* needs `up' set, so can't do it earlier */ chandevinit(); i8250console(); /* might be redundant, but harmless */ if(kbdq == nil) panic("init0: nil kbdq"); if(serialoq == nil) panic("init0: nil serialoq"); normalprint = 1; if(!waserror()){ snprint(buf, sizeof(buf), "%s %s", "ARM", conffile); ksetenv("terminal", buf, 0); ksetenv("cputype", "arm", 0); if(cpuserver) ksetenv("service", "cpu", 0); else ksetenv("service", "terminal", 0); /* convert plan9.ini variables to #e and #ec */ for(i = 0; i < nconf; i++) { ksetenv(confname[i], confval[i], 0); ksetenv(confname[i], confval[i], 1); } poperror(); } kproc("alarm", alarmkproc, 0); touser(sp);} static voidbootargs(uintptr base){ int i; ulong ssize; char **av, *p; /* * Push the boot args onto the stack. * The initial value of the user stack must be such * that the total used is larger than the maximum size * of the argument list checked in syscall. */Lines 1–500 of 712. Load more
/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ #include "u.h" #include "../port/lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "io.h" #include "init.h" #include <pool.h> #include <tos.h> #include "reboot.h" /* * Where configuration info is left for the loaded programme. * This will turn into a structure as more is done by the boot loader * (e.g. why parse the .ini file twice?). * There are 3584 bytes available at CONFADDR. */ #define BOOTARGS ((char*)CONFADDR) #define BOOTARGSLEN (16*KiB) /* limit in devenv.c */ #define MAXCONF 64 #define MAXCONFLINE 160 enum { Minmem = 256*MB, /* conservative default */ /* space for syscall args, return PC, top-of-stack struct */ Ustkheadroom = sizeof(Sargs) + sizeof(uintptr) + sizeof(Tos), }; #define isascii(c) ((uchar)(c) > 0 && (uchar)(c) < 0177) uintptr kseg0 = KZERO; Mach* machaddr[MAXMACH]; /* * Option arguments from the command line. * oargv[0] is the boot file. * Optionsinit() is called from multiboot() * or some other machine-dependent place * to set it all up. */ static int oargc; static char* oargv[20]; static char oargb[128]; static int oargblen; static char oenv[4096]; static uintptr sp; /* XXX - must go - user stack of init proc */ int vflag; int normalprint; char debug[256]; /* store plan9.ini contents here at least until we stash them in #ec */ static char confname[MAXCONF][KNAMELEN]; static char confval[MAXCONF][MAXCONFLINE]; static int nconf; static int findconf(char *name) { int i; for(i = 0; i < nconf; i++) if(cistrcmp(confname[i], name) == 0) return i; return -1; } char* getconf(char *name) { int i; i = findconf(name); if(i >= 0) return confval[i]; return nil; } void addconf(char *name, char *val) { int i; i = findconf(name); if(i < 0){ if(val == nil || nconf >= MAXCONF) return; i = nconf++; strecpy(confname[i], confname[i]+sizeof(confname[i]), name); } // confval[i] = val; strecpy(confval[i], confval[i]+sizeof(confval[i]), val); } static void writeconf(void) { char *p, *q; int n; p = getconfenv(); if(waserror()) { free(p); nexterror(); } /* convert to name=value\n format */ for(q=p; *q; q++) { q += strlen(q); *q = '='; q += strlen(q); *q = '\n'; } n = q - p + 1; if(n >= BOOTARGSLEN) error("kernel configuration too large"); memmove(BOOTARGS, p, n); memset(BOOTARGS + n, '\n', BOOTARGSLEN - n); poperror(); free(p); } /* * assumes that we have loaded our /cfg/pxe/mac file at 0x1000 with * tftp in u-boot. no longer uses malloc, so can be called early. */ static void plan9iniinit(void) { char *k, *v, *next; k = (char *)CONFADDR; if(!isascii(*k)) return; for(; k && *k != '\0'; k = next) { if (!isascii(*k)) /* sanity check */ break; next = strchr(k, '\n'); if (next) *next++ = '\0'; if (*k == '\0' || *k == '\n' || *k == '#') continue; v = strchr(k, '='); if(v == nil) continue; /* mal-formed line */ *v++ = '\0'; addconf(k, v); } } static void optionsinit(char* s) { char *o; strcpy(oenv, ""); o = strecpy(oargb, oargb+sizeof(oargb), s)+1; if(getenv("bootargs", o, o - oargb) != nil) *(o-1) = ' '; oargblen = strlen(oargb); oargc = tokenize(oargb, oargv, nelem(oargv)-1); oargv[oargc] = nil; } char* getenv(char* name, char* buf, int n) { char *e, *p, *q; p = oenv; while(*p != 0){ if((e = strchr(p, '=')) == nil) break; for(q = name; p < e; p++){ if(*p != *q) break; q++; } if(p == e && *q == 0){ strecpy(buf, buf+n, e+1); return buf; } p += strlen(p)+1; } return nil; } void main(void) { // int i; extern char bdata[], edata[], end[], etext[]; static ulong vfy = 0xcafebabe; /* l.s has already printed "Plan 9 from Be" */ // m = mach; /* now done in l.s */ /* realign data seg; apparently -H0 -R4096 does not pad the text seg */ if (vfy != 0xcafebabe) { // wave('<'); wave('-'); memmove(bdata, etext, edata - bdata); } /* * once data segment is in place, always zero bss since we may * have been loaded by another Plan 9 kernel. */ memset(edata, 0, end - edata); /* zero BSS */ cacheuwbinv(); l2cacheuwbinv(); if (vfy != 0xcafebabe) panic("data segment misaligned"); vfy = 0; wave('l'); machinit(); mmuinit(); optionsinit("/boot/boot boot"); quotefmtinstall(); /* want plan9.ini to be able to affect memory sizing in confinit */ plan9iniinit(); /* before we step on plan9.ini in low memory */ trapinit(); /* so confinit can probe memory to size it */ confinit(); /* figures out amount of memory */ /* xinit prints (if it can), so finish up the banner here. */ delay(500); iprint("l Labs\n\n"); delay(500); xinit(); mainmem->flags |= POOL_ANTAGONISM /* | POOL_PARANOIA */ ; /* * Printinit will cause the first malloc call. * (printinit->qopen->malloc) unless any of the * above (like clockinit) do an irqenable, which * will call malloc. * If the system dies here it's probably due * to malloc(->xalloc) not being initialised * correctly, or the data segment is misaligned * (it's amazing how far you can get with * things like that completely broken). * * (Should be) boilerplate from here on. */ archreset(); /* configure clock signals */ clockinit(); /* start clocks */ timersinit(); watchdoginit(); delay(250); /* let uart catch up */ printinit(); kbdenable(); cpuidprint(); // chkmissing(); procinit0(); initseg(); dmainit(); links(); conf.monitor = 1; screeninit(); chandevreset(); /* most devices are discovered here */ // i8250console(); /* too early; see init0 */ pageinit(); swapinit(); userinit(); schedinit(); } void machinit(void) { if (m == 0) wave('?'); // memset(m, 0, sizeof(Mach)); /* done by l.s, now contains stack */ m->machno = 0; machaddr[m->machno] = m; m->ticks = 1; m->perf.period = 1; conf.nmach = 1; active.machs = 1; active.exiting = 0; up = nil; } static void shutdown(int ispanic) { int ms, once; lock(&active); if(ispanic) active.ispanic = ispanic; else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0) active.ispanic = 0; once = active.machs & (1<<m->machno); active.machs &= ~(1<<m->machno); active.exiting = 1; unlock(&active); if(once) iprint("cpu%d: exiting\n", m->machno); spllo(); for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){ delay(TK2MS(2)); if(active.machs == 0 && consactive() == 0) break; } delay(1000); } /* * exit kernel either on a panic or user request */ void exit(int code) { shutdown(code); splhi(); archreboot(); } int isaconfig(char *class, int ctlrno, ISAConf *isa) { char cc[32], *p; int i; snprint(cc, sizeof cc, "%s%d", class, ctlrno); p = getconf(cc); if(p == nil) return 0; isa->type = ""; isa->nopt = tokenize(p, isa->opt, NISAOPT); for(i = 0; i < isa->nopt; i++){ p = isa->opt[i]; if(cistrncmp(p, "type=", 5) == 0) isa->type = p + 5; else if(cistrncmp(p, "port=", 5) == 0) isa->port = strtoul(p+5, &p, 0); else if(cistrncmp(p, "irq=", 4) == 0) isa->irq = strtoul(p+4, &p, 0); else if(cistrncmp(p, "dma=", 4) == 0) isa->dma = strtoul(p+4, &p, 0); else if(cistrncmp(p, "mem=", 4) == 0) isa->mem = strtoul(p+4, &p, 0); else if(cistrncmp(p, "size=", 5) == 0) isa->size = strtoul(p+5, &p, 0); else if(cistrncmp(p, "freq=", 5) == 0) isa->freq = strtoul(p+5, &p, 0); } return 1; } /* * the new kernel is already loaded at address `code' * of size `size' and entry point `entry'. */ void reboot(void *entry, void *code, ulong size) { void (*f)(ulong, ulong, ulong); print("starting reboot..."); writeconf(); shutdown(0); /* * should be the only processor running now */ print("reboot entry %#lux code %#lux size %ld\n", PADDR(entry), PADDR(code), size); delay(100); /* turn off buffered serial console */ serialoq = nil; kprintoq = nil; screenputs = nil; /* shutdown devices */ chandevshutdown(); /* call off the dog */ clockshutdown(); splhi(); intrsoff(); /* setup reboot trampoline function */ f = (void*)REBOOTADDR; memmove(f, rebootcode, sizeof(rebootcode)); cacheuwbinv(); l2cacheuwbinv(); /* off we go - never to return */ (*f)(PADDR(entry), PADDR(code), size); iprint("loaded kernel returned!\n"); delay(1000); archreboot(); } /* * starting place for first process */ void init0(void) { int i; char buf[2*KNAMELEN]; up->nerrlab = 0; coherence(); spllo(); /* * These are o.k. because rootinit is null. * Then early kproc's will have a root and dot. */ up->slash = namec("#/", Atodir, 0, 0); pathclose(up->slash->path); up->slash->path = newpath("/"); up->dot = cclone(up->slash); dmatest(); /* needs `up' set, so can't do it earlier */ chandevinit(); i8250console(); /* might be redundant, but harmless */ if(kbdq == nil) panic("init0: nil kbdq"); if(serialoq == nil) panic("init0: nil serialoq"); normalprint = 1; if(!waserror()){ snprint(buf, sizeof(buf), "%s %s", "ARM", conffile); ksetenv("terminal", buf, 0); ksetenv("cputype", "arm", 0); if(cpuserver) ksetenv("service", "cpu", 0); else ksetenv("service", "terminal", 0); /* convert plan9.ini variables to #e and #ec */ for(i = 0; i < nconf; i++) { ksetenv(confname[i], confval[i], 0); ksetenv(confname[i], confval[i], 1); } poperror(); } kproc("alarm", alarmkproc, 0); touser(sp); } static void bootargs(uintptr base) { int i; ulong ssize; char **av, *p; /* * Push the boot args onto the stack. * The initial value of the user stack must be such * that the total used is larger than the maximum size * of the argument list checked in syscall. */