gdb: Initial baremetal riscv support

This commit introduces basic support for baremetal RiscV as a GDB
target.  This target is currently only tested against the RiscV software
simulator, which is not included as part of this commit.  The target has
been tested against the following RiscV variants: rv32im, rv32imc,
rv32imf, rv32imfc, rv64im, rv64imc, rv64imfd, rv64imfdc.

Across these variants we pass on average 34858 tests, and fail 272
tests, which is ~0.8%.

The RiscV has a feature of its ABI where structures with a single
floating point field, a single complex float field, or one float and
one integer field are treated differently for argument passing.  The
new test gdb.base/infcall-nested-structs.exp is added to cover this
feature.  As passing these structures should work on all targets then
I've made the test as a generic one, even though, for most targets,
there's probably nothing special about any of these cases.

gdb/ChangeLog:

	* Makefile.in (ALL_TARGET_OBS): Add riscv-tdep.o
	(HFILES_NO_SRCDIR): Add riscv-tdep.h.
	(ALLDEPFILES): Add riscv-tdep.c
	* configure.tgt: Add riscv support.
	* riscv-tdep.c: New file.
	* riscv-tdep.h: New file.
	* NEWS: Mention new target.
	* MAINTAINERS: Add entry for riscv.

gdb/testsuite/ChangeLog:

	* gdb.base/infcall-nested-structs.exp: New file.
	* gdb.base/infcall-nested-structs.c: New file.
	* gdb.base/float.exp: Add riscv support.
This commit is contained in:
Andrew Burgess 2017-11-09 20:59:13 +00:00
parent 5dc4391345
commit dbbb1059e6
11 changed files with 3198 additions and 0 deletions

View File

@ -1,3 +1,17 @@
2018-03-06 Andrew Burgess <andrew.burgess@embecosm.com>
Tim Newsome <tim@sifive.com>
Albert Ou <a0u@eecs.berkeley.edu>
Darius Rad <darius@bluespec.com>
* Makefile.in (ALL_TARGET_OBS): Add riscv-tdep.o
(HFILES_NO_SRCDIR): Add riscv-tdep.h.
(ALLDEPFILES): Add riscv-tdep.c
* configure.tgt: Add riscv support.
* riscv-tdep.c: New file.
* riscv-tdep.h: New file.
* NEWS: Mention new target.
* MAINTAINERS: Add entry for riscv.
2018-03-06 Andrew Burgess <andrew.burgess@embecosm.com>
* amd64-tdep.c (amd64_classify_aggregate): Ignore zero sized

View File

@ -294,6 +294,11 @@ the native maintainer when resolving ABI issues.
powerpc --target=powerpc-eabi ,-Werror
riscv --target=riscv32-elf ,-Werror
--target=riscv64-elf ,-Werror
Andrew Burgess andrew.burgess@embecosm.com
Palmer Dabbelt palmer@sifive.com
rl78 --target=rl78-elf ,-Werror
rx --target=rx-elf ,-Werror

View File

@ -750,6 +750,7 @@ ALL_TARGET_OBS = \
ppc-sysv-tdep.o \
ppc64-tdep.o \
ravenscar-thread.o \
riscv-tdep.o \
rl78-tdep.o \
rs6000-aix-tdep.o \
rs6000-lynx178-tdep.o \
@ -1331,6 +1332,7 @@ HFILES_NO_SRCDIR = \
remote.h \
remote-fileio.h \
remote-notif.h \
riscv-tdep.h \
rs6000-aix-tdep.h \
rs6000-tdep.h \
s390-linux-tdep.h \
@ -2302,6 +2304,7 @@ ALLDEPFILES = \
procfs.c \
ravenscar-thread.c \
remote-sim.c \
riscv-tdep.c \
rl78-tdep.c \
rs6000-lynx178-tdep.c \
rs6000-nat.c \

View File

@ -12,6 +12,10 @@ set debug fbsd-nat
show debug fbsd-nat
Control display of debugging info regarding the FreeBSD native target.
* New targets
RiscV ELF riscv*-*-elf
*** Changes in GDB 8.1
* GDB now supports dynamically creating arbitrary register groups specified

View File

