Skip to contentSkip to conversation

xen/arm: Trigger Xen suspend when Dom0 completes suspend

PATCH In review Mykyta Poturai 5 participants 37 messages Oct 7, 2022 – Dec 6, 2022 1 message not in the archive

lore
Share & tools
lore
#7 lore raw

PATCH 4/19 xen/arm: Trigger Xen suspend when Dom0 completes suspend

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

When Dom0 finalizes its suspend procedure the suspend of Xen is triggered by calling system_suspend(). Dom0 finalizes the suspend from its boot core (VCPU#0), which could be mapped to any physical CPU, i.e. the system_suspend() function could be executed by any physical CPU. Since Xen suspend procedure has to be run by the boot CPU (non-boot CPUs will be disabled at some point in suspend procedure), system_suspend() execution has to continue on CPU#0.

Though it is not clearly stated that the PSCI suspend call should be issued from cpu0, we assume that it is to simplify the process. This assumption is based on a fact that most platforms call some form of disable_nonboot_cpus routine before issuing the PSCI suspend call.

When the system_suspend() returns 0, it means that the system was suspended and it is coming out of the resume procedure. Regardless of the system_suspend() return value, after this function returns Xen is fully functional, and its state, including all devices and data structures, matches the state prior to calling system_suspend(). The status is returned by system_suspend() for debugging/logging purposes and function prototype compatibility.

---
 xen/arch/arm/suspend.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
xen/arch/arm/suspend.c +34 -0
index d19545744f..b09bf319d0 100644
@@ -132,11 +132,20 @@ void vcpu_resume(struct vcpu *v)
     clear_bit(_VPF_suspended, &v->pause_flags);
 }
 
+/* Xen suspend. Note: data is not used (suspend is the suspend to RAM) */
+static long system_suspend(void *data)
+{
+    BUG_ON(system_state != SYS_STATE_active);
+
+    return -ENOSYS;
+}
+
 int32_t domain_suspend(register_t epoint, register_t cid)
 {
     struct vcpu *v;
     struct domain *d = current->domain;
     bool is_thumb = epoint & 1;
+    int status;
 
     dprintk(XENLOG_DEBUG,
             "Dom%d suspend: epoint=0x%"PRIregister", cid=0x%"PRIregister"\n",
@@ -173,6 +182,31 @@ int32_t domain_suspend(register_t epoint, register_t cid)
      */
     vcpu_block_unless_event_pending(current);
 
+    /* If this was dom0 the whole system should suspend: trigger Xen suspend */
+    if ( is_hardware_domain(d) )
+    {
+        /*
+         * system_suspend should be called when Dom0 finalizes the suspend
+         * procedure from its boot core (VCPU#0). However, Dom0's VCPU#0 could
+         * be mapped to any PCPU (this function could be executed by any PCPU).
+         * The suspend procedure has to be finalized by the PCPU#0 (non-boot
+         * PCPUs will be disabled during the suspend).
+         */
+        status = continue_hypercall_on_cpu(0, system_suspend, NULL);
+        /*
+         * If an error happened, there is nothing that needs to be done here
+         * because the system_suspend always returns in fully functional state
+         * no matter what the outcome of suspend procedure is. If the system
+         * suspended successfully the function will return 0 after the resume.
+         * Otherwise, if an error is returned it means Xen did not suspended,
+         * but it is still in the same state as if the system_suspend was never
+         * called. We dump a debug message in case of an error for debugging/
+         * logging purpose.
+         */
+        if ( status )
+            dprintk(XENLOG_ERR, "Failed to suspend, errno=%d\n", status);
+    }
+
     return PSCI_SUCCESS;
 }
 
Signature · 1 line
2.37.1
#6 lore raw

PATCH 3/19 xen/arm: While a domain is suspended put its watchdogs on pause

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

While a domain is suspended its watchdogs must be paused. Otherwise, if the domain stays in the suspend state for a longer period of time compared to the watchdog period, the domain would be shutdown on resume. Proper solution to this problem is to stop (suspend) the watchdog timers after the domain suspends and to restart (resume) the watchdog timers before the domain resumes. The suspend/resume of watchdog timers is done in Xen and is invisible to the guests.
Just before the domain starts resuming the watchdog timers are programmed with a new expire value. The new expire value is set according to the last offset provided by the last hypercall before suspend was triggered, effectively restarting the timer.

---
 xen/arch/arm/suspend.c  |  4 ++++
 xen/common/sched/core.c | 36 ++++++++++++++++++++++++++++++++++++
 xen/include/xen/sched.h |  7 +++++++
 3 files changed, 47 insertions(+)
xen/arch/arm/suspend.c +4 -0
index 987ba6ac11..d19545744f 100644
@@ -128,6 +128,7 @@ void vcpu_resume(struct vcpu *v)
 
     /* Initialize VCPU registers */
     arch_set_info_guest(v, &ctxt);
+    watchdog_domain_resume(v->domain);
     clear_bit(_VPF_suspended, &v->pause_flags);
 }
 
@@ -162,6 +163,9 @@ int32_t domain_suspend(register_t epoint, register_t cid)
      */
     vcpu_suspend(epoint, cid);
 
+    /* Disable watchdogs of this domain */
+    watchdog_domain_suspend(d);
+
     /*
      * The calling domain is suspended by blocking its last running VCPU. If an
      * event is pending the domain will resume right away (VCPU will not block,
xen/common/sched/core.c +36 -0
index 0ecb41cfe1..ebed0ec49e 100644
@@ -1616,6 +1616,42 @@ void watchdog_domain_destroy(struct domain *d)
         kill_timer(&d->watchdog_timer[i].timer);
 }
 
+void watchdog_domain_suspend(struct domain *d)
+{
+    unsigned int i;
+
+    spin_lock(&d->watchdog_lock);
+
+    for ( i = 0; i < NR_DOMAIN_WATCHDOG_TIMERS; i++ )
+    {
+        if ( test_bit(i, &d->watchdog_inuse_map) )
+        {
+            stop_timer(&d->watchdog_timer[i].timer);
+        }
+    }
+
+    spin_unlock(&d->watchdog_lock);
+}
+
+void watchdog_domain_resume(struct domain *d)
+{
+    unsigned int i;
+
+    spin_lock(&d->watchdog_lock);
+
+    for ( i = 0; i < NR_DOMAIN_WATCHDOG_TIMERS; i++ )
+    {
+        if ( test_bit(i, &d->watchdog_inuse_map) )
+        {
+            set_timer(&d->watchdog_timer[i].timer,
+                      NOW() + SECONDS(d->watchdog_timer[i].timeout));
+        }
+    }
+
+    spin_unlock(&d->watchdog_lock);
+}
+
+
 /*
  * Pin a vcpu temporarily to a specific CPU (or restore old pinning state if
  * cpu is NR_CPUS).
xen/include/xen/sched.h +7 -0
index 7a4aef4c17..00a939aa01 100644
@@ -1025,6 +1025,13 @@ void scheduler_disable(void);
 void watchdog_domain_init(struct domain *d);
 void watchdog_domain_destroy(struct domain *d);
 
+/*
+ * Suspend/resume watchdogs of domain (while the domain is suspended its
+ * watchdogs should be on pause)
+ */
+void watchdog_domain_suspend(struct domain *d);
+void watchdog_domain_resume(struct domain *d);
+
 /*
  * Use this check when the following are both true:
  *  - Using this feature or interface requires full access to the hardware
Signature · 1 line
2.37.1
#10 lore raw

PATCH 6/19 xen/arm: Freeze domains on suspend and thaw them on resume

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

Freeze and thaw of domains is reused as implemented for x86. In addition, system_state variable is updated to represent the actual state of the system.

---
 xen/arch/arm/suspend.c | 8 ++++++++
 1 file changed, 8 insertions(+)
xen/arch/arm/suspend.c +8 -0
index b09bf319d0..2b94816b63 100644
@@ -137,6 +137,14 @@ static long system_suspend(void *data)
 {
     BUG_ON(system_state != SYS_STATE_active);
 
+    system_state = SYS_STATE_suspend;
+    freeze_domains();
+
+    system_state = SYS_STATE_resume;
+
+    thaw_domains();
+    system_state = SYS_STATE_active;
+
     return -ENOSYS;
 }
 
Signature · 1 line
2.37.1
#12 lore raw

PATCH 7/19 xen/arm: Disable/enable non-boot physical CPUs on suspend/resume

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

Non-boot CPUs have to be disabled on suspend and enabled on resume (hotplug-based mechanism). Disabling non-boot CPUs will lead to PSCI CPU_OFF to be called by each non-boot CPU. Depending on the underlying platform capabilities, this may lead to the physical powering down of CPUs. Tested on Xilinx Zynq Ultrascale+ MPSoC (including power down of each non-boot CPU).

---
 xen/arch/arm/suspend.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
xen/arch/arm/suspend.c +14 -1
index 2b94816b63..0784979e4f 100644
@@ -13,6 +13,7 @@
  */
 
 #include <xen/sched.h>
+#include <xen/cpu.h>
 #include <asm/cpufeature.h>
 #include <asm/event.h>
 #include <asm/psci.h>
@@ -135,17 +136,29 @@ void vcpu_resume(struct vcpu *v)
 /* Xen suspend. Note: data is not used (suspend is the suspend to RAM) */
 static long system_suspend(void *data)
 {
+    int status;
+
     BUG_ON(system_state != SYS_STATE_active);
 
     system_state = SYS_STATE_suspend;
     freeze_domains();
 
+    status = disable_nonboot_cpus();
+    if ( status )
+    {
+        system_state = SYS_STATE_resume;
+        goto resume_nonboot_cpus;
+    }
+
     system_state = SYS_STATE_resume;
 
+resume_nonboot_cpus:
+    enable_nonboot_cpus();
     thaw_domains();
     system_state = SYS_STATE_active;
+    dsb(sy);
 
-    return -ENOSYS;
+    return status;
 }
 
 int32_t domain_suspend(register_t epoint, register_t cid)
Signature · 1 line
2.37.1
#15 lore raw

PATCH 9/19 xen/arm: Implement GIC suspend/resume functions (gicv2 only)

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

System suspend may lead to a state where GIC would be powered down. Therefore, Xen should save/restore the context of GIC on suspend/resume. Note that the context consists of states of registers which are controlled by the hypervisor. Other GIC registers which are accessible by guests are saved/restored on context switch.
Tested on Xilinx Ultrascale+ MPSoC with (and without) powering down the GIC.

---
 xen/arch/arm/gic-v2.c     | 138 +++++++++++++++++++++++++++++++++++++-
 xen/arch/arm/gic.c        |  27 ++++++++
 xen/include/asm-arm/gic.h |   8 +++
 3 files changed, 172 insertions(+), 1 deletion(-)
xen/arch/arm/gic-v2.c +137 -1
index b2adc8ec9a..b077b66d92 100644
@@ -123,6 +123,23 @@ static DEFINE_PER_CPU(u8, gic_cpu_id);
 /* Maximum cpu interface per GIC */
 #define NR_GIC_CPU_IF 8
 
+/* GICv2 registers to be saved/restored on system suspend/resume */
+struct gicv2_context {
+    /* GICC context */
+    uint32_t gicc_ctlr;
+    uint32_t gicc_pmr;
+    uint32_t gicc_bpr;
+    /* GICD context */
+    uint32_t gicd_ctlr;
+    uint32_t *gicd_isenabler;
+    uint32_t *gicd_isactiver;
+    uint32_t *gicd_ipriorityr;
+    uint32_t *gicd_itargetsr;
+    uint32_t *gicd_icfgr;
+};
+
+static struct gicv2_context gicv2_context;
+
 static inline void writeb_gicd(uint8_t val, unsigned int offset)
 {
     writeb_relaxed(val, gicv2.map_dbase + offset);
@@ -1251,6 +1268,40 @@ static void __init gicv2_acpi_init(void)
 static void __init gicv2_acpi_init(void) { }
 #endif
 
+static int gicv2_alloc_context(struct gicv2_context *gc)
+{
+    uint32_t n = gicv2_info.nr_lines;
+
+    gc->gicd_isenabler = xzalloc_array(uint32_t, DIV_ROUND_UP(n, 32));
+    if ( !gc->gicd_isenabler )
+        goto err_free;
+
+    gc->gicd_isactiver = xzalloc_array(uint32_t, DIV_ROUND_UP(n, 32));
+    if ( !gc->gicd_isactiver )
+        goto err_free;
+
+    gc->gicd_itargetsr = xzalloc_array(uint32_t, DIV_ROUND_UP(n, 4));
+    if ( !gc->gicd_itargetsr )
+        goto err_free;
+
+    gc->gicd_ipriorityr = xzalloc_array(uint32_t, DIV_ROUND_UP(n, 4));
+    if ( !gc->gicd_ipriorityr )
+        goto err_free;
+
+    gc->gicd_icfgr = xzalloc_array(uint32_t, DIV_ROUND_UP(n, 16));
+    if ( !gc->gicd_icfgr )
+        goto err_free;
+
+    return 0;
+err_free:
+    xfree(gc->gicd_icfgr);
+    xfree(gc->gicd_ipriorityr);
+    xfree(gc->gicd_itargetsr);
+    xfree(gc->gicd_isactiver);
+    xfree(gc->gicd_isenabler);
+    return -ENOMEM;
+}
+
 static int __init gicv2_init(void)
 {
     uint32_t aliased_offset = 0;
@@ -1318,7 +1369,8 @@ static int __init gicv2_init(void)
 
     spin_unlock(&gicv2.lock);
 
-    return 0;
+    /* Allocate memory to be used for saving GIC context during the suspend */
+    return gicv2_alloc_context(&gicv2_context);
 }
 
 static void gicv2_do_LPI(unsigned int lpi)
@@ -1327,6 +1379,88 @@ static void gicv2_do_LPI(unsigned int lpi)
     BUG();
 }
 
+static int gicv2_suspend(void)
+{
+    int i;
+
+    ASSERT(gicv2_context.gicd_isenabler);
+    ASSERT(gicv2_context.gicd_isactiver);
+    ASSERT(gicv2_context.gicd_ipriorityr);
+    ASSERT(gicv2_context.gicd_itargetsr);
+    ASSERT(gicv2_context.gicd_icfgr);
+
+    /* Save GICC configuration */
+    gicv2_context.gicc_ctlr = readl_gicc(GICC_CTLR);
+    gicv2_context.gicc_pmr = readl_gicc(GICC_PMR);
+    gicv2_context.gicc_bpr = readl_gicc(GICC_BPR);
+
+    /* Save GICD configuration */
+    gicv2_context.gicd_ctlr = readl_gicd(GICD_CTLR);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 32); i++ )
+        gicv2_context.gicd_isenabler[i] = readl_gicd(GICD_ISENABLER + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 32); i++ )
+        gicv2_context.gicd_isactiver[i] = readl_gicd(GICD_ISACTIVER + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 4); i++ )
+        gicv2_context.gicd_ipriorityr[i] = readl_gicd(GICD_IPRIORITYR + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 4); i++ )
+        gicv2_context.gicd_itargetsr[i] = readl_gicd(GICD_ITARGETSR + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 16); i++ )
+        gicv2_context.gicd_icfgr[i] = readl_gicd(GICD_ICFGR + i * 4);
+
+    return 0;
+}
+
+static void gicv2_resume(void)
+{
+    int i;
+
+    ASSERT(gicv2_context.gicd_isenabler);
+    ASSERT(gicv2_context.gicd_isactiver);
+    ASSERT(gicv2_context.gicd_ipriorityr);
+    ASSERT(gicv2_context.gicd_itargetsr);
+    ASSERT(gicv2_context.gicd_icfgr);
+
+    /* Disable CPU interface and distributor */
+    writel_gicc(0, GICC_CTLR);
+    writel_gicd(0, GICD_CTLR);
+
+    /* Restore GICD configuration */
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 32); i++ ) {
+        writel_gicd(0xffffffff, GICD_ICENABLER + i * 4);
+        writel_gicd(gicv2_context.gicd_isenabler[i], GICD_ISENABLER + i * 4);
+    }
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 32); i++ ) {
+        writel_gicd(0xffffffff, GICD_ICACTIVER + i * 4);
+        writel_gicd(gicv2_context.gicd_isactiver[i], GICD_ISACTIVER + i * 4);
+    }
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 4); i++ )
+        writel_gicd(gicv2_context.gicd_ipriorityr[i], GICD_IPRIORITYR + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 4); i++ )
+        writel_gicd(gicv2_context.gicd_itargetsr[i], GICD_ITARGETSR + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 16); i++ )
+        writel_gicd(gicv2_context.gicd_icfgr[i], GICD_ICFGR + i * 4);
+
+    /* Make sure all registers are restored and enable distributor */
+    writel_gicd(gicv2_context.gicd_ctlr | GICD_CTL_ENABLE, GICD_CTLR);
+
+    /* Restore GIC CPU interface configuration */
+    writel_gicc(gicv2_context.gicc_pmr, GICC_PMR);
+    writel_gicc(gicv2_context.gicc_bpr, GICC_BPR);
+
+    /* Enable GIC CPU interface */
+    writel_gicc(gicv2_context.gicc_ctlr | GICC_CTL_ENABLE | GICC_CTL_EOI,
+                GICC_CTLR);
+}
+
 const static struct gic_hw_operations gicv2_ops = {
     .info                = &gicv2_info,
     .init                = gicv2_init,
@@ -1361,6 +1495,8 @@ const static struct gic_hw_operations gicv2_ops = {
     .map_hwdom_extra_mappings = gicv2_map_hwdown_extra_mappings,
     .iomem_deny_access   = gicv2_iomem_deny_access,
     .do_LPI              = gicv2_do_LPI,
+    .suspend             = gicv2_suspend,
+    .resume              = gicv2_resume,
 };
 
 /* Set up the GIC */
xen/arch/arm/gic.c +27 -0
index 3b0331b538..e9feb1fd3b 100644
@@ -467,6 +467,33 @@ int gic_iomem_deny_access(const struct domain *d)
     return gic_hw_ops->iomem_deny_access(d);
 }
 
+int gic_suspend(void)
+{
+    /* Must be called by boot CPU#0 with interrupts disabled */
+    ASSERT(!local_irq_is_enabled());
+    ASSERT(!smp_processor_id());
+
+    if ( !gic_hw_ops->suspend || !gic_hw_ops->resume )
+        return -ENOSYS;
+
+    gic_hw_ops->suspend();
+
+    return 0;
+}
+
+void gic_resume(void)
+{
+    /*
+     * Must be called by boot CPU#0 with interrupts disabled after gic_suspend
+     * has returned successfully.
+     */
+    ASSERT(!local_irq_is_enabled());
+    ASSERT(!smp_processor_id());
+    ASSERT(gic_hw_ops->resume);
+
+    gic_hw_ops->resume();
+}
+
 static int cpu_gic_callback(struct notifier_block *nfb,
                             unsigned long action,
                             void *hcpu)
