mem.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 BLOCKALIGN 8
  20. #define MAXMACH 24 /* max # cpus system can run */
  21. #define KSTACK 4096 /* Size of kernel stack */
  22. /*
  23. * Time
  24. */
  25. #define HZ (100) /* clock frequency */
  26. #define MS2HZ (1000/HZ) /* millisec per clock tick */
  27. #define TK2SEC(t) ((t)/HZ) /* ticks to seconds */
  28. /*
  29. * Address spaces
  30. */
  31. #define KZERO 0xF0000000 /* base of kernel address space */
  32. #define KTZERO (KZERO+0x100000) /* first address in kernel text - 9load sits below */
  33. #define VPT (KZERO-VPTSIZE)
  34. #define VPTSIZE BY2XPG
  35. #define NVPT (VPTSIZE/BY2WD)
  36. #define KMAP (VPT-KMAPSIZE)
  37. #define KMAPSIZE BY2XPG
  38. #define VMAP (KMAP-VMAPSIZE)
  39. #define VMAPSIZE (0x10000000-VPTSIZE-KMAPSIZE)
  40. #define UZERO 0 /* base of user address space */
  41. #define UTZERO (UZERO+BY2PG) /* first address in user text */
  42. #define USTKTOP (VMAP-BY2PG) /* byte just beyond user stack */
  43. #define USTKSIZE (16*1024*1024) /* size of user stack */
  44. #define TSTKTOP (USTKTOP-USTKSIZE) /* end of new stack in sysexec */
  45. #define TSTKSIZ 100 /* pages in new stack; limits exec args */
  46. /*
  47. * Fundamental addresses - bottom 64kB saved for return to real mode
  48. */
  49. #define CONFADDR (KZERO+0x1200) /* info passed from boot loader */
  50. #define TMPADDR (KZERO+0x2000) /* used for temporary mappings */
  51. #define APBOOTSTRAP (KZERO+0x3000) /* AP bootstrap code */
  52. #define RMUADDR (KZERO+0x7C00) /* real mode Ureg */
  53. #define RMCODE (KZERO+0x8000) /* copy of first page of KTEXT */
  54. #define RMBUF (KZERO+0x9000) /* buffer for user space - known to vga */
  55. #define IDTADDR (KZERO+0x10800) /* idt */
  56. #define REBOOTADDR (0x11000) /* reboot code - physical address */
  57. #define CPU0PDB (KZERO+0x12000) /* bootstrap processor PDB */
  58. #define CPU0PTE (KZERO+0x13000) /* bootstrap processor PTE's for 0-4MB */
  59. #define CPU0GDT (KZERO+0x14000) /* bootstrap processor GDT */
  60. #define MACHADDR (KZERO+0x15000) /* as seen by current processor */
  61. #define CPU0MACH (KZERO+0x16000) /* Mach for bootstrap processor */
  62. #define MACHSIZE BY2PG
  63. #define CPU0PTE1 (KZERO+0x17000) /* bootstrap processor PTE's for 4MB-8MB */
  64. #define CPU0END (CPU0PTE1+BY2PG)
  65. /*
  66. * N.B. ramscan knows that CPU0END is the end of reserved data
  67. * N.B. _startPADDR knows that CPU0PDB is the first reserved page
  68. * and that there are 6 of them.
  69. */
  70. /*
  71. * known x86 segments (in GDT) and their selectors
  72. */
  73. #define NULLSEG 0 /* null segment */
  74. #define KDSEG 1 /* kernel data/stack */
  75. #define KESEG 2 /* kernel executable */
  76. #define UDSEG 3 /* user data/stack */
  77. #define UESEG 4 /* user executable */
  78. #define TSSSEG 5 /* task segment */
  79. #define APMCSEG 6 /* APM code segment */
  80. #define APMCSEG16 7 /* APM 16-bit code segment */
  81. #define APMDSEG 8 /* APM data segment */
  82. #define KESEG16 9 /* kernel executable 16-bit */
  83. #define NGDT 10 /* number of GDT entries required */
  84. /* #define APM40SEG 8 /* APM segment 0x40 */
  85. #define SELGDT (0<<2) /* selector is in gdt */
  86. #define SELLDT (1<<2) /* selector is in ldt */
  87. #define SELECTOR(i, t, p) (((i)<<3) | (t) | (p))
  88. #define NULLSEL SELECTOR(NULLSEG, SELGDT, 0)
  89. #define KDSEL SELECTOR(KDSEG, SELGDT, 0)
  90. #define KESEL SELECTOR(KESEG, SELGDT, 0)
  91. #define UESEL SELECTOR(UESEG, SELGDT, 3)
  92. #define UDSEL SELECTOR(UDSEG, SELGDT, 3)
  93. #define TSSSEL SELECTOR(TSSSEG, SELGDT, 0)
  94. #define APMCSEL SELECTOR(APMCSEG, SELGDT, 0)
  95. #define APMCSEL16 SELECTOR(APMCSEG16, SELGDT, 0)
  96. #define APMDSEL SELECTOR(APMDSEG, SELGDT, 0)
  97. /* #define APM40SEL SELECTOR(APM40SEG, SELGDT, 0) */
  98. /*
  99. * fields in segment descriptors
  100. */
  101. #define SEGDATA (0x10<<8) /* data/stack segment */
  102. #define SEGEXEC (0x18<<8) /* executable segment */
  103. #define SEGTSS (0x9<<8) /* TSS segment */
  104. #define SEGCG (0x0C<<8) /* call gate */
  105. #define SEGIG (0x0E<<8) /* interrupt gate */
  106. #define SEGTG (0x0F<<8) /* trap gate */
  107. #define SEGTYPE (0x1F<<8)
  108. #define SEGP (1<<15) /* segment present */
  109. #define SEGPL(x) ((x)<<13) /* priority level */
  110. #define SEGB (1<<22) /* granularity 1==4k (for expand-down) */
  111. #define SEGG (1<<23) /* granularity 1==4k (for other) */
  112. #define SEGE (1<<10) /* expand down */
  113. #define SEGW (1<<9) /* writable (for data/stack) */
  114. #define SEGR (1<<9) /* readable (for code) */
  115. #define SEGD (1<<22) /* default 1==32bit (for code) */
  116. /*
  117. * virtual MMU
  118. */
  119. #define PTEMAPMEM (1024*1024)
  120. #define PTEPERTAB (PTEMAPMEM/BY2PG)
  121. #define SEGMAPSIZE 1984
  122. #define SSEGMAPSIZE 16
  123. #define PPN(x) ((x)&~(BY2PG-1))
  124. /*
  125. * physical MMU
  126. */
  127. #define PTEVALID (1<<0)
  128. #define PTEWT (1<<3)
  129. #define PTEUNCACHED (1<<4)
  130. #define PTEWRITE (1<<1)
  131. #define PTERONLY (0<<1)
  132. #define PTEKERNEL (0<<2)
  133. #define PTEUSER (1<<2)
  134. #define PTESIZE (1<<7)
  135. #define PTEGLOBAL (1<<8)
  136. /*
  137. * Macros for calculating offsets within the page directory base
  138. * and page tables.
  139. */
  140. #define PDX(va) ((((ulong)(va))>>22) & 0x03FF)
  141. #define PTX(va) ((((ulong)(va))>>12) & 0x03FF)
  142. #define getpgcolor(a) 0