Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1583424rdh; Fri, 27 Oct 2023 21:59:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEsu0rnUqDAt/h++u0GJNi5uDiyh31uP0jivythWI33IX7sJRACw/xzTqlgO3Lu4Ym7997d X-Received: by 2002:a17:902:face:b0:1ca:3cd7:eadb with SMTP id ld14-20020a170902face00b001ca3cd7eadbmr4781120plb.9.1698469145150; Fri, 27 Oct 2023 21:59:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698469145; cv=none; d=google.com; s=arc-20160816; b=NQyV/dHx8owfIXm0VihTMYvBNoV2xbd8SoU0WqVMBN10vT5qicZktjrIG0gZnXe5gR WTh/FRPcy3HyT82G+2yU+hZmUaHQv5jHrvWmFIRFFrZHG++dCdmuBwAuVkJz1BBtg1iQ 3HaNNtRfbOk7FsQ/XyFYtZXi1eJHkkua0na3JYwmgp+N+MCszCwA2hr5GviS/XS+mrkd DE9G6NnDXctX9S1UyMlFtIWNwgoLKkI4M8BPcmtXQa9JZUy3gvrJw4G36SuJw86rNXPc x+CvfcidAj+pYQ02KXvp0XZ7EYYydzUNS+aFGueWkI0xx54kWYm99JDgEwyp5O02E5ap kslw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=8q790Td8gv55CpXw6jh1r5+WUtiBYMYeRT8D2CcfTbo=; fh=N1VvDTD+d8Y9JXXQbS0rwoOea9a4TJlv54FOJnfGlKU=; b=a42ipfCGb6QQV6u/alWiMKxkEO8OhBf/+qfVWc+ytbrHtm/98pdUxYxd+y7ll6Fp6K i17L8UQVn1wU0IzYDEB2TSdlpGKq8E0+bg2VFmTI/0GxS0hyPvzVtTeK9WD+GNIp8WwL sruRYCC6/yzGgB5mow71r4BdoVWElfoPZAgqMJLmV5s7XOBIJcZmNhH+RNwrl5cl2EzU EIW1sdQ4/2e+kjFUwzsQUQmYukA6JnZsou7wzzkrxXRhXeZzIFulQOk+rL7SBCs/WaDv RMqSzuas6qQZrJ/DtcYxs6494DpsZ37cBO2bcNBcAn0SqDw7Cifphzmr/rCRa/LoZjAH MiLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Gimxlkxv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id i7-20020a170902c94700b001c74d83470bsi2129884pla.206.2023.10.27.21.59.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 21:59:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Gimxlkxv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 5C97E81EB9FC; Fri, 27 Oct 2023 21:59:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229484AbjJ1E6f (ORCPT + 99 others); Sat, 28 Oct 2023 00:58:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229468AbjJ1E6d (ORCPT ); Sat, 28 Oct 2023 00:58:33 -0400 Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD790119 for ; Fri, 27 Oct 2023 21:58:30 -0700 (PDT) Received: by mail-oi1-x235.google.com with SMTP id 5614622812f47-3b3f938331fso1368718b6e.1 for ; Fri, 27 Oct 2023 21:58:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698469110; x=1699073910; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=8q790Td8gv55CpXw6jh1r5+WUtiBYMYeRT8D2CcfTbo=; b=Gimxlkxv4ucXVM3PXcTTGFaF4z8adZA2Nh/yTWxeBV83ua9tdQe7cjVleruGt3SLWj aIAQ6S+iT9Q+JgrTRM0yF8ajQ8W2KORySlFdtTjqkM/4/bFhF34NBaKE6Htw2VH6570R FFzXwuIeruMEN9weRyAQC5rk9O4jdIqbuoafXbBmagX+Wv8XT2PJfLb2E9qH0V/J37XF foPxjEMEJYnZil/vxilEelxpjZ+KdmBH2LGC9nbwKLWb9TGh2iaxb/4KYYH1P0y1m9nT QnmUWmUSgFbwAazPIfNNWJMKewtCQcUxcJrIZS9eJVmMoSsIUK9FOibrxiHY4WjY199q HstQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698469110; x=1699073910; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8q790Td8gv55CpXw6jh1r5+WUtiBYMYeRT8D2CcfTbo=; b=QyXK6cgg6FeKEySap+TD6wW77/r6pox3TKsOw0wqj568Kd89HwJPADAgupmzedTLKR PoMoA3QGA2JWvwqDdxVU3RKdK1E8IiQMmI5yDxLJMcKu2k8VrRYJFEvgMzhTIPjJ/i2h k7PCD6wMvXXmCnuvjxcExDAUl3KDCDNOdZB13/vlF34KErRosP33YJKxbDP8r1LB+Jkt 40Xjr7Z8eXZ8kkmEM8PREjIJMXhWvNZ2hNV/u1CA2+WaZicx+ykJs5dL+ne4edXeLR04 PQwe9zt8E/P0WlBHfYnjh/OrlGcO9NDLlnKPgVc/i7csRElqEqAm/xwxhi8NL5mRnH0P F4PQ== X-Gm-Message-State: AOJu0YyHWaiIHgMs188VJPlleZnlIwMYbkBSmfpYA27CReGIKP2gZNcw 9q7tRs6QZ577X2Nnaze5/MHCewMyAg8RD7vLOIBjYw== X-Received: by 2002:a05:6808:6d5:b0:3a7:5d6e:dcfb with SMTP id m21-20020a05680806d500b003a75d6edcfbmr2757787oih.8.1698469109750; Fri, 27 Oct 2023 21:58:29 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id k16-20020a544710000000b003a8560a9d34sm567488oik.25.2023.10.27.21.58.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 21:58:29 -0700 (PDT) From: Deepak Gupta To: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Jan Kiszka , Kieran Bingham , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Deepak Gupta , Andrew Jones , Palmer Dabbelt , Hsieh-Tseng Shen , Andrew Morton , Glenn Washburn , Jeff Xie Subject: [PATCH v6] scripts/gdb: add lx_current support for riscv Date: Fri, 27 Oct 2023 21:58:00 -0700 Message-ID: <20231028045813.685389-1-debug@rivosinc.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Fri, 27 Oct 2023 21:59:02 -0700 (PDT) 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) 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 v5 --> v6: - rebased on 6.6-rc5. dropped changes in utils.py as they're upstream --- scripts/gdb/linux/cpus.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/scripts/gdb/linux/cpus.py b/scripts/gdb/linux/cpus.py index 255dc18cb9da..cba589e5b57d 100644 --- a/scripts/gdb/linux/cpus.py +++ b/scripts/gdb/linux/cpus.py @@ -179,6 +179,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") -- 2.42.0