qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Michael Roth <mdroth@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: qemu-stable@nongnu.org, christian.ehrhardt@canonical.com,
	David Gibson <david@gibson.dropbear.id.au>
Subject: [Qemu-devel] [PATCH 38/54] spapr: Capabilities infrastructure
Date: Tue,  6 Feb 2018 13:14:59 -0600	[thread overview]
Message-ID: <20180206191515.25830-39-mdroth@linux.vnet.ibm.com> (raw)
In-Reply-To: <20180206191515.25830-1-mdroth@linux.vnet.ibm.com>

From: David Gibson <david@gibson.dropbear.id.au>

Because PAPR is a paravirtual environment access to certain CPU (or other)
facilities can be blocked by the hypervisor.  PAPR provides ways to
advertise in the device tree whether or not those features are available to
the guest.

In some places we automatically determine whether to make a feature
available based on whether our host can support it, in most cases this is
based on limitations in the available KVM implementation.

Although we correctly advertise this to the guest, it means that host
factors might make changes to the guest visible environment which is bad:
as well as generaly reducing reproducibility, it means that a migration
between different host environments can easily go bad.

We've mostly gotten away with it because the environments considered mature
enough to be well supported (basically, KVM on POWER8) have had consistent
feature availability.  But, it's still not right and some limitations on
POWER9 is going to make it more of an issue in future.

This introduces an infrastructure for defining "sPAPR capabilities".  These
are set by default based on the machine version, masked by the capabilities
of the chosen cpu, but can be overriden with machine properties.

The intention is at reset time we verify that the requested capabilities
can be supported on the host (considering TCG, KVM and/or host cpu
limitations).  If not we simply fail, rather than silently modifying the
advertised featureset to the guest.

This does mean that certain configurations that "worked" may now fail, but
such configurations were already more subtly broken.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Greg Kurz <groug@kaod.org>
(cherry picked from commit 33face6b8981add8eba1f7cdaf4cf6cede415d2e)
 Conflicts:
	include/hw/ppc/spapr.h
*drop context dep on 60c6823b9bc
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 hw/ppc/Makefile.objs   |   2 +-
 hw/ppc/spapr.c         |   7 ++
 hw/ppc/spapr_caps.c    | 181 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/spapr.h |  31 +++++++++
 4 files changed, 220 insertions(+), 1 deletion(-)
 create mode 100644 hw/ppc/spapr_caps.c

diff --git a/hw/ppc/Makefile.objs b/hw/ppc/Makefile.objs
index 7efc686748..1faff853b7 100644
--- a/hw/ppc/Makefile.objs
+++ b/hw/ppc/Makefile.objs
@@ -1,7 +1,7 @@
 # shared objects
 obj-y += ppc.o ppc_booke.o fdt.o
 # IBM pSeries (sPAPR)
-obj-$(CONFIG_PSERIES) += spapr.o spapr_vio.o spapr_events.o
+obj-$(CONFIG_PSERIES) += spapr.o spapr_caps.o spapr_vio.o spapr_events.o
 obj-$(CONFIG_PSERIES) += spapr_hcall.o spapr_iommu.o spapr_rtas.o
 obj-$(CONFIG_PSERIES) += spapr_pci.o spapr_rtc.o spapr_drc.o spapr_rng.o
 obj-$(CONFIG_PSERIES) += spapr_cpu_core.o spapr_ovec.o
diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
index bedc39a2e5..958d894add 100644
--- a/hw/ppc/spapr.c
+++ b/hw/ppc/spapr.c
@@ -1440,6 +1440,8 @@ static void ppc_spapr_reset(void)
     /* Check for unknown sysbus devices */
     foreach_dynamic_sysbus_device(find_unknown_sysbus_device, NULL);
 
