From patchwork Wed Nov 8 11:17:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162963 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp842904vqo; Wed, 8 Nov 2023 03:21:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IERo4KApWSEl2jBRPFdyAMhDP0aT1/Hw+2P/cgIpLaxXkxnRIVi/hrq4vHF9k02nlYlRHFm X-Received: by 2002:a17:903:244b:b0:1cc:644a:211b with SMTP id l11-20020a170903244b00b001cc644a211bmr1892356pls.47.1699442514386; Wed, 08 Nov 2023 03:21:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442514; cv=none; d=google.com; s=arc-20160816; b=Y6ri8vm3xfe31lVcvqgCwfHHqvvfymE/SgLjj2ehBsQd8UdPZBEHjTmHYYhr5OI375 uPdE1/nJHpJfRAXE2esU/KFwi4o+NZy/XfH3Iup4gxN7jxR2F3NU0F2zLmPwdn73bPXm P1CGTEdusq36WUs/dR+cI6Gygn/dVIsM+c6Xac77gy9cavCX7XaQv0m8NUTxJ0Sz+gHK zatxnEh4SwsBZhQ6d04fX3ZLnWcC5amUcO4EWcIuzvgKBUd2DXxhd/PZ8UKj09gQvkc2 2VFh8lHtzhE1TpOWNXjs5iCdvshS1Yta86S5IxxganJulvAULkNAExJrcfLdMC+Qcx9d eknA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=G8tv4EqIfPFJRUiW9cZCsErULQwL6MkqMdyO6PRnfzY=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=lSbnQonK3FuAEaJKfydvGD8V5tTAfflNbbPGt87yvhjd0uVHEG/g49YDgdzNM7ruzI KxzR9HIzleI1De8mD+QsU3ik/ruQ9Y2o3LljWQ9xS1FWK8apFwjCigQQ6W6buJC2A6w9 8QRmVZ+iHRo5D7usLa+9HMjw52tlIDNFabe2shXjCgGxf41KyujYyA5PlSy0QqCQznzq f+n13MGeJKYhrsnHdOd8CfplSp6am7Q10IER40Xyn67P0vT4ZwZyoHwXEpMlKy4TNY8G CZvnnX1cwzsOgTxlcy7rWLWYIoGIL+sEd/B86Ore1BtDI8PXLsV88cvymWvrtLIrucDF f4OQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=TNe4BukL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id kg14-20020a170903060e00b001cc58f174fasi1835911plb.364.2023.11.08.03.21.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:21:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=TNe4BukL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 918E3807E792; Wed, 8 Nov 2023 03:20:22 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344609AbjKHLTe (ORCPT + 32 others); Wed, 8 Nov 2023 06:19:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344455AbjKHLTR (ORCPT ); Wed, 8 Nov 2023 06:19:17 -0500 Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45B491729; Wed, 8 Nov 2023 03:19:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442355; x=1730978355; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=G8tv4EqIfPFJRUiW9cZCsErULQwL6MkqMdyO6PRnfzY=; b=TNe4BukLRNg0YJfWA90/7JIByXU+dM2VDsbXlcy/qOnFybyCFrYRcHD8 ZQ2wY9eXVgY73blHeJws0Szlh9DQPrDdIa28YsluQgaSq9LIeTWGrNCzO gSWADZe4RXHIN6DqUxD2pE+OXoWeGGMtdU66EPFsK+1fAs3dn0w0wwAlm U=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="251427581" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-edda28d4.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:19:09 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1a-m6i4x-edda28d4.us-east-1.amazon.com (Postfix) with ESMTPS id 8CC3F8057A; Wed, 8 Nov 2023 11:19:05 +0000 (UTC) Received: from EX19MTAEUB001.ant.amazon.com [10.0.43.254:28968] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.17.103:2525] with esmtp (Farcaster) id a4bb631b-a22d-4d34-9086-5dcfe9a4daf9; Wed, 8 Nov 2023 11:19:04 +0000 (UTC) X-Farcaster-Flow-ID: a4bb631b-a22d-4d34-9086-5dcfe9a4daf9 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:19:04 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:18:59 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 04/33] KVM: x86: hyper-v: Move hypercall page handling into separate function Date: Wed, 8 Nov 2023 11:17:37 +0000 Message-ID: <20231108111806.92604-5-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D036UWB003.ant.amazon.com (10.13.139.172) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:20:22 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994633836850460 X-GMAIL-MSGID: 1781994633836850460 The hypercall page patching is about to grow considerably, move it into its own function. No functional change intended. Signed-off-by: Nicolas Saenz Julienne Reviewed-by: Maxim Levitsky --- arch/x86/kvm/hyperv.c | 69 ++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index e1bc861ab3b0..78d053042667 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -256,6 +256,42 @@ static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) kvm_make_request(KVM_REQ_HV_EXIT, vcpu); } +static int patch_hypercall_page(struct kvm_vcpu *vcpu, u64 data) +{ + struct kvm *kvm = vcpu->kvm; + u8 instructions[9]; + int i = 0; + u64 addr; + + /* + * If Xen and Hyper-V hypercalls are both enabled, disambiguate + * the same way Xen itself does, by setting the bit 31 of EAX + * which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just + * going to be clobbered on 64-bit. + */ + if (kvm_xen_hypercall_enabled(kvm)) { + /* orl $0x80000000, %eax */ + instructions[i++] = 0x0d; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0x00; + instructions[i++] = 0x80; + } + + /* vmcall/vmmcall */ + static_call(kvm_x86_patch_hypercall)(vcpu, instructions + i); + i += 3; + + /* ret */ + ((unsigned char *)instructions)[i++] = 0xc3; + + addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK; + if (kvm_vcpu_write_guest(vcpu, addr, instructions, i)) + return 1; + + return 0; +} + static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 data, bool host) { @@ -1338,11 +1374,7 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, if (!hv->hv_guest_os_id) hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; break; - case HV_X64_MSR_HYPERCALL: { - u8 instructions[9]; - int i = 0; - u64 addr; - + case HV_X64_MSR_HYPERCALL: /* if guest os id is not set hypercall should remain disabled */ if (!hv->hv_guest_os_id) break; @@ -1351,34 +1383,11 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, break; } - /* - * If Xen and Hyper-V hypercalls are both enabled, disambiguate - * the same way Xen itself does, by setting the bit 31 of EAX - * which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just - * going to be clobbered on 64-bit. - */ - if (kvm_xen_hypercall_enabled(kvm)) { - /* orl $0x80000000, %eax */ - instructions[i++] = 0x0d; - instructions[i++] = 0x00; - instructions[i++] = 0x00; - instructions[i++] = 0x00; - instructions[i++] = 0x80; - } - - /* vmcall/vmmcall */ - static_call(kvm_x86_patch_hypercall)(vcpu, instructions + i); - i += 3; - - /* ret */ - ((unsigned char *)instructions)[i++] = 0xc3; - - addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK; - if (kvm_vcpu_write_guest(vcpu, addr, instructions, i)) + if (patch_hypercall_page(vcpu, data)) return 1; + hv->hv_hypercall = data; break; - } case HV_X64_MSR_REFERENCE_TSC: hv->hv_tsc_page = data; if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) {