xen/include/asm-arm/gic.h +8 -0
index c7f0c343d1..113e39460d 100644
@@ -275,6 +275,10 @@ extern int gicv_setup(struct domain *d);
 extern void gic_save_state(struct vcpu *v);
 extern void gic_restore_state(struct vcpu *v);
 
+/* Suspend/resume */
+extern int gic_suspend(void);
+extern void gic_resume(void);
+
 /* SGI (AKA IPIs) */
 enum gic_sgi {
     GIC_SGI_EVENT_CHECK,
@@ -390,6 +394,10 @@ struct gic_hw_operations {
     int (*iomem_deny_access)(const struct domain *d);
     /* Handle LPIs, which require special handling */
     void (*do_LPI)(unsigned int lpi);
+    /* Save GIC configuration due to the system suspend */
+    int (*suspend)(void);
+    /* Restore GIC configuration due to the system resume */
+    void (*resume)(void);
 };
 
 extern const struct gic_hw_operations *gic_hw_ops;
Signature · 1 line
2.37.1
#1 lore raw

PATCH 1/19 xen/arm: Implement PSCI system suspend

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

The implementation consists of:
-Adding PSCI system suspend call as new PSCI function -Trapping PSCI system_suspend HVC
-Implementing PSCI system suspend call (virtual interface that allows
guests to suspend themselves)

The PSCI system suspend should be called by a guest from its boot VCPU. Non-boot VCPUs of the guest should be hot-unplugged using PSCI CPU_OFF call prior to issuing PSCI system suspend. Interrupts that are left enabled by the guest are assumed to be its wake-up interrupts. Therefore, a wake-up interrupt triggers the resume of the guest. Guest should resume regardless of the state of Xen (suspended or not).

When a guest calls PSCI system suspend the respective domain will be suspended if the following conditions are met:
1) Given resume entry point is not invalid
2) Other (if any) VCPUs of the calling guest are hot-unplugged

If the conditions above are met the calling domain is labeled as suspended and the calling VCPU is blocked. If nothing else wouldn't be done the suspended domain would resume from the place where it called PSCI system suspend. This is expected if processing of the PSCI system suspend call fails. However, in the case of success the calling guest should resume (continue execution after the wake-up) from the entry point which is given as the first argument of the PSCI system suspend call. In addition to the entry point, the guest expects to start within the environment whose state matches the state after reset. This means that the guest should find reset register values, MMU disabled, etc. Thereby, the context of VCPU should be 'reset' (as if the system is comming out of reset), the program counter should contain entry point, which is 1st argument, and r0/x0 should contain context ID which is 2nd argument of PSCI system suspend call. The context of VCPU is set during resume path, to prevent it being overwritten by ctxt_switch_from after vcpu is blocked and scheduled out.

VCPU is marked as suspended with _VPF_suspended flag. A suspended domain will resume after the Xen receives an interrupt which is targeted to the domain, unblocks the domain's VCPU, and schedules it in. During the vcpu_unblock execution the VCPU is checked for VPF_suspended flag. If the flag is present, the context of that VCPU gets cleared and entry point/cid are set.

---
 xen/arch/arm/Makefile            |   1 +
 xen/arch/arm/domain.c            |   4 +
 xen/arch/arm/suspend.c           | 182 +++++++++++++++++++++++++++++++
 xen/arch/arm/vpsci.c             |  28 +++++
 xen/common/sched/core.c          |   8 ++
 xen/include/asm-arm/domain.h     |   3 +
 xen/include/asm-arm/perfc_defn.h |   1 +
 xen/include/asm-arm/psci.h       |   2 +
 xen/include/asm-arm/suspend.h    |  17 +++
 xen/include/xen/sched.h          |   3 +
 10 files changed, 249 insertions(+)
 create mode 100644 xen/arch/arm/suspend.c
 create mode 100644 xen/include/asm-arm/suspend.h
xen/arch/arm/Makefile +1 -0
index b5913c9d39..07dbbd99a3 100644
@@ -49,6 +49,7 @@ obj-y += setup.o
 obj-y += shutdown.o
 obj-y += smp.o
 obj-y += smpboot.o
+obj-y += suspend.o
 obj-y += sysctl.o
 obj-y += time.o
 obj-y += traps.o
xen/arch/arm/domain.c +4 -0
index 23c8b345d4..4110154bda 100644
@@ -40,6 +40,8 @@
 #include <asm/vtimer.h>
 #include <asm/vscmi.h>
 
+#include <public/sched.h>
+
 #include "vpci.h"
 #include "vuart.h"
 
@@ -101,6 +103,8 @@ static void ctxt_switch_from(struct vcpu *p)
     if ( is_idle_vcpu(p) )
         return;
 
+    /* VCPU's context should not be saved if its domain is suspended */
+
     p2m_save_state(p);
 
     /* CP 15 */
xen/arch/arm/suspend.c new file +182 -0

182 lines not on this page · raw message

xen/arch/arm/vpsci.c +28 -0
index c1e250be59..f4e6e92873 100644
@@ -18,6 +18,7 @@
 #include <asm/vgic.h>
 #include <asm/vpsci.h>
 #include <asm/event.h>
+#include <asm/suspend.h>
 
 #include <public/sched.h>
 
@@ -208,6 +209,11 @@ static void do_psci_0_2_system_reset(void)
     domain_shutdown(d,SHUTDOWN_reboot);
 }
 
+static int32_t do_psci_1_0_system_suspend(register_t epoint, register_t cid)
+{
+    return domain_suspend(epoint, cid);
+}
+
 static int32_t do_psci_1_0_features(uint32_t psci_func_id)
 {
     /* /!\ Ordered by function ID and not name */
@@ -225,6 +231,8 @@ static int32_t do_psci_1_0_features(uint32_t psci_func_id)
     case PSCI_0_2_FN32_SYSTEM_OFF:
     case PSCI_0_2_FN32_SYSTEM_RESET:
     case PSCI_1_0_FN32_PSCI_FEATURES:
+    case PSCI_1_0_FN32_SYSTEM_SUSPEND:
+    case PSCI_1_0_FN64_SYSTEM_SUSPEND:
     case ARM_SMCCC_VERSION_FID:
         return 0;
     default:
@@ -355,6 +363,26 @@ bool do_vpsci_0_2_call(struct cpu_user_regs *regs, uint32_t fid)
         return true;
     }
 
+    case PSCI_1_0_FN32_SYSTEM_SUSPEND:
+    case PSCI_1_0_FN64_SYSTEM_SUSPEND:
+    {
+        register_t epoint = PSCI_ARG(regs,1);
+        register_t cid = PSCI_ARG(regs,2);
+        register_t ret;
+
+        perfc_incr(vpsci_system_suspend);
+        /* Set the result to PSCI_SUCCESS if the call fails.
+         * Otherwise preserve the context_id in x0. For now 
+         * we don't support the case where the system is suspended
+         * to a shallower level and PSCI_SUCCESS is returned to the 
+         * caller.
+         */
+        ret = do_psci_1_0_system_suspend(epoint, cid);
+        if ( ret != PSCI_SUCCESS )
+            PSCI_SET_RESULT(regs, ret);
+        return true;
+    }
+
     default:
         return false;
     }
xen/common/sched/core.c +8 -0
index 8f4b1ca10d..4e1ea62c44 100644
@@ -38,6 +38,10 @@
 #include <xsm/xsm.h>
 #include <xen/err.h>
 
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
+#include <asm/suspend.h>
+#endif
+
 #include "private.h"
 
 #ifdef CONFIG_XEN_GUEST
