proc.c 24 KB

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