devcons.c 20 KB

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