Skip to contentSkip to conversation

target-arm: add CPU core TrustZone support

RFC PATCH In review Sergey Fedorov 3 participants 67 messages Dec 3, 2013 – Dec 23, 2013

lore
Share & tools
lore
#0 lore raw

cover target-arm: add CPU core TrustZone support

This patch set implements a basic support of CPU core TrustZone feature. The following major functionalities are implemented:
* CPU monitor mode
* Separate code translation for each secure state
* CPACR & NSACR co-processor access control
* Separate TLB for each secure state
* Co-processor register banking
* SMC instruction
* FIQ/IRQ routing to monitor mode

There is no support for banked co-processor register migration, save/load its VM state yet. That is an open question how to implement this functionality. Any suggestions is greatly appreciated.

This patch set is a request for comments for the proof of concept.

Sergey Fedorov (18):
target-arm: move SCR & VBAR into TrustZone register list
target-arm: adjust TTBCR for TrustZone feature
target-arm: add arm_is_secure() helper
target-arm: reject switching to monitor mode from non-secure state
target-arm: adjust arm_current_pl() for TrustZone
target-arm: adjust SCR CP15 register access rights
target-arm: add non-secure Translation Block flag
target-arm: implement CPACR register logic
target-arm: add NSACR support
target-arm: add SDER definition
target-arm: split TLB for secure state
target-arm: add banked coprocessor register type
target-arm: convert appropriate coprocessor registers to banked type
target-arm: use c13_context field for CONTEXTIDR
target-arm: switch banked CP registers
target-arm: add MVBAR support
target-arm: implement SMC instruction
target-arm: implement IRQ/FIQ routing to Monitor mode

Svetlana Fedoseeva (3):
target-arm: add TrustZone CPU feature
target-arm: preserve RAO/WI bits of ARMv7 SCTLR
target-arm: add CPU Monitor mode

 target-arm/cpu.c       |    6 +-
 target-arm/cpu.h       |  126 ++++++++++++----
 target-arm/helper.c    |  308 +++++++++++++++++++++++++++++---------
 target-arm/machine.c   |   12 +-
 target-arm/translate.c |  388 ++++++++++++++++++++++++++++++------------------
 target-arm/translate.h |    2 +
 6 files changed, 585 insertions(+), 257 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

RFC PATCH 1/21 target-arm: add TrustZone CPU feature

From: Svetlana Fedoseeva <[email protected]>

Define TrustZone CPU feature. Set that feature for relevant CPUs.

---
 target-arm/cpu.c |    4 ++++
 target-arm/cpu.h |    1 +
 2 files changed, 5 insertions(+)
target-arm/cpu.c +4 -0
index d40f2a7..4607ca8 100644
@@ -439,6 +439,7 @@ static void arm1176_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
     set_feature(&cpu->env, ARM_FEATURE_CACHE_DIRTY_REG);
     set_feature(&cpu->env, ARM_FEATURE_CACHE_BLOCK_OPS);
+    set_feature(&cpu->env, ARM_FEATURE_TRUSTZONE);
     cpu->midr = 0x410fb767;
     cpu->reset_fpsid = 0x410120b5;
     cpu->mvfr0 = 0x11111111;
@@ -521,6 +522,7 @@ static void cortex_a8_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_NEON);
     set_feature(&cpu->env, ARM_FEATURE_THUMB2EE);
     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
+    set_feature(&cpu->env, ARM_FEATURE_TRUSTZONE);
     cpu->midr = 0x410fc080;
     cpu->reset_fpsid = 0x410330c0;
     cpu->mvfr0 = 0x11110222;
@@ -585,6 +587,7 @@ static void cortex_a9_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_VFP_FP16);
     set_feature(&cpu->env, ARM_FEATURE_NEON);
     set_feature(&cpu->env, ARM_FEATURE_THUMB2EE);
+    set_feature(&cpu->env, ARM_FEATURE_TRUSTZONE);
     /* Note that A9 supports the MP extensions even for
      * A9UP and single-core A9MP (which are both different
      * and valid configurations; we don't model A9UP).
@@ -658,6 +661,7 @@ static void cortex_a15_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER);
     set_feature(&cpu->env, ARM_FEATURE_DUMMY_C15_REGS);
     set_feature(&cpu->env, ARM_FEATURE_LPAE);
+    set_feature(&cpu->env, ARM_FEATURE_TRUSTZONE);
     cpu->midr = 0x412fc0f1;
     cpu->reset_fpsid = 0x410430f0;
     cpu->mvfr0 = 0x10110222;
target-arm/cpu.h +1 -0
index 9f110f1..0b93e39 100644
@@ -465,6 +465,7 @@ enum arm_features {
     ARM_FEATURE_LPAE, /* has Large Physical Address Extension */
     ARM_FEATURE_V8,
     ARM_FEATURE_AARCH64, /* supports 64 bit mode */
+    ARM_FEATURE_TRUSTZONE, /* has TrustZone Security Extensions */
 };
 
 static inline int arm_feature(CPUARMState *env, int feature)
Signature · 1 line
1.7.9.5
#2 lore raw

RFC PATCH 2/21 target-arm: move SCR & VBAR into TrustZone register list

In reply to Sergey Fedorov

Define a new ARM CP register info list for TrustZone Security Extension feature. Register that list only for ARM cores with TrustZone support. SCR and VBAR are security extension registers. So move them into TrustZone feature register list.

---
 target-arm/helper.c |   39 +++++++++++++++++++++++++--------------
 1 file changed, 25 insertions(+), 14 deletions(-)
target-arm/helper.c +25 -14
index 3445813..a247ca0 100644
@@ -543,13 +543,6 @@ static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
     return 0;
 }
 
