#include "u.h" #include "../port/lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "io.h" #include "ureg.h" #include "init.h" #include uchar *sp; /* stack pointer for /boot */ extern PCArch nsx20, generic, ncr3170; PCArch *arch; PCArch *knownarch[] = { &nsx20, &ncr3170, &generic, }; /* where b.com leaves configuration info */ #define BOOTARGS ((char*)(KZERO|1024)) #define BOOTARGSLEN 1024 #define MAXCONF 32 char *confname[MAXCONF]; char *confval[MAXCONF]; int nconf; void main(void) { ident(); i8042a20(); /* enable address lines 20 and up */ machinit(); active.exiting = 0; active.machs = 1; confinit(); xinit(); screeninit(); printinit(); mmuinit(); pageinit(); trapinit(); mathinit(); clockinit(); faultinit(); kbdinit(); procinit0(); initseg(); chandevreset(); streaminit(); swapinit(); userinit(); schedinit(); } /* * This tries to capture architecture dependencies since things * like power management/reseting/mouse are outside the hardware * model. */ void ident(void) { char *id = (char*)(ROMBIOS + 0xFF40); PCArch **p; for(p = knownarch; *p != &generic; p++) if(strncmp((*p)->id, id, strlen((*p)->id)) == 0) break; arch = *p; } void machinit(void) { int n; n = m->machno; memset(m, 0, sizeof(Mach)); m->machno = n; m->mmask = 1<machno; } ulong garbage; void init0(void) { int i; char tstr[32]; u->nerrlab = 0; m->proc = u->p; u->p->state = Running; u->p->mach = m; spllo(); /* * These are o.k. because rootinit is null. * Then early kproc's will have a root and dot. */ u->slash = (*devtab[0].attach)(0); u->dot = clone(u->slash, 0); kproc("alarm", alarmkproc, 0); chandevinit(); if(!waserror()){ for(i = 0; i < nconf; i++) ksetenv(confname[i], confval[i]); strcpy(tstr, arch->id); strcat(tstr, " %s"); ksetterm(tstr); ksetenv("cputype", "386"); poperror(); } touser(sp); } void userinit(void) { Proc *p; Segment *s; User *up; KMap *k; Page *pg; p = newproc(); p->pgrp = newpgrp(); p->egrp = smalloc(sizeof(Egrp)); p->egrp->ref = 1; p->fgrp = smalloc(sizeof(Fgrp)); p->fgrp->ref = 1; p->procmode = 0640; strcpy(p->text, "*init*"); strcpy(p->user, eve); p->fpstate = FPinit; fpoff(); /* * Kernel Stack * * N.B. The -12 for the stack pointer is important. * 4 bytes for gotolabel's return PC */ p->sched.pc = (ulong)init0; p->sched.sp = USERADDR + BY2PG - 4; p->upage = newpage(1, 0, USERADDR|(p->pid&0xFFFF)); /* * User */ k = kmap(p->upage); up = (User*)VA(k); up->p = p; kunmap(k); /* * User Stack */ s = newseg(SG_STACK, USTKTOP-BY2PG, 1); p->seg[SSEG] = s; pg = newpage(1, 0, USTKTOP-BY2PG); segpage(s, pg); k = kmap(pg); bootargs(VA(k)); kunmap(k); /* * Text */ s = newseg(SG_TEXT, UTZERO, 1); p->seg[TSEG] = s; segpage(s, newpage(1, 0, UTZERO)); k = kmap(s->map[0]->pages[0]); memmove((ulong*)VA(k), initcode, sizeof initcode); kunmap(k); ready(p); } uchar * pusharg(char *p) { int n; n = strlen(p)+1; sp -= n; memmove(sp, p, n); return sp; } void bootargs(ulong base) { int i, ac; uchar *av[32]; uchar **lsp; char *cp = BOOTLINE; char buf[64]; sp = (uchar*)base + BY2PG - MAXSYSARG*BY2WD; ac = 0; av[ac++] = pusharg("/386/9safari"); av[ac++] = pusharg("-p"); cp[64] = 0; if(strncmp(cp, "fd!", 3) == 0){ sprint(buf, "local!#f/fd%ddisk", atoi(cp+3)); av[ac++] = pusharg(buf); } else if(strncmp(cp, "hd!", 3) == 0){ sprint(buf, "local!#h/hd%ddisk", atoi(cp+3)); av[ac++] = pusharg(buf); } /* 4 byte word align stack */ sp = (uchar*)((ulong)sp & ~3); /* build argc, argv on stack */ sp -= (ac+1)*sizeof(sp); lsp = (uchar**)sp; for(i = 0; i < ac; i++) *lsp++ = av[i] + ((USTKTOP - BY2PG) - base); *lsp = 0; sp += (USTKTOP - BY2PG) - base - sizeof(ulong); } Conf conf; void confinit(void) { long x, i, j, *l; int pcnt; ulong ktop; char *cp; char *line[MAXCONF]; /* * the first 640k is the standard useful memory * the next 128K is the display, I/O mem, and BIOS * the last 256k belongs to the roms and other devices */ conf.npage0 = 640/4; conf.base0 = 0; /* * size the non-standard memory */ x = 0x12345678; for(i=2; i<17; i++){ /* * write the word */ l = (long*)(KZERO|(i*MB)); l--; *l = x; /* * take care of wraps */ for(j = 1; j < i; j++){ l = (long*)(KZERO|(j*MB)); l--; *l = ~x; } /* * check */ l = (long*)(KZERO|(i*MB)); l--; if(*l != x) break; memset((char*)(KZERO|((i-1)*MB)), 0, MB); x += 0x3141526; } i--; conf.base1 = 1*MB; conf.npage1 = (i*MB - conf.base1)/BY2PG; conf.npage = conf.npage0 + conf.npage1; conf.ldepth = 0; pcnt = (1<