devcons.c 23 KB

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