Blame SOURCES/gdb-rhbz1480496-power-atomic-step-4of5.patch

dc7ef9
commit 7f03bd92e389a32da490bb55037881cf374d0f69
dc7ef9
Author: Pedro Alves <palves@redhat.com>
dc7ef9
Date:   Thu Aug 6 18:23:00 2015 +0100
dc7ef9
dc7ef9
    PPC64: Fix gdb.arch/ppc64-atomic-inst.exp with displaced stepping
dc7ef9
    
dc7ef9
    The ppc64 displaced step code can't handle atomic sequences.  Fallback
dc7ef9
    to stepping over the breakpoint in-line if we detect one.
dc7ef9
    
dc7ef9
    gdb/ChangeLog:
dc7ef9
    2015-08-07  Pedro Alves  <palves@redhat.com>
dc7ef9
    
dc7ef9
            * infrun.c (displaced_step_prepare_throw): Return -1 if
dc7ef9
            gdbarch_displaced_step_copy_insn returns NULL.  Update intro
dc7ef9
            comment.
dc7ef9
            * rs6000-tdep.c (LWARX_MASK, LWARX_INSTRUCTION, LDARX_INSTRUCTION)
dc7ef9
            (STWCX_MASK, STWCX_INSTRUCTION, STDCX_INSTRUCTION): Move higher up
dc7ef9
            in file.
dc7ef9
            (ppc_displaced_step_copy_insn): New function.
dc7ef9
            (ppc_displaced_step_fixup): Update comment.
dc7ef9
            (rs6000_gdbarch_init): Install ppc_displaced_step_copy_insn as
dc7ef9
            gdbarch_displaced_step_copy_insn hook.
dc7ef9
            * gdbarch.sh (displaced_step_copy_insn): Document what happens on
dc7ef9
            NULL return.
dc7ef9
            * gdbarch.h: Regenerate.
dc7ef9
    
dc7ef9
    gdb/testsuite/ChangeLog:
dc7ef9
    2015-08-07  Pedro Alves  <palves@redhat.com>
dc7ef9
    
dc7ef9
            * gdb.arch/ppc64-atomic-inst.exp (do_test): New procedure, move
dc7ef9
            tests here.
dc7ef9
            (top level): Run do_test with and without displaced stepping.
dc7ef9
dc7ef9
### a/gdb/ChangeLog
dc7ef9
### b/gdb/ChangeLog
dc7ef9
## -1,5 +1,21 @@
dc7ef9
 2015-08-07  Pedro Alves  <palves@redhat.com>
dc7ef9
 
dc7ef9
+	* infrun.c (displaced_step_prepare_throw): Return -1 if
dc7ef9
+	gdbarch_displaced_step_copy_insn returns NULL.  Update intro
dc7ef9
+	comment.
dc7ef9
+	* rs6000-tdep.c (LWARX_MASK, LWARX_INSTRUCTION, LDARX_INSTRUCTION)
dc7ef9
+	(STWCX_MASK, STWCX_INSTRUCTION, STDCX_INSTRUCTION): Move higher up
dc7ef9
+	in file.
dc7ef9
+	(ppc_displaced_step_copy_insn): New function.
dc7ef9
+	(ppc_displaced_step_fixup): Update comment.
dc7ef9
+	(rs6000_gdbarch_init): Install ppc_displaced_step_copy_insn as
dc7ef9
+	gdbarch_displaced_step_copy_insn hook.
dc7ef9
+	* gdbarch.sh (displaced_step_copy_insn): Document what happens on
dc7ef9
+	NULL return.
dc7ef9
+	* gdbarch.h: Regenerate.
dc7ef9
+
dc7ef9
+2015-08-07  Pedro Alves  <palves@redhat.com>
dc7ef9
+
dc7ef9
 	* inferior.h (struct inferior) <displaced_stepping_failed>: New
dc7ef9
 	field.
dc7ef9
 	* infrun.c (use_displaced_stepping_now_p): New parameter 'inf'.
dc7ef9
Index: gdb-7.6.1/gdb/gdbarch.h
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/gdbarch.h	2017-08-29 21:41:21.400218851 +0200
dc7ef9
+++ gdb-7.6.1/gdb/gdbarch.h	2017-08-29 21:41:29.832299833 +0200
dc7ef9
@@ -870,7 +870,11 @@
dc7ef9
   
dc7ef9
    If your architecture doesn't need to adjust instructions before
