devcons.c 18 KB

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