+    spapr_caps_reset(spapr);
+
     first_ppc_cpu = POWERPC_CPU(first_cpu);
     if (kvm_enabled() && kvmppc_has_cap_mmu_radix() &&
         ppc_check_compat(first_ppc_cpu, CPU_POWERPC_LOGICAL_3_00, 0,
@@ -2285,6 +2287,8 @@ static void ppc_spapr_init(MachineState *machine)
     char *filename;
     Error *resize_hpt_err = NULL;
 
+    spapr_caps_validate(spapr, &error_fatal);
+
     msi_nonbroken = true;
 
     QLIST_INIT(&spapr->phbs);
@@ -3665,6 +3669,9 @@ static void spapr_machine_class_init(ObjectClass *oc, void *data)
      * in which LMBs are represented and hot-added
      */
     mc->numa_mem_align_shift = 28;
+
+    smc->default_caps = spapr_caps(0);
+    spapr_caps_add_properties(smc, &error_abort);
 }
 
 static const TypeInfo spapr_machine_info = {
diff --git a/hw/ppc/spapr_caps.c b/hw/ppc/spapr_caps.c
new file mode 100644
index 0000000000..968ba7b857
--- /dev/null
+++ b/hw/ppc/spapr_caps.c
@@ -0,0 +1,181 @@
+/*
+ * QEMU PowerPC pSeries Logical Partition capabilities handling
+ *
+ * Copyright (c) 2017 David Gibson, Red Hat Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qapi/visitor.h"
+
+#include "hw/ppc/spapr.h"
+
+typedef struct sPAPRCapabilityInfo {
+    const char *name;
+    const char *description;
+    uint64_t flag;
+
+    /* Make sure the virtual hardware can support this capability */
+    void (*allow)(sPAPRMachineState *spapr, Error **errp);
+
+    /* If possible, tell the virtual hardware not to allow the cap to
+     * be used at all */
+    void (*disallow)(sPAPRMachineState *spapr, Error **errp);
+} sPAPRCapabilityInfo;
+
+static sPAPRCapabilityInfo capability_table[] = {
+};
+
+static sPAPRCapabilities default_caps_with_cpu(sPAPRMachineState *spapr,
+                                               CPUState *cs)
+{
+    sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
+    sPAPRCapabilities caps;
+
+    caps = smc->default_caps;
+
+    /* TODO: clamp according to cpu model */
+
+    return caps;
+}
+
+void spapr_caps_reset(sPAPRMachineState *spapr)
+{
+    Error *local_err = NULL;
+    sPAPRCapabilities caps;
+    int i;
+
+    /* First compute the actual set of caps we're running with.. */
+    caps = default_caps_with_cpu(spapr, first_cpu);
+
+    caps.mask |= spapr->forced_caps.mask;
+    caps.mask &= ~spapr->forbidden_caps.mask;
+
+    spapr->effective_caps = caps;
+
+    /* .. then apply those caps to the virtual hardware */
+
+    for (i = 0; i < ARRAY_SIZE(capability_table); i++) {
+        sPAPRCapabilityInfo *info = &capability_table[i];
+
+        if (spapr->effective_caps.mask & info->flag) {
+            /* Failure to allow a cap is fatal - if the guest doesn't
+             * have it, we'll be supplying an incorrect environment */
+            if (info->allow) {
+                info->allow(spapr, &error_fatal);
+            }
+        } else {
+            /* Failure to enforce a cap is only a warning.  The guest
+             * shouldn't be using it, since it's not advertised, so it
+             * doesn't get to complain about weird behaviour if it
+             * goes ahead anyway */
+            if (info->disallow) {
+                info->disallow(spapr, &local_err);
+            }
+            if (local_err) {
+                warn_report_err(local_err);
+                local_err = NULL;
+            }
+        }
+    }
+}
+
+static void spapr_cap_get(Object *obj, Visitor *v, const char *name,
+                          void *opaque, Error **errp)
+{
+    sPAPRCapabilityInfo *cap = opaque;
+    sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
+    bool value = spapr_has_cap(spapr, cap->flag);
+
+    /* TODO: Could this get called before effective_caps is finalized
+     * in spapr_caps_reset()? */
+
+    visit_type_bool(v, name, &value, errp);
+}
+
+static void spapr_cap_set(Object *obj, Visitor *v, const char *name,
+                          void *opaque, Error **errp)
+{
+    sPAPRCapabilityInfo *cap = opaque;
+    sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
+    bool value;
+    Error *local_err = NULL;
+
+    visit_type_bool(v, name, &value, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+
+    if (value) {
+        spapr->forced_caps.mask |= cap->flag;
+    } else {
+        spapr->forbidden_caps.mask |= cap->flag;
+    }
+}
+
+void spapr_caps_validate(sPAPRMachineState *spapr, Error **errp)
+{
+    uint64_t allcaps = 0;
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(capability_table); i++) {
+        g_assert((allcaps & capability_table[i].flag) == 0);
+        allcaps |= capability_table[i].flag;
+    }
+
+    g_assert((spapr->forced_caps.mask & ~allcaps) == 0);
+    g_assert((spapr->forbidden_caps.mask & ~allcaps) == 0);
+
+    if (spapr->forced_caps.mask & spapr->forbidden_caps.mask) {
+        error_setg(errp, "Some sPAPR capabilities set both on and off");
+        return;
+    }
+
+    /* Check for any caps incompatible with other caps.  Nothing to do
+     * yet */
+}
+
+void spapr_caps_add_properties(sPAPRMachineClass *smc, Error **errp)
+{
+    Error *local_err = NULL;
+    ObjectClass *klass = OBJECT_CLASS(smc);
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(capability_table); i++) {
+        sPAPRCapabilityInfo *cap = &capability_table[i];
+        const char *name = g_strdup_printf("cap-%s", cap->name);
+
+        object_class_property_add(klass, name, "bool",
+                                  spapr_cap_get, spapr_cap_set, NULL,
+                                  cap, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
+
+        object_class_property_set_description(klass, name, cap->description,
+                                              &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
+    }
+}
diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h
index 9d21ca9bde..c8852dfbba 100644
--- a/include/hw/ppc/spapr.h
+++ b/include/hw/ppc/spapr.h
@@ -51,6 +51,15 @@ typedef enum {
 } sPAPRResizeHPT;
 
 /**
+ * Capabilities
+ */
+
+typedef struct sPAPRCapabilities sPAPRCapabilities;
+struct sPAPRCapabilities {
+    uint64_t mask;
+};
+
+/**
  * sPAPRMachineClass:
  */
 struct sPAPRMachineClass {
@@ -66,6 +75,7 @@ struct sPAPRMachineClass {
                           hwaddr *mmio32, hwaddr *mmio64,
                           unsigned n_dma, uint32_t *liobns, Error **errp);
     sPAPRResizeHPT resize_hpt_default;
+    sPAPRCapabilities default_caps;
 };
 
 /**
@@ -127,6 +137,9 @@ struct sPAPRMachineState {
     MemoryHotplugState hotplug_memory;
 
     const char *icp_type;
+
+    sPAPRCapabilities forced_caps, forbidden_caps;
+    sPAPRCapabilities effective_caps;
 };
 
 #define H_SUCCESS         0
@@ -707,4 +720,22 @@ void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg);
 int spapr_vcpu_id(PowerPCCPU *cpu);
 PowerPCCPU *spapr_find_cpu(int vcpu_id);
 
+/*
+ * Handling of optional capabilities
+ */
+static inline sPAPRCapabilities spapr_caps(uint64_t mask)
+{
+    sPAPRCapabilities caps = { mask };
+    return caps;
+}
+
+static inline bool spapr_has_cap(sPAPRMachineState *spapr, uint64_t cap)
+{
+    return !!(spapr->effective_caps.mask & cap);
+}
+
+void spapr_caps_reset(sPAPRMachineState *spapr);
+void spapr_caps_validate(sPAPRMachineState *spapr, Error **errp);
+void spapr_caps_add_properties(sPAPRMachineClass *smc, Error **errp);
+
 #endif /* HW_SPAPR_H */
-- 
2.11.0

  parent reply	other threads:[~2018-02-06 19:16 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-06 19:14 [Qemu-devel] [PATCH 00/54] Patch Round-up for stable 2.11.1, freeze on 2018-02-12 Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 01/54] target/i386: Fix handling of VEX prefixes Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 02/54] block/iscsi: dont leave allocmap in an invalid state on UNMAP failure Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 03/54] target/sh4: fix TCG leak during gusa sequence Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 04/54] qemu-options: Remove stray colons from output of --help Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 05/54] qemu-pr-helper: miscellaneous fixes Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 06/54] block/nbd: fix segmentation fault when .desc is not null-terminated Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 07/54] block: Make bdrv_drain_invoke() recursive Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 08/54] block: Call .drain_begin only once in bdrv_drain_all_begin() Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 09/54] block: Open backing image in force share mode for size probe Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 10/54] vfio: Fix vfio-kvm group registration Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 11/54] hw/intc/arm_gicv3: Make reserved register addresses RAZ/WI Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 12/54] hw/intc/arm_gic: reserved register addresses are RAZ/WI Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 13/54] virtio_error: don't invoke status callbacks Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 14/54] vhost: remove assertion to prevent crash Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 15/54] hw/sd/pl181: Reset SD card on controller reset Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 16/54] hw/sd/milkymist-memcard: " Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 17/54] hw/sd/ssi-sd: " Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 18/54] scsi-disk: release AioContext in unaligned WRITE SAME case Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 19/54] hw/pci-bridge: fix QEMU crash because of pcie-root-port Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 20/54] i386: Change X86CPUDefinition::model_id to const char* Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 21/54] i386: Add support for SPEC_CTRL MSR Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 22/54] i386: Add spec-ctrl CPUID bit Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 23/54] i386: Add FEAT_8000_0008_EBX CPUID feature word Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 24/54] i386: Add new -IBRS versions of Intel CPU models Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 25/54] i386: Add EPYC-IBPB CPU model Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 26/54] linux-user: Fix locking order in fork_start() Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 27/54] s390x: fix storage attributes migration for non-small guests Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 28/54] linux-headers: update to 4.15-rc1 Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 29/54] linux-headers: update Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 30/54] s390x/kvm: Handle bpb feature Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 31/54] s390x/kvm: provide stfle.81 Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 32/54] osdep: Retry SETLK upon EINTR Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 33/54] usb-storage: Fix share-rw option parsing Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 34/54] spapr_pci: fix MSI/MSIX selection Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 35/54] linux-user/signal.c: Rename MC_* defines Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 36/54] spapr: don't initialize PATB entry if max-cpu-compat < power9 Michael Roth