-static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
-                      uint64_t value)
-{
-    env->cp15.c12_vbar = value & ~0x1Ful;
-    return 0;
-}
-
 static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
                        uint64_t *value)
 {
@@ -635,13 +628,6 @@ static const ARMCPRegInfo v7_cp_reginfo[] = {
       .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
       .resetvalue = 0, .writefn = pmintenclr_write, },
-    { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
-      .access = PL1_RW, .writefn = vbar_write,
-      .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
-      .resetvalue = 0 },
-    { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
-      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
-      .resetvalue = 0, },
     { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
       .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
     { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
@@ -1526,6 +1512,28 @@ static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
     return 0;
 }
 
+#ifndef CONFIG_USER_ONLY
+static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
+                      uint64_t value)
+{
+    env->cp15.c12_vbar = value & ~0x1Ful;
+    return 0;
+}
+#endif
+
+static const ARMCPRegInfo tz_cp_reginfo[] = {
+#ifndef CONFIG_USER_ONLY
+    { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
+      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
+      .resetvalue = 0 },
+    { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
+      .access = PL1_RW, .writefn = vbar_write,
+      .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
+      .resetvalue = 0 },
+#endif
+    REGINFO_SENTINEL
+};
+
 void register_cp_regs_for_features(ARMCPU *cpu)
 {
     /* Register all the coprocessor registers based on feature bits */
@@ -1663,6 +1671,9 @@ void register_cp_regs_for_features(ARMCPU *cpu)
     if (arm_feature(env, ARM_FEATURE_LPAE)) {
         define_arm_cp_regs(cpu, lpae_cp_reginfo);
     }
+    if (arm_feature(env, ARM_FEATURE_TRUSTZONE)) {
+        define_arm_cp_regs(cpu, tz_cp_reginfo);
+    }
     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
      * be read-only (ie write causes UNDEF exception).
Signature · 1 line
1.7.9.5
#3 lore raw

Re: [Qemu-devel] [RFC PATCH 02/21] target-arm: move SCR & VBAR into TrustZone register list

Quoted 72 lines · Sergey Fedorov

Define a new ARM CP register info list for TrustZone Security Extension feature. Register that list only for ARM cores with TrustZone support. SCR and VBAR are security extension registers. So move them into TrustZone feature register list.

---
 target-arm/helper.c |   39 +++++++++++++++++++++++++--------------
 1 file changed, 25 insertions(+), 14 deletions(-)
target-arm/helper.c +22 -14
index 3445813..a247ca0 100644
@@ -543,13 +543,6 @@ static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
     return 0;
 }
 
-static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
-                      uint64_t value)
-{
-    env->cp15.c12_vbar = value & ~0x1Ful;
-    return 0;
-}
-
 static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
                        uint64_t *value)
 {
@@ -635,13 +628,6 @@ static const ARMCPRegInfo v7_cp_reginfo[] = {
       .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
       .resetvalue = 0, .writefn = pmintenclr_write, },
-    { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
-      .access = PL1_RW, .writefn = vbar_write,
-      .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
-      .resetvalue = 0 },
-    { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
-      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
-      .resetvalue = 0, },
     { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
       .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
     { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
@@ -1526,6 +1512,28 @@ static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
     return 0;
 }
 
+#ifndef CONFIG_USER_ONLY
+static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
+                      uint64_t value)
+{
+    env->cp15.c12_vbar = value & ~0x1Ful;
+    return 0;
+}
+#endif
+
+static const ARMCPRegInfo tz_cp_reginfo[] = {
+#ifndef CONFIG_USER_ONLY
+    { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
+      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
+      .resetvalue = 0 },
+    { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
+      .access = PL1_RW, .writefn = vbar_write,
+      .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
+      .resetvalue = 0 },
+#endif
+    REGINFO_SENTINEL
+};
+
 void register_cp_regs_for_features(ARMCPU *cpu)
 {
     /* Register all the coprocessor registers based on feature bits */

target-arm/helper.c void register_cp_regs_for_features(ARMCPU *cpu) · line 1675

Quoted 5 lines
target-arm/helper.c +2 -0
@@ -1663,6 +1671,9 @@ void register_cp_regs_for_features(ARMCPU *cpu)
     if (arm_feature(env, ARM_FEATURE_LPAE)) {
         define_arm_cp_regs(cpu, lpae_cp_reginfo);
     }
+    if (arm_feature(env, ARM_FEATURE_TRUSTZONE)) {
+        define_arm_cp_regs(cpu, tz_cp_reginfo);

So ARM docmentation refers to these features as being conditional on the "security extensions" option, not "trustzone". To match documentation i think it may actually be ARM_FEATURE_SECURITY_EXTENSIONS (or some truntaction thereof for brevity). On what level of ARM documentation is the "trustzone" term defined?

Regards,
Peter

Quoted 8 lines
target-arm/helper.c +1 -1
+    }
     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
      * be read-only (ie write causes UNDEF exception).
--

1.7.9.5

#4 lore raw

Re: [Qemu-devel] [RFC PATCH 02/21] target-arm: move SCR & VBAR into TrustZone register list

Quoted 85 lines · Peter Crosthwaite
Quoted 72 lines · Sergey Fedorov

Define a new ARM CP register info list for TrustZone Security Extension feature. Register that list only for ARM cores with TrustZone support. SCR and VBAR are security extension registers. So move them into TrustZone feature register list.

---
  target-arm/helper.c |   39 +++++++++++++++++++++++++--------------
  1 file changed, 25 insertions(+), 14 deletions(-)
target-arm/helper.c +22 -14
index 3445813..a247ca0 100644
@@ -543,13 +543,6 @@ static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
      return 0;
  }
 
-static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
-                      uint64_t value)
-{
-    env->cp15.c12_vbar = value & ~0x1Ful;
-    return 0;
-}
-
  static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
                         uint64_t *value)
  {
@@ -635,13 +628,6 @@ static const ARMCPRegInfo v7_cp_reginfo[] = {
        .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
        .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
        .resetvalue = 0, .writefn = pmintenclr_write, },
-    { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
-      .access = PL1_RW, .writefn = vbar_write,
-      .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
-      .resetvalue = 0 },
-    { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
-      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
-      .resetvalue = 0, },
      { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
        .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
      { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
@@ -1526,6 +1512,28 @@ static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
      return 0;
  }
 
+#ifndef CONFIG_USER_ONLY
+static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
+                      uint64_t value)
+{
+    env->cp15.c12_vbar = value & ~0x1Ful;
+    return 0;
+}
+#endif
+
+static const ARMCPRegInfo tz_cp_reginfo[] = {
+#ifndef CONFIG_USER_ONLY
+    { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
+      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
+      .resetvalue = 0 },
+    { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
+      .access = PL1_RW, .writefn = vbar_write,
+      .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
+      .resetvalue = 0 },
+#endif
+    REGINFO_SENTINEL
+};
+
  void register_cp_regs_for_features(ARMCPU *cpu)
  {
      /* Register all the coprocessor registers based on feature bits */

target-arm/helper.c void register_cp_regs_for_features(ARMCPU *cpu) · line 1675

Quoted 5 lines
target-arm/helper.c +2 -0
@@ -1663,6 +1671,9 @@ void register_cp_regs_for_features(ARMCPU *cpu)
      if (arm_feature(env, ARM_FEATURE_LPAE)) {
          define_arm_cp_regs(cpu, lpae_cp_reginfo);
      }
+    if (arm_feature(env, ARM_FEATURE_TRUSTZONE)) {
+        define_arm_cp_regs(cpu, tz_cp_reginfo);

So ARM docmentation refers to these features as being conditional on the "security extensions" option, not "trustzone". To match documentation i think it may actually be ARM_FEATURE_SECURITY_EXTENSIONS (or some truntaction thereof for brevity). On what level of ARM documentation is the "trustzone" term defined?

Quoted 2 lines

Regards,
Peter

The "TrustZone" term is not mentioned in ARM architecture manual. That is a name for technology of system-wide approach to security. So strictly speaking there should be used term "Security Extensions". I cannot find any official truncation of this term.

Best regards,
Sergey Fedorov

Quoted 8 lines
Quoted 8 lines
target-arm/helper.c +1 -1
+    }
      /* Slightly awkwardly, the OMAP and StrongARM cores need all of
       * cp15 crn=0 to be writes-ignored, whereas for other cores they should
       * be read-only (ie write causes UNDEF exception).
--

1.7.9.5

#5 lore raw

RFC PATCH 3/21 target-arm: adjust TTBCR for TrustZone feature

In reply to Sergey Fedorov

TTBCR has additional fields PD0 and PD1 when using Short-descriptor translation table format on a CPU with TrustZone feature support.

---
 target-arm/helper.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
target-arm/helper.c +3 -1
index a247ca0..6642e53 100644
@@ -1159,8 +1159,10 @@ static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
 {
     int maskshift = extract32(value, 0, 3);
 
-    if (arm_feature(env, ARM_FEATURE_LPAE)) {
+    if (arm_feature(env, ARM_FEATURE_LPAE) && (value & (1 << 31))) {
         value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
+    } else if (arm_feature(env, ARM_FEATURE_TRUSTZONE)) {
+        value &= 0x37;
     } else {
         value &= 7;
     }
Signature · 1 line
1.7.9.5
#6 lore raw

Re: [Qemu-devel] [RFC PATCH 03/21] target-arm: adjust TTBCR for TrustZone feature

Quoted 13 lines · Sergey Fedorov

TTBCR has additional fields PD0 and PD1 when using Short-descriptor translation table format on a CPU with TrustZone feature support.

---
 target-arm/helper.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
target-arm/helper.c +0 -0
index a247ca0..6642e53 100644

target-arm/helper.c static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1162

Quoted 5 lines
target-arm/helper.c +1 -1
@@ -1159,8 +1159,10 @@ static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
 {
     int maskshift = extract32(value, 0, 3);
 
-    if (arm_feature(env, ARM_FEATURE_LPAE)) {
+    if (arm_feature(env, ARM_FEATURE_LPAE) && (value & (1 << 31))) {

This appears to be changing more than just trustzone dependent behavior. That is, if we take just this hunk and ignore the one below you see a change in the non-tz behaviour. Is the hunk legitimate irrespective of trustzone support?

target-arm/helper.c static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1168

Quoted 6 lines
target-arm/helper.c +2 -0
         value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
+    } else if (arm_feature(env, ARM_FEATURE_TRUSTZONE)) {
+        value &= 0x37;
     } else {
         value &= 7;
     }

There are a few magic numbers in the patch probably worth macrofiying.

Regards,
Peter

Quoted 4 lines

--
1.7.9.5

#7 lore raw

Re: [Qemu-devel] [RFC PATCH 03/21] target-arm: adjust TTBCR for TrustZone feature

Quoted 17 lines · Peter Crosthwaite
Quoted 16 lines · Sergey Fedorov

TTBCR has additional fields PD0 and PD1 when using Short-descriptor translation table format on a CPU with TrustZone feature support.

---
  target-arm/helper.c |    4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
target-arm/helper.c +0 -0
index a247ca0..6642e53 100644
@@ -1159,8 +1159,10 @@ static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
  {
      int maskshift = extract32(value, 0, 3);
 
Quoted 6 lines

target-arm/helper.c static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1162

Quoted 2 lines
target-arm/helper.c +1 -1
-    if (arm_feature(env, ARM_FEATURE_LPAE)) {
+    if (arm_feature(env, ARM_FEATURE_LPAE) && (value & (1 << 31))) {

This appears to be changing more than just trustzone dependent behavior. That is, if we take just this hunk and ignore the one below you see a change in the non-tz behaviour. Is the hunk legitimate irrespective of trustzone support?

Yes, current implementation is not accurate according to ARMv7-AR reference manual. See "B4.1.153 TTBCR, Translation Table Base Control Register, VMSA | TTBCR format when using the Long-descriptor translation table format". When LPAE feature is supported, EAE, bit[31] selects translation descriptor format and, therefore, TTBCR format.

Quoted 8 lines

target-arm/helper.c static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1168

Quoted 7 lines
target-arm/helper.c +2 -0
          value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
+    } else if (arm_feature(env, ARM_FEATURE_TRUSTZONE)) {
+        value &= 0x37;
      } else {
          value &= 7;
      }

There are a few magic numbers in the patch probably worth macrofiying.

As I can see, magic numbers are widely used through all of this file to represent CP register fields and other things. Maybe the macrofying should be done separately from this patch series?

Quoted 9 lines

Regards,
Peter

Quoted 4 lines

--
1.7.9.5

Best regards,
Sergey Fedorov

#8 lore raw

Re: [Qemu-devel] [RFC PATCH 03/21] target-arm: adjust TTBCR for TrustZone feature

Quoted 32 lines · Fedorov Sergey
Quoted 21 lines · Peter Crosthwaite
Quoted 18 lines · Sergey Fedorov

TTBCR has additional fields PD0 and PD1 when using Short-descriptor translation table format on a CPU with TrustZone feature support.

---
  target-arm/helper.c |    4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
target-arm/helper.c +0 -0
index a247ca0..6642e53 100644
@@ -1159,8 +1159,10 @@ static int vmsa_ttbcr_raw_write(CPUARMState *env,
 const ARMCPRegInfo *ri,
  {
      int maskshift = extract32(value, 0, 3);
 
Quoted 7 lines

target-arm/helper.c static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1162

Quoted 2 lines
target-arm/helper.c +1 -1
-    if (arm_feature(env, ARM_FEATURE_LPAE)) {
+    if (arm_feature(env, ARM_FEATURE_LPAE) && (value & (1 << 31))) {

This appears to be changing more than just trustzone dependent behavior. That is, if we take just this hunk and ignore the one below you see a change in the non-tz behaviour. Is the hunk legitimate irrespective of trustzone support?

Quoted 6 lines

Yes, current implementation is not accurate according to ARMv7-AR reference manual. See "B4.1.153 TTBCR, Translation Table Base Control Register, VMSA | TTBCR format when using the Long-descriptor translation table format". When LPAE feature is supported, EAE, bit[31] selects translation descriptor format and, therefore, TTBCR format.

Ok,

You should probably split it off as a separate patch. And you could send probably send it immediately. What you just wrote is a nice commit message.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
target-arm/helper.c +2 -0
          value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
+    } else if (arm_feature(env, ARM_FEATURE_TRUSTZONE)) {
+        value &= 0x37;
      } else {
          value &= 7;
      }
Quoted 7 lines
Quoted 1 line

There are a few magic numbers in the patch probably worth macrofiying.

As I can see, magic numbers are widely used through all of this file to represent CP register fields and other things. Maybe the macrofying should be done separately from this patch series?

So target-arm is riddled with legacy style. Converting all of target-arm to self-doccing macros is a big job, but if we keep expanding without doing it that job only gets bigger. I'll defer to PMM on what we want to policy to be going forward. - any thoughts?

Regards,
Peter

Quoted 13 lines
Quoted 9 lines

Regards,
Peter

Quoted 4 lines

--
1.7.9.5

Best regards,
Sergey Fedorov

#9 lore raw

Re: [Qemu-devel] [RFC PATCH 03/21] target-arm: adjust TTBCR for TrustZone feature

Quoted 40 lines · Peter Crosthwaite
Quoted 32 lines · Fedorov Sergey
Quoted 21 lines · Peter Crosthwaite
Quoted 18 lines · Sergey Fedorov

TTBCR has additional fields PD0 and PD1 when using Short-descriptor translation table format on a CPU with TrustZone feature support.

---
  target-arm/helper.c |    4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
target-arm/helper.c +0 -0
index a247ca0..6642e53 100644
@@ -1159,8 +1159,10 @@ static int vmsa_ttbcr_raw_write(CPUARMState *env,
 const ARMCPRegInfo *ri,
  {
      int maskshift = extract32(value, 0, 3);
 
Quoted 7 lines

target-arm/helper.c static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1162

Quoted 2 lines
target-arm/helper.c +1 -1
-    if (arm_feature(env, ARM_FEATURE_LPAE)) {
+    if (arm_feature(env, ARM_FEATURE_LPAE) && (value & (1 << 31))) {

This appears to be changing more than just trustzone dependent behavior. That is, if we take just this hunk and ignore the one below you see a change in the non-tz behaviour. Is the hunk legitimate irrespective of trustzone support?

Quoted 6 lines

Yes, current implementation is not accurate according to ARMv7-AR reference manual. See "B4.1.153 TTBCR, Translation Table Base Control Register, VMSA | TTBCR format when using the Long-descriptor translation table format". When LPAE feature is supported, EAE, bit[31] selects translation descriptor format and, therefore, TTBCR format.

Quoted 11 lines

Ok,

You should probably split it off as a separate patch. And you could send probably send it immediately. What you just wrote is a nice commit message.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
target-arm/helper.c +2 -0
          value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
+    } else if (arm_feature(env, ARM_FEATURE_TRUSTZONE)) {
+        value &= 0x37;

So if we let the magic numbers slide, can we at least add a comment about what fields these are?

/* PD0, PD1, N */

You would also be a little more self documenting with:

1 << 5 | 1 << 4 | 0x7

Regards,
Peter

Quoted 33 lines
Quoted 11 lines
Quoted 5 lines
Quoted 3 lines
target-arm/helper.c +0 -0
      } else {
          value &= 7;
      }

There are a few magic numbers in the patch probably worth macrofiying.

As I can see, magic numbers are widely used through all of this file to represent CP register fields and other things. Maybe the macrofying should be done separately from this patch series?

So target-arm is riddled with legacy style. Converting all of target-arm to self-doccing macros is a big job, but if we keep expanding without doing it that job only gets bigger. I'll defer to PMM on what we want to policy to be going forward. - any thoughts?

Regards,
Peter

Quoted 13 lines
Quoted 9 lines

Regards,
Peter

Quoted 4 lines

--
1.7.9.5

Best regards,
Sergey Fedorov

#10 lore raw

RFC PATCH 4/21 target-arm: preserve RAO/WI bits of ARMv7 SCTLR

In reply to Sergey Fedorov

From: Svetlana Fedoseeva <[email protected]>

---
 target-arm/helper.c |    4 ++++
 1 file changed, 4 insertions(+)
target-arm/helper.c +4 -0
index 6642e53..d7922ad 100644
@@ -1507,6 +1507,10 @@ static const ARMCPRegInfo lpae_cp_reginfo[] = {
 
 static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 {
+    if (arm_feature(env, ARM_FEATURE_V7)) {
+        value = value | 0x00c50078; /* This bits are RAO/WI */
+    }
+
     env->cp15.c1_sys = value;
     /* ??? Lots of these bits are not implemented.  */
     /* This may enable/disable the MMU, so do a TLB flush.  */
Signature · 1 line
1.7.9.5
#11 lore raw

Re: [Qemu-devel] [RFC PATCH 04/21] target-arm: preserve RAO/WI bits of ARMv7 SCTLR

Quoted 13 lines · Sergey Fedorov

From: Svetlana Fedoseeva <[email protected]>

---
 target-arm/helper.c |    4 ++++
 1 file changed, 4 insertions(+)
target-arm/helper.c +0 -0
index 6642e53..d7922ad 100644

target-arm/helper.c static const ARMCPRegInfo lpae_cp_reginfo[] = { · line 1511

Quoted 5 lines
target-arm/helper.c +2 -0
@@ -1507,6 +1507,10 @@ static const ARMCPRegInfo lpae_cp_reginfo[] = {
 
 static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 {
+    if (arm_feature(env, ARM_FEATURE_V7)) {
+        value = value | 0x00c50078; /* This bits are RAO/WI */

Magic number. "these bits ".

Quoted 9 lines
target-arm/helper.c +2 -1
+    }
+
     env->cp15.c1_sys = value;
     /* ??? Lots of these bits are not implemented.  */
     /* This may enable/disable the MMU, so do a TLB flush.  */
--

1.7.9.5

#12 lore raw

Re: [Qemu-devel] [RFC PATCH 04/21] target-arm: preserve RAO/WI bits of ARMv7 SCTLR

Quoted 15 lines · Peter Crosthwaite
Quoted 14 lines · Sergey Fedorov

From: Svetlana Fedoseeva <[email protected]>

---
  target-arm/helper.c |    4 ++++
  1 file changed, 4 insertions(+)
target-arm/helper.c +0 -0
index 6642e53..d7922ad 100644
@@ -1507,6 +1507,10 @@ static const ARMCPRegInfo lpae_cp_reginfo[] = {
 
Quoted 5 lines

target-arm/helper.c static const ARMCPRegInfo lpae_cp_reginfo[] = { · line 1511

Quoted 4 lines
target-arm/helper.c +2 -0
  static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
  {
+    if (arm_feature(env, ARM_FEATURE_V7)) {
+        value = value | 0x00c50078; /* This bits are RAO/WI */

Magic number. "these bits ".

Would be acceptable to substitute this magic number with "bitshifted constants combined with bitwise or", e.g. as in vmsa_ttbcr_raw_write()?

Quoted 11 lines
Quoted 10 lines
target-arm/helper.c +2 -1
+    }
+
      env->cp15.c1_sys = value;
      /* ??? Lots of these bits are not implemented.  */
      /* This may enable/disable the MMU, so do a TLB flush.  */
--

1.7.9.5

Signature · 4 lines
Best regards,
Sergey Fedorov, Junior Software Engineer,
Samsung R&D Institute Rus.
E-mail: [email protected]
#13 lore raw

Re: [Qemu-devel] [RFC PATCH 04/21] target-arm: preserve RAO/WI bits of ARMv7 SCTLR

Quoted 29 lines · Fedorov Sergey
Quoted 18 lines · Peter Crosthwaite
Quoted 15 lines · Sergey Fedorov

From: Svetlana Fedoseeva <[email protected]>

---
  target-arm/helper.c |    4 ++++
  1 file changed, 4 insertions(+)
target-arm/helper.c +0 -0
index 6642e53..d7922ad 100644
@@ -1507,6 +1507,10 @@ static const ARMCPRegInfo lpae_cp_reginfo[] = {
 
Quoted 7 lines

target-arm/helper.c static const ARMCPRegInfo lpae_cp_reginfo[] = { · line 1511

Quoted 5 lines
target-arm/helper.c +2 -0
  static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 uint64_t value)
  {
+    if (arm_feature(env, ARM_FEATURE_V7)) {
+        value = value | 0x00c50078; /* This bits are RAO/WI */

Magic number. "these bits ".

Quoted 3 lines

Would be acceptable to substitute this magic number with "bitshifted constants combined with bitwise or", e.g. as in vmsa_ttbcr_raw_write()?

Yes I think so, that will make life easier for the big macro conversion (one day) :)

Regards,
Peter

Quoted 20 lines
Quoted 12 lines
Quoted 11 lines
target-arm/helper.c +2 -0
+    }
+
      env->cp15.c1_sys = value;
      /* ??? Lots of these bits are not implemented.  */
      /* This may enable/disable the MMU, so do a TLB flush.  */

--
1.7.9.5

--
Best regards,
Sergey Fedorov, Junior Software Engineer,
Samsung R&D Institute Rus.
E-mail: [email protected]

#14 lore raw

RFC PATCH 5/21 target-arm: add CPU Monitor mode

In reply to Sergey Fedorov

From: Svetlana Fedoseeva <[email protected]>

Define CPU monitor mode. Adjust core registers banking. Adjust CPU VM state info. Provide CPU mode name for monitor mode.

---
 target-arm/cpu.h       |    7 ++++---
 target-arm/helper.c    |    3 +++
 target-arm/machine.c   |   12 ++++++------
 target-arm/translate.c |    2 +-
 4 files changed, 14 insertions(+), 10 deletions(-)
target-arm/cpu.h +4 -3
index 0b93e39..94d8bd1 100644
@@ -124,9 +124,9 @@ typedef struct CPUARMState {
     uint32_t spsr;
 
     /* Banked registers.  */
-    uint32_t banked_spsr[6];
-    uint32_t banked_r13[6];
-    uint32_t banked_r14[6];
+    uint32_t banked_spsr[7];
+    uint32_t banked_r13[7];
+    uint32_t banked_r14[7];
 
     /* These hold r8-r12.  */
     uint32_t usr_regs[5];
@@ -402,6 +402,7 @@ enum arm_cpu_mode {
   ARM_CPU_MODE_FIQ = 0x11,
   ARM_CPU_MODE_IRQ = 0x12,
   ARM_CPU_MODE_SVC = 0x13,
+  ARM_CPU_MODE_MON = 0x16,
   ARM_CPU_MODE_ABT = 0x17,
   ARM_CPU_MODE_UND = 0x1b,
   ARM_CPU_MODE_SYS = 0x1f
target-arm/helper.c +3 -0
index d7922ad..d4407cf 100644
@@ -2018,6 +2018,7 @@ static int bad_mode_switch(CPUARMState *env, int mode)
     case ARM_CPU_MODE_USR:
     case ARM_CPU_MODE_SYS:
     case ARM_CPU_MODE_SVC:
+    case ARM_CPU_MODE_MON:
     case ARM_CPU_MODE_ABT:
     case ARM_CPU_MODE_UND:
     case ARM_CPU_MODE_IRQ:
@@ -2202,6 +2203,8 @@ int bank_number(int mode)
         return 4;
     case ARM_CPU_MODE_FIQ:
         return 5;
+    case ARM_CPU_MODE_MON:
+        return 6;
     }
     hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
 }
target-arm/machine.c +6 -6
index 74f010f..51d0c79 100644
@@ -222,9 +222,9 @@ static int cpu_post_load(void *opaque, int version_id)
 
 const VMStateDescription vmstate_arm_cpu = {
     .name = "cpu",
-    .version_id = 13,
-    .minimum_version_id = 13,
-    .minimum_version_id_old = 13,
+    .version_id = 14,
+    .minimum_version_id = 14,
+    .minimum_version_id_old = 14,
     .pre_save = cpu_pre_save,
     .post_load = cpu_post_load,
     .fields = (VMStateField[]) {
@@ -238,9 +238,9 @@ const VMStateDescription vmstate_arm_cpu = {
             .offset = 0,
         },
         VMSTATE_UINT32(env.spsr, ARMCPU),
-        VMSTATE_UINT32_ARRAY(env.banked_spsr, ARMCPU, 6),
-        VMSTATE_UINT32_ARRAY(env.banked_r13, ARMCPU, 6),
-        VMSTATE_UINT32_ARRAY(env.banked_r14, ARMCPU, 6),
+        VMSTATE_UINT32_ARRAY(env.banked_spsr, ARMCPU, 7),
+        VMSTATE_UINT32_ARRAY(env.banked_r13, ARMCPU, 7),
+        VMSTATE_UINT32_ARRAY(env.banked_r14, ARMCPU, 7),
         VMSTATE_UINT32_ARRAY(env.usr_regs, ARMCPU, 5),
         VMSTATE_UINT32_ARRAY(env.fiq_regs, ARMCPU, 5),
         /* The length-check must come before the arrays to avoid
target-arm/translate.c +1 -1
index 5f003e7..665c8ac 100644
@@ -10295,7 +10295,7 @@ void gen_intermediate_code_pc(CPUARMState *env, TranslationBlock *tb)
 }
 
 static const char *cpu_mode_names[16] = {
-  "usr", "fiq", "irq", "svc", "???", "???", "???", "abt",
+  "usr", "fiq", "irq", "svc", "???", "???", "mon", "abt",
   "???", "???", "???", "und", "???", "???", "???", "sys"
 };
 
Signature · 1 line
1.7.9.5
#15 lore raw

Re: [Qemu-devel] [RFC PATCH 05/21] target-arm: add CPU Monitor mode

Quoted 19 lines · Sergey Fedorov

From: Svetlana Fedoseeva <[email protected]>

Define CPU monitor mode. Adjust core registers banking. Adjust CPU VM state info. Provide CPU mode name for monitor mode.

---
 target-arm/cpu.h       |    7 ++++---
 target-arm/helper.c    |    3 +++
 target-arm/machine.c   |   12 ++++++------
 target-arm/translate.c |    2 +-
 4 files changed, 14 insertions(+), 10 deletions(-)
target-arm/cpu.h +0 -0
index 0b93e39..94d8bd1 100644

target-arm/cpu.h typedef struct CPUARMState { · line 130

Quoted 10 lines
target-arm/cpu.h +3 -3
@@ -124,9 +124,9 @@ typedef struct CPUARMState {
     uint32_t spsr;
 
     /* Banked registers.  */
-    uint32_t banked_spsr[6];
-    uint32_t banked_r13[6];
-    uint32_t banked_r14[6];
+    uint32_t banked_spsr[7];
+    uint32_t banked_r13[7];
+    uint32_t banked_r14[7];
 

Are there any more modes yet to be implemented? It might save on future VMSD version bumps if we just pad this out to its ultimate value now.

Quoted 78 lines
target-arm/cpu.h +1 -0
     /* These hold r8-r12.  */
     uint32_t usr_regs[5];
@@ -402,6 +402,7 @@ enum arm_cpu_mode {
   ARM_CPU_MODE_FIQ = 0x11,
   ARM_CPU_MODE_IRQ = 0x12,
   ARM_CPU_MODE_SVC = 0x13,
+  ARM_CPU_MODE_MON = 0x16,
   ARM_CPU_MODE_ABT = 0x17,
   ARM_CPU_MODE_UND = 0x1b,
   ARM_CPU_MODE_SYS = 0x1f
target-arm/helper.c +3 -0
index d7922ad..d4407cf 100644
@@ -2018,6 +2018,7 @@ static int bad_mode_switch(CPUARMState *env, int mode)
     case ARM_CPU_MODE_USR:
     case ARM_CPU_MODE_SYS:
     case ARM_CPU_MODE_SVC:
+    case ARM_CPU_MODE_MON:
     case ARM_CPU_MODE_ABT:
     case ARM_CPU_MODE_UND:
     case ARM_CPU_MODE_IRQ:
@@ -2202,6 +2203,8 @@ int bank_number(int mode)
         return 4;
     case ARM_CPU_MODE_FIQ:
         return 5;
+    case ARM_CPU_MODE_MON:
+        return 6;
     }
     hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
 }
target-arm/machine.c +6 -6
index 74f010f..51d0c79 100644
@@ -222,9 +222,9 @@ static int cpu_post_load(void *opaque, int version_id)
 
 const VMStateDescription vmstate_arm_cpu = {
     .name = "cpu",
-    .version_id = 13,
-    .minimum_version_id = 13,
-    .minimum_version_id_old = 13,
+    .version_id = 14,
+    .minimum_version_id = 14,
+    .minimum_version_id_old = 14,
     .pre_save = cpu_pre_save,
     .post_load = cpu_post_load,
     .fields = (VMStateField[]) {
@@ -238,9 +238,9 @@ const VMStateDescription vmstate_arm_cpu = {
             .offset = 0,
         },
         VMSTATE_UINT32(env.spsr, ARMCPU),
-        VMSTATE_UINT32_ARRAY(env.banked_spsr, ARMCPU, 6),
-        VMSTATE_UINT32_ARRAY(env.banked_r13, ARMCPU, 6),
-        VMSTATE_UINT32_ARRAY(env.banked_r14, ARMCPU, 6),
+        VMSTATE_UINT32_ARRAY(env.banked_spsr, ARMCPU, 7),
+        VMSTATE_UINT32_ARRAY(env.banked_r13, ARMCPU, 7),
+        VMSTATE_UINT32_ARRAY(env.banked_r14, ARMCPU, 7),
         VMSTATE_UINT32_ARRAY(env.usr_regs, ARMCPU, 5),
         VMSTATE_UINT32_ARRAY(env.fiq_regs, ARMCPU, 5),
         /* The length-check must come before the arrays to avoid
target-arm/translate.c +1 -2
index 5f003e7..665c8ac 100644
@@ -10295,7 +10295,7 @@ void gen_intermediate_code_pc(CPUARMState *env, TranslationBlock *tb)
 }
 
 static const char *cpu_mode_names[16] = {
-  "usr", "fiq", "irq", "svc", "???", "???", "???", "abt",
+  "usr", "fiq", "irq", "svc", "???", "???", "mon", "abt",
   "???", "???", "???", "und", "???", "???", "???", "sys"
 };
 
--

1.7.9.5

#16 lore raw

Re: [Qemu-devel] [RFC PATCH 05/21] target-arm: add CPU Monitor mode

Quoted 22 lines · Peter Crosthwaite
Quoted 21 lines · Sergey Fedorov

From: Svetlana Fedoseeva <[email protected]>

Define CPU monitor mode. Adjust core registers banking. Adjust CPU VM state info. Provide CPU mode name for monitor mode.

---
 target-arm/cpu.h       |    7 ++++---
 target-arm/helper.c    |    3 +++
 target-arm/machine.c   |   12 ++++++------
 target-arm/translate.c |    2 +-
 4 files changed, 14 insertions(+), 10 deletions(-)
target-arm/cpu.h +0 -0
index 0b93e39..94d8bd1 100644
@@ -124,9 +124,9 @@ typedef struct CPUARMState {
     uint32_t spsr;
 
Quoted 12 lines

target-arm/cpu.h typedef struct CPUARMState { · line 130

Quoted 8 lines
target-arm/cpu.h +3 -3
     /* Banked registers.  */
-    uint32_t banked_spsr[6];
-    uint32_t banked_r13[6];
-    uint32_t banked_r14[6];
+    uint32_t banked_spsr[7];
+    uint32_t banked_r13[7];
+    uint32_t banked_r14[7];
 

Are there any more modes yet to be implemented? It might save on future VMSD version bumps if we just pad this out to its ultimate value now.

The remaining mode defined for AArch32 which we don't implement yet is Hyp mode, which has a banked R13 and SPSR, but not a banked LR.

-- PMM

#17 lore raw

Re: [Qemu-devel] [RFC PATCH 05/21] target-arm: add CPU Monitor mode

Quoted 35 lines · Peter Maydell
Quoted 22 lines · Peter Crosthwaite
Quoted 21 lines · Sergey Fedorov

From: Svetlana Fedoseeva <[email protected]>

Define CPU monitor mode. Adjust core registers banking. Adjust CPU VM state info. Provide CPU mode name for monitor mode.

---
  target-arm/cpu.h       |    7 ++++---
  target-arm/helper.c    |    3 +++
  target-arm/machine.c   |   12 ++++++------
  target-arm/translate.c |    2 +-
  4 files changed, 14 insertions(+), 10 deletions(-)
target-arm/cpu.h +0 -0
index 0b93e39..94d8bd1 100644
@@ -124,9 +124,9 @@ typedef struct CPUARMState {
      uint32_t spsr;
 
Quoted 11 lines

target-arm/cpu.h typedef struct CPUARMState { · line 130

Quoted 8 lines
target-arm/cpu.h +3 -3
      /* Banked registers.  */
-    uint32_t banked_spsr[6];
-    uint32_t banked_r13[6];
-    uint32_t banked_r14[6];
+    uint32_t banked_spsr[7];
+    uint32_t banked_r13[7];
+    uint32_t banked_r14[7];
 

Are there any more modes yet to be implemented? It might save on future VMSD version bumps if we just pad this out to its ultimate value now.

Quoted 7 lines

The remaining mode defined for AArch32 which we don't implement yet is Hyp mode, which has a banked R13 and SPSR, but not a banked LR.

-- PMM

So should a number of banked core registers be increased more? Personally, I'd like to keep this patch only TZ-related.

Best regards,
Sergey Fedorov

#18 lore raw

Re: [Qemu-devel] [RFC PATCH 05/21] target-arm: add CPU Monitor mode

Quoted 46 lines · Fedorov Sergey
Quoted 44 lines · Peter Maydell
Quoted 25 lines · Peter Crosthwaite
Quoted 22 lines · Sergey Fedorov

From: Svetlana Fedoseeva <[email protected]>

Define CPU monitor mode. Adjust core registers banking. Adjust CPU VM state info. Provide CPU mode name for monitor mode.

---
  target-arm/cpu.h       |    7 ++++---
  target-arm/helper.c    |    3 +++
  target-arm/machine.c   |   12 ++++++------
  target-arm/translate.c |    2 +-
  4 files changed, 14 insertions(+), 10 deletions(-)
target-arm/cpu.h +0 -0
index 0b93e39..94d8bd1 100644
@@ -124,9 +124,9 @@ typedef struct CPUARMState {
      uint32_t spsr;
 
Quoted 11 lines

target-arm/cpu.h typedef struct CPUARMState { · line 130

Quoted 8 lines
target-arm/cpu.h +3 -3
      /* Banked registers.  */
-    uint32_t banked_spsr[6];
-    uint32_t banked_r13[6];
-    uint32_t banked_r14[6];
+    uint32_t banked_spsr[7];
+    uint32_t banked_r13[7];
+    uint32_t banked_r14[7];
 

Are there any more modes yet to be implemented? It might save on future VMSD version bumps if we just pad this out to its ultimate value now.

The remaining mode defined for AArch32 which we don't implement yet is Hyp mode, which has a banked R13 and SPSR, but not a banked LR.

Quoted 7 lines
Quoted 3 lines

-- PMM

So should a number of banked core registers be increased more? Personally, I'd like to keep this patch only TZ-related.

So what im proposing is just a slightly more general patch. Is it really any more complicated than just applying your change pattern for the hyp mode? Patch subject would be something like:

"target-arm: Add all remaining missing modes"

The motiviation is less VMSD version bumps in ARM CPU (a place where I expect assume such version bumps to be considerable annoyance).

Regards,
Peter

Quoted 3 lines

Best regards,
Sergey Fedorov

#19 lore raw

Re: [Qemu-devel] [RFC PATCH 05/21] target-arm: add CPU Monitor mode

Quoted 3 lines · Peter Crosthwaite

So what im proposing is just a slightly more general patch. Is it really any more complicated than just applying your change pattern for the hyp mode?

I think it would be, because of the wrinkle that hyp mode doesn't have a banked LR, so the existing "assume we can just translate the mode into a single index good for both LR and SP" logic would break.

The minimal change if we wanted to keep VMSD bumps to a minimum would be to increase the size of the banked_spsr[] and banked_r13[] arrays to allow for Hyp mode but do nothing else (except add a comment about it I guess).

Quoted 2 lines

The motiviation is less VMSD version bumps in ARM CPU (a place where I expect assume such version bumps to be considerable annoyance).

Well, they're only a problem at the point where we start trying to support cross-version migration; we're not at that point yet...

thanks
-- PMM

#20 lore raw

Re: [Qemu-devel] [RFC PATCH 05/21] target-arm: add CPU Monitor mode

Quoted 10 lines · Peter Maydell
Quoted 3 lines · Peter Crosthwaite

So what im proposing is just a slightly more general patch. Is it really any more complicated than just applying your change pattern for the hyp mode?

I think it would be, because of the wrinkle that hyp mode doesn't have a banked LR, so the existing "assume we can just translate the mode into a single index good for both LR and SP" logic would break.

Quoted 4 lines

The minimal change if we wanted to keep VMSD bumps to a minimum would be to increase the size of the banked_spsr[] and banked_r13[] arrays to allow for Hyp mode but do nothing else (except add a comment about it I guess).

If we want to bump VMSD just once for monitor + hypervisor mode then we need to add ELR_hyp register definition too. I think then it would be better to implement hypervisor mode and its special banking scheme, too. But I doubt it worth to combine these two things into one patch.

Quoted 9 lines
Quoted 3 lines

The motiviation is less VMSD version bumps in ARM CPU (a place where I expect assume such version bumps to be considerable annoyance).

Well, they're only a problem at the point where we start trying to support cross-version migration; we're not at that point yet...

thanks
-- PMM

Best regards,
Sergey Fedorov

#21 lore raw

Re: [Qemu-devel] [RFC PATCH 05/21] target-arm: add CPU Monitor mode

Quoted 21 lines · Fedorov Sergey
Quoted 13 lines · Peter Maydell
Quoted 4 lines · Peter Crosthwaite

So what im proposing is just a slightly more general patch. Is it really any more complicated than just applying your change pattern for the hyp mode?

I think it would be, because of the wrinkle that hyp mode doesn't have a banked LR, so the existing "assume we can just translate the mode into a single index good for both LR and SP" logic would break.

Quoted 4 lines

The minimal change if we wanted to keep VMSD bumps to a minimum would be to increase the size of the banked_spsr[] and banked_r13[] arrays to allow for Hyp mode but do nothing else (except add a comment about it I guess).

Quoted 4 lines

If we want to bump VMSD just once for monitor + hypervisor mode then we need to add ELR_hyp register definition too. I think then it would be better to implement hypervisor mode and its special banking scheme, too. But I doubt it worth to combine these two things into one patch.

It's possible to add single new fields to the VMState without requiring a compatibility break, by marking the new field as "only present in version X or greater"; new elements on the end of arrays are a little fiddlier.

But yes, I think we should just not worry about possible future Hyp mode now. Let's stick with your current patch.

thanks
-- PMM

#22 lore raw

Re: [Qemu-devel] [RFC PATCH 05/21] target-arm: add CPU Monitor mode

Quoted 27 lines · Peter Maydell
Quoted 21 lines · Fedorov Sergey
Quoted 13 lines · Peter Maydell
Quoted 4 lines · Peter Crosthwaite

So what im proposing is just a slightly more general patch. Is it really any more complicated than just applying your change pattern for the hyp mode?

I think it would be, because of the wrinkle that hyp mode doesn't have a banked LR, so the existing "assume we can just translate the mode into a single index good for both LR and SP" logic would break.

Quoted 4 lines

The minimal change if we wanted to keep VMSD bumps to a minimum would be to increase the size of the banked_spsr[] and banked_r13[] arrays to allow for Hyp mode but do nothing else (except add a comment about it I guess).

Quoted 4 lines

If we want to bump VMSD just once for monitor + hypervisor mode then we need to add ELR_hyp register definition too. I think then it would be better to implement hypervisor mode and its special banking scheme, too. But I doubt it worth to combine these two things into one patch.

Quoted 8 lines

It's possible to add single new fields to the VMState without requiring a compatibility break, by marking the new field as "only present in version X or greater"; new elements on the end of arrays are a little fiddlier.

But yes, I think we should just not worry about possible future Hyp mode now. Let's stick with your current patch.

+1. Patch is good for the moment.

Quoted 3 lines

thanks
-- PMM

#23 lore raw

RFC PATCH 6/21 target-arm: add arm_is_secure() helper

In reply to Sergey Fedorov

arm_is_secure() helper allows to determine CPU security state.

---
 target-arm/cpu.h |   11 +++++++++++
 1 file changed, 11 insertions(+)
target-arm/cpu.h +11 -0
index 94d8bd1..a00c86f 100644
@@ -474,6 +474,17 @@ static inline int arm_feature(CPUARMState *env, int feature)
     return (env->features & (1ULL << feature)) != 0;
 }
 
+/* Return non-zero if the processor is in Secure state */
+static inline bool arm_is_secure(CPUARMState *env)
+{
+#if !defined(CONFIG_USER_ONLY)
+    return ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) ||
+            !(env->cp15.c1_scr & 1);
+#else
+    return false;
+#endif
+}
+
 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf);
 
 /* Interface between CPU and Interrupt controller.  */
Signature · 1 line
1.7.9.5
#24 lore raw

Re: [Qemu-devel] [RFC PATCH 06/21] target-arm: add arm_is_secure() helper

Quoted 2 lines · Sergey Fedorov

arm_is_secure() helper allows to determine CPU security state.

Helper in the target-foo context usually refers to a TCG->C code helper fn, whereas you are using in a general sense. Just s/helper/function.

Quoted 10 lines
---
 target-arm/cpu.h |   11 +++++++++++
 1 file changed, 11 insertions(+)
target-arm/cpu.h +0 -0
index 94d8bd1..a00c86f 100644

target-arm/cpu.h static inline int arm_feature(CPUARMState *env, int feature) · line 477

Quoted 4 lines
target-arm/cpu.h +1 -0
@@ -474,6 +474,17 @@ static inline int arm_feature(CPUARMState *env, int feature)
     return (env->features & (1ULL << feature)) != 0;
 }
 
+/* Return non-zero if the processor is in Secure state */

"Return true"

Regards,
Peter

Quoted 17 lines
target-arm/cpu.h +10 -1
+static inline bool arm_is_secure(CPUARMState *env)
+{
+#if !defined(CONFIG_USER_ONLY)
+    return ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) ||
+            !(env->cp15.c1_scr & 1);
+#else
+    return false;
+#endif
+}
+
 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf);
 
 /* Interface between CPU and Interrupt controller.  */
--

1.7.9.5

#25 lore raw

RFC PATCH 7/21 target-arm: reject switching to monitor mode from non-secure state

In reply to Sergey Fedorov
---
 target-arm/helper.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target-arm/helper.c +2 -1
index d4407cf..e406ec9 100644
@@ -2018,12 +2018,13 @@ static int bad_mode_switch(CPUARMState *env, int mode)
     case ARM_CPU_MODE_USR:
     case ARM_CPU_MODE_SYS:
     case ARM_CPU_MODE_SVC:
-    case ARM_CPU_MODE_MON:
     case ARM_CPU_MODE_ABT:
     case ARM_CPU_MODE_UND:
     case ARM_CPU_MODE_IRQ:
     case ARM_CPU_MODE_FIQ:
         return 0;
+    case ARM_CPU_MODE_MON:
+        return !arm_is_secure(env);
     default:
         return 1;
     }
Signature · 1 line
1.7.9.5
#26 lore raw

Re: [Qemu-devel] [RFC PATCH 07/21] target-arm: reject switching to monitor mode from non-secure state

Quoted 11 lines · Sergey Fedorov
---
 target-arm/helper.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target-arm/helper.c +0 -0
index d4407cf..e406ec9 100644

target-arm/helper.c static int bad_mode_switch(CPUARMState *env, int mode) · line 2021

Quoted 4 lines
target-arm/helper.c +0 -1
@@ -2018,12 +2018,13 @@ static int bad_mode_switch(CPUARMState *env, int mode)
     case ARM_CPU_MODE_USR:
     case ARM_CPU_MODE_SYS:
     case ARM_CPU_MODE_SVC:
-    case ARM_CPU_MODE_MON:

You added this in the last patch to delete it now. I dont think you need it in prev patch for bisectability as your TZ functionality isn't online at this stage of the series, I would just revert both the addition and subtraction of this line in prev and this patches respectively.

Regards,
Peter

Quoted 14 lines
target-arm/helper.c +2 -1
     case ARM_CPU_MODE_ABT:
     case ARM_CPU_MODE_UND:
     case ARM_CPU_MODE_IRQ:
     case ARM_CPU_MODE_FIQ:
         return 0;
+    case ARM_CPU_MODE_MON:
+        return !arm_is_secure(env);
     default:
         return 1;
     }
--

1.7.9.5

#27 lore raw

RFC PATCH 8/21 target-arm: adjust arm_current_pl() for TrustZone

In reply to Sergey Fedorov

Make arm_current_pl() to return PL3 in secure privileged mode.

---
 target-arm/cpu.h |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target-arm/cpu.h +4 -2
index a00c86f..1b03450 100644
@@ -622,9 +622,11 @@ static inline int arm_current_pl(CPUARMState *env)
 {
     if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {
         return 0;
+    } else if (arm_is_secure(env)) {
+        return 3;
     }
-    /* We don't currently implement the Virtualization or TrustZone
-     * extensions, so PL2 and PL3 don't exist for us.
+    /* We don't currently implement the Virtualization extensions, so PL2 don't
+     * exist for us.
      */
     return 1;
 }
Signature · 1 line
1.7.9.5
#28 lore raw

Re: [Qemu-devel] [RFC PATCH 08/21] target-arm: adjust arm_current_pl() for TrustZone

Quoted 12 lines · Sergey Fedorov

Make arm_current_pl() to return PL3 in secure privileged mode.

---
 target-arm/cpu.h |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target-arm/cpu.h +0 -0
index a00c86f..1b03450 100644

target-arm/cpu.h static inline int arm_current_pl(CPUARMState *env) · line 628

Quoted 9 lines
target-arm/cpu.h +3 -2
@@ -622,9 +622,11 @@ static inline int arm_current_pl(CPUARMState *env)
 {
     if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {
         return 0;
+    } else if (arm_is_secure(env)) {
+        return 3;
     }
-    /* We don't currently implement the Virtualization or TrustZone
-     * extensions, so PL2 and PL3 don't exist for us.
+    /* We don't currently implement the Virtualization extensions, so PL2 don't

"doesn't". (second "don't" not first).

Quoted 8 lines
target-arm/cpu.h +1 -1
+     * exist for us.
      */
     return 1;
 }
--

1.7.9.5

#29 lore raw

RFC PATCH 9/21 target-arm: adjust SCR CP15 register access rights

In reply to Sergey Fedorov

SCR register is accessible in PL3 (secure privileged) mode only. Fix SRC access rights since arm_current_pl() can return PL3 now.

---
 target-arm/helper.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target-arm/helper.c +1 -1
index e406ec9..3bd0a64 100644
@@ -1530,7 +1530,7 @@ static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
 static const ARMCPRegInfo tz_cp_reginfo[] = {
 #ifndef CONFIG_USER_ONLY
     { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
-      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
+      .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
       .resetvalue = 0 },
     { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
       .access = PL1_RW, .writefn = vbar_write,
Signature · 1 line
1.7.9.5
#30 lore raw

RFC PATCH 10/21 target-arm: add non-secure Translation Block flag

In reply to Sergey Fedorov

This patch is based on idea found in patch at
git://github.com/jowinter/qemu-trustzone.git
f3d955c6c0ed8c46bc0eb10b634201032a651dd2 by
Johannes Winter <[email protected]>.

This flag prevents from executing TCG code generated for other CPU secure state. It also allows to generate different TCG code depending on CPU secure state.

---
 target-arm/cpu.h       |    7 +++++++
 target-arm/translate.c |    3 +++
 target-arm/translate.h |    1 +
 3 files changed, 11 insertions(+)
target-arm/cpu.h +7 -0
index 1b03450..f47dbdf 100644
@@ -854,6 +854,8 @@ static inline int cpu_mmu_index (CPUARMState *env)
 #define ARM_TBFLAG_CONDEXEC_MASK    (0xff << ARM_TBFLAG_CONDEXEC_SHIFT)
 #define ARM_TBFLAG_BSWAP_CODE_SHIFT 16
 #define ARM_TBFLAG_BSWAP_CODE_MASK  (1 << ARM_TBFLAG_BSWAP_CODE_SHIFT)
+#define ARM_TBFLAG_NS_SHIFT         17
+#define ARM_TBFLAG_NS_MASK          (1 << ARM_TBFLAG_NS_SHIFT)
 
 /* Bit usage when in AArch64 state: currently no bits defined */
 
@@ -874,6 +876,8 @@ static inline int cpu_mmu_index (CPUARMState *env)
     (((F) & ARM_TBFLAG_CONDEXEC_MASK) >> ARM_TBFLAG_CONDEXEC_SHIFT)
 #define ARM_TBFLAG_BSWAP_CODE(F) \
     (((F) & ARM_TBFLAG_BSWAP_CODE_MASK) >> ARM_TBFLAG_BSWAP_CODE_SHIFT)
+#define ARM_TBFLAG_NS(F) \
+    (((F) & ARM_TBFLAG_NS_MASK) >> ARM_TBFLAG_NS_SHIFT)
 
 static inline void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
                                         target_ulong *cs_base, int *flags)
@@ -897,6 +901,9 @@ static inline void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
         if (privmode) {
             *flags |= ARM_TBFLAG_PRIV_MASK;
         }
+        if (!arm_is_secure(env)) {
+            *flags |= ARM_TBFLAG_NS_MASK;
+        }
         if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
             *flags |= ARM_TBFLAG_VFPEN_MASK;
         }
target-arm/translate.c +3 -0
index 665c8ac..a47fcdb 100644
@@ -52,8 +52,10 @@ static uint32_t gen_opc_condexec_bits[OPC_BUF_SIZE];
 
 #if defined(CONFIG_USER_ONLY)
 #define IS_USER(s) 1
+#define IS_NS(s) 1
 #else
 #define IS_USER(s) (s->user)
+#define IS_NS(s) (s->ns)
 #endif
 
 /* These instructions trap after executing, so defer them until after the
@@ -10036,6 +10038,7 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu,
         dc->condexec_cond = ARM_TBFLAG_CONDEXEC(tb->flags) >> 4;
 #if !defined(CONFIG_USER_ONLY)
         dc->user = (ARM_TBFLAG_PRIV(tb->flags) == 0);
+        dc->ns = ARM_TBFLAG_NS(tb->flags);
 #endif
         dc->vfp_enabled = ARM_TBFLAG_VFPEN(tb->flags);
         dc->vec_len = ARM_TBFLAG_VECLEN(tb->flags);
target-arm/translate.h +1 -0
index 67c7760..05b4f34 100644
@@ -19,6 +19,7 @@ typedef struct DisasContext {
     int bswap_code;
 #if !defined(CONFIG_USER_ONLY)
     int user;
+    int ns;
 #endif
     int vfp_enabled;
     int vec_len;
Signature · 1 line
1.7.9.5
#31 lore raw

RFC PATCH 11/21 target-arm: implement CPACR register logic

In reply to Sergey Fedorov

CPACR register allows to control access rights to coprocessor 0-13 interfaces. Bits corresponding to unimplemented coprocessors should be RAZ/WI. QEMU implement only VFP coprocessor on ARMv6+ targets. So only cp10 & cp11 bits is writable.

---
 target-arm/helper.c    |    6 ++++++
 target-arm/translate.c |   36 +++++++++++++++++++++++++-----------
 2 files changed, 31 insertions(+), 11 deletions(-)
target-arm/helper.c +6 -0
index 3bd0a64..19f5830 100644
@@ -418,6 +418,12 @@ static const ARMCPRegInfo not_v7_cp_reginfo[] = {
 
 static int cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 {
+    uint32_t mask = 0;
+
+    if (arm_feature(env, ARM_FEATURE_VFP)) {
+        mask |= 0x00f00000; /* VFP coprocessor: cp10 & cp11 */
+    }
+    value &= mask;
     if (env->cp15.c1_coproc != value) {
         env->cp15.c1_coproc = value;
         /* ??? Is this safe when called from within a TB?  */
target-arm/translate.c +25 -11
index a47fcdb..78612c8 100644
@@ -6284,20 +6284,34 @@ static int disas_coproc_insn(CPUARMState * env, DisasContext *s, uint32_t insn)
     ARMCPU *cpu = arm_env_get_cpu(env);
 
     cpnum = (insn >> 8) & 0xf;
-    if (arm_feature(env, ARM_FEATURE_XSCALE)
-	    && ((env->cp15.c15_cpar ^ 0x3fff) & (1 << cpnum)))
-	return 1;
+    if (cpnum < 14) {
+        if (arm_feature(env, ARM_FEATURE_XSCALE)) {
+            if (~env->cp15.c15_cpar & (1 << cpnum)) {
+                return 1;
+            }
+        } else {
+            switch ((env->cp15.c1_coproc >> (cpnum * 2)) & 3) {
+            case 0:
+            case 2:
+                return 1;
+            case 1:
+                if (IS_USER(s)) {
+                    return 1;
+                }
+            }
+        }
+    }
 
     /* First check for coprocessor space used for actual instructions */
     switch (cpnum) {
-      case 0:
-      case 1:
-	if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
-	    return disas_iwmmxt_insn(env, s, insn);
-	} else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
-	    return disas_dsp_insn(env, s, insn);
-	}
-	return 1;
+    case 0:
+    case 1:
+        if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
+            return disas_iwmmxt_insn(env, s, insn);
+        } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
+            return disas_dsp_insn(env, s, insn);
+        }
+        return 1;
     case 10:
     case 11:
 	return disas_vfp_insn (env, s, insn);
Signature · 1 line
1.7.9.5
#32 lore raw

RFC PATCH 12/21 target-arm: add NSACR support

In reply to Sergey Fedorov

NSACR allows to control non-secure access to coprocessor interfaces 0-13 and CPACR bits.

---
 target-arm/cpu.h       |    1 +
 target-arm/helper.c    |   21 +++++++++++++++++++++
 target-arm/translate.c |    3 +++
 3 files changed, 25 insertions(+)
target-arm/cpu.h +1 -0
index f47dbdf..5aeb630 100644
@@ -150,6 +150,7 @@ typedef struct CPUARMState {
         uint32_t c1_coproc; /* Coprocessor access register.  */
         uint32_t c1_xscaleauxcr; /* XScale auxiliary control register.  */
         uint32_t c1_scr; /* secure config register.  */
+        uint32_t c1_nsacr; /* Non-secure access control register. */
         uint32_t c2_base0; /* MMU translation table base 0.  */
         uint32_t c2_base0_hi; /* MMU translation table base 0, high 32 bits */
         uint32_t c2_base1; /* MMU translation table base 0.  */
target-arm/helper.c +21 -0
index 19f5830..b6d8a3c 100644
@@ -424,6 +424,17 @@ static int cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
         mask |= 0x00f00000; /* VFP coprocessor: cp10 & cp11 */
     }
     value &= mask;
+    if (!arm_is_secure(env)) {
+        int cpnum;
+        mask = 0;
+        for (cpnum = 0; cpnum < 14; ++cpnum) {
+            if (env->cp15.c1_nsacr & (1 << cpnum)) {
+                mask |= (3 << (cpnum * 2));
+            }
+        }
+        value &= mask;
+        value |= env->cp15.c1_coproc & ~mask;
+    }
     if (env->cp15.c1_coproc != value) {
         env->cp15.c1_coproc = value;
         /* ??? Is this safe when called from within a TB?  */
@@ -1531,6 +1542,13 @@ static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
     env->cp15.c12_vbar = value & ~0x1Ful;
     return 0;
 }
+
+static int nsacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
+                      uint64_t value)
+{
+    env->cp15.c1_nsacr = value & 0x3fff;
+    return 0;
+}
 #endif
 
 static const ARMCPRegInfo tz_cp_reginfo[] = {
@@ -1542,6 +1560,9 @@ static const ARMCPRegInfo tz_cp_reginfo[] = {
       .access = PL1_RW, .writefn = vbar_write,
       .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
       .resetvalue = 0 },
+    { .name = "NSACR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 2,
+      .access = PL3_RW | PL2_R, .resetvalue = 0, .writefn = nsacr_write,
+      .fieldoffset = offsetof(CPUARMState, cp15.c1_nsacr) },
 #endif
     REGINFO_SENTINEL
 };
target-arm/translate.c +3 -0
index 78612c8..b3615fc 100644
@@ -6290,6 +6290,9 @@ static int disas_coproc_insn(CPUARMState * env, DisasContext *s, uint32_t insn)
                 return 1;
             }
         } else {
+            if (IS_NS(s) & ~env->cp15.c1_nsacr & (1 << cpnum)) {
+                return 1;
+            }
             switch ((env->cp15.c1_coproc >> (cpnum * 2)) & 3) {
             case 0:
             case 2:
Signature · 1 line
1.7.9.5
#33 lore raw

RFC PATCH 13/21 target-arm: add SDER definition

In reply to Sergey Fedorov
---
 target-arm/cpu.h    |    1 +
 target-arm/helper.c |    3 +++
 2 files changed, 4 insertions(+)
target-arm/cpu.h +1 -0
index 5aeb630..ffc1b21 100644
@@ -150,6 +150,7 @@ typedef struct CPUARMState {
         uint32_t c1_coproc; /* Coprocessor access register.  */
         uint32_t c1_xscaleauxcr; /* XScale auxiliary control register.  */
         uint32_t c1_scr; /* secure config register.  */
+        uint32_t c1_sder; /* Secure debug enable register. */
         uint32_t c1_nsacr; /* Non-secure access control register. */
         uint32_t c2_base0; /* MMU translation table base 0.  */
         uint32_t c2_base0_hi; /* MMU translation table base 0, high 32 bits */
target-arm/helper.c +3 -0
index b6d8a3c..780d0a0 100644
@@ -1560,6 +1560,9 @@ static const ARMCPRegInfo tz_cp_reginfo[] = {
       .access = PL1_RW, .writefn = vbar_write,
       .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
       .resetvalue = 0 },
+    { .name = "SDER", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 1,
+      .access = PL3_RW, .resetvalue = 0,
+      .fieldoffset = offsetof(CPUARMState, cp15.c1_sder) },
     { .name = "NSACR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 2,
       .access = PL3_RW | PL2_R, .resetvalue = 0, .writefn = nsacr_write,
       .fieldoffset = offsetof(CPUARMState, cp15.c1_nsacr) },
Signature · 1 line
1.7.9.5
#34 lore raw

RFC PATCH 14/21 target-arm: split TLB for secure state

In reply to Sergey Fedorov

This patch is based on idea found in patch at
git://github.com/jowinter/qemu-trustzone.git
a9ad01767c4b25e14700b5682a412f4fd8146ee8 by
Johannes Winter <[email protected]>.

Each secure state has its own MMU state. So provide a separate TLB for each secure state to avoid flushing the whole TLB on each secure state change. Do not use IS_USER() macro anymore as MMU index in translation code. Use new MEM_INDEX() and MEM_INDEX_USER() macros instead.

---
 target-arm/cpu.h       |   14 ++-
 target-arm/helper.c    |    2 +-
 target-arm/translate.c |  247 +++++++++++++++++++++++++-----------------------
 target-arm/translate.h |    1 +
 4 files changed, 140 insertions(+), 124 deletions(-)
target-arm/cpu.h +11 -3
index ffc1b21..a20f354 100644
@@ -75,7 +75,7 @@ typedef uint32_t ARMReadCPFunc(void *opaque, int cp_info,
 
 struct arm_boot_info;
 
-#define NB_MMU_MODES 2
+#define NB_MMU_MODES 4
 
 /* We currently assume float and double are IEEE single and double
    precision respectively.
@@ -827,10 +827,18 @@ static inline CPUARMState *cpu_init(const char *cpu_model)
 /* MMU modes definitions */
 #define MMU_MODE0_SUFFIX _kernel
 #define MMU_MODE1_SUFFIX _user
-#define MMU_USER_IDX 1
+#define MMU_MODE2_SUFFIX _ns_kernel
+#define MMU_MODE3_SUFFIX _ns_user
+#define MMU_USER_BIT 1
+#define MMU_NS_BIT 2
+#define MMU_KERN_IDX (0)
+#define MMU_USER_IDX (MMU_USER_BIT)
+#define MMU_NS_KERN_IDX (MMU_NS_BIT | MMU_KERN_IDX)
+#define MMU_NS_USER_IDX (MMU_NS_BIT | MMU_USER_IDX)
 static inline int cpu_mmu_index (CPUARMState *env)
 {
-    return (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR ? 1 : 0;
+    return (arm_is_secure(env) << 1) |
+        ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR);
 }
 
 #include "exec/cpu-all.h"
target-arm/helper.c +1 -1
index 780d0a0..c145cfe 100644
@@ -3141,7 +3141,7 @@ int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
     int prot;
     int ret, is_user;
 
-    is_user = mmu_idx == MMU_USER_IDX;
+    is_user = mmu_idx & MMU_USER_BIT;
     ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
                         &page_size);
     if (ret == 0) {
target-arm/translate.c +127 -120

745 lines not on this page · raw message

target-arm/translate.h +1 -0
index 05b4f34..be974e3 100644
@@ -20,6 +20,7 @@ typedef struct DisasContext {
 #if !defined(CONFIG_USER_ONLY)
     int user;
     int ns;
+    int mem_idx;
 #endif
     int vfp_enabled;
     int vec_len;
Signature · 1 line
1.7.9.5
#35 lore raw

RFC PATCH 15/21 target-arm: add banked coprocessor register type

In reply to Sergey Fedorov

Banked CP registers are defined with BANKED_CP_REG macro which defines an active register state field followed by an adjacent banked register state array field. An active CP register state is accessed as usual through an active state field. Banked CP register state is save in banked register state array. The array is indexed by NS bit value.

When translating a banked CP register access instruction in secure state, SCR.NS bit determines which field should be used. If SCR.NS bit is set then a non-secure banked value is used instead of an active one. That is possible only in monitor CPU mode. In non-secure state an active register state field is always used.

---
 target-arm/cpu.h       |   14 ++++++++++-
 target-arm/translate.c |   60 ++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 69 insertions(+), 5 deletions(-)
target-arm/cpu.h +13 -1
index a20f354..fe3a646 100644
@@ -73,6 +73,17 @@ typedef void ARMWriteCPFunc(void *opaque, int cp_info,
 typedef uint32_t ARMReadCPFunc(void *opaque, int cp_info,
                                int dstreg, int operand);
 
+/* Define a banked coprocessor register state field. Use %name as the active
+ * register state field name. The banked register state array field name has
+ * "banked_" prefix. The banked register state array indexes corresponds to
+ * SCR.NS bit value.
+ */
+#define BANKED_CP_REG(type, name) \
+    struct { \
+        type name; \
+        type banked_##name[2]; \
+    }
+
 struct arm_boot_info;
 
 #define NB_MMU_MODES 4
@@ -572,13 +583,14 @@ static inline uint64_t cpreg_to_kvm_id(uint32_t cpregid)
 #define ARM_CP_OVERRIDE 16
 #define ARM_CP_NO_MIGRATE 32
 #define ARM_CP_IO 64
+#define ARM_CP_BANKED 128
 #define ARM_CP_NOP (ARM_CP_SPECIAL | (1 << 8))
 #define ARM_CP_WFI (ARM_CP_SPECIAL | (2 << 8))
 #define ARM_LAST_SPECIAL ARM_CP_WFI
 /* Used only as a terminator for ARMCPRegInfo lists */
 #define ARM_CP_SENTINEL 0xffff
 /* Mask of only the flag bits in a type field */
-#define ARM_CP_FLAG_MASK 0x7f
+#define ARM_CP_FLAG_MASK 0xff
 
 /* Return true if cptype is a valid type field. This is used to try to
  * catch errors where the sentinel has been accidentally left off the end
target-arm/translate.c +56 -4

90 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#36 lore raw

RFC PATCH 16/21 target-arm: convert appropriate coprocessor registers to banked type

In reply to Sergey Fedorov

Define appropriate coprocessor registers with banked type. The register state fields are defined with BANKED_CP_REG() macro.

Banked coprocessor registers with a special behaviour is adjusted to correctly use its active or banked non-secure state.

---
 target-arm/cpu.c    |    2 +-
 target-arm/cpu.h    |   62 +++++++++++++++++---------
 target-arm/helper.c |  120 +++++++++++++++++++++++++++++----------------------
 3 files changed, 111 insertions(+), 73 deletions(-)
target-arm/cpu.c +1 -1
index 4607ca8..3bb3deb 100644
@@ -356,7 +356,7 @@ static void arm1026_initfn(Object *obj)
         /* The 1026 had an IFAR at c6,c0,0,1 rather than the ARMv6 c6,c0,0,2 */
         ARMCPRegInfo ifar = {
             .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
-            .access = PL1_RW,
+            .access = PL1_RW, .type = ARM_CP_BANKED,
             .fieldoffset = offsetof(CPUARMState, cp15.c6_insn),
             .resetvalue = 0
         };
target-arm/cpu.h +41 -21

90 lines not on this page · raw message

target-arm/helper.c +69 -51

315 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#37 lore raw

RFC PATCH 17/21 target-arm: use c13_context field for CONTEXTIDR

In reply to Sergey Fedorov

Use c13_context field instead of c13_fcse for CONTEXTIDR register definition.

---
 target-arm/helper.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target-arm/helper.c +1 -1
index 9442e08..e1e9762 100644
@@ -359,7 +359,7 @@ static const ARMCPRegInfo cp_reginfo[] = {
       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
     { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
       .access = PL1_RW, .type = ARM_CP_BANKED,
-      .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
+      .fieldoffset = offsetof(CPUARMState, cp15.c13_context),
       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
     /* ??? This covers not just the impdef TLB lockdown registers but also
      * some v7VMSA registers relating to TEX remap, so it is overly broad.
Signature · 1 line
1.7.9.5
#38 lore raw

Re: [Qemu-devel] [RFC PATCH 17/21] target-arm: use c13_context field for CONTEXTIDR

Quoted 2 lines · Sergey Fedorov

Use c13_context field instead of c13_fcse for CONTEXTIDR register definition.

This a standalone (I.E. not TZ related) bug?

Regards,
peter

Quoted 23 lines
---
 target-arm/helper.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target-arm/helper.c +1 -2
index 9442e08..e1e9762 100644
@@ -359,7 +359,7 @@ static const ARMCPRegInfo cp_reginfo[] = {
       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
     { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
       .access = PL1_RW, .type = ARM_CP_BANKED,
-      .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
+      .fieldoffset = offsetof(CPUARMState, cp15.c13_context),
       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
     /* ??? This covers not just the impdef TLB lockdown registers but also
      * some v7VMSA registers relating to TEX remap, so it is overly broad.
--

1.7.9.5

#39 lore raw

Re: [Qemu-devel] [RFC PATCH 17/21] target-arm: use c13_context field for CONTEXTIDR

Quoted 7 lines · Peter Crosthwaite
Quoted 2 lines · Sergey Fedorov

Use c13_context field instead of c13_fcse for CONTEXTIDR register definition.

This a standalone (I.E. not TZ related) bug?

Regards,
peter

Yes, I think so. Then I will submit this patch separately soon.

Best regards,
Sergey Fedorov

Quoted 23 lines
Quoted 23 lines
---
  target-arm/helper.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target-arm/helper.c +1 -2
index 9442e08..e1e9762 100644
@@ -359,7 +359,7 @@ static const ARMCPRegInfo cp_reginfo[] = {
        .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
      { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
        .access = PL1_RW, .type = ARM_CP_BANKED,
-      .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
+      .fieldoffset = offsetof(CPUARMState, cp15.c13_context),
        .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
      /* ??? This covers not just the impdef TLB lockdown registers but also
       * some v7VMSA registers relating to TEX remap, so it is overly broad.
--

1.7.9.5

Reviewed-by on v1 17/21
#40 lore raw

Re: [Qemu-devel] [RFC PATCH 17/21] target-arm: use c13_context field for CONTEXTIDR

In reply to Sergey Fedorov
Quoted 4 lines · Sergey Fedorov

Use c13_context field instead of c13_fcse for CONTEXTIDR register definition.

Add this to your commit msg on your planned single-send. Thanks for catching this.

Regards,
Peter

Quoted 21 lines
---
 target-arm/helper.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target-arm/helper.c +1 -2
index 9442e08..e1e9762 100644
@@ -359,7 +359,7 @@ static const ARMCPRegInfo cp_reginfo[] = {
       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
     { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
       .access = PL1_RW, .type = ARM_CP_BANKED,
-      .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
+      .fieldoffset = offsetof(CPUARMState, cp15.c13_context),
       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
     /* ??? This covers not just the impdef TLB lockdown registers but also
      * some v7VMSA registers relating to TEX remap, so it is overly broad.
--

1.7.9.5

#41 lore raw

RFC PATCH 18/21 target-arm: switch banked CP registers

In reply to Sergey Fedorov

Banked coprocessor registers are switched on two cases:
1) Entering or leaving CPU monitor mode with SCR.NS bit set;
2) Setting SCR.NS bit not from CPU monitor mode

Coprocessor register banking is done similar to CPU core register banking. Some of SCTRL bits are common for secure and non-secure state. Translation table base masks are updated on register switch instead of TTBCR write.

---
 target-arm/helper.c |   77 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 76 insertions(+), 1 deletion(-)
target-arm/helper.c +76 -1
index e1e9762..7bfadb0 100644
@@ -11,6 +11,7 @@ static inline int get_phys_addr(CPUARMState *env, uint32_t address,
                                 int access_type, int is_user,
                                 hwaddr *phys_ptr, int *prot,
                                 target_ulong *page_size);
+static void switch_cp15_regs(CPUARMState *env, int secure);
 #endif
 
 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
@@ -1553,6 +1554,17 @@ static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 }
 
 #ifndef CONFIG_USER_ONLY
+static int scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
+{
+    if ((value & 1/*NS*/) && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_MON) {
+        /* We are going to Non-secure state; switch banked system control registers */
+        switch_cp15_regs(env, 0);
+    }
+
+    env->cp15.c1_scr = value;
+    return 0;
+}
+
 static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
                       uint64_t value)
 {
@@ -1572,7 +1584,7 @@ static const ARMCPRegInfo tz_cp_reginfo[] = {
 #ifndef CONFIG_USER_ONLY
     { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
-      .resetvalue = 0 },
+      .writefn = scr_write, .resetvalue = 0 },
     { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
       .access = PL1_RW, .type = ARM_CP_BANKED, .writefn = vbar_write,
       .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
@@ -2284,6 +2296,69 @@ void switch_mode(CPUARMState *env, int mode)
     env->regs[13] = env->banked_r13[i];
     env->regs[14] = env->banked_r14[i];
     env->spsr = env->banked_spsr[i];
+
+    if ((mode == ARM_CPU_MODE_MON || old_mode == ARM_CPU_MODE_MON) &&
+            (env->cp15.c1_scr & 1/*NS*/)) {
+        /* We are going to change Security state; switch banked system control registers */
+        switch_cp15_regs(env, (mode == ARM_CPU_MODE_MON));
+    }
+}
+
+void switch_cp15_regs(CPUARMState *env, int secure)
+{
+    int i;
+
+    /* Save current Security state registers */
+    i = arm_is_secure(env) ? 0 : 1;
+    env->cp15.banked_c0_cssel[i] = env->cp15.c0_cssel;
+    env->cp15.banked_c1_sys[i] = env->cp15.c1_sys;
+    env->cp15.banked_c2_base0[i] = env->cp15.c2_base0;
+    env->cp15.banked_c2_base0_hi[i] = env->cp15.c2_base0_hi;
+    env->cp15.banked_c2_base1[i] = env->cp15.c2_base1;
+    env->cp15.banked_c2_base1_hi[i] = env->cp15.c2_base1_hi;
+    env->cp15.banked_c2_control[i] = env->cp15.c2_control;
+    env->cp15.banked_c3[i] = env->cp15.c3;
+    env->cp15.banked_c5_data[i] = env->cp15.c5_data;
+    env->cp15.banked_c5_insn[i] = env->cp15.c5_insn;
+    env->cp15.banked_c6_data[i] = env->cp15.c6_data;
+    env->cp15.banked_c6_insn[i] = env->cp15.c6_insn;
+    env->cp15.banked_c7_par[i] = env->cp15.c7_par;
+    env->cp15.banked_c7_par_hi[i] = env->cp15.c7_par_hi;
+    env->cp15.banked_c13_context[i] = env->cp15.c13_context;
+    env->cp15.banked_c13_fcse[i] = env->cp15.c13_fcse;
+    env->cp15.banked_c13_tls1[i] = env->cp15.c13_tls1;
+    env->cp15.banked_c13_tls2[i] = env->cp15.c13_tls2;
+    env->cp15.banked_c13_tls3[i] = env->cp15.c13_tls3;
+
+    /* Restore new Security state registers */
+    i = secure ? 0 : 1;
+    env->cp15.c0_cssel = env->cp15.banked_c0_cssel[i];
+    /* Maintain the value of common bits */
+    env->cp15.c1_sys &= 0x8204000;
+    env->cp15.c1_sys |= env->cp15.banked_c1_sys[i] & ~0x8204000;
+    env->cp15.c2_base0 = env->cp15.banked_c2_base0[i];
+    env->cp15.c2_base0_hi = env->cp15.banked_c2_base0_hi[i];
+    env->cp15.c2_base1 = env->cp15.banked_c2_base1[i];
+    env->cp15.c2_base1_hi = env->cp15.banked_c2_base1_hi[i];
+    {
+        int maskshift;
+        env->cp15.c2_control = env->cp15.banked_c2_control[i];
+        maskshift = extract32(env->cp15.c2_control, 0, 3);
+        env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
+        env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
+    }
+    env->cp15.c3 = env->cp15.banked_c3[i];
+    env->cp15.c5_data = env->cp15.banked_c5_data[i];
+    env->cp15.c5_insn = env->cp15.banked_c5_insn[i];
+    env->cp15.c6_data = env->cp15.banked_c6_data[i];
+    env->cp15.c6_insn = env->cp15.banked_c6_insn[i];
+    env->cp15.c7_par = env->cp15.banked_c7_par[i];
+    env->cp15.c7_par_hi = env->cp15.banked_c7_par_hi[i];
+    env->cp15.c13_context = env->cp15.banked_c13_context[i];
+    env->cp15.c13_fcse = env->cp15.banked_c13_fcse[i];
+    env->cp15.c13_tls1 = env->cp15.banked_c13_tls1[i];
+    env->cp15.c13_tls2 = env->cp15.banked_c13_tls2[i];
+    env->cp15.c13_tls3 = env->cp15.banked_c13_tls3[i];
 }
 
 static void v7m_push(CPUARMState *env, uint32_t val)
Signature · 1 line
1.7.9.5
#42 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 9 lines · Sergey Fedorov

Banked coprocessor registers are switched on two cases:
1) Entering or leaving CPU monitor mode with SCR.NS bit set;
2) Setting SCR.NS bit not from CPU monitor mode

Coprocessor register banking is done similar to CPU core register banking. Some of SCTRL bits are common for secure and non-secure state. Translation table base masks are updated on register switch instead of TTBCR write.

So I was rather confused as to your banking scheme until I got to this patch. Now I see the implementation. You are mass-hot-swapping in the active state on critical CPU state changing events. ....

Quoted 102 lines
---
 target-arm/helper.c |   77 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 76 insertions(+), 1 deletion(-)
target-arm/helper.c +67 -1
index e1e9762..7bfadb0 100644
@@ -11,6 +11,7 @@ static inline int get_phys_addr(CPUARMState *env, uint32_t address,
                                 int access_type, int is_user,
                                 hwaddr *phys_ptr, int *prot,
                                 target_ulong *page_size);
+static void switch_cp15_regs(CPUARMState *env, int secure);
 #endif
 
 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
@@ -1553,6 +1554,17 @@ static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 }
 
 #ifndef CONFIG_USER_ONLY
+static int scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
+{
+    if ((value & 1/*NS*/) && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_MON) {
+        /* We are going to Non-secure state; switch banked system control registers */
+        switch_cp15_regs(env, 0);
+    }
+
+    env->cp15.c1_scr = value;
+    return 0;
+}
+
 static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
                       uint64_t value)
 {
@@ -1572,7 +1584,7 @@ static const ARMCPRegInfo tz_cp_reginfo[] = {
 #ifndef CONFIG_USER_ONLY
     { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
-      .resetvalue = 0 },
+      .writefn = scr_write, .resetvalue = 0 },
     { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
       .access = PL1_RW, .type = ARM_CP_BANKED, .writefn = vbar_write,
       .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
@@ -2284,6 +2296,69 @@ void switch_mode(CPUARMState *env, int mode)
     env->regs[13] = env->banked_r13[i];
     env->regs[14] = env->banked_r14[i];
     env->spsr = env->banked_spsr[i];
+
+    if ((mode == ARM_CPU_MODE_MON || old_mode == ARM_CPU_MODE_MON) &&
+            (env->cp15.c1_scr & 1/*NS*/)) {
+        /* We are going to change Security state; switch banked system control registers */
+        switch_cp15_regs(env, (mode == ARM_CPU_MODE_MON));
+    }
+}
+
+void switch_cp15_regs(CPUARMState *env, int secure)
+{
+    int i;
+
+    /* Save current Security state registers */
+    i = arm_is_secure(env) ? 0 : 1;
+    env->cp15.banked_c0_cssel[i] = env->cp15.c0_cssel;
+    env->cp15.banked_c1_sys[i] = env->cp15.c1_sys;
+    env->cp15.banked_c2_base0[i] = env->cp15.c2_base0;
+    env->cp15.banked_c2_base0_hi[i] = env->cp15.c2_base0_hi;
+    env->cp15.banked_c2_base1[i] = env->cp15.c2_base1;
+    env->cp15.banked_c2_base1_hi[i] = env->cp15.c2_base1_hi;
+    env->cp15.banked_c2_control[i] = env->cp15.c2_control;
+    env->cp15.banked_c3[i] = env->cp15.c3;
+    env->cp15.banked_c5_data[i] = env->cp15.c5_data;
+    env->cp15.banked_c5_insn[i] = env->cp15.c5_insn;
+    env->cp15.banked_c6_data[i] = env->cp15.c6_data;
+    env->cp15.banked_c6_insn[i] = env->cp15.c6_insn;
+    env->cp15.banked_c7_par[i] = env->cp15.c7_par;
+    env->cp15.banked_c7_par_hi[i] = env->cp15.c7_par_hi;
+    env->cp15.banked_c13_context[i] = env->cp15.c13_context;
+    env->cp15.banked_c13_fcse[i] = env->cp15.c13_fcse;
+    env->cp15.banked_c13_tls1[i] = env->cp15.c13_tls1;
+    env->cp15.banked_c13_tls2[i] = env->cp15.c13_tls2;
+    env->cp15.banked_c13_tls3[i] = env->cp15.c13_tls3;
+
+    /* Restore new Security state registers */
+    i = secure ? 0 : 1;
+    env->cp15.c0_cssel = env->cp15.banked_c0_cssel[i];
+    /* Maintain the value of common bits */
+    env->cp15.c1_sys &= 0x8204000;
+    env->cp15.c1_sys |= env->cp15.banked_c1_sys[i] & ~0x8204000;
+    env->cp15.c2_base0 = env->cp15.banked_c2_base0[i];
+    env->cp15.c2_base0_hi = env->cp15.banked_c2_base0_hi[i];
+    env->cp15.c2_base1 = env->cp15.banked_c2_base1[i];
+    env->cp15.c2_base1_hi = env->cp15.banked_c2_base1_hi[i];
+    {
+        int maskshift;
+        env->cp15.c2_control = env->cp15.banked_c2_control[i];
+        maskshift = extract32(env->cp15.c2_control, 0, 3);
+        env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
+        env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
+    }
+    env->cp15.c3 = env->cp15.banked_c3[i];
+    env->cp15.c5_data = env->cp15.banked_c5_data[i];
+    env->cp15.c5_insn = env->cp15.banked_c5_insn[i];

target-arm/helper.c void switch_mode(CPUARMState *env, int mode) · line 2362

Quoted 10 lines
target-arm/helper.c +9 -0
+    env->cp15.c6_data = env->cp15.banked_c6_data[i];
+    env->cp15.c6_insn = env->cp15.banked_c6_insn[i];
+    env->cp15.c7_par = env->cp15.banked_c7_par[i];
+    env->cp15.c7_par_hi = env->cp15.banked_c7_par_hi[i];
+    env->cp15.c13_context = env->cp15.banked_c13_context[i];
+    env->cp15.c13_fcse = env->cp15.banked_c13_fcse[i];
+    env->cp15.c13_tls1 = env->cp15.banked_c13_tls1[i];
+    env->cp15.c13_tls2 = env->cp15.banked_c13_tls2[i];
+    env->cp15.c13_tls3 = env->cp15.banked_c13_tls3[i];
 }

And I think this is awkward. Can't we just teach TCG to get the right value out of the banked array and do away with these active copies completely? This greatly complicates the change pattern for adding a new banked CP.

Regards,
Peter

Quoted 6 lines
target-arm/helper.c +0 -1
 static void v7m_push(CPUARMState *env, uint32_t val)
--

1.7.9.5

#43 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 131 lines · Peter Crosthwaite
Quoted 9 lines · Sergey Fedorov

Banked coprocessor registers are switched on two cases:
1) Entering or leaving CPU monitor mode with SCR.NS bit set;
2) Setting SCR.NS bit not from CPU monitor mode

Coprocessor register banking is done similar to CPU core register banking. Some of SCTRL bits are common for secure and non-secure state. Translation table base masks are updated on register switch instead of TTBCR write.

So I was rather confused as to your banking scheme until I got to this patch. Now I see the implementation. You are mass-hot-swapping in the active state on critical CPU state changing events. ....

Quoted 102 lines
---
  target-arm/helper.c |   77 ++++++++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 76 insertions(+), 1 deletion(-)
target-arm/helper.c +67 -1
index e1e9762..7bfadb0 100644
@@ -11,6 +11,7 @@ static inline int get_phys_addr(CPUARMState *env, uint32_t address,
                                  int access_type, int is_user,
                                  hwaddr *phys_ptr, int *prot,
                                  target_ulong *page_size);
+static void switch_cp15_regs(CPUARMState *env, int secure);
  #endif
 
  static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
@@ -1553,6 +1554,17 @@ static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
  }
 
  #ifndef CONFIG_USER_ONLY
+static int scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
+{
+    if ((value & 1/*NS*/) && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_MON) {
+        /* We are going to Non-secure state; switch banked system control registers */
+        switch_cp15_regs(env, 0);
+    }
+
+    env->cp15.c1_scr = value;
+    return 0;
+}
+
  static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
                        uint64_t value)
  {
@@ -1572,7 +1584,7 @@ static const ARMCPRegInfo tz_cp_reginfo[] = {
  #ifndef CONFIG_USER_ONLY
      { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
        .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
-      .resetvalue = 0 },
+      .writefn = scr_write, .resetvalue = 0 },
      { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
        .access = PL1_RW, .type = ARM_CP_BANKED, .writefn = vbar_write,
        .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
@@ -2284,6 +2296,69 @@ void switch_mode(CPUARMState *env, int mode)
      env->regs[13] = env->banked_r13[i];
      env->regs[14] = env->banked_r14[i];
      env->spsr = env->banked_spsr[i];
+
+    if ((mode == ARM_CPU_MODE_MON || old_mode == ARM_CPU_MODE_MON) &&
+            (env->cp15.c1_scr & 1/*NS*/)) {
+        /* We are going to change Security state; switch banked system control registers */
+        switch_cp15_regs(env, (mode == ARM_CPU_MODE_MON));
+    }
+}
+
+void switch_cp15_regs(CPUARMState *env, int secure)
+{
+    int i;
+
+    /* Save current Security state registers */
+    i = arm_is_secure(env) ? 0 : 1;
+    env->cp15.banked_c0_cssel[i] = env->cp15.c0_cssel;
+    env->cp15.banked_c1_sys[i] = env->cp15.c1_sys;
+    env->cp15.banked_c2_base0[i] = env->cp15.c2_base0;
+    env->cp15.banked_c2_base0_hi[i] = env->cp15.c2_base0_hi;
+    env->cp15.banked_c2_base1[i] = env->cp15.c2_base1;
+    env->cp15.banked_c2_base1_hi[i] = env->cp15.c2_base1_hi;
+    env->cp15.banked_c2_control[i] = env->cp15.c2_control;
+    env->cp15.banked_c3[i] = env->cp15.c3;
+    env->cp15.banked_c5_data[i] = env->cp15.c5_data;
+    env->cp15.banked_c5_insn[i] = env->cp15.c5_insn;
+    env->cp15.banked_c6_data[i] = env->cp15.c6_data;
+    env->cp15.banked_c6_insn[i] = env->cp15.c6_insn;
+    env->cp15.banked_c7_par[i] = env->cp15.c7_par;
+    env->cp15.banked_c7_par_hi[i] = env->cp15.c7_par_hi;
+    env->cp15.banked_c13_context[i] = env->cp15.c13_context;
+    env->cp15.banked_c13_fcse[i] = env->cp15.c13_fcse;
+    env->cp15.banked_c13_tls1[i] = env->cp15.c13_tls1;
+    env->cp15.banked_c13_tls2[i] = env->cp15.c13_tls2;
+    env->cp15.banked_c13_tls3[i] = env->cp15.c13_tls3;
+
+    /* Restore new Security state registers */
+    i = secure ? 0 : 1;
+    env->cp15.c0_cssel = env->cp15.banked_c0_cssel[i];
+    /* Maintain the value of common bits */
+    env->cp15.c1_sys &= 0x8204000;
+    env->cp15.c1_sys |= env->cp15.banked_c1_sys[i] & ~0x8204000;
+    env->cp15.c2_base0 = env->cp15.banked_c2_base0[i];
+    env->cp15.c2_base0_hi = env->cp15.banked_c2_base0_hi[i];
+    env->cp15.c2_base1 = env->cp15.banked_c2_base1[i];
+    env->cp15.c2_base1_hi = env->cp15.banked_c2_base1_hi[i];
+    {
+        int maskshift;
+        env->cp15.c2_control = env->cp15.banked_c2_control[i];
+        maskshift = extract32(env->cp15.c2_control, 0, 3);
+        env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
+        env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
+    }
+    env->cp15.c3 = env->cp15.banked_c3[i];
+    env->cp15.c5_data = env->cp15.banked_c5_data[i];
+    env->cp15.c5_insn = env->cp15.banked_c5_insn[i];

target-arm/helper.c void switch_mode(CPUARMState *env, int mode) · line 2362

Quoted 10 lines
target-arm/helper.c +9 -0
+    env->cp15.c6_data = env->cp15.banked_c6_data[i];
+    env->cp15.c6_insn = env->cp15.banked_c6_insn[i];
+    env->cp15.c7_par = env->cp15.banked_c7_par[i];
+    env->cp15.c7_par_hi = env->cp15.banked_c7_par_hi[i];
+    env->cp15.c13_context = env->cp15.banked_c13_context[i];
+    env->cp15.c13_fcse = env->cp15.banked_c13_fcse[i];
+    env->cp15.c13_tls1 = env->cp15.banked_c13_tls1[i];
+    env->cp15.c13_tls2 = env->cp15.banked_c13_tls2[i];
+    env->cp15.c13_tls3 = env->cp15.banked_c13_tls3[i];
  }

And I think this is awkward. Can't we just teach TCG to get the right value out of the banked array and do away with these active copies completely? This greatly complicates the change pattern for adding a new banked CP.

Quoted 2 lines

Regards,
Peter

Yes, this banking scheme makes state changing events quite heavy. But maintaining the active copies allows to keep translation table walking code untouched. I think there is a trade-off between state changing and translation table walking overheads.

I think the CP banking is the most fragile thing in this patch series and this should become much better after review :)

Thanks!

Quoted 6 lines
Quoted 6 lines
target-arm/helper.c +0 -1
  static void v7m_push(CPUARMState *env, uint32_t val)
--

1.7.9.5

Signature · 2 lines
Best regards,
Sergey Fedorov
#44 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 4 lines · Fedorov Sergey

Yes, this banking scheme makes state changing events quite heavy. But maintaining the active copies allows to keep translation table walking code untouched. I think there is a trade-off between state changing and translation table walking overheads.

We shouldn't be doing tlb walks that often that it makes a difference whether we do env->ttbr0 or env->ttbr0[env->ns] ...

Quoted 2 lines

I think the CP banking is the most fragile thing in this patch series and this should become much better after review :)

