Skip to contentSkip to conversation

arm: enable TZ support for the GIC

PATCH Superseded Peter Maydell 2 participants 14 messages Jun 30, 2015 – Jul 7, 2015

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover arm: enable TZ support for the GIC

This patchset enables the TZ support in the GIC for the systems where we enable TZ support in the CPU. In practice that means just the "virt" and "vexpress" boards, since all the others disable the CPU TZ support.

Patch 1 I've already put out on list as a bugfix targeting 2.4.

The main interesting part in this is that we need some special handling for the case where both the CPU and the GIC have TZ support but we want to directly boot a kernel in NonSecure (this only happens with the "virt" board). In this case our hw/arm/boot.c minimalist bootloader needs to do the work of configuring the GIC that would normally be done by Secure firmware before it started the NS kernel. I've opted to do this by adding a property on the GIC which says "reset yourself as if you'd been configured by firmware to make all the IRQs NS", and then set that property from boot.c if appropriate.

The other possible approach would be to have the code that boot.c runs in the guest do this reconfiguration, but that would be awkward because we'd need to start both the primary and secondary CPUs in Secure and have them do the step-down-to-NS themselves after messing with the GIC. If people strongly prefer that I can have a go at it, but I'd really rather not :-)

Andreas, I cc'd you in case you wanted to have a look at whether I got the property-which-can-be-set-after-realize right (patch 2).

thanks
-- PMM

Peter Maydell (5):
hw/intc/arm_gic_common.c: Reset all registers hw/intc/arm_gic_common: Provide property to make IRQs reset as
NonSecure
hw/arm/boot: Configure secure GIC to make IRQs NS if booting an NS
kernel
hw/cpu/{a15mpcore,a9mpcore}: enable TrustZone in GIC if it is enabled
in CPUs
hw/arm/virt: Enable TZ extensions on the GIC if we are using them

 hw/arm/boot.c                    | 39 ++++++++++++++++++++++
 hw/arm/virt.c                    |  7 ++--
 hw/cpu/a15mpcore.c               | 13 ++++++++
 hw/cpu/a9mpcore.c                | 11 ++++++
 hw/intc/arm_gic_common.c         | 72 +++++++++++++++++++++++++++++++++++++---
 include/hw/intc/arm_gic_common.h |  1 +
 6 files changed, 137 insertions(+), 6 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/5 hw/intc/arm_gic_common.c: Reset all registers

The arm_gic_common reset function was missing reset code for several of the GIC's state fields:
* bpr[]
* abpr[]
* priority1[]
* priority2[]
* sgi_pending[]
* irq_target[] (SMP configurations only)

These probably went unnoticed because most guests will either never touch them, or will write to them in the process of configuring the GIC before enabling interrupts.

---
 hw/intc/arm_gic_common.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
