devcons.c 23 KB

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