It would probably be a good idea to look at the v8 ARM ARM and figure out how banked-for-NS/S registers should fit in with the AArch64 vs AArch32 split.
[if you don't have a copy, it's on the ARM website:
http://infocenter.arm.com/help/topic/com.arm.doc.ddi0487a.a_errata2/index.html
you'll need to register an account on the website if you don't already have one but it's a fairly simple "fill in the form" automated process]

Note in particular that:
* many of the current uint32_t fields in our CPU state struct are likely to widen to uint64_t, so the AArch64 representation is canonical, and the AArch32 register accessors access a part of that state (typically the lower 32 bits)
* registers which are banked S/NS in AArch32 are not necessarily banked in AArch64

AArch64 support is likely to land before your TrustZone stuff does so we need to make the two features work together cleanly.

thanks
-- PMM

#45 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 20 lines · Peter Maydell
Quoted 4 lines · Fedorov Sergey

Yes, this banking scheme makes state changing events quite heavy. But maintaining the active copies allows to keep translation table walking code untouched. I think there is a trade-off between state changing and translation table walking overheads.

We shouldn't be doing tlb walks that often that it makes a difference whether we do env->ttbr0 or env->ttbr0[env->ns] ...

Quoted 2 lines

I think the CP banking is the most fragile thing in this patch series and this should become much better after review :)

