devproc.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. #include "u.h"
  2. #include <trace.h>
  3. #include "tos.h"
  4. #include "../port/lib.h"
  5. #include "mem.h"
  6. #include "dat.h"
  7. #include "fns.h"
  8. #include "../port/error.h"
  9. #include "ureg.h"
  10. #include "edf.h"
  11. enum
  12. {
  13. Qdir,
  14. Qtrace,
  15. Qargs,
  16. Qctl,
  17. Qfd,
  18. Qfpregs,
  19. Qkregs,
  20. Qmem,
  21. Qnote,
  22. Qnoteid,
  23. Qnotepg,
  24. Qns,
  25. Qproc,
  26. Qregs,
  27. Qsegment,
  28. Qstatus,
  29. Qtext,
  30. Qwait,
  31. Qprofile,
  32. };
  33. enum
  34. {
  35. CMclose,
  36. CMclosefiles,
  37. CMfixedpri,
  38. CMhang,
  39. CMkill,
  40. CMnohang,
  41. CMnoswap,
  42. CMpri,
  43. CMprivate,
  44. CMprofile,
  45. CMstart,
  46. CMstartstop,
  47. CMstartsyscall,
  48. CMstop,
  49. CMwaitstop,
  50. CMwired,
  51. CMfair,
  52. CMunfair,
  53. CMtrace,
  54. /* real time */
  55. CMperiod,
  56. CMdeadline,
  57. CMcost,
  58. CMsporadic,
  59. CMdeadlinenotes,
  60. CMadmit,
  61. CMexpel,
  62. };
  63. enum{
  64. Nevents = 0x4000,
  65. Emask = Nevents - 1,
  66. };
  67. #define STATSIZE (2*KNAMELEN+12+9*12)
  68. /*
  69. * Status, fd, and ns are left fully readable (0444) because of their use in debugging,
  70. * particularly on shared servers.
  71. * Arguably, ns and fd shouldn't be readable; if you'd prefer, change them to 0000
  72. */
  73. Dirtab procdir[] =
  74. {
  75. "args", {Qargs}, 0, 0660,
  76. "ctl", {Qctl}, 0, 0000,
  77. "fd", {Qfd}, 0, 0444,
  78. "fpregs", {Qfpregs}, sizeof(FPsave), 0000,
  79. "kregs", {Qkregs}, sizeof(Ureg), 0400,
  80. "mem", {Qmem}, 0, 0000,
  81. "note", {Qnote}, 0, 0000,
  82. "noteid", {Qnoteid}, 0, 0664,
  83. "notepg", {Qnotepg}, 0, 0000,
  84. "ns", {Qns}, 0, 0444,
  85. "proc", {Qproc}, 0, 0400,
  86. "regs", {Qregs}, sizeof(Ureg), 0000,
  87. "segment", {Qsegment}, 0, 0444,
  88. "status", {Qstatus}, STATSIZE, 0444,
  89. "text", {Qtext}, 0, 0000,
  90. "wait", {Qwait}, 0, 0400,
  91. "profile", {Qprofile}, 0, 0400,
  92. };
  93. static
  94. Cmdtab proccmd[] = {
  95. CMclose, "close", 2,
  96. CMclosefiles, "closefiles", 1,
  97. CMfixedpri, "fixedpri", 2,
  98. CMhang, "hang", 1,
  99. CMnohang, "nohang", 1,
  100. CMnoswap, "noswap", 1,
  101. CMkill, "kill", 1,
  102. CMpri, "pri", 2,
  103. CMprivate, "private", 1,
  104. CMprofile, "profile", 1,
  105. CMstart, "start", 1,
  106. CMstartstop, "startstop", 1,
  107. CMstartsyscall, "startsyscall", 1,
  108. CMstop, "stop", 1,
  109. CMwaitstop, "waitstop", 1,
  110. CMwired, "wired", 2,
  111. CMfair, "fair", 1,
  112. CMunfair, "unfair", 1,
  113. CMtrace, "trace", 1,
  114. CMperiod, "period", 2,
  115. CMdeadline, "deadline", 2,
  116. CMcost, "cost", 2,
  117. CMsporadic, "sporadic", 1,
  118. CMdeadlinenotes, "deadlinenotes", 1,
  119. CMadmit, "admit", 1,
  120. CMexpel, "expel", 1,
  121. };
  122. /* Segment type from portdat.h */
  123. static char *sname[]={ "Text", "Data", "Bss", "Stack", "Shared", "Phys", };
  124. /*
  125. * Qids are, in path:
  126. * 4 bits of file type (qids above)
  127. * 23 bits of process slot number + 1
  128. * in vers,
  129. * 32 bits of pid, for consistency checking
  130. * If notepg, c->pgrpid.path is pgrp slot, .vers is noteid.
  131. */
  132. #define QSHIFT 5 /* location in qid of proc slot # */
  133. #define QID(q) ((((ulong)(q).path)&0x0000001F)>>0)
  134. #define SLOT(q) (((((ulong)(q).path)&0x07FFFFFE0)>>QSHIFT)-1)
  135. #define PID(q) ((q).vers)
  136. #define NOTEID(q) ((q).vers)
  137. void procctlreq(Proc*, char*, int);
  138. int procctlmemio(Proc*, ulong, int, void*, int);
  139. Chan* proctext(Chan*, Proc*);
  140. Segment* txt2data(Proc*, Segment*);
  141. int procstopped(void*);
  142. void mntscan(Mntwalk*, Proc*);
  143. static Traceevent *tevents;
  144. static Lock tlock;
  145. static int topens;
  146. static int tproduced, tconsumed;
  147. static Rendez teventr;
  148. void (*proctrace)(Proc*, int, vlong);
  149. extern int unfair;
  150. static void
  151. profclock(Ureg *ur, Timer *)
  152. {
  153. Tos *tos;
  154. if(up == 0 || up->state != Running)
  155. return;
  156. /* user profiling clock */
  157. if(userureg(ur)){
  158. tos = (Tos*)(USTKTOP-sizeof(Tos));
  159. tos->clock += TK2MS(1);
  160. segclock(ur->pc);
  161. }
  162. }
  163. static int
  164. procgen(Chan *c, char *name, Dirtab *tab, int, int s, Dir *dp)
  165. {
  166. Qid qid;
  167. Proc *p;
  168. char *ename;
  169. Segment *q;
  170. ulong pid, path, perm, len;
  171. if(s == DEVDOTDOT){
  172. mkqid(&qid, Qdir, 0, QTDIR);
  173. devdir(c, qid, "#p", 0, eve, 0555, dp);
  174. return 1;
  175. }
  176. if(c->qid.path == Qdir){
  177. if(s == 0){
  178. strcpy(up->genbuf, "trace");
  179. mkqid(&qid, Qtrace, -1, QTFILE);
  180. devdir(c, qid, up->genbuf, 0, eve, 0444, dp);
  181. return 1;
  182. }
  183. if(name != nil){
  184. /* ignore s and use name to find pid */
  185. pid = strtol(name, &ename, 10);
  186. if(pid==0 || ename[0]!='\0')
  187. return -1;
  188. s = procindex(pid);
  189. if(s < 0)
  190. return -1;
  191. }
  192. else if(--s >= conf.nproc)
  193. return -1;
  194. p = proctab(s);
  195. pid = p->pid;
  196. if(pid == 0)
  197. return 0;
  198. sprint(up->genbuf, "%lud", pid);
  199. /*
  200. * String comparison is done in devwalk so name must match its formatted pid
  201. */
  202. if(name != nil && strcmp(name, up->genbuf) != 0)
  203. return -1;
  204. mkqid(&qid, (s+1)<<QSHIFT, pid, QTDIR);
  205. devdir(c, qid, up->genbuf, 0, p->user, DMDIR|0555, dp);
  206. return 1;
  207. }
  208. if(c->qid.path == Qtrace){
  209. strcpy(up->genbuf, "trace");
  210. mkqid(&qid, Qtrace, -1, QTFILE);
  211. devdir(c, qid, up->genbuf, 0, eve, 0444, dp);
  212. return 1;
  213. }
  214. if(s >= nelem(procdir))
  215. return -1;
  216. if(tab)
  217. panic("procgen");
  218. tab = &procdir[s];
  219. path = c->qid.path&~(((1<<QSHIFT)-1)); /* slot component */
  220. p = proctab(SLOT(c->qid));
  221. perm = tab->perm;
  222. if(perm == 0)
  223. perm = p->procmode;
  224. else /* just copy read bits */
  225. perm |= p->procmode & 0444;
  226. len = tab->length;
  227. switch(QID(c->qid)) {
  228. case Qwait:
  229. len = p->nwait; /* incorrect size, but >0 means there's something to read */
  230. break;
  231. case Qprofile:
  232. q = p->seg[TSEG];
  233. if(q && q->profile) {
  234. len = (q->top-q->base)>>LRESPROF;
  235. len *= sizeof(*q->profile);
  236. }
  237. break;
  238. }
  239. mkqid(&qid, path|tab->qid.path, c->qid.vers, QTFILE);
  240. devdir(c, qid, tab->name, len, p->user, perm, dp);
  241. return 1;
  242. }
  243. static void
  244. _proctrace(Proc* p, Tevent etype, vlong ts)
  245. {
  246. Traceevent *te;
  247. if (p->trace == 0 || topens == 0)
  248. return;
  249. if(tproduced - tconsumed >= Nevents)
  250. tconsumed = tproduced - Nevents + 1;
  251. /* discard oldest, never mind the race */
  252. te = &tevents[tproduced&Emask];
  253. te->pid = p->pid;
  254. te->etype = etype;
  255. if (ts == 0)
  256. te->time = todget(nil);
  257. else
  258. te->time = ts;
  259. tproduced++;
  260. /* To avoid circular wakeup when used in combination with
  261. * EDF scheduling.
  262. */
  263. if (teventr.p && teventr.p->state == Wakeme)
  264. wakeup(&teventr);
  265. }
  266. static void
  267. procinit(void)
  268. {
  269. if(conf.nproc >= (1<<(16-QSHIFT))-1)
  270. print("warning: too many procs for devproc\n");
  271. addclock0link((void (*)(void))profclock, 113); /* Relative prime to HZ */
  272. }
  273. static Chan*
  274. procattach(char *spec)
  275. {
  276. return devattach('p', spec);
  277. }
  278. static Walkqid*
  279. procwalk(Chan *c, Chan *nc, char **name, int nname)
  280. {
  281. return devwalk(c, nc, name, nname, 0, 0, procgen);
  282. }
  283. static int
  284. procstat(Chan *c, uchar *db, int n)
  285. {
  286. return devstat(c, db, n, 0, 0, procgen);
  287. }
  288. /*
  289. * none can't read or write state on other
  290. * processes. This is to contain access of
  291. * servers running as none should they be
  292. * subverted by, for example, a stack attack.
  293. */
  294. static void
  295. nonone(Proc *p)
  296. {
  297. if(p == up)
  298. return;
  299. if(strcmp(up->user, "none") != 0)
  300. return;
  301. if(iseve())
  302. return;
  303. error(Eperm);
  304. }
  305. static Chan*
  306. procopen(Chan *c, int omode)
  307. {
  308. Proc *p;
  309. Pgrp *pg;
  310. Chan *tc;
  311. int pid;
  312. if(c->qid.type & QTDIR)
  313. return devopen(c, omode, 0, 0, procgen);
  314. if(QID(c->qid) == Qtrace){
  315. if (omode != OREAD)
  316. error(Eperm);
  317. lock(&tlock);
  318. if (waserror()){
  319. unlock(&tlock);
  320. nexterror();
  321. }
  322. if (topens > 0)
  323. error("already open");
  324. topens++;
  325. if (tevents == nil){
  326. tevents = (Traceevent*)malloc(sizeof(Traceevent) * Nevents);
  327. if(tevents == nil)
  328. error(Enomem);
  329. tproduced = tconsumed = 0;
  330. }
  331. proctrace = _proctrace;
  332. unlock(&tlock);
  333. poperror();
  334. c->mode = openmode(omode);
  335. c->flag |= COPEN;
  336. c->offset = 0;
  337. return c;
  338. }
  339. p = proctab(SLOT(c->qid));
  340. qlock(&p->debug);
  341. if(waserror()){
  342. qunlock(&p->debug);
  343. nexterror();
  344. }
  345. pid = PID(c->qid);
  346. if(p->pid != pid)
  347. error(Eprocdied);
  348. omode = openmode(omode);
  349. switch(QID(c->qid)){
  350. case Qtext:
  351. if(omode != OREAD)
  352. error(Eperm);
  353. tc = proctext(c, p);
  354. tc->offset = 0;
  355. qunlock(&p->debug);
  356. poperror();
  357. return tc;
  358. case Qproc:
  359. case Qkregs:
  360. case Qsegment:
  361. case Qprofile:
  362. case Qfd:
  363. if(omode != OREAD)
  364. error(Eperm);
  365. break;
  366. case Qmem:
  367. case Qnote:
  368. case Qctl:
  369. if(p->privatemem)
  370. error(Eperm);
  371. /* fall through */
  372. case Qargs:
  373. case Qnoteid:
  374. case Qstatus:
  375. case Qwait:
  376. case Qregs:
  377. case Qfpregs:
  378. nonone(p);
  379. break;
  380. case Qns:
  381. if(omode != OREAD)
  382. error(Eperm);
  383. c->aux = malloc(sizeof(Mntwalk));
  384. break;
  385. case Qnotepg:
  386. nonone(p);
  387. pg = p->pgrp;
  388. if(pg == nil)
  389. error(Eprocdied);
  390. if(omode!=OWRITE || pg->pgrpid == 1)
  391. error(Eperm);
  392. c->pgrpid.path = pg->pgrpid+1;
  393. c->pgrpid.vers = p->noteid;
  394. break;
  395. default:
  396. pprint("procopen %lux\n", c->qid);
  397. error(Egreg);
  398. }
  399. /* Affix pid to qid */
  400. if(p->state != Dead)
  401. c->qid.vers = p->pid;
  402. /* make sure the process slot didn't get reallocated while we were playing */
  403. coherence();
  404. if(p->pid != pid)
  405. error(Eprocdied);
  406. tc = devopen(c, omode, 0, 0, procgen);
  407. qunlock(&p->debug);
  408. poperror();
  409. return tc;
  410. }
  411. static int
  412. procwstat(Chan *c, uchar *db, int n)
  413. {
  414. Proc *p;
  415. Dir *d;
  416. if(c->qid.type&QTDIR)
  417. error(Eperm);
  418. if(QID(c->qid) == Qtrace)
  419. return devwstat(c, db, n);
  420. p = proctab(SLOT(c->qid));
  421. nonone(p);
  422. d = nil;
  423. if(waserror()){
  424. free(d);
  425. qunlock(&p->debug);
  426. nexterror();
  427. }
  428. qlock(&p->debug);
  429. if(p->pid != PID(c->qid))
  430. error(Eprocdied);
  431. if(strcmp(up->user, p->user) != 0 && strcmp(up->user, eve) != 0)
  432. error(Eperm);
  433. d = smalloc(sizeof(Dir)+n);
  434. n = convM2D(db, n, &d[0], (char*)&d[1]);
  435. if(n == 0)
  436. error(Eshortstat);
  437. if(!emptystr(d->uid) && strcmp(d->uid, p->user) != 0){
  438. if(strcmp(up->user, eve) != 0)
  439. error(Eperm);
  440. else
  441. kstrdup(&p->user, d->uid);
  442. }
  443. if(d->mode != ~0UL)
  444. p->procmode = d->mode&0777;
  445. poperror();
  446. free(d);
  447. qunlock(&p->debug);
  448. return n;
  449. }
  450. static long
  451. procoffset(long offset, char *va, int *np)
  452. {
  453. if(offset > 0) {
  454. offset -= *np;
  455. if(offset < 0) {
  456. memmove(va, va+*np+offset, -offset);
  457. *np = -offset;
  458. }
  459. else
  460. *np = 0;
  461. }
  462. return offset;
  463. }
  464. static int
  465. procqidwidth(Chan *c)
  466. {
  467. char buf[32];
  468. return sprint(buf, "%lud", c->qid.vers);
  469. }
  470. int
  471. procfdprint(Chan *c, int fd, int w, char *s, int ns)
  472. {
  473. int n;
  474. if(w == 0)
  475. w = procqidwidth(c);
  476. n = snprint(s, ns, "%3d %.2s %C %4ld (%.16llux %*lud %.2ux) %5ld %8lld %s\n",
  477. fd,
  478. &"r w rw"[(c->mode&3)<<1],
  479. devtab[c->type]->dc, c->dev,
  480. c->qid.path, w, c->qid.vers, c->qid.type,
  481. c->iounit, c->offset, c->name->s);
  482. return n;
  483. }
  484. static int
  485. procfds(Proc *p, char *va, int count, long offset)
  486. {
  487. Fgrp *f;
  488. Chan *c;
  489. char buf[256];
  490. int n, i, w, ww;
  491. char *a;
  492. /* print to buf to avoid holding fgrp lock while writing to user space */
  493. if(count > sizeof buf)
  494. count = sizeof buf;
  495. a = buf;
  496. qlock(&p->debug);
  497. f = p->fgrp;
  498. if(f == nil){
  499. qunlock(&p->debug);
  500. return 0;
  501. }
  502. lock(f);
  503. if(waserror()){
  504. unlock(f);
  505. qunlock(&p->debug);
  506. nexterror();
  507. }
  508. n = readstr(0, a, count, p->dot->name->s);
  509. n += snprint(a+n, count-n, "\n");
  510. offset = procoffset(offset, a, &n);
  511. /* compute width of qid.path */
  512. w = 0;
  513. for(i = 0; i <= f->maxfd; i++) {
  514. c = f->fd[i];
  515. if(c == nil)
  516. continue;
  517. ww = procqidwidth(c);
  518. if(ww > w)
  519. w = ww;
  520. }
  521. for(i = 0; i <= f->maxfd; i++) {
  522. c = f->fd[i];
  523. if(c == nil)
  524. continue;
  525. n += procfdprint(c, i, w, a+n, count-n);
  526. offset = procoffset(offset, a, &n);
  527. }
  528. unlock(f);
  529. qunlock(&p->debug);
  530. poperror();
  531. /* copy result to user space, now that locks are released */
  532. memmove(va, buf, n);
  533. return n;
  534. }
  535. static void
  536. procclose(Chan * c)
  537. {
  538. if(QID(c->qid) == Qtrace){
  539. lock(&tlock);
  540. if(topens > 0)
  541. topens--;
  542. if(topens == 0)
  543. proctrace = nil;
  544. unlock(&tlock);
  545. }
  546. if(QID(c->qid) == Qns && c->aux != 0)
  547. free(c->aux);
  548. }
  549. static void
  550. int2flag(int flag, char *s)
  551. {
  552. if(flag == 0){
  553. *s = '\0';
  554. return;
  555. }
  556. *s++ = '-';
  557. if(flag & MAFTER)
  558. *s++ = 'a';
  559. if(flag & MBEFORE)
  560. *s++ = 'b';
  561. if(flag & MCREATE)
  562. *s++ = 'c';
  563. if(flag & MCACHE)
  564. *s++ = 'C';
  565. *s = '\0';
  566. }
  567. static int
  568. procargs(Proc *p, char *buf, int nbuf)
  569. {
  570. int j, k, m;
  571. char *a;
  572. int n;
  573. a = p->args;
  574. if(p->setargs){
  575. snprint(buf, nbuf, "%s [%s]", p->text, p->args);
  576. return strlen(buf);
  577. }
  578. n = p->nargs;
  579. for(j = 0; j < nbuf - 1; j += m){
  580. if(n <= 0)
  581. break;
  582. if(j != 0)
  583. buf[j++] = ' ';
  584. m = snprint(buf+j, nbuf-j, "%q", a);
  585. k = strlen(a) + 1;
  586. a += k;
  587. n -= k;
  588. }
  589. return j;
  590. }
  591. static int
  592. eventsavailable(void *)
  593. {
  594. return tproduced > tconsumed;
  595. }
  596. static long
  597. procread(Chan *c, void *va, long n, vlong off)
  598. {
  599. int m, navail, ne;
  600. long l;
  601. Proc *p;
  602. Waitq *wq;
  603. Ureg kur;
  604. uchar *rptr;
  605. Mntwalk *mw;
  606. Segment *sg, *s;
  607. char *a = va, *sps;
  608. int i, j, rsize, pid;
  609. char statbuf[NSEG*32], *srv, flag[10];
  610. ulong offset = off;
  611. if(c->qid.type & QTDIR)
  612. return devdirread(c, a, n, 0, 0, procgen);
  613. if(QID(c->qid) == Qtrace){
  614. if(!eventsavailable(nil))
  615. return 0;
  616. rptr = (uchar*)va;
  617. navail = tproduced - tconsumed;
  618. if(navail > n / sizeof(Traceevent))
  619. navail = n / sizeof(Traceevent);
  620. while(navail > 0) {
  621. ne = ((tconsumed & Emask) + navail > Nevents)?
  622. Nevents - (tconsumed & Emask): navail;
  623. memmove(rptr, &tevents[tconsumed & Emask],
  624. ne * sizeof(Traceevent));
  625. tconsumed += ne;
  626. rptr += ne * sizeof(Traceevent);
  627. navail -= ne;
  628. }
  629. return rptr - (uchar*)va;
  630. }
  631. p = proctab(SLOT(c->qid));
  632. if(p->pid != PID(c->qid))
  633. error(Eprocdied);
  634. switch(QID(c->qid)){
  635. case Qargs:
  636. qlock(&p->debug);
  637. j = procargs(p, p->genbuf, sizeof p->genbuf);
  638. qunlock(&p->debug);
  639. if(offset >= j)
  640. return 0;
  641. if(offset+n > j)
  642. n = j-offset;
  643. memmove(a, &p->genbuf[offset], n);
  644. return n;
  645. case Qmem:
  646. if(offset < KZERO
  647. || (offset >= USTKTOP-USTKSIZE && offset < USTKTOP))
  648. return procctlmemio(p, offset, n, va, 1);
  649. if(!iseve())
  650. error(Eperm);
  651. /* validate kernel addresses */
  652. if(offset < (ulong)end) {
  653. if(offset+n > (ulong)end)
  654. n = (ulong)end - offset;
  655. memmove(a, (char*)offset, n);
  656. return n;
  657. }
  658. /* conf.base* and conf.npage* are set by xinit to refer to kernel allocation, not user pages */
  659. if(offset >= conf.base0 && offset < conf.npage0){
  660. if(offset+n > conf.npage0)
  661. n = conf.npage0 - offset;
  662. memmove(a, (char*)offset, n);
  663. return n;
  664. }
  665. if(offset >= conf.base1 && offset < conf.npage1){
  666. if(offset+n > conf.npage1)
  667. n = conf.npage1 - offset;
  668. memmove(a, (char*)offset, n);
  669. return n;
  670. }
  671. error(Ebadarg);
  672. case Qprofile:
  673. s = p->seg[TSEG];
  674. if(s == 0 || s->profile == 0)
  675. error("profile is off");
  676. i = (s->top-s->base)>>LRESPROF;
  677. i *= sizeof(*s->profile);
  678. if(offset >= i)
  679. return 0;
  680. if(offset+n > i)
  681. n = i - offset;
  682. memmove(a, ((char*)s->profile)+offset, n);
  683. return n;
  684. case Qnote:
  685. qlock(&p->debug);
  686. if(waserror()){
  687. qunlock(&p->debug);
  688. nexterror();
  689. }
  690. if(p->pid != PID(c->qid))
  691. error(Eprocdied);
  692. if(n < 1) /* must accept at least the '\0' */
  693. error(Etoosmall);
  694. if(p->nnote == 0)
  695. n = 0;
  696. else {
  697. m = strlen(p->note[0].msg) + 1;
  698. if(m > n)
  699. m = n;
  700. memmove(va, p->note[0].msg, m);
  701. ((char*)va)[m-1] = '\0';
  702. p->nnote--;
  703. memmove(p->note, p->note+1, p->nnote*sizeof(Note));
  704. n = m;
  705. }
  706. if(p->nnote == 0)
  707. p->notepending = 0;
  708. poperror();
  709. qunlock(&p->debug);
  710. return n;
  711. case Qproc:
  712. if(offset >= sizeof(Proc))
  713. return 0;
  714. if(offset+n > sizeof(Proc))
  715. n = sizeof(Proc) - offset;
  716. memmove(a, ((char*)p)+offset, n);
  717. return n;
  718. case Qregs:
  719. rptr = (uchar*)p->dbgreg;
  720. rsize = sizeof(Ureg);
  721. goto regread;
  722. case Qkregs:
  723. memset(&kur, 0, sizeof(Ureg));
  724. setkernur(&kur, p);
  725. rptr = (uchar*)&kur;
  726. rsize = sizeof(Ureg);
  727. goto regread;
  728. case Qfpregs:
  729. rptr = (uchar*)&p->fpsave;
  730. rsize = sizeof(FPsave);
  731. regread:
  732. if(rptr == 0)
  733. error(Enoreg);
  734. if(offset >= rsize)
  735. return 0;
  736. if(offset+n > rsize)
  737. n = rsize - offset;
  738. memmove(a, rptr+offset, n);
  739. return n;
  740. case Qstatus:
  741. if(offset >= STATSIZE)
  742. return 0;
  743. if(offset+n > STATSIZE)
  744. n = STATSIZE - offset;
  745. sps = p->psstate;
  746. if(sps == 0)
  747. sps = statename[p->state];
  748. memset(statbuf, ' ', sizeof statbuf);
  749. memmove(statbuf+0*KNAMELEN, p->text, strlen(p->text));
  750. memmove(statbuf+1*KNAMELEN, p->user, strlen(p->user));
  751. memmove(statbuf+2*KNAMELEN, sps, strlen(sps));
  752. j = 2*KNAMELEN + 12;
  753. for(i = 0; i < 6; i++) {
  754. l = p->time[i];
  755. if(i == TReal)
  756. l = MACHP(0)->ticks - l;
  757. l = TK2MS(l);
  758. readnum(0, statbuf+j+NUMSIZE*i, NUMSIZE, l, NUMSIZE);
  759. }
  760. /* ignore stack, which is mostly non-existent */
  761. l = 0;
  762. for(i=1; i<NSEG; i++){
  763. s = p->seg[i];
  764. if(s)
  765. l += s->top - s->base;
  766. }
  767. readnum(0, statbuf+j+NUMSIZE*6, NUMSIZE, l>>10, NUMSIZE);
  768. readnum(0, statbuf+j+NUMSIZE*7, NUMSIZE, p->basepri, NUMSIZE);
  769. readnum(0, statbuf+j+NUMSIZE*8, NUMSIZE, p->priority, NUMSIZE);
  770. memmove(a, statbuf+offset, n);
  771. return n;
  772. case Qsegment:
  773. j = 0;
  774. for(i = 0; i < NSEG; i++) {
  775. sg = p->seg[i];
  776. if(sg == 0)
  777. continue;
  778. j += sprint(statbuf+j, "%-6s %c%c %.8lux %.8lux %4ld\n",
  779. sname[sg->type&SG_TYPE],
  780. sg->type&SG_RONLY ? 'R' : ' ',
  781. sg->profile ? 'P' : ' ',
  782. sg->base, sg->top, sg->ref);
  783. }
  784. if(offset >= j)
  785. return 0;
  786. if(offset+n > j)
  787. n = j-offset;
  788. if(n == 0 && offset == 0)
  789. exhausted("segments");
  790. memmove(a, &statbuf[offset], n);
  791. return n;
  792. case Qwait:
  793. if(!canqlock(&p->qwaitr))
  794. error(Einuse);
  795. if(waserror()) {
  796. qunlock(&p->qwaitr);
  797. nexterror();
  798. }
  799. lock(&p->exl);
  800. if(up == p && p->nchild == 0 && p->waitq == 0) {
  801. unlock(&p->exl);
  802. error(Enochild);
  803. }
  804. pid = p->pid;
  805. while(p->waitq == 0) {
  806. unlock(&p->exl);
  807. sleep(&p->waitr, haswaitq, p);
  808. if(p->pid != pid)
  809. error(Eprocdied);
  810. lock(&p->exl);
  811. }
  812. wq = p->waitq;
  813. p->waitq = wq->next;
  814. p->nwait--;
  815. unlock(&p->exl);
  816. qunlock(&p->qwaitr);
  817. poperror();
  818. n = snprint(a, n, "%d %lud %lud %lud %q",
  819. wq->w.pid,
  820. wq->w.time[TUser], wq->w.time[TSys], wq->w.time[TReal],
  821. wq->w.msg);
  822. free(wq);
  823. return n;
  824. case Qns:
  825. qlock(&p->debug);
  826. if(waserror()){
  827. qunlock(&p->debug);
  828. nexterror();
  829. }
  830. if(p->pgrp == nil || p->pid != PID(c->qid))
  831. error(Eprocdied);
  832. mw = c->aux;
  833. if(mw->cddone){
  834. qunlock(&p->debug);
  835. poperror();
  836. return 0;
  837. }
  838. mntscan(mw, p);
  839. if(mw->mh == 0){
  840. mw->cddone = 1;
  841. i = snprint(a, n, "cd %s\n", p->dot->name->s);
  842. qunlock(&p->debug);
  843. poperror();
  844. return i;
  845. }
  846. int2flag(mw->cm->mflag, flag);
  847. if(strcmp(mw->cm->to->name->s, "#M") == 0){
  848. srv = srvname(mw->cm->to->mchan);
  849. i = snprint(a, n, "mount %s %s %s %s\n", flag,
  850. srv==nil? mw->cm->to->mchan->name->s : srv,
  851. mw->mh->from->name->s, mw->cm->spec? mw->cm->spec : "");
  852. free(srv);
  853. }else
  854. i = snprint(a, n, "bind %s %s %s\n", flag,
  855. mw->cm->to->name->s, mw->mh->from->name->s);
  856. qunlock(&p->debug);
  857. poperror();
  858. return i;
  859. case Qnoteid:
  860. return readnum(offset, va, n, p->noteid, NUMSIZE);
  861. case Qfd:
  862. return procfds(p, va, n, offset);
  863. }
  864. error(Egreg);
  865. return 0; /* not reached */
  866. }
  867. void
  868. mntscan(Mntwalk *mw, Proc *p)
  869. {
  870. Pgrp *pg;
  871. Mount *t;
  872. Mhead *f;
  873. int nxt, i;
  874. ulong last, bestmid;
  875. pg = p->pgrp;
  876. rlock(&pg->ns);
  877. nxt = 0;
  878. bestmid = ~0;
  879. last = 0;
  880. if(mw->mh)
  881. last = mw->cm->mountid;
  882. for(i = 0; i < MNTHASH; i++) {
  883. for(f = pg->mnthash[i]; f; f = f->hash) {
  884. for(t = f->mount; t; t = t->next) {
  885. if(mw->mh == 0 ||
  886. (t->mountid > last && t->mountid < bestmid)) {
  887. mw->cm = t;
  888. mw->mh = f;
  889. bestmid = mw->cm->mountid;
  890. nxt = 1;
  891. }
  892. }
  893. }
  894. }
  895. if(nxt == 0)
  896. mw->mh = 0;
  897. runlock(&pg->ns);
  898. }
  899. static long
  900. procwrite(Chan *c, void *va, long n, vlong off)
  901. {
  902. int id, m;
  903. Proc *p, *t, *et;
  904. char *a, *arg, buf[ERRMAX];
  905. ulong offset = off;
  906. a = va;
  907. if(c->qid.type & QTDIR)
  908. error(Eisdir);
  909. p = proctab(SLOT(c->qid));
  910. /* Use the remembered noteid in the channel rather
  911. * than the process pgrpid
  912. */
  913. if(QID(c->qid) == Qnotepg) {
  914. pgrpnote(NOTEID(c->pgrpid), va, n, NUser);
  915. return n;
  916. }
  917. qlock(&p->debug);
  918. if(waserror()){
  919. qunlock(&p->debug);
  920. nexterror();
  921. }
  922. if(p->pid != PID(c->qid))
  923. error(Eprocdied);
  924. switch(QID(c->qid)){
  925. case Qargs:
  926. if(n == 0)
  927. error(Eshort);
  928. if(n >= ERRMAX)
  929. error(Etoobig);
  930. arg = malloc(n+1);
  931. if(arg == nil)
  932. error(Enomem);
  933. memmove(arg, va, n);
  934. m = n;
  935. if(arg[m-1] != 0)
  936. arg[m++] = 0;
  937. free(p->args);
  938. p->nargs = m;
  939. p->args = arg;
  940. p->setargs = 1;
  941. break;
  942. case Qmem:
  943. if(p->state != Stopped)
  944. error(Ebadctl);
  945. n = procctlmemio(p, offset, n, va, 0);
  946. break;
  947. case Qregs:
  948. if(offset >= sizeof(Ureg))
  949. return 0;
  950. if(offset+n > sizeof(Ureg))
  951. n = sizeof(Ureg) - offset;
  952. if(p->dbgreg == 0)
  953. error(Enoreg);
  954. setregisters(p->dbgreg, (char*)(p->dbgreg)+offset, va, n);
  955. break;
  956. case Qfpregs:
  957. if(offset >= sizeof(FPsave))
  958. return 0;
  959. if(offset+n > sizeof(FPsave))
  960. n = sizeof(FPsave) - offset;
  961. memmove((uchar*)&p->fpsave+offset, va, n);
  962. break;
  963. case Qctl:
  964. procctlreq(p, va, n);
  965. break;
  966. case Qnote:
  967. if(p->kp)
  968. error(Eperm);
  969. if(n >= ERRMAX-1)
  970. error(Etoobig);
  971. memmove(buf, va, n);
  972. buf[n] = 0;
  973. if(!postnote(p, 0, buf, NUser))
  974. error("note not posted");
  975. break;
  976. case Qnoteid:
  977. id = atoi(a);
  978. if(id == p->pid) {
  979. p->noteid = id;
  980. break;
  981. }
  982. t = proctab(0);
  983. for(et = t+conf.nproc; t < et; t++) {
  984. if(id == t->noteid) {
  985. if(strcmp(p->user, t->user) != 0)
  986. error(Eperm);
  987. p->noteid = id;
  988. break;
  989. }
  990. }
  991. if(p->noteid != id)
  992. error(Ebadarg);
  993. break;
  994. default:
  995. pprint("unknown qid in procwrite\n");
  996. error(Egreg);
  997. }
  998. poperror();
  999. qunlock(&p->debug);
  1000. return n;
  1001. }
  1002. Dev procdevtab = {
  1003. 'p',
  1004. "proc",
  1005. devreset,
  1006. procinit,
  1007. devshutdown,
  1008. procattach,
  1009. procwalk,
  1010. procstat,
  1011. procopen,
  1012. devcreate,
  1013. procclose,
  1014. procread,
  1015. devbread,
  1016. procwrite,
  1017. devbwrite,
  1018. devremove,
  1019. procwstat,
  1020. };
  1021. Chan*
  1022. proctext(Chan *c, Proc *p)
  1023. {
  1024. Chan *tc;
  1025. Image *i;
  1026. Segment *s;
  1027. s = p->seg[TSEG];
  1028. if(s == 0)
  1029. error(Enonexist);
  1030. if(p->state==Dead)
  1031. error(Eprocdied);
  1032. lock(s);
  1033. i = s->image;
  1034. if(i == 0) {
  1035. unlock(s);
  1036. error(Eprocdied);
  1037. }
  1038. unlock(s);
  1039. lock(i);
  1040. if(waserror()) {
  1041. unlock(i);
  1042. nexterror();
  1043. }
  1044. tc = i->c;
  1045. if(tc == 0)
  1046. error(Eprocdied);
  1047. if(incref(tc) == 1 || (tc->flag&COPEN) == 0 || tc->mode!=OREAD) {
  1048. cclose(tc);
  1049. error(Eprocdied);
  1050. }
  1051. if(p->pid != PID(c->qid))
  1052. error(Eprocdied);
  1053. unlock(i);
  1054. poperror();
  1055. return tc;
  1056. }
  1057. void
  1058. procstopwait(Proc *p, int ctl)
  1059. {
  1060. int pid;
  1061. if(p->pdbg)
  1062. error(Einuse);
  1063. if(procstopped(p) || p->state == Broken)
  1064. return;
  1065. if(ctl != 0)
  1066. p->procctl = ctl;
  1067. p->pdbg = up;
  1068. pid = p->pid;
  1069. qunlock(&p->debug);
  1070. up->psstate = "Stopwait";
  1071. if(waserror()) {
  1072. p->pdbg = 0;
  1073. qlock(&p->debug);
  1074. nexterror();
  1075. }
  1076. sleep(&up->sleep, procstopped, p);
  1077. poperror();
  1078. qlock(&p->debug);
  1079. if(p->pid != pid)
  1080. error(Eprocdied);
  1081. }
  1082. static void
  1083. procctlcloseone(Proc *p, Fgrp *f, int fd)
  1084. {
  1085. Chan *c;
  1086. c = f->fd[fd];
  1087. if(c == nil)
  1088. return;
  1089. f->fd[fd] = nil;
  1090. unlock(f);
  1091. qunlock(&p->debug);
  1092. cclose(c);
  1093. qlock(&p->debug);
  1094. lock(f);
  1095. }
  1096. void
  1097. procctlclosefiles(Proc *p, int all, int fd)
  1098. {
  1099. int i;
  1100. Fgrp *f;
  1101. f = p->fgrp;
  1102. if(f == nil)
  1103. error(Eprocdied);
  1104. lock(f);
  1105. f->ref++;
  1106. if(all)
  1107. for(i = 0; i < f->maxfd; i++)
  1108. procctlcloseone(p, f, i);
  1109. else
  1110. procctlcloseone(p, f, fd);
  1111. unlock(f);
  1112. closefgrp(f);
  1113. }
  1114. static char *
  1115. parsetime(vlong *rt, char *s)
  1116. {
  1117. uvlong ticks;
  1118. ulong l;
  1119. char *e, *p;
  1120. static int p10[] = {100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1};
  1121. if (s == nil)
  1122. return("missing value");
  1123. ticks=strtoul(s, &e, 10);
  1124. if (*e == '.'){
  1125. p = e+1;
  1126. l = strtoul(p, &e, 10);
  1127. if(e-p > nelem(p10))
  1128. return "too many digits after decimal point";
  1129. if(e-p == 0)
  1130. return "ill-formed number";
  1131. l *= p10[e-p-1];
  1132. }else
  1133. l = 0;
  1134. if (*e == '\0' || strcmp(e, "s") == 0){
  1135. ticks = 1000000000 * ticks + l;
  1136. }else if (strcmp(e, "ms") == 0){
  1137. ticks = 1000000 * ticks + l/1000;
  1138. }else if (strcmp(e, "µs") == 0 || strcmp(e, "us") == 0){
  1139. ticks = 1000 * ticks + l/1000000;
  1140. }else if (strcmp(e, "ns") != 0)
  1141. return "unrecognized unit";
  1142. *rt = ticks;
  1143. return nil;
  1144. }
  1145. void
  1146. procctlreq(Proc *p, char *va, int n)
  1147. {
  1148. Segment *s;
  1149. int npc, pri;
  1150. Cmdbuf *cb;
  1151. Cmdtab *ct;
  1152. vlong time;
  1153. char *e;
  1154. if(p->kp) /* no ctl requests to kprocs */
  1155. error(Eperm);
  1156. cb = parsecmd(va, n);
  1157. if(waserror()){
  1158. free(cb);
  1159. nexterror();
  1160. }
  1161. ct = lookupcmd(cb, proccmd, nelem(proccmd));
  1162. switch(ct->index){
  1163. case CMclose:
  1164. procctlclosefiles(p, 0, atoi(cb->f[1]));
  1165. break;
  1166. case CMclosefiles:
  1167. procctlclosefiles(p, 1, 0);
  1168. break;
  1169. case CMhang:
  1170. p->hang = 1;
  1171. break;
  1172. case CMkill:
  1173. switch(p->state) {
  1174. case Broken:
  1175. unbreak(p);
  1176. break;
  1177. case Stopped:
  1178. postnote(p, 0, "sys: killed", NExit);
  1179. p->procctl = Proc_exitme;
  1180. ready(p);
  1181. break;
  1182. default:
  1183. postnote(p, 0, "sys: killed", NExit);
  1184. p->procctl = Proc_exitme;
  1185. }
  1186. break;
  1187. case CMnohang:
  1188. p->hang = 0;
  1189. break;
  1190. case CMnoswap:
  1191. p->noswap = 1;
  1192. break;
  1193. case CMpri:
  1194. pri = atoi(cb->f[1]);
  1195. if(pri > PriNormal && !iseve())
  1196. error(Eperm);
  1197. procpriority(p, pri, 0);
  1198. break;
  1199. case CMfixedpri:
  1200. pri = atoi(cb->f[1]);
  1201. if(pri > PriNormal && !iseve())
  1202. error(Eperm);
  1203. procpriority(p, pri, 1);
  1204. break;
  1205. case CMprivate:
  1206. p->privatemem = 1;
  1207. break;
  1208. case CMprofile:
  1209. s = p->seg[TSEG];
  1210. if(s == 0 || (s->type&SG_TYPE) != SG_TEXT)
  1211. error(Ebadctl);
  1212. if(s->profile != 0)
  1213. free(s->profile);
  1214. npc = (s->top-s->base)>>LRESPROF;
  1215. s->profile = malloc(npc*sizeof(*s->profile));
  1216. if(s->profile == 0)
  1217. error(Enomem);
  1218. break;
  1219. case CMstart:
  1220. if(p->state != Stopped)
  1221. error(Ebadctl);
  1222. ready(p);
  1223. break;
  1224. case CMstartstop:
  1225. if(p->state != Stopped)
  1226. error(Ebadctl);
  1227. p->procctl = Proc_traceme;
  1228. ready(p);
  1229. procstopwait(p, Proc_traceme);
  1230. break;
  1231. case CMstartsyscall:
  1232. if(p->state != Stopped)
  1233. error(Ebadctl);
  1234. p->procctl = Proc_tracesyscall;
  1235. ready(p);
  1236. procstopwait(p, Proc_tracesyscall);
  1237. break;
  1238. case CMstop:
  1239. procstopwait(p, Proc_stopme);
  1240. break;
  1241. case CMwaitstop:
  1242. procstopwait(p, 0);
  1243. break;
  1244. case CMwired:
  1245. procwired(p, atoi(cb->f[1]));
  1246. break;
  1247. case CMtrace:
  1248. p->trace ^= 1;
  1249. break;
  1250. /* real time */
  1251. case CMperiod:
  1252. if(p->edf == nil)
  1253. edfinit(p);
  1254. if(e=parsetime(&time, cb->f[1]))
  1255. error(e);
  1256. edfstop(p);
  1257. p->edf->T = time;
  1258. break;
  1259. case CMdeadline:
  1260. if(p->edf == nil)
  1261. edfinit(p);
  1262. if(e=parsetime(&time, cb->f[1]))
  1263. error(e);
  1264. edfstop(p);
  1265. p->edf->D = time;
  1266. break;
  1267. case CMcost:
  1268. if(p->edf == nil)
  1269. edfinit(p);
  1270. if(e=parsetime(&time, cb->f[1]))
  1271. error(e);
  1272. edfstop(p);
  1273. p->edf->C = time;
  1274. break;
  1275. case CMsporadic:
  1276. if(p->edf == nil)
  1277. edfinit(p);
  1278. p->edf->flags |= Sporadic;
  1279. break;
  1280. case CMdeadlinenotes:
  1281. if(p->edf == nil)
  1282. edfinit(p);
  1283. p->edf->flags |= Sendnotes;
  1284. break;
  1285. case CMadmit:
  1286. if(p->edf == 0)
  1287. error("edf params");
  1288. if(e = edfadmit(p))
  1289. error(e);
  1290. break;
  1291. case CMexpel:
  1292. if(p->edf)
  1293. edfstop(p);
  1294. break;
  1295. }
  1296. poperror();
  1297. free(cb);
  1298. }
  1299. int
  1300. procstopped(void *a)
  1301. {
  1302. Proc *p = a;
  1303. return p->state == Stopped;
  1304. }
  1305. int
  1306. procctlmemio(Proc *p, ulong offset, int n, void *va, int read)
  1307. {
  1308. KMap *k;
  1309. Pte *pte;
  1310. Page *pg;
  1311. Segment *s;
  1312. ulong soff, l;
  1313. char *a = va, *b;
  1314. for(;;) {
  1315. s = seg(p, offset, 1);
  1316. if(s == 0)
  1317. error(Ebadarg);
  1318. if(offset+n >= s->top)
  1319. n = s->top-offset;
  1320. if(!read && (s->type&SG_TYPE) == SG_TEXT)
  1321. s = txt2data(p, s);
  1322. s->steal++;
  1323. soff = offset-s->base;
  1324. if(waserror()) {
  1325. s->steal--;
  1326. nexterror();
  1327. }
  1328. if(fixfault(s, offset, read, 0) == 0)
  1329. break;
  1330. poperror();
  1331. s->steal--;
  1332. }
  1333. poperror();
  1334. pte = s->map[soff/PTEMAPMEM];
  1335. if(pte == 0)
  1336. panic("procctlmemio");
  1337. pg = pte->pages[(soff&(PTEMAPMEM-1))/BY2PG];
  1338. if(pagedout(pg))
  1339. panic("procctlmemio1");
  1340. l = BY2PG - (offset&(BY2PG-1));
  1341. if(n > l)
  1342. n = l;
  1343. k = kmap(pg);
  1344. if(waserror()) {
  1345. s->steal--;
  1346. kunmap(k);
  1347. nexterror();
  1348. }
  1349. b = (char*)VA(k);
  1350. b += offset&(BY2PG-1);
  1351. if(read == 1)
  1352. memmove(a, b, n); /* This can fault */
  1353. else
  1354. memmove(b, a, n);
  1355. kunmap(k);
  1356. poperror();
  1357. /* Ensure the process sees text page changes */
  1358. if(s->flushme)
  1359. memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
  1360. s->steal--;
  1361. if(read == 0)
  1362. p->newtlb = 1;
  1363. return n;
  1364. }
  1365. Segment*
  1366. txt2data(Proc *p, Segment *s)
  1367. {
  1368. int i;
  1369. Segment *ps;
  1370. ps = newseg(SG_DATA, s->base, s->size);
  1371. ps->image = s->image;
  1372. incref(ps->image);
  1373. ps->fstart = s->fstart;
  1374. ps->flen = s->flen;
  1375. ps->flushme = 1;
  1376. qlock(&p->seglock);
  1377. for(i = 0; i < NSEG; i++)
  1378. if(p->seg[i] == s)
  1379. break;
  1380. if(p->seg[i] != s)
  1381. panic("segment gone");
  1382. qunlock(&s->lk);
  1383. putseg(s);
  1384. qlock(&ps->lk);
  1385. p->seg[i] = ps;
  1386. qunlock(&p->seglock);
  1387. return ps;
  1388. }
  1389. Segment*
  1390. data2txt(Segment *s)
  1391. {
  1392. Segment *ps;
  1393. ps = newseg(SG_TEXT, s->base, s->size);
  1394. ps->image = s->image;
  1395. incref(ps->image);
  1396. ps->fstart = s->fstart;
  1397. ps->flen = s->flen;
  1398. ps->flushme = 1;
  1399. return ps;
  1400. }