hw/intc/arm_gic_common.c +18 -3
index 044ad66..a64d071 100644
@@ -123,7 +123,7 @@ static void arm_gic_common_realize(DeviceState *dev, Error **errp)
 static void arm_gic_common_reset(DeviceState *dev)
 {
     GICState *s = ARM_GIC_COMMON(dev);
-    int i;
+    int i, j;
     memset(s->irq_state, 0, GIC_MAXIRQ * sizeof(gic_irq_state));
     for (i = 0 ; i < s->num_cpu; i++) {
         if (s->revision == REV_11MPCORE) {
@@ -135,15 +135,30 @@ static void arm_gic_common_reset(DeviceState *dev)
         s->running_irq[i] = 1023;
         s->running_priority[i] = 0x100;
         s->cpu_ctlr[i] = 0;
+        s->bpr[i] = GIC_MIN_BPR;
+        s->abpr[i] = GIC_MIN_ABPR;
+        for (j = 0; j < GIC_INTERNAL; j++) {
+            s->priority1[j][i] = 0;
+        }
+        for (j = 0; j < GIC_NR_SGIS; j++) {
+            s->sgi_pending[j][i] = 0;
+        }
     }
     for (i = 0; i < GIC_NR_SGIS; i++) {
         GIC_SET_ENABLED(i, ALL_CPU_MASK);
         GIC_SET_EDGE_TRIGGER(i);
     }
-    if (s->num_cpu == 1) {
+
+    for (i = 0; i < ARRAY_SIZE(s->priority2); i++) {
+        s->priority2[i] = 0;
+    }
+
+    for (i = 0; i < GIC_MAXIRQ; i++) {
         /* For uniprocessor GICs all interrupts always target the sole CPU */
-        for (i = 0; i < GIC_MAXIRQ; i++) {
+        if (s->num_cpu == 1) {
             s->irq_target[i] = 1;
+        } else {
+            s->irq_target[i] = 0;
         }
     }
     s->ctlr = 0;
Signature · 1 line
1.9.1
#2 lore raw

PATCH 2/5 hw/intc/arm_gic_common: Provide property to make IRQs reset as NonSecure

In reply to Peter Maydell

Implement a property which causes the GIC to reset with interrupts configured into group 1 (NonSecure) rather than the usual group 0, and with their initial priority set to the highest NonSecure priority rather than the usual highest Secure priority.

This is necessary for the situation where we directly boot a kernel in NonSecure on a system where the GIC supports the security extensions:
otherwise the kernel will be unable to access any of its interrupts.

The property is writable after realize, because we won't know whether we need to set it until after machine creation is complete. If the setting is changed it will take effect at the next reset.

---
 hw/intc/arm_gic_common.c         | 55 +++++++++++++++++++++++++++++++++++++---
 include/hw/intc/arm_gic_common.h |  1 +
 2 files changed, 53 insertions(+), 3 deletions(-)
hw/intc/arm_gic_common.c +52 -3
index a64d071..bc8b9c9 100644
@@ -84,6 +84,33 @@ static const VMStateDescription vmstate_gic = {
     }
 };
 
+static bool arm_gic_irq_reset_nonsecure_get(Object *obj, Error **errp)
+{
+    GICState *s = ARM_GIC_COMMON(obj);
+
+    return s->irq_reset_nonsecure;
+}
+
+static void arm_gic_irq_reset_nonsecure_set(Object *obj, bool val, Error **errp)
+{
+    GICState *s = ARM_GIC_COMMON(obj);
+
+    s->irq_reset_nonsecure = val;
+}
+
+static void arm_gic_common_init(Object *obj)
+{
+    /* This property should be set true if the GIC should configure
+     * IRQs as group 1 (NS) on reset, rather than the usual group 0.
+     * It is essentially a standard bool property, except that it can be
+     * set after realize (and will take effect at the next reset).
+     */
+    object_property_add_bool(obj, "irqs-reset-nonsecure",
+                             arm_gic_irq_reset_nonsecure_get,
+                             arm_gic_irq_reset_nonsecure_set,
+                             &error_abort);
+}
+
 static void arm_gic_common_realize(DeviceState *dev, Error **errp)
 {
     GICState *s = ARM_GIC_COMMON(dev);
@@ -124,12 +151,27 @@ static void arm_gic_common_reset(DeviceState *dev)
 {
     GICState *s = ARM_GIC_COMMON(dev);
     int i, j;
+    int resetprio;
+
+    /* If we're resetting a TZ-aware GIC as if secure firmware
+     * had set it up ready to start a kernel in non-secure,
+     * we need to set interrupt priorities to a "zero for the
+     * NS view" value. This is particularly critical for the
+     * priority_mask[] values, because if they are zero then NS
+     * code cannot ever rewrite the priority to anything else.
+     */
+    if (s->security_extn && s->irq_reset_nonsecure) {
+        resetprio = 0x80;
+    } else {
+        resetprio = 0;
+    }
+
     memset(s->irq_state, 0, GIC_MAXIRQ * sizeof(gic_irq_state));
     for (i = 0 ; i < s->num_cpu; i++) {
         if (s->revision == REV_11MPCORE) {
             s->priority_mask[i] = 0xf0;
         } else {
-            s->priority_mask[i] = 0;
+            s->priority_mask[i] = resetprio;
         }
         s->current_pending[i] = 1023;
         s->running_irq[i] = 1023;
@@ -138,7 +180,7 @@ static void arm_gic_common_reset(DeviceState *dev)
         s->bpr[i] = GIC_MIN_BPR;
         s->abpr[i] = GIC_MIN_ABPR;
         for (j = 0; j < GIC_INTERNAL; j++) {
-            s->priority1[j][i] = 0;
+            s->priority1[j][i] = resetprio;
         }
         for (j = 0; j < GIC_NR_SGIS; j++) {
             s->sgi_pending[j][i] = 0;
@@ -150,7 +192,7 @@ static void arm_gic_common_reset(DeviceState *dev)
     }
 
     for (i = 0; i < ARRAY_SIZE(s->priority2); i++) {
-        s->priority2[i] = 0;
+        s->priority2[i] = resetprio;
     }
 
     for (i = 0; i < GIC_MAXIRQ; i++) {
@@ -161,6 +203,12 @@ static void arm_gic_common_reset(DeviceState *dev)
             s->irq_target[i] = 0;
         }
     }
+    if (s->security_extn && s->irq_reset_nonsecure) {
+        for (i = 0; i < GIC_MAXIRQ; i++) {
+            GIC_SET_GROUP(i, ALL_CPU_MASK);
+        }
+    }
+
     s->ctlr = 0;
 }
 
@@ -191,6 +239,7 @@ static const TypeInfo arm_gic_common_type = {
     .name = TYPE_ARM_GIC_COMMON,
     .parent = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(GICState),
+    .instance_init = arm_gic_common_init,
     .class_size = sizeof(ARMGICCommonClass),
     .class_init = arm_gic_common_class_init,
     .abstract = true,
include/hw/intc/arm_gic_common.h +1 -0
index 899db3d..cfc1cce 100644
@@ -118,6 +118,7 @@ typedef struct GICState {
     uint32_t num_irq;
     uint32_t revision;
     bool security_extn;
+    bool irq_reset_nonsecure; /* configure IRQs as group 1 (NS) on reset? */
     int dev_fd; /* kvm device fd if backed by kvm vgic support */
 } GICState;
 
Signature · 1 line
1.9.1
#3 lore raw

PATCH 3/5 hw/arm/boot: Configure secure GIC to make IRQs NS if booting an NS kernel

In reply to Peter Maydell

If our builtin kernel bootloader is directly booting a kernel in the NonSecure world, then we must configure the GIC to put all the IRQs into the NonSecure group. (By default all interrupts are configured to be Secure on reset, which means that a NonSecure guest kernel cannot use any of them.) This job would usually be done by the Secure boot firmware, but our builtin bootloader is doing the job of firmware.

---
 hw/arm/boot.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
hw/arm/boot.c +39 -0
index 1e7fd28..3974499 100644
@@ -13,6 +13,7 @@
 #include "sysemu/sysemu.h"
 #include "hw/boards.h"
 #include "hw/loader.h"
+#include "hw/intc/arm_gic_common.h"
 #include "elf.h"
 #include "sysemu/device_tree.h"
 #include "qemu/config-file.h"
@@ -557,6 +558,33 @@ static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
 }
 
+static int find_gics(Object *obj, void *opaque)
+{
+    GICState *gic = (GICState *)object_dynamic_cast(obj, TYPE_ARM_GIC_COMMON);
+    bool has_sec_extns;
+
+    if (!gic) {
+        /* Might be a container, traverse it for children */
+        return object_child_foreach(obj, find_gics, opaque);
+    }
+
+    has_sec_extns = object_property_get_bool(obj, "has-security-extensions",
+                                             &error_abort);
+    if (has_sec_extns) {
+        object_property_set_bool(obj, true, "irqs-reset-nonsecure",
+                                 &error_abort);
+    }
+    return 0;
+}
+
+static void reconfigure_gics_nonsecure(void)
+{
+    /* Find every GIC in the system and tell it to reconfigure
+     * itself with interrupts as NonSecure.
+     */
+    object_child_foreach(qdev_get_machine(), find_gics, NULL);
+}
+
 static void arm_load_kernel_notify(Notifier *notifier, void *data)
 {
     CPUState *cs;
@@ -767,6 +795,17 @@ static void arm_load_kernel_notify(Notifier *notifier, void *data)
     }
     info->is_linux = is_linux;
 
+    if (is_linux && arm_feature(&cpu->env, ARM_FEATURE_EL3) &&
+        !info->secure_boot) {
+        /* We're directly booting a kernel into NonSecure. If the system
+         * has a GIC which implements the security extensions then we must
+         * configure it to have all the interrupts be NonSecure (this is
+         * a job that is done by the Secure boot firmware, and boot.c is
+         * a minimalist firmware-and-boot-loader equivalent).
+         */
+        reconfigure_gics_nonsecure();
+    }
+
     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
         ARM_CPU(cs)->env.boot_info = info;
     }
Signature · 1 line
1.9.1
#4 lore raw

Re: [Qemu-devel] [PATCH 3/5] hw/arm/boot: Configure secure GIC to make IRQs NS if booting an NS kernel

Quoted 28 lines · Peter Maydell

If our builtin kernel bootloader is directly booting a kernel in the NonSecure world, then we must configure the GIC to put all the IRQs into the NonSecure group. (By default all interrupts are configured to be Secure on reset, which means that a NonSecure guest kernel cannot use any of them.) This job would usually be done by the Secure boot firmware, but our builtin bootloader is doing the job of firmware.

---
 hw/arm/boot.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
hw/arm/boot.c +1 -0
index 1e7fd28..3974499 100644
@@ -13,6 +13,7 @@
 #include "sysemu/sysemu.h"
 #include "hw/boards.h"
 #include "hw/loader.h"
+#include "hw/intc/arm_gic_common.h"
 #include "elf.h"
 #include "sysemu/device_tree.h"
 #include "qemu/config-file.h"
@@ -557,6 +558,33 @@ static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
 }

hw/arm/boot.c static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key, · line 569

Quoted 10 lines
hw/arm/boot.c +9 -0
 
+static int find_gics(Object *obj, void *opaque)
+{
+    GICState *gic = (GICState *)object_dynamic_cast(obj, TYPE_ARM_GIC_COMMON);
+    bool has_sec_extns;
+
+    if (!gic) {
+        /* Might be a container, traverse it for children */
+        return object_child_foreach(obj, find_gics, opaque);
+    }

This need to traverse the whole tree has come up more than once for me, so I think this should be a core feature of QOM.

Quoted 22 lines
hw/arm/boot.c +18 -0
+
+    has_sec_extns = object_property_get_bool(obj, "has-security-extensions",
+                                             &error_abort);
+    if (has_sec_extns) {
+        object_property_set_bool(obj, true, "irqs-reset-nonsecure",
+                                 &error_abort);
+    }
+    return 0;
+}
+
+static void reconfigure_gics_nonsecure(void)
+{
+    /* Find every GIC in the system and tell it to reconfigure
+     * itself with interrupts as NonSecure.
+     */
+    object_child_foreach(qdev_get_machine(), find_gics, NULL);
+}
+
 static void arm_load_kernel_notify(Notifier *notifier, void *data)
 {
     CPUState *cs;

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data) · line 798

Quoted 4 lines
hw/arm/boot.c +1 -0
@@ -767,6 +795,17 @@ static void arm_load_kernel_notify(Notifier *notifier, void *data)
     }
     info->is_linux = is_linux;
 
+    if (is_linux && arm_feature(&cpu->env, ARM_FEATURE_EL3) &&

Do we need to conditional on ARM_FEATURE_EL3 or can we make GIC logic independent of the primary CPU?

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data) · line 805

Quoted 7 lines
hw/arm/boot.c +7 -0
+        !info->secure_boot) {
+        /* We're directly booting a kernel into NonSecure. If the system
+         * has a GIC which implements the security extensions then we must
+         * configure it to have all the interrupts be NonSecure (this is
+         * a job that is done by the Secure boot firmware, and boot.c is
+         * a minimalist firmware-and-boot-loader equivalent).
+         */

So I actually had my own patches for this one that went in a different direction. The reason is, there are also other devs out there which need post-firmware state setup. The one I an thinking of mainly is the Zynq SLCR setup for non-firmware boots, I.E. the Linux kernel expects firmware to setup devices to some sort of initialized state (mainly turning clocks on). I think this GIC setup falls in the same category. The third use case is the GIC_CPU_IF stuff currently managed by machine code in boot.c that could be outsourced to GIC (in either a similar way to what is done here, or more fully outsourced using my new API).

So with these three use cases of initing devices in post-firmware state, I created a little interface to allow devices to generically specify their post linux state. Patches on list overnight. I'll write more in the cover.

Regards,
Peter

Quoted 10 lines
hw/arm/boot.c +3 -1
+        reconfigure_gics_nonsecure();
+    }
+
     for (cs = CPU(cpu); cs; cs = CPU_NEXT(cs)) {
         ARM_CPU(cs)->env.boot_info = info;
     }
