devcons.c 26 KB

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