@@ -957,6 +961,10 @@ void vcpu_unblock(struct vcpu *v)
 {
     if ( !test_and_clear_bit(_VPF_blocked, &v->pause_flags) )
         return;
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
+    if ( test_bit(_VPF_suspended, &v->pause_flags) )
+        vcpu_resume(v);
+#endif
 
     /* Polling period ends when a VCPU is unblocked. */
     if ( unlikely(v->poll_evtchn != 0) )
xen/include/asm-arm/domain.h +3 -0
index 413e5a2a18..715841e0b5 100644
@@ -203,6 +203,9 @@ struct arch_vcpu
     struct vtimer virt_timer;
     bool   vtimer_initialized;
 
+    register_t suspend_ep;
+    register_t suspend_cid;
+
     /*
      * The full P2M may require some cleaning (e.g when emulation
      * set/way). As the action can take a long time, it requires
xen/include/asm-arm/perfc_defn.h +1 -0
index 31f071222b..d71e91a5e4 100644
@@ -33,6 +33,7 @@ PERFCOUNTER(vpsci_system_reset,        "vpsci: system_reset")
 PERFCOUNTER(vpsci_cpu_suspend,         "vpsci: cpu_suspend")
 PERFCOUNTER(vpsci_cpu_affinity_info,   "vpsci: cpu_affinity_info")
 PERFCOUNTER(vpsci_features,            "vpsci: features")
+PERFCOUNTER(vpsci_system_suspend,      "vpsci: system_suspend")
 
 PERFCOUNTER(vcpu_kick,                 "vcpu: notify other vcpu")
 
xen/include/asm-arm/psci.h +2 -0
index 832f77afff..26462d0c47 100644
@@ -43,10 +43,12 @@ void call_psci_system_reset(void);
 #define PSCI_0_2_FN32_SYSTEM_OFF          PSCI_0_2_FN32(8)
 #define PSCI_0_2_FN32_SYSTEM_RESET        PSCI_0_2_FN32(9)
 #define PSCI_1_0_FN32_PSCI_FEATURES       PSCI_0_2_FN32(10)
+#define PSCI_1_0_FN32_SYSTEM_SUSPEND      PSCI_0_2_FN32(14)
 
 #define PSCI_0_2_FN64_CPU_SUSPEND         PSCI_0_2_FN64(1)
 #define PSCI_0_2_FN64_CPU_ON              PSCI_0_2_FN64(3)
 #define PSCI_0_2_FN64_AFFINITY_INFO       PSCI_0_2_FN64(4)
+#define PSCI_1_0_FN64_SYSTEM_SUSPEND      PSCI_0_2_FN64(14)
 
 /* PSCI v0.2 affinity level state returned by AFFINITY_INFO */
 #define PSCI_0_2_AFFINITY_LEVEL_ON      0
xen/include/asm-arm/suspend.h new file +17 -0
new file mode 100644
index 0000000000..fbaa414f0c
@@ -0,0 +1,17 @@
+#ifndef __ASM_ARM_SUSPEND_H__
+#define __ASM_ARM_SUSPEND_H__
+
+int32_t domain_suspend(register_t epoint, register_t cid);
+void vcpu_resume(struct vcpu *v);
+
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/xen/sched.h +3 -0
index 3b4ed3a2ab..b2f6d1af28 100644
@@ -903,6 +903,9 @@ static inline struct domain *next_domain_in_cpupool(
 /* VCPU is parked. */
 #define _VPF_parked          8
 #define VPF_parked           (1UL<<_VPF_parked)
+/* VCPU is suspended */
+#define _VPF_suspended       9
+#define VPF_suspended        (1UL<<_VPF_suspended)
 
 static inline bool vcpu_runnable(const struct vcpu *v)
 {
Signature · 1 line
2.37.1
#8 lore raw

PATCH 5/19 xen/x86: Move freeze/thaw_domains into common files

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

These functions will be reused by suspend/resume support for ARM.

---
 xen/common/domain.c     | 29 +++++++++++++++++++++++++++++
 xen/include/xen/sched.h |  3 +++
 2 files changed, 32 insertions(+)
xen/common/domain.c +29 -0
index 56d47dd664..5e5894c468 100644
@@ -1884,6 +1884,35 @@ int continue_hypercall_on_cpu(
     return 0;
 }
 
+
+void freeze_domains(void)
+{
+    struct domain *d;
+
+    rcu_read_lock(&domlist_read_lock);
+    /*
+     * Note that we iterate in order of domain-id. Hence we will pause dom0
+     * first which is required for correctness (as only dom0 can add domains to
+     * the domain list). Otherwise we could miss concurrently-created domains.
+     */
+    for_each_domain ( d )
+        domain_pause(d);
+    rcu_read_unlock(&domlist_read_lock);
+}
+
+void thaw_domains(void)
+{
+    struct domain *d;
+
+    rcu_read_lock(&domlist_read_lock);
+    for_each_domain ( d )
+    {
+        restore_vcpu_affinity(d);
+        domain_unpause(d);
+    }
+    rcu_read_unlock(&domlist_read_lock);
+}
+
 /*
  * Local variables:
  * mode: C
xen/include/xen/sched.h +3 -0
index 00a939aa01..c8ddfdd51c 100644
@@ -978,6 +978,9 @@ static inline struct vcpu *domain_vcpu(const struct domain *d,
     return vcpu_id >= d->max_vcpus ? NULL : d->vcpu[idx];
 }
 
+void freeze_domains(void);
+void thaw_domains(void);
+
 void cpu_init(void);
 
 /*
Signature · 1 line
2.37.1
#14 lore raw

PATCH 8/19 xen/arm: Add rcu_barrier() before enabling non-boot CPUs on resume

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

The rcu_barrier() has to be added to ensure that the per cpu area is freed before a non-boot CPU tries to initialize it (_free_percpu_area() has to be called before the init_percpu_area()). This scenario occurs when non-boot CPUs are hot-unplugged on suspend and hotplugged on resume.

---
 xen/arch/arm/suspend.c | 1 +
 1 file changed, 1 insertion(+)
xen/arch/arm/suspend.c +1 -0
index 0784979e4f..0c16cfc750 100644
@@ -153,6 +153,7 @@ static long system_suspend(void *data)
     system_state = SYS_STATE_resume;
 
 resume_nonboot_cpus:
+    rcu_barrier();
     enable_nonboot_cpus();
     thaw_domains();
     system_state = SYS_STATE_active;
Signature · 1 line
2.37.1
#5 lore raw

PATCH 2/19 watchdog: Introduce a separate struct for watchdog timers

In reply to a message not in the archive

This change is needed to properly implement the suspend/resume actions for the watchdog timers. To be able to restart watchdog timer after suspend we need to remember their frequency somewhere. To not bloat the struct timer a new struct watchdog_timer is introduced, containing the original timer and the last set timeout.

---
 xen/common/keyhandler.c    |  2 +-
 xen/common/sched/core.c    | 11 ++++++-----
 xen/include/xen/sched.h    |  3 ++-
 xen/include/xen/watchdog.h |  6 ++++++
 4 files changed, 15 insertions(+), 7 deletions(-)
xen/common/keyhandler.c +1 -1
index 8b9f378371..e9bd48fa5b 100644
@@ -289,7 +289,7 @@ static void dump_domains(unsigned char key)
         for ( i = 0 ; i < NR_DOMAIN_WATCHDOG_TIMERS; i++ )
             if ( test_bit(i, &d->watchdog_inuse_map) )
                 printk("    watchdog %d expires in %d seconds\n",
-                       i, (u32)((d->watchdog_timer[i].expires - NOW()) >> 30));
+                       i, (u32)((d->watchdog_timer[i].timer.expires - NOW()) >> 30));
 
         arch_dump_domain_info(d);
 
xen/common/sched/core.c +6 -5
index 4e1ea62c44..0ecb41cfe1 100644
@@ -1567,7 +1567,8 @@ static long domain_watchdog(struct domain *d, uint32_t id, uint32_t timeout)
         {
             if ( test_and_set_bit(id, &d->watchdog_inuse_map) )
                 continue;
-            set_timer(&d->watchdog_timer[id], NOW() + SECONDS(timeout));
+            d->watchdog_timer[id].timeout = timeout;
+            set_timer(&d->watchdog_timer[id].timer, NOW() + SECONDS(timeout));
             break;
         }
         spin_unlock(&d->watchdog_lock);
@@ -1583,12 +1584,12 @@ static long domain_watchdog(struct domain *d, uint32_t id, uint32_t timeout)
 
     if ( timeout == 0 )
     {
-        stop_timer(&d->watchdog_timer[id]);
+        stop_timer(&d->watchdog_timer[id].timer);
         clear_bit(id, &d->watchdog_inuse_map);
     }
     else
     {
-        set_timer(&d->watchdog_timer[id], NOW() + SECONDS(timeout));
+        set_timer(&d->watchdog_timer[id].timer, NOW() + SECONDS(timeout));
     }
 
     spin_unlock(&d->watchdog_lock);
@@ -1604,7 +1605,7 @@ void watchdog_domain_init(struct domain *d)
     d->watchdog_inuse_map = 0;
 
     for ( i = 0; i < NR_DOMAIN_WATCHDOG_TIMERS; i++ )
-        init_timer(&d->watchdog_timer[i], domain_watchdog_timeout, d, 0);
+        init_timer(&d->watchdog_timer[i].timer, domain_watchdog_timeout, d, 0);
 }
 
 void watchdog_domain_destroy(struct domain *d)
@@ -1612,7 +1613,7 @@ void watchdog_domain_destroy(struct domain *d)
     unsigned int i;
 
     for ( i = 0; i < NR_DOMAIN_WATCHDOG_TIMERS; i++ )
-        kill_timer(&d->watchdog_timer[i]);
+        kill_timer(&d->watchdog_timer[i].timer);
 }
 
 /*
xen/include/xen/sched.h +2 -1
index b2f6d1af28..7a4aef4c17 100644
@@ -24,6 +24,7 @@
 #include <asm/atomic.h>
 #include <xen/vpci.h>
 #include <xen/wait.h>
+#include <xen/watchdog.h>
 #include <public/xen.h>
 #include <public/domctl.h>
 #include <public/sysctl.h>
@@ -517,7 +518,7 @@ struct domain
 #define NR_DOMAIN_WATCHDOG_TIMERS 2
     spinlock_t watchdog_lock;
     uint32_t watchdog_inuse_map;
-    struct timer watchdog_timer[NR_DOMAIN_WATCHDOG_TIMERS];
+    struct watchdog_timer watchdog_timer[NR_DOMAIN_WATCHDOG_TIMERS];
 
     struct rcu_head rcu;
 
xen/include/xen/watchdog.h +6 -0
index 86fde0884a..3398dcba37 100644
@@ -8,6 +8,12 @@
 #define __XEN_WATCHDOG_H__
 
 #include <xen/types.h>
+#include <xen/timer.h>
+
+struct watchdog_timer {
+    struct timer timer;
+    uint32_t timeout;
+};
 
 #ifdef CONFIG_WATCHDOG
 
Signature · 1 line
2.37.1
#17 lore raw

PATCH 10/19 xen/arm: Suspend/resume GIC on system suspend/resume

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

GIC state is saved on system suspend by calling gic_suspend (this function does not change current state of the GIC but only saves the values of configuration registers).
The state of GIC has to be restored by calling gic_resume, but only if the gic_suspend has succeeded. If gic_suspend fails, we'll just restore interrupts configuration and abort suspend.

---
 xen/arch/arm/gic.c     |  4 +---
 xen/arch/arm/suspend.c | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +1 -3
index e9feb1fd3b..ef90664929 100644
@@ -476,9 +476,7 @@ int gic_suspend(void)
     if ( !gic_hw_ops->suspend || !gic_hw_ops->resume )
         return -ENOSYS;
 
-    gic_hw_ops->suspend();
-
-    return 0;
+    return gic_hw_ops->suspend();
 }
 
 void gic_resume(void)
xen/arch/arm/suspend.c +14 -0
index 0c16cfc750..b94a6df86d 100644
@@ -137,6 +137,7 @@ void vcpu_resume(struct vcpu *v)
 static long system_suspend(void *data)
 {
     int status;
+    unsigned long flags;
 
     BUG_ON(system_state != SYS_STATE_active);
 
@@ -150,8 +151,21 @@ static long system_suspend(void *data)
         goto resume_nonboot_cpus;
     }
 
+    local_irq_save(flags);
+    status = gic_suspend();
+    if ( status )
+    {
+        system_state = SYS_STATE_resume;
+        goto resume_irqs;
+    }
+
     system_state = SYS_STATE_resume;
 
+    gic_resume();
+
+resume_irqs:
+    local_irq_restore(flags);
+
 resume_nonboot_cpus:
     rcu_barrier();
     enable_nonboot_cpus();
Signature · 1 line
2.37.1
#19 lore raw

PATCH 11/19 xen/arm: Suspend/resume timer interrupt generation

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

Timer interrupts have to be disabled while the system is in suspend. Otherwise, a timer interrupt would fire and wake-up the system. Suspending the timer interrupts consists of disabling physical EL1 and EL2 timers. The resume consists only of raising timer softirq, which will trigger the generic timer code to reprogram the EL2 timer as needed. Enabling of EL1 physical timer will be triggered by an entity which uses it.

---
 xen/arch/arm/suspend.c     |  4 ++++
 xen/arch/arm/time.c        | 22 ++++++++++++++++++++++
 xen/include/asm-arm/time.h |  3 +++
 3 files changed, 29 insertions(+)
xen/arch/arm/suspend.c +4 -0
index b94a6df86d..05c43ce502 100644
@@ -151,6 +151,8 @@ static long system_suspend(void *data)
         goto resume_nonboot_cpus;
     }
 
+    time_suspend();
+
     local_irq_save(flags);
     status = gic_suspend();
     if ( status )
@@ -166,6 +168,8 @@ static long system_suspend(void *data)
 resume_irqs:
     local_irq_restore(flags);
 
+    time_resume();
+
 resume_nonboot_cpus:
     rcu_barrier();
     enable_nonboot_cpus();
xen/arch/arm/time.c +22 -0
index dec53b5f7d..ca54bcfe68 100644
@@ -363,6 +363,28 @@ void domain_set_time_offset(struct domain *d, int64_t time_offset_seconds)
     /* XXX update guest visible wallclock time */
 }
 
+void time_suspend(void)
+{
+    /* Disable physical EL1 timer */
+    WRITE_SYSREG(0, CNTP_CTL_EL0);
+
+    /* Disable hypervisor's timer */
+    WRITE_SYSREG(0, CNTHP_CTL_EL2);
+    isb();
+}
+
+void time_resume(void)
+{
+    /*
+     * Raising timer softirq will trigger generic timer code to reprogram_timer
+     * with the correct timeout value (which is not known here). There is no
+     * need to do anything else in order to recover the time keeping from power
+     * down, because the system counter is not affected by the power down (it
+     * resides out of the ARM's cluster in an always-on part of the SoC).
+     */
+    raise_softirq(TIMER_SOFTIRQ);
+}
+
 static int cpu_time_callback(struct notifier_block *nfb,
                              unsigned long action,
                              void *hcpu)
xen/include/asm-arm/time.h +3 -0
index 4b401c1110..ded93b490a 100644
@@ -107,6 +107,9 @@ void preinit_xen_time(void);
 
 void force_update_vcpu_system_time(struct vcpu *v);
 
+void time_suspend(void);
+void time_resume(void);
+
 #endif /* __ARM_TIME_H__ */
 /*
  * Local variables:
Signature · 1 line
2.37.1
#23 lore raw

PATCH 13/19 xen/arm: Resume memory management on Xen resume

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

The MMU needs to be enabled in the resume flow before the context can be restored (we need to be able to access the context data by virtual address in order to restore it). The configuration of system registers prior to branching to the routine that sets up the page tables is copied from xen/arch/arm/arm64/head.S.
After the MMU is enabled, the content of TTBR0_EL2 is changed to point to init_ttbr (page tables used at runtime).

At boot the init_ttbr variable is updated when a secondary CPU is hotplugged. In the scenario where there is only one physical CPU in the system, the init_ttbr would not be initialized for the use in resume flow. To get the variable initialized in all scenarios in this patch we add that the boot CPU updates init_ttbr after it sets the page tables for runtime.

After the memory management is resumed, the SCTLR_WXN in SCTLR_EL2 has to be set in order to configure that a mapping cannot be both writable and executable (this was configured prior to suspend). This is done using an existing function (mmu_init_secondary_cpu).

Update: moved hyp_resume to head.S to place it near the rest of the start code

---
 xen/arch/arm/arm64/entry.S      |  2 ++
 xen/arch/arm/arm64/head.S       | 30 ++++++++++++++++++++++++++++++
 xen/arch/arm/mm.c               |  1 +
 xen/arch/arm/suspend.c          |  6 ++++++
 xen/include/asm-arm/processor.h | 22 ++++++++++++++++++++++
 5 files changed, 61 insertions(+)
xen/arch/arm/arm64/entry.S +2 -0
index fc3811ad0a..f49f1daf46 100644
@@ -1,4 +1,6 @@
 #include <asm/current.h>
+#include <asm/macros.h>
+#include <asm/page.h>
 #include <asm/regs.h>
 #include <asm/alternative.h>
 #include <asm/smccc.h>
xen/arch/arm/arm64/head.S +30 -0
index 8857955699..82d83214dc 100644
@@ -958,6 +958,36 @@ ENTRY(efi_xen_start)
 ENDPROC(efi_xen_start)
 
 ENTRY(hyp_resume)
+        msr   DAIFSet, 0xf           /* Disable all interrupts */
+
+        tlbi  alle2
+        dsb   sy                     /* Ensure completion of TLB flush */
+        isb
+
+        ldr   x0, =start
+        adr   x19, start             /* x19 := paddr (start) */
+        sub   x20, x19, x0           /* x20 := phys-offset */
+
+        mov   x22, #0                /* x22 := is_secondary_cpu */
+
+        bl    check_cpu_mode
+        bl    cpu_init
+        bl    create_page_tables
+        bl    enable_mmu
+
+        ldr   x0, =mmu_resumed      /* Explicit vaddr, not RIP-relative */
+        br    x0                    /* Get a proper vaddr into PC */
+
+mmu_resumed:
+        ldr   x4, =init_ttbr         /* VA of TTBR0_EL2 stashed by CPU 0 */
+        ldr   x4, [x4]               /* Actual value */
+        dsb   sy
+        msr   TTBR0_EL2, x4
+        dsb   sy
+        isb
+        tlbi  alle2
+        dsb   sy                     /* Ensure completion of TLB flush */
+        isb
         b .
 
 /*
xen/arch/arm/mm.c +1 -0
index eea926d823..29cdaff3bf 100644
@@ -708,6 +708,7 @@ void __init setup_pagetables(unsigned long boot_phys_offset)
     switch_ttbr(ttbr);
 
     xen_pt_enforce_wnx();
+    init_secondary_pagetables(0);
 
 #ifdef CONFIG_ARM_32
     per_cpu(xen_pgtable, 0) = cpu0_pgtable;
xen/arch/arm/suspend.c +6 -0
index a0258befc9..aa5ee4714b 100644
@@ -167,6 +167,12 @@ static long system_suspend(void *data)
 
     system_state = SYS_STATE_resume;
 
+    /*
+     * SCTLR_WXN needs to be set to configure that a mapping cannot be both
+     * writable and executable. This is done by mmu_init_secondary_cpu.
+     */
+    mmu_init_secondary_cpu();
+
     gic_resume();
 
 resume_irqs:
xen/include/asm-arm/processor.h +22 -0
index 8ab2940f68..ecf97f1ab4 100644
@@ -133,6 +133,28 @@
 #define TTBCR_PD1       (_AC(1,U)<<5)
 
 /* SCTLR System Control Register. */
+/* HSCTLR is a subset of this. */
+#define SCTLR_TE        (_AC(1,U)<<30)
+#define SCTLR_AFE       (_AC(1,U)<<29)
+#define SCTLR_TRE       (_AC(1,U)<<28)
+#define SCTLR_NMFI      (_AC(1,U)<<27)
+#define SCTLR_EE        (_AC(1,U)<<25)
+#define SCTLR_VE        (_AC(1,U)<<24)
+#define SCTLR_U         (_AC(1,U)<<22)
+#define SCTLR_FI        (_AC(1,U)<<21)
+#define SCTLR_WXN       (_AC(1,U)<<19)
+#define SCTLR_HA        (_AC(1,U)<<17)
+#define SCTLR_RR        (_AC(1,U)<<14)
+#define SCTLR_V         (_AC(1,U)<<13)
+#define SCTLR_I         (_AC(1,U)<<12)
+#define SCTLR_Z         (_AC(1,U)<<11)
+#define SCTLR_SW        (_AC(1,U)<<10)
+#define SCTLR_B         (_AC(1,U)<<7)
+#define SCTLR_C         (_AC(1,U)<<2)
+#define SCTLR_A         (_AC(1,U)<<1)
+#define SCTLR_M         (_AC(1,U)<<0)
+
+#define HSCTLR_BASE     _AC(0x30c51878,U)
 
 /* Bits specific to SCTLR_EL1 for Arm32 */
 
Signature · 1 line
2.37.1
#27 lore raw

PATCH 15/19 xen/arm: Resume Dom0 after Xen resumes

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

The resume of Dom0 should always follow Xen's resume. This is done by unblocking the first vCPU of Dom0.

---
 xen/arch/arm/suspend.c | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/arm/suspend.c +3 -0
index 13d1aba658..4a690eac3b 100644
@@ -206,6 +206,9 @@ resume_nonboot_cpus:
     system_state = SYS_STATE_active;
     dsb(sy);
 
+    /* Wake-up hardware domain (should always resume after Xen) */
+    vcpu_unblock(hardware_domain->vcpu[0]);
+
     return status;
 }
 
Signature · 1 line
2.37.1
#21 lore raw

PATCH 12/19 xen/arm: Implement PSCI SYSTEM_SUSPEND call (physical interface)

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

PSCI system suspend function shall be invoked to finalize Xen suspend procedure. Resume entry point, which needs to be passed via 1st argument of PSCI system suspend call to the EL3, is hyp_resume. For now, hyp_resume is just a placeholder that will be implemented in assembly. Context ID, which is 2nd argument of system suspend PSCI call, is unused, as in Linux.

Update: moved hyp_resume to head.S to place it near the rest of the start code

---
 xen/arch/arm/arm64/head.S     |  3 +++
 xen/arch/arm/psci.c           | 16 ++++++++++++++++
 xen/arch/arm/suspend.c        |  4 ++++
 xen/include/asm-arm/psci.h    |  1 +
 xen/include/asm-arm/suspend.h |  1 +
 5 files changed, 25 insertions(+)
xen/arch/arm/arm64/head.S +3 -0
index aa1f88c764..8857955699 100644
@@ -957,6 +957,9 @@ ENTRY(efi_xen_start)
         b     real_start_efi
 ENDPROC(efi_xen_start)
 
+ENTRY(hyp_resume)
+        b .
+
 /*
  * Local variables:
  * mode: ASM
xen/arch/arm/psci.c +16 -0
index 0c90c2305c..43a67eb345 100644
@@ -25,6 +25,7 @@
 #include <asm/cpufeature.h>
 #include <asm/psci.h>
 #include <asm/acpi.h>
+#include <asm/suspend.h>
 
 /*
  * While a 64-bit OS can make calls with SMC32 calling conventions, for
@@ -68,6 +69,21 @@ void call_psci_cpu_off(void)
     }
 }
 
+int call_psci_system_suspend(void)
+{
+#ifdef CONFIG_ARM_64
+    struct arm_smccc_res res;
+
+    /* 2nd argument (context ID) is not used */
+    arm_smccc_smc(PSCI_1_0_FN64_SYSTEM_SUSPEND, __pa(hyp_resume), &res);
+
+    return PSCI_RET(res);
+#else
+    /* not supported */
+    return 1;
+#endif
+}
+
 void call_psci_system_off(void)
 {
     if ( psci_ver > PSCI_VERSION(0, 1) )
xen/arch/arm/suspend.c +4 -0
index 05c43ce502..a0258befc9 100644
@@ -161,6 +161,10 @@ static long system_suspend(void *data)
         goto resume_irqs;
     }
 
+    status = call_psci_system_suspend();
+    if ( status )
+        dprintk(XENLOG_ERR, "PSCI system suspend failed, err=%d\n", status);
+
     system_state = SYS_STATE_resume;
 
     gic_resume();
xen/include/asm-arm/psci.h +1 -0
index 26462d0c47..9f6116a224 100644
@@ -20,6 +20,7 @@ extern uint32_t psci_ver;
 
 int psci_init(void);
 int call_psci_cpu_on(int cpu);
+int call_psci_system_suspend(void);
 void call_psci_cpu_off(void);
 void call_psci_system_off(void);
 void call_psci_system_reset(void);
xen/include/asm-arm/suspend.h +1 -0
index fbaa414f0c..29420e27fa 100644
@@ -3,6 +3,7 @@
 
 int32_t domain_suspend(register_t epoint, register_t cid);
 void vcpu_resume(struct vcpu *v);
+void hyp_resume(void);
 
 #endif
 
Signature · 1 line
2.37.1
#25 lore raw

PATCH 14/19 xen/arm: Save/restore context on suspend/resume

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

The context of CPU general purpose and system control registers has to be saved on suspend and restored on resume. This is implemented in hyp_suspend and before the return from hyp_resume function. The hyp_suspend is invoked just before the PSCI system suspend call is issued to the ATF. The hyp_suspend has to return a non-zero value so that the calling 'if' statement evaluates to true, causing the system suspend to be invoked. Upon the resume, context saved on suspend will be restored, including the link register. Therefore, after restoring the context the control flow will return to the address pointed by the saved link register, which is the place from which the hyp_suspend was called. To ensure that the calling 'if' statement doesn't again evaluate to true and initiate system suspend, hyp_resume has to return a zero value after restoring the context.
Note that the order of saving register context into cpu_context structure has to match the order of restoring.
Since the suspend/resume is supported only for arm64, we define a null cpu_context structure so arm32 could compile.

Update: moved hyp_resume to head.S to place it near the rest of the start code

---
 xen/arch/arm/arm64/head.S     | 90 ++++++++++++++++++++++++++++++++++-
 xen/arch/arm/suspend.c        | 25 ++++++++--
 xen/include/asm-arm/suspend.h | 22 +++++++++
 3 files changed, 133 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/head.S +89 -1
index 82d83214dc..e2c46a864c 100644
@@ -957,6 +957,53 @@ ENTRY(efi_xen_start)
         b     real_start_efi
 ENDPROC(efi_xen_start)
 
+/*
+ * void hyp_suspend(struct cpu_context *ptr)
+ *
+ * x0 - pointer to the storage where callee's context will be saved
+ *
+ * CPU context saved here will be restored on resume in hyp_resume function.
+ * hyp_suspend shall return a non-zero value. Upon restoring context
+ * hyp_resume shall return value zero instead. From C code that invokes
+ * hyp_suspend, the return value is interpreted to determine whether the context
+ * is saved (hyp_suspend) or restored (hyp_resume).
+ */
+ENTRY(hyp_suspend)
+        /* Store callee-saved registers */
+        stp     x19, x20, [x0], #16
+        stp     x21, x22, [x0], #16
+        stp     x23, x24, [x0], #16
+        stp     x25, x26, [x0], #16
+        stp     x27, x28, [x0], #16
+        stp     x29, lr, [x0], #16
+
+        /* Store stack-pointer */
+        mov     x2, sp
+        str     x2, [x0], #8
+
+        /* Store system control registers */
+        mrs     x2, VBAR_EL2
+        str     x2, [x0], #8
+        mrs     x2, VTCR_EL2
+        str     x2, [x0], #8
+        mrs     x2, VTTBR_EL2
+        str     x2, [x0], #8
+        mrs     x2, TPIDR_EL2
+        str     x2, [x0], #8
+        mrs     x2, MDCR_EL2
+        str     x2, [x0], #8
+        mrs     x2, HSTR_EL2
+        str     x2, [x0], #8
+        mrs     x2, CPTR_EL2
+        str     x2, [x0], #8
+        mrs     x2, HCR_EL2
+        str     x2, [x0], #8
+
+        /* hyp_suspend must return a non-zero value */
+        mov     x0, #1
+        ret
+
+
 ENTRY(hyp_resume)
         msr   DAIFSet, 0xf           /* Disable all interrupts */
 
@@ -988,7 +1035,48 @@ mmu_resumed:
         tlbi  alle2
         dsb   sy                     /* Ensure completion of TLB flush */
         isb
-        b .
+
+        /* Now we can access the cpu_context, so restore the context here */
+        ldr     x0, =cpu_context
+
+        /* Restore callee-saved registers */
+        ldp     x19, x20, [x0], #16
+        ldp     x21, x22, [x0], #16
+        ldp     x23, x24, [x0], #16
+        ldp     x25, x26, [x0], #16
+        ldp     x27, x28, [x0], #16
+        ldp     x29, lr, [x0], #16
+
+        /* Restore stack pointer */
+        ldr     x2, [x0], #8
+        mov     sp, x2
+
+        /* Restore system control registers */
+        ldr     x2, [x0], #8
+        msr     VBAR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     VTCR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     VTTBR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     TPIDR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     MDCR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     HSTR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     CPTR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     HCR_EL2, x2
+        isb
+
+        /* Since context is restored return from this function will appear as
+         * return from hyp_suspend. To distinguish a return from hyp_suspend
+         * which is called upon finalizing the suspend, as opposed to return
+         * from this function which executes on resume, we need to return zero
+         * value here. */
+        mov x0, #0
+        ret
 
 /*
  * Local variables:
xen/arch/arm/suspend.c +22 -3
index aa5ee4714b..13d1aba658 100644
@@ -133,6 +133,11 @@ void vcpu_resume(struct vcpu *v)
     clear_bit(_VPF_suspended, &v->pause_flags);
 }
 
+#ifndef CONFIG_ARM_64
+/* not supported on ARM_32 */
+int32_t hyp_suspend(struct cpu_context *ptr) { return 1; }
+#endif
+
 /* Xen suspend. Note: data is not used (suspend is the suspend to RAM) */
 static long system_suspend(void *data)
 {
@@ -161,9 +166,23 @@ static long system_suspend(void *data)
         goto resume_irqs;
     }
 
