qemu-e2k/hw/ppc/spapr_tpm_proxy.c

178 lines
4.7 KiB
C
Raw Normal View History

/*
* SPAPR TPM Proxy/Hypercall
*
* Copyright IBM Corp. 2019
*
* Authors:
* Michael Roth <mdroth@linux.vnet.ibm.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "sysemu/reset.h"
#include "hw/ppc/spapr.h"
#include "hw/qdev-properties.h"
#include "trace.h"
#define TPM_SPAPR_BUFSIZE 4096
enum {
TPM_COMM_OP_EXECUTE = 1,
TPM_COMM_OP_CLOSE_SESSION = 2,
};
static void spapr_tpm_proxy_reset(void *opaque)
{
SpaprTpmProxy *tpm_proxy = SPAPR_TPM_PROXY(opaque);
if (tpm_proxy->host_fd != -1) {
close(tpm_proxy->host_fd);
tpm_proxy->host_fd = -1;
}
}
static ssize_t tpm_execute(SpaprTpmProxy *tpm_proxy, target_ulong *args)
{
uint64_t data_in = ppc64_phys_to_real(args[1]);
target_ulong data_in_size = args[2];
uint64_t data_out = ppc64_phys_to_real(args[3]);
target_ulong data_out_size = args[4];
uint8_t buf_in[TPM_SPAPR_BUFSIZE];
uint8_t buf_out[TPM_SPAPR_BUFSIZE];
ssize_t ret;
trace_spapr_tpm_execute(data_in, data_in_size, data_out, data_out_size);
if (data_in_size > TPM_SPAPR_BUFSIZE) {
error_report("invalid TPM input buffer size: " TARGET_FMT_lu,
data_in_size);
return H_P3;
}
if (data_out_size < TPM_SPAPR_BUFSIZE) {
error_report("invalid TPM output buffer size: " TARGET_FMT_lu,
data_out_size);
return H_P5;
}
if (tpm_proxy->host_fd == -1) {
tpm_proxy->host_fd = open(tpm_proxy->host_path, O_RDWR);
if (tpm_proxy->host_fd == -1) {
error_report("failed to open TPM device %s: %d",
tpm_proxy->host_path, errno);
return H_RESOURCE;
}
}
cpu_physical_memory_read(data_in, buf_in, data_in_size);
do {
ret = write(tpm_proxy->host_fd, buf_in, data_in_size);
if (ret > 0) {
data_in_size -= ret;
}
} while ((ret >= 0 && data_in_size > 0) || (ret == -1 && errno == EINTR));
if (ret == -1) {
error_report("failed to write to TPM device %s: %d",
tpm_proxy->host_path, errno);
return H_RESOURCE;
}
do {
ret = read(tpm_proxy->host_fd, buf_out, data_out_size);
} while (ret == 0 || (ret == -1 && errno == EINTR));
if (ret == -1) {
error_report("failed to read from TPM device %s: %d",
tpm_proxy->host_path, errno);
return H_RESOURCE;
}
cpu_physical_memory_write(data_out, buf_out, ret);
args[0] = ret;
return H_SUCCESS;
}
static target_ulong h_tpm_comm(PowerPCCPU *cpu,
SpaprMachineState *spapr,
target_ulong opcode,
target_ulong *args)
{
target_ulong op = args[0];
SpaprTpmProxy *tpm_proxy = spapr->tpm_proxy;
if (!tpm_proxy) {
error_report("TPM proxy not available");
return H_FUNCTION;
}
trace_spapr_h_tpm_comm(tpm_proxy->host_path, op);
switch (op) {
case TPM_COMM_OP_EXECUTE:
return tpm_execute(tpm_proxy, args);
case TPM_COMM_OP_CLOSE_SESSION:
spapr_tpm_proxy_reset(tpm_proxy);
return H_SUCCESS;
default:
return H_PARAMETER;
}
}
static void spapr_tpm_proxy_realize(DeviceState *d, Error **errp)
{
SpaprTpmProxy *tpm_proxy = SPAPR_TPM_PROXY(d);
if (tpm_proxy->host_path == NULL) {
error_setg(errp, "must specify 'host-path' option for device");
return;
}
tpm_proxy->host_fd = -1;
qemu_register_reset(spapr_tpm_proxy_reset, tpm_proxy);
}
qdev: Unrealize must not fail Devices may have component devices and buses. Device realization may fail. Realization is recursive: a device's realize() method realizes its components, and device_set_realized() realizes its buses (which should in turn realize the devices on that bus, except bus_set_realized() doesn't implement that, yet). When realization of a component or bus fails, we need to roll back: unrealize everything we realized so far. If any of these unrealizes failed, the device would be left in an inconsistent state. Must not happen. device_set_realized() lets it happen: it ignores errors in the roll back code starting at label child_realize_fail. Since realization is recursive, unrealization must be recursive, too. But how could a partly failed unrealize be rolled back? We'd have to re-realize, which can fail. This design is fundamentally broken. device_set_realized() does not roll back at all. Instead, it keeps unrealizing, ignoring further errors. It can screw up even for a device with no buses: if the lone dc->unrealize() fails, it still unregisters vmstate, and calls listeners' unrealize() callback. bus_set_realized() does not roll back either. Instead, it stops unrealizing. Fortunately, no unrealize method can fail, as we'll see below. To fix the design error, drop parameter @errp from all the unrealize methods. Any unrealize method that uses @errp now needs an update. This leads us to unrealize() methods that can fail. Merely passing it to another unrealize method cannot cause failure, though. Here are the ones that do other things with @errp: * virtio_serial_device_unrealize() Fails when qbus_set_hotplug_handler() fails, but still does all the other work. On failure, the device would stay realized with its resources completely gone. Oops. Can't happen, because qbus_set_hotplug_handler() can't actually fail here. Pass &error_abort to qbus_set_hotplug_handler() instead. * hw/ppc/spapr_drc.c's unrealize() Fails when object_property_del() fails, but all the other work is already done. On failure, the device would stay realized with its vmstate registration gone. Oops. Can't happen, because object_property_del() can't actually fail here. Pass &error_abort to object_property_del() instead. * spapr_phb_unrealize() Fails and bails out when remove_drcs() fails, but other work is already done. On failure, the device would stay realized with some of its resources gone. Oops. remove_drcs() fails only when chassis_from_bus()'s object_property_get_uint() fails, and it can't here. Pass &error_abort to remove_drcs() instead. Therefore, no unrealize method can fail before this patch. device_set_realized()'s recursive unrealization via bus uses object_property_set_bool(). Can't drop @errp there, so pass &error_abort. We similarly unrealize with object_property_set_bool() elsewhere, always ignoring errors. Pass &error_abort instead. Several unrealize methods no longer handle errors from other unrealize methods: virtio_9p_device_unrealize(), virtio_input_device_unrealize(), scsi_qdev_unrealize(), ... Much of the deleted error handling looks wrong anyway. One unrealize methods no longer ignore such errors: usb_ehci_pci_exit(). Several realize methods no longer ignore errors when rolling back: v9fs_device_realize_common(), pci_qdev_unrealize(), spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(), virtio_device_realize(). Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 17:29:24 +02:00
static void spapr_tpm_proxy_unrealize(DeviceState *d)
{
SpaprTpmProxy *tpm_proxy = SPAPR_TPM_PROXY(d);
qemu_unregister_reset(spapr_tpm_proxy_reset, tpm_proxy);
}
static Property spapr_tpm_proxy_properties[] = {
DEFINE_PROP_STRING("host-path", SpaprTpmProxy, host_path),
DEFINE_PROP_END_OF_LIST(),
};
static void spapr_tpm_proxy_class_init(ObjectClass *k, void *data)
{
DeviceClass *dk = DEVICE_CLASS(k);
dk->realize = spapr_tpm_proxy_realize;
dk->unrealize = spapr_tpm_proxy_unrealize;
dk->user_creatable = true;
device_class_set_props(dk, spapr_tpm_proxy_properties);
}
static const TypeInfo spapr_tpm_proxy_info = {
.name = TYPE_SPAPR_TPM_PROXY,
.parent = TYPE_DEVICE,
.instance_size = sizeof(SpaprTpmProxy),
.class_init = spapr_tpm_proxy_class_init,
};
static void spapr_tpm_proxy_register_types(void)
{
type_register_static(&spapr_tpm_proxy_info);
spapr_register_hypercall(SVM_H_TPM_COMM, h_tpm_comm);
}
type_init(spapr_tpm_proxy_register_types)