Skip to contentSkip to conversation

xen/arm : Adding helper function for WFI

PATCH Superseded Parth Dixit 2 participants 9 messages Jul 3, 2014 – Jul 16, 2014

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 xen/arm : Adding helper function for WFI

WFI functionality is required at different places in xen. Moving it to seperate helper function so that it is easier to call WFI function and avoid duplication of code

---
Changelog v3 :
- new patch for helper function, this functionality existed in same patch in v2

 xen/arch/arm/domain.c       | 13 +++++++++++++
 xen/arch/arm/traps.c        | 10 +---------
 xen/include/asm-arm/event.h |  1 +
 3 files changed, 15 insertions(+), 9 deletions(-)
xen/arch/arm/domain.c +13 -0
index 2ae6941..bc5e56d 100644
@@ -779,6 +779,19 @@ void vcpu_mark_events_pending(struct vcpu *v)
     vgic_vcpu_inject_irq(v, v->domain->arch.evtchn_irq, 1);
 }
 
+/* The ARM spec declares that even if local irqs are masked in
+* the CPSR register, an irq should wake up a cpu from WFI anyway.
+* For this reason we need to check for irqs that need delivery,
+* ignoring the CPSR register, *after* calling SCHEDOP_block to
+* avoid races with vgic_vcpu_inject_irq.
+*/
+void vcpu_block_event(struct vcpu *v)
+{
+    vcpu_block();
+    if ( local_events_need_delivery_nomask() )
+        vcpu_unblock(current);
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/traps.c +1 -9
index 03a3da6..8d2e308 100644
@@ -1670,15 +1670,7 @@ asmlinkage void do_trap_hypervisor(struct cpu_user_regs *regs)
             return;
         }
         /* at the moment we only trap WFI */
-        vcpu_block();
-        /* The ARM spec declares that even if local irqs are masked in
-         * the CPSR register, an irq should wake up a cpu from WFI anyway.
-         * For this reason we need to check for irqs that need delivery,
-         * ignoring the CPSR register, *after* calling SCHEDOP_block to
-         * avoid races with vgic_vcpu_inject_irq.
-         */
-        if ( local_events_need_delivery_nomask() )
-            vcpu_unblock(current);
+        vcpu_block_event(current);
         advance_pc(regs, hsr);
         break;
     case HSR_EC_CP15_32:
xen/include/asm-arm/event.h +1 -0
index dd3ad13..a94ba2e 100644
@@ -6,6 +6,7 @@
 
 void vcpu_kick(struct vcpu *v);
 void vcpu_mark_events_pending(struct vcpu *v);
+void vcpu_block_event(struct vcpu *v);
 
 static inline int vcpu_event_delivery_is_enabled(struct vcpu *v)
 {
Signature · 1 line
1.9.1
#1 lore raw

PATCH v3 2/2 xen/arm : emulation of arm's PSCI v0.2 standard

Arm based virtual machines dom0/guest will request power related functionality from xen through PSCI interface. This patch implements version 0.2 of PSCI standard specified by arm for 64bit and 32 bit arm machines.

- modified arm_psci_fn_t to take three arguments
- implemented psci_cpu_on with additional error conditions
- removed switch-case in do_trap_psci function
- added PSCI v0.2 macros in psci.h
- added seperate tables for PSCI v0.1 and v0.2
- implemented affinity_info

---
Changelog v3
- moved wfi helper to seperate patch
- replaced new wfi function in traps.c
- removed defining power_state variable using value directly
- added new line between declaration
- merged PSCI v0.1 and v0.2 cpu_on function to avoid duplication
- removed spurious change
- renamed PSCI return values to reflect v0.2 moved them to top
- removed PSCI v0.1 version declaration for now, will introduce it when needed
- removed hard tabs in the code lifted from linux
- removed PSCI_0_1_MAX
- did not retained copyright header from linux as most of functions are removed
- added two function tables for PSCI v0.1 and PSCI v0.2
- added compatibility string to libxl_arm to expose new functionality
- refactored affinity_info code
- added table for masking function
- removed definitions of unused PSCI v0.2 functions
- removed function id's of unused PSCI v0.2 functions
- renamed constant PSCI_0_2_TOS_MP ( multicore aware) as per spec

tools/libxl/libxl_arm.c | 2 +-

 xen/arch/arm/domain.c           |   2 +
 xen/arch/arm/domain_build.c     |   5 +-
 xen/arch/arm/traps.c            |  45 +++++++++++--
 xen/arch/arm/vpsci.c            | 138 +++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h    |   3 +
 xen/include/asm-arm/processor.h |   3 +
 xen/include/asm-arm/psci.h      |  45 +++++++++++--
 8 files changed, 222 insertions(+), 21 deletions(-)
tools/libxl/libxl_arm.c +1 -1
index 4f0f0e2..e8bcd05 100644
@@ -237,7 +237,7 @@ static int make_psci_node(libxl__gc *gc, void *fdt)
     res = fdt_begin_node(fdt, "psci");
     if (res) return res;
 
-    res = fdt_property_compat(gc, fdt, 1, "arm,psci");
+    res = fdt_property_compat(gc, fdt, 2, "arm,psci-0.2","arm,psci");
     if (res) return res;
 
     res = fdt_property_string(fdt, "method", "hvc");
xen/arch/arm/domain.c +2 -0
index bc5e56d..f19cc1d 100644
@@ -504,6 +504,8 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags)
     /* Default the virtual ID to match the physical */
     d->arch.vpidr = boot_cpu_data.midr.bits;
 
