mem.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /*
  10. * Memory and machine-specific definitions. Used in C and assembler.
  11. */
  12. /*
  13. * unfortunately, gas does not accept [u]l* suffixes, then we must to avoid them.
  14. * https://sourceware.org/bugzilla/show_bug.cgi?id=190
  15. */
  16. #ifndef __ASSEMBLER__
  17. #define KiB 1024u /* Kibi 0x0000000000000400 */
  18. #define MiB 1048576u /* Mebi 0x0000000000100000 */
  19. #define GiB 1073741824u /* Gibi 000000000040000000 */
  20. #define TiB 1099511627776ull /* Tebi 0x0000010000000000 */
  21. #define PiB 1125899906842624ull /* Pebi 0x0004000000000000 */
  22. #define EiB 1152921504606846976ull /* Exbi 0x1000000000000000 */
  23. #else
  24. #define KiB 1024
  25. #define MiB 1048576
  26. #define GiB 1073741824
  27. #define TiB 1099511627776
  28. #define PiB 1125899906842624
  29. #define EiB 1152921504606846976
  30. #endif
  31. #define HOWMANY(x, y) (((x)+((y)-1))/(y))
  32. #define ROUNDUP(x, y) (HOWMANY((x), (y))*(y))
  33. #define ROUNDDN(x, y) (((x)/(y))*(y))
  34. #define MIN(a, b) ((a) < (b)? (a): (b))
  35. #define MAX(a, b) ((a) > (b)? (a): (b))
  36. #define ALIGNED(p, a) (!(((uintptr)(p)) & ((a)-1)))
  37. /*
  38. * Sizes
  39. */
  40. #define BI2BY 8 /* bits per byte */
  41. #define BY2V 8 /* bytes per double word */
  42. #define BY2SE 8 /* bytes per stack element */
  43. #define BLOCKALIGN 8
  44. /*
  45. * 4K pages
  46. * these defines could go.
  47. */
  48. #define PGSZ (4*KiB) /* page size */
  49. #define PGSHFT 12 /* log(PGSZ) */
  50. #define PTSZ (4*KiB) /* page table page size */
  51. #define PTSHFT 9 /* */
  52. #define MACHSZ (4*KiB) /* Mach+stack size */
  53. #define MACHMAX 32 /* max. number of cpus */
  54. #define MACHSTKSZ (6*(4*KiB)) /* Mach stack size */
  55. #define KSTACK (16*1024) /* Size of Proc kernel stack */
  56. #define STACKALIGN(sp) ((sp) & ~(BY2SE-1)) /* bug: assure with alloc */
  57. /*
  58. * 2M pages
  59. * these defines must go.
  60. */
  61. #define BIGPGSHFT 21
  62. #ifndef __ASSEMBLER__
  63. #define BIGPGSZ (1ull<<BIGPGSHFT)
  64. #else
  65. #define BIGPGSZ (1<<BIGPGSHFT)
  66. #endif
  67. #define BIGPGROUND(x) ROUNDUP((x), BIGPGSZ)
  68. #define PGSPERBIG (BIGPGSZ/PGSZ)
  69. /*
  70. * Time
  71. */
  72. #define HZ (100) /* clock frequency */
  73. #define MS2HZ (1000/HZ) /* millisec per clock tick */
  74. #define TK2SEC(t) ((t)/HZ) /* ticks to seconds */
  75. /*
  76. * Address spaces. User:
  77. */
  78. #define UTZERO (0+2*MiB) /* first address in user text */
  79. #define UTROUND(t) ROUNDUP((t), BIGPGSZ)
  80. #ifndef __ASSEMBLER__
  81. #define USTKTOP (0x00007ffffffff000ull & ~(BIGPGSZ-1))
  82. #else
  83. #define USTKTOP (0x00007ffffffff000 & ~(BIGPGSZ-1))
  84. #endif
  85. #define USTKSIZE (16*1024*1024) /* size of user stack */
  86. #define TSTKTOP (USTKTOP-USTKSIZE) /* end of new stack in sysexec */
  87. #define NIXCALL (TSTKTOP-USTKSIZE) /* nix syscall queues (2MiB) */
  88. #ifndef __ASSEMBLER__
  89. #define BIGBSSTOP ((NIXCALL-BIGPGSZ) & ~(1ULL*GiB-1))
  90. #define BIGBSSSIZE (32ull*GiB) /* size of big heap segment */
  91. #else
  92. #define BIGBSSTOP ((NIXCALL-BIGPGSZ) & ~(1*GiB-1))
  93. #define BIGBSSSIZE (32*GiB) /* size of big heap segment */
  94. #endif
  95. #define HEAPTOP (BIGBSSTOP-BIGBSSSIZE) /* end of shared heap segments */
  96. /*
  97. * Address spaces. Kernel, sorted by address.
  98. */
  99. #ifndef __ASSEMBLER__
  100. #define KSEG2 (0xfffffe0000000000ull) /* 1TB - KMAP */
  101. /* 0xffffff0000000000ull end of KSEG2 */
  102. #define VMAP (0xffffffffe0000000ull)
  103. #define VMAPSZ (256*MiB)
  104. #define KSEG0 (0xfffffffff0000000ull) /* 256MB - this is confused */
  105. #define KZERO (0xfffffffff0000000ull)
  106. #define KTZERO (KZERO+1*MiB+64*KiB)
  107. #define PDMAP (0xffffffffff800000ull)
  108. #define PMAPADDR (0xffffffffffe00000ull) /* unused as of yet */
  109. /* 0xffffffffffffffffull end of KSEG0 */
  110. #else
  111. #define KSEG2 (0xfffffe0000000000)
  112. #define VMAPSZ (256*MiB)
  113. #define VMAP (0xffffffffe0000000)
  114. #define KSEG0 (0xfffffffff0000000)
  115. #define KZERO (0xfffffffff0000000)
  116. #define KTZERO (KZERO+1*MiB+64*KiB)
  117. #define PDMAP (0xffffffffff800000)
  118. #define PMAPADDR (0xffffffffffe00000)
  119. #endif
  120. /*
  121. * Hierarchical Page Tables.
  122. * For example, traditional IA-32 paging structures have 2 levels,
  123. * level 1 is the PD, and level 0 the PT pages; with IA-32e paging,
  124. * level 3 is the PML4(!), level 2 the PDP, level 1 the PD,
  125. * and level 0 the PT pages. The PTLX macro gives an index into the
  126. * page-table page at level 'l' for the virtual address 'v'.
  127. */
  128. #define PTLX(v, l) (((v)>>(((l)*PTSHFT)+PGSHFT)) & ((1<<PTSHFT)-1))
  129. #define PGLSZ(l) (1<<(((l)*PTSHFT)+PGSHFT))