dc7ef9
    single-stepping them, consider using simple_displaced_step_copy_insn
dc7ef9
-   here. */
dc7ef9
+   here.
dc7ef9
+
dc7ef9
+   If the instruction cannot execute out of line, return NULL.  The
dc7ef9
+   core falls back to stepping past the instruction in-line instead in
dc7ef9
+   that case. */
dc7ef9
 
dc7ef9
 extern int gdbarch_displaced_step_copy_insn_p (struct gdbarch *gdbarch);
dc7ef9
 
dc7ef9
Index: gdb-7.6.1/gdb/gdbarch.sh
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/gdbarch.sh	2017-08-29 21:41:21.401218861 +0200
dc7ef9
+++ gdb-7.6.1/gdb/gdbarch.sh	2017-08-29 21:41:29.833299843 +0200
dc7ef9
@@ -724,6 +724,10 @@
dc7ef9
 # If your architecture doesn't need to adjust instructions before
dc7ef9
 # single-stepping them, consider using simple_displaced_step_copy_insn
dc7ef9
 # here.
dc7ef9
+#
dc7ef9
+# If the instruction cannot execute out of line, return NULL.  The
dc7ef9
+# core falls back to stepping past the instruction in-line instead in
dc7ef9
+# that case.
dc7ef9
 M:struct displaced_step_closure *:displaced_step_copy_insn:CORE_ADDR from, CORE_ADDR to, struct regcache *regs:from, to, regs
dc7ef9
 
dc7ef9
 # Return true if GDB should use hardware single-stepping to execute
dc7ef9
Index: gdb-7.6.1/gdb/infrun.c
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/infrun.c	2017-08-29 21:41:21.404218890 +0200
dc7ef9
+++ gdb-7.6.1/gdb/infrun.c	2017-08-29 21:41:29.835299862 +0200
dc7ef9
@@ -1323,7 +1323,9 @@
dc7ef9
    explain how we handle this case instead.
dc7ef9
 
dc7ef9
    Returns 1 if preparing was successful -- this thread is going to be
dc7ef9
-   stepped now; or 0 if displaced stepping this thread got queued.  */
dc7ef9
+   stepped now; 0 if displaced stepping this thread got queued; or -1
dc7ef9
+   if this instruction can't be displaced stepped.  */
dc7ef9
+
dc7ef9
 static int
dc7ef9
 displaced_step_prepare (ptid_t ptid)
