Blame SOURCES/gdb-rhbz1093259-aarch64-single-step-atomic-seq.patch

01917d
All attempts:
01917d
01917d
 - <https://sourceware.org/ml/gdb-patches/2014-03/msg00576.html>
01917d
   Message-ID: <20140324161056.GB23291@redacted.bos.redhat.com>
01917d
01917d
 - <https://sourceware.org/ml/gdb-patches/2014-03/msg00624.html>
01917d
   Message-ID: <20140327015125.GE3075@redacted.bos.redhat.com>
01917d
   (see below)
01917d
01917d
 - <https://sourceware.org/ml/gdb-patches/2014-04/msg00433.html>
01917d
   Message-ID: <20140422165542.GA748@redacted.bos.redhat.com>
01917d
01917d
 - <https://sourceware.org/ml/gdb-patches/2014-04/msg00505.html>
01917d
   Message-ID: <20140424183510.GI7588@redacted.bos.redhat.com>
01917d
01917d
 - <https://sourceware.org/ml/gdb-patches/2014-04/msg00642.html>
01917d
   Message-ID: <20140430160450.GE2148@redacted.bos.redhat.com>
01917d
   (last version, applied)
01917d
01917d
01917d
Second message from Kyle, which seems complete enough.
01917d
01917d
  Date: Wed, 26 Mar 2014 21:51:26 -0400
01917d
  From: Kyle McMartin <kmcmarti at redhat dot com>
01917d
  To: gdb-patches at sourceware dot org
01917d
  Subject: [PATCHv2] aarch64: detect atomic sequences like other ll/sc architectures
01917d
  Message-ID: <20140327015125.GE3075@redacted.bos.redhat.com>
01917d
01917d
  Add similar single-stepping over atomic sequences support like other
01917d
  load-locked/store-conditional architectures (alpha, powerpc, arm, etc.)
01917d
  do. Verified the decode_masked_match, and decode_bcond works against the
01917d
  atomic sequences used in the Linux kernel atomic.h, and also gcc
01917d
  libatomic. Thanks to Richard Henderson for feedback on my initial
01917d
  attempt at this patch, and for the feedback from gdb-patches, which I
01917d
  hope I've addressed...
01917d
01917d
01917d
01917d
commit 9404b58f46328b3b171b0d5eeb0691bd685bc4f5
01917d
Author: Kyle McMartin <kmcmarti@redhat.com>
01917d
Date:   Wed Apr 30 12:04:50 2014 -0400
01917d
01917d
    aarch64: detect atomic sequences like other ll/sc architectures
01917d
    
01917d
    gdb/Changelog:
01917d
    
01917d
            * aarch64-tdep.c (aarch64_software_single_step): New function.
01917d
            (aarch64_gdbarch_init): Handle single stepping of atomic sequences
01917d
            with aarch64_software_single_step.
01917d
    
01917d
    gdb/testsuite/ChangeLog:
01917d
    
01917d
            * gdb.arch/aarch64-atomic-inst.c: New file.
01917d
            * gdb.arch/aarch64-atomic-inst.exp: New file.
01917d
01917d
Index: gdb-7.6.1/gdb/aarch64-tdep.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/aarch64-tdep.c
01917d
+++ gdb-7.6.1/gdb/aarch64-tdep.c
01917d
@@ -2518,6 +2518,84 @@ value_of_aarch64_user_reg (struct frame_
01917d
 }
01917d
 
01917d
 
