Blame SOURCES/gdb-rhbz1870031-p10-prefixed-insn-1of3.patch

405ea9
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
405ea9
From: Keith Seitz <keiths@redhat.com>
405ea9
Date: Thu, 6 May 2021 14:12:00 -0400
405ea9
Subject: gdb-rhbz1870031-p10-prefixed-insn-1of3.patch
405ea9
405ea9
;; Backport "displaced stepping across addpcis/lnia"
405ea9
;; (Will Schmidt, RHBZ 1870031)
405ea9
405ea9
   commit e3d528d7e6a6b863d30aaecf74adf8c78286f84c
405ea9
   Author: Will Schmidt <will_schmidt@vnet.ibm.com>
405ea9
   Date:   Mon Apr 12 13:35:54 2021 -0500
405ea9
405ea9
    [PATCH, rs6000, v3][PR gdb/27525] displaced stepping across addpcis/lnia.
405ea9
405ea9
      This addresses PR gdb/27525.     The lnia and other variations
405ea9
    of the addpcis instruction write the value of the NIA into a target register.
405ea9
    If we are single-stepping across a breakpoint, the instruction is executed
405ea9
    from a displaced location, and thusly the written value of the PC/NIA
405ea9
    will be incorrect.   The changes here will measure the displacement
405ea9
    offset, and adjust the target register value to compensate.
405ea9
405ea9
    YYYY-MM-DD  Will Schmidt  <will_schmidt@vnet.ibm.com>
405ea9
405ea9
    gdb/ChangeLog:
405ea9
405ea9
            * rs6000-tdep.c (ppc_displaced_step_fixup): Update to handle
405ea9
            the addpcis/lnia instruction.
405ea9
405ea9
    gdb/testsuite/ChangeLog:
405ea9
405ea9
            * gdb.arch/powerpc-addpcis.exp: Testcase harness to
405ea9
            exercise single-stepping over subpcis,lnia,addpcis instructions
405ea9
            with displacement.
405ea9
            * gdb.arch/powerpc-addpcis.s: Testcase with stream
405ea9
            of addpcis/lnia/subpcis instructions.
405ea9
            * gdb.arch/powerpc-lnia.exp: Testcase harness to exercise
405ea9
            single-stepping over lnia instructions with displacement.
405ea9
            * gdb.arch/powerpc-lnia.s: Testcase with stream of
405ea9
            lnia instructions.
405ea9
405ea9
diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c
405ea9
--- a/gdb/rs6000-tdep.c
405ea9
+++ b/gdb/rs6000-tdep.c
405ea9
@@ -836,6 +836,12 @@ typedef BP_MANIPULATION_ENDIAN (little_breakpoint, big_breakpoint)
405ea9
 #define STHCX_INSTRUCTION 0x7c0005ad
405ea9
 #define STQCX_INSTRUCTION 0x7c00016d
405ea9
 
405ea9
+/* Instruction masks for single-stepping of addpcis/lnia.  */
405ea9
+#define ADDPCIS_INSN            0x4c000004
405ea9
+#define ADDPCIS_INSN_MASK       0xfc00003e
405ea9
+#define ADDPCIS_TARGET_REGISTER 0x03F00000
405ea9
+#define ADDPCIS_INSN_REGSHIFT   21
405ea9
+
405ea9
 /* Check if insn is one of the Load And Reserve instructions used for atomic
405ea9
    sequences.  */
405ea9
 #define IS_LOAD_AND_RESERVE_INSN(insn)	((insn & LOAD_AND_RESERVE_MASK) == LWARX_INSTRUCTION \
405ea9
@@ -923,8 +929,31 @@ ppc_displaced_step_fixup (struct gdbarch *gdbarch,
405ea9
 			paddress (gdbarch, from), paddress (gdbarch, to));
405ea9
 
405ea9
 
405ea9
+  /* Handle the addpcis/lnia instruction.  */
405ea9
+  if ((insn & ADDPCIS_INSN_MASK) == ADDPCIS_INSN)
405ea9
+    {
405ea9
+      LONGEST displaced_offset;
405ea9
+      ULONGEST current_val;
405ea9
+      /* Measure the displacement.  */
405ea9
+      displaced_offset = from - to;
405ea9
+      /* Identify the target register that was updated by the instruction.  */
405ea9
+      int regnum = (insn & ADDPCIS_TARGET_REGISTER) >> ADDPCIS_INSN_REGSHIFT;
405ea9
+      /* Read and update the target value.  */
405ea9
+      regcache_cooked_read_unsigned (regs, regnum , &current_val);
405ea9
+      if (debug_displaced)
405ea9
+	fprintf_unfiltered (gdb_stdlog,
405ea9
+			    "displaced: {ppc} addpcis target regnum %d was "
405ea9
+			    "0x%lx now 0x%lx",
405ea9
+			    regnum, current_val,
405ea9
+			    current_val + displaced_offset);
405ea9
+      regcache_cooked_write_unsigned (regs, regnum,
405ea9
+					current_val + displaced_offset);
405ea9
+      /* point the PC back at the non-displaced instruction.  */
405ea9
+      regcache_cooked_write_unsigned (regs, gdbarch_pc_regnum (gdbarch),
405ea9
+				    from + offset);
405ea9
+    }
405ea9
   /* Handle PC-relative branch instructions.  */
405ea9
-  if (opcode == B_INSN || opcode == BC_INSN || opcode == BXL_INSN)
405ea9
+  else if (opcode == B_INSN || opcode == BC_INSN || opcode == BXL_INSN)
405ea9
     {
405ea9
       ULONGEST current_pc;
405ea9