From patchwork Fri Jul 19 10:57:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 33737 Received: (qmail 3445 invoked by alias); 19 Jul 2019 10:57:16 -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 3436 invoked by uid 89); 19 Jul 2019 10:57:16 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS, UNSUBSCRIBE_BODY autolearn=ham version=3.3.1 spammy=accommodate X-HELO: mail-wm1-f53.google.com Received: from mail-wm1-f53.google.com (HELO mail-wm1-f53.google.com) (209.85.128.53) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 19 Jul 2019 10:57:14 +0000 Received: by mail-wm1-f53.google.com with SMTP id f17so28585979wme.2 for ; Fri, 19 Jul 2019 03:57:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id; bh=28+zrVUaWnSoefGRzXOYTAQU2C7nCmqLIqky9ZTOtpM=; b=JHSNkAJhyx+sMUiui4uIT3J8AYZd9Ot8YHoZ4Tw4pBKo6mTfk+sldAxLJWo6xHRU9B W+V/19Hwesf3a76IEsMXYzKsU98XjRV78VQIMZFCYMbVdCfacsIqMXXAkBgY4kGML6To 5OnFywOc/r4apB8/QkwgslO2k4s/wFYIurR9WULMf0D5H1BhSA/L5s1JJrW8OLabZasI uXzWGavV/Pq6XfR4Oqo8D0u85jVthqeLiFa491tpiSEg/Z3qQpBC8AYN61JidNzXHUsc vBYrJZ5+LuWO3/LwB1eS/EW/4ysb6RXsOuNFXBlHG9RefL09J615yu/nhsYlvplOzLcB WL/g== Return-Path: Received: from localhost (host86-171-250-129.range86-171.btcentralplus.com. [86.171.250.129]) by smtp.gmail.com with ESMTPSA id g19sm31539783wmg.10.2019.07.19.03.57.11 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 19 Jul 2019 03:57:11 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Jim Wilson , Palmer Dabbelt , John Baldwin , Andrew Burgess Subject: [PATCH] gdb/riscv: Write 4-byte nop to dummy code region before inferior calls Date: Fri, 19 Jul 2019 11:57:07 +0100 Message-Id: <20190719105707.26806-1-andrew.burgess@embecosm.com> X-IsSubscribed: yes When making inferior function calls GDB sets up a dummy code region on the stack, and places a breakpoint within that region. If the random stack contents appear to be a compressed instruction then GDB will place a compressed instruction, which can cause problems if the target doesn't support compressed instructions. This commit prevents this issue by writing a 4-byte nop instruction to the dummy region at the time the region is allocated. With this nop instruction in place, when we come to insert the breakpoint then an uncompressed breakpoint will be used. This is similar to other targets, for example mips. gdb/ChangeLog: * riscv-tdep.c (riscv_push_dummy_code): Write a 4-byte nop instruction to the dummy code region. gdb/testsuite/ChangeLog: * gdb.arch/riscv-bp-infcall.c: New file. * gdb.arch/riscv-bp-infcall.exp: New file. --- gdb/ChangeLog | 5 +++ gdb/riscv-tdep.c | 35 +++++++++++++++++- gdb/testsuite/ChangeLog | 5 +++ gdb/testsuite/gdb.arch/riscv-bp-infcall.c | 29 +++++++++++++++ gdb/testsuite/gdb.arch/riscv-bp-infcall.exp | 56 +++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.arch/riscv-bp-infcall.c create mode 100644 gdb/testsuite/gdb.arch/riscv-bp-infcall.exp diff --git a/gdb/riscv-tdep.c b/gdb/riscv-tdep.c index 7f3a1f6cbc3..817e76a423d 100644 --- a/gdb/riscv-tdep.c +++ b/gdb/riscv-tdep.c @@ -1621,11 +1621,44 @@ riscv_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp, struct type *value_type, CORE_ADDR *real_pc, CORE_ADDR *bp_addr, struct regcache *regcache) { + /* A nop instruction is 'add x0, x0, 0'. */ + static gdb_byte nop_insn[] = { 0x13, 0x00, 0x00, 0x00 }; + /* Allocate space for a breakpoint, and keep the stack correctly - aligned. */ + aligned. The space allocated here must be at least big enough to + accommodate the NOP_INSN defined above. */ sp -= 16; *bp_addr = sp; *real_pc = funaddr; + + /* When we insert a breakpoint we select whether to use a compressed + breakpoint or not based on the existing contents of the memory. + + If the breakpoint is being placed onto the stack as part of setting up + for an inferior call from GDB, then the existing stack contents may + randomly appear to be a compressed instruction, causing GDB to insert + a compressed breakpoint. If this happens on a target that does not + support compressed instructions then this could cause problems. + + To prevent this issue we write an uncompressed nop onto the stack at + the location where the breakpoint will be inserted. In this way we + ensure that we always use an uncompressed breakpoint, which should + work on all targets. + + We call TARGET_WRITE_MEMORY here so that if the write fails we don't + throw an exception. Instead we ignore the error and move on. The + assumption is that either GDB will error later when actually trying to + insert a software breakpoint, or GDB will use hardware breakpoints and + there will be no need to write to memory later. */ + int status = target_write_memory (*bp_addr, nop_insn, sizeof (nop_insn)); + + if (riscv_debug_breakpoints || riscv_debug_infcall) + fprintf_unfiltered (gdb_stdlog, + "Writing %lld-byte nop instruction to %s: %s\n", + ((unsigned long long) sizeof (nop_insn)), + paddress (gdbarch, *bp_addr), + (status == 0 ? "success" : "failed")); + return sp; } diff --git a/gdb/testsuite/gdb.arch/riscv-bp-infcall.c b/gdb/testsuite/gdb.arch/riscv-bp-infcall.c new file mode 100644 index 00000000000..9bd40af7eed --- /dev/null +++ b/gdb/testsuite/gdb.arch/riscv-bp-infcall.c @@ -0,0 +1,29 @@ +/* This file is part of GDB, the GNU debugger. + + Copyright 2019 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 . */ + +void +dummy_call () +{ + asm ("" ::: "memory"); +} + +int +main () +{ + dummy_call (); + return 0; +} diff --git a/gdb/testsuite/gdb.arch/riscv-bp-infcall.exp b/gdb/testsuite/gdb.arch/riscv-bp-infcall.exp new file mode 100644 index 00000000000..6d41c477a13 --- /dev/null +++ b/gdb/testsuite/gdb.arch/riscv-bp-infcall.exp @@ -0,0 +1,56 @@ +# Copyright 2019 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 . + +# Test GDB for RISC-V always uses an uncompressed breakpoint when +# setting up for an inferior call. + +if {![istarget "riscv*-*-*"]} { + verbose "Skipping ${gdb_test_file_name}." + return +} + +standard_testfile + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} { + return -1 +} + +if ![runto_main] then { + fail "can't run to main" + return 0 +} + +# Figure out where the breakpoint will be placed taking account for +# stack alignment, and alloction of the dummy code area. +set bp_addr [get_valueof "/x" "\$sp" 0] +set bp_addr [format 0x%x [expr ($bp_addr & ~0xf) - 0x20]] + +# Fill the region we know will be used as the scratch area with the +# compressed nop instruction. If GDB fails to overwrite this with an +# uncompressed nop then a compressed breakpoint will be used in the +# following inferior call. +for {set i 0} {$i < 16} {incr i 2} { + gdb_test_no_output "set *((unsigned short *) (${bp_addr} + $i))=0x1" \ + "place compressed nop in scratch area at offset $i" +} + +# Make an inferior call. GDB should write an uncompressed nop into +# the scratch area and so force the use of an uncompressed breakpoint, +# however, if this mechanism fails and GDB uses a compressed +# breakpoint, and the target doesn't support compressed instructions, +# then we would expect weird things to happen here. +gdb_test_no_output "set debug riscv breakpoints 1" +gdb_test "call dummy_call ()" \ + ".*Using EBREAK for breakpoint at $bp_addr \\(instruction length 4\\).*"