devcons.c 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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 <authsrv.h>
  8. void (*consdebug)(void) = nil;
  9. void (*screenputs)(char*, int) = nil;
  10. Queue* kbdq; /* unprocessed console input */
  11. Queue* lineq; /* processed console input */
  12. Queue* serialoq; /* serial console output */
  13. Queue* kprintoq; /* console output, for /dev/kprint */
  14. ulong kprintinuse; /* test and set whether /dev/kprint is open */
  15. int iprintscreenputs = 1;
  16. int panicking;
  17. static struct
  18. {
  19. QLock;
  20. int raw; /* true if we shouldn't process input */
  21. int ctl; /* number of opens to the control file */
  22. int x; /* index into line */
  23. char line[1024]; /* current input line */
  24. int count;
  25. int ctlpoff;
  26. /* a place to save up characters at interrupt time before dumping them in the queue */
  27. Lock lockputc;
  28. char istage[1024];
  29. char *iw;
  30. char *ir;
  31. char *ie;
  32. } kbd = {
  33. .iw = kbd.istage,
  34. .ir = kbd.istage,
  35. .ie = kbd.istage + sizeof(kbd.istage),
  36. };
  37. char *sysname;
  38. vlong fasthz;
  39. static void seedrand(void);
  40. static int readtime(ulong, char*, int);
  41. static int readbintime(char*, int);
  42. static int writetime(char*, int);
  43. static int writebintime(char*, int);
  44. enum
  45. {
  46. CMreboot,
  47. CMpanic,
  48. };
  49. Cmdtab rebootmsg[] =
  50. {
  51. CMreboot, "reboot", 0,
  52. CMpanic, "panic", 0,
  53. };
  54. void
  55. printinit(void)
  56. {
  57. lineq = qopen(2*1024, 0, nil, nil);
  58. if(lineq == nil)
  59. panic("printinit");
  60. qnoblock(lineq, 1);
  61. }
  62. int
  63. consactive(void)
  64. {
  65. if(serialoq)
  66. return qlen(serialoq) > 0;
  67. return 0;
  68. }
  69. void
  70. prflush(void)
  71. {
  72. ulong now;
  73. now = m->ticks;
  74. while(consactive())
  75. if(m->ticks - now >= HZ)
  76. break;
  77. }
  78. /*
  79. * Print a string on the console. Convert \n to \r\n for serial
  80. * line consoles. Locking of the queues is left up to the screen
  81. * or uart code. Multi-line messages to serial consoles may get
  82. * interspersed with other messages.
  83. */
  84. static void
  85. putstrn0(char *str, int n, int usewrite)
  86. {
  87. int m;
  88. char *t;
  89. /*
  90. * if someone is reading /dev/kprint,
  91. * put the message there.
  92. * if not and there's an attached bit mapped display,
  93. * put the message there.
  94. *
  95. * if there's a serial line being used as a console,
  96. * put the message there.
  97. */
  98. if(kprintoq != nil && !qisclosed(kprintoq)){
  99. if(usewrite)
  100. qwrite(kprintoq, str, n);
  101. else
  102. qiwrite(kprintoq, str, n);
  103. }else if(screenputs != nil)
  104. screenputs(str, n);
  105. if(serialoq == nil){
  106. uartputs(str, n);
  107. return;
  108. }
  109. while(n > 0) {
  110. t = memchr(str, '\n', n);
  111. if(t && !kbd.raw) {
  112. m = t-str;
  113. if(usewrite){
  114. qwrite(serialoq, str, m);
  115. qwrite(serialoq, "\r\n", 2);
  116. } else {
  117. qiwrite(serialoq, str, m);
  118. qiwrite(serialoq, "\r\n", 2);
  119. }
  120. n -= m+1;
  121. str = t+1;
  122. } else {
  123. if(usewrite)
  124. qwrite(serialoq, str, n);
  125. else
  126. qiwrite(serialoq, str, n);
  127. break;
  128. }
  129. }
  130. }
  131. void
  132. putstrn(char *str, int n)
  133. {
  134. putstrn0(str, n, 0);
  135. }
  136. int noprint;
  137. int
  138. print(char *fmt, ...)
  139. {
  140. int n;
  141. va_list arg;
  142. char buf[PRINTSIZE];
  143. if(noprint)
  144. return -1;
  145. va_start(arg, fmt);
  146. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  147. va_end(arg);
  148. putstrn(buf, n);
  149. return n;
  150. }
  151. int
  152. iprint(char *fmt, ...)
  153. {
  154. int n, s;
  155. va_list arg;
  156. char buf[PRINTSIZE];
  157. s = splhi();
  158. va_start(arg, fmt);
  159. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  160. va_end(arg);
  161. if(screenputs != nil && iprintscreenputs)
  162. screenputs(buf, n);
  163. uartputs(buf, n);
  164. splx(s);
  165. return n;
  166. }
  167. void
  168. panic(char *fmt, ...)
  169. {
  170. int n;
  171. va_list arg;
  172. char buf[PRINTSIZE];
  173. kprintoq = nil; /* don't try to write to /dev/kprint */
  174. if(panicking)
  175. for(;;);
  176. panicking = 1;
  177. splhi();
  178. strcpy(buf, "panic: ");
  179. va_start(arg, fmt);
  180. n = vseprint(buf+strlen(buf), buf+sizeof(buf), fmt, arg) - buf;
  181. va_end(arg);
  182. buf[n] = '\n';
  183. uartputs(buf, n+1);
  184. if(consdebug)
  185. (*consdebug)();
  186. spllo();
  187. prflush();
  188. putstrn(buf, n+1);
  189. dumpstack();
  190. exit(1);
  191. }
  192. void
  193. _assert(char *fmt)
  194. {
  195. panic("assert failed: %s", fmt);
  196. }
  197. int
  198. pprint(char *fmt, ...)
  199. {
  200. int n;
  201. Chan *c;
  202. va_list arg;
  203. char buf[2*PRINTSIZE];
  204. if(up == nil || up->fgrp == nil)
  205. return 0;
  206. c = up->fgrp->fd[2];
  207. if(c==0 || (c->mode!=OWRITE && c->mode!=ORDWR))
  208. return 0;
  209. n = sprint(buf, "%s %lud: ", up->text, up->pid);
  210. va_start(arg, fmt);
  211. n = vseprint(buf+n, buf+sizeof(buf), fmt, arg) - buf;
  212. va_end(arg);
  213. if(waserror())
  214. return 0;
  215. devtab[c->type]->write(c, buf, n, c->offset);
  216. poperror();
  217. lock(c);
  218. c->offset += n;
  219. unlock(c);
  220. return n;
  221. }
  222. static void
  223. echoscreen(char *buf, int n)
  224. {
  225. char *e, *p;
  226. char ebuf[128];
  227. int x;
  228. p = ebuf;
  229. e = ebuf + sizeof(ebuf) - 4;
  230. while(n-- > 0){
  231. if(p >= e){
  232. screenputs(ebuf, p - ebuf);
  233. p = ebuf;
  234. }
  235. x = *buf++;
  236. if(x == 0x15){
  237. *p++ = '^';
  238. *p++ = 'U';
  239. *p++ = '\n';
  240. } else
  241. *p++ = x;
  242. }
  243. if(p != ebuf)
  244. screenputs(ebuf, p - ebuf);
  245. }
  246. static void
  247. echoserialoq(char *buf, int n)
  248. {
  249. char *e, *p;
  250. char ebuf[128];
  251. int x;
  252. p = ebuf;
  253. e = ebuf + sizeof(ebuf) - 4;
  254. while(n-- > 0){
  255. if(p >= e){
  256. qiwrite(serialoq, ebuf, p - ebuf);
  257. p = ebuf;
  258. }
  259. x = *buf++;
  260. if(x == '\n'){
  261. *p++ = '\r';
  262. *p++ = '\n';
  263. } else if(x == 0x15){
  264. *p++ = '^';
  265. *p++ = 'U';
  266. *p++ = '\n';
  267. } else
  268. *p++ = x;
  269. }
  270. if(p != ebuf)
  271. qiwrite(serialoq, ebuf, p - ebuf);
  272. }
  273. void
  274. echo(char *buf, int n)
  275. {
  276. static int ctrlt, pid;
  277. extern ulong etext;
  278. int x;
  279. char *e, *p;
  280. e = buf+n;
  281. for(p = buf; p < e; p++){
  282. switch(*p){
  283. case 0x10: /* ^P */
  284. if(cpuserver && !kbd.ctlpoff){
  285. active.exiting = 1;
  286. return;
  287. }
  288. break;
  289. case 0x14: /* ^T */
  290. ctrlt++;
  291. if(ctrlt > 2)
  292. ctrlt = 2;
  293. continue;
  294. }
  295. if(ctrlt != 2)
  296. continue;
  297. /* ^T escapes */
  298. ctrlt = 0;
  299. switch(*p){
  300. case 'S':
  301. x = splhi();
  302. dumpstack();
  303. procdump();
  304. splx(x);
  305. return;
  306. case 's':
  307. dumpstack();
  308. return;
  309. case 'x':
  310. xsummary();
  311. ixsummary();
  312. mallocsummary();
  313. pagersummary();
  314. return;
  315. case 'd':
  316. if(consdebug == nil)
  317. consdebug = rdb;
  318. else
  319. consdebug = nil;
  320. print("consdebug now 0x%p\n", consdebug);
  321. return;
  322. case 'D':
  323. if(consdebug == nil)
  324. consdebug = rdb;
  325. consdebug();
  326. return;
  327. case 'p':
  328. x = spllo();
  329. procdump();
  330. splx(x);
  331. return;
  332. case 'q':
  333. scheddump();
  334. return;
  335. case 'k':
  336. killbig();
  337. return;
  338. case 'r':
  339. exit(0);
  340. return;
  341. }
  342. }
  343. qproduce(kbdq, buf, n);
  344. if(kbd.raw)
  345. return;
  346. if(screenputs != nil)
  347. echoscreen(buf, n);
  348. if(serialoq)
  349. echoserialoq(buf, n);
  350. }
  351. /*
  352. * Called by a uart interrupt for console input.
  353. *
  354. * turn '\r' into '\n' before putting it into the queue.
  355. */
  356. int
  357. kbdcr2nl(Queue*, int ch)
  358. {
  359. char *next;
  360. ilock(&kbd.lockputc); /* just a mutex */
  361. if(ch == '\r' && !kbd.raw)
  362. ch = '\n';
  363. next = kbd.iw+1;
  364. if(next >= kbd.ie)
  365. next = kbd.istage;
  366. if(next != kbd.ir){
  367. *kbd.iw = ch;
  368. kbd.iw = next;
  369. }
  370. iunlock(&kbd.lockputc);
  371. return 0;
  372. }
  373. /*
  374. * Put character, possibly a rune, into read queue at interrupt time.
  375. * Called at interrupt time to process a character.
  376. */
  377. int
  378. kbdputc(Queue*, int ch)
  379. {
  380. int i, n;
  381. char buf[3];
  382. Rune r;
  383. char *next;
  384. if(kbd.ir == nil)
  385. return 0; /* in case we're not inited yet */
  386. ilock(&kbd.lockputc); /* just a mutex */
  387. r = ch;
  388. n = runetochar(buf, &r);
  389. for(i = 0; i < n; i++){
  390. next = kbd.iw+1;
  391. if(next >= kbd.ie)
  392. next = kbd.istage;
  393. if(next == kbd.ir)
  394. break;
  395. *kbd.iw = buf[i];
  396. kbd.iw = next;
  397. }
  398. iunlock(&kbd.lockputc);
  399. return 0;
  400. }
  401. /*
  402. * we save up input characters till clock time to reduce
  403. * per character interrupt overhead.
  404. */
  405. static void
  406. kbdputcclock(void)
  407. {
  408. char *iw;
  409. /* this amortizes cost of qproduce */
  410. if(kbd.iw != kbd.ir){
  411. iw = kbd.iw;
  412. if(iw < kbd.ir){
  413. echo(kbd.ir, kbd.ie-kbd.ir);
  414. kbd.ir = kbd.istage;
  415. }
  416. echo(kbd.ir, iw-kbd.ir);
  417. kbd.ir = iw;
  418. }
  419. }
  420. enum{
  421. Qdir,
  422. Qbintime,
  423. Qcons,
  424. Qconsctl,
  425. Qcputime,
  426. Qdrivers,
  427. Qkprint,
  428. Qhostdomain,
  429. Qhostowner,
  430. Qnull,
  431. Qosversion,
  432. Qpgrpid,
  433. Qpid,
  434. Qppid,
  435. Qrandom,
  436. Qreboot,
  437. Qswap,
  438. Qsysname,
  439. Qsysstat,
  440. Qtime,
  441. Quser,
  442. Qzero,
  443. };
  444. enum
  445. {
  446. VLNUMSIZE= 22,
  447. };
  448. static Dirtab consdir[]={
  449. ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
  450. "bintime", {Qbintime}, 24, 0664,
  451. "cons", {Qcons}, 0, 0660,
  452. "consctl", {Qconsctl}, 0, 0220,
  453. "cputime", {Qcputime}, 6*NUMSIZE, 0444,
  454. "drivers", {Qdrivers}, 0, 0444,
  455. "hostdomain", {Qhostdomain}, DOMLEN, 0664,
  456. "hostowner", {Qhostowner}, 0, 0664,
  457. "kprint", {Qkprint, 0, QTEXCL}, 0, DMEXCL|0440,
  458. "null", {Qnull}, 0, 0666,
  459. "osversion", {Qosversion}, 0, 0444,
  460. "pgrpid", {Qpgrpid}, NUMSIZE, 0444,
  461. "pid", {Qpid}, NUMSIZE, 0444,
  462. "ppid", {Qppid}, NUMSIZE, 0444,
  463. "random", {Qrandom}, 0, 0444,
  464. "reboot", {Qreboot}, 0, 0664,
  465. "swap", {Qswap}, 0, 0664,
  466. "sysname", {Qsysname}, 0, 0664,
  467. "sysstat", {Qsysstat}, 0, 0666,
  468. "time", {Qtime}, NUMSIZE+3*VLNUMSIZE, 0664,
  469. "user", {Quser}, 0, 0666,
  470. "zero", {Qzero}, 0, 0444,
  471. };
  472. int
  473. readnum(ulong off, char *buf, ulong n, ulong val, int size)
  474. {
  475. char tmp[64];
  476. snprint(tmp, sizeof(tmp), "%*.0lud", size-1, val);
  477. tmp[size-1] = ' ';
  478. if(off >= size)
  479. return 0;
  480. if(off+n > size)
  481. n = size-off;
  482. memmove(buf, tmp+off, n);
  483. return n;
  484. }
  485. int
  486. readstr(ulong off, char *buf, ulong n, char *str)
  487. {
  488. int size;
  489. size = strlen(str);
  490. if(off >= size)
  491. return 0;
  492. if(off+n > size)
  493. n = size-off;
  494. memmove(buf, str+off, n);
  495. return n;
  496. }
  497. static void
  498. consinit(void)
  499. {
  500. todinit();
  501. randominit();
  502. /*
  503. * at 115200 baud, the 1024 char buffer takes 56 ms to process,
  504. * processing it every 22 ms should be fine
  505. */
  506. addclock0link(kbdputcclock, 22);
  507. }
  508. static Chan*
  509. consattach(char *spec)
  510. {
  511. return devattach('c', spec);
  512. }
  513. static Walkqid*
  514. conswalk(Chan *c, Chan *nc, char **name, int nname)
  515. {
  516. return devwalk(c, nc, name,nname, consdir, nelem(consdir), devgen);
  517. }
  518. static int
  519. consstat(Chan *c, uchar *dp, int n)
  520. {
  521. return devstat(c, dp, n, consdir, nelem(consdir), devgen);
  522. }
  523. static Chan*
  524. consopen(Chan *c, int omode)
  525. {
  526. c->aux = nil;
  527. c = devopen(c, omode, consdir, nelem(consdir), devgen);
  528. switch((ulong)c->qid.path){
  529. case Qconsctl:
  530. qlock(&kbd);
  531. kbd.ctl++;
  532. qunlock(&kbd);
  533. break;
  534. case Qkprint:
  535. if(tas(&kprintinuse) != 0){
  536. c->flag &= ~COPEN;
  537. error(Einuse);
  538. }
  539. if(kprintoq == nil){
  540. kprintoq = qopen(8*1024, Qcoalesce, 0, 0);
  541. if(kprintoq == nil){
  542. c->flag &= ~COPEN;
  543. error(Enomem);
  544. }
  545. qnoblock(kprintoq, 1);
  546. }else
  547. qreopen(kprintoq);
  548. c->iounit = qiomaxatomic;
  549. break;
  550. }
  551. return c;
  552. }
  553. static void
  554. consclose(Chan *c)
  555. {
  556. switch((ulong)c->qid.path){
  557. /* last close of control file turns off raw */
  558. case Qconsctl:
  559. if(c->flag&COPEN){
  560. qlock(&kbd);
  561. if(--kbd.ctl == 0)
  562. kbd.raw = 0;
  563. qunlock(&kbd);
  564. }
  565. break;
  566. /* close of kprint allows other opens */
  567. case Qkprint:
  568. if(c->flag & COPEN){
  569. kprintinuse = 0;
  570. qhangup(kprintoq, nil);
  571. }
  572. break;
  573. }
  574. }
  575. static long
  576. consread(Chan *c, void *buf, long n, vlong off)
  577. {
  578. ulong l;
  579. Mach *mp;
  580. char *b, *bp;
  581. char tmp[128]; /* must be >= 6*NUMSIZE */
  582. char *cbuf = buf;
  583. int ch, i, k, id, eol;
  584. vlong offset = off;
  585. if(n <= 0)
  586. return n;
  587. switch((ulong)c->qid.path){
  588. case Qdir:
  589. return devdirread(c, buf, n, consdir, nelem(consdir), devgen);
  590. case Qcons:
  591. qlock(&kbd);
  592. if(waserror()) {
  593. qunlock(&kbd);
  594. nexterror();
  595. }
  596. if(kbd.raw) {
  597. if(qcanread(lineq))
  598. n = qread(lineq, buf, n);
  599. else {
  600. /* read as much as possible */
  601. do {
  602. i = qread(kbdq, cbuf, n);
  603. cbuf += i;
  604. n -= i;
  605. } while (n>0 && qcanread(kbdq));
  606. n = cbuf - (char*)buf;
  607. }
  608. } else {
  609. while(!qcanread(lineq)) {
  610. qread(kbdq, &kbd.line[kbd.x], 1);
  611. ch = kbd.line[kbd.x];
  612. eol = 0;
  613. switch(ch){
  614. case '\b':
  615. if(kbd.x)
  616. kbd.x--;
  617. break;
  618. case 0x15:
  619. kbd.x = 0;
  620. break;
  621. case '\n':
  622. case 0x04:
  623. eol = 1;
  624. default:
  625. kbd.line[kbd.x++] = ch;
  626. break;
  627. }
  628. if(kbd.x == sizeof(kbd.line) || eol){
  629. if(ch == 0x04)
  630. kbd.x--;
  631. qwrite(lineq, kbd.line, kbd.x);
  632. kbd.x = 0;
  633. }
  634. }
  635. n = qread(lineq, buf, n);
  636. }
  637. qunlock(&kbd);
  638. poperror();
  639. return n;
  640. case Qcputime:
  641. k = offset;
  642. if(k >= 6*NUMSIZE)
  643. return 0;
  644. if(k+n > 6*NUMSIZE)
  645. n = 6*NUMSIZE - k;
  646. /* easiest to format in a separate buffer and copy out */
  647. for(i=0; i<6 && NUMSIZE*i<k+n; i++){
  648. l = up->time[i];
  649. if(i == TReal)
  650. l = MACHP(0)->ticks - l;
  651. l = TK2MS(l);
  652. readnum(0, tmp+NUMSIZE*i, NUMSIZE, l, NUMSIZE);
  653. }
  654. memmove(buf, tmp+k, n);
  655. return n;
  656. case Qkprint:
  657. return qread(kprintoq, buf, n);
  658. case Qpgrpid:
  659. return readnum((ulong)offset, buf, n, up->pgrp->pgrpid, NUMSIZE);
  660. case Qpid:
  661. return readnum((ulong)offset, buf, n, up->pid, NUMSIZE);
  662. case Qppid:
  663. return readnum((ulong)offset, buf, n, up->parentpid, NUMSIZE);
  664. case Qtime:
  665. return readtime((ulong)offset, buf, n);
  666. case Qbintime:
  667. return readbintime(buf, n);
  668. case Qhostowner:
  669. return readstr((ulong)offset, buf, n, eve);
  670. case Qhostdomain:
  671. return readstr((ulong)offset, buf, n, hostdomain);
  672. case Quser:
  673. return readstr((ulong)offset, buf, n, up->user);
  674. case Qnull:
  675. return 0;
  676. case Qsysstat:
  677. b = smalloc(conf.nmach*(NUMSIZE*11+1) + 1); /* +1 for NUL */
  678. bp = b;
  679. for(id = 0; id < 32; id++) {
  680. if(active.machs & (1<<id)) {
  681. mp = MACHP(id);
  682. readnum(0, bp, NUMSIZE, id, NUMSIZE);
  683. bp += NUMSIZE;
  684. readnum(0, bp, NUMSIZE, mp->cs, NUMSIZE);
  685. bp += NUMSIZE;
  686. readnum(0, bp, NUMSIZE, mp->intr, NUMSIZE);
  687. bp += NUMSIZE;
  688. readnum(0, bp, NUMSIZE, mp->syscall, NUMSIZE);
  689. bp += NUMSIZE;
  690. readnum(0, bp, NUMSIZE, mp->pfault, NUMSIZE);
  691. bp += NUMSIZE;
  692. readnum(0, bp, NUMSIZE, mp->tlbfault, NUMSIZE);
  693. bp += NUMSIZE;
  694. readnum(0, bp, NUMSIZE, mp->tlbpurge, NUMSIZE);
  695. bp += NUMSIZE;
  696. readnum(0, bp, NUMSIZE, mp->load, NUMSIZE);
  697. bp += NUMSIZE;
  698. readnum(0, bp, NUMSIZE,
  699. (mp->perf.avg_inidle*100)/mp->perf.period,
  700. NUMSIZE);
  701. bp += NUMSIZE;
  702. readnum(0, bp, NUMSIZE,
  703. (mp->perf.avg_inintr*100)/mp->perf.period,
  704. NUMSIZE);
  705. bp += NUMSIZE;
  706. *bp++ = '\n';
  707. }
  708. }
  709. if(waserror()){
  710. free(b);
  711. nexterror();
  712. }
  713. n = readstr((ulong)offset, buf, n, b);
  714. free(b);
  715. poperror();
  716. return n;
  717. case Qswap:
  718. sprint(tmp, "%lud/%lud memory %lud/%lud swap\n",
  719. palloc.user-palloc.freecount,
  720. palloc.user, conf.nswap-swapalloc.free, conf.nswap);
  721. return readstr((ulong)offset, buf, n, tmp);
  722. case Qsysname:
  723. if(sysname == nil)
  724. return 0;
  725. return readstr((ulong)offset, buf, n, sysname);
  726. case Qrandom:
  727. return randomread(buf, n);
  728. case Qdrivers:
  729. b = malloc(READSTR);
  730. if(b == nil)
  731. error(Enomem);
  732. n = 0;
  733. for(i = 0; devtab[i] != nil; i++)
  734. n += snprint(b+n, READSTR-n, "#%C %s\n", devtab[i]->dc, devtab[i]->name);
  735. if(waserror()){
  736. free(b);
  737. nexterror();
  738. }
  739. n = readstr((ulong)offset, buf, n, b);
  740. free(b);
  741. poperror();
  742. return n;
  743. case Qzero:
  744. memset(buf, 0, n);
  745. return n;
  746. case Qosversion:
  747. snprint(tmp, sizeof tmp, "2000");
  748. n = readstr((ulong)offset, buf, n, tmp);
  749. return n;
  750. default:
  751. print("consread 0x%llux\n", c->qid.path);
  752. error(Egreg);
  753. }
  754. return -1; /* never reached */
  755. }
  756. static long
  757. conswrite(Chan *c, void *va, long n, vlong off)
  758. {
  759. char buf[256];
  760. long l, bp;
  761. char *a = va;
  762. Mach *mp;
  763. int id, fd;
  764. Chan *swc;
  765. ulong offset = off;
  766. Cmdbuf *cb;
  767. Cmdtab *ct;
  768. switch((ulong)c->qid.path){
  769. case Qcons:
  770. /*
  771. * Can't page fault in putstrn, so copy the data locally.
  772. */
  773. l = n;
  774. while(l > 0){
  775. bp = l;
  776. if(bp > sizeof buf)
  777. bp = sizeof buf;
  778. memmove(buf, a, bp);
  779. putstrn0(buf, bp, 1);
  780. a += bp;
  781. l -= bp;
  782. }
  783. break;
  784. case Qconsctl:
  785. if(n >= sizeof(buf))
  786. n = sizeof(buf)-1;
  787. strncpy(buf, a, n);
  788. buf[n] = 0;
  789. for(a = buf; a;){
  790. if(strncmp(a, "rawon", 5) == 0){
  791. qlock(&kbd);
  792. if(kbd.x){
  793. qwrite(kbdq, kbd.line, kbd.x);
  794. kbd.x = 0;
  795. }
  796. kbd.raw = 1;
  797. qunlock(&kbd);
  798. } else if(strncmp(a, "rawoff", 6) == 0){
  799. qlock(&kbd);
  800. kbd.raw = 0;
  801. kbd.x = 0;
  802. qunlock(&kbd);
  803. } else if(strncmp(a, "ctlpon", 6) == 0){
  804. kbd.ctlpoff = 0;
  805. } else if(strncmp(a, "ctlpoff", 7) == 0){
  806. kbd.ctlpoff = 1;
  807. }
  808. if(a = strchr(a, ' '))
  809. a++;
  810. }
  811. break;
  812. case Qtime:
  813. if(!iseve())
  814. error(Eperm);
  815. return writetime(a, n);
  816. case Qbintime:
  817. if(!iseve())
  818. error(Eperm);
  819. return writebintime(a, n);
  820. case Qhostowner:
  821. return hostownerwrite(a, n);
  822. case Qhostdomain:
  823. return hostdomainwrite(a, n);
  824. case Quser:
  825. return userwrite(a, n);
  826. case Qnull:
  827. break;
  828. case Qreboot:
  829. if(!iseve())
  830. error(Eperm);
  831. cb = parsecmd(a, n);
  832. if(waserror()) {
  833. free(cb);
  834. nexterror();
  835. }
  836. ct = lookupcmd(cb, rebootmsg, nelem(rebootmsg));
  837. switch(ct->index) {
  838. case CMreboot:
  839. rebootcmd(cb->nf-1, cb->f+1);
  840. break;
  841. case CMpanic:
  842. panic("/dev/reboot");
  843. }
  844. poperror();
  845. free(cb);
  846. break;
  847. case Qsysstat:
  848. for(id = 0; id < 32; id++) {
  849. if(active.machs & (1<<id)) {
  850. mp = MACHP(id);
  851. mp->cs = 0;
  852. mp->intr = 0;
  853. mp->syscall = 0;
  854. mp->pfault = 0;
  855. mp->tlbfault = 0;
  856. mp->tlbpurge = 0;
  857. }
  858. }
  859. break;
  860. case Qswap:
  861. if(n >= sizeof buf)
  862. error(Egreg);
  863. memmove(buf, va, n); /* so we can NUL-terminate */
  864. buf[n] = 0;
  865. /* start a pager if not already started */
  866. if(strncmp(buf, "start", 5) == 0){
  867. kickpager();
  868. break;
  869. }
  870. if(cpuserver && !iseve())
  871. error(Eperm);
  872. if(buf[0]<'0' || '9'<buf[0])
  873. error(Ebadarg);
  874. fd = strtoul(buf, 0, 0);
  875. swc = fdtochan(fd, -1, 1, 1);
  876. setswapchan(swc);
  877. break;
  878. case Qsysname:
  879. if(offset != 0)
  880. error(Ebadarg);
  881. if(n <= 0 || n >= sizeof buf)
  882. error(Ebadarg);
  883. strncpy(buf, a, n);
  884. buf[n] = 0;
  885. if(buf[n-1] == '\n')
  886. buf[n-1] = 0;
  887. kstrdup(&sysname, buf);
  888. break;
  889. default:
  890. print("conswrite: 0x%llux\n", c->qid.path);
  891. error(Egreg);
  892. }
  893. return n;
  894. }
  895. Dev consdevtab = {
  896. 'c',
  897. "cons",
  898. devreset,
  899. consinit,
  900. devshutdown,
  901. consattach,
  902. conswalk,
  903. consstat,
  904. consopen,
  905. devcreate,
  906. consclose,
  907. consread,
  908. devbread,
  909. conswrite,
  910. devbwrite,
  911. devremove,
  912. devwstat,
  913. };
  914. static ulong randn;
  915. static void
  916. seedrand(void)
  917. {
  918. randomread((void*)&randn, sizeof(randn));
  919. }
  920. int
  921. nrand(int n)
  922. {
  923. if(randn == 0)
  924. seedrand();
  925. randn = randn*1103515245 + 12345 + MACHP(0)->ticks;
  926. return (randn>>16) % n;
  927. }
  928. int
  929. rand(void)
  930. {
  931. nrand(1);
  932. return randn;
  933. }
  934. static uvlong uvorder = 0x0001020304050607ULL;
  935. static uchar*
  936. le2vlong(vlong *to, uchar *f)
  937. {
  938. uchar *t, *o;
  939. int i;
  940. t = (uchar*)to;
  941. o = (uchar*)&uvorder;
  942. for(i = 0; i < sizeof(vlong); i++)
  943. t[o[i]] = f[i];
  944. return f+sizeof(vlong);
  945. }
  946. static uchar*
  947. vlong2le(uchar *t, vlong from)
  948. {
  949. uchar *f, *o;
  950. int i;
  951. f = (uchar*)&from;
  952. o = (uchar*)&uvorder;
  953. for(i = 0; i < sizeof(vlong); i++)
  954. t[i] = f[o[i]];
  955. return t+sizeof(vlong);
  956. }
  957. static long order = 0x00010203;
  958. static uchar*
  959. le2long(long *to, uchar *f)
  960. {
  961. uchar *t, *o;
  962. int i;
  963. t = (uchar*)to;
  964. o = (uchar*)&order;
  965. for(i = 0; i < sizeof(long); i++)
  966. t[o[i]] = f[i];
  967. return f+sizeof(long);
  968. }
  969. static uchar*
  970. long2le(uchar *t, long from)
  971. {
  972. uchar *f, *o;
  973. int i;
  974. f = (uchar*)&from;
  975. o = (uchar*)&order;
  976. for(i = 0; i < sizeof(long); i++)
  977. t[i] = f[o[i]];
  978. return t+sizeof(long);
  979. }
  980. char *Ebadtimectl = "bad time control";
  981. /*
  982. * like the old #c/time but with added info. Return
  983. *
  984. * secs nanosecs fastticks fasthz
  985. */
  986. static int
  987. readtime(ulong off, char *buf, int n)
  988. {
  989. vlong nsec, ticks;
  990. long sec;
  991. char str[7*NUMSIZE];
  992. nsec = todget(&ticks);
  993. if(fasthz == 0LL)
  994. fastticks((uvlong*)&fasthz);
  995. sec = nsec/1000000000ULL;
  996. snprint(str, sizeof(str), "%*.0lud %*.0llud %*.0llud %*.0llud ",
  997. NUMSIZE-1, sec,
  998. VLNUMSIZE-1, nsec,
  999. VLNUMSIZE-1, ticks,
  1000. VLNUMSIZE-1, fasthz);
  1001. return readstr(off, buf, n, str);
  1002. }
  1003. /*
  1004. * set the time in seconds
  1005. */
  1006. static int
  1007. writetime(char *buf, int n)
  1008. {
  1009. char b[13];
  1010. long i;
  1011. vlong now;
  1012. if(n >= sizeof(b))
  1013. error(Ebadtimectl);
  1014. strncpy(b, buf, n);
  1015. b[n] = 0;
  1016. i = strtol(b, 0, 0);
  1017. if(i <= 0)
  1018. error(Ebadtimectl);
  1019. now = i*1000000000LL;
  1020. todset(now, 0, 0);
  1021. return n;
  1022. }
  1023. /*
  1024. * read binary time info. all numbers are little endian.
  1025. * ticks and nsec are syncronized.
  1026. */
  1027. static int
  1028. readbintime(char *buf, int n)
  1029. {
  1030. int i;
  1031. vlong nsec, ticks;
  1032. uchar *b = (uchar*)buf;
  1033. i = 0;
  1034. if(fasthz == 0LL)
  1035. fastticks((uvlong*)&fasthz);
  1036. nsec = todget(&ticks);
  1037. if(n >= 3*sizeof(uvlong)){
  1038. vlong2le(b+2*sizeof(uvlong), fasthz);
  1039. i += sizeof(uvlong);
  1040. }
  1041. if(n >= 2*sizeof(uvlong)){
  1042. vlong2le(b+sizeof(uvlong), ticks);
  1043. i += sizeof(uvlong);
  1044. }
  1045. if(n >= 8){
  1046. vlong2le(b, nsec);
  1047. i += sizeof(vlong);
  1048. }
  1049. return i;
  1050. }
  1051. /*
  1052. * set any of the following
  1053. * - time in nsec
  1054. * - nsec trim applied over some seconds
  1055. * - clock frequency
  1056. */
  1057. static int
  1058. writebintime(char *buf, int n)
  1059. {
  1060. uchar *p;
  1061. vlong delta;
  1062. long period;
  1063. n--;
  1064. p = (uchar*)buf + 1;
  1065. switch(*buf){
  1066. case 'n':
  1067. if(n < sizeof(vlong))
  1068. error(Ebadtimectl);
  1069. le2vlong(&delta, p);
  1070. todset(delta, 0, 0);
  1071. break;
  1072. case 'd':
  1073. if(n < sizeof(vlong)+sizeof(long))
  1074. error(Ebadtimectl);
  1075. p = le2vlong(&delta, p);
  1076. le2long(&period, p);
  1077. todset(-1, delta, period);
  1078. break;
  1079. case 'f':
  1080. if(n < sizeof(uvlong))
  1081. error(Ebadtimectl);
  1082. le2vlong(&fasthz, p);
  1083. todsetfreq(fasthz);
  1084. break;
  1085. }
  1086. return n;
  1087. }