2018-02-06 19:14 ` [Qemu-devel] [PATCH 37/54] spapr: Add pseries-2.12 machine type Michael Roth
2018-02-06 19:14 ` Michael Roth [this message]
2018-02-06 19:15 ` [Qemu-devel] [PATCH 39/54] spapr: Treat Hardware Transactional Memory (HTM) as an optional capability Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 40/54] spapr: Validate capabilities on migration Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 41/54] target/ppc: Clean up probing of VMX, VSX and DFP availability on KVM Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 42/54] spapr: Handle VMX/VSX presence as an spapr capability flag Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 43/54] spapr: Handle Decimal Floating Point (DFP) as an optional capability Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 44/54] hw/ppc/spapr_caps: Rework spapr_caps to use uint8 internal representation Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 45/54] ppc: Change Power9 compat table to support at most 8 threads/core Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 46/54] spapr: fix device tree properties when using compatibility mode Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 47/54] target/ppc: introduce the PPC_BIT() macro Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 48/54] target/ppc/spapr_caps: Add macro to generate spapr_caps migration vmstate Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 49/54] target/ppc/kvm: Add cap_ppc_safe_[cache/bounds_check/indirect_branch] Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 50/54] target/ppc/spapr_caps: Add support for tristate spapr_capabilities Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 51/54] target/ppc/spapr_caps: Add new tristate cap safe_cache Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 52/54] target/ppc/spapr_caps: Add new tristate cap safe_bounds_check Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 53/54] target/ppc/spapr_caps: Add new tristate cap safe_indirect_branch Michael Roth
2018-02-06 19:15 ` [Qemu-devel] [PATCH 54/54] target/ppc/spapr: Add H-Call H_GET_CPU_CHARACTERISTICS Michael Roth
2018-02-07  6:47 ` [Qemu-devel] [PATCH 00/54] Patch Round-up for stable 2.11.1, freeze on 2018-02-12 Thomas Huth
2018-02-07 10:28   ` Daniel P. Berrangé
2018-02-07  9:28 ` Cornelia Huck
2018-02-07  9:42 ` [Qemu-devel] [Qemu-stable] " Greg Kurz
2018-02-08 12:51 ` Peter Lieven
2018-02-12 16:13   ` Dr. David Alan Gilbert
2018-02-13 16:30     ` Greg Kurz
2018-02-16  9:46     ` Peter Lieven
2018-02-08 13:26 ` [Qemu-devel] " Philippe Mathieu-Daudé
2018-02-13  1:44 ` [Qemu-devel] [Qemu-stable] " Michael Roth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180206191515.25830-39-mdroth@linux.vnet.ibm.com \
    --to=mdroth@linux.vnet.ibm.com \
    --cc=christian.ehrhardt@canonical.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).