proc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. int nrdy;
  8. Ref noteidalloc;
  9. long delayedscheds; /* statistics */
  10. static Ref pidalloc;
  11. static struct Procalloc
  12. {
  13. Lock;
  14. Proc* ht[128];
  15. Proc* arena;
  16. Proc* free;
  17. } procalloc;
  18. enum
  19. {
  20. Q=(HZ/20)*4,
  21. DQ=((HZ-Q)/40)*2,
  22. };
  23. static Schedq runq[Nrq];
  24. static ulong runvec;
  25. static int quanta[Nrq] =
  26. {
  27. Q+19*DQ, Q+18*DQ, Q+17*DQ, Q+16*DQ,
  28. Q+15*DQ, Q+14*DQ, Q+13*DQ, Q+12*DQ,
  29. Q+11*DQ, Q+10*DQ, Q+ 9*DQ, Q+ 8*DQ,
  30. Q+ 7*DQ, Q+ 6*DQ, Q+ 5*DQ, Q+ 4*DQ,
  31. Q+ 3*DQ, Q+ 2*DQ, Q+ 1*DQ, Q+ 0*DQ,
  32. };
  33. extern Edfinterface nulledf;
  34. Edfinterface *edf = &nulledf;
  35. char *statename[] =
  36. { /* BUG: generate automatically */
  37. "Dead",
  38. "Moribund",
  39. "Ready",
  40. "Scheding",
  41. "Running",
  42. "Queueing",
  43. "QueueingR",
  44. "QueueingW",
  45. "Wakeme",
  46. "Broken",
  47. "Stopped",
  48. "Rendez",
  49. "Released",
  50. };
  51. static void pidhash(Proc*);
  52. static void pidunhash(Proc*);
  53. /*
  54. * Always splhi()'ed.
  55. */
  56. void
  57. schedinit(void) /* never returns */
  58. {
  59. setlabel(&m->sched);
  60. if(up) {
  61. m->proc = 0;
  62. switch(up->state) {
  63. case Running:
  64. ready(up);
  65. break;
  66. case Moribund:
  67. up->state = Dead;
  68. if (edf->isedf(up))
  69. edf->edfbury(up);
  70. /*
  71. * Holding locks from pexit:
  72. * procalloc
  73. * palloc
  74. */
  75. mmurelease(up);
  76. up->qnext = procalloc.free;
  77. procalloc.free = up;
  78. unlock(&palloc);
  79. unlock(&procalloc);
  80. break;
  81. }
  82. up->mach = 0;
  83. up = nil;
  84. }
  85. sched();
  86. }
  87. /*
  88. * If changing this routine, look also at sleep(). It
  89. * contains a copy of the guts of sched().
  90. */
  91. void
  92. sched(void)
  93. {
  94. int x[1];
  95. if(m->ilockdepth)
  96. panic("ilockdepth %d, last lock 0x%p at 0x%lux, sched called from 0x%lux",
  97. m->ilockdepth, up?up->lastilock:nil,
  98. (up && up->lastilock)?up->lastilock->pc:0, getcallerpc(x+3));
  99. if(up){
  100. if(up->nlocks && up->state != Moribund){
  101. delayedscheds++;
  102. return;
  103. }
  104. splhi();
  105. /* statistics */
  106. m->cs++;
  107. procsave(up);
  108. if(setlabel(&up->sched)){
  109. procrestore(up);
  110. spllo();
  111. return;
  112. }
  113. gotolabel(&m->sched);
  114. }
  115. up = runproc();
  116. up->state = Running;
  117. up->mach = MACHP(m->machno);
  118. m->proc = up;
  119. mmuswitch(up);
  120. gotolabel(&up->sched);
  121. }
  122. int
  123. anyready(void)
  124. {
  125. return runvec || edf->edfanyready();
  126. }
  127. int
  128. anyhigher(void)
  129. {
  130. return runvec & ~((1<<(up->priority+1))-1);
  131. }
  132. /*
  133. * here once per clock tick to see if we should resched
  134. */
  135. void
  136. hzsched(void)
  137. {
  138. /* another cycle, another quantum */
  139. up->quanta--;
  140. /* edf scheduler always gets first chance */
  141. if(edf->isedf(up))
  142. return;
  143. /* don't bother unless someone is elegible */
  144. if(anyhigher() || (!up->fixedpri && anyready())){
  145. sched();
  146. splhi();
  147. }
  148. }
  149. /*
  150. * here at the end of non-clock interrupts to see if we should preempt the
  151. * current process. Returns 1 if preempted, 0 otherwise.
  152. */
  153. int
  154. preempted(void)
  155. {
  156. if(up && up->state == Running)
  157. if(up->preempted == 0)
  158. if(anyhigher())
  159. if(!active.exiting){
  160. up->preempted = 1;
  161. sched();
  162. splhi();
  163. up->preempted = 0;
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. /*
  169. * ready(p) picks a new priority for a process and sticks it in the
  170. * runq for that priority.
  171. *
  172. * - fixed priority processes never move
  173. * - a process that uses all its quanta before blocking goes down a
  174. * priority level
  175. * - a process that uses less than half its quanta before blocking
  176. * goes up a priority level
  177. * - a process that blocks after using up half or more of it's quanta
  178. * stays at the same level
  179. *
  180. * new quanta are assigned each time a process blocks or changes level
  181. */
  182. void
  183. ready(Proc *p)
  184. {
  185. int s, pri;
  186. Schedq *rq;
  187. s = splhi();
  188. if(edf->isedf(p)){
  189. edf->edfready(p);
  190. splx(s);
  191. return;
  192. }
  193. pri = p->priority;
  194. if(p->fixedpri){
  195. pri = p->basepri;
  196. p->quanta = HZ;
  197. } else if(p->state == Running){
  198. if(p->quanta <= 0){
  199. /* degrade priority of anyone that used their whole quanta */
  200. if(pri > 0)
  201. pri--;
  202. p->quanta = quanta[pri];
  203. }
  204. } else {
  205. if(p->quanta > quanta[pri]/2){
  206. /* blocked before using half its quanta, go up */
  207. if(++pri > p->basepri)
  208. pri = p->basepri;
  209. }
  210. p->quanta = quanta[pri];
  211. }
  212. rq = &runq[pri];
  213. p->priority = pri;
  214. lock(runq);
  215. p->rnext = 0;
  216. if(rq->tail)
  217. rq->tail->rnext = p;
  218. else
  219. rq->head = p;
  220. rq->tail = p;
  221. rq->n++;
  222. nrdy++;
  223. runvec |= 1<<pri;
  224. p->readytime = m->ticks;
  225. p->state = Ready;
  226. unlock(runq);
  227. splx(s);
  228. }
  229. /*
  230. * remove a process from a scheduling queue (called splhi)
  231. */
  232. static Proc*
  233. dequeueproc(Schedq *rq, Proc *tp)
  234. {
  235. Proc *l, *p;
  236. if(!canlock(runq))
  237. return nil;
  238. /*
  239. * the queue may have changed before we locked runq,
  240. * refind the target process.
  241. */
  242. l = 0;
  243. for(p = rq->head; p; p = p->rnext){
  244. if(p == tp)
  245. break;
  246. l = p;
  247. }
  248. /*
  249. * p->mach==0 only when process state is saved
  250. */
  251. if(p == 0 || p->mach){
  252. unlock(runq);
  253. return nil;
  254. }
  255. if(p->rnext == 0)
  256. rq->tail = l;
  257. if(l)
  258. l->rnext = p->rnext;
  259. else
  260. rq->head = p->rnext;
  261. if(rq->head == nil)
  262. runvec &= ~(1<<(rq-runq));
  263. rq->n--;
  264. nrdy--;
  265. if(p->state != Ready)
  266. print("dequeueproc %s %lud %s\n", p->text, p->pid, statename[p->state]);
  267. unlock(runq);
  268. return p;
  269. }
  270. /*
  271. * yield the processor and drop our priority
  272. */
  273. void
  274. yield(void)
  275. {
  276. if(anyready()){
  277. up->quanta = 0; /* act like you used them all up */
  278. sched();
  279. }
  280. }
  281. /*
  282. * move up any process waiting more than its quanta
  283. */
  284. static void
  285. rebalance(void)
  286. {
  287. Schedq *rq;
  288. Proc *p;
  289. for(rq = runq; rq < &runq[Nrq]; rq++){
  290. p = rq->head;
  291. if(p == nil)
  292. continue;
  293. if(p->mp != MACHP(m->machno))
  294. continue;
  295. if(p->priority == p->basepri)
  296. continue;
  297. if(m->ticks - p->readytime < quanta[p->priority]/4)
  298. continue;
  299. splhi();
  300. p = dequeueproc(rq, p);
  301. spllo();
  302. if(p == nil)
  303. continue;
  304. p->quanta = quanta[p->priority]; /* act like we used none */
  305. ready(p);
  306. }
  307. }
  308. /*
  309. * pick a process to run
  310. */
  311. Proc*
  312. runproc(void)
  313. {
  314. Schedq *rq;
  315. Proc *p;
  316. ulong start, now;
  317. int i;
  318. start = perfticks();
  319. if ((p = edf->edfrunproc()) != nil)
  320. return p;
  321. if(m->fairness++ == 10){
  322. m->fairness = 0;
  323. rebalance();
  324. }
  325. loop:
  326. /*
  327. * find a process that last ran on this processor (affinity),
  328. * or one that hasn't moved in a while (load balancing). Every
  329. * time around the loop affinity goes down.
  330. */
  331. spllo();
  332. for(i = 0;; i++){
  333. /*
  334. * find the highest priority target process that this
  335. * processor can run given affinity constraints.
  336. *
  337. */
  338. for(rq = &runq[Nrq-1]; rq >= runq; rq--){
  339. for(p = rq->head; p; p = p->rnext){
  340. if(p->mp == nil || p->mp == MACHP(m->machno)
  341. || (!p->wired && i > 0))
  342. goto found;
  343. }
  344. }
  345. /* waste time or halt the CPU */
  346. idlehands();
  347. /* remember how much time we're here */
  348. now = perfticks();
  349. m->perf.inidle += now-start;
  350. start = now;
  351. }
  352. found:
  353. splhi();
  354. p = dequeueproc(rq, p);
  355. if(p == nil)
  356. goto loop;
  357. p->state = Scheding;
  358. p->mp = MACHP(m->machno);
  359. return p;
  360. }
  361. int
  362. canpage(Proc *p)
  363. {
  364. int ok = 0;
  365. splhi();
  366. lock(runq);
  367. /* Only reliable way to see if we are Running */
  368. if(p->mach == 0) {
  369. p->newtlb = 1;
  370. ok = 1;
  371. }
  372. unlock(runq);
  373. spllo();
  374. return ok;
  375. }
  376. Proc*
  377. newproc(void)
  378. {
  379. Proc *p;
  380. lock(&procalloc);
  381. for(;;) {
  382. if(p = procalloc.free)
  383. break;
  384. unlock(&procalloc);
  385. resrcwait("no procs");
  386. lock(&procalloc);
  387. }
  388. procalloc.free = p->qnext;
  389. unlock(&procalloc);
  390. p->state = Scheding;
  391. p->psstate = "New";
  392. p->mach = 0;
  393. p->qnext = 0;
  394. p->nchild = 0;
  395. p->nwait = 0;
  396. p->waitq = 0;
  397. p->parent = 0;
  398. p->pgrp = 0;
  399. p->egrp = 0;
  400. p->fgrp = 0;
  401. p->rgrp = 0;
  402. p->pdbg = 0;
  403. p->fpstate = FPinit;
  404. p->kp = 0;
  405. p->procctl = 0;
  406. p->notepending = 0;
  407. p->ureg = 0;
  408. p->privatemem = 0;
  409. p->noswap = 0;
  410. p->errstr = p->errbuf0;
  411. p->syserrstr = p->errbuf1;
  412. p->errbuf0[0] = '\0';
  413. p->errbuf1[0] = '\0';
  414. p->nlocks = 0;
  415. p->delaysched = 0;
  416. kstrdup(&p->user, "*nouser");
  417. kstrdup(&p->text, "*notext");
  418. kstrdup(&p->args, "");
  419. p->nargs = 0;
  420. p->setargs = 0;
  421. memset(p->seg, 0, sizeof p->seg);
  422. p->pid = incref(&pidalloc);
  423. pidhash(p);
  424. p->noteid = incref(&noteidalloc);
  425. if(p->pid==0 || p->noteid==0)
  426. panic("pidalloc");
  427. if(p->kstack == 0)
  428. p->kstack = smalloc(KSTACK);
  429. /* sched params */
  430. p->mp = 0;
  431. p->wired = 0;
  432. procpriority(p, PriNormal, 0);
  433. p->task = nil;
  434. return p;
  435. }
  436. /*
  437. * wire this proc to a machine
  438. */
  439. void
  440. procwired(Proc *p, int bm)
  441. {
  442. Proc *pp;
  443. int i;
  444. char nwired[MAXMACH];
  445. Mach *wm;
  446. if(bm < 0){
  447. /* pick a machine to wire to */
  448. memset(nwired, 0, sizeof(nwired));
  449. p->wired = 0;
  450. pp = proctab(0);
  451. for(i=0; i<conf.nproc; i++, pp++){
  452. wm = pp->wired;
  453. if(wm && pp->pid)
  454. nwired[wm->machno]++;
  455. }
  456. bm = 0;
  457. for(i=0; i<conf.nmach; i++)
  458. if(nwired[i] < nwired[bm])
  459. bm = i;
  460. } else {
  461. /* use the virtual machine requested */
  462. bm = bm % conf.nmach;
  463. }
  464. p->wired = MACHP(bm);
  465. p->mp = p->wired;
  466. }
  467. void
  468. procpriority(Proc *p, int pri, int fixed)
  469. {
  470. if(pri >= Nrq)
  471. pri = Nrq - 1;
  472. else if(pri < 0)
  473. pri = 0;
  474. p->basepri = pri;
  475. p->priority = pri;
  476. if(fixed){
  477. p->quanta = 0xfffff;
  478. p->fixedpri = 1;
  479. } else {
  480. p->quanta = quanta[pri];
  481. p->fixedpri = 0;
  482. }
  483. }
  484. void
  485. procinit0(void) /* bad planning - clashes with devproc.c */
  486. {
  487. Proc *p;
  488. int i;
  489. procalloc.free = xalloc(conf.nproc*sizeof(Proc));
  490. if(procalloc.free == nil)
  491. panic("cannot allocate %lud procs\n", conf.nproc);
  492. procalloc.arena = procalloc.free;
  493. p = procalloc.free;
  494. for(i=0; i<conf.nproc-1; i++,p++)
  495. p->qnext = p+1;
  496. p->qnext = 0;
  497. }
  498. /*
  499. * sleep if a condition is not true. Another process will
  500. * awaken us after it sets the condition. When we awaken
  501. * the condition may no longer be true.
  502. *
  503. * we lock both the process and the rendezvous to keep r->p
  504. * and p->r synchronized.
  505. */
  506. void
  507. sleep(Rendez *r, int (*f)(void*), void *arg)
  508. {
  509. int s;
  510. s = splhi();
  511. if (up->nlocks)
  512. print("process %lud sleeps with %lud locks held, last lock 0x%p locked at pc 0x%lux\n",
  513. up->pid, up->nlocks, up->lastlock, up->lastlock->pc);
  514. lock(r);
  515. lock(&up->rlock);
  516. if(r->p){
  517. print("double sleep %lud %lud\n", r->p->pid, up->pid);
  518. dumpstack();
  519. }
  520. /*
  521. * Wakeup only knows there may be something to do by testing
  522. * r->p in order to get something to lock on.
  523. * Flush that information out to memory in case the sleep is
  524. * committed.
  525. */
  526. r->p = up;
  527. if((*f)(arg) || up->notepending){
  528. /*
  529. * if condition happened or a note is pending
  530. * never mind
  531. */
  532. r->p = nil;
  533. unlock(&up->rlock);
  534. unlock(r);
  535. } else {
  536. /*
  537. * now we are committed to
  538. * change state and call scheduler
  539. */
  540. up->state = Wakeme;
  541. up->r = r;
  542. /* statistics */
  543. m->cs++;
  544. procsave(up);
  545. if(setlabel(&up->sched)) {
  546. /*
  547. * here when the process is awakened
  548. */
  549. procrestore(up);
  550. spllo();
  551. } else {
  552. /*
  553. * here to go to sleep (i.e. stop Running)
  554. */
  555. unlock(&up->rlock);
  556. unlock(r);
  557. // Behind unlock, we may call wakeup on ourselves.
  558. if (edf->isedf(up))
  559. edf->edfblock(up);
  560. gotolabel(&m->sched);
  561. }
  562. }
  563. if(up->notepending) {
  564. up->notepending = 0;
  565. splx(s);
  566. error(Eintr);
  567. }
  568. splx(s);
  569. }
  570. int
  571. tfn(void *arg)
  572. {
  573. return MACHP(0)->ticks >= up->twhen || up->tfn(arg);
  574. }
  575. void
  576. tsleep(Rendez *r, int (*fn)(void*), void *arg, int ms)
  577. {
  578. ulong when;
  579. Proc *f, **l;
  580. when = ms2tk(ms) + MACHP(0)->ticks;
  581. lock(&talarm);
  582. /* take out of list if checkalarm didn't */
  583. if(up->trend) {
  584. l = &talarm.list;
  585. for(f = *l; f; f = f->tlink) {
  586. if(f == up) {
  587. *l = up->tlink;
  588. break;
  589. }
  590. l = &f->tlink;
  591. }
  592. }
  593. /* insert in increasing time order */
  594. l = &talarm.list;
  595. for(f = *l; f; f = f->tlink) {
  596. if(f->twhen >= when)
  597. break;
  598. l = &f->tlink;
  599. }
  600. up->trend = r;
  601. up->twhen = when;
  602. up->tfn = fn;
  603. up->tlink = *l;
  604. *l = up;
  605. unlock(&talarm);
  606. if(waserror()){
  607. up->twhen = 0;
  608. nexterror();
  609. }
  610. sleep(r, tfn, arg);
  611. up->twhen = 0;
  612. poperror();
  613. }
  614. /*
  615. * Expects that only one process can call wakeup for any given Rendez.
  616. * We hold both locks to ensure that r->p and p->r remain consistent.
  617. * Richard Miller has a better solution that doesn't require both to
  618. * be held simultaneously, but I'm a paranoid - presotto.
  619. */
  620. Proc*
  621. wakeup(Rendez *r)
  622. {
  623. Proc *p;
  624. int s;
  625. s = splhi();
  626. lock(r);
  627. p = r->p;
  628. if(p != nil){
  629. lock(&p->rlock);
  630. if(p->state != Wakeme || p->r != r)
  631. panic("wakeup: state");
  632. r->p = nil;
  633. p->r = nil;
  634. ready(p);
  635. unlock(&p->rlock);
  636. }
  637. unlock(r);
  638. splx(s);
  639. return p;
  640. }
  641. /*
  642. * if waking a sleeping process, this routine must hold both
  643. * p->rlock and r->lock. However, it can't know them in
  644. * the same order as wakeup causing a possible lock ordering
  645. * deadlock. We break the deadlock by giving up the p->rlock
  646. * lock if we can't get the r->lock and retrying.
  647. */
  648. int
  649. postnote(Proc *p, int dolock, char *n, int flag)
  650. {
  651. int s, ret;
  652. Rendez *r;
  653. Proc *d, **l;
  654. if(dolock)
  655. qlock(&p->debug);
  656. if(flag != NUser && (p->notify == 0 || p->notified))
  657. p->nnote = 0;
  658. ret = 0;
  659. if(p->nnote < NNOTE) {
  660. strcpy(p->note[p->nnote].msg, n);
  661. p->note[p->nnote++].flag = flag;
  662. ret = 1;
  663. }
  664. p->notepending = 1;
  665. if(dolock)
  666. qunlock(&p->debug);
  667. /* this loop is to avoid lock ordering problems. */
  668. for(;;){
  669. s = splhi();
  670. lock(&p->rlock);
  671. r = p->r;
  672. /* waiting for a wakeup? */
  673. if(r == nil)
  674. break; /* no */
  675. /* try for the second lock */
  676. if(canlock(r)){
  677. if(p->state != Wakeme || r->p != p)
  678. panic("postnote: state %d %d %d", r->p != p, p->r != r, p->state);
  679. p->r = nil;
  680. r->p = nil;
  681. ready(p);
  682. unlock(r);
  683. break;
  684. }
  685. /* give other process time to get out of critical section and try again */
  686. unlock(&p->rlock);
  687. splx(s);
  688. sched();
  689. }
  690. unlock(&p->rlock);
  691. splx(s);
  692. if(p->state != Rendezvous)
  693. return ret;
  694. /* Try and pull out of a rendezvous */
  695. lock(p->rgrp);
  696. if(p->state == Rendezvous) {
  697. p->rendval = ~0;
  698. l = &REND(p->rgrp, p->rendtag);
  699. for(d = *l; d; d = d->rendhash) {
  700. if(d == p) {
  701. *l = p->rendhash;
  702. break;
  703. }
  704. l = &d->rendhash;
  705. }
  706. ready(p);
  707. }
  708. unlock(p->rgrp);
  709. return ret;
  710. }
  711. /*
  712. * weird thing: keep at most NBROKEN around
  713. */
  714. #define NBROKEN 4
  715. struct
  716. {
  717. QLock;
  718. int n;
  719. Proc *p[NBROKEN];
  720. }broken;
  721. void
  722. addbroken(Proc *p)
  723. {
  724. qlock(&broken);
  725. if(broken.n == NBROKEN) {
  726. ready(broken.p[0]);
  727. memmove(&broken.p[0], &broken.p[1], sizeof(Proc*)*(NBROKEN-1));
  728. --broken.n;
  729. }
  730. broken.p[broken.n++] = p;
  731. qunlock(&broken);
  732. if (edf->isedf(up))
  733. edf->edfbury(up);
  734. p->state = Broken;
  735. p->psstate = 0;
  736. sched();
  737. }
  738. void
  739. unbreak(Proc *p)
  740. {
  741. int b;
  742. qlock(&broken);
  743. for(b=0; b < broken.n; b++)
  744. if(broken.p[b] == p) {
  745. broken.n--;
  746. memmove(&broken.p[b], &broken.p[b+1],
  747. sizeof(Proc*)*(NBROKEN-(b+1)));
  748. ready(p);
  749. break;
  750. }
  751. qunlock(&broken);
  752. }
  753. int
  754. freebroken(void)
  755. {
  756. int i, n;
  757. qlock(&broken);
  758. n = broken.n;
  759. for(i=0; i<n; i++) {
  760. ready(broken.p[i]);
  761. broken.p[i] = 0;
  762. }
  763. broken.n = 0;
  764. qunlock(&broken);
  765. return n;
  766. }
  767. void
  768. pexit(char *exitstr, int freemem)
  769. {
  770. Proc *p;
  771. Segment **s, **es;
  772. long utime, stime;
  773. Waitq *wq, *f, *next;
  774. Fgrp *fgrp;
  775. Egrp *egrp;
  776. Rgrp *rgrp;
  777. Pgrp *pgrp;
  778. Chan *dot;
  779. up->alarm = 0;
  780. /* nil out all the resources under lock (free later) */
  781. qlock(&up->debug);
  782. fgrp = up->fgrp;
  783. up->fgrp = nil;
  784. egrp = up->egrp;
  785. up->egrp = nil;
  786. rgrp = up->rgrp;
  787. up->rgrp = nil;
  788. pgrp = up->pgrp;
  789. up->pgrp = nil;
  790. dot = up->dot;
  791. up->dot = nil;
  792. qunlock(&up->debug);
  793. if(fgrp)
  794. closefgrp(fgrp);
  795. if(egrp)
  796. closeegrp(egrp);
  797. if(rgrp)
  798. closergrp(rgrp);
  799. if(dot)
  800. cclose(dot);
  801. if(pgrp)
  802. closepgrp(pgrp);
  803. /*
  804. * if not a kernel process and have a parent,
  805. * do some housekeeping.
  806. */
  807. if(up->kp == 0) {
  808. p = up->parent;
  809. if(p == 0) {
  810. if(exitstr == 0)
  811. exitstr = "unknown";
  812. panic("boot process died: %s", exitstr);
  813. }
  814. while(waserror())
  815. ;
  816. wq = smalloc(sizeof(Waitq));
  817. poperror();
  818. wq->w.pid = up->pid;
  819. utime = up->time[TUser] + up->time[TCUser];
  820. stime = up->time[TSys] + up->time[TCSys];
  821. wq->w.time[TUser] = tk2ms(utime);
  822. wq->w.time[TSys] = tk2ms(stime);
  823. wq->w.time[TReal] = tk2ms(MACHP(0)->ticks - up->time[TReal]);
  824. if(exitstr && exitstr[0])
  825. snprint(wq->w.msg, sizeof(wq->w.msg), "%s %lud: %s", up->text, up->pid, exitstr);
  826. else
  827. wq->w.msg[0] = '\0';
  828. lock(&p->exl);
  829. /*
  830. * If my parent is no longer alive, or if there would be more
  831. * than 128 zombie child processes for my parent, then don't
  832. * leave a wait record behind. This helps prevent badly
  833. * written daemon processes from accumulating lots of wait
  834. * records.
  835. */
  836. if(p->pid == up->parentpid && p->state != Broken && p->nwait < 128) {
  837. p->nchild--;
  838. p->time[TCUser] += utime;
  839. p->time[TCSys] += stime;
  840. wq->next = p->waitq;
  841. p->waitq = wq;
  842. p->nwait++;
  843. wakeup(&p->waitr);
  844. unlock(&p->exl);
  845. }
  846. else {
  847. unlock(&p->exl);
  848. free(wq);
  849. }
  850. }
  851. if(!freemem)
  852. addbroken(up);
  853. qlock(&up->seglock);
  854. es = &up->seg[NSEG];
  855. for(s = up->seg; s < es; s++) {
  856. if(*s) {
  857. putseg(*s);
  858. *s = 0;
  859. }
  860. }
  861. qunlock(&up->seglock);
  862. lock(&up->exl); /* Prevent my children from leaving waits */
  863. pidunhash(up);
  864. up->pid = 0;
  865. wakeup(&up->waitr);
  866. unlock(&up->exl);
  867. for(f = up->waitq; f; f = next) {
  868. next = f->next;
  869. free(f);
  870. }
  871. /* release debuggers */
  872. qlock(&up->debug);
  873. if(up->pdbg) {
  874. wakeup(&up->pdbg->sleep);
  875. up->pdbg = 0;
  876. }
  877. qunlock(&up->debug);
  878. /* Sched must not loop for these locks */
  879. lock(&procalloc);
  880. lock(&palloc);
  881. if (edf->isedf(up))
  882. edf->edfbury(up);
  883. up->state = Moribund;
  884. sched();
  885. panic("pexit");
  886. }
  887. int
  888. haswaitq(void *x)
  889. {
  890. Proc *p;
  891. p = (Proc *)x;
  892. return p->waitq != 0;
  893. }
  894. ulong
  895. pwait(Waitmsg *w)
  896. {
  897. ulong cpid;
  898. Waitq *wq;
  899. if(!canqlock(&up->qwaitr))
  900. error(Einuse);
  901. if(waserror()) {
  902. qunlock(&up->qwaitr);
  903. nexterror();
  904. }
  905. lock(&up->exl);
  906. if(up->nchild == 0 && up->waitq == 0) {
  907. unlock(&up->exl);
  908. error(Enochild);
  909. }
  910. unlock(&up->exl);
  911. sleep(&up->waitr, haswaitq, up);
  912. lock(&up->exl);
  913. wq = up->waitq;
  914. up->waitq = wq->next;
  915. up->nwait--;
  916. unlock(&up->exl);
  917. qunlock(&up->qwaitr);
  918. poperror();
  919. if(w)
  920. memmove(w, &wq->w, sizeof(Waitmsg));
  921. cpid = wq->w.pid;
  922. free(wq);
  923. return cpid;
  924. }
  925. Proc*
  926. proctab(int i)
  927. {
  928. return &procalloc.arena[i];
  929. }
  930. void
  931. dumpaproc(Proc *p)
  932. {
  933. ulong bss;
  934. char *s;
  935. if(p == 0)
  936. return;
  937. bss = 0;
  938. if(p->seg[BSEG])
  939. bss = p->seg[BSEG]->top;
  940. s = p->psstate;
  941. if(s == 0)
  942. s = statename[p->state];
  943. print("%3lud:%10s pc %8lux dbgpc %8lux %8s (%s) ut %ld st %ld bss %lux qpc %lux nl %lud nd %lud lpc %lux pri %lud\n",
  944. p->pid, p->text, p->pc, dbgpc(p), s, statename[p->state],
  945. p->time[0], p->time[1], bss, p->qpc, p->nlocks, p->delaysched, p->lastlock ? p->lastlock->pc : 0, p->priority);
  946. }
  947. void
  948. procdump(void)
  949. {
  950. int i;
  951. Proc *p;
  952. if(up)
  953. print("up %lud\n", up->pid);
  954. else
  955. print("no current process\n");
  956. for(i=0; i<conf.nproc; i++) {
  957. p = &procalloc.arena[i];
  958. if(p->state == Dead)
  959. continue;
  960. dumpaproc(p);
  961. }
  962. }
  963. /*
  964. * wait till all processes have flushed their mmu
  965. * state about segement s
  966. */
  967. void
  968. procflushseg(Segment *s)
  969. {
  970. int i, ns, nm, nwait;
  971. Proc *p;
  972. /*
  973. * tell all processes with this
  974. * segment to flush their mmu's
  975. */
  976. nwait = 0;
  977. for(i=0; i<conf.nproc; i++) {
  978. p = &procalloc.arena[i];
  979. if(p->state == Dead)
  980. continue;
  981. for(ns = 0; ns < NSEG; ns++)
  982. if(p->seg[ns] == s){
  983. p->newtlb = 1;
  984. for(nm = 0; nm < conf.nmach; nm++){
  985. if(MACHP(nm)->proc == p){
  986. MACHP(nm)->flushmmu = 1;
  987. nwait++;
  988. }
  989. }
  990. break;
  991. }
  992. }
  993. if(nwait == 0)
  994. return;
  995. /*
  996. * wait for all processors to take a clock interrupt
  997. * and flush their mmu's
  998. */
  999. for(nm = 0; nm < conf.nmach; nm++)
  1000. if(MACHP(nm) != m)
  1001. while(MACHP(nm)->flushmmu)
  1002. sched();
  1003. }
  1004. void
  1005. scheddump(void)
  1006. {
  1007. Proc *p;
  1008. Schedq *rq;
  1009. for(rq = &runq[Nrq-1]; rq >= runq; rq--){
  1010. if(rq->head == 0)
  1011. continue;
  1012. print("rq%ld:", rq-runq);
  1013. for(p = rq->head; p; p = p->rnext)
  1014. print(" %lud(%lud)", p->pid, m->ticks - p->readytime);
  1015. print("\n");
  1016. delay(150);
  1017. }
  1018. print("nrdy %d\n", nrdy);
  1019. }
  1020. void
  1021. kproc(char *name, void (*func)(void *), void *arg)
  1022. {
  1023. Proc *p;
  1024. static Pgrp *kpgrp;
  1025. p = newproc();
  1026. p->psstate = 0;
  1027. p->procmode = 0640;
  1028. p->kp = 1;
  1029. p->noswap = 1;
  1030. p->fpsave = up->fpsave;
  1031. p->scallnr = up->scallnr;
  1032. p->s = up->s;
  1033. p->nerrlab = 0;
  1034. p->slash = up->slash;
  1035. p->dot = up->dot;
  1036. incref(p->dot);
  1037. memmove(p->note, up->note, sizeof(p->note));
  1038. p->nnote = up->nnote;
  1039. p->notified = 0;
  1040. p->lastnote = up->lastnote;
  1041. p->notify = up->notify;
  1042. p->ureg = 0;
  1043. p->dbgreg = 0;
  1044. procpriority(p, PriKproc, 0);
  1045. kprocchild(p, func, arg);
  1046. kstrdup(&p->user, eve);
  1047. kstrdup(&p->text, name);
  1048. if(kpgrp == 0)
  1049. kpgrp = newpgrp();
  1050. p->pgrp = kpgrp;
  1051. incref(kpgrp);
  1052. memset(p->time, 0, sizeof(p->time));
  1053. p->time[TReal] = MACHP(0)->ticks;
  1054. ready(p);
  1055. /*
  1056. * since the bss/data segments are now shareable,
  1057. * any mmu info about this process is now stale
  1058. * and has to be discarded.
  1059. */
  1060. p->newtlb = 1;
  1061. flushmmu();
  1062. }
  1063. /*
  1064. * called splhi() by notify(). See comment in notify for the
  1065. * reasoning.
  1066. */
  1067. void
  1068. procctl(Proc *p)
  1069. {
  1070. char *state;
  1071. ulong s;
  1072. switch(p->procctl) {
  1073. case Proc_exitbig:
  1074. spllo();
  1075. pexit("Killed: Insufficient physical memory", 1);
  1076. case Proc_exitme:
  1077. spllo(); /* pexit has locks in it */
  1078. pexit("Killed", 1);
  1079. case Proc_traceme:
  1080. if(p->nnote == 0)
  1081. return;
  1082. /* No break */
  1083. case Proc_stopme:
  1084. p->procctl = 0;
  1085. state = p->psstate;
  1086. p->psstate = "Stopped";
  1087. /* free a waiting debugger */
  1088. s = spllo();
  1089. qlock(&p->debug);
  1090. if(p->pdbg) {
  1091. wakeup(&p->pdbg->sleep);
  1092. p->pdbg = 0;
  1093. }
  1094. qunlock(&p->debug);
  1095. splhi();
  1096. p->state = Stopped;
  1097. if (edf->isedf(up))
  1098. edf->edfblock(up);
  1099. sched();
  1100. p->psstate = state;
  1101. splx(s);
  1102. return;
  1103. }
  1104. }
  1105. #include "errstr.h"
  1106. void
  1107. error(char *err)
  1108. {
  1109. spllo();
  1110. kstrcpy(up->errstr, err, ERRMAX);
  1111. setlabel(&up->errlab[NERR-1]);
  1112. nexterror();
  1113. }
  1114. void
  1115. nexterror(void)
  1116. {
  1117. gotolabel(&up->errlab[--up->nerrlab]);
  1118. }
  1119. void
  1120. exhausted(char *resource)
  1121. {
  1122. char buf[ERRMAX];
  1123. sprint(buf, "no free %s", resource);
  1124. iprint("%s\n", buf);
  1125. error(buf);
  1126. }
  1127. void
  1128. killbig(void)
  1129. {
  1130. int i;
  1131. Segment *s;
  1132. ulong l, max;
  1133. Proc *p, *ep, *kp;
  1134. max = 0;
  1135. kp = 0;
  1136. ep = procalloc.arena+conf.nproc;
  1137. for(p = procalloc.arena; p < ep; p++) {
  1138. if(p->state == Dead || p->kp)
  1139. continue;
  1140. l = 0;
  1141. for(i=1; i<NSEG; i++) {
  1142. s = p->seg[i];
  1143. if(s != 0)
  1144. l += s->top - s->base;
  1145. }
  1146. if(l > max && strcmp(p->text, "kfs") != 0){
  1147. kp = p;
  1148. max = l;
  1149. }
  1150. }
  1151. kp->procctl = Proc_exitbig;
  1152. for(i = 0; i < NSEG; i++) {
  1153. s = kp->seg[i];
  1154. if(s != 0 && canqlock(&s->lk)) {
  1155. mfreeseg(s, s->base, (s->top - s->base)/BY2PG);
  1156. qunlock(&s->lk);
  1157. }
  1158. }
  1159. print("%lud: %s killed because no swap configured\n", kp->pid, kp->text);
  1160. }
  1161. /*
  1162. * change ownership to 'new' of all processes owned by 'old'. Used when
  1163. * eve changes.
  1164. */
  1165. void
  1166. renameuser(char *old, char *new)
  1167. {
  1168. Proc *p, *ep;
  1169. ep = procalloc.arena+conf.nproc;
  1170. for(p = procalloc.arena; p < ep; p++)
  1171. if(p->user!=nil && strcmp(old, p->user)==0)
  1172. kstrdup(&p->user, new);
  1173. }
  1174. /*
  1175. * time accounting called by clock() splhi'd
  1176. */
  1177. void
  1178. accounttime(void)
  1179. {
  1180. Proc *p;
  1181. ulong n, per;
  1182. static ulong nrun;
  1183. p = m->proc;
  1184. if(p) {
  1185. nrun++;
  1186. p->time[p->insyscall]++;
  1187. }
  1188. /* calculate decaying duty cycles */
  1189. n = perfticks();
  1190. per = n - m->perf.last;
  1191. m->perf.last = n;
  1192. per = (m->perf.period*(HZ-1) + per)/HZ;
  1193. if(per != 0)
  1194. m->perf.period = per;
  1195. m->perf.avg_inidle = (m->perf.avg_inidle*(HZ-1)+m->perf.inidle)/HZ;
  1196. m->perf.inidle = 0;
  1197. m->perf.avg_inintr = (m->perf.avg_inintr*(HZ-1)+m->perf.inintr)/HZ;
  1198. m->perf.inintr = 0;
  1199. /* only one processor gets to compute system load averages */
  1200. if(m->machno != 0)
  1201. return;
  1202. /* calculate decaying load average */
  1203. n = nrun;
  1204. nrun = 0;
  1205. n = (nrdy+n)*1000;
  1206. m->load = (m->load*19+n)/20;
  1207. }
  1208. static void
  1209. pidhash(Proc *p)
  1210. {
  1211. int h;
  1212. h = p->pid % nelem(procalloc.ht);
  1213. lock(&procalloc);
  1214. p->pidhash = procalloc.ht[h];
  1215. procalloc.ht[h] = p;
  1216. unlock(&procalloc);
  1217. }
  1218. static void
  1219. pidunhash(Proc *p)
  1220. {
  1221. int h;
  1222. Proc **l;
  1223. h = p->pid % nelem(procalloc.ht);
  1224. lock(&procalloc);
  1225. for(l = &procalloc.ht[h]; *l != nil; l = &(*l)->pidhash)
  1226. if(*l == p){
  1227. *l = p->pidhash;
  1228. break;
  1229. }
  1230. unlock(&procalloc);
  1231. }
  1232. int
  1233. procindex(ulong pid)
  1234. {
  1235. Proc *p;
  1236. int h;
  1237. int s;
  1238. s = -1;
  1239. h = pid % nelem(procalloc.ht);
  1240. lock(&procalloc);
  1241. for(p = procalloc.ht[h]; p != nil; p = p->pidhash)
  1242. if(p->pid == pid){
  1243. s = p - procalloc.arena;
  1244. break;
  1245. }
  1246. unlock(&procalloc);
  1247. return s;
  1248. }