Blame SOURCES/kvm-spapr-Capabilities-infrastructure.patch

4a2fec
From 466322847847d3948eb7699aafcf9afd9ac3fc26 Mon Sep 17 00:00:00 2001
4a2fec
From: David Gibson <dgibson@redhat.com>
4a2fec
Date: Fri, 19 Jan 2018 02:34:36 +0100
4a2fec
Subject: [PATCH 08/21] spapr: Capabilities infrastructure
4a2fec
4a2fec
RH-Author: David Gibson <dgibson@redhat.com>
4a2fec
Message-id: <20180119023442.28577-2-dgibson@redhat.com>
4a2fec
Patchwork-id: 78668
4a2fec
O-Subject: [RHEL-7.5 qemu-kvm-rhev PATCH 1/7] spapr: Capabilities infrastructure
4a2fec
Bugzilla: 1523414
4a2fec
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
4a2fec
RH-Acked-by: Thomas Huth <thuth@redhat.com>
4a2fec
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
4a2fec
From: David Gibson <david@gibson.dropbear.id.au>
4a2fec
4a2fec
Because PAPR is a paravirtual environment access to certain CPU (or other)
4a2fec
facilities can be blocked by the hypervisor.  PAPR provides ways to
4a2fec
advertise in the device tree whether or not those features are available to
4a2fec
the guest.
4a2fec
4a2fec
In some places we automatically determine whether to make a feature
4a2fec
available based on whether our host can support it, in most cases this is
4a2fec
based on limitations in the available KVM implementation.
4a2fec
4a2fec
Although we correctly advertise this to the guest, it means that host
4a2fec
factors might make changes to the guest visible environment which is bad:
4a2fec
as well as generaly reducing reproducibility, it means that a migration
4a2fec
between different host environments can easily go bad.
4a2fec
4a2fec
We've mostly gotten away with it because the environments considered mature
4a2fec
enough to be well supported (basically, KVM on POWER8) have had consistent
4a2fec
feature availability.  But, it's still not right and some limitations on
4a2fec
POWER9 is going to make it more of an issue in future.
4a2fec
4a2fec
This introduces an infrastructure for defining "sPAPR capabilities".  These
4a2fec
are set by default based on the machine version, masked by the capabilities
4a2fec
of the chosen cpu, but can be overriden with machine properties.
4a2fec
4a2fec
The intention is at reset time we verify that the requested capabilities
4a2fec
can be supported on the host (considering TCG, KVM and/or host cpu
4a2fec
limitations).  If not we simply fail, rather than silently modifying the
4a2fec
advertised featureset to the guest.
4a2fec
4a2fec
This does mean that certain configurations that "worked" may now fail, but
4a2fec
such configurations were already more subtly broken.
4a2fec
4a2fec
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
4a2fec
Reviewed-by: Greg Kurz <groug@kaod.org>
4a2fec
(cherry picked from commit 33face6b8981add8eba1f7cdaf4cf6cede415d2e)
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
4a2fec
Conflicts:
4a2fec
	hw/ppc/spapr.c
4a2fec
	include/hw/ppc/spapr.h
4a2fec
4a2fec
Simple contextual conflicts.
4a2fec
4a2fec
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1523414
4a2fec
4a2fec
Signed-off-by: David Gibson <dgibson@redhat.com>
4a2fec
---
4a2fec
 hw/ppc/Makefile.objs   |   2 +-
4a2fec
 hw/ppc/spapr.c         |   7 ++
4a2fec
 hw/ppc/spapr_caps.c    | 181 +++++++++++++++++++++++++++++++++++++++++++++++++
4a2fec
 include/hw/ppc/spapr.h |  31 +++++++++
4a2fec
 4 files changed, 220 insertions(+), 1 deletion(-)
4a2fec
 create mode 100644 hw/ppc/spapr_caps.c
4a2fec
4a2fec
diff --git a/hw/ppc/Makefile.objs b/hw/ppc/Makefile.objs
4a2fec
index 856cef5..d4cdbb8 100644
4a2fec
--- a/hw/ppc/Makefile.objs
4a2fec
+++ b/hw/ppc/Makefile.objs
4a2fec
@@ -1,7 +1,7 @@
4a2fec
 # shared objects
4a2fec
 obj-y += ppc.o ppc_booke.o fdt.o
4a2fec
 # IBM pSeries (sPAPR)
4a2fec
-obj-$(CONFIG_PSERIES) += spapr.o spapr_vio.o spapr_events.o
4a2fec
+obj-$(CONFIG_PSERIES) += spapr.o spapr_caps.o spapr_vio.o spapr_events.o
4a2fec
 obj-$(CONFIG_PSERIES) += spapr_hcall.o spapr_iommu.o spapr_rtas.o