-    status = call_psci_system_suspend();
-    if ( status )
-        dprintk(XENLOG_ERR, "PSCI system suspend failed, err=%d\n", status);
+    if ( hyp_suspend(&cpu_context) )
+    {
+        status = call_psci_system_suspend();
+        /*
+         * If suspend is finalized properly by above system suspend PSCI call,
+         * the code below in this 'if' branch will never execute. Execution
+         * will continue from hyp_resume which is the hypervisor's resume point.
+         * In hyp_resume CPU context will be restored and since link-register is
+         * restored as well, it will appear to return from hyp_suspend. The
+         * difference in returning from hyp_suspend on system suspend versus
+         * resume is in function's return value: on suspend, the return value is
+         * a non-zero value, on resume it is zero. That is why the control flow
+         * will not re-enter this 'if' branch on resume.
+         */
+        if ( status )
+            dprintk(XENLOG_ERR, "PSCI system suspend failed, err=%d\n", status);
+    }
 
     system_state = SYS_STATE_resume;
 
xen/include/asm-arm/suspend.h +22 -0
index 29420e27fa..70dbf4e208 100644
@@ -1,9 +1,31 @@
 #ifndef __ASM_ARM_SUSPEND_H__
 #define __ASM_ARM_SUSPEND_H__
 
+#ifdef CONFIG_ARM_64
+struct cpu_context {
+    uint64_t callee_regs[12];
+    uint64_t sp;
+    uint64_t vbar_el2;
+    uint64_t vtcr_el2;
+    uint64_t vttbr_el2;
+    uint64_t tpidr_el2;
+    uint64_t mdcr_el2;
+    uint64_t hstr_el2;
+    uint64_t cptr_el2;
+    uint64_t hcr_el2;
+} __aligned(16);
+#else
+struct cpu_context {
+    uint8_t pad;
+};
+#endif
+
+extern struct cpu_context cpu_context;
+
 int32_t domain_suspend(register_t epoint, register_t cid);
 void vcpu_resume(struct vcpu *v);
 void hyp_resume(void);
+int32_t hyp_suspend(struct cpu_context *ptr);
 
 #endif
 
Signature · 1 line
2.37.1
#28 lore raw

PATCH 16/19 xen/arm: Suspend/resume console on Xen suspend/resume

In reply to a message not in the archive

From: Mirela Simonovic <[email protected]>

This is done using generic console_suspend/resume functions that cause uart driver specific suspend/resume handlers to be called for each initialized port (if the port has suspend/resume driver handlers implemented).

---
 xen/arch/arm/suspend.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
xen/arch/arm/suspend.c +14 -0
index 4a690eac3b..cf3aab0099 100644
@@ -14,6 +14,7 @@
 
 #include <xen/sched.h>
 #include <xen/cpu.h>
+#include <xen/console.h>
 #include <asm/cpufeature.h>
 #include <asm/event.h>
 #include <asm/psci.h>
@@ -166,6 +167,15 @@ static long system_suspend(void *data)
         goto resume_irqs;
     }
 
