1
0

portdat.h 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 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 l;
  81. int ref;
  82. };
  83. struct Rendez
  84. {
  85. Lock l;
  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 ql;
  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 r; /* 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 r;
  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 r;
  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 l;
  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 l;
  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 machp()->pgsz[] */
  356. };
  357. struct Image
  358. {
  359. Ref r;
  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 PTENOEXEC (1<<3)
  387. #define PTEUNCACHED (1<<4)
  388. struct Pte
  389. {
  390. Page **first; /* First used entry */
  391. Page **last; /* Last used entry */
  392. Page *pages[]; /* Page map for this chunk of pte */
  393. };
  394. /* Segment types */
  395. enum
  396. {
  397. /* TODO(aki): these types are going to go */
  398. SG_TYPE = 0xf, /* Mask type of segment */
  399. SG_BAD0 = 0x0,
  400. SG_TEXT = 0x1,
  401. SG_DATA = 0x2,
  402. SG_BSS = 0x3,
  403. SG_STACK = 0x4,
  404. SG_SHARED = 0x5,
  405. SG_PHYSICAL = 0x6,
  406. SG_MMAP = 0x7,
  407. SG_LOAD = 0x8, /* replaces SG_TEXT, SG_DATA */
  408. SG_PERM = 0xf0,
  409. SG_READ = 0x10,
  410. SG_WRITE = 0x20,
  411. SG_EXEC = 0x40,
  412. SG_FLAG = 0xf00,
  413. SG_CACHED = 0x100, /* Physseg can be cached */
  414. SG_CEXEC = 0x200, /* Detach at exec */
  415. SG_ZIO = 0x400, /* used for zero copy */
  416. SG_KZIO = 0x800, /* kernel zero copy segment */
  417. };
  418. extern char *segtypes[]; /* port/segment.c */
  419. enum
  420. {
  421. FT_WRITE = 0,
  422. FT_READ,
  423. FT_EXEC,
  424. };
  425. extern char *faulttypes[]; /* port/fault.c */
  426. #define PG_ONSWAP 1
  427. #define onswap(s) (PTR2UINT(s) & PG_ONSWAP)
  428. #define pagedout(s) (PTR2UINT(s) == 0 || onswap(s))
  429. #define swapaddr(s) (PTR2UINT(s) & ~PG_ONSWAP)
  430. #define SEGMAXPG (SEGMAPSIZE)
  431. struct Physseg
  432. {
  433. uint32_t attr; /* Segment attributes */
  434. char *name; /* Attach name */
  435. uintmem pa; /* Physical address */
  436. usize size; /* Maximum segment size in pages */
  437. int pgszi; /* Page size index in Mach */
  438. Page *(*pgalloc)(Segment*, uintptr_t); /* Allocation if we need it */
  439. void (*pgfree)(Page*);
  440. uintptr_t gva; /* optional global virtual address */
  441. };
  442. struct Sema
  443. {
  444. Rendez rend;
  445. int* addr;
  446. int waiting;
  447. Sema* next;
  448. Sema* prev;
  449. };
  450. /* NIX semaphores */
  451. struct Sem
  452. {
  453. Lock l;
  454. int* np; /* user-level semaphore */
  455. Proc** q;
  456. int nq;
  457. Sem* next; /* in list of semaphores for this Segment */
  458. };
  459. /* NIX semaphores */
  460. struct Sems
  461. {
  462. Sem** s;
  463. int ns;
  464. };
  465. /* Zero copy per-segment information (locked using Segment.lk) */
  466. struct Zseg
  467. {
  468. void* map; /* memory map for buffers within this segment */
  469. uintptr_t *addr; /* array of addresses released */
  470. int naddr; /* size allocated for the array */
  471. int end; /* 1+ last used index in addr */
  472. Rendez rr; /* process waiting to read free addresses */
  473. };
  474. #define NOCOLOR -1
  475. /* demand loading params of a segment */
  476. struct Ldseg {
  477. int64_t memsz;
  478. int64_t filesz;
  479. int64_t pg0fileoff;
  480. uintptr_t pg0vaddr;
  481. uint32_t pg0off;
  482. uint32_t pgsz;
  483. uint16_t type;
  484. };
  485. struct Segment
  486. {
  487. Ref r;
  488. QLock lk;
  489. uint16_t steal; /* Page stealer lock */
  490. uint16_t type; /* segment type */
  491. int pgszi; /* page size index in Mach MMMU */
  492. uint ptepertab;
  493. int color;
  494. uintptr_t base; /* virtual base */
  495. uintptr_t top; /* virtual top */
  496. usize size; /* size in pages */
  497. Ldseg ldseg;
  498. int flushme; /* maintain icache for this segment */
  499. Image *image; /* text in file attached to this segment */
  500. Physseg *pseg;
  501. uint32_t *profile; /* Tick profile area */
  502. Pte **map;
  503. int mapsize;
  504. Pte *ssegmap[SSEGMAPSIZE];
  505. Lock semalock;
  506. Sema sema;
  507. Sems sems;
  508. Zseg zseg;
  509. };
  510. /*
  511. * NIX zero-copy IO structure.
  512. */
  513. struct Kzio
  514. {
  515. Zio Zio;
  516. Segment* seg;
  517. };
  518. enum
  519. {
  520. RENDLOG = 5,
  521. RENDHASH = 1<<RENDLOG, /* Hash to lookup rendezvous tags */
  522. MNTLOG = 5,
  523. MNTHASH = 1<<MNTLOG, /* Hash to walk mount table */
  524. NFD = 100, /* per process file descriptors */
  525. PGHLOG = 9,
  526. PGHSIZE = 1<<PGHLOG, /* Page hash for image lookup */
  527. };
  528. #define REND(p,s) ((p)->rendhash[(s)&((1<<RENDLOG)-1)])
  529. #define MOUNTH(p,qid) ((p)->mnthash[(qid).path&((1<<MNTLOG)-1)])
  530. struct Pgrp
  531. {
  532. Ref r; /* also used as a lock when mounting */
  533. int noattach;
  534. uint32_t pgrpid;
  535. QLock debug; /* single access via devproc.c */
  536. RWlock ns; /* Namespace n read/one write lock */
  537. Mhead *mnthash[MNTHASH];
  538. };
  539. struct Rgrp
  540. {
  541. Ref r; /* the Ref's lock is also the Rgrp's lock */
  542. Proc *rendhash[RENDHASH]; /* Rendezvous tag hash */
  543. };
  544. struct Egrp
  545. {
  546. Ref r;
  547. RWlock rwl;
  548. Evalue **ent;
  549. int nent;
  550. int ment;
  551. uint32_t path; /* qid.path of next Evalue to be allocated */
  552. uint32_t vers; /* of Egrp */
  553. };
  554. struct Evalue
  555. {
  556. char *name;
  557. char *value;
  558. int len;
  559. Evalue *link;
  560. Qid qid;
  561. };
  562. struct Fgrp
  563. {
  564. Ref r;
  565. Chan **fd;
  566. int nfd; /* number allocated */
  567. int maxfd; /* highest fd in use */
  568. int exceed; /* debugging */
  569. };
  570. enum
  571. {
  572. DELTAFD = 20 /* incremental increase in Fgrp.fd's */
  573. };
  574. struct Pgsza
  575. {
  576. uint32_t freecount; /* how many pages in the free list? */
  577. Ref npages; /* how many pages of this size? */
  578. Page *head; /* MRU */
  579. Page *tail; /* LRU */
  580. };
  581. struct Pgalloc
  582. {
  583. Lock l;
  584. int userinit; /* working in user init mode */
  585. Pgsza pgsza[NPGSZ]; /* allocs for m->npgsz page sizes */
  586. Page* hash[PGHSIZE]; /* only used for user pages */
  587. Lock hashlock;
  588. Rendez rend; /* sleep for free mem */
  589. QLock pwait; /* queue of procs waiting for this pgsz */
  590. };
  591. struct Waitq
  592. {
  593. Waitmsg w;
  594. Waitq *next;
  595. };
  596. /*
  597. * fasttick timer interrupts
  598. */
  599. enum {
  600. /* Mode */
  601. Trelative, /* timer programmed in ns from now */
  602. Tperiodic, /* periodic timer, period in ns */
  603. };
  604. struct Timer
  605. {
  606. /* Public interface */
  607. int tmode; /* See above */
  608. int64_t tns; /* meaning defined by mode */
  609. void (*tf)(Ureg*, Timer*);
  610. void *ta;
  611. /* Internal */
  612. Lock l;
  613. Timers *tt; /* Timers queue this timer runs on */
  614. int64_t twhen; /* ns represented in fastticks */
  615. Timer *tnext;
  616. };
  617. enum
  618. {
  619. RFNAMEG = (1<<0),
  620. RFENVG = (1<<1),
  621. RFFDG = (1<<2),
  622. RFNOTEG = (1<<3),
  623. RFPROC = (1<<4),
  624. RFMEM = (1<<5),
  625. RFNOWAIT = (1<<6),
  626. RFCNAMEG = (1<<10),
  627. RFCENVG = (1<<11),
  628. RFCFDG = (1<<12),
  629. RFREND = (1<<13),
  630. RFNOMNT = (1<<14),
  631. RFPREPAGE = (1<<15),
  632. RFCPREPAGE = (1<<16),
  633. RFCORE = (1<<17),
  634. RFCCORE = (1<<18),
  635. };
  636. /* execac */
  637. enum
  638. {
  639. EXTC = 0, /* exec on time-sharing */
  640. EXAC, /* want an AC for the exec'd image */
  641. EXXC, /* want an XC for the exec'd image */
  642. };
  643. /*
  644. * Maximum number of process memory segments
  645. */
  646. enum {
  647. NSEG = 12
  648. };
  649. enum
  650. {
  651. Dead = 0, /* Process states */
  652. Moribund,
  653. Ready,
  654. Scheding,
  655. Running,
  656. Queueing,
  657. QueueingR,
  658. QueueingW,
  659. Wakeme,
  660. Broken,
  661. Stopped,
  662. Rendezvous,
  663. Waitrelease,
  664. Exotic, /* NIX */
  665. Semdown,
  666. Proc_stopme = 1, /* devproc requests */
  667. Proc_exitme,
  668. Proc_traceme,
  669. Proc_exitbig,
  670. Proc_tracesyscall,
  671. Proc_toac,
  672. Proc_totc,
  673. TUser = 0, /* Proc.time */
  674. TSys,
  675. TReal,
  676. TCUser,
  677. TCSys,
  678. TCReal,
  679. NERR = 64,
  680. NNOTE = 5,
  681. Npriq = 20, /* number of scheduler priority levels */
  682. Nrq = Npriq+2, /* number of priority levels including real time */
  683. PriRelease = Npriq, /* released edf processes */
  684. PriEdf = Npriq+1, /* active edf processes */
  685. PriNormal = 10, /* base priority for normal processes */
  686. PriExtra = Npriq-1, /* edf processes at high best-effort pri */
  687. PriKproc = 13, /* base priority for kernel processes */
  688. PriRoot = 13, /* base priority for root processes */
  689. };
  690. struct Schedq
  691. {
  692. Proc* head;
  693. Proc* tail;
  694. int n;
  695. };
  696. struct Sched
  697. {
  698. Lock l; /* runq */
  699. int nrdy;
  700. uint32_t delayedscheds; /* statistics */
  701. int32_t skipscheds;
  702. int32_t preempts;
  703. int schedgain;
  704. uint32_t balancetime;
  705. Schedq runq[Nrq];
  706. uint32_t runvec;
  707. int nmach; /* # of cores with this color */
  708. uint32_t nrun; /* to compute load */
  709. };
  710. typedef union Ar0 Ar0;
  711. union Ar0 {
  712. int i;
  713. int32_t l;
  714. uintptr_t p;
  715. usize u;
  716. void* v;
  717. int64_t vl;
  718. };
  719. typedef struct Nixpctl Nixpctl;
  720. #pragma incomplete Nixpctl
  721. struct Proc
  722. {
  723. Label sched; /* known to l.s */
  724. char *kstack; /* known to l.s */
  725. void *dbgreg; /* known to l.s User registers for devproc */
  726. uintptr_t tls; /* known to l.s thread local storage */
  727. Mach *mach; /* machine running this proc */
  728. char *text;
  729. char *user;
  730. char *args;
  731. int nargs; /* number of bytes of args */
  732. Proc *rnext; /* next process in run queue */
  733. Proc *qnext; /* next process on queue for a QLock */
  734. QLock *qlock; /* addr of qlock being queued for DEBUG */
  735. int state;
  736. char *psstate; /* What /proc/#/status reports */
  737. Segment *seg[NSEG];
  738. QLock seglock; /* locked whenever seg[] changes */
  739. int pid;
  740. int index; /* index (slot) in proc array */
  741. int ref; /* indirect reference */
  742. int noteid; /* Equivalent of note group */
  743. Proc *pidhash; /* next proc in pid hash */
  744. Lock exl; /* Lock count and waitq */
  745. Waitq *waitq; /* Exited processes wait children */
  746. int nchild; /* Number of living children */
  747. int nwait; /* Number of uncollected wait records */
  748. QLock qwaitr;
  749. Rendez waitr; /* Place to hang out in wait */
  750. Proc *parent;
  751. Pgrp *pgrp; /* Process group for namespace */
  752. Egrp *egrp; /* Environment group */
  753. Fgrp *fgrp; /* File descriptor group */
  754. Rgrp *rgrp; /* Rendez group */
  755. Fgrp *closingfgrp; /* used during teardown */
  756. int parentpid;
  757. uint32_t time[6]; /* User, Sys, Real; child U, S, R */
  758. uint64_t kentry; /* Kernel entry time stamp (for profiling) */
  759. /*
  760. * pcycles: cycles spent in this process (updated on procsave/restore)
  761. * when this is the current proc and we're in the kernel
  762. * (procrestores outnumber procsaves by one)
  763. * the number of cycles spent in the proc is pcycles + cycles()
  764. * when this is not the current process or we're in user mode
  765. * (procrestores and procsaves balance), it is pcycles.
  766. */
  767. int64_t pcycles;
  768. int insyscall;
  769. QLock debug; /* to access debugging elements of User */
  770. Proc *pdbg; /* the debugging process */
  771. uint32_t procmode; /* proc device file mode */
  772. uint32_t privatemem; /* proc does not let anyone read mem */
  773. int hang; /* hang at next exec for debug */
  774. int procctl; /* Control for /proc debugging */
  775. uintptr pc; /* DEBUG only */
  776. Lock rlock; /* sync sleep/wakeup with postnote */
  777. Rendez *r; /* rendezvous point slept on */
  778. Rendez sleep; /* place for syssleep/debug/tsleep */
  779. int notepending; /* note issued but not acted on */
  780. int kp; /* true if a kernel process */
  781. Proc *palarm; /* Next alarm time */
  782. uint32_t alarm; /* Time of call */
  783. int newtlb; /* Pager has changed my pte's, I must flush */
  784. int noswap; /* process is not swappable */
  785. uintptr rendtag; /* Tag for rendezvous */
  786. uintptr rendval; /* Value for rendezvous */
  787. Proc *rendhash; /* Hash list for tag values */
  788. Timer Timer; /* For tsleep and real-time */
  789. Rendez *trend;
  790. int (*tfn)(void*);
  791. void (*kpfun)(void*);
  792. void *kparg;
  793. int scallnr; /* system call number */
  794. unsigned char arg[MAXSYSARG*sizeof(void*)]; /* system call arguments */
  795. int nerrlab;
  796. Label errlab[NERR];
  797. char *syserrstr; /* last error from a system call, errbuf0 or 1 */
  798. char *errstr; /* reason we're unwinding the error stack, errbuf1 or 0 */
  799. char errbuf0[ERRMAX];
  800. char errbuf1[ERRMAX];
  801. char genbuf[128]; /* buffer used e.g. for last name element from namec */
  802. Chan *slash;
  803. Chan *dot;
  804. Note note[NNOTE];
  805. short nnote;
  806. short notified; /* sysnoted is due */
  807. Note lastnote;
  808. void (*notify)(void*, char*);
  809. Lock *lockwait;
  810. Lock *lastlock; /* debugging */
  811. Lock *lastilock; /* debugging */
  812. Mach *wired;
  813. Mach *mp; /* machine this process last ran on */
  814. int nlocks; /* number of locks held by proc */
  815. uint32_t delaysched;
  816. uint32_t priority; /* priority level */
  817. uint32_t basepri; /* base priority level */
  818. int fixedpri; /* priority level does not change */
  819. uint32_t cpu; /* cpu average */
  820. uint32_t lastupdate;
  821. uint32_t readytime; /* time process came ready */
  822. uint32_t movetime; /* last time process switched processors */
  823. int preempted; /* true if this process hasn't finished the interrupt
  824. * that last preempted it
  825. */
  826. Edf *edf; /* if non-null, real-time proc, edf contains scheduling params */
  827. int trace; /* process being traced? */
  828. uintptr qpc; /* pc calling last blocking qlock */
  829. int setargs;
  830. void *ureg; /* User registers for notes */
  831. int color;
  832. Fastcall* fc;
  833. int fcount;
  834. char* syscalltrace;
  835. /* NIX */
  836. Mach *ac;
  837. Page *acpml4;
  838. Sem *waitsem;
  839. int prepagemem;
  840. Nixpctl *nixpctl; /* NIX queue based system calls */
  841. uint ntrap; /* # of traps while in this process */
  842. uint nintr; /* # of intrs while in this process */
  843. uint nsyscall; /* # of syscalls made by the process */
  844. uint nactrap; /* # of traps in the AC for this process */
  845. uint nacsyscall; /* # of syscalls in the AC for this process */
  846. uint nicc; /* # of ICCs for the process */
  847. uint64_t actime1; /* ticks as of last call in AC */
  848. uint64_t actime; /* ∑time from call in AC to ret to AC, and... */
  849. uint64_t tctime; /* ∑time from call received to call handled */
  850. int nqtrap; /* # of traps in last quantum */
  851. int nqsyscall; /* # of syscalls in the last quantum */
  852. int nfullq;
  853. /*
  854. * machine specific fpu, mmu and notify
  855. */
  856. PFPU FPU;
  857. PMMU MMU;
  858. PNOTIFY NOTIFY;
  859. /*
  860. * mmap support.
  861. * For addresses that we reference that don't have a mapping,
  862. * if this queue is not NULL, we will send a message on it to be
  863. * handled by some other proc (not ourselves) and block on reading
  864. * a result back.
  865. */
  866. Queue *req, *resp;
  867. };
  868. struct Procalloc
  869. {
  870. Lock l;
  871. int nproc;
  872. Proc* ht[128];
  873. Proc* arena;
  874. Proc* free;
  875. };
  876. enum
  877. {
  878. PRINTSIZE = 256,
  879. MAXCRYPT = 127,
  880. NUMSIZE = 12, /* size of formatted number */
  881. MB = (1024*1024),
  882. /* READSTR was 1000, which is way too small for usb's ctl file */
  883. READSTR = 4000, /* temporary buffer size for device reads */
  884. WRITESTR = 256, /* ctl file write max */
  885. };
  886. extern Conf conf;
  887. extern char* conffile;
  888. extern char* cputype;
  889. extern int cpuserver;
  890. extern char* eve;
  891. extern char hostdomain[];
  892. extern uint8_t initcode[];
  893. extern int kbdbuttons;
  894. extern Ref noteidalloc;
  895. extern int nphysseg;
  896. extern int nsyscall;
  897. extern Pgalloc pga;
  898. extern Physseg physseg[];
  899. extern Procalloc procalloc;
  900. extern uint qiomaxatomic;
  901. extern char* statename[];
  902. extern char* sysname;
  903. typedef struct Systab Systab;
  904. struct Systab {
  905. char* n;
  906. void (*f)(Ar0*, ...);
  907. Ar0 r;
  908. };
  909. extern Systab systab[];
  910. enum
  911. {
  912. LRESPROF = 3,
  913. };
  914. /*
  915. * action log
  916. */
  917. struct Log {
  918. Lock l;
  919. int opens;
  920. char* buf;
  921. char *end;
  922. char *rptr;
  923. int len;
  924. int nlog;
  925. int minread;
  926. int logmask; /* mask of things to debug */
  927. QLock readq;
  928. Rendez readr;
  929. };
  930. struct Logflag {
  931. char* name;
  932. int mask;
  933. };
  934. enum
  935. {
  936. NCMDFIELD = 128
  937. };
  938. struct Cmdbuf
  939. {
  940. char *buf;
  941. char **f;
  942. int nf;
  943. };
  944. struct Cmdtab
  945. {
  946. int index; /* used by client to switch on result */
  947. char *cmd; /* command name */
  948. int narg; /* expected #args; 0 ==> variadic */
  949. };
  950. /*
  951. * routines to access UART hardware
  952. */
  953. struct PhysUart
  954. {
  955. char* name;
  956. Uart* (*pnp)(void);
  957. void (*enable)(Uart*, int);
  958. void (*disable)(Uart*);
  959. void (*kick)(Uart*);
  960. void (*dobreak)(Uart*, int);
  961. int (*baud)(Uart*, int);
  962. int (*bits)(Uart*, int);
  963. int (*stop)(Uart*, int);
  964. int (*parity)(Uart*, int);
  965. void (*modemctl)(Uart*, int);
  966. void (*rts)(Uart*, int);
  967. void (*dtr)(Uart*, int);
  968. int32_t (*status)(Uart*, void*, int32_t, int32_t);
  969. void (*fifo)(Uart*, int);
  970. void (*power)(Uart*, int);
  971. int (*getc)(Uart*); /* polling version for rdb */
  972. void (*putc)(Uart*, int); /* polling version for iprint */
  973. void (*poll)(Uart*); /* polled interrupt routine */
  974. };
  975. enum {
  976. Stagesize= 2048
  977. };
  978. /*
  979. * software UART
  980. */
  981. struct Uart
  982. {
  983. void* regs; /* hardware stuff */
  984. void* saveregs; /* place to put registers on power down */
  985. char* name; /* internal name */
  986. uint32_t freq; /* clock frequency */
  987. int bits; /* bits per character */
  988. int stop; /* stop bits */
  989. int parity; /* even, odd or no parity */
  990. int baud; /* baud rate */
  991. PhysUart*phys;
  992. int console; /* used as a serial console */
  993. int special; /* internal kernel device */
  994. Uart* next; /* list of allocated uarts */
  995. QLock ql;
  996. int type; /* ?? */
  997. int dev;
  998. int opens;
  999. int enabled;
  1000. Uart *elist; /* next enabled interface */
  1001. int perr; /* parity errors */
  1002. int ferr; /* framing errors */
  1003. int oerr; /* rcvr overruns */
  1004. int berr; /* no input buffers */
  1005. int serr; /* input queue overflow */
  1006. /* buffers */
  1007. int (*putc)(Queue*, int);
  1008. Queue *iq;
  1009. Queue *oq;
  1010. Lock rlock;
  1011. unsigned char istage[Stagesize];
  1012. unsigned char *iw;
  1013. unsigned char *ir;
  1014. unsigned char *ie;
  1015. Lock tlock; /* transmit */
  1016. unsigned char ostage[Stagesize];
  1017. unsigned char *op;
  1018. unsigned char *oe;
  1019. int drain;
  1020. int modem; /* hardware flow control on */
  1021. int xonoff; /* software flow control on */
  1022. int blocked;
  1023. int cts, dsr, dcd; /* keep track of modem status */
  1024. int ctsbackoff;
  1025. int hup_dsr, hup_dcd; /* send hangup upstream? */
  1026. int dohup;
  1027. Rendez rend;
  1028. };
  1029. extern Uart* consuart;
  1030. /*
  1031. * performance timers, all units in perfticks
  1032. */
  1033. struct Perf
  1034. {
  1035. uint32_t intrts; /* time of last interrupt */
  1036. uint32_t inintr; /* time since last clock tick in interrupt handlers */
  1037. uint32_t avg_inintr; /* avg time per clock tick in interrupt handlers */
  1038. uint32_t inidle; /* time since last clock tick in idle loop */
  1039. uint32_t avg_inidle; /* avg time per clock tick in idle loop */
  1040. uint32_t last; /* value of perfticks() at last clock tick */
  1041. uint32_t period; /* perfticks() per clock tick */
  1042. };
  1043. struct Watchdog
  1044. {
  1045. void (*enable)(void); /* watchdog enable */
  1046. void (*disable)(void); /* watchdog disable */
  1047. void (*restart)(void); /* watchdog restart */
  1048. void (*stat)(char*, char*); /* watchdog statistics */
  1049. };
  1050. struct Watermark
  1051. {
  1052. int highwater;
  1053. int curr;
  1054. int max;
  1055. int hitmax; /* count: how many times hit max? */
  1056. char *name;
  1057. };
  1058. /* queue state bits, Qmsg, Qcoalesce, and Qkick can be set in qopen */
  1059. enum
  1060. {
  1061. /* Queue.state */
  1062. Qstarve = (1<<0), /* consumer starved */
  1063. Qmsg = (1<<1), /* message stream */
  1064. Qclosed = (1<<2), /* queue has been closed/hungup */
  1065. Qflow = (1<<3), /* producer flow controlled */
  1066. Qcoalesce = (1<<4), /* coallesce packets on read */
  1067. Qkick = (1<<5), /* always call the kick routine after qwrite */
  1068. };
  1069. /* Fast system call struct -- these are dynamically allocted in Proc struct */
  1070. struct Fastcall {
  1071. int scnum;
  1072. Chan* c;
  1073. void (*fun)(Ar0*, Fastcall*);
  1074. void* buf;
  1075. int n;
  1076. int64_t offset;
  1077. };
  1078. #define DEVDOTDOT -1
  1079. #pragma varargck type "I" unsigned char*
  1080. #pragma varargck type "V" unsigned char*
  1081. #pragma varargck type "E" unsigned char*
  1082. #pragma varargck type "M" unsigned char*
  1083. #pragma varargck type "W" uint64_t
  1084. #pragma varargck type "Z" Kzio*