--

1.9.1

#5 lore raw

Re: [Qemu-devel] [PATCH 3/5] hw/arm/boot: Configure secure GIC to make IRQs NS if booting an NS kernel

Quoted 30 lines · Peter Crosthwaite
Quoted 29 lines · Peter Maydell

If our builtin kernel bootloader is directly booting a kernel in the NonSecure world, then we must configure the GIC to put all the IRQs into the NonSecure group. (By default all interrupts are configured to be Secure on reset, which means that a NonSecure guest kernel cannot use any of them.) This job would usually be done by the Secure boot firmware, but our builtin bootloader is doing the job of firmware.

---
 hw/arm/boot.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
hw/arm/boot.c +1 -0
index 1e7fd28..3974499 100644
@@ -13,6 +13,7 @@
 #include "sysemu/sysemu.h"
 #include "hw/boards.h"
 #include "hw/loader.h"
+#include "hw/intc/arm_gic_common.h"
 #include "elf.h"
 #include "sysemu/device_tree.h"
 #include "qemu/config-file.h"
@@ -557,6 +558,33 @@ static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
 }
 
Quoted 12 lines

hw/arm/boot.c static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key, · line 569

Quoted 9 lines
hw/arm/boot.c +9 -0
+static int find_gics(Object *obj, void *opaque)
+{
+    GICState *gic = (GICState *)object_dynamic_cast(obj, TYPE_ARM_GIC_COMMON);
+    bool has_sec_extns;
+
+    if (!gic) {
+        /* Might be a container, traverse it for children */
+        return object_child_foreach(obj, find_gics, opaque);
+    }

This need to traverse the whole tree has come up more than once for me, so I think this should be a core feature of QOM.

I did think about that, yeah. I guess something like object_descendants_foreach(), same signature as object_child_foreach(), same semantics except it also iterates through the whole tree (and calls the callback for the nodes-with-children as well as the leaves) ?

Quoted 25 lines
Quoted 25 lines
hw/arm/boot.c +18 -0
+
+    has_sec_extns = object_property_get_bool(obj, "has-security-extensions",
+                                             &error_abort);
+    if (has_sec_extns) {
+        object_property_set_bool(obj, true, "irqs-reset-nonsecure",
+                                 &error_abort);
+    }
+    return 0;
+}
+
+static void reconfigure_gics_nonsecure(void)
+{
+    /* Find every GIC in the system and tell it to reconfigure
+     * itself with interrupts as NonSecure.
+     */
+    object_child_foreach(qdev_get_machine(), find_gics, NULL);
+}
+
 static void arm_load_kernel_notify(Notifier *notifier, void *data)
 {
     CPUState *cs;
@@ -767,6 +795,17 @@ static void arm_load_kernel_notify(Notifier *notifier, void *data)
     }
     info->is_linux = is_linux;
 
