qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-arm@nongnu.org, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 10/26] target/arm: Handle floating point registers in exception entry
Date: Tue, 16 Apr 2019 13:57:28 +0100	[thread overview]
Message-ID: <20190416125744.27770-11-peter.maydell@linaro.org> (raw)
In-Reply-To: <20190416125744.27770-1-peter.maydell@linaro.org>

Handle floating point registers in exception entry.
This corresponds to the FP-specific parts of the pseudocode
functions ActivateException() and PushStack().

We defer the code corresponding to UpdateFPCCR() to a later patch.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 target/arm/helper.c | 98 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 95 insertions(+), 3 deletions(-)

diff --git a/target/arm/helper.c b/target/arm/helper.c
index a2222f84803..7b2174a5e61 100644
--- a/target/arm/helper.c
+++ b/target/arm/helper.c
@@ -8172,6 +8172,9 @@ static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain,
     switch_v7m_security_state(env, targets_secure);
     write_v7m_control_spsel(env, 0);
     arm_clear_exclusive(env);
+    /* Clear SFPA and FPCA (has no effect if no FPU) */
+    env->v7m.control[M_REG_S] &=
+        ~(R_V7M_CONTROL_FPCA_MASK | R_V7M_CONTROL_SFPA_MASK);
     /* Clear IT bits */
     env->condexec_bits = 0;
     env->regs[14] = lr;
@@ -8192,6 +8195,20 @@ static bool v7m_push_stack(ARMCPU *cpu)
     uint32_t xpsr = xpsr_read(env);
     uint32_t frameptr = env->regs[13];
     ARMMMUIdx mmu_idx = arm_mmu_idx(env);
+    uint32_t framesize;
+    bool nsacr_cp10 = extract32(env->v7m.nsacr, 10, 1);
+
+    if ((env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) &&
+        (env->v7m.secure || nsacr_cp10)) {
+        if (env->v7m.secure &&
+            env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK) {
+            framesize = 0xa8;
+        } else {
+            framesize = 0x68;
+        }
+    } else {
+        framesize = 0x20;
+    }
 
     /* Align stack pointer if the guest wants that */
     if ((frameptr & 4) &&
@@ -8200,7 +8217,13 @@ static bool v7m_push_stack(ARMCPU *cpu)
         xpsr |= XPSR_SPREALIGN;
     }
 
