2
0

mmu_helper.c 27 KB

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