Skip to content

Commit

Permalink
scripts/gdb: add lx_current support for arm64
Browse files Browse the repository at this point in the history
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.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Barry Song <[email protected]>
Cc: Jan Kiszka <[email protected]>
Cc: Jonathan Corbet <[email protected]>
Cc: Kieran Bingham <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Barry Song authored and torvalds committed May 7, 2021
1 parent dc95868 commit 526940e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Documentation/dev-tools/gdb-kernel-debugging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions scripts/gdb/linux/cpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from linux import tasks, utils


task_type = utils.CachedType("struct task_struct")


MAX_CPUS = 4096


Expand Down Expand Up @@ -157,9 +160,19 @@ def invoke(self, var_name, cpu=-1):
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")
Expand Down

0 comments on commit 526940e

Please sign in to comment.