It would probably be a good idea to look at the v8 ARM ARM and figure out how banked-for-NS/S registers should fit in with the AArch64 vs AArch32 split.
[if you don't have a copy, it's on the ARM website:
http://infocenter.arm.com/help/topic/com.arm.doc.ddi0487a.a_errata2/index.html
you'll need to register an account on the website if you don't already have one but it's a fairly simple "fill in the form" automated process]

Quoted 8 lines

Note in particular that:
* many of the current uint32_t fields in our CPU state struct are likely to widen to uint64_t, so the AArch64 representation is canonical, and the AArch32 register accessors access a part of that state (typically the lower 32 bits)
* registers which are banked S/NS in AArch32 are not necessarily banked in AArch64

Adding to that, are there any other reasons to bank a register other than sec-extensions? It seems like what you have implemented here is too sec specific for simply calling it "banked" (without further clarification of what you are banking for).

Regards,
Peter

Quoted 6 lines

AArch64 support is likely to land before your TrustZone stuff does so we need to make the two features work together cleanly.

thanks
-- PMM

#46 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 27 lines · Peter Crosthwaite
Quoted 18 lines · Peter Maydell
Quoted 4 lines · Fedorov Sergey

Yes, this banking scheme makes state changing events quite heavy. But maintaining the active copies allows to keep translation table walking code untouched. I think there is a trade-off between state changing and translation table walking overheads.

We shouldn't be doing tlb walks that often that it makes a difference whether we do env->ttbr0 or env->ttbr0[env->ns] ...

Quoted 2 lines

I think the CP banking is the most fragile thing in this patch series and this should become much better after review :)

