dat.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 ACVctl ACVctl;
  10. typedef struct Conf Conf;
  11. typedef struct Confmem Confmem;
  12. typedef struct Fxsave Fxsave;
  13. typedef struct ICC ICC;
  14. typedef struct ICCparms ICCparms;
  15. typedef struct ISAConf ISAConf;
  16. typedef struct Label Label;
  17. typedef struct Lock Lock;
  18. typedef struct MCPU MCPU;
  19. typedef struct MFPU MFPU;
  20. typedef struct MMMU MMMU;
  21. typedef struct NIX NIX;
  22. typedef struct Mach Mach;
  23. typedef u64 Mpl;
  24. typedef struct Page Page;
  25. typedef struct Pcidev Pcidev;
  26. typedef struct PAMap PAMap;
  27. typedef struct PFPU PFPU;
  28. typedef struct PmcCtr PmcCtr;
  29. typedef struct PmcCtl PmcCtl;
  30. typedef struct PmcWait PmcWait;
  31. typedef struct PMMU PMMU;
  32. typedef struct PNOTIFY PNOTIFY;
  33. typedef u64 PTE;
  34. typedef struct Proc Proc;
  35. typedef struct Sys Sys;
  36. typedef struct Stackframe Stackframe;
  37. typedef u64 uintmem; /* Physical address (hideous) */
  38. typedef struct Ureg Ureg;
  39. typedef struct Vctl Vctl;
  40. /*
  41. * Conversion for Ureg to gdb reg. This avoids a lot of nonsense
  42. * in the outside world.
  43. */
  44. enum regnames {
  45. GDB_AX, /* 0 */
  46. GDB_BX, /* 1 */
  47. GDB_CX, /* 2 */
  48. GDB_DX, /* 3 */
  49. GDB_SI, /* 4 */
  50. GDB_DI, /* 5 */
  51. GDB_BP, /* 6 */
  52. GDB_SP, /* 7 */
  53. GDB_R8, /* 8 */
  54. GDB_R9, /* 9 */
  55. GDB_R10, /* 10 */
  56. GDB_R11, /* 11 */
  57. GDB_R12, /* 12 */
  58. GDB_R13, /* 13 */
  59. GDB_R14, /* 14 */
  60. GDB_R15, /* 15 */
  61. GDB_PC, /* 16 */
  62. GDB_PS, /* 17 */
  63. GDB_CS, /* 18 */
  64. GDB_SS, /* 19 */
  65. GDB_DS, /* 20 */
  66. GDB_ES, /* 21 */
  67. GDB_FS, /* 22 */
  68. GDB_GS, /* 23 */
  69. };
  70. #define DBG_MAX_REG_NUM 24
  71. /* 17 64 bit regs and 5 32 bit regs */
  72. #define GDB_NUMREGBYTES ((17 * 8) + (7 * 4))
  73. #define MAXSYSARG 5 /* for mount(fd, afd, mpt, flag, arg) */
  74. /*
  75. * parameters for sysproc.c
  76. */
  77. #define AOUT_MAGIC (S_MAGIC)
  78. #define ELF_MAGIC (ELF_MAG)
  79. /*
  80. * machine dependent definitions used by ../port/portdat.h
  81. */
  82. struct Lock {
  83. u32 key;
  84. int isilock;
  85. Mpl pl;
  86. usize _pc;
  87. Proc *p;
  88. Mach *m;
  89. u64 lockcycles;
  90. };
  91. struct Label {
  92. usize sp;
  93. usize pc;
  94. usize fp;
  95. usize _pad[13];
  96. };
  97. struct Fxsave {
  98. u16 fcw; /* x87 control word */
  99. u16 fsw; /* x87 status word */
  100. u8 ftw; /* x87 tag word */
  101. u8 zero; /* 0 */
  102. u16 fop; /* last x87 opcode */
  103. u64 rip; /* last x87 instruction pointer */
  104. u64 rdp; /* last x87 data pointer */
  105. u32 mxcsr; /* MMX control and status */
  106. u32 mxcsrmask; /* supported MMX feature bits */
  107. unsigned char st[128]; /* shared 64-bit media and x87 regs */
  108. unsigned char xmm[256]; /* 128-bit media regs */
  109. unsigned char ign[96]; /* reserved, ignored */
  110. } __attribute__((aligned(16)));
  111. /*
  112. * FPU stuff in Proc
  113. */
  114. struct PFPU {
  115. Fxsave fxsave;
  116. };
  117. /*
  118. * MMU stuff in Proc
  119. */
  120. #define NCOLOR 1
  121. struct PMMU {
  122. Page *root;
  123. };
  124. /*
  125. * things saved in the Proc structure during a notify
  126. */
  127. struct PNOTIFY {
  128. // void emptiness;
  129. char emptiness;
  130. };
  131. struct Confmem {
  132. usize base;
  133. usize npage;
  134. usize kbase;
  135. usize klimit;
  136. };
  137. struct Conf {
  138. u32 nproc; /* processes */
  139. Confmem mem[4]; /* physical memory */
  140. u64 npage; /* total physical pages of memory */
  141. usize upages; /* user page pool */
  142. u32 copymode; /* 0 is copy on write, 1 is copy on reference */
  143. u32 ialloc; /* max interrupt time allocation in bytes */
  144. u32 nimage; /* number of page cache image headers */
  145. };
  146. enum {
  147. NPGSZ = 4 /* # of supported pages sizes in Mach */
  148. };
  149. #include "../port/portdat.h"
  150. /*
  151. * CPU stuff in Mach.
  152. */
  153. struct MCPU {
  154. u32 cpuinfo[3][4]; /* CPUID Functions 0, 1, and 5 (n.b.: 2-4 are invalid) */
  155. int ncpuinfos; /* number of standard entries */
  156. int ncpuinfoe; /* number of extended entries */
  157. int isintelcpu; /* */
  158. };
  159. /*
  160. * FPU stuff in Mach.
  161. */
  162. struct MFPU {
  163. u16 fcw; /* x87 control word */
  164. u32 mxcsr; /* MMX control and status */
  165. u32 mxcsrmask; /* supported MMX feature bits */
  166. };
  167. struct NIX {
  168. ICC *icc; /* inter-core call */
  169. int nixtype;
  170. };
  171. /*
  172. * MMU stuff in Mach.
  173. */
  174. struct MMMU {
  175. usize cr2;
  176. Page *pml4; /* pml4 for this processor */
  177. Page pml4kludge; /* NIX KLUDGE: we need a page */
  178. };
  179. /*
  180. * Inter core calls
  181. */
  182. enum {
  183. ICCLNSZ = 128, /* Cache line size for inter core calls */
  184. ICCOK = 0, /* Return codes: Ok; trap; syscall */
  185. ICCTRAP,
  186. ICCSYSCALL
  187. };
  188. struct ICC {
  189. /* fn is kept in its own cache line */
  190. alignas(ICCLNSZ) void (*fn)(void);
  191. int flushtlb; /* on the AC, before running fn */
  192. int rc; /* return code from AC to TC */
  193. char *note; /* to be posted in the TC after returning */
  194. unsigned char data[ICCLNSZ]; /* sent to the AC */
  195. };
  196. /*
  197. * hw perf counters
  198. */
  199. struct PmcCtl {
  200. Ref r;
  201. u32 _coreno;
  202. int enab;
  203. int user;
  204. int os;
  205. int nodesc;
  206. char descstr[KNAMELEN];
  207. int reset;
  208. };
  209. struct PmcWait {
  210. Ref r;
  211. Rendez rend;
  212. PmcWait *next;
  213. };
  214. struct PmcCtr {
  215. int stale;
  216. PmcWait *wq;
  217. u64 ctr;
  218. int ctrset;
  219. PmcCtl PmcCtl;
  220. int ctlset;
  221. };
  222. enum {
  223. PmcMaxCtrs = 4,
  224. PmcIgn = 0,
  225. PmcGet = 1,
  226. PmcSet = 2,
  227. };
  228. /*
  229. * Per processor information.
  230. *
  231. * The offsets of the first few elements may be known
  232. * to low-level assembly code, so do not re-order:
  233. * self - machp()
  234. * splpc - splhi, spllo, splx
  235. * proc - syscallentry
  236. * stack - acsyscall
  237. * externup - externup()
  238. */
  239. struct Mach {
  240. /* WARNING! Known to assembly! */
  241. usize self; /* %gs:0 still gives us a Mach* */
  242. u64 splpc; /* pc of last caller to splhi */
  243. Proc *proc; /* current process on this processor */
  244. usize stack; /* mach stack, kstack is in proc->kstack */
  245. usize rathole; /* to save a reg in syscallentry */
  246. Proc *externup; /* Forsyth recommends we replace the global up with this. */
  247. /* end warning, I think */
  248. int machno; /* physical id of processor */
  249. int apicno;
  250. int online;
  251. MMMU MMU;
  252. unsigned char *vsvm;
  253. void *gdt;
  254. void *tss;
  255. u64 ticks; /* of the clock since boot time */
  256. Label sched; /* scheduler wakeup */
  257. Lock alarmlock; /* access to alarm list */
  258. void *alarm; /* alarms bound to this clock */
  259. int inclockintr;
  260. Proc *readied; /* old runproc, only relevant if kernel booted with nosmp (-n append) */
  261. u64 schedticks; /* next forced context switch, same as above */
  262. u64 qstart; /* time when up started running */
  263. int qexpired; /* quantum expired */
  264. int tlbfault;
  265. int tlbpurge;
  266. int pfault;
  267. int cs;
  268. int syscall;
  269. int intr;
  270. int mmuflush; /* make current proc flush it's mmu state */
  271. int ilockdepth;
  272. Perf perf; /* performance counters */
  273. int inidle; /* profiling */
  274. int lastintr;
  275. Lock apictimerlock;
  276. u64 cyclefreq; /* Frequency of user readable cycle counter */
  277. i64 cpuhz;
  278. int cpumhz;
  279. u64 rdtsc;
  280. Lock pmclock;
  281. PmcCtr pmc[PmcMaxCtrs];
  282. MFPU FPU;
  283. MCPU CPU;
  284. NIX NIX;
  285. /* for restoring pre-AMP scheduler */
  286. Sched *sch;
  287. int load;
  288. };
  289. static_assert(sizeof(Mach) <= PGSZ, "Mach is too big");
  290. struct Stackframe {
  291. Stackframe *next;
  292. usize pc;
  293. };
  294. /*
  295. * This is the low memory map, between 1MiB and 2MiB.
  296. *
  297. * It is located there to allow fundamental data structures to be
  298. * created and used before knowing where free memory begins
  299. * (e.g. there may be modules located after the kernel BSS end).
  300. * The layout is known in the bootstrap code in entry.S
  301. *
  302. * It is logically two parts: the per processor data structures
  303. * for the bootstrap processor (stack, Mach, vsvm, and page tables),
  304. * and the global information about the system (syspage, ptrpage).
  305. * Some of the elements must be aligned on page boundaries, hence
  306. * the unions.
  307. */
  308. struct Sys {
  309. alignas(4096) unsigned char machstk[MACHSTKSZ];
  310. PTE ipml4[PTSZ / sizeof(PTE)]; // Only used very early in boot
  311. PTE epml4[PTSZ / sizeof(PTE)]; // Only used for ...
  312. PTE epml3[PTSZ / sizeof(PTE)]; // ...BSP initialization...
  313. PTE epml2[PTSZ / sizeof(PTE)][4]; // ...and AP early boot.
  314. PTE pml4[PTSZ / sizeof(PTE)]; // Real PML4
  315. PTE pml3[((128 + 64) * PTSZ) / sizeof(PTE)];
  316. unsigned char vsvmpage[4 * KiB];
  317. alignas(4096) Mach mach;
  318. alignas(4096) Mach *machptr[MACHMAX];
  319. u64 pmstart; /* physical memory */
  320. u64 pmend; /* total span */
  321. u64 epoch; /* crude time synchronisation */
  322. int nc[NIXROLES]; /* number of online processors */
  323. int nmach;
  324. int load;
  325. u64 ticks; /* of the clock since boot time */
  326. u64 cyclefreq; /* Frequency of user readable cycle counter (mach 0) */
  327. u32 pgszlg2[NPGSZ]; /* per Mach or per Sys? */
  328. u32 pgszmask[NPGSZ]; /* Per sys -aki */
  329. u32 pgsz[NPGSZ];
  330. int npgsz;
  331. };
  332. static_assert(sizeof(Sys) <= (1 * MiB - 1 * KiB), "Sys is too big");
  333. extern Sys *const sys;
  334. #define MACHP(x) (sys->machptr[(x)])
  335. /*
  336. * The Physical Address Map. This describes the physical address
  337. * space layout of the machine, also taking into account where the
  338. * kernel is loaded, multiboot modules, etc. Unused regions do not
  339. * appear in the map.
  340. */
  341. #define PHYSADDRSIZE (1ULL << 46)
  342. struct PAMap {
  343. u64 addr;
  344. usize size;
  345. int type;
  346. PAMap *next;
  347. };
  348. enum {
  349. PamNONE = 0,
  350. PamMEMORY,
  351. PamRESERVED,
  352. PamACPI,
  353. PamPRESERVE,
  354. PamUNUSABLE,
  355. PamDEV,
  356. PamMODULE,
  357. PamKTEXT,
  358. PamKRDONLY,
  359. PamKRDWR,
  360. };
  361. extern PAMap *pamap;
  362. /*
  363. * KMap
  364. */
  365. typedef void KMap;
  366. extern KMap *kmap(Page *);
  367. #define kunmap(k)
  368. #define VA(k) PTR2UINT(k)
  369. struct {
  370. Lock l;
  371. int nonline; /* # of active CPUs */
  372. int nbooting; /* # of CPUs waiting for the bTC to go */
  373. int exiting; /* shutdown */
  374. int ispanic; /* shutdown in response to a panic */
  375. int thunderbirdsarego; /* lets the added processors continue */
  376. } active;
  377. /*
  378. * a parsed plan9.ini line
  379. */
  380. #define NISAOPT 8
  381. struct ISAConf {
  382. char *type;
  383. usize port;
  384. int irq;
  385. u32 dma;
  386. usize mem;
  387. usize size;
  388. u32 freq;
  389. int nopt;
  390. char *opt[NISAOPT];
  391. };
  392. /*
  393. * The Mach structures must be available via the per-processor
  394. * MMU information array machptr, mainly for disambiguation and access to
  395. * the clock which is only maintained by the bootstrap processor (0).
  396. */
  397. extern char *rolename[];
  398. /*
  399. * Horrid.
  400. */
  401. // HARVEY: TODO: bring this back, it's actually nice. Or do something better.
  402. // Talk to Ron before you condemn it.
  403. #ifdef _DBGC_
  404. #define DBGFLG (dbgflg[_DBGC_])
  405. #else
  406. #define DBGFLG (0)
  407. #endif /* _DBGC_ */
  408. #define DBG(...) \
  409. do { \
  410. if(DBGFLG) \
  411. dbgprint(__VA_ARGS__); \
  412. } while(0)
  413. extern char dbgflg[256];
  414. #define dbgprint print /* for now */