From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 A36DF22301 for ; Sat, 1 Feb 2025 01:55:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374924; cv=none; b=JW1RJyEeX3SxwEoyJE2rp7lYGzILQVedJnhaI5f8sqj75n6BsUBAJjDR4fv2JIYBktWvGxYwxyxSnfC65epM+LcsItPdcpqE/K+wVP89BQ1pzFTuT2I7HS6YjSwAtqRRpRDotugtvQYX7ToNvsJ982HSW84KoZLy/+/PmiMy9qE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374924; c=relaxed/simple; bh=0UVEz7Yf4APmVdd/+AGLKukxGO1cn8yh9iUuPAteZH0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Y+AD2wr64OFur1xl2ohPPVbsg5MOEQMQRpiYYi4L/qTaIXEfMcR88hooY+MjD3KOnvS7tDBSgRcBvtIfn6Q/zmkB7ZjzYhACDEyYwNfYv2yvoi1Xy+LfeEDA/BXG/lb0/2pd8jEjE0umS2u4U5EZOszIHM2a04bR6cg+IJdIoW0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iicOtG3b; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iicOtG3b" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ee5616e986so7225551a91.2 for ; Fri, 31 Jan 2025 17:55:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374922; x=1738979722; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=ch7J5FApSjtLmMgjboJuXdLA8zQpXCRwdiOWm7e70JA=; b=iicOtG3bpbeHW44yFQ8yLwn+p2w8oZxs+jt0zfEvwHRKOqtxxcSbkYpF/M3ZM0feDi jplq2VOTsiLMtCjZHRcMV8TOVt9p2xeElMWuebUR1U+j2ffVWWviJxyITT9s6bx9dXRn FRdBEzBTmJ+lyqxWo5PQDuY1s1HVKUlxM60gNcUoI5E+1VL893FVim4v+V+NE1lBr8sl hF8LzVGKihB5MfYybljA+uemWgsIQ9sU4b9W7OpA9HIsPjxA8Li7mpo9GbQIlCTE06UP iNWqrnso19e3Z4gcrZOpM4e1CFSC4vtNvLbAqkg4F0nDsi0Z2lOAs6Z9Mw+5CC2JSOZ5 Diew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374922; x=1738979722; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ch7J5FApSjtLmMgjboJuXdLA8zQpXCRwdiOWm7e70JA=; b=doaB43Gbf/PFpz412MgGMMI484jX33TyV4ZORWhNLxMVxbtjO8Krphebw7RWXdSOV3 l1z6wJDN3Bq3vqjYn1+7Xc3yavVdNsfKNDCZBAopJpXTS4O9jEm4dg8zZYskUQevAVuy fUgHhKVfZXpJLGigqXoKlYfMLF55AzEiFx6cH0mr/GUZQ+oNKpbliONvgkKoxO5oTO/v tapaRTc/SNLtnEWwuCGJrARLmMCxehZfOpGlG52c89snPPirYzsw5Ov6t62+3WLX6R5c P72H2ODHpVX1wzKD1QrPzDVfatV2EwgSZYb3+JNg8EGrpr7UvDosOdGY+0MLQ+BGKT2S fTCg== X-Forwarded-Encrypted: i=1; AJvYcCUzDnFmD/HsGCnmfRrWc5S0hjiw6t6j+vT615Vb9HdNtFVGzFRMLQn2PjaAP6qHRF2j4pTfwwmJFZp1vGU=@vger.kernel.org X-Gm-Message-State: AOJu0YxXg1wMnK922+LkWmKzRciDMosUqpiuXIKMKsz7edTplzphnbkI qr0Dxs0p8G9C182eLTd5cqGZa36E3jZsHvV7zr/ytt+n4fGxeGSqodfIXwpvprrDdCumMtgE64b fOg== X-Google-Smtp-Source: AGHT+IGvjkw8fnnEbcSuYvUL2VtZua6gMJ2cUrDqwz56RDm9CnaQqFl8dZRTAPoXgRzxuzyE3ZLzmsbAtzc= X-Received: from pjbqb8.prod.google.com ([2002:a17:90b:2808:b0:2ea:6b84:3849]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:c88e:b0:2ee:ad18:b309 with SMTP id 98e67ed59e1d1-2f83aba9d18mr18457927a91.3.1738374921915; Fri, 31 Jan 2025 17:55:21 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:08 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-2-seanjc@google.com> Subject: [PATCH v2 01/11] KVM: nVMX: Check PAUSE_EXITING, not BUS_LOCK_DETECTION, on PAUSE emulation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When emulating PAUSE on behalf of L2, check for interception in vmcs12 by looking at primary execution controls, not secondary execution controls. Checking for PAUSE_EXITING in secondary execution controls effectively results in KVM looking for BUS_LOCK_DETECTION, which KVM doesn't expose to L1, i.e. is always off in vmcs12, and ultimately results in KVM failing to "intercept" PAUSE. Because KVM doesn't handle interception during emulation correctly on VMX, i.e. the "fixed" code is still quite broken, and not intercepting PAUSE is relatively benign, for all intents and purposes the bug means that L2 gets to live when it would otherwise get an unexpected #UD. Fixes: 4984563823f0 ("KVM: nVMX: Emulate NOPs in L2, and PAUSE if it's not = intercepted") Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index f72835e85b6d..3654c08cfa31 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8092,7 +8092,7 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu, * the PAUSE. */ if ((info->rep_prefix !=3D REPE_PREFIX) || - !nested_cpu_has2(vmcs12, CPU_BASED_PAUSE_EXITING)) + !nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING)) return X86EMUL_CONTINUE; =20 break; --=20 2.48.1.362.g079036d154-goog From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 169CD381AF for ; Sat, 1 Feb 2025 01:55:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374925; cv=none; b=KGfZkAq0r6N9tgn2pxNxjPVwlEXFJ2TRNuKfMpV5rcJZ4MKOXY6HtYyUliBdYgWkMRvA7SYiqIqQ5LzdA0AHIS0i2HVOujHgN2qwVuLM6r5UmUQLn9gAm6D0sNhVxCMO/5CqwBV9yf1X7j0niR/jnZgbUfcmuxeY/dKaKro4woI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374925; c=relaxed/simple; bh=2nRKDkSWDDf8jzC//34iIp61t0/QFHqNiM2wu/iV0a0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MnPH/VrkV3enJGay0X75jM3dmB4NcFtREukoe4vAkzjkIA33LL7hHH+aPnxrdTCKiJHv2H8BZDqIxDj8z93/cNl1fn3MRMSOsM+Y+YWbZaMLwY41h7ALdtGsDtCbcwZ0mjtOcMAwNQJ49IMHsW+XHtqFQYgFtpPQj4T0HrlXI+Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2hbg7+8i; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2hbg7+8i" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef7fbd99a6so4966708a91.1 for ; Fri, 31 Jan 2025 17:55:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374923; x=1738979723; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=hIrDaupeXQ8y0x86Zzi3JQgYPukIHboTrBwjEiiwuw4=; b=2hbg7+8ikYMk2wIJazfc04kvyiiaxdRdbmdhJnjgsgjvTYgqmqnzEnd7wBlWn5W+LP xgctsMkQS0QUGWNMuWeI5LfRnaYJNyB+6CqEqAuVla3EG253GePIUK+qe5e6N3Jf32k3 04fgHo0qEMr6OJ+NSUBvJal16/dFl1fkpL+nKceaZHf1nleDH9HLstqA0eH499yNlolU gFPTa/HfCAHpmQxp25ZM1wQLzVeGD3B5uMZAsOqPjY7GOWLwH6eeLgkWWg6y7rkxmlBF DfrSAGG3sF8stO6CzWp6OzliQ2Xze10hFLY4oro2EXfOSbGz2xXjN96q3pOzT7iu8DiW Y8JQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374923; x=1738979723; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hIrDaupeXQ8y0x86Zzi3JQgYPukIHboTrBwjEiiwuw4=; b=Q85s3UcG/dDWZ/V16AVE8DcPjvj75IPvGsNW1ibk0OpWFyrVLhOfneuQbOyCnjROG/ lU9SQfLhbHmEpXS8AayyknEVLHVAP4wpjjRrar3g5efKK2te5Joe9VxVKCb792O7NCBQ haaUFs0UZsbPl3LICFCibj6F0/94XInhrAv8GVLbYv+LxhAn3s9dsc5hh9AS/KzXWA9Z HmcmZB3c4tAItkNw5cBlNm/K+mcB7852iuozVvoL4uv1IgOqC57Lqd44tplHXf/qRkK2 7NTvk9P2rsPU6+lrbvHOllrFQnCjliWGjMdoJWxp3UAJHd5UVaz457UTGHBZFEC8LomL yZTw== X-Forwarded-Encrypted: i=1; AJvYcCWdfUGNOqnVKqOmpLCg/Wdl3Qn3u0lIroAnL/J/B6L+Rx/NSDxhzNFQfZgcmTpios9mwmaRN1xDExczd+Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yy4NTFpNnZv/MReWiR5x0Dne1lhLxtMpkKPdxvfsxJuhz3zU0Qk feoYSMH3aunn7WCKMkISa604vbmSdiReu2oMirAQupu2O5BhsZnjoMw8Ii95/5f/K8ai3K4TZM/ AsQ== X-Google-Smtp-Source: AGHT+IHT7N7Q088xoXBNld7OpzfjuKQVGlZu3JPwD40gzpt9LBs7F2KvjEbfG5sWR9bPdoFOLRy/GEsuv5U= X-Received: from pjtu5.prod.google.com ([2002:a17:90a:c885:b0:2ee:3128:390f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:538e:b0:2ea:7fd8:9dc1 with SMTP id 98e67ed59e1d1-2f83abfedfemr22296630a91.18.1738374923421; Fri, 31 Jan 2025 17:55:23 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:09 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-3-seanjc@google.com> Subject: [PATCH v2 02/11] KVM: nSVM: Pass next RIP, not current RIP, for nested VM-Exit on emulation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Set "next_rip" in the emulation interception info passed to vendor code using the emulator context's "_eip", not "eip". "eip" holds RIP from the start of emulation, i.e. the RIP of the instruction that's being emulated, whereas _eip tracks the context's current position in decoding the code stream, which at the time of the intercept checks is effectively the RIP of the next instruction. Passing the current RIP as next_rip causes SVM to stuff the wrong value value into vmcb12->control.next_rip if a nested VM-Exit is generated, i.e. if L1 wants to intercept the instruction, and could result in L1 putting L2 into an infinite loop due to restarting L2 with the same RIP over and over. Fixes: 8a76d7f25f8f ("KVM: x86: Add x86 callback for intercept check") Signed-off-by: Sean Christopherson --- arch/x86/kvm/emulate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 60986f67c35a..0915b5e8aa71 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -478,7 +478,7 @@ static int emulator_check_intercept(struct x86_emulate_= ctxt *ctxt, .src_bytes =3D ctxt->src.bytes, .dst_bytes =3D ctxt->dst.bytes, .ad_bytes =3D ctxt->ad_bytes, - .next_rip =3D ctxt->eip, + .next_rip =3D ctxt->_eip, }; =20 return ctxt->ops->intercept(ctxt, &info, stage); --=20 2.48.1.362.g079036d154-goog From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 D526C7FBD6 for ; Sat, 1 Feb 2025 01:55:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374927; cv=none; b=tzpOFtdqapJI4RKsdJUA8JtK4j0V/J68JTjjQ21eVYby09q0rIUGVG7LG/uhJUj6uFlFWKjezXxSUp4ee5rtGpvYZSbMLBQ1G9D5uUw7kZdGDgpWo6nv51IavwWt6gOaavSkrQ+fwJOb0XciGrrKXR9366aoOodWHFqm0CPiH7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374927; c=relaxed/simple; bh=WTPZCV619SX538Cn6Abym6cnK99EEExx7aoodOVpBsw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CwepP6HYejkLLuN/kVZd+xttxqsoB5QmdSlXXLRO5PYzXYGSqaeBweGrl8nLlizh0MQWUul7aaVHciOXRmHfDSYJUgLTD7+GaYph5NuTBZqke/C961jOKZr77/x6V1H0HnlCwJL/JfYAWAupiw0bQV0yKjlViezsJELA0bvG1KQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uhG4C+Js; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uhG4C+Js" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f780a3d6e5so5196120a91.0 for ; Fri, 31 Jan 2025 17:55:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374925; x=1738979725; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=m3XOaiGIiENgl8NnCw2lngMp/lfgzjhOH8n22r5XeXs=; b=uhG4C+Jsyf7SQb5nbd/RoqDQo9efDc6yDtsZKyKZm+kR6nzG52nYL9hE1aOETaplfi GbDmZsmF2/JDGIL23IU9gjFwYdXMzTvcTVrouABvrG68Xp94rzXwHMP4u+FOy4ZUk97U z9Ypl9yVt5P2+3MiYy3W58DSP4XqnUqr2GuYMIMztqdDZdXfC2cNhAw2a6tflvkqcwAU Z0trLM7UxfFRecZvatlz4DekyTxFcZ436V4rA1I2jxKMjZSEmO2ZzdZeZ3DwghqkTkJM 61PbFxtCDitM9eyFQxqVvjleKzQWwRNiUWQjJe3fwAGp6jOGrlAL4VGHmruWkf00Oow5 /7cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374925; x=1738979725; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=m3XOaiGIiENgl8NnCw2lngMp/lfgzjhOH8n22r5XeXs=; b=WioudpRzcpUQza13LFwDtqsnpqrp4IBH5gmO+xHCLlkaU1nitxwoG7kk7dytUd/Cx0 jx+1HP0daNzUN+wliLV24hMVPJuvbRaL+PUHH1ueKJ1M2Fz/IuO0/1X3rc7KFeY25V4n 14ak+ysWD7q5JilQCNrrWcHJTa4zEIbmHV5vRuP/Rghl8cwPEtYYwIGKZrxQHpDga6OG rE30k7HXnKxydRfMLiDvP8o3sIkpYVXebvloptKDe3q34CZ/747wGi35Ak0i22se6FjE oWm7NYdTSN4ZoSQGKYf1I+6GS+6JepeNtylhqkzXP1Dr31MeRK7dfwRK6jjeAnyE54KN /yGg== X-Forwarded-Encrypted: i=1; AJvYcCWQb+3DozdyKBTG5BjJ4MEvUj9OUBmpWRmtRLNNI3IbB1Ngts+CmHQ4yA3yLCcts38R9Gv92qSbvn1cmws=@vger.kernel.org X-Gm-Message-State: AOJu0Yxm0yDFOIIyHg7DTtVgpNzsU8Byp92c3fwj9o69+XyQx7CMlPCP HRDz/Dz/keg111F3ZozrWHBUibYoTngc8axeveniSXPbCTCtGfQVYRgoDdCPYl0uH/mjqjwpGZo lKQ== X-Google-Smtp-Source: AGHT+IFuItppbgWp7/fYWByB+OH6h9nXO6RPKexQwfXO/X+6riuHAEF3pgKvJptYfZThPrJiMZ3r6hXCHQI= X-Received: from pjb14.prod.google.com ([2002:a17:90b:2f0e:b0:2e5:5ffc:1c36]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d610:b0:2ee:4513:f1d1 with SMTP id 98e67ed59e1d1-2f83ac5e5bcmr16213901a91.23.1738374925164; Fri, 31 Jan 2025 17:55:25 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:10 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-4-seanjc@google.com> Subject: [PATCH v2 03/11] KVM: nVMX: Allow emulating RDPID on behalf of L2 From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Return X86EMUL_CONTINUE instead X86EMUL_UNHANDLEABLE when emulating RDPID on behalf of L2 and L1 _does_ expose RDPID/RDTSCP to L2. When RDPID emulation was added by commit fb6d4d340e05 ("KVM: x86: emulate RDPID"), KVM incorrectly allowed emulation by default. Commit 07721feee46b ("KVM: nVMX: Don't emulate instructions in guest mode") fixed that flaw, but missed that RDPID emulation was relying on the common return path to allow emulation on behalf of L2. Fixes: 07721feee46b ("KVM: nVMX: Don't emulate instructions in guest mode") Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 3654c08cfa31..9773287acade 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8049,18 +8049,19 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12 =3D get_vmcs12(vcpu); =20 switch (info->intercept) { - /* - * RDPID causes #UD if disabled through secondary execution controls. - * Because it is marked as EmulateOnUD, we need to intercept it here. - * Note, RDPID is hidden behind ENABLE_RDTSCP. - */ case x86_intercept_rdpid: + /* + * RDPID causes #UD if not enabled through secondary execution + * controls (ENABLE_RDTSCP). Note, the implicit MSR access to + * TSC_AUX is NOT subject to interception, i.e. checking only + * the dedicated execution control is architecturally correct. + */ if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_RDTSCP)) { exception->vector =3D UD_VECTOR; exception->error_code_valid =3D false; return X86EMUL_PROPAGATE_FAULT; } - break; + return X86EMUL_CONTINUE; =20 case x86_intercept_in: case x86_intercept_ins: --=20 2.48.1.362.g079036d154-goog From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 7E084139D05 for ; Sat, 1 Feb 2025 01:55:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374929; cv=none; b=YdlnpvbgGoBDyfZLNpUSSHcyYYn8ANbBTKCe7Lt6GNCvbHssT7ezZ2Chr1z7d74dpwbYJd3TQPBRTudxccIbf19/EW9s+dZTrVx0/xaboSEEoBxafAHXsIpzrIgIgpOAeHVeQfMHiHVbHpwHLaobG5Z+rWDfVAc09Ueigk23te0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374929; c=relaxed/simple; bh=2L/cnO9EtXQAJOsgX3k8m5pz/6fkrIp1s4+pPXI7COU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EVImZGvpKWrSfSGHP8fdHUk7ueo63Ab2pU5jXqDD8DdirJUEikm5achWgE4YFYrtZ8uLTbNLe6skUdvLipzhbimvflRI1jFEcLuylLloWgdNGBOQjOGhbgIB+70NJBKuK4nox99e2ftLxIkE3Kam+DvCQ/2RiOnhhtSIUscKduI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=togLWU4P; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="togLWU4P" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21661949f23so77483845ad.3 for ; Fri, 31 Jan 2025 17:55:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374927; x=1738979727; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=POYPQ5yMJ4zL9xLSOyVPlGOIMkdLL5mZFIi+OVlxA4k=; b=togLWU4PwCPHAA9d27IUou1fAo1AgBKoNtYLoN/TjCSmNc6m22heoekzY5LXtskzne AXw4GyyRamuPrHc6RzvRUoMFPvuPyS2/6STU6OSATnCrHPB4E8NLwI2aW38HJJw92XpH HLiikN9eT0HpvsNxpqPzBGHFJtieNu2BCHxGtXcuvLI22vYb8Wa2GNvIt4Un6/PDkjDp lS5fwE4Rsgoz15nHmBpyFTsjNRGBpmlBTZDse1cUrrlC7t8HC1Q5UstHJdYIMaydLWAJ UdTH6CAcH15H2F28aCj0L03uGZ5m95iJe6FzHlY6MOG1qLGvMy1lzImRB3rod/HIdmna KDvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374927; x=1738979727; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=POYPQ5yMJ4zL9xLSOyVPlGOIMkdLL5mZFIi+OVlxA4k=; b=P6OnOx/pJNoiNtyOOsnx2N1zgD9xaV3RbUov5PcE9ZUIWikGSMa6Si2yPtCxKeTdnV q0O2EpRFcY2PpLY2sZ4XgIhrIhACNkhLaWK+KEnIRew2ng0eNGSbKwtWmkdw97nvOWR5 lw1iuuYk/8ag/o1G2SmD/r/uv/ebDyswF/wSAfklnCnzmxS90jURUPbPcy5sXVNBogzq YDehv0ZEfJzRZRdcsKRn8cPdCOWpvqYr5+v3jsdnubQ0AREiLcGxyTYrnc0knU4SEMPK wbN+73VWoCderUy3s/tbv17wYFd/qiBXEo7cJ92we+7mMPAT8zLjOQBIc/x1NJc3C/30 JjEQ== X-Forwarded-Encrypted: i=1; AJvYcCWMM6eSdsg47EwpFZzHbUwGqdkzqWK+oXieOXTkCrSO3/EDF2Pc8+bg5nV6jbEmvSy2vn8Ydo0A0dSkjUo=@vger.kernel.org X-Gm-Message-State: AOJu0YxVa1/v2Xxvi4sUmKhLiNCYQpdqEwUm5LdN3nLw6Zk4F9lIkson jtssO78XcwzHd9cWUIPSaZzRIXBImynKX8Mf4TpVHALRV2Jnw04Nbec2ydBdknSblPy2z9S1k5T 9Eg== X-Google-Smtp-Source: AGHT+IFucuJj48dbyTnCbLgl25vShezhlevEBnCH9ynRc5KSrkn3lU4zojxv7hZQDWHfYCSBvzxzdIeNmFE= X-Received: from pjtu8.prod.google.com ([2002:a17:90a:c888:b0:2f7:f660:cfe7]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:7c8c:b0:216:7ee9:21ff with SMTP id d9443c01a7336-21dd7df06f5mr157673425ad.49.1738374926702; Fri, 31 Jan 2025 17:55:26 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:11 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-5-seanjc@google.com> Subject: [PATCH v2 04/11] KVM: nVMX: Emulate HLT in L2 if it's not intercepted From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend VMX's nested intercept logic for emulated instructions to handle HLT interception, primarily for testing purposes. Failure to allow emulation of HLT isn't all that interesting, as emulating HLT while L2 is active either requires forced emulation (and no #UD intercept in L1), TLB games in the guest to coerce KVM into emulating the wrong instruction, or a bug elsewhere in KVM. E.g. without commit 47ef3ef843c0 ("KVM: VMX: Handle event vectoring error in check_emulate_instruction()"), KVM can end up trying to emulate HLT if RIP happens to point at a HLT when a vectored event arrives with L2's IDT pointing at emulated MMIO. Note, vmx_check_intercept() is still broken when L1 wants to intercept an instruction, as KVM injects a #UD instead of synthesizing a nested VM-Exit. That issue extends far beyond HLT, punt on it for now. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 9773287acade..fb4e9290e6c4 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8083,6 +8083,11 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu, /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */ break; =20 + case x86_intercept_hlt: + if (!nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING)) + return X86EMUL_CONTINUE; + break; + case x86_intercept_pause: /* * PAUSE is a single-byte NOP with a REPE prefix, i.e. collides --=20 2.48.1.362.g079036d154-goog From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 A961813E41A for ; Sat, 1 Feb 2025 01:55:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374930; cv=none; b=JUKCxl2/sPyzoQkUymZj8OaOV2eVWMitjqvt3AZY3+1MxGXEg8ePWaOfgIgU7JY5hswt7YeVuvdLsC3qFYPsYlcDluc4smYvKD6RRT2RIjK9HbK8i7yqEgqUS21l70Ofv4J6rFu62to1lPX+MOnXxUMz9kXFLZbXXzNnHN9G6ho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374930; c=relaxed/simple; bh=4KLJLbNb3dm5XQSSplNU+uwKog1RwQjU+uH/1EHjBWE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Va7Bp1ApZL9smiNDId4CN166VXNNHtNEymYQCAh+ot6KR+sE0g1Un76bi1IlBZKpyfoIvEbRoi/5qpyoaElyPGSQEQQ/rkHJjATQMKgP47jiIOWyi4vRLTJ4iWKmPQs+PYAf4SN694W7craZNLAlBlFD2fvGggGLaqGBw3EqiLU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=y92IgAIr; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="y92IgAIr" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef79d9c692so7102352a91.0 for ; Fri, 31 Jan 2025 17:55:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374928; x=1738979728; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=8fgNag4pP/Al/+jEnlzyFsUV+JTyAkY6cS8rvrrIp0U=; b=y92IgAIrikDSXHbuB6TMccQFPaLb9fatP0vEq4PCjTd0nlt8Yyvex1YY86Lj4kJzMs 99XRQsTBFgjJzFqbTn8Bv0Ar7WXlQAZfJinb6Cz5M1m1b4YBtmSxX247QJbpDTipN6vf zA7cJ7lzVVwdkWsIqJ/vSRJTzqscmpDaPvycpOhy/bim1SA0SjvurFxmXPbIbkoIlmPH 7uRWxgiLKY0ylo3IRon9grhvjaDZmELAoK5DqAnGC5y1GnfKXgw8ZBiwWLkhJR8h78kl hbInnlZG+ECU2mU2vLeubLCupv/ZcKu3UyegWnRicdRObRTllLMEll5RCndDpNc13VSq QZyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374928; x=1738979728; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8fgNag4pP/Al/+jEnlzyFsUV+JTyAkY6cS8rvrrIp0U=; b=qrZlEDV9a9Ed53wwZILTcyUhIoTZU1RIIIeslyA95F+hAtm7IY7XHCZ2VOBjOy9U6X I0QWZqPaKaC1HwMGgnRWCl7Jl+97Ee8346+oeatk2+FK8qWSD6/HZPKEOyxFVKkmXlFI MdLxNE6SB7TZq/HFrHX819kmbQGI4nQ+lolDLK424MWQS9FTicSbIsKYGtJpTEhJLTUh 88SnAkjfm3IpP4xmDv2+eeioBb9WbKXBrFNlE8gtne6Jvbeo5YPIoqy0q9dvrXWu9aI+ bPjZL0je0xdptrO1hRnBmUVQf5MXWt0JGW+MUH++jbj0Q5eljCpUbR3RLLkBkGOuxmXa 4awA== X-Forwarded-Encrypted: i=1; AJvYcCVVHQ/gOE0NI1ntSOA/c9pr0vfS1hbvKQxARvK4cOH566bwyn6W0138vxuLTCdXX3TCZG+n5LZHFL+xHzQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yy3SyHIp6ncqhL8xCr561Kd8+/3mCEcJpp4XiU1kz+3L/VnIdQ9 Iu8lnPLVKiHmfcXIoeq1AGXcmhKg66j+o4izR9C/ONZAB+EaKFLUrAvxUwYy+l+2ADW2WdYQEhn t0A== X-Google-Smtp-Source: AGHT+IGat+UI+PbiqPp0x4IlhMTMlQ4Wh/0OH9JXwg33xEB/TjQQwBsT7F9d2kQn++dFr13DA3e5CEPT9Os= X-Received: from pjbfr16.prod.google.com ([2002:a17:90a:e2d0:b0:2ea:5c73:542c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:264e:b0:2ee:53b3:3f1c with SMTP id 98e67ed59e1d1-2f83abb4032mr18429535a91.5.1738374928261; Fri, 31 Jan 2025 17:55:28 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:12 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-6-seanjc@google.com> Subject: [PATCH v2 05/11] KVM: nVMX: Consolidate missing X86EMUL_INTERCEPTED logic in L2 emulation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor the handling of port I/O interception checks when emulating on behalf of L2 in anticipation of synthesizing a nested VM-Exit to L1 instead of injecting a #UD into L2. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index fb4e9290e6c4..dba22536eea3 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8007,12 +8007,11 @@ static __init void vmx_set_cpu_caps(void) kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG); } =20 -static int vmx_check_intercept_io(struct kvm_vcpu *vcpu, +static bool vmx_is_io_intercepted(struct kvm_vcpu *vcpu, struct x86_instruction_info *info) { struct vmcs12 *vmcs12 =3D get_vmcs12(vcpu); unsigned short port; - bool intercept; int size; =20 if (info->intercept =3D=3D x86_intercept_in || @@ -8032,13 +8031,9 @@ static int vmx_check_intercept_io(struct kvm_vcpu *v= cpu, * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps. */ if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) - intercept =3D nested_cpu_has(vmcs12, - CPU_BASED_UNCOND_IO_EXITING); - else - intercept =3D nested_vmx_check_io_bitmaps(vcpu, port, size); + return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); =20 - /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */ - return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; + return nested_vmx_check_io_bitmaps(vcpu, port, size); } =20 int vmx_check_intercept(struct kvm_vcpu *vcpu, @@ -8067,7 +8062,9 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu, case x86_intercept_ins: case x86_intercept_out: case x86_intercept_outs: - return vmx_check_intercept_io(vcpu, info); + if (!vmx_is_io_intercepted(vcpu, info)) + return X86EMUL_CONTINUE; + break; =20 case x86_intercept_lgdt: case x86_intercept_lidt: @@ -8079,8 +8076,6 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu, case x86_intercept_str: if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC)) return X86EMUL_CONTINUE; - - /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */ break; =20 case x86_intercept_hlt: @@ -8108,6 +8103,7 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu, break; } =20 + /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */ return X86EMUL_UNHANDLEABLE; } =20 --=20 2.48.1.362.g079036d154-goog From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 8C1B01474CF for ; Sat, 1 Feb 2025 01:55:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374932; cv=none; b=nol9tKWr3KnFZOSYfN+JGDQLLDPOaGNohH/98vnAbVfE5LLEO+tvLQqnZLrhUM2LTo9H1bjMVXbEPujAYfpe3Lhwo6VHuwSgLHS5Xwzf9cF4hgmlMVM66sy3mONT7cEM4oXauKTe+zV043ojhWcWmQFqLVZAAbL4Rw/swxRYuJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374932; c=relaxed/simple; bh=UVo6Z/4dhiFoOhJN3891jfwkXaQHO7XmA6oXNp4w27k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=M8TqozYOYHW7iaaqtcDUTvMw9wacJQpx0KIR26ubuNQvKNajKN7lnL3ZsEQf0BUm9fYeWmCzfIiWoMieLAWlHEwSjfjyNBpPdjbqyxdkgXQ7DxL2zi8NfkN1ViK4BLvEpDCLeW0X+VWMgV7vHbWJ4AAVesfTNwsIsgZca4qGfdE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mzEZ5Vs9; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mzEZ5Vs9" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2166855029eso52187725ad.0 for ; Fri, 31 Jan 2025 17:55:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374930; x=1738979730; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=0INf1hRV37RA1+YvoWbzTJR4dkVI99fKTSGmyPC5dJU=; b=mzEZ5Vs97O31l/9kt1fkZuYauIIbpjCeKMbgYDDnqRfI7tAH+JqYZ7JiGKepum6kSi sMsAOjezG5AT61e8Sg8y7+ftKjTF9N/hO2BQ75xLpS48efhBTLhQkwkxAZMLFcchmTVo ouFvVRijd2AWPVIlEMouW+lTPLJNoVKec7+QduM6BMSgOY/8SXx+1SC+RHtBqguaNAK6 AGTM07VsC/rThvxKV81U6zKgpmlzJUnIRPi+2244yPkt9VGKLBichYKOALxXe314sncB uz23Qfyz4miMcqqCCghzvIECRTgmtlkXasa46tNwY+J46+quTwNxPw+9hwKmOdRfLxCU gy7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374930; x=1738979730; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0INf1hRV37RA1+YvoWbzTJR4dkVI99fKTSGmyPC5dJU=; b=VNNlKfGqV/gnrxZksQ+1DsifkHZq1lm/Q/7xNHNyEYr+BBgyFvfvNjpYvfDgHazGVe PP4/ZQTijzNAZQgPiU0effc9ONcZAjhnt8VXBUCK53AfPR8ONQyp8rOU6IxyJIST1rMe /baLLJt/iOeH0RZiyCJ2HiQ7iVTa8+9fMHluioQBNj4vcUIdGurkzcK7NNN931AQ3cL+ tPitCKI0UG/gNInNnjxLgczcrJgM/MovC+EK+jWWdAGoChGklWWKnax3ZTYsSx7xmD3i wUznHWcuSXkfMdMjevZ1/oawxPJMGmVRF6U5hMju2M6KD2EHk0ZFSjp6kpLfpCzaUVYC eU1w== X-Forwarded-Encrypted: i=1; AJvYcCUXzg4ttXLI+gnZXY/wtbPB+bSsFY3FirGugC4vA1u/iemNwsvVj2BOT8yu+yMYasvW+DJXbNIgTSK8ozQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yxi2eKdlyNiJYbhJn1NGcXiX4uH8SGMU4Wd4bVDoMppurlOMgpe EJTpU6kV6lZzTc7LYzNatwJwH2pyimwzvz/tqNr8FXKbSebk0oOAYPD8wtF4UGXorbYVkV9UT8V rnA== X-Google-Smtp-Source: AGHT+IGLVsvqFq/nTpkDat1Ox1psJO6hj3GlefNPyyLrelICESYBuGk1uv8jjM8KmSylaMLfqRUmiTgQ0Cw= X-Received: from pjbov11.prod.google.com ([2002:a17:90b:258b:b0:2ef:7352:9e97]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d582:b0:215:19ae:77bf with SMTP id d9443c01a7336-21dd7c66949mr218457115ad.19.1738374929971; Fri, 31 Jan 2025 17:55:29 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:13 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-7-seanjc@google.com> Subject: [PATCH v2 06/11] KVM: x86: Plumb the src/dst operand types through to .check_intercept() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When checking for intercept when emulating an instruction on behalf of L2, forward the source and destination operand types to vendor code so that VMX can synthesize the correct EXIT_QUALIFICATION for port I/O VM-Exits. Signed-off-by: Sean Christopherson --- arch/x86/kvm/emulate.c | 2 ++ arch/x86/kvm/kvm_emulate.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 0915b5e8aa71..ca613796b5af 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -477,6 +477,8 @@ static int emulator_check_intercept(struct x86_emulate_= ctxt *ctxt, .dst_val =3D ctxt->dst.val64, .src_bytes =3D ctxt->src.bytes, .dst_bytes =3D ctxt->dst.bytes, + .src_type =3D ctxt->src.type, + .dst_type =3D ctxt->dst.type, .ad_bytes =3D ctxt->ad_bytes, .next_rip =3D ctxt->_eip, }; diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index 73072585e164..49ab8b060137 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -44,6 +44,8 @@ struct x86_instruction_info { u64 dst_val; /* value of destination operand */ u8 src_bytes; /* size of source operand */ u8 dst_bytes; /* size of destination operand */ + u8 src_type; /* type of source operand */ + u8 dst_type; /* type of destination operand */ u8 ad_bytes; /* size of src/dst address */ u64 next_rip; /* rip following the instruction */ }; --=20 2.48.1.362.g079036d154-goog From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 F318514A4D1 for ; Sat, 1 Feb 2025 01:55:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374933; cv=none; b=IpC3ECo8dbOc6FEN5Ms2CVAVTJmhA1mkbsFp/LBHjuiG91seNm/d49w9U9a4eGEABII9X+WAMKXlEy73EAWMAiDacyAF5Op6Nzjs0TVB9TX/Dez5YRY55pmEYcZZoaZWmWR/GM7ZpR8QpyhT3uR9OOIE7unsGmIHWkt0VkJeBbc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374933; c=relaxed/simple; bh=vmEbPbxbMS9aUOXfyT+ZSZRy/eMxMmE8Iua31butyQA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lbsg7mGl/03glSmQFKqkHvwjwryYypZc0ouJtl6ZIQk8Sql3eNVVCr+HYjhQEfUyJrmFQmgCoMKkLgqDqx37TMy5zOmSaMs6OhQmuXx6wVwFcebRUPVliDoPmbsv5kGXxc0/ojDV2KWBrqHtSb8OPogYLFtM7DpbLk1KeC1JDRE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ixcVm+kT; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ixcVm+kT" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2efa0eb9dacso5045276a91.1 for ; Fri, 31 Jan 2025 17:55:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374931; x=1738979731; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=SSq5rYLAYG2vKFAF70mfCTOkmocHERBtCqn9i2vcAtY=; b=ixcVm+kT6Brw2ZztMSq1Qyqza4qGWq2GYtbj9GQ15xl8XlxP79inYPtXDjRukspZ8f BQBwUFx+MGdJQA4lWbdCCEgjY25fvewS4POj/zc9S0w4T8THjpMDNkNa088ALea2BZ12 1sfHiQTshHN6rcm7Vqikk79brVhKjGDzZpDAraJc8CmfOT3bSiHFdpwpK9BG7KYX2xVU npj5+6oIbaviw8k/1FZp6j9z16axRIdh5tEvZLLgEMjfvOahLknWXdYSSL2Xk1tThzDX 5p6rJ0+vej5A6ruM0M7xZg9nvbUEk77sVdZng49Q+g2C7Ld9vbHEXrIEg5h85OQmoRR9 8Ogg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374931; x=1738979731; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SSq5rYLAYG2vKFAF70mfCTOkmocHERBtCqn9i2vcAtY=; b=xQmQoUOhM6vGc0RPVlBGay2ymkPFW4NX44AesieUvJyJCTg80qoTXKoCmWwOEe8M67 0jrma/2yuL6ZQ84Z2rxneOM04LG+zVhrXvAH5IL+fMYOSewLBfTDQcrLL1pRTYqgp18j fMtiudyPu+hz//N/U/5Oe7nhMbRuqDqSrYIjBhhSLtiY+0X2ksYzC3eEuVJtsKth5B3w 9k5847Ff2hkYV3tRgMkHHQM0hw9v+lWmN+VknItggtDAqB7ywXsFEfJZ0tHTGX+K4xQO jnu1s72Gj9V7QjLsb0+D2CnRNaR+qVkvQ8eRDUhYAjNYN6imnzaU2ARNS3K8+NTRZ/qD woZA== X-Forwarded-Encrypted: i=1; AJvYcCVEy7ZqeHyGIx0Xy0QQnLtKWFb5U213tlYwbOAzlBzvlEvj3D6oWMvaZfAkOKnKOVT1g+Uyemu3hIMk4I0=@vger.kernel.org X-Gm-Message-State: AOJu0YzygEBTxB/zlT1IoGpc4/s6vuIsmi/jcXGG86njHG7aIm59+NnK OGJzsLG5o8zbMf26rFLIbN+WpS60O4KEZFqCHeYn1PmOlXda3z33ZDW3qYR+WKaJa05uWzK4pGc qPg== X-Google-Smtp-Source: AGHT+IEFpzfH3dkO4+Y860/dfQ4O1AiD1t9jfO7r11k85kQFFgtWqnFjO6ht6BavxIM9aAIGs8WsY8WQD2s= X-Received: from pjbeu6.prod.google.com ([2002:a17:90a:f946:b0:2f4:47fc:7f17]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:390c:b0:2f8:4a3f:dd37 with SMTP id 98e67ed59e1d1-2f84a3fed3dmr12867960a91.16.1738374931514; Fri, 31 Jan 2025 17:55:31 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:14 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-8-seanjc@google.com> Subject: [PATCH v2 07/11] KVM: x86: Plumb the emulator's starting RIP into nested intercept checks From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When checking for intercept when emulating an instruction on behalf of L2, pass the emulator's view of the RIP of the instruction being emulated to vendor code. Unlike SVM, which communicates the next RIP on VM-Exit, VMX communicates the length of the instruction that generated the VM-Exit, i.e. requires the current and next RIPs. Note, unless userspace modifies RIP during a userspace exit that requires completion, kvm_rip_read() will contain the same information. Pass the emulator's view largely out of a paranoia, and because there is no meaningful cost in doing so. Signed-off-by: Sean Christopherson --- arch/x86/kvm/emulate.c | 1 + arch/x86/kvm/kvm_emulate.h | 1 + 2 files changed, 2 insertions(+) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index ca613796b5af..1349e278cd2a 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -480,6 +480,7 @@ static int emulator_check_intercept(struct x86_emulate_= ctxt *ctxt, .src_type =3D ctxt->src.type, .dst_type =3D ctxt->dst.type, .ad_bytes =3D ctxt->ad_bytes, + .rip =3D ctxt->eip, .next_rip =3D ctxt->_eip, }; =20 diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index 49ab8b060137..35029b12667f 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -47,6 +47,7 @@ struct x86_instruction_info { u8 src_type; /* type of source operand */ u8 dst_type; /* type of destination operand */ u8 ad_bytes; /* size of src/dst address */ + u64 rip; /* rip of the instruction */ u64 next_rip; /* rip following the instruction */ }; =20 --=20 2.48.1.362.g079036d154-goog From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 9F4A535957 for ; Sat, 1 Feb 2025 01:55:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374935; cv=none; b=Ybz5QdfvGiUckWrBwfJNOlK8Ck68eHRfzXkkkKLKdhohfrKJ28p45Anz92zgoejrL7fC9TWHFU2lkSEBnoT13ACWIOFXhLAUGyeRRhUn6q1a3Zhm+N7SJ/XpGgMeawp6aepTWYipzrNdN9nm5eUNxik5mU0p8Hgt0DnQ4jGYJqM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374935; c=relaxed/simple; bh=U3aXYtXvj8rJ6en4q57EqfG8FlNrWEsTovi78JWBpUA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=b9Djie6nsPRwl0Mw7few7foUN9BWigKO4QrCWSanttzB8l0gMLalLUtNI1B/IcVDFR2/YkQy5bOh9tmYvwVr3qDZEmZ9RFpg7gBcyD4fR148i/C8V298RxZyBnqxCXXoar78BHkoptKoluJCrlUS8HsFwgkQ3cQGHEh3PJKbYwQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0dgpIlbX; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0dgpIlbX" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef7fbd99a6so4966842a91.1 for ; Fri, 31 Jan 2025 17:55:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374933; x=1738979733; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=v6o3I4DBxjxkxXVmgva/ae+3lJMpdQxl+ZvTS60XPAM=; b=0dgpIlbXH/nk/f+ntCR66WCWi/dJgWHyos7+TcjRTir2R2E0YiHc8DlenTQu+1AoCr uq2NOpg9P/wetKjvuO1pguoWkFXeI6G5J1GUQiEt9rDvYnfvjdMBRw0Rw0/L1tmdKXHO 0S3+ftzo+1Fa7H9x/zVBzzwnnXWrcKZQ34Tn2R2jWA29wj7Muu6wmphUfzV+J18S5fss 2Vfz13DMInf5peD8VGGJ2Z7H9C27qstEpz03YyCSX0qkbyOd6MOcq5naf5C7xhghRYaO JoeHGYtVV0JanVuyD2xrFXqgsffnonm+sptkugnB0580g+q/MGblVzdJ3Mgx97JwcLU4 MLqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374933; x=1738979733; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=v6o3I4DBxjxkxXVmgva/ae+3lJMpdQxl+ZvTS60XPAM=; b=W/QF3SEOg9mmldSYIwYAyjxGEfKzy2gyxj9LSpooUFFzW2K3Li3Zo1Q+iA+4d1hJo0 5PesZFPJVH/F6W4S7DGLRR4/z5x+WDHZAcSEywfYDwGsyfnYAfEOz9zvx9a15p7NyGfy IUgzD3Bznx4jq8KHJ8AgKvM7980HKw8mUhX/EvBSFRR0sfnazDOEDciJoGYbqUa78uZb 00bPW3TH1cZwVmCm/jRh5N8WLk3eASor7wcqdu+shyaQ1h26FqWR2p4IlzmwcUl5Xyb3 EKPRdhdMf1SEtrRSoFEcapEbTKsFYrFH9qtyxKhC4VT9tjTyw+tbMGh6b6fGRGkZwv39 dSFQ== X-Forwarded-Encrypted: i=1; AJvYcCWN8JZjOVQl3sh7jFEKFQ5E53mhBzmMDJzsQHKKDU23Cgc5gDgiHLQVxC4tZLpeO6Gvqr+y+cgynHck7mE=@vger.kernel.org X-Gm-Message-State: AOJu0Ywzbl+27g25R+tYPhgbUmwBGmbBAU0eVitxsCnCz76tGWASQhNZ iSHTL26GSH0yrVpccInyPORZA7CEGJwAJGeO8jh2zTpqOl3Ud5vLjtp/yKVR1GG+L9GdZo6oIPZ +Ow== X-Google-Smtp-Source: AGHT+IEJyl6b/CyC+v0x5BmLUOl5YD0/ntJw9qfYUQQK6gKPQKvDqtvewlO1PBrsM/VzRTPHCwo9rVN/4f8= X-Received: from pjbpx11.prod.google.com ([2002:a17:90b:270b:b0:2e9:5043:f55b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:3de4:b0:2f8:4589:a305 with SMTP id 98e67ed59e1d1-2f84589a325mr13600663a91.1.1738374932981; Fri, 31 Jan 2025 17:55:32 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:15 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-9-seanjc@google.com> Subject: [PATCH v2 08/11] KVM: x86: Add a #define for the architectural max instruction length From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a #define to capture x86's architecturally defined max instruction length instead of open coding the literal in a variety of places. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/kvm_emulate.h | 4 +++- arch/x86/kvm/trace.h | 14 +++++++------- arch/x86/kvm/vmx/nested.c | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index 35029b12667f..c1df5acfacaf 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -275,8 +275,10 @@ struct operand { }; }; =20 +#define X86_MAX_INSTRUCTION_LENGTH 15 + struct fetch_cache { - u8 data[15]; + u8 data[X86_MAX_INSTRUCTION_LENGTH]; u8 *ptr; u8 *end; }; diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 0b844cb97978..ccda95e53f62 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -830,12 +830,12 @@ TRACE_EVENT(kvm_emulate_insn, TP_ARGS(vcpu, failed), =20 TP_STRUCT__entry( - __field( __u64, rip ) - __field( __u32, csbase ) - __field( __u8, len ) - __array( __u8, insn, 15 ) - __field( __u8, flags ) - __field( __u8, failed ) + __field( __u64, rip ) + __field( __u32, csbase ) + __field( __u8, len ) + __array( __u8, insn, X86_MAX_INSTRUCTION_LENGTH ) + __field( __u8, flags ) + __field( __u8, failed ) ), =20 TP_fast_assign( @@ -846,7 +846,7 @@ TRACE_EVENT(kvm_emulate_insn, __entry->rip =3D vcpu->arch.emulate_ctxt->_eip - __entry->len; memcpy(__entry->insn, vcpu->arch.emulate_ctxt->fetch.data, - 15); + X86_MAX_INSTRUCTION_LENGTH); __entry->flags =3D kei_decode_mode(vcpu->arch.emulate_ctxt->mode); __entry->failed =3D failed; ), diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 8a7af02d466e..fb4fd96ce0f8 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -2970,7 +2970,7 @@ static int nested_check_vm_entry_controls(struct kvm_= vcpu *vcpu, case INTR_TYPE_SOFT_EXCEPTION: case INTR_TYPE_SOFT_INTR: case INTR_TYPE_PRIV_SW_EXCEPTION: - if (CC(vmcs12->vm_entry_instruction_len > 15) || + if (CC(vmcs12->vm_entry_instruction_len > X86_MAX_INSTRUCTION_LENGTH) || CC(vmcs12->vm_entry_instruction_len =3D=3D 0 && CC(!nested_cpu_has_zero_length_injection(vcpu)))) return -EINVAL; --=20 2.48.1.362.g079036d154-goog From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 89DF415746B for ; Sat, 1 Feb 2025 01:55:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374937; cv=none; b=E2eGfWgLZft1tT+NeWh9fBJ2f2lAZnTfJhAFoOBXzxzBb/2DT7xZKEgrSPRWyHrrXwJgrtLfbTNmDSl6KYOeTxjRPXCaQWuiZeYr2gzp+umTYx4mV/yIZ7Gg34XYEpR4KEbzUfZyOZfydGRj7TZ+d40DkxhiD3oJJkEWrsWV4mk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374937; c=relaxed/simple; bh=9iTUooTz+GzVh7vu6TOu+J68+KiYzSjVIC9jQQLaLOA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HfM16FTHRObVtJxKgnCGBFjBykc9thIdlj6qAKbJoamCJk60xygQnJfhJdd+MK8jkVSXtIQJZj5XNCpKMLG6Wn2cFHei1Fz6l3/ejhWMWNMGZL1Lb7U19asb60smNa89YMcs5TfI9BCXOI8ADmM2D9i8BqP1y3UNVQUW9epp71Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GkxcYgxu; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GkxcYgxu" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef6ef86607so7000054a91.0 for ; Fri, 31 Jan 2025 17:55:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374935; x=1738979735; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=3XvAvsURv6o+VBrJhyeGBkEYBU2JVuvbJ6IwloO/TZA=; b=GkxcYgxu0vHUVBHp9OhoP7jzzjcnUoodKe9oW04kojqnxnyVLvpMRB0CzTr6V70GN5 G+K/mKCruldubRQGcOZzIBNkpB8CBULmyHPJ6yMKUvFdR4uvf2ceZjBVL50WGQZL3d97 9w7VQcNJLnOa1RippEhRgNAJoDmKJ1ThW7AhzDcDVkGipMrbNUWzWBRg+B/vqCPlmMNb Dt3axv0NOgefFiZ+fp9H+nFMg/y9/Sz0eURfmCwgyK3gpHF1N8VQHvmFSwxrRfYtvuTm 8zt4obe9/8TrUaGjrAb71tlb3xKz5vzVFtGrpjoUpPrySEHvt7dCObV8jwQUZhqHhosf WB1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374935; x=1738979735; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3XvAvsURv6o+VBrJhyeGBkEYBU2JVuvbJ6IwloO/TZA=; b=kiGJBgCXo7RlA+RWz7OljdqiePTodKmzpK/ot5xBWknkQEbSVzDGMvBIjUQl5ASNFr bgjQcXUKFU+vZbvnGgc715vbn0MHN0XRqncz0pQZV5ByYNLz5uojqWf++ut7uuL1om4F A+e485X+CcT1ekrHb+BR3UUazrJD4X/gR9FOMt+SMKQH5jTgv8WRAVWVaRtNEbatfqYL +2W7RTAeCR0JhuUsfWTIxO+qvMv54ETo06RYtfYFVUbFdRGLlTLNeIpD8ouLVUqmxmiG Z9f0HqI4rqenoumKhsxGjUVbnfggti6tnBcedcZIWklkl9mYP1pjwRWXeYgNImi35fFY 2qHw== X-Forwarded-Encrypted: i=1; AJvYcCVucpRZ9CJ98a+rZVbLYT8jG2iQ07kN5J19edmJDfWw8ga4btAJMek/QY65bE0ejDJV/KBO9ExbWe7Vw0Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yxl4C0nYzyAxPL7LNnCtx5CvlpLG8nBD/AiuWWt6K+tZvWvYnUx 4rq4kE+CWbqPa7gGOwcppeUobKW6W2uiiLO0A1R2En7JQ5l4rp5uR1uqLGkIp+SMRXFrwcjahr/ 51Q== X-Google-Smtp-Source: AGHT+IGOzvDdQI0HEUYlP00LQniZPDx482XdAtwcGW/8CaLpA5IH2YAm75ndxRKBH6ny5DWYhQDp8G6wGwM= X-Received: from pjbeu13.prod.google.com ([2002:a17:90a:f94d:b0:2ef:85ba:108f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:568f:b0:2ea:8aac:6ac1 with SMTP id 98e67ed59e1d1-2f8464056b9mr14855857a91.15.1738374934730; Fri, 31 Jan 2025 17:55:34 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:16 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-10-seanjc@google.com> Subject: [PATCH v2 09/11] KVM: nVMX: Allow the caller to provide instruction length on nested VM-Exit From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rework the nested VM-Exit helper to take the instruction length as a parameter, and convert nested_vmx_vmexit() into a "default" wrapper that grabs the length from vmcs02 as appropriate. This will allow KVM to set the correct instruction length when synthesizing a nested VM-Exit when emulating an instruction that L1 wants to intercept. No functional change intended, as the path to prepare_vmcs12()'s reading of vmcs02.VM_EXIT_INSTRUCTION_LEN is gated on the same set of conditions as the VMREAD in the new nested_vmx_vmexit(). Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 12 +++++++----- arch/x86/kvm/vmx/nested.h | 22 ++++++++++++++++++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index fb4fd96ce0f8..791e00d467df 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4618,7 +4618,7 @@ static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vc= pu, struct vmcs12 *vmcs12) */ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, u32 vm_exit_reason, u32 exit_intr_info, - unsigned long exit_qualification) + unsigned long exit_qualification, u32 exit_insn_len) { /* update exit information fields: */ vmcs12->vm_exit_reason =3D vm_exit_reason; @@ -4646,7 +4646,7 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, str= uct vmcs12 *vmcs12, vm_exit_reason, exit_intr_info); =20 vmcs12->vm_exit_intr_info =3D exit_intr_info; - vmcs12->vm_exit_instruction_len =3D vmcs_read32(VM_EXIT_INSTRUCTION_LEN); + vmcs12->vm_exit_instruction_len =3D exit_insn_len; vmcs12->vmx_instruction_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); =20 /* @@ -4930,8 +4930,9 @@ static void nested_vmx_restore_host_state(struct kvm_= vcpu *vcpu) * and modify vmcs12 to make it see what it would expect to see there if * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) */ -void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, - u32 exit_intr_info, unsigned long exit_qualification) +void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, + u32 exit_intr_info, unsigned long exit_qualification, + u32 exit_insn_len) { struct vcpu_vmx *vmx =3D to_vmx(vcpu); struct vmcs12 *vmcs12 =3D get_vmcs12(vcpu); @@ -4981,7 +4982,8 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_= exit_reason, =20 if (vm_exit_reason !=3D -1) prepare_vmcs12(vcpu, vmcs12, vm_exit_reason, - exit_intr_info, exit_qualification); + exit_intr_info, exit_qualification, + exit_insn_len); =20 /* * Must happen outside of sync_vmcs02_to_vmcs12() as it will diff --git a/arch/x86/kvm/vmx/nested.h b/arch/x86/kvm/vmx/nested.h index 2c296b6abb8c..6eedcfc91070 100644 --- a/arch/x86/kvm/vmx/nested.h +++ b/arch/x86/kvm/vmx/nested.h @@ -26,8 +26,26 @@ void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu); enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *v= cpu, bool from_vmentry); bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu); -void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, - u32 exit_intr_info, unsigned long exit_qualification); +void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, + u32 exit_intr_info, unsigned long exit_qualification, + u32 exit_insn_len); + +static inline void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_re= ason, + u32 exit_intr_info, + unsigned long exit_qualification) +{ + u32 exit_insn_len; + + if (to_vmx(vcpu)->fail || vm_exit_reason =3D=3D -1 || + (vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) + exit_insn_len =3D 0; + else + exit_insn_len =3D vmcs_read32(VM_EXIT_INSTRUCTION_LEN); + + __nested_vmx_vmexit(vcpu, vm_exit_reason, exit_intr_info, + exit_qualification, exit_insn_len); +} + void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu); int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data); int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdat= a); --=20 2.48.1.362.g079036d154-goog From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 1B70A170A2C for ; Sat, 1 Feb 2025 01:55:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374938; cv=none; b=XJDPocROY6/3nkLLW63eP32gA1WrXAOc0+l5ytMWju03xirx6slYU1fxp0SmKdzk2YgnI/OeBWPMx4ct/j2/tYY1C/f45YRCj1OAuDviCi8szBn5OMh8ztZe9dj90r2nhzlC+Py3KkJz3KUjmSVn8Yo1PZ5BfxFyqzwBWJAxOwo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374938; c=relaxed/simple; bh=fM1n6FDclVKHXlvkcF8mqpqAtx9unY5xnqToGnbHTSI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HykbCDXjanwjbTjiGgTrRGIAKuN369KEuDEhLDfja182e2wGydXqLUxoIYRziEUPRgK9zF0YL1LOve8hP5CgVxidiRX9cVOHnkVsJR8jEyB2sgwjmD7v17xONIrG+FnQ+XVqImO9lmvIFBy6i3DU2n9MMoZeXRJTxggfgzmccvk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WZC8sR7k; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WZC8sR7k" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef114d8346so5067772a91.0 for ; Fri, 31 Jan 2025 17:55:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374936; x=1738979736; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=3rffIwZzBf9w84td9LkYxb8GhDME9X7gCVMSc0UnAfs=; b=WZC8sR7k5BetvDrfTeU1Q4m0g6YPFGcOY/l+Hnzl6Vqr0Q0wLiUltxdw5n4TONAreX 11hcStpGpCR9A9jBIIuaFomY9BfYoUUX/WlK47R994X4D0sL72xD12a39+/Ir1ts4lmN YoxJu7AFkkpmKq00QT1i3gf5HAJAjZvFLx1DZJc7Sa2f4hpqZOpVqJVvFBcBFisb6fqn Vxv1qwY4mFH6WbLr5rDRGj8cv8k2Sr6wU9dIqyPw0N4K8Hvzog1raXGDfE5IktH4VOsF Oj92rxUfHQPSFitYYgXVuXzQMvbnouLM5gta9S8oh2VR6J/7hctiFbV0d5uUXx4BvRWK VY2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374936; x=1738979736; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3rffIwZzBf9w84td9LkYxb8GhDME9X7gCVMSc0UnAfs=; b=TNH9PC2ZDST43O+bO2RABHu08R0jYZ8DdyyzoAz1bHnOLmSDGWT89HQBDntyjQgpOt J2dPSeepEqZVYO3SdXkUKoN/NxGtnMEaawFy6srS9FbIyGc18DFehXYQVRiBIGkV+kOf peNYYt502i7Z6NuGTv/1bC2vJ9WnnmcHoTXXznZO9wwnW7c5MfOK833GVuz0/fZjQKoo 46QV2HdFBJuhNlqLaaZCF2JR11Or+TAL4sC2P+8Rx1ktUNXyqamT+TkLhQXSzBiaPQxz 4hGQ1hgxYbTLHcaZZhd/dYRl33q5epnUVjWYxbD6+z1Olx7HjpSC02hgrsUW77W+tZuF tZpw== X-Forwarded-Encrypted: i=1; AJvYcCUbAQU9jIgtIo1UW6vpkyszo5cWI6Y5vW1Bn4MzRCez7xXLO0CUbenqW7cVVHOzl9oK2RasGUTtx8kqSFY=@vger.kernel.org X-Gm-Message-State: AOJu0Yz3hSuy3Lx8O5dCgJx3WuXJTx9OBR5c09iikgTTYiGATXNGoNRc 2H8ZeL41xUqNq22gHsIofO776+fDlOw2n+GvKsZaD5axP7MHgz0jYrj1BqLdxIOa9NRN5LP4AG3 i8w== X-Google-Smtp-Source: AGHT+IGBrZrpg8PhfwimecksOm2WshEEHhnGVFTAyj8Gg4iqO42wRkXAmfH9Cuj7cPTKzFEH7NV75D/W+ME= X-Received: from pjbqi9.prod.google.com ([2002:a17:90b:2749:b0:2ee:4679:4a6b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2709:b0:2ee:693e:ed7c with SMTP id 98e67ed59e1d1-2f83acb10c6mr22641043a91.33.1738374936503; Fri, 31 Jan 2025 17:55:36 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:17 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-11-seanjc@google.com> Subject: [PATCH v2 10/11] KVM: nVMX: Synthesize nested VM-Exit for supported emulation intercepts From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When emulating an instruction on behalf of L2 that L1 wants to intercept, generate a nested VM-Exit instead of injecting a #UD into L2. Now that (most of) the necessary information is available, synthesizing a VM-Exit isn't terribly difficult. Punt on decoding the ModR/M for descriptor table exits for now. There is no evidence that any hypervisor intercepts descriptor table accesses *and* uses the EXIT_QUALIFICATION to expedite emulation, i.e. it's not worth delaying basic support for. To avoid doing more harm than good, e.g. by putting L2 into an infinite or effectively corrupting its code stream, inject #UD if the instruction length is nonsensical. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 70 +++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index dba22536eea3..7b2a6921f156 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8008,20 +8008,13 @@ static __init void vmx_set_cpu_caps(void) } =20 static bool vmx_is_io_intercepted(struct kvm_vcpu *vcpu, - struct x86_instruction_info *info) + struct x86_instruction_info *info, + unsigned long *exit_qualification) { struct vmcs12 *vmcs12 =3D get_vmcs12(vcpu); unsigned short port; int size; - - if (info->intercept =3D=3D x86_intercept_in || - info->intercept =3D=3D x86_intercept_ins) { - port =3D info->src_val; - size =3D info->dst_bytes; - } else { - port =3D info->dst_val; - size =3D info->src_bytes; - } + bool imm; =20 /* * If the 'use IO bitmaps' VM-execution control is 0, IO instruction @@ -8033,6 +8026,30 @@ static bool vmx_is_io_intercepted(struct kvm_vcpu *v= cpu, if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); =20 + if (info->intercept =3D=3D x86_intercept_in || + info->intercept =3D=3D x86_intercept_ins) { + port =3D info->src_val; + size =3D info->dst_bytes; + imm =3D info->src_type =3D=3D OP_IMM; + } else { + port =3D info->dst_val; + size =3D info->src_bytes; + imm =3D info->dst_type =3D=3D OP_IMM; + } + + + *exit_qualification =3D ((unsigned long)port << 16) | (size - 1); + + if (info->intercept =3D=3D x86_intercept_ins || + info->intercept =3D=3D x86_intercept_outs) + *exit_qualification |=3D BIT(4); + + if (info->rep_prefix) + *exit_qualification |=3D BIT(5); + + if (imm) + *exit_qualification |=3D BIT(6); + return nested_vmx_check_io_bitmaps(vcpu, port, size); } =20 @@ -8042,6 +8059,9 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu, struct x86_exception *exception) { struct vmcs12 *vmcs12 =3D get_vmcs12(vcpu); + unsigned long exit_qualification =3D 0; + u32 vm_exit_reason; + u64 exit_insn_len; =20 switch (info->intercept) { case x86_intercept_rdpid: @@ -8062,8 +8082,10 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu, case x86_intercept_ins: case x86_intercept_out: case x86_intercept_outs: - if (!vmx_is_io_intercepted(vcpu, info)) + if (!vmx_is_io_intercepted(vcpu, info, &exit_qualification)) return X86EMUL_CONTINUE; + + vm_exit_reason =3D EXIT_REASON_IO_INSTRUCTION; break; =20 case x86_intercept_lgdt: @@ -8076,11 +8098,25 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu, case x86_intercept_str: if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC)) return X86EMUL_CONTINUE; + + if (info->intercept =3D=3D x86_intercept_lldt || + info->intercept =3D=3D x86_intercept_ltr || + info->intercept =3D=3D x86_intercept_sldt || + info->intercept =3D=3D x86_intercept_str) + vm_exit_reason =3D EXIT_REASON_LDTR_TR; + else + vm_exit_reason =3D EXIT_REASON_GDTR_IDTR; + /* + * FIXME: Decode the ModR/M to generate the correct exit + * qualification for memory operands. + */ break; =20 case x86_intercept_hlt: if (!nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING)) return X86EMUL_CONTINUE; + + vm_exit_reason =3D EXIT_REASON_HLT; break; =20 case x86_intercept_pause: @@ -8096,15 +8132,21 @@ int vmx_check_intercept(struct kvm_vcpu *vcpu, !nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING)) return X86EMUL_CONTINUE; =20 + vm_exit_reason =3D EXIT_REASON_PAUSE_INSTRUCTION; break; =20 /* TODO: check more intercepts... */ default: - break; + return X86EMUL_UNHANDLEABLE; } =20 - /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */ - return X86EMUL_UNHANDLEABLE; + exit_insn_len =3D abs_diff((s64)info->next_rip, (s64)info->rip); + if (!exit_insn_len || exit_insn_len > X86_MAX_INSTRUCTION_LENGTH) + return X86EMUL_UNHANDLEABLE; + + __nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification, + exit_insn_len); + return X86EMUL_INTERCEPTED; } =20 #ifdef CONFIG_X86_64 --=20 2.48.1.362.g079036d154-goog From nobody Mon Feb 9 13:36:58 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 1E15619149F for ; Sat, 1 Feb 2025 01:55:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374940; cv=none; b=aOgnxeTKiRCvTLrZMDSf53uCFAe1KyNhQkqtGX9RapkSSOfQ0ZhYDXnSoztlrI2b/dFwzLkDfgatQzgMGBtd1UCAEAMbWyYYobqHAB+mb8tdZ3cAYH+FM7/7+6oG1X37zt712VeXwnY8DdieFkt2PpVrK4NAzM3/x4DF6F5T5Fk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738374940; c=relaxed/simple; bh=b6etz9i2RP+7QRExjC2SLRQVHjwYvlu/F+QZh/r2q6U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lYJOSRkP+u1cuD2qWH06tMhQ3NVnYNprrQEWpagKckzkiWfxhhUk7asBD4zl6HqyRPfLW8lr+ngoSw8A/pyLI4PGxl8OdD4YlXa+Vmb45JGXxQ5TTxkqZgtxywbmEkmQ+5Iyh9P2mFc9xt3/FEkSTsWGlEL5SF3akw7PrXMnPhs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NP2hW6Ua; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NP2hW6Ua" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f550d28f7dso4950010a91.3 for ; Fri, 31 Jan 2025 17:55:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738374938; x=1738979738; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=P9OmM+sDu38vDrQYwzEU5YwA1UJzt3FjXx/dBaYsJEk=; b=NP2hW6Ua0MdGpog1jd5NlkOEcb5m+DbqxUJ1zUGJAL4Gyr1J1vF8c5mLynFcz1g587 GIFRWkOKjXuqtB6Hofvk2vOFuzdwN88IqArgE1SUGbdvrhV5ijSgJpXLugLbbzGRYiRv mS5KttbvM+6CTw9PIaCSTxxT8Id/27hrUijhy3pU+bXzl2RBdbS9io4WoP2iOewfG7Hu B++b6ESTibM5TKUs+VDqRNJx4DxVrHgKnSy5MJHBW0+5s+RkJJxO64YDvogvc4rjo4jv qCzu4m1DrH3x3U6jNWPXRpPzuq0hHo3zNVq3Oq4YXD6DB7i4Nf5QT93seYy1HWkUT7NN AdGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738374938; x=1738979738; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=P9OmM+sDu38vDrQYwzEU5YwA1UJzt3FjXx/dBaYsJEk=; b=GCGcSCXFcFbTb7kai9rL6x2GH0LGE+eXXBIY9vMe8qXiw2H17GiLjvUWmfkgZtpNQm So9nyE2/bZEfYrnHy/H3G/8pkI6K8YdNk9W3KyZ27NP1y90jGlNGANviWynOPmsTeYrE 5hrfQy0VwLfrpgVfDdHZCMlbaN47Sh5/sDcgHCFU7A3HnL5j8oSwBrI0ezUDzpldsce6 MnkgDAnJl96rVvcY+XOywiXmDVVfKcYOK1B3aLlyACtsGJhfU7enBoPW8l6zHm4YbH/N grENSu52KaNolYUOYgLrsR9GCmSPq7MwgqwZuOe8LzsJcX2VxZdyrdsfWelRzK4i0P4V B2zg== X-Forwarded-Encrypted: i=1; AJvYcCUGwl8nIuGZukRHfl/+VwdUdcgJgLLAHvh1vwXfCFalWWUtxh+HN1CrAWIjM00bj0br1bCKCzeH6EEUGA8=@vger.kernel.org X-Gm-Message-State: AOJu0YxusOuXA9aY7GvzD6AhcRpIMuCFMamrHiYWrrDUBZHHxSdMivtI Ot+AD8HFlkrpY60uh4Km7qPE8qlcHjubVYhrXropcmPt0OB9PRL887vDnVDABkjLNYfkBLb4Kex qPQ== X-Google-Smtp-Source: AGHT+IGdd3i7WLWE6L1sJjjSeejxkxKdfst0PcdT8Ti/xqhwNIREB3iVs/B5jAoGWZX/W33UitjN50Udz8U= X-Received: from pjbeu16.prod.google.com ([2002:a17:90a:f950:b0:2ea:756d:c396]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3f50:b0:2f8:34df:5652 with SMTP id 98e67ed59e1d1-2f83ac1a52fmr18331282a91.21.1738374938398; Fri, 31 Jan 2025 17:55:38 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 31 Jan 2025 17:55:18 -0800 In-Reply-To: <20250201015518.689704-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250201015518.689704-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250201015518.689704-12-seanjc@google.com> Subject: [PATCH v2 11/11] KVM: selftests: Add a nested (forced) emulation intercept test for x86 From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a rudimentary test for validating KVM's handling of L1 hypervisor intercepts during instruction emulation on behalf of L2. To minimize complexity and avoid overlap with other tests, only validate KVM's handling of instructions that L1 wants to intercept, i.e. that generate a nested VM-Exit. Full testing of emulation on behalf of L2 is better achieved by running existing (forced) emulation tests in a VM, (although on VMX, getting L0 to emulate on #UD requires modifying either L1 KVM to not intercept #UD, or modifying L0 KVM to prioritize L0's exception intercepts over L1's intercepts, as is done by KVM for SVM). Since emulation should never be successful, i.e. L2 always exits to L1, dynamically generate the L2 code stream instead of adding a helper for each instruction. Doing so requires hand coding instruction opcodes, but makes it significantly easier for the test to compute the expected "next RIP" and instruction length. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/nested_emulation_test.c | 146 ++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/nested_emulation_test.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index 4277b983cace..f773f8f99249 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -69,6 +69,7 @@ TEST_GEN_PROGS_x86 +=3D x86/hyperv_tlb_flush TEST_GEN_PROGS_x86 +=3D x86/kvm_clock_test TEST_GEN_PROGS_x86 +=3D x86/kvm_pv_test TEST_GEN_PROGS_x86 +=3D x86/monitor_mwait_test +TEST_GEN_PROGS_x86 +=3D x86/nested_emulation_test TEST_GEN_PROGS_x86 +=3D x86/nested_exceptions_test TEST_GEN_PROGS_x86 +=3D x86/platform_info_test TEST_GEN_PROGS_x86 +=3D x86/pmu_counters_test diff --git a/tools/testing/selftests/kvm/x86/nested_emulation_test.c b/tool= s/testing/selftests/kvm/x86/nested_emulation_test.c new file mode 100644 index 000000000000..abc824dba04f --- /dev/null +++ b/tools/testing/selftests/kvm/x86/nested_emulation_test.c @@ -0,0 +1,146 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "vmx.h" +#include "svm_util.h" + +enum { + SVM_F, + VMX_F, + NR_VIRTUALIZATION_FLAVORS, +}; + +struct emulated_instruction { + const char name[32]; + uint8_t opcode[15]; + uint32_t exit_reason[NR_VIRTUALIZATION_FLAVORS]; +}; + +static struct emulated_instruction instructions[] =3D { + { + .name =3D "pause", + .opcode =3D { 0xf3, 0x90 }, + .exit_reason =3D { SVM_EXIT_PAUSE, + EXIT_REASON_PAUSE_INSTRUCTION, } + }, + { + .name =3D "hlt", + .opcode =3D { 0xf4 }, + .exit_reason =3D { SVM_EXIT_HLT, + EXIT_REASON_HLT, } + }, +}; + +static uint8_t kvm_fep[] =3D { 0x0f, 0x0b, 0x6b, 0x76, 0x6d }; /* ud2 ; .a= scii "kvm" */ +static uint8_t l2_guest_code[sizeof(kvm_fep) + 15]; +static uint8_t *l2_instruction =3D &l2_guest_code[sizeof(kvm_fep)]; + +static uint32_t get_instruction_length(struct emulated_instruction *insn) +{ + uint32_t i; + + for (i =3D 0; i < ARRAY_SIZE(insn->opcode) && insn->opcode[i]; i++) + ; + + return i; +} + +static void guest_code(void *test_data) +{ + int f =3D this_cpu_has(X86_FEATURE_SVM) ? SVM_F : VMX_F; + int i; + + memcpy(l2_guest_code, kvm_fep, sizeof(kvm_fep)); + + if (f =3D=3D SVM_F) { + struct svm_test_data *svm =3D test_data; + struct vmcb *vmcb =3D svm->vmcb; + + generic_svm_setup(svm, NULL, NULL); + vmcb->save.idtr.limit =3D 0; + vmcb->save.rip =3D (u64)l2_guest_code; + + vmcb->control.intercept |=3D BIT_ULL(INTERCEPT_SHUTDOWN) | + BIT_ULL(INTERCEPT_PAUSE) | + BIT_ULL(INTERCEPT_HLT); + vmcb->control.intercept_exceptions =3D 0; + } else { + GUEST_ASSERT(prepare_for_vmx_operation(test_data)); + GUEST_ASSERT(load_vmcs(test_data)); + + prepare_vmcs(test_data, NULL, NULL); + GUEST_ASSERT(!vmwrite(GUEST_IDTR_LIMIT, 0)); + GUEST_ASSERT(!vmwrite(GUEST_RIP, (u64)l2_guest_code)); + GUEST_ASSERT(!vmwrite(EXCEPTION_BITMAP, 0)); + + vmwrite(CPU_BASED_VM_EXEC_CONTROL, vmreadz(CPU_BASED_VM_EXEC_CONTROL) | + CPU_BASED_PAUSE_EXITING | + CPU_BASED_HLT_EXITING); + } + + for (i =3D 0; i < ARRAY_SIZE(instructions); i++) { + struct emulated_instruction *insn =3D &instructions[i]; + uint32_t insn_len =3D get_instruction_length(insn); + uint32_t exit_insn_len; + u32 exit_reason; + + /* + * Copy the target instruction to the L2 code stream, and fill + * the remaining bytes with INT3s so that a missed intercept + * results in a consistent failure mode (SHUTDOWN). + */ + memcpy(l2_instruction, insn->opcode, insn_len); + memset(l2_instruction + insn_len, 0xcc, sizeof(insn->opcode) - insn_len); + + if (f =3D=3D SVM_F) { + struct svm_test_data *svm =3D test_data; + struct vmcb *vmcb =3D svm->vmcb; + + run_guest(vmcb, svm->vmcb_gpa); + exit_reason =3D vmcb->control.exit_code; + exit_insn_len =3D vmcb->control.next_rip - vmcb->save.rip; + GUEST_ASSERT_EQ(vmcb->save.rip, (u64)l2_instruction); + } else { + GUEST_ASSERT_EQ(i ? vmresume() : vmlaunch(), 0); + exit_reason =3D vmreadz(VM_EXIT_REASON); + exit_insn_len =3D vmreadz(VM_EXIT_INSTRUCTION_LEN); + GUEST_ASSERT_EQ(vmreadz(GUEST_RIP), (u64)l2_instruction); + } + + __GUEST_ASSERT(exit_reason =3D=3D insn->exit_reason[f], + "Wanted exit_reason '0x%x' for '%s', got '0x%x'", + insn->exit_reason[f], insn->name, exit_reason); + + __GUEST_ASSERT(exit_insn_len =3D=3D insn_len, + "Wanted insn_len '%u' for '%s', got '%u'", + insn_len, insn->name, exit_insn_len); + } + + GUEST_DONE(); +} + +int main(int argc, char *argv[]) +{ + vm_vaddr_t nested_test_data_gva; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + TEST_REQUIRE(is_forced_emulation_enabled); + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86_FEATURE_VMX)= ); + + vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); + vm_enable_cap(vm, KVM_CAP_EXCEPTION_PAYLOAD, -2ul); + + if (kvm_cpu_has(X86_FEATURE_SVM)) + vcpu_alloc_svm(vm, &nested_test_data_gva); + else + vcpu_alloc_vmx(vm, &nested_test_data_gva); + + vcpu_args_set(vcpu, 1, nested_test_data_gva); + + vcpu_run(vcpu); + TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE); + + kvm_vm_free(vm); +} --=20 2.48.1.362.g079036d154-goog