devcons.c 22 KB

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