dc7ef9
 {
dc7ef9
@@ -1412,9 +1414,14 @@
dc7ef9
 
dc7ef9
   closure = gdbarch_displaced_step_copy_insn (gdbarch,
dc7ef9
 					      original, copy, regcache);
dc7ef9
-
dc7ef9
-  /* We don't support the fully-simulated case at present.  */
dc7ef9
-  gdb_assert (closure);
dc7ef9
+  if (closure == NULL)
dc7ef9
+    {
dc7ef9
+      /* The architecture doesn't know how or want to displaced step
dc7ef9
+	 this instruction or instruction sequence.  Fallback to
dc7ef9
+	 stepping over the breakpoint in-line.  */
dc7ef9
+      do_cleanups (old_cleanups);
dc7ef9
+      return -1;
dc7ef9
+    }
dc7ef9
 
dc7ef9
   /* Save the information we need to fix things up if the step
dc7ef9
      succeeds.  */
dc7ef9
Index: gdb-7.6.1/gdb/rs6000-tdep.c
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/rs6000-tdep.c	2017-08-29 21:41:21.407218918 +0200
dc7ef9
+++ gdb-7.6.1/gdb/rs6000-tdep.c	2017-08-29 21:41:29.837299881 +0200
dc7ef9
@@ -975,6 +975,61 @@
dc7ef9
 #define BXL_INSN 0x4c000000
dc7ef9
 #define BP_INSN 0x7C000008
dc7ef9
 
dc7ef9
+/* Instruction masks used during single-stepping of atomic
dc7ef9
+   sequences.  */
dc7ef9
+#define LWARX_MASK 0xfc0007fe
dc7ef9
+#define LWARX_INSTRUCTION 0x7c000028
dc7ef9
+#define LDARX_INSTRUCTION 0x7c0000A8
dc7ef9
+#define STWCX_MASK 0xfc0007ff
dc7ef9
+#define STWCX_INSTRUCTION 0x7c00012d
dc7ef9
+#define STDCX_INSTRUCTION 0x7c0001ad
dc7ef9
+
dc7ef9
+/* We can't displaced step atomic sequences.  Otherwise this is just
dc7ef9
+   like simple_displaced_step_copy_insn.  */
dc7ef9
+
dc7ef9
+static struct displaced_step_closure *
dc7ef9
+ppc_displaced_step_copy_insn (struct gdbarch *gdbarch,
dc7ef9
+			      CORE_ADDR from, CORE_ADDR to,
dc7ef9
+			      struct regcache *regs)
dc7ef9
+{
dc7ef9
+  size_t len = gdbarch_max_insn_length (gdbarch);
dc7ef9
+  gdb_byte *buf = xmalloc (len);
dc7ef9
+  struct cleanup *old_chain = make_cleanup (xfree, buf);
dc7ef9
+  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
dc7ef9
+  int insn;
dc7ef9
+
dc7ef9
+  read_memory (from, buf, len);
dc7ef9
+
dc7ef9
+  insn = extract_signed_integer (buf, PPC_INSN_SIZE, byte_order);
dc7ef9
+
dc7ef9
+  /* Assume all atomic sequences start with a lwarx/ldarx instruction.  */
dc7ef9
+  if ((insn & LWARX_MASK) == LWARX_INSTRUCTION
dc7ef9
+      || (insn & LWARX_MASK) == LDARX_INSTRUCTION)
dc7ef9
+    {
dc7ef9
+      if (debug_displaced)
dc7ef9
+	{
dc7ef9
+	  fprintf_unfiltered (gdb_stdlog,
dc7ef9
+			      "displaced: can't displaced step "
dc7ef9
+			      "atomic sequence at %s\n",
dc7ef9
+			      paddress (gdbarch, from));
dc7ef9
+	}
dc7ef9
+      do_cleanups (old_chain);
dc7ef9
+      return NULL;
dc7ef9
+    }
dc7ef9
+
dc7ef9
+  write_memory (to, buf, len);
dc7ef9
+
dc7ef9
+  if (debug_displaced)
dc7ef9
+    {
dc7ef9
+      fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
dc7ef9
+                          paddress (gdbarch, from), paddress (gdbarch, to));
dc7ef9
+      displaced_step_dump_bytes (gdb_stdlog, buf, len);
dc7ef9
+    }
dc7ef9
+
dc7ef9
+  discard_cleanups (old_chain);
dc7ef9
+  return (struct displaced_step_closure *) buf;
dc7ef9
+}
dc7ef9
+
dc7ef9
 /* Fix up the state of registers and memory after having single-stepped
dc7ef9
    a displaced instruction.  */
dc7ef9
 static void
dc7ef9
@@ -984,8 +1039,7 @@
dc7ef9
 			  struct regcache *regs)
dc7ef9
 {
dc7ef9
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
dc7ef9
-  /* Since we use simple_displaced_step_copy_insn, our closure is a
dc7ef9
-     copy of the instruction.  */
dc7ef9
+  /* Our closure is a copy of the instruction.  */
dc7ef9
   ULONGEST insn  = extract_unsigned_integer ((gdb_byte *) closure,
dc7ef9
 					      PPC_INSN_SIZE, byte_order);
dc7ef9
   ULONGEST opcode = 0;
dc7ef9
@@ -1078,14 +1132,6 @@
dc7ef9
   return 1;
dc7ef9
 }
dc7ef9
 
dc7ef9
-/* Instruction masks used during single-stepping of atomic sequences.  */
dc7ef9
-#define LWARX_MASK 0xfc0007fe
dc7ef9
-#define LWARX_INSTRUCTION 0x7c000028
dc7ef9
-#define LDARX_INSTRUCTION 0x7c0000A8
dc7ef9
-#define STWCX_MASK 0xfc0007ff
dc7ef9
-#define STWCX_INSTRUCTION 0x7c00012d
dc7ef9
-#define STDCX_INSTRUCTION 0x7c0001ad
dc7ef9
-
dc7ef9
 /* Checks for an atomic sequence of instructions beginning with a LWARX/LDARX
dc7ef9
    instruction and ending with a STWCX/STDCX instruction.  If such a sequence
dc7ef9
    is found, attempt to step through it.  A breakpoint is placed at the end of 
dc7ef9
@@ -6427,7 +6473,7 @@
dc7ef9
 
dc7ef9
   /* Setup displaced stepping.  */
