portdat.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. typedef struct Alarms Alarms;
  10. typedef struct Block Block;
  11. typedef struct Chan Chan;
  12. typedef struct Cmdbuf Cmdbuf;
  13. typedef struct Cmdtab Cmdtab;
  14. typedef struct Confmem Confmem;
  15. typedef struct Dev Dev;
  16. typedef struct DevConf DevConf;
  17. typedef struct Dirtab Dirtab;
  18. typedef struct Edf Edf;
  19. typedef struct Egrp Egrp;
  20. typedef struct Evalue Evalue;
  21. typedef struct Fastcall Fastcall;
  22. typedef struct Fgrp Fgrp;
  23. typedef struct Image Image;
  24. typedef struct Kzio Kzio;
  25. typedef struct Ldseg Ldseg;
  26. typedef struct Log Log;
  27. typedef struct Logflag Logflag;
  28. typedef struct Lockstats Lockstats;
  29. typedef struct Mhead Mhead;
  30. typedef struct Mnt Mnt;
  31. typedef struct Mntcache Mntcache;
  32. typedef struct Mntrpc Mntrpc;
  33. typedef struct Mntwalk Mntwalk;
  34. typedef struct Mount Mount;
  35. typedef struct Note Note;
  36. typedef struct Page Page;
  37. typedef struct Path Path;
  38. typedef struct Perf Perf;
  39. typedef struct Pgalloc Pgalloc;
  40. typedef struct Pgrp Pgrp;
  41. typedef struct Pgsza Pgsza;
  42. typedef struct Physseg Physseg;
  43. typedef struct PhysUart PhysUart;
  44. typedef struct Proc Proc;
  45. typedef struct Procalloc Procalloc;
  46. typedef struct Pte Pte;
  47. typedef struct QLock QLock;
  48. typedef struct QLockstats QLockstats;
  49. typedef struct Queue Queue;
  50. typedef struct Ref Ref;
  51. typedef struct Rendez Rendez;
  52. typedef struct Rgrp Rgrp;
  53. typedef struct RWlock RWlock;
  54. typedef struct Sched Sched;
  55. typedef struct Schedq Schedq;
  56. typedef struct Segment Segment;
  57. typedef struct Sema Sema;
  58. typedef struct Strace Strace;
  59. typedef struct Timer Timer;
  60. typedef struct Timers Timers;
  61. typedef struct Uart Uart;
  62. typedef struct Waitq Waitq;
  63. typedef struct Waitstats Waitstats;
  64. typedef struct Walkqid Walkqid;
  65. typedef struct Watchdog Watchdog;
  66. typedef struct Watermark Watermark;
  67. typedef struct Zseg Zseg;
  68. typedef int Devgen(Chan *, char *, Dirtab *, int, int, Dir *);
  69. #include <fcall.h>
  70. #define ROUND(s, sz) (((s) + (sz - 1)) & ~(sz - 1))
  71. struct Ref {
  72. Lock l;
  73. int ref;
  74. };
  75. struct Rendez {
  76. Lock l;
  77. Proc *_p; // There is already a Proc *p into Lock
  78. };
  79. enum {
  80. NWstats = 500,
  81. WSlock = 0,
  82. WSqlock,
  83. WSslock,
  84. };
  85. /*
  86. * different arrays with stat info, so we can memset any of them
  87. * to 0 to clear stats.
  88. */
  89. struct Waitstats {
  90. int on;
  91. int npcs;
  92. int *type;
  93. usize *pcs;
  94. int *ns;
  95. u64 *wait;
  96. u64 *total;
  97. };
  98. extern Waitstats waitstats;
  99. struct Lockstats {
  100. u32 locks;
  101. u32 glare;
  102. u32 inglare;
  103. };
  104. extern Lockstats lockstats;
  105. struct QLockstats {
  106. u32 rlock;
  107. u32 rlockq;
  108. u32 wlock;
  109. u32 wlockq;
  110. u32 qlock;
  111. u32 qlockq;
  112. };
  113. extern QLockstats qlockstats;
  114. struct QLock {
  115. Lock use; /* to access Qlock structure */
  116. Proc *head; /* next process waiting for object */
  117. Proc *tail; /* last process waiting for object */
  118. int locked; /* flag */
  119. usize pc;
  120. };
  121. struct RWlock {
  122. Lock use;
  123. Proc *head; /* list of waiting processes */
  124. Proc *tail;
  125. usize wpc; /* pc of writer */
  126. Proc *wproc; /* writing proc */
  127. int readers; /* number of readers */
  128. int writer; /* number of writers */
  129. };
  130. struct Alarms {
  131. QLock ql;
  132. Proc *_head;
  133. };
  134. /*
  135. * Access types in namec & channel flags
  136. */
  137. enum {
  138. Aaccess, /* as in stat, wstat */
  139. Abind, /* for left-hand-side of bind */
  140. Atodir, /* as in chdir */
  141. Aopen, /* for i/o */
  142. Amount, /* to be mounted or mounted upon */
  143. Acreate, /* is to be created */
  144. Aremove, /* will be removed by caller */
  145. COPEN = 0x0001, /* for i/o */
  146. CMSG = 0x0002, /* the message channel for a mount */
  147. /*rsc CCREATE = 0x0004,*/ /* permits creation if c->mnt */
  148. CCEXEC = 0x0008, /* close on exec */
  149. CFREE = 0x0010, /* not in use */
  150. CRCLOSE = 0x0020, /* remove on close */
  151. CCACHE = 0x0080, /* client cache */
  152. };
  153. /* flag values */
  154. enum {
  155. BINTR = (1 << 0),
  156. Bipck = (1 << 2), /* ip checksum */
  157. Budpck = (1 << 3), /* udp checksum */
  158. Btcpck = (1 << 4), /* tcp checksum */
  159. Bpktck = (1 << 5), /* packet checksum */
  160. };
  161. struct Block {
  162. i32 ref;
  163. Block *next;
  164. Block *list;
  165. unsigned char *rp; /* first unconsumed byte */
  166. unsigned char *wp; /* first empty byte */
  167. unsigned char *lim; /* 1 past the end of the buffer */
  168. unsigned char *base; /* start of the buffer */
  169. void (*free)(Block *);
  170. u16 flag;
  171. u16 checksum; /* IP checksum of complete packet (minus media header) */
  172. u32 magic;
  173. };
  174. #define BLEN(s) ((s)->wp - (s)->rp)
  175. #define BALLOC(s) ((s)->lim - (s)->base)
  176. struct Chan {
  177. Ref r; /* the Lock in this Ref is also Chan's lock */
  178. Chan *next; /* allocation */
  179. Chan *link;
  180. i64 offset; /* in fd */
  181. i64 devoffset; /* in underlying device; see read */
  182. Dev *dev;
  183. u32 devno;
  184. u16 mode; /* read/write */
  185. u16 flag;
  186. Qid qid;
  187. int fid; /* for devmnt */
  188. u32 iounit; /* chunk size for i/o; 0==default */
  189. Mhead *umh; /* mount point that derived Chan; used in unionread */
  190. Chan *umc; /* channel in union; held for union read */
  191. QLock umqlock; /* serialize unionreads */
  192. int uri; /* union read index */
  193. int dri; /* devdirread index */
  194. unsigned char *dirrock; /* directory entry rock for translations */
  195. int nrock;
  196. int mrock;
  197. QLock rockqlock;
  198. int ismtpt;
  199. Mntcache *mc; /* Mount cache pointer */
  200. Mnt *mux; /* Mnt for clients using me for messages */
  201. union {
  202. void *aux;
  203. Qid pgrpid; /* for #p/notepg */
  204. u32 mid; /* for ns in devproc */
  205. };
  206. Chan *mchan; /* channel to mounted server */
  207. Qid mqid; /* qid of root of mount point */
  208. Path *path;
  209. unsigned char *writebuff;
  210. int buffend;
  211. int writeoffset;
  212. int buffsize;
  213. };
  214. struct Path {
  215. Ref r;
  216. char *s;
  217. Chan **mtpt; /* mtpt history */
  218. int len; /* strlen(s) */
  219. int alen; /* allocated length of s */
  220. int mlen; /* number of path elements */
  221. int malen; /* allocated length of mtpt */
  222. };
  223. struct Dev {
  224. int dc;
  225. char *name;
  226. void (*reset)(void);
  227. void (*init)(void);
  228. void (*shutdown)(void);
  229. Chan *(*attach)(char *);
  230. Walkqid *(*walk)(Chan *, Chan *, char **, int);
  231. i32 (*stat)(Chan *, unsigned char *, i32);
  232. Chan *(*open)(Chan *, int);
  233. void (*create)(Chan *, char *, int, int);
  234. void (*close)(Chan *);
  235. i32 (*read)(Chan *, void *, i32, i64);
  236. Block *(*bread)(Chan *, i32, i64);
  237. i32 (*write)(Chan *, void *, i32, i64);
  238. i32 (*bwrite)(Chan *, Block *, i64);
  239. void (*remove)(Chan *);
  240. i32 (*wstat)(Chan *, unsigned char *, i32);
  241. void (*power)(int); /* power mgt: power(1) => on, power (0) => off */
  242. int (*config)(int, char *, DevConf *); /* returns 0 on error */
  243. int (*zread)(Chan *, Kzio *, int, usize, i64);
  244. int (*zwrite)(Chan *, Kzio *, int, i64);
  245. };
  246. struct Dirtab {
  247. char name[KNAMELEN];
  248. Qid qid;
  249. i64 length;
  250. i32 perm;
  251. };
  252. struct Walkqid {
  253. Chan *clone;
  254. int nqid;
  255. Qid qid[1];
  256. };
  257. enum {
  258. NSMAX = 1000,
  259. NSLOG = 7,
  260. NSCACHE = (1 << NSLOG),
  261. };
  262. struct Mntwalk /* state for /proc/#/ns */
  263. {
  264. int cddone;
  265. Mhead *mh;
  266. Mount *cm;
  267. };
  268. struct Mount {
  269. int mountid;
  270. Mount *next;
  271. Mhead *head;
  272. Mount *copy;
  273. Mount *order;
  274. Chan *to; /* channel replacing channel */
  275. int mflag;
  276. char *spec;
  277. };
  278. struct Mhead {
  279. Ref r;
  280. RWlock lock;
  281. Chan *from; /* channel mounted upon */
  282. Mount *mount; /* what's mounted upon it */
  283. Mhead *hash; /* Hash chain */
  284. };
  285. struct Mnt {
  286. Lock l;
  287. /* references are counted using c->ref; channels on this mount point incref(c->mchan) == Mnt.c */
  288. Chan *c; /* Channel to file service */
  289. Proc *rip; /* Reader in progress */
  290. Mntrpc *queue; /* Queue of pending requests on this channel */
  291. u32 id; /* Multiplexer id for channel check */
  292. Mnt *list; /* Free list */
  293. int flags; /* cache */
  294. int msize; /* data + IOHDRSZ */
  295. char *version; /* 9P version */
  296. Queue *q; /* input queue */
  297. };
  298. enum {
  299. NUser, /* note provided externally */
  300. NExit, /* deliver note quietly */
  301. NDebug, /* print debug message */
  302. };
  303. struct Note {
  304. char msg[ERRMAX];
  305. int flag; /* whether system posted it */
  306. };
  307. enum {
  308. PG_NOFLUSH = 0,
  309. PG_TXTFLUSH = 1, /* flush dcache and invalidate icache */
  310. PG_DATFLUSH = 2, /* flush both i & d caches (UNUSED) */
  311. PG_NEWCOL = 3, /* page has been recolored */
  312. PG_MOD = 0x01, /* software modified bit */
  313. PG_REF = 0x02, /* software referenced bit */
  314. };
  315. struct Page {
  316. Lock l;
  317. u64 pa; /* Physical address in memory */
  318. usize va; /* Virtual address for user */
  319. u32 daddr; /* Disc address on swap */
  320. int ref; /* Reference count */
  321. unsigned char modref; /* Simulated modify/reference bits */
  322. int color; /* Cache coloring */
  323. char cachectl[MACHMAX]; /* Cache flushing control for mmuput */
  324. Image *image; /* Associated text or swap image */
  325. Page *next; /* Lru free list */
  326. Page *prev;
  327. Page *hash; /* Image hash chains */
  328. int pgszi; /* size index in machp()->pgsz[] */
  329. };
  330. struct Image {
  331. Ref r;
  332. Chan *c;
  333. Qid qid; /* Qid for page cache coherence */
  334. Qid mqid;
  335. Chan *mchan;
  336. int dc; /* Device type of owning channel */
  337. //subtype
  338. Segment *s; /* TEXT segment for image if running */
  339. Image *hash; /* Qid hash chains */
  340. Image *next; /* Free list or lru list */
  341. Image *prev; /* lru list */
  342. int notext; /* no file associated */
  343. int color;
  344. };
  345. /*
  346. * virtual MMU
  347. */
  348. #define PTEMAPMEM (1ULL * GiB)
  349. #define SEGMAPSIZE 1984
  350. #define SSEGMAPSIZE 16 /* XXX: shouldn't be 32 at least? */
  351. /*
  352. * Interface between fixfault and mmuput.
  353. */
  354. #define PTEVALID (1 << 0)
  355. #define PTEWRITE (1 << 1)
  356. #define PTERONLY (0 << 1)
  357. #define PTEUSER (1 << 2)
  358. #define PTENOEXEC (1 << 3)
  359. #define PTEUNCACHED (1 << 4)
  360. struct Pte {
  361. Page **first; /* First used entry */
  362. Page **last; /* Last used entry */
  363. Page *pages[]; /* Page map for this chunk of pte */
  364. };
  365. /* Segment types */
  366. enum {
  367. /* TODO(aki): these types are going to go */
  368. SG_TYPE = 0xf, /* Mask type of segment */
  369. SG_BAD0 = 0x0,
  370. SG_TEXT = 0x1,
  371. SG_DATA = 0x2,
  372. SG_BSS = 0x3,
  373. SG_STACK = 0x4,
  374. SG_SHARED = 0x5,
  375. SG_PHYSICAL = 0x6,
  376. SG_MMAP = 0x7,
  377. SG_LOAD = 0x8, /* replaces SG_TEXT, SG_DATA */
  378. SG_PERM = 0xf0,
  379. SG_READ = 0x10,
  380. SG_WRITE = 0x20,
  381. SG_EXEC = 0x40,
  382. SG_FLAG = 0xf00,
  383. SG_CACHED = 0x100, /* Physseg can be cached */
  384. SG_CEXEC = 0x200, /* Detach at exec */
  385. SG_ZIO = 0x400, /* used for zero copy */
  386. SG_KZIO = 0x800, /* kernel zero copy segment */
  387. };
  388. extern char *segtypes[]; /* port/segment.c */
  389. enum {
  390. FT_WRITE = 0,
  391. FT_READ,
  392. FT_EXEC,
  393. };
  394. extern char *faulttypes[]; /* port/fault.c */
  395. #define PG_ONSWAP 1
  396. #define onswap(s) (PTR2UINT(s) & PG_ONSWAP)
  397. #define pagedout(s) (PTR2UINT(s) == 0 || onswap(s))
  398. #define swapaddr(s) (PTR2UINT(s) & ~PG_ONSWAP)
  399. #define SEGMAXPG (SEGMAPSIZE)
  400. struct Physseg {
  401. u32 attr; /* Segment attributes */
  402. char *name; /* Attach name */
  403. u64 pa; /* Physical address */
  404. usize size; /* Maximum segment size in pages */
  405. int pgszi; /* Page size index in Mach */
  406. Page *(*pgalloc)(Segment *, usize); /* Allocation if we need it */
  407. void (*pgfree)(Page *);
  408. usize gva; /* optional global virtual address */
  409. };
  410. struct Sema {
  411. Rendez rend;
  412. int *addr;
  413. int waiting;
  414. Sema *next;
  415. Sema *prev;
  416. };
  417. /* Zero copy per-segment information (locked using Segment.lk) */
  418. struct Zseg {
  419. void *map; /* memory map for buffers within this segment */
  420. usize *addr; /* array of addresses released */
  421. int naddr; /* size allocated for the array */
  422. int end; /* 1+ last used index in addr */
  423. Rendez rr; /* process waiting to read free addresses */
  424. };
  425. #define NOCOLOR -1
  426. /* demand loading params of a segment */
  427. struct Ldseg {
  428. i64 memsz;
  429. i64 filesz;
  430. i64 pg0fileoff;
  431. usize pg0vaddr;
  432. u32 pg0off;
  433. u32 pgsz;
  434. u16 type;
  435. };
  436. struct Segment {
  437. Ref r;
  438. QLock lk;
  439. u16 steal; /* Page stealer lock */
  440. u16 type; /* segment type */
  441. int pgszi; /* page size index in Mach MMMU */
  442. u32 ptepertab;
  443. int color;
  444. usize base; /* virtual base */
  445. usize top; /* virtual top */
  446. usize size; /* size in pages */
  447. Ldseg ldseg;
  448. int flushme; /* maintain icache for this segment */
  449. Image *image; /* text in file attached to this segment */
  450. Physseg *pseg;
  451. u32 *profile; /* Tick profile area */
  452. Pte **map;
  453. int mapsize;
  454. Pte *ssegmap[SSEGMAPSIZE];
  455. Lock semalock;
  456. Sema sema;
  457. Zseg zseg;
  458. };
  459. /*
  460. * NIX zero-copy IO structure.
  461. */
  462. struct Kzio {
  463. Zio Zio;
  464. Segment *seg;
  465. };
  466. enum {
  467. RENDLOG = 5,
  468. RENDHASH = 1 << RENDLOG, /* Hash to lookup rendezvous tags */
  469. MNTLOG = 5,
  470. MNTHASH = 1 << MNTLOG, /* Hash to walk mount table */
  471. NFD = 100, /* per process file descriptors */
  472. PGHLOG = 9,
  473. PGHSIZE = 1 << PGHLOG, /* Page hash for image lookup */
  474. };
  475. #define REND(p, s) ((p)->rendhash[(s) & ((1 << RENDLOG) - 1)])
  476. #define MOUNTH(p, qid) ((p)->mnthash[(qid).path & ((1 << MNTLOG) - 1)])
  477. struct Pgrp {
  478. Ref r; /* also used as a lock when mounting */
  479. int noattach;
  480. u32 pgrpid;
  481. QLock debug; /* single access via devproc.c */
  482. RWlock ns; /* Namespace n read/one write lock */
  483. Mhead *mnthash[MNTHASH];
  484. };
  485. struct Rgrp {
  486. Ref r; /* the Ref's lock is also the Rgrp's lock */
  487. Proc *rendhash[RENDHASH]; /* Rendezvous tag hash */
  488. };
  489. struct Egrp {
  490. Ref r;
  491. RWlock rwl;
  492. Evalue **ent;
  493. int nent;
  494. int ment;
  495. u32 path; /* qid.path of next Evalue to be allocated */
  496. u32 vers; /* of Egrp */
  497. };
  498. struct Evalue {
  499. char *name;
  500. char *value;
  501. int len;
  502. Evalue *link;
  503. Qid qid;
  504. };
  505. struct Fgrp {
  506. Ref r;
  507. Chan **fd;
  508. int nfd; /* number allocated */
  509. int maxfd; /* highest fd in use */
  510. int exceed; /* debugging */
  511. };
  512. enum {
  513. DELTAFD = 20 /* incremental increase in Fgrp.fd's */
  514. };
  515. struct Pgsza {
  516. u32 freecount; /* how many pages in the free list? */
  517. Ref npages; /* how many pages of this size? */
  518. Page *head; /* MRU */
  519. Page *tail; /* LRU */
  520. };
  521. struct Pgalloc {
  522. Lock l;
  523. int userinit; /* working in user init mode */
  524. Pgsza pgsza[NPGSZ]; /* allocs for m->npgsz page sizes */
  525. Page *hash[PGHSIZE]; /* only used for user pages */
  526. Lock hashlock;
  527. Rendez rend; /* sleep for free mem */
  528. QLock pwait; /* queue of procs waiting for this pgsz */
  529. };
  530. struct Waitq {
  531. Waitmsg w;
  532. Waitq *next;
  533. };
  534. /*
  535. * fasttick timer interrupts
  536. */
  537. enum {
  538. /* Mode */
  539. Trelative, /* timer programmed in ns from now */
  540. Tperiodic, /* periodic timer, period in ns */
  541. };
  542. struct Timer {
  543. /* Public interface */
  544. int tmode; /* See above */
  545. i64 tns; /* meaning defined by mode */
  546. void (*tf)(Ureg *, Timer *);
  547. void *ta;
  548. /* Internal */
  549. Lock l;
  550. Timers *tt; /* Timers queue this timer runs on */
  551. i64 twhen; /* ns represented in fastticks */
  552. Timer *tnext;
  553. };
  554. enum {
  555. RFNAMEG = (1 << 0),
  556. RFENVG = (1 << 1),
  557. RFFDG = (1 << 2),
  558. RFNOTEG = (1 << 3),
  559. RFPROC = (1 << 4),
  560. RFMEM = (1 << 5),
  561. RFNOWAIT = (1 << 6),
  562. RFCNAMEG = (1 << 10),
  563. RFCENVG = (1 << 11),
  564. RFCFDG = (1 << 12),
  565. RFREND = (1 << 13),
  566. RFNOMNT = (1 << 14),
  567. RFPREPAGE = (1 << 15),
  568. RFCPREPAGE = (1 << 16),
  569. RFCORE = (1 << 17),
  570. RFCCORE = (1 << 18),
  571. };
  572. /* execac */
  573. enum {
  574. EXTC = 0, /* exec on time-sharing */
  575. EXAC, /* want an AC for the exec'd image */
  576. EXXC, /* want an XC for the exec'd image */
  577. };
  578. /*
  579. * Maximum number of process memory segments
  580. */
  581. enum {
  582. NSEG = 12
  583. };
  584. enum {
  585. Dead = 0, /* Process states */
  586. Moribund,
  587. Ready,
  588. Scheding,
  589. Running,
  590. Queueing,
  591. QueueingR,
  592. QueueingW,
  593. Wakeme,
  594. Broken,
  595. Stopped,
  596. Rendezvous,
  597. Waitrelease,
  598. Exotic, /* NIX */
  599. Semdown,
  600. Proc_stopme = 1, /* devproc requests */
  601. Proc_exitme,
  602. Proc_traceme,
  603. Proc_exitbig,
  604. Proc_tracesyscall,
  605. Proc_toac,
  606. Proc_totc,
  607. TUser = 0, /* Proc.time */
  608. TSys,
  609. TReal,
  610. TCUser,
  611. TCSys,
  612. TCReal,
  613. NERR = 64,
  614. NNOTE = 5,
  615. Npriq = 20, /* number of scheduler priority levels */
  616. Nrq = Npriq + 2, /* number of priority levels including real time */
  617. PriRelease = Npriq, /* released edf processes */
  618. PriEdf = Npriq + 1, /* active edf processes */
  619. PriNormal = 10, /* base priority for normal processes */
  620. PriExtra = Npriq - 1, /* edf processes at high best-effort pri */
  621. PriKproc = 13, /* base priority for kernel processes */
  622. PriRoot = 13, /* base priority for root processes */
  623. };
  624. struct Schedq {
  625. Proc *head;
  626. Proc *tail;
  627. int n;
  628. };
  629. struct Sched {
  630. Lock l; /* runq */
  631. int nrdy;
  632. u32 delayedscheds; /* statistics */
  633. i32 skipscheds;
  634. i32 preempts;
  635. int schedgain;
  636. u32 balancetime;
  637. Schedq runq[Nrq];
  638. u32 runvec;
  639. int nmach; /* # of cores with this color */
  640. u32 nrun; /* to compute load */
  641. };
  642. typedef union Ar0 Ar0;
  643. union Ar0 {
  644. isize i;
  645. i32 l;
  646. usize p;
  647. usize u;
  648. void *v;
  649. i64 vl;
  650. };
  651. typedef struct Nixpctl Nixpctl;
  652. struct Strace {
  653. int opened;
  654. int tracing;
  655. int inherit;
  656. Ref procs; /* when procs goes to zero, q is hung up. */
  657. Ref users; /* when users goes to zero, q and struct are freed. */
  658. Queue *q;
  659. };
  660. struct Proc {
  661. Label sched; /* known to l.s */
  662. char *kstack; /* known to l.s */
  663. void *dbgreg; /* known to l.s User registers for devproc */
  664. usize tls; /* known to l.s thread local storage */
  665. Mach *mach; /* machine running this proc */
  666. char *text;
  667. char *user;
  668. char *args;
  669. int nargs; /* number of bytes of args */
  670. Proc *rnext; /* next process in run queue */
  671. Proc *qnext; /* next process on queue for a QLock */
  672. QLock *qlock; /* addr of qlock being queued for DEBUG */
  673. int state;
  674. char *psstate; /* What /proc/#/status reports */
  675. Segment *seg[NSEG];
  676. QLock seglock; /* locked whenever seg[] changes */
  677. int pid;
  678. int index; /* index (slot) in proc array */
  679. int ref; /* indirect reference */
  680. int noteid; /* Equivalent of note group */
  681. Proc *pidhash; /* next proc in pid hash */
  682. Lock exl; /* Lock count and waitq */
  683. Waitq *waitq; /* Exited processes wait children */
  684. int nchild; /* Number of living children */
  685. int nwait; /* Number of uncollected wait records */
  686. QLock qwaitr;
  687. Rendez waitr; /* Place to hang out in wait */
  688. Proc *parent;
  689. Pgrp *pgrp; /* Process group for namespace */
  690. Egrp *egrp; /* Environment group */
  691. Fgrp *fgrp; /* File descriptor group */
  692. Rgrp *rgrp; /* Rendez group */
  693. Fgrp *closingfgrp; /* used during teardown */
  694. int parentpid;
  695. u64 time[6]; /* User, Sys, Real; child U, S, R */
  696. u64 kentry; /* Kernel entry time stamp (for profiling) */
  697. /*
  698. * pcycles: cycles spent in this process (updated on procsave/restore)
  699. * when this is the current proc and we're in the kernel
  700. * (procrestores outnumber procsaves by one)
  701. * the number of cycles spent in the proc is pcycles + cycles()
  702. * when this is not the current process or we're in user mode
  703. * (procrestores and procsaves balance), it is pcycles.
  704. */
  705. i64 pcycles;
  706. int insyscall;
  707. QLock debug; /* to access debugging elements of User */
  708. Proc *pdbg; /* the debugging process */
  709. u32 procmode; /* proc device file mode */
  710. u32 privatemem; /* proc does not let anyone read mem */
  711. int hang; /* hang at next exec for debug */
  712. int procctl; /* Control for /proc debugging */
  713. usize pc; /* DEBUG only */
  714. Lock rlock; /* sync sleep/wakeup with postnote */
  715. Rendez *r; /* rendezvous point slept on */
  716. Rendez sleep; /* place for syssleep/debug/tsleep */
  717. int notepending; /* note issued but not acted on */
  718. int kp; /* true if a kernel process */
  719. Proc *palarm; /* Next alarm time */
  720. u32 alarm; /* Time of call */
  721. int newtlb; /* Pager has changed my pte's, I must flush */
  722. int noswap; /* process is not swappable */
  723. usize rendtag; /* Tag for rendezvous */
  724. usize rendval; /* Value for rendezvous */
  725. Proc *rendhash; /* Hash list for tag values */
  726. Timer Timer; /* For tsleep and real-time */
  727. Rendez *trend;
  728. int (*tfn)(void *);
  729. void (*kpfun)(void *);
  730. void *kparg;
  731. int scallnr; /* system call number */
  732. unsigned char arg[MAXSYSARG * sizeof(void *)]; /* system call arguments */
  733. int nerrlab;
  734. Label errlab[NERR];
  735. char *syserrstr; /* last error from a system call, errbuf0 or 1 */
  736. char *errstr; /* reason we're unwinding the error stack, errbuf1 or 0 */
  737. char errbuf0[ERRMAX];
  738. char errbuf1[ERRMAX];
  739. char genbuf[128]; /* buffer used e.g. for last name element from namec */
  740. Chan *slash;
  741. Chan *dot;
  742. Note note[NNOTE];
  743. short nnote;
  744. short notified; /* sysnoted is due */
  745. Note lastnote;
  746. void (*notify)(void *, char *);
  747. Lock *lockwait;
  748. Lock *lastlock; /* debugging */
  749. Lock *lastilock; /* debugging */
  750. Mach *wired;
  751. Mach *mp; /* machine this process last ran on */
  752. int nlocks; /* number of locks held by proc */
  753. u32 delaysched;
  754. u32 priority; /* priority level */
  755. u32 basepri; /* base priority level */
  756. int fixedpri; /* priority level does not change */
  757. u64 cpu; /* cpu average */
  758. u64 lastupdate;
  759. u64 readytime; /* time process came ready */
  760. u64 movetime; /* last time process switched processors */
  761. int preempted; /* true if this process hasn't finished the interrupt
  762. * that last preempted it
  763. */
  764. Edf *edf; /* if non-null, real-time proc, edf contains scheduling params */
  765. int trace; /* process being traced? */
  766. usize qpc; /* pc calling last blocking qlock */
  767. int setargs;
  768. void *ureg; /* User registers for notes */
  769. int color;
  770. Fastcall *fc;
  771. int fcount;
  772. char *syscalltrace;
  773. /* NIX */
  774. Mach *ac;
  775. Page *acPageTableRoot;
  776. int prepagemem;
  777. Nixpctl *nixpctl; /* NIX queue based system calls */
  778. u32 ntrap; /* # of traps while in this process */
  779. u32 nintr; /* # of intrs while in this process */
  780. u32 nsyscall; /* # of syscalls made by the process */
  781. u32 nactrap; /* # of traps in the AC for this process */
  782. u32 nacsyscall; /* # of syscalls in the AC for this process */
  783. u32 nicc; /* # of ICCs for the process */
  784. u64 actime1; /* ticks as of last call in AC */
  785. u64 actime; /* ∑time from call in AC to ret to AC, and... */
  786. u64 tctime; /* ∑time from call received to call handled */
  787. int nqtrap; /* # of traps in last quantum */
  788. int nqsyscall; /* # of syscalls in the last quantum */
  789. int nfullq;
  790. /*
  791. * machine specific fpu, mmu and notify
  792. */
  793. PFPU FPU;
  794. PMMU MMU;
  795. PNOTIFY NOTIFY;
  796. /*
  797. * mmap support.
  798. * For addresses that we reference that don't have a mapping,
  799. * if this queue is not NULL, we will send a message on it to be
  800. * handled by some other proc (not ourselves) and block on reading
  801. * a result back.
  802. */
  803. Queue *req, *resp;
  804. /* new strace support */
  805. Strace *strace;
  806. int strace_on;
  807. int strace_inherit;
  808. /* plan 9 binaries */
  809. int plan9;
  810. };
  811. struct Procalloc {
  812. Lock l;
  813. int nproc;
  814. Proc *ht[128];
  815. Proc *arena;
  816. Proc *free;
  817. };
  818. enum {
  819. PRINTSIZE = 256,
  820. MAXCRYPT = 127,
  821. NUMSIZE = 12, /* size of formatted number */
  822. MB = (1024 * 1024),
  823. /* READSTR was 1000, which is way too small for usb's ctl file */
  824. READSTR = 4000, /* temporary buffer size for device reads */
  825. WRITESTR = 256, /* ctl file write max */
  826. };
  827. extern Conf conf;
  828. extern char *cputype;
  829. extern int cpuserver;
  830. extern char *eve;
  831. extern char hostdomain[];
  832. extern u8 initcode[];
  833. extern int kbdbuttons;
  834. extern Ref noteidalloc;
  835. extern int nphysseg;
  836. extern int nsyscall;
  837. extern Pgalloc pga;
  838. extern Physseg physseg[];
  839. extern Procalloc procalloc;
  840. extern u32 qiomaxatomic;
  841. extern char *statename[];
  842. extern char *sysname;
  843. typedef struct Systab Systab;
  844. struct Systab {
  845. char *n;
  846. void (*f)(Ar0 *, ...);
  847. Ar0 r;
  848. };
  849. extern Systab systab[];
  850. enum {
  851. LRESPROF = 3,
  852. };
  853. /*
  854. * action log
  855. */
  856. struct Log {
  857. Lock l;
  858. int opens;
  859. char *buf;
  860. char *end;
  861. char *rptr;
  862. int len;
  863. int nlog;
  864. int minread;
  865. int logmask; /* mask of things to debug */
  866. QLock readq;
  867. Rendez readr;
  868. };
  869. struct Logflag {
  870. char *name;
  871. int mask;
  872. };
  873. enum {
  874. NCMDFIELD = 128
  875. };
  876. struct Cmdbuf {
  877. char *buf;
  878. char **f;
  879. int nf;
  880. };
  881. struct Cmdtab {
  882. int index; /* used by client to switch on result */
  883. char *cmd; /* command name */
  884. int narg; /* expected #args; 0 ==> variadic */
  885. };
  886. /*
  887. * routines to access UART hardware
  888. */
  889. struct PhysUart {
  890. char *name;
  891. Uart *(*pnp)(void);
  892. void (*enable)(Uart *, int);
  893. void (*disable)(Uart *);
  894. void (*kick)(Uart *);
  895. void (*dobreak)(Uart *, int);
  896. int (*baud)(Uart *, int);
  897. int (*bits)(Uart *, int);
  898. int (*stop)(Uart *, int);
  899. int (*parity)(Uart *, int);
  900. void (*modemctl)(Uart *, int);
  901. void (*rts)(Uart *, int);
  902. void (*dtr)(Uart *, int);
  903. i32 (*status)(Uart *, void *, i32, i32);
  904. void (*fifo)(Uart *, int);
  905. void (*power)(Uart *, int);
  906. int (*getc)(Uart *); /* polling version for rdb */
  907. void (*putc)(Uart *, int); /* polling version for iprint */
  908. void (*poll)(Uart *); /* polled interrupt routine */
  909. };
  910. enum {
  911. Stagesize = 2048
  912. };
  913. /*
  914. * software UART
  915. */
  916. struct Uart {
  917. void *regs; /* hardware stuff */
  918. void *saveregs; /* place to put registers on power down */
  919. char *name; /* internal name */
  920. u32 freq; /* clock frequency */
  921. int bits; /* bits per character */
  922. int stop; /* stop bits */
  923. int parity; /* even, odd or no parity */
  924. int baud; /* baud rate */
  925. PhysUart *phys;
  926. int console; /* used as a serial console */
  927. int special; /* internal kernel device */
  928. Uart *next; /* list of allocated uarts */
  929. QLock ql;
  930. int type; /* ?? */
  931. int dev;
  932. int opens;
  933. int enabled;
  934. Uart *elist; /* next enabled interface */
  935. int perr; /* parity errors */
  936. int ferr; /* framing errors */
  937. int oerr; /* rcvr overruns */
  938. int berr; /* no input buffers */
  939. int serr; /* input queue overflow */
  940. /* buffers */
  941. int (*putc)(Queue *, int);
  942. Queue *iq;
  943. Queue *oq;
  944. Lock rlock;
  945. unsigned char istage[Stagesize];
  946. unsigned char *iw;
  947. unsigned char *ir;
  948. unsigned char *ie;
  949. Lock tlock; /* transmit */
  950. unsigned char ostage[Stagesize];
  951. unsigned char *op;
  952. unsigned char *oe;
  953. int drain;
  954. int modem; /* hardware flow control on */
  955. int xonoff; /* software flow control on */
  956. int blocked;
  957. int cts, dsr, dcd; /* keep track of modem status */
  958. int ctsbackoff;
  959. int hup_dsr, hup_dcd; /* send hangup upstream? */
  960. int dohup;
  961. Rendez rend;
  962. };
  963. extern Uart *consuart;
  964. /*
  965. * performance timers, all units in perfticks
  966. */
  967. struct Perf {
  968. u64 intrts; /* time of last interrupt */
  969. u64 inintr; /* time since last clock tick in interrupt handlers */
  970. u64 avg_inintr; /* avg time per clock tick in interrupt handlers */
  971. u64 inidle; /* time since last clock tick in idle loop */
  972. u64 avg_inidle; /* avg time per clock tick in idle loop */
  973. u64 last; /* value of perfticks() at last clock tick */
  974. u64 period; /* perfticks() per clock tick */
  975. };
  976. struct Watchdog {
  977. void (*enable)(void); /* watchdog enable */
  978. void (*disable)(void); /* watchdog disable */
  979. void (*restart)(void); /* watchdog restart */
  980. void (*stat)(char *, char *); /* watchdog statistics */
  981. };
  982. struct Watermark {
  983. int highwater;
  984. int curr;
  985. int max;
  986. int hitmax; /* count: how many times hit max? */
  987. char *name;
  988. };
  989. /* queue state bits, Qmsg, Qcoalesce, and Qkick can be set in qopen */
  990. enum {
  991. /* Queue.state */
  992. Qstarve = (1 << 0), /* consumer starved */
  993. Qmsg = (1 << 1), /* message stream */
  994. Qclosed = (1 << 2), /* queue has been closed/hungup */
  995. Qflow = (1 << 3), /* producer flow controlled */
  996. Qcoalesce = (1 << 4), /* coallesce packets on read */
  997. Qkick = (1 << 5), /* always call the kick routine after qwrite */
  998. };
  999. /* Fast system call struct -- these are dynamically allocted in Proc struct */
  1000. struct Fastcall {
  1001. int scnum;
  1002. Chan *c;
  1003. void (*fun)(Ar0 *, Fastcall *);
  1004. void *buf;
  1005. int n;
  1006. i64 offset;
  1007. };
  1008. #define DEVDOTDOT -1