mem.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 BI2WD 32 /* bits per word */
  42. #define BY2WD 4 /* bytes per word */
  43. #define BY2V 8 /* bytes per double word */
  44. #define BY2SE 8 /* bytes per stack element */
  45. #define BLOCKALIGN 8
  46. /*
  47. * 4K pages
  48. * these defines could go.
  49. */
  50. #define PGSZ (4*KiB) /* page size */
  51. #define PGSHFT 12 /* log(PGSZ) */
  52. #define PTSZ (4*KiB) /* page table page size */
  53. #define PTSHFT 9 /* */
  54. #define MACHSZ (4*KiB) /* Mach+stack size */
  55. #define MACHMAX 32 /* max. number of cpus */
  56. #define MACHSTKSZ (6*(4*KiB)) /* Mach stack size */
  57. #define KSTACK (16*1024) /* Size of Proc kernel stack */
  58. #define STACKALIGN(sp) ((sp) & ~(BY2SE-1)) /* bug: assure with alloc */
  59. #define STACKGUARD 0xdeadbeefcafebabeULL /* magic number we keep at stack bottom and check on traps */
  60. /*
  61. * 2M pages
  62. * these defines must go.
  63. */
  64. #define BIGPGSHFT 21
  65. #ifndef __ASSEMBLER__
  66. #define BIGPGSZ (1ull<<BIGPGSHFT)
  67. #else
  68. #define BIGPGSZ (1<<BIGPGSHFT)
  69. #endif
  70. #define BIGPGROUND(x) ROUNDUP((x), BIGPGSZ)
  71. #define PGROUND(x) ROUNDUP((x), PGSZ)
  72. #define PGSPERBIG (BIGPGSZ/PGSZ)
  73. /*
  74. * Time
  75. */
  76. #define HZ (100) /* clock frequency */
  77. #define MS2HZ (1000/HZ) /* millisec per clock tick */
  78. #define TK2SEC(t) ((t)/HZ) /* ticks to seconds */
  79. /*
  80. * Address spaces. User:
  81. */
  82. #define UTZERO (0+2*MiB) /* first address in user text */
  83. #define UTROUND(t) ROUNDUP((t), BIGPGSZ)
  84. #ifndef __ASSEMBLER__
  85. #define USTKTOP (0x3fffffffffULL & ~(BIGPGSZ-1))
  86. #else
  87. #define USTKTOP (0x3fffffffffULL & ~(BIGPGSZ-1))
  88. #endif
  89. /* U means "user" */
  90. #define USTKSIZE (16*1024*1024) /* size of user stack */
  91. /* T means "temporary" */
  92. #define TSTKTOP (USTKTOP-USTKSIZE) /* end of new stack in sysexec */
  93. #define NIXCALL (TSTKTOP-USTKSIZE) /* nix syscall queues (2MiB) */
  94. #ifndef __ASSEMBLER__
  95. #define BIGBSSTOP ((NIXCALL-BIGPGSZ) & ~(1ULL*GiB-1))
  96. #define BIGBSSSIZE (32ull*GiB) /* size of big heap segment */
  97. #else
  98. #define BIGBSSTOP ((NIXCALL-BIGPGSZ) & ~(1*GiB-1))
  99. #define BIGBSSSIZE (32*GiB) /* size of big heap segment */
  100. #endif
  101. #define HEAPTOP (BIGBSSTOP-BIGBSSSIZE) /* end of shared heap segments */
  102. /*
  103. * Address spaces. Kernel, sorted by address.
  104. */
  105. #ifndef __ASSEMBLER__
  106. #define KSEG0 (0xffffffff80000000ull) /* 256MB - this is confused */
  107. #define KZERO (0xffffffff80000000ull)
  108. #define KTZERO (KZERO+1*MiB+64*KiB)
  109. /* 0xffffffffffffffffull end of KSEG0 */
  110. #else
  111. #define KSEG0 (0xffffffff80000000)
  112. #define KZERO (0xffffffff80000000)
  113. #define KTZERO (KZERO+1*MiB+64*KiB)
  114. #endif
  115. /*
  116. * Hierarchical Page Tables.
  117. * For example, traditional IA-32 paging structures have 2 levels,
  118. * level 1 is the PD, and level 0 the PT pages; with IA-32e paging,
  119. * level 3 is the PML4(!), level 2 the PDP, level 1 the PD,
  120. * and level 0 the PT pages. The PTLX macro gives an index into the
  121. * page-table page at level 'l' for the virtual address 'v'.
  122. */
  123. #define PTLX(v, l) (((v)>>(((l)*PTSHFT)+PGSHFT)) & ((1<<PTSHFT)-1))
  124. #define PGLSZ(l) (1<<(((l)*PTSHFT)+PGSHFT))