From nobody Tue Apr 30 14:13:36 2024 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.zoho.com; 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; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492695148263187.7450994317419; Thu, 20 Apr 2017 06:32:28 -0700 (PDT) Received: from localhost ([::1]:53944 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1CCA-0006Hj-Un for importer@patchew.org; Thu, 20 Apr 2017 09:32:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36817) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1CAx-0005n9-Ky for qemu-devel@nongnu.org; Thu, 20 Apr 2017 09:31:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1CAt-0002ff-Dk for qemu-devel@nongnu.org; Thu, 20 Apr 2017 09:31:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55240) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d1CAt-0002fF-0I for qemu-devel@nongnu.org; Thu, 20 Apr 2017 09:31:07 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B077264DA0 for ; Thu, 20 Apr 2017 13:31:05 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-9.ams2.redhat.com [10.36.117.9]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v3KDUxo3028463; Thu, 20 Apr 2017 09:31:02 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B077264DA0 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=pbonzini@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com B077264DA0 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 20 Apr 2017 15:30:58 +0200 Message-Id: <20170420133058.12911-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 20 Apr 2017 13:31:05 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for 2.10 v3] hmp: gpa2hva and gpa2hpa hostaddr command 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: dgilbert@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" These commands are useful when testing machine-check passthrough. gpa2hva is useful to inject a MADV_HWPOISON madvise from gdb, while gpa2hpa is useful to inject an error with the mce-inject kernel module. Signed-off-by: Paolo Bonzini Message-Id: <1490021158-4469-1-git-send-email-pbonzini@redhat.com> Signed-off-by: Paolo Bonzini Reviewed-by: Dr. David Alan Gilbert --- hmp-commands.hx | 32 ++++++++++++++++++ monitor.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 133 insertions(+) diff --git a/hmp-commands.hx b/hmp-commands.hx index 8819281..0aca984 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -526,6 +526,38 @@ Dump 80 16 bit values at the start of the video memory. ETEXI =20 { + .name =3D "gpa2hva", + .args_type =3D "addr:l", + .params =3D "addr", + .help =3D "print the host virtual address corresponding to a= guest physical address", + .cmd =3D hmp_gpa2hva, + }, + +STEXI +@item gpa2hva @var{addr} +@findex gpa2hva +Print the host virtual address at which the guest's physical address @var{= addr} +is mapped. +ETEXI + +#ifdef CONFIG_LINUX + { + .name =3D "gpa2hpa", + .args_type =3D "addr:l", + .params =3D "addr", + .help =3D "print the host physical address corresponding to = a guest physical address", + .cmd =3D hmp_gpa2hpa, + }, +#endif + +STEXI +@item gpa2hpa @var{addr} +@findex gpa2hpa +Print the host physical address at which the guest's physical address @var= {addr} +is mapped. +ETEXI + + { .name =3D "p|print", .args_type =3D "fmt:/,val:l", .params =3D "/fmt expr", diff --git a/monitor.c b/monitor.c index 079eeb8..4e9d041 100644 --- a/monitor.c +++ b/monitor.c @@ -1421,6 +1421,107 @@ static void hmp_physical_memory_dump(Monitor *mon, = const QDict *qdict) memory_dump(mon, count, format, size, addr, 1); } =20 +static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp) +{ + MemoryRegionSection mrs =3D memory_region_find(get_system_memory(), + addr, 1); + + if (!mrs.mr) { + error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx,= addr); + return NULL; + } + + if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) { + error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM",= addr); + memory_region_unref(mrs.mr); + return NULL; + } + + *p_mr =3D mrs.mr; + return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region); +} + +static void hmp_gpa2hva(Monitor *mon, const QDict *qdict) +{ + hwaddr addr =3D qdict_get_int(qdict, "addr"); + Error *local_err =3D NULL; + MemoryRegion *mr =3D NULL; + void *ptr; + + ptr =3D gpa2hva(&mr, addr, &local_err); + if (local_err) { + error_report_err(local_err); + return; + } + + monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx + " (%s) is %p\n", + addr, mr->name, ptr); + + memory_region_unref(mr); +} + +#ifdef CONFIG_LINUX +static uint64_t vtop(void *ptr, Error **errp) +{ + uint64_t pinfo; + uint64_t ret =3D -1; + uintptr_t addr =3D (uintptr_t) ptr; + uintptr_t pagesize =3D getpagesize(); + off_t offset =3D addr / pagesize * sizeof(pinfo); + int fd; + + fd =3D open("/proc/self/pagemap", O_RDONLY); + if (fd =3D=3D -1) { + error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap"); + return -1; + } + + /* Force copy-on-write if necessary. */ + atomic_add((uint8_t *)ptr, 0); + + if (pread(fd, &pinfo, sizeof(pinfo), offset) !=3D sizeof(pinfo)) { + error_setg_errno(errp, errno, "Cannot read pagemap"); + goto out; + } + if ((pinfo & (1ull << 63)) =3D=3D 0) { + error_setg(errp, "Page not present"); + goto out; + } + ret =3D ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesi= ze - 1)); + +out: + close(fd); + return ret; +} + +static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict) +{ + hwaddr addr =3D qdict_get_int(qdict, "addr"); + Error *local_err =3D NULL; + MemoryRegion *mr =3D NULL; + void *ptr; + uint64_t physaddr; + + ptr =3D gpa2hva(&mr, addr, &local_err); + if (local_err) { + error_report_err(local_err); + return; + } + + physaddr =3D vtop(ptr, &local_err); + if (local_err) { + error_report_err(local_err); + } else { + monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx + " (%s) is 0x%" PRIx64 "\n", + addr, mr->name, (uint64_t) physaddr); + } + + memory_region_unref(mr); +} +#endif + static void do_print(Monitor *mon, const QDict *qdict) { int format =3D qdict_get_int(qdict, "format"); --=20 2.9.3