From nobody Mon Apr 13 15:47:43 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B21A4C433FE for ; Tue, 15 Nov 2022 01:29:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231894AbiKOB3g (ORCPT ); Mon, 14 Nov 2022 20:29:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229865AbiKOB3d (ORCPT ); Mon, 14 Nov 2022 20:29:33 -0500 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B836D5FDA for ; Mon, 14 Nov 2022 17:29:31 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id y4so11773724plb.2 for ; Mon, 14 Nov 2022 17:29:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=w9j9azrr5inQKGRnsCPduX09/ehA5bMl/3GVVoPZxxc=; b=MuozMWB88+EewPEyNda8KjeV+i3ykswg0ciKOoKOZKVds7D6FKlZ1PWiILdbrIfKH7 m2WJm9MpxqEJAoLH8TJtxSh7qhUbiuV93o76f4Fki6iYkPp3X3etEV6UWInE5Tynd6y3 QpgbLkWVJ198yRocUG9mJYy10E7Tr89DYG43pHnwJv7sROQN2iwaaePwLu4A1/F1Dpq+ Q6XcYpwyXVsGFiqv34i6wc3WeuQoOQeTk3RkHGocK+500+xfgBlI5GD9JtUC+lOnBbas w/GLYCUq0XzCQwtag0i8nbBmPYyWTYakKYR3Lyu46Pawqv45zbFfnejsAiGEHecCiKLh mEJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=w9j9azrr5inQKGRnsCPduX09/ehA5bMl/3GVVoPZxxc=; b=C4mZFLu4FDLodQZ3dET2/6E/LGNHOHQFnGd5wLM2924LzJGkfE+Bq6W1vaE4p4Fo9w FU4EsMRKShOdg0PepZ2oH2U24NR8HcqP3F8EpIC7luvEcc/4lWWtnEMh7eb+Av906toX r2rJDDAe6rILHx33rLDLffyPAVdDUj8jG6cu7+iJsqor22b2otvObv22fBFWov1SXPhS AHPXEHrU4H2lwx64goydrHNEnshLkwMQvJkrthdfIJP6uyGfRnNWtgATolj8OZpeyLsp 2OrgsRFsVs+L3Sf4v/qZjyykPUesfrqPaItHYwwBlGeXpG5iXy4VchQDrfsPCctlUpgk ud2w== X-Gm-Message-State: ANoB5pnomXSUbAcECMjKOi0pM2EXcO7NX2zLI0vMaQZfCKwexk2PJEw8 UGmB706x9bETirgitpljCxWXjw== X-Google-Smtp-Source: AA0mqf4pZXY8/SPXCsrlGjQCb/4iYLUpAyb1vYtv/I+ZXIRBp6wRswqnXyiHp/HCceOIdg1ojiCc2A== X-Received: by 2002:a17:902:7283:b0:17f:def6:a483 with SMTP id d3-20020a170902728300b0017fdef6a483mr1805442pll.66.1668475771239; Mon, 14 Nov 2022 17:29:31 -0800 (PST) Received: from debug.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id e127-20020a621e85000000b0056bb06ce1cfsm7587231pfe.97.2022.11.14.17.29.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Nov 2022 17:29:30 -0800 (PST) From: Deepak Gupta To: debug@rivosinc.com Cc: aou@eecs.berkeley.edu, jan.kiszka@siemens.com, kbingham@kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, palmer@dabbelt.com, paul.walmsley@sifive.com Subject: [PATCH v2] scripts/gdb: add lx_current support for riscv Date: Mon, 14 Nov 2022 17:29:17 -0800 Message-Id: <20221115012917.1781185-1-debug@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221111195938.1499148-2-debug@rivosinc.com> References: <20221111195938.1499148-2-debug@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" csr_sscratch CSR holds current task_struct address when hart is in user spa= ce. trap handler on entry spills csr_sscratch into "tp" (x2) register and zeroe= s out csr_sscratch CSR. trap handler on exit reloads "tp" with expected user mode= value and place current task_struct address again in csr_scratch CSR. This patch assumes "tp" is pointing to task_struct. If value in csr_scratch= is numerically greater than "tp" then it assumes csr_scratch is correct address of current= task_struct. This logic holds when - hart is in user space, "tp" will be less than csr_scratch. - hart is in kernel space but not in trap handler, "tp" will be more tha= n csr_scratch. - hart is executing trap handler - "tp" is still pointing to user mode but csr_scratch contains p= tr to task_struct. numerically higher. - "tp" is now pointing to task_struct but csr_scratch now contai= ns either 0 or numerically smaller value. Patch also adds new cached type "ulong" in scripts/gdb/linux/utils.py Signed-off-by: Deepak Gupta Reviewed-by: Andrew Jones --- scripts/gdb/linux/cpus.py | 15 +++++++++++++++ scripts/gdb/linux/utils.py | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/scripts/gdb/linux/cpus.py b/scripts/gdb/linux/cpus.py index 15fc4626d236..fd818d7896ce 100644 --- a/scripts/gdb/linux/cpus.py +++ b/scripts/gdb/linux/cpus.py @@ -173,6 +173,21 @@ def get_current_task(cpu): else: raise gdb.GdbError("Sorry, obtaining the current task is not = allowed " "while running in userspace(EL0)") + elif utils.is_target_arch("riscv"): + current_tp =3D gdb.parse_and_eval("$tp") + scratch_reg =3D gdb.parse_and_eval("$sscratch") + + # by default tp points to current task + current_task =3D current_tp.cast(task_ptr_type) + =20 + # scratch register is set 0 in trap handler after entering kernel. + # When hart is in user mode, scratch register is pointing to task= _struct. + # So when scratch register holds higher value (negative address a= s ulong is bigger value) than tp, + # then use scratch register + if (scratch_reg.cast(utils.get_ulong_type()) > current_tp.cast(u= tils.get_ulong_type())): + current_task =3D scratch_reg.cast(task_ptr_type) + =20 + return current_task.dereference() else: raise gdb.GdbError("Sorry, obtaining the current task is not yet " "supported with this arch") diff --git a/scripts/gdb/linux/utils.py b/scripts/gdb/linux/utils.py index 1553f68716cc..ddaf3089170d 100644 --- a/scripts/gdb/linux/utils.py +++ b/scripts/gdb/linux/utils.py @@ -35,12 +35,17 @@ class CachedType: =20 =20 long_type =3D CachedType("long") +ulong_type =3D CachedType("ulong") atomic_long_type =3D CachedType("atomic_long_t") =20 def get_long_type(): global long_type return long_type.get_type() =20 +def get_ulong_type(): + global ulong_type + return ulong_type.get_type() + def offset_of(typeobj, field): element =3D gdb.Value(0).cast(typeobj) return int(str(element[field].address).split()[0], 16) --=20 2.25.1