devcons.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. #include "pool.h"
  8. #include <authsrv.h>
  9. void (*consdebug)(void) = nil;
  10. void (*screenputs)(char*, int) = nil;
  11. Queue* kbdq; /* unprocessed console input */
  12. Queue* lineq; /* processed console input */
  13. Queue* serialoq; /* serial console output */
  14. Queue* kprintoq; /* console output, for /dev/kprint */
  15. ulong kprintinuse; /* test and set whether /dev/kprint is open */
  16. int iprintscreenputs = 1;
  17. int panicking;
  18. static struct
  19. {
  20. QLock;
  21. int raw; /* true if we shouldn't process input */
  22. Ref ctl; /* number of opens to the control file */
  23. int x; /* index into line */
  24. char line[1024]; /* current input line */
  25. int count;
  26. int ctlpoff;
  27. /* a place to save up characters at interrupt time before dumping them in the queue */
  28. Lock lockputc;
  29. char istage[1024];
  30. char *iw;
  31. char *ir;
  32. char *ie;
  33. } kbd = {
  34. .iw = kbd.istage,
  35. .ir = kbd.istage,
  36. .ie = kbd.istage + sizeof(kbd.istage),
  37. };
  38. char *sysname;
  39. vlong fasthz;
  40. static void seedrand(void);
  41. static int readtime(ulong, char*, int);
  42. static int readbintime(char*, int);
  43. static int writetime(char*, int);
  44. static int writebintime(char*, int);
  45. enum
  46. {
  47. CMhalt,
  48. CMreboot,
  49. CMpanic,
  50. };
  51. Cmdtab rebootmsg[] =
  52. {
  53. CMhalt, "halt", 1,
  54. CMreboot, "reboot", 0,
  55. CMpanic, "panic", 0,
  56. };
  57. void
  58. printinit(void)
  59. {
  60. lineq = qopen(2*1024, 0, nil, nil);
  61. if(lineq == nil)
  62. panic("printinit");
  63. qnoblock(lineq, 1);
  64. }
  65. int
  66. consactive(void)
  67. {
  68. if(serialoq)
  69. return qlen(serialoq) > 0;
  70. return 0;
  71. }
  72. void
  73. prflush(void)
  74. {
  75. ulong now;
  76. now = m->ticks;
  77. while(consactive())
  78. if(m->ticks - now >= HZ)
  79. break;
  80. }
  81. /*
  82. * Log console output so it can be retrieved via /dev/kmesg.
  83. * This is good for catching boot-time messages after the fact.
  84. */
  85. struct {
  86. Lock lk;
  87. char buf[16384];
  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. putstrn(buf, n);
  192. return n;
  193. }
  194. /*
  195. * Want to interlock iprints to avoid interlaced output on
  196. * multiprocessor, but don't want to deadlock if one processor
  197. * dies during print and another has something important to say.
  198. * Make a good faith effort.
  199. */
  200. static Lock iprintlock;
  201. static int
  202. iprintcanlock(Lock *l)
  203. {
  204. int i;
  205. for(i=0; i<1000; i++){
  206. if(canlock(l))
  207. return 1;
  208. if(l->m == MACHP(m->machno))
  209. return 0;
  210. microdelay(100);
  211. }
  212. return 0;
  213. }
  214. int
  215. iprint(char *fmt, ...)
  216. {
  217. int n, s, locked;
  218. va_list arg;
  219. char buf[PRINTSIZE];
  220. s = splhi();
  221. va_start(arg, fmt);
  222. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  223. va_end(arg);
  224. locked = iprintcanlock(&iprintlock);
  225. if(screenputs != nil && iprintscreenputs)
  226. screenputs(buf, n);
  227. uartputs(buf, n);
  228. if(locked)
  229. unlock(&iprintlock);
  230. splx(s);
  231. return n;
  232. }
  233. void
  234. panic(char *fmt, ...)
  235. {
  236. int n, s;
  237. va_list arg;
  238. char buf[PRINTSIZE];
  239. kprintoq = nil; /* don't try to write to /dev/kprint */
  240. if(panicking)
  241. for(;;);
  242. panicking = 1;
  243. s = splhi();
  244. strcpy(buf, "panic: ");
  245. va_start(arg, fmt);
  246. n = vseprint(buf+strlen(buf), buf+sizeof(buf), fmt, arg) - buf;
  247. va_end(arg);
  248. iprint("%s\n", buf);
  249. if(consdebug)
  250. (*consdebug)();
  251. splx(s);
  252. prflush();
  253. buf[n] = '\n';
  254. putstrn(buf, n+1);
  255. dumpstack();
  256. exit(1);
  257. }
  258. void
  259. _assert(char *fmt)
  260. {
  261. panic("assert failed at 0x%lux: %s", getcallerpc(&fmt), fmt);
  262. }
  263. int
  264. pprint(char *fmt, ...)
  265. {
  266. int n;
  267. Chan *c;
  268. va_list arg;
  269. char buf[2*PRINTSIZE];
  270. if(up == nil || up->fgrp == nil)
  271. return 0;
  272. c = up->fgrp->fd[2];
  273. if(c==0 || (c->mode!=OWRITE && c->mode!=ORDWR))
  274. return 0;
  275. n = snprint(buf, sizeof buf, "%s %lud: ", up->text, up->pid);
  276. va_start(arg, fmt);
  277. n = vseprint(buf+n, buf+sizeof(buf), fmt, arg) - buf;
  278. va_end(arg);
  279. if(waserror())
  280. return 0;
  281. devtab[c->type]->write(c, buf, n, c->offset);
  282. poperror();
  283. lock(c);
  284. c->offset += n;
  285. unlock(c);
  286. return n;
  287. }
  288. static void
  289. echoscreen(char *buf, int n)
  290. {
  291. char *e, *p;
  292. char ebuf[128];
  293. int x;
  294. p = ebuf;
  295. e = ebuf + sizeof(ebuf) - 4;
  296. while(n-- > 0){
  297. if(p >= e){
  298. screenputs(ebuf, p - ebuf);
  299. p = ebuf;
  300. }
  301. x = *buf++;
  302. if(x == 0x15){
  303. *p++ = '^';
  304. *p++ = 'U';
  305. *p++ = '\n';
  306. } else
  307. *p++ = x;
  308. }
  309. if(p != ebuf)
  310. screenputs(ebuf, p - ebuf);
  311. }
  312. static void
  313. echoserialoq(char *buf, int n)
  314. {
  315. char *e, *p;
  316. char ebuf[128];
  317. int x;
  318. p = ebuf;
  319. e = ebuf + sizeof(ebuf) - 4;
  320. while(n-- > 0){
  321. if(p >= e){
  322. qiwrite(serialoq, ebuf, p - ebuf);
  323. p = ebuf;
  324. }
  325. x = *buf++;
  326. if(x == '\n'){
  327. *p++ = '\r';
  328. *p++ = '\n';
  329. } else if(x == 0x15){
  330. *p++ = '^';
  331. *p++ = 'U';
  332. *p++ = '\n';
  333. } else
  334. *p++ = x;
  335. }
  336. if(p != ebuf)
  337. qiwrite(serialoq, ebuf, p - ebuf);
  338. }
  339. static void
  340. echo(char *buf, int n)
  341. {
  342. static int ctrlt, pid;
  343. int x;
  344. char *e, *p;
  345. if(n == 0)
  346. return;
  347. e = buf+n;
  348. for(p = buf; p < e; p++){
  349. switch(*p){
  350. case 0x10: /* ^P */
  351. if(cpuserver && !kbd.ctlpoff){
  352. active.exiting = 1;
  353. return;
  354. }
  355. break;
  356. case 0x14: /* ^T */
  357. ctrlt++;
  358. if(ctrlt > 2)
  359. ctrlt = 2;
  360. continue;
  361. }
  362. if(ctrlt != 2)
  363. continue;
  364. /* ^T escapes */
  365. ctrlt = 0;
  366. switch(*p){
  367. case 'S':
  368. x = splhi();
  369. dumpstack();
  370. procdump();
  371. splx(x);
  372. return;
  373. case 's':
  374. dumpstack();
  375. return;
  376. case 'x':
  377. xsummary();
  378. ixsummary();
  379. mallocsummary();
  380. // memorysummary();
  381. pagersummary();
  382. return;
  383. case 'd':
  384. if(consdebug == nil)
  385. consdebug = rdb;
  386. else
  387. consdebug = nil;
  388. print("consdebug now 0x%p\n", consdebug);
  389. return;
  390. case 'D':
  391. if(consdebug == nil)
  392. consdebug = rdb;
  393. consdebug();
  394. return;
  395. case 'p':
  396. x = spllo();
  397. procdump();
  398. splx(x);
  399. return;
  400. case 'q':
  401. scheddump();
  402. return;
  403. case 'k':
  404. killbig("^t ^t k");
  405. return;
  406. case 'r':
  407. exit(0);
  408. return;
  409. }
  410. }
  411. qproduce(kbdq, buf, n);
  412. if(kbd.raw)
  413. return;
  414. kmesgputs(buf, n);
  415. if(screenputs != nil)
  416. echoscreen(buf, n);
  417. if(serialoq)
  418. echoserialoq(buf, n);
  419. }
  420. /*
  421. * Called by a uart interrupt for console input.
  422. *
  423. * turn '\r' into '\n' before putting it into the queue.
  424. */
  425. int
  426. kbdcr2nl(Queue*, int ch)
  427. {
  428. char *next;
  429. ilock(&kbd.lockputc); /* just a mutex */
  430. if(ch == '\r' && !kbd.raw)
  431. ch = '\n';
  432. next = kbd.iw+1;
  433. if(next >= kbd.ie)
  434. next = kbd.istage;
  435. if(next != kbd.ir){
  436. *kbd.iw = ch;
  437. kbd.iw = next;
  438. }
  439. iunlock(&kbd.lockputc);
  440. return 0;
  441. }
  442. /*
  443. * Put character, possibly a rune, into read queue at interrupt time.
  444. * Called at interrupt time to process a character.
  445. */
  446. int
  447. kbdputc(Queue*, int ch)
  448. {
  449. int i, n;
  450. char buf[3];
  451. Rune r;
  452. char *next;
  453. if(kbd.ir == nil)
  454. return 0; /* in case we're not inited yet */
  455. ilock(&kbd.lockputc); /* just a mutex */
  456. r = ch;
  457. n = runetochar(buf, &r);
  458. for(i = 0; i < n; i++){
  459. next = kbd.iw+1;
  460. if(next >= kbd.ie)
  461. next = kbd.istage;
  462. if(next == kbd.ir)
  463. break;
  464. *kbd.iw = buf[i];
  465. kbd.iw = next;
  466. }
  467. iunlock(&kbd.lockputc);
  468. return 0;
  469. }
  470. /*
  471. * we save up input characters till clock time to reduce
  472. * per character interrupt overhead.
  473. */
  474. static void
  475. kbdputcclock(void)
  476. {
  477. char *iw;
  478. /* this amortizes cost of qproduce */
  479. if(kbd.iw != kbd.ir){
  480. iw = kbd.iw;
  481. if(iw < kbd.ir){
  482. echo(kbd.ir, kbd.ie-kbd.ir);
  483. kbd.ir = kbd.istage;
  484. }
  485. if(kbd.ir != iw){
  486. echo(kbd.ir, iw-kbd.ir);
  487. kbd.ir = iw;
  488. }
  489. }
  490. }
  491. enum{
  492. Qdir,
  493. Qbintime,
  494. Qcons,
  495. Qconsctl,
  496. Qcputime,
  497. Qdrivers,
  498. Qkmesg,
  499. Qkprint,
  500. Qhostdomain,
  501. Qhostowner,
  502. Qnull,
  503. Qosversion,
  504. Qpgrpid,
  505. Qpid,
  506. Qppid,
  507. Qrandom,
  508. Qreboot,
  509. Qswap,
  510. Qsysname,
  511. Qsysstat,
  512. Qtime,
  513. Quser,
  514. Qzero,
  515. };
  516. enum
  517. {
  518. VLNUMSIZE= 22,
  519. };
  520. static Dirtab consdir[]={
  521. ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
  522. "bintime", {Qbintime}, 24, 0664,
  523. "cons", {Qcons}, 0, 0660,
  524. "consctl", {Qconsctl}, 0, 0220,
  525. "cputime", {Qcputime}, 6*NUMSIZE, 0444,
  526. "drivers", {Qdrivers}, 0, 0444,
  527. "hostdomain", {Qhostdomain}, DOMLEN, 0664,
  528. "hostowner", {Qhostowner}, 0, 0664,
  529. "kmesg", {Qkmesg}, 0, 0440,
  530. "kprint", {Qkprint, 0, QTEXCL}, 0, DMEXCL|0440,
  531. "null", {Qnull}, 0, 0666,
  532. "osversion", {Qosversion}, 0, 0444,
  533. "pgrpid", {Qpgrpid}, NUMSIZE, 0444,
  534. "pid", {Qpid}, NUMSIZE, 0444,
  535. "ppid", {Qppid}, NUMSIZE, 0444,
  536. "random", {Qrandom}, 0, 0444,
  537. "reboot", {Qreboot}, 0, 0664,
  538. "swap", {Qswap}, 0, 0664,
  539. "sysname", {Qsysname}, 0, 0664,
  540. "sysstat", {Qsysstat}, 0, 0666,
  541. "time", {Qtime}, NUMSIZE+3*VLNUMSIZE, 0664,
  542. "user", {Quser}, 0, 0666,
  543. "zero", {Qzero}, 0, 0444,
  544. };
  545. int
  546. readnum(ulong off, char *buf, ulong n, ulong val, int size)
  547. {
  548. char tmp[64];
  549. snprint(tmp, sizeof(tmp), "%*.0lud", size-1, val);
  550. tmp[size-1] = ' ';
  551. if(off >= size)
  552. return 0;
  553. if(off+n > size)
  554. n = size-off;
  555. memmove(buf, tmp+off, n);
  556. return n;
  557. }
  558. int
  559. readstr(ulong off, char *buf, ulong n, char *str)
  560. {
  561. int size;
  562. size = strlen(str);
  563. if(off >= size)
  564. return 0;
  565. if(off+n > size)
  566. n = size-off;
  567. memmove(buf, str+off, n);
  568. return n;
  569. }
  570. static void
  571. consinit(void)
  572. {
  573. todinit();
  574. randominit();
  575. /*
  576. * at 115200 baud, the 1024 char buffer takes 56 ms to process,
  577. * processing it every 22 ms should be fine
  578. */
  579. addclock0link(kbdputcclock, 22);
  580. }
  581. static Chan*
  582. consattach(char *spec)
  583. {
  584. return devattach('c', spec);
  585. }
  586. static Walkqid*
  587. conswalk(Chan *c, Chan *nc, char **name, int nname)
  588. {
  589. return devwalk(c, nc, name,nname, consdir, nelem(consdir), devgen);
  590. }
  591. static int
  592. consstat(Chan *c, uchar *dp, int n)
  593. {
  594. return devstat(c, dp, n, consdir, nelem(consdir), devgen);
  595. }
  596. static Chan*
  597. consopen(Chan *c, int omode)
  598. {
  599. c->aux = nil;
  600. c = devopen(c, omode, consdir, nelem(consdir), devgen);
  601. switch((ulong)c->qid.path){
  602. case Qconsctl:
  603. incref(&kbd.ctl);
  604. break;
  605. case Qkprint:
  606. if(tas(&kprintinuse) != 0){
  607. c->flag &= ~COPEN;
  608. error(Einuse);
  609. }
  610. if(kprintoq == nil){
  611. kprintoq = qopen(8*1024, Qcoalesce, 0, 0);
  612. if(kprintoq == nil){
  613. c->flag &= ~COPEN;
  614. error(Enomem);
  615. }
  616. qnoblock(kprintoq, 1);
  617. }else
  618. qreopen(kprintoq);
  619. c->iounit = qiomaxatomic;
  620. break;
  621. }
  622. return c;
  623. }
  624. static void
  625. consclose(Chan *c)
  626. {
  627. switch((ulong)c->qid.path){
  628. /* last close of control file turns off raw */
  629. case Qconsctl:
  630. if(c->flag&COPEN){
  631. if(decref(&kbd.ctl) == 0)
  632. kbd.raw = 0;
  633. }
  634. break;
  635. /* close of kprint allows other opens */
  636. case Qkprint:
  637. if(c->flag & COPEN){
  638. kprintinuse = 0;
  639. qhangup(kprintoq, nil);
  640. }
  641. break;
  642. }
  643. }
  644. static long
  645. consread(Chan *c, void *buf, long n, vlong off)
  646. {
  647. ulong l;
  648. Mach *mp;
  649. char *b, *bp, ch;
  650. char tmp[256]; /* must be >= 18*NUMSIZE (Qswap) */
  651. int i, k, id, send;
  652. vlong offset = off;
  653. if(n <= 0)
  654. return n;
  655. switch((ulong)c->qid.path){
  656. case Qdir:
  657. return devdirread(c, buf, n, consdir, nelem(consdir), devgen);
  658. case Qcons:
  659. qlock(&kbd);
  660. if(waserror()) {
  661. qunlock(&kbd);
  662. nexterror();
  663. }
  664. while(!qcanread(lineq)){
  665. if(qread(kbdq, &ch, 1) == 0)
  666. continue;
  667. send = 0;
  668. if(ch == 0){
  669. /* flush output on rawoff -> rawon */
  670. if(kbd.x > 0)
  671. send = !qcanread(kbdq);
  672. }else if(kbd.raw){
  673. kbd.line[kbd.x++] = ch;
  674. send = !qcanread(kbdq);
  675. }else{
  676. switch(ch){
  677. case '\b':
  678. if(kbd.x > 0)
  679. kbd.x--;
  680. break;
  681. case 0x15: /* ^U */
  682. kbd.x = 0;
  683. break;
  684. case '\n':
  685. case 0x04: /* ^D */
  686. send = 1;
  687. default:
  688. if(ch != 0x04)
  689. kbd.line[kbd.x++] = ch;
  690. break;
  691. }
  692. }
  693. if(send || kbd.x == sizeof kbd.line){
  694. qwrite(lineq, kbd.line, kbd.x);
  695. kbd.x = 0;
  696. }
  697. }
  698. n = qread(lineq, buf, n);
  699. qunlock(&kbd);
  700. poperror();
  701. return n;
  702. case Qcputime:
  703. k = offset;
  704. if(k >= 6*NUMSIZE)
  705. return 0;
  706. if(k+n > 6*NUMSIZE)
  707. n = 6*NUMSIZE - k;
  708. /* easiest to format in a separate buffer and copy out */
  709. for(i=0; i<6 && NUMSIZE*i<k+n; i++){
  710. l = up->time[i];
  711. if(i == TReal)
  712. l = MACHP(0)->ticks - l;
  713. l = TK2MS(l);
  714. readnum(0, tmp+NUMSIZE*i, NUMSIZE, l, NUMSIZE);
  715. }
  716. memmove(buf, tmp+k, n);
  717. return n;
  718. case Qkmesg:
  719. /*
  720. * This is unlocked to avoid tying up a process
  721. * that's writing to the buffer. kmesg.n never
  722. * gets smaller, so worst case the reader will
  723. * see a slurred buffer.
  724. */
  725. if(off >= kmesg.n)
  726. n = 0;
  727. else{
  728. if(off+n > kmesg.n)
  729. n = kmesg.n - off;
  730. memmove(buf, kmesg.buf+off, n);
  731. }
  732. return n;
  733. case Qkprint:
  734. return qread(kprintoq, buf, n);
  735. case Qpgrpid:
  736. return readnum((ulong)offset, buf, n, up->pgrp->pgrpid, NUMSIZE);
  737. case Qpid:
  738. return readnum((ulong)offset, buf, n, up->pid, NUMSIZE);
  739. case Qppid:
  740. return readnum((ulong)offset, buf, n, up->parentpid, NUMSIZE);
  741. case Qtime:
  742. return readtime((ulong)offset, buf, n);
  743. case Qbintime:
  744. return readbintime(buf, n);
  745. case Qhostowner:
  746. return readstr((ulong)offset, buf, n, eve);
  747. case Qhostdomain:
  748. return readstr((ulong)offset, buf, n, hostdomain);
  749. case Quser:
  750. return readstr((ulong)offset, buf, n, up->user);
  751. case Qnull:
  752. return 0;
  753. case Qsysstat:
  754. b = smalloc(conf.nmach*(NUMSIZE*11+1) + 1); /* +1 for NUL */
  755. bp = b;
  756. for(id = 0; id < 32; id++) {
  757. if(active.machs & (1<<id)) {
  758. mp = MACHP(id);
  759. readnum(0, bp, NUMSIZE, id, NUMSIZE);
  760. bp += NUMSIZE;
  761. readnum(0, bp, NUMSIZE, mp->cs, NUMSIZE);
  762. bp += NUMSIZE;
  763. readnum(0, bp, NUMSIZE, mp->intr, NUMSIZE);
  764. bp += NUMSIZE;
  765. readnum(0, bp, NUMSIZE, mp->syscall, NUMSIZE);
  766. bp += NUMSIZE;
  767. readnum(0, bp, NUMSIZE, mp->pfault, NUMSIZE);
  768. bp += NUMSIZE;
  769. readnum(0, bp, NUMSIZE, mp->tlbfault, NUMSIZE);
  770. bp += NUMSIZE;
  771. readnum(0, bp, NUMSIZE, mp->tlbpurge, NUMSIZE);
  772. bp += NUMSIZE;
  773. readnum(0, bp, NUMSIZE, mp->load, NUMSIZE);
  774. bp += NUMSIZE;
  775. readnum(0, bp, NUMSIZE,
  776. (mp->perf.avg_inidle*100)/mp->perf.period,
  777. NUMSIZE);
  778. bp += NUMSIZE;
  779. readnum(0, bp, NUMSIZE,
  780. (mp->perf.avg_inintr*100)/mp->perf.period,
  781. NUMSIZE);
  782. bp += NUMSIZE;
  783. *bp++ = '\n';
  784. }
  785. }
  786. if(waserror()){
  787. free(b);
  788. nexterror();
  789. }
  790. n = readstr((ulong)offset, buf, n, b);
  791. free(b);
  792. poperror();
  793. return n;
  794. case Qswap:
  795. snprint(tmp, sizeof tmp,
  796. "%lud memory\n"
  797. "%d pagesize\n"
  798. "%lud kernel\n"
  799. "%lud/%lud user\n"
  800. "%lud/%lud swap\n"
  801. "%lud/%lud kernel malloc\n"
  802. "%lud/%lud kernel draw\n",
  803. conf.npage*BY2PG,
  804. BY2PG,
  805. conf.npage-conf.upages,
  806. palloc.user-palloc.freecount, palloc.user,
  807. conf.nswap-swapalloc.free, conf.nswap,
  808. mainmem->cursize, mainmem->maxsize,
  809. imagmem->cursize, imagmem->maxsize);
  810. return readstr((ulong)offset, buf, n, tmp);
  811. case Qsysname:
  812. if(sysname == nil)
  813. return 0;
  814. return readstr((ulong)offset, buf, n, sysname);
  815. case Qrandom:
  816. return randomread(buf, n);
  817. case Qdrivers:
  818. b = malloc(READSTR);
  819. if(b == nil)
  820. error(Enomem);
  821. n = 0;
  822. for(i = 0; devtab[i] != nil; i++)
  823. n += snprint(b+n, READSTR-n, "#%C %s\n", devtab[i]->dc, devtab[i]->name);
  824. if(waserror()){
  825. free(b);
  826. nexterror();
  827. }
  828. n = readstr((ulong)offset, buf, n, b);
  829. free(b);
  830. poperror();
  831. return n;
  832. case Qzero:
  833. memset(buf, 0, n);
  834. return n;
  835. case Qosversion:
  836. snprint(tmp, sizeof tmp, "2000");
  837. n = readstr((ulong)offset, buf, n, tmp);
  838. return n;
  839. default:
  840. print("consread 0x%llux\n", c->qid.path);
  841. error(Egreg);
  842. }
  843. return -1; /* never reached */
  844. }
  845. static long
  846. conswrite(Chan *c, void *va, long n, vlong off)
  847. {
  848. char buf[256], ch;
  849. long l, bp;
  850. char *a;
  851. Mach *mp;
  852. int id, fd;
  853. Chan *swc;
  854. ulong offset;
  855. Cmdbuf *cb;
  856. Cmdtab *ct;
  857. a = va;
  858. offset = off;
  859. switch((ulong)c->qid.path){
  860. case Qcons:
  861. /*
  862. * Can't page fault in putstrn, so copy the data locally.
  863. */
  864. l = n;
  865. while(l > 0){
  866. bp = l;
  867. if(bp > sizeof buf)
  868. bp = sizeof buf;
  869. memmove(buf, a, bp);
  870. putstrn0(buf, bp, 1);
  871. a += bp;
  872. l -= bp;
  873. }
  874. break;
  875. case Qconsctl:
  876. if(n >= sizeof(buf))
  877. n = sizeof(buf)-1;
  878. strncpy(buf, a, n);
  879. buf[n] = 0;
  880. for(a = buf; a;){
  881. if(strncmp(a, "rawon", 5) == 0){
  882. kbd.raw = 1;
  883. /* clumsy hack - wake up reader */
  884. ch = 0;
  885. qwrite(kbdq, &ch, 1);
  886. } else if(strncmp(a, "rawoff", 6) == 0){
  887. kbd.raw = 0;
  888. } else if(strncmp(a, "ctlpon", 6) == 0){
  889. kbd.ctlpoff = 0;
  890. } else if(strncmp(a, "ctlpoff", 7) == 0){
  891. kbd.ctlpoff = 1;
  892. }
  893. if(a = strchr(a, ' '))
  894. a++;
  895. }
  896. break;
  897. case Qtime:
  898. if(!iseve())
  899. error(Eperm);
  900. return writetime(a, n);
  901. case Qbintime:
  902. if(!iseve())
  903. error(Eperm);
  904. return writebintime(a, n);
  905. case Qhostowner:
  906. return hostownerwrite(a, n);
  907. case Qhostdomain:
  908. return hostdomainwrite(a, n);
  909. case Quser:
  910. return userwrite(a, n);
  911. case Qnull:
  912. break;
  913. case Qreboot:
  914. if(!iseve())
  915. error(Eperm);
  916. cb = parsecmd(a, n);
  917. if(waserror()) {
  918. free(cb);
  919. nexterror();
  920. }
  921. ct = lookupcmd(cb, rebootmsg, nelem(rebootmsg));
  922. switch(ct->index) {
  923. case CMhalt:
  924. reboot(nil, 0, 0);
  925. break;
  926. case CMreboot:
  927. rebootcmd(cb->nf-1, cb->f+1);
  928. break;
  929. case CMpanic:
  930. *(ulong*)0=0;
  931. panic("/dev/reboot");
  932. }
  933. poperror();
  934. free(cb);
  935. break;
  936. case Qsysstat:
  937. for(id = 0; id < 32; id++) {
  938. if(active.machs & (1<<id)) {
  939. mp = MACHP(id);
  940. mp->cs = 0;
  941. mp->intr = 0;
  942. mp->syscall = 0;
  943. mp->pfault = 0;
  944. mp->tlbfault = 0;
  945. mp->tlbpurge = 0;
  946. }
  947. }
  948. break;
  949. case Qswap:
  950. if(n >= sizeof buf)
  951. error(Egreg);
  952. memmove(buf, va, n); /* so we can NUL-terminate */
  953. buf[n] = 0;
  954. /* start a pager if not already started */
  955. if(strncmp(buf, "start", 5) == 0){
  956. kickpager();
  957. break;
  958. }
  959. if(!iseve())
  960. error(Eperm);
  961. if(buf[0]<'0' || '9'<buf[0])
  962. error(Ebadarg);
  963. fd = strtoul(buf, 0, 0);
  964. swc = fdtochan(fd, -1, 1, 1);
  965. setswapchan(swc);
  966. break;
  967. case Qsysname:
  968. if(offset != 0)
  969. error(Ebadarg);
  970. if(n <= 0 || n >= sizeof buf)
  971. error(Ebadarg);
  972. strncpy(buf, a, n);
  973. buf[n] = 0;
  974. if(buf[n-1] == '\n')
  975. buf[n-1] = 0;
  976. kstrdup(&sysname, buf);
  977. break;
  978. default:
  979. print("conswrite: 0x%llux\n", c->qid.path);
  980. error(Egreg);
  981. }
  982. return n;
  983. }
  984. Dev consdevtab = {
  985. 'c',
  986. "cons",
  987. devreset,
  988. consinit,
  989. devshutdown,
  990. consattach,
  991. conswalk,
  992. consstat,
  993. consopen,
  994. devcreate,
  995. consclose,
  996. consread,
  997. devbread,
  998. conswrite,
  999. devbwrite,
  1000. devremove,
  1001. devwstat,
  1002. };
  1003. static ulong randn;
  1004. static void
  1005. seedrand(void)
  1006. {
  1007. if(!waserror()){
  1008. randomread((void*)&randn, sizeof(randn));
  1009. poperror();
  1010. }
  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. }