devcons.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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 int nconsdevs = 1;
  37. static Consdev consdevs[Nconsdevs] = /* keep this order */
  38. {
  39. {nil, nil, kmesgputs, 0}, /* kmesg */
  40. };
  41. static struct
  42. {
  43. QLock QLock;
  44. int raw; /* true if we shouldn't process input */
  45. Ref ctl; /* number of opens to the control file */
  46. int x; /* index into line */
  47. char line[1024]; /* current input line */
  48. int count;
  49. int ctlpoff;
  50. /* a place to save up characters at interrupt time before dumping them in the queue */
  51. Lock lockputc;
  52. char istage[1024];
  53. char *iw;
  54. char *ir;
  55. char *ie;
  56. } kbd = {
  57. .iw = kbd.istage,
  58. .ir = kbd.istage,
  59. .ie = kbd.istage + sizeof(kbd.istage),
  60. };
  61. int panicking;
  62. char *sysname;
  63. int64_t fasthz;
  64. static void seedrand(void);
  65. static int readtime(uint32_t, char*, int);
  66. static int readbintime(char*, int);
  67. static int writetime(char*, int);
  68. static int writebintime(char*, int);
  69. enum
  70. {
  71. CMhalt,
  72. CMreboot,
  73. CMpanic,
  74. };
  75. Cmdtab rebootmsg[] =
  76. {
  77. CMhalt, "halt", 1,
  78. CMreboot, "reboot", 0,
  79. CMpanic, "panic", 0,
  80. };
  81. /*
  82. * Log console output so it can be retrieved via /dev/kmesg.
  83. * This is good for catching boot-time messages after the fact.
  84. */
  85. struct {
  86. Lock lk;
  87. char buf[1048576];
  88. uint n;
  89. } kmesg;
  90. static void
  91. kmesgputs(char *str, int n)
  92. {
  93. uint nn, d;
  94. ilock(&kmesg.lk);
  95. /* take the tail of huge writes */
  96. if(n > sizeof kmesg.buf){
  97. d = n - sizeof kmesg.buf;
  98. str += d;
  99. n -= d;
  100. }
  101. /* slide the buffer down to make room */
  102. nn = kmesg.n;
  103. if(nn + n >= sizeof kmesg.buf){
  104. d = nn + n - sizeof kmesg.buf;
  105. if(d)
  106. memmove(kmesg.buf, kmesg.buf+d, sizeof kmesg.buf-d);
  107. nn -= d;
  108. }
  109. /* copy the data in */
  110. memmove(kmesg.buf+nn, str, n);
  111. nn += n;
  112. kmesg.n = nn;
  113. iunlock(&kmesg.lk);
  114. }
  115. /*
  116. * Print a string on the console. Convert \n to \r\n for serial
  117. * line consoles. Locking of the queues is left up to the screen
  118. * or uart code. Multi-line messages to serial consoles may get
  119. * interspersed with other messages.
  120. */
  121. static void
  122. putstrn0(char *str, int n, int usewrite)
  123. {
  124. kmesgputs(str, n);
  125. if(consputs != nil)
  126. consputs(str, n);
  127. if(consuartputs != nil)
  128. consuartputs(str, n);
  129. }
  130. void
  131. putstrn(char *str, int n)
  132. {
  133. putstrn0(str, n, 0);
  134. }
  135. int
  136. print(char *fmt, ...)
  137. {
  138. int n;
  139. va_list arg;
  140. char buf[PRINTSIZE];
  141. va_start(arg, fmt);
  142. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  143. va_end(arg);
  144. putstrn(buf, n);
  145. return n;
  146. }
  147. int
  148. kmprint(char *fmt, ...)
  149. {
  150. int n;
  151. va_list arg;
  152. char buf[PRINTSIZE];
  153. va_start(arg, fmt);
  154. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  155. va_end(arg);
  156. kmesgputs(buf, n);
  157. return n;
  158. }
  159. /*
  160. * Want to interlock iprints to avoid interlaced output on
  161. * multiprocessor, but don't want to deadlock if one processor
  162. * dies during print and another has something important to say.
  163. * Make a good faith effort.
  164. */
  165. static Lock iprintlock;
  166. static int
  167. iprintcanlock(Lock *l)
  168. {
  169. int i;
  170. for(i=0; i<1000; i++){
  171. if(canlock(l))
  172. return 1;
  173. if(l->m == machp())
  174. return 0;
  175. microdelay(100);
  176. }
  177. return 0;
  178. }
  179. int
  180. iprint(char *fmt, ...)
  181. {
  182. Mpl pl;
  183. int i, n, locked;
  184. va_list arg;
  185. char buf[PRINTSIZE];
  186. pl = splhi();
  187. va_start(arg, fmt);
  188. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  189. va_end(arg);
  190. locked = iprintcanlock(&iprintlock);
  191. for(i = 0; i < nconsdevs; i++)
  192. if((consdevs[i].flags&Ciprint) != 0){
  193. if(consdevs[i].q != nil)
  194. qiwrite(consdevs[i].q, buf, n);
  195. else
  196. consdevs[i].fn(buf, n);
  197. }
  198. if(locked)
  199. unlock(&iprintlock);
  200. splx(pl);
  201. return n;
  202. }
  203. #pragma profile 0
  204. void
  205. panic(char *fmt, ...)
  206. {
  207. int n;
  208. Mpl pl;
  209. va_list arg;
  210. char buf[PRINTSIZE];
  211. consdevs[1].q = nil; /* don't try to write to /dev/kprint */
  212. if(panicking)
  213. for(;;);
  214. panicking = 1;
  215. pl = splhi();
  216. seprint(buf, buf+sizeof buf, "panic: cpu%d: ", machp()->machno);
  217. va_start(arg, fmt);
  218. n = vseprint(buf+strlen(buf), buf+sizeof(buf), fmt, arg) - buf;
  219. va_end(arg);
  220. iprint("%s\n", buf);
  221. if(consdebug)
  222. (*consdebug)();
  223. splx(pl);
  224. //prflush();
  225. buf[n] = '\n';
  226. putstrn(buf, n+1);
  227. //dumpstack();
  228. delay(1000); /* give time to consoles */
  229. die("wait forever");
  230. exit(1);
  231. }
  232. #pragma profile 1
  233. /* libmp at least contains a few calls to sysfatal; simulate with panic */
  234. void
  235. sysfatal(char *fmt, ...)
  236. {
  237. char err[256];
  238. va_list arg;
  239. va_start(arg, fmt);
  240. vseprint(err, err + sizeof err, fmt, arg);
  241. va_end(arg);
  242. panic("sysfatal: %s", err);
  243. }
  244. void
  245. _assert(char *fmt)
  246. {
  247. panic("assert failed at %#p: %s", getcallerpc(&fmt), fmt);
  248. }
  249. int
  250. pprint(char *fmt, ...)
  251. {
  252. Proc *up = externup();
  253. int n;
  254. Chan *c;
  255. va_list arg;
  256. char buf[2*PRINTSIZE];
  257. if(up == nil || up->fgrp == nil)
  258. return 0;
  259. c = up->fgrp->fd[2];
  260. if(c==0 || (c->mode!=OWRITE && c->mode!=ORDWR))
  261. return 0;
  262. n = snprint(buf, sizeof buf, "%s %d: ", up->text, up->pid);
  263. va_start(arg, fmt);
  264. n = vseprint(buf+n, buf+sizeof(buf), fmt, arg) - buf;
  265. va_end(arg);
  266. if(waserror())
  267. return 0;
  268. c->dev->write(c, buf, n, c->offset);
  269. poperror();
  270. lock(&c->r.l);
  271. c->offset += n;
  272. unlock(&c->r.l);
  273. return n;
  274. }
  275. /*
  276. * Put character, possibly a rune, into read queue at interrupt time.
  277. * Called at interrupt time to process a character.
  278. */
  279. int
  280. kbdputc(Queue *q, int ch)
  281. {
  282. int i, n;
  283. char buf[3];
  284. Rune r;
  285. char *next;
  286. if(kbd.ir == nil)
  287. return 0; /* in case we're not inited yet */
  288. ilock(&kbd.lockputc); /* just a mutex */
  289. r = ch;
  290. n = runetochar(buf, &r);
  291. for(i = 0; i < n; i++){
  292. next = kbd.iw+1;
  293. if(next >= kbd.ie)
  294. next = kbd.istage;
  295. if(next == kbd.ir)
  296. break;
  297. *kbd.iw = buf[i];
  298. kbd.iw = next;
  299. }
  300. iunlock(&kbd.lockputc);
  301. return 0;
  302. }
  303. enum{
  304. Qdir,
  305. Qbintime,
  306. Qcons,
  307. Qconsctl,
  308. Qcputime,
  309. Qdrivers,
  310. Qkmesg,
  311. Qkprint,
  312. Qhostdomain,
  313. Qhostowner,
  314. Qnull,
  315. Qosversion,
  316. Qpgrpid,
  317. Qpid,
  318. Qppid,
  319. Qrandom,
  320. Qurandom,
  321. Qreboot,
  322. Qswap,
  323. Qsysname,
  324. Qsysstat,
  325. Qtime,
  326. Quser,
  327. Qzero,
  328. Qsyscall,
  329. Qdebug,
  330. };
  331. enum
  332. {
  333. VLNUMSIZE= 22,
  334. };
  335. static Dirtab consdir[]={
  336. ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
  337. "bintime", {Qbintime}, 24, 0664,
  338. "cons", {Qcons}, 0, 0660,
  339. "consctl", {Qconsctl}, 0, 0220,
  340. "cputime", {Qcputime}, 6*NUMSIZE, 0444,
  341. "drivers", {Qdrivers}, 0, 0444,
  342. "hostdomain", {Qhostdomain}, DOMLEN, 0664,
  343. "hostowner", {Qhostowner}, 0, 0664,
  344. "kmesg", {Qkmesg}, 0, 0440,
  345. "kprint", {Qkprint, 0, QTEXCL}, 0, DMEXCL|0440,
  346. "null", {Qnull}, 0, 0666,
  347. "osversion", {Qosversion}, 0, 0444,
  348. "pgrpid", {Qpgrpid}, NUMSIZE, 0444,
  349. "pid", {Qpid}, NUMSIZE, 0444,
  350. "ppid", {Qppid}, NUMSIZE, 0444,
  351. "random", {Qrandom}, 0, 0444,
  352. "urandom", {Qurandom}, 0, 0444,
  353. "reboot", {Qreboot}, 0, 0664,
  354. "swap", {Qswap}, 0, 0664,
  355. "sysname", {Qsysname}, 0, 0664,
  356. "sysstat", {Qsysstat}, 0, 0666,
  357. "time", {Qtime}, NUMSIZE+3*VLNUMSIZE, 0664,
  358. "user", {Quser}, 0, 0666,
  359. "zero", {Qzero}, 0, 0444,
  360. "syscall", {Qsyscall}, 0, 0666,
  361. "debug", {Qdebug}, 0, 0666,
  362. };
  363. int
  364. readnum(uint32_t off, char *buf, uint32_t n, uint32_t val, int size)
  365. {
  366. char tmp[64];
  367. snprint(tmp, sizeof(tmp), "%*lu", size-1, val);
  368. tmp[size-1] = ' ';
  369. if(off >= size)
  370. return 0;
  371. if(off+n > size)
  372. n = size-off;
  373. memmove(buf, tmp+off, n);
  374. return n;
  375. }
  376. int32_t
  377. readmem(int32_t offset, void *buf, int32_t n, void *v, int32_t size)
  378. {
  379. if(offset >= size)
  380. return 0;
  381. if(offset+n > size)
  382. n = size-offset;
  383. memmove(buf, v+offset, n);
  384. return n;
  385. }
  386. int32_t
  387. readstr(int32_t offset, char *buf, int32_t n, char *str)
  388. {
  389. int32_t size;
  390. size = strlen(str);
  391. if(offset >= size)
  392. return 0;
  393. if(offset+n > size)
  394. n = size-offset;
  395. memmove(buf, str+offset, n);
  396. return n;
  397. }
  398. static void
  399. consinit(void)
  400. {
  401. todinit();
  402. }
  403. static Chan*
  404. consattach(char *spec)
  405. {
  406. return devattach('c', spec);
  407. }
  408. static Walkqid*
  409. conswalk(Chan *c, Chan *nc, char **name, int nname)
  410. {
  411. return devwalk(c, nc, name,nname, consdir, nelem(consdir), devgen);
  412. }
  413. static int32_t
  414. consstat(Chan *c, uint8_t *dp, int32_t n)
  415. {
  416. return devstat(c, dp, n, consdir, nelem(consdir), devgen);
  417. }
  418. static Chan*
  419. consopen(Chan *c, int omode)
  420. {
  421. c->aux = nil;
  422. c = devopen(c, omode, consdir, nelem(consdir), devgen);
  423. switch((uint32_t)c->qid.path){
  424. case Qconsctl:
  425. incref(&kbd.ctl);
  426. break;
  427. case Qkprint:
  428. error(Egreg);
  429. }
  430. return c;
  431. }
  432. static void
  433. consclose(Chan *c)
  434. {
  435. }
  436. static int32_t
  437. consread(Chan *c, void *buf, int32_t n, int64_t off)
  438. {
  439. Proc *up = externup();
  440. uint32_t l;
  441. Mach *mp;
  442. char *b, *bp, *s, *e;
  443. char tmp[512]; /* Qswap is 381 bytes at clu */
  444. int i, k, id;
  445. int32_t offset;
  446. if(n <= 0)
  447. return n;
  448. offset = off;
  449. switch((uint32_t)c->qid.path){
  450. case Qdir:
  451. return devdirread(c, buf, n, consdir, nelem(consdir), devgen);
  452. case Qcons:
  453. error(Egreg);
  454. case Qcputime:
  455. k = offset;
  456. if(k >= 6*NUMSIZE)
  457. return 0;
  458. if(k+n > 6*NUMSIZE)
  459. n = 6*NUMSIZE - k;
  460. /* easiest to format in a separate buffer and copy out */
  461. for(i=0; i<6 && NUMSIZE*i<k+n; i++){
  462. l = up->time[i];
  463. if(i == TReal)
  464. l = sys->ticks - l;
  465. l = TK2MS(l);
  466. readnum(0, tmp+NUMSIZE*i, NUMSIZE, l, NUMSIZE);
  467. }
  468. memmove(buf, tmp+k, n);
  469. return n;
  470. case Qkmesg:
  471. /*
  472. * This is unlocked to avoid tying up a process
  473. * that's writing to the buffer. kmesg.n never
  474. * gets smaller, so worst case the reader will
  475. * see a slurred buffer.
  476. */
  477. if(off >= kmesg.n)
  478. n = 0;
  479. else{
  480. if(off+n > kmesg.n)
  481. n = kmesg.n - off;
  482. memmove(buf, kmesg.buf+off, n);
  483. }
  484. return n;
  485. case Qkprint:
  486. error(Egreg);
  487. case Qpgrpid:
  488. return readnum(offset, buf, n, up->pgrp->pgrpid, NUMSIZE);
  489. case Qpid:
  490. return readnum(offset, buf, n, up->pid, NUMSIZE);
  491. case Qppid:
  492. return readnum(offset, buf, n, up->parentpid, NUMSIZE);
  493. case Qtime:
  494. return readtime(offset, buf, n);
  495. case Qbintime:
  496. return readbintime(buf, n);
  497. case Qhostowner:
  498. return readstr(offset, buf, n, eve);
  499. case Qhostdomain:
  500. return readstr(offset, buf, n, hostdomain);
  501. case Quser:
  502. return readstr(offset, buf, n, up->user);
  503. case Qnull:
  504. return 0;
  505. case Qsysstat:
  506. n = MACHMAX*(NUMSIZE*11+2+1);
  507. b = smalloc(n + 1); /* +1 for NUL */
  508. bp = b;
  509. e = bp + n;
  510. for(id = 0; id < MACHMAX; id++)
  511. if((mp = sys->machptr[id]) != nil && mp->online){
  512. readnum(0, bp, NUMSIZE, mp->machno, NUMSIZE);
  513. bp += NUMSIZE;
  514. readnum(0, bp, NUMSIZE, mp->cs, NUMSIZE);
  515. bp += NUMSIZE;
  516. readnum(0, bp, NUMSIZE, mp->intr, NUMSIZE);
  517. bp += NUMSIZE;
  518. readnum(0, bp, NUMSIZE, mp->syscall, NUMSIZE);
  519. bp += NUMSIZE;
  520. readnum(0, bp, NUMSIZE, mp->pfault, NUMSIZE);
  521. bp += NUMSIZE;
  522. readnum(0, bp, NUMSIZE, mp->tlbfault, NUMSIZE);
  523. bp += NUMSIZE;
  524. readnum(0, bp, NUMSIZE, mp->tlbpurge, NUMSIZE);
  525. bp += NUMSIZE;
  526. readnum(0, bp, NUMSIZE, sys->load, NUMSIZE);
  527. bp += NUMSIZE;
  528. readnum(0, bp, NUMSIZE,
  529. (mp->perf.avg_inidle*100)/mp->perf.period,
  530. NUMSIZE);
  531. bp += NUMSIZE;
  532. readnum(0, bp, NUMSIZE,
  533. (mp->perf.avg_inintr*100)/mp->perf.period,
  534. NUMSIZE);
  535. bp += NUMSIZE;
  536. readnum(0, bp, NUMSIZE, 0, NUMSIZE); /* sched # */
  537. bp += NUMSIZE;
  538. bp = strecpy(bp, e, rolename[mp->NIX.nixtype]);
  539. *bp++ = '\n';
  540. }
  541. if(waserror()){
  542. free(b);
  543. nexterror();
  544. }
  545. n = readstr(offset, buf, n, b);
  546. free(b);
  547. poperror();
  548. return n;
  549. case Qswap:
  550. tmp[0] = 0;
  551. s = seprintpagestats(tmp, tmp + sizeof tmp);
  552. s = seprintphysstats(s, tmp + sizeof tmp);
  553. b = buf;
  554. l = s - tmp;
  555. i = readstr(offset, b, l, tmp);
  556. b += i;
  557. n -= i;
  558. if(offset > l)
  559. offset -= l;
  560. else
  561. offset = 0;
  562. return i + mallocreadsummary(c, b, n, offset);
  563. case Qsysname:
  564. if(sysname == nil)
  565. return 0;
  566. return readstr(offset, buf, n, sysname);
  567. case Qrandom:
  568. return randomread(buf, n);
  569. case Qurandom:
  570. return urandomread(buf, n);
  571. case Qdrivers:
  572. return devtabread(c, buf, n, off);
  573. case Qzero:
  574. memset(buf, 0, n);
  575. return n;
  576. case Qosversion:
  577. snprint(tmp, sizeof tmp, "2000");
  578. n = readstr(offset, buf, n, tmp);
  579. return n;
  580. case Qdebug:
  581. s = seprint(tmp, tmp + sizeof tmp, "locks %lu\n", lockstats.locks);
  582. s = seprint(s, tmp + sizeof tmp, "glare %lu\n", lockstats.glare);
  583. s = seprint(s, tmp + sizeof tmp, "inglare %lu\n", lockstats.inglare);
  584. s = seprint(s, tmp + sizeof tmp, "qlock %lu\n", qlockstats.qlock);
  585. seprint(s, tmp + sizeof tmp, "qlockq %lu\n", qlockstats.qlockq);
  586. return readstr(offset, buf, n, tmp);
  587. break;
  588. case Qsyscall:
  589. snprint(tmp, sizeof tmp, "%s", printallsyscalls ? "on" : "off");
  590. return readstr(offset, buf, n, tmp);
  591. break;
  592. default:
  593. print("consread %#llx\n", c->qid.path);
  594. error(Egreg);
  595. }
  596. return -1; /* never reached */
  597. }
  598. static int32_t
  599. conswrite(Chan *c, void *va, int32_t n, int64_t off)
  600. {
  601. Proc *up = externup();
  602. char buf[256];
  603. int32_t l, bp;
  604. char *a;
  605. Mach *mp;
  606. int i;
  607. uint32_t offset;
  608. Cmdbuf *cb;
  609. Cmdtab *ct;
  610. a = va;
  611. offset = off;
  612. extern int printallsyscalls;
  613. switch((uint32_t)c->qid.path){
  614. case Qcons:
  615. /*
  616. * Can't page fault in putstrn, so copy the data locally.
  617. */
  618. l = n;
  619. while(l > 0){
  620. bp = l;
  621. if(bp > sizeof buf)
  622. bp = sizeof buf;
  623. memmove(buf, a, bp);
  624. putstrn0(buf, bp, 1);
  625. a += bp;
  626. l -= bp;
  627. }
  628. break;
  629. case Qconsctl:
  630. print("consctl\n");
  631. if(n >= sizeof(buf))
  632. n = sizeof(buf)-1;
  633. strncpy(buf, a, n);
  634. buf[n] = 0;
  635. for(a = buf; a;){
  636. if(strncmp(a, "sys", 3) == 0) {
  637. printallsyscalls = ! printallsyscalls;
  638. print("%sracing syscalls\n", printallsyscalls ? "T" : "Not t");
  639. }
  640. if(a = strchr(a, ' '))
  641. a++;
  642. }
  643. break;
  644. case Qtime:
  645. if(!iseve())
  646. error(Eperm);
  647. return writetime(a, n);
  648. case Qbintime:
  649. if(!iseve())
  650. error(Eperm);
  651. return writebintime(a, n);
  652. case Qhostowner:
  653. return hostownerwrite(a, n);
  654. case Qhostdomain:
  655. return hostdomainwrite(a, n);
  656. case Quser:
  657. return userwrite(a, n);
  658. case Qnull:
  659. break;
  660. case Qreboot:
  661. if(!iseve())
  662. error(Eperm);
  663. cb = parsecmd(a, n);
  664. if(waserror()) {
  665. free(cb);
  666. nexterror();
  667. }
  668. ct = lookupcmd(cb, rebootmsg, nelem(rebootmsg));
  669. switch(ct->index) {
  670. case CMhalt:
  671. reboot(nil, 0, 0);
  672. break;
  673. case CMreboot:
  674. rebootcmd(cb->nf-1, cb->f+1);
  675. break;
  676. case CMpanic:
  677. *(volatile uint32_t*)0=0;
  678. panic("/dev/reboot");
  679. }
  680. poperror();
  681. free(cb);
  682. break;
  683. case Qsysstat:
  684. for(i = 0; i < MACHMAX; i++)
  685. if((mp = sys->machptr[i]) != nil && mp->online){
  686. mp = sys->machptr[i];
  687. mp->cs = 0;
  688. mp->intr = 0;
  689. mp->syscall = 0;
  690. mp->pfault = 0;
  691. mp->tlbfault = 0; /* not updated */
  692. mp->tlbpurge = 0; /* # mmuflushtlb */
  693. }
  694. break;
  695. case Qswap:
  696. if(n >= sizeof buf)
  697. error(Egreg);
  698. memmove(buf, va, n); /* so we can NUL-terminate */
  699. buf[n] = 0;
  700. if(!iseve())
  701. error(Eperm);
  702. if(buf[0]<'0' || '9'<buf[0])
  703. error(Ebadarg);
  704. if(strncmp(buf, "start", 5) == 0){
  705. print("request to start pager ignored\n");
  706. break;
  707. }
  708. break;
  709. case Qsysname:
  710. if(offset != 0)
  711. error(Ebadarg);
  712. if(n <= 0 || n >= sizeof buf)
  713. error(Ebadarg);
  714. strncpy(buf, a, n);
  715. buf[n] = 0;
  716. if(buf[n-1] == '\n')
  717. buf[n-1] = 0;
  718. kstrdup(&sysname, buf);
  719. break;
  720. case Qdebug:
  721. if(n >= sizeof(buf))
  722. n = sizeof(buf)-1;
  723. strncpy(buf, a, n);
  724. buf[n] = 0;
  725. if(n > 0 && buf[n-1] == '\n')
  726. buf[n-1] = 0;
  727. error(Ebadctl);
  728. break;
  729. case Qsyscall:
  730. if(n >= sizeof(buf))
  731. n = sizeof(buf)-1;
  732. strncpy(buf, a, n);
  733. buf[n] = 0;
  734. if(n > 0 && buf[n-1] == '\n')
  735. buf[n-1] = 0;
  736. // Doing strncmp right is just painful and overkill here.
  737. if (buf[0] == 'o') {
  738. if (buf[1] == 'n' && buf[2] == 0) {
  739. printallsyscalls = 1;
  740. break;
  741. }
  742. if (buf[1] == 'f' && buf[2] == 'f' && buf[3] == 0) {
  743. printallsyscalls = 0;
  744. break;
  745. }
  746. }
  747. error("#c/syscall: can only write on or off");
  748. break;
  749. default:
  750. print("conswrite: %#llx\n", c->qid.path);
  751. error(Egreg);
  752. }
  753. return n;
  754. }
  755. Dev consdevtab = {
  756. .dc = 'c',
  757. .name = "cons",
  758. .reset = devreset,
  759. .init = consinit,
  760. .shutdown = devshutdown,
  761. .attach = consattach,
  762. .walk = conswalk,
  763. .stat = consstat,
  764. .open = consopen,
  765. .create = devcreate,
  766. .close = consclose,
  767. .read = consread,
  768. .bread = devbread,
  769. .write = conswrite,
  770. .bwrite = devbwrite,
  771. .remove = devremove,
  772. .wstat = devwstat,
  773. };
  774. static uint32_t randn;
  775. static void
  776. seedrand(void)
  777. {
  778. Proc *up = externup();
  779. if(!waserror()){
  780. randomread((void*)&randn, sizeof(randn));
  781. poperror();
  782. }
  783. }
  784. int
  785. nrand(int n)
  786. {
  787. if(randn == 0)
  788. seedrand();
  789. randn = randn*1103515245 + 12345 + sys->ticks;
  790. return (randn>>16) % n;
  791. }
  792. int
  793. rand(void)
  794. {
  795. nrand(1);
  796. return randn;
  797. }
  798. static uint64_t uvorder = 0x0001020304050607ULL;
  799. static uint8_t*
  800. le2int64_t(int64_t *to, uint8_t *f)
  801. {
  802. uint8_t *t, *o;
  803. int i;
  804. t = (uint8_t*)to;
  805. o = (uint8_t*)&uvorder;
  806. for(i = 0; i < sizeof(int64_t); i++)
  807. t[o[i]] = f[i];
  808. return f+sizeof(int64_t);
  809. }
  810. static uint8_t*
  811. int64_t2le(uint8_t *t, int64_t from)
  812. {
  813. uint8_t *f, *o;
  814. int i;
  815. f = (uint8_t*)&from;
  816. o = (uint8_t*)&uvorder;
  817. for(i = 0; i < sizeof(int64_t); i++)
  818. t[i] = f[o[i]];
  819. return t+sizeof(int64_t);
  820. }
  821. static int32_t order = 0x00010203;
  822. static uint8_t*
  823. le2long(int32_t *to, uint8_t *f)
  824. {
  825. uint8_t *t, *o;
  826. int i;
  827. t = (uint8_t*)to;
  828. o = (uint8_t*)&order;
  829. for(i = 0; i < sizeof(int32_t); i++)
  830. t[o[i]] = f[i];
  831. return f+sizeof(int32_t);
  832. }
  833. #if 0
  834. static uint8_t*
  835. long2le(uint8_t *t, int32_t from)
  836. {
  837. uint8_t *f, *o;
  838. int i;
  839. f = (uint8_t*)&from;
  840. o = (uint8_t*)&order;
  841. for(i = 0; i < sizeof(int32_t); i++)
  842. t[i] = f[o[i]];
  843. return t+sizeof(int32_t);
  844. }
  845. #endif
  846. char *Ebadtimectl = "bad time control";
  847. /*
  848. * like the old #c/time but with added info. Return
  849. *
  850. * secs nanosecs fastticks fasthz
  851. */
  852. static int
  853. readtime(uint32_t off, char *buf, int n)
  854. {
  855. int64_t nsec, ticks;
  856. int32_t sec;
  857. char str[7*NUMSIZE];
  858. nsec = todget(&ticks);
  859. if(fasthz == 0LL)
  860. fastticks((uint64_t*)&fasthz);
  861. sec = nsec/1000000000ULL;
  862. snprint(str, sizeof(str), "%*lu %*llu %*llu %*llu ",
  863. NUMSIZE-1, sec,
  864. VLNUMSIZE-1, nsec,
  865. VLNUMSIZE-1, ticks,
  866. VLNUMSIZE-1, fasthz);
  867. return readstr(off, buf, n, str);
  868. }
  869. /*
  870. * set the time in seconds
  871. */
  872. static int
  873. writetime(char *buf, int n)
  874. {
  875. char b[13];
  876. int32_t i;
  877. int64_t now;
  878. if(n >= sizeof(b))
  879. error(Ebadtimectl);
  880. strncpy(b, buf, n);
  881. b[n] = 0;
  882. i = strtol(b, 0, 0);
  883. if(i <= 0)
  884. error(Ebadtimectl);
  885. now = i*1000000000LL;
  886. todset(now, 0, 0);
  887. return n;
  888. }
  889. /*
  890. * read binary time info. all numbers are little endian.
  891. * ticks and nsec are syncronized.
  892. */
  893. static int
  894. readbintime(char *buf, int n)
  895. {
  896. int i;
  897. int64_t nsec, ticks;
  898. uint8_t *b = (uint8_t*)buf;
  899. i = 0;
  900. if(fasthz == 0LL)
  901. fastticks((uint64_t*)&fasthz);
  902. nsec = todget(&ticks);
  903. if(n >= 3*sizeof(uint64_t)){
  904. int64_t2le(b+2*sizeof(uint64_t), fasthz);
  905. i += sizeof(uint64_t);
  906. }
  907. if(n >= 2*sizeof(uint64_t)){
  908. int64_t2le(b+sizeof(uint64_t), ticks);
  909. i += sizeof(uint64_t);
  910. }
  911. if(n >= 8){
  912. int64_t2le(b, nsec);
  913. i += sizeof(int64_t);
  914. }
  915. return i;
  916. }
  917. /*
  918. * set any of the following
  919. * - time in nsec
  920. * - nsec trim applied over some seconds
  921. * - clock frequency
  922. */
  923. static int
  924. writebintime(char *buf, int n)
  925. {
  926. uint8_t *p;
  927. int64_t delta;
  928. int32_t period;
  929. n--;
  930. p = (uint8_t*)buf + 1;
  931. switch(*buf){
  932. case 'n':
  933. if(n < sizeof(int64_t))
  934. error(Ebadtimectl);
  935. le2int64_t(&delta, p);
  936. todset(delta, 0, 0);
  937. break;
  938. case 'd':
  939. if(n < sizeof(int64_t)+sizeof(int32_t))
  940. error(Ebadtimectl);
  941. p = le2int64_t(&delta, p);
  942. le2long(&period, p);
  943. todset(-1, delta, period);
  944. break;
  945. case 'f':
  946. if(n < sizeof(uint64_t))
  947. error(Ebadtimectl);
  948. le2int64_t(&fasthz, p);
  949. todsetfreq(fasthz);
  950. break;
  951. }
  952. return n;
  953. }