It would probably be a good idea to look at the v8 ARM ARM and figure out how banked-for-NS/S registers should fit in with the AArch64 vs AArch32 split.
[if you don't have a copy, it's on the ARM website:
http://infocenter.arm.com/help/topic/com.arm.doc.ddi0487a.a_errata2/index.html
you'll need to register an account on the website if you don't already have one but it's a fairly simple "fill in the form" automated process]

Quoted 8 lines

Note in particular that:
* many of the current uint32_t fields in our CPU state struct are likely to widen to uint64_t, so the AArch64 representation is canonical, and the AArch32 register accessors access a part of that state (typically the lower 32 bits)
* registers which are banked S/NS in AArch32 are not necessarily banked in AArch64

Quoted 7 lines

Adding to that, are there any other reasons to bank a register other than sec-extensions? It seems like what you have implemented here is too sec specific for simply calling it "banked" (without further clarification of what you are banking for).

Regards,
Peter

I'm not sure that I understand your question correctly but I try to answer. From ARMv7 ARM document section "B3.15.3 Classification of system control registers":

"Banked system control registers have two copies, one Secure and one Non-secure."

I don't know any use of term "CP15 banked registers" other that related to Security Extensions.

Best regards,
Sergey Fedorov

Quoted 7 lines
Quoted 7 lines

AArch64 support is likely to land before your TrustZone stuff does so we need to make the two features work together cleanly.

thanks
-- PMM

#47 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 47 lines · Fedorov Sergey
Quoted 36 lines · Peter Crosthwaite
Quoted 25 lines · Peter Maydell
Quoted 6 lines · Fedorov Sergey

Yes, this banking scheme makes state changing events quite heavy. But maintaining the active copies allows to keep translation table walking code
untouched. I think there is a trade-off between state changing and translation table walking overheads.

We shouldn't be doing tlb walks that often that it makes a difference whether we do env->ttbr0 or env->ttbr0[env->ns] ...

Quoted 3 lines

I think the CP banking is the most fragile thing in this patch series and
this should become much better after review :)

