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

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