devcons.c 23 KB

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