Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp7482959rwb; Tue, 15 Nov 2022 13:03:24 -0800 (PST) X-Google-Smtp-Source: AA0mqf6vCJnB7m3i/ZTcYFh7O/K0ZeDn2kFXHz/Rgj/C1jPyn6Lapd1RvacjFczGuxSWFQEwWacO X-Received: by 2002:a65:6e96:0:b0:470:928:3b5d with SMTP id bm22-20020a656e96000000b0047009283b5dmr17518675pgb.305.1668546203790; Tue, 15 Nov 2022 13:03:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668546203; cv=none; d=google.com; s=arc-20160816; b=mn8X9kBhhAElzD6sSikwqDyEqJufSRg68OL+RXllpY/iWeip7pAuoogsT3XHhJbDLf 2J53QhHGbopxyqmFUkwfwe8p3MAyWCTk/uL8siBgs6tOczBOGKaa6wFhFHlj8avzQb3B hgomzHZsPMjTasbp23wsNur649ux/wvOT0rEeUHuheSUEakTYX/YzeLogKDYNVYhLRxm ziw07beYSgqpNl9E7HXH2LcNOxfwLCi4sgygqZJPUkcorNyUht7TYJMnlcouo/++KgZ9 6/+MdYDptEfL7ov8mhoYqAVwCj+VqzM/+/pJO42q/yWq4oLBd4A9q9wq1mZ5kpvqq53F CFLw== 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=AmLprh1sbZt2eCw30nE1Tak+7KqUeEPNgaa2olliB1I=; b=HCCa7tkeAzGNpLqE9vRbVUOEmK5yLQ1cimJecjlez3GoCyEDiO4Li4K5piBAno+N7Z ZTpDLyRlr6bWNEfxvhxu1F74BlDSX0TciP8GL8SibSOQhsU8dvdE0cwDMqDf1ZT5Girc JVGExmLSQLFynnoJS3lgWu00ssvZRyG2vYpXWalXSUrVH6r7EhsYwlQD5bihdZoZ6NU+ jHTNEQAQNOD4jV5ImhYz+NVbUy9PMwhRmsFeQ34AT9g7Kjf3JTVdcVcBwpMEjh+/CqbS sRxVrhiCrNxWUCroTn5evDPJcgtbgBbA25zRWKomjZ9VN+18oTaZXS5WEKKXSGQeSq+Y qfaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=ReFv9H8J; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c8-20020a630d08000000b004629cd1dbb1si13232160pgl.329.2022.11.15.13.03.04; Tue, 15 Nov 2022 13:03:23 -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=ReFv9H8J; 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 S231666AbiKOUkg (ORCPT + 91 others); Tue, 15 Nov 2022 15:40:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230255AbiKOUk0 (ORCPT ); Tue, 15 Nov 2022 15:40:26 -0500 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CE9713CE6 for ; Tue, 15 Nov 2022 12:40:24 -0800 (PST) Received: by mail-pl1-x633.google.com with SMTP id b21so14235779plc.9 for ; Tue, 15 Nov 2022 12:40:24 -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=AmLprh1sbZt2eCw30nE1Tak+7KqUeEPNgaa2olliB1I=; b=ReFv9H8JORI6bQggp7WipRxKojLCqrHn3A54Qkd2vXlzxEQHkGai71M1d9foIK3No8 XvqDO4x1RgEt6qf5dd0xTKF1myzr6kqJfOKwfhw7DfHwl5GcLAhdes1yjV4QqoACv75f kJvrTRBwO5rLl6YS9vMpSlrjsEjk4fj3ClPEurbda2t0E9ZbAO1gCqHNMym+ApWKrrdP tTsejKyUG7B8adIYmCoUafwGUyr+Fj5qWC0UdgMHAdXsYFwhAPIprMciZu+NCFV2KwMU yfdpuF1yrF7MmNzI+KWiJGf3BWvRNZDHC2ghIcn5TMgPFdqEhEgOLcWBZbii0M875+3l F+yw== 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=AmLprh1sbZt2eCw30nE1Tak+7KqUeEPNgaa2olliB1I=; b=FbLbmCEt8PJkw4nJW+B/x5OyAYHJ8yrnG21Cjw4cxmp7RyS+Oh80fE0u/8sA3uuqf3 aBr0lb9UdZs0AVG7DKcPQy8uyjx2J+E9BmRMPzkYrL3H2GLJk9ODVVZZlTkDJLfrKY9T n9KPuv06PuYTwc5VUsmsJgSJ890Nab8iDweg3Rd2e4n6tOmuJwWBmVEgUuXTkHqBbGW4 dg+UqBrym6mN26xststyZcXLDYQfESeKgwZ6dWG4G0STlBWvNSMtbhK2b9xiGuY0p1pt D5LF81u6PISF6sktIot4cR9DN8+zq4g7J/4SswZPTIqgeB53C/uLDQR+DJIS8shsBBrF hJmg== X-Gm-Message-State: ANoB5pnLBK0k7rhY/qO1bToT4FU+GKNYbLlAF+gObWaCQtEaCmbsz72p JSx4zJA1L4aK8kgQJN+dEfEuRQ== X-Received: by 2002:a17:90a:4e4a:b0:211:55d8:4cdd with SMTP id t10-20020a17090a4e4a00b0021155d84cddmr132065pjl.133.1668544823609; Tue, 15 Nov 2022 12:40:23 -0800 (PST) Received: from debug.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id y2-20020aa79e02000000b005672daedc8fsm9199721pfq.81.2022.11.15.12.40.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 12:40:23 -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 v4] scripts/gdb: add lx_current support for riscv Date: Tue, 15 Nov 2022 12:40:03 -0800 Message-Id: <20221115204003.1866421-1-debug@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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 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 --- 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..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) -- 2.25.1