+    dprintk(XENLOG_DEBUG, "Suspend\n");
+    status = console_suspend();
+    if ( status )
+    {
+        dprintk(XENLOG_ERR, "Failed to suspend the console, err=%d\n", status);
+        system_state = SYS_STATE_resume;
+        goto resume_console;
+    }
+
     if ( hyp_suspend(&cpu_context) )
     {
         status = call_psci_system_suspend();
@@ -192,6 +202,10 @@ static long system_suspend(void *data)
      */
     mmu_init_secondary_cpu();
 
+resume_console:
+    console_resume();
+    dprintk(XENLOG_DEBUG, "Resume\n");
+
     gic_resume();
 
 resume_irqs:
Signature · 1 line
2.37.1
#31 lore raw

PATCH 17/19 xen: don't free percpu areas during suspend

In reply to a message not in the archive

From: Juergen Gross <[email protected]>

Instead of freeing percpu areas during suspend and allocating them again when resuming keep them. Only free an area in case a cpu didn't come up again when resuming.

It should be noted that there is a potential change in behaviour as the percpu areas are no longer zeroed out during suspend/resume. While I have checked the called cpu notifier hooks to cope with that there might be some well hidden dependency on the previous behaviour. OTOH a component not registering itself for cpu down/up and expecting to see a zeroed percpu variable after suspend/resume is kind of broken already. And the opposite case, where a component is not registered to be called for cpu down/up and is not expecting a percpu variable suddenly to be zero due to suspend/resume is much more probable, especially as the suspend/resume functionality seems not to be tested that often.

---
 xen/arch/arm/percpu.c | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/arm/percpu.c +3 -0
index 25442c48fe..0642705544 100644
@@ -58,10 +58,13 @@ static int cpu_percpu_callback(
     switch ( action )
     {
     case CPU_UP_PREPARE:
+      if ( system_state != SYS_STATE_resume )
         rc = init_percpu_area(cpu);
         break;
     case CPU_UP_CANCELED:
     case CPU_DEAD:
+    case CPU_RESUME_FAILED:
+      if ( system_state != SYS_STATE_suspend )
         free_percpu_area(cpu);
         break;
     default:
Signature · 1 line
2.37.1
#34 lore raw

PATCH 18/19 timers: Don't migrate timers during suspend

In reply to a message not in the archive

Migrating timers during suspend causes Dom0 to freeze after resume.

---
 xen/common/timer.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/common/timer.c +2 -1
index 1bb265ceea..52d4f72a76 100644
@@ -658,7 +658,8 @@ static int cpu_callback(
     case CPU_UP_CANCELED:
     case CPU_DEAD:
     case CPU_RESUME_FAILED:
-        migrate_timers_from_cpu(cpu);
+        if ( system_state != SYS_STATE_suspend )
+            migrate_timers_from_cpu(cpu);
 
         if ( !park_offline_cpus && system_state != SYS_STATE_suspend )
             free_percpu_timers(cpu);
Signature · 1 line
2.37.1
#36 lore raw

PATCH 19/19 Fix misleading indentation gcc warning

In reply to a message not in the archive

From: Oleksandr Andrushchenko <[email protected]>

percpu.c: In function 'cpu_percpu_callback':
percpu.c:61:7: error: this 'if' clause does not guard... [-Werror=misleading-indentation]

       if ( system_state != SYS_STATE_resume )
       ^~
percpu.c:63:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
         break;
         ^~~~~
percpu.c:67:7: error: this 'if' clause does not guard... [-Werror=misleading-indentation]
       if ( system_state != SYS_STATE_suspend )
       ^~
percpu.c:69:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
         break;
         ^~~~~
Fixes c3109b76d967 ("xen: don't free percpu areas during suspend")
---
 xen/arch/arm/percpu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/arm/percpu.c +2 -2
index 0642705544..9eab1e6d7c 100644
@@ -60,13 +60,13 @@ static int cpu_percpu_callback(
     case CPU_UP_PREPARE:
       if ( system_state != SYS_STATE_resume )
         rc = init_percpu_area(cpu);
-        break;
+      break;
     case CPU_UP_CANCELED:
     case CPU_DEAD:
     case CPU_RESUME_FAILED:
       if ( system_state != SYS_STATE_suspend )
         free_percpu_area(cpu);
-        break;
+      break;
     default:
         break;
     }
Signature · 1 line
2.37.1
#2 lore raw

Re: [PATCH 01/19] xen/arm: Implement PSCI system suspend

In reply to Mykyta Poturai

Hi Mykyta,

I couldn't find any cover letter for this series in neither my inbox nor xen-devel. Can you provide one explain the goal of this series (you seem to have a mix of domain suspend and host suspend)? If it is also based on an existing series, then it would be nice to mention it (this would be helpful to look at the existings and check if they were addressed).

Also, we are currently in the middle of the code freeze. So the review will likely be quite slow until 4.17 is out and I caught up with the rest of my backlog.

That said from a brief look, I see you included a patch that was merged around 4.12. So is this series actually based on staging?

If not, then the first step would be to rebase this series to the latest staging.

Cheers,

Quoted 450 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

The implementation consists of:

(quoted diff) +0 -3
-Adding PSCI system suspend call as new PSCI function
-Trapping PSCI system_suspend HVC
-Implementing PSCI system suspend call (virtual interface that allows
  guests to suspend themselves)

The PSCI system suspend should be called by a guest from its boot VCPU. Non-boot VCPUs of the guest should be hot-unplugged using PSCI CPU_OFF call prior to issuing PSCI system suspend. Interrupts that are left enabled by the guest are assumed to be its wake-up interrupts. Therefore, a wake-up interrupt triggers the resume of the guest. Guest should resume regardless of the state of Xen (suspended or not).

When a guest calls PSCI system suspend the respective domain will be suspended if the following conditions are met:
1) Given resume entry point is not invalid
2) Other (if any) VCPUs of the calling guest are hot-unplugged

If the conditions above are met the calling domain is labeled as suspended and the calling VCPU is blocked. If nothing else wouldn't be done the suspended domain would resume from the place where it called PSCI system suspend. This is expected if processing of the PSCI system suspend call fails. However, in the case of success the calling guest should resume (continue execution after the wake-up) from the entry point which is given as the first argument of the PSCI system suspend call. In addition to the entry point, the guest expects to start within the environment whose state matches the state after reset. This means that the guest should find reset register values, MMU disabled, etc. Thereby, the context of VCPU should be 'reset' (as if the system is comming out of reset), the program counter should contain entry point, which is 1st argument, and r0/x0 should contain context ID which is 2nd argument of PSCI system suspend call. The context of VCPU is set during resume path, to prevent it being overwritten by ctxt_switch_from after vcpu is blocked and scheduled out.

VCPU is marked as suspended with _VPF_suspended flag. A suspended domain will resume after the Xen receives an interrupt which is targeted to the domain, unblocks the domain's VCPU, and schedules it in. During the vcpu_unblock execution the VCPU is checked for VPF_suspended flag. If the flag is present, the context of that VCPU gets cleared and entry point/cid are set.

---
  xen/arch/arm/Makefile            |   1 +
  xen/arch/arm/domain.c            |   4 +
  xen/arch/arm/suspend.c           | 182 +++++++++++++++++++++++++++++++
  xen/arch/arm/vpsci.c             |  28 +++++
  xen/common/sched/core.c          |   8 ++
  xen/include/asm-arm/domain.h     |   3 +
  xen/include/asm-arm/perfc_defn.h |   1 +
  xen/include/asm-arm/psci.h       |   2 +
  xen/include/asm-arm/suspend.h    |  17 +++
  xen/include/xen/sched.h          |   3 +
  10 files changed, 249 insertions(+)

create mode 100644 xen/arch/arm/suspend.c create mode 100644 xen/include/asm-arm/suspend.h

xen/arch/arm/Makefile +1 -0
index b5913c9d39..07dbbd99a3 100644
@@ -49,6 +49,7 @@ obj-y += setup.o
  obj-y += shutdown.o
  obj-y += smp.o
  obj-y += smpboot.o
+obj-y += suspend.o
  obj-y += sysctl.o
  obj-y += time.o
  obj-y += traps.o
xen/arch/arm/domain.c +4 -0
index 23c8b345d4..4110154bda 100644
@@ -40,6 +40,8 @@
  #include <asm/vtimer.h>
  #include <asm/vscmi.h>
 
+#include <public/sched.h>
+
  #include "vpci.h"
  #include "vuart.h"
 
@@ -101,6 +103,8 @@ static void ctxt_switch_from(struct vcpu *p)
      if ( is_idle_vcpu(p) )
          return;
 
+    /* VCPU's context should not be saved if its domain is suspended */
+
      p2m_save_state(p);
 
      /* CP 15 */
xen/arch/arm/suspend.c new file +182 -0
new file mode 100644
index 0000000000..987ba6ac11
@@ -0,0 +1,182 @@
+/*
+ * Copyright (C) 2022 EPAM Systems Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+#include <xen/sched.h>
+#include <asm/cpufeature.h>
+#include <asm/event.h>
+#include <asm/psci.h>
+#include <asm/suspend.h>
+#include <public/sched.h>
+
+struct cpu_context cpu_context;
+
+/* Reset values of VCPU architecture specific registers */
+static void vcpu_arch_reset(struct vcpu *v)
+{
+    v->arch.ttbr0 = 0;
+    v->arch.ttbr1 = 0;
+    v->arch.ttbcr = 0;
+
+    v->arch.csselr = 0;
+    v->arch.cpacr = 0;
+    v->arch.contextidr = 0;
+    v->arch.tpidr_el0 = 0;
+    v->arch.tpidrro_el0 = 0;
+    v->arch.tpidr_el1 = 0;
+    v->arch.vbar = 0;
+    v->arch.dacr = 0;
+    v->arch.par = 0;
+#if defined(CONFIG_ARM_32)
+    v->arch.mair0 = 0;
+    v->arch.mair1 = 0;
+    v->arch.amair0 = 0;
+    v->arch.amair1 = 0;
+#else
+    v->arch.mair = 0;
+    v->arch.amair = 0;
+#endif
+    /* Fault Status */
+#if defined(CONFIG_ARM_32)
+    v->arch.dfar = 0;
+    v->arch.ifar = 0;
+    v->arch.dfsr = 0;
+#elif defined(CONFIG_ARM_64)
+    v->arch.far = 0;
+    v->arch.esr = 0;
+#endif
+
+    v->arch.ifsr  = 0;
+    v->arch.afsr0 = 0;
+    v->arch.afsr1 = 0;
+
+#ifdef CONFIG_ARM_32
+    v->arch.joscr = 0;
+    v->arch.jmcr = 0;
+#endif
+
+    if ( is_32bit_domain(v->domain) && cpu_has_thumbee )
+    {
+        v->arch.teecr = 0;
+        v->arch.teehbr = 0;
+    }
+}
+
+
+static void vcpu_suspend(register_t epoint, register_t cid)
+{
+    struct vcpu *v = current;
+
+    v->arch.suspend_ep = epoint;
+    v->arch.suspend_cid = cid;
+    set_bit(_VPF_suspended, &v->pause_flags);
+    return;
+}
+
+/*
+ * This function sets the context of current VCPU to the state which is expected
+ * by the guest on resume. The expected VCPU state is:
+ * 1) pc to contain resume entry point (1st argument of PSCI SYSTEM_SUSPEND)
+ * 2) r0/x0 to contain context ID (2nd argument of PSCI SYSTEM_SUSPEND)
+ * 3) All other general purpose and system registers should have reset values
+ */
+void vcpu_resume(struct vcpu *v)
+{
+
+    struct vcpu_guest_context ctxt;
+
+    /* Make sure that VCPU guest regs are zeroed */
+    memset(&ctxt, 0, sizeof(ctxt));
+
+    /* Set non-zero values to the registers prior to copying */
+    ctxt.user_regs.pc64 = (u64)v->arch.suspend_ep;
+
+    if ( is_32bit_domain(v->domain) )
+    {
+        ctxt.user_regs.r0_usr = v->arch.suspend_cid;
+        ctxt.user_regs.cpsr = PSR_GUEST32_INIT;
+
+        /* Thumb set is allowed only for 32-bit domain */
+        if ( v->arch.suspend_ep & 1 )
+        {
+            ctxt.user_regs.cpsr |= PSR_THUMB;
+            ctxt.user_regs.pc64 &= ~(u64)1;
+        }
+    }
+#ifdef CONFIG_ARM_64
+    else
+    {
+        ctxt.user_regs.x0 = v->arch.suspend_cid;
+        ctxt.user_regs.cpsr = PSR_GUEST64_INIT;
+    }
+#endif
+    ctxt.sctlr = SCTLR_GUEST_INIT;
+    ctxt.flags = VGCF_online;
+
+    /* Reset architecture specific registers */
+    vcpu_arch_reset(v);
+
+    /* Initialize VCPU registers */
+    arch_set_info_guest(v, &ctxt);
+    clear_bit(_VPF_suspended, &v->pause_flags);
+}
+
+int32_t domain_suspend(register_t epoint, register_t cid)
+{
+    struct vcpu *v;
+    struct domain *d = current->domain;
+    bool is_thumb = epoint & 1;
+
+    dprintk(XENLOG_DEBUG,
+            "Dom%d suspend: epoint=0x%"PRIregister", cid=0x%"PRIregister"\n",
+            d->domain_id, epoint, cid);
+
+    /* THUMB set is not allowed with 64-bit domain */
+    if ( is_64bit_domain(d) && is_thumb )
+        return PSCI_INVALID_ADDRESS;
+
+    /* Ensure that all CPUs other than the calling one are offline */
+    for_each_vcpu ( d, v )
+    {
+        if ( v != current && is_vcpu_online(v) )
+            return PSCI_DENIED;
+    }
+
+    //TODO: add support for suspending from any VCPU
+    if (current->vcpu_id != 0)
+        return PSCI_DENIED;
+
+    /*
+     * Prepare the calling VCPU for suspend (reset its context, save entry point
+     * into pc and context ID into r0/x0 as specified by PSCI SYSTEM_SUSPEND)
+     */
+    vcpu_suspend(epoint, cid);
+
+    /*
+     * The calling domain is suspended by blocking its last running VCPU. If an
+     * event is pending the domain will resume right away (VCPU will not block,
+     * but when scheduled in it will resume from the given entry point).
+     */
+    vcpu_block_unless_event_pending(current);
+
+    return PSCI_SUCCESS;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vpsci.c +28 -0
index c1e250be59..f4e6e92873 100644
@@ -18,6 +18,7 @@
  #include <asm/vgic.h>
  #include <asm/vpsci.h>
  #include <asm/event.h>
+#include <asm/suspend.h>
 
  #include <public/sched.h>
 
@@ -208,6 +209,11 @@ static void do_psci_0_2_system_reset(void)
      domain_shutdown(d,SHUTDOWN_reboot);
  }
 
+static int32_t do_psci_1_0_system_suspend(register_t epoint, register_t cid)
+{
+    return domain_suspend(epoint, cid);
+}
+
  static int32_t do_psci_1_0_features(uint32_t psci_func_id)
  {
      /* /!\ Ordered by function ID and not name */
@@ -225,6 +231,8 @@ static int32_t do_psci_1_0_features(uint32_t psci_func_id)
      case PSCI_0_2_FN32_SYSTEM_OFF:
      case PSCI_0_2_FN32_SYSTEM_RESET:
      case PSCI_1_0_FN32_PSCI_FEATURES:
+    case PSCI_1_0_FN32_SYSTEM_SUSPEND:
+    case PSCI_1_0_FN64_SYSTEM_SUSPEND:
      case ARM_SMCCC_VERSION_FID:
          return 0;
      default:
@@ -355,6 +363,26 @@ bool do_vpsci_0_2_call(struct cpu_user_regs *regs, uint32_t fid)
          return true;
      }
 
+    case PSCI_1_0_FN32_SYSTEM_SUSPEND:
+    case PSCI_1_0_FN64_SYSTEM_SUSPEND:
+    {
+        register_t epoint = PSCI_ARG(regs,1);
+        register_t cid = PSCI_ARG(regs,2);
+        register_t ret;
+
+        perfc_incr(vpsci_system_suspend);
+        /* Set the result to PSCI_SUCCESS if the call fails.
+         * Otherwise preserve the context_id in x0. For now
+         * we don't support the case where the system is suspended
+         * to a shallower level and PSCI_SUCCESS is returned to the
+         * caller.
+         */
+        ret = do_psci_1_0_system_suspend(epoint, cid);
+        if ( ret != PSCI_SUCCESS )
+            PSCI_SET_RESULT(regs, ret);
+        return true;
+    }
+
      default:
          return false;
      }
xen/common/sched/core.c +8 -0
index 8f4b1ca10d..4e1ea62c44 100644
@@ -38,6 +38,10 @@
  #include <xsm/xsm.h>
  #include <xen/err.h>
 
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
+#include <asm/suspend.h>
+#endif
+
  #include "private.h"
 
  #ifdef CONFIG_XEN_GUEST
@@ -957,6 +961,10 @@ void vcpu_unblock(struct vcpu *v)
  {
      if ( !test_and_clear_bit(_VPF_blocked, &v->pause_flags) )
          return;
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
+    if ( test_bit(_VPF_suspended, &v->pause_flags) )
+        vcpu_resume(v);
+#endif
 
      /* Polling period ends when a VCPU is unblocked. */
      if ( unlikely(v->poll_evtchn != 0) )
xen/include/asm-arm/domain.h +3 -0
index 413e5a2a18..715841e0b5 100644
@@ -203,6 +203,9 @@ struct arch_vcpu
      struct vtimer virt_timer;
      bool   vtimer_initialized;
 
+    register_t suspend_ep;
+    register_t suspend_cid;
+
      /*
       * The full P2M may require some cleaning (e.g when emulation
       * set/way). As the action can take a long time, it requires
xen/include/asm-arm/perfc_defn.h +1 -0
index 31f071222b..d71e91a5e4 100644
@@ -33,6 +33,7 @@ PERFCOUNTER(vpsci_system_reset,        "vpsci: system_reset")
  PERFCOUNTER(vpsci_cpu_suspend,         "vpsci: cpu_suspend")
  PERFCOUNTER(vpsci_cpu_affinity_info,   "vpsci: cpu_affinity_info")
  PERFCOUNTER(vpsci_features,            "vpsci: features")
+PERFCOUNTER(vpsci_system_suspend,      "vpsci: system_suspend")
 
  PERFCOUNTER(vcpu_kick,                 "vcpu: notify other vcpu")
 
xen/include/asm-arm/psci.h +2 -0
index 832f77afff..26462d0c47 100644
@@ -43,10 +43,12 @@ void call_psci_system_reset(void);
  #define PSCI_0_2_FN32_SYSTEM_OFF          PSCI_0_2_FN32(8)
  #define PSCI_0_2_FN32_SYSTEM_RESET        PSCI_0_2_FN32(9)
  #define PSCI_1_0_FN32_PSCI_FEATURES       PSCI_0_2_FN32(10)
+#define PSCI_1_0_FN32_SYSTEM_SUSPEND      PSCI_0_2_FN32(14)
 
  #define PSCI_0_2_FN64_CPU_SUSPEND         PSCI_0_2_FN64(1)
  #define PSCI_0_2_FN64_CPU_ON              PSCI_0_2_FN64(3)
  #define PSCI_0_2_FN64_AFFINITY_INFO       PSCI_0_2_FN64(4)
+#define PSCI_1_0_FN64_SYSTEM_SUSPEND      PSCI_0_2_FN64(14)
 
  /* PSCI v0.2 affinity level state returned by AFFINITY_INFO */
  #define PSCI_0_2_AFFINITY_LEVEL_ON      0
xen/include/asm-arm/suspend.h new file +17 -0
new file mode 100644
index 0000000000..fbaa414f0c
@@ -0,0 +1,17 @@
+#ifndef __ASM_ARM_SUSPEND_H__
+#define __ASM_ARM_SUSPEND_H__
+
+int32_t domain_suspend(register_t epoint, register_t cid);
+void vcpu_resume(struct vcpu *v);
+
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/xen/sched.h +3 -0
index 3b4ed3a2ab..b2f6d1af28 100644
@@ -903,6 +903,9 @@ static inline struct domain *next_domain_in_cpupool(
  /* VCPU is parked. */
  #define _VPF_parked          8
  #define VPF_parked           (1UL<<_VPF_parked)
+/* VCPU is suspended */
+#define _VPF_suspended       9
+#define VPF_suspended        (1UL<<_VPF_suspended)
 
  static inline bool vcpu_runnable(const struct vcpu *v)
  {
Signature · 1 line
Julien Grall
#32 lore raw

Re: [PATCH 17/19] xen: don't free percpu areas during suspend

In reply to Mykyta Poturai
Quoted 18 lines · Mykyta Poturai

From: Juergen Gross <[email protected]>

Instead of freeing percpu areas during suspend and allocating them again when resuming keep them. Only free an area in case a cpu didn't come up again when resuming.

It should be noted that there is a potential change in behaviour as the percpu areas are no longer zeroed out during suspend/resume. While I have checked the called cpu notifier hooks to cope with that there might be some well hidden dependency on the previous behaviour. OTOH a component not registering itself for cpu down/up and expecting to see a zeroed percpu variable after suspend/resume is kind of broken already. And the opposite case, where a component is not registered to be called for cpu down/up and is not expecting a percpu variable suddenly to be zero due to suspend/resume is much more probable, especially as the suspend/resume functionality seems not to be tested that often.

Quoted 1 line

I can't remember having written this patch. The one I remember was for x86.

Quoted 1 line

I doubt that, reasoning see above.

Juergen

Quoted 22 lines
---
  xen/arch/arm/percpu.c | 3 +++
  1 file changed, 3 insertions(+)
xen/arch/arm/percpu.c +3 -0
index 25442c48fe..0642705544 100644
@@ -58,10 +58,13 @@ static int cpu_percpu_callback(
      switch ( action )
      {
      case CPU_UP_PREPARE:
+      if ( system_state != SYS_STATE_resume )
          rc = init_percpu_area(cpu);
          break;
      case CPU_UP_CANCELED:
      case CPU_DEAD:
+    case CPU_RESUME_FAILED:
+      if ( system_state != SYS_STATE_suspend )
          free_percpu_area(cpu);
          break;
      default:
#3 lore raw

Re: [PATCH 01/19] xen/arm: Implement PSCI system suspend

In reply to Mykyta Poturai

Hi Mykyta,

Review

Quoted 5 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

The implementation consists of:

(quoted diff) +0 -2
-Adding PSCI system suspend call as new PSCI function
-Trapping PSCI system_suspend HVC

AFAIK, this can be SMC as well.

Quoted 5 lines

-Implementing PSCI system suspend call (virtual interface that allows
guests to suspend themselves)

The PSCI system suspend should be called by a guest from its boot VCPU.

Why such limititation? PSCI standart does not limit choise of a CPU.

Quoted 70 lines

Non-boot VCPUs of the guest should be hot-unplugged using PSCI CPU_OFF call prior to issuing PSCI system suspend. Interrupts that are left enabled by the guest are assumed to be its wake-up interrupts. Therefore, a wake-up interrupt triggers the resume of the guest. Guest should resume regardless of the state of Xen (suspended or not).

When a guest calls PSCI system suspend the respective domain will be suspended if the following conditions are met:
1) Given resume entry point is not invalid
2) Other (if any) VCPUs of the calling guest are hot-unplugged

If the conditions above are met the calling domain is labeled as suspended and the calling VCPU is blocked. If nothing else wouldn't be done the suspended domain would resume from the place where it called PSCI system suspend. This is expected if processing of the PSCI system suspend call fails. However, in the case of success the calling guest should resume (continue execution after the wake-up) from the entry point which is given as the first argument of the PSCI system suspend call. In addition to the entry point, the guest expects to start within the environment whose state matches the state after reset. This means that the guest should find reset register values, MMU disabled, etc. Thereby, the context of VCPU should be 'reset' (as if the system is comming out of reset), the program counter should contain entry point, which is 1st argument, and r0/x0 should contain context ID which is 2nd argument of PSCI system suspend call. The context of VCPU is set during resume path, to prevent it being overwritten by ctxt_switch_from after vcpu is blocked and scheduled out.

VCPU is marked as suspended with _VPF_suspended flag. A suspended domain will resume after the Xen receives an interrupt which is targeted to the domain, unblocks the domain's VCPU, and schedules it in. During the vcpu_unblock execution the VCPU is checked for VPF_suspended flag. If the flag is present, the context of that VCPU gets cleared and entry point/cid are set.

---
 xen/arch/arm/Makefile            |   1 +
 xen/arch/arm/domain.c            |   4 +
 xen/arch/arm/suspend.c           | 182 +++++++++++++++++++++++++++++++
 xen/arch/arm/vpsci.c             |  28 +++++
 xen/common/sched/core.c          |   8 ++
 xen/include/asm-arm/domain.h     |   3 +
 xen/include/asm-arm/perfc_defn.h |   1 +
 xen/include/asm-arm/psci.h       |   2 +
 xen/include/asm-arm/suspend.h    |  17 +++
 xen/include/xen/sched.h          |   3 +
 10 files changed, 249 insertions(+)
 create mode 100644 xen/arch/arm/suspend.c
 create mode 100644 xen/include/asm-arm/suspend.h
xen/arch/arm/Makefile +1 -0
index b5913c9d39..07dbbd99a3 100644
@@ -49,6 +49,7 @@ obj-y += setup.o
 obj-y += shutdown.o
 obj-y += smp.o
 obj-y += smpboot.o
+obj-y += suspend.o
 obj-y += sysctl.o
 obj-y += time.o
 obj-y += traps.o
xen/arch/arm/domain.c +0 -0
index 23c8b345d4..4110154bda 100644

xen/arch/arm/domain.c line 44

Quoted 5 lines
xen/arch/arm/domain.c +2 -0
@@ -40,6 +40,8 @@
 #include <asm/vtimer.h>
 #include <asm/vscmi.h>
 
+#include <public/sched.h>
+

Do you really need this include?

xen/arch/arm/domain.c static void ctxt_switch_from(struct vcpu *p) · line 107

Quoted 9 lines
xen/arch/arm/domain.c +2 -0
 #include "vpci.h"
 #include "vuart.h"
 
@@ -101,6 +103,8 @@ static void ctxt_switch_from(struct vcpu *p)
     if ( is_idle_vcpu(p) )
         return;
 
+    /* VCPU's context should not be saved if its domain is suspended */
+

Is this a some leftover?

Quoted 154 lines
xen/arch/arm/domain.c +0 -0
     p2m_save_state(p);
 
     /* CP 15 */
xen/arch/arm/suspend.c new file +145 -0
new file mode 100644
index 0000000000..987ba6ac11
@@ -0,0 +1,182 @@
+/*
+ * Copyright (C) 2022 EPAM Systems Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+#include <xen/sched.h>
+#include <asm/cpufeature.h>
+#include <asm/event.h>
+#include <asm/psci.h>
+#include <asm/suspend.h>
+#include <public/sched.h>
+
+struct cpu_context cpu_context;
+
+/* Reset values of VCPU architecture specific registers */
+static void vcpu_arch_reset(struct vcpu *v)
+{
+    v->arch.ttbr0 = 0;
+    v->arch.ttbr1 = 0;
+    v->arch.ttbcr = 0;
+
+    v->arch.csselr = 0;
+    v->arch.cpacr = 0;
+    v->arch.contextidr = 0;
+    v->arch.tpidr_el0 = 0;
+    v->arch.tpidrro_el0 = 0;
+    v->arch.tpidr_el1 = 0;
+    v->arch.vbar = 0;
+    v->arch.dacr = 0;
+    v->arch.par = 0;
+#if defined(CONFIG_ARM_32)
+    v->arch.mair0 = 0;
+    v->arch.mair1 = 0;
+    v->arch.amair0 = 0;
+    v->arch.amair1 = 0;
+#else
+    v->arch.mair = 0;
+    v->arch.amair = 0;
+#endif
+    /* Fault Status */
+#if defined(CONFIG_ARM_32)
+    v->arch.dfar = 0;
+    v->arch.ifar = 0;
+    v->arch.dfsr = 0;
+#elif defined(CONFIG_ARM_64)
+    v->arch.far = 0;
+    v->arch.esr = 0;
+#endif
+
+    v->arch.ifsr  = 0;
+    v->arch.afsr0 = 0;
+    v->arch.afsr1 = 0;
+
+#ifdef CONFIG_ARM_32
+    v->arch.joscr = 0;
+    v->arch.jmcr = 0;
+#endif
+
+    if ( is_32bit_domain(v->domain) && cpu_has_thumbee )
+    {
+        v->arch.teecr = 0;
+        v->arch.teehbr = 0;
+    }
+}
+
+
+static void vcpu_suspend(register_t epoint, register_t cid)
+{
+    struct vcpu *v = current;
+
+    v->arch.suspend_ep = epoint;
+    v->arch.suspend_cid = cid;
+    set_bit(_VPF_suspended, &v->pause_flags);
+    return;
+}
+
+/*
+ * This function sets the context of current VCPU to the state which is expected
+ * by the guest on resume. The expected VCPU state is:
+ * 1) pc to contain resume entry point (1st argument of PSCI SYSTEM_SUSPEND)
+ * 2) r0/x0 to contain context ID (2nd argument of PSCI SYSTEM_SUSPEND)
+ * 3) All other general purpose and system registers should have reset values
+ */
+void vcpu_resume(struct vcpu *v)
+{
+
+    struct vcpu_guest_context ctxt;
+
+    /* Make sure that VCPU guest regs are zeroed */
+    memset(&ctxt, 0, sizeof(ctxt));
+
+    /* Set non-zero values to the registers prior to copying */
+    ctxt.user_regs.pc64 = (u64)v->arch.suspend_ep;
+
+    if ( is_32bit_domain(v->domain) )
+    {
+        ctxt.user_regs.r0_usr = v->arch.suspend_cid;
+        ctxt.user_regs.cpsr = PSR_GUEST32_INIT;
+
+        /* Thumb set is allowed only for 32-bit domain */
+        if ( v->arch.suspend_ep & 1 )
+        {
+            ctxt.user_regs.cpsr |= PSR_THUMB;
+            ctxt.user_regs.pc64 &= ~(u64)1;
+        }
+    }
+#ifdef CONFIG_ARM_64
+    else
+    {
+        ctxt.user_regs.x0 = v->arch.suspend_cid;
+        ctxt.user_regs.cpsr = PSR_GUEST64_INIT;
+    }
+#endif
+    ctxt.sctlr = SCTLR_GUEST_INIT;
+    ctxt.flags = VGCF_online;
+
+    /* Reset architecture specific registers */
+    vcpu_arch_reset(v);
+
+    /* Initialize VCPU registers */
+    arch_set_info_guest(v, &ctxt);
+    clear_bit(_VPF_suspended, &v->pause_flags);
+}
+
+int32_t domain_suspend(register_t epoint, register_t cid)
+{
+    struct vcpu *v;
+    struct domain *d = current->domain;
+    bool is_thumb = epoint & 1;
+
+    dprintk(XENLOG_DEBUG,
+            "Dom%d suspend: epoint=0x%"PRIregister", cid=0x%"PRIregister"\n",
+            d->domain_id, epoint, cid);
+
+    /* THUMB set is not allowed with 64-bit domain */
+    if ( is_64bit_domain(d) && is_thumb )

xen/arch/arm/suspend.c line 155

Quoted 10 lines
xen/arch/arm/suspend.c new file +10 -0
+        return PSCI_INVALID_ADDRESS;
+
+    /* Ensure that all CPUs other than the calling one are offline */
+    for_each_vcpu ( d, v )
+    {
+        if ( v != current && is_vcpu_online(v) )
+            return PSCI_DENIED;
+    }
+
+    //TODO: add support for suspending from any VCPU

How hard it would be to implement?

Quoted 64 lines
xen/arch/arm/suspend.c new file +27 -0
+    if (current->vcpu_id != 0)
+        return PSCI_DENIED;
+
+    /*
+     * Prepare the calling VCPU for suspend (reset its context, save entry point
+     * into pc and context ID into r0/x0 as specified by PSCI SYSTEM_SUSPEND)
+     */
+    vcpu_suspend(epoint, cid);
+
+    /*
+     * The calling domain is suspended by blocking its last running VCPU. If an
+     * event is pending the domain will resume right away (VCPU will not block,
+     * but when scheduled in it will resume from the given entry point).
+     */
+    vcpu_block_unless_event_pending(current);
+
+    return PSCI_SUCCESS;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vpsci.c +8 -0
index c1e250be59..f4e6e92873 100644
@@ -18,6 +18,7 @@
 #include <asm/vgic.h>
 #include <asm/vpsci.h>
 #include <asm/event.h>
+#include <asm/suspend.h>
 
 #include <public/sched.h>
 
@@ -208,6 +209,11 @@ static void do_psci_0_2_system_reset(void)
     domain_shutdown(d,SHUTDOWN_reboot);
 }
 
+static int32_t do_psci_1_0_system_suspend(register_t epoint, register_t cid)
+{
+    return domain_suspend(epoint, cid);
+}
+
 static int32_t do_psci_1_0_features(uint32_t psci_func_id)
 {
     /* /!\ Ordered by function ID and not name */
@@ -225,6 +231,8 @@ static int32_t do_psci_1_0_features(uint32_t psci_func_id)
     case PSCI_0_2_FN32_SYSTEM_OFF:
     case PSCI_0_2_FN32_SYSTEM_RESET:
     case PSCI_1_0_FN32_PSCI_FEATURES:
+    case PSCI_1_0_FN32_SYSTEM_SUSPEND:
+    case PSCI_1_0_FN64_SYSTEM_SUSPEND:
     case ARM_SMCCC_VERSION_FID:
         return 0;
     default:
@@ -355,6 +363,26 @@ bool do_vpsci_0_2_call(struct cpu_user_regs *regs, uint32_t fid)
         return true;
     }
 

xen/arch/arm/vpsci.c bool do_vpsci_0_2_call(struct cpu_user_regs *regs, uint32_t fid) · line 375

Quoted 10 lines
xen/arch/arm/vpsci.c +10 -0
+    case PSCI_1_0_FN32_SYSTEM_SUSPEND:
+    case PSCI_1_0_FN64_SYSTEM_SUSPEND:
+    {
+        register_t epoint = PSCI_ARG(regs,1);
+        register_t cid = PSCI_ARG(regs,2);
+        register_t ret;
+
+        perfc_incr(vpsci_system_suspend);
+        /* Set the result to PSCI_SUCCESS if the call fails.
+         * Otherwise preserve the context_id in x0. For now 

Looks like there is a trailing space ^

xen/arch/arm/vpsci.c bool do_vpsci_0_2_call(struct cpu_user_regs *regs, uint32_t fid) · line 377

Quoted 2 lines
xen/arch/arm/vpsci.c +2 -0
+         * we don't support the case where the system is suspended
+         * to a shallower level and PSCI_SUCCESS is returned to the 

And in this line also

Quoted 27 lines
xen/arch/arm/vpsci.c +8 -0
+         * caller.
+         */
+        ret = do_psci_1_0_system_suspend(epoint, cid);
+        if ( ret != PSCI_SUCCESS )
+            PSCI_SET_RESULT(regs, ret);
+        return true;
+    }
+
     default:
         return false;
     }
xen/common/sched/core.c +4 -0
index 8f4b1ca10d..4e1ea62c44 100644
@@ -38,6 +38,10 @@
 #include <xsm/xsm.h>
 #include <xen/err.h>
 
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
+#include <asm/suspend.h>
+#endif
+
 #include "private.h"
 
 #ifdef CONFIG_XEN_GUEST

xen/common/sched/core.c void vcpu_unblock(struct vcpu *v) · line 967

Quoted 7 lines
xen/common/sched/core.c +4 -0
@@ -957,6 +961,10 @@ void vcpu_unblock(struct vcpu *v)
 {
     if ( !test_and_clear_bit(_VPF_blocked, &v->pause_flags) )
         return;
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
+    if ( test_bit(_VPF_suspended, &v->pause_flags) )
+        vcpu_resume(v);
+#endif

This does not look good. I do understant that that was I, who suggested to add this flag, but I didn't expected that it will get into common code.

Also, this is not justified in the commit message. I remeber that there was some discussion about why vcpu_block()/vcpu_unblock() could not be used, and I'd love to see its summary in the commit message.

Quoted 83 lines
xen/common/sched/core.c +0 -0
     /* Polling period ends when a VCPU is unblocked. */
     if ( unlikely(v->poll_evtchn != 0) )
xen/include/asm-arm/domain.h +3 -0
index 413e5a2a18..715841e0b5 100644
@@ -203,6 +203,9 @@ struct arch_vcpu
     struct vtimer virt_timer;
     bool   vtimer_initialized;
 
+    register_t suspend_ep;
+    register_t suspend_cid;
+
     /*
      * The full P2M may require some cleaning (e.g when emulation
      * set/way). As the action can take a long time, it requires
xen/include/asm-arm/perfc_defn.h +1 -0
index 31f071222b..d71e91a5e4 100644
@@ -33,6 +33,7 @@ PERFCOUNTER(vpsci_system_reset,        "vpsci: system_reset")
 PERFCOUNTER(vpsci_cpu_suspend,         "vpsci: cpu_suspend")
 PERFCOUNTER(vpsci_cpu_affinity_info,   "vpsci: cpu_affinity_info")
 PERFCOUNTER(vpsci_features,            "vpsci: features")
+PERFCOUNTER(vpsci_system_suspend,      "vpsci: system_suspend")
 
 PERFCOUNTER(vcpu_kick,                 "vcpu: notify other vcpu")
 
xen/include/asm-arm/psci.h +2 -0
index 832f77afff..26462d0c47 100644
@@ -43,10 +43,12 @@ void call_psci_system_reset(void);
 #define PSCI_0_2_FN32_SYSTEM_OFF          PSCI_0_2_FN32(8)
 #define PSCI_0_2_FN32_SYSTEM_RESET        PSCI_0_2_FN32(9)
 #define PSCI_1_0_FN32_PSCI_FEATURES       PSCI_0_2_FN32(10)
+#define PSCI_1_0_FN32_SYSTEM_SUSPEND      PSCI_0_2_FN32(14)
 
 #define PSCI_0_2_FN64_CPU_SUSPEND         PSCI_0_2_FN64(1)
 #define PSCI_0_2_FN64_CPU_ON              PSCI_0_2_FN64(3)
 #define PSCI_0_2_FN64_AFFINITY_INFO       PSCI_0_2_FN64(4)
+#define PSCI_1_0_FN64_SYSTEM_SUSPEND      PSCI_0_2_FN64(14)
 
 /* PSCI v0.2 affinity level state returned by AFFINITY_INFO */
 #define PSCI_0_2_AFFINITY_LEVEL_ON      0
xen/include/asm-arm/suspend.h new file +17 -0
new file mode 100644
index 0000000000..fbaa414f0c
@@ -0,0 +1,17 @@
+#ifndef __ASM_ARM_SUSPEND_H__
+#define __ASM_ARM_SUSPEND_H__
+
+int32_t domain_suspend(register_t epoint, register_t cid);
+void vcpu_resume(struct vcpu *v);
+
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/xen/sched.h +3 -0
index 3b4ed3a2ab..b2f6d1af28 100644
@@ -903,6 +903,9 @@ static inline struct domain *next_domain_in_cpupool(
 /* VCPU is parked. */
 #define _VPF_parked          8
 #define VPF_parked           (1UL<<_VPF_parked)
+/* VCPU is suspended */
+#define _VPF_suspended       9
+#define VPF_suspended        (1UL<<_VPF_suspended)
 
 static inline bool vcpu_runnable(const struct vcpu *v)
 {
Signature · 1 line
Volodymyr Babchuk at EPAM
#4 lore raw

Re: [PATCH 01/19] xen/arm: Implement PSCI system suspend

Hi,

Quoted 8 lines · Volodymyr Babchuk

xen/arch/arm/suspend.c line 2

Quoted 8 lines
xen/arch/arm/suspend.c new file +2 -0
new file mode 100644
index 0000000000..987ba6ac11
@@ -0,0 +1,182 @@
+/*
+ * Copyright (C) 2022 EPAM Systems Inc.

Not related to Volodymyr's answer but I will reply here as I noticed it.

The code was written in 2018 by Aggios. So shouldn't this be a 2018 copyright from Aggios? You can add yours on top if you want but it is not super clear what has been modified (a changelog after --- in the commit message would have been useful).

Quoted 11 lines

xen/arch/arm/suspend.c line 13

Quoted 11 lines
xen/arch/arm/suspend.c new file +11 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */

We are not allowing LGPL* license in the hypervisor. This has to be GPLv2-only (this was the original contribution even if there were no copyright).

But it would be better to use an SPDX tag as it makes clearer which license is used (see [1]).

[...]

Quoted 14 lines
Quoted 14 lines
xen/common/sched/core.c +4 -0
index 8f4b1ca10d..4e1ea62c44 100644
@@ -38,6 +38,10 @@
  #include <xsm/xsm.h>
  #include <xen/err.h>
 
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
+#include <asm/suspend.h>
+#endif
+
  #include "private.h"
 
Quoted 12 lines

xen/common/sched/core.c void vcpu_unblock(struct vcpu *v) · line 967

Quoted 9 lines
xen/common/sched/core.c +4 -0
  #ifdef CONFIG_XEN_GUEST
@@ -957,6 +961,10 @@ void vcpu_unblock(struct vcpu *v)
  {
      if ( !test_and_clear_bit(_VPF_blocked, &v->pause_flags) )
          return;
+#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
+    if ( test_bit(_VPF_suspended, &v->pause_flags) )
+        vcpu_resume(v);
+#endif

This does not look good. I do understant that that was I, who suggested to add this flag, but I didn't expected that it will get into common code.

AFAIU, you are using the flag to indicate whether the vCPU should be reset. If I am not mistaken, this should only happen for vCPU0 (other vCPUs will be brought up using PSCI CPU ON). So you could reset the vCPU before blocking it.

With that, the flag should not be necessary (at least in this situation).

Quoted 4 lines

Also, this is not justified in the commit message. I remeber that there was some discussion about why vcpu_block()/vcpu_unblock() could not be used, and I'd love to see its summary in the commit message.

Are you referring to the discussion in [2]?

Cheers,

[1] [email protected]
[2] [email protected]

Cheers,

Signature · 1 line
Julien Grall
#33 lore raw

Re: [PATCH 17/19] xen: don't free percpu areas during suspend

In reply to Juergen Gross
Quoted 4 lines · Juergen Gross
Quoted 2 lines · Mykyta Poturai
Quoted 8 lines

I can't remember having written this patch. The one I remember was for
x86.

Quoted 1 line

I doubt that, reasoning see above.

Right. In fact, I can't find any records of having sent an email with such a tag... Or to have ever even replied to any patch sent from this email address, for what matters.

Thanks and Regards

Signature · 6 lines
Dario Faggioli, Ph.D
http://about.me/dario.faggioli
Virtualization Software Engineer
SUSE Labs, SUSE https://www.suse.com/
-------------------------------------------------------------------
<<This happens because _I_ choose it to happen!>> (Raistlin Majere)
#9 lore raw

Re: [PATCH 05/19] xen/x86: Move freeze/thaw_domains into common files

In reply to Mykyta Poturai

Hi,

Quoted 6 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

These functions will be reused by suspend/resume support for ARM.

Your Signed-off-by is missing.

Quoted 2 lines
---
  xen/common/domain.c     | 29 +++++++++++++++++++++++++++++

The title suggests that there will be code movement. However... I only see addition here. Did you intend to remove the functions from arch/x86/acpi/power.c?

Quoted 48 lines
  xen/include/xen/sched.h |  3 +++
  2 files changed, 32 insertions(+)
xen/common/domain.c +29 -0
index 56d47dd664..5e5894c468 100644
@@ -1884,6 +1884,35 @@ int continue_hypercall_on_cpu(
      return 0;
  }
 
+
+void freeze_domains(void)
+{
+    struct domain *d;
+
+    rcu_read_lock(&domlist_read_lock);
+    /*
+     * Note that we iterate in order of domain-id. Hence we will pause dom0
+     * first which is required for correctness (as only dom0 can add domains to
+     * the domain list). Otherwise we could miss concurrently-created domains.
+     */
+    for_each_domain ( d )
+        domain_pause(d);
+    rcu_read_unlock(&domlist_read_lock);
+}
+
+void thaw_domains(void)
+{
+    struct domain *d;
+
+    rcu_read_lock(&domlist_read_lock);
+    for_each_domain ( d )
+    {
+        restore_vcpu_affinity(d);
+        domain_unpause(d);
+    }
+    rcu_read_unlock(&domlist_read_lock);
+}
+
  /*
   * Local variables:
   * mode: C
xen/include/xen/sched.h +0 -0
index 00a939aa01..c8ddfdd51c 100644

xen/include/xen/sched.h static inline struct vcpu *domain_vcpu(const struct domain *d, · line 986

Quoted 9 lines
xen/include/xen/sched.h +3 -0
@@ -978,6 +978,9 @@ static inline struct vcpu *domain_vcpu(const struct domain *d,
      return vcpu_id >= d->max_vcpus ? NULL : d->vcpu[idx];
  }
 
+void freeze_domains(void);
+void thaw_domains(void);
+
  void cpu_init(void);
 
  /*

Cheers,

Signature · 1 line
Julien Grall
#11 lore raw

Re: [PATCH 06/19] xen/arm: Freeze domains on suspend and thaw them on resume

In reply to Mykyta Poturai

Hi,

Quoted 8 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

Freeze and thaw of domains is reused as implemented for x86. In addition, system_state variable is updated to represent the actual state of the system.

Your signed-off-by is missing.

xen/arch/arm/suspend.c static long system_suspend(void *data)

Quoted 9 lines
---
  xen/arch/arm/suspend.c | 8 ++++++++
  1 file changed, 8 insertions(+)
xen/arch/arm/suspend.c +0 -0
index b09bf319d0..2b94816b63 100644
@@ -137,6 +137,14 @@ static long system_suspend(void *data)

So this is something I already hinted in the original series. But I will repeat here.

I find quite difficult to review the code in system_suspend() when this introduced in piece meal. For instance, patch #8 is fixing a bug introduced by patch #7.

Patch #7 adds barrier after code your added here...

So I would much prefer if we introduce the helpers in a few patches and then have one patch that will in system_suspend().

Cheers,

Signature · 1 line
Julien Grall
#13 lore raw

Re: [PATCH 07/19] xen/arm: Disable/enable non-boot physical CPUs on suspend/resume

In reply to Mykyta Poturai

Hi,

Quoted 42 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

Non-boot CPUs have to be disabled on suspend and enabled on resume (hotplug-based mechanism). Disabling non-boot CPUs will lead to PSCI CPU_OFF to be called by each non-boot CPU. Depending on the underlying platform capabilities, this may lead to the physical powering down of CPUs. Tested on Xilinx Zynq Ultrascale+ MPSoC (including power down of each non-boot CPU).

---
  xen/arch/arm/suspend.c | 15 ++++++++++++++-
  1 file changed, 14 insertions(+), 1 deletion(-)
xen/arch/arm/suspend.c +7 -0
index 2b94816b63..0784979e4f 100644
@@ -13,6 +13,7 @@
   */
 
  #include <xen/sched.h>
+#include <xen/cpu.h>
  #include <asm/cpufeature.h>
  #include <asm/event.h>
  #include <asm/psci.h>
@@ -135,17 +136,29 @@ void vcpu_resume(struct vcpu *v)
  /* Xen suspend. Note: data is not used (suspend is the suspend to RAM) */
  static long system_suspend(void *data)
  {
+    int status;
+
      BUG_ON(system_state != SYS_STATE_active);
 
      system_state = SYS_STATE_suspend;
      freeze_domains();
 
+    status = disable_nonboot_cpus();
+    if ( status )
+    {
+        system_state = SYS_STATE_resume;

xen/arch/arm/suspend.c void vcpu_resume(struct vcpu *v) · line 159

Quoted 10 lines
xen/arch/arm/suspend.c +6 -0
+        goto resume_nonboot_cpus;
+    }
+
      system_state = SYS_STATE_resume;
 
+resume_nonboot_cpus:
+    enable_nonboot_cpus();
      thaw_domains();
      system_state = SYS_STATE_active;
+    dsb(sy);

Please document what is this dsb() for.

xen/arch/arm/suspend.c void vcpu_resume(struct vcpu *v) · line 164

Quoted 6 lines
xen/arch/arm/suspend.c +1 -1
-    return -ENOSYS;
+    return status;
  }
 
  int32_t domain_suspend(register_t epoint, register_t cid)

Cheers,

Signature · 1 line
Julien Grall
#16 lore raw

Re: [PATCH 09/19] xen/arm: Implement GIC suspend/resume functions (gicv2 only)

In reply to Mykyta Poturai

Hi,

Quoted 10 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

System suspend may lead to a state where GIC would be powered down. Therefore, Xen should save/restore the context of GIC on suspend/resume. Note that the context consists of states of registers which are controlled by the hypervisor. Other GIC registers which are accessible by guests are saved/restored on context switch.
Tested on Xilinx Ultrascale+ MPSoC with (and without) powering down the GIC.

Quoted 2 lines

Your signed-off-by is missing.

Quoted 59 lines
---
  xen/arch/arm/gic-v2.c     | 138 +++++++++++++++++++++++++++++++++++++-
  xen/arch/arm/gic.c        |  27 ++++++++
  xen/include/asm-arm/gic.h |   8 +++
  3 files changed, 172 insertions(+), 1 deletion(-)
xen/arch/arm/gic-v2.c +38 -0
index b2adc8ec9a..b077b66d92 100644
@@ -123,6 +123,23 @@ static DEFINE_PER_CPU(u8, gic_cpu_id);
  /* Maximum cpu interface per GIC */
  #define NR_GIC_CPU_IF 8
 
+/* GICv2 registers to be saved/restored on system suspend/resume */
+struct gicv2_context {
+    /* GICC context */
+    uint32_t gicc_ctlr;
+    uint32_t gicc_pmr;
+    uint32_t gicc_bpr;
+    /* GICD context */
+    uint32_t gicd_ctlr;
+    uint32_t *gicd_isenabler;
+    uint32_t *gicd_isactiver;
+    uint32_t *gicd_ipriorityr;
+    uint32_t *gicd_itargetsr;
+    uint32_t *gicd_icfgr;
+};
+
+static struct gicv2_context gicv2_context;
+
  static inline void writeb_gicd(uint8_t val, unsigned int offset)
  {
      writeb_relaxed(val, gicv2.map_dbase + offset);
@@ -1251,6 +1268,40 @@ static void __init gicv2_acpi_init(void)
  static void __init gicv2_acpi_init(void) { }
  #endif
 
+static int gicv2_alloc_context(struct gicv2_context *gc)
+{
+    uint32_t n = gicv2_info.nr_lines;
+
+    gc->gicd_isenabler = xzalloc_array(uint32_t, DIV_ROUND_UP(n, 32));
+    if ( !gc->gicd_isenabler )
+        goto err_free;
+
+    gc->gicd_isactiver = xzalloc_array(uint32_t, DIV_ROUND_UP(n, 32));
+    if ( !gc->gicd_isactiver )
+        goto err_free;
+
+    gc->gicd_itargetsr = xzalloc_array(uint32_t, DIV_ROUND_UP(n, 4));
+    if ( !gc->gicd_itargetsr )
+        goto err_free;
+
+    gc->gicd_ipriorityr = xzalloc_array(uint32_t, DIV_ROUND_UP(n, 4));
+    if ( !gc->gicd_ipriorityr )
+        goto err_free;
+
+    gc->gicd_icfgr = xzalloc_array(uint32_t, DIV_ROUND_UP(n, 16));

xen/arch/arm/gic-v2.c static void __init gicv2_acpi_init(void) · line 1301

Quoted 10 lines
xen/arch/arm/gic-v2.c +10 -0
+    if ( !gc->gicd_icfgr )
+        goto err_free;
+
+    return 0;
+err_free:
+    xfree(gc->gicd_icfgr);
+    xfree(gc->gicd_ipriorityr);
+    xfree(gc->gicd_itargetsr);
+    xfree(gc->gicd_isactiver);
+    xfree(gc->gicd_isenabler);

Please add a newline.

xen/arch/arm/gic-v2.c static int __init gicv2_init(void) · line 1373

Quoted 13 lines
xen/arch/arm/gic-v2.c +5 -1
+    return -ENOMEM;
+}
+
  static int __init gicv2_init(void)
  {
      uint32_t aliased_offset = 0;
@@ -1318,7 +1369,8 @@ static int __init gicv2_init(void)
 
      spin_unlock(&gicv2.lock);
 
-    return 0;
+    /* Allocate memory to be used for saving GIC context during the suspend */
+    return gicv2_alloc_context(&gicv2_context);

As I pointed out in the previous version, suspend/resume is not going to be widely used. So I don't think we should prevent booting if we can't allocate the memory.

In addition to that, I think we should consider to have:
* a command line option to avoid wasting memory if the feature is not
going to be used
* ifdef the suspend/resume code to allow an integrator to compile out
any related code.

xen/arch/arm/gic-v2.c static void gicv2_do_LPI(unsigned int lpi) · line 1384

Quoted 10 lines
xen/arch/arm/gic-v2.c +3 -0
  }
 
  static void gicv2_do_LPI(unsigned int lpi)
@@ -1327,6 +1379,88 @@ static void gicv2_do_LPI(unsigned int lpi)
      BUG();
  }
 
+static int gicv2_suspend(void)
+{
+    int i;

This should be unsigned int.

Quoted 26 lines
xen/arch/arm/gic-v2.c +26 -0
+
+    ASSERT(gicv2_context.gicd_isenabler);
+    ASSERT(gicv2_context.gicd_isactiver);
+    ASSERT(gicv2_context.gicd_ipriorityr);
+    ASSERT(gicv2_context.gicd_itargetsr);
+    ASSERT(gicv2_context.gicd_icfgr);
+
+    /* Save GICC configuration */
+    gicv2_context.gicc_ctlr = readl_gicc(GICC_CTLR);
+    gicv2_context.gicc_pmr = readl_gicc(GICC_PMR);
+    gicv2_context.gicc_bpr = readl_gicc(GICC_BPR);
+
+    /* Save GICD configuration */
+    gicv2_context.gicd_ctlr = readl_gicd(GICD_CTLR);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 32); i++ )
+        gicv2_context.gicd_isenabler[i] = readl_gicd(GICD_ISENABLER + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 32); i++ )
+        gicv2_context.gicd_isactiver[i] = readl_gicd(GICD_ISACTIVER + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 4); i++ )
+        gicv2_context.gicd_ipriorityr[i] = readl_gicd(GICD_IPRIORITYR + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 4); i++ )
+        gicv2_context.gicd_itargetsr[i] = readl_gicd(GICD_ITARGETSR + i * 4);

xen/arch/arm/gic-v2.c static void gicv2_do_LPI(unsigned int lpi) · line 1420

Quoted 10 lines
xen/arch/arm/gic-v2.c +10 -0
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 16); i++ )
+        gicv2_context.gicd_icfgr[i] = readl_gicd(GICD_ICFGR + i * 4);
+
+    return 0;
+}
+
+static void gicv2_resume(void)
+{
+    int i;

Ditto.

xen/arch/arm/gic-v2.c static void gicv2_do_LPI(unsigned int lpi) · line 1430

Quoted 10 lines
xen/arch/arm/gic-v2.c +10 -0
+
+    ASSERT(gicv2_context.gicd_isenabler);
+    ASSERT(gicv2_context.gicd_isactiver);
+    ASSERT(gicv2_context.gicd_ipriorityr);
+    ASSERT(gicv2_context.gicd_itargetsr);
+    ASSERT(gicv2_context.gicd_icfgr);
+
+    /* Disable CPU interface and distributor */
+    writel_gicc(0, GICC_CTLR);
+    writel_gicd(0, GICD_CTLR);

We don't need those two lines during boot. So why do you need them during resume?

Quoted 12 lines
xen/arch/arm/gic-v2.c +12 -0
+
+    /* Restore GICD configuration */
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 32); i++ ) {
+        writel_gicd(0xffffffff, GICD_ICENABLER + i * 4);
+        writel_gicd(gicv2_context.gicd_isenabler[i], GICD_ISENABLER + i * 4);
+    }
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 32); i++ ) {
+        writel_gicd(0xffffffff, GICD_ICACTIVER + i * 4);
+        writel_gicd(gicv2_context.gicd_isactiver[i], GICD_ISACTIVER + i * 4);
+    }
+

xen/arch/arm/gic-v2.c static void gicv2_do_LPI(unsigned int lpi) · line 1452

Quoted 10 lines
xen/arch/arm/gic-v2.c +10 -0
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 4); i++ )
+        writel_gicd(gicv2_context.gicd_ipriorityr[i], GICD_IPRIORITYR + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 4); i++ )
+        writel_gicd(gicv2_context.gicd_itargetsr[i], GICD_ITARGETSR + i * 4);
+
+    for ( i = 0; i < DIV_ROUND_UP(gicv2_info.nr_lines, 16); i++ )
+        writel_gicd(gicv2_context.gicd_icfgr[i], GICD_ICFGR + i * 4);
+
+    /* Make sure all registers are restored and enable distributor */

The first part reads like there is a missing barrier.

Quoted 77 lines
xen/arch/arm/gic-v2.c +13 -0
+    writel_gicd(gicv2_context.gicd_ctlr | GICD_CTL_ENABLE, GICD_CTLR);
+
+    /* Restore GIC CPU interface configuration */
+    writel_gicc(gicv2_context.gicc_pmr, GICC_PMR);
+    writel_gicc(gicv2_context.gicc_bpr, GICC_BPR);
+
+    /* Enable GIC CPU interface */
+    writel_gicc(gicv2_context.gicc_ctlr | GICC_CTL_ENABLE | GICC_CTL_EOI,
+                GICC_CTLR);
+}
+
  const static struct gic_hw_operations gicv2_ops = {
      .info                = &gicv2_info,
      .init                = gicv2_init,
@@ -1361,6 +1495,8 @@ const static struct gic_hw_operations gicv2_ops = {
      .map_hwdom_extra_mappings = gicv2_map_hwdown_extra_mappings,
      .iomem_deny_access   = gicv2_iomem_deny_access,
      .do_LPI              = gicv2_do_LPI,
+    .suspend             = gicv2_suspend,
+    .resume              = gicv2_resume,
  };
 
  /* Set up the GIC */
xen/arch/arm/gic.c +27 -0
index 3b0331b538..e9feb1fd3b 100644
@@ -467,6 +467,33 @@ int gic_iomem_deny_access(const struct domain *d)
      return gic_hw_ops->iomem_deny_access(d);
  }
 
+int gic_suspend(void)
+{
+    /* Must be called by boot CPU#0 with interrupts disabled */
+    ASSERT(!local_irq_is_enabled());
+    ASSERT(!smp_processor_id());
+
+    if ( !gic_hw_ops->suspend || !gic_hw_ops->resume )
+        return -ENOSYS;
+
+    gic_hw_ops->suspend();
+
+    return 0;
+}
+
+void gic_resume(void)
+{
+    /*
+     * Must be called by boot CPU#0 with interrupts disabled after gic_suspend
+     * has returned successfully.
+     */
+    ASSERT(!local_irq_is_enabled());
+    ASSERT(!smp_processor_id());
+    ASSERT(gic_hw_ops->resume);
+
+    gic_hw_ops->resume();
+}
+
  static int cpu_gic_callback(struct notifier_block *nfb,
                              unsigned long action,
                              void *hcpu)
xen/include/asm-arm/gic.h +4 -0
index c7f0c343d1..113e39460d 100644
@@ -275,6 +275,10 @@ extern int gicv_setup(struct domain *d);
  extern void gic_save_state(struct vcpu *v);
  extern void gic_restore_state(struct vcpu *v);
 
+/* Suspend/resume */
+extern int gic_suspend(void);
+extern void gic_resume(void);
+
  /* SGI (AKA IPIs) */
  enum gic_sgi {
      GIC_SGI_EVENT_CHECK,

xen/include/asm-arm/gic.h struct gic_hw_operations { · line 403

Quoted 10 lines
xen/include/asm-arm/gic.h +4 -0
@@ -390,6 +394,10 @@ struct gic_hw_operations {
      int (*iomem_deny_access)(const struct domain *d);
      /* Handle LPIs, which require special handling */
      void (*do_LPI)(unsigned int lpi);
+    /* Save GIC configuration due to the system suspend */
+    int (*suspend)(void);
+    /* Restore GIC configuration due to the system resume */
+    void (*resume)(void);
  };
 
  extern const struct gic_hw_operations *gic_hw_ops;

Cheers,

Signature · 1 line
Julien Grall
#18 lore raw

Re: [PATCH 10/19] xen/arm: Suspend/resume GIC on system suspend/resume

In reply to Mykyta Poturai

Hi,

Quoted 21 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

GIC state is saved on system suspend by calling gic_suspend (this function does not change current state of the GIC but only saves the values of configuration registers).
The state of GIC has to be restored by calling gic_resume, but only if the gic_suspend has succeeded. If gic_suspend fails, we'll just restore interrupts configuration and abort suspend.

---
  xen/arch/arm/gic.c     |  4 +---
  xen/arch/arm/suspend.c | 14 ++++++++++++++
  2 files changed, 15 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +0 -0
index e9feb1fd3b..ef90664929 100644

xen/arch/arm/gic.c int gic_suspend(void) · line 479

Quoted 7 lines
xen/arch/arm/gic.c +1 -3
@@ -476,9 +476,7 @@ int gic_suspend(void)
      if ( !gic_hw_ops->suspend || !gic_hw_ops->resume )
          return -ENOSYS;
 
-    gic_hw_ops->suspend();
-
-    return 0;
+    return gic_hw_ops->suspend();

This should be part of the previous patch.

Cheers,

Signature · 1 line
Julien Grall
#20 lore raw

Re: [PATCH 11/19] xen/arm: Suspend/resume timer interrupt generation

In reply to Mykyta Poturai

Hi,

Quoted 57 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

Timer interrupts have to be disabled while the system is in suspend. Otherwise, a timer interrupt would fire and wake-up the system. Suspending the timer interrupts consists of disabling physical EL1 and EL2 timers. The resume consists only of raising timer softirq, which will trigger the generic timer code to reprogram the EL2 timer as needed. Enabling of EL1 physical timer will be triggered by an entity which uses it.

---
  xen/arch/arm/suspend.c     |  4 ++++
  xen/arch/arm/time.c        | 22 ++++++++++++++++++++++
  xen/include/asm-arm/time.h |  3 +++
  3 files changed, 29 insertions(+)
xen/arch/arm/suspend.c +4 -0
index b94a6df86d..05c43ce502 100644
@@ -151,6 +151,8 @@ static long system_suspend(void *data)
          goto resume_nonboot_cpus;
      }
 
+    time_suspend();
+
      local_irq_save(flags);
      status = gic_suspend();
      if ( status )
@@ -166,6 +168,8 @@ static long system_suspend(void *data)
  resume_irqs:
      local_irq_restore(flags);
 
+    time_resume();
+
  resume_nonboot_cpus:
      rcu_barrier();
      enable_nonboot_cpus();
xen/arch/arm/time.c +9 -0
index dec53b5f7d..ca54bcfe68 100644
@@ -363,6 +363,28 @@ void domain_set_time_offset(struct domain *d, int64_t time_offset_seconds)
      /* XXX update guest visible wallclock time */
  }
 
+void time_suspend(void)
+{
+    /* Disable physical EL1 timer */
+    WRITE_SYSREG(0, CNTP_CTL_EL0);
+
+    /* Disable hypervisor's timer */
+    WRITE_SYSREG(0, CNTHP_CTL_EL2);
+    isb();
+}

xen/arch/arm/time.c void domain_set_time_offset(struct domain *d, int64_t time_offset_seconds) · line 384

Quoted 10 lines
xen/arch/arm/time.c +10 -0
+
+void time_resume(void)
+{
+    /*
+     * Raising timer softirq will trigger generic timer code to reprogram_timer
+     * with the correct timeout value (which is not known here). There is no
+     * need to do anything else in order to recover the time keeping from power
+     * down, because the system counter is not affected by the power down (it
+     * resides out of the ARM's cluster in an always-on part of the SoC).
+     */

Can you point me to the section in the Arm Arm stating the system counter is in an always-on part of the SoC?

xen/arch/arm/time.c void domain_set_time_offset(struct domain *d, int64_t time_offset_seconds) · line 385

Quoted 1 line
xen/arch/arm/time.c +1 -0
+    raise_softirq(TIMER_SOFTIRQ);

My expectation is that the time_resume() would closely match the x86 version (aside the arch specific part). I can't see the raise_softirq(). So why do we need it here?

Also, there is a missing call to do_settime() and update_vcpu_system_time().

Quoted 10 lines
xen/arch/arm/time.c +2 -0
+}
+
  static int cpu_time_callback(struct notifier_block *nfb,
                               unsigned long action,
                               void *hcpu)
xen/include/asm-arm/time.h +0 -0
index 4b401c1110..ded93b490a 100644

xen/include/asm-arm/time.h void preinit_xen_time(void); · line 115

Quoted 9 lines
xen/include/asm-arm/time.h +3 -0
@@ -107,6 +107,9 @@ void preinit_xen_time(void);
 
  void force_update_vcpu_system_time(struct vcpu *v);
 
+void time_suspend(void);
+void time_resume(void);
+
  #endif /* __ARM_TIME_H__ */
  /*
   * Local variables:

Cheers,

Signature · 1 line
Julien Grall
#22 lore raw

Re: [PATCH 12/19] xen/arm: Implement PSCI SYSTEM_SUSPEND call (physical interface)

In reply to Mykyta Poturai

Hi,

Quoted 27 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

PSCI system suspend function shall be invoked to finalize Xen suspend procedure. Resume entry point, which needs to be passed via 1st argument of PSCI system suspend call to the EL3, is hyp_resume. For now, hyp_resume is just a placeholder that will be implemented in assembly. Context ID, which is 2nd argument of system suspend PSCI call, is unused, as in Linux.

Update: moved hyp_resume to head.S to place it near the rest of the start code

---
  xen/arch/arm/arm64/head.S     |  3 +++
  xen/arch/arm/psci.c           | 16 ++++++++++++++++
  xen/arch/arm/suspend.c        |  4 ++++
  xen/include/asm-arm/psci.h    |  1 +
  xen/include/asm-arm/suspend.h |  1 +
  5 files changed, 25 insertions(+)
xen/arch/arm/arm64/head.S +0 -0
index aa1f88c764..8857955699 100644

xen/arch/arm/arm64/head.S ENTRY(efi_xen_start) · line 961

Quoted 5 lines
xen/arch/arm/arm64/head.S +2 -0
@@ -957,6 +957,9 @@ ENTRY(efi_xen_start)
          b     real_start_efi
  ENDPROC(efi_xen_start)
 
+ENTRY(hyp_resume)
+        b .

This needs to be part of the idmap. At the moment, this will only cover up to _end_boot.

Quoted 70 lines
xen/arch/arm/arm64/head.S +1 -0
+
  /*
   * Local variables:
   * mode: ASM
xen/arch/arm/psci.c +16 -0
index 0c90c2305c..43a67eb345 100644
@@ -25,6 +25,7 @@
  #include <asm/cpufeature.h>
  #include <asm/psci.h>
  #include <asm/acpi.h>
+#include <asm/suspend.h>
 
  /*
   * While a 64-bit OS can make calls with SMC32 calling conventions, for
@@ -68,6 +69,21 @@ void call_psci_cpu_off(void)
      }
  }
 
+int call_psci_system_suspend(void)
+{
+#ifdef CONFIG_ARM_64
+    struct arm_smccc_res res;
+
+    /* 2nd argument (context ID) is not used */
+    arm_smccc_smc(PSCI_1_0_FN64_SYSTEM_SUSPEND, __pa(hyp_resume), &res);
+
+    return PSCI_RET(res);
+#else
+    /* not supported */
+    return 1;
+#endif
+}
+
  void call_psci_system_off(void)
  {
      if ( psci_ver > PSCI_VERSION(0, 1) )
xen/arch/arm/suspend.c +4 -0
index 05c43ce502..a0258befc9 100644
@@ -161,6 +161,10 @@ static long system_suspend(void *data)
          goto resume_irqs;
      }
 
+    status = call_psci_system_suspend();
+    if ( status )
+        dprintk(XENLOG_ERR, "PSCI system suspend failed, err=%d\n", status);
+
      system_state = SYS_STATE_resume;
 
      gic_resume();
xen/include/asm-arm/psci.h +1 -0
index 26462d0c47..9f6116a224 100644
@@ -20,6 +20,7 @@ extern uint32_t psci_ver;
 
  int psci_init(void);
  int call_psci_cpu_on(int cpu);
+int call_psci_system_suspend(void);
  void call_psci_cpu_off(void);
  void call_psci_system_off(void);
  void call_psci_system_reset(void);
xen/include/asm-arm/suspend.h +0 -0
index fbaa414f0c..29420e27fa 100644

xen/include/asm-arm/suspend.h line 9

Quoted 7 lines
xen/include/asm-arm/suspend.h +1 -0
@@ -3,6 +3,7 @@
 
  int32_t domain_suspend(register_t epoint, register_t cid);
  void vcpu_resume(struct vcpu *v);
+void hyp_resume(void);
 
  #endif
 

Cheers,

Signature · 1 line
Julien Grall
#24 lore raw

Re: [PATCH 13/19] xen/arm: Resume memory management on Xen resume

In reply to Mykyta Poturai

Hi,

Quoted 9 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

The MMU needs to be enabled in the resume flow before the context can be restored (we need to be able to access the context data by virtual address in order to restore it). The configuration of system registers prior to branching to the routine that sets up the page tables is copied from xen/arch/arm/arm64/head.S.
After the MMU is enabled, the content of TTBR0_EL2 is changed to point to init_ttbr (page tables used at runtime).

This is not Arm Arm compliant. Please look at the series [1] to see how you can safely switch the MMU on and use the page tables.

Quoted 43 lines

At boot the init_ttbr variable is updated when a secondary CPU is hotplugged. In the scenario where there is only one physical CPU in the system, the init_ttbr would not be initialized for the use in resume flow. To get the variable initialized in all scenarios in this patch we add that the boot CPU updates init_ttbr after it sets the page tables for runtime.

After the memory management is resumed, the SCTLR_WXN in SCTLR_EL2 has to be set in order to configure that a mapping cannot be both writable and executable (this was configured prior to suspend). This is done using an existing function (mmu_init_secondary_cpu).

Update: moved hyp_resume to head.S to place it near the rest of the start code

---
  xen/arch/arm/arm64/entry.S      |  2 ++
  xen/arch/arm/arm64/head.S       | 30 ++++++++++++++++++++++++++++++
  xen/arch/arm/mm.c               |  1 +
  xen/arch/arm/suspend.c          |  6 ++++++
  xen/include/asm-arm/processor.h | 22 ++++++++++++++++++++++
  5 files changed, 61 insertions(+)
xen/arch/arm/arm64/entry.S +2 -0
index fc3811ad0a..f49f1daf46 100644
@@ -1,4 +1,6 @@
  #include <asm/current.h>
+#include <asm/macros.h>
+#include <asm/page.h>
  #include <asm/regs.h>
  #include <asm/alternative.h>
  #include <asm/smccc.h>
xen/arch/arm/arm64/head.S +0 -0
index 8857955699..82d83214dc 100644

xen/arch/arm/arm64/head.S ENTRY(efi_xen_start) · line 965

Quoted 8 lines
xen/arch/arm/arm64/head.S +5 -0
@@ -958,6 +958,36 @@ ENTRY(efi_xen_start)
  ENDPROC(efi_xen_start)
 
  ENTRY(hyp_resume)
