devcons.c 25 KB

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