pte.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* $OpenBSD: pte.h,v 1.10 2013/03/25 20:19:03 deraadt Exp $ */
  2. /* $NetBSD: pte.h,v 1.19 1997/08/05 11:00:10 pk Exp $ */
  3. /*
  4. * Copyright (c) 1996
  5. * The President and Fellows of Harvard College. All rights reserved.
  6. * Copyright (c) 1992, 1993
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * This software was developed by the Computer Systems Engineering group
  10. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  11. * contributed to Berkeley.
  12. *
  13. * All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgements:
  15. * This product includes software developed by Harvard University.
  16. * This product includes software developed by the University of
  17. * California, Lawrence Berkeley Laboratory.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * 3. All advertising materials mentioning features or use of this software
  28. * must display the following acknowledgements:
  29. * This product includes software developed by Harvard University.
  30. * This product includes software developed by the University of
  31. * California, Berkeley and its contributors.
  32. * 4. Neither the name of the University nor the names of its contributors
  33. * may be used to endorse or promote products derived from this software
  34. * without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  37. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  40. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  42. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. *
  48. * @(#)pte.h 8.1 (Berkeley) 6/11/93
  49. */
  50. /*
  51. * Sun-4 (sort of), 4c (SparcStation), and 4m Page Table Entries
  52. * (Sun calls them `Page Map Entries').
  53. */
  54. #ifndef _LOCORE
  55. /*
  56. * Segment maps contain `pmeg' (Page Map Entry Group) numbers.
  57. * A PMEG is simply an index that names a group of 32 (sun4) or
  58. * 64 (sun4c) PTEs.
  59. * Depending on the CPU model, we need 7 (sun4c) to 10 (sun4/400) bits
  60. * to hold the hardware MMU resource number.
  61. */
  62. typedef u_short pmeg_t; /* 10 bits needed per Sun-4 segmap entry */
  63. /*
  64. * Region maps contain `smeg' (Segment Entry Group) numbers.
  65. * An SMEG is simply an index that names a group of 64 PMEGs.
  66. */
  67. typedef u_char smeg_t; /* 8 bits needed per Sun-4 regmap entry */
  68. #endif
  69. /*
  70. * Address translation works as follows:
  71. *
  72. * (for sun4c and 2-level sun4)
  73. * 1. test va<31:29> -- these must be 000 or 111 (or you get a fault)
  74. * 2. concatenate context_reg<2:0> and va<29:18> to get a 15 bit number;
  75. * use this to index the segment maps, yielding a 7 or 9 bit value.
  76. * (for 3-level sun4)
  77. * 1. concatenate context_reg<3:0> and va<31:24> to get a 8 bit number;
  78. * use this to index the region maps, yielding a 10 bit value.
  79. * 2. take the value from (1) above and concatenate va<17:12> to
  80. * get a `segment map entry' index. This gives a 9 bit value.
  81. * (for sun4c)
  82. * 3. take the value from (2) above and concatenate va<17:12> to
  83. * get a `page map entry' index. This gives a 32-bit PTE.
  84. * (for sun4)
  85. * 3. take the value from (2 or 3) above and concatenate va<17:13> to
  86. * get a `page map entry' index. This gives a 32-bit PTE.
  87. **
  88. * For sun4m:
  89. * 1. Use context_reg<3:0> to index the context table (located at
  90. * (context_reg << 2) | ((ctx_tbl_ptr_reg >> 2) << 6) ). This
  91. * gives a 32-bit page-table-descriptor (PTP).
  92. * 2. Use va<31:24> to index the region table located by the PTP from (1):
  93. * PTP<31:6> << 10. This gives another PTP for the segment tables
  94. * 3. Use va<23:18> to index the segment table located by the PTP from (2)
  95. * as follows: PTP<31:4> << 8. This gives another PTP for the page tbl.
  96. * 4. Use va<17:12> to index the page table given by (3)'s PTP:
  97. * PTP<31:4> << 8. This gives a 32-bit PTE.
  98. *
  99. * In other words:
  100. *
  101. * struct sun4_3_levelmmu_virtual_addr {
  102. * u_int va_reg:8, (virtual region)
  103. * va_seg:6, (virtual segment)
  104. * va_pg:5, (virtual page within segment)
  105. * va_off:13; (offset within page)
  106. * };
  107. * struct sun4_virtual_addr {
  108. * u_int :2, (required to be the same as bit 29)
  109. * va_seg:12, (virtual segment)
  110. * va_pg:5, (virtual page within segment)
  111. * va_off:13; (offset within page)
  112. * };
  113. * struct sun4c_virtual_addr {
  114. * u_int :2, (required to be the same as bit 29)
  115. * va_seg:12, (virtual segment)
  116. * va_pg:6, (virtual page within segment)
  117. * va_off:12; (offset within page)
  118. * };
  119. *
  120. * struct sun4m_virtual_addr {
  121. * u_int va_reg:8, (virtual region)
  122. * va_seg:6, (virtual segment within region)
  123. * va_pg:6, (virtual page within segment)
  124. * va_off:12; (offset within page)
  125. * };
  126. *
  127. * Then, given any `va':
  128. *
  129. * extern smeg_t regmap[16][1<<8]; (3-level MMU only)
  130. * extern pmeg_t segmap[8][1<<12]; ([16][1<<12] for sun4)
  131. * extern int ptetable[128][1<<6]; ([512][1<<5] for sun4)
  132. *
  133. * extern u_int s4m_ctxmap[16]; (sun4m SRMMU only)
  134. * extern u_int s4m_regmap[16][1<<8]; (sun4m SRMMU only)
  135. * extern u_int s4m_segmap[1<<8][1<<6]; (sun4m SRMMU only)
  136. * extern u_int s4m_pagmap[1<<14][1<<6]; (sun4m SRMMU only)
  137. *
  138. * (the above being in the hardware, accessed as Alternate Address Spaces on
  139. * all machines but the Sun4m SRMMU, in which case the tables are in physical
  140. * kernel memory. In the 4m architecture, the tables are not layed out as
  141. * 2-dim arrays, but are sparsely allocated as needed, and point to each
  142. * other.)
  143. *
  144. * if (cputyp==CPU_SUN4M) // SPARC Reference MMU
  145. * regptp = s4m_ctxmap[curr_ctx];
  146. * if (!(regptp & SRMMU_TEPTD)) TRAP();
  147. * segptp = *(u_int *)(((regptp & ~0x3) << 4) | va.va_reg);
  148. * if (!(segptp & SRMMU_TEPTD)) TRAP();
  149. * pagptp = *(u_int *)(((segptp & ~0x3) << 4) | va.va_seg);
  150. * if (!(pagptp & SRMMU_TEPTD)) TRAP();
  151. * pte = *(u_int *)(((pagptp & ~0x3) << 4) | va.va_pg);
  152. * if (!(pte & SRMMU_TEPTE)) TRAP(); // like PG_V
  153. * if (usermode && PTE_PROT_LEVEL(pte) > 0x5) TRAP();
  154. * if (writing && !PTE_PROT_LEVEL_ALLOWS_WRITING(pte)) TRAP();
  155. * if (!(pte & SRMMU_PG_C)) DO_NOT_USE_CACHE_FOR_THIS_ACCESS();
  156. * pte |= SRMMU_PG_R;
  157. * if (writing) pte |= SRMMU_PG_M;
  158. * physaddr = ((pte & SRMMU_PG_PFNUM) << SRMMU_PGSHIFT)|va.va_off;
  159. * return;
  160. * if (mmu_3l)
  161. * physreg = regmap[curr_ctx][va.va_reg];
  162. * physseg = segmap[physreg][va.va_seg];
  163. * else
  164. * physseg = segmap[curr_ctx][va.va_seg];
  165. * pte = ptetable[physseg][va.va_pg];
  166. * if (!(pte & PG_V)) TRAP();
  167. * if (writing && !pte.pg_w) TRAP();
  168. * if (usermode && pte.pg_s) TRAP();
  169. * if (pte & PG_NC) DO_NOT_USE_CACHE_FOR_THIS_ACCESS();
  170. * pte |= PG_U; (mark used/accessed)
  171. * if (writing) pte |= PG_M; (mark modified)
  172. * ptetable[physseg][va.va_pg] = pte;
  173. * physadr = ((pte & PG_PFNUM) << PAGE_SHIFT) | va.va_off;
  174. */
  175. #if defined(SUN4_MMU3L) && !defined(SUN4)
  176. #error "configuration error"
  177. #endif
  178. #define NBPRG (1 << 24) /* bytes per region */
  179. #define RGSHIFT 24 /* log2(NBPRG) */
  180. #define RGOFSET (NBPRG - 1) /* mask for region offset */
  181. #define NSEGRG (NBPRG / NBPSG) /* segments per region */
  182. #define NBPSG (1 << 18) /* bytes per segment */
  183. #define SGSHIFT 18 /* log2(NBPSG) */
  184. #define SGOFSET (NBPSG - 1) /* mask for segment offset */
  185. /* number of PTEs that map one segment (not number that fit in one segment!) */
  186. #if (defined(SUN4) || defined(SUN4E)) && (defined(SUN4C) || defined(SUN4D) || defined(SUN4M))
  187. extern int nptesg;
  188. #define NPTESG nptesg /* (which someone will have to initialize) */
  189. #else
  190. #define NPTESG (NBPSG / PAGE_SIZE)
  191. #endif
  192. /* virtual address to virtual region number */
  193. #define VA_VREG(va) (((unsigned int)(va) >> RGSHIFT) & 255)
  194. /* virtual address to virtual segment number */
  195. #define VA_VSEG(va) (((unsigned int)(va) >> SGSHIFT) & 63)
  196. /* virtual address to virtual page number, for Sun-4 and Sun-4c */
  197. #define VA_SUN4_VPG(va) (((int)(va) >> 13) & 31)
  198. #define VA_SUN4C_VPG(va) (((int)(va) >> 12) & 63)
  199. #define VA_SUN4M_VPG(va) (((int)(va) >> 12) & 63)
  200. /* virtual address to offset within page */
  201. #define VA_SUN4_OFF(va) (((int)(va)) & 0x1FFF)
  202. #define VA_SUN4C_OFF(va) (((int)(va)) & 0xFFF)
  203. #define VA_SUN4M_OFF(va) (((int)(va)) & 0xFFF)
  204. /* truncate virtual address to region base */
  205. #define VA_ROUNDDOWNTOREG(va) ((int)(va) & ~RGOFSET)
  206. /* truncate virtual address to segment base */
  207. #define VA_ROUNDDOWNTOSEG(va) ((int)(va) & ~SGOFSET)
  208. /* virtual segment to virtual address (must sign extend on holy MMUs!) */
  209. #define VRTOVA(vr) ((CPU_ISSUN4M || HASSUN4_MMU3L) \
  210. ? ((int)(vr) << RGSHIFT) \
  211. : (((int)(vr) << (RGSHIFT+2)) >> 2))
  212. #define VSTOVA(vr,vs) ((CPU_ISSUN4M || HASSUN4_MMU3L) \
  213. ? (((int)(vr) << RGSHIFT) + ((int)(vs) << SGSHIFT)) \
  214. : ((((int)(vr) << (RGSHIFT+2)) >> 2) + ((int)(vs) << SGSHIFT)))
  215. extern int mmu_has_hole;
  216. #define VA_INHOLE(va) (mmu_has_hole \
  217. ? ( (unsigned int)(((int)(va) >> PG_VSHIFT) + 1) > 1) \
  218. : 0)
  219. /* Define the virtual address space hole */
  220. #define MMU_HOLE_START 0x20000000
  221. #define MMU_HOLE_END 0xe0000000
  222. #if defined(SUN4D) || defined(SUN4M) /* Optimization: sun4c/d/m have same */
  223. #if defined(SUN4) || defined(SUN4E) /* page size, so they're used interchangeably */
  224. #define VA_VPG(va) (CPU_ISSUN4OR4E ? VA_SUN4_VPG(va) : VA_SUN4C_VPG(va))
  225. #define VA_OFF(va) (CPU_ISSUN4OR4E ? VA_SUN4_OFF(va) : VA_SUN4C_OFF(va))
  226. #else
  227. #define VA_VPG(va) VA_SUN4M_VPG(va)
  228. #define VA_OFF(va) VA_SUN4M_OFF(va)
  229. #endif /* defined SUN4 || defined SUN4E */
  230. #else /* 4d,4m not defined */
  231. #if (defined(SUN4) || defined(SUN4E)) && defined(SUN4C)
  232. #define VA_VPG(va) (CPU_ISSUN4C ? VA_SUN4C_VPG(va) : VA_SUN4_VPG(va))
  233. #define VA_OFF(va) (CPU_ISSUN4C ? VA_SUN4C_OFF(va) : VA_SUN4_OFF(va))
  234. #endif
  235. #if defined(SUN4C) && !(defined(SUN4) || defined(SUN4E))
  236. #define VA_VPG(va) VA_SUN4C_VPG(va)
  237. #define VA_OFF(va) VA_SUN4C_OFF(va)
  238. #endif
  239. #if !defined(SUN4C) && (defined(SUN4) || defined(SUN4E))
  240. #define VA_VPG(va) VA_SUN4_VPG(va)
  241. #define VA_OFF(va) VA_SUN4_OFF(va)
  242. #endif
  243. #endif /* defined 4d,4m */
  244. /* there is no `struct pte'; we just use `int'; this is for non-4M only */
  245. #define PG_V 0x80000000
  246. #define PG_PROT 0x60000000 /* both protection bits */
  247. #define PG_W 0x40000000 /* allowed to write */
  248. #define PG_S 0x20000000 /* supervisor only */
  249. #define PG_NC 0x10000000 /* non-cacheable */
  250. #define PG_TYPE 0x0c000000 /* both type bits */
  251. #define PG_OBMEM 0x00000000 /* on board memory */
  252. #define PG_OBIO 0x04000000 /* on board I/O (incl. SBus on 4c) */
  253. #define PG_VME16 0x08000000 /* 16-bit-data VME space */
  254. #define PG_VME32 0x0c000000 /* 32-bit-data VME space */
  255. #if defined(SUN4D) || defined(SUN4M)
  256. #define PG_SUN4M_OBMEM 0x0 /* No type bits=>obmem on 4m */
  257. #define PG_SUN4M_OBIO 0xf /* obio maps to 0xf on 4M */
  258. #define SRMMU_PGTYPE 0xf0000000 /* Top 4 bits of pte PPN give type */
  259. #endif
  260. #define PG_U 0x02000000
  261. #define PG_M 0x01000000
  262. #define PG_IOC 0x00800000
  263. #define PG_MBZ 0x00780000 /* unused; must be zero (oh really?) */
  264. #define PG_PFNUM 0x0007ffff /* n.b.: only 16 bits on sun4c */
  265. #define PG_TNC_SHIFT 26 /* shift to get PG_TYPE + PG_NC */
  266. #define PG_M_SHIFT 24 /* shift to get PG_M, PG_U */
  267. #define PG_M_SHIFT4M 5 /* shift to get SRMMU_PG_M,R on 4m */
  268. /*efine PG_NOACC 0 ** XXX */
  269. #define PG_KR 0x20000000
  270. #define PG_KW 0x60000000
  271. #define PG_URKR 0
  272. #define PG_UW 0x40000000
  273. #ifdef KGDB
  274. /* but we will define one for gdb anyway */
  275. struct pte {
  276. u_int pg_v:1,
  277. pg_w:1,
  278. pg_s:1,
  279. pg_nc:1;
  280. enum pgtype { pg_obmem, pg_obio, pg_vme16, pg_vme32 } pg_type:2;
  281. u_int pg_u:1,
  282. pg_m:1,
  283. pg_mbz:5,
  284. pg_pfnum:19;
  285. };
  286. #if defined(SUN4M)
  287. struct srmmu_pte {
  288. u_int pg_pfnum:20,
  289. pg_c:1,
  290. pg_m:1,
  291. pg_u:1;
  292. enum pgprot { pprot_r_r, pprot_rw_rw, pprot_rx_rx, pprot_rwx_rwx,
  293. pprot_x_x, pprot_r_rw, pprot_n_rx, pprot_n_rwx }
  294. pg_prot:3; /* prot. bits: pprot_<user>_<supervisor> */
  295. u_int pg_must_be_2:2;
  296. };
  297. #endif
  298. #endif
  299. /*
  300. * These are needed in the register window code
  301. * to check the validity of (ostensible) user stack PTEs.
  302. */
  303. #define PG_VSHIFT 29 /* (va>>vshift)==0 or -1 => valid */
  304. /* XXX fix this name, it is a va shift not a pte bit shift! */
  305. #define PG_PROTSHIFT 29
  306. #define PG_PROTUWRITE 6 /* PG_V,PG_W,!PG_S */
  307. #define PG_PROTUREAD 4 /* PG_V,!PG_W,!PG_S */
  308. /* %%%: Fix above and below for 4m? */
  309. /* static __inline int PG_VALID(void *va) {
  310. register int t = va; t >>= PG_VSHIFT; return (t == 0 || t == -1);
  311. } */
  312. /*
  313. * Here are the bit definitions for 4M/SRMMU pte's
  314. */
  315. /* MMU TABLE ENTRIES */
  316. #define SRMMU_TEINVALID 0x0 /* invalid (serves as !valid bit) */
  317. #define SRMMU_TEPTD 0x1 /* Page Table Descriptor */
  318. #define SRMMU_TEPTE 0x2 /* Page Table Entry */
  319. #define SRMMU_TERES 0x3 /* reserved */
  320. #define SRMMU_TETYPE 0x3 /* mask for table entry type */
  321. /* PTE FIELDS */
  322. #define SRMMU_PPNMASK 0xFFFFFF00
  323. #define SRMMU_PPNSHIFT 0x8
  324. #define SRMMU_PPNPASHIFT 0x4 /* shift to put ppn into PAddr */
  325. #define SRMMU_L1PPNSHFT 0x14
  326. #define SRMMU_L1PPNMASK 0xFFF00000
  327. #define SRMMU_L2PPNSHFT 0xE
  328. #define SRMMU_L2PPNMASK 0xFC000
  329. #define SRMMU_L3PPNSHFT 0x8
  330. #define SRMMU_L3PPNMASK 0x3F00
  331. /* PTE BITS */
  332. #define SRMMU_PG_C 0x80 /* cacheable */
  333. #define SRMMU_PG_M 0x40 /* modified (dirty) */
  334. #define SRMMU_PG_R 0x20 /* referenced */
  335. #define SRMMU_PGBITSMSK 0xE0
  336. /* PTE PROTECTION */
  337. #define SRMMU_PROT_MASK 0x1C /* Mask protection bits out of pte */
  338. #define SRMMU_PROT_SHFT 0x2
  339. #define PPROT_R_R 0x0 /* These are in the form: */
  340. #define PPROT_RW_RW 0x4 /* PPROT_<u>_<s> */
  341. #define PPROT_RX_RX 0x8 /* where <u> is the user-mode */
  342. #define PPROT_RWX_RWX 0xC /* permission, and <s> is the */
  343. #define PPROT_X_X 0x10 /* supervisor mode permission. */
  344. #define PPROT_R_RW 0x14 /* R=read, W=write, X=execute */
  345. #define PPROT_N_RX 0x18 /* N=none. */
  346. #define PPROT_N_RWX 0x1C
  347. #define PPROT_WRITE 0x4 /* set iff write priv. allowed */
  348. #define PPROT_S 0x18 /* effective S bit */
  349. #define PPROT_U2S_OMASK 0x18 /* OR with prot. to revoke user priv */
  350. /* TABLE SIZES */
  351. #define SRMMU_L1SIZE 0x100
  352. #define SRMMU_L2SIZE 0x40
  353. #define SRMMU_L3SIZE 0x40