+        msr   DAIFSet, 0xf           /* Disable all interrupts */
+
+        tlbi  alle2
+        dsb   sy                     /* Ensure completion of TLB flush */
+        isb

Please explain what this TLB is for.

xen/arch/arm/arm64/head.S ENTRY(efi_xen_start) · line 971

Quoted 6 lines
xen/arch/arm/arm64/head.S +6 -0
+
+        ldr   x0, =start
+        adr   x19, start             /* x19 := paddr (start) */
+        sub   x20, x19, x0           /* x20 := phys-offset */
+
+        mov   x22, #0                /* x22 := is_secondary_cpu */

x22 is not hold the is_secondary_cpu anymore.

Quoted 27 lines
xen/arch/arm/arm64/head.S +19 -0
+
+        bl    check_cpu_mode
+        bl    cpu_init
+        bl    create_page_tables
+        bl    enable_mmu
+
+        ldr   x0, =mmu_resumed      /* Explicit vaddr, not RIP-relative */
+        br    x0                    /* Get a proper vaddr into PC */
+
+mmu_resumed:
+        ldr   x4, =init_ttbr         /* VA of TTBR0_EL2 stashed by CPU 0 */
+        ldr   x4, [x4]               /* Actual value */
+        dsb   sy
+        msr   TTBR0_EL2, x4
+        dsb   sy
+        isb
+        tlbi  alle2
+        dsb   sy                     /* Ensure completion of TLB flush */
+        isb
          b .
 
  /*
xen/arch/arm/mm.c +0 -0
index eea926d823..29cdaff3bf 100644

xen/arch/arm/mm.c void __init setup_pagetables(unsigned long boot_phys_offset) · line 711

Quoted 4 lines
xen/arch/arm/mm.c +1 -0
@@ -708,6 +708,7 @@ void __init setup_pagetables(unsigned long boot_phys_offset)
      switch_ttbr(ttbr);
 
      xen_pt_enforce_wnx();
+    init_secondary_pagetables(0);

This function is used to prepare the page tables for the secondary CPUs. This may allocate memory. So this is incorrect to call for CPU0.

In this case, I think it would be better if the code to suspend sets init_ttbr and clear the boot pages tables. This could be done by split init_secondary_pagetables() in two:
1) Allocate memory for the page tables
2) Clear the boot page tables and the init_ttbr

Quoted 8 lines
xen/arch/arm/mm.c +0 -0
  #ifdef CONFIG_ARM_32
      per_cpu(xen_pgtable, 0) = cpu0_pgtable;
xen/arch/arm/suspend.c +0 -0
index a0258befc9..aa5ee4714b 100644

xen/arch/arm/suspend.c static long system_suspend(void *data) · line 173

Quoted 7 lines
xen/arch/arm/suspend.c +4 -0
@@ -167,6 +167,12 @@ static long system_suspend(void *data)
 
      system_state = SYS_STATE_resume;
 
+    /*
+     * SCTLR_WXN needs to be set to configure that a mapping cannot be both
+     * writable and executable. This is done by mmu_init_secondary_cpu.
+     */

Let's avoid to describe what a function does in the caller. This can be easily rot.

xen/arch/arm/suspend.c static long system_suspend(void *data) · line 174

Quoted 1 line
xen/arch/arm/suspend.c +1 -0
+    mmu_init_secondary_cpu();

I dislike the idea of using this function here. It is meant to be used by secondary CPUs, not CPU0.

If you want to use it here, then it should be renamed to reflect how the function is used.

Quoted 24 lines
xen/arch/arm/suspend.c +1 -0
+
      gic_resume();
 
  resume_irqs:
xen/include/asm-arm/processor.h +12 -0
index 8ab2940f68..ecf97f1ab4 100644
@@ -133,6 +133,28 @@
  #define TTBCR_PD1       (_AC(1,U)<<5)
 
  /* SCTLR System Control Register. */
+/* HSCTLR is a subset of this. */
+#define SCTLR_TE        (_AC(1,U)<<30)
+#define SCTLR_AFE       (_AC(1,U)<<29)
+#define SCTLR_TRE       (_AC(1,U)<<28)
+#define SCTLR_NMFI      (_AC(1,U)<<27)
+#define SCTLR_EE        (_AC(1,U)<<25)
+#define SCTLR_VE        (_AC(1,U)<<24)
+#define SCTLR_U         (_AC(1,U)<<22)
+#define SCTLR_FI        (_AC(1,U)<<21)
+#define SCTLR_WXN       (_AC(1,U)<<19)
+#define SCTLR_HA        (_AC(1,U)<<17)
+#define SCTLR_RR        (_AC(1,U)<<14)

xen/include/asm-arm/processor.h line 157

Quoted 10 lines
xen/include/asm-arm/processor.h +10 -0
+#define SCTLR_V         (_AC(1,U)<<13)
+#define SCTLR_I         (_AC(1,U)<<12)
+#define SCTLR_Z         (_AC(1,U)<<11)
+#define SCTLR_SW        (_AC(1,U)<<10)
+#define SCTLR_B         (_AC(1,U)<<7)
+#define SCTLR_C         (_AC(1,U)<<2)
+#define SCTLR_A         (_AC(1,U)<<1)
+#define SCTLR_M         (_AC(1,U)<<0)
+
+#define HSCTLR_BASE     _AC(0x30c51878,U)

I don't see any use of SCTLR_* and HSCTLR_* here. What why do you need to define them?

xen/include/asm-arm/processor.h line 160

Quoted 3 lines
xen/include/asm-arm/processor.h +0 -0
  /* Bits specific to SCTLR_EL1 for Arm32 */
 

Cheers,

[2] https://lore.kernel.org/all/[email protected]/

Signature · 1 line
Julien Grall
#26 lore raw

Re: [PATCH 14/19] xen/arm: Save/restore context on suspend/resume

In reply to Mykyta Poturai

Hi,

Quoted 6 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

The context of CPU general purpose and system control registers
has to be saved on suspend and restored on resume. This is
implemented in hyp_suspend and before the return from hyp_resume
function. The hyp_suspend is invoked just before the PSCI system > suspend call is issued to the ATF.

The software handling the PSCI call may not be ATF. It could be another vendor specific firmware or even another hypervisor (if running in nested environment). So let's avoid mention it.

Quoted 15 lines

The hyp_suspend has to return a
non-zero value so that the calling 'if' statement evaluates to true, causing the system suspend to be invoked. > Upon the resume, context saved on suspend will be restored, including the link register. Therefore, after restoring the context the control flow will return to the address pointed by the saved link register, which is the place from which the hyp_suspend was called. To ensure that the calling 'if' statement doesn't again evaluate to true and initiate system suspend, hyp_resume has to return a zero value after restoring the context.
Note that the order of saving register context into cpu_context structure has to match the order of restoring.
Since the suspend/resume is supported only for arm64, we define a null cpu_context structure so arm32 could compile.

Quoted 2 lines

Update: moved hyp_resume to head.S to place it near the rest of the start code

hyp_resume() is not moved in this patch. But this looks like more a changelog rather than something that should be part of the commit message.

Quoted 15 lines
---
  xen/arch/arm/arm64/head.S     | 90 ++++++++++++++++++++++++++++++++++-
  xen/arch/arm/suspend.c        | 25 ++++++++--
  xen/include/asm-arm/suspend.h | 22 +++++++++
  3 files changed, 133 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/head.S +0 -0
index 82d83214dc..e2c46a864c 100644

xen/arch/arm/arm64/head.S ENTRY(efi_xen_start) · line 961

Quoted 5 lines
xen/arch/arm/arm64/head.S +2 -0
@@ -957,6 +957,53 @@ ENTRY(efi_xen_start)
          b     real_start_efi
  ENDPROC(efi_xen_start)
 
+/*
+ * void hyp_suspend(struct cpu_context *ptr)

This prototype doesn't match the code nor the commit message.

Quoted 79 lines
xen/arch/arm/arm64/head.S +71 -1
+ *
+ * x0 - pointer to the storage where callee's context will be saved
+ *
+ * CPU context saved here will be restored on resume in hyp_resume function.
+ * hyp_suspend shall return a non-zero value. Upon restoring context
+ * hyp_resume shall return value zero instead. From C code that invokes
+ * hyp_suspend, the return value is interpreted to determine whether the context
+ * is saved (hyp_suspend) or restored (hyp_resume).
+ */
+ENTRY(hyp_suspend)
+        /* Store callee-saved registers */
+        stp     x19, x20, [x0], #16
+        stp     x21, x22, [x0], #16
+        stp     x23, x24, [x0], #16
+        stp     x25, x26, [x0], #16
+        stp     x27, x28, [x0], #16
+        stp     x29, lr, [x0], #16
+
+        /* Store stack-pointer */
+        mov     x2, sp
+        str     x2, [x0], #8
+
+        /* Store system control registers */
+        mrs     x2, VBAR_EL2
+        str     x2, [x0], #8
+        mrs     x2, VTCR_EL2
+        str     x2, [x0], #8
+        mrs     x2, VTTBR_EL2
+        str     x2, [x0], #8
+        mrs     x2, TPIDR_EL2
+        str     x2, [x0], #8
+        mrs     x2, MDCR_EL2
+        str     x2, [x0], #8
+        mrs     x2, HSTR_EL2
+        str     x2, [x0], #8
+        mrs     x2, CPTR_EL2
+        str     x2, [x0], #8
+        mrs     x2, HCR_EL2
+        str     x2, [x0], #8
+
+        /* hyp_suspend must return a non-zero value */
+        mov     x0, #1
+        ret
+
+
  ENTRY(hyp_resume)
          msr   DAIFSet, 0xf           /* Disable all interrupts */
 
@@ -988,7 +1035,48 @@ mmu_resumed:
          tlbi  alle2
          dsb   sy                     /* Ensure completion of TLB flush */
          isb
-        b .
+
+        /* Now we can access the cpu_context, so restore the context here */
+        ldr     x0, =cpu_context
+
+        /* Restore callee-saved registers */
+        ldp     x19, x20, [x0], #16
+        ldp     x21, x22, [x0], #16
+        ldp     x23, x24, [x0], #16
+        ldp     x25, x26, [x0], #16
+        ldp     x27, x28, [x0], #16
+        ldp     x29, lr, [x0], #16
+
+        /* Restore stack pointer */
+        ldr     x2, [x0], #8
+        mov     sp, x2
+
+        /* Restore system control registers */
+        ldr     x2, [x0], #8
+        msr     VBAR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     VTCR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     VTTBR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     TPIDR_EL2, x2
+        ldr     x2, [x0], #8

xen/arch/arm/arm64/head.S mmu_resumed: · line 1073

Quoted 10 lines
xen/arch/arm/arm64/head.S +10 -0
+        msr     MDCR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     HSTR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     CPTR_EL2, x2
+        ldr     x2, [x0], #8
+        msr     HCR_EL2, x2
+        isb
+
+        /* Since context is restored return from this function will appear as

I can't parse it.

xen/arch/arm/arm64/head.S mmu_resumed: · line 1077

Quoted 4 lines
xen/arch/arm/arm64/head.S +4 -0
+         * return from hyp_suspend. To distinguish a return from hyp_suspend
+         * which is called upon finalizing the suspend, as opposed to return
+         * from this function which executes on resume, we need to return zero
+         * value here. */

Coding style. Multi-line comments looks like:

/*
* Foo
* Bar
*/

Quoted 10 lines
xen/arch/arm/arm64/head.S +2 -0
+        mov x0, #0
+        ret
 
  /*
   * Local variables:
xen/arch/arm/suspend.c +0 -0
index aa5ee4714b..13d1aba658 100644

xen/arch/arm/suspend.c void vcpu_resume(struct vcpu *v) · line 140

Quoted 8 lines
xen/arch/arm/suspend.c +5 -0
@@ -133,6 +133,11 @@ void vcpu_resume(struct vcpu *v)
      clear_bit(_VPF_suspended, &v->pause_flags);
  }
 
+#ifndef CONFIG_ARM_64
+/* not supported on ARM_32 */
+int32_t hyp_suspend(struct cpu_context *ptr) { return 1; }
+#endif
+

It would be better if suspend.c is compiled out. So we avoid introduce hyp_suspend().

Quoted 56 lines
xen/arch/arm/suspend.c +17 -3
  /* Xen suspend. Note: data is not used (suspend is the suspend to RAM) */
  static long system_suspend(void *data)
  {
@@ -161,9 +166,23 @@ static long system_suspend(void *data)
          goto resume_irqs;
      }
 
-    status = call_psci_system_suspend();
-    if ( status )
-        dprintk(XENLOG_ERR, "PSCI system suspend failed, err=%d\n", status);
+    if ( hyp_suspend(&cpu_context) )
+    {
+        status = call_psci_system_suspend();
+        /*
+         * If suspend is finalized properly by above system suspend PSCI call,
+         * the code below in this 'if' branch will never execute. Execution
+         * will continue from hyp_resume which is the hypervisor's resume point.
+         * In hyp_resume CPU context will be restored and since link-register is
+         * restored as well, it will appear to return from hyp_suspend. The
+         * difference in returning from hyp_suspend on system suspend versus
+         * resume is in function's return value: on suspend, the return value is
+         * a non-zero value, on resume it is zero. That is why the control flow
+         * will not re-enter this 'if' branch on resume.
+         */
+        if ( status )
+            dprintk(XENLOG_ERR, "PSCI system suspend failed, err=%d\n", status);
+    }
 
      system_state = SYS_STATE_resume;
 
xen/include/asm-arm/suspend.h +18 -0
index 29420e27fa..70dbf4e208 100644
@@ -1,9 +1,31 @@
  #ifndef __ASM_ARM_SUSPEND_H__
  #define __ASM_ARM_SUSPEND_H__
 
+#ifdef CONFIG_ARM_64
+struct cpu_context {
+    uint64_t callee_regs[12];
+    uint64_t sp;
+    uint64_t vbar_el2;
+    uint64_t vtcr_el2;
+    uint64_t vttbr_el2;
+    uint64_t tpidr_el2;
+    uint64_t mdcr_el2;
+    uint64_t hstr_el2;
+    uint64_t cptr_el2;
+    uint64_t hcr_el2;
+} __aligned(16);
+#else
+struct cpu_context {
+    uint8_t pad;
+};
+#endif

xen/include/asm-arm/suspend.h line 31

Quoted 10 lines
xen/include/asm-arm/suspend.h +4 -0
+
+extern struct cpu_context cpu_context;
+
  int32_t domain_suspend(register_t epoint, register_t cid);
  void vcpu_resume(struct vcpu *v);
  void hyp_resume(void);
+int32_t hyp_suspend(struct cpu_context *ptr);
 
  #endif
 

Cheers,

Signature · 1 line
Julien Grall
#29 lore raw

Re: [PATCH 16/19] xen/arm: Suspend/resume console on Xen suspend/resume

In reply to Mykyta Poturai

Hi,

Quoted 6 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

This is done using generic console_suspend/resume functions that cause uart driver specific suspend/resume handlers to be called for each initialized port (if the port has suspend/resume driver handlers implemented).

Looking at the UART driver for Arm, most of them (if not all) implement suspend/resume with BUG(). So don't you need to properly implement them?

Quoted 3 lines

Your signed-off-by is missing.

Quoted 33 lines
---
  xen/arch/arm/suspend.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
xen/arch/arm/suspend.c +10 -0
index 4a690eac3b..cf3aab0099 100644
@@ -14,6 +14,7 @@
 
  #include <xen/sched.h>
  #include <xen/cpu.h>
+#include <xen/console.h>
  #include <asm/cpufeature.h>
  #include <asm/event.h>
  #include <asm/psci.h>
@@ -166,6 +167,15 @@ static long system_suspend(void *data)
          goto resume_irqs;
      }
 
+    dprintk(XENLOG_DEBUG, "Suspend\n");
+    status = console_suspend();
+    if ( status )
+    {
+        dprintk(XENLOG_ERR, "Failed to suspend the console, err=%d\n", status);
+        system_state = SYS_STATE_resume;
+        goto resume_console;
+    }
+
      if ( hyp_suspend(&cpu_context) )
      {
          status = call_psci_system_suspend();

xen/arch/arm/suspend.c static long system_suspend(void *data) · line 211

Quoted 10 lines
xen/arch/arm/suspend.c +4 -0
@@ -192,6 +202,10 @@ static long system_suspend(void *data)
       */
      mmu_init_secondary_cpu();
 
+resume_console:
+    console_resume();
+    dprintk(XENLOG_DEBUG, "Resume\n");
+
      gic_resume();
 
  resume_irqs:

Cheers,

Signature · 1 line
Julien Grall
#30 lore raw

Re: [PATCH 16/19] xen/arm: Suspend/resume console on Xen suspend/resume

Quoted 11 lines · Julien Grall

Hi,

Quoted 6 lines · Mykyta Poturai

From: Mirela Simonovic <[email protected]>

This is done using generic console_suspend/resume functions that cause uart driver specific suspend/resume handlers to be called for each initialized port (if the port has suspend/resume driver handlers implemented).

Looking at the UART driver for Arm, most of them (if not all) implement suspend/resume with BUG(). So don't you need to properly implement them?

I forgot to clarify. No need to implement for every driver. I am just puzzled how this was actually tested. What UART are you using on your platform?

Cheers,

Signature · 1 line
Julien Grall
#37 lore raw

Re: [PATCH 19/19] Fix misleading indentation gcc warning

In reply to Mykyta Poturai

Hi,

Quoted 16 lines · Mykyta Poturai

From: Oleksandr Andrushchenko <[email protected]>

percpu.c: In function 'cpu_percpu_callback':
percpu.c:61:7: error: this 'if' clause does not guard... [-Werror=misleading-indentation]

        if ( system_state != SYS_STATE_resume )
        ^~
percpu.c:63:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
          break;
          ^~~~~
percpu.c:67:7: error: this 'if' clause does not guard... [-Werror=misleading-indentation]
        if ( system_state != SYS_STATE_suspend )
        ^~
percpu.c:69:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
          break;
          ^~~~~
Quoted 1 line
Fixes c3109b76d967 ("xen: don't free percpu areas during suspend")

This commit hash doesn't seem to exist upstream. But it sounds like you are fixing a bug that was introduced by this series.

In general, patch within a series should be able to compile without any follow-up requirement. Similarly, we should not bug fix a patch within the same series.

Instead, this should be folded in the patch that introduced the problem.

Cheers,

Signature · 1 line
Julien Grall
#35 lore raw

Re: [PATCH 18/19] timers: Don't migrate timers during suspend

In reply to Mykyta Poturai

Hi,

Quoted 1 line · Mykyta Poturai

Migrating timers during suspend causes Dom0 to freeze after resume.

This wants a bit more details such as why dom0 will freeze. But looking at upstream, there might be some patches that (e.g. 37f82facd62f "xen/sched: migrate timers to correct cpus after suspend") could have solved a similar problem.

Cheers,

Signature · 1 line
Julien Grall