Skip to content

Commit 4608c15

Browse files
guoren83palmer-dabbelt
authored andcommitted
riscv: compat: ptrace: Add compat_arch_ptrace implement
Now, you can use native gdb on riscv64 for rv32 app debugging. $ uname -a Linux buildroot 5.16.0-rc4-00036-gbef6b82fdf23-dirty #53 SMP Mon Dec 20 23:06:53 CST 2021 riscv64 GNU/Linux $ cat /proc/cpuinfo processor : 0 hart : 0 isa : rv64imafdcsuh mmu : sv48 $ file /bin/busybox /bin/busybox: setuid ELF 32-bit LSB shared object, UCB RISC-V, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-riscv32-ilp32d.so.1, for GNU/Linux 5.15.0, stripped $ file /usr/bin/gdb /usr/bin/gdb: ELF 32-bit LSB shared object, UCB RISC-V, version 1 (GNU/Linux), dynamically linked, interpreter /lib/ld-linux-riscv32-ilp32d.so.1, for GNU/Linux 5.15.0, stripped $ /usr/bin/gdb /bin/busybox GNU gdb (GDB) 10.2 Copyright (C) 2021 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> ... Reading symbols from /bin/busybox... (No debugging symbols found in /bin/busybox) (gdb) b main Breakpoint 1 at 0x8ddc (gdb) r Starting program: /bin/busybox Failed to read a valid object file image from memory. Breakpoint 1, 0x555a8ddc in main () (gdb) i r ra 0x77df0b74 0x77df0b74 sp 0x7fdd3d10 0x7fdd3d10 gp 0x5567e800 0x5567e800 <bb_common_bufsiz1+160> tp 0x77f64280 0x77f64280 t0 0x0 0 t1 0x555a6fac 1431990188 t2 0x77dd8db4 2011008436 fp 0x7fdd3e34 0x7fdd3e34 s1 0x7fdd3e34 2145205812 a0 0xffffffff -1 a1 0x2000 8192 a2 0x7fdd3e3c 2145205820 a3 0x0 0 a4 0x7fdd3d30 2145205552 a5 0x555a8dc0 1431997888 a6 0x77f2c170 2012397936 a7 0x6a7c7a2f 1786542639 s2 0x0 0 s3 0x0 0 s4 0x555a8dc0 1431997888 s5 0x77f8a3a8 2012783528 s6 0x7fdd3e3c 2145205820 s7 0x5567cecc 1432866508 --Type <RET> for more, q to quit, c to continue without paging-- s8 0x1 1 s9 0x0 0 s10 0x55634448 1432568904 s11 0x0 0 t3 0x77df0bb8 2011106232 t4 0x42fc 17148 t5 0x0 0 t6 0x40 64 pc 0x555a8ddc 0x555a8ddc <main+28> (gdb) si 0x555a78f0 in mallopt@plt () (gdb) c Continuing. BusyBox v1.34.1 (2021-12-19 22:39:48 CST) multi-call binary. BusyBox is copyrighted by many authors between 1998-2015. Licensed under GPLv2. See source distribution for detailed copyright notices. Usage: busybox [function [arguments]...] or: busybox --list[-full] ... [Inferior 1 (process 107) exited normally] (gdb) q Signed-off-by: Guo Ren <guoren@linux.alibaba.com> Signed-off-by: Guo Ren <guoren@kernel.org> Reviewed-by: Palmer Dabbelt <palmer@rivosinc.com> Reviewed-by: Arnd Bergmann <arnd@arndb.de> Tested-by: Heiko Stuebner <heiko@sntech.de> Link: https://lore.kernel.org/r/20220405071314.3225832-20-guoren@kernel.org Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 7383ee0 commit 4608c15

1 file changed

Lines changed: 82 additions & 5 deletions

File tree

arch/riscv/kernel/ptrace.c

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <asm/thread_info.h>
1313
#include <asm/switch_to.h>
1414
#include <linux/audit.h>
15+
#include <linux/compat.h>
1516
#include <linux/ptrace.h>
1617
#include <linux/elf.h>
1718
#include <linux/regset.h>
@@ -110,11 +111,6 @@ static const struct user_regset_view riscv_user_native_view = {
110111
.n = ARRAY_SIZE(riscv_user_regset),
111112
};
112113

113-
const struct user_regset_view *task_user_regset_view(struct task_struct *task)
114-
{
115-
return &riscv_user_native_view;
116-
}
117-
118114
struct pt_regs_offset {
119115
const char *name;
120116
int offset;
@@ -272,3 +268,84 @@ __visible void do_syscall_trace_exit(struct pt_regs *regs)
272268
trace_sys_exit(regs, regs_return_value(regs));
273269
#endif
274270
}
271+
272+
#ifdef CONFIG_COMPAT
273+
static int compat_riscv_gpr_get(struct task_struct *target,
274+
const struct user_regset *regset,
275+
struct membuf to)
276+
{
277+
struct compat_user_regs_struct cregs;
278+
279+
regs_to_cregs(&cregs, task_pt_regs(target));
280+
281+
return membuf_write(&to, &cregs,
282+
sizeof(struct compat_user_regs_struct));
283+
}
284+
285+
static int compat_riscv_gpr_set(struct task_struct *target,
286+
const struct user_regset *regset,
287+
unsigned int pos, unsigned int count,
288+
const void *kbuf, const void __user *ubuf)
289+
{
290+
int ret;
291+
struct compat_user_regs_struct cregs;
292+
293+
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1);
294+
295+
cregs_to_regs(&cregs, task_pt_regs(target));
296+
297+
return ret;
298+
}
299+
300+
static const struct user_regset compat_riscv_user_regset[] = {
301+
[REGSET_X] = {
302+
.core_note_type = NT_PRSTATUS,
303+
.n = ELF_NGREG,
304+
.size = sizeof(compat_elf_greg_t),
305+
.align = sizeof(compat_elf_greg_t),
306+
.regset_get = compat_riscv_gpr_get,
307+
.set = compat_riscv_gpr_set,
308+
},
309+
#ifdef CONFIG_FPU
310+
[REGSET_F] = {
311+
.core_note_type = NT_PRFPREG,
312+
.n = ELF_NFPREG,
313+
.size = sizeof(elf_fpreg_t),
314+
.align = sizeof(elf_fpreg_t),
315+
.regset_get = riscv_fpr_get,
316+
.set = riscv_fpr_set,
317+
},
318+
#endif
319+
};
320+
321+
static const struct user_regset_view compat_riscv_user_native_view = {
322+
.name = "riscv",
323+
.e_machine = EM_RISCV,
324+
.regsets = compat_riscv_user_regset,
325+
.n = ARRAY_SIZE(compat_riscv_user_regset),
326+
};
327+
328+
long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
329+
compat_ulong_t caddr, compat_ulong_t cdata)
330+
{
331+
long ret = -EIO;
332+
333+
switch (request) {
334+
default:
335+
ret = compat_ptrace_request(child, request, caddr, cdata);
336+
break;
337+
}
338+
339+
return ret;
340+
}
341+
#endif /* CONFIG_COMPAT */
342+
343+
const struct user_regset_view *task_user_regset_view(struct task_struct *task)
344+
{
345+
#ifdef CONFIG_COMPAT
346+
if (test_tsk_thread_flag(task, TIF_32BIT))
347+
return &compat_riscv_user_native_view;
348+
else
349+
#endif
350+
return &riscv_user_native_view;
351+
}

0 commit comments

Comments
 (0)