mem.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Memory and machine-specific definitions. Used in C and assembler.
  3. */
  4. #define MIN(a, b) ((a) < (b)? (a): (b))
  5. #define MAX(a, b) ((a) > (b)? (a): (b))
  6. /*
  7. * Sizes
  8. */
  9. #define BI2BY 8 /* bits per byte */
  10. #define BI2WD 32 /* bits per word */
  11. #define BY2WD 4 /* bytes per word */
  12. #define BY2V 8 /* bytes per double word */
  13. #define BY2PG 4096 /* bytes per page */
  14. #define WD2PG (BY2PG/BY2WD) /* words per page */
  15. #define BY2XPG (4096*1024) /* bytes per big page */
  16. #define PGSHIFT 12 /* log(BY2PG) */
  17. #define ROUND(s, sz) (((s)+((sz)-1))&~((sz)-1))
  18. #define PGROUND(s) ROUND(s, BY2PG)
  19. #define CACHELINESZ 32 /* pentium & later */
  20. #define BLOCKALIGN 8
  21. #define MAXMACH 1 /* max # cpus system can run */
  22. /*
  23. * we use a larger-than-normal kernel stack in the bootstraps as we have
  24. * significant arrays (buffers) on the stack. we typically consume about
  25. * 4.5K of stack.
  26. */
  27. #define KSTACK (4*BY2PG) /* Size of kernel stack */
  28. #define MACHSIZE BY2PG
  29. /*
  30. * Time
  31. */
  32. #define HZ (100) /* clock frequency */
  33. #define MS2HZ (1000/HZ) /* millisec per clock tick */
  34. #define TK2SEC(t) ((t)/HZ) /* ticks to seconds */
  35. /*
  36. * Address spaces
  37. *
  38. * Kernel is at 2GB-4GB
  39. */
  40. #define KZERO 0x80000000 /* base of kernel address space */
  41. #define KSEGM 0xF0000000
  42. #define VPT (KZERO-VPTSIZE)
  43. #define VPTSIZE BY2XPG
  44. #define NVPT (VPTSIZE/BY2WD)
  45. #define KMAP (VPT-KMAPSIZE)
  46. #define KMAPSIZE BY2XPG
  47. #define VMAP (KMAP-VMAPSIZE)
  48. #define VMAPSIZE (0x10000000-VPTSIZE-KMAPSIZE)
  49. #define UZERO 0 /* base of user address space */
  50. #define UTZERO (UZERO+BY2PG) /* first address in user text */
  51. #define USTKTOP (VMAP-BY2PG) /* byte just beyond user stack */
  52. #define USTKSIZE (16*1024*1024) /* size of user stack */
  53. #define TSTKTOP (USTKTOP-USTKSIZE) /* end of new stack in sysexec */
  54. #define TSTKSIZ 100 /* pages in new stack; limits exec args */
  55. /*
  56. * Fundamental addresses - bottom 64kB saved for return to real mode
  57. *
  58. * we need some fixed address space for things that normally fit below the
  59. * kernel and some of it needs to be addressible from real mode, thus under 1MB.
  60. *
  61. * pxe loading starts us at 0x7c00 and non-pxe loading starts us at 0x10000.
  62. *
  63. * this assertion must hold: PDX(TMPADDR) == PDX(MACHADDR).
  64. * see realmode0.s for a description of the Chinese puzzle.
  65. */
  66. /* first 1K is real-mode IVT (vectors) */
  67. /* next 256 bytes are bios data area (bda) */
  68. /* 0x500 to 0x800 unused [768] */
  69. /*
  70. * RMCODE should be the lowest address used in real mode.
  71. * all real-mode buffers, etc. should follow it.
  72. */
  73. #define RMCODE (KZERO+0x800) /* copy of initial KTZERO [2K, magic] */
  74. #define REBOOTADDR (RMCODE-KZERO) /* reboot code - physical address [128] */
  75. #define RMSIZE 2048
  76. /* 0x1000 to 0x1200 unused [512] */
  77. /* CONFADDR must match def'n in kernel being loaded */
  78. #define CONFADDR (KZERO+0x1200) /* cfg passed to kernel [3.5K, fixed] */
  79. #define BIOSXCHG (KZERO+0x2000) /* BIOS data exchange [2K+32] */
  80. /* 0x2820 to 0x2900 unused [224] */
  81. #define RMUADDR (KZERO+0x2900) /* real mode Ureg [128 (76 actual)] */
  82. /* 0x2980 to 0x3000 unused [1664] */
  83. #define IDTADDR (KZERO+0x3000) /* protected-mode idt [2K] */
  84. /* 0x3800 to 0x4000 unused [2K] */
  85. /* we only use one processor for bootstrapping, so merge MACHADDR & CPU0MACH */
  86. #define MACHADDR (KZERO+0x4000) /* as seen by current processor */
  87. #define CPU0MACH MACHADDR /* Mach for bootstrap processor */
  88. #define CPU0GDT (KZERO+0x5000) /* bootstrap processor GDT after Mach */
  89. #define TMPADDR (KZERO+0x6000) /* used for temporary mappings */
  90. /* 0x7000—0x7800 unused [2K], could be extra real-mode stack */
  91. #define PXEBASE 0x7c00 /* pxe loads us here */
  92. #define RMSTACK PXEBASE /* real phys. stack base [1K below] */
  93. #define PBSBASE 0x10000 /* pbs loads us here (at 64K) */
  94. /*
  95. * we used to use 9boot's `end', rounded up, but that was when the boot loader
  96. * was in the first 640K; now end is up around 10MB (at least for 9boot).
  97. * various machines nibble away at the top of the lowest 640K.
  98. */
  99. #define BIOSTABLES (512*1024)
  100. // #define BIOSTABLES (600*1024) /* fails on amd64 */
  101. // #define BIOSTABLES 0x3800 /* 2K: fails on amd64 */
  102. #define Bootkernaddr (9*MB) /* where to put decompressed boot kernel */
  103. #define Bootkernmax (4*MB) /* max size */
  104. #define Unzipbuf (13*MB)
  105. #define Mallocbase (16*MB)
  106. /*
  107. * MemMin is what the bootstrap code in l*.s has already mapped;
  108. * MemMax is the limit of physical memory to scan.
  109. */
  110. #define MemMin (20*MB) /* don't have PTEs for more allocated yet */
  111. #define MemMax (512*MB) /* allow for huge 10gbe buffers */
  112. #define Lowmemsz (640*KB)
  113. #define LOWPTEPAGES (MemMin / (4*MB))
  114. #define Kernelmax (8*MB) /* max size of real kernel, not an address */
  115. /*
  116. * known x86 segments (in GDT) and their selectors
  117. */
  118. #define NULLSEG 0 /* null segment */
  119. #define KDSEG 1 /* kernel data/stack */
  120. #define KESEG 2 /* kernel executable */
  121. #define UDSEG 3 /* user data/stack */
  122. #define UESEG 4 /* user executable */
  123. #define TSSSEG 5 /* task segment */
  124. #define APMCSEG 6 /* APM code segment */
  125. #define APMCSEG16 7 /* APM 16-bit code segment */
  126. #define APMDSEG 8 /* APM data segment */
  127. #define KESEG16 9 /* kernel executable 16-bit */
  128. #define NGDT 10 /* number of GDT entries required */
  129. /* #define APM40SEG 8 /* APM segment 0x40 */
  130. #define SELGDT (0<<2) /* selector is in gdt */
  131. #define SELLDT (1<<2) /* selector is in ldt */
  132. #define SELECTOR(i, t, p) (((i)<<3) | (t) | (p))
  133. #define NULLSEL SELECTOR(NULLSEG, SELGDT, 0)
  134. #define KDSEL SELECTOR(KDSEG, SELGDT, 0)
  135. #define KESEL SELECTOR(KESEG, SELGDT, 0)
  136. #define UESEL SELECTOR(UESEG, SELGDT, 3)
  137. #define UDSEL SELECTOR(UDSEG, SELGDT, 3)
  138. #define TSSSEL SELECTOR(TSSSEG, SELGDT, 0)
  139. #define APMCSEL SELECTOR(APMCSEG, SELGDT, 0)
  140. #define APMCSEL16 SELECTOR(APMCSEG16, SELGDT, 0)
  141. #define APMDSEL SELECTOR(APMDSEG, SELGDT, 0)
  142. /* #define APM40SEL SELECTOR(APM40SEG, SELGDT, 0) */
  143. /*
  144. * fields in segment descriptors
  145. */
  146. #define SEGDATA (0x10<<8) /* data/stack segment */
  147. #define SEGEXEC (0x18<<8) /* executable segment */
  148. #define SEGTSS (0x9<<8) /* TSS segment */
  149. #define SEGCG (0x0C<<8) /* call gate */
  150. #define SEGIG (0x0E<<8) /* interrupt gate */
  151. #define SEGTG (0x0F<<8) /* trap gate */
  152. #define SEGTYPE (0x1F<<8)
  153. #define SEGP (1<<15) /* segment present */
  154. #define SEGPL(x) ((x)<<13) /* priority level */
  155. #define SEGB (1<<22) /* granularity 1==4k (for expand-down) */
  156. #define SEGG (1<<23) /* granularity 1==4k (for other) */
  157. #define SEGE (1<<10) /* expand down */
  158. #define SEGW (1<<9) /* writable (for data/stack) */
  159. #define SEGR (1<<9) /* readable (for code) */
  160. #define SEGD (1<<22) /* default 1==32bit (for code) */
  161. /*
  162. * virtual MMU
  163. */
  164. #define PTEMAPMEM (1024*1024)
  165. #define PTEPERTAB (PTEMAPMEM/BY2PG)
  166. #define SEGMAPSIZE 1984
  167. #define SSEGMAPSIZE 16
  168. #define PPN(x) ((x)&~(BY2PG-1))
  169. /*
  170. * physical MMU
  171. */
  172. #define PTEVALID (1<<0)
  173. #define PTEWT (1<<3)
  174. #define PTEUNCACHED (1<<4)
  175. #define PTEWRITE (1<<1)
  176. #define PTERONLY (0<<1)
  177. #define PTEKERNEL (0<<2)
  178. #define PTEUSER (1<<2)
  179. #define PTESIZE (1<<7)
  180. #define PTEGLOBAL (1<<8)
  181. /* CR0 */
  182. #define PG 0x80000000
  183. /*
  184. * Macros for calculating offsets within the page directory base
  185. * and page tables.
  186. */
  187. #define PDX(va) ((((ulong)(va))>>22) & 0x03FF)
  188. #define PTX(va) ((((ulong)(va))>>12) & 0x03FF)
  189. #define getpgcolor(a) 0