0002-Revert-target-arm-Fix-usage-of-MMU-indexes-when-EL3-.patch 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. From 4a6a8a9e87e2de4acd37a9de44617d9b773b0b7c Mon Sep 17 00:00:00 2001
  2. From: Peter Maydell <peter.maydell@linaro.org>
  3. Date: Fri, 1 Nov 2024 14:28:44 +0000
  4. Subject: [PATCH] Revert "target/arm: Fix usage of MMU indexes when EL3 is
  5. AArch32"
  6. This reverts commit 4c2c0474693229c1f533239bb983495c5427784d.
  7. This commit tried to fix a problem with our usage of MMU indexes when
  8. EL3 is AArch32, using what it described as a "more complicated
  9. approach" where we share the same MMU index values for Secure PL1&0
  10. and NonSecure PL1&0. In theory this should work, but the change
  11. didn't account for (at least) two things:
  12. (1) The design change means we need to flush the TLBs at any point
  13. where the CPU state flips from one to the other. We already flush
  14. the TLB when SCR.NS is changed, but we don't flush the TLB when we
  15. take an exception from NS PL1&0 into Mon or when we return from Mon
  16. to NS PL1&0, and the commit didn't add any code to do that.
  17. (2) The ATS12NS* address translate instructions allow Mon code (which
  18. is Secure) to do a stage 1+2 page table walk for NS. I thought this
  19. was OK because do_ats_write() does a page table walk which doesn't
  20. use the TLBs, so because it can pass both the MMU index and also an
  21. ARMSecuritySpace argument we can tell the table walk that we want NS
  22. stage1+2, not S. But that means that all the code within the ptw
  23. that needs to find e.g. the regime EL cannot do so only with an
  24. mmu_idx -- all these functions like regime_sctlr(), regime_el(), etc
  25. would need to pass both an mmu_idx and the security_space, so they
  26. can tell whether this is a translation regime controlled by EL1 or
  27. EL3 (and so whether to look at SCTLR.S or SCTLR.NS, etc).
  28. In particular, because regime_el() wasn't updated to look at the
  29. ARMSecuritySpace it would return 1 even when the CPU was in Monitor
  30. mode (and the controlling EL is 3). This meant that page table walks
  31. in Monitor mode would look at the wrong SCTLR, TCR, etc and would
  32. generally fault when they should not.
  33. Rather than trying to make the complicated changes needed to rescue
  34. the design of 4c2c04746932, we revert it in order to instead take the
  35. route that that commit describes as "the most straightforward" fix,
  36. where we add new MMU indexes EL30_0, EL30_3, EL30_3_PAN to correspond
  37. to "Secure PL1&0 at PL0", "Secure PL1&0 at PL1", and "Secure PL1&0 at
  38. PL1 with PAN".
  39. This revert will re-expose the "spurious alignment faults in
  40. Secure PL0" issue #2326; we'll fix it again in the next commit.
  41. Cc: qemu-stable@nongnu.org
  42. Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
  43. Upstream-Status: Pending
  44. Upstream: https://patchew.org/QEMU/20241101142845.1712482-1-peter.maydell@linaro.org
  45. See: https://gitlab.com/qemu-project/qemu/-/issues/2588#note_2189338512
  46. Signed-off-by: Romain Naour <romain.naour@smile.fr>
  47. ---
  48. target/arm/cpu.h | 31 +++++++++++++------------------
  49. target/arm/helper.c | 34 +++++++++++-----------------------
  50. target/arm/internals.h | 27 ++++-----------------------
  51. target/arm/ptw.c | 6 +-----
  52. target/arm/tcg/hflags.c | 4 ----
  53. target/arm/tcg/translate-a64.c | 2 +-
  54. target/arm/tcg/translate.c | 9 ++++-----
  55. target/arm/tcg/translate.h | 2 --
  56. 8 files changed, 34 insertions(+), 81 deletions(-)
  57. diff --git a/target/arm/cpu.h b/target/arm/cpu.h
  58. index 9a3fd59562..216774f5d3 100644
  59. --- a/target/arm/cpu.h
  60. +++ b/target/arm/cpu.h
  61. @@ -2784,7 +2784,8 @@ bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync);
  62. * + NonSecure PL1 & 0 stage 1
  63. * + NonSecure PL1 & 0 stage 2
  64. * + NonSecure PL2
  65. - * + Secure PL1 & 0
  66. + * + Secure PL0
  67. + * + Secure PL1
  68. * (reminder: for 32 bit EL3, Secure PL1 is *EL3*, not EL1.)
  69. *
  70. * For QEMU, an mmu_idx is not quite the same as a translation regime because:
  71. @@ -2802,39 +2803,37 @@ bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync);
  72. * The only use of stage 2 translations is either as part of an s1+2
  73. * lookup or when loading the descriptors during a stage 1 page table walk,
  74. * and in both those cases we don't use the TLB.
  75. - * 4. we want to be able to use the TLB for accesses done as part of a
  76. + * 4. we can also safely fold together the "32 bit EL3" and "64 bit EL3"
  77. + * translation regimes, because they map reasonably well to each other
  78. + * and they can't both be active at the same time.
  79. + * 5. we want to be able to use the TLB for accesses done as part of a
  80. * stage1 page table walk, rather than having to walk the stage2 page
  81. * table over and over.
  82. - * 5. we need separate EL1/EL2 mmu_idx for handling the Privileged Access
  83. + * 6. we need separate EL1/EL2 mmu_idx for handling the Privileged Access
  84. * Never (PAN) bit within PSTATE.
  85. - * 6. we fold together most secure and non-secure regimes for A-profile,
  86. + * 7. we fold together most secure and non-secure regimes for A-profile,
  87. * because there are no banked system registers for aarch64, so the
  88. * process of switching between secure and non-secure is
  89. * already heavyweight.
  90. - * 7. we cannot fold together Stage 2 Secure and Stage 2 NonSecure,
  91. + * 8. we cannot fold together Stage 2 Secure and Stage 2 NonSecure,
  92. * because both are in use simultaneously for Secure EL2.
  93. *
  94. * This gives us the following list of cases:
  95. *
  96. - * EL0 EL1&0 stage 1+2 (or AArch32 PL0 PL1&0 stage 1+2)
  97. - * EL1 EL1&0 stage 1+2 (or AArch32 PL1 PL1&0 stage 1+2)
  98. - * EL1 EL1&0 stage 1+2 +PAN (or AArch32 PL1 PL1&0 stage 1+2 +PAN)
  99. + * EL0 EL1&0 stage 1+2 (aka NS PL0)
  100. + * EL1 EL1&0 stage 1+2 (aka NS PL1)
  101. + * EL1 EL1&0 stage 1+2 +PAN
  102. * EL0 EL2&0
  103. * EL2 EL2&0
  104. * EL2 EL2&0 +PAN
  105. * EL2 (aka NS PL2)
  106. - * EL3 (not used when EL3 is AArch32)
  107. + * EL3 (aka S PL1)
  108. * Stage2 Secure
  109. * Stage2 NonSecure
  110. * plus one TLB per Physical address space: S, NS, Realm, Root
  111. *
  112. * for a total of 14 different mmu_idx.
  113. *
  114. - * Note that when EL3 is AArch32, the usage is potentially confusing
  115. - * because the MMU indexes are named for their AArch64 use, so code
  116. - * using the ARMMMUIdx_E10_1 might be at EL3, not EL1. This is because
  117. - * Secure PL1 is always at EL3.
  118. - *
  119. * R profile CPUs have an MPU, but can use the same set of MMU indexes
  120. * as A profile. They only need to distinguish EL0 and EL1 (and
  121. * EL2 for cores like the Cortex-R52).
  122. @@ -3127,10 +3126,6 @@ FIELD(TBFLAG_A32, NS, 10, 1)
  123. * This requires an SME trap from AArch32 mode when using NEON.
  124. */
  125. FIELD(TBFLAG_A32, SME_TRAP_NONSTREAMING, 11, 1)
  126. -/*
  127. - * Indicates whether we are in the Secure PL1&0 translation regime
  128. - */
  129. -FIELD(TBFLAG_A32, S_PL1_0, 12, 1)
  130. /*
  131. * Bit usage when in AArch32 state, for M-profile only.
  132. diff --git a/target/arm/helper.c b/target/arm/helper.c
  133. index 0a582c1cd3..8fb4b474e8 100644
  134. --- a/target/arm/helper.c
  135. +++ b/target/arm/helper.c
  136. @@ -3700,7 +3700,7 @@ static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
  137. */
  138. format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
  139. - if (arm_feature(env, ARM_FEATURE_EL2) && !arm_aa32_secure_pl1_0(env)) {
  140. + if (arm_feature(env, ARM_FEATURE_EL2)) {
  141. if (mmu_idx == ARMMMUIdx_E10_0 ||
  142. mmu_idx == ARMMMUIdx_E10_1 ||
  143. mmu_idx == ARMMMUIdx_E10_1_PAN) {
  144. @@ -3774,11 +3774,13 @@ static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
  145. case 0:
  146. /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
  147. switch (el) {
  148. + case 3:
  149. + mmu_idx = ARMMMUIdx_E3;
  150. + break;
  151. case 2:
  152. g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
  153. /* fall through */
  154. case 1:
  155. - case 3:
  156. if (ri->crm == 9 && arm_pan_enabled(env)) {
  157. mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
  158. } else {
  159. @@ -11859,11 +11861,8 @@ void arm_cpu_do_interrupt(CPUState *cs)
  160. uint64_t arm_sctlr(CPUARMState *env, int el)
  161. {
  162. - if (arm_aa32_secure_pl1_0(env)) {
  163. - /* In Secure PL1&0 SCTLR_S is always controlling */
  164. - el = 3;
  165. - } else if (el == 0) {
  166. - /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
  167. + /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
  168. + if (el == 0) {
  169. ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
  170. el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
  171. }
  172. @@ -12523,12 +12522,8 @@ int fp_exception_el(CPUARMState *env, int cur_el)
  173. return 0;
  174. }
  175. -/*
  176. - * Return the exception level we're running at if this is our mmu_idx.
  177. - * s_pl1_0 should be true if this is the AArch32 Secure PL1&0 translation
  178. - * regime.
  179. - */
  180. -int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx, bool s_pl1_0)
  181. +/* Return the exception level we're running at if this is our mmu_idx */
  182. +int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
  183. {
  184. if (mmu_idx & ARM_MMU_IDX_M) {
  185. return mmu_idx & ARM_MMU_IDX_M_PRIV;
  186. @@ -12540,7 +12535,7 @@ int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx, bool s_pl1_0)
  187. return 0;
  188. case ARMMMUIdx_E10_1:
  189. case ARMMMUIdx_E10_1_PAN:
  190. - return s_pl1_0 ? 3 : 1;
  191. + return 1;
  192. case ARMMMUIdx_E2:
  193. case ARMMMUIdx_E20_2:
  194. case ARMMMUIdx_E20_2_PAN:
  195. @@ -12578,15 +12573,6 @@ ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
  196. idx = ARMMMUIdx_E10_0;
  197. }
  198. break;
  199. - case 3:
  200. - /*
  201. - * AArch64 EL3 has its own translation regime; AArch32 EL3
  202. - * uses the Secure PL1&0 translation regime.
  203. - */
  204. - if (arm_el_is_aa64(env, 3)) {
  205. - return ARMMMUIdx_E3;
  206. - }
  207. - /* fall through */
  208. case 1:
  209. if (arm_pan_enabled(env)) {
  210. idx = ARMMMUIdx_E10_1_PAN;
  211. @@ -12606,6 +12592,8 @@ ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
  212. idx = ARMMMUIdx_E2;
  213. }
  214. break;
  215. + case 3:
  216. + return ARMMMUIdx_E3;
  217. default:
  218. g_assert_not_reached();
  219. }
  220. diff --git a/target/arm/internals.h b/target/arm/internals.h
  221. index 38545552d0..47624318c2 100644
  222. --- a/target/arm/internals.h
  223. +++ b/target/arm/internals.h
  224. @@ -275,20 +275,6 @@ FIELD(CNTHCTL, CNTPMASK, 19, 1)
  225. #define M_FAKE_FSR_NSC_EXEC 0xf /* NS executing in S&NSC memory */
  226. #define M_FAKE_FSR_SFAULT 0xe /* SecureFault INVTRAN, INVEP or AUVIOL */
  227. -/**
  228. - * arm_aa32_secure_pl1_0(): Return true if in Secure PL1&0 regime
  229. - *
  230. - * Return true if the CPU is in the Secure PL1&0 translation regime.
  231. - * This requires that EL3 exists and is AArch32 and we are currently
  232. - * Secure. If this is the case then the ARMMMUIdx_E10* apply and
  233. - * mean we are in EL3, not EL1.
  234. - */
  235. -static inline bool arm_aa32_secure_pl1_0(CPUARMState *env)
  236. -{
  237. - return arm_feature(env, ARM_FEATURE_EL3) &&
  238. - !arm_el_is_aa64(env, 3) && arm_is_secure(env);
  239. -}
  240. -
  241. /**
  242. * raise_exception: Raise the specified exception.
  243. * Raise a guest exception with the specified value, syndrome register
  244. @@ -822,12 +808,7 @@ static inline ARMMMUIdx core_to_aa64_mmu_idx(int mmu_idx)
  245. return mmu_idx | ARM_MMU_IDX_A;
  246. }
  247. -/**
  248. - * Return the exception level we're running at if our current MMU index
  249. - * is @mmu_idx. @s_pl1_0 should be true if this is the AArch32
  250. - * Secure PL1&0 translation regime.
  251. - */
  252. -int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx, bool s_pl1_0);
  253. +int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx);
  254. /* Return the MMU index for a v7M CPU in the specified security state */
  255. ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate);
  256. @@ -922,11 +903,11 @@ static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
  257. return 3;
  258. case ARMMMUIdx_E10_0:
  259. case ARMMMUIdx_Stage1_E0:
  260. - case ARMMMUIdx_E10_1:
  261. - case ARMMMUIdx_E10_1_PAN:
  262. + return arm_el_is_aa64(env, 3) || !arm_is_secure_below_el3(env) ? 1 : 3;
  263. case ARMMMUIdx_Stage1_E1:
  264. case ARMMMUIdx_Stage1_E1_PAN:
  265. - return arm_el_is_aa64(env, 3) || !arm_is_secure_below_el3(env) ? 1 : 3;
  266. + case ARMMMUIdx_E10_1:
  267. + case ARMMMUIdx_E10_1_PAN:
  268. case ARMMMUIdx_MPrivNegPri:
  269. case ARMMMUIdx_MUserNegPri:
  270. case ARMMMUIdx_MPriv:
  271. diff --git a/target/arm/ptw.c b/target/arm/ptw.c
  272. index 26e670290f..20ab736793 100644
  273. --- a/target/arm/ptw.c
  274. +++ b/target/arm/ptw.c
  275. @@ -3576,11 +3576,7 @@ bool get_phys_addr(CPUARMState *env, vaddr address,
  276. case ARMMMUIdx_Stage1_E1:
  277. case ARMMMUIdx_Stage1_E1_PAN:
  278. case ARMMMUIdx_E2:
  279. - if (arm_aa32_secure_pl1_0(env)) {
  280. - ss = ARMSS_Secure;
  281. - } else {
  282. - ss = arm_security_space_below_el3(env);
  283. - }
  284. + ss = arm_security_space_below_el3(env);
  285. break;
  286. case ARMMMUIdx_Stage2:
  287. /*
  288. diff --git a/target/arm/tcg/hflags.c b/target/arm/tcg/hflags.c
  289. index bab7822ef6..f03977b4b0 100644
  290. --- a/target/arm/tcg/hflags.c
  291. +++ b/target/arm/tcg/hflags.c
  292. @@ -198,10 +198,6 @@ static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
  293. DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
  294. }
  295. - if (arm_aa32_secure_pl1_0(env)) {
  296. - DP_TBFLAG_A32(flags, S_PL1_0, 1);
  297. - }
  298. -
  299. return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
  300. }
  301. diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
  302. index 4684e7eb6e..bc2d64e883 100644
  303. --- a/target/arm/tcg/translate-a64.c
  304. +++ b/target/arm/tcg/translate-a64.c
  305. @@ -11979,7 +11979,7 @@ static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
  306. dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
  307. dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
  308. dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
  309. - dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx, false);
  310. + dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
  311. #if !defined(CONFIG_USER_ONLY)
  312. dc->user = (dc->current_el == 0);
  313. #endif
  314. diff --git a/target/arm/tcg/translate.c b/target/arm/tcg/translate.c
  315. index e2748ff2bb..c5bc691d92 100644
  316. --- a/target/arm/tcg/translate.c
  317. +++ b/target/arm/tcg/translate.c
  318. @@ -7546,6 +7546,10 @@ static void arm_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
  319. core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
  320. dc->mmu_idx = core_to_arm_mmu_idx(env, core_mmu_idx);
  321. + dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
  322. +#if !defined(CONFIG_USER_ONLY)
  323. + dc->user = (dc->current_el == 0);
  324. +#endif
  325. dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
  326. dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
  327. dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
  328. @@ -7576,12 +7580,7 @@ static void arm_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
  329. }
  330. dc->sme_trap_nonstreaming =
  331. EX_TBFLAG_A32(tb_flags, SME_TRAP_NONSTREAMING);
  332. - dc->s_pl1_0 = EX_TBFLAG_A32(tb_flags, S_PL1_0);
  333. }
  334. - dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx, dc->s_pl1_0);
  335. -#if !defined(CONFIG_USER_ONLY)
  336. - dc->user = (dc->current_el == 0);
  337. -#endif
  338. dc->lse2 = false; /* applies only to aarch64 */
  339. dc->cp_regs = cpu->cp_regs;
  340. dc->features = env->features;
  341. diff --git a/target/arm/tcg/translate.h b/target/arm/tcg/translate.h
  342. index 3f0e9ceaa3..01c217f4a4 100644
  343. --- a/target/arm/tcg/translate.h
  344. +++ b/target/arm/tcg/translate.h
  345. @@ -165,8 +165,6 @@ typedef struct DisasContext {
  346. uint8_t gm_blocksize;
  347. /* True if the current insn_start has been updated. */
  348. bool insn_start_updated;
  349. - /* True if this is the AArch32 Secure PL1&0 translation regime */
  350. - bool s_pl1_0;
  351. /* Bottom two bits of XScale c15_cpar coprocessor access control reg */
  352. int c15_cpar;
  353. /* Offset from VNCR_EL2 when FEAT_NV2 redirects this reg to memory */
  354. --
  355. 2.45.0