Quoted 4 lines

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data) · line 798

Quoted 1 line
hw/arm/boot.c +1 -0
+    if (is_linux && arm_feature(&cpu->env, ARM_FEATURE_EL3) &&

Do we need to conditional on ARM_FEATURE_EL3 or can we make GIC logic independent of the primary CPU?

The point here is that "do we need to do this" is exactly dependent on what we're doing with the CPU. Only if we want to put the guest into NS do we do this, and the condition for "are we going to put the guest into NS" is "is this a Linux boot on a CPU with EL3 but where the board says don't boot in S". It matches what the existing logic does for when it sets the SCR_NS bit in do_cpu_reset() in this file.

Quoted 8 lines

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data) · line 805

Quoted 7 lines
hw/arm/boot.c +7 -0
+        !info->secure_boot) {
+        /* We're directly booting a kernel into NonSecure. If the system
+         * has a GIC which implements the security extensions then we must
+         * configure it to have all the interrupts be NonSecure (this is
+         * a job that is done by the Secure boot firmware, and boot.c is
+         * a minimalist firmware-and-boot-loader equivalent).
+         */
Quoted 10 lines

So I actually had my own patches for this one that went in a different direction. The reason is, there are also other devs out there which need post-firmware state setup. The one I an thinking of mainly is the Zynq SLCR setup for non-firmware boots, I.E. the Linux kernel expects firmware to setup devices to some sort of initialized state (mainly turning clocks on). I think this GIC setup falls in the same category. The third use case is the GIC_CPU_IF stuff currently managed by machine code in boot.c that could be outsourced to GIC (in either a similar way to what is done here, or more fully outsourced using my new API).

I'm a bit torn here -- I don't want to make it *too* easy for people to add linux-booting specific code to random devices, as this will lead to the bootloader code having its tentacles everywhere within the tree...

thanks
-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH 3/5] hw/arm/boot: Configure secure GIC to make IRQs NS if booting an NS kernel

