From nobody Sun May 19 02:06:36 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=1621886258; cv=none; d=zohomail.com; s=zohoarc; b=Z6InaTne1npY8BjIbHQpxTKfT5bp18CfDfEbCA8wYPXEPKndkL59up8sLP3xJ4lu+9vIyrizgPjiV1kjLnYx1IqLyttjeVuyITgMeq+VlFE6GZjWE5s63ubRt6+Gksm6eziY7r4F528yocFkYv7ExRNrwfHRYjrStanZCd0pk1U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621886258; 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=d+1tx/IxkvK2i87/M4fauRryprbzzxYJBlOqlqU7m4HUOu6uZYdSfbbekMCLUdA7VuBZRsMxPzYcKSmplHdyRB9QnmfarGFXPNDE+CV3oG4qyVpjgRBMLm4e/EvWF24DMfbff+sJMz7+Mtz80Rz5Gh7TzcOnpQ+jfsFFWObsKDg= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621886258991879.423285978565; Mon, 24 May 2021 12:57:38 -0700 (PDT) Received: from localhost ([::1]:57202 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llGhl-0005lf-Nn for importer@patchew.org; Mon, 24 May 2021 15:57:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58200) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGft-0003hm-T3 for qemu-devel@nongnu.org; Mon, 24 May 2021 15:55:41 -0400 Received: from smtp-fw-9103.amazon.com ([207.171.188.200]:17818) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGfp-0001ZA-Vw for qemu-devel@nongnu.org; Mon, 24 May 2021 15:55:41 -0400 Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-1e-27fb8269.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-9103.sea19.amazon.com with ESMTP; 24 May 2021 19:55:29 +0000 Received: from EX13D28EUC003.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-1e-27fb8269.us-east-1.amazon.com (Postfix) with ESMTPS id D0105A047E; Mon, 24 May 2021 19:55:27 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.161.253) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Mon, 24 May 2021 19:55:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1621886138; x=1653422138; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=mZIir676oMjXxkMnK5aSOC8G9jHVo4LGLAT1tTWIcP8=; b=fVI1e5Psh2PrqtXXNJmDQlzXFTYUD6jN6IiFEErtSy7rGn1APZBU1sh6 gFiFcdCKnfq0FelpPHppfKkqhKchhx1T9dDTvUC1mAbjnUmxqCJff9upo 4i9/FlhF0mQPKtqoz/Zw+vOrIDdaECQIoL/cd4S4xvEaB+vDVKBkPHZpC Y=; X-IronPort-AV: E=Sophos;i="5.82,325,1613433600"; d="scan'208";a="934795156" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH 1/6] hyper-v: Overlay abstraction for synic event and msg pages Date: Mon, 24 May 2021 21:54:04 +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.253] X-ClientProxiedBy: EX13D10UWB004.ant.amazon.com (10.43.161.121) 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.188.200; envelope-from=prvs=771b08666=sidcha@amazon.de; helo=smtp-fw-9103.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.371, 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" 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 Reviewed-by: Alexander Graf --- 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 Sun May 19 02:06:36 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=1621886358; cv=none; d=zohomail.com; s=zohoarc; b=grOGLgNy9g0CLOXmhDVgQLErJJVfXWarUxLKVOGyyZxuPqIlP8sCr75UoiusQmaZ6vxrgMCAa0DL7DDcjMs/8BmTCg/qQjIoFWJAWvlKkesMU1MYITEmnPqe4N0e7zpPusCg83z7TmYhmozUlwiMwKu/L2RQ7MGR8RuADeieArc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621886358; 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=cZSno09wl4A6j3k3psh4wIAM9Cgtf8APvFQogi8M/0U0RV661BubyMfetFfcGXgJhK+8nAsaAt4kodQX/C7S99niPQD33kL1jGWBxxchztQRc28BaTlS6lUIKnTs0UZ1bOWwUIUG+O0sTQVc3hdcUJfOWjzJ7ddKWX5tMhWMPtM= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 162188635862521.165737718706055; Mon, 24 May 2021 12:59:18 -0700 (PDT) Received: from localhost ([::1]:34402 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llGjN-00011V-L7 for importer@patchew.org; Mon, 24 May 2021 15:59:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58244) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGfz-0003pu-GQ for qemu-devel@nongnu.org; Mon, 24 May 2021 15:55:47 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:22670) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGfx-0001aL-2F for qemu-devel@nongnu.org; Mon, 24 May 2021 15:55:47 -0400 Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-1d-98acfc19.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP; 24 May 2021 19:55:35 +0000 Received: from EX13D28EUC003.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-1d-98acfc19.us-east-1.amazon.com (Postfix) with ESMTPS id 2B9EAA20BC; Mon, 24 May 2021 19:55:32 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.161.253) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Mon, 24 May 2021 19:55:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1621886146; x=1653422146; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=/cT7Nw140FNR31PuXevCkr1pgU1HbzKiOZ6t4fkkRnQ=; b=MLPxjH3m0FdpBU4/ycIs2TawVDcmCSZbO2bQzqhFcqlhVFr5q7X3BdzX ChwTKM4LQtj5nEBriaaSH191xxMKPrBgnbPWO0HXm1WhDFcgf7AzeVvKi qAJhE1zW97Xk192nAI9CuFJKFVdeOI4It1cfZAcbnnU4pymm8MxUJZiAE 0=; X-IronPort-AV: E=Sophos;i="5.82,325,1613433600"; d="scan'208";a="114312601" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH 2/6] hyper-v: Use -1 as invalid overlay address Date: Mon, 24 May 2021 21:54:05 +0200 Message-ID: <13aa6b6a4434198ad3d43e48501bce1796266850.1621885749.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.253] X-ClientProxiedBy: EX13D10UWB004.ant.amazon.com (10.43.161.121) 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=771b08666=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.371, 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 Reviewed-by: Alexander Graf --- 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 Sun May 19 02:06:36 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=1621886264; cv=none; d=zohomail.com; s=zohoarc; b=VKH23cKqMe0FxUPLHiCtGUbsQdvMxGjXF4xvIwnXjRnI8AUkVqYBYQpK1rbWiiPib6RifCdIT8rGfZ7ooCkxa0Cm3EeEzRyQBhcCwQbwkn/VeL+T+NIZGv/fwyP3ELRInZisOc/pQyQjsqBLosUhHzGmm8sbdRog0A8WdpDUvoM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621886264; 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=jtPJZRoIcIGMgdID0wVBNd10aPPe6ZAomW4bRSwxqzk=; b=iiVWdzX7IUcxMPoQt/U1Yg54e0rd8Q0euKDs093FtgaxdXHjQi1W8u+x9WsDPsfiBCEbGqL346CUckJfXYed2Ty7LNtZgxUKOF84N6AY5LVGwWdisUaYTCePPIBa0071LyxfPjX04D2i/dTPPk0uvuallzahxvob8PmL9Eb5t28= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621886264366840.5102143959316; Mon, 24 May 2021 12:57:44 -0700 (PDT) Received: from localhost ([::1]:57314 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llGhr-0005q4-Be for importer@patchew.org; Mon, 24 May 2021 15:57:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58228) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGfw-0003mt-Su for qemu-devel@nongnu.org; Mon, 24 May 2021 15:55:44 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:22670) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGft-0001aL-48 for qemu-devel@nongnu.org; Mon, 24 May 2021 15:55:44 -0400 Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-1d-98acfc19.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP; 24 May 2021 19:55:40 +0000 Received: from EX13D28EUC003.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-1d-98acfc19.us-east-1.amazon.com (Postfix) with ESMTPS id 292A2A2121; Mon, 24 May 2021 19:55:37 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.161.253) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Mon, 24 May 2021 19:55:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1621886142; x=1653422142; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=jtPJZRoIcIGMgdID0wVBNd10aPPe6ZAomW4bRSwxqzk=; b=YsSb7TjOBDQzrPM37EneoR98Wp+eYsOaNlEYyr3jvm9ZTNxlyedp5pGd gtoWGin93pY2iXA/oCXskP7Xmbf6u0HBVZ3Y6zad+LCt4CTZRNS1oObVj Ce2JloD9iFXXfFe1umd06mRHreBFVGezjJCoFbr76bgTM06daoMP4v2q6 Q=; X-IronPort-AV: E=Sophos;i="5.82,325,1613433600"; d="scan'208";a="114312619" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH 3/6] kvm/i386: Stop using cpu->kvm_msr_buf in kvm_put_one_msr() Date: Mon, 24 May 2021 21:54:06 +0200 Message-ID: <04c81a02c19a47e799e06b9c9ccb97e9a77f5927.1621885749.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.253] X-ClientProxiedBy: EX13D10UWB004.ant.amazon.com (10.43.161.121) 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=771b08666=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.371, 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 Reviewed-by: Alexander Graf --- 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 d972eb4705..d19a2913fd 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -2534,12 +2534,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 Sun May 19 02:06:36 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=1621886363; cv=none; d=zohomail.com; s=zohoarc; b=AMqSIDjyOsiZhQ+q26vSLt2wgUdeRF1A+cnAO4VRwr1UZ9Dys72Zq/BUB+0v78+MqiIE7DzX2K+WVYD9nACqU6igLmEx3HhTp0LTzGeA/ircNU5hKpLCdqMV8Ke3jClVF3e4jspbLYlbFJI+trnBeCnJbTszi2dPdBmFZCMhwZw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621886363; 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=5pzt10yIIjzwUF6BelVfnSNpnXyncgMpqkanGTHNZ9E=; b=EPlTj1jp4PGkRLMCABFvwz9VJAOhRPifF4+CchEk2iglys60ee8FPVr6GEz/9QFt/ziF86cowKwXhFXchrVVRTRT9NY9DWFIHxjeAJbpdRP9i6H6coldm6hKMC5b1Ho5kDYraRnOe51OICTVlXPo2faGZQDq+Xy3vzVCDoSLYIg= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621886363931535.2379799913151; Mon, 24 May 2021 12:59:23 -0700 (PDT) Received: from localhost ([::1]:34752 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llGjT-0001H4-01 for importer@patchew.org; Mon, 24 May 2021 15:59:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58292) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGgE-0004Rp-1V for qemu-devel@nongnu.org; Mon, 24 May 2021 15:56:03 -0400 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:44744) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGgA-0001kA-Qq for qemu-devel@nongnu.org; Mon, 24 May 2021 15:56:01 -0400 Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-1d-16425a8d.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP; 24 May 2021 19:55:49 +0000 Received: from EX13D28EUC003.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-1d-16425a8d.us-east-1.amazon.com (Postfix) with ESMTPS id 66646100B25; Mon, 24 May 2021 19:55:47 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.161.253) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Mon, 24 May 2021 19:55:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1621886159; x=1653422159; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=5pzt10yIIjzwUF6BelVfnSNpnXyncgMpqkanGTHNZ9E=; b=Z74vSDASyr5qdoUCxYMlJ8TgtQbIV1iyj8xezQg7qlavOiETkDyAM2T7 9s1Lnmq8pVWowZawFHD7mYKWV1lt2AfSpdQxVElvOcIRxEhSNuV469Ej1 AVJllZNAi4s1/9PRLRgwuEEWVgUwr8SKvQrdPTH5mIIX78mc5bOx5EHN8 A=; X-IronPort-AV: E=Sophos;i="5.82,325,1613433600"; d="scan'208";a="136607583" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH 4/6] kvm/i386: Avoid multiple calls to check_extension(KVM_CAP_HYPERV) Date: Mon, 24 May 2021 21:54:07 +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.253] X-ClientProxiedBy: EX13D10UWB004.ant.amazon.com (10.43.161.121) 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=771b08666=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.371, 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 Reviewed-by: Alexander Graf --- target/i386/kvm/kvm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index d19a2913fd..362f04ab3f 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; @@ -715,8 +716,7 @@ unsigned long kvm_arch_vcpu_id(CPUState *cs) =20 static bool hyperv_enabled(X86CPU *cpu) { - CPUState *cs =3D CPU(cpu); - return kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV) > 0 && + return has_hyperv && ((cpu->hyperv_spinlock_attempts !=3D HYPERV_SPINLOCK_NEVER_NOTIFY)= || cpu->hyperv_features || cpu->hyperv_passthrough); } @@ -2172,6 +2172,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 Sun May 19 02:06:36 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=1621886594; cv=none; d=zohomail.com; s=zohoarc; b=c7GqyVdbJbXIhTgx+IVF4Tzbts5PBL7s9bdEyec479P+0GNNLBKuRTJy5UzqslIJRsyo0lEjwh9fAQzBa++c4xHa7akPdTtLH+OjAWscYdr3+pZbdcsQtklrd98piacDEPGjf3ea2UVxKksrgLA9JaLYSPYbs5/EkAyZbpC19M4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621886594; 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=xpGv7I/JVbWk3gCMlwhBWH3s3VAIfzbRaEG/9eDRB2o=; b=aYgXQ6thn7dSYrkl54KGeR/oQVGR6UoBnPsbyGjQ21Mjvk8P/oF/QkJDiD2qoBr0KavBM5wJRulcpk/T/OUZDhTksunlLnDztIn1imu97BFQ54VPRw1EMgyT9mtN/rtAa3u+9XlY+gCqoDdTOOfVbingO9NNpCr7HeD3btNBdF8= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621886594622145.2397967750942; Mon, 24 May 2021 13:03:14 -0700 (PDT) Received: from localhost ([::1]:40778 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llGnB-0005RK-56 for importer@patchew.org; Mon, 24 May 2021 16:03:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59388) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGlv-0004DP-JG for qemu-devel@nongnu.org; Mon, 24 May 2021 16:01:56 -0400 Received: from smtp-fw-9103.amazon.com ([207.171.188.200]:27262) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGlp-0005GZ-Kr for qemu-devel@nongnu.org; Mon, 24 May 2021 16:01:53 -0400 Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-1e-c7f73527.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-9103.sea19.amazon.com with ESMTP; 24 May 2021 20:01:47 +0000 Received: from EX13D28EUC003.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-1e-c7f73527.us-east-1.amazon.com (Postfix) with ESMTPS id 1C54EA06B4; Mon, 24 May 2021 20:01:45 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.160.110) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Mon, 24 May 2021 20:01:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1621886509; x=1653422509; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=xpGv7I/JVbWk3gCMlwhBWH3s3VAIfzbRaEG/9eDRB2o=; b=nSmdEKlnPwyPQM5WHs3hRQPFTuZke55n7JYe+lwECBJUAklmxiDNW4CC tDRDX/2ixnTpZ851C96pSK0p2Zmvfsmxzv4MryhvXB/FnYJrilmU7DrbO ffe3e99PST3wbjO1Mjk2GlNhLSFdVDVgOgPXZGEa7Vbyw/Kf9OF+Wc/EX 8=; X-IronPort-AV: E=Sophos;i="5.82,325,1613433600"; d="scan'208";a="934796182" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH 5/6] kvm/i386: Add support for user space MSR filtering Date: Mon, 24 May 2021 22:01:20 +0200 Message-ID: <4c7ecaab0295e8420ee03baf37c7722e01bb81ce.1621885749.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.160.110] X-ClientProxiedBy: EX13D22UWC001.ant.amazon.com (10.43.162.192) 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.188.200; envelope-from=prvs=771b08666=sidcha@amazon.de; helo=smtp-fw-9103.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.371, 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" 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 | 72 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 362f04ab3f..3591f8cecc 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; @@ -2138,6 +2140,57 @@ 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, 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}; + + filter.flags =3D KVM_MSR_FILTER_DEFAULT_ALLOW; + + if (has_hyperv) { + /* Hyper-V overlay page MSRs */ + 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"); + return -1; + } + + return 0; +} + int kvm_arch_init(MachineState *ms, KVMState *s) { uint64_t identity_base =3D 0xfffbc000; @@ -2269,6 +2322,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 @@ -4542,6 +4606,11 @@ static bool host_supports_vmx(void) return ecx & CPUID_EXT_VMX; } =20 +static int kvm_handle_wrmsr(X86CPU *cpu, struct kvm_run *run) +{ + return 0; +} + #define VMX_INVALID_GUEST_STATE 0x80000021 =20 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) @@ -4600,6 +4669,9 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run= *run) ioapic_eoi_broadcast(run->eoi.vector); ret =3D 0; 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 Sun May 19 02:06:36 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=1621886643; cv=none; d=zohomail.com; s=zohoarc; b=TuhwqA5/7fUEIqV4peMCee9i8z2bQSiXBpdlHlQanfgd4KExT/Y7qUxq3ud6/1T385lDYNJb4fhDlWUXliQuwDXqO2ddAS9Cw7Wglyuamm2zZtot8YzuATKaOSAJqp/qtVFvi6Y/eUeQ+TMAqi0sUW0xkkx43wDuyoN0liCkgZQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621886643; 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=0DlcipmBXBPIGReA/Oi5A44bEFbMqBCheMbxmencY+Y=; b=Vl1+36B+vWbIeMPRwVUQeu9Ds/QicXg3Z2Yl8zfy6B0Nd0GnBl46oPBTEeKFIt69CmpcVtJ5ZfFhEaA4yN1WFHeu82KgOufkwattJiOb28isP763iJwUjATo9fsN5dsauVPCVjG6sgvkcCh3nlPC/okyTrtc99vRt0CVk6wrDRE= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 162188664363161.39171622963988; Mon, 24 May 2021 13:04:03 -0700 (PDT) Received: from localhost ([::1]:43552 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llGnw-0007MC-Lf for importer@patchew.org; Mon, 24 May 2021 16:04:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59730) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGmz-0005XR-AC for qemu-devel@nongnu.org; Mon, 24 May 2021 16:03:01 -0400 Received: from smtp-fw-80007.amazon.com ([99.78.197.218]:32860) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llGmx-0005uZ-5i for qemu-devel@nongnu.org; Mon, 24 May 2021 16:03:01 -0400 Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-2a-41350382.us-west-2.amazon.com) ([10.25.36.214]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP; 24 May 2021 20:02:51 +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-41350382.us-west-2.amazon.com (Postfix) with ESMTPS id 9CA35C0AD4; Mon, 24 May 2021 20:02:49 +0000 (UTC) Received: from uc8bbc9586ea454.ant.amazon.com (10.43.160.17) by EX13D28EUC003.ant.amazon.com (10.43.164.43) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Mon, 24 May 2021 20:02:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1621886580; x=1653422580; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=0DlcipmBXBPIGReA/Oi5A44bEFbMqBCheMbxmencY+Y=; b=K9r8YsmdBG3UnOuY0IR3qfYcHjDzAhFIPBdHfFsPR+Hrjfq1WkjQTjhm T5zODSoh1ruyFQ26lP8oBKjOEx8sOw4Jf5y00sMm0CNmbcDMYk8n8Jk90 nnWD2MqA9IRoRMrGxZpQaVcPRGugzn+NQdotg+RO5IrYWqk6mXufmRc6I k=; X-IronPort-AV: E=Sophos;i="5.82,325,1613433600"; d="scan'208";a="3031144" From: Siddharth Chandrasekaran To: Paolo Bonzini , Marcelo Tosatti CC: Siddharth Chandrasekaran , Siddharth Chandrasekaran , Alexander Graf , Evgeny Iakovlev , Liran Alon , Ioannis Aslanidis , , Subject: [PATCH 6/6] hyper-v: Handle hypercall code page as an overlay page Date: Mon, 24 May 2021 22:02:22 +0200 Message-ID: <8f62de7363c68b52200d864c8e0139221617dba2.1621885749.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.160.17] X-ClientProxiedBy: EX13D14UWB001.ant.amazon.com (10.43.161.158) 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.218; envelope-from=prvs=771b08666=sidcha@amazon.de; helo=smtp-fw-80007.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.371, 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" 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 | 84 ++++++++++++++++++++++++++++++++++++++ target/i386/kvm/hyperv.h | 4 ++ target/i386/kvm/kvm.c | 26 +++++++++++- 5 files changed, 127 insertions(+), 2 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..01c9c2468c 100644 --- a/target/i386/kvm/hyperv.c +++ b/target/i386/kvm/hyperv.c @@ -16,6 +16,76 @@ #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) +{ + 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. + */ + 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: + do_overlay_update(cpu, &hcall_page, msr, data); + break; + default: + return; + } +} =20 int hyperv_x86_synic_add(X86CPU *cpu) { @@ -44,6 +114,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 3591f8cecc..bfb9eff440 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -2333,6 +2333,10 @@ int kvm_arch_init(MachineState *ms, KVMState *s) } } =20 + if (has_hyperv && msr_filters_active) { + hyperv_overlay_init(); + } + return 0; } =20 @@ -4608,7 +4612,27 @@ static bool host_supports_vmx(void) =20 static int kvm_handle_wrmsr(X86CPU *cpu, struct kvm_run *run) { - return 0; + int r =3D -1; + uint32_t msr; + uint64_t data; + + if (run->msr.reason !=3D KVM_MSR_EXIT_REASON_FILTER) { + return -1; + } + + 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"); + } + + return r; } =20 #define VMX_INVALID_GUEST_STATE 0x80000021 --=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