devcons.c 23 KB

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