Quoted 81 lines · Peter Maydell
Quoted 30 lines · Peter Crosthwaite
Quoted 29 lines · Peter Maydell

If our builtin kernel bootloader is directly booting a kernel in the NonSecure world, then we must configure the GIC to put all the IRQs into the NonSecure group. (By default all interrupts are configured to be Secure on reset, which means that a NonSecure guest kernel cannot use any of them.) This job would usually be done by the Secure boot firmware, but our builtin bootloader is doing the job of firmware.

---
 hw/arm/boot.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
hw/arm/boot.c +1 -0
index 1e7fd28..3974499 100644
@@ -13,6 +13,7 @@
 #include "sysemu/sysemu.h"
 #include "hw/boards.h"
 #include "hw/loader.h"
+#include "hw/intc/arm_gic_common.h"
 #include "elf.h"
 #include "sysemu/device_tree.h"
 #include "qemu/config-file.h"
@@ -557,6 +558,33 @@ static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
 }
 
Quoted 12 lines

hw/arm/boot.c static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key, · line 569

Quoted 9 lines
hw/arm/boot.c +9 -0
+static int find_gics(Object *obj, void *opaque)
+{
+    GICState *gic = (GICState *)object_dynamic_cast(obj, TYPE_ARM_GIC_COMMON);
+    bool has_sec_extns;
+
+    if (!gic) {
+        /* Might be a container, traverse it for children */
+        return object_child_foreach(obj, find_gics, opaque);
+    }

This need to traverse the whole tree has come up more than once for me, so I think this should be a core feature of QOM.

I did think about that, yeah. I guess something like object_descendants_foreach(), same signature as object_child_foreach(), same semantics except it also iterates through the whole tree (and calls the callback for the nodes-with-children as well as the leaves) ?

Quoted 25 lines
Quoted 25 lines
hw/arm/boot.c +18 -0
+
+    has_sec_extns = object_property_get_bool(obj, "has-security-extensions",
+                                             &error_abort);
+    if (has_sec_extns) {
+        object_property_set_bool(obj, true, "irqs-reset-nonsecure",
+                                 &error_abort);
+    }
+    return 0;
+}
+
+static void reconfigure_gics_nonsecure(void)
+{
+    /* Find every GIC in the system and tell it to reconfigure
+     * itself with interrupts as NonSecure.
+     */
+    object_child_foreach(qdev_get_machine(), find_gics, NULL);
+}
+
 static void arm_load_kernel_notify(Notifier *notifier, void *data)
 {
     CPUState *cs;
@@ -767,6 +795,17 @@ static void arm_load_kernel_notify(Notifier *notifier, void *data)
     }
     info->is_linux = is_linux;
 