It would probably be a good idea to look at the v8 ARM ARM and figure out how banked-for-NS/S registers should fit in with the AArch64 vs AArch32 split.
[if you don't have a copy, it's on the ARM website:

http://infocenter.arm.com/help/topic/com.arm.doc.ddi0487a.a_errata2/index.html
you'll need to register an account on the website if you don't already have one but it's a fairly simple "fill in the form" automated process]

Quoted 8 lines

Note in particular that:
* many of the current uint32_t fields in our CPU state struct are likely to widen to uint64_t, so the AArch64 representation is canonical, and the AArch32 register accessors access a part of that state (typically the lower 32 bits)
* registers which are banked S/NS in AArch32 are not necessarily banked in AArch64

Quoted 7 lines

Adding to that, are there any other reasons to bank a register other than sec-extensions? It seems like what you have implemented here is too sec specific for simply calling it "banked" (without further clarification of what you are banking for).

Regards,
Peter

Quoted 7 lines

I'm not sure that I understand your question correctly but I try to answer. From ARMv7 ARM document section "B3.15.3 Classification of system control registers":

"Banked system control registers have two copies, one Secure and one Non-secure."

Ok fair enough. I will wager though that sooner or later ARM will find a reason to bank a reg other than sec-ext. But let's stick to their terminology which by that quote its quite clear that "banked" means "banked for security" (No change needed).

Regards,
Peter

Quoted 16 lines

I don't know any use of term "CP15 banked registers" other that related to Security Extensions.

Best regards,
Sergey Fedorov

Quoted 7 lines
Quoted 7 lines

AArch64 support is likely to land before your TrustZone stuff does so we need to make the two features work together cleanly.

thanks
-- PMM

#48 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 9 lines · Peter Crosthwaite
Quoted 3 lines · Fedorov Sergey

"Banked system control registers have two copies, one Secure and one Non-secure."

Ok fair enough. I will wager though that sooner or later ARM will find a reason to bank a reg other than sec-ext. But let's stick to their terminology which by that quote its quite clear that "banked" means "banked for security" (No change needed).

Well, the other register banking is the gp regs which get banks for user/svc/fiq/etc in AArch32. But I think in the context of system registers "banked register" is fairly clear.

thanks
-- PMM

#49 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

In reply to Peter Maydell
Quoted 26 lines · Peter Maydell
Quoted 4 lines · Fedorov Sergey

Yes, this banking scheme makes state changing events quite heavy. But maintaining the active copies allows to keep translation table walking code untouched. I think there is a trade-off between state changing and translation table walking overheads.

We shouldn't be doing tlb walks that often that it makes a difference whether we do env->ttbr0 or env->ttbr0[env->ns] ...

Quoted 2 lines

I think the CP banking is the most fragile thing in this patch series and this should become much better after review :)

