devproc.c 28 KB

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