Quoted 4 lines

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data) · line 798

Quoted 1 line
hw/arm/boot.c +1 -0
+    if (is_linux && arm_feature(&cpu->env, ARM_FEATURE_EL3) &&

Do we need to conditional on ARM_FEATURE_EL3 or can we make GIC logic independent of the primary CPU?

Quoted 9 lines

The point here is that "do we need to do this" is exactly dependent on what we're doing with the CPU. Only if we want to put the guest into NS do we do this, and the condition for "are we going to put the guest into NS" is "is this a Linux boot on a CPU with EL3 but where the board says don't boot in S". It matches what the existing logic does for when it sets the SCR_NS bit in do_cpu_reset() in this file.

Then maybe this belongs on the lowest common denominator for GIC and CPU - the SoC level. SoCs can register a linux_init function that checks the CPU and GIC NS support and does the switchup. Can make it a common function that multiple socs/machines can call (virt, vexpress and zynq-mp so far I think). For cases where the linux_init in genuinely self contained (like my zynq SLCR case), the device can register it.

Quoted 19 lines
Quoted 8 lines

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data) · line 805

Quoted 7 lines
hw/arm/boot.c +7 -0
+        !info->secure_boot) {
+        /* We're directly booting a kernel into NonSecure. If the system
+         * has a GIC which implements the security extensions then we must
+         * configure it to have all the interrupts be NonSecure (this is
+         * a job that is done by the Secure boot firmware, and boot.c is
+         * a minimalist firmware-and-boot-loader equivalent).
+         */
Quoted 10 lines

So I actually had my own patches for this one that went in a different direction. The reason is, there are also other devs out there which need post-firmware state setup. The one I an thinking of mainly is the Zynq SLCR setup for non-firmware boots, I.E. the Linux kernel expects firmware to setup devices to some sort of initialized state (mainly turning clocks on). I think this GIC setup falls in the same category. The third use case is the GIC_CPU_IF stuff currently managed by machine code in boot.c that could be outsourced to GIC (in either a similar way to what is done here, or more fully outsourced using my new API).

Quoted 5 lines

I'm a bit torn here -- I don't want to make it *too* easy for people to add linux-booting specific code to random devices, as this will lead to the bootloader code having its tentacles everywhere within the tree...

Maybe the compromise to to restrict this API to SoCs and machines and I can handle my SLCR case with a single "post-firmware" boolean property?

Regards,
Peter

Quoted 3 lines

thanks
-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH 3/5] hw/arm/boot: Configure secure GIC to make IRQs NS if booting an NS kernel

Quoted 12 lines · Peter Crosthwaite
Quoted 9 lines · Peter Maydell

The point here is that "do we need to do this" is exactly dependent on what we're doing with the CPU. Only if we want to put the guest into NS do we do this, and the condition for "are we going to put the guest into NS" is "is this a Linux boot on a CPU with EL3 but where the board says don't boot in S". It matches what the existing logic does for when it sets the SCR_NS bit in do_cpu_reset() in this file.

Quoted 3 lines

Then maybe this belongs on the lowest common denominator for GIC and CPU - the SoC level. SoCs can register a linux_init function that checks the CPU and GIC NS support and does the switchup.

The one board we have so far that needs this code doesn't have an SoC level object -- virt just creates the CPU and GIC itself...

-- PMM

#8 lore raw

Re: [Qemu-devel] [PATCH 3/5] hw/arm/boot: Configure secure GIC to make IRQs NS if booting an NS kernel

Quoted 14 lines · Peter Maydell
Quoted 12 lines · Peter Crosthwaite
Quoted 9 lines · Peter Maydell