@ -520,6 +520,11 @@ s390*-*-linux*)
build_gdbserver=yes
;;
riscv*-*-*)
# Target: RISC-V architecture
gdb_target_obs="riscv-tdep.o"
;;
rl78-*-elf)
# Target: Renesas rl78
gdb_target_obs="rl78-tdep.o"

2749
gdb/riscv-tdep.c Normal file

File diff suppressed because it is too large Load Diff

84
gdb/riscv-tdep.h Normal file
View File

@ -0,0 +1,84 @@
/* Target-dependent header for the RISC-V architecture, for GDB, the GNU Debugger.
Copyright (C) 2018 Free Software Foundation, Inc.
Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin
and by Todd Snyder <todd@bluespec.com>
and by Mike Frysinger <vapier@gentoo.org>.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef RISCV_TDEP_H
#define RISCV_TDEP_H
/* RiscV register numbers. */
enum
{
RISCV_ZERO_REGNUM = 0, /* Read-only register, always 0. */
RISCV_RA_REGNUM = 1, /* Return Address. */
RISCV_SP_REGNUM = 2, /* Stack Pointer. */
RISCV_GP_REGNUM = 3, /* Global Pointer. */
RISCV_TP_REGNUM = 4, /* Thread Pointer. */
RISCV_FP_REGNUM = 8, /* Frame Pointer. */
RISCV_A0_REGNUM = 10, /* First argument. */
RISCV_A1_REGNUM = 11, /* Second argument. */
RISCV_PC_REGNUM = 32, /* Program Counter. */
RISCV_FIRST_FP_REGNUM = 33, /* First Floating Point Register */
RISCV_FA0_REGNUM = 43,
RISCV_FA1_REGNUM = RISCV_FA0_REGNUM + 1,
RISCV_LAST_FP_REGNUM = 64, /* Last Floating Point Register */
RISCV_FIRST_CSR_REGNUM = 65, /* First CSR */
#define DECLARE_CSR(name, num) RISCV_ ## num ## _REGNUM = RISCV_LAST_FP_REGNUM + 1 + num,
#include "opcode/riscv-opc.h"
#undef DECLARE_CSR
RISCV_LAST_CSR_REGNUM = 4160,
RISCV_CSR_LEGACY_MISA_REGNUM = 0xf10,
RISCV_PRIV_REGNUM = 4161,
RISCV_LAST_REGNUM = RISCV_PRIV_REGNUM
};
/* RISC-V specific per-architecture information. */
struct gdbarch_tdep
{
union
{
/* Provide access to the whole ABI in one value. */
unsigned value;
struct
{
/* Encode the base machine length following the same rules as in the
MISA register. */
unsigned base_len : 2;
/* Encode which floating point ABI is in use following the same rules
as the ELF e_flags field. */
unsigned float_abi : 2;
} fields;
} abi;
/* Only the least significant 26 bits are (possibly) valid, and indicate
features that are supported on the target. These could be cached from
the target, or read from the executable when available. */
unsigned core_features;
};
#endif /* RISCV_TDEP_H */

View File

@ -1,3 +1,9 @@
2018-03-06 Andrew Burgess <andrew.burgess@embecosm.com>
* gdb.base/infcall-nested-structs.exp: New file.
* gdb.base/infcall-nested-structs.c: New file.
* gdb.base/float.exp: Add riscv support.
2018-03-02 Thomas Preud'homme <thomas.preudhomme@arm.com>
* gdb.dlang/watch-loc.c: Use %progbits instead of @progbits.

View File

@ -110,6 +110,8 @@ if { [is_aarch64_target] } then {
gdb_test "info float" "fr4.*fr4R.*fr31R.*" "info float"
} elseif [istarget "sparc*-*-*"] then {
gdb_test "info float" "f0.*f1.*f31.*d0.*d30.*" "info float"
} elseif [istarget "riscv*-*-*"] then {
gdb_test "info float" "ft0.*ft1.*ft11.*fflags.*frm.*fcsr.*" "info float"
} else {
gdb_test "info float" "No floating.point info available for this processor." "info float (unknown target)"
}

View File

