From patchwork Tue Nov 15 22:10:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 20609 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2967752wru; Tue, 15 Nov 2022 14:18:50 -0800 (PST) X-Google-Smtp-Source: AA0mqf58amNZaRF2w5vKg6LKzixRRQ/b/eVkzCbrTLu53LrORCnCBd/LB+dfBD8j3uyjXLcrspGy X-Received: by 2002:a63:511b:0:b0:438:f2ce:2379 with SMTP id f27-20020a63511b000000b00438f2ce2379mr17402106pgb.333.1668550729833; Tue, 15 Nov 2022 14:18:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668550729; cv=none; d=google.com; s=arc-20160816; b=KFsLZZzy+yNWdVferq1i4FkUAJrpMIBZU+rlBe9A4RRS8VYzmaTaEEjXtSxI/DUSK0 l7nycrzVCIRbniM1B3UYOgGm+KBM4TKldXxvdFNGr6qofdo4+pa2/tgFRQhtaf3HuukD uuUGloHZVRsM+pwxJMjRRe3s0vagTLP5ypgyCLN8pIFzdfBWyEQ0EVPT9LL97LLoRck/ SYSBLjwFXXiVeTTzuFNDXjUpvgzg45dX2jtC8CR+mi2BtT8HtE9ud3Ngy03ioSDe/lVd 8Q1MN/CzISG+sJar6zAWAfTSHwE9ssb5aGhPlNBY6iy+P2J+/48m6Xk2h+B30+W/vwZh WA7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MkMnn2hGaVIkC9WoaV7C4n+L3kmdU9eKYGAAaB0CkLs=; b=W/CU4/4ju/Elr55tA//ifvCiP6sZEPNRdpsz2CqECgEpuWWuLd+26eKFhZP8ITTAWm r5aQ8w/Z5cCmzRhdnBfnRIUbRCCRylvhwvFb7/5SqCxPQjVVIL/uTLamoTV/TmEm9ngr fsol9AY8gPmZZm4nOCtr/aoO4p2Qg76y6KUaD7ScIo72PWb9U2zoCbY3WMSozZxtoFis /q4WST0UnnmGcBNZ7SeZ0m9z4NSjRBnjr59RISTOP+5KaQuFAVzx7YfWLIqP3cOf9e6S lbIrLBS7V/DqMBpwJ56GL7g3xSIlov5SCYV1O67xw5qef8HJwi2Qg3DuOFQQveQ/dqPT xt7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=omrubLvB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x17-20020a1709027c1100b0018863e1bc39si12095921pll.248.2022.11.15.14.18.35; Tue, 15 Nov 2022 14:18:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=omrubLvB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229724AbiKOWLg (ORCPT + 99 others); Tue, 15 Nov 2022 17:11:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231211AbiKOWLa (ORCPT ); Tue, 15 Nov 2022 17:11:30 -0500 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3333F23E9E for ; Tue, 15 Nov 2022 14:11:29 -0800 (PST) Received: by mail-pf1-x42a.google.com with SMTP id k22so15596473pfd.3 for ; Tue, 15 Nov 2022 14:11:29 -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=MkMnn2hGaVIkC9WoaV7C4n+L3kmdU9eKYGAAaB0CkLs=; b=omrubLvBhU67yWHuCUt157MtsLyqmOxlOY90kmUhIC+fxrSEJuMAhD3Q3dstqlRjRs QDAPvF0t3LvqGN2e/OQhw0tUQy9A7AIZfA9uibwsGGGcZcB2sFokQc5YWbymwn6sXU+D ua3URoZeBOQ1ejBwqsq8SFUbz9hL3lrJVgU692VI9J9wfdd9CwnTx/LIBlUz74GeC7i3 5ei/ujLx0cfYNc0oAsg4vsKAMZLylCiUfzf1O7vSSMvITeHvsvZ+xyHJXkHPeyMYdFxC HxVFUVlB+mMdnbKusTfnG0V8VZGNMC+q7lq0AAvHDCYWZSijG43qQ2QOTplhFbBEsYKn resA== 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=MkMnn2hGaVIkC9WoaV7C4n+L3kmdU9eKYGAAaB0CkLs=; b=E+nlJMar2L2DgO1Sc1gXRBTudQqoLEQacarUGrkVmCq0ftz9ezgu8jwPLpYUVtY1Kb 9m7jwKL3jNaxohu2+SVDILYAciNtJQg53p+DIUCYdncEga2EMHQFOhBoWzqnUdjGhJwq q24nqfCxkg462eRG8OV8VDkZ9824RxpbiBv6IzkUA4r+bf4wMNuULQZm8Xx6bcWO/W6O lNb/fwlsfBCuS0Mjq30l6BzbJgBTu4TPqc3D6b6wJPkZ83sW98atihERCf9rux25fnux ZpdpEhfou0OIXst4r/SCAu9mS0LBgKK46DCvSXuMw92chkniZDAojvp9COnfGDoYRJ0n g5RQ== X-Gm-Message-State: ANoB5plwKjERjpfrVIklB7GUudrO8XEMwbY3B2v1/ENHxn9QQnI+mOZ6 +Tfy8Xz1A5UJ7oRa/vnK4rdOmw== X-Received: by 2002:a63:b14:0:b0:470:18d4:f18d with SMTP id 20-20020a630b14000000b0047018d4f18dmr17900349pgl.295.1668550288686; Tue, 15 Nov 2022 14:11:28 -0800 (PST) Received: from debug.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id t6-20020a170902e84600b001785fa792f4sm10534216plg.243.2022.11.15.14.11.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 14:11:28 -0800 (PST) From: Deepak Gupta To: conor.dooley@microchip.com Cc: ajones@ventanamicro.com, aou@eecs.berkeley.edu, debug@rivosinc.com, 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 v5] scripts/gdb: add lx_current support for riscv Date: Tue, 15 Nov 2022 14:10:51 -0800 Message-Id: <20221115221051.1871569-1-debug@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749523804126868167?= X-GMAIL-MSGID: =?utf-8?q?1749602250249525038?= csr_sscratch CSR holds current task_struct address when hart is in user space. Trap handler on entry spills csr_sscratch into "tp" (x2) register and zeroes out csr_sscratch CSR. Trap handler on exit reloads "tp" with expected user mode value and place current task_struct address again in csr_sscratch CSR. This patch assumes "tp" is pointing to task_struct. If value in csr_sscratch is numerically greater than "tp" then it assumes csr_sscratch is correct address of current task_struct. This logic holds when - hart is in user space, "tp" will be less than csr_sscratch. - hart is in kernel space but not in trap handler, "tp" will be more than csr_sscratch (csr_sscratch being equal to 0). - hart is executing trap handler - "tp" is still pointing to user mode but csr_sscratch contains ptr to task_struct. Thus numerically higher. - "tp" is pointing to task_struct but csr_sscratch now contains either 0 or numerically smaller value (transiently holds user mode tp) Patch also adds new cached type "ulong" in scripts/gdb/linux/utils.py Signed-off-by: Deepak Gupta Reviewed-by: Andrew Jones Reviewed-by: Palmer Dabbelt Acked-by: Palmer Dabbelt Tested-by: Hsieh-Tseng Shen --- Since patch has changed a little bit from v1 and I didn't include changelog earlier, here it is. v1 --> v2: - added logic to locate task_struct irrespective of priv - made locating task_struct agnostic to bitness(32 vs 64). - added caching of ulong type in scripts/gdb/linux/utils.py - added more descriptive commit message v2 --> v3: - amended commit message and source line to fit column width v3 --> v4: - amended commit message and remove whitespace in source - added Reviewed-by for reviewers v4 --> v5: - changing the order of changelog and sign off/review tags in commit --- --- 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..14c22f82449b 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 = gdb.parse_and_eval("$tp") + scratch_reg = gdb.parse_and_eval("$sscratch") + + # by default tp points to current task + current_task = current_tp.cast(task_ptr_type) + + # scratch register is set 0 in trap handler after entering kernel. + # When hart is in user mode, scratch register is pointing to task_struct. + # and tp is used by user mode. So when scratch register holds larger value + # (negative address as ulong is larger value) than tp, then use scratch register. + if (scratch_reg.cast(utils.get_ulong_type()) > current_tp.cast(utils.get_ulong_type())): + current_task = scratch_reg.cast(task_ptr_type) + + 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: long_type = CachedType("long") +ulong_type = CachedType("ulong") atomic_long_type = CachedType("atomic_long_t") def get_long_type(): global long_type return long_type.get_type() +def get_ulong_type(): + global ulong_type + return ulong_type.get_type() + def offset_of(typeobj, field): element = gdb.Value(0).cast(typeobj) return int(str(element[field].address).split()[0], 16)