The point here is that "do we need to do this" is exactly dependent on what we're doing with the CPU. Only if we want to put the guest into NS do we do this, and the condition for "are we going to put the guest into NS" is "is this a Linux boot on a CPU with EL3 but where the board says don't boot in S". It matches what the existing logic does for when it sets the SCR_NS bit in do_cpu_reset() in this file.

Quoted 7 lines
Quoted 3 lines

Then maybe this belongs on the lowest common denominator for GIC and CPU - the SoC level. SoCs can register a linux_init function that checks the CPU and GIC NS support and does the switchup.

The one board we have so far that needs this code doesn't have an SoC level object -- virt just creates the CPU and GIC itself...

Right, but machines are QOMified objects now, so they should be able to implement this interface.

zynqmp SoC needs this feature for when we switch on EL3.

Regards,
Peter

Quoted 3 lines

-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH 3/5] hw/arm/boot: Configure secure GIC to make IRQs NS if booting an NS kernel

In reply to Peter Crosthwaite
Quoted 15 lines · Peter Crosthwaite
Quoted 13 lines · Peter Maydell
Quoted 10 lines · Peter Crosthwaite

So I actually had my own patches for this one that went in a different direction. The reason is, there are also other devs out there which need post-firmware state setup. The one I an thinking of mainly is the Zynq SLCR setup for non-firmware boots, I.E. the Linux kernel expects firmware to setup devices to some sort of initialized state (mainly turning clocks on). I think this GIC setup falls in the same category. The third use case is the GIC_CPU_IF stuff currently managed by machine code in boot.c that could be outsourced to GIC (in either a similar way to what is done here, or more fully outsourced using my new API).

Quoted 4 lines
Quoted 4 lines

I'm a bit torn here -- I don't want to make it *too* easy for people to add linux-booting specific code to random devices, as this will lead to the bootloader code having its tentacles everywhere within the tree...

So I thought about this a bit, and I guess that having a general interface is probably better than specifically setting a GIC property. It does need to be called once at arm_kernel_load_notify time, not as a reset hook. I also think it should pass in the arm_boot_info* as a parameter. This would let the GIC do the right thing based on whether we're booting S or NS.

Are you planning to respin this patchset, or should I just pull the relevant bits into my series?

thanks
-- PMM

#10 lore raw

Re: [Qemu-devel] [PATCH 3/5] hw/arm/boot: Configure secure GIC to make IRQs NS if booting an NS kernel

Quoted 29 lines · Peter Maydell
Quoted 15 lines · Peter Crosthwaite
Quoted 13 lines · Peter Maydell
Quoted 10 lines · Peter Crosthwaite

So I actually had my own patches for this one that went in a different direction. The reason is, there are also other devs out there which need post-firmware state setup. The one I an thinking of mainly is the Zynq SLCR setup for non-firmware boots, I.E. the Linux kernel expects firmware to setup devices to some sort of initialized state (mainly turning clocks on). I think this GIC setup falls in the same category. The third use case is the GIC_CPU_IF stuff currently managed by machine code in boot.c that could be outsourced to GIC (in either a similar way to what is done here, or more fully outsourced using my new API).

Quoted 4 lines
Quoted 4 lines

I'm a bit torn here -- I don't want to make it *too* easy for people to add linux-booting specific code to random devices, as this will lead to the bootloader code having its tentacles everywhere within the tree...

So I thought about this a bit, and I guess that having a general interface is probably better than specifically setting a GIC property. It does need to be called once at arm_kernel_load_notify time, not as a reset hook. I also think it should pass in the arm_boot_info* as a parameter. This would let the GIC do the right thing based on whether we're booting S or NS.

Quoted 3 lines

Are you planning to respin this patchset, or should I just pull the relevant bits into my series?

Just pull relevants I think. Should be just patches 1 and 2, the boot.c one needs rewrite and the gic one is a write-off as yours is much more functionally correct.

Regards,
Peter

Quoted 3 lines

thanks
-- PMM

#11 lore raw

PATCH 4/5 hw/cpu/{a15mpcore, a9mpcore}: enable TrustZone in GIC if it is enabled in CPUs

In reply to Peter Maydell

If the A9 and A15 CPUs which we're creating the peripherals for have TrustZone (EL3) enabled, then also enable it in the GIC we create.

---
 hw/cpu/a15mpcore.c | 13 +++++++++++++
 hw/cpu/a9mpcore.c  | 11 +++++++++++
 2 files changed, 24 insertions(+)
hw/cpu/a15mpcore.c +13 -0
index acc419e..e3814be 100644
@@ -56,10 +56,23 @@ static void a15mp_priv_realize(DeviceState *dev, Error **errp)
     SysBusDevice *busdev;
     int i;
     Error *err = NULL;
