portdat.h 26 KB

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