2
0

mem_helper.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * HPPA memory access helper routines
  3. *
  4. * Copyright (c) 2017 Helge Deller
  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.1 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 "qemu/osdep.h"
  20. #include "qemu/log.h"
  21. #include "cpu.h"
  22. #include "exec/exec-all.h"
  23. #include "exec/cputlb.h"
  24. #include "exec/page-protection.h"
  25. #include "exec/helper-proto.h"
  26. #include "hw/core/cpu.h"
  27. #include "trace.h"
  28. hwaddr hppa_abs_to_phys_pa2_w1(vaddr addr)
  29. {
  30. /*
  31. * Figure H-8 "62-bit Absolute Accesses when PSW W-bit is 1" describes
  32. * an algorithm in which a 62-bit absolute address is transformed to
  33. * a 64-bit physical address. This must then be combined with that
  34. * pictured in Figure H-11 "Physical Address Space Mapping", in which
  35. * the full physical address is truncated to the N-bit physical address
  36. * supported by the implementation.
  37. *
  38. * Since the supported physical address space is below 54 bits, the
  39. * H-8 algorithm is moot and all that is left is to truncate.
  40. */
  41. QEMU_BUILD_BUG_ON(TARGET_PHYS_ADDR_SPACE_BITS > 54);
  42. return sextract64(addr, 0, TARGET_PHYS_ADDR_SPACE_BITS);
  43. }
  44. hwaddr hppa_abs_to_phys_pa2_w0(vaddr addr)
  45. {
  46. /*
  47. * See Figure H-10, "Absolute Accesses when PSW W-bit is 0",
  48. * combined with Figure H-11, as above.
  49. */
  50. if (likely(extract32(addr, 28, 4) != 0xf)) {
  51. /* Memory address space */
  52. addr = (uint32_t)addr;
  53. } else if (extract32(addr, 24, 4) != 0) {
  54. /* I/O address space */
  55. addr = (int32_t)addr;
  56. } else {
  57. /*
  58. * PDC address space:
  59. * Figures H-10 and H-11 of the parisc2.0 spec do not specify
  60. * where to map into the 64-bit PDC address space.
  61. * We map with an offset which equals the 32-bit address, which
  62. * is what can be seen on physical machines too.
  63. */
  64. addr = (uint32_t)addr;
  65. addr |= -1ull << (TARGET_PHYS_ADDR_SPACE_BITS - 4);
  66. }
  67. return addr;
  68. }
  69. static HPPATLBEntry *hppa_find_tlb(CPUHPPAState *env, vaddr addr)
  70. {
  71. IntervalTreeNode *i = interval_tree_iter_first(&env->tlb_root, addr, addr);
  72. if (i) {
  73. HPPATLBEntry *ent = container_of(i, HPPATLBEntry, itree);
  74. trace_hppa_tlb_find_entry(env, ent, ent->entry_valid,
  75. ent->itree.start, ent->itree.last, ent->pa);
  76. return ent;
  77. }
  78. trace_hppa_tlb_find_entry_not_found(env, addr);
  79. return NULL;
  80. }
  81. static void hppa_flush_tlb_ent(CPUHPPAState *env, HPPATLBEntry *ent,
  82. bool force_flush_btlb)
  83. {
  84. CPUState *cs = env_cpu(env);
  85. bool is_btlb;
  86. if (!ent->entry_valid) {
  87. return;
  88. }
  89. trace_hppa_tlb_flush_ent(env, ent, ent->itree.start,
  90. ent->itree.last, ent->pa);
  91. tlb_flush_range_by_mmuidx(cs, ent->itree.start,
  92. ent->itree.last - ent->itree.start + 1,
  93. HPPA_MMU_FLUSH_MASK, TARGET_LONG_BITS);
  94. /* Never clear BTLBs, unless forced to do so. */
  95. is_btlb = ent < &env->tlb[HPPA_BTLB_ENTRIES(env)];
  96. if (is_btlb && !force_flush_btlb) {
  97. return;
  98. }
  99. interval_tree_remove(&ent->itree, &env->tlb_root);
  100. memset(ent, 0, sizeof(*ent));
  101. if (!is_btlb) {
  102. ent->unused_next = env->tlb_unused;
  103. env->tlb_unused = ent;
  104. }
  105. }
  106. static void hppa_flush_tlb_range(CPUHPPAState *env, vaddr va_b, vaddr va_e)
  107. {
  108. IntervalTreeNode *i, *n;
  109. i = interval_tree_iter_first(&env->tlb_root, va_b, va_e);
  110. for (; i ; i = n) {
  111. HPPATLBEntry *ent = container_of(i, HPPATLBEntry, itree);
  112. /*
  113. * Find the next entry now: In the normal case the current entry
  114. * will be removed, but in the BTLB case it will remain.
  115. */
  116. n = interval_tree_iter_next(i, va_b, va_e);
  117. hppa_flush_tlb_ent(env, ent, false);
  118. }
  119. }
  120. static HPPATLBEntry *hppa_alloc_tlb_ent(CPUHPPAState *env)
  121. {
  122. HPPATLBEntry *ent = env->tlb_unused;
  123. if (ent == NULL) {
  124. uint32_t btlb_entries = HPPA_BTLB_ENTRIES(env);
  125. uint32_t i = env->tlb_last;
  126. if (i < btlb_entries || i >= ARRAY_SIZE(env->tlb)) {
  127. i = btlb_entries;
  128. }
  129. env->tlb_last = i + 1;
  130. ent = &env->tlb[i];
  131. hppa_flush_tlb_ent(env, ent, false);
  132. }
  133. env->tlb_unused = ent->unused_next;
  134. return ent;
  135. }
  136. #define ACCESS_ID_MASK 0xffff
  137. /* Return the set of protections allowed by a PID match. */
  138. static int match_prot_id_1(uint32_t access_id, uint32_t prot_id)
  139. {
  140. if (((access_id ^ (prot_id >> 1)) & ACCESS_ID_MASK) == 0) {
  141. return (prot_id & 1
  142. ? PAGE_EXEC | PAGE_READ
  143. : PAGE_EXEC | PAGE_READ | PAGE_WRITE);
  144. }
  145. return 0;
  146. }
  147. static int match_prot_id32(CPUHPPAState *env, uint32_t access_id)
  148. {
  149. int r, i;
  150. for (i = CR_PID1; i <= CR_PID4; ++i) {
  151. r = match_prot_id_1(access_id, env->cr[i]);
  152. if (r) {
  153. return r;
  154. }
  155. }
  156. return 0;
  157. }
  158. static int match_prot_id64(CPUHPPAState *env, uint32_t access_id)
  159. {
  160. int r, i;
  161. for (i = CR_PID1; i <= CR_PID4; ++i) {
  162. r = match_prot_id_1(access_id, env->cr[i]);
  163. if (r) {
  164. return r;
  165. }
  166. r = match_prot_id_1(access_id, env->cr[i] >> 32);
  167. if (r) {
  168. return r;
  169. }
  170. }
  171. return 0;
  172. }
  173. int hppa_get_physical_address(CPUHPPAState *env, vaddr addr, int mmu_idx,
  174. int type, MemOp mop, hwaddr *pphys, int *pprot)
  175. {
  176. hwaddr phys;
  177. int prot, r_prot, w_prot, x_prot, priv;
  178. HPPATLBEntry *ent;
  179. int ret = -1;
  180. /* Virtual translation disabled. Map absolute to physical. */
  181. if (MMU_IDX_MMU_DISABLED(mmu_idx)) {
  182. switch (mmu_idx) {
  183. case MMU_ABS_W_IDX:
  184. phys = hppa_abs_to_phys_pa2_w1(addr);
  185. break;
  186. case MMU_ABS_IDX:
  187. if (hppa_is_pa20(env)) {
  188. phys = hppa_abs_to_phys_pa2_w0(addr);
  189. } else {
  190. phys = (uint32_t)addr;
  191. }
  192. break;
  193. default:
  194. g_assert_not_reached();
  195. }
  196. prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
  197. goto egress_align;
  198. }
  199. /* Find a valid tlb entry that matches the virtual address. */
  200. ent = hppa_find_tlb(env, addr);
  201. if (ent == NULL) {
  202. phys = 0;
  203. prot = 0;
  204. ret = (type == PAGE_EXEC) ? EXCP_ITLB_MISS : EXCP_DTLB_MISS;
  205. goto egress;
  206. }
  207. /* We now know the physical address. */
  208. phys = ent->pa + (addr - ent->itree.start);
  209. /* Map TLB access_rights field to QEMU protection. */
  210. priv = MMU_IDX_TO_PRIV(mmu_idx);
  211. r_prot = (priv <= ent->ar_pl1) * PAGE_READ;
  212. w_prot = (priv <= ent->ar_pl2) * PAGE_WRITE;
  213. x_prot = (ent->ar_pl2 <= priv && priv <= ent->ar_pl1) * PAGE_EXEC;
  214. switch (ent->ar_type) {
  215. case 0: /* read-only: data page */
  216. prot = r_prot;
  217. break;
  218. case 1: /* read/write: dynamic data page */
  219. prot = r_prot | w_prot;
  220. break;
  221. case 2: /* read/execute: normal code page */
  222. prot = r_prot | x_prot;
  223. break;
  224. case 3: /* read/write/execute: dynamic code page */
  225. prot = r_prot | w_prot | x_prot;
  226. break;
  227. default: /* execute: promote to privilege level type & 3 */
  228. prot = x_prot;
  229. break;
  230. }
  231. /*
  232. * No guest access type indicates a non-architectural access from
  233. * within QEMU. Bypass checks for access, D, B, P and T bits.
  234. */
  235. if (type == 0) {
  236. goto egress;
  237. }
  238. if (unlikely(!(prot & type))) {
  239. /* Not allowed -- Inst/Data Memory Access Rights Fault. */
  240. ret = (type & PAGE_EXEC) ? EXCP_IMP : EXCP_DMAR;
  241. goto egress;
  242. }
  243. /* access_id == 0 means public page and no check is performed */
  244. if (ent->access_id && MMU_IDX_TO_P(mmu_idx)) {
  245. int access_prot = (hppa_is_pa20(env)
  246. ? match_prot_id64(env, ent->access_id)
  247. : match_prot_id32(env, ent->access_id));
  248. if (unlikely(!(type & access_prot))) {
  249. /* Not allowed -- Inst/Data Memory Protection Id Fault. */
  250. ret = type & PAGE_EXEC ? EXCP_IMP : EXCP_DMPI;
  251. goto egress;
  252. }
  253. /* Otherwise exclude permissions not allowed (i.e WD). */
  254. prot &= access_prot;
  255. }
  256. /*
  257. * In reverse priority order, check for conditions which raise faults.
  258. * Remove PROT bits that cover the condition we want to check,
  259. * so that the resulting PROT will force a re-check of the
  260. * architectural TLB entry for the next access.
  261. */
  262. if (unlikely(ent->t)) {
  263. prot &= PAGE_EXEC;
  264. if (!(type & PAGE_EXEC)) {
  265. /* The T bit is set -- Page Reference Fault. */
  266. ret = EXCP_PAGE_REF;
  267. }
  268. }
  269. if (unlikely(!ent->d)) {
  270. prot &= PAGE_READ | PAGE_EXEC;
  271. if (type & PAGE_WRITE) {
  272. /* The D bit is not set -- TLB Dirty Bit Fault. */
  273. ret = EXCP_TLB_DIRTY;
  274. }
  275. }
  276. if (unlikely(ent->b)) {
  277. prot &= PAGE_READ | PAGE_EXEC;
  278. if (type & PAGE_WRITE) {
  279. /*
  280. * The B bit is set -- Data Memory Break Fault.
  281. * Except when PSW_X is set, allow this single access to succeed.
  282. * The write bit will be invalidated for subsequent accesses.
  283. */
  284. if (env->psw_xb & PSW_X) {
  285. prot |= PAGE_WRITE_INV;
  286. } else {
  287. ret = EXCP_DMB;
  288. }
  289. }
  290. }
  291. egress_align:
  292. if (addr & ((1u << memop_alignment_bits(mop)) - 1)) {
  293. ret = EXCP_UNALIGN;
  294. }
  295. egress:
  296. *pphys = phys;
  297. *pprot = prot;
  298. trace_hppa_tlb_get_physical_address(env, ret, prot, addr, phys);
  299. return ret;
  300. }
  301. hwaddr hppa_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
  302. {
  303. HPPACPU *cpu = HPPA_CPU(cs);
  304. hwaddr phys;
  305. int prot, excp, mmu_idx;
  306. /* If the (data) mmu is disabled, bypass translation. */
  307. /* ??? We really ought to know if the code mmu is disabled too,
  308. in order to get the correct debugging dumps. */
  309. mmu_idx = (cpu->env.psw & PSW_D ? MMU_KERNEL_IDX :
  310. cpu->env.psw & PSW_W ? MMU_ABS_W_IDX : MMU_ABS_IDX);
  311. excp = hppa_get_physical_address(&cpu->env, addr, mmu_idx, 0, 0,
  312. &phys, &prot);
  313. /* Since we're translating for debugging, the only error that is a
  314. hard error is no translation at all. Otherwise, while a real cpu
  315. access might not have permission, the debugger does. */
  316. return excp == EXCP_DTLB_MISS ? -1 : phys;
  317. }
  318. void hppa_set_ior_and_isr(CPUHPPAState *env, vaddr addr, bool mmu_disabled)
  319. {
  320. if (env->psw & PSW_Q) {
  321. /*
  322. * For pa1.x, the offset and space never overlap, and so we
  323. * simply extract the high and low part of the virtual address.
  324. *
  325. * For pa2.0, the formation of these are described in section
  326. * "Interruption Parameter Registers", page 2-15.
  327. */
  328. env->cr[CR_IOR] = (uint32_t)addr;
  329. env->cr[CR_ISR] = addr >> 32;
  330. if (hppa_is_pa20(env)) {
  331. if (mmu_disabled) {
  332. /*
  333. * If data translation was disabled, the ISR contains
  334. * the upper portion of the abs address, zero-extended.
  335. */
  336. env->cr[CR_ISR] &= 0x3fffffff;
  337. } else {
  338. /*
  339. * If data translation was enabled, the upper two bits
  340. * of the IOR (the b field) are equal to the two space
  341. * bits from the base register used to form the gva.
  342. */
  343. uint64_t b;
  344. b = env->unwind_breg ? env->gr[env->unwind_breg] : 0;
  345. b >>= (env->psw & PSW_W ? 62 : 30);
  346. env->cr[CR_IOR] |= b << 62;
  347. }
  348. }
  349. }
  350. }
  351. G_NORETURN static void
  352. raise_exception_with_ior(CPUHPPAState *env, int excp, uintptr_t retaddr,
  353. vaddr addr, bool mmu_disabled)
  354. {
  355. CPUState *cs = env_cpu(env);
  356. cs->exception_index = excp;
  357. cpu_restore_state(cs, retaddr);
  358. hppa_set_ior_and_isr(env, addr, mmu_disabled);
  359. cpu_loop_exit(cs);
  360. }
  361. void hppa_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
  362. vaddr addr, unsigned size,
  363. MMUAccessType access_type,
  364. int mmu_idx, MemTxAttrs attrs,
  365. MemTxResult response, uintptr_t retaddr)
  366. {
  367. CPUHPPAState *env = cpu_env(cs);
  368. qemu_log_mask(LOG_GUEST_ERROR, "HPMC at " TARGET_FMT_lx ":" TARGET_FMT_lx
  369. " while accessing I/O at %#08" HWADDR_PRIx "\n",
  370. env->iasq_f, env->iaoq_f, physaddr);
  371. /* FIXME: Enable HPMC exceptions when firmware has clean device probing */
  372. if (0) {
  373. raise_exception_with_ior(env, EXCP_HPMC, retaddr, addr,
  374. MMU_IDX_MMU_DISABLED(mmu_idx));
  375. }
  376. }
  377. bool hppa_cpu_tlb_fill_align(CPUState *cs, CPUTLBEntryFull *out, vaddr addr,
  378. MMUAccessType type, int mmu_idx,
  379. MemOp memop, int size, bool probe, uintptr_t ra)
  380. {
  381. CPUHPPAState *env = cpu_env(cs);
  382. int prot, excp, a_prot;
  383. hwaddr phys;
  384. switch (type) {
  385. case MMU_INST_FETCH:
  386. a_prot = PAGE_EXEC;
  387. break;
  388. case MMU_DATA_STORE:
  389. a_prot = PAGE_WRITE;
  390. break;
  391. default:
  392. a_prot = PAGE_READ;
  393. break;
  394. }
  395. excp = hppa_get_physical_address(env, addr, mmu_idx, a_prot, memop,
  396. &phys, &prot);
  397. if (unlikely(excp >= 0)) {
  398. if (probe) {
  399. return false;
  400. }
  401. trace_hppa_tlb_fill_excp(env, addr, size, type, mmu_idx);
  402. /* Failure. Raise the indicated exception. */
  403. raise_exception_with_ior(env, excp, ra, addr,
  404. MMU_IDX_MMU_DISABLED(mmu_idx));
  405. }
  406. trace_hppa_tlb_fill_success(env, addr & TARGET_PAGE_MASK,
  407. phys & TARGET_PAGE_MASK, size, type, mmu_idx);
  408. /*
  409. * Success! Store the translation into the QEMU TLB.
  410. * Note that we always install a single-page entry, because that
  411. * is what works best with softmmu -- anything else will trigger
  412. * the large page protection mask. We do not require this,
  413. * because we record the large page here in the hppa tlb.
  414. */
  415. memset(out, 0, sizeof(*out));
  416. out->phys_addr = phys;
  417. out->prot = prot;
  418. out->attrs = MEMTXATTRS_UNSPECIFIED;
  419. out->lg_page_size = TARGET_PAGE_BITS;
  420. return true;
  421. }
  422. /* Insert (Insn/Data) TLB Address. Note this is PA 1.1 only. */
  423. void HELPER(itlba_pa11)(CPUHPPAState *env, target_ulong addr, target_ulong reg)
  424. {
  425. HPPATLBEntry *ent;
  426. /* Zap any old entries covering ADDR. */
  427. addr &= TARGET_PAGE_MASK;
  428. hppa_flush_tlb_range(env, addr, addr + TARGET_PAGE_SIZE - 1);
  429. ent = env->tlb_partial;
  430. if (ent == NULL) {
  431. ent = hppa_alloc_tlb_ent(env);
  432. env->tlb_partial = ent;
  433. }
  434. /* Note that ent->entry_valid == 0 already. */
  435. ent->itree.start = addr;
  436. ent->itree.last = addr + TARGET_PAGE_SIZE - 1;
  437. ent->pa = extract32(reg, 5, 20) << TARGET_PAGE_BITS;
  438. trace_hppa_tlb_itlba(env, ent, ent->itree.start, ent->itree.last, ent->pa);
  439. }
  440. static void set_access_bits_pa11(CPUHPPAState *env, HPPATLBEntry *ent,
  441. target_ulong reg)
  442. {
  443. ent->access_id = extract32(reg, 1, 18);
  444. ent->u = extract32(reg, 19, 1);
  445. ent->ar_pl2 = extract32(reg, 20, 2);
  446. ent->ar_pl1 = extract32(reg, 22, 2);
  447. ent->ar_type = extract32(reg, 24, 3);
  448. ent->b = extract32(reg, 27, 1);
  449. ent->d = extract32(reg, 28, 1);
  450. ent->t = extract32(reg, 29, 1);
  451. ent->entry_valid = 1;
  452. interval_tree_insert(&ent->itree, &env->tlb_root);
  453. trace_hppa_tlb_itlbp(env, ent, ent->access_id, ent->u, ent->ar_pl2,
  454. ent->ar_pl1, ent->ar_type, ent->b, ent->d, ent->t);
  455. }
  456. /* Insert (Insn/Data) TLB Protection. Note this is PA 1.1 only. */
  457. void HELPER(itlbp_pa11)(CPUHPPAState *env, target_ulong addr, target_ulong reg)
  458. {
  459. HPPATLBEntry *ent = env->tlb_partial;
  460. if (ent) {
  461. env->tlb_partial = NULL;
  462. if (ent->itree.start <= addr && addr <= ent->itree.last) {
  463. set_access_bits_pa11(env, ent, reg);
  464. return;
  465. }
  466. }
  467. qemu_log_mask(LOG_GUEST_ERROR, "ITLBP not following ITLBA\n");
  468. }
  469. static void itlbt_pa20(CPUHPPAState *env, target_ulong r1,
  470. target_ulong r2, vaddr va_b)
  471. {
  472. HPPATLBEntry *ent;
  473. vaddr va_e;
  474. uint64_t va_size;
  475. int mask_shift;
  476. mask_shift = 2 * (r1 & 0xf);
  477. va_size = (uint64_t)TARGET_PAGE_SIZE << mask_shift;
  478. va_b &= -va_size;
  479. va_e = va_b + va_size - 1;
  480. hppa_flush_tlb_range(env, va_b, va_e);
  481. ent = hppa_alloc_tlb_ent(env);
  482. ent->itree.start = va_b;
  483. ent->itree.last = va_e;
  484. /* Extract all 52 bits present in the page table entry. */
  485. ent->pa = r1 << (TARGET_PAGE_BITS - 5);
  486. /* Align per the page size. */
  487. ent->pa &= TARGET_PAGE_MASK << mask_shift;
  488. /* Ignore the bits beyond physical address space. */
  489. ent->pa = sextract64(ent->pa, 0, TARGET_PHYS_ADDR_SPACE_BITS);
  490. ent->t = extract64(r2, 61, 1);
  491. ent->d = extract64(r2, 60, 1);
  492. ent->b = extract64(r2, 59, 1);
  493. ent->ar_type = extract64(r2, 56, 3);
  494. ent->ar_pl1 = extract64(r2, 54, 2);
  495. ent->ar_pl2 = extract64(r2, 52, 2);
  496. ent->u = extract64(r2, 51, 1);
  497. /* o = bit 50 */
  498. /* p = bit 49 */
  499. ent->access_id = extract64(r2, 1, 31);
  500. ent->entry_valid = 1;
  501. interval_tree_insert(&ent->itree, &env->tlb_root);
  502. trace_hppa_tlb_itlba(env, ent, ent->itree.start, ent->itree.last, ent->pa);
  503. trace_hppa_tlb_itlbp(env, ent, ent->access_id, ent->u,
  504. ent->ar_pl2, ent->ar_pl1, ent->ar_type,
  505. ent->b, ent->d, ent->t);
  506. }
  507. void HELPER(idtlbt_pa20)(CPUHPPAState *env, target_ulong r1, target_ulong r2)
  508. {
  509. vaddr va_b = deposit64(env->cr[CR_IOR], 32, 32, env->cr[CR_ISR]);
  510. itlbt_pa20(env, r1, r2, va_b);
  511. }
  512. void HELPER(iitlbt_pa20)(CPUHPPAState *env, target_ulong r1, target_ulong r2)
  513. {
  514. vaddr va_b = deposit64(env->cr[CR_IIAOQ], 32, 32, env->cr[CR_IIASQ]);
  515. itlbt_pa20(env, r1, r2, va_b);
  516. }
  517. /* Purge (Insn/Data) TLB. */
  518. static void ptlb_work(CPUState *cpu, run_on_cpu_data data)
  519. {
  520. vaddr start = data.target_ptr;
  521. vaddr end;
  522. /*
  523. * PA2.0 allows a range of pages encoded into GR[b], which we have
  524. * copied into the bottom bits of the otherwise page-aligned address.
  525. * PA1.x will always provide zero here, for a single page flush.
  526. */
  527. end = start & 0xf;
  528. start &= TARGET_PAGE_MASK;
  529. end = (vaddr)TARGET_PAGE_SIZE << (2 * end);
  530. end = start + end - 1;
  531. hppa_flush_tlb_range(cpu_env(cpu), start, end);
  532. }
  533. /* This is local to the current cpu. */
  534. void HELPER(ptlb_l)(CPUHPPAState *env, target_ulong addr)
  535. {
  536. trace_hppa_tlb_ptlb_local(env);
  537. ptlb_work(env_cpu(env), RUN_ON_CPU_TARGET_PTR(addr));
  538. }
  539. /* This is synchronous across all processors. */
  540. void HELPER(ptlb)(CPUHPPAState *env, target_ulong addr)
  541. {
  542. CPUState *src = env_cpu(env);
  543. CPUState *cpu;
  544. bool wait = false;
  545. trace_hppa_tlb_ptlb(env);
  546. run_on_cpu_data data = RUN_ON_CPU_TARGET_PTR(addr);
  547. CPU_FOREACH(cpu) {
  548. if (cpu != src) {
  549. async_run_on_cpu(cpu, ptlb_work, data);
  550. wait = true;
  551. }
  552. }
  553. if (wait) {
  554. async_safe_run_on_cpu(src, ptlb_work, data);
  555. } else {
  556. ptlb_work(src, data);
  557. }
  558. }
  559. void hppa_ptlbe(CPUHPPAState *env)
  560. {
  561. uint32_t btlb_entries = HPPA_BTLB_ENTRIES(env);
  562. uint32_t i;
  563. /* Zap the (non-btlb) tlb entries themselves. */
  564. memset(&env->tlb[btlb_entries], 0,
  565. sizeof(env->tlb) - btlb_entries * sizeof(env->tlb[0]));
  566. env->tlb_last = btlb_entries;
  567. env->tlb_partial = NULL;
  568. /* Put them all onto the unused list. */
  569. env->tlb_unused = &env->tlb[btlb_entries];
  570. for (i = btlb_entries; i < ARRAY_SIZE(env->tlb) - 1; ++i) {
  571. env->tlb[i].unused_next = &env->tlb[i + 1];
  572. }
  573. /* Re-initialize the interval tree with only the btlb entries. */
  574. memset(&env->tlb_root, 0, sizeof(env->tlb_root));
  575. for (i = 0; i < btlb_entries; ++i) {
  576. if (env->tlb[i].entry_valid) {
  577. interval_tree_insert(&env->tlb[i].itree, &env->tlb_root);
  578. }
  579. }
  580. tlb_flush_by_mmuidx(env_cpu(env), HPPA_MMU_FLUSH_MASK);
  581. }
  582. /* Purge (Insn/Data) TLB entry. This affects an implementation-defined
  583. number of pages/entries (we choose all), and is local to the cpu. */
  584. void HELPER(ptlbe)(CPUHPPAState *env)
  585. {
  586. trace_hppa_tlb_ptlbe(env);
  587. qemu_log_mask(CPU_LOG_MMU, "FLUSH ALL TLB ENTRIES\n");
  588. hppa_ptlbe(env);
  589. }
  590. void cpu_hppa_change_prot_id(CPUHPPAState *env)
  591. {
  592. tlb_flush_by_mmuidx(env_cpu(env), HPPA_MMU_FLUSH_P_MASK);
  593. }
  594. void HELPER(change_prot_id)(CPUHPPAState *env)
  595. {
  596. cpu_hppa_change_prot_id(env);
  597. }
  598. target_ulong HELPER(lpa)(CPUHPPAState *env, target_ulong addr)
  599. {
  600. hwaddr phys;
  601. int prot, excp;
  602. excp = hppa_get_physical_address(env, addr, MMU_KERNEL_IDX, 0, 0,
  603. &phys, &prot);
  604. if (excp >= 0) {
  605. if (excp == EXCP_DTLB_MISS) {
  606. excp = EXCP_NA_DTLB_MISS;
  607. }
  608. trace_hppa_tlb_lpa_failed(env, addr);
  609. raise_exception_with_ior(env, excp, GETPC(), addr, false);
  610. }
  611. trace_hppa_tlb_lpa_success(env, addr, phys);
  612. return phys;
  613. }
  614. /*
  615. * diag_btlb() emulates the PDC PDC_BLOCK_TLB firmware call to
  616. * allow operating systems to modify the Block TLB (BTLB) entries.
  617. * For implementation details see page 1-13 in
  618. * https://parisc.wiki.kernel.org/images-parisc/e/ef/Pdc11-v0.96-Ch1-procs.pdf
  619. */
  620. void HELPER(diag_btlb)(CPUHPPAState *env)
  621. {
  622. unsigned int phys_page, len, slot;
  623. int mmu_idx = cpu_mmu_index(env_cpu(env), 0);
  624. uintptr_t ra = GETPC();
  625. HPPATLBEntry *btlb;
  626. uint64_t virt_page;
  627. uint32_t *vaddr;
  628. uint32_t btlb_entries = HPPA_BTLB_ENTRIES(env);
  629. /* BTLBs are not supported on 64-bit CPUs */
  630. if (btlb_entries == 0) {
  631. env->gr[28] = -1; /* nonexistent procedure */
  632. return;
  633. }
  634. env->gr[28] = 0; /* PDC_OK */
  635. switch (env->gr[25]) {
  636. case 0:
  637. /* return BTLB parameters */
  638. qemu_log_mask(CPU_LOG_MMU, "PDC_BLOCK_TLB: PDC_BTLB_INFO\n");
  639. vaddr = probe_access(env, env->gr[24], 4 * sizeof(uint32_t),
  640. MMU_DATA_STORE, mmu_idx, ra);
  641. if (vaddr == NULL) {
  642. env->gr[28] = -10; /* invalid argument */
  643. } else {
  644. vaddr[0] = cpu_to_be32(1);
  645. vaddr[1] = cpu_to_be32(16 * 1024);
  646. vaddr[2] = cpu_to_be32(PA10_BTLB_FIXED);
  647. vaddr[3] = cpu_to_be32(PA10_BTLB_VARIABLE);
  648. }
  649. break;
  650. case 1:
  651. /* insert BTLB entry */
  652. virt_page = env->gr[24]; /* upper 32 bits */
  653. virt_page <<= 32;
  654. virt_page |= env->gr[23]; /* lower 32 bits */
  655. phys_page = env->gr[22];
  656. len = env->gr[21];
  657. slot = env->gr[19];
  658. qemu_log_mask(CPU_LOG_MMU, "PDC_BLOCK_TLB: PDC_BTLB_INSERT "
  659. "0x%08llx-0x%08llx: vpage 0x%llx for phys page 0x%04x len %d "
  660. "into slot %d\n",
  661. (long long) virt_page << TARGET_PAGE_BITS,
  662. (long long) (virt_page + len) << TARGET_PAGE_BITS,
  663. (long long) virt_page, phys_page, len, slot);
  664. if (slot < btlb_entries) {
  665. btlb = &env->tlb[slot];
  666. /* Force flush of possibly existing BTLB entry. */
  667. hppa_flush_tlb_ent(env, btlb, true);
  668. /* Create new BTLB entry */
  669. btlb->itree.start = virt_page << TARGET_PAGE_BITS;
  670. btlb->itree.last = btlb->itree.start + len * TARGET_PAGE_SIZE - 1;
  671. btlb->pa = phys_page << TARGET_PAGE_BITS;
  672. set_access_bits_pa11(env, btlb, env->gr[20]);
  673. btlb->t = 0;
  674. btlb->d = 1;
  675. } else {
  676. env->gr[28] = -10; /* invalid argument */
  677. }
  678. break;
  679. case 2:
  680. /* Purge BTLB entry */
  681. slot = env->gr[22];
  682. qemu_log_mask(CPU_LOG_MMU, "PDC_BLOCK_TLB: PDC_BTLB_PURGE slot %d\n",
  683. slot);
  684. if (slot < btlb_entries) {
  685. btlb = &env->tlb[slot];
  686. hppa_flush_tlb_ent(env, btlb, true);
  687. } else {
  688. env->gr[28] = -10; /* invalid argument */
  689. }
  690. break;
  691. case 3:
  692. /* Purge all BTLB entries */
  693. qemu_log_mask(CPU_LOG_MMU, "PDC_BLOCK_TLB: PDC_BTLB_PURGE_ALL\n");
  694. for (slot = 0; slot < btlb_entries; slot++) {
  695. btlb = &env->tlb[slot];
  696. hppa_flush_tlb_ent(env, btlb, true);
  697. }
  698. break;
  699. default:
  700. env->gr[28] = -2; /* nonexistent option */
  701. break;
  702. }
  703. }
  704. uint64_t HELPER(b_gate_priv)(CPUHPPAState *env, uint64_t iaoq_f)
  705. {
  706. uint64_t gva = hppa_form_gva(env, env->iasq_f, iaoq_f);
  707. HPPATLBEntry *ent = hppa_find_tlb(env, gva);
  708. if (ent == NULL) {
  709. raise_exception_with_ior(env, EXCP_ITLB_MISS, GETPC(), gva, false);
  710. }
  711. /*
  712. * There should be no need to check page permissions, as that will
  713. * already have been done by tb_lookup via get_page_addr_code.
  714. * All we need at this point is to check the ar_type.
  715. *
  716. * No change for non-gateway pages or for priv decrease.
  717. */
  718. if (ent->ar_type & 4) {
  719. int old_priv = iaoq_f & 3;
  720. int new_priv = ent->ar_type & 3;
  721. if (new_priv < old_priv) {
  722. iaoq_f = (iaoq_f & -4) | new_priv;
  723. }
  724. }
  725. return iaoq_f;
  726. }
  727. void HELPER(update_gva_offset_mask)(CPUHPPAState *env)
  728. {
  729. update_gva_offset_mask(env);
  730. }