From nobody Sun Apr 13 18:20:15 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1612546160; cv=none; d=zohomail.com; s=zohoarc; b=TTZ1foW1fb/47Y4vuu0bZ61py3rlTatRn6vCYTKI3Ymncb5cw0f3JVoGOtNs/uMcedPThKqoclgD4DgpHU4To5jPFwIjdsHPg6tBPzZYk9PF+3a6HnTk25aY+sTdyjF+ycd6JgV61zHAIgznfnEr26nHfyhDJu4tF+TwffS4cG8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612546160; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3HDQhqZ5SSxWL3Nt8c0FBuiTbxlmJ4xLR+JoGhuim/U=; b=ZZx6NHWorKUp5nQAogyyt0yEHeKzT5lsZjCIsZecIfyhCIZ0RthIGadR4o6WfPzu6IfQyOh2yuG6mhKaRKn2SiAlqRLFsVQRjltOr5KMH8v2ZAIVg8Q6h7oetulgZmYS8ypXR+cY+wE6EEfkiFDrykFhP2mIXz2bvxsR5RY464c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1612546160643587.8303629731876; Fri, 5 Feb 2021 09:29:20 -0800 (PST) Received: from localhost ([::1]:41892 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l84v1-0002kS-Gc for importer@patchew.org; Fri, 05 Feb 2021 12:29:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:43268) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l84IO-00044E-1u for qemu-devel@nongnu.org; Fri, 05 Feb 2021 11:49:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:41242) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l84IM-0007U0-02 for qemu-devel@nongnu.org; Fri, 05 Feb 2021 11:49:23 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-235-aY2nuOYdP26MCu4zlHMzkw-1; Fri, 05 Feb 2021 11:49:19 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 893FB804059; Fri, 5 Feb 2021 16:49:17 +0000 (UTC) Received: from localhost (ovpn-112-54.ams2.redhat.com [10.36.112.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5744960C77; Fri, 5 Feb 2021 16:49:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612543761; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3HDQhqZ5SSxWL3Nt8c0FBuiTbxlmJ4xLR+JoGhuim/U=; b=Lb3OpODDTOLl/FOEGk+g0rV1/siutLhHQIZu4dsQOb9if9hcjuPfm0oS2TNCFciJzjHypn uBm4r/QtAYeQOyT/VRDX7bUzfByOOnSbUxHBBhLaWpIUDDK/vhDEwe3gXvXpmDyW4mp/PW 3TkXns97rzTody5HVmSMUMKiCzBRoPI= X-MC-Unique: aY2nuOYdP26MCu4zlHMzkw-1 From: Stefan Hajnoczi To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL v3 22/27] multi-process: PCI BAR read/write handling for proxy & remote endpoints Date: Fri, 5 Feb 2021 16:44:54 +0000 Message-Id: <20210205164459.432714-23-stefanha@redhat.com> In-Reply-To: <20210205164459.432714-1-stefanha@redhat.com> References: <20210205164459.432714-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.352, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , John G Johnson , Thomas Huth , Jagannathan Raman , Stefan Hajnoczi , qemu-block@nongnu.org, "Michael S. Tsirkin" , "Denis V. Lunev" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Elena Ufimtseva , Wainer dos Santos Moschetta , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Jagannathan Raman Proxy device object implements handler for PCI BAR writes and reads. The handler uses BAR_WRITE/BAR_READ message to communicate to the remote process with the BAR address and value to be written/read. The remote process implements handler for BAR_WRITE/BAR_READ message. Signed-off-by: Jagannathan Raman Signed-off-by: Elena Ufimtseva Signed-off-by: John G Johnson Reviewed-by: Stefan Hajnoczi Message-id: a8b76714a9688be5552c4c92d089bc9e8a4707ff.1611938319.git.jag.ram= an@oracle.com Signed-off-by: Stefan Hajnoczi --- include/hw/remote/mpqemu-link.h | 10 ++++ include/hw/remote/proxy.h | 9 ++++ hw/remote/message.c | 83 +++++++++++++++++++++++++++++++++ hw/remote/mpqemu-link.c | 6 +++ hw/remote/proxy.c | 60 ++++++++++++++++++++++++ 5 files changed, 168 insertions(+) diff --git a/include/hw/remote/mpqemu-link.h b/include/hw/remote/mpqemu-lin= k.h index 7bc0bddb5a..6303e62b17 100644 --- a/include/hw/remote/mpqemu-link.h +++ b/include/hw/remote/mpqemu-link.h @@ -37,6 +37,8 @@ typedef enum { MPQEMU_CMD_RET, MPQEMU_CMD_PCI_CFGWRITE, MPQEMU_CMD_PCI_CFGREAD, + MPQEMU_CMD_BAR_WRITE, + MPQEMU_CMD_BAR_READ, MPQEMU_CMD_MAX, } MPQemuCmd; =20 @@ -52,6 +54,13 @@ typedef struct { int len; } PciConfDataMsg; =20 +typedef struct { + hwaddr addr; + uint64_t val; + unsigned size; + bool memory; +} BarAccessMsg; + /** * MPQemuMsg: * @cmd: The remote command @@ -71,6 +80,7 @@ typedef struct { uint64_t u64; PciConfDataMsg pci_conf_data; SyncSysmemMsg sync_sysmem; + BarAccessMsg bar_access; } data; =20 int fds[REMOTE_MAX_FDS]; diff --git a/include/hw/remote/proxy.h b/include/hw/remote/proxy.h index faa9c4d580..ea7fa4fb3c 100644 --- a/include/hw/remote/proxy.h +++ b/include/hw/remote/proxy.h @@ -15,6 +15,14 @@ #define TYPE_PCI_PROXY_DEV "x-pci-proxy-dev" OBJECT_DECLARE_SIMPLE_TYPE(PCIProxyDev, PCI_PROXY_DEV) =20 +typedef struct ProxyMemoryRegion { + PCIProxyDev *dev; + MemoryRegion mr; + bool memory; + bool present; + uint8_t type; +} ProxyMemoryRegion; + struct PCIProxyDev { PCIDevice parent_dev; char *fd; @@ -28,6 +36,7 @@ struct PCIProxyDev { QemuMutex io_mutex; QIOChannel *ioc; Error *migration_blocker; + ProxyMemoryRegion region[PCI_NUM_REGIONS]; }; =20 #endif /* PROXY_H */ diff --git a/hw/remote/message.c b/hw/remote/message.c index 636bd161bd..f2e84457e0 100644 --- a/hw/remote/message.c +++ b/hw/remote/message.c @@ -16,11 +16,14 @@ #include "qapi/error.h" #include "sysemu/runstate.h" #include "hw/pci/pci.h" +#include "exec/memattrs.h" =20 static void process_config_write(QIOChannel *ioc, PCIDevice *dev, MPQemuMsg *msg, Error **errp); static void process_config_read(QIOChannel *ioc, PCIDevice *dev, MPQemuMsg *msg, Error **errp); +static void process_bar_write(QIOChannel *ioc, MPQemuMsg *msg, Error **err= p); +static void process_bar_read(QIOChannel *ioc, MPQemuMsg *msg, Error **errp= ); =20 void coroutine_fn mpqemu_remote_msg_loop_co(void *data) { @@ -52,6 +55,12 @@ void coroutine_fn mpqemu_remote_msg_loop_co(void *data) case MPQEMU_CMD_PCI_CFGREAD: process_config_read(com->ioc, pci_dev, &msg, &local_err); break; + case MPQEMU_CMD_BAR_WRITE: + process_bar_write(com->ioc, &msg, &local_err); + break; + case MPQEMU_CMD_BAR_READ: + process_bar_read(com->ioc, &msg, &local_err); + break; default: error_setg(&local_err, "Unknown command (%d) received for device %s" @@ -115,3 +124,77 @@ static void process_config_read(QIOChannel *ioc, PCIDe= vice *dev, getpid()); } } + +static void process_bar_write(QIOChannel *ioc, MPQemuMsg *msg, Error **err= p) +{ + ERRP_GUARD(); + BarAccessMsg *bar_access =3D &msg->data.bar_access; + AddressSpace *as =3D + bar_access->memory ? &address_space_memory : &address_space_io; + MPQemuMsg ret =3D { 0 }; + MemTxResult res; + uint64_t val; + + if (!is_power_of_2(bar_access->size) || + (bar_access->size > sizeof(uint64_t))) { + ret.data.u64 =3D UINT64_MAX; + goto fail; + } + + val =3D cpu_to_le64(bar_access->val); + + res =3D address_space_rw(as, bar_access->addr, MEMTXATTRS_UNSPECIFIED, + (void *)&val, bar_access->size, true); + + if (res !=3D MEMTX_OK) { + error_setg(errp, "Bad address %"PRIx64" for mem write, pid "FMT_pi= d".", + bar_access->addr, getpid()); + ret.data.u64 =3D -1; + } + +fail: + ret.cmd =3D MPQEMU_CMD_RET; + ret.size =3D sizeof(ret.data.u64); + + if (!mpqemu_msg_send(&ret, ioc, NULL)) { + error_prepend(errp, "Error returning code to proxy, pid "FMT_pid":= ", + getpid()); + } +} + +static void process_bar_read(QIOChannel *ioc, MPQemuMsg *msg, Error **errp) +{ + ERRP_GUARD(); + BarAccessMsg *bar_access =3D &msg->data.bar_access; + MPQemuMsg ret =3D { 0 }; + AddressSpace *as; + MemTxResult res; + uint64_t val =3D 0; + + as =3D bar_access->memory ? &address_space_memory : &address_space_io; + + if (!is_power_of_2(bar_access->size) || + (bar_access->size > sizeof(uint64_t))) { + val =3D UINT64_MAX; + goto fail; + } + + res =3D address_space_rw(as, bar_access->addr, MEMTXATTRS_UNSPECIFIED, + (void *)&val, bar_access->size, false); + + if (res !=3D MEMTX_OK) { + error_setg(errp, "Bad address %"PRIx64" for mem read, pid "FMT_pid= ".", + bar_access->addr, getpid()); + val =3D UINT64_MAX; + } + +fail: + ret.cmd =3D MPQEMU_CMD_RET; + ret.data.u64 =3D le64_to_cpu(val); + ret.size =3D sizeof(ret.data.u64); + + if (!mpqemu_msg_send(&ret, ioc, NULL)) { + error_prepend(errp, "Error returning code to proxy, pid "FMT_pid":= ", + getpid()); + } +} diff --git a/hw/remote/mpqemu-link.c b/hw/remote/mpqemu-link.c index b45f325686..52bfeddcdc 100644 --- a/hw/remote/mpqemu-link.c +++ b/hw/remote/mpqemu-link.c @@ -248,6 +248,12 @@ bool mpqemu_msg_valid(MPQemuMsg *msg) return false; } break; + case MPQEMU_CMD_BAR_WRITE: + case MPQEMU_CMD_BAR_READ: + if ((msg->size !=3D sizeof(BarAccessMsg)) || (msg->num_fds !=3D 0)= ) { + return false; + } + break; default: break; } diff --git a/hw/remote/proxy.c b/hw/remote/proxy.c index 2b1439432b..22eb422231 100644 --- a/hw/remote/proxy.c +++ b/hw/remote/proxy.c @@ -152,3 +152,63 @@ static void pci_proxy_dev_register_types(void) } =20 type_init(pci_proxy_dev_register_types) + +static void send_bar_access_msg(PCIProxyDev *pdev, MemoryRegion *mr, + bool write, hwaddr addr, uint64_t *val, + unsigned size, bool memory) +{ + MPQemuMsg msg =3D { 0 }; + long ret =3D -EINVAL; + Error *local_err =3D NULL; + + msg.size =3D sizeof(BarAccessMsg); + msg.data.bar_access.addr =3D mr->addr + addr; + msg.data.bar_access.size =3D size; + msg.data.bar_access.memory =3D memory; + + if (write) { + msg.cmd =3D MPQEMU_CMD_BAR_WRITE; + msg.data.bar_access.val =3D *val; + } else { + msg.cmd =3D MPQEMU_CMD_BAR_READ; + } + + ret =3D mpqemu_msg_send_and_await_reply(&msg, pdev, &local_err); + if (local_err) { + error_report_err(local_err); + } + + if (!write) { + *val =3D ret; + } +} + +static void proxy_bar_write(void *opaque, hwaddr addr, uint64_t val, + unsigned size) +{ + ProxyMemoryRegion *pmr =3D opaque; + + send_bar_access_msg(pmr->dev, &pmr->mr, true, addr, &val, size, + pmr->memory); +} + +static uint64_t proxy_bar_read(void *opaque, hwaddr addr, unsigned size) +{ + ProxyMemoryRegion *pmr =3D opaque; + uint64_t val; + + send_bar_access_msg(pmr->dev, &pmr->mr, false, addr, &val, size, + pmr->memory); + + return val; +} + +const MemoryRegionOps proxy_mr_ops =3D { + .read =3D proxy_bar_read, + .write =3D proxy_bar_write, + .endianness =3D DEVICE_NATIVE_ENDIAN, + .impl =3D { + .min_access_size =3D 1, + .max_access_size =3D 8, + }, +}; --=20 2.29.2