devcons.c 21 KB

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