4a2fec
 obj-$(CONFIG_PSERIES) += spapr_pci.o spapr_rtc.o spapr_drc.o
4a2fec
 obj-$(CONFIG_PSERIES) += spapr_cpu_core.o spapr_ovec.o
4a2fec
diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
4a2fec
index cdc56f3..7a4191d 100644
4a2fec
--- a/hw/ppc/spapr.c
4a2fec
+++ b/hw/ppc/spapr.c
4a2fec
@@ -1424,6 +1424,8 @@ static void ppc_spapr_reset(void)
4a2fec
     /* Check for unknown sysbus devices */
4a2fec
     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
4a2fec
 
4a2fec
+    spapr_caps_reset(spapr);
4a2fec
+
4a2fec
     first_ppc_cpu = POWERPC_CPU(first_cpu);
4a2fec
     if (kvm_enabled() && kvmppc_has_cap_mmu_radix() &&
4a2fec
         ppc_check_compat(first_ppc_cpu, CPU_POWERPC_LOGICAL_3_00, 0,
4a2fec
@@ -2267,6 +2269,8 @@ static void ppc_spapr_init(MachineState *machine)
4a2fec
     char *filename;
4a2fec
     Error *resize_hpt_err = NULL;
4a2fec
 
4a2fec
+    spapr_caps_validate(spapr, &error_fatal);
4a2fec
+
4a2fec
     msi_nonbroken = true;
4a2fec
 
4a2fec
     QLIST_INIT(&spapr->phbs);
4a2fec
@@ -3635,6 +3639,9 @@ static void spapr_machine_class_init(ObjectClass *oc, void *data)
4a2fec
      */
4a2fec
     mc->numa_mem_align_shift = 28;
4a2fec
     smc->has_power9_support = true;
4a2fec
+
4a2fec
+    smc->default_caps = spapr_caps(0);
4a2fec
+    spapr_caps_add_properties(smc, &error_abort);
4a2fec
 }
4a2fec
 
4a2fec
 static const TypeInfo spapr_machine_info = {
4a2fec
diff --git a/hw/ppc/spapr_caps.c b/hw/ppc/spapr_caps.c
4a2fec
new file mode 100644
4a2fec
index 0000000..968ba7b
4a2fec
--- /dev/null
4a2fec
+++ b/hw/ppc/spapr_caps.c
4a2fec
@@ -0,0 +1,181 @@
4a2fec
+/*
4a2fec
+ * QEMU PowerPC pSeries Logical Partition capabilities handling
4a2fec
+ *
4a2fec
+ * Copyright (c) 2017 David Gibson, Red Hat Inc.
4a2fec
+ *
4a2fec
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4a2fec
+ * of this software and associated documentation files (the "Software"), to deal
4a2fec
+ * in the Software without restriction, including without limitation the rights
4a2fec
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
4a2fec
+ * copies of the Software, and to permit persons to whom the Software is
4a2fec
+ * furnished to do so, subject to the following conditions:
4a2fec
+ *
4a2fec
+ * The above copyright notice and this permission notice shall be included in
4a2fec
+ * all copies or substantial portions of the Software.
4a2fec
+ *
4a2fec
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4a2fec
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4a2fec
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
4a2fec
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4a2fec
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
4a2fec
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
4a2fec
+ * THE SOFTWARE.
4a2fec
+ */
4a2fec
+#include "qemu/osdep.h"
4a2fec
+#include "qapi/error.h"
4a2fec
+#include "qapi/visitor.h"
4a2fec
+
4a2fec
+#include "hw/ppc/spapr.h"
4a2fec
+
4a2fec
+typedef struct sPAPRCapabilityInfo {
4a2fec
+    const char *name;
4a2fec
+    const char *description;
4a2fec
+    uint64_t flag;
4a2fec
+
4a2fec
+    /* Make sure the virtual hardware can support this capability */
4a2fec
+    void (*allow)(sPAPRMachineState *spapr, Error **errp);
4a2fec
+
4a2fec
+    /* If possible, tell the virtual hardware not to allow the cap to
4a2fec
+     * be used at all */
4a2fec
+    void (*disallow)(sPAPRMachineState *spapr, Error **errp);
4a2fec
+} sPAPRCapabilityInfo;
4a2fec
+
4a2fec
+static sPAPRCapabilityInfo capability_table[] = {
4a2fec
+};
4a2fec
+
4a2fec
+static sPAPRCapabilities default_caps_with_cpu(sPAPRMachineState *spapr,
4a2fec
+                                               CPUState *cs)
4a2fec
+{
4a2fec
+    sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
4a2fec
+    sPAPRCapabilities caps;
4a2fec
+
4a2fec
+    caps = smc->default_caps;
4a2fec
+
4a2fec
+    /* TODO: clamp according to cpu model */
4a2fec
+
4a2fec
+    return caps;
4a2fec
+}
4a2fec
+
4a2fec
+void spapr_caps_reset(sPAPRMachineState *spapr)
4a2fec
+{
4a2fec
+    Error *local_err = NULL;
4a2fec
+    sPAPRCapabilities caps;
4a2fec
+    int i;
4a2fec
+
4a2fec
+    /* First compute the actual set of caps we're running with.. */
4a2fec
+    caps = default_caps_with_cpu(spapr, first_cpu);
4a2fec
+
4a2fec
+    caps.mask |= spapr->forced_caps.mask;
4a2fec
+    caps.mask &= ~spapr->forbidden_caps.mask;
4a2fec
+
4a2fec
+    spapr->effective_caps = caps;
4a2fec
+
4a2fec
+    /* .. then apply those caps to the virtual hardware */
4a2fec
+
4a2fec
+    for (i = 0; i < ARRAY_SIZE(capability_table); i++) {
4a2fec
+        sPAPRCapabilityInfo *info = &capability_table[i];
4a2fec
+
4a2fec
+        if (spapr->effective_caps.mask & info->flag) {
4a2fec
+            /* Failure to allow a cap is fatal - if the guest doesn't
4a2fec
+             * have it, we'll be supplying an incorrect environment */
4a2fec
+            if (info->allow) {
4a2fec
+                info->allow(spapr, &error_fatal);
4a2fec
+            }
4a2fec
+        } else {
4a2fec
+            /* Failure to enforce a cap is only a warning.  The guest
4a2fec
+             * shouldn't be using it, since it's not advertised, so it
4a2fec
+             * doesn't get to complain about weird behaviour if it
4a2fec
+             * goes ahead anyway */
4a2fec
+            if (info->disallow) {
4a2fec
+                info->disallow(spapr, &local_err);
4a2fec
+            }
4a2fec
+            if (local_err) {
4a2fec
+                warn_report_err(local_err);
4a2fec
+                local_err = NULL;
4a2fec
+            }
4a2fec
+        }
4a2fec
+    }
4a2fec
+}
4a2fec
+
4a2fec
+static void spapr_cap_get(Object *obj, Visitor *v, const char *name,
4a2fec
+                          void *opaque, Error **errp)
4a2fec
+{
4a2fec
+    sPAPRCapabilityInfo *cap = opaque;
4a2fec
+    sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
4a2fec
+    bool value = spapr_has_cap(spapr, cap->flag);
4a2fec
+
4a2fec
+    /* TODO: Could this get called before effective_caps is finalized
4a2fec
+     * in spapr_caps_reset()? */
4a2fec
+
4a2fec
+    visit_type_bool(v, name, &value, errp);
4a2fec
+}
4a2fec
+
4a2fec
+static void spapr_cap_set(Object *obj, Visitor *v, const char *name,
4a2fec
+                          void *opaque, Error **errp)
4a2fec
+{
4a2fec
+    sPAPRCapabilityInfo *cap = opaque;
4a2fec
+    sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
4a2fec
+    bool value;
4a2fec
+    Error *local_err = NULL;
4a2fec
+
4a2fec
+    visit_type_bool(v, name, &value, &local_err);
4a2fec
+    if (local_err) {
4a2fec
+        error_propagate(errp, local_err);
4a2fec
+        return;
4a2fec
+    }
4a2fec
+
4a2fec
+    if (value) {
4a2fec
+        spapr->forced_caps.mask |= cap->flag;
4a2fec
+    } else {
4a2fec
+        spapr->forbidden_caps.mask |= cap->flag;
4a2fec
+    }
4a2fec
+}
4a2fec
+
4a2fec
+void spapr_caps_validate(sPAPRMachineState *spapr, Error **errp)
4a2fec
+{
4a2fec
+    uint64_t allcaps = 0;
4a2fec
+    int i;
4a2fec
+
4a2fec
+    for (i = 0; i < ARRAY_SIZE(capability_table); i++) {
4a2fec
+        g_assert((allcaps & capability_table[i].flag) == 0);
4a2fec
+        allcaps |= capability_table[i].flag;
4a2fec
+    }
4a2fec
+
4a2fec
+    g_assert((spapr->forced_caps.mask & ~allcaps) == 0);
4a2fec
+    g_assert((spapr->forbidden_caps.mask & ~allcaps) == 0);
4a2fec
+
4a2fec
+    if (spapr->forced_caps.mask & spapr->forbidden_caps.mask) {
4a2fec
+        error_setg(errp, "Some sPAPR capabilities set both on and off");
4a2fec
+        return;
4a2fec
+    }
4a2fec
+
4a2fec
+    /* Check for any caps incompatible with other caps.  Nothing to do
4a2fec
+     * yet */
4a2fec
+}
4a2fec
+
4a2fec
+void spapr_caps_add_properties(sPAPRMachineClass *smc, Error **errp)
4a2fec
+{
4a2fec
+    Error *local_err = NULL;
4a2fec
+    ObjectClass *klass = OBJECT_CLASS(smc);
4a2fec
+    int i;
4a2fec
+
4a2fec
+    for (i = 0; i < ARRAY_SIZE(capability_table); i++) {
4a2fec
+        sPAPRCapabilityInfo *cap = &capability_table[i];
4a2fec
+        const char *name = g_strdup_printf("cap-%s", cap->name);
4a2fec
+
4a2fec
+        object_class_property_add(klass, name, "bool",
4a2fec
+                                  spapr_cap_get, spapr_cap_set, NULL,
4a2fec
+                                  cap, &local_err);
4a2fec
+        if (local_err) {
4a2fec
+            error_propagate(errp, local_err);
4a2fec
+            return;
4a2fec
+        }
4a2fec
+
4a2fec
+        object_class_property_set_description(klass, name, cap->description,
4a2fec
+                                              &local_err);
4a2fec
+        if (local_err) {
4a2fec
+            error_propagate(errp, local_err);
4a2fec
+            return;
4a2fec
+        }
4a2fec
+    }
4a2fec
+}
4a2fec
diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h
4a2fec
index d9e8e5a..7267151 100644
4a2fec
--- a/include/hw/ppc/spapr.h
4a2fec
+++ b/include/hw/ppc/spapr.h
4a2fec
@@ -51,6 +51,15 @@ typedef enum {
4a2fec
 } sPAPRResizeHPT;
4a2fec
 
4a2fec
 /**
4a2fec
+ * Capabilities
4a2fec
+ */
4a2fec
+
4a2fec
+typedef struct sPAPRCapabilities sPAPRCapabilities;
4a2fec
+struct sPAPRCapabilities {
4a2fec
+    uint64_t mask;
4a2fec
+};
4a2fec
+
4a2fec
+/**
4a2fec
  * sPAPRMachineClass:
4a2fec
  */
4a2fec
 struct sPAPRMachineClass {
4a2fec
@@ -68,6 +77,7 @@ struct sPAPRMachineClass {
4a2fec
                           hwaddr *mmio32, hwaddr *mmio64,
4a2fec
                           unsigned n_dma, uint32_t *liobns, Error **errp);
4a2fec
     sPAPRResizeHPT resize_hpt_default;
4a2fec
+    sPAPRCapabilities default_caps;
4a2fec
 };
4a2fec
 
4a2fec
 /**
4a2fec
@@ -129,6 +139,9 @@ struct sPAPRMachineState {
4a2fec
     MemoryHotplugState hotplug_memory;
4a2fec
 
4a2fec
     const char *icp_type;
4a2fec
+
4a2fec
+    sPAPRCapabilities forced_caps, forbidden_caps;
4a2fec
+    sPAPRCapabilities effective_caps;
4a2fec
 };
4a2fec
 
4a2fec
 #define H_SUCCESS         0
4a2fec
@@ -707,4 +720,22 @@ void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg);
4a2fec
 
4a2fec
 #define HTAB_SIZE(spapr)        (1ULL << ((spapr)->htab_shift))
4a2fec
 
4a2fec
+/*
4a2fec
+ * Handling of optional capabilities
4a2fec
+ */
4a2fec
+static inline sPAPRCapabilities spapr_caps(uint64_t mask)
4a2fec
+{
4a2fec
+    sPAPRCapabilities caps = { mask };
4a2fec
+    return caps;
4a2fec
+}
4a2fec
+
4a2fec
+static inline bool spapr_has_cap(sPAPRMachineState *spapr, uint64_t cap)
4a2fec
+{
4a2fec
+    return !!(spapr->effective_caps.mask & cap);
4a2fec
+}
4a2fec
+
4a2fec
+void spapr_caps_reset(sPAPRMachineState *spapr);
4a2fec
+void spapr_caps_validate(sPAPRMachineState *spapr, Error **errp);
4a2fec
+void spapr_caps_add_properties(sPAPRMachineClass *smc, Error **errp);
4a2fec
+
4a2fec
 #endif /* HW_SPAPR_H */
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec