portdat.h 27 KB

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