proc.c 24 KB

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