portdat.h 26 KB

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