+    bool has_el3;
+    Object *cpuobj;
 
     gicdev = DEVICE(&s->gic);
     qdev_prop_set_uint32(gicdev, "num-cpu", s->num_cpu);
     qdev_prop_set_uint32(gicdev, "num-irq", s->num_irq);
+
+    if (!kvm_irqchip_in_kernel()) {
+        /* Make the GIC's TZ support match the CPUs. We assume that
+         * either all the CPUs have TZ, or none do.
+         */
+        cpuobj = OBJECT(qemu_get_cpu(0));
+        has_el3 = object_property_find(cpuobj, "has_el3", &error_abort) &&
+            object_property_get_bool(cpuobj, "has_el3", &error_abort);
+        qdev_prop_set_bit(gicdev, "has-security-extensions", has_el3);
+    }
+
     object_property_set_bool(OBJECT(&s->gic), true, "realized", &err);
     if (err != NULL) {
         error_propagate(errp, err);
hw/cpu/a9mpcore.c +11 -0
index c09358c..7046246 100644
@@ -49,6 +49,8 @@ static void a9mp_priv_realize(DeviceState *dev, Error **errp)
                  *wdtbusdev;
     Error *err = NULL;
     int i;
+    bool has_el3;
+    Object *cpuobj;
 
     scudev = DEVICE(&s->scu);
     qdev_prop_set_uint32(scudev, "num-cpu", s->num_cpu);
@@ -62,6 +64,15 @@ static void a9mp_priv_realize(DeviceState *dev, Error **errp)
     gicdev = DEVICE(&s->gic);
     qdev_prop_set_uint32(gicdev, "num-cpu", s->num_cpu);
     qdev_prop_set_uint32(gicdev, "num-irq", s->num_irq);
+
+    /* Make the GIC's TZ support match the CPUs. We assume that
+     * either all the CPUs have TZ, or none do.
+     */
+    cpuobj = OBJECT(qemu_get_cpu(0));
+    has_el3 = object_property_find(cpuobj, "has_el3", &error_abort) &&
+        object_property_get_bool(cpuobj, "has_el3", &error_abort);
+    qdev_prop_set_bit(gicdev, "has-security-extensions", has_el3);
+
     object_property_set_bool(OBJECT(&s->gic), true, "realized", &err);
     if (err != NULL) {
         error_propagate(errp, err);
Signature · 1 line
1.9.1
#12 lore raw

PATCH 5/5 hw/arm/virt: Enable TZ extensions on the GIC if we are using them

In reply to Peter Maydell

If we're creating a board with support for TrustZone, then enable it on the GIC model as well as on the CPUs.

---
 hw/arm/virt.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/arm/virt.c +5 -2
index 4846892..084a551 100644
@@ -360,7 +360,7 @@ static void create_v2m(VirtBoardInfo *vbi, qemu_irq *pic)
     fdt_add_v2m_gic_node(vbi);
 }
 
-static void create_gic(VirtBoardInfo *vbi, qemu_irq *pic)
+static void create_gic(VirtBoardInfo *vbi, qemu_irq *pic, bool secure)
 {
     /* We create a standalone GIC v2 */
     DeviceState *gicdev;
@@ -379,6 +379,9 @@ static void create_gic(VirtBoardInfo *vbi, qemu_irq *pic)
      * interrupts; there are always 32 of the former (mandated by GIC spec).
      */
     qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
+    if (!kvm_irqchip_in_kernel()) {
+        qdev_prop_set_bit(gicdev, "has-security-extensions", secure);
+    }
     qdev_init_nofail(gicdev);
     gicbusdev = SYS_BUS_DEVICE(gicdev);
     sysbus_mmio_map(gicbusdev, 0, vbi->memmap[VIRT_GIC_DIST].base);
@@ -875,7 +878,7 @@ static void machvirt_init(MachineState *machine)
 
     create_flash(vbi);
 
-    create_gic(vbi, pic);
+    create_gic(vbi, pic, vms->secure);
 
     create_uart(vbi, pic);
 
Signature · 1 line
1.9.1
#13 lore raw
In reply to Peter Maydell
Quoted 4 lines · Peter Maydell

This patchset enables the TZ support in the GIC for the systems where we enable TZ support in the CPU. In practice that means just the "virt" and "vexpress" boards, since all the others disable the CPU TZ support.

Further playing about with this has raised a couple of interesting issues:
(1) we forgot to actually implement the secure physical timer
(and we need to fix up the wiring bodge we do in the boards where we assume the physical timer is the NS phys timer)
(2) the UEFI firmware for the virt board assumes it is
not running in a Secure environment, so is likely to go wrong if we present it with a TZ-aware CPU and GIC. We probably need to default to no-TZ.

(This also sort of links in with the wider question of how we tell firmware running on 'virt' about system configurations where some devices are S only.)

-- PMM