From patchwork Wed Jan 29 18:14:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Maciej W. Rozycki" X-Patchwork-Id: 37614 Received: (qmail 13598 invoked by alias); 29 Jan 2020 18:14:22 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 13580 invoked by uid 89); 29 Jan 2020 18:14:20 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-9.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_SHORT autolearn=ham version=3.3.1 spammy=favour X-HELO: esa4.hgst.iphmx.com Received: from esa4.hgst.iphmx.com (HELO esa4.hgst.iphmx.com) (216.71.154.42) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 29 Jan 2020 18:14:17 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1580321657; x=1611857657; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-id:content-transfer-encoding: mime-version; bh=7MweSfYkzmbR/skPXXZJYmxhgFD+cua6xoMnSj8yskU=; b=b/iWCRJNyUWb8mJ0QSfDIKJhU12n51+Tq838E0myWaBnjxh0TWIGmK0G S8dlPOaMhL2rHh8ZTwLOqQ+4GRuPwXmlfHH1QCwd1KK/YL9S9HcPKhlFj FW7+rJDsp6LkovsybfKDnhGIR772JJXZ47rR7jAd7zlgeYPsMM7GP7Lgr jak0ApA8t1bCkhxb04/cIQxguQVPdX5GiEgMvybSN48a3nrWOTU0sL9xs T8tkEa45HTGxOfsuIpYmes9BjJUMeuRmOziEuwel0yJmozmY5M6YNtIMx o8W6fxwRRhLhlNbGEwWoUvqOVLt+7czwK+WvkS8xpIQvu4yczxeI2hv5l g==; IronPort-SDR: uiZAcFLC23+uEyO0sszZ72mdImbXKQnVx34DI6mDU8z/KJ4o0YSHlGhLDUFQ2kUeY5Fd9/EjrX y6bJ+P0V4R3AITMN6ufVbrYDLtRUpgxf+CVa2mvHAJk/jiJBik9vOAR81R1iBBVl3iI59J+llx xnp+sjaLk8mEAruR6Vvw+eDOeZ5J6jQ0SsuNhDtF6qmurYVq4JtabEDPFke+5ttm0HOdfqi5n1 t+2eiDZECahelsgk3l3hgrHa9y3qo+A3vIRCKf7Z9dVg7rNOfgjexzMiQfCxtFOaQhSfmAjfgK l9U= Received: from mail-dm6nam11lp2169.outbound.protection.outlook.com (HELO NAM11-DM6-obe.outbound.protection.outlook.com) ([104.47.57.169]) by ob1.hgst.iphmx.com with ESMTP; 30 Jan 2020 02:14:14 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QI6dL+g8n42CabSplCgQOhd78Fw/lmdxUID4iJn9uft4J0GFojduX77t61uIGiEMx76zGaqOEctD0c125g4PjLFQ/eVBH1NwFvXI6/e4DpwFUiLedvrQAsGNMByVxSHF+Lmd/T9vwld0+2LnmJouwPDhHHLTMjENP+qOYp7gYbamdVyqdnjfFN1aaQYJrTALC+WMERf4y8/FYLjynTF3yk9+HJnCe9cDIjY8ZtaSu8lq03OXvlDWY1mnu++wldiatRmYvqlh8NVhqQqreGQOy0OHJUYKWN6xH0k/n0/R7ojsRb7HK4gq6yyRVjMsmXTNivZbg+jnbvqKJ/RzxHOafQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=FelZJrvZNk8BRsSCDWqZI7E8KwUdl1rcdfxYZ9sCAkI=; b=GS94WCRtB+Lf0refhTnF1rgmAEKr2RZrQwkpJB8TBWVdcRCeA5AKjLWJB0bfblRC7oeu2FbqaVDA97IlVn6/w3AZlgOuP8smK3vf+ty3LScwPSAmcBL0zXnI8E6oxBa/5WBfiv+CN4pFZ9TLvx1Pp4xQXGpKDHAj3MtDB0CXzPqtAqZ7+WJuN4ZFjeNCd+x58sugCz55BN1tTSkkrom8SHuC0CDFfkCJPrCMX+pesB1Zqf3h4kna3IWOjLCE3aaA46JLFmCLAk5WS8k1heKyraHGqujsSxpXnf8uBFDuHceyI6fp4hUjT70zJJ31wb7psyJbdZd+2d1m/M/auakt2Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=FelZJrvZNk8BRsSCDWqZI7E8KwUdl1rcdfxYZ9sCAkI=; b=dj8CMWw1/H6CTFVfs/yM94twGK/I63vhjveptVpglTro0G4AxkxjJM5AYAtWuaW+wV3gU8OIfjf+resykeH17eGCsdBGUR6POgE9PBMgddq7WloMWejIj/xhdXQtlz4Ld4j1hE9R951ey73v6DtEccanzoJUJvQXY75KUbspefA= Received: from BY5PR04MB6980.namprd04.prod.outlook.com (10.186.134.11) by BY5PR04MB6627.namprd04.prod.outlook.com (10.186.135.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2665.19; Wed, 29 Jan 2020 18:14:13 +0000 Received: from BY5PR04MB6980.namprd04.prod.outlook.com ([fe80::d068:7819:b5cf:353]) by BY5PR04MB6980.namprd04.prod.outlook.com ([fe80::d068:7819:b5cf:353%5]) with mapi id 15.20.2665.027; Wed, 29 Jan 2020 18:14:13 +0000 From: "Maciej W. Rozycki" To: "gdb-patches@sourceware.org" CC: Jim Wilson , Andrew Burgess , Palmer Dabbelt , Tom Tromey , "guoren@kernel.org" , "lifang_xia@c-sky.com" , "yunhai_shang@c-sky.com" , "jiangshuai_li@c-sky.com" Subject: [PATCH v2 3/3] gdbserver: Add RISC-V/Linux support Date: Wed, 29 Jan 2020 18:14:13 +0000 Message-ID: References: In-Reply-To: authentication-results: spf=none (sender IP is ) smtp.mailfrom=macro@wdc.com; x-ms-exchange-transport-forked: True wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:9508; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-messagedata: m0s5FNaaiYJ9dfY97RbtegjehGzRNQf2f5dQvKGkNM6ayLPtrTwY93HOMHgj7ShSbTvx0lfF6WXWQN7o6eHjV1547Z2Xf0Y+RLFW+A5Iw38fw48vV9C4kCUzaXkkm5RjjekZTniUE3ag/FNNF8fKdQ== Content-ID: <1853E36718214E458612A102EF14237C@namprd04.prod.outlook.com> MIME-Version: 1.0 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: le0Uq25GeBUgPUNn78ybovZJ7phKmjC9x+rceW9tDHyo03dIFbWuwy7nXkzD5Crh Implement RISC-V/Linux support for both RV64 and RV32 systems, including XML target description handling based on features determined, GPR and FPR regset support including dynamic sizing of the latter, and software breakpoint handling. Define two NT_FPREGSET regsets of a different size matching the FPR sizes supported for generic `gdbserver' code to pick from according to what the OS supplies. Also handle a glibc bug where ELF_NFPREG is defined in terms of NFPREG, however NFPREG is nowhere defined. gdb/ * arch/riscv.h (riscv_create_target_description): Remove `const' qualifier from the return type. * arch/riscv.c (riscv_create_target_description): Likewise. * nat/riscv-linux-tdesc.h (riscv_linux_read_description): Likewise. * nat/riscv-linux-tdesc.c (riscv_linux_read_description): Likewise. * configure.tgt : Set build_gdbserver=yes. gdb/gdbserver/ * linux-riscv-low.c: New file. * Makefile.in (SFILES): Add linux-riscv-low.c, arch/riscv.c, and nat/riscv-linux-tdesc.c. * configure.srv (srv_tgtobj) (srv_linux_regsets, srv_linux_usrregs, srv_linux_thread_db): Define. --- Changes from v1: - Make `gdbserver' selected for automatic build in a RISC-V/Linux/native GDB configuration (thanks, Jim, for pointing this out!). - Remove most of `riscv_arch_setup' and use `riscv_linux_read_description' from 2/3 instead. - Stop using `elf_fpregset_t*' in favour to just a raw `gdb_byte *' buffer and size the regset according to the FPR size in `riscv_fill_fpregset' and `riscv_store_fpregset'. - Define 2 NT_FPREGSET regsets of a different size for generic `gdbserver' code to pick from according to what the OS supplies. --- gdb/arch/riscv.c | 2 gdb/arch/riscv.h | 4 gdb/configure.tgt | 1 gdb/gdbserver/Makefile.in | 3 gdb/gdbserver/configure.srv | 7 + gdb/gdbserver/linux-riscv-low.c | 257 ++++++++++++++++++++++++++++++++++++++++ gdb/nat/riscv-linux-tdesc.c | 2 gdb/nat/riscv-linux-tdesc.h | 2 8 files changed, 273 insertions(+), 5 deletions(-) gdb-riscv-gdbserver-linux.diff Index: binutils-gdb/gdb/arch/riscv.c =================================================================== --- binutils-gdb.orig/gdb/arch/riscv.c +++ binutils-gdb/gdb/arch/riscv.c @@ -43,7 +43,7 @@ static std::unordered_map. */ + +#include "server.h" + +#include "linux-low.h" +#include "tdesc.h" +#include "elf/common.h" +#include "nat/riscv-linux-tdesc.h" +#include "opcode/riscv.h" + +/* Work around glibc header breakage causing ELF_NFPREG not to be usable. */ +#ifndef NFPREG +# define NFPREG 33 +#endif + +/* Implementation of linux_target_ops method "arch_setup". */ + +static void +riscv_arch_setup () +{ + static const char *expedite_regs[] = { "sp", "pc", NULL }; + target_desc *tdesc; + + tdesc = riscv_linux_read_description (lwpid_of (current_thread)); + if (!tdesc->expedite_regs) + init_target_desc (tdesc, expedite_regs); + current_process ()->tdesc = tdesc; +} + +static void +riscv_fill_gregset (struct regcache *regcache, void *buf) +{ + const struct target_desc *tdesc = regcache->tdesc; + elf_gregset_t *regset = (elf_gregset_t *) buf; + int regno = find_regno (tdesc, "zero"); + int i; + + collect_register_by_name (regcache, "pc", *regset); + for (i = 1; i < ARRAY_SIZE (*regset); i++) + collect_register (regcache, regno + i, *regset + i); +} + +static void +riscv_store_gregset (struct regcache *regcache, const void *buf) +{ + const elf_gregset_t *regset = (const elf_gregset_t *) buf; + const struct target_desc *tdesc = regcache->tdesc; + int regno = find_regno (tdesc, "zero"); + int i; + + supply_register_by_name (regcache, "pc", *regset); + supply_register_zeroed (regcache, regno); + for (i = 1; i < ARRAY_SIZE (*regset); i++) + supply_register (regcache, regno + i, *regset + i); +} + +static void +riscv_fill_fpregset (struct regcache *regcache, void *buf) +{ + const struct target_desc *tdesc = regcache->tdesc; + int regno = find_regno (tdesc, "ft0"); + int flen = register_size (regcache->tdesc, regno); + gdb_byte *regset = (gdb_byte *) buf; + int i; + + for (i = 0; i < ELF_NFPREG - 1; i++) + collect_register (regcache, regno + i, regset + i * flen); + collect_register_by_name (regcache, "fcsr", regset + i * flen); +} + +static void +riscv_store_fpregset (struct regcache *regcache, const void *buf) +{ + const struct target_desc *tdesc = regcache->tdesc; + int regno = find_regno (tdesc, "ft0"); + int flen = register_size (regcache->tdesc, regno); + const gdb_byte *regset = (const gdb_byte *) buf; + int i; + + for (i = 0; i < ELF_NFPREG - 1; i++) + supply_register (regcache, regno + i, regset + i * flen); + supply_register_by_name (regcache, "fcsr", regset + i * flen); +} + +static struct regset_info riscv_regsets[] = { + { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS, + sizeof (elf_gregset_t), GENERAL_REGS, + riscv_fill_gregset, riscv_store_gregset }, + { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET, + sizeof (struct __riscv_mc_d_ext_state), FP_REGS, + riscv_fill_fpregset, riscv_store_fpregset }, + { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET, + sizeof (struct __riscv_mc_f_ext_state), FP_REGS, + riscv_fill_fpregset, riscv_store_fpregset }, + NULL_REGSET +}; + +static struct regsets_info riscv_regsets_info = + { + riscv_regsets, /* regsets */ + 0, /* num_regsets */ + NULL, /* disabled_regsets */ + }; + +static struct regs_info riscv_regs = + { + NULL, /* regset_bitmap */ + NULL, /* usrregs */ + &riscv_regsets_info, + }; + +/* Implementation of linux_target_ops method "regs_info". */ + +static const struct regs_info * +riscv_regs_info () +{ + return &riscv_regs; +} + +/* Implementation of linux_target_ops method "fetch_register". */ + +static int +riscv_fetch_register (struct regcache *regcache, int regno) +{ + const struct target_desc *tdesc = regcache->tdesc; + + if (regno != find_regno (tdesc, "zero")) + return 0; + supply_register_zeroed (regcache, regno); + return 1; +} + +/* Implementation of linux_target_ops method "get_pc". */ + +static CORE_ADDR +riscv_get_pc (struct regcache *regcache) +{ + elf_gregset_t regset; + + if (sizeof (regset[0]) == 8) + return linux_get_pc_64bit (regcache); + else + return linux_get_pc_32bit (regcache); +} + +/* Implementation of linux_target_ops method "set_pc". */ + +static void +riscv_set_pc (struct regcache *regcache, CORE_ADDR newpc) +{ + elf_gregset_t regset; + + if (sizeof (regset[0]) == 8) + linux_set_pc_64bit (regcache, newpc); + else + linux_set_pc_32bit (regcache, newpc); +} + +/* Correct in either endianness. */ +static const uint16_t riscv_ibreakpoint[] = { 0x0073, 0x0010 }; +static const uint16_t riscv_cbreakpoint = 0x9002; + +/* Implementation of linux_target_ops method "breakpoint_kind_from_pc". */ + +static int +riscv_breakpoint_kind_from_pc (CORE_ADDR *pcptr) +{ + union + { + gdb_byte bytes[2]; + uint16_t insn; + } + buf; + + if (target_read_memory (*pcptr, buf.bytes, sizeof (buf.insn)) == 0 + && riscv_insn_length (buf.insn == sizeof (riscv_ibreakpoint))) + return sizeof (riscv_ibreakpoint); + else + return sizeof (riscv_cbreakpoint); +} + +/* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */ + +static const gdb_byte * +riscv_sw_breakpoint_from_kind (int kind, int *size) +{ + *size = kind; + switch (kind) + { + case sizeof (riscv_ibreakpoint): + return (const gdb_byte *) &riscv_ibreakpoint; + default: + return (const gdb_byte *) &riscv_cbreakpoint; + } +} + +/* Implementation of linux_target_ops method "breakpoint_at". */ + +static int +riscv_breakpoint_at (CORE_ADDR pc) +{ + union + { + gdb_byte bytes[2]; + uint16_t insn; + } + buf; + + if (target_read_memory (pc, buf.bytes, sizeof (buf.insn)) == 0 + && (buf.insn == riscv_cbreakpoint + || (buf.insn == riscv_ibreakpoint[0] + && target_read_memory (pc + sizeof (buf.insn), buf.bytes, + sizeof (buf.insn)) == 0 + && buf.insn == riscv_ibreakpoint[1]))) + return 1; + else + return 0; +} + +struct linux_target_ops the_low_target = +{ + riscv_arch_setup, + riscv_regs_info, + NULL, /* cannot_fetch_register */ + NULL, /* cannot_store_register */ + riscv_fetch_register, + riscv_get_pc, + riscv_set_pc, + riscv_breakpoint_kind_from_pc, + riscv_sw_breakpoint_from_kind, + NULL, /* get_next_pcs */ + 0, /* decr_pc_after_break */ + riscv_breakpoint_at, +}; + +void +initialize_low_arch () +{ + initialize_regsets_info (&riscv_regsets_info); +} Index: binutils-gdb/gdb/nat/riscv-linux-tdesc.c =================================================================== --- binutils-gdb.orig/gdb/nat/riscv-linux-tdesc.c +++ binutils-gdb/gdb/nat/riscv-linux-tdesc.c @@ -33,7 +33,7 @@ /* Determine XLEN and FLEN and return a corresponding target description. */ -const struct target_desc * +struct target_desc * riscv_linux_read_description (int tid) { struct riscv_gdbarch_features features; Index: binutils-gdb/gdb/nat/riscv-linux-tdesc.h =================================================================== --- binutils-gdb.orig/gdb/nat/riscv-linux-tdesc.h +++ binutils-gdb/gdb/nat/riscv-linux-tdesc.h @@ -22,6 +22,6 @@ struct target_desc; /* Return a target description for the LWP identified by TID. */ -const struct target_desc *riscv_linux_read_description (int tid); +struct target_desc *riscv_linux_read_description (int tid); #endif /* NAT_RISCV_LINUX_TDESC_H */