portdat.h 27 KB

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