From nobody Thu Nov 6 06:17:08 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=oracle.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 15393885925841016.8388795065695; Fri, 12 Oct 2018 16:56:32 -0700 (PDT) Received: from localhost ([::1]:43112 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gB7I8-0004zk-KJ for importer@patchew.org; Fri, 12 Oct 2018 19:56:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60831) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gB7Ax-0008C6-PJ for qemu-devel@nongnu.org; Fri, 12 Oct 2018 19:49:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gB7Aq-0006VR-Me for qemu-devel@nongnu.org; Fri, 12 Oct 2018 19:48:57 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:47410) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gB7Aq-0006T4-9v for qemu-devel@nongnu.org; Fri, 12 Oct 2018 19:48:52 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w9CNiIP7088170 for ; Fri, 12 Oct 2018 23:48:49 GMT Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp2120.oracle.com with ESMTP id 2mxnprndsw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 12 Oct 2018 23:48:49 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w9CNmmhI009600 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 12 Oct 2018 23:48:48 GMT Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w9CNmmXl012056 for ; Fri, 12 Oct 2018 23:48:48 GMT Received: from jaraman-bur-1.us.oracle.com (/10.152.33.39) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 12 Oct 2018 23:48:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : in-reply-to : references; s=corp-2018-07-02; bh=tXSgvtaZJ1GRbY7tE+doyJWWSqi4lBdKY2sWTPLwh2g=; b=s5SroICF0Z+ck5f2wS6KVzK0oid+JG2T0BIusH2g0G3mAfmPJXKngoqruxFTh64uW76e eD6dXMQITkI4pxWfJcvWtRA5JGyo7V8J//5znc5GFT4JeCLlCtxVUjzsYqt1wl4d8bE+ iTvbLd9rQna4Vlgerf5g2Z8f3+wlgLpH3ofPWf0jwCdZ1OPDtGafOvY1CM8dU11pxKqY /TBuUJ5INLJ9zN8lIqv2D0S+MwymRyAJjENGqneOutA8SL7Mia5jjJoXYJTsZsSkrRtZ GeGaGh0VjCrt77mhonLnyRo+J9MJLL0WSf/V8gIwxiaPOTRH426TEU7SL6TVt4gjYehP Tg== From: Jagannathan Raman To: qemu-devel@nongnu.org Date: Fri, 12 Oct 2018 19:48:35 -0400 Message-Id: X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9044 signatures=668706 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=4 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1810120235 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 156.151.31.85 Subject: [Qemu-devel] [RFC PATCH v1 8/8] multi-process QEMU: synchronize RAM between QEMU & remote device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: elena.ufimtseva@oracle.com, john.g.johnson@oracle.com, jag.raman@oracle.com, Kanth.Ghatraju@oracle.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" - Setup MemoryListener in QEMU to get all updates to sysmem, and forward all RAM updates to remote device process - Remote device process updates its "system_memory" container using shared file descriptors provided by SYNC_SYSMEM message Signed-off-by: Jagannathan Raman --- hw/qemu-proxy.c | 156 ++++++++++++++++++++++++++++++++++++++++++++= ++++ hw/scsi/qemu-scsi-dev.c | 10 ++++ include/hw/qemu-proxy.h | 3 + 3 files changed, 169 insertions(+) diff --git a/hw/qemu-proxy.c b/hw/qemu-proxy.c index 1712b41..79cdd3f 100644 --- a/hw/qemu-proxy.c +++ b/hw/qemu-proxy.c @@ -159,6 +159,8 @@ static void pci_proxy_dev_realize(PCIDevice *device, Er= ror **errp) printf("Proxy link is not set\n"); } =20 + configure_memory_listener(dev); + pci_conf[PCI_LATENCY_TIMER] =3D 0xff; =20 memory_region_init_io(&dev->mmio_io, OBJECT(dev), &proxy_device_mmio_o= ps, @@ -213,3 +215,157 @@ void init_emulation_process(PCIProxyDev *pdev, char *= command, Error **errp) } =20 type_init(pci_proxy_dev_register_types) + +static void proxy_ml_begin(MemoryListener *listener) +{ + int mrs; + struct proxy_device *pdev =3D container_of(listener, struct proxy_devi= ce, + memory_listener); + + for (mrs =3D 0; mrs < pdev->n_mr_sections; mrs++) { + memory_region_unref(pdev->mr_sections[mrs].mr); + } + + g_free(pdev->mr_sections); + pdev->mr_sections =3D NULL; + pdev->n_mr_sections =3D 0; +} + +static bool proxy_mrs_can_merge(uint64_t host, uint64_t prev_host, size_t = size) +{ + bool merge; + ram_addr_t offset; + int fd1, fd2; + MemoryRegion *mr; + + mr =3D memory_region_from_host((void *)(uintptr_t)host, &offset); + fd1 =3D memory_region_get_fd(mr); + + mr =3D memory_region_from_host((void *)(uintptr_t)prev_host, &offset); + fd2 =3D memory_region_get_fd(mr); + + merge =3D (fd1 =3D=3D fd2); + + merge &=3D ((prev_host + size) =3D=3D host); + + return merge; +} + +static void proxy_ml_region_addnop(MemoryListener *listener, + MemoryRegionSection *section) +{ + bool need_add =3D true; + uint64_t mrs_size, mrs_gpa, mrs_page; + uintptr_t mrs_host; + RAMBlock *mrs_rb; + MemoryRegionSection *prev_sec; + struct proxy_device *pdev =3D container_of(listener, struct proxy_devi= ce, + memory_listener); + + if (!(memory_region_is_ram(section->mr) && + !memory_region_is_rom(section->mr))) { + return; + } + + mrs_rb =3D section->mr->ram_block; + mrs_page =3D (uint64_t)qemu_ram_pagesize(mrs_rb); + mrs_size =3D int128_get64(section->size); + mrs_gpa =3D section->offset_within_address_space; + mrs_host =3D (uintptr_t)memory_region_get_ram_ptr(section->mr) + + section->offset_within_region; + + mrs_host =3D mrs_host & ~(mrs_page - 1); + mrs_gpa =3D mrs_gpa & ~(mrs_page - 1); + mrs_size =3D ROUND_UP(mrs_size, mrs_page); + + if (pdev->n_mr_sections) { + prev_sec =3D pdev->mr_sections + (pdev->n_mr_sections - 1); + uint64_t prev_gpa_start =3D prev_sec->offset_within_address_space; + uint64_t prev_size =3D int128_get64(prev_sec->size); + uint64_t prev_gpa_end =3D range_get_last(prev_gpa_start, prev_si= ze); + uint64_t prev_host_start =3D + (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr) + + prev_sec->offset_within_region; + uint64_t prev_host_end =3D range_get_last(prev_host_start, prev_si= ze); + + if (mrs_gpa <=3D (prev_gpa_end + 1)) { + if (mrs_gpa < prev_gpa_start) { + assert(0); + } + + if ((section->mr =3D=3D prev_sec->mr) && + proxy_mrs_can_merge(mrs_host, prev_host_start, + (mrs_gpa - prev_gpa_start))) { + uint64_t max_end =3D MAX(prev_host_end, mrs_host + mrs_siz= e); + need_add =3D false; + prev_sec->offset_within_address_space =3D + MIN(prev_gpa_start, mrs_gpa); + prev_sec->offset_within_region =3D + MIN(prev_host_start, mrs_host) - + (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr); + prev_sec->size =3D int128_make64(max_end - MIN(prev_host_s= tart, + mrs_host)); + } + } + } + + if (need_add) { + ++pdev->n_mr_sections; + pdev->mr_sections =3D g_renew(MemoryRegionSection, pdev->mr_sectio= ns, + pdev->n_mr_sections); + pdev->mr_sections[pdev->n_mr_sections - 1] =3D *section; + pdev->mr_sections[pdev->n_mr_sections - 1].fv =3D NULL; + memory_region_ref(section->mr); + } +} + +static void proxy_ml_commit(MemoryListener *listener) +{ + ProcMsg msg; + ram_addr_t offset; + MemoryRegion *mr; + MemoryRegionSection section; + uintptr_t host_addr; + int region; + struct proxy_device *pdev =3D container_of(listener, struct proxy_devi= ce, + memory_listener); + + msg.cmd =3D SYNC_SYSMEM; + msg.bytestream =3D 0; + msg.num_fds =3D pdev->n_mr_sections; + assert(msg.num_fds <=3D MAX_FDS); + + for (region =3D 0; region < pdev->n_mr_sections; region++) { + section =3D pdev->mr_sections[region]; + msg.data1.sync_sysmem.gpas[region] =3D + section.offset_within_address_space; + msg.data1.sync_sysmem.sizes[region] =3D int128_get64(section.size); + host_addr =3D (uintptr_t)memory_region_get_ram_ptr(section.mr) + + section.offset_within_region; + mr =3D memory_region_from_host((void *)host_addr, &offset); + msg.fds[region] =3D memory_region_get_fd(mr); + } + proxy_proc_send(pdev->proxy_link, &msg); +} + +void deconfigure_memory_listener(PCIProxyDev *pdev) +{ + memory_listener_unregister(&pdev->proxy_dev.memory_listener); +} + +static MemoryListener proxy_listener =3D { + .begin =3D proxy_ml_begin, + .commit =3D proxy_ml_commit, + .region_add =3D proxy_ml_region_addnop, + .region_nop =3D proxy_ml_region_addnop, + .priority =3D 10, +}; + +void configure_memory_listener(PCIProxyDev *dev) +{ + dev->proxy_dev.memory_listener =3D proxy_listener; + dev->proxy_dev.n_mr_sections =3D 0; + dev->proxy_dev.mr_sections =3D NULL; + memory_listener_register(&dev->proxy_dev.memory_listener, + &address_space_memory); +} diff --git a/hw/scsi/qemu-scsi-dev.c b/hw/scsi/qemu-scsi-dev.c index ede0c4b..ff34cbc 100644 --- a/hw/scsi/qemu-scsi-dev.c +++ b/hw/scsi/qemu-scsi-dev.c @@ -65,6 +65,16 @@ static void process_msg(GIOCondition cond) break; case CONF_READ: break; + case SYNC_SYSMEM: + /* + * TODO: ensure no active DMA is happening when + * sysmem is being updated + */ + remote_sysmem_reconfig(msg, &err); + if (err) { + goto finalize_loop; + } + break; default: error_setg(&err, "Unknown command"); goto finalize_loop; diff --git a/include/hw/qemu-proxy.h b/include/hw/qemu-proxy.h index 0903626..867c3ce 100644 --- a/include/hw/qemu-proxy.h +++ b/include/hw/qemu-proxy.h @@ -33,6 +33,7 @@ typedef struct proxy_device { int n_mr_sections; MemoryRegionSection *mr_sections; ProxyLinkState *proxy_link; + MemoryListener memory_listener; int link_up; } proxy_device; =20 @@ -52,5 +53,7 @@ typedef struct PCIProxyDev { void init_emulation_process(PCIProxyDev *pdev, char *command, Error **errp= ); int config_op_send(PCIProxyDev *dev, uint32_t addr, uint32_t val, int l, unsigned int op); +void deconfigure_memory_listener(PCIProxyDev *pdev); +void configure_memory_listener(PCIProxyDev *pdev); =20 #endif --=20 1.8.3.1