-    frameptr -= 0x20;
+    xpsr &= ~XPSR_SFPA;
+    if (env->v7m.secure &&
+        (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) {
+        xpsr |= XPSR_SFPA;
+    }
+
+    frameptr -= framesize;
 
     if (arm_feature(env, ARM_FEATURE_V8)) {
         uint32_t limit = v7m_sp_limit(env);
@@ -8244,6 +8267,73 @@ static bool v7m_push_stack(ARMCPU *cpu)
         v7m_stack_write(cpu, frameptr + 24, env->regs[15], mmu_idx, false) &&
         v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, false);
 
+    if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) {
+        /* FPU is active, try to save its registers */
+        bool fpccr_s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
+        bool lspact = env->v7m.fpccr[fpccr_s] & R_V7M_FPCCR_LSPACT_MASK;
+
+        if (lspact && arm_feature(env, ARM_FEATURE_M_SECURITY)) {
+            qemu_log_mask(CPU_LOG_INT,
+                          "...SecureFault because LSPACT and FPCA both set\n");
+            env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK;
+            armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
+        } else if (!env->v7m.secure && !nsacr_cp10) {
+            qemu_log_mask(CPU_LOG_INT,
+                          "...Secure UsageFault with CFSR.NOCP because "
+                          "NSACR.CP10 prevents stacking FP regs\n");
+            armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S);
+            env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK;
+        } else {
+            if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) {
+                /* Lazy stacking disabled, save registers now */
+                int i;
+                bool cpacr_pass = v7m_cpacr_pass(env, env->v7m.secure,
+                                                 arm_current_el(env) != 0);
+
+                if (stacked_ok && !cpacr_pass) {
+                    /*
+                     * Take UsageFault if CPACR forbids access. The pseudocode
+                     * here does a full CheckCPEnabled() but we know the NSACR
+                     * check can never fail as we have already handled that.
+                     */
+                    qemu_log_mask(CPU_LOG_INT,
+                                  "...UsageFault with CFSR.NOCP because "
+                                  "CPACR.CP10 prevents stacking FP regs\n");
+                    armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
+                                            env->v7m.secure);
+                    env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
+                    stacked_ok = false;
+                }
+
+                for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) {
+                    uint64_t dn = *aa32_vfp_dreg(env, i / 2);
+                    uint32_t faddr = frameptr + 0x20 + 4 * i;
+                    uint32_t slo = extract64(dn, 0, 32);
+                    uint32_t shi = extract64(dn, 32, 32);
+
+                    if (i >= 16) {
+                        faddr += 8; /* skip the slot for the FPSCR */
+                    }
+                    stacked_ok = stacked_ok &&
+                        v7m_stack_write(cpu, faddr, slo, mmu_idx, false) &&
+                        v7m_stack_write(cpu, faddr + 4, shi, mmu_idx, false);
+                }
+                stacked_ok = stacked_ok &&
+                    v7m_stack_write(cpu, frameptr + 0x60,
+                                    vfp_get_fpscr(env), mmu_idx, false);
+                if (cpacr_pass) {
+                    for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) {
+                        *aa32_vfp_dreg(env, i / 2) = 0;
+                    }
+                    vfp_set_fpscr(env, 0);
+                }
+            } else {
+                /* Lazy stacking enabled, save necessary info to stack later */
+                /* TODO : equivalent of UpdateFPCCR() pseudocode */
+            }
+        }
+    }
+
     /*
      * If we broke a stack limit then SP was already updated earlier;
      * otherwise we update SP regardless of whether any of the stack
@@ -9004,8 +9094,7 @@ void arm_v7m_cpu_do_interrupt(CPUState *cs)
 
     if (arm_feature(env, ARM_FEATURE_V8)) {
         lr = R_V7M_EXCRET_RES1_MASK |
-            R_V7M_EXCRET_DCRS_MASK |
-            R_V7M_EXCRET_FTYPE_MASK;
+            R_V7M_EXCRET_DCRS_MASK;
         /* The S bit indicates whether we should return to Secure
          * or NonSecure (ie our current state).
          * The ES bit indicates whether we're taking this exception
@@ -9020,6 +9109,9 @@ void arm_v7m_cpu_do_interrupt(CPUState *cs)
         if (env->v7m.secure) {
             lr |= R_V7M_EXCRET_S_MASK;
         }
+        if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) {
+            lr |= R_V7M_EXCRET_FTYPE_MASK;
+        }
     } else {
         lr = R_V7M_EXCRET_RES1_MASK |
             R_V7M_EXCRET_S_MASK |
-- 
2.20.1

  parent reply	other threads:[~2019-04-16 12:58 UTC|newest]

Thread overview: 104+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-16 12:57 [Qemu-devel] [PATCH 00/26] target/arm: Implement M profile floating point Peter Maydell
2019-04-16 12:57 ` Peter Maydell
2019-04-16 12:57 ` [Qemu-devel] [PATCH 01/26] target/arm: Make sure M-profile FPSCR RES0 bits are not settable Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 17:25   ` Richard Henderson
2019-04-23 17:25     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 02/26] hw/intc/armv7m_nvic: Allow reading of M-profile MVFR* registers Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 17:27   ` Richard Henderson
2019-04-23 17:27     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 03/26] target/arm: Implement dummy versions of M-profile FP-related registers Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 17:55   ` Richard Henderson
2019-04-23 17:55     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 04/26] target/arm: Disable most VFP sysregs for M-profile Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 18:08   ` Richard Henderson
2019-04-23 18:08     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 05/26] target/arm: Honour M-profile FP enable bits Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 18:19   ` Richard Henderson
2019-04-23 18:19     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 06/26] target/arm: Decode FP instructions for M profile Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 18:37   ` Richard Henderson
2019-04-23 18:37     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 07/26] target/arm: Clear CONTROL_S.SFPA in SG insn if FPU present Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 20:58   ` Richard Henderson
2019-04-23 20:58     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 08/26] target/arm: Handle SFPA and FPCA bits in reads and writes of CONTROL Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 21:33   ` Richard Henderson
2019-04-23 21:33     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 09/26] target/arm/helper: don't return early for STKOF faults during stacking Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 21:46   ` Richard Henderson
2019-04-23 21:46     ` Richard Henderson
2019-04-16 12:57 ` Peter Maydell [this message]
2019-04-16 12:57   ` [Qemu-devel] [PATCH 10/26] target/arm: Handle floating point registers in exception entry Peter Maydell
2019-04-23 22:21   ` Richard Henderson
2019-04-23 22:21     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 11/26] target/arm: Implement v7m_update_fpccr() Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-16 12:57 ` [Qemu-devel] [PATCH 12/26] target/arm: Clear CONTROL.SFPA in BXNS and BLXNS Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 22:50   ` Richard Henderson
2019-04-23 22:50     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 13/26] target/arm: Clean excReturn bits when tail chaining Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 22:54   ` Richard Henderson
2019-04-23 22:54     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 14/26] target/arm: Allow for floating point in callee stack integrity check Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 23:04   ` Richard Henderson
2019-04-23 23:04     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 15/26] target/arm: Handle floating point registers in exception return Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 23:29   ` Richard Henderson
2019-04-23 23:29     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 16/26] target/arm: Move NS TBFLAG from bit 19 to bit 6 Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 23:47   ` Richard Henderson
2019-04-23 23:47     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 17/26] target/arm: Overlap VECSTRIDE and XSCALE_CPAR TB flags Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-23 23:51   ` Richard Henderson
2019-04-23 23:51     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 18/26] target/arm: Set FPCCR.S when executing M-profile floating point insns Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-24  0:00   ` Richard Henderson
2019-04-24  0:00     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 19/26] target/arm: Activate M-profile floating point context when FPCCR.ASPEN is set Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-24  0:08   ` Richard Henderson
2019-04-24  0:08     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 20/26] target/arm: New helper function arm_v7m_mmu_idx_all() Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-24  0:12   ` Richard Henderson
2019-04-24  0:12     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 21/26] target/arm: New function armv7m_nvic_set_pending_lazyfp() Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-24  1:10   ` Richard Henderson
2019-04-24  1:10     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 22/26] target/arm: Add lazy-FP-stacking support to v7m_stack_write() Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-24  1:27   ` Richard Henderson
2019-04-24  1:27     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 23/26] target/arm: Implement M-profile lazy FP state preservation Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-24  2:04   ` Richard Henderson
2019-04-24  2:04     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 24/26] target/arm: Implement VLSTM for v7M CPUs with an FPU Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-24  2:17   ` Richard Henderson
2019-04-24  2:17     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 25/26] target/arm: Implement VLLDM " Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-24  2:21   ` Richard Henderson
2019-04-24  2:21     ` Richard Henderson
2019-04-16 12:57 ` [Qemu-devel] [PATCH 26/26] target/arm: Enable FPU for Cortex-M4 and Cortex-M33 Peter Maydell
2019-04-16 12:57   ` Peter Maydell
2019-04-24  2:25   ` Richard Henderson
2019-04-24  2:25     ` Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190416125744.27770-11-peter.maydell@linaro.org \
    --to=peter.maydell@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).