proc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. /* this comparison is too arbitrary - need a better one */
  298. /* presotto */
  299. if(m->ticks - p->readytime < quanta[p->priority]/4)
  300. continue;
  301. splhi();
  302. p = dequeueproc(rq, p);
  303. spllo();
  304. if(p == nil)
  305. continue;
  306. p->quanta = quanta[p->priority]; /* act like we used none */
  307. ready(p);
  308. }
  309. }
  310. /*
  311. * pick a process to run
  312. */
  313. Proc*
  314. runproc(void)
  315. {
  316. Schedq *rq;
  317. Proc *p;
  318. ulong start, now;
  319. int i;
  320. start = perfticks();
  321. if((p = edf->edfrunproc()) != nil)
  322. return p;
  323. /* 10 is completely arbitrary - it interacts with the comparison in rebalance */
  324. /* presotto */
  325. if(m->fairness++ == 10){
  326. m->fairness = 0;
  327. rebalance();
  328. }
  329. loop:
  330. /*
  331. * find a process that last ran on this processor (affinity),
  332. * or one that hasn't moved in a while (load balancing). Every
  333. * time around the loop affinity goes down.
  334. */
  335. spllo();
  336. for(i = 0;; i++){
  337. /*
  338. * find the highest priority target process that this
  339. * processor can run given affinity constraints.
  340. *
  341. */
  342. for(rq = &runq[Nrq-1]; rq >= runq; rq--){
  343. for(p = rq->head; p; p = p->rnext){
  344. if(p->mp == nil || p->mp == MACHP(m->machno)
  345. || (!p->wired && i > 0))
  346. goto found;
  347. }
  348. }
  349. /* waste time or halt the CPU */
  350. idlehands();
  351. /* remember how much time we're here */
  352. now = perfticks();
  353. m->perf.inidle += now-start;
  354. start = now;
  355. }
  356. found:
  357. splhi();
  358. p = dequeueproc(rq, p);
  359. if(p == nil)
  360. goto loop;
  361. p->state = Scheding;
  362. p->mp = MACHP(m->machno);
  363. return p;
  364. }
  365. int
  366. canpage(Proc *p)
  367. {
  368. int ok = 0;
  369. splhi();
  370. lock(runq);
  371. /* Only reliable way to see if we are Running */
  372. if(p->mach == 0) {
  373. p->newtlb = 1;
  374. ok = 1;
  375. }
  376. unlock(runq);
  377. spllo();
  378. return ok;
  379. }
  380. Proc*
  381. newproc(void)
  382. {
  383. Proc *p;
  384. lock(&procalloc);
  385. for(;;) {
  386. if(p = procalloc.free)
  387. break;
  388. unlock(&procalloc);
  389. resrcwait("no procs");
  390. lock(&procalloc);
  391. }
  392. procalloc.free = p->qnext;
  393. unlock(&procalloc);
  394. p->state = Scheding;
  395. p->psstate = "New";
  396. p->mach = 0;
  397. p->qnext = 0;
  398. p->nchild = 0;
  399. p->nwait = 0;
  400. p->waitq = 0;
  401. p->parent = 0;
  402. p->pgrp = 0;
  403. p->egrp = 0;
  404. p->fgrp = 0;
  405. p->rgrp = 0;
  406. p->pdbg = 0;
  407. p->fpstate = FPinit;
  408. p->kp = 0;
  409. p->procctl = 0;
  410. p->notepending = 0;
  411. p->ureg = 0;
  412. p->privatemem = 0;
  413. p->noswap = 0;
  414. p->errstr = p->errbuf0;
  415. p->syserrstr = p->errbuf1;
  416. p->errbuf0[0] = '\0';
  417. p->errbuf1[0] = '\0';
  418. p->nlocks = 0;
  419. p->delaysched = 0;
  420. kstrdup(&p->user, "*nouser");
  421. kstrdup(&p->text, "*notext");
  422. kstrdup(&p->args, "");
  423. p->nargs = 0;
  424. p->setargs = 0;
  425. memset(p->seg, 0, sizeof p->seg);
  426. p->pid = incref(&pidalloc);
  427. pidhash(p);
  428. p->noteid = incref(&noteidalloc);
  429. if(p->pid==0 || p->noteid==0)
  430. panic("pidalloc");
  431. if(p->kstack == 0)
  432. p->kstack = smalloc(KSTACK);
  433. /* sched params */
  434. p->mp = 0;
  435. p->wired = 0;
  436. procpriority(p, PriNormal, 0);
  437. p->task = nil;
  438. return p;
  439. }
  440. /*
  441. * wire this proc to a machine
  442. */
  443. void
  444. procwired(Proc *p, int bm)
  445. {
  446. Proc *pp;
  447. int i;
  448. char nwired[MAXMACH];
  449. Mach *wm;
  450. if(bm < 0){
  451. /* pick a machine to wire to */
  452. memset(nwired, 0, sizeof(nwired));
  453. p->wired = 0;
  454. pp = proctab(0);
  455. for(i=0; i<conf.nproc; i++, pp++){
  456. wm = pp->wired;
  457. if(wm && pp->pid)
  458. nwired[wm->machno]++;
  459. }
  460. bm = 0;
  461. for(i=0; i<conf.nmach; i++)
  462. if(nwired[i] < nwired[bm])
  463. bm = i;
  464. } else {
  465. /* use the virtual machine requested */
  466. bm = bm % conf.nmach;
  467. }
  468. p->wired = MACHP(bm);
  469. p->mp = p->wired;
  470. }
  471. void
  472. procpriority(Proc *p, int pri, int fixed)
  473. {
  474. if(pri >= Nrq)
  475. pri = Nrq - 1;
  476. else if(pri < 0)
  477. pri = 0;
  478. p->basepri = pri;
  479. p->priority = pri;
  480. if(fixed){
  481. p->quanta = 0xfffff;
  482. p->fixedpri = 1;
  483. } else {
  484. p->quanta = quanta[pri];
  485. p->fixedpri = 0;
  486. }
  487. }
  488. void
  489. procinit0(void) /* bad planning - clashes with devproc.c */
  490. {
  491. Proc *p;
  492. int i;
  493. procalloc.free = xalloc(conf.nproc*sizeof(Proc));
  494. if(procalloc.free == nil)
  495. panic("cannot allocate %lud procs\n", conf.nproc);
  496. procalloc.arena = procalloc.free;
  497. p = procalloc.free;
  498. for(i=0; i<conf.nproc-1; i++,p++)
  499. p->qnext = p+1;
  500. p->qnext = 0;
  501. }
  502. /*
  503. * sleep if a condition is not true. Another process will
  504. * awaken us after it sets the condition. When we awaken
  505. * the condition may no longer be true.
  506. *
  507. * we lock both the process and the rendezvous to keep r->p
  508. * and p->r synchronized.
  509. */
  510. void
  511. sleep(Rendez *r, int (*f)(void*), void *arg)
  512. {
  513. int s;
  514. s = splhi();
  515. if(up->nlocks)
  516. print("process %lud sleeps with %lud locks held, last lock 0x%p locked at pc 0x%lux\n",
  517. up->pid, up->nlocks, up->lastlock, up->lastlock->pc);
  518. lock(r);
  519. lock(&up->rlock);
  520. if(r->p){
  521. print("double sleep %lud %lud\n", r->p->pid, up->pid);
  522. dumpstack();
  523. }
  524. /*
  525. * Wakeup only knows there may be something to do by testing
  526. * r->p in order to get something to lock on.
  527. * Flush that information out to memory in case the sleep is
  528. * committed.
  529. */
  530. r->p = up;
  531. if((*f)(arg) || up->notepending){
  532. /*
  533. * if condition happened or a note is pending
  534. * never mind
  535. */
  536. r->p = nil;
  537. unlock(&up->rlock);
  538. unlock(r);
  539. } else {
  540. /*
  541. * now we are committed to
  542. * change state and call scheduler
  543. */
  544. up->state = Wakeme;
  545. up->r = r;
  546. /* statistics */
  547. m->cs++;
  548. procsave(up);
  549. if(setlabel(&up->sched)) {
  550. /*
  551. * here when the process is awakened
  552. */
  553. procrestore(up);
  554. spllo();
  555. } else {
  556. /*
  557. * here to go to sleep (i.e. stop Running)
  558. */
  559. unlock(&up->rlock);
  560. unlock(r);
  561. if(edf->isedf(up))
  562. edf->edfblock(up);
  563. gotolabel(&m->sched);
  564. }
  565. }
  566. if(up->notepending) {
  567. up->notepending = 0;
  568. splx(s);
  569. error(Eintr);
  570. }
  571. splx(s);
  572. }
  573. int
  574. tfn(void *arg)
  575. {
  576. return MACHP(0)->ticks >= up->twhen || up->tfn(arg);
  577. }
  578. void
  579. tsleep(Rendez *r, int (*fn)(void*), void *arg, int ms)
  580. {
  581. ulong when;
  582. Proc *f, **l;
  583. when = ms2tk(ms) + MACHP(0)->ticks;
  584. lock(&talarm);
  585. /* take out of list if checkalarm didn't */
  586. if(up->trend) {
  587. l = &talarm.list;
  588. for(f = *l; f; f = f->tlink) {
  589. if(f == up) {
  590. *l = up->tlink;
  591. break;
  592. }
  593. l = &f->tlink;
  594. }
  595. }
  596. /* insert in increasing time order */
  597. l = &talarm.list;
  598. for(f = *l; f; f = f->tlink) {
  599. if(f->twhen >= when)
  600. break;
  601. l = &f->tlink;
  602. }
  603. up->trend = r;
  604. up->twhen = when;
  605. up->tfn = fn;
  606. up->tlink = *l;
  607. *l = up;
  608. unlock(&talarm);
  609. if(waserror()){
  610. up->twhen = 0;
  611. nexterror();
  612. }
  613. sleep(r, tfn, arg);
  614. up->twhen = 0;
  615. poperror();
  616. }
  617. /*
  618. * Expects that only one process can call wakeup for any given Rendez.
  619. * We hold both locks to ensure that r->p and p->r remain consistent.
  620. * Richard Miller has a better solution that doesn't require both to
  621. * be held simultaneously, but I'm a paranoid - presotto.
  622. */
  623. Proc*
  624. wakeup(Rendez *r)
  625. {
  626. Proc *p;
  627. int s;
  628. s = splhi();
  629. lock(r);
  630. p = r->p;
  631. if(p != nil){
  632. lock(&p->rlock);
  633. if(p->state != Wakeme || p->r != r)
  634. panic("wakeup: state");
  635. r->p = nil;
  636. p->r = nil;
  637. ready(p);
  638. unlock(&p->rlock);
  639. }
  640. unlock(r);
  641. splx(s);
  642. return p;
  643. }
  644. /*
  645. * if waking a sleeping process, this routine must hold both
  646. * p->rlock and r->lock. However, it can't know them in
  647. * the same order as wakeup causing a possible lock ordering
  648. * deadlock. We break the deadlock by giving up the p->rlock
  649. * lock if we can't get the r->lock and retrying.
  650. */
  651. int
  652. postnote(Proc *p, int dolock, char *n, int flag)
  653. {
  654. int s, ret;
  655. Rendez *r;
  656. Proc *d, **l;
  657. if(dolock)
  658. qlock(&p->debug);
  659. if(flag != NUser && (p->notify == 0 || p->notified))
  660. p->nnote = 0;
  661. ret = 0;
  662. if(p->nnote < NNOTE) {
  663. strcpy(p->note[p->nnote].msg, n);
  664. p->note[p->nnote++].flag = flag;
  665. ret = 1;
  666. }
  667. p->notepending = 1;
  668. if(dolock)
  669. qunlock(&p->debug);
  670. /* this loop is to avoid lock ordering problems. */
  671. for(;;){
  672. s = splhi();
  673. lock(&p->rlock);
  674. r = p->r;
  675. /* waiting for a wakeup? */
  676. if(r == nil)
  677. break; /* no */
  678. /* try for the second lock */
  679. if(canlock(r)){
  680. if(p->state != Wakeme || r->p != p)
  681. panic("postnote: state %d %d %d", r->p != p, p->r != r, p->state);
  682. p->r = nil;
  683. r->p = nil;
  684. ready(p);
  685. unlock(r);
  686. break;
  687. }
  688. /* give other process time to get out of critical section and try again */
  689. unlock(&p->rlock);
  690. splx(s);
  691. sched();
  692. }
  693. unlock(&p->rlock);
  694. splx(s);
  695. if(p->state != Rendezvous)
  696. return ret;
  697. /* Try and pull out of a rendezvous */
  698. lock(p->rgrp);
  699. if(p->state == Rendezvous) {
  700. p->rendval = ~0;
  701. l = &REND(p->rgrp, p->rendtag);
  702. for(d = *l; d; d = d->rendhash) {
  703. if(d == p) {
  704. *l = p->rendhash;
  705. break;
  706. }
  707. l = &d->rendhash;
  708. }
  709. ready(p);
  710. }
  711. unlock(p->rgrp);
  712. return ret;
  713. }
  714. /*
  715. * weird thing: keep at most NBROKEN around
  716. */
  717. #define NBROKEN 4
  718. struct
  719. {
  720. QLock;
  721. int n;
  722. Proc *p[NBROKEN];
  723. }broken;
  724. void
  725. addbroken(Proc *p)
  726. {
  727. qlock(&broken);
  728. if(broken.n == NBROKEN) {
  729. ready(broken.p[0]);
  730. memmove(&broken.p[0], &broken.p[1], sizeof(Proc*)*(NBROKEN-1));
  731. --broken.n;
  732. }
  733. broken.p[broken.n++] = p;
  734. qunlock(&broken);
  735. if(edf->isedf(up))
  736. edf->edfbury(up);
  737. p->state = Broken;
  738. p->psstate = 0;
  739. sched();
  740. }
  741. void
  742. unbreak(Proc *p)
  743. {
  744. int b;
  745. qlock(&broken);
  746. for(b=0; b < broken.n; b++)
  747. if(broken.p[b] == p) {
  748. broken.n--;
  749. memmove(&broken.p[b], &broken.p[b+1],
  750. sizeof(Proc*)*(NBROKEN-(b+1)));
  751. ready(p);
  752. break;
  753. }
  754. qunlock(&broken);
  755. }
  756. int
  757. freebroken(void)
  758. {
  759. int i, n;
  760. qlock(&broken);
  761. n = broken.n;
  762. for(i=0; i<n; i++) {
  763. ready(broken.p[i]);
  764. broken.p[i] = 0;
  765. }
  766. broken.n = 0;
  767. qunlock(&broken);
  768. return n;
  769. }
  770. void
  771. pexit(char *exitstr, int freemem)
  772. {
  773. Proc *p;
  774. Segment **s, **es;
  775. long utime, stime;
  776. Waitq *wq, *f, *next;
  777. Fgrp *fgrp;
  778. Egrp *egrp;
  779. Rgrp *rgrp;
  780. Pgrp *pgrp;
  781. Chan *dot;
  782. up->alarm = 0;
  783. /* nil out all the resources under lock (free later) */
  784. qlock(&up->debug);
  785. fgrp = up->fgrp;
  786. up->fgrp = nil;
  787. egrp = up->egrp;
  788. up->egrp = nil;
  789. rgrp = up->rgrp;
  790. up->rgrp = nil;
  791. pgrp = up->pgrp;
  792. up->pgrp = nil;
  793. dot = up->dot;
  794. up->dot = nil;
  795. qunlock(&up->debug);
  796. if(fgrp)
  797. closefgrp(fgrp);
  798. if(egrp)
  799. closeegrp(egrp);
  800. if(rgrp)
  801. closergrp(rgrp);
  802. if(dot)
  803. cclose(dot);
  804. if(pgrp)
  805. closepgrp(pgrp);
  806. /*
  807. * if not a kernel process and have a parent,
  808. * do some housekeeping.
  809. */
  810. if(up->kp == 0) {
  811. p = up->parent;
  812. if(p == 0) {
  813. if(exitstr == 0)
  814. exitstr = "unknown";
  815. panic("boot process died: %s", exitstr);
  816. }
  817. while(waserror())
  818. ;
  819. wq = smalloc(sizeof(Waitq));
  820. poperror();
  821. wq->w.pid = up->pid;
  822. utime = up->time[TUser] + up->time[TCUser];
  823. stime = up->time[TSys] + up->time[TCSys];
  824. wq->w.time[TUser] = tk2ms(utime);
  825. wq->w.time[TSys] = tk2ms(stime);
  826. wq->w.time[TReal] = tk2ms(MACHP(0)->ticks - up->time[TReal]);
  827. if(exitstr && exitstr[0])
  828. snprint(wq->w.msg, sizeof(wq->w.msg), "%s %lud: %s", up->text, up->pid, exitstr);
  829. else
  830. wq->w.msg[0] = '\0';
  831. lock(&p->exl);
  832. /*
  833. * If my parent is no longer alive, or if there would be more
  834. * than 128 zombie child processes for my parent, then don't
  835. * leave a wait record behind. This helps prevent badly
  836. * written daemon processes from accumulating lots of wait
  837. * records.
  838. */
  839. if(p->pid == up->parentpid && p->state != Broken && p->nwait < 128) {
  840. p->nchild--;
  841. p->time[TCUser] += utime;
  842. p->time[TCSys] += stime;
  843. wq->next = p->waitq;
  844. p->waitq = wq;
  845. p->nwait++;
  846. wakeup(&p->waitr);
  847. unlock(&p->exl);
  848. }
  849. else {
  850. unlock(&p->exl);
  851. free(wq);
  852. }
  853. }
  854. if(!freemem)
  855. addbroken(up);
  856. qlock(&up->seglock);
  857. es = &up->seg[NSEG];
  858. for(s = up->seg; s < es; s++) {
  859. if(*s) {
  860. putseg(*s);
  861. *s = 0;
  862. }
  863. }
  864. qunlock(&up->seglock);
  865. lock(&up->exl); /* Prevent my children from leaving waits */
  866. pidunhash(up);
  867. up->pid = 0;
  868. wakeup(&up->waitr);
  869. unlock(&up->exl);
  870. for(f = up->waitq; f; f = next) {
  871. next = f->next;
  872. free(f);
  873. }
  874. /* release debuggers */
  875. qlock(&up->debug);
  876. if(up->pdbg) {
  877. wakeup(&up->pdbg->sleep);
  878. up->pdbg = 0;
  879. }
  880. qunlock(&up->debug);
  881. /* Sched must not loop for these locks */
  882. lock(&procalloc);
  883. lock(&palloc);
  884. if(edf->isedf(up))
  885. edf->edfbury(up);
  886. up->state = Moribund;
  887. sched();
  888. panic("pexit");
  889. }
  890. int
  891. haswaitq(void *x)
  892. {
  893. Proc *p;
  894. p = (Proc *)x;
  895. return p->waitq != 0;
  896. }
  897. ulong
  898. pwait(Waitmsg *w)
  899. {
  900. ulong cpid;
  901. Waitq *wq;
  902. if(!canqlock(&up->qwaitr))
  903. error(Einuse);
  904. if(waserror()) {
  905. qunlock(&up->qwaitr);
  906. nexterror();
  907. }
  908. lock(&up->exl);
  909. if(up->nchild == 0 && up->waitq == 0) {
  910. unlock(&up->exl);
  911. error(Enochild);
  912. }
  913. unlock(&up->exl);
  914. sleep(&up->waitr, haswaitq, up);
  915. lock(&up->exl);
  916. wq = up->waitq;
  917. up->waitq = wq->next;
  918. up->nwait--;
  919. unlock(&up->exl);
  920. qunlock(&up->qwaitr);
  921. poperror();
  922. if(w)
  923. memmove(w, &wq->w, sizeof(Waitmsg));
  924. cpid = wq->w.pid;
  925. free(wq);
  926. return cpid;
  927. }
  928. Proc*
  929. proctab(int i)
  930. {
  931. return &procalloc.arena[i];
  932. }
  933. void
  934. dumpaproc(Proc *p)
  935. {
  936. ulong bss;
  937. char *s;
  938. if(p == 0)
  939. return;
  940. bss = 0;
  941. if(p->seg[BSEG])
  942. bss = p->seg[BSEG]->top;
  943. s = p->psstate;
  944. if(s == 0)
  945. s = statename[p->state];
  946. 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",
  947. p->pid, p->text, p->pc, dbgpc(p), s, statename[p->state],
  948. p->time[0], p->time[1], bss, p->qpc, p->nlocks, p->delaysched, p->lastlock ? p->lastlock->pc : 0, p->priority);
  949. }
  950. void
  951. procdump(void)
  952. {
  953. int i;
  954. Proc *p;
  955. if(up)
  956. print("up %lud\n", up->pid);
  957. else
  958. print("no current process\n");
  959. for(i=0; i<conf.nproc; i++) {
  960. p = &procalloc.arena[i];
  961. if(p->state == Dead)
  962. continue;
  963. dumpaproc(p);
  964. }
  965. }
  966. /*
  967. * wait till all processes have flushed their mmu
  968. * state about segement s
  969. */
  970. void
  971. procflushseg(Segment *s)
  972. {
  973. int i, ns, nm, nwait;
  974. Proc *p;
  975. /*
  976. * tell all processes with this
  977. * segment to flush their mmu's
  978. */
  979. nwait = 0;
  980. for(i=0; i<conf.nproc; i++) {
  981. p = &procalloc.arena[i];
  982. if(p->state == Dead)
  983. continue;
  984. for(ns = 0; ns < NSEG; ns++)
  985. if(p->seg[ns] == s){
  986. p->newtlb = 1;
  987. for(nm = 0; nm < conf.nmach; nm++){
  988. if(MACHP(nm)->proc == p){
  989. MACHP(nm)->flushmmu = 1;
  990. nwait++;
  991. }
  992. }
  993. break;
  994. }
  995. }
  996. if(nwait == 0)
  997. return;
  998. /*
  999. * wait for all processors to take a clock interrupt
  1000. * and flush their mmu's
  1001. */
  1002. for(nm = 0; nm < conf.nmach; nm++)
  1003. if(MACHP(nm) != m)
  1004. while(MACHP(nm)->flushmmu)
  1005. sched();
  1006. }
  1007. void
  1008. scheddump(void)
  1009. {
  1010. Proc *p;
  1011. Schedq *rq;
  1012. for(rq = &runq[Nrq-1]; rq >= runq; rq--){
  1013. if(rq->head == 0)
  1014. continue;
  1015. print("rq%ld:", rq-runq);
  1016. for(p = rq->head; p; p = p->rnext)
  1017. print(" %lud(%lud)", p->pid, m->ticks - p->readytime);
  1018. print("\n");
  1019. delay(150);
  1020. }
  1021. print("nrdy %d\n", nrdy);
  1022. }
  1023. void
  1024. kproc(char *name, void (*func)(void *), void *arg)
  1025. {
  1026. Proc *p;
  1027. static Pgrp *kpgrp;
  1028. p = newproc();
  1029. p->psstate = 0;
  1030. p->procmode = 0640;
  1031. p->kp = 1;
  1032. p->noswap = 1;
  1033. p->fpsave = up->fpsave;
  1034. p->scallnr = up->scallnr;
  1035. p->s = up->s;
  1036. p->nerrlab = 0;
  1037. p->slash = up->slash;
  1038. p->dot = up->dot;
  1039. incref(p->dot);
  1040. memmove(p->note, up->note, sizeof(p->note));
  1041. p->nnote = up->nnote;
  1042. p->notified = 0;
  1043. p->lastnote = up->lastnote;
  1044. p->notify = up->notify;
  1045. p->ureg = 0;
  1046. p->dbgreg = 0;
  1047. procpriority(p, PriKproc, 0);
  1048. kprocchild(p, func, arg);
  1049. kstrdup(&p->user, eve);
  1050. kstrdup(&p->text, name);
  1051. if(kpgrp == 0)
  1052. kpgrp = newpgrp();
  1053. p->pgrp = kpgrp;
  1054. incref(kpgrp);
  1055. memset(p->time, 0, sizeof(p->time));
  1056. p->time[TReal] = MACHP(0)->ticks;
  1057. ready(p);
  1058. /*
  1059. * since the bss/data segments are now shareable,
  1060. * any mmu info about this process is now stale
  1061. * and has to be discarded.
  1062. */
  1063. p->newtlb = 1;
  1064. flushmmu();
  1065. }
  1066. /*
  1067. * called splhi() by notify(). See comment in notify for the
  1068. * reasoning.
  1069. */
  1070. void
  1071. procctl(Proc *p)
  1072. {
  1073. char *state;
  1074. ulong s;
  1075. switch(p->procctl) {
  1076. case Proc_exitbig:
  1077. spllo();
  1078. pexit("Killed: Insufficient physical memory", 1);
  1079. case Proc_exitme:
  1080. spllo(); /* pexit has locks in it */
  1081. pexit("Killed", 1);
  1082. case Proc_traceme:
  1083. if(p->nnote == 0)
  1084. return;
  1085. /* No break */
  1086. case Proc_stopme:
  1087. p->procctl = 0;
  1088. state = p->psstate;
  1089. p->psstate = "Stopped";
  1090. /* free a waiting debugger */
  1091. s = spllo();
  1092. qlock(&p->debug);
  1093. if(p->pdbg) {
  1094. wakeup(&p->pdbg->sleep);
  1095. p->pdbg = 0;
  1096. }
  1097. qunlock(&p->debug);
  1098. splhi();
  1099. p->state = Stopped;
  1100. if(edf->isedf(up))
  1101. edf->edfblock(up);
  1102. sched();
  1103. p->psstate = state;
  1104. splx(s);
  1105. return;
  1106. }
  1107. }
  1108. #include "errstr.h"
  1109. void
  1110. error(char *err)
  1111. {
  1112. spllo();
  1113. assert(up->nerrlab < NERR);
  1114. kstrcpy(up->errstr, err, ERRMAX);
  1115. setlabel(&up->errlab[NERR-1]);
  1116. nexterror();
  1117. }
  1118. void
  1119. nexterror(void)
  1120. {
  1121. gotolabel(&up->errlab[--up->nerrlab]);
  1122. }
  1123. void
  1124. exhausted(char *resource)
  1125. {
  1126. char buf[ERRMAX];
  1127. sprint(buf, "no free %s", resource);
  1128. iprint("%s\n", buf);
  1129. error(buf);
  1130. }
  1131. void
  1132. killbig(void)
  1133. {
  1134. int i;
  1135. Segment *s;
  1136. ulong l, max;
  1137. Proc *p, *ep, *kp;
  1138. max = 0;
  1139. kp = 0;
  1140. ep = procalloc.arena+conf.nproc;
  1141. for(p = procalloc.arena; p < ep; p++) {
  1142. if(p->state == Dead || p->kp)
  1143. continue;
  1144. l = 0;
  1145. for(i=1; i<NSEG; i++) {
  1146. s = p->seg[i];
  1147. if(s != 0)
  1148. l += s->top - s->base;
  1149. }
  1150. if(l > max && strcmp(p->text, "kfs") != 0){
  1151. kp = p;
  1152. max = l;
  1153. }
  1154. }
  1155. kp->procctl = Proc_exitbig;
  1156. for(i = 0; i < NSEG; i++) {
  1157. s = kp->seg[i];
  1158. if(s != 0 && canqlock(&s->lk)) {
  1159. mfreeseg(s, s->base, (s->top - s->base)/BY2PG);
  1160. qunlock(&s->lk);
  1161. }
  1162. }
  1163. print("%lud: %s killed because no swap configured\n", kp->pid, kp->text);
  1164. }
  1165. /*
  1166. * change ownership to 'new' of all processes owned by 'old'. Used when
  1167. * eve changes.
  1168. */
  1169. void
  1170. renameuser(char *old, char *new)
  1171. {
  1172. Proc *p, *ep;
  1173. ep = procalloc.arena+conf.nproc;
  1174. for(p = procalloc.arena; p < ep; p++)
  1175. if(p->user!=nil && strcmp(old, p->user)==0)
  1176. kstrdup(&p->user, new);
  1177. }
  1178. /*
  1179. * time accounting called by clock() splhi'd
  1180. */
  1181. void
  1182. accounttime(void)
  1183. {
  1184. Proc *p;
  1185. ulong n, per;
  1186. static ulong nrun;
  1187. p = m->proc;
  1188. if(p) {
  1189. nrun++;
  1190. p->time[p->insyscall]++;
  1191. }
  1192. /* calculate decaying duty cycles */
  1193. n = perfticks();
  1194. per = n - m->perf.last;
  1195. m->perf.last = n;
  1196. per = (m->perf.period*(HZ-1) + per)/HZ;
  1197. if(per != 0)
  1198. m->perf.period = per;
  1199. m->perf.avg_inidle = (m->perf.avg_inidle*(HZ-1)+m->perf.inidle)/HZ;
  1200. m->perf.inidle = 0;
  1201. m->perf.avg_inintr = (m->perf.avg_inintr*(HZ-1)+m->perf.inintr)/HZ;
  1202. m->perf.inintr = 0;
  1203. /* only one processor gets to compute system load averages */
  1204. if(m->machno != 0)
  1205. return;
  1206. /* calculate decaying load average */
  1207. n = nrun;
  1208. nrun = 0;
  1209. n = (nrdy+n)*1000;
  1210. m->load = (m->load*19+n)/20;
  1211. }
  1212. static void
  1213. pidhash(Proc *p)
  1214. {
  1215. int h;
  1216. h = p->pid % nelem(procalloc.ht);
  1217. lock(&procalloc);
  1218. p->pidhash = procalloc.ht[h];
  1219. procalloc.ht[h] = p;
  1220. unlock(&procalloc);
  1221. }
  1222. static void
  1223. pidunhash(Proc *p)
  1224. {
  1225. int h;
  1226. Proc **l;
  1227. h = p->pid % nelem(procalloc.ht);
  1228. lock(&procalloc);
  1229. for(l = &procalloc.ht[h]; *l != nil; l = &(*l)->pidhash)
  1230. if(*l == p){
  1231. *l = p->pidhash;
  1232. break;
  1233. }
  1234. unlock(&procalloc);
  1235. }
  1236. int
  1237. procindex(ulong pid)
  1238. {
  1239. Proc *p;
  1240. int h;
  1241. int s;
  1242. s = -1;
  1243. h = pid % nelem(procalloc.ht);
  1244. lock(&procalloc);
  1245. for(p = procalloc.ht[h]; p != nil; p = p->pidhash)
  1246. if(p->pid == pid){
  1247. s = p - procalloc.arena;
  1248. break;
  1249. }
  1250. unlock(&procalloc);
  1251. return s;
  1252. }