mmu_helper.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * Sparc MMU helpers
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "cpu.h"
  20. #include "trace.h"
  21. #include "exec-memory.h"
  22. /* Sparc MMU emulation */
  23. #if defined(CONFIG_USER_ONLY)
  24. int cpu_sparc_handle_mmu_fault(CPUSPARCState *env1, target_ulong address, int rw,
  25. int mmu_idx)
  26. {
  27. if (rw & 2) {
  28. env1->exception_index = TT_TFAULT;
  29. } else {
  30. env1->exception_index = TT_DFAULT;
  31. }
  32. return 1;
  33. }
  34. #else
  35. #ifndef TARGET_SPARC64
  36. /*
  37. * Sparc V8 Reference MMU (SRMMU)
  38. */
  39. static const int access_table[8][8] = {
  40. { 0, 0, 0, 0, 8, 0, 12, 12 },
  41. { 0, 0, 0, 0, 8, 0, 0, 0 },
  42. { 8, 8, 0, 0, 0, 8, 12, 12 },
  43. { 8, 8, 0, 0, 0, 8, 0, 0 },
  44. { 8, 0, 8, 0, 8, 8, 12, 12 },
  45. { 8, 0, 8, 0, 8, 0, 8, 0 },
  46. { 8, 8, 8, 0, 8, 8, 12, 12 },
  47. { 8, 8, 8, 0, 8, 8, 8, 0 }
  48. };
  49. static const int perm_table[2][8] = {
  50. {
  51. PAGE_READ,
  52. PAGE_READ | PAGE_WRITE,
  53. PAGE_READ | PAGE_EXEC,
  54. PAGE_READ | PAGE_WRITE | PAGE_EXEC,
  55. PAGE_EXEC,
  56. PAGE_READ | PAGE_WRITE,
  57. PAGE_READ | PAGE_EXEC,
  58. PAGE_READ | PAGE_WRITE | PAGE_EXEC
  59. },
  60. {
  61. PAGE_READ,
  62. PAGE_READ | PAGE_WRITE,
  63. PAGE_READ | PAGE_EXEC,
  64. PAGE_READ | PAGE_WRITE | PAGE_EXEC,
  65. PAGE_EXEC,
  66. PAGE_READ,
  67. 0,
  68. 0,
  69. }
  70. };
  71. static int get_physical_address(CPUSPARCState *env, target_phys_addr_t *physical,
  72. int *prot, int *access_index,
  73. target_ulong address, int rw, int mmu_idx,
  74. target_ulong *page_size)
  75. {
  76. int access_perms = 0;
  77. target_phys_addr_t pde_ptr;
  78. uint32_t pde;
  79. int error_code = 0, is_dirty, is_user;
  80. unsigned long page_offset;
  81. is_user = mmu_idx == MMU_USER_IDX;
  82. if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
  83. *page_size = TARGET_PAGE_SIZE;
  84. /* Boot mode: instruction fetches are taken from PROM */
  85. if (rw == 2 && (env->mmuregs[0] & env->def->mmu_bm)) {
  86. *physical = env->prom_addr | (address & 0x7ffffULL);
  87. *prot = PAGE_READ | PAGE_EXEC;
  88. return 0;
  89. }
  90. *physical = address;
  91. *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
  92. return 0;
  93. }
  94. *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user ? 0 : 1);
  95. *physical = 0xffffffffffff0000ULL;
  96. /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
  97. /* Context base + context number */
  98. pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
  99. pde = ldl_phys(pde_ptr);
  100. /* Ctx pde */
  101. switch (pde & PTE_ENTRYTYPE_MASK) {
  102. default:
  103. case 0: /* Invalid */
  104. return 1 << 2;
  105. case 2: /* L0 PTE, maybe should not happen? */
  106. case 3: /* Reserved */
  107. return 4 << 2;
  108. case 1: /* L0 PDE */
  109. pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
  110. pde = ldl_phys(pde_ptr);
  111. switch (pde & PTE_ENTRYTYPE_MASK) {
  112. default:
  113. case 0: /* Invalid */
  114. return (1 << 8) | (1 << 2);
  115. case 3: /* Reserved */
  116. return (1 << 8) | (4 << 2);
  117. case 1: /* L1 PDE */
  118. pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
  119. pde = ldl_phys(pde_ptr);
  120. switch (pde & PTE_ENTRYTYPE_MASK) {
  121. default:
  122. case 0: /* Invalid */
  123. return (2 << 8) | (1 << 2);
  124. case 3: /* Reserved */
  125. return (2 << 8) | (4 << 2);
  126. case 1: /* L2 PDE */
  127. pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
  128. pde = ldl_phys(pde_ptr);
  129. switch (pde & PTE_ENTRYTYPE_MASK) {
  130. default:
  131. case 0: /* Invalid */
  132. return (3 << 8) | (1 << 2);
  133. case 1: /* PDE, should not happen */
  134. case 3: /* Reserved */
  135. return (3 << 8) | (4 << 2);
  136. case 2: /* L3 PTE */
  137. page_offset = 0;
  138. }
  139. *page_size = TARGET_PAGE_SIZE;
  140. break;
  141. case 2: /* L2 PTE */
  142. page_offset = address & 0x3f000;
  143. *page_size = 0x40000;
  144. }
  145. break;
  146. case 2: /* L1 PTE */
  147. page_offset = address & 0xfff000;
  148. *page_size = 0x1000000;
  149. }
  150. }
  151. /* check access */
  152. access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
  153. error_code = access_table[*access_index][access_perms];
  154. if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user)) {
  155. return error_code;
  156. }
  157. /* update page modified and dirty bits */
  158. is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
  159. if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
  160. pde |= PG_ACCESSED_MASK;
  161. if (is_dirty) {
  162. pde |= PG_MODIFIED_MASK;
  163. }
  164. stl_phys_notdirty(pde_ptr, pde);
  165. }
  166. /* the page can be put in the TLB */
  167. *prot = perm_table[is_user][access_perms];
  168. if (!(pde & PG_MODIFIED_MASK)) {
  169. /* only set write access if already dirty... otherwise wait
  170. for dirty access */
  171. *prot &= ~PAGE_WRITE;
  172. }
  173. /* Even if large ptes, we map only one 4KB page in the cache to
  174. avoid filling it too fast */
  175. *physical = ((target_phys_addr_t)(pde & PTE_ADDR_MASK) << 4) + page_offset;
  176. return error_code;
  177. }
  178. /* Perform address translation */
  179. int cpu_sparc_handle_mmu_fault(CPUSPARCState *env, target_ulong address, int rw,
  180. int mmu_idx)
  181. {
  182. target_phys_addr_t paddr;
  183. target_ulong vaddr;
  184. target_ulong page_size;
  185. int error_code = 0, prot, access_index;
  186. address &= TARGET_PAGE_MASK;
  187. error_code = get_physical_address(env, &paddr, &prot, &access_index,
  188. address, rw, mmu_idx, &page_size);
  189. vaddr = address;
  190. if (error_code == 0) {
  191. #ifdef DEBUG_MMU
  192. printf("Translate at " TARGET_FMT_lx " -> " TARGET_FMT_plx ", vaddr "
  193. TARGET_FMT_lx "\n", address, paddr, vaddr);
  194. #endif
  195. tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
  196. return 0;
  197. }
  198. if (env->mmuregs[3]) { /* Fault status register */
  199. env->mmuregs[3] = 1; /* overflow (not read before another fault) */
  200. }
  201. env->mmuregs[3] |= (access_index << 5) | error_code | 2;
  202. env->mmuregs[4] = address; /* Fault address register */
  203. if ((env->mmuregs[0] & MMU_NF) || env->psret == 0) {
  204. /* No fault mode: if a mapping is available, just override
  205. permissions. If no mapping is available, redirect accesses to
  206. neverland. Fake/overridden mappings will be flushed when
  207. switching to normal mode. */
  208. prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
  209. tlb_set_page(env, vaddr, paddr, prot, mmu_idx, TARGET_PAGE_SIZE);
  210. return 0;
  211. } else {
  212. if (rw & 2) {
  213. env->exception_index = TT_TFAULT;
  214. } else {
  215. env->exception_index = TT_DFAULT;
  216. }
  217. return 1;
  218. }
  219. }
  220. target_ulong mmu_probe(CPUSPARCState *env, target_ulong address, int mmulev)
  221. {
  222. target_phys_addr_t pde_ptr;
  223. uint32_t pde;
  224. /* Context base + context number */
  225. pde_ptr = (target_phys_addr_t)(env->mmuregs[1] << 4) +
  226. (env->mmuregs[2] << 2);
  227. pde = ldl_phys(pde_ptr);
  228. switch (pde & PTE_ENTRYTYPE_MASK) {
  229. default:
  230. case 0: /* Invalid */
  231. case 2: /* PTE, maybe should not happen? */
  232. case 3: /* Reserved */
  233. return 0;
  234. case 1: /* L1 PDE */
  235. if (mmulev == 3) {
  236. return pde;
  237. }
  238. pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
  239. pde = ldl_phys(pde_ptr);
  240. switch (pde & PTE_ENTRYTYPE_MASK) {
  241. default:
  242. case 0: /* Invalid */
  243. case 3: /* Reserved */
  244. return 0;
  245. case 2: /* L1 PTE */
  246. return pde;
  247. case 1: /* L2 PDE */
  248. if (mmulev == 2) {
  249. return pde;
  250. }
  251. pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
  252. pde = ldl_phys(pde_ptr);
  253. switch (pde & PTE_ENTRYTYPE_MASK) {
  254. default:
  255. case 0: /* Invalid */
  256. case 3: /* Reserved */
  257. return 0;
  258. case 2: /* L2 PTE */
  259. return pde;
  260. case 1: /* L3 PDE */
  261. if (mmulev == 1) {
  262. return pde;
  263. }
  264. pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
  265. pde = ldl_phys(pde_ptr);
  266. switch (pde & PTE_ENTRYTYPE_MASK) {
  267. default:
  268. case 0: /* Invalid */
  269. case 1: /* PDE, should not happen */
  270. case 3: /* Reserved */
  271. return 0;
  272. case 2: /* L3 PTE */
  273. return pde;
  274. }
  275. }
  276. }
  277. }
  278. return 0;
  279. }
  280. void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUSPARCState *env)
  281. {
  282. target_ulong va, va1, va2;
  283. unsigned int n, m, o;
  284. target_phys_addr_t pde_ptr, pa;
  285. uint32_t pde;
  286. pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
  287. pde = ldl_phys(pde_ptr);
  288. (*cpu_fprintf)(f, "Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
  289. (target_phys_addr_t)env->mmuregs[1] << 4, env->mmuregs[2]);
  290. for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
  291. pde = mmu_probe(env, va, 2);
  292. if (pde) {
  293. pa = cpu_get_phys_page_debug(env, va);
  294. (*cpu_fprintf)(f, "VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
  295. " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
  296. for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
  297. pde = mmu_probe(env, va1, 1);
  298. if (pde) {
  299. pa = cpu_get_phys_page_debug(env, va1);
  300. (*cpu_fprintf)(f, " VA: " TARGET_FMT_lx ", PA: "
  301. TARGET_FMT_plx " PDE: " TARGET_FMT_lx "\n",
  302. va1, pa, pde);
  303. for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
  304. pde = mmu_probe(env, va2, 0);
  305. if (pde) {
  306. pa = cpu_get_phys_page_debug(env, va2);
  307. (*cpu_fprintf)(f, " VA: " TARGET_FMT_lx ", PA: "
  308. TARGET_FMT_plx " PTE: "
  309. TARGET_FMT_lx "\n",
  310. va2, pa, pde);
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. /* Gdb expects all registers windows to be flushed in ram. This function handles
  319. * reads (and only reads) in stack frames as if windows were flushed. We assume
  320. * that the sparc ABI is followed.
  321. */
  322. int target_memory_rw_debug(CPUSPARCState *env, target_ulong addr,
  323. uint8_t *buf, int len, int is_write)
  324. {
  325. int i;
  326. int len1;
  327. int cwp = env->cwp;
  328. if (!is_write) {
  329. for (i = 0; i < env->nwindows; i++) {
  330. int off;
  331. target_ulong fp = env->regbase[cwp * 16 + 22];
  332. /* Assume fp == 0 means end of frame. */
  333. if (fp == 0) {
  334. break;
  335. }
  336. cwp = cpu_cwp_inc(env, cwp + 1);
  337. /* Invalid window ? */
  338. if (env->wim & (1 << cwp)) {
  339. break;
  340. }
  341. /* According to the ABI, the stack is growing downward. */
  342. if (addr + len < fp) {
  343. break;
  344. }
  345. /* Not in this frame. */
  346. if (addr > fp + 64) {
  347. continue;
  348. }
  349. /* Handle access before this window. */
  350. if (addr < fp) {
  351. len1 = fp - addr;
  352. if (cpu_memory_rw_debug(env, addr, buf, len1, is_write) != 0) {
  353. return -1;
  354. }
  355. addr += len1;
  356. len -= len1;
  357. buf += len1;
  358. }
  359. /* Access byte per byte to registers. Not very efficient but speed
  360. * is not critical.
  361. */
  362. off = addr - fp;
  363. len1 = 64 - off;
  364. if (len1 > len) {
  365. len1 = len;
  366. }
  367. for (; len1; len1--) {
  368. int reg = cwp * 16 + 8 + (off >> 2);
  369. union {
  370. uint32_t v;
  371. uint8_t c[4];
  372. } u;
  373. u.v = cpu_to_be32(env->regbase[reg]);
  374. *buf++ = u.c[off & 3];
  375. addr++;
  376. len--;
  377. off++;
  378. }
  379. if (len == 0) {
  380. return 0;
  381. }
  382. }
  383. }
  384. return cpu_memory_rw_debug(env, addr, buf, len, is_write);
  385. }
  386. #else /* !TARGET_SPARC64 */
  387. /* 41 bit physical address space */
  388. static inline target_phys_addr_t ultrasparc_truncate_physical(uint64_t x)
  389. {
  390. return x & 0x1ffffffffffULL;
  391. }
  392. /*
  393. * UltraSparc IIi I/DMMUs
  394. */
  395. /* Returns true if TTE tag is valid and matches virtual address value
  396. in context requires virtual address mask value calculated from TTE
  397. entry size */
  398. static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
  399. uint64_t address, uint64_t context,
  400. target_phys_addr_t *physical)
  401. {
  402. uint64_t mask;
  403. switch (TTE_PGSIZE(tlb->tte)) {
  404. default:
  405. case 0x0: /* 8k */
  406. mask = 0xffffffffffffe000ULL;
  407. break;
  408. case 0x1: /* 64k */
  409. mask = 0xffffffffffff0000ULL;
  410. break;
  411. case 0x2: /* 512k */
  412. mask = 0xfffffffffff80000ULL;
  413. break;
  414. case 0x3: /* 4M */
  415. mask = 0xffffffffffc00000ULL;
  416. break;
  417. }
  418. /* valid, context match, virtual address match? */
  419. if (TTE_IS_VALID(tlb->tte) &&
  420. (TTE_IS_GLOBAL(tlb->tte) || tlb_compare_context(tlb, context))
  421. && compare_masked(address, tlb->tag, mask)) {
  422. /* decode physical address */
  423. *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
  424. return 1;
  425. }
  426. return 0;
  427. }
  428. static int get_physical_address_data(CPUSPARCState *env,
  429. target_phys_addr_t *physical, int *prot,
  430. target_ulong address, int rw, int mmu_idx)
  431. {
  432. unsigned int i;
  433. uint64_t context;
  434. uint64_t sfsr = 0;
  435. int is_user = (mmu_idx == MMU_USER_IDX ||
  436. mmu_idx == MMU_USER_SECONDARY_IDX);
  437. if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
  438. *physical = ultrasparc_truncate_physical(address);
  439. *prot = PAGE_READ | PAGE_WRITE;
  440. return 0;
  441. }
  442. switch (mmu_idx) {
  443. case MMU_USER_IDX:
  444. case MMU_KERNEL_IDX:
  445. context = env->dmmu.mmu_primary_context & 0x1fff;
  446. sfsr |= SFSR_CT_PRIMARY;
  447. break;
  448. case MMU_USER_SECONDARY_IDX:
  449. case MMU_KERNEL_SECONDARY_IDX:
  450. context = env->dmmu.mmu_secondary_context & 0x1fff;
  451. sfsr |= SFSR_CT_SECONDARY;
  452. break;
  453. case MMU_NUCLEUS_IDX:
  454. sfsr |= SFSR_CT_NUCLEUS;
  455. /* FALLTHRU */
  456. default:
  457. context = 0;
  458. break;
  459. }
  460. if (rw == 1) {
  461. sfsr |= SFSR_WRITE_BIT;
  462. } else if (rw == 4) {
  463. sfsr |= SFSR_NF_BIT;
  464. }
  465. for (i = 0; i < 64; i++) {
  466. /* ctx match, vaddr match, valid? */
  467. if (ultrasparc_tag_match(&env->dtlb[i], address, context, physical)) {
  468. int do_fault = 0;
  469. /* access ok? */
  470. /* multiple bits in SFSR.FT may be set on TT_DFAULT */
  471. if (TTE_IS_PRIV(env->dtlb[i].tte) && is_user) {
  472. do_fault = 1;
  473. sfsr |= SFSR_FT_PRIV_BIT; /* privilege violation */
  474. trace_mmu_helper_dfault(address, context, mmu_idx, env->tl);
  475. }
  476. if (rw == 4) {
  477. if (TTE_IS_SIDEEFFECT(env->dtlb[i].tte)) {
  478. do_fault = 1;
  479. sfsr |= SFSR_FT_NF_E_BIT;
  480. }
  481. } else {
  482. if (TTE_IS_NFO(env->dtlb[i].tte)) {
  483. do_fault = 1;
  484. sfsr |= SFSR_FT_NFO_BIT;
  485. }
  486. }
  487. if (do_fault) {
  488. /* faults above are reported with TT_DFAULT. */
  489. env->exception_index = TT_DFAULT;
  490. } else if (!TTE_IS_W_OK(env->dtlb[i].tte) && (rw == 1)) {
  491. do_fault = 1;
  492. env->exception_index = TT_DPROT;
  493. trace_mmu_helper_dprot(address, context, mmu_idx, env->tl);
  494. }
  495. if (!do_fault) {
  496. *prot = PAGE_READ;
  497. if (TTE_IS_W_OK(env->dtlb[i].tte)) {
  498. *prot |= PAGE_WRITE;
  499. }
  500. TTE_SET_USED(env->dtlb[i].tte);
  501. return 0;
  502. }
  503. if (env->dmmu.sfsr & SFSR_VALID_BIT) { /* Fault status register */
  504. sfsr |= SFSR_OW_BIT; /* overflow (not read before
  505. another fault) */
  506. }
  507. if (env->pstate & PS_PRIV) {
  508. sfsr |= SFSR_PR_BIT;
  509. }
  510. /* FIXME: ASI field in SFSR must be set */
  511. env->dmmu.sfsr = sfsr | SFSR_VALID_BIT;
  512. env->dmmu.sfar = address; /* Fault address register */
  513. env->dmmu.tag_access = (address & ~0x1fffULL) | context;
  514. return 1;
  515. }
  516. }
  517. trace_mmu_helper_dmiss(address, context);
  518. /*
  519. * On MMU misses:
  520. * - UltraSPARC IIi: SFSR and SFAR unmodified
  521. * - JPS1: SFAR updated and some fields of SFSR updated
  522. */
  523. env->dmmu.tag_access = (address & ~0x1fffULL) | context;
  524. env->exception_index = TT_DMISS;
  525. return 1;
  526. }
  527. static int get_physical_address_code(CPUSPARCState *env,
  528. target_phys_addr_t *physical, int *prot,
  529. target_ulong address, int mmu_idx)
  530. {
  531. unsigned int i;
  532. uint64_t context;
  533. int is_user = (mmu_idx == MMU_USER_IDX ||
  534. mmu_idx == MMU_USER_SECONDARY_IDX);
  535. if ((env->lsu & IMMU_E) == 0 || (env->pstate & PS_RED) != 0) {
  536. /* IMMU disabled */
  537. *physical = ultrasparc_truncate_physical(address);
  538. *prot = PAGE_EXEC;
  539. return 0;
  540. }
  541. if (env->tl == 0) {
  542. /* PRIMARY context */
  543. context = env->dmmu.mmu_primary_context & 0x1fff;
  544. } else {
  545. /* NUCLEUS context */
  546. context = 0;
  547. }
  548. for (i = 0; i < 64; i++) {
  549. /* ctx match, vaddr match, valid? */
  550. if (ultrasparc_tag_match(&env->itlb[i],
  551. address, context, physical)) {
  552. /* access ok? */
  553. if (TTE_IS_PRIV(env->itlb[i].tte) && is_user) {
  554. /* Fault status register */
  555. if (env->immu.sfsr & SFSR_VALID_BIT) {
  556. env->immu.sfsr = SFSR_OW_BIT; /* overflow (not read before
  557. another fault) */
  558. } else {
  559. env->immu.sfsr = 0;
  560. }
  561. if (env->pstate & PS_PRIV) {
  562. env->immu.sfsr |= SFSR_PR_BIT;
  563. }
  564. if (env->tl > 0) {
  565. env->immu.sfsr |= SFSR_CT_NUCLEUS;
  566. }
  567. /* FIXME: ASI field in SFSR must be set */
  568. env->immu.sfsr |= SFSR_FT_PRIV_BIT | SFSR_VALID_BIT;
  569. env->exception_index = TT_TFAULT;
  570. env->immu.tag_access = (address & ~0x1fffULL) | context;
  571. trace_mmu_helper_tfault(address, context);
  572. return 1;
  573. }
  574. *prot = PAGE_EXEC;
  575. TTE_SET_USED(env->itlb[i].tte);
  576. return 0;
  577. }
  578. }
  579. trace_mmu_helper_tmiss(address, context);
  580. /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
  581. env->immu.tag_access = (address & ~0x1fffULL) | context;
  582. env->exception_index = TT_TMISS;
  583. return 1;
  584. }
  585. static int get_physical_address(CPUSPARCState *env, target_phys_addr_t *physical,
  586. int *prot, int *access_index,
  587. target_ulong address, int rw, int mmu_idx,
  588. target_ulong *page_size)
  589. {
  590. /* ??? We treat everything as a small page, then explicitly flush
  591. everything when an entry is evicted. */
  592. *page_size = TARGET_PAGE_SIZE;
  593. /* safety net to catch wrong softmmu index use from dynamic code */
  594. if (env->tl > 0 && mmu_idx != MMU_NUCLEUS_IDX) {
  595. if (rw == 2) {
  596. trace_mmu_helper_get_phys_addr_code(env->tl, mmu_idx,
  597. env->dmmu.mmu_primary_context,
  598. env->dmmu.mmu_secondary_context,
  599. address);
  600. } else {
  601. trace_mmu_helper_get_phys_addr_data(env->tl, mmu_idx,
  602. env->dmmu.mmu_primary_context,
  603. env->dmmu.mmu_secondary_context,
  604. address);
  605. }
  606. }
  607. if (rw == 2) {
  608. return get_physical_address_code(env, physical, prot, address,
  609. mmu_idx);
  610. } else {
  611. return get_physical_address_data(env, physical, prot, address, rw,
  612. mmu_idx);
  613. }
  614. }
  615. /* Perform address translation */
  616. int cpu_sparc_handle_mmu_fault(CPUSPARCState *env, target_ulong address, int rw,
  617. int mmu_idx)
  618. {
  619. target_ulong vaddr;
  620. target_phys_addr_t paddr;
  621. target_ulong page_size;
  622. int error_code = 0, prot, access_index;
  623. address &= TARGET_PAGE_MASK;
  624. error_code = get_physical_address(env, &paddr, &prot, &access_index,
  625. address, rw, mmu_idx, &page_size);
  626. if (error_code == 0) {
  627. vaddr = address;
  628. trace_mmu_helper_mmu_fault(address, paddr, mmu_idx, env->tl,
  629. env->dmmu.mmu_primary_context,
  630. env->dmmu.mmu_secondary_context);
  631. tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
  632. return 0;
  633. }
  634. /* XXX */
  635. return 1;
  636. }
  637. void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUSPARCState *env)
  638. {
  639. unsigned int i;
  640. const char *mask;
  641. (*cpu_fprintf)(f, "MMU contexts: Primary: %" PRId64 ", Secondary: %"
  642. PRId64 "\n",
  643. env->dmmu.mmu_primary_context,
  644. env->dmmu.mmu_secondary_context);
  645. if ((env->lsu & DMMU_E) == 0) {
  646. (*cpu_fprintf)(f, "DMMU disabled\n");
  647. } else {
  648. (*cpu_fprintf)(f, "DMMU dump\n");
  649. for (i = 0; i < 64; i++) {
  650. switch (TTE_PGSIZE(env->dtlb[i].tte)) {
  651. default:
  652. case 0x0:
  653. mask = " 8k";
  654. break;
  655. case 0x1:
  656. mask = " 64k";
  657. break;
  658. case 0x2:
  659. mask = "512k";
  660. break;
  661. case 0x3:
  662. mask = " 4M";
  663. break;
  664. }
  665. if (TTE_IS_VALID(env->dtlb[i].tte)) {
  666. (*cpu_fprintf)(f, "[%02u] VA: %" PRIx64 ", PA: %llx"
  667. ", %s, %s, %s, %s, ctx %" PRId64 " %s\n",
  668. i,
  669. env->dtlb[i].tag & (uint64_t)~0x1fffULL,
  670. TTE_PA(env->dtlb[i].tte),
  671. mask,
  672. TTE_IS_PRIV(env->dtlb[i].tte) ? "priv" : "user",
  673. TTE_IS_W_OK(env->dtlb[i].tte) ? "RW" : "RO",
  674. TTE_IS_LOCKED(env->dtlb[i].tte) ?
  675. "locked" : "unlocked",
  676. env->dtlb[i].tag & (uint64_t)0x1fffULL,
  677. TTE_IS_GLOBAL(env->dtlb[i].tte) ?
  678. "global" : "local");
  679. }
  680. }
  681. }
  682. if ((env->lsu & IMMU_E) == 0) {
  683. (*cpu_fprintf)(f, "IMMU disabled\n");
  684. } else {
  685. (*cpu_fprintf)(f, "IMMU dump\n");
  686. for (i = 0; i < 64; i++) {
  687. switch (TTE_PGSIZE(env->itlb[i].tte)) {
  688. default:
  689. case 0x0:
  690. mask = " 8k";
  691. break;
  692. case 0x1:
  693. mask = " 64k";
  694. break;
  695. case 0x2:
  696. mask = "512k";
  697. break;
  698. case 0x3:
  699. mask = " 4M";
  700. break;
  701. }
  702. if (TTE_IS_VALID(env->itlb[i].tte)) {
  703. (*cpu_fprintf)(f, "[%02u] VA: %" PRIx64 ", PA: %llx"
  704. ", %s, %s, %s, ctx %" PRId64 " %s\n",
  705. i,
  706. env->itlb[i].tag & (uint64_t)~0x1fffULL,
  707. TTE_PA(env->itlb[i].tte),
  708. mask,
  709. TTE_IS_PRIV(env->itlb[i].tte) ? "priv" : "user",
  710. TTE_IS_LOCKED(env->itlb[i].tte) ?
  711. "locked" : "unlocked",
  712. env->itlb[i].tag & (uint64_t)0x1fffULL,
  713. TTE_IS_GLOBAL(env->itlb[i].tte) ?
  714. "global" : "local");
  715. }
  716. }
  717. }
  718. }
  719. #endif /* TARGET_SPARC64 */
  720. static int cpu_sparc_get_phys_page(CPUSPARCState *env, target_phys_addr_t *phys,
  721. target_ulong addr, int rw, int mmu_idx)
  722. {
  723. target_ulong page_size;
  724. int prot, access_index;
  725. return get_physical_address(env, phys, &prot, &access_index, addr, rw,
  726. mmu_idx, &page_size);
  727. }
  728. #if defined(TARGET_SPARC64)
  729. target_phys_addr_t cpu_get_phys_page_nofault(CPUSPARCState *env, target_ulong addr,
  730. int mmu_idx)
  731. {
  732. target_phys_addr_t phys_addr;
  733. if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 4, mmu_idx) != 0) {
  734. return -1;
  735. }
  736. return phys_addr;
  737. }
  738. #endif
  739. target_phys_addr_t cpu_get_phys_page_debug(CPUSPARCState *env, target_ulong addr)
  740. {
  741. target_phys_addr_t phys_addr;
  742. int mmu_idx = cpu_mmu_index(env);
  743. MemoryRegionSection section;
  744. if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 2, mmu_idx) != 0) {
  745. if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 0, mmu_idx) != 0) {
  746. return -1;
  747. }
  748. }
  749. section = memory_region_find(get_system_memory(), phys_addr, 1);
  750. if (!section.size) {
  751. return -1;
  752. }
  753. return phys_addr;
  754. }
  755. #endif