It would probably be a good idea to look at the v8 ARM ARM and figure out how banked-for-NS/S registers should fit in with the AArch64 vs AArch32 split.
[if you don't have a copy, it's on the ARM website:
http://infocenter.arm.com/help/topic/com.arm.doc.ddi0487a.a_errata2/index.html
you'll need to register an account on the website if you don't already have one but it's a fairly simple "fill in the form" automated process]

Note in particular that:
* many of the current uint32_t fields in our CPU state struct are likely to widen to uint64_t, so the AArch64 representation is canonical, and the AArch32 register accessors access a part of that state (typically the lower 32 bits)
* registers which are banked S/NS in AArch32 are not necessarily banked in AArch64

Quoted 6 lines

AArch64 support is likely to land before your TrustZone stuff does so we need to make the two features work together cleanly.

thanks
-- PMM

I've briefly looked at the v8 ARM ARM. As I can see there is no banked system control registers in AArch64. Seems the concept is changed to provide separate registers for each meaningful execution level. Please, correct me if I am wrong.

So I think there shouldn't be "active" and "banked" fields for banked AArch32 CP15 registers as in my patch. Seems it is worth to use AArch64 view of system control registers as a basis. That means there would be separate S and NS register fields in CPU state structure that will me mapped to separate AArch64 registers. ARMCPRegInfo structure would have additional field holding NS register state filed offset for AArch32 banked registers.

Which branch in https://git.linaro.org/people/peter.maydell/qemu-arm.git repository holds the most actual A64 support?

Thanks!

Best regards,
Sergey Fedorov

#50 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 4 lines · Fedorov Sergey

I've briefly looked at the v8 ARM ARM. As I can see there is no banked system control registers in AArch64. Seems the concept is changed to provide separate registers for each meaningful execution level. Please, correct me if I am wrong.

Yes, I think this is generally correct.

Quoted 6 lines

So I think there shouldn't be "active" and "banked" fields for banked AArch32 CP15 registers as in my patch. Seems it is worth to use AArch64 view of system control registers as a basis. That means there would be separate S and NS register fields in CPU state structure that will me mapped to separate AArch64 registers. ARMCPRegInfo structure would have additional field holding NS register state filed offset for AArch32 banked registers.

This sounds like it could work, though there are some wrinkles for registers with readfns/writefns -- do we have extra s vs ns read/write functions, or just one set of functions which has to look in env->ns to figure out whether to use the S or NS version?

Quoted 2 lines

Which branch in https://git.linaro.org/people/peter.maydell/qemu-arm.git
repository holds the most actual A64 support?

It's still a work in progress so it depends what you want. a64-third-fourth-set is the last set of patches that went out for review, and should generally work for integer instructions. a64-working is my work-in-progress branch so it will have the most recent versions of everything, but it rebases frequently and is liable to occasionally be broken...

thanks
-- PMM

#51 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 13 lines · Peter Maydell
Quoted 4 lines · Fedorov Sergey

I've briefly looked at the v8 ARM ARM. As I can see there is no banked system control registers in AArch64. Seems the concept is changed to provide separate registers for each meaningful execution level. Please, correct me if I am wrong.

Yes, I think this is generally correct.

Quoted 6 lines

So I think there shouldn't be "active" and "banked" fields for banked AArch32 CP15 registers as in my patch. Seems it is worth to use AArch64 view of system control registers as a basis. That means there would be separate S and NS register fields in CPU state structure that will me mapped to separate AArch64 registers. ARMCPRegInfo structure would have additional field holding NS register state filed offset for AArch32 banked registers.

Quoted 4 lines

This sounds like it could work, though there are some wrinkles for registers with readfns/writefns -- do we have extra s vs ns read/write functions, or just one set of functions which has to look in env->ns to figure out whether to use the S or NS version?

I think if most read/write functions do the same work for both S/NS versions then this code should not be duplicated.

Quoted 9 lines
Quoted 3 lines

Which branch in https://git.linaro.org/people/peter.maydell/qemu-arm.git
repository holds the most actual A64 support?

It's still a work in progress so it depends what you want. a64-third-fourth-set is the last set of patches that went out for review, and should generally work for integer instructions. a64-working is my work-in-progress branch so it will have the most recent versions of everything, but it rebases frequently and is liable to occasionally be broken...

Thanks.

Quoted 4 lines

thanks
-- PMM

Signature · 2 lines
Best regards,
Sergey Fedorov
#52 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 14 lines · Fedorov Sergey
Quoted 13 lines · Peter Maydell
Quoted 4 lines · Fedorov Sergey

I've briefly looked at the v8 ARM ARM. As I can see there is no banked system control registers in AArch64. Seems the concept is changed to provide separate registers for each meaningful execution level. Please, correct me if I am wrong.

Yes, I think this is generally correct.

Quoted 6 lines

So I think there shouldn't be "active" and "banked" fields for banked AArch32 CP15 registers as in my patch. Seems it is worth to use AArch64 view of system control registers as a basis. That means there would be separate S and NS register fields in CPU state structure that will me mapped to separate AArch64 registers. ARMCPRegInfo structure would have additional field holding NS register state filed offset for AArch32 banked registers.

Quoted 6 lines
Quoted 4 lines

This sounds like it could work, though there are some wrinkles for registers with readfns/writefns -- do we have extra s vs ns read/write functions, or just one set of functions which has to look in env->ns to figure out whether to use the S or NS version?

I think if most read/write functions do the same work for both S/NS versions then this code should not be duplicated.

But on the other hand, separate S/NS read/write functions could be reused for AArch64 register descriptions that is separate for each EL...

Quoted 14 lines
Quoted 9 lines
Quoted 3 lines

Which branch in https://git.linaro.org/people/peter.maydell/qemu-arm.git
repository holds the most actual A64 support?

It's still a work in progress so it depends what you want. a64-third-fourth-set is the last set of patches that went out for review, and should generally work for integer instructions. a64-working is my work-in-progress branch so it will have the most recent versions of everything, but it rebases frequently and is liable to occasionally be broken...

Thanks.

Quoted 3 lines

thanks
-- PMM

Signature · 2 lines
Best regards,
Sergey Fedorov
#53 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

In reply to Peter Maydell
Quoted 5 lines · Peter Maydell
Quoted 4 lines · Fedorov Sergey

I've briefly looked at the v8 ARM ARM. As I can see there is no banked system control registers in AArch64. Seems the concept is changed to provide separate registers for each meaningful execution level. Please, correct me if I am wrong.

Isn't that just another definition of banking?

Quoted 5 lines

Yes, I think this is generally correct.

Quoted 2 lines

So I think there shouldn't be "active" and "banked" fields for banked AArch32 CP15 registers as in my patch.

I don't see how this extra scheme warrants the active-set+mass-hotswapping impl. Why can the accessors just be aware of the two different banking schemes at access time?

Seems it is worth to use AArch64 view

Quoted 6 lines
Quoted 4 lines

of system control registers as a basis. That means there would be separate S and NS register fields in CPU state structure that will me mapped to separate AArch64 registers. ARMCPRegInfo structure would have additional field holding NS register state filed offset for AArch32 banked registers.

This sounds like it could work,

I largely agree, except for the need for an active set.

Quoted 4 lines

though there are some wrinkles for
registers with readfns/writefns -- do we have extra s vs ns read/write functions, or just one set of functions which has to look in env->ns to figure out whether to use the S or NS version?

One set sounds better to me - if your resorting to open coding your situation is probably complicated enough such that there is little you can do in a data driven way. That said, it could be useful to define both r.w handlers and fieldoffsets, and then the custom handlers access do actual register read/write through a generic helper fn (passed the CPRegInfo) that uses ->fieldoffset and is banking aware. This handlers the common cases where helper functions are doing:

1: Normal access + some side effects
2: Manipulation of the read/written value on the way in/out.

without the need for all handlers having to open code banking functionality.

Regards,
Peter

Quoted 14 lines
Quoted 3 lines

Which branch in https://git.linaro.org/people/peter.maydell/qemu-arm.git
repository holds the most actual A64 support?

It's still a work in progress so it depends what you want. a64-third-fourth-set is the last set of patches that went out for review, and should generally work for integer instructions. a64-working is my work-in-progress branch so it will have the most recent versions of everything, but it rebases frequently and is liable to occasionally be broken...

thanks
-- PMM

#54 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 9 lines · Peter Crosthwaite
Quoted 5 lines · Peter Maydell
Quoted 4 lines · Fedorov Sergey

I've briefly looked at the v8 ARM ARM. As I can see there is no banked system control registers in AArch64. Seems the concept is changed to provide separate registers for each meaningful execution level. Please, correct me if I am wrong.

Isn't that just another definition of banking?

No: the crn/crm/op values differ for eg ESR_EL1, ESR_EL2 and ESR_EL3, and if you're in EL3 you can access all three of them (the access permissions are what stop EL1 from messing with EL3's ESR). Banked registers are where the same access instruction reads or writes different state depending on the value of something else (whether we're secure/nonsecure, in this case).

thanks
-- PMM

#55 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

In reply to Peter Crosthwaite
Quoted 37 lines · Peter Crosthwaite
Quoted 5 lines · Peter Maydell
Quoted 4 lines · Fedorov Sergey

I've briefly looked at the v8 ARM ARM. As I can see there is no banked system control registers in AArch64. Seems the concept is changed to provide separate registers for each meaningful execution level. Please, correct me if I am wrong.

Isn't that just another definition of banking?

Quoted 4 lines

Yes, I think this is generally correct.

Quoted 2 lines

So I think there shouldn't be "active" and "banked" fields for banked AArch32 CP15 registers as in my patch.

I don't see how this extra scheme warrants the active-set+mass-hotswapping impl. Why can the accessors just be aware of the two different banking schemes at access time?

Seems it is worth to use AArch64 view

Quoted 5 lines
Quoted 4 lines

of system control registers as a basis. That means there would be separate S and NS register fields in CPU state structure that will me mapped to separate AArch64 registers. ARMCPRegInfo structure would have additional field holding NS register state filed offset for AArch32 banked registers.

This sounds like it could work,

I largely agree, except for the need for an active set.

Quoted 4 lines

though there are some wrinkles for
registers with readfns/writefns -- do we have extra s vs ns read/write functions, or just one set of functions which has to look in env->ns to figure out whether to use the S or NS version?

One set sounds better to me - if your resorting to open coding your situation is probably complicated enough such that there is little you can do in a data driven way. That said, it could be useful to define both r.w handlers and fieldoffsets, and then the custom handlers access do actual register read/write through a generic helper fn (passed the CPRegInfo) that uses ->fieldoffset and is banking aware. This handlers the common cases where helper functions are doing:

Quoted 20 lines

1: Normal access + some side effects
2: Manipulation of the read/written value on the way in/out.

without the need for all handlers having to open code banking functionality.

Regards,
Peter

Quoted 12 lines
Quoted 2 lines

Which branch in https://git.linaro.org/people/peter.maydell/qemu-arm.git
repository holds the most actual A64 support?

It's still a work in progress so it depends what you want. a64-third-fourth-set is the last set of patches that went out for review, and should generally work for integer instructions. a64-working is my work-in-progress branch so it will have the most recent versions of everything, but it rebases frequently and is liable to occasionally be broken...

thanks
-- PMM

Seems I agree with you.

Best regards,
Sergey Fedorov

#56 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

In reply to Peter Maydell
Quoted 4 lines · Peter Maydell

This sounds like it could work, though there are some wrinkles for registers with readfns/writefns -- do we have extra s vs ns read/write functions, or just one set of functions which has to look in env->ns to figure out whether to use the S or NS version?

What about defining a separate ARMCPRegInfo for each banked AArch32 system register? I think it would be close to AArch64 concept. It would allow to use separate read/write handlers if necessary or reuse the same handlers otherwise. When the handlers is not used, the translation code would simply lookup the ARMCPRegInfo for corresponding secure state and use the field offset.

Best regards,
Sergey Fedorov

#57 lore raw

Re: [Qemu-devel] [RFC PATCH 18/21] target-arm: switch banked CP registers

Quoted 7 lines · Fedorov Sergey
Quoted 4 lines · Peter Maydell

This sounds like it could work, though there are some wrinkles for registers with readfns/writefns -- do we have extra s vs ns read/write functions, or just one set of functions which has to look in env->ns to figure out whether to use the S or NS version?

Quoted 6 lines

What about defining a separate ARMCPRegInfo for each banked AArch32 system register? I think it would be close to AArch64 concept. It would allow to use separate read/write handlers if necessary or reuse the same handlers otherwise. When the handlers is not used, the translation code would simply lookup the ARMCPRegInfo for corresponding secure state and use the field offset.

Yes, I was thinking about that the other day -- add the S/NS to the set of things in the hash table key, and have the 32 bit MCR/MRC code pass in the right value to get the correct banked register out. (We'd have to make all the non-banked registers go into the hashtable twice, though, but that's not a big deal I think.)

thanks
-- PMM

#58 lore raw

RFC PATCH 19/21 target-arm: add MVBAR support

In reply to Sergey Fedorov

MVBAR register provides an exception vector base address for exceptions taking to CPU monitor mode.

---
 target-arm/cpu.h    |    1 +
 target-arm/helper.c |   16 +++++++---------
 2 files changed, 8 insertions(+), 9 deletions(-)
target-arm/cpu.h +1 -0
index b4500b4..3e5b860 100644
@@ -210,6 +210,7 @@ typedef struct CPUARMState {
         uint32_t c9_pmuserenr; /* perf monitor user enable */
         uint32_t c9_pminten; /* perf monitor interrupt enables */
         BANKED_CP_REG(uint32_t, c12_vbar); /* vector base address register */
+        uint32_t c12_mvbar; /* monitor vector base address register */
         BANKED_CP_REG(uint32_t, c13_fcse); /* FCSE PID.  */
         BANKED_CP_REG(uint32_t, c13_context); /* Context ID.  */
         BANKED_CP_REG(uint32_t, c13_tls1); /* User RW Thread register.  */
target-arm/helper.c +7 -9
index 7bfadb0..582de74 100644
@@ -1568,7 +1568,7 @@ static int scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
                       uint64_t value)
 {
-    env->cp15.c12_vbar = value & ~0x1Ful;
+    CPREG_FIELD32(env, ri) = value & ~0x1Ful;
     return 0;
 }
 
@@ -1589,6 +1589,9 @@ static const ARMCPRegInfo tz_cp_reginfo[] = {
       .access = PL1_RW, .type = ARM_CP_BANKED, .writefn = vbar_write,
       .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
       .resetvalue = 0 },
+    { .name = "MVBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 1,
+      .access = PL3_RW, .resetvalue = 0, .writefn = vbar_write,
+      .fieldoffset = offsetof(CPUARMState, cp15.c12_mvbar) },
     { .name = "SDER", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 1,
       .access = PL3_RW, .resetvalue = 0,
       .fieldoffset = offsetof(CPUARMState, cp15.c1_sder) },
@@ -2630,17 +2633,12 @@ void arm_cpu_do_interrupt(CPUState *cs)
         return; /* Never happens.  Keep compiler happy.  */
     }
     /* High vectors.  */
-    if (env->cp15.c1_sys & (1 << 13)) {
+    if (new_mode == ARM_CPU_MODE_MON) {
+        addr += env->cp15.c12_mvbar;
+    } else if (env->cp15.c1_sys & (1 << 13)) {
         /* when enabled, base address cannot be remapped.  */
         addr += 0xffff0000;
     } else {
-        /* ARM v7 architectures provide a vector base address register to remap
-         * the interrupt vector table.
-         * This register is only followed in non-monitor mode, and has a secure
-         * and un-secure copy. Since the cpu is always in a un-secure operation
-         * and is never in monitor mode this feature is always active.
-         * Note: only bits 31:5 are valid.
-         */
         addr += env->cp15.c12_vbar;
     }
     switch_mode (env, new_mode);
Signature · 1 line
1.7.9.5
#59 lore raw

Re: [Qemu-devel] [RFC PATCH 19/21] target-arm: add MVBAR support

Quoted 45 lines · Sergey Fedorov

MVBAR register provides an exception vector base address for exceptions taking to CPU monitor mode.

---
 target-arm/cpu.h    |    1 +
 target-arm/helper.c |   16 +++++++---------
 2 files changed, 8 insertions(+), 9 deletions(-)
target-arm/cpu.h +1 -0
index b4500b4..3e5b860 100644
@@ -210,6 +210,7 @@ typedef struct CPUARMState {
         uint32_t c9_pmuserenr; /* perf monitor user enable */
         uint32_t c9_pminten; /* perf monitor interrupt enables */
         BANKED_CP_REG(uint32_t, c12_vbar); /* vector base address register */
+        uint32_t c12_mvbar; /* monitor vector base address register */
         BANKED_CP_REG(uint32_t, c13_fcse); /* FCSE PID.  */
         BANKED_CP_REG(uint32_t, c13_context); /* Context ID.  */
         BANKED_CP_REG(uint32_t, c13_tls1); /* User RW Thread register.  */
target-arm/helper.c +4 -1
index 7bfadb0..582de74 100644
@@ -1568,7 +1568,7 @@ static int scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
                       uint64_t value)
 {
-    env->cp15.c12_vbar = value & ~0x1Ful;
+    CPREG_FIELD32(env, ri) = value & ~0x1Ful;
     return 0;
 }
 
@@ -1589,6 +1589,9 @@ static const ARMCPRegInfo tz_cp_reginfo[] = {
       .access = PL1_RW, .type = ARM_CP_BANKED, .writefn = vbar_write,
       .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
       .resetvalue = 0 },
+    { .name = "MVBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 1,
+      .access = PL3_RW, .resetvalue = 0, .writefn = vbar_write,
+      .fieldoffset = offsetof(CPUARMState, cp15.c12_mvbar) },
     { .name = "SDER", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 1,
       .access = PL3_RW, .resetvalue = 0,
       .fieldoffset = offsetof(CPUARMState, cp15.c1_sder) },

target-arm/helper.c void arm_cpu_do_interrupt(CPUState *cs) · line 2635

Quoted 3 lines
target-arm/helper.c +0 -0
@@ -2630,17 +2633,12 @@ void arm_cpu_do_interrupt(CPUState *cs)
         return; /* Never happens.  Keep compiler happy.  */
     }
     /* High vectors.  */

