From nobody Mon Oct 6 19:09:16 2025 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.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 793AC2405E7 for ; Thu, 17 Jul 2025 23:47:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752796078; cv=none; b=ccQdeBAPBkufwu+mqNdu0p4cOlMZO83xRwhCUUly88d+7FIpWRPyvshAthNefhPvBezsbAc96k+vWtFjHmuLC7jn8YYDo3HX3rkiRZdhQFZA7kVev1rLgMGApFMI29xIC2RI3UNaX+rkrqftKETSqqvJEyFXb1Y/L22nm6cCz+M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752796078; c=relaxed/simple; bh=tdO3rvbLSuTpzfP82KhIVxFo9mNfKAFeekY7qAlj770=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aqvNsNr4fgrAPr7Lp8g4b/7yp0K/cwFTPOwLrXDAWbAjH7CZoU01lCMBXzIvVvHnqPUG+Bt/yiJmw3rWBAyeq57xBouOMRLrA9jvIsBtRVyN7b67QZP72o/7YXuG+DP+KEAFV4nffoJa13E1Ti5DgC3ZQjqxXcsq/icki1wnlFI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=odRQC4sq; arc=none smtp.client-ip=209.85.221.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--smostafa.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="odRQC4sq" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-3a50816ccc6so908228f8f.1 for ; Thu, 17 Jul 2025 16:47:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752796075; x=1753400875; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=LvbtjEj18PWGX0x1t0phF/x8ckso/td4YZy4kcOPVcw=; b=odRQC4sqgINLyadFf42rK3QSpSU+AIAPjxdFu/BOm6jmTzP8jA3aORS9jy1xaSC5tk bl2FAyJoS9c6VnfhLbz5Pk7K2nn3w6v/9yYIpzFqWamYScNgVv6ed9yNcogSk8kCgBJO ilHqmunuE1oePox85TPrAelc8XWhYck31cUFONUkNNEPz0y+38fx8mzQR8/QOPIp39Bc C1baMdZdtxJju/hLgfR7YJICiJLW7NMoMeE/6Y8pLdkRp/or9nGwvtsG9hR1SyJfxofC q7Su2H6sghfoPtYMy+m0Af/TMSTWxS3oRF6MKODmzkJRLh5LcBYeenq4TeWRwVKLKd8l Up0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752796075; x=1753400875; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=LvbtjEj18PWGX0x1t0phF/x8ckso/td4YZy4kcOPVcw=; b=G1DVdgR5cixARs1+MVb5GD3EUD88UPi244j1aDCpizLVJ69jXhG9H7sQaE67qm9h2X 83MVygnNEH0TYioo0BCQDnVO9zbcxc0ReJK+L1iEaaXMNPnr1omYSG6eIOkmN6LLwFak Yj+B/2u45Y/gct+TkpQUbRseGFfFFMGyLh8BB0CMWd2TDf2NoePyc25C/jMNnyMuI2Q6 o3LzMqgVHFTroghzYAoPrh13iQ4ACTtO8I/1bYcmoaSXr3lVKcngRt/HT2irh2TsS97L Ji4lSc3d3B29JK389tONL1mChJmlh+LKRkpdG/ZrVA0nR2NFtiu/p0kaF5Cnx5SNGdex g+rw== X-Forwarded-Encrypted: i=1; AJvYcCX8SyecJS7juL58++F0o7KMcR9SXP/yjmL1iVoCkfmSPmIG+6D3DPWa0TDzAKu2DBFVs87Hh90h41NsZ8w=@vger.kernel.org X-Gm-Message-State: AOJu0YwQqGoio+YC6+iom5h2+UiGXVKFh+cJcWSfPBQiok7Ixc7j1Lgy 5gukhSdM6C43TxlySV3XMh8s7oPQvtB+1yq7HeQMeQSEoOm5L+uWzhkloK6K1gei/W84vw8sRTb E5qg2izq/mIsQrg== X-Google-Smtp-Source: AGHT+IEqpHOI0zrJb9yR1cFU4cC/jK8HJFkKzvKnqDRgTErFGhbJbdyVNV2Vo7dJPqX0P3eL6UZg+etWnzq0eA== X-Received: from wmbek11.prod.google.com ([2002:a05:600c:3ecb:b0:450:def3:7fc6]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:430f:b0:3a5:2e84:cc7b with SMTP id ffacd0b85a97d-3b60e4c94b7mr6619655f8f.11.1752796074851; Thu, 17 Jul 2025 16:47:54 -0700 (PDT) Date: Thu, 17 Jul 2025 23:47:43 +0000 In-Reply-To: <20250717234744.2254371-1-smostafa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717234744.2254371-1-smostafa@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717234744.2254371-2-smostafa@google.com> Subject: [PATCH 1/2] KVM: arm64: Dump instruction on hyp panic From: Mostafa Saleh To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev Cc: catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, qperret@google.com, keirf@google.com, Mostafa Saleh Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Similar to the kernel panic, where the instruction code is printed, we can do the same for hypervisor panics. This patch does that only in case of =E2=80=9CCONFIG_NVHE_EL2_DEBUG=E2=80= =9D or nvhe. The next patch adds support for pKVM. Also, remove the hardcoded argument dump_kernel_instr(). Signed-off-by: Mostafa Saleh Reviewed-by: Kunwu Chan Tested-by: Kunwu Chan --- arch/arm64/include/asm/traps.h | 1 + arch/arm64/kernel/traps.c | 20 +++++++++++++------- arch/arm64/kvm/handle_exit.c | 5 +++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/traps.h b/arch/arm64/include/asm/traps.h index 82cf1f879c61..0d7e86a95d62 100644 --- a/arch/arm64/include/asm/traps.h +++ b/arch/arm64/include/asm/traps.h @@ -30,6 +30,7 @@ void arm64_force_sig_mceerr(int code, unsigned long far, = short lsb, const char * void arm64_force_sig_ptrace_errno_trap(int errno, unsigned long far, const= char *str); =20 int early_brk64(unsigned long addr, unsigned long esr, struct pt_regs *reg= s); +void dump_instr(unsigned long addr); =20 /* * Move regs->pc to next instruction and do necessary setup before it diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c index 9bfa5c944379..d692c05e3686 100644 --- a/arch/arm64/kernel/traps.c +++ b/arch/arm64/kernel/traps.c @@ -149,15 +149,11 @@ pstate_check_t * const aarch32_opcode_cond_checks[16]= =3D { =20 int show_unhandled_signals =3D 0; =20 -static void dump_kernel_instr(const char *lvl, struct pt_regs *regs) +void dump_instr(unsigned long addr) { - unsigned long addr =3D instruction_pointer(regs); char str[sizeof("00000000 ") * 5 + 2 + 1], *p =3D str; int i; =20 - if (user_mode(regs)) - return; - for (i =3D -4; i < 1; i++) { unsigned int val, bad; =20 @@ -169,7 +165,17 @@ static void dump_kernel_instr(const char *lvl, struct = pt_regs *regs) p +=3D sprintf(p, i =3D=3D 0 ? "(????????) " : "???????? "); } =20 - printk("%sCode: %s\n", lvl, str); + printk(KERN_EMERG "Code: %s\n", str); +} + +static void dump_kernel_instr(struct pt_regs *regs) +{ + unsigned long addr =3D instruction_pointer(regs); + + if (user_mode(regs)) + return; + + dump_instr(addr); } =20 #define S_SMP " SMP" @@ -190,7 +196,7 @@ static int __die(const char *str, long err, struct pt_r= egs *regs) print_modules(); show_regs(regs); =20 - dump_kernel_instr(KERN_EMERG, regs); + dump_kernel_instr(regs); =20 return ret; } diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 453266c96481..de12b4d4bccd 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -565,6 +565,11 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr,= u64 spsr, /* Dump the nVHE hypervisor backtrace */ kvm_nvhe_dump_backtrace(hyp_offset); =20 + /* Dump the faulting instruction */ + if (!is_protected_kvm_enabled() || + IS_ENABLED(CONFIG_NVHE_EL2_DEBUG)) + dump_instr(panic_addr + kaslr_offset()); + /* * Hyp has panicked and we're going to handle that by panicking the * kernel. The kernel offset will be revealed in the panic so we're --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:16 2025 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.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 5ABB424BBEB for ; Thu, 17 Jul 2025 23:47:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752796080; cv=none; b=EhR4QNJQPzP3qf9/9BpSPUXiLlcfCMo5s8yyc3NotzTb4FJkdN3qoduT+ApLLgVhbEZzLgsuQMJo0GwN4LvL9IXe80gffqs9hZfsY+ALCFkk26tYNOXnGr/BCUITX+6I7WML6nlAbTm8UFRDUfz9otdfecPbKVoBuISI0DumEjw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752796080; c=relaxed/simple; bh=4Lm6rMZZKj9i8zZ/NnoVMfxriOkG8OOdGzwZV10mDmc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pAJD9O5dbcXA1/+HR66FVQDGd7r5nUXSt33s9EZnHIUalJ3e/0Ye57/OZB/V5OaE4WW14kVrQmXubwg022oS9IKI8VF8eC+FMMzfgmc2xpAPcUfuI/IeV6Mv0/UrVDDwhNArWlJVzl5N8jJabiebeSfPLJsqUJmRyagVxAbjps8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HQApwqHb; arc=none smtp.client-ip=209.85.221.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--smostafa.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HQApwqHb" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3a6d1394b07so996208f8f.3 for ; Thu, 17 Jul 2025 16:47:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752796077; x=1753400877; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=i36b1JU3bh8bkcjM27fcG1Vv3a5VEOWwHxSUo3VGonw=; b=HQApwqHb7AimiOpZoYWcuhZ6JoYNLHZXd8SC/9/756W37O3eSzavwtome/FhMNlWCD iCs1yQQS3RaAn3FeoYL8WWgiEJNT7ru9bbKuDxkvJVFjIMmtF7MRfdmnwF2RXJyXJIFH +VM87p51U8ExC+Z0U+lCaOaHNqQroTPdU156hoOBn5Aemp9hjFFqIsYE4PY/KFH4k659 PcR5p8BNR+WQHlrcbSsMLMZFTlb5AnbcEYnVWZwMCXYICc9ANuzM7g+Gg71+O/ioanAK li3euIz7knSnWw0NAFMwoIGru+EeLjrUowfHNZm9vfCCQJdp/C8iqqmmfvh5jlZ+8lt5 liDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752796077; x=1753400877; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=i36b1JU3bh8bkcjM27fcG1Vv3a5VEOWwHxSUo3VGonw=; b=UQkUneaW079z1rZHhpPH+BH4AdheQqLOb5DRo4qRXO/3WkWnKzPr6jm4Irl1RH17VP DBd552/pzjBzOPBk8YeNlFjMLYLiOiGi7yPzXVGu4i9g+sortIDC1HTcBKm/OmfvmTts 7YCRAKkdZqZ0sjpmz4qu2XOANWqjwZoDFfORlYrUMz4je8n0dXiBG6Vm0GBcWPjVrY2o sIviRuAVJBHxaXIPMxq6A+Fbd5jzGKB9RUSTXOoIQiXMY4g3P86ToLpufG/9tQLAN+Qf PNbCYN8IdNAVpbL9T1o38xv92jkM5SrzNfxYI3bbqtmUE/+fLGwtZjjPOgw35WmdLNn/ 1ssg== X-Forwarded-Encrypted: i=1; AJvYcCXWjg+HlwnLACxJz0jTrVWRu4DlTvako3kZ94Mrb73bQ6z6dajFielFx1js2lq/STioVnOLmubMu823n+A=@vger.kernel.org X-Gm-Message-State: AOJu0YxCYQHbFCtRzxcZlVtg08SjuOWBQklQAWryqtVluEsTlJRzilb7 o9oKy502qY2A315kj/vcTFBiJs704XXtA1HB+Z2vGuprQQWX3PZBc2Ca6bJBBZlwvIrrG8kYPtj MEyimCWM7TY6roQ== X-Google-Smtp-Source: AGHT+IHKyMuHhag8ZOByJFmk1Ifp+SWFFmJZZLFql4fjdBJ3bTCSJXcxMK4YsTuE1B/9R5Fp15TBOEDcXOc6wQ== X-Received: from wmbdz10.prod.google.com ([2002:a05:600c:670a:b0:450:cf3f:2a89]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a5d:5d10:0:b0:3a5:5270:c38f with SMTP id ffacd0b85a97d-3b60dd0db62mr8326635f8f.0.1752796076663; Thu, 17 Jul 2025 16:47:56 -0700 (PDT) Date: Thu, 17 Jul 2025 23:47:44 +0000 In-Reply-To: <20250717234744.2254371-1-smostafa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717234744.2254371-1-smostafa@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717234744.2254371-3-smostafa@google.com> Subject: [PATCH 2/2] KVM: arm64: Map hyp text as RO and dump instr on panic From: Mostafa Saleh To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev Cc: catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, qperret@google.com, keirf@google.com, Mostafa Saleh Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Map the hyp text section as RO, there are no secrets there and that allows the kernel extract info for debugging. As in case of panic we can now dump the faulting instructions similar to the kernel. Signed-off-by: Mostafa Saleh Acked-by: Will Deacon --- arch/arm64/kvm/handle_exit.c | 4 +--- arch/arm64/kvm/hyp/nvhe/setup.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index de12b4d4bccd..d59f33c40767 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -566,9 +566,7 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, = u64 spsr, kvm_nvhe_dump_backtrace(hyp_offset); =20 /* Dump the faulting instruction */ - if (!is_protected_kvm_enabled() || - IS_ENABLED(CONFIG_NVHE_EL2_DEBUG)) - dump_instr(panic_addr + kaslr_offset()); + dump_instr(panic_addr + kaslr_offset()); =20 /* * Hyp has panicked and we're going to handle that by panicking the diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setu= p.c index a48d3f5a5afb..90bd014e952f 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -192,6 +192,7 @@ static int fix_host_ownership_walker(const struct kvm_p= gtable_visit_ctx *ctx, enum pkvm_page_state state; struct hyp_page *page; phys_addr_t phys; + enum kvm_pgtable_prot prot; =20 if (!kvm_pte_valid(ctx->old)) return 0; @@ -210,11 +211,18 @@ static int fix_host_ownership_walker(const struct kvm= _pgtable_visit_ctx *ctx, * configured in the hypervisor stage-1, and make sure to propagate them * to the hyp_vmemmap state. */ - state =3D pkvm_getstate(kvm_pgtable_hyp_pte_prot(ctx->old)); + prot =3D kvm_pgtable_hyp_pte_prot(ctx->old); + state =3D pkvm_getstate(prot); switch (state) { case PKVM_PAGE_OWNED: set_hyp_state(page, PKVM_PAGE_OWNED); - return host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HYP); + /* hyp text is RO in the host stage-2 to be inspected on panic. */ + if (prot =3D=3D PAGE_HYP_EXEC) { + set_host_state(page, PKVM_NOPAGE); + return host_stage2_idmap_locked(phys, PAGE_SIZE, KVM_PGTABLE_PROT_R); + } else { + return host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HYP); + } case PKVM_PAGE_SHARED_OWNED: set_hyp_state(page, PKVM_PAGE_SHARED_OWNED); set_host_state(page, PKVM_PAGE_SHARED_BORROWED); --=20 2.50.0.727.gbf7dc18ff4-goog