devcons.c 22 KB

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