@ -0,0 +1,157 @@
/* This testcase is part of GDB, the GNU debugger.
Copyright 2018 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* This file is used for testing GDBs ability to pass structures to, and
return structures from, functions. All of the structures in this test
are special in that they are small structures containing only 1 or 2
scalar fields, the fields can be inside nested structures, and there can
be empty structures around too.
This test was originally written for RiscV which has some special ABI
rules for structures like these, however, there should be no harm in
running these tests on other targets, though in many cases the
structures will treated no differently to the structures already covered
in the structs.exp test script. */
#include <string.h>
/* Useful abreviations. */
typedef char tc;
typedef short ts;
typedef int ti;
typedef long tl;
typedef long long tll;
typedef float tf;
typedef double td;
typedef long double tld;
#ifdef TEST_COMPLEX
typedef float _Complex tfc;
typedef double _Complex tdc;
typedef long double _Complex tldc;
#endif /* TEST_COMPLEX */
#define MAKE_CHECK_FUNCS(TYPE) \
int \
check_arg_ ## TYPE (struct TYPE arg) \
{ \
return cmp_ ## TYPE (arg, ref_val_ ## TYPE); \
} \
\
struct TYPE \
rtn_str_ ## TYPE (void) \
{ \
return (ref_val_ ## TYPE); \
}
#define REF_VAL(NAME) struct NAME ref_val_ ## NAME
#define ES(NAME) struct { } NAME
#if defined tA && ! defined tB
/* Structures with a single field nested to various depths, along with
some empty structures. */
struct struct01 { ES(es1); struct { struct { tA a; } s1; } s2; };
struct struct02 { tA a; struct { struct { ES(es1); } s1; } s2; };
struct struct03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4;};
struct struct04 { ES(es1); ES(es2); tA a; ES(es3); };
int cmp_struct01 (struct struct01 a, struct struct01 b)
{ return a.s2.s1.a == b.s2.s1.a; }
int cmp_struct02 (struct struct02 a, struct struct02 b)
{ return a.a == b.a; }
int cmp_struct03 (struct struct03 a, struct struct03 b)
{ return a.s4.s3.a == b.s4.s3.a; }
int cmp_struct04 (struct struct04 a, struct struct04 b)
{ return a.a == b.a; }
REF_VAL(struct01) = { {}, { { 'a' } } };
REF_VAL(struct02) = { 'a', { { {} } } };
REF_VAL(struct03) = { { { {} } }, {}, { { 'a' } } };
REF_VAL(struct04) = { {}, {}, 'a', {} };
#elif defined tA && defined tB
/* These structures all have 2 fields, nested to various depths, along
with some empty structures. */
struct struct01 { struct { tA a; } s1; ES (e1); struct { struct { tB b; } s2;} s3;};
struct struct02 { struct { struct { tA a; } s1; ES(e1); } s2; struct { struct { tB b; } s3;} s4; ES(e2);};
struct struct03 { ES(e1); tA a; ES (e2); struct { struct { tB b; } s2;} s3;};
struct struct04 { ES(e1); ES (e2); struct { struct { struct { tA a; struct { ES(e3); } s1; tB b; } s2; } s3;} s4;};
int cmp_struct01 (struct struct01 a, struct struct01 b)
{ return a.s1.a == b.s1.a && a.s3.s2.b == b.s3.s2.b; }
int cmp_struct02 (struct struct02 a, struct struct02 b)
{ return a.s2.s1.a == b.s2.s1.a && a.s4.s3.b == b.s4.s3.b; }
int cmp_struct03 (struct struct03 a, struct struct03 b)
{ return a.a == b.a && a.s3.s2.b == b.s3.s2.b; }
int cmp_struct04 (struct struct04 a, struct struct04 b)
{ return a.s4.s3.s2.a == b.s4.s3.s2.a && a.s4.s3.s2.b == b.s4.s3.s2.b; }
REF_VAL(struct01) = { { 'a' }, {}, { { '1' } } };
REF_VAL(struct02) = { { { 'a' }, {} }, { { '1' } }, {} };
REF_VAL(struct03) = { {}, 'a', {}, { { '1' } } };
REF_VAL(struct04) = { {}, {}, { { { 'a', {}, '1'} } } } ;
#else
#error "Incorrect configuration of tA and tB defines"
#endif
/* Create all of the functions GDB will call to check functionality. */
MAKE_CHECK_FUNCS(struct01)
MAKE_CHECK_FUNCS(struct02)
MAKE_CHECK_FUNCS(struct03)
MAKE_CHECK_FUNCS(struct04)
#define CALL_LINE(NAME) val += check_arg_ ## NAME (rtn_str_ ## NAME ())
int
call_all ()
{
int val;
CALL_LINE(struct01);
CALL_LINE(struct02);
CALL_LINE(struct03);
CALL_LINE(struct04);
return (val != 4);
}
void
breakpt (void)
{
/* Nothing. */
}
int
main ()
{
int res;
res = call_all ();
breakpt (); /* Break Here. */
return res;
}

