portdat.h 26 KB

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