main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "ureg.h"
  8. #include "init.h"
  9. #include "pool.h"
  10. #include "reboot.h"
  11. Mach *m;
  12. /*
  13. * Where configuration info is left for the loaded programme.
  14. * This will turn into a structure as more is done by the boot loader
  15. * (e.g. why parse the .ini file twice?).
  16. * There are 3584 bytes available at CONFADDR.
  17. */
  18. #define BOOTLINE ((char*)CONFADDR)
  19. #define BOOTLINELEN 64
  20. #define BOOTARGS ((char*)(CONFADDR+BOOTLINELEN))
  21. #define BOOTARGSLEN (4096-0x200-BOOTLINELEN)
  22. #define MAXCONF 64
  23. char bootdisk[KNAMELEN];
  24. Conf conf;
  25. char *confname[MAXCONF];
  26. char *confval[MAXCONF];
  27. int nconf;
  28. uchar *sp; /* user stack of init proc */
  29. static void
  30. options(void)
  31. {
  32. long i, n;
  33. char *cp, *line[MAXCONF], *p, *q;
  34. /*
  35. * parse configuration args from dos file plan9.ini
  36. */
  37. cp = BOOTARGS; /* where b.com leaves its config */
  38. cp[BOOTARGSLEN-1] = 0;
  39. /*
  40. * Strip out '\r', change '\t' -> ' '.
  41. */
  42. p = cp;
  43. for(q = cp; *q; q++){
  44. if(*q == '\r')
  45. continue;
  46. if(*q == '\t')
  47. *q = ' ';
  48. *p++ = *q;
  49. }
  50. *p = 0;
  51. n = getfields(cp, line, MAXCONF, 1, "\n");
  52. for(i = 0; i < n; i++){
  53. if(*line[i] == '#')
  54. continue;
  55. cp = strchr(line[i], '=');
  56. if(cp == nil)
  57. continue;
  58. *cp++ = '\0';
  59. confname[nconf] = line[i];
  60. confval[nconf] = cp;
  61. nconf++;
  62. }
  63. }
  64. void
  65. main(void)
  66. {
  67. mach0init();
  68. options();
  69. ioinit();
  70. i8250console();
  71. quotefmtinstall();
  72. print("\nPlan 9\n");
  73. kbdinit();
  74. i8253init();
  75. cpuidentify();
  76. screeninit();
  77. meminit();
  78. confinit();
  79. archinit();
  80. xinit();
  81. trapinit();
  82. printinit();
  83. cpuidprint();
  84. mmuinit();
  85. if(arch->intrinit)
  86. arch->intrinit();
  87. timersinit();
  88. mathinit();
  89. kbdenable();
  90. if(arch->clockenable)
  91. arch->clockenable();
  92. procinit0();
  93. initseg();
  94. links();
  95. conf.monitor = 1;
  96. chandevreset();
  97. i8253link();
  98. pageinit();
  99. swapinit();
  100. userinit();
  101. schedinit();
  102. }
  103. void
  104. mach0init(void)
  105. {
  106. conf.nmach = 1;
  107. MACHP(0) = (Mach*)CPU0MACH;
  108. m->pdb = (ulong*)CPU0PDB;
  109. m->gdt = (Segdesc*)CPU0GDT;
  110. machinit();
  111. active.machs = 1;
  112. active.exiting = 0;
  113. }
  114. void
  115. machinit(void)
  116. {
  117. int machno;
  118. ulong *pdb;
  119. Segdesc *gdt;
  120. machno = m->machno;
  121. pdb = m->pdb;
  122. gdt = m->gdt;
  123. memset(m, 0, sizeof(Mach));
  124. m->machno = machno;
  125. m->pdb = pdb;
  126. m->gdt = gdt;
  127. m->perf.period = 1;
  128. /*
  129. * For polled uart output at boot, need
  130. * a default delay constant. 100000 should
  131. * be enough for a while. Cpuidentify will
  132. * calculate the real value later.
  133. */
  134. m->loopconst = 100000;
  135. }
  136. void
  137. init0(void)
  138. {
  139. int i;
  140. char buf[2*KNAMELEN];
  141. up->nerrlab = 0;
  142. spllo();
  143. /*
  144. * These are o.k. because rootinit is null.
  145. * Then early kproc's will have a root and dot.
  146. */
  147. up->slash = namec("#/", Atodir, 0, 0);
  148. cnameclose(up->slash->name);
  149. up->slash->name = newcname("/");
  150. up->dot = cclone(up->slash);
  151. chandevinit();
  152. if(!waserror()){
  153. snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
  154. ksetenv("terminal", buf, 0);
  155. ksetenv("cputype", "386", 0);
  156. if(cpuserver)
  157. ksetenv("service", "cpu", 0);
  158. else
  159. ksetenv("service", "terminal", 0);
  160. for(i = 0; i < nconf; i++){
  161. if(confname[i][0] != '*')
  162. ksetenv(confname[i], confval[i], 0);
  163. ksetenv(confname[i], confval[i], 1);
  164. }
  165. poperror();
  166. }
  167. kproc("alarm", alarmkproc, 0);
  168. touser(sp);
  169. }
  170. void
  171. userinit(void)
  172. {
  173. Proc *p;
  174. Segment *s;
  175. KMap *k;
  176. Page *pg;
  177. p = newproc();
  178. p->pgrp = newpgrp();
  179. p->egrp = smalloc(sizeof(Egrp));
  180. p->egrp->ref = 1;
  181. p->fgrp = dupfgrp(nil);
  182. p->rgrp = newrgrp();
  183. p->procmode = 0640;
  184. kstrdup(&eve, "");
  185. kstrdup(&p->text, "*init*");
  186. kstrdup(&p->user, eve);
  187. p->fpstate = FPinit;
  188. fpoff();
  189. /*
  190. * Kernel Stack
  191. *
  192. * N.B. make sure there's enough space for syscall to check
  193. * for valid args and
  194. * 4 bytes for gotolabel's return PC
  195. */
  196. p->sched.pc = (ulong)init0;
  197. p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
  198. /*
  199. * User Stack
  200. */
  201. s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
  202. p->seg[SSEG] = s;
  203. pg = newpage(1, 0, USTKTOP-BY2PG);
  204. segpage(s, pg);
  205. k = kmap(pg);
  206. bootargs(VA(k));
  207. kunmap(k);
  208. /*
  209. * Text
  210. */
  211. s = newseg(SG_TEXT, UTZERO, 1);
  212. s->flushme++;
  213. p->seg[TSEG] = s;
  214. pg = newpage(1, 0, UTZERO);
  215. memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
  216. segpage(s, pg);
  217. k = kmap(s->map[0]->pages[0]);
  218. memmove((ulong*)VA(k), initcode, sizeof initcode);
  219. kunmap(k);
  220. ready(p);
  221. }
  222. uchar *
  223. pusharg(char *p)
  224. {
  225. int n;
  226. n = strlen(p)+1;
  227. sp -= n;
  228. memmove(sp, p, n);
  229. return sp;
  230. }
  231. void
  232. bootargs(ulong base)
  233. {
  234. int i, ac;
  235. uchar *av[32];
  236. uchar **lsp;
  237. char *cp = BOOTLINE;
  238. char buf[64];
  239. sp = (uchar*)base + BY2PG - MAXSYSARG*BY2WD;
  240. ac = 0;
  241. av[ac++] = pusharg("/386/9dos");
  242. /* when boot is changed to only use rc, this code can go away */
  243. cp[BOOTLINELEN-1] = 0;
  244. buf[0] = 0;
  245. if(strncmp(cp, "fd", 2) == 0){
  246. sprint(buf, "local!#f/fd%lddisk", strtol(cp+2, 0, 0));
  247. av[ac++] = pusharg(buf);
  248. } else if(strncmp(cp, "sd", 2) == 0){
  249. sprint(buf, "local!#S/sd%c%c/fs", *(cp+2), *(cp+3));
  250. av[ac++] = pusharg(buf);
  251. } else if(strncmp(cp, "ether", 5) == 0)
  252. av[ac++] = pusharg("-n");
  253. /* 4 byte word align stack */
  254. sp = (uchar*)((ulong)sp & ~3);
  255. /* build argc, argv on stack */
  256. sp -= (ac+1)*sizeof(sp);
  257. lsp = (uchar**)sp;
  258. for(i = 0; i < ac; i++)
  259. *lsp++ = av[i] + ((USTKTOP - BY2PG) - base);
  260. *lsp = 0;
  261. sp += (USTKTOP - BY2PG) - base - sizeof(ulong);
  262. }
  263. char*
  264. getconf(char *name)
  265. {
  266. int i;
  267. for(i = 0; i < nconf; i++)
  268. if(cistrcmp(confname[i], name) == 0)
  269. return confval[i];
  270. return 0;
  271. }
  272. static void
  273. writeconf(void)
  274. {
  275. char *p, *q;
  276. int n;
  277. p = getconfenv();
  278. if(waserror()) {
  279. free(p);
  280. nexterror();
  281. }
  282. /* convert to name=value\n format */
  283. for(q=p; *q; q++) {
  284. q += strlen(q);
  285. *q = '=';
  286. q += strlen(q);
  287. *q = '\n';
  288. }
  289. n = q - p + 1;
  290. if(n >= BOOTARGSLEN)
  291. error("kernel configuration too large");
  292. memset(BOOTLINE, 0, BOOTLINELEN);
  293. memmove(BOOTARGS, p, n);
  294. poperror();
  295. free(p);
  296. }
  297. void
  298. confinit(void)
  299. {
  300. char *p;
  301. int userpcnt;
  302. ulong kpages;
  303. if(p = getconf("*kernelpercent"))
  304. userpcnt = 100 - strtol(p, 0, 0);
  305. else
  306. userpcnt = 0;
  307. conf.npage = conf.npage0 + conf.npage1;
  308. conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
  309. if(cpuserver)
  310. conf.nproc *= 3;
  311. if(conf.nproc > 2000)
  312. conf.nproc = 2000;
  313. conf.nimage = 200;
  314. conf.nswap = conf.nproc*80;
  315. conf.nswppo = 4096;
  316. if(cpuserver) {
  317. if(userpcnt < 10)
  318. userpcnt = 70;
  319. kpages = conf.npage - (conf.npage*userpcnt)/100;
  320. /*
  321. * Hack for the big boys. Only good while physmem < 4GB.
  322. * Give the kernel fixed max + enough to allocate the
  323. * page pool.
  324. * This is an overestimate as conf.upages < conf.npages.
  325. * The patch of nimage is a band-aid, scanning the whole
  326. * page list in imagereclaim just takes too long.
  327. */
  328. if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
  329. kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
  330. conf.nimage = 2000;
  331. kpages += (conf.nproc*KSTACK)/BY2PG;
  332. }
  333. } else {
  334. if(userpcnt < 10) {
  335. if(conf.npage*BY2PG < 16*MB)
  336. userpcnt = 40;
  337. else
  338. userpcnt = 60;
  339. }
  340. kpages = conf.npage - (conf.npage*userpcnt)/100;
  341. /*
  342. * Make sure terminals with low memory get at least
  343. * 4MB on the first Image chunk allocation.
  344. */
  345. if(conf.npage*BY2PG < 16*MB)
  346. imagmem->minarena = 4*1024*1024;
  347. }
  348. conf.upages = conf.npage - kpages;
  349. conf.ialloc = (kpages/2)*BY2PG;
  350. /*
  351. * Guess how much is taken by the large permanent
  352. * datastructures. Mntcache and Mntrpc are not accounted for
  353. * (probably ~300KB).
  354. */
  355. kpages *= BY2PG;
  356. kpages -= conf.upages*sizeof(Page)
  357. + conf.nproc*sizeof(Proc)
  358. + conf.nimage*sizeof(Image)
  359. + conf.nswap
  360. + conf.nswppo*sizeof(Page);
  361. mainmem->maxsize = kpages;
  362. if(!cpuserver){
  363. /*
  364. * give terminals lots of image memory, too; the dynamic
  365. * allocation will balance the load properly, hopefully.
  366. * be careful with 32-bit overflow.
  367. */
  368. imagmem->maxsize = kpages;
  369. }
  370. }
  371. static char* mathmsg[] =
  372. {
  373. nil, /* handled below */
  374. "denormalized operand",
  375. "division by zero",
  376. "numeric overflow",
  377. "numeric underflow",
  378. "precision loss",
  379. };
  380. static void
  381. mathnote(void)
  382. {
  383. int i;
  384. ulong status;
  385. char *msg, note[ERRMAX];
  386. status = up->fpsave.status;
  387. /*
  388. * Some attention should probably be paid here to the
  389. * exception masks and error summary.
  390. */
  391. msg = "unknown exception";
  392. for(i = 1; i <= 5; i++){
  393. if(!((1<<i) & status))
  394. continue;
  395. msg = mathmsg[i];
  396. break;
  397. }
  398. if(status & 0x01){
  399. if(status & 0x30){
  400. if(status & 0x200)
  401. msg = "stack overflow";
  402. else
  403. msg = "stack underflow";
  404. }else
  405. msg = "invalid operation";
  406. }
  407. sprint(note, "sys: fp: %s fppc=0x%lux status=0x%lux", msg, up->fpsave.pc, status);
  408. postnote(up, 1, note, NDebug);
  409. }
  410. /*
  411. * math coprocessor error
  412. */
  413. static void
  414. matherror(Ureg *ur, void*)
  415. {
  416. /*
  417. * a write cycle to port 0xF0 clears the interrupt latch attached
  418. * to the error# line from the 387
  419. */
  420. if(!(m->cpuiddx & 0x01))
  421. outb(0xF0, 0xFF);
  422. /*
  423. * save floating point state to check out error
  424. */
  425. fpenv(&up->fpsave);
  426. mathnote();
  427. if(ur->pc & KZERO)
  428. panic("fp: status %ux fppc=0x%lux pc=0x%lux",
  429. up->fpsave.status, up->fpsave.pc, ur->pc);
  430. }
  431. /*
  432. * math coprocessor emulation fault
  433. */
  434. static void
  435. mathemu(Ureg*, void*)
  436. {
  437. if(up->fpstate & FPillegal){
  438. /* someone did floating point in a note handler */
  439. postnote(up, 1, "sys: floating point in note handler", NDebug);
  440. return;
  441. }
  442. switch(up->fpstate){
  443. case FPinit:
  444. fpinit();
  445. up->fpstate = FPactive;
  446. break;
  447. case FPinactive:
  448. /*
  449. * Before restoring the state, check for any pending
  450. * exceptions, there's no way to restore the state without
  451. * generating an unmasked exception.
  452. * More attention should probably be paid here to the
  453. * exception masks and error summary.
  454. */
  455. if((up->fpsave.status & ~up->fpsave.control) & 0x07F){
  456. mathnote();
  457. break;
  458. }
  459. fprestore(&up->fpsave);
  460. up->fpstate = FPactive;
  461. break;
  462. case FPactive:
  463. panic("math emu");
  464. break;
  465. }
  466. }
  467. /*
  468. * math coprocessor segment overrun
  469. */
  470. static void
  471. mathover(Ureg*, void*)
  472. {
  473. pexit("math overrun", 0);
  474. }
  475. void
  476. mathinit(void)
  477. {
  478. trapenable(VectorCERR, matherror, 0, "matherror");
  479. if(X86FAMILY(m->cpuidax) == 3)
  480. intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
  481. trapenable(VectorCNA, mathemu, 0, "mathemu");
  482. trapenable(VectorCSO, mathover, 0, "mathover");
  483. }
  484. /*
  485. * set up floating point for a new process
  486. */
  487. void
  488. procsetup(Proc *p)
  489. {
  490. p->fpstate = FPinit;
  491. fpoff();
  492. }
  493. /*
  494. * Save the mach dependent part of the process state.
  495. */
  496. void
  497. procsave(Proc *p)
  498. {
  499. if(p->fpstate == FPactive){
  500. if(p->state == Moribund)
  501. fpoff();
  502. else{
  503. /*
  504. * Fpsave() stores without handling pending
  505. * unmasked exeptions. Postnote() can't be called
  506. * here as sleep() already has up->rlock, so
  507. * the handling of pending exceptions is delayed
  508. * until the process runs again and generates an
  509. * emulation fault to activate the FPU.
  510. */
  511. fpsave(&up->fpsave);
  512. }
  513. p->fpstate = FPinactive;
  514. }
  515. /*
  516. * While this processor is in the scheduler, the process could run
  517. * on another processor and exit, returning the page tables to
  518. * the free list where they could be reallocated and overwritten.
  519. * When this processor eventually has to get an entry from the
  520. * trashed page tables it will crash.
  521. */
  522. mmuflushtlb(PADDR(m->pdb));
  523. }
  524. static void
  525. shutdown(int ispanic)
  526. {
  527. int ms, once;
  528. lock(&active);
  529. if(ispanic)
  530. active.ispanic = ispanic;
  531. else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
  532. active.ispanic = 0;
  533. once = active.machs & (1<<m->machno);
  534. active.machs &= ~(1<<m->machno);
  535. active.exiting = 1;
  536. unlock(&active);
  537. if(once)
  538. print("cpu%d: exiting\n", m->machno);
  539. spllo();
  540. for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
  541. delay(TK2MS(2));
  542. if(active.machs == 0 && consactive() == 0)
  543. break;
  544. }
  545. if(active.ispanic && m->machno == 0){
  546. if(cpuserver)
  547. delay(10000);
  548. else
  549. for(;;);
  550. }
  551. else
  552. delay(1000);
  553. }
  554. void
  555. reboot(void *entry, void *code, ulong size)
  556. {
  557. void (*f)(ulong, ulong, ulong);
  558. ulong *pdb;
  559. writeconf();
  560. shutdown(0);
  561. /*
  562. * should be the only processor running now
  563. */
  564. print("shutting down...\n");
  565. delay(200);
  566. splhi();
  567. /* turn off buffered serial console */
  568. serialoq = nil;
  569. /* shutdown devices */
  570. chandevshutdown();
  571. /*
  572. * Modify the machine page table to directly map the low 4MB of memory
  573. * This allows the reboot code to turn off the page mapping
  574. */
  575. pdb = m->pdb;
  576. pdb[PDX(0)] = pdb[PDX(KZERO)];
  577. mmuflushtlb(PADDR(pdb));
  578. /* setup reboot trampoline function */
  579. f = (void*)REBOOTADDR;
  580. memmove(f, rebootcode, sizeof(rebootcode));
  581. print("rebooting...\n");
  582. /* off we go - never to return */
  583. (*f)(PADDR(entry), PADDR(code), size);
  584. }
  585. void
  586. exit(int ispanic)
  587. {
  588. shutdown(ispanic);
  589. arch->reset();
  590. }
  591. int
  592. isaconfig(char *class, int ctlrno, ISAConf *isa)
  593. {
  594. char cc[32], *p;
  595. int i;
  596. snprint(cc, sizeof cc, "%s%d", class, ctlrno);
  597. p = getconf(cc);
  598. if(p == nil)
  599. return 0;
  600. isa->type = "";
  601. isa->nopt = tokenize(p, isa->opt, NISAOPT);
  602. for(i = 0; i < isa->nopt; i++){
  603. p = isa->opt[i];
  604. if(cistrncmp(p, "type=", 5) == 0)
  605. isa->type = p + 5;
  606. else if(cistrncmp(p, "port=", 5) == 0)
  607. isa->port = strtoul(p+5, &p, 0);
  608. else if(cistrncmp(p, "irq=", 4) == 0)
  609. isa->irq = strtoul(p+4, &p, 0);
  610. else if(cistrncmp(p, "dma=", 4) == 0)
  611. isa->dma = strtoul(p+4, &p, 0);
  612. else if(cistrncmp(p, "mem=", 4) == 0)
  613. isa->mem = strtoul(p+4, &p, 0);
  614. else if(cistrncmp(p, "size=", 5) == 0)
  615. isa->size = strtoul(p+5, &p, 0);
  616. else if(cistrncmp(p, "freq=", 5) == 0)
  617. isa->freq = strtoul(p+5, &p, 0);
  618. }
  619. return 1;
  620. }
  621. int
  622. cistrcmp(char *a, char *b)
  623. {
  624. int ac, bc;
  625. for(;;){
  626. ac = *a++;
  627. bc = *b++;
  628. if(ac >= 'A' && ac <= 'Z')
  629. ac = 'a' + (ac - 'A');
  630. if(bc >= 'A' && bc <= 'Z')
  631. bc = 'a' + (bc - 'A');
  632. ac -= bc;
  633. if(ac)
  634. return ac;
  635. if(bc == 0)
  636. break;
  637. }
  638. return 0;
  639. }
  640. int
  641. cistrncmp(char *a, char *b, int n)
  642. {
  643. unsigned ac, bc;
  644. while(n > 0){
  645. ac = *a++;
  646. bc = *b++;
  647. n--;
  648. if(ac >= 'A' && ac <= 'Z')
  649. ac = 'a' + (ac - 'A');
  650. if(bc >= 'A' && bc <= 'Z')
  651. bc = 'a' + (bc - 'A');
  652. ac -= bc;
  653. if(ac)
  654. return ac;
  655. if(bc == 0)
  656. break;
  657. }
  658. return 0;
  659. }
  660. /*
  661. * put the processor in the halt state if we've no processes to run.
  662. * an interrupt will get us going again.
  663. */
  664. void
  665. idlehands(void)
  666. {
  667. if(conf.nmach == 1)
  668. halt();
  669. }