linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RESEND PATCH v2 0/2] scripts/gdb: clarify the platforms supporting lx_current and add arm64 support
@ 2021-03-14 20:34 Barry Song
  2021-03-14 20:34 ` [RESEND PATCH v2 1/2] scripts/gdb: document lx_current is only supported by x86 Barry Song
  2021-03-14 20:34 ` [RESEND PATCH v2 2/2] scripts/gdb: add lx_current support for arm64 Barry Song
  0 siblings, 2 replies; 3+ messages in thread
From: Barry Song @ 2021-03-14 20:34 UTC (permalink / raw)
  To: akpm, jan.kiszka, kbingham
  Cc: corbet, linux-kernel, linux-arm-kernel, linuxarm, Barry Song

lx_current depends on per_cpu current_task variable which exists on x86 only.
so it actually works on x86 only. the 1st patch documents this clearly; the
2nd patch adds support for arm64.

-resend
 resending to Andrew as Kieran Bingham explained patches of scripts/gdb
 usually go through the tree of Andrew Morton;

Barry Song (2):
  scripts/gdb: document lx_current is only supported by x86
  scripts/gdb: add lx_current support for arm64

 .../dev-tools/gdb-kernel-debugging.rst        |  2 +-
 scripts/gdb/linux/cpus.py                     | 23 +++++++++++++++++--
 2 files changed, 22 insertions(+), 3 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RESEND PATCH v2 1/2] scripts/gdb: document lx_current is only supported by x86
  2021-03-14 20:34 [RESEND PATCH v2 0/2] scripts/gdb: clarify the platforms supporting lx_current and add arm64 support Barry Song
@ 2021-03-14 20:34 ` Barry Song
  2021-03-14 20:34 ` [RESEND PATCH v2 2/2] scripts/gdb: add lx_current support for arm64 Barry Song
  1 sibling, 0 replies; 3+ messages in thread
From: Barry Song @ 2021-03-14 20:34 UTC (permalink / raw)
  To: akpm, jan.kiszka, kbingham
  Cc: corbet, linux-kernel, linux-arm-kernel, linuxarm, Barry Song

x86 is the only architecture which has per_cpu current_task:
arch$ git grep current_task | grep -i per_cpu
x86/include/asm/current.h:DECLARE_PER_CPU(struct task_struct *, current_task);
x86/kernel/cpu/common.c:DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
x86/kernel/cpu/common.c:EXPORT_PER_CPU_SYMBOL(current_task);
x86/kernel/cpu/common.c:DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
x86/kernel/cpu/common.c:EXPORT_PER_CPU_SYMBOL(current_task);
x86/kernel/smpboot.c:	per_cpu(current_task, cpu) = idle;

On other architectures, lx_current() will lead to a python exception:
(gdb) p $lx_current().pid
Python Exception <class 'gdb.error'> No symbol "current_task" in current context.:
Error occurred in Python: No symbol "current_task" in current context.

To avoid more people struggling and wasting time in other architectures,
document it.

Cc: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
---
 Documentation/dev-tools/gdb-kernel-debugging.rst |  2 +-
 scripts/gdb/linux/cpus.py                        | 10 ++++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/Documentation/dev-tools/gdb-kernel-debugging.rst b/Documentation/dev-tools/gdb-kernel-debugging.rst