View File

@ -0,0 +1,169 @@
# This testcase is part of GDB, the GNU debugger.
# Copyright 2018 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Some targets can't call functions, so don't even bother with this
# test.
if [target_info exists gdb,cannot_call_functions] {
unsupported "this target can not call functions"
continue
}
set int_types { tc ts ti tl tll }
set float_types { tf td tld }
set complex_types { tfc tdc tldc }
set compile_flags {debug}
if [support_complex_tests] {
lappend compile_flags "additional_flags=-DTEST_COMPLEX"
}
# Given N (0..25), return the corresponding alphabetic letter in upper
# case.
proc I2A { n } {
return [string range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" $n $n]
}
# Compile a variant of nested-structs.c using TYPES to specify the
# types of the struct fields within the source. Run up to main.
# Also updates the global "testfile" to reflect the most recent build.
proc start_nested_structs_test { types } {
global testfile
global srcfile
global binfile
global subdir
global srcdir
global compile_flags
standard_testfile .c
# Create the additional flags
set flags $compile_flags
for {set n 0} {$n<[llength ${types}]} {incr n} {
set m [I2A ${n}]
set t [lindex ${types} $n]
lappend flags "additional_flags=-Dt${m}=${t}"
append testfile "-" "$t"
}
set binfile [standard_output_file ${testfile}]
if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "${flags}"] != "" } {
unresolved "failed to compile"
return 0
}
# Start with a fresh gdb.
clean_restart ${binfile}
# Make certain that the output is consistent
gdb_test_no_output "set print sevenbit-strings"
gdb_test_no_output "set print address off"
gdb_test_no_output "set print pretty off"
gdb_test_no_output "set width 0"
gdb_test_no_output "set print elements 300"
# Advance to main
if { ![runto_main] } then {
fail "can't run to main"
return 0
}
# Now continue forward to a suitable location to run the tests.
# Some targets only enable the FPU on first use, so ensure that we
# have used the FPU before we make calls from GDB to code that
# could use the FPU.
gdb_breakpoint [gdb_get_line_number "Break Here"] temporary
gdb_continue_to_breakpoint "breakpt" ".* Break Here\\. .*"
return 1
}
# Assuming GDB is stopped at main within a test binary, run some tests
# passing structures, and reading return value structures.
proc run_tests {} {
global gdb_prompt
foreach {name} {struct01 struct02 struct03 struct04} {
gdb_test "p/d check_arg_${name} (ref_val_${name})" "= 1"
set refval [ get_valueof "" "ref_val_${name}" "" ]
verbose -log "Refval: ${refval}"
set test "check return value ${name}"
if { ${refval} != "" } {
set answer [ get_valueof "" "rtn_str_${name} ()" "XXXX"]
verbose -log "Answer: ${answer}"
gdb_assert [string eq ${answer} ${refval}] ${test}
} else {
unresolved $test
}
}
}
# Set up a test prefix, compile the test binary, run to main, and then
# run some tests.
proc start_gdb_and_run_tests { types } {
set prefix "types"
foreach t $types {
append prefix "-" "${t}"
}
with_test_prefix $prefix {
if { [start_nested_structs_test $types] } {
run_tests
}
}
}
foreach ta $int_types {
start_gdb_and_run_tests $ta
}
if [support_complex_tests] {
foreach ta $complex_types {
start_gdb_and_run_tests $ta
}
}
if ![gdb_skip_float_test] {
foreach ta $float_types {
start_gdb_and_run_tests $ta
}
foreach ta $int_types {
foreach tb $float_types {
start_gdb_and_run_tests [list $ta $tb]
}
}
foreach ta $float_types {
foreach tb $int_types {
start_gdb_and_run_tests [list $ta $tb]
}
foreach tb $float_types {
start_gdb_and_run_tests [list $ta $tb]
}
}
}