devcons.c 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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[16384];
  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), "%*lud", 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. readstr(int32_t offset, char *buf, int32_t n, char *str)
  366. {
  367. int32_t size;
  368. size = strlen(str);
  369. if(offset >= size)
  370. return 0;
  371. if(offset+n > size)
  372. n = size-offset;
  373. memmove(buf, str+offset, n);
  374. return n;
  375. }
  376. static void
  377. consinit(void)
  378. {
  379. todinit();
  380. }
  381. static Chan*
  382. consattach(char *spec)
  383. {
  384. return devattach('c', spec);
  385. }
  386. static Walkqid*
  387. conswalk(Chan *c, Chan *nc, char **name, int nname)
  388. {
  389. return devwalk(c, nc, name,nname, consdir, nelem(consdir), devgen);
  390. }
  391. static int32_t
  392. consstat(Chan *c, uint8_t *dp, int32_t n)
  393. {
  394. return devstat(c, dp, n, consdir, nelem(consdir), devgen);
  395. }
  396. static Chan*
  397. consopen(Chan *c, int omode)
  398. {
  399. c->aux = nil;
  400. c = devopen(c, omode, consdir, nelem(consdir), devgen);
  401. switch((uint32_t)c->qid.path){
  402. case Qconsctl:
  403. incref(&kbd.ctl);
  404. break;
  405. case Qkprint:
  406. error(Egreg);
  407. }
  408. return c;
  409. }
  410. static void
  411. consclose(Chan *c)
  412. {
  413. }
  414. static int32_t
  415. consread(Chan *c, void *buf, int32_t n, int64_t off)
  416. {
  417. Proc *up = externup();
  418. uint32_t l;
  419. Mach *mp;
  420. char *b, *bp, *s, *e;
  421. char tmp[512]; /* Qswap is 381 bytes at clu */
  422. int i, k, id;
  423. int32_t offset;
  424. if(n <= 0)
  425. return n;
  426. offset = off;
  427. switch((uint32_t)c->qid.path){
  428. case Qdir:
  429. return devdirread(c, buf, n, consdir, nelem(consdir), devgen);
  430. case Qcons:
  431. error(Egreg);
  432. case Qcputime:
  433. k = offset;
  434. if(k >= 6*NUMSIZE)
  435. return 0;
  436. if(k+n > 6*NUMSIZE)
  437. n = 6*NUMSIZE - k;
  438. /* easiest to format in a separate buffer and copy out */
  439. for(i=0; i<6 && NUMSIZE*i<k+n; i++){
  440. l = up->time[i];
  441. if(i == TReal)
  442. l = sys->ticks - l;
  443. l = TK2MS(l);
  444. readnum(0, tmp+NUMSIZE*i, NUMSIZE, l, NUMSIZE);
  445. }
  446. memmove(buf, tmp+k, n);
  447. return n;
  448. case Qkmesg:
  449. /*
  450. * This is unlocked to avoid tying up a process
  451. * that's writing to the buffer. kmesg.n never
  452. * gets smaller, so worst case the reader will
  453. * see a slurred buffer.
  454. */
  455. if(off >= kmesg.n)
  456. n = 0;
  457. else{
  458. if(off+n > kmesg.n)
  459. n = kmesg.n - off;
  460. memmove(buf, kmesg.buf+off, n);
  461. }
  462. return n;
  463. case Qkprint:
  464. error(Egreg);
  465. case Qpgrpid:
  466. return readnum(offset, buf, n, up->pgrp->pgrpid, NUMSIZE);
  467. case Qpid:
  468. return readnum(offset, buf, n, up->pid, NUMSIZE);
  469. case Qppid:
  470. return readnum(offset, buf, n, up->parentpid, NUMSIZE);
  471. case Qtime:
  472. return readtime(offset, buf, n);
  473. case Qbintime:
  474. return readbintime(buf, n);
  475. case Qhostowner:
  476. return readstr(offset, buf, n, eve);
  477. case Qhostdomain:
  478. return readstr(offset, buf, n, hostdomain);
  479. case Quser:
  480. return readstr(offset, buf, n, up->user);
  481. case Qnull:
  482. return 0;
  483. case Qsysstat:
  484. n = MACHMAX*(NUMSIZE*11+2+1);
  485. b = smalloc(n + 1); /* +1 for NUL */
  486. bp = b;
  487. e = bp + n;
  488. for(id = 0; id < MACHMAX; id++)
  489. if((mp = sys->machptr[id]) != nil && mp->online){
  490. readnum(0, bp, NUMSIZE, mp->machno, NUMSIZE);
  491. bp += NUMSIZE;
  492. readnum(0, bp, NUMSIZE, mp->cs, NUMSIZE);
  493. bp += NUMSIZE;
  494. readnum(0, bp, NUMSIZE, mp->intr, NUMSIZE);
  495. bp += NUMSIZE;
  496. readnum(0, bp, NUMSIZE, mp->syscall, NUMSIZE);
  497. bp += NUMSIZE;
  498. readnum(0, bp, NUMSIZE, mp->pfault, NUMSIZE);
  499. bp += NUMSIZE;
  500. readnum(0, bp, NUMSIZE, mp->tlbfault, NUMSIZE);
  501. bp += NUMSIZE;
  502. readnum(0, bp, NUMSIZE, mp->tlbpurge, NUMSIZE);
  503. bp += NUMSIZE;
  504. readnum(0, bp, NUMSIZE, sys->load, NUMSIZE);
  505. bp += NUMSIZE;
  506. readnum(0, bp, NUMSIZE,
  507. (mp->perf.avg_inidle*100)/mp->perf.period,
  508. NUMSIZE);
  509. bp += NUMSIZE;
  510. readnum(0, bp, NUMSIZE,
  511. (mp->perf.avg_inintr*100)/mp->perf.period,
  512. NUMSIZE);
  513. bp += NUMSIZE;
  514. readnum(0, bp, NUMSIZE, 0, NUMSIZE); /* sched # */
  515. bp += NUMSIZE;
  516. bp = strecpy(bp, e, rolename[mp->NIX.nixtype]);
  517. *bp++ = '\n';
  518. }
  519. if(waserror()){
  520. free(b);
  521. nexterror();
  522. }
  523. n = readstr(offset, buf, n, b);
  524. free(b);
  525. poperror();
  526. return n;
  527. case Qswap:
  528. tmp[0] = 0;
  529. s = seprintpagestats(tmp, tmp + sizeof tmp);
  530. s = seprintphysstats(s, tmp + sizeof tmp);
  531. b = buf;
  532. l = s - tmp;
  533. i = readstr(offset, b, l, tmp);
  534. b += i;
  535. n -= i;
  536. if(offset > l)
  537. offset -= l;
  538. else
  539. offset = 0;
  540. return i + mallocreadsummary(c, b, n, offset);
  541. case Qsysname:
  542. if(sysname == nil)
  543. return 0;
  544. return readstr(offset, buf, n, sysname);
  545. case Qrandom:
  546. return randomread(buf, n);
  547. case Qurandom:
  548. return urandomread(buf, n);
  549. case Qdrivers:
  550. return devtabread(c, buf, n, off);
  551. case Qzero:
  552. memset(buf, 0, n);
  553. return n;
  554. case Qosversion:
  555. snprint(tmp, sizeof tmp, "2000");
  556. n = readstr(offset, buf, n, tmp);
  557. return n;
  558. case Qdebug:
  559. s = seprint(tmp, tmp + sizeof tmp, "locks %lu\n", lockstats.locks);
  560. s = seprint(s, tmp + sizeof tmp, "glare %lu\n", lockstats.glare);
  561. s = seprint(s, tmp + sizeof tmp, "inglare %lu\n", lockstats.inglare);
  562. s = seprint(s, tmp + sizeof tmp, "qlock %lu\n", qlockstats.qlock);
  563. seprint(s, tmp + sizeof tmp, "qlockq %lu\n", qlockstats.qlockq);
  564. return readstr(offset, buf, n, tmp);
  565. break;
  566. case Qsyscall:
  567. snprint(tmp, sizeof tmp, "%s", printallsyscalls ? "on" : "off");
  568. return readstr(offset, buf, n, tmp);
  569. break;
  570. default:
  571. print("consread %#llux\n", c->qid.path);
  572. error(Egreg);
  573. }
  574. return -1; /* never reached */
  575. }
  576. static int32_t
  577. conswrite(Chan *c, void *va, int32_t n, int64_t off)
  578. {
  579. Proc *up = externup();
  580. char buf[256];
  581. int32_t l, bp;
  582. char *a;
  583. Mach *mp;
  584. int i;
  585. uint32_t offset;
  586. Cmdbuf *cb;
  587. Cmdtab *ct;
  588. a = va;
  589. offset = off;
  590. extern int printallsyscalls;
  591. switch((uint32_t)c->qid.path){
  592. case Qcons:
  593. /*
  594. * Can't page fault in putstrn, so copy the data locally.
  595. */
  596. l = n;
  597. while(l > 0){
  598. bp = l;
  599. if(bp > sizeof buf)
  600. bp = sizeof buf;
  601. memmove(buf, a, bp);
  602. putstrn0(buf, bp, 1);
  603. a += bp;
  604. l -= bp;
  605. }
  606. break;
  607. case Qconsctl:
  608. print("consctl\n");
  609. if(n >= sizeof(buf))
  610. n = sizeof(buf)-1;
  611. strncpy(buf, a, n);
  612. buf[n] = 0;
  613. for(a = buf; a;){
  614. if(strncmp(a, "sys", 3) == 0) {
  615. printallsyscalls = ! printallsyscalls;
  616. print("%sracing syscalls\n", printallsyscalls ? "T" : "Not t");
  617. }
  618. if(a = strchr(a, ' '))
  619. a++;
  620. }
  621. break;
  622. case Qtime:
  623. if(!iseve())
  624. error(Eperm);
  625. return writetime(a, n);
  626. case Qbintime:
  627. if(!iseve())
  628. error(Eperm);
  629. return writebintime(a, n);
  630. case Qhostowner:
  631. return hostownerwrite(a, n);
  632. case Qhostdomain:
  633. return hostdomainwrite(a, n);
  634. case Quser:
  635. return userwrite(a, n);
  636. case Qnull:
  637. break;
  638. case Qreboot:
  639. if(!iseve())
  640. error(Eperm);
  641. cb = parsecmd(a, n);
  642. if(waserror()) {
  643. free(cb);
  644. nexterror();
  645. }
  646. ct = lookupcmd(cb, rebootmsg, nelem(rebootmsg));
  647. switch(ct->index) {
  648. case CMhalt:
  649. reboot(nil, 0, 0);
  650. break;
  651. case CMreboot:
  652. rebootcmd(cb->nf-1, cb->f+1);
  653. break;
  654. case CMpanic:
  655. *(volatile uint32_t*)0=0;
  656. panic("/dev/reboot");
  657. }
  658. poperror();
  659. free(cb);
  660. break;
  661. case Qsysstat:
  662. for(i = 0; i < MACHMAX; i++)
  663. if((mp = sys->machptr[i]) != nil && mp->online){
  664. mp = sys->machptr[i];
  665. mp->cs = 0;
  666. mp->intr = 0;
  667. mp->syscall = 0;
  668. mp->pfault = 0;
  669. mp->tlbfault = 0; /* not updated */
  670. mp->tlbpurge = 0; /* # mmuflushtlb */
  671. }
  672. break;
  673. case Qswap:
  674. if(n >= sizeof buf)
  675. error(Egreg);
  676. memmove(buf, va, n); /* so we can NUL-terminate */
  677. buf[n] = 0;
  678. if(!iseve())
  679. error(Eperm);
  680. if(buf[0]<'0' || '9'<buf[0])
  681. error(Ebadarg);
  682. if(strncmp(buf, "start", 5) == 0){
  683. print("request to start pager ignored\n");
  684. break;
  685. }
  686. break;
  687. case Qsysname:
  688. if(offset != 0)
  689. error(Ebadarg);
  690. if(n <= 0 || n >= sizeof buf)
  691. error(Ebadarg);
  692. strncpy(buf, a, n);
  693. buf[n] = 0;
  694. if(buf[n-1] == '\n')
  695. buf[n-1] = 0;
  696. kstrdup(&sysname, buf);
  697. break;
  698. case Qdebug:
  699. if(n >= sizeof(buf))
  700. n = sizeof(buf)-1;
  701. strncpy(buf, a, n);
  702. buf[n] = 0;
  703. if(n > 0 && buf[n-1] == '\n')
  704. buf[n-1] = 0;
  705. error(Ebadctl);
  706. break;
  707. case Qsyscall:
  708. if(n >= sizeof(buf))
  709. n = sizeof(buf)-1;
  710. strncpy(buf, a, n);
  711. buf[n] = 0;
  712. if(n > 0 && buf[n-1] == '\n')
  713. buf[n-1] = 0;
  714. // Doing strncmp right is just painful and overkill here.
  715. if (buf[0] == 'o') {
  716. if (buf[1] == 'n' && buf[2] == 0) {
  717. printallsyscalls = 1;
  718. break;
  719. }
  720. if (buf[1] == 'f' && buf[2] == 'f' && buf[3] == 0) {
  721. printallsyscalls = 0;
  722. break;
  723. }
  724. }
  725. error("#c/syscall: can only write on or off");
  726. break;
  727. default:
  728. print("conswrite: %#llux\n", c->qid.path);
  729. error(Egreg);
  730. }
  731. return n;
  732. }
  733. Dev consdevtab = {
  734. .dc = 'c',
  735. .name = "cons",
  736. .reset = devreset,
  737. .init = consinit,
  738. .shutdown = devshutdown,
  739. .attach = consattach,
  740. .walk = conswalk,
  741. .stat = consstat,
  742. .open = consopen,
  743. .create = devcreate,
  744. .close = consclose,
  745. .read = consread,
  746. .bread = devbread,
  747. .write = conswrite,
  748. .bwrite = devbwrite,
  749. .remove = devremove,
  750. .wstat = devwstat,
  751. };
  752. static uint32_t randn;
  753. static void
  754. seedrand(void)
  755. {
  756. Proc *up = externup();
  757. if(!waserror()){
  758. randomread((void*)&randn, sizeof(randn));
  759. poperror();
  760. }
  761. }
  762. int
  763. nrand(int n)
  764. {
  765. if(randn == 0)
  766. seedrand();
  767. randn = randn*1103515245 + 12345 + sys->ticks;
  768. return (randn>>16) % n;
  769. }
  770. int
  771. rand(void)
  772. {
  773. nrand(1);
  774. return randn;
  775. }
  776. static uint64_t uvorder = 0x0001020304050607ULL;
  777. static uint8_t*
  778. le2int64_t(int64_t *to, uint8_t *f)
  779. {
  780. uint8_t *t, *o;
  781. int i;
  782. t = (uint8_t*)to;
  783. o = (uint8_t*)&uvorder;
  784. for(i = 0; i < sizeof(int64_t); i++)
  785. t[o[i]] = f[i];
  786. return f+sizeof(int64_t);
  787. }
  788. static uint8_t*
  789. int64_t2le(uint8_t *t, int64_t from)
  790. {
  791. uint8_t *f, *o;
  792. int i;
  793. f = (uint8_t*)&from;
  794. o = (uint8_t*)&uvorder;
  795. for(i = 0; i < sizeof(int64_t); i++)
  796. t[i] = f[o[i]];
  797. return t+sizeof(int64_t);
  798. }
  799. static int32_t order = 0x00010203;
  800. static uint8_t*
  801. le2long(int32_t *to, uint8_t *f)
  802. {
  803. uint8_t *t, *o;
  804. int i;
  805. t = (uint8_t*)to;
  806. o = (uint8_t*)&order;
  807. for(i = 0; i < sizeof(int32_t); i++)
  808. t[o[i]] = f[i];
  809. return f+sizeof(int32_t);
  810. }
  811. #if 0
  812. static uint8_t*
  813. long2le(uint8_t *t, int32_t from)
  814. {
  815. uint8_t *f, *o;
  816. int i;
  817. f = (uint8_t*)&from;
  818. o = (uint8_t*)&order;
  819. for(i = 0; i < sizeof(int32_t); i++)
  820. t[i] = f[o[i]];
  821. return t+sizeof(int32_t);
  822. }
  823. #endif
  824. char *Ebadtimectl = "bad time control";
  825. /*
  826. * like the old #c/time but with added info. Return
  827. *
  828. * secs nanosecs fastticks fasthz
  829. */
  830. static int
  831. readtime(uint32_t off, char *buf, int n)
  832. {
  833. int64_t nsec, ticks;
  834. int32_t sec;
  835. char str[7*NUMSIZE];
  836. nsec = todget(&ticks);
  837. if(fasthz == 0LL)
  838. fastticks((uint64_t*)&fasthz);
  839. sec = nsec/1000000000ULL;
  840. snprint(str, sizeof(str), "%*lud %*llud %*llud %*llud ",
  841. NUMSIZE-1, sec,
  842. VLNUMSIZE-1, nsec,
  843. VLNUMSIZE-1, ticks,
  844. VLNUMSIZE-1, fasthz);
  845. return readstr(off, buf, n, str);
  846. }
  847. /*
  848. * set the time in seconds
  849. */
  850. static int
  851. writetime(char *buf, int n)
  852. {
  853. char b[13];
  854. int32_t i;
  855. int64_t now;
  856. if(n >= sizeof(b))
  857. error(Ebadtimectl);
  858. strncpy(b, buf, n);
  859. b[n] = 0;
  860. i = strtol(b, 0, 0);
  861. if(i <= 0)
  862. error(Ebadtimectl);
  863. now = i*1000000000LL;
  864. todset(now, 0, 0);
  865. return n;
  866. }
  867. /*
  868. * read binary time info. all numbers are little endian.
  869. * ticks and nsec are syncronized.
  870. */
  871. static int
  872. readbintime(char *buf, int n)
  873. {
  874. int i;
  875. int64_t nsec, ticks;
  876. uint8_t *b = (uint8_t*)buf;
  877. i = 0;
  878. if(fasthz == 0LL)
  879. fastticks((uint64_t*)&fasthz);
  880. nsec = todget(&ticks);
  881. if(n >= 3*sizeof(uint64_t)){
  882. int64_t2le(b+2*sizeof(uint64_t), fasthz);
  883. i += sizeof(uint64_t);
  884. }
  885. if(n >= 2*sizeof(uint64_t)){
  886. int64_t2le(b+sizeof(uint64_t), ticks);
  887. i += sizeof(uint64_t);
  888. }
  889. if(n >= 8){
  890. int64_t2le(b, nsec);
  891. i += sizeof(int64_t);
  892. }
  893. return i;
  894. }
  895. /*
  896. * set any of the following
  897. * - time in nsec
  898. * - nsec trim applied over some seconds
  899. * - clock frequency
  900. */
  901. static int
  902. writebintime(char *buf, int n)
  903. {
  904. uint8_t *p;
  905. int64_t delta;
  906. int32_t period;
  907. n--;
  908. p = (uint8_t*)buf + 1;
  909. switch(*buf){
  910. case 'n':
  911. if(n < sizeof(int64_t))
  912. error(Ebadtimectl);
  913. le2int64_t(&delta, p);
  914. todset(delta, 0, 0);
  915. break;
  916. case 'd':
  917. if(n < sizeof(int64_t)+sizeof(int32_t))
  918. error(Ebadtimectl);
  919. p = le2int64_t(&delta, p);
  920. le2long(&period, p);
  921. todset(-1, delta, period);
  922. break;
  923. case 'f':
  924. if(n < sizeof(uint64_t))
  925. error(Ebadtimectl);
  926. le2int64_t(&fasthz, p);
  927. todsetfreq(fasthz);
  928. break;
  929. }
  930. return n;
  931. }