index 4756f6b3a04e..1586901b683c 100644
--- a/Documentation/dev-tools/gdb-kernel-debugging.rst
+++ b/Documentation/dev-tools/gdb-kernel-debugging.rst
@@ -114,7 +114,7 @@ Examples of using the Linux-provided gdb helpers
     [     0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
     ....
 
-- Examine fields of the current task struct::
+- Examine fields of the current task struct(supported by x86 only)::
 
     (gdb) p $lx_current().pid
     $1 = 4998
diff --git a/scripts/gdb/linux/cpus.py b/scripts/gdb/linux/cpus.py
index 008e62f3190d..f382762509d3 100644
--- a/scripts/gdb/linux/cpus.py
+++ b/scripts/gdb/linux/cpus.py
@@ -156,6 +156,13 @@ Note that VAR has to be quoted as string."""
 
 PerCpu()
 
+def get_current_task(cpu):
+    if utils.is_target_arch("x86"):
+         var_ptr = gdb.parse_and_eval("&current_task")
+         return per_cpu(var_ptr, cpu).dereference()
+    else:
+        raise gdb.GdbError("Sorry, obtaining the current task is not yet "
+                           "supported with this arch")
 
 class LxCurrentFunc(gdb.Function):
     """Return current task.
@@ -167,8 +174,7 @@ number. If CPU is omitted, the CPU of the current context is used."""
         super(LxCurrentFunc, self).__init__("lx_current")
 
     def invoke(self, cpu=-1):
-        var_ptr = gdb.parse_and_eval("&current_task")
-        return per_cpu(var_ptr, cpu).dereference()
+        return get_current_task(cpu)
 
 
 LxCurrentFunc()
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [RESEND PATCH v2 2/2] scripts/gdb: add lx_current support for arm64
  2021-03-14 20:34 [RESEND PATCH v2 0/2] scripts/gdb: clarify the platforms supporting lx_current and add arm64 support Barry Song
  2021-03-14 20:34 ` [RESEND PATCH v2 1/2] scripts/gdb: document lx_current is only supported by x86 Barry Song
@ 2021-03-14 20:34 ` Barry Song
  1 sibling, 0 replies; 3+ messages in thread
From: Barry Song @ 2021-03-14 20:34 UTC (permalink / raw)
  To: akpm, jan.kiszka, kbingham
  Cc: corbet, linux-kernel, linux-arm-kernel, linuxarm, Barry Song

arm64 uses SP_EL0 to save the current task_struct address. While running
in EL0, SP_EL0 is clobbered by userspace. So if the upper bit is not 1
(not TTBR1), the current address is invalid. This patch checks the upper
bit of SP_EL0, if the upper bit is 1, lx_current() of arm64 will return
the derefrence of current task. Otherwise, lx_current() will tell users
they are running in userspace(EL0).

While arm64 is running in EL0, it is actually pointless to print current
task as the memory of kernel space is not accessible in EL0.

Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
---
 Documentation/dev-tools/gdb-kernel-debugging.rst |  2 +-
 scripts/gdb/linux/cpus.py                        | 13 +++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/Documentation/dev-tools/gdb-kernel-debugging.rst b/Documentation/dev-tools/gdb-kernel-debugging.rst
index 1586901b683c..8e0f1fe8d17a 100644
--- a/Documentation/dev-tools/gdb-kernel-debugging.rst
+++ b/Documentation/dev-tools/gdb-kernel-debugging.rst
@@ -114,7 +114,7 @@ Examples of using the Linux-provided gdb helpers
     [     0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
     ....
 
-- Examine fields of the current task struct(supported by x86 only)::
+- Examine fields of the current task struct(supported by x86 and arm64 only)::
 
     (gdb) p $lx_current().pid
     $1 = 4998
diff --git a/scripts/gdb/linux/cpus.py b/scripts/gdb/linux/cpus.py
index f382762509d3..15fc4626d236 100644
--- a/scripts/gdb/linux/cpus.py
+++ b/scripts/gdb/linux/cpus.py
@@ -16,6 +16,9 @@ import gdb
 from linux import tasks, utils
 
 
+task_type = utils.CachedType("struct task_struct")
+
+
 MAX_CPUS = 4096
 
 
@@ -157,9 +160,19 @@ Note that VAR has to be quoted as string."""
 PerCpu()
 
 def get_current_task(cpu):
+    task_ptr_type = task_type.get_type().pointer()
+
     if utils.is_target_arch("x86"):
          var_ptr = gdb.parse_and_eval("&current_task")
          return per_cpu(var_ptr, cpu).dereference()
+    elif utils.is_target_arch("aarch64"):
+         current_task_addr = gdb.parse_and_eval("$SP_EL0")
+         if((current_task_addr >> 63) != 0):
+             current_task = current_task_addr.cast(task_ptr_type)
+             return current_task.dereference()
+         else:
+             raise gdb.GdbError("Sorry, obtaining the current task is not allowed "
+                                "while running in userspace(EL0)")
     else:
         raise gdb.GdbError("Sorry, obtaining the current task is not yet "
                            "supported with this arch")
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-03-14 20:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-14 20:34 [RESEND PATCH v2 0/2] scripts/gdb: clarify the platforms supporting lx_current and add arm64 support Barry Song
2021-03-14 20:34 ` [RESEND PATCH v2 1/2] scripts/gdb: document lx_current is only supported by x86 Barry Song
2021-03-14 20:34 ` [RESEND PATCH v2 2/2] scripts/gdb: add lx_current support for arm64 Barry Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).