+    d->arch.vpsci_ver = 0;
+
     clear_page(d->shared_info);
     share_xen_page_with_guest(
         virt_to_page(d->shared_info), d, XENSHARE_writable);
xen/arch/arm/domain_build.c +4 -1
index c424793..ebd4170 100644
@@ -388,6 +388,9 @@ static int make_hypervisor_node(struct domain *d,
 static int make_psci_node(void *fdt, const struct dt_device_node *parent)
 {
     int res;
+    const char compat[] =
+        "arm,psci-0.2""\0"
+        "arm,psci";
 
     DPRINT("Create PSCI node\n");
 
@@ -396,7 +399,7 @@ static int make_psci_node(void *fdt, const struct dt_device_node *parent)
     if ( res )
         return res;
 
-    res = fdt_property_string(fdt, "compatible", "arm,psci");
+    res = fdt_property(fdt, "compatible", compat, sizeof(compat));
     if ( res )
         return res;
 
xen/arch/arm/traps.c +38 -7

80 lines not on this page · raw message

xen/arch/arm/vpsci.c +130 -8

176 lines not on this page · raw message

xen/include/asm-arm/domain.h +3 -0
index b296923..53dbe27 100644
@@ -124,6 +124,9 @@ struct arch_domain
     /* Virtual CPUID */
     uint32_t vpidr;
 
+    /* PSCI version */
+    uint32_t vpsci_ver;
+
     struct {
         uint64_t offset;
     } phys_timer_base;
xen/include/asm-arm/processor.h +3 -0
index 9267c1b..8a237c4 100644
@@ -16,6 +16,9 @@
 #define MPIDR_AFF0_MASK     (_AC(0xff,U) << MPIDR_AFF0_SHIFT)
 #define MPIDR_HWID_MASK     _AC(0xffffff,U)
 #define MPIDR_INVALID       (~MPIDR_HWID_MASK)
+#define MPIDR_LEVEL_BITS    (8)
+#define AFFINITY_MASK(level)    ~((_AC(0x1,U) << ((level) * MPIDR_LEVEL_BITS)) - 1)
+
 
 /* TTBCR Translation Table Base Control Register */
 #define TTBCR_EAE    _AC(0x80000000,U)
xen/include/asm-arm/psci.h +41 -4

57 lines not on this page · raw message

Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH v3 2/2] xen/arm : emulation of arm's PSCI v0.2 standard

Quoted 9 lines · Parth Dixit

static void do_trap_psci(struct cpu_user_regs *regs)

 {
     arm_psci_fn_t psci_call = NULL;
xen/arch/arm/traps.c +1 -3
-    if ( PSCI_OP_REG(regs) >= ARRAY_SIZE(arm_psci_table) )
+    if ( PSCI_OP_REG(regs) < PSCI_migrate )
     {
-        domain_crash_synchronous();
-        return;

xen/arch/arm/traps.c static void do_debug_trap(struct cpu_user_regs *regs, unsigned int code) · line 1126

Quoted 10 lines
xen/arch/arm/traps.c +10 -0
+        if ( PSCI_OP_REG(regs) >= ARRAY_SIZE(arm_psci_table) )
+        {
+            domain_crash_synchronous();
+            return;
+        }
+        psci_call = arm_psci_table[PSCI_OP_REG(regs)].fn;
+    }
+    else
+    {
+        if ( ( PSCI_OP_REG(regs) & PSCI_FN_MASK ) >= ARRAY_SIZE(arm_psci_0_2_table) )

I think you need to also check that "PSCI_OP_REG(regs) & ~PSCI_FN_MASK" is either the 32-bit or 64-bit base. Otherwise I could call 0xdeadfe01 and it would work.

Do you not also need to check that the guest is of the appropriate type? Is an aarch64 guest allowed to call the aarch32 entry points? The spec doesn't say so explicitly AFAICT.

If it is allowed then I think you need to be truncating the 32-bit arguments to 32-bits when an aarch64 guest calls the 32-bit entry points.

Hrm, checking through the spec I see now that only some of the functions have both a 32- and 64-bit function id. VERSION, CPU_OFF, MIGRATE_INFO_TYPE, SYSTEM_OFF and SYSTEM_RESET only have a single function id (I suppose because they do not take any arguments which are mode specific).

I'm afraid that AFAICT you need to handle this distinction, which I expect makes it rather hard to share the same lookup table between 32- and 64-bit. A switch(PSCI_OP_REG(regs)) is looking increasingly like the correct approach to me.

xen/arch/arm/vpsci.c line 40

Quoted 5 lines
xen/arch/arm/vpsci.c +5 -0
+int do_psci_0_2_version(void)
+{
+    struct domain *d = current->domain;
+
+    return ( d->arch.vpsci_ver = XEN_PSCI_V_0_2 );

Is this assignment really intentional?

I see you use d->arch.vpsci_ver later in the common do_psci_0_2_cpu_on function, so I think you did mean to do this.

Does the spec say somewhere that after calling PSCI_0_2_VERSION an OS is forbidden from calling PSCI v0.1 functions?

Likewise does it mandate that an OS which supports v0.2 always calls VERSION before any other function?

I can't find anything in the spec which says either of those things. Please do point out if I am wrong.

In any case I think this approach to a common function is wrong. I think you should implement something like:

int do_common_cpu_on(int version, register_t target_cpu, register_t entry_point,
register_t context_id)

similar to your current do_psci_0_2_cpu_suspend but using version instead of d->arch.vpsci_ver. Then you have:

        int do_psci_cpu_on(uint32_t vcpuid, register_t entry_point)
        {
           return do_common_cpu_on(PSCI_0_1_VERSION, vcpuid, entry_point, 0);
        }
        int do_psci_0_2_cpu_on(register_t target_cpu, register_t entry_point,
                               register_t context_id)
        {
            return do_common_cpu_on(PSCI_0_2_VERSION, target_cpu,
                                    entry_point, context_id)
        }

IOW the mode of operation is determined by the entry point used not some bit of domain state.

xen/arch/arm/vpsci.c line 79

Quoted 9 lines
xen/arch/arm/vpsci.c +4 -0
+int do_psci_0_2_cpu_on(register_t target_cpu, register_t entry_point,
+                       register_t context_id)
+{
     struct vcpu *v;
     struct domain *d = current->domain;
     struct vcpu_guest_context *ctxt;
     int rc;
     int is_thumb = entry_point & 1;
+    uint32_t vcpuid ;

Stray " " here.

xen/arch/arm/vpsci.c line 82

Quoted 3 lines
xen/arch/arm/vpsci.c +3 -0
+
+    if( d->arch.vpsci_ver == XEN_PSCI_V_0_2 )
+        vcpuid = (u32)(target_cpu & MPIDR_HWID_MASK);

Doesn't this mask off AFF3 on 64-bit? I know we don't use AFF3 today in Xen, but this is setting a pretty subtle trap for the person who does come to implement something which uses it.

Quoted 25 lines
xen/arch/arm/vpsci.c +15 -5
@@ -75,12 +144,65 @@ int do_psci_cpu_on(uint32_t vcpuid, register_t entry_point)
     return PSCI_SUCCESS;
 }
 
-int do_psci_cpu_off(uint32_t power_state)
+static const unsigned long target_affinity_mask[] = {
+    ( MPIDR_HWID_MASK & AFFINITY_MASK( 0 ) ),
+    ( MPIDR_HWID_MASK & AFFINITY_MASK( 1 ) ),
+    ( MPIDR_HWID_MASK & AFFINITY_MASK( 2 ) )
+#ifdef CONFIG_ARM_64
+    ,( MPIDR_HWID_MASK & AFFINITY_MASK( 3 ) )
+#endif
+};
+
+int do_psci_0_2_affinity_info(register_t target_affinity,
+                              uint32_t lowest_affinity_level)
 {
-    struct vcpu *v = current;
-    if ( !test_and_set_bit(_VPF_down, &v->pause_flags) )
-        vcpu_sleep_nosync(v);
-    return PSCI_SUCCESS;
+    struct domain *d = current->domain;
+    struct vcpu *v;
+    uint32_t vcpuid;
+

xen/arch/arm/vpsci.c int do_psci_cpu_on(uint32_t vcpuid, register_t entry_point) · line 172

Quoted 10 lines
xen/arch/arm/vpsci.c +10 -0
+    if ( lowest_affinity_level < ARRAY_SIZE(target_affinity_mask) )
+        target_affinity &= target_affinity_mask[lowest_affinity_level];
+    else
+        return PSCI_INVALID_PARAMETERS;
+
+    for ( vcpuid = 0; vcpuid < d->max_vcpus; vcpuid++ )
+    {
+        v = d->vcpu[vcpuid];
+
+        if ( ( ( v->arch.vmpidr & target_affinity_mask[lowest_affinity_level] )

if you assign target_affinity_mask[lowest_affinity_level] to a local variable you can avoid the awful state of line wrapping here.

Ian.

#3 lore raw

Re: [PATCH v3 2/2] xen/arm : emulation of arm's PSCI v0.2 standard

Quoted 3 lines · Ian Campbell

If it is allowed then I think you need to be truncating the 32-bit arguments to 32-bits when an aarch64 guest calls the 32-bit entry points.

Looking at the SMC calling convention (ARM DEN 0028A) I think this is indeed the case, since it specifies the use of w0..w7 for arguments when an aarch64 cpu calls an SMC32 interface. It does say upper bits are zero, but I think you need to check?

Ian.

#4 lore raw

Re: [PATCH v3 2/2] xen/arm : emulation of arm's PSCI v0.2 standard

In reply to Ian Campbell

Hi,

Quoted 39 lines · Ian Campbell
Quoted 9 lines · Parth Dixit

static void do_trap_psci(struct cpu_user_regs *regs)

 {
     arm_psci_fn_t psci_call = NULL;
xen/arch/arm/traps.c +1 -3
-    if ( PSCI_OP_REG(regs) >= ARRAY_SIZE(arm_psci_table) )
+    if ( PSCI_OP_REG(regs) < PSCI_migrate )
     {
-        domain_crash_synchronous();
-        return;

xen/arch/arm/traps.c static void do_debug_trap(struct cpu_user_regs *regs, unsigned int code) · line 1126

Quoted 10 lines
xen/arch/arm/traps.c +10 -0
+        if ( PSCI_OP_REG(regs) >= ARRAY_SIZE(arm_psci_table) )
+        {
+            domain_crash_synchronous();
+            return;
+        }
+        psci_call = arm_psci_table[PSCI_OP_REG(regs)].fn;
+    }
+    else
+    {
+        if ( ( PSCI_OP_REG(regs) & PSCI_FN_MASK ) >= ARRAY_SIZE(arm_psci_0_2_table) )

I think you need to also check that "PSCI_OP_REG(regs) & ~PSCI_FN_MASK" is either the 32-bit or 64-bit base. Otherwise I could call 0xdeadfe01 and it would work.

Do you not also need to check that the guest is of the appropriate type? Is an aarch64 guest allowed to call the aarch32 entry points? The spec doesn't say so explicitly AFAICT.

If it is allowed then I think you need to be truncating the 32-bit arguments to 32-bits when an aarch64 guest calls the 32-bit entry points.

Hrm, checking through the spec I see now that only some of the functions have both a 32- and 64-bit function id. VERSION, CPU_OFF, MIGRATE_INFO_TYPE, SYSTEM_OFF and SYSTEM_RESET only have a single function id (I suppose because they do not take any arguments which are mode specific).

Quoted 4 lines

I'm afraid that AFAICT you need to handle this distinction, which I expect makes it rather hard to share the same lookup table between 32- and 64-bit. A switch(PSCI_OP_REG(regs)) is looking increasingly like the correct approach to me.

I am bit confused, are you saying for eg. aarch64 can call "psci_0_2_suspend" function with aarch32 function id (effectively 32 bit version of function?)

Quoted 41 lines

xen/arch/arm/vpsci.c line 40

Quoted 6 lines
xen/arch/arm/vpsci.c +5 -0
+int do_psci_0_2_version(void)
+{
+    struct domain *d = current->domain;
+
+    return ( d->arch.vpsci_ver = XEN_PSCI_V_0_2 );

Is this assignment really intentional?

I see you use d->arch.vpsci_ver later in the common do_psci_0_2_cpu_on function, so I think you did mean to do this.

Does the spec say somewhere that after calling PSCI_0_2_VERSION an OS is forbidden from calling PSCI v0.1 functions?

Likewise does it mandate that an OS which supports v0.2 always calls VERSION before any other function?

I can't find anything in the spec which says either of those things. Please do point out if I am wrong.

In any case I think this approach to a common function is wrong. I think you should implement something like:

int do_common_cpu_on(int version, register_t target_cpu, register_t entry_point,
register_t context_id)

similar to your current do_psci_0_2_cpu_suspend but using version instead of d->arch.vpsci_ver. Then you have:

        int do_psci_cpu_on(uint32_t vcpuid, register_t entry_point)
        {
           return do_common_cpu_on(PSCI_0_1_VERSION, vcpuid, entry_point, 0);
        }
        int do_psci_0_2_cpu_on(register_t target_cpu, register_t entry_point,
                               register_t context_id)
        {
            return do_common_cpu_on(PSCI_0_2_VERSION, target_cpu,
                                    entry_point, context_id)
        }
Quoted 3 lines

IOW the mode of operation is determined by the entry point used not some bit of domain state.

Sure, i'll switch to entry point based approach.

Quoted 11 lines

xen/arch/arm/vpsci.c line 79

Quoted 9 lines
xen/arch/arm/vpsci.c +4 -0
+int do_psci_0_2_cpu_on(register_t target_cpu, register_t entry_point,
+                       register_t context_id)
+{
     struct vcpu *v;
     struct domain *d = current->domain;
     struct vcpu_guest_context *ctxt;
     int rc;
     int is_thumb = entry_point & 1;
+    uint32_t vcpuid ;

Stray " " here.

Will remove it next patchset

Quoted 8 lines

xen/arch/arm/vpsci.c line 82

Quoted 3 lines
xen/arch/arm/vpsci.c +3 -0
+
+    if( d->arch.vpsci_ver == XEN_PSCI_V_0_2 )
+        vcpuid = (u32)(target_cpu & MPIDR_HWID_MASK);

Doesn't this mask off AFF3 on 64-bit? I know we don't use AFF3 today in Xen, but this is setting a pretty subtle trap for the person who does come to implement something which uses it.

Sure, will take care of it.

Quoted 30 lines
Quoted 30 lines
xen/arch/arm/vpsci.c +20 -5
@@ -75,12 +144,65 @@ int do_psci_cpu_on(uint32_t vcpuid, register_t entry_point)
     return PSCI_SUCCESS;
 }
 
-int do_psci_cpu_off(uint32_t power_state)
+static const unsigned long target_affinity_mask[] = {
+    ( MPIDR_HWID_MASK & AFFINITY_MASK( 0 ) ),
+    ( MPIDR_HWID_MASK & AFFINITY_MASK( 1 ) ),
+    ( MPIDR_HWID_MASK & AFFINITY_MASK( 2 ) )
+#ifdef CONFIG_ARM_64
+    ,( MPIDR_HWID_MASK & AFFINITY_MASK( 3 ) )
+#endif
+};
+
+int do_psci_0_2_affinity_info(register_t target_affinity,
+                              uint32_t lowest_affinity_level)
 {
-    struct vcpu *v = current;
-    if ( !test_and_set_bit(_VPF_down, &v->pause_flags) )
-        vcpu_sleep_nosync(v);
-    return PSCI_SUCCESS;
+    struct domain *d = current->domain;
+    struct vcpu *v;
+    uint32_t vcpuid;
+
+    if ( lowest_affinity_level < ARRAY_SIZE(target_affinity_mask) )
+        target_affinity &= target_affinity_mask[lowest_affinity_level];
+    else
+        return PSCI_INVALID_PARAMETERS;
+
Quoted 9 lines

xen/arch/arm/vpsci.c int do_psci_cpu_on(uint32_t vcpuid, register_t entry_point) · line 172

Quoted 5 lines
xen/arch/arm/vpsci.c +5 -0
+    for ( vcpuid = 0; vcpuid < d->max_vcpus; vcpuid++ )
+    {
+        v = d->vcpu[vcpuid];
+
+        if ( ( ( v->arch.vmpidr & target_affinity_mask[lowest_affinity_level] )

if you assign target_affinity_mask[lowest_affinity_level] to a local variable you can avoid the awful state of line wrapping here.

will introduce local variable

Quoted 2 lines

Ian.

#5 lore raw

Re: [PATCH v3 2/2] xen/arm : emulation of arm's PSCI v0.2 standard

Quoted 42 lines · Parth Dixit

Hi,

Quoted 39 lines · Ian Campbell
Quoted 9 lines · Parth Dixit

static void do_trap_psci(struct cpu_user_regs *regs)

 {
     arm_psci_fn_t psci_call = NULL;
xen/arch/arm/traps.c +1 -3
-    if ( PSCI_OP_REG(regs) >= ARRAY_SIZE(arm_psci_table) )
+    if ( PSCI_OP_REG(regs) < PSCI_migrate )
     {
-        domain_crash_synchronous();
-        return;

xen/arch/arm/traps.c static void do_debug_trap(struct cpu_user_regs *regs, unsigned int code) · line 1126

Quoted 10 lines
xen/arch/arm/traps.c +10 -0
+        if ( PSCI_OP_REG(regs) >= ARRAY_SIZE(arm_psci_table) )
+        {
+            domain_crash_synchronous();
+            return;
+        }
+        psci_call = arm_psci_table[PSCI_OP_REG(regs)].fn;
+    }
+    else
+    {
+        if ( ( PSCI_OP_REG(regs) & PSCI_FN_MASK ) >= ARRAY_SIZE(arm_psci_0_2_table) )

I think you need to also check that "PSCI_OP_REG(regs) & ~PSCI_FN_MASK" is either the 32-bit or 64-bit base. Otherwise I could call 0xdeadfe01 and it would work.

Do you not also need to check that the guest is of the appropriate type? Is an aarch64 guest allowed to call the aarch32 entry points? The spec doesn't say so explicitly AFAICT.

If it is allowed then I think you need to be truncating the 32-bit arguments to 32-bits when an aarch64 guest calls the 32-bit entry points.

Hrm, checking through the spec I see now that only some of the functions have both a 32- and 64-bit function id. VERSION, CPU_OFF, MIGRATE_INFO_TYPE, SYSTEM_OFF and SYSTEM_RESET only have a single function id (I suppose because they do not take any arguments which are mode specific).

Quoted 8 lines
Quoted 4 lines

I'm afraid that AFAICT you need to handle this distinction, which I expect makes it rather hard to share the same lookup table between 32- and 64-bit. A switch(PSCI_OP_REG(regs)) is looking increasingly like the correct approach to me.

I am bit confused, are you saying for eg. aarch64 can call "psci_0_2_suspend" function with aarch32 function id (effectively 32 bit version of function?)

SUSPEND is not a good example since it has both 32- and 64-bit fn ids.

But e.g CPU_OFF only defines a single id, 0x84000002. I think calling c4000002 would need to be rejected.

It seems like the SMC Calling Convention doc which I referenced covers the requirements here.

Ian.

#6 lore raw

Re: [PATCH v3 2/2] xen/arm : emulation of arm's PSCI v0.2 standard

Hi,

Quoted 52 lines · Ian Campbell
Quoted 42 lines · Parth Dixit

Hi,

Quoted 39 lines · Ian Campbell
Quoted 9 lines · Parth Dixit

static void do_trap_psci(struct cpu_user_regs *regs)

 {
     arm_psci_fn_t psci_call = NULL;
xen/arch/arm/traps.c +1 -3
-    if ( PSCI_OP_REG(regs) >= ARRAY_SIZE(arm_psci_table) )
+    if ( PSCI_OP_REG(regs) < PSCI_migrate )
     {
-        domain_crash_synchronous();
-        return;

xen/arch/arm/traps.c static void do_debug_trap(struct cpu_user_regs *regs, unsigned int code) · line 1126

Quoted 10 lines
xen/arch/arm/traps.c +10 -0
+        if ( PSCI_OP_REG(regs) >= ARRAY_SIZE(arm_psci_table) )
+        {
+            domain_crash_synchronous();
+            return;
+        }
+        psci_call = arm_psci_table[PSCI_OP_REG(regs)].fn;
+    }
+    else
+    {
+        if ( ( PSCI_OP_REG(regs) & PSCI_FN_MASK ) >= ARRAY_SIZE(arm_psci_0_2_table) )

I think you need to also check that "PSCI_OP_REG(regs) & ~PSCI_FN_MASK" is either the 32-bit or 64-bit base. Otherwise I could call 0xdeadfe01 and it would work.

Do you not also need to check that the guest is of the appropriate type? Is an aarch64 guest allowed to call the aarch32 entry points? The spec doesn't say so explicitly AFAICT.

If it is allowed then I think you need to be truncating the 32-bit arguments to 32-bits when an aarch64 guest calls the 32-bit entry points.

Hrm, checking through the spec I see now that only some of the functions have both a 32- and 64-bit function id. VERSION, CPU_OFF, MIGRATE_INFO_TYPE, SYSTEM_OFF and SYSTEM_RESET only have a single function id (I suppose because they do not take any arguments which are mode specific).

Quoted 8 lines
Quoted 4 lines

I'm afraid that AFAICT you need to handle this distinction, which I expect makes it rather hard to share the same lookup table between 32- and 64-bit. A switch(PSCI_OP_REG(regs)) is looking increasingly like the correct approach to me.

I am bit confused, are you saying for eg. aarch64 can call "psci_0_2_suspend" function with aarch32 function id (effectively 32 bit version of function?)

Quoted 8 lines

SUSPEND is not a good example since it has both 32- and 64-bit fn ids.

But e.g CPU_OFF only defines a single id, 0x84000002. I think calling c4000002 would need to be rejected.

It seems like the SMC Calling Convention doc which I referenced covers the requirements here.

Ok, got it, will take care in patcset v4

Quoted 2 lines

Ian.

Thanks
parth

#7 lore raw

Re: [PATCH v3 2/2] xen/arm : emulation of arm's PSCI v0.2 standard

Quoted 2 lines · Parth Dixit
Quoted 2 lines

It seems like the SMC Calling Convention doc which I referenced covers the requirements here.

FWIW I asked Charles (the author of the spec) about this, his response is below (with permission). I've not yet looked at your v4 with this in mind.

Ian.
---------

Quoted 6 lines

For functions which have both an SMC32 and SMC64 variant is it it permissible for a cpu to call the entry point which doesn't correspond to the mode it is in? From the SMC calling convention spec I think it is illegal for an aarch32 cpu to call an SMC64 interface but permissible for an aarch64 cpu to call SMC32, since not all interfaces have an SMC64 variant. Is that true even when an SMC64 variant is defined?

This is valid for the SCM calling convention but not in general, for PSCI, in fact the spec requires INVALID_PARAMETERS to be returned in the case the execution state of the caller (AArch32/64) doesn't match for the ID. It' valid for the SMC calling convention as you may have calls which make sense as 32 bit calls in the first place, and for which there is no 64 equivalent. There may even be cases where a caller specifically wants to use a 32 bit variant for a given SMC based API (though I can't think of any off the top my head). For the PSCI calls that are affected by this issue it would have been complicated to allow this. It would have been confusing for a 64 bit caller to call say SUSPEND as 32 bit, but obviously would expect to be resumed as 64.

Quoted 2 lines

What is the expected behaviour if the top bits of xN are non-zero for an aarch64 cpu calling an SMC32 interface?

Results are passed in Wx and returned in Wx, so I would expect the implementer is not required to preserve upper words, as the SMC32 calling convention is dictating use of 32 bit widths in the first place for parameters and returns

When you load into a Wx it will trash the upper word (make it zero). So coming into the function you'd expect there are zeros there anyway. Returns should be through Wx so an implementation should not use upper words when returning values, nor should the callee assume anything there.

Quoted 2 lines

Is it permissible to clobber the top bits of the return register in this case?

Yup, in theory there should zeros there.

Quoted 5 lines

Are cpus allowed to mix and match the use of v0.1 and v0.2 interfaces (assuming both have been advertised to them)?

Is there any requirement to call e.g. VERSION before using other v0.2 interfaces?

Well in an ACPI world we are going to be explicitly linking to PSCI v0.2 and beyond. So there is no need to support both there. On an DT world you do have the complication that somebody could in theory supply the original PSCI. However in that case the DT should be describing that.

PSCI_VERSION will come more into its own when we rev PSCI itself, as it will always be a mandatory API going forward.

#8 lore raw
In reply to Parth Dixit
Quoted 18 lines · Parth Dixit

WFI functionality is required at different places in xen. Moving it to seperate helper function so that it is easier to call WFI function and avoid duplication of code

---
Changelog v3 :
- new patch for helper function, this functionality existed in same patch in v2

 xen/arch/arm/domain.c       | 13 +++++++++++++
 xen/arch/arm/traps.c        | 10 +---------
 xen/include/asm-arm/event.h |  1 +
 3 files changed, 15 insertions(+), 9 deletions(-)
xen/arch/arm/domain.c +0 -0
index 2ae6941..bc5e56d 100644

xen/arch/arm/domain.c void vcpu_mark_events_pending(struct vcpu *v) · line 786

Quoted 8 lines
xen/arch/arm/domain.c +5 -0
@@ -779,6 +779,19 @@ void vcpu_mark_events_pending(struct vcpu *v)
     vgic_vcpu_inject_irq(v, v->domain->arch.evtchn_irq, 1);
 }
 
+/* The ARM spec declares that even if local irqs are masked in
+* the CPSR register, an irq should wake up a cpu from WFI anyway.
+* For this reason we need to check for irqs that need delivery,
+* ignoring the CPSR register, *after* calling SCHEDOP_block to
+* avoid races with vgic_vcpu_inject_irq.

There should be a space before each of the continuation lines' *.

xen/arch/arm/domain.c void vcpu_mark_events_pending(struct vcpu *v) · line 788

Quoted 2 lines
xen/arch/arm/domain.c +2 -0
+*/
+void vcpu_block_event(struct vcpu *v)

Can anyone think of a more descriptive name for this function?

vcpu_block_unless_event_pending() is a bit of a mouthful.

Ian.