From nobody Sat May 18 22:54:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass(p=quarantine dis=none) header.from=amazon.de ARC-Seal: i=1; a=rsa-sha256; t=1624616299; cv=none; d=zohomail.com; s=zohoarc; b=Ag4b+rV3xKl5FrBeL8rD0nKRLXwDqL1V2vLuJmgjHtCO8zPSGRkCwoW1SDOCdOYi31A6V+Jvbl4zo7HRl29Ohv5SmF5OVnlLVQZZpioVS1n3crRVArCsXziah1/rv4JaJL3fSNmRWCKQ8ah7hOuSNiJn21qjXlnwEpzHHVTylOk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624616299; h=Content-Type: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=mZIir676oMjXxkMnK5aSOC8G9jHVo4LGLAT1tTWIcP8=; b=APPQQ3UQk/Wd1MTJiX/IUW7NSW+PtY9qDyex113evHXEiiGQbRasd11iUh+W1xyTqPzU//dBS1V3w9XkoL6TIFDGlLgTiCMLciSkYtxgE83yw7CYlHVMJsJj4+7RBn4v3x+WmBUYgLGXjrjlsc+NjKCPHp6GHwRS21GU3y2WFZg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624616299571481.0767722759317; Fri, 25 Jun 2021 03:18:19 -0700 (PDT) Received: from localhost ([::1]:34472 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lwiug-0005CO-E6 for importer@patchew.org; Fri, 25 Jun 2021 06:18:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35908) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwis6-0007P9-Eq for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:15:38 -0400 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:35173) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwis2-0006mi-O6 for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:15:38 -0400 Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-2c-397e131e.us-west-2.amazon.com) ([10.25.36.210]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP; 25 Jun 2021 10:15:26 +0000 Received: from EX13D28EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-2c-397e131e.us-west-2.amazon.com (Postfix) with ESMTPS id EBAA4A0659; Fri, 25 Jun 2021 10:15:25 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.161.69) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Fri, 25 Jun 2021 10:15:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1624616134; x=1656152134; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=mZIir676oMjXxkMnK5aSOC8G9jHVo4LGLAT1tTWIcP8=; b=uarU6pAVkuXb38w1SzYSmeg1mWX+Dg+TtC8WR49qnQyxITPey0UYWY15 Gl0PPgrs2HIweXjnlppN1LFo6AZwFycyrCJEqhdYROIB1JR5knym3Zap9 RkmLpCvbY/3nRmXAD2FGCitCdcBylBb258gV97+5hfoGp7x79t9QGpdul Y=; X-IronPort-AV: E=Sophos;i="5.83,298,1616457600"; d="scan'208";a="132938746" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH v2 1/6] hyper-v: Overlay abstraction for synic event and msg pages Date: Fri, 25 Jun 2021 12:14:36 +0200 Message-ID: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.161.69] X-ClientProxiedBy: EX13D14UWC004.ant.amazon.com (10.43.162.99) To EX13D28EUC003.ant.amazon.com (10.43.164.43) Precedence: Bulk 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=207.171.190.10; envelope-from=prvs=8031d3708=sidcha@amazon.de; helo=smtp-fw-33001.amazon.com X-Spam_score_int: -47 X-Spam_score: -4.8 X-Spam_bar: ---- X-Spam_report: (-4.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.362, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=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 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @amazon.de) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Capture overlay page semantic variables into 'struct overlay_page' and add methods that operate over it. Adapt existing synic event and mesage pages to use these methods to setup and manage overlays. Since all overlay pages use bit 0 of the GPA to indicate if the overlay is enabled, the checks for this bit is moved into the unified overlaying method hyperv_overlay_update() so the caller does not need to care about it. Signed-off-by: Siddharth Chandrasekaran --- hw/hyperv/hyperv.c | 103 ++++++++++++++++++++----------------- include/hw/hyperv/hyperv.h | 9 ++++ target/i386/kvm/hyperv.c | 10 ++-- 3 files changed, 68 insertions(+), 54 deletions(-) diff --git a/hw/hyperv/hyperv.c b/hw/hyperv/hyperv.c index cb1074f234..8d09206702 100644 --- a/hw/hyperv/hyperv.c +++ b/hw/hyperv/hyperv.c @@ -28,12 +28,8 @@ struct SynICState { CPUState *cs; =20 bool enabled; - hwaddr msg_page_addr; - hwaddr event_page_addr; - MemoryRegion msg_page_mr; - MemoryRegion event_page_mr; - struct hyperv_message_page *msg_page; - struct hyperv_event_flags_page *event_page; + struct hyperv_overlay_page msg_page; + struct hyperv_overlay_page event_page; }; =20 #define TYPE_SYNIC "hyperv-synic" @@ -41,43 +37,52 @@ OBJECT_DECLARE_SIMPLE_TYPE(SynICState, SYNIC) =20 static bool synic_enabled; =20 -bool hyperv_is_synic_enabled(void) +static void alloc_overlay_page(struct hyperv_overlay_page *overlay, + Object *owner, const char *name) { - return synic_enabled; + memory_region_init_ram(&overlay->mr, owner, name, + qemu_real_host_page_size, &error_abort); + overlay->ram_ptr =3D memory_region_get_ram_ptr(&overlay->mr); + overlay->addr =3D 0; } =20 -static SynICState *get_synic(CPUState *cs) +/** + * This method must be called with iothread lock taken as it modifies + * the memory hierarchy. + */ +static void hyperv_overlay_update(struct hyperv_overlay_page *overlay, hwa= ddr addr) { - return SYNIC(object_resolve_path_component(OBJECT(cs), "synic")); + /* check if overlay page is enabled */ + addr =3D (addr & HYPERV_OVERLAY_ENABLED) ? (addr & TARGET_PAGE_MASK) := 0; + + if (overlay->addr !=3D addr) { + if (overlay->addr) { + memory_region_del_subregion(get_system_memory(), &overlay->mr); + } + if (addr) { + memory_region_add_subregion(get_system_memory(), addr, &overla= y->mr); + overlay->ram_ptr =3D memory_region_get_ram_ptr(&overlay->mr); + } + overlay->addr =3D addr; + } } =20 static void synic_update(SynICState *synic, bool enable, hwaddr msg_page_addr, hwaddr event_page_addr) { - synic->enabled =3D enable; - if (synic->msg_page_addr !=3D msg_page_addr) { - if (synic->msg_page_addr) { - memory_region_del_subregion(get_system_memory(), - &synic->msg_page_mr); - } - if (msg_page_addr) { - memory_region_add_subregion(get_system_memory(), msg_page_addr, - &synic->msg_page_mr); - } - synic->msg_page_addr =3D msg_page_addr; - } - if (synic->event_page_addr !=3D event_page_addr) { - if (synic->event_page_addr) { - memory_region_del_subregion(get_system_memory(), - &synic->event_page_mr); - } - if (event_page_addr) { - memory_region_add_subregion(get_system_memory(), event_page_ad= dr, - &synic->event_page_mr); - } - synic->event_page_addr =3D event_page_addr; - } + hyperv_overlay_update(&synic->msg_page, msg_page_addr); + hyperv_overlay_update(&synic->event_page, event_page_addr); +} + +bool hyperv_is_synic_enabled(void) +{ + return synic_enabled; +} + +static SynICState *get_synic(CPUState *cs) +{ + return SYNIC(object_resolve_path_component(OBJECT(cs), "synic")); } =20 void hyperv_synic_update(CPUState *cs, bool enable, @@ -104,21 +109,18 @@ static void synic_realize(DeviceState *dev, Error **e= rrp) msgp_name =3D g_strdup_printf("synic-%u-msg-page", vp_index); eventp_name =3D g_strdup_printf("synic-%u-event-page", vp_index); =20 - memory_region_init_ram(&synic->msg_page_mr, obj, msgp_name, - sizeof(*synic->msg_page), &error_abort); - memory_region_init_ram(&synic->event_page_mr, obj, eventp_name, - sizeof(*synic->event_page), &error_abort); - synic->msg_page =3D memory_region_get_ram_ptr(&synic->msg_page_mr); - synic->event_page =3D memory_region_get_ram_ptr(&synic->event_page_mr); + alloc_overlay_page(&synic->msg_page, obj, msgp_name); + alloc_overlay_page(&synic->event_page, obj, eventp_name); =20 g_free(msgp_name); g_free(eventp_name); } + static void synic_reset(DeviceState *dev) { SynICState *synic =3D SYNIC(dev); - memset(synic->msg_page, 0, sizeof(*synic->msg_page)); - memset(synic->event_page, 0, sizeof(*synic->event_page)); + memset(synic->msg_page.ram_ptr, 0, sizeof(struct hyperv_message_page)); + memset(synic->event_page.ram_ptr, 0, sizeof(struct hyperv_event_flags_= page)); synic_update(synic, false, 0, 0); } =20 @@ -254,17 +256,19 @@ static void cpu_post_msg(CPUState *cs, run_on_cpu_dat= a data) HvSintRoute *sint_route =3D data.host_ptr; HvSintStagedMessage *staged_msg =3D sint_route->staged_msg; SynICState *synic =3D sint_route->synic; + struct hyperv_message_page *msg_page; struct hyperv_message *dst_msg; bool wait_for_sint_ack =3D false; =20 assert(staged_msg->state =3D=3D HV_STAGED_MSG_BUSY); =20 - if (!synic->enabled || !synic->msg_page_addr) { + if (!synic->enabled || !synic->msg_page.addr) { staged_msg->status =3D -ENXIO; goto posted; } =20 - dst_msg =3D &synic->msg_page->slot[sint_route->sint]; + msg_page =3D synic->msg_page.ram_ptr; + dst_msg =3D &msg_page->slot[sint_route->sint]; =20 if (dst_msg->header.message_type !=3D HV_MESSAGE_NONE) { dst_msg->header.message_flags |=3D HV_MESSAGE_FLAG_PENDING; @@ -275,7 +279,8 @@ static void cpu_post_msg(CPUState *cs, run_on_cpu_data = data) staged_msg->status =3D hyperv_sint_route_set_sint(sint_route); } =20 - memory_region_set_dirty(&synic->msg_page_mr, 0, sizeof(*synic->msg_pag= e)); + memory_region_set_dirty(&synic->msg_page.mr, 0, + sizeof(struct hyperv_message_page)); =20 posted: qatomic_set(&staged_msg->state, HV_STAGED_MSG_POSTED); @@ -338,22 +343,24 @@ int hyperv_set_event_flag(HvSintRoute *sint_route, un= signed eventno) int ret; SynICState *synic =3D sint_route->synic; unsigned long *flags, set_mask; + struct hyperv_event_flags_page *event_page; unsigned set_idx; =20 if (eventno > HV_EVENT_FLAGS_COUNT) { return -EINVAL; } - if (!synic->enabled || !synic->event_page_addr) { + if (!synic->enabled || !synic->event_page.addr) { return -ENXIO; } =20 set_idx =3D BIT_WORD(eventno); set_mask =3D BIT_MASK(eventno); - flags =3D synic->event_page->slot[sint_route->sint].flags; + event_page =3D synic->event_page.ram_ptr; + flags =3D event_page->slot[sint_route->sint].flags; =20 if ((qatomic_fetch_or(&flags[set_idx], set_mask) & set_mask) !=3D set_= mask) { - memory_region_set_dirty(&synic->event_page_mr, 0, - sizeof(*synic->event_page)); + memory_region_set_dirty(&synic->event_page.mr, 0, + sizeof(struct hyperv_event_flags_page)); ret =3D hyperv_sint_route_set_sint(sint_route); } else { ret =3D 0; diff --git a/include/hw/hyperv/hyperv.h b/include/hw/hyperv/hyperv.h index a63ee0003c..3b2e0093b5 100644 --- a/include/hw/hyperv/hyperv.h +++ b/include/hw/hyperv/hyperv.h @@ -12,6 +12,15 @@ =20 #include "cpu-qom.h" #include "hw/hyperv/hyperv-proto.h" +#include "exec/memory.h" + +#define HYPERV_OVERLAY_ENABLED (1u << 0) + +struct hyperv_overlay_page { + hwaddr addr; + MemoryRegion mr; + void *ram_ptr; +}; =20 typedef struct HvSintRoute HvSintRoute; =20 diff --git a/target/i386/kvm/hyperv.c b/target/i386/kvm/hyperv.c index 26efc1e0e6..f49ed2621d 100644 --- a/target/i386/kvm/hyperv.c +++ b/target/i386/kvm/hyperv.c @@ -31,12 +31,10 @@ void hyperv_x86_synic_reset(X86CPU *cpu) void hyperv_x86_synic_update(X86CPU *cpu) { CPUX86State *env =3D &cpu->env; - bool enable =3D env->msr_hv_synic_control & HV_SYNIC_ENABLE; - hwaddr msg_page_addr =3D (env->msr_hv_synic_msg_page & HV_SIMP_ENABLE)= ? - (env->msr_hv_synic_msg_page & TARGET_PAGE_MASK) : 0; - hwaddr event_page_addr =3D (env->msr_hv_synic_evt_page & HV_SIEFP_ENAB= LE) ? - (env->msr_hv_synic_evt_page & TARGET_PAGE_MASK) : 0; - hyperv_synic_update(CPU(cpu), enable, msg_page_addr, event_page_addr); + + hyperv_synic_update(CPU(cpu), env->msr_hv_synic_control & HV_SYNIC_ENA= BLE, + env->msr_hv_synic_msg_page, + env->msr_hv_synic_evt_page); } =20 static void async_synic_update(CPUState *cs, run_on_cpu_data data) --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 From nobody Sat May 18 22:54:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass(p=quarantine dis=none) header.from=amazon.de ARC-Seal: i=1; a=rsa-sha256; t=1624616286; cv=none; d=zohomail.com; s=zohoarc; b=Puv/XLzQGN7GDSFj1+OK9k0UtPcEiAqEgeKpTXjjN+osva3TIOGpI2uBwUHoHLISeRD5ElASv0UtCbS3BSKcDwzlBBecNcNNl8z1WL4fTXvu+u0HM+gO41yUEyLKj+wjXodQ/Z2gvpxr2RfH3Z+naY1F2bht9Wq+6LrKyVFh1hE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624616286; h=Content-Type: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=/cT7Nw140FNR31PuXevCkr1pgU1HbzKiOZ6t4fkkRnQ=; b=RwK6uonVjJaRq6YN4bZHTEK4MhPpFTTtTbxMriOkaZ0cYQtCmq7oAZST0dED9Gwq7sKKcrORkpeHtwtcqvMSc/YOJt1pOdvZa2AFZs+2gnWUDO1b2kCxITklEDCZIccjjwgw0S7+wYB8/WuHB+0RYngzeJnWkN3uXX1gPQNkQC0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624616286806762.6431007217361; Fri, 25 Jun 2021 03:18:06 -0700 (PDT) Received: from localhost ([::1]:33664 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lwiuT-0004fH-O7 for importer@patchew.org; Fri, 25 Jun 2021 06:18:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35874) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwis2-0007Li-RC for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:15:34 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:6138) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwirz-0006kk-PY for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:15:34 -0400 Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-2a-c5104f52.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP; 25 Jun 2021 10:15:30 +0000 Received: from EX13D28EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-2a-c5104f52.us-west-2.amazon.com (Postfix) with ESMTPS id 060D2A0646; Fri, 25 Jun 2021 10:15:29 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.161.69) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Fri, 25 Jun 2021 10:15:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1624616131; x=1656152131; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=/cT7Nw140FNR31PuXevCkr1pgU1HbzKiOZ6t4fkkRnQ=; b=a1ta29zBEiverNoJPomkO0e6kb/4/3ftl6Qrdsk7c0rEbiCXOj/RAg4z YMQwF1t26t28LHfXAuZfI2tVZOGqU0a7+pPFfqTWXRHIh5II9C0itVMy7 yObACle8aNeiKyTYexK1iTNifOgc6vqOawncX1xjWcw4jTBrToJaMwNO5 s=; X-IronPort-AV: E=Sophos;i="5.83,298,1616457600"; d="scan'208";a="121286473" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH v2 2/6] hyper-v: Use -1 as invalid overlay address Date: Fri, 25 Jun 2021 12:14:37 +0200 Message-ID: <6dc5d59ae0077de3bb20918a553ee05cb768bfec.1624615713.git.sidcha@amazon.de> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.161.69] X-ClientProxiedBy: EX13D14UWC004.ant.amazon.com (10.43.162.99) To EX13D28EUC003.ant.amazon.com (10.43.164.43) Precedence: Bulk 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=52.95.49.90; envelope-from=prvs=8031d3708=sidcha@amazon.de; helo=smtp-fw-6002.amazon.com X-Spam_score_int: -47 X-Spam_score: -4.8 X-Spam_bar: ---- X-Spam_report: (-4.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.362, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @amazon.de) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When managing overlay pages, we used hwaddr 0 to signal an invalid address (to disable a page). Although unlikely, 0 _could_ be a valid overlay offset as Hyper-V TLFS does not specify anything about it. Use -1 as the invalid address indicator as it can never be a valid address. Signed-off-by: Siddharth Chandrasekaran --- hw/hyperv/hyperv.c | 15 +++++++++------ include/hw/hyperv/hyperv.h | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/hw/hyperv/hyperv.c b/hw/hyperv/hyperv.c index 8d09206702..ac45e8e139 100644 --- a/hw/hyperv/hyperv.c +++ b/hw/hyperv/hyperv.c @@ -43,7 +43,7 @@ static void alloc_overlay_page(struct hyperv_overlay_page= *overlay, memory_region_init_ram(&overlay->mr, owner, name, qemu_real_host_page_size, &error_abort); overlay->ram_ptr =3D memory_region_get_ram_ptr(&overlay->mr); - overlay->addr =3D 0; + overlay->addr =3D HYPERV_INVALID_OVERLAY_GPA; } =20 /** @@ -52,14 +52,17 @@ static void alloc_overlay_page(struct hyperv_overlay_pa= ge *overlay, */ static void hyperv_overlay_update(struct hyperv_overlay_page *overlay, hwa= ddr addr) { - /* check if overlay page is enabled */ - addr =3D (addr & HYPERV_OVERLAY_ENABLED) ? (addr & TARGET_PAGE_MASK) := 0; + if (addr !=3D HYPERV_INVALID_OVERLAY_GPA) { + /* check if overlay page is enabled */ + addr =3D (addr & HYPERV_OVERLAY_ENABLED) ? + (addr & TARGET_PAGE_MASK) : HYPERV_INVALID_OVERLAY_GPA; + } =20 if (overlay->addr !=3D addr) { - if (overlay->addr) { + if (overlay->addr !=3D HYPERV_INVALID_OVERLAY_GPA) { memory_region_del_subregion(get_system_memory(), &overlay->mr); } - if (addr) { + if (addr !=3D HYPERV_INVALID_OVERLAY_GPA) { memory_region_add_subregion(get_system_memory(), addr, &overla= y->mr); overlay->ram_ptr =3D memory_region_get_ram_ptr(&overlay->mr); } @@ -121,7 +124,7 @@ static void synic_reset(DeviceState *dev) SynICState *synic =3D SYNIC(dev); memset(synic->msg_page.ram_ptr, 0, sizeof(struct hyperv_message_page)); memset(synic->event_page.ram_ptr, 0, sizeof(struct hyperv_event_flags_= page)); - synic_update(synic, false, 0, 0); + synic_update(synic, false, HYPERV_INVALID_OVERLAY_GPA, HYPERV_INVALID_= OVERLAY_GPA); } =20 static void synic_class_init(ObjectClass *klass, void *data) diff --git a/include/hw/hyperv/hyperv.h b/include/hw/hyperv/hyperv.h index 3b2e0093b5..d989193e84 100644 --- a/include/hw/hyperv/hyperv.h +++ b/include/hw/hyperv/hyperv.h @@ -15,6 +15,7 @@ #include "exec/memory.h" =20 #define HYPERV_OVERLAY_ENABLED (1u << 0) +#define HYPERV_INVALID_OVERLAY_GPA ((hwaddr)-1) =20 struct hyperv_overlay_page { hwaddr addr; --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 From nobody Sat May 18 22:54:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass(p=quarantine dis=none) header.from=amazon.de ARC-Seal: i=1; a=rsa-sha256; t=1624616206; cv=none; d=zohomail.com; s=zohoarc; b=cSQR+AjEaSzJTi7fFs/TGK+hzo+IOTWOOnsjialiCyOm9i+RfUowy+P3FlCxAxa8+ju5kvANuO+gA7WojgKdeW0rxcAtMh427Ds/IQGw/Muv0SzXSyE92B83O+2PRO/2VsBljdR4t9ojvP0wiix4l8FN46ldVg2EEFgymO341gc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624616206; h=Content-Type: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=gojSS2OyTyAP5gmkFDKD8MsYdNtGHCgL8o38XcphUVg=; b=noIo3/ESryC4/93JF8CitfRefZPfXhF2d05XEJQcDWdwh2+vg0HeDGnoRw8ZHm7zswsGoVgf6F7k8Bq+z6agX7/U6GQBgfTtD0nfT8UtDZ7ujSjS+r1VEWtm5jOMcVEwTF9A7bYEZ8c46307q3HrHqfhzl0rtWbDuHtYxmR2Kj8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624616206222827.0435061310317; Fri, 25 Jun 2021 03:16:46 -0700 (PDT) Received: from localhost ([::1]:56632 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lwitB-00016M-2m for importer@patchew.org; Fri, 25 Jun 2021 06:16:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35922) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwis8-0007T6-F9 for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:15:40 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:6220) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwis4-0006os-Cm for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:15:40 -0400 Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-2a-c5104f52.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP; 25 Jun 2021 10:15:34 +0000 Received: from EX13D28EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-2a-c5104f52.us-west-2.amazon.com (Postfix) with ESMTPS id E6AF0A0646; Fri, 25 Jun 2021 10:15:33 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.161.69) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Fri, 25 Jun 2021 10:15:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1624616136; x=1656152136; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=gojSS2OyTyAP5gmkFDKD8MsYdNtGHCgL8o38XcphUVg=; b=jVhnDr9d/7e/3g4pSJZVluZeNnN7vXhU2nTzIXdhbs5gtglPAJLoRI4y TouHEdVsqB+3up6u4ugrG3gHrToIVZ1sD/Gos9sbOnPRk1YLiNhArC2bz WxvxKtng0avRdKztdqaXi88Oy22J8xEZPcJo7qYkBHN4+IBePzJMW/FT/ E=; X-IronPort-AV: E=Sophos;i="5.83,298,1616457600"; d="scan'208";a="121286488" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH v2 3/6] kvm/i386: Stop using cpu->kvm_msr_buf in kvm_put_one_msr() Date: Fri, 25 Jun 2021 12:14:38 +0200 Message-ID: <3d53e2f2c73c8ecc8adab0c50b23fa4f613a2f08.1624615713.git.sidcha@amazon.de> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.161.69] X-ClientProxiedBy: EX13D14UWC004.ant.amazon.com (10.43.162.99) To EX13D28EUC003.ant.amazon.com (10.43.164.43) Precedence: Bulk 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=52.95.49.90; envelope-from=prvs=8031d3708=sidcha@amazon.de; helo=smtp-fw-6002.amazon.com X-Spam_score_int: -47 X-Spam_score: -4.8 X-Spam_bar: ---- X-Spam_report: (-4.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.362, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @amazon.de) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" kvm_put_one_msr() zeros cpu->kvm_msr_buf and uses it to set one MSR to KVM. It is pretty wasteful as cpu->kvm_msr_buf is 4096 bytes long; instead use a local buffer to avoid memset. Also, expose this method from kvm_i386.h as hyperv.c needs to set MSRs in a subsequent patch. Signed-off-by: Siddharth Chandrasekaran --- target/i386/kvm/kvm.c | 12 ++++++++---- target/i386/kvm/kvm_i386.h | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index c676ee8b38..03202bd06b 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -2578,12 +2578,16 @@ static void kvm_msr_entry_add(X86CPU *cpu, uint32_t= index, uint64_t value) msrs->nmsrs++; } =20 -static int kvm_put_one_msr(X86CPU *cpu, int index, uint64_t value) +int kvm_put_one_msr(X86CPU *cpu, int index, uint64_t value) { - kvm_msr_buf_reset(cpu); - kvm_msr_entry_add(cpu, index, value); + uint8_t msr_buf[sizeof(struct kvm_msrs) + sizeof(struct kvm_msr_entry)= ] =3D { 0 }; + struct kvm_msrs *msr =3D (struct kvm_msrs *)msr_buf; + + msr->nmsrs =3D 1; + msr->entries[0].index =3D index; + msr->entries[0].data =3D value; =20 - return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, cpu->kvm_msr_buf); + return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, msr); } =20 void kvm_put_apicbase(X86CPU *cpu, uint64_t value) diff --git a/target/i386/kvm/kvm_i386.h b/target/i386/kvm/kvm_i386.h index dc72508389..0c4cd08071 100644 --- a/target/i386/kvm/kvm_i386.h +++ b/target/i386/kvm/kvm_i386.h @@ -40,6 +40,7 @@ void kvm_synchronize_all_tsc(void); void kvm_arch_reset_vcpu(X86CPU *cs); void kvm_arch_do_init_vcpu(X86CPU *cs); =20 +int kvm_put_one_msr(X86CPU *cpu, int index, uint64_t value); void kvm_put_apicbase(X86CPU *cpu, uint64_t value); =20 bool kvm_enable_x2apic(void); --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 From nobody Sat May 18 22:54:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass(p=quarantine dis=none) header.from=amazon.de ARC-Seal: i=1; a=rsa-sha256; t=1624616917; cv=none; d=zohomail.com; s=zohoarc; b=jy1yKUwh64lbvC3rCf4FjhwKMDYsgPspKFxTJDUyURNOITAhExk8JPe1aHvAWbR9Pal3kgPKuuSPhB9eBFrnAVjlJADz3IOGRT4II2BnE5tx5MYT0wf/n5KjCGuuFCExpF++85f9IYQyF5eKPjGT5qdEfqvNAehESkxILvY+IKE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624616917; h=Content-Type: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=emG7j8fZPGMdCYAjeHFKGkXfq8HitffOpSus8mjL/7E=; b=GMENY4yJ2xwdHRdgUOoiRl+Kg8kAm+059W1geyXd0ztDX+JdCYDgTgrAEpHfb04TP1syWdjTJQdJus7HDQqVJU8uOgS26XTr5vYh/8kB33DTZzkjRdOXX4iDLwGt5gcDr4p7Da9GpT/AGXAT7LwCJhfM+nshBuVEcaGhfhOv6vs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16246169176651006.4161187577891; Fri, 25 Jun 2021 03:28:37 -0700 (PDT) Received: from localhost ([::1]:42124 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lwj4e-0002jp-MM for importer@patchew.org; Fri, 25 Jun 2021 06:28:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38404) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwj0u-0007E1-V5 for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:24:44 -0400 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:62683) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwj0g-0005Jv-EF for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:24:44 -0400 Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-2b-859fe132.us-west-2.amazon.com) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP; 25 Jun 2021 10:24:21 +0000 Received: from EX13D28EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-2b-859fe132.us-west-2.amazon.com (Postfix) with ESMTPS id BA5A3220122; Fri, 25 Jun 2021 10:24:20 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.161.183) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Fri, 25 Jun 2021 10:24:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1624616670; x=1656152670; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=emG7j8fZPGMdCYAjeHFKGkXfq8HitffOpSus8mjL/7E=; b=s8Wc15TmtcY4WBrvK2PjiVQLourfga+VZ0w9oxa8m24ZciuS1Zaj4Mfc ed5LnjR+puThdmsBKXCL5XezZzG977uotsqUGPlzdt1VGrmSKs1DW4i7D zGk1ZntC9usALxZrHLK3r6eV/jRe2FO0CzmT/623yfQ1lDpSZQ9Hx/n/P Q=; X-IronPort-AV: E=Sophos;i="5.83,298,1616457600"; d="scan'208";a="142145158" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH v2 4/6] kvm/i386: Avoid multiple calls to check_extension(KVM_CAP_HYPERV) Date: Fri, 25 Jun 2021 12:23:29 +0200 Message-ID: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.161.183] X-ClientProxiedBy: EX13D05UWC004.ant.amazon.com (10.43.162.223) To EX13D28EUC003.ant.amazon.com (10.43.164.43) Precedence: Bulk 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=207.171.184.29; envelope-from=prvs=8031d3708=sidcha@amazon.de; helo=smtp-fw-9102.amazon.com X-Spam_score_int: -74 X-Spam_score: -7.5 X-Spam_bar: ------- X-Spam_report: (-7.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.362, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_HI=-5, 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 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @amazon.de) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" KVM_CAP_HYPERV is a VM ioctl and can be cached at kvm_arch_init() instead of performing an ioctl each time in hyperv_enabled() which is called foreach vCPU. Apart from that, this variable will come in handy in a subsequent patch. Signed-off-by: Siddharth Chandrasekaran --- target/i386/kvm/kvm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 03202bd06b..bcf1b4f2d0 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -93,6 +93,7 @@ static bool has_msr_misc_enable; static bool has_msr_smbase; static bool has_msr_bndcfgs; static int lm_capable_kernel; +static bool has_hyperv; static bool has_msr_hv_hypercall; static bool has_msr_hv_crash; static bool has_msr_hv_reset; @@ -716,7 +717,7 @@ unsigned long kvm_arch_vcpu_id(CPUState *cs) =20 static bool hyperv_enabled(X86CPU *cpu) { - return kvm_check_extension(kvm_state, KVM_CAP_HYPERV) > 0 && + return has_hyperv && ((cpu->hyperv_spinlock_attempts !=3D HYPERV_SPINLOCK_NEVER_NOTIFY)= || cpu->hyperv_features || cpu->hyperv_passthrough); } @@ -2216,6 +2217,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s) has_xsave =3D kvm_check_extension(s, KVM_CAP_XSAVE); has_xcrs =3D kvm_check_extension(s, KVM_CAP_XCRS); has_pit_state2 =3D kvm_check_extension(s, KVM_CAP_PIT_STATE2); + has_hyperv =3D kvm_check_extension(s, KVM_CAP_HYPERV); =20 hv_vpindex_settable =3D kvm_check_extension(s, KVM_CAP_HYPERV_VP_INDEX= ); =20 --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 From nobody Sat May 18 22:54:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass(p=quarantine dis=none) header.from=amazon.de ARC-Seal: i=1; a=rsa-sha256; t=1624616829; cv=none; d=zohomail.com; s=zohoarc; b=e0ptJa73xPuCGbGTZDPoX01cSaDIM5sihMgPdEddWEHe9SOLx/bMthd/gQ5oj+2Obf/xPHsESX1/eTGZxtt4FXHHWvK5I8z/eWaDi6x+apBLbLOThK4ItvQNw32RWWasOfWwpFd3rBZZeRpXwDMarkYHlY9NDbUPnK6OZBkKU2I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624616829; h=Content-Type: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=uRsQqgTK+aUKa+iPFFwN/xEXyo2o9xgMWDsa/xJb4bU=; b=ekDP9LMNVnErgI53/XErJ6YXPVgNr9h/g1Cr65CX8JBLAMd5+OidPv0NhI7uKHEJ4VHr162bHSrhTTJfUYhA0rLxIch65OqziAd+eXvt3TVE9R8znpjQOzDPI/dpauie2y90B59tucLd8UA3PffOkeQP1V5rH7jZM0sBW0i/2EQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624616829323583.5556560897165; Fri, 25 Jun 2021 03:27:09 -0700 (PDT) Received: from localhost ([::1]:38794 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lwj39-0000Nn-Gv for importer@patchew.org; Fri, 25 Jun 2021 06:27:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38398) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwj0u-0007Dz-Qm for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:24:44 -0400 Received: from smtp-fw-80006.amazon.com ([99.78.197.217]:59606) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwj0l-0005Me-0a for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:24:44 -0400 Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-2a-1c1b5cdd.us-west-2.amazon.com) ([10.25.36.214]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP; 25 Jun 2021 10:24:26 +0000 Received: from EX13D28EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-2a-1c1b5cdd.us-west-2.amazon.com (Postfix) with ESMTPS id D6C04A19CD; Fri, 25 Jun 2021 10:24:26 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.161.183) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Fri, 25 Jun 2021 10:24:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1624616674; x=1656152674; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=uRsQqgTK+aUKa+iPFFwN/xEXyo2o9xgMWDsa/xJb4bU=; b=P/DMc15/wOuJJjY5NC6Jrb1eVuHFV5QWVFaDEgdGAlJMrD//LSW3nInJ 65VehlPOyA4i+Zl4ghihHesHwI0FfANePZHGnj3a9ScN6VX0jjNMz9IKJ noK87rrHIKds3VnHDfL8orRR5db8cnryyS7nQ/ZyIUDFyYcp3O/MgSTws M=; X-IronPort-AV: E=Sophos;i="5.83,298,1616457600"; d="scan'208";a="8698652" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH v2 5/6] kvm/i386: Add support for user space MSR filtering Date: Fri, 25 Jun 2021 12:23:30 +0200 Message-ID: <678fba871629cc645cfe5b95292327cd8ae3e61f.1624615713.git.sidcha@amazon.de> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.161.183] X-ClientProxiedBy: EX13D05UWC004.ant.amazon.com (10.43.162.223) To EX13D28EUC003.ant.amazon.com (10.43.164.43) Precedence: Bulk 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=99.78.197.217; envelope-from=prvs=8031d3708=sidcha@amazon.de; helo=smtp-fw-80006.amazon.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.362, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H4=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 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @amazon.de) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Check and enable user space MSR filtering capability and handle new exit reason KVM_EXIT_X86_WRMSR. This will be used in a follow up patch to implement hyper-v overlay pages. Signed-off-by: Siddharth Chandrasekaran --- target/i386/kvm/kvm.c | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index bcf1b4f2d0..b89b343acc 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -117,6 +117,8 @@ static bool has_msr_ucode_rev; static bool has_msr_vmx_procbased_ctls2; static bool has_msr_perf_capabs; static bool has_msr_pkrs; +static bool has_msr_filtering; +static bool msr_filters_active; =20 static uint32_t has_architectural_pmu_version; static uint32_t num_architectural_pmu_gp_counters; @@ -2183,6 +2185,42 @@ static void register_smram_listener(Notifier *n, voi= d *unused) &smram_address_space, 1); } =20 +static void kvm_set_msr_filter_range(struct kvm_msr_filter_range *range, u= int32_t flags, + uint32_t base, uint32_t nmsrs, ...) +{ + int i, filter_to_userspace; + va_list ap; + + range->flags =3D flags; + range->nmsrs =3D nmsrs; + range->base =3D base; + + va_start(ap, nmsrs); + for (i =3D 0; i < nmsrs; i++) { + filter_to_userspace =3D va_arg(ap, int); + if (!filter_to_userspace) { + range->bitmap[i / 8] =3D 1 << (i % 8); + } + } + va_end(ap); +} + +static int kvm_set_msr_filters(KVMState *s) +{ + int r; + struct kvm_msr_filter filter =3D { }; + + filter.flags =3D KVM_MSR_FILTER_DEFAULT_ALLOW; + + r =3D kvm_vm_ioctl(s, KVM_X86_SET_MSR_FILTER, &filter); + if (r !=3D 0) { + error_report("kvm: failed to set MSR filters"); + return -1; + } + + return 0; +} + int kvm_arch_init(MachineState *ms, KVMState *s) { uint64_t identity_base =3D 0xfffbc000; @@ -2314,6 +2352,17 @@ int kvm_arch_init(MachineState *ms, KVMState *s) } } =20 + has_msr_filtering =3D kvm_check_extension(s, KVM_CAP_X86_USER_SPACE_MS= R) && + kvm_check_extension(s, KVM_CAP_X86_MSR_FILTER); + if (has_msr_filtering) { + ret =3D kvm_vm_enable_cap(s, KVM_CAP_X86_USER_SPACE_MSR, 0, + KVM_MSR_EXIT_REASON_FILTER); + if (ret =3D=3D 0) { + ret =3D kvm_set_msr_filters(s); + msr_filters_active =3D (ret =3D=3D 0); + } + } + return 0; } =20 @@ -4587,6 +4636,18 @@ static bool host_supports_vmx(void) return ecx & CPUID_EXT_VMX; } =20 +static int kvm_handle_rdmsr(X86CPU *cpu, struct kvm_run *run) +{ + run->msr.error =3D 1; + return 0; +} + +static int kvm_handle_wrmsr(X86CPU *cpu, struct kvm_run *run) +{ + run->msr.error =3D 1; + return 0; +} + #define VMX_INVALID_GUEST_STATE 0x80000021 =20 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) @@ -4645,6 +4706,12 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_ru= n *run) ioapic_eoi_broadcast(run->eoi.vector); ret =3D 0; break; + case KVM_EXIT_X86_RDMSR: + ret =3D kvm_handle_rdmsr(cpu, run); + break; + case KVM_EXIT_X86_WRMSR: + ret =3D kvm_handle_wrmsr(cpu, run); + break; default: fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason); ret =3D -1; --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 From nobody Sat May 18 22:54:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass(p=quarantine dis=none) header.from=amazon.de ARC-Seal: i=1; a=rsa-sha256; t=1624616829; cv=none; d=zohomail.com; s=zohoarc; b=QMKEzaLIKmhjFAtTCk0QHIb9SSyEYkXisF0RYpDJP6hE4eisy552zCyK0ad42XdQ6UfhBLSIrGC/e7lzqLeFJYOLtFj7nd4Sih9e6XFK01IhxrGLf8zMKTb+QN3h3hsmucfzIN/5xausr/7VbmVx5GFZrGEXpCVrZTT1KiIDysE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624616829; h=Content-Type: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=92xTIyz5g5W9oyuktdQVwvkkdrVToLiu7+iyaniqZKg=; b=PpI355zHbyc5f6IXwRnYaDWqzm3ZIV1klaYPB1Gxfw/Ij6WWKqC+eOu2B4k4LAcaLP5Tu6QbaeAfPQKpsO+o6RWxQl3XvxCfU6BwaCowUohJiL2GFKf7SQniuzQJJnT7MK9TS06bVXwSU5y/fRn9CqzleO6c3yr7P52TH7wYdGg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@amazon.de; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624616829323156.71587910529172; Fri, 25 Jun 2021 03:27:09 -0700 (PDT) Received: from localhost ([::1]:38948 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lwj3C-0000V8-16 for importer@patchew.org; Fri, 25 Jun 2021 06:27:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38456) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwj0x-0007Fb-98 for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:24:47 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:18770) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lwj0q-0005RO-0z for qemu-devel@nongnu.org; Fri, 25 Jun 2021 06:24:47 -0400 Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-2a-22cc717f.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP; 25 Jun 2021 10:24:38 +0000 Received: from EX13D28EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-2a-22cc717f.us-west-2.amazon.com (Postfix) with ESMTPS id 4AB96A1D20; Fri, 25 Jun 2021 10:24:37 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.161.183) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Fri, 25 Jun 2021 10:24:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1624616680; x=1656152680; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=92xTIyz5g5W9oyuktdQVwvkkdrVToLiu7+iyaniqZKg=; b=Axc5HTJvE77t/vyKZu+k2KCTBYbIkfDptM/KFlcCabmAi0gXs0EfwNJH qGcxcayO//xM3FDTdfP4HHhGbRjpCVTM8YP7TAThL1pm7nbZpmCePbVGz caXoVZ04VQl1BGQ4KskgxsjY8ru4g4P4ZCouqhNovAvfP9oWrbO79Ltbk 4=; X-IronPort-AV: E=Sophos;i="5.83,298,1616457600"; d="scan'208";a="121287971" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH v2 6/6] hyper-v: Handle hypercall code page as an overlay page Date: Fri, 25 Jun 2021 12:23:31 +0200 Message-ID: <451360b22145157b660802af9ff8beb67440b4cd.1624615713.git.sidcha@amazon.de> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.161.183] X-ClientProxiedBy: EX13D05UWC004.ant.amazon.com (10.43.162.223) To EX13D28EUC003.ant.amazon.com (10.43.164.43) Precedence: Bulk 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=52.95.49.90; envelope-from=prvs=8031d3708=sidcha@amazon.de; helo=smtp-fw-6002.amazon.com X-Spam_score_int: -47 X-Spam_score: -4.8 X-Spam_bar: ---- X-Spam_report: (-4.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.362, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @amazon.de) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hypercall code page is specified in the Hyper-V TLFS to be an overlay page, ie., guest chooses a GPA and the host _places_ a page at that location, making it visible to the guest and the existing page becomes inaccessible. Similarly when disabled, the host should _remove_ the overlay and the old page should become visible to the guest. Until now, KVM patched the hypercall code directly into the guest chosen GPA which is incorrect; instead, use the new user space MSR filtering feature to trap hypercall page MSR writes, overlay it as requested and then invoke a KVM_SET_MSR from user space to bounce back control KVM. This bounce back is needed as KVM may have to write data into the newly overlaid page. Signed-off-by: Siddharth Chandrasekaran --- hw/hyperv/hyperv.c | 10 ++++- include/hw/hyperv/hyperv.h | 5 +++ target/i386/kvm/hyperv.c | 91 ++++++++++++++++++++++++++++++++++++++ target/i386/kvm/hyperv.h | 4 ++ target/i386/kvm/kvm.c | 49 +++++++++++++++++++- 5 files changed, 156 insertions(+), 3 deletions(-) diff --git a/hw/hyperv/hyperv.c b/hw/hyperv/hyperv.c index ac45e8e139..aa5ac5226e 100644 --- a/hw/hyperv/hyperv.c +++ b/hw/hyperv/hyperv.c @@ -36,6 +36,7 @@ struct SynICState { OBJECT_DECLARE_SIMPLE_TYPE(SynICState, SYNIC) =20 static bool synic_enabled; +struct hyperv_overlay_page hcall_page; =20 static void alloc_overlay_page(struct hyperv_overlay_page *overlay, Object *owner, const char *name) @@ -50,7 +51,7 @@ static void alloc_overlay_page(struct hyperv_overlay_page= *overlay, * This method must be called with iothread lock taken as it modifies * the memory hierarchy. */ -static void hyperv_overlay_update(struct hyperv_overlay_page *overlay, hwa= ddr addr) +void hyperv_overlay_update(struct hyperv_overlay_page *overlay, hwaddr add= r) { if (addr !=3D HYPERV_INVALID_OVERLAY_GPA) { /* check if overlay page is enabled */ @@ -70,6 +71,13 @@ static void hyperv_overlay_update(struct hyperv_overlay_= page *overlay, hwaddr ad } } =20 +void hyperv_overlay_init(void) +{ + memory_region_init_ram(&hcall_page.mr, NULL, "hyperv.hcall_page", + qemu_real_host_page_size, &error_abort); + hcall_page.addr =3D HYPERV_INVALID_OVERLAY_GPA; +} + static void synic_update(SynICState *synic, bool enable, hwaddr msg_page_addr, hwaddr event_page_addr) { diff --git a/include/hw/hyperv/hyperv.h b/include/hw/hyperv/hyperv.h index d989193e84..f444431a81 100644 --- a/include/hw/hyperv/hyperv.h +++ b/include/hw/hyperv/hyperv.h @@ -85,6 +85,11 @@ static inline uint32_t hyperv_vp_index(CPUState *cs) return cs->cpu_index; } =20 +extern struct hyperv_overlay_page hcall_page; + +void hyperv_overlay_init(void); +void hyperv_overlay_update(struct hyperv_overlay_page *page, hwaddr addr); + void hyperv_synic_add(CPUState *cs); void hyperv_synic_reset(CPUState *cs); void hyperv_synic_update(CPUState *cs, bool enable, diff --git a/target/i386/kvm/hyperv.c b/target/i386/kvm/hyperv.c index f49ed2621d..d5eb47b8b0 100644 --- a/target/i386/kvm/hyperv.c +++ b/target/i386/kvm/hyperv.c @@ -16,6 +16,83 @@ #include "hyperv.h" #include "hw/hyperv/hyperv.h" #include "hyperv-proto.h" +#include "kvm_i386.h" + +struct x86_hv_overlay { + struct hyperv_overlay_page *page; + uint32_t msr; + hwaddr gpa; +}; + +static void async_overlay_update(CPUState *cs, run_on_cpu_data data) +{ + X86CPU *cpu =3D X86_CPU(cs); + struct x86_hv_overlay *overlay =3D data.host_ptr; + + qemu_mutex_lock_iothread(); + hyperv_overlay_update(overlay->page, overlay->gpa); + qemu_mutex_unlock_iothread(); + + /** + * Call KVM so it can keep a copy of the MSR data and do other post-ov= erlay + * actions such as filling the overlay page contents before returning = to + * guest. This works because MSR filtering is inactive for KVM_SET_MSRS + */ + kvm_put_one_msr(cpu, overlay->msr, overlay->gpa); + + g_free(overlay); +} + +static void do_overlay_update(X86CPU *cpu, struct hyperv_overlay_page *pag= e, + uint32_t msr, uint64_t data) +{ + struct x86_hv_overlay *overlay =3D g_malloc(sizeof(struct x86_hv_overl= ay)); + + *overlay =3D (struct x86_hv_overlay) { + .page =3D page, + .msr =3D msr, + .gpa =3D data + }; + + /** + * This will run in this cpu thread before it returns to KVM, but in a + * safe environment (i.e. when all cpus are quiescent) -- this is + * necessary because memory hierarchy is being changed + */ + async_safe_run_on_cpu(CPU(cpu), async_overlay_update, + RUN_ON_CPU_HOST_PTR(overlay)); +} + +static void overlay_update(X86CPU *cpu, uint32_t msr, uint64_t data) +{ + CPUX86State *env =3D &cpu->env; + + switch (msr) { + case HV_X64_MSR_GUEST_OS_ID: + /** + * When GUEST_OS_ID is cleared, hypercall overlay should be remove= d; + * otherwise it is a NOP. We still need to do a SET_MSR here as the + * kernel need to keep a copy of data. + */ + env->msr_hv_guest_os_id =3D data; + if (data !=3D 0) { + kvm_put_one_msr(cpu, msr, data); + return; + } + /* Fake a zero write to the overlay page hcall to invalidate the m= apping */ + do_overlay_update(cpu, &hcall_page, msr, 0); + break; + case HV_X64_MSR_HYPERCALL: + if (env->msr_hv_guest_os_id =3D=3D 0) { + /* This is a NOP in KVM too so we don't need to write back the= MSR */ + return; + } + do_overlay_update(cpu, &hcall_page, msr, data); + break; + default: + return; + } +} =20 int hyperv_x86_synic_add(X86CPU *cpu) { @@ -44,6 +121,20 @@ static void async_synic_update(CPUState *cs, run_on_cpu= _data data) qemu_mutex_unlock_iothread(); } =20 +int kvm_hv_handle_wrmsr(X86CPU *cpu, uint32_t msr, uint64_t data) +{ + switch (msr) { + case HV_X64_MSR_GUEST_OS_ID: + case HV_X64_MSR_HYPERCALL: + overlay_update(cpu, msr, data); + break; + default: + return 1; + } + + return 0; +} + int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit) { CPUX86State *env =3D &cpu->env; diff --git a/target/i386/kvm/hyperv.h b/target/i386/kvm/hyperv.h index 67543296c3..8e90fa949f 100644 --- a/target/i386/kvm/hyperv.h +++ b/target/i386/kvm/hyperv.h @@ -20,8 +20,12 @@ =20 #ifdef CONFIG_KVM int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit); +int kvm_hv_handle_wrmsr(X86CPU *cpu, uint32_t msr, uint64_t data); + #endif =20 +void hyperv_x86_hcall_page_update(X86CPU *cpu); + int hyperv_x86_synic_add(X86CPU *cpu); void hyperv_x86_synic_reset(X86CPU *cpu); void hyperv_x86_synic_update(X86CPU *cpu); diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index b89b343acc..c279c13437 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -2207,11 +2207,30 @@ static void kvm_set_msr_filter_range(struct kvm_msr= _filter_range *range, uint32_ =20 static int kvm_set_msr_filters(KVMState *s) { - int r; + int r, nmsrs, nfilt =3D 0, bitmap_pos =3D 0; struct kvm_msr_filter filter =3D { }; + struct kvm_msr_filter_range *range; + uint8_t bitmap_buf[KVM_MSR_FILTER_MAX_RANGES * 8] =3D {0}; =20 filter.flags =3D KVM_MSR_FILTER_DEFAULT_ALLOW; =20 + if (has_hyperv) { + /* + * Set filter on WRMSR to trap HV_X64_MSR_GUEST_OS_ID and + * HV_X64_MSR_HYPERCALL to userspace. These will be used to + * handle overlay requests. + */ + nmsrs =3D 2; + range =3D &filter.ranges[nfilt++]; + range->bitmap =3D &bitmap_buf[bitmap_pos]; + kvm_set_msr_filter_range(range, KVM_MSR_FILTER_WRITE, + HV_X64_MSR_GUEST_OS_ID, nmsrs, + true, /* HV_X64_MSR_GUEST_OS_ID */ + true /* HV_X64_MSR_HYPERCALL */); + bitmap_pos +=3D ROUND_UP(nmsrs, 8) / 8; + assert(bitmap_pos < sizeof(bitmap_buf)); + } + r =3D kvm_vm_ioctl(s, KVM_X86_SET_MSR_FILTER, &filter); if (r !=3D 0) { error_report("kvm: failed to set MSR filters"); @@ -2363,6 +2382,10 @@ int kvm_arch_init(MachineState *ms, KVMState *s) } } =20 + if (has_hyperv && msr_filters_active) { + hyperv_overlay_init(); + } + return 0; } =20 @@ -4644,7 +4667,29 @@ static int kvm_handle_rdmsr(X86CPU *cpu, struct kvm_= run *run) =20 static int kvm_handle_wrmsr(X86CPU *cpu, struct kvm_run *run) { - run->msr.error =3D 1; + int r =3D 1; + uint32_t msr; + uint64_t data; + + if (run->msr.reason !=3D KVM_MSR_EXIT_REASON_FILTER) { + error_report("MSR exit without exit reason FILTER"); + goto error; + } + + msr =3D run->msr.index; + data =3D run->msr.data; + + switch (msr) { + case HV_X64_MSR_GUEST_OS_ID: + case HV_X64_MSR_HYPERCALL: + r =3D kvm_hv_handle_wrmsr(cpu, msr, data); + break; + default: + error_report("Unknown MSR exit"); + } + +error: + run->msr.error =3D r; return 0; } =20 --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879