dc7ef9
   set_gdbarch_displaced_step_copy_insn (gdbarch,
dc7ef9
-					simple_displaced_step_copy_insn);
dc7ef9
+					ppc_displaced_step_copy_insn);
dc7ef9
   set_gdbarch_displaced_step_hw_singlestep (gdbarch,
dc7ef9
 					    ppc_displaced_step_hw_singlestep);
dc7ef9
   set_gdbarch_displaced_step_fixup (gdbarch, ppc_displaced_step_fixup);
dc7ef9
Index: gdb-7.6.1/gdb/testsuite/gdb.arch/ppc64-atomic-inst.exp
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/testsuite/gdb.arch/ppc64-atomic-inst.exp	2017-08-29 21:41:21.408218928 +0200
dc7ef9
+++ gdb-7.6.1/gdb/testsuite/gdb.arch/ppc64-atomic-inst.exp	2017-08-29 21:41:29.837299881 +0200
dc7ef9
@@ -32,27 +32,41 @@
dc7ef9
     return -1
dc7ef9
 }
dc7ef9
 
dc7ef9
-if ![runto_main] then {
dc7ef9
-    untested "could not run to main"
dc7ef9
-    return -1
dc7ef9
-}
dc7ef9
+# The test proper.  DISPLACED is true if we should try with displaced
dc7ef9
+# stepping.
dc7ef9
+proc do_test { displaced } {
dc7ef9
+    global decimal hex
dc7ef9
+
dc7ef9
+    if ![runto_main] then {
dc7ef9
+	untested "could not run to main"
dc7ef9
+	return -1
dc7ef9
+    }
dc7ef9
+
dc7ef9
+    gdb_test_no_output "set displaced-stepping $displaced"
dc7ef9
 
dc7ef9
-set bp1 [gdb_get_line_number "lwarx"]
dc7ef9
-gdb_breakpoint "$bp1" "Breakpoint $decimal at $hex" \
dc7ef9
-  "Set the breakpoint at the start of the lwarx/stwcx sequence"
dc7ef9
+    set bp1 [gdb_get_line_number "lwarx"]
dc7ef9
+    gdb_breakpoint "$bp1" "Breakpoint $decimal at $hex" \
dc7ef9
+	"Set the breakpoint at the start of the lwarx/stwcx sequence"
dc7ef9
 
dc7ef9
-set bp2 [gdb_get_line_number "ldarx"]
dc7ef9
-gdb_breakpoint "$bp2" "Breakpoint $decimal at $hex" \
dc7ef9
-  "Set the breakpoint at the start of the ldarx/stdcx sequence"
dc7ef9
+    set bp2 [gdb_get_line_number "ldarx"]
dc7ef9
+    gdb_breakpoint "$bp2" "Breakpoint $decimal at $hex" \
dc7ef9
+	"Set the breakpoint at the start of the ldarx/stdcx sequence"
dc7ef9
 
dc7ef9
-gdb_test continue "Continuing.*Breakpoint $decimal.*" \
dc7ef9
-  "Continue until lwarx/stwcx start breakpoint"
dc7ef9
+    gdb_test continue "Continuing.*Breakpoint $decimal.*" \
dc7ef9
+	"Continue until lwarx/stwcx start breakpoint"
dc7ef9
 
dc7ef9
-gdb_test nexti "bne.*1b" \
dc7ef9
-  "Step through the lwarx/stwcx sequence"
dc7ef9
+    gdb_test nexti "bne.*1b" \
dc7ef9
+	"Step through the lwarx/stwcx sequence"
dc7ef9
 
dc7ef9
-gdb_test continue "Continuing.*Breakpoint $decimal.*" \
dc7ef9
-  "Continue until ldarx/stdcx start breakpoint"
dc7ef9
+    gdb_test continue "Continuing.*Breakpoint $decimal.*" \
dc7ef9
+	"Continue until ldarx/stdcx start breakpoint"
dc7ef9
 
dc7ef9
-gdb_test nexti "bne.*1b" \
dc7ef9
-  "Step through the ldarx/stdcx sequence"
dc7ef9
+    gdb_test nexti "bne.*1b" \
dc7ef9
+	"Step through the ldarx/stdcx sequence"
dc7ef9
+}
dc7ef9
+
dc7ef9
+foreach displaced { "off" "on" } {
dc7ef9
+    with_test_prefix "displaced=$displaced" {
dc7ef9
+	do_test $displaced
dc7ef9
+    }
dc7ef9
+}