From nobody Tue Feb 10 01:35:40 2026 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; 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=bitdefender.com ARC-Seal: i=1; a=rsa-sha256; t=1586913146; cv=none; d=zohomail.com; s=zohoarc; b=aBuyTuCsnE5QRvkhq3vztkg4OpGfGezs8tDQS/DnOPkqPvOG3zm4QRJxd06fKYU31nGCzkeLANYcBeTOBw3Ctwg1Uc5vkxlJc1S612rqWrzbWN/k1QDYSpalDxzeauAk7jlNkAUBiDWjuUiz4P03qXuQvZb9joAYCF2h8Sbqnvo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586913146; 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=RGqzCSBgZZjGkyhbIK/AUx22WGBR3/MW7ivADe7VHpE=; b=DHEIEfZtwRhdaHSY74OHmz4GX8K/3HbZ1zEcBJTIb+6g7hs1+5rU2P8ZMNmnIL00vUqq7ACWnMYjcU3LEAAmsBCYVUYAcZQ9THEGlIffkwhrxcRSGxBX9IP/4ejeafudx0qE48YnFm03RBYv1AN1fyue7xZN+5gryGg8XBLdbsc= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 1586913146668387.14072295704966; Tue, 14 Apr 2020 18:12:26 -0700 (PDT) Received: from localhost ([::1]:40990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jOWbJ-00078e-AZ for importer@patchew.org; Tue, 14 Apr 2020 21:12:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58535) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jOWPB-0001Oc-P3 for qemu-devel@nongnu.org; Tue, 14 Apr 2020 20:59:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jOWP9-0005D1-A2 for qemu-devel@nongnu.org; Tue, 14 Apr 2020 20:59:53 -0400 Received: from mx01.bbu.dsd.mx.bitdefender.com ([91.199.104.161]:49116) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jOWP8-00052n-QN for qemu-devel@nongnu.org; Tue, 14 Apr 2020 20:59:51 -0400 Received: from smtp.bitdefender.com (smtp02.buh.bitdefender.net [10.17.80.76]) by mx01.bbu.dsd.mx.bitdefender.com (Postfix) with ESMTPS id 7960A3074895 for ; Wed, 15 Apr 2020 03:59:36 +0300 (EEST) Received: from localhost.localdomain (unknown [91.199.104.27]) by smtp.bitdefender.com (Postfix) with ESMTPSA id 67278305B7A4; Wed, 15 Apr 2020 03:59:36 +0300 (EEST) From: =?UTF-8?q?Adalbert=20Laz=C4=83r?= To: qemu-devel@nongnu.org Subject: [RFC PATCH v1 25/26] kvm: vmi: extend handshake to include the e820 table Date: Wed, 15 Apr 2020 03:59:37 +0300 Message-Id: <20200415005938.23895-26-alazar@bitdefender.com> In-Reply-To: <20200415005938.23895-1-alazar@bitdefender.com> References: <20200415005938.23895-1-alazar@bitdefender.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 91.199.104.161 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: =?UTF-8?q?Adalbert=20Laz=C4=83r?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The introspection tool can use the e820 table to avoid accessing (read/write) or modifying access (rwx) for reserved memory pages. Signed-off-by: Adalbert Laz=C4=83r --- accel/kvm/vmi.c | 68 ++++++++++++++++++++++++++++++---- include/sysemu/vmi-handshake.h | 23 +++++++++++- 2 files changed, 82 insertions(+), 9 deletions(-) diff --git a/accel/kvm/vmi.c b/accel/kvm/vmi.c index 02877eec06..f70d78848a 100644 --- a/accel/kvm/vmi.c +++ b/accel/kvm/vmi.c @@ -26,6 +26,7 @@ #include "migration/misc.h" #include "qapi/qmp/qobject.h" #include "monitor/monitor.h" +#include "hw/i386/e820_memory_layout.h" =20 #include "sysemu/vmi-intercept.h" #include "sysemu/vmi-handshake.h" @@ -412,23 +413,74 @@ static void register_types(void) =20 type_init(register_types); =20 +static uint8_t handshake_cpu_type(void) +{ +#ifdef TARGET_X86_64 + return QEMU_VMI_CPU_TYPE_X86_64; +#elif TARGET_I386 + return QEMU_VMI_CPU_TYPE_I386; +#else + return QEMU_VMI_CPU_TYPE_UNKNOWN; +#endif +} + +static int cmp_address(const void *a, const void *b) +{ + uint64_t addr_a =3D ((qemu_vmi_e820_entry *)a)->address; + uint64_t addr_b =3D ((qemu_vmi_e820_entry *)b)->address; + + return (addr_a > addr_b) - (addr_a < addr_b); +} + +static void fill_e820_info(qemu_vmi_e820_entry *dest, int n) +{ + int idx; + + for (idx =3D 0; idx < n; idx++) + e820_get_entry2(idx, &dest[idx].type, &dest[idx].address, + &dest[idx].length); + + qsort(dest, n, sizeof(*dest), cmp_address); +} + static bool send_handshake_info(VMIntrospection *i, Error **errp) { - qemu_vmi_to_introspector send =3D {}; + qemu_vmi_to_introspector *send; + int max_n_e820, n_e820; const char *vm_name; + size_t send_sz; int r; =20 - send.struct_size =3D sizeof(send); - send.start_time =3D i->vm_start_time; - memcpy(&send.uuid, &qemu_uuid, sizeof(send.uuid)); + max_n_e820 =3D 8 * sizeof(((qemu_vmi_to_introspector *)0)->arch.e820_c= ount); + n_e820 =3D e820_get_num_entries(); + + if (n_e820 < 0 || n_e820 > max_n_e820) { + warn_report("VMI: discard e820 info (size %d, max %d)", + n_e820, max_n_e820); + n_e820 =3D 0; + } + + send_sz =3D sizeof(*send) + n_e820 * sizeof(qemu_vmi_e820_entry); + + send =3D g_malloc0(send_sz); + + send->struct_size =3D send_sz; + send->start_time =3D i->vm_start_time; + send->cpu_type =3D handshake_cpu_type(); + memcpy(&send->uuid, &qemu_uuid, sizeof(send->uuid)); vm_name =3D qemu_get_vm_name(); if (vm_name) { - snprintf(send.name, sizeof(send.name), "%s", vm_name); - send.name[sizeof(send.name) - 1] =3D 0; + snprintf(send->name, sizeof(send->name), "%s", vm_name); + send->name[sizeof(send->name) - 1] =3D 0; + } + send->arch.e820_count =3D n_e820; + if (n_e820) { + fill_e820_info(send->arch.e820_entries, n_e820); } =20 - r =3D qemu_chr_fe_write_all(&i->sock, (uint8_t *)&send, sizeof(send)); - if (r !=3D sizeof(send)) { + r =3D qemu_chr_fe_write_all(&i->sock, (uint8_t *)send, send_sz); + g_free(send); + if (r !=3D send_sz) { error_setg_errno(errp, errno, "VMI: error writing to '%s'", i->chardevid); return false; diff --git a/include/sysemu/vmi-handshake.h b/include/sysemu/vmi-handshake.h index 19bdfb6740..3c5201d37b 100644 --- a/include/sysemu/vmi-handshake.h +++ b/include/sysemu/vmi-handshake.h @@ -9,6 +9,25 @@ enum { QEMU_VMI_NAME_SIZE =3D 64 }; enum { QEMU_VMI_COOKIE_HASH_SIZE =3D 20}; =20 +enum { + QEMU_VMI_CPU_TYPE_I386 =3D 0, + QEMU_VMI_CPU_TYPE_X86_64 =3D 1, + QEMU_VMI_CPU_TYPE_UNKNOWN =3D 255 +}; + +typedef struct qemu_vmi_e820_entry { + uint64_t address; + uint64_t length; + uint32_t type; + uint32_t padding; +} qemu_vmi_e820_entry; + +typedef struct qemu_vmi_to_introspector_x86 { + uint8_t e820_count; + uint8_t padding[3]; + qemu_vmi_e820_entry e820_entries[0]; +} qemu_vmi_to_introspector_x86; + /** * qemu_vmi_to_introspector: * @@ -22,9 +41,11 @@ enum { QEMU_VMI_COOKIE_HASH_SIZE =3D 20}; typedef struct qemu_vmi_to_introspector { uint32_t struct_size; uint8_t uuid[16]; - uint32_t padding; + uint8_t cpu_type; + uint8_t padding[3]; int64_t start_time; char name[QEMU_VMI_NAME_SIZE]; + qemu_vmi_to_introspector_x86 arch; /* ... */ } qemu_vmi_to_introspector; =20