devcons.c 25 KB

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