Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1352647rbb; Mon, 26 Feb 2024 06:50:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUjsISYAe1YtiV2/WP4uK+37ul2Z5gZ0bs+GYpLukuSfh4FwKpLT6amUrzpaKqEVP34fYghlxjUeoIcL2mNLPxwDjH58ndBQ7+3iGPdpw== X-Google-Smtp-Source: AGHT+IF5zCPFg5pMCt1XCrwAaVELYl/9ddLLqzpcYuerl6P+tmOCSYBEgVQMtKnzu5+oO0xA1wNI X-Received: by 2002:a05:620a:46a2:b0:787:bae4:2473 with SMTP id bq34-20020a05620a46a200b00787bae42473mr14555208qkb.5.1708959006815; Mon, 26 Feb 2024 06:50:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708959006; cv=pass; d=google.com; s=arc-20160816; b=j0IgOLftaa+kOlYWRADBr+Bidx3f7F2K/4BE8TwGV5Qqml2n0O0PAEzUF2X7b9mVnh GGhYEF4rmz3/0qjbZLtwfQ+PggCnesaEGkiIwgaQcdCEmWkQRye8TTG0vCz7/wnBRBt6 Tm7f5cUBGV8YhYQA4hizZbfGpMyc4JJXRzhW2/nf71ni2dyoFkndh0g3+KCULgVShDYv IvfC0txQVhEPvA7ZUVaoCquA5p5dWcszBCPxcll2Iplv66ty7mHgm1CsWBLAhJio0dF7 T5C54KvyHP/jqss+7r1+o2pcpGcv2qP3deIKBPrUNqKEz0UO7sNppg4woS2FlIZIL02x 3VfA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=r31fdmf865h0HOvGM8QPICowYa+OjjDSIGglWYpsdYg=; fh=+puDlAsxirYQ5qrJ7TOXghSVwexzKaD6Try0539GwZw=; b=AgNOMcUKtbeoalv3yJucjADMi6xt1WvxQsonhkdgG78mBNBE1n7/96iRxS53qQSvkD 4eNSVXpQLALtwuq2ARrxVvoLoHK4QqwGBkG6+siHxMLK+6ZjcLzkVnoQSufJcMgFt9Wd 8Co4f9+bEbb117hg0IFau4w8zk1de5tjpPO/GIYUhbv+/4Gi93ZTme+Fu/T8lGq7+k3w KedFNz7O0EmIuXbA/5Kx5eOyp+1rbbyxGdVSlkSHYpzkC+3Cnl2Bwik67E5aHQdglkNn 7lIcgKRo3lu1MZFTlzC+lOjeJpyxklWCuUd2be1wMTAzVbHph0iW3mSg/AjEHEWmr+wD eNJA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=lbTF86xe; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-81681-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-81681-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id vy4-20020a05620a490400b0078791072564si5163737qkn.215.2024.02.26.06.50.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 06:50:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-81681-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=lbTF86xe; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-81681-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-81681-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 57B331C2561F for ; Mon, 26 Feb 2024 14:50:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4F7DF1384AF; Mon, 26 Feb 2024 14:36:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lbTF86xe" Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 247A6137C28; Mon, 26 Feb 2024 14:36:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708958194; cv=none; b=IskGchOQhhtV498mrCMkKrMgH4EHwfkd38zyTefqoUB6iOqYcX8u4kg9R5En8YtUdfB3LJ/fxFT050MOtoQfHxQWABtIRxeUInswvvu3mI8NM01jrnJw0v16FWv8ZJCJ+KVOkOg0rNoTriFT4vttvGqK2Pyn4h666BkQt97dtFY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708958194; c=relaxed/simple; bh=TBy/bNE8b+sGzQQeRO6wKchQYra2E1+jmrOAziFjCG8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XA3qLeYfp0gLlZqSc3V+xLYx8N86kRZUcnQt7zFD5D4xBIx8i6a/+dvA9KugU0Gr35D6ujV8HIyJE7+2K3u2WzUaUKbA4PneCdomeqRcTpX9WT7z1Ulxqm7HmfEQAUb3Ph3fvFJBLe7c5UCp8uUiqOZ7xH64kaA5wjchG5PmmQg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lbTF86xe; arc=none smtp.client-ip=209.85.210.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-6e08dd0fa0bso2687626b3a.1; Mon, 26 Feb 2024 06:36:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708958192; x=1709562992; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=r31fdmf865h0HOvGM8QPICowYa+OjjDSIGglWYpsdYg=; b=lbTF86xew8igCPX7Kxo6Qq8LBVTNC+jWBKSgCaQrKn7SiHqQ7377v5AYkRUriK1EGJ WtUMlCVONnUHt+YBWbuAOBAGQ4T0bl6Yd9hn4TsmjyNS+C5mVXQbprWV1A/ySeRbF6hI k26Prx3RHTsKQtcgpw0USJJ02Qtij5OEwSrGLwrmFBjIreQn1hsJv9izRVtvjdmNy8k6 7Ewij0RJeYlXEnknQ+wbnhoT7+srLXAtCUEE1YzoT9a9vNf4sEtzZq1EN/IPtNqtLG1B NuQYidbEKB/uoPMUHOC8dcIP7haOc0NT1fHDTyKHLhS5bYAdM9UY7N08736EyXh+HooD d9YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708958192; x=1709562992; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=r31fdmf865h0HOvGM8QPICowYa+OjjDSIGglWYpsdYg=; b=tv8c7eJ57AnmJ4xFy/7oOfK+BmQas1QwPR7BmjAfAcQoqSGqxLR8Ovt5Idg2LevlD/ W06ZpSc3LdrEP7OP3bwDDPKuFNp5Y75tLPjJa8VmVH4bp4sRsFXQL2z94WiNh/bhFl6j Hk6QAc/SCnPAaBW+BsrTIhkln4g8SLUw/Q/GuE2XfADchLBKEFIrlIGAwIG50epLdhth I6PkE9UH/q3twMH3zhNKAZ/zwctuglp8K4vjzMrLIq/jfsKbT4AwqeuUTUqiW6SRw6un BThOSVZ9Xj8HKYl3Ia0Lf3sRVVElF0KxrzE3SpLatMtD5DM+Xk962gKpU8tDLQnIdyn1 /+bg== X-Forwarded-Encrypted: i=1; AJvYcCU4Ucg5EtKtSdlzvKX2U3n5f6J2yguYGKbGNvTUpcWlh8G5XFnnGqVDPYkUYIpHvzE99Iki1RZgc8p3wKuWt07SWahA X-Gm-Message-State: AOJu0YyTij2vheVYR7NWdYi4eXddHbzNFMWKq5HUrO7ZuRKoxqG1ltIN sq++hIgBijDC6nMPTC2KAOMsPsb6pZO+69GpZv/IjXIHYETQ6OUVvLe3EziC X-Received: by 2002:a05:6a20:9f43:b0:1a0:694c:c467 with SMTP id ml3-20020a056a209f4300b001a0694cc467mr8755527pzb.14.1708958192240; Mon, 26 Feb 2024 06:36:32 -0800 (PST) Received: from localhost ([47.254.32.37]) by smtp.gmail.com with ESMTPSA id h20-20020a635314000000b005dc1edf7371sm4012540pgb.9.2024.02.26.06.36.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Feb 2024 06:36:31 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: Lai Jiangshan , Hou Wenlong , Linus Torvalds , Peter Zijlstra , Sean Christopherson , Thomas Gleixner , Borislav Petkov , Ingo Molnar , kvm@vger.kernel.org, Paolo Bonzini , x86@kernel.org, Kees Cook , Juergen Gross , Dave Hansen , "H. Peter Anvin" Subject: [RFC PATCH 35/73] KVM: x86/PVM: Handle PVM_SYNTHETIC_CPUID synthetic instruction Date: Mon, 26 Feb 2024 22:35:52 +0800 Message-Id: <20240226143630.33643-36-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20240226143630.33643-1-jiangshanlai@gmail.com> References: <20240226143630.33643-1-jiangshanlai@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Lai Jiangshan The PVM guest utilizes the CPUID instruction for detecting PVM hypervisor support. However, the CPUID instruction in the PVM guest is not directly trapped and emulated. Instead, the PVM guest employs the "invlpg 0xffffffffff4d5650; cpuid;" instructions to cause a #GP trap. The hypervisor must identify this trap and handle the emulation of the CPUID instruction within the #GP handling process. Signed-off-by: Lai Jiangshan Signed-off-by: Hou Wenlong --- arch/x86/kvm/pvm/pvm.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/arch/x86/kvm/pvm/pvm.c b/arch/x86/kvm/pvm/pvm.c index 514f0573f70f..a2602d9828a5 100644 --- a/arch/x86/kvm/pvm/pvm.c +++ b/arch/x86/kvm/pvm/pvm.c @@ -1294,6 +1294,36 @@ static int handle_exit_breakpoint(struct kvm_vcpu *vcpu) return 1; } +static bool handle_synthetic_instruction_pvm_cpuid(struct kvm_vcpu *vcpu) +{ + /* invlpg 0xffffffffff4d5650; cpuid; */ + static const char pvm_synthetic_cpuid_insns[] = { PVM_SYNTHETIC_CPUID }; + char insns[10]; + struct x86_exception e; + + if (kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu), + insns, sizeof(insns), &e) == 0 && + memcmp(insns, pvm_synthetic_cpuid_insns, sizeof(insns)) == 0) { + u32 eax, ebx, ecx, edx; + + if (unlikely(pvm_guest_allowed_va(vcpu, PVM_SYNTHETIC_CPUID_ADDRESS))) + kvm_mmu_invlpg(vcpu, PVM_SYNTHETIC_CPUID_ADDRESS); + + eax = kvm_rax_read(vcpu); + ecx = kvm_rcx_read(vcpu); + kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false); + kvm_rax_write(vcpu, eax); + kvm_rbx_write(vcpu, ebx); + kvm_rcx_write(vcpu, ecx); + kvm_rdx_write(vcpu, edx); + + kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(insns)); + return true; + } + + return false; +} + static int handle_exit_exception(struct kvm_vcpu *vcpu) { struct vcpu_pvm *pvm = to_pvm(vcpu); @@ -1321,6 +1351,9 @@ static int handle_exit_exception(struct kvm_vcpu *vcpu) return kvm_handle_page_fault(vcpu, error_code, pvm->exit_cr2, NULL, 0); case GP_VECTOR: + if (is_smod(pvm) && handle_synthetic_instruction_pvm_cpuid(vcpu)) + return 1; + err = kvm_emulate_instruction(vcpu, EMULTYPE_PVM_GP); if (!err) return 0; -- 2.19.1.6.gb485710b