Cut this comment ....

target-arm/helper.c void arm_cpu_do_interrupt(CPUState *cs) · line 2638

Quoted 4 lines
target-arm/helper.c +3 -1
-    if (env->cp15.c1_sys & (1 << 13)) {
+    if (new_mode == ARM_CPU_MODE_MON) {
+        addr += env->cp15.c12_mvbar;
+    } else if (env->cp15.c1_sys & (1 << 13)) {

and paste it back here. Your prepend of the monitor logic makes it stale in that location.

Regards,
Peter

Quoted 17 lines
target-arm/helper.c +0 -8
         /* when enabled, base address cannot be remapped.  */
         addr += 0xffff0000;
     } else {
-        /* ARM v7 architectures provide a vector base address register to remap
-         * the interrupt vector table.
-         * This register is only followed in non-monitor mode, and has a secure
-         * and un-secure copy. Since the cpu is always in a un-secure operation
-         * and is never in monitor mode this feature is always active.
-         * Note: only bits 31:5 are valid.
-         */
         addr += env->cp15.c12_vbar;
     }
     switch_mode (env, new_mode);
--

1.7.9.5

#60 lore raw

RFC PATCH 20/21 target-arm: implement SMC instruction

In reply to Sergey Fedorov

SMC instruction is implemented similar to SVC instruction. When executing SMC instruction from monitor CPU mode SCR.NS bit is reset.

---
 target-arm/cpu.h       |    1 +
 target-arm/helper.c    |   11 +++++++++++
 target-arm/translate.c |   37 +++++++++++++++++++++++++++----------
 3 files changed, 39 insertions(+), 10 deletions(-)
target-arm/cpu.h +1 -0
index 3e5b860..556d630 100644
@@ -49,6 +49,7 @@
 #define EXCP_EXCEPTION_EXIT  8   /* Return from v7M exception.  */
 #define EXCP_KERNEL_TRAP     9   /* Jumped to kernel code page.  */
 #define EXCP_STREX          10
+#define EXCP_SMC            11   /* secure monitor call */
 
 #define ARMV7M_EXCP_RESET   1
 #define ARMV7M_EXCP_NMI     2
target-arm/helper.c +11 -0
index 582de74..ba442c0 100644
@@ -2628,6 +2628,12 @@ void arm_cpu_do_interrupt(CPUState *cs)
         mask = CPSR_A | CPSR_I | CPSR_F;
         offset = 4;
         break;
+    case EXCP_SMC:
+        new_mode = ARM_CPU_MODE_MON;
+        addr = 0x08;
+        mask = CPSR_A | CPSR_I | CPSR_F;
+        offset = 0;
+        break;
     default:
         cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
         return; /* Never happens.  Keep compiler happy.  */
@@ -2641,6 +2647,11 @@ void arm_cpu_do_interrupt(CPUState *cs)
     } else {
         addr += env->cp15.c12_vbar;
     }
+
+    if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
+        env->cp15.c1_scr &= ~1 /* NS */;
+    }
+
     switch_mode (env, new_mode);
     env->spsr = cpsr_read(env);
     /* Clear IT bits.  */
target-arm/translate.c +27 -10
index 345866c..b81f09a 100644
@@ -66,6 +66,7 @@ static uint32_t gen_opc_condexec_bits[OPC_BUF_SIZE];
    conditional execution state has been updated.  */
 #define DISAS_WFI 4
 #define DISAS_SWI 5
+#define DISAS_SMC 6
 
 TCGv_ptr cpu_env;
 /* We reuse the same 64-bit temporaries for efficiency.  */
@@ -7105,15 +7106,21 @@ static void disas_arm_insn(CPUARMState * env, DisasContext *s)
             store_reg(s, rd, tmp);
             break;
         case 7:
-            /* SMC instruction (op1 == 3)
-               and undefined instructions (op1 == 0 || op1 == 2)
-               will trap */
-            if (op1 != 1) {
+            if (op1 == 1) {
+                /* bkpt */
+                ARCH(5);
+                gen_exception_insn(s, 4, EXCP_BKPT);
+            } else if (op1 == 3) {
+                /* smi/smc */
+                if (!arm_feature(env, ARM_FEATURE_TRUSTZONE) || IS_USER(s)) {
+                    goto illegal_op;
+                }
+                gen_set_pc_im(s, s->pc);
+                s->is_jmp = DISAS_SMC;
+                break;
+            } else {
                 goto illegal_op;
             }
-            /* bkpt */
-            ARCH(5);
-            gen_exception_insn(s, 4, EXCP_BKPT);
             break;
         case 0x8: /* signed multiply */
         case 0xa:
@@ -8885,9 +8892,12 @@ static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw
 
                 if (insn & (1 << 26)) {
                     /* Secure monitor call (v6Z) */
-                    qemu_log_mask(LOG_UNIMP,
-                                  "arm: unimplemented secure monitor call\n");
-                    goto illegal_op; /* not implemented.  */
+                    if (!arm_feature(env, ARM_FEATURE_TRUSTZONE) ||
+                            IS_USER(s)) {
+                        goto illegal_op;
+                    }
+                    gen_set_pc_im(s, s->pc);
+                    s->is_jmp = DISAS_SMC;
                 } else {
                     op = (insn >> 20) & 7;
                     switch (op) {
@@ -10284,6 +10294,8 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu,
             gen_set_condexec(dc);
             if (dc->is_jmp == DISAS_SWI) {
                 gen_exception(EXCP_SWI);
+            } else if (dc->is_jmp == DISAS_SMC) {
+                gen_exception(EXCP_SMC);
             } else {
                 gen_exception(EXCP_DEBUG);
             }
@@ -10296,6 +10308,8 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu,
         gen_set_condexec(dc);
         if (dc->is_jmp == DISAS_SWI && !dc->condjmp) {
             gen_exception(EXCP_SWI);
+        } else if (dc->is_jmp == DISAS_SMC && !dc->condjmp) {
+            gen_exception(EXCP_SMC);
         } else {
             /* FIXME: Single stepping a WFI insn will not halt
                the CPU.  */
@@ -10330,6 +10344,9 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu,
         case DISAS_SWI:
             gen_exception(EXCP_SWI);
             break;
+        case DISAS_SMC:
+            gen_exception(EXCP_SMC);
+            break;
         }
         if (dc->condjmp) {
             gen_set_label(dc->condlabel);
Signature · 1 line
1.7.9.5
#61 lore raw

RFC PATCH 21/21 target-arm: implement IRQ/FIQ routing to Monitor mode

In reply to Sergey Fedorov

SCR.{IRQ/FIQ} bits allows to route IRQ/FIQ exceptions to monitor CPU mode. When taking IRQ exception to monitor mode FIQ exception is additionally masked.

---
 target-arm/helper.c |    7 +++++++
 1 file changed, 7 insertions(+)
target-arm/helper.c +7 -0
index ba442c0..6472c09 100644
@@ -2620,12 +2620,19 @@ void arm_cpu_do_interrupt(CPUState *cs)
         /* Disable IRQ and imprecise data aborts.  */
         mask = CPSR_A | CPSR_I;
         offset = 4;
+        if (env->cp15.c1_scr & (1 << 1)) {
+            new_mode = ARM_CPU_MODE_MON;
+            mask |= CPSR_F;
+        }
         break;
     case EXCP_FIQ:
         new_mode = ARM_CPU_MODE_FIQ;
         addr = 0x1c;
         /* Disable FIQ, IRQ and imprecise data aborts.  */
         mask = CPSR_A | CPSR_I | CPSR_F;
+        if (env->cp15.c1_scr & (1 << 2)) {
+            new_mode = ARM_CPU_MODE_MON;
+        }
         offset = 4;
         break;
     case EXCP_SMC:
Signature · 1 line
1.7.9.5
#62 lore raw
In reply to Sergey Fedorov
Quoted 41 lines · Sergey Fedorov

This patch set implements a basic support of CPU core TrustZone feature. The following major functionalities are implemented:
* CPU monitor mode
* Separate code translation for each secure state
* CPACR & NSACR co-processor access control
* Separate TLB for each secure state
* Co-processor register banking
* SMC instruction
* FIQ/IRQ routing to monitor mode

There is no support for banked co-processor register migration, save/load its VM state yet. That is an open question how to implement this functionality. Any suggestions is greatly appreciated.

This patch set is a request for comments for the proof of concept.

Sergey Fedorov (18):
target-arm: move SCR & VBAR into TrustZone register list
target-arm: adjust TTBCR for TrustZone feature
target-arm: add arm_is_secure() helper
target-arm: reject switching to monitor mode from non-secure state
target-arm: adjust arm_current_pl() for TrustZone
target-arm: adjust SCR CP15 register access rights
target-arm: add non-secure Translation Block flag
target-arm: implement CPACR register logic
target-arm: add NSACR support
target-arm: add SDER definition
target-arm: split TLB for secure state
target-arm: add banked coprocessor register type
target-arm: convert appropriate coprocessor registers to banked type
target-arm: use c13_context field for CONTEXTIDR
target-arm: switch banked CP registers
target-arm: add MVBAR support
target-arm: implement SMC instruction
target-arm: implement IRQ/FIQ routing to Monitor mode

Svetlana Fedoseeva (3):
target-arm: add TrustZone CPU feature
target-arm: preserve RAO/WI bits of ARMv7 SCTLR
target-arm: add CPU Monitor mode

Quoted 8 lines
  target-arm/cpu.c       |    6 +-
  target-arm/cpu.h       |  126 ++++++++++++----
  target-arm/helper.c    |  308 +++++++++++++++++++++++++++++---------
  target-arm/machine.c   |   12 +-
  target-arm/translate.c |  388 ++++++++++++++++++++++++++++++------------------
  target-arm/translate.h |    2 +
  6 files changed, 585 insertions(+), 257 deletions(-)

We'd like this patch series finally to be merged into mainstream. What should be done to achieve this goal?

Best regards,
Sergey Fedorov

#63 lore raw

Hi Sergey,

Quoted 56 lines · Fedorov Sergey
Quoted 45 lines · Sergey Fedorov

This patch set implements a basic support of CPU core TrustZone feature. The
following major functionalities are implemented:
* CPU monitor mode
* Separate code translation for each secure state
* CPACR & NSACR co-processor access control
* Separate TLB for each secure state
* Co-processor register banking
* SMC instruction
* FIQ/IRQ routing to monitor mode

There is no support for banked co-processor register migration, save/load its
VM state yet. That is an open question how to implement this functionality. Any
suggestions is greatly appreciated.

This patch set is a request for comments for the proof of concept.

Sergey Fedorov (18):
target-arm: move SCR & VBAR into TrustZone register list
target-arm: adjust TTBCR for TrustZone feature
target-arm: add arm_is_secure() helper
target-arm: reject switching to monitor mode from non-secure state
target-arm: adjust arm_current_pl() for TrustZone
target-arm: adjust SCR CP15 register access rights
target-arm: add non-secure Translation Block flag
target-arm: implement CPACR register logic
target-arm: add NSACR support
target-arm: add SDER definition
target-arm: split TLB for secure state
target-arm: add banked coprocessor register type
target-arm: convert appropriate coprocessor registers to banked type
target-arm: use c13_context field for CONTEXTIDR
target-arm: switch banked CP registers
target-arm: add MVBAR support
target-arm: implement SMC instruction
target-arm: implement IRQ/FIQ routing to Monitor mode

Svetlana Fedoseeva (3):
target-arm: add TrustZone CPU feature
target-arm: preserve RAO/WI bits of ARMv7 SCTLR
target-arm: add CPU Monitor mode

Quoted 9 lines
  target-arm/cpu.c       |    6 +-
  target-arm/cpu.h       |  126 ++++++++++++----
  target-arm/helper.c    |  308 +++++++++++++++++++++++++++++---------
  target-arm/machine.c   |   12 +-
  target-arm/translate.c |  388
++++++++++++++++++++++++++++++------------------
  target-arm/translate.h |    2 +
  6 files changed, 585 insertions(+), 257 deletions(-)
Quoted 3 lines

We'd like this patch series finally to be merged into mainstream. What should be done to achieve this goal?

Send patch series' just like this and either action or challenge reviewer comments (as you have already done). Next time, you can drop the RFC, that means you intend the series for review+merge. If you get no reply after a week ping the series.

I'm about half way through the series on my first pass. And I guess PMM has had a look as he has commented on one my comments.

A 21 patch series with +500/-250 is long, and queue maintainers are generally hesitant to take partial series (as its often contrary to author intention). Things generally go faster with smaller series. You have some low-complexity cleanup type patches that dont add new features than you could potentially send as a smaller initial series to get upstream quickly, then take more time on the harder stuff as follow up series with reduced patch count.

Regards,
Peter

Quoted 3 lines

Best regards,
Sergey Fedorov

#64 lore raw
In reply to Fedorov Sergey
Quoted 6 lines · Fedorov Sergey
Quoted 2 lines · Sergey Fedorov

This patch set implements a basic support of CPU core TrustZone feature.

We'd like this patch series finally to be merged into mainstream. What should be done to achieve this goal?

I'd like to see TZ support in mainline too. The high level answer is that it needs to get code reviewed, and you need to fix issues that are raised in code review. Unfortunately my review queue is currently pretty full (it has Allwinner board support, DIGIC board support, a bunch of Cadence fixes, ARMv8 32 bit new instructions and the A64 64 bit instruction support in it, all of which are fairly big patchsets), so it may take me a little while to get to this patchset. It is on my todo list though, so it won't get forgotten :-)

thanks
-- PMM

#65 lore raw
Quoted 6 lines · Peter Maydell
Quoted 4 lines · Fedorov Sergey
Quoted 1 line · Sergey Fedorov

This patch set implements a basic support of CPU core TrustZone feature.

We'd like this patch series finally to be merged into mainstream. What should be done to achieve this goal?

I'd like to see TZ support in mainline too.

That is the most important for me now :-) So I will try these patches to get shape suitable for mainline. Thanks!

Quoted 9 lines

The high level answer
is that it needs to get code reviewed, and you need to fix issues that are raised in code review. Unfortunately my review queue is currently pretty full (it has Allwinner board support, DIGIC board support, a bunch of Cadence fixes, ARMv8 32 bit new instructions and the A64 64 bit instruction support in it, all of which are fairly big patchsets), so it may take me a little while to get to this patchset. It is on my todo list though, so it won't get forgotten :-)

Quoted 3 lines

thanks
-- PMM

Best regards,
Sergey Fedorov

#66 lore raw
Quoted 9 lines · Fedorov Sergey
Quoted 7 lines · Peter Maydell
Quoted 5 lines · Fedorov Sergey
Quoted 2 lines · Sergey Fedorov

This patch set implements a basic support of CPU core TrustZone feature.

Quoted 13 lines
Quoted 4 lines
Quoted 2 lines

We'd like this patch series finally to be merged into mainstream. What should be done to achieve this goal?

I'd like to see TZ support in mainline too.

That is the most important for me now :-) So I will try these patches to get shape suitable for mainline. Thanks!

Quoted 3 lines

The high level answer
is that it needs to get code reviewed, and you need to fix issues that are raised in code review.

I'm done with first round review here - sorry about the delay. Mostly trivials but two bigger points:

1: The name "Trustzone" does not match ARM doco which uses "security extensions" 2: I think that banking scheme where you maintain active copies is a bit awkward.

Regards,
Peter

Quoted 17 lines
Quoted 10 lines

Unfortunately my review queue is
currently pretty full (it has Allwinner board support, DIGIC board support, a bunch of Cadence fixes, ARMv8 32 bit new instructions and the A64 64 bit instruction support in it, all of which are fairly big patchsets), so it may take me a little while to get to this patchset. It is on my todo list though, so it won't get forgotten :-)

Quoted 3 lines

thanks
-- PMM

Best regards,
Sergey Fedorov