Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp124907pxm; Tue, 22 Feb 2022 18:32:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJxahEAa60Dd2s+JPuIEblZCJHQKB5Xmrc4eqLNmqyRohE3oDmdu7xcmnExu3OBaMXM5w4KJ X-Received: by 2002:a17:906:5e13:b0:6cf:42c:56b7 with SMTP id n19-20020a1709065e1300b006cf042c56b7mr21491488eju.725.1645583558181; Tue, 22 Feb 2022 18:32:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645583558; cv=none; d=google.com; s=arc-20160816; b=UsJ6vfrMf7bWxOGYjkgkGqf0HzwQdtZPH6b8wCz2qzeCS8199s1aiqH1Xp1iFXPTnT QGakwizA7RqEDnJmEbjEa8DFQ9PN52eecI464uN2Allx6PILc5YNnFvwx843WxL809Ve e4wMwCyFyi0NSWFK9xZpsXikt5vdCVwYCBmwGyHiH+1o53+HxtdC+hCYdVu4VlKa9B2o t9F96golxdZgkaIgrLD+mCEP2EeDgsaZjeXfWJDiTaosHOD/clxMyzZ1zPj8orCN+IfA gWrqFS824WYG34djddJ2FrDGhFan9sLRByI8krtcFOoieQJxWEZP57/udt+uaHOgBvaJ pRkw== 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:to:from:cc:in-reply-to:subject:date:dkim-signature; bh=CUt3fT/uuV0gaDZr82IxRQYON9jArt/u8BEXVuBiL/k=; b=QmyB3kRP7V9g3dpoOohj8KYBml7JIqTYjclBDQBJjnRNAQsK45yc+JligNAgfJF0Te wbSjkvcPB6H0FqyZO+96u7Ut67z5mUr7P68FDNGZTvgEVcpLADiwxggI7bXBOnjLyaIb A4eCHMgAe86faQfVW0GutKzyAN3sa4xklYM1fqm9vltkud6/ZcIyAc3/oS7AMmLqzgIs 53R3LuHz6hw78bmwNu21MaI3yjb06moPfgYiuJjKnrw4mzsKj2LsHfx2+ul24vCYDCT3 kOC10Ai874JxPdkEREzjv1gAwdnAS5jxc0NLUXra7/jQbJMyZ6bjWon6RZ3yZsKthjuR b/kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dabbelt-com.20210112.gappssmtp.com header.s=20210112 header.b=d5biXJ9G; 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 cw3si14713081ejc.762.2022.02.22.18.32.15; Tue, 22 Feb 2022 18:32:38 -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=@dabbelt-com.20210112.gappssmtp.com header.s=20210112 header.b=d5biXJ9G; 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 S232184AbiBWBoC (ORCPT + 99 others); Tue, 22 Feb 2022 20:44:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236677AbiBWBn3 (ORCPT ); Tue, 22 Feb 2022 20:43:29 -0500 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3C9C50B14 for ; Tue, 22 Feb 2022 17:42:57 -0800 (PST) Received: by mail-pj1-x1029.google.com with SMTP id ev16-20020a17090aead000b001bc3835fea8so1350497pjb.0 for ; Tue, 22 Feb 2022 17:42:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dabbelt-com.20210112.gappssmtp.com; s=20210112; h=date:subject:in-reply-to:cc:from:to:message-id:mime-version :content-transfer-encoding; bh=CUt3fT/uuV0gaDZr82IxRQYON9jArt/u8BEXVuBiL/k=; b=d5biXJ9GxQMu04AFeYHwejSk8w+590qVJOkBF/bpDIzSLkz2zdCw8KJuYjHAwr9mEV Wgbr7ud3jGsbz5LBPEJYrd7V5yYHecxcBpDxpZFmS6VjGKbcfMqz1U5KW8TRPCc1Clu7 D7pff6rpq5wRkJdHsdwndWixeOnUM+Kufa2y/KIsJnJIYuz5j+OM6IjU0RNfxPMrbaIp QseC1jQuKkZN6xjj+WQStAksKeqv8OONsbxZSHJAPIQdVeNcdiEkYV17vD6EuzdBzc0E MZV1SvJl3WJSlNLmRDubF74JYsSImHzRVaDYYvn94VKmAte8UOWngNBkd+yP/LUJV51J Et2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:subject:in-reply-to:cc:from:to:message-id :mime-version:content-transfer-encoding; bh=CUt3fT/uuV0gaDZr82IxRQYON9jArt/u8BEXVuBiL/k=; b=rjsoS+Y7Fppk+3jafs+Od9hnKp+VnPnzANSEm7illrnZKGdo9b1uxptLviY4PXv/E+ IqlqSWbpmMfHEC+duXbmpefKbEZ0fBAOxRZFAUwSUCtSkon4HGhq5KXj3kauVX9xpqx3 ACMVhjGUZcHGftSlIXZnHAjeTBPVuL+ZKEULbFRdtPYjfrV58TCwsS7+6ODvR9d2I8m4 tAW4Pnd8E3Tz8JguRCC53OowRfPbpmvGJ1zKfSN2bIKdFPTvq3tTy3gg3jGoRxCybWo7 S9tohvS/MvbI2AYorByakn6VGCVJOiXLYB4w4YuBbUM+RGGXMtmmWzveLzgNAJi0r2T/ EvYg== X-Gm-Message-State: AOAM53074CT258BZnxrN5ekP6lBGXyY2jThIfB4BBKnxy4ULtGQdsrdT LGrTCNW2OsCSm5tRzFQWBATL/Q== X-Received: by 2002:a17:90a:480e:b0:1bc:1d88:8d4e with SMTP id a14-20020a17090a480e00b001bc1d888d4emr6796297pjh.157.1645580577313; Tue, 22 Feb 2022 17:42:57 -0800 (PST) Received: from localhost ([12.3.194.138]) by smtp.gmail.com with ESMTPSA id ms7sm882191pjb.56.2022.02.22.17.42.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Feb 2022 17:42:56 -0800 (PST) Date: Tue, 22 Feb 2022 17:42:56 -0800 (PST) X-Google-Original-Date: Tue, 22 Feb 2022 17:20:55 PST (-0800) Subject: Re: [PATCH V5 19/21] riscv: compat: ptrace: Add compat_arch_ptrace implement In-Reply-To: <20220201150545.1512822-20-guoren@kernel.org> CC: guoren@kernel.org, Arnd Bergmann , anup@brainfault.org, Greg KH , liush@allwinnertech.com, wefu@redhat.com, drew@beagleboard.org, wangjunqiang@iscas.ac.cn, Christoph Hellwig , linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-csky@vger.kernel.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-parisc@vger.kernel.org, linux-mips@vger.kernel.org, linux-arm-kernel@lists.infradead.org, x86@kernel.org, guoren@linux.alibaba.com From: Palmer Dabbelt To: guoren@kernel.org Message-ID: Mime-Version: 1.0 (MHng) Content-Type: text/plain; charset=utf-8; format=flowed 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, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 On Tue, 01 Feb 2022 07:05:43 PST (-0800), guoren@kernel.org wrote: > From: Guo Ren > > 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 > ... > 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 > 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 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 > (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 > Signed-off-by: Guo Ren > Cc: Arnd Bergmann > Cc: Palmer Dabbelt > --- > arch/riscv/kernel/ptrace.c | 87 +++++++++++++++++++++++++++++++++++--- > 1 file changed, 82 insertions(+), 5 deletions(-) > > diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c > index a89243730153..bb387593a121 100644 > --- a/arch/riscv/kernel/ptrace.c > +++ b/arch/riscv/kernel/ptrace.c > @@ -12,6 +12,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -111,11 +112,6 @@ static const struct user_regset_view riscv_user_native_view = { > .n = ARRAY_SIZE(riscv_user_regset), > }; > > -const struct user_regset_view *task_user_regset_view(struct task_struct *task) > -{ > - return &riscv_user_native_view; > -} > - > struct pt_regs_offset { > const char *name; > int offset; > @@ -273,3 +269,84 @@ __visible void do_syscall_trace_exit(struct pt_regs *regs) > trace_sys_exit(regs, regs_return_value(regs)); > #endif > } > + > +#ifdef CONFIG_COMPAT > +static int compat_riscv_gpr_get(struct task_struct *target, > + const struct user_regset *regset, > + struct membuf to) > +{ > + struct compat_user_regs_struct cregs; > + > + regs_to_cregs(&cregs, task_pt_regs(target)); > + > + return membuf_write(&to, &cregs, > + sizeof(struct compat_user_regs_struct)); > +} > + > +static int compat_riscv_gpr_set(struct task_struct *target, > + const struct user_regset *regset, > + unsigned int pos, unsigned int count, > + const void *kbuf, const void __user *ubuf) > +{ > + int ret; > + struct compat_user_regs_struct cregs; > + > + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1); > + > + cregs_to_regs(&cregs, task_pt_regs(target)); > + > + return ret; > +} > + > +static const struct user_regset compat_riscv_user_regset[] = { > + [REGSET_X] = { > + .core_note_type = NT_PRSTATUS, > + .n = ELF_NGREG, > + .size = sizeof(compat_elf_greg_t), > + .align = sizeof(compat_elf_greg_t), > + .regset_get = compat_riscv_gpr_get, > + .set = compat_riscv_gpr_set, > + }, > +#ifdef CONFIG_FPU > + [REGSET_F] = { > + .core_note_type = NT_PRFPREG, > + .n = ELF_NFPREG, > + .size = sizeof(elf_fpreg_t), > + .align = sizeof(elf_fpreg_t), > + .regset_get = riscv_fpr_get, > + .set = riscv_fpr_set, > + }, > +#endif > +}; > + > +static const struct user_regset_view compat_riscv_user_native_view = { > + .name = "riscv", > + .e_machine = EM_RISCV, > + .regsets = compat_riscv_user_regset, > + .n = ARRAY_SIZE(compat_riscv_user_regset), > +}; > + > +long compat_arch_ptrace(struct task_struct *child, compat_long_t request, > + compat_ulong_t caddr, compat_ulong_t cdata) > +{ > + long ret = -EIO; > + > + switch (request) { > + default: > + ret = compat_ptrace_request(child, request, caddr, cdata); > + break; > + } > + > + return ret; > +} > +#endif /* CONFIG_COMPAT */ > + > +const struct user_regset_view *task_user_regset_view(struct task_struct *task) > +{ > +#ifdef CONFIG_COMPAT > + if (test_tsk_thread_flag(task, TIF_32BIT)) > + return &compat_riscv_user_native_view; > + else > +#endif > + return &riscv_user_native_view; > +} Reviewed-by: Palmer Dabbelt