01917d
+/* Implement the "software_single_step" gdbarch method, needed to
01917d
+   single step through atomic sequences on AArch64.  */
01917d
+
01917d
+static int
01917d
+aarch64_software_single_step (struct frame_info *frame)
01917d
+{
01917d
+  struct gdbarch *gdbarch = get_frame_arch (frame);
01917d
+  struct address_space *aspace = get_frame_address_space (frame);
01917d
+  enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
01917d
+  const int insn_size = 4;
01917d
+  const int atomic_sequence_length = 16; /* Instruction sequence length.  */
01917d
+  CORE_ADDR pc = get_frame_pc (frame);
01917d
+  CORE_ADDR breaks[2] = { -1, -1 };
01917d
+  CORE_ADDR loc = pc;
01917d
+  CORE_ADDR closing_insn = 0;
01917d
+  uint32_t insn = read_memory_unsigned_integer (loc, insn_size,
01917d
+						byte_order_for_code);
01917d
+  int index;
01917d
+  int insn_count;
01917d
+  int bc_insn_count = 0; /* Conditional branch instruction count.  */
01917d
+  int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
01917d
+
01917d
+  /* Look for a Load Exclusive instruction which begins the sequence.  */
01917d
+  if (!decode_masked_match (insn, 0x3fc00000, 0x08400000))
01917d
+    return 0;
01917d
+
01917d
+  for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
01917d
+    {
01917d
+      int32_t offset;
01917d
+      unsigned cond;
01917d
+
01917d
+      loc += insn_size;
01917d
+      insn = read_memory_unsigned_integer (loc, insn_size,
01917d
+					   byte_order_for_code);
01917d
+
01917d
+      /* Check if the instruction is a conditional branch.  */
01917d
+      if (decode_bcond (loc, insn, &cond, &offset))
01917d
+	{
01917d
+	  if (bc_insn_count >= 1)
01917d
+	    return 0;
01917d
+
01917d
+	  /* It is, so we'll try to set a breakpoint at the destination.  */
01917d
+	  breaks[1] = loc + offset;
01917d
+
01917d
+	  bc_insn_count++;
01917d
+	  last_breakpoint++;
01917d
+	}
01917d
+
01917d
+      /* Look for the Store Exclusive which closes the atomic sequence.  */
01917d
+      if (decode_masked_match (insn, 0x3fc00000, 0x08000000))
01917d
+	{
01917d
+	  closing_insn = loc;
01917d
+	  break;
01917d
+	}
01917d
+    }
01917d
+
01917d
+  /* We didn't find a closing Store Exclusive instruction, fall back.  */
01917d
+  if (!closing_insn)
01917d
+    return 0;
01917d
+
01917d
+  /* Insert breakpoint after the end of the atomic sequence.  */
01917d
+  breaks[0] = loc + insn_size;
01917d
+
01917d
+  /* Check for duplicated breakpoints, and also check that the second
01917d
+     breakpoint is not within the atomic sequence.  */
01917d
+  if (last_breakpoint
01917d
+      && (breaks[1] == breaks[0]
01917d
+	  || (breaks[1] >= pc && breaks[1] <= closing_insn)))
01917d
+    last_breakpoint = 0;
01917d
+
01917d
+  /* Insert the breakpoint at the end of the sequence, and one at the
01917d
+     destination of the conditional branch, if it exists.  */
01917d
+  for (index = 0; index <= last_breakpoint; index++)
01917d
+    insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
01917d
+
01917d
+  return 1;
01917d
+}
01917d
+
01917d
 /* Initialize the current architecture based on INFO.  If possible,
01917d
    re-use an architecture from ARCHES, which is a list of
01917d
    architectures already created during this debugging session.
01917d
@@ -2635,6 +2713,7 @@ aarch64_gdbarch_init (struct gdbarch_inf
01917d
   set_gdbarch_breakpoint_from_pc (gdbarch, aarch64_breakpoint_from_pc);
01917d
   set_gdbarch_cannot_step_breakpoint (gdbarch, 1);
01917d
   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
01917d
+  set_gdbarch_software_single_step (gdbarch, aarch64_software_single_step);
01917d
 
01917d
   /* Information about registers, etc.  */
01917d
   set_gdbarch_sp_regnum (gdbarch, AARCH64_SP_REGNUM);
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.arch/aarch64-atomic-inst.c
01917d
===================================================================
01917d
--- /dev/null
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.arch/aarch64-atomic-inst.c
01917d
@@ -0,0 +1,48 @@
01917d
+/* This file is part of GDB, the GNU debugger.
01917d
+
01917d
+   Copyright 2008-2014 Free Software Foundation, Inc.
01917d
+
01917d
+   This program is free software; you can redistribute it and/or modify
01917d
+   it under the terms of the GNU General Public License as published by
01917d
+   the Free Software Foundation; either version 3 of the License, or
01917d
+   (at your option) any later version.
01917d
+
01917d
+   This program is distributed in the hope that it will be useful,
01917d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
01917d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
01917d
+   GNU General Public License for more details.
01917d
+
01917d
+   You should have received a copy of the GNU General Public License
01917d
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
01917d
+
01917d
+int main(void)
01917d
+{
01917d
+  unsigned long tmp, cond;
01917d
+  unsigned long dword = 0;
01917d
+
01917d
+  /* Test that we can step over ldxr/stxr. This sequence should step from
01917d
+     ldxr to the following __asm __volatile.  */
01917d
+  __asm __volatile ("1:     ldxr    %0,%2\n"                             \
01917d
+                    "       cmp     %0,#1\n"                             \
01917d
+                    "       b.eq    out\n"                               \
01917d
+                    "       add     %0,%0,1\n"                           \
01917d
+                    "       stxr    %w1,%0,%2\n"                         \
01917d
+                    "       cbnz    %w1,1b"                              \
01917d
+                    : "=&r" (tmp), "=&r" (cond), "+Q" (dword)            \
01917d
+                    : : "memory");
01917d
+
01917d
+  /* This sequence should take the conditional branch and step from ldxr
01917d
+     to the return dword line.  */
01917d
+  __asm __volatile ("1:     ldxr    %0,%2\n"                             \
01917d
+                    "       cmp     %0,#1\n"                             \
01917d
+                    "       b.eq    out\n"                               \
01917d
+                    "       add     %0,%0,1\n"                           \
01917d
+                    "       stxr    %w1,%0,%2\n"                         \
01917d
+                    "       cbnz    %w1,1b\n"                            \
01917d
+                    : "=&r" (tmp), "=&r" (cond), "+Q" (dword)            \
01917d
+                    : : "memory");
01917d
+
01917d
+  dword = -1;
01917d
+__asm __volatile ("out:\n");
01917d
+  return dword;
01917d
+}
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.arch/aarch64-atomic-inst.exp
01917d
===================================================================
01917d
--- /dev/null
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.arch/aarch64-atomic-inst.exp
01917d
@@ -0,0 +1,48 @@
01917d
+# Copyright 2008-2014 Free Software Foundation, Inc.
01917d
+#
01917d
+# This program is free software; you can redistribute it and/or modify
01917d
+# it under the terms of the GNU General Public License as published by
01917d
+# the Free Software Foundation; either version 3 of the License, or
01917d
+# (at your option) any later version.
01917d
+#
01917d
+# This program is distributed in the hope that it will be useful,
01917d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
01917d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
01917d
+# GNU General Public License for more details.
01917d
+#
01917d
+# You should have received a copy of the GNU General Public License
01917d
+# along with this program; if not, write to the Free Software
01917d
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
01917d
+#
01917d
+# This file is part of the gdb testsuite.
01917d
+
01917d
+# Test single stepping through atomic sequences beginning with
01917d
+# a ldxr instruction and ending with a stxr instruction.
01917d
+
01917d
+if {![istarget "aarch64*"]} {
01917d
+    verbose "Skipping ${gdb_test_file_name}."
01917d
+    return
01917d
+}
01917d
+
01917d
+standard_testfile
01917d
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
01917d
+    return -1
01917d
+}
01917d
+
01917d
+if ![runto_main] {
01917d
+    untested "could not run to main"
01917d
+    return -1
01917d
+}
01917d
+
01917d
+gdb_breakpoint "[gdb_get_line_number "ldxr"]" \
01917d
+  "Breakpoint $decimal at $hex" \
01917d
+  "Set the breakpoint at the start of the sequence"
01917d
+
01917d
+gdb_test "continue" "Continuing.*Breakpoint $decimal.*" \
01917d
+  "Continue until breakpoint"
01917d
+
01917d
+gdb_test "next" ".*__asm __volatile.*" \
01917d
+  "Step through the ldxr/stxr sequence"
01917d
+
01917d
+gdb_test "next" ".*return dword.*" \
01917d
+  "Stepped through sequence through conditional branch"