]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/i386-tdep.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / i386-tdep.c
index 3462125feac5dbaa16c931d01c202016665c4d2a..f1f909e16169f201df468ab0ef5ca07a18e07ce2 100644 (file)
@@ -1,6 +1,6 @@
 /* Intel 386 target-dependent stuff.
 
-   Copyright (C) 1988-2023 Free Software Foundation, Inc.
+   Copyright (C) 1988-2024 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -17,7 +17,7 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-#include "defs.h"
+#include "extract-store-integer.h"
 #include "opcode/i386.h"
 #include "arch-utils.h"
 #include "command.h"
@@ -28,7 +28,7 @@
 #include "frame-unwind.h"
 #include "inferior.h"
 #include "infrun.h"
-#include "gdbcmd.h"
+#include "cli/cli-cmds.h"
 #include "gdbcore.h"
 #include "gdbtypes.h"
 #include "objfiles.h"
@@ -146,7 +146,7 @@ static const char * const i386_mmx_names[] =
 
 static const char * const i386_byte_names[] =
 {
-  "al", "cl", "dl", "bl", 
+  "al", "cl", "dl", "bl",
   "ah", "ch", "dh", "bh"
 };
 
@@ -389,7 +389,7 @@ i386_fpc_regnum_p (struct gdbarch *gdbarch, int regnum)
   if (I387_ST0_REGNUM (tdep) < 0)
     return 0;
 
-  return (I387_FCTRL_REGNUM (tdep) <= regnum 
+  return (I387_FCTRL_REGNUM (tdep) <= regnum
          && regnum < I387_XMM0_REGNUM (tdep));
 }
 
@@ -830,7 +830,7 @@ i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
 
   displaced_debug_printf ("%s->%s: %s",
                          paddress (gdbarch, from), paddress (gdbarch, to),
-                         displaced_step_dump_bytes (buf, len).c_str ());
+                         bytes_to_string (buf, len).c_str ());
 
   /* This is a work around for a problem with g++ 4.8.  */
   return displaced_step_copy_insn_closure_up (closure.release ());
@@ -843,7 +843,7 @@ void
 i386_displaced_step_fixup (struct gdbarch *gdbarch,
                           struct displaced_step_copy_insn_closure *closure_,
                           CORE_ADDR from, CORE_ADDR to,
-                          struct regcache *regs)
+                          struct regcache *regs, bool completed_p)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
 
@@ -886,14 +886,14 @@ i386_displaced_step_fixup (struct gdbarch *gdbarch,
      the displaced instruction; make it relative.  Well, signal
      handler returns don't need relocation either, but we use the
      value of %eip to recognize those; see below.  */
-  if (! i386_absolute_jmp_p (insn)
-      && ! i386_absolute_call_p (insn)
-      && ! i386_ret_p (insn))
+  if (!completed_p
+      || (!i386_absolute_jmp_p (insn)
+         && !i386_absolute_call_p (insn)
+         && !i386_ret_p (insn)))
     {
-      ULONGEST orig_eip;
       int insn_len;
 
-      regcache_cooked_read_unsigned (regs, I386_EIP_REGNUM, &orig_eip);
+      CORE_ADDR pc = regcache_read_pc (regs);
 
       /* A signal trampoline system call changes the %eip, resuming
         execution of the main program after the signal handler has
@@ -910,25 +910,25 @@ i386_displaced_step_fixup (struct gdbarch *gdbarch,
         it unrelocated.  Goodness help us if there are PC-relative
         system calls.  */
       if (i386_syscall_p (insn, &insn_len)
-         && orig_eip != to + (insn - insn_start) + insn_len
+         && pc != to + (insn - insn_start) + insn_len
          /* GDB can get control back after the insn after the syscall.
             Presumably this is a kernel bug.
-            i386_displaced_step_copy_insn ensures its a nop,
+            i386_displaced_step_copy_insn ensures it's a nop,
             we add one to the length for it.  */
-         && orig_eip != to + (insn - insn_start) + insn_len + 1)
+         && pc != to + (insn - insn_start) + insn_len + 1)
        displaced_debug_printf ("syscall changed %%eip; not relocating");
       else
        {
-         ULONGEST eip = (orig_eip - insn_offset) & 0xffffffffUL;
+         ULONGEST eip = (pc - insn_offset) & 0xffffffffUL;
 
          /* If we just stepped over a breakpoint insn, we don't backup
             the pc on purpose; this is to match behaviour without
             stepping.  */
 
-         regcache_cooked_write_unsigned (regs, I386_EIP_REGNUM, eip);
+         regcache_write_pc (regs, eip);
 
          displaced_debug_printf ("relocated %%eip from %s to %s",
-                                 paddress (gdbarch, orig_eip),
+                                 paddress (gdbarch, pc),
                                  paddress (gdbarch, eip));
        }
     }
@@ -941,7 +941,7 @@ i386_displaced_step_fixup (struct gdbarch *gdbarch,
   /* If the instruction was a call, the return address now atop the
      stack is the address following the copied instruction.  We need
      to make it the address following the original instruction.  */
-  if (i386_call_p (insn))
+  if (completed_p && i386_call_p (insn))
     {
       ULONGEST esp;
       ULONGEST retaddr;
@@ -1202,7 +1202,7 @@ i386_analyze_struct_return (CORE_ADDR pc, CORE_ADDR current_pc,
       cache->pc_in_eax = 1;
       return current_pc;
     }
-  
+
   if (buf[1] == proto1[1])
     return pc + 4;
   else
@@ -1217,7 +1217,7 @@ i386_skip_probe (CORE_ADDR pc)
        pushl constant
        call _probe
        addl $4, %esp
-          
+
      followed by
 
        pushl %ebp
@@ -1277,7 +1277,7 @@ i386_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
                pushl -4(%reg)
 
      "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
-     
+
        0x83 0xe4 0xf0                  andl $-16, %esp
        0x81 0xe4 0x00 0xff 0xff 0xff   andl $-256, %esp
    */
@@ -1328,7 +1328,7 @@ i386_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
       /* MOD must be binary 10 and R/M must be binary 100.  */
       if ((buf[2] & 0xc7) != 0x44)
        return pc;
-      
+
       /* REG has register number.  Registers in pushl and leal have to
         be the same.  */
       if (reg != ((buf[2] >> 3) & 7))
@@ -1473,7 +1473,7 @@ i386_match_insn_block (CORE_ADDR pc, struct i386_insn *insn_patterns)
 static i386_insn i386_frame_setup_skip_insns[] =
 {
   /* Check for `movb imm8, r' and `movl imm32, r'.
-    
+
      ??? Should we handle 16-bit operand-sizes here?  */
 
   /* `movb imm8, %al' and `movb imm8, %ah' */
@@ -1548,11 +1548,11 @@ i386_skip_noop (CORE_ADDR pc)
   if (target_read_code (pc, &op, 1))
     return pc;
 
-  while (check) 
+  while (check)
     {
       check = 0;
       /* Ignore `nop' instruction.  */
-      if (op == 0x90) 
+      if (op == 0x90)
        {
          pc += 1;
          if (target_read_code (pc, &op, 1))
@@ -1585,7 +1585,7 @@ i386_skip_noop (CORE_ADDR pc)
            }
        }
     }
-  return pc; 
+  return pc;
 }
 
 /* Check whether PC points at a code that sets up a new stack frame.
@@ -1694,7 +1694,7 @@ i386_analyze_frame_setup (struct gdbarch *gdbarch,
       if (limit <= pc)
        return limit;
 
-      /* Check for stack adjustment 
+      /* Check for stack adjustment
 
            subl $XXX, %esp
         or
@@ -1805,7 +1805,7 @@ i386_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
    %ebx (and sometimes a harmless bug causes it to also save but not
    restore %eax); however, the code below is willing to see the pushes
    in any order, and will handle up to 8 of them.
+
    If the setup sequence is at the end of the function, then the next
    instruction will be a branch back to the start.  */
 
@@ -1856,9 +1856,9 @@ i386_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
              && cust->producer () != NULL
              && (producer_is_llvm (cust->producer ())
              || producer_is_icc_ge_19 (cust->producer ()))))
-        return std::max (start_pc, post_prologue_pc);
+       return std::max (start_pc, post_prologue_pc);
     }
+
   cache.locals = -1;
   pc = i386_analyze_prologue (gdbarch, start_pc, 0xffffffff, &cache);
   if (cache.locals < 0)
@@ -1965,7 +1965,7 @@ i386_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
 /* This function is 64-bit safe.  */
 
 static CORE_ADDR
-i386_unwind_pc (struct gdbarch *gdbarch, frame_info_ptr next_frame)
+i386_unwind_pc (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
 {
   gdb_byte buf[8];
 
@@ -1977,7 +1977,7 @@ i386_unwind_pc (struct gdbarch *gdbarch, frame_info_ptr next_frame)
 /* Normal frames.  */
 
 static void
-i386_frame_cache_1 (frame_info_ptr this_frame,
+i386_frame_cache_1 (const frame_info_ptr &this_frame,
                    struct i386_frame_cache *cache)
 {
   struct gdbarch *gdbarch = get_frame_arch (this_frame);
@@ -2078,7 +2078,7 @@ i386_frame_cache_1 (frame_info_ptr this_frame,
 }
 
 static struct i386_frame_cache *
-i386_frame_cache (frame_info_ptr this_frame, void **this_cache)
+i386_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
 {
   struct i386_frame_cache *cache;
 
@@ -2102,7 +2102,7 @@ i386_frame_cache (frame_info_ptr this_frame, void **this_cache)
 }
 
 static void
-i386_frame_this_id (frame_info_ptr this_frame, void **this_cache,
+i386_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
                    struct frame_id *this_id)
 {
   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
@@ -2121,7 +2121,7 @@ i386_frame_this_id (frame_info_ptr this_frame, void **this_cache,
 }
 
 static enum unwind_stop_reason
-i386_frame_unwind_stop_reason (frame_info_ptr this_frame,
+i386_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
                               void **this_cache)
 {
   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
@@ -2137,7 +2137,7 @@ i386_frame_unwind_stop_reason (frame_info_ptr this_frame,
 }
 
 static struct value *
-i386_frame_prev_register (frame_info_ptr this_frame, void **this_cache,
+i386_frame_prev_register (const frame_info_ptr &this_frame, void **this_cache,
                          int regnum)
 {
   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
@@ -2219,12 +2219,6 @@ static int
 i386_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
 {
   gdb_byte insn;
-  struct compunit_symtab *cust;
-
-  cust = find_pc_compunit_symtab (pc);
-  if (cust != NULL && cust->epilogue_unwind_valid ())
-    return 0;
-
   if (target_read_memory (pc, &insn, 1))
     return 0;  /* Can't read memory at pc.  */
 
@@ -2234,20 +2228,59 @@ i386_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
   return 1;
 }
 
+static int
+i386_epilogue_frame_sniffer_1 (const struct frame_unwind *self,
+                              const frame_info_ptr &this_frame,
+                              void **this_prologue_cache, bool override_p)
+{
+  struct gdbarch *gdbarch = get_frame_arch (this_frame);
+  CORE_ADDR pc = get_frame_pc (this_frame);
+
+  if (frame_relative_level (this_frame) != 0)
+    /* We're not in the inner frame, so assume we're not in an epilogue.  */
+    return 0;
+
+  bool unwind_valid_p
+    = compunit_epilogue_unwind_valid (find_pc_compunit_symtab (pc));
+  if (override_p)
+    {
+      if (unwind_valid_p)
+       /* Don't override the symtab unwinders, skip
+          "i386 epilogue override".  */
+       return 0;
+    }
+  else
+    {
+      if (!unwind_valid_p)
+       /* "i386 epilogue override" unwinder already ran, skip
+          "i386 epilogue".  */
+       return 0;
+    }
+
+  /* Check whether we're in an epilogue.  */
+  return i386_stack_frame_destroyed_p (gdbarch, pc);
+}
+
+static int
+i386_epilogue_override_frame_sniffer (const struct frame_unwind *self,
+                                     const frame_info_ptr &this_frame,
+                                     void **this_prologue_cache)
+{
+  return i386_epilogue_frame_sniffer_1 (self, this_frame, this_prologue_cache,
+                                       true);
+}
+
 static int
 i386_epilogue_frame_sniffer (const struct frame_unwind *self,
-                            frame_info_ptr this_frame,
+                            const frame_info_ptr &this_frame,
                             void **this_prologue_cache)
 {
-  if (frame_relative_level (this_frame) == 0)
-    return i386_stack_frame_destroyed_p (get_frame_arch (this_frame),
-                                        get_frame_pc (this_frame));
-  else
-    return 0;
+  return i386_epilogue_frame_sniffer_1 (self, this_frame, this_prologue_cache,
+                                       false);
 }
 
 static struct i386_frame_cache *
-i386_epilogue_frame_cache (frame_info_ptr this_frame, void **this_cache)
+i386_epilogue_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
 {
   struct i386_frame_cache *cache;
   CORE_ADDR sp;
@@ -2282,7 +2315,7 @@ i386_epilogue_frame_cache (frame_info_ptr this_frame, void **this_cache)
 }
 
 static enum unwind_stop_reason
-i386_epilogue_frame_unwind_stop_reason (frame_info_ptr this_frame,
+i386_epilogue_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
                                        void **this_cache)
 {
   struct i386_frame_cache *cache =
@@ -2295,7 +2328,7 @@ i386_epilogue_frame_unwind_stop_reason (frame_info_ptr this_frame,
 }
 
 static void
-i386_epilogue_frame_this_id (frame_info_ptr this_frame,
+i386_epilogue_frame_this_id (const frame_info_ptr &this_frame,
                             void **this_cache,
                             struct frame_id *this_id)
 {
@@ -2309,7 +2342,7 @@ i386_epilogue_frame_this_id (frame_info_ptr this_frame,
 }
 
 static struct value *
-i386_epilogue_frame_prev_register (frame_info_ptr this_frame,
+i386_epilogue_frame_prev_register (const frame_info_ptr &this_frame,
                                   void **this_cache, int regnum)
 {
   /* Make sure we've initialized the cache.  */
@@ -2318,6 +2351,17 @@ i386_epilogue_frame_prev_register (frame_info_ptr this_frame,
   return i386_frame_prev_register (this_frame, this_cache, regnum);
 }
 
+static const struct frame_unwind i386_epilogue_override_frame_unwind =
+{
+  "i386 epilogue override",
+  NORMAL_FRAME,
+  i386_epilogue_frame_unwind_stop_reason,
+  i386_epilogue_frame_this_id,
+  i386_epilogue_frame_prev_register,
+  NULL,
+  i386_epilogue_override_frame_sniffer
+};
+
 static const struct frame_unwind i386_epilogue_frame_unwind =
 {
   "i386 epilogue",
@@ -2325,7 +2369,7 @@ static const struct frame_unwind i386_epilogue_frame_unwind =
   i386_epilogue_frame_unwind_stop_reason,
   i386_epilogue_frame_this_id,
   i386_epilogue_frame_prev_register,
-  NULL, 
+  NULL,
   i386_epilogue_frame_sniffer
 };
 \f
@@ -2391,7 +2435,7 @@ i386_in_stack_tramp_p (CORE_ADDR pc)
 
 static int
 i386_stack_tramp_frame_sniffer (const struct frame_unwind *self,
-                               frame_info_ptr this_frame,
+                               const frame_info_ptr &this_frame,
                                void **this_cache)
 {
   if (frame_relative_level (this_frame) == 0)
@@ -2407,7 +2451,7 @@ static const struct frame_unwind i386_stack_tramp_frame_unwind =
   i386_epilogue_frame_unwind_stop_reason,
   i386_epilogue_frame_this_id,
   i386_epilogue_frame_prev_register,
-  NULL, 
+  NULL,
   i386_stack_tramp_frame_sniffer
 };
 \f
@@ -2431,7 +2475,7 @@ i386_gen_return_address (struct gdbarch *gdbarch,
 /* Signal trampolines.  */
 
 static struct i386_frame_cache *
-i386_sigtramp_frame_cache (frame_info_ptr this_frame, void **this_cache)
+i386_sigtramp_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
 {
   struct gdbarch *gdbarch = get_frame_arch (this_frame);
   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
@@ -2480,7 +2524,7 @@ i386_sigtramp_frame_cache (frame_info_ptr this_frame, void **this_cache)
 }
 
 static enum unwind_stop_reason
-i386_sigtramp_frame_unwind_stop_reason (frame_info_ptr this_frame,
+i386_sigtramp_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
                                        void **this_cache)
 {
   struct i386_frame_cache *cache =
@@ -2493,7 +2537,7 @@ i386_sigtramp_frame_unwind_stop_reason (frame_info_ptr this_frame,
 }
 
 static void
-i386_sigtramp_frame_this_id (frame_info_ptr this_frame, void **this_cache,
+i386_sigtramp_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
                             struct frame_id *this_id)
 {
   struct i386_frame_cache *cache =
@@ -2509,7 +2553,7 @@ i386_sigtramp_frame_this_id (frame_info_ptr this_frame, void **this_cache,
 }
 
 static struct value *
-i386_sigtramp_frame_prev_register (frame_info_ptr this_frame,
+i386_sigtramp_frame_prev_register (const frame_info_ptr &this_frame,
                                   void **this_cache, int regnum)
 {
   /* Make sure we've initialized the cache.  */
@@ -2520,7 +2564,7 @@ i386_sigtramp_frame_prev_register (frame_info_ptr this_frame,
 
 static int
 i386_sigtramp_frame_sniffer (const struct frame_unwind *self,
-                            frame_info_ptr this_frame,
+                            const frame_info_ptr &this_frame,
                             void **this_prologue_cache)
 {
   gdbarch *arch = get_frame_arch (this_frame);
@@ -2562,7 +2606,7 @@ static const struct frame_unwind i386_sigtramp_frame_unwind =
 \f
 
 static CORE_ADDR
-i386_frame_base_address (frame_info_ptr this_frame, void **this_cache)
+i386_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
 {
   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
 
@@ -2578,7 +2622,7 @@ static const struct frame_base i386_frame_base =
 };
 
 static struct frame_id
-i386_dummy_id (struct gdbarch *gdbarch, frame_info_ptr this_frame)
+i386_dummy_id (struct gdbarch *gdbarch, const frame_info_ptr &this_frame)
 {
   CORE_ADDR fp;
 
@@ -2605,7 +2649,7 @@ i386_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
    success.  */
 
 static int
-i386_get_longjmp_target (frame_info_ptr frame, CORE_ADDR *pc)
+i386_get_longjmp_target (const frame_info_ptr &frame, CORE_ADDR *pc)
 {
   gdb_byte buf[4];
   CORE_ADDR sp, jb_addr;
@@ -2654,7 +2698,7 @@ i386_16_byte_align_p (struct type *type)
       int i;
       for (i = 0; i < type->num_fields (); i++)
        {
-         if (field_is_static (&type->field (i)))
+         if (type->field (i).is_static ())
            continue;
          if (i386_16_byte_align_p (type->field (i).type ()))
            return 1;
@@ -2733,7 +2777,7 @@ i386_thiscall_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
                args_space_used = align_up (args_space_used, 16);
 
              write_memory (sp + args_space_used,
-                           value_contents_all (args[i]).data (), len);
+                           args[i]->contents_all ().data (), len);
              /* The System V ABI says that:
 
              "An argument's size is increased, if necessary, to make it a
@@ -2778,7 +2822,7 @@ i386_thiscall_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
   /* The 'this' pointer needs to be in ECX.  */
   if (thiscall)
     regcache->cooked_write (I386_ECX_REGNUM,
-                           value_contents_all (args[0]).data ());
+                           args[0]->contents_all ().data ());
 
   /* If the PLT is position-independent, the SYSTEM V ABI requires %ebx to be
      set to the address of the GOT when doing a call to a PLT address.
@@ -3095,7 +3139,7 @@ i386_return_value (struct gdbarch *gdbarch, struct value *function,
     {
       *read_value = value::allocate (type);
       i386_extract_return_value (gdbarch, type, regcache,
-                                value_contents_raw (*read_value).data ());
+                                (*read_value)->contents_raw ().data ());
     }
   if (writebuf)
     i386_store_return_value (gdbarch, type, regcache, writebuf);
@@ -3351,18 +3395,15 @@ i386_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
    the MMX registers need to be mapped onto floating point registers.  */
 
 static int
-i386_mmx_regnum_to_fp_regnum (readable_regcache *regcache, int regnum)
+i386_mmx_regnum_to_fp_regnum (const frame_info_ptr &next_frame, int regnum)
 {
-  gdbarch *arch = regcache->arch ();
+  gdbarch *arch = frame_unwind_arch (next_frame);
   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
-  int mmxreg, fpreg;
-  ULONGEST fstat;
-  int tos;
-
-  mmxreg = regnum - tdep->mm0_regnum;
-  regcache->raw_read (I387_FSTAT_REGNUM (tdep), &fstat);
-  tos = (fstat >> 11) & 0x7;
-  fpreg = (mmxreg + tos) % 8;
+  ULONGEST fstat
+    = frame_unwind_register_unsigned (next_frame, I387_FSTAT_REGNUM (tdep));
+  int tos = (fstat >> 11) & 0x7;
+  int mmxreg = regnum - tdep->mm0_regnum;
+  int fpreg = (mmxreg + tos) % 8;
 
   return (I387_ST0_REGNUM (tdep) + fpreg);
 }
@@ -3371,316 +3412,197 @@ i386_mmx_regnum_to_fp_regnum (readable_regcache *regcache, int regnum)
    amd64_pseudo_register_read_value.  It does all the work but reads
    the data into an already-allocated value.  */
 
-void
-i386_pseudo_register_read_into_value (struct gdbarch *gdbarch,
-                                     readable_regcache *regcache,
-                                     int regnum,
-                                     struct value *result_value)
+value *
+i386_pseudo_register_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
+                                const int pseudo_reg_num)
 {
-  gdb_byte raw_buf[I386_MAX_REGISTER_SIZE];
-  enum register_status status;
-  gdb_byte *buf = value_contents_raw (result_value).data ();
-
-  if (i386_mmx_regnum_p (gdbarch, regnum))
+  if (i386_mmx_regnum_p (gdbarch, pseudo_reg_num))
     {
-      int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
+      int fpnum = i386_mmx_regnum_to_fp_regnum (next_frame, pseudo_reg_num);
 
       /* Extract (always little endian).  */
-      status = regcache->raw_read (fpnum, raw_buf);
-      if (status != REG_VALID)
-       mark_value_bytes_unavailable (result_value, 0,
-                                     result_value->type ()->length ());
-      else
-       memcpy (buf, raw_buf, register_size (gdbarch, regnum));
+      return pseudo_from_raw_part (next_frame, pseudo_reg_num, fpnum, 0);
     }
   else
     {
       i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
-      if (i386_bnd_regnum_p (gdbarch, regnum))
+      if (i386_bnd_regnum_p (gdbarch, pseudo_reg_num))
        {
-         regnum -= tdep->bnd0_regnum;
+         int i = pseudo_reg_num - tdep->bnd0_regnum;
 
          /* Extract (always little endian).  Read lower 128bits.  */
-         status = regcache->raw_read (I387_BND0R_REGNUM (tdep) + regnum,
-                                      raw_buf);
-         if (status != REG_VALID)
-           mark_value_bytes_unavailable (result_value, 0, 16);
-         else
-           {
-             enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
-             LONGEST upper, lower;
-             int size = builtin_type (gdbarch)->builtin_data_ptr->length ();
+         value *bndr_value
+           = value_of_register (I387_BND0R_REGNUM (tdep) + i, next_frame);
+         int size = builtin_type (gdbarch)->builtin_data_ptr->length ();
+         value *result
+           = value::allocate_register (next_frame, pseudo_reg_num);
 
-             lower = extract_unsigned_integer (raw_buf, 8, byte_order);
-             upper = extract_unsigned_integer (raw_buf + 8, 8, byte_order);
-             upper = ~upper;
+         /* Copy the lower. */
+         bndr_value->contents_copy (result, 0, 0, size);
+
+         /* Copy the upper.  */
+         bndr_value->contents_copy (result, size, 8, size);
 
-             memcpy (buf, &lower, size);
-             memcpy (buf + size, &upper, size);
+         /* If upper bytes are available, compute ones' complement.  */
+         if (result->bytes_available (size, size))
+           {
+             bfd_endian byte_order
+               = gdbarch_byte_order (frame_unwind_arch (next_frame));
+             gdb::array_view<gdb_byte> upper_bytes
+               = result->contents_raw ().slice (size, size);
+             ULONGEST upper
+               = extract_unsigned_integer (upper_bytes, byte_order);
+             upper = ~upper;
+             store_unsigned_integer (upper_bytes, byte_order, upper);
            }
-       }
-      else if (i386_k_regnum_p (gdbarch, regnum))
-       {
-         regnum -= tdep->k0_regnum;
 
-         /* Extract (always little endian).  */
-         status = regcache->raw_read (tdep->k0_regnum + regnum, raw_buf);
-         if (status != REG_VALID)
-           mark_value_bytes_unavailable (result_value, 0, 8);
-         else
-           memcpy (buf, raw_buf, 8);
+         return result;
        }
-      else if (i386_zmm_regnum_p (gdbarch, regnum))
+      else if (i386_zmm_regnum_p (gdbarch, pseudo_reg_num))
        {
-         regnum -= tdep->zmm0_regnum;
+         /* Which register is it, relative to zmm0.  */
+         int i_0 = pseudo_reg_num - tdep->zmm0_regnum;
 
-         if (regnum < num_lower_zmm_regs)
-           {
-             /* Extract (always little endian).  Read lower 128bits.  */
-             status = regcache->raw_read (I387_XMM0_REGNUM (tdep) + regnum,
-                                          raw_buf);
-             if (status != REG_VALID)
-               mark_value_bytes_unavailable (result_value, 0, 16);
-             else
-               memcpy (buf, raw_buf, 16);
-
-             /* Extract (always little endian).  Read upper 128bits.  */
-             status = regcache->raw_read (tdep->ymm0h_regnum + regnum,
-                                          raw_buf);
-             if (status != REG_VALID)
-               mark_value_bytes_unavailable (result_value, 16, 16);
-             else
-               memcpy (buf + 16, raw_buf, 16);
-           }
+         if (i_0 < num_lower_zmm_regs)
+           return pseudo_from_concat_raw (next_frame, pseudo_reg_num,
+                                          I387_XMM0_REGNUM (tdep) + i_0,
+                                          tdep->ymm0h_regnum + i_0,
+                                          tdep->zmm0h_regnum + i_0);
          else
            {
-             /* Extract (always little endian).  Read lower 128bits.  */
-             status = regcache->raw_read (I387_XMM16_REGNUM (tdep) + regnum
-                                          - num_lower_zmm_regs,
-                                          raw_buf);
-             if (status != REG_VALID)
-               mark_value_bytes_unavailable (result_value, 0, 16);
-             else
-               memcpy (buf, raw_buf, 16);
-
-             /* Extract (always little endian).  Read upper 128bits.  */
-             status = regcache->raw_read (I387_YMM16H_REGNUM (tdep) + regnum
-                                          - num_lower_zmm_regs,
-                                          raw_buf);
-             if (status != REG_VALID)
-               mark_value_bytes_unavailable (result_value, 16, 16);
-             else
-               memcpy (buf + 16, raw_buf, 16);
-           }
+             /* Which register is it, relative to zmm16.  */
+             int i_16 = i_0 - num_lower_zmm_regs;
 
-         /* Read upper 256bits.  */
-         status = regcache->raw_read (tdep->zmm0h_regnum + regnum,
-                                      raw_buf);
-         if (status != REG_VALID)
-           mark_value_bytes_unavailable (result_value, 32, 32);
-         else
-           memcpy (buf + 32, raw_buf, 32);
+             return pseudo_from_concat_raw (next_frame, pseudo_reg_num,
+                                            I387_XMM16_REGNUM (tdep) + i_16,
+                                            I387_YMM16H_REGNUM (tdep) + i_16,
+                                            tdep->zmm0h_regnum + i_0);
+           }
        }
-      else if (i386_ymm_regnum_p (gdbarch, regnum))
+      else if (i386_ymm_regnum_p (gdbarch, pseudo_reg_num))
        {
-         regnum -= tdep->ymm0_regnum;
+         int i = pseudo_reg_num - tdep->ymm0_regnum;
 
-         /* Extract (always little endian).  Read lower 128bits.  */
-         status = regcache->raw_read (I387_XMM0_REGNUM (tdep) + regnum,
-                                      raw_buf);
-         if (status != REG_VALID)
-           mark_value_bytes_unavailable (result_value, 0, 16);
-         else
-           memcpy (buf, raw_buf, 16);
-         /* Read upper 128bits.  */
-         status = regcache->raw_read (tdep->ymm0h_regnum + regnum,
-                                      raw_buf);
-         if (status != REG_VALID)
-           mark_value_bytes_unavailable (result_value, 16, 32);
-         else
-           memcpy (buf + 16, raw_buf, 16);
+         return pseudo_from_concat_raw (next_frame, pseudo_reg_num,
+                                        I387_XMM0_REGNUM (tdep) + i,
+                                        tdep->ymm0h_regnum + i);
        }
-      else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
+      else if (i386_ymm_avx512_regnum_p (gdbarch, pseudo_reg_num))
        {
-         regnum -= tdep->ymm16_regnum;
-         /* Extract (always little endian).  Read lower 128bits.  */
-         status = regcache->raw_read (I387_XMM16_REGNUM (tdep) + regnum,
-                                      raw_buf);
-         if (status != REG_VALID)
-           mark_value_bytes_unavailable (result_value, 0, 16);
-         else
-           memcpy (buf, raw_buf, 16);
-         /* Read upper 128bits.  */
-         status = regcache->raw_read (tdep->ymm16h_regnum + regnum,
-                                      raw_buf);
-         if (status != REG_VALID)
-           mark_value_bytes_unavailable (result_value, 16, 16);
-         else
-           memcpy (buf + 16, raw_buf, 16);
+         int i = pseudo_reg_num - tdep->ymm16_regnum;
+
+         return pseudo_from_concat_raw (next_frame, pseudo_reg_num,
+                                        I387_XMM16_REGNUM (tdep) + i,
+                                        tdep->ymm16h_regnum + i);
        }
-      else if (i386_word_regnum_p (gdbarch, regnum))
+      else if (i386_word_regnum_p (gdbarch, pseudo_reg_num))
        {
-         int gpnum = regnum - tdep->ax_regnum;
+         int gpnum = pseudo_reg_num - tdep->ax_regnum;
 
          /* Extract (always little endian).  */
-         status = regcache->raw_read (gpnum, raw_buf);
-         if (status != REG_VALID)
-           mark_value_bytes_unavailable (result_value, 0,
-                                         result_value->type ()->length ());
-         else
-           memcpy (buf, raw_buf, 2);
+         return pseudo_from_raw_part (next_frame, pseudo_reg_num, gpnum, 0);
        }
-      else if (i386_byte_regnum_p (gdbarch, regnum))
+      else if (i386_byte_regnum_p (gdbarch, pseudo_reg_num))
        {
-         int gpnum = regnum - tdep->al_regnum;
+         int gpnum = pseudo_reg_num - tdep->al_regnum;
 
          /* Extract (always little endian).  We read both lower and
             upper registers.  */
-         status = regcache->raw_read (gpnum % 4, raw_buf);
-         if (status != REG_VALID)
-           mark_value_bytes_unavailable (result_value, 0,
-                                         result_value->type ()->length ());
-         else if (gpnum >= 4)
-           memcpy (buf, raw_buf + 1, 1);
-         else
-           memcpy (buf, raw_buf, 1);
+         return pseudo_from_raw_part (next_frame, pseudo_reg_num, gpnum % 4,
+                                      gpnum >= 4 ? 1 : 0);
        }
       else
        internal_error (_("invalid regnum"));
     }
 }
 
-static struct value *
-i386_pseudo_register_read_value (struct gdbarch *gdbarch,
-                                readable_regcache *regcache,
-                                int regnum)
-{
-  struct value *result;
-
-  result = value::allocate (register_type (gdbarch, regnum));
-  VALUE_LVAL (result) = lval_register;
-  VALUE_REGNUM (result) = regnum;
-
-  i386_pseudo_register_read_into_value (gdbarch, regcache, regnum, result);
-
-  return result;
-}
-
 void
-i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
-                           int regnum, const gdb_byte *buf)
+i386_pseudo_register_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
+                           const int pseudo_reg_num,
+                           gdb::array_view<const gdb_byte> buf)
 {
-  gdb_byte raw_buf[I386_MAX_REGISTER_SIZE];
-
-  if (i386_mmx_regnum_p (gdbarch, regnum))
+  if (i386_mmx_regnum_p (gdbarch, pseudo_reg_num))
     {
-      int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
+      int fpnum = i386_mmx_regnum_to_fp_regnum (next_frame, pseudo_reg_num);
 
-      /* Read ...  */
-      regcache->raw_read (fpnum, raw_buf);
-      /* ... Modify ... (always little endian).  */
-      memcpy (raw_buf, buf, register_size (gdbarch, regnum));
-      /* ... Write.  */
-      regcache->raw_write (fpnum, raw_buf);
+      pseudo_to_raw_part (next_frame, buf, fpnum, 0);
     }
   else
     {
       i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
 
-      if (i386_bnd_regnum_p (gdbarch, regnum))
+      if (i386_bnd_regnum_p (gdbarch, pseudo_reg_num))
        {
-         ULONGEST upper, lower;
          int size = builtin_type (gdbarch)->builtin_data_ptr->length ();
-         enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
+         bfd_endian byte_order
+           = gdbarch_byte_order (current_inferior ()->arch ());
 
          /* New values from input value.  */
-         regnum -= tdep->bnd0_regnum;
-         lower = extract_unsigned_integer (buf, size, byte_order);
-         upper = extract_unsigned_integer (buf + size, size, byte_order);
+         int reg_index = pseudo_reg_num - tdep->bnd0_regnum;
+         int raw_regnum = I387_BND0R_REGNUM (tdep) + reg_index;
 
-         /* Fetching register buffer.  */
-         regcache->raw_read (I387_BND0R_REGNUM (tdep) + regnum,
-                             raw_buf);
+         value *bndr_value = value_of_register (raw_regnum, next_frame);
+         gdb::array_view<gdb_byte> bndr_view
+           = bndr_value->contents_writeable ();
 
-         upper = ~upper;
+         /* Copy lower bytes directly.  */
+         copy (buf.slice (0, size), bndr_view.slice (0, size));
 
-         /* Set register bits.  */
-         memcpy (raw_buf, &lower, 8);
-         memcpy (raw_buf + 8, &upper, 8);
-
-         regcache->raw_write (I387_BND0R_REGNUM (tdep) + regnum, raw_buf);
-       }
-      else if (i386_k_regnum_p (gdbarch, regnum))
-       {
-         regnum -= tdep->k0_regnum;
+         /* Convert and then copy upper bytes.  */
+         ULONGEST upper
+           = extract_unsigned_integer (buf.slice (size, size), byte_order);
+         upper = ~upper;
+         store_unsigned_integer (bndr_view.slice (8, size), byte_order,
+                                 upper);
 
-         regcache->raw_write (tdep->k0_regnum + regnum, buf);
+         put_frame_register (next_frame, raw_regnum, bndr_view);
        }
-      else if (i386_zmm_regnum_p (gdbarch, regnum))
+      else if (i386_zmm_regnum_p (gdbarch, pseudo_reg_num))
        {
-         regnum -= tdep->zmm0_regnum;
+         /* Which register is it, relative to zmm0.  */
+         int reg_index_0 = pseudo_reg_num - tdep->zmm0_regnum;
 
-         if (regnum < num_lower_zmm_regs)
-           {
-             /* Write lower 128bits.  */
-             regcache->raw_write (I387_XMM0_REGNUM (tdep) + regnum, buf);
-             /* Write upper 128bits.  */
-             regcache->raw_write (I387_YMM0_REGNUM (tdep) + regnum, buf + 16);
-           }
+         if (reg_index_0 < num_lower_zmm_regs)
+           pseudo_to_concat_raw (next_frame, buf,
+                                 I387_XMM0_REGNUM (tdep) + reg_index_0,
+                                 I387_YMM0_REGNUM (tdep) + reg_index_0,
+                                 tdep->zmm0h_regnum + reg_index_0);
          else
            {
-             /* Write lower 128bits.  */
-             regcache->raw_write (I387_XMM16_REGNUM (tdep) + regnum
-                                  - num_lower_zmm_regs, buf);
-             /* Write upper 128bits.  */
-             regcache->raw_write (I387_YMM16H_REGNUM (tdep) + regnum
-                                  - num_lower_zmm_regs, buf + 16);
+             /* Which register is it, relative to zmm16.  */
+             int reg_index_16 = reg_index_0 - num_lower_zmm_regs;
+
+             pseudo_to_concat_raw (next_frame, buf,
+                                   I387_XMM16_REGNUM (tdep) + reg_index_16,
+                                   I387_YMM16H_REGNUM (tdep) + reg_index_16,
+                                   tdep->zmm0h_regnum + +reg_index_0);
            }
-         /* Write upper 256bits.  */
-         regcache->raw_write (tdep->zmm0h_regnum + regnum, buf + 32);
        }
-      else if (i386_ymm_regnum_p (gdbarch, regnum))
+      else if (i386_ymm_regnum_p (gdbarch, pseudo_reg_num))
        {
-         regnum -= tdep->ymm0_regnum;
+         int i = pseudo_reg_num - tdep->ymm0_regnum;
 
-         /* ... Write lower 128bits.  */
-         regcache->raw_write (I387_XMM0_REGNUM (tdep) + regnum, buf);
-         /* ... Write upper 128bits.  */
-         regcache->raw_write (tdep->ymm0h_regnum + regnum, buf + 16);
+         pseudo_to_concat_raw (next_frame, buf, I387_XMM0_REGNUM (tdep) + i,
+                               tdep->ymm0h_regnum + i);
        }
-      else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
+      else if (i386_ymm_avx512_regnum_p (gdbarch, pseudo_reg_num))
        {
-         regnum -= tdep->ymm16_regnum;
+         int i = pseudo_reg_num - tdep->ymm16_regnum;
 
-         /* ... Write lower 128bits.  */
-         regcache->raw_write (I387_XMM16_REGNUM (tdep) + regnum, buf);
-         /* ... Write upper 128bits.  */
-         regcache->raw_write (tdep->ymm16h_regnum + regnum, buf + 16);
+         pseudo_to_concat_raw (next_frame, buf, I387_XMM16_REGNUM (tdep) + i,
+                               tdep->ymm16h_regnum + i);
        }
-      else if (i386_word_regnum_p (gdbarch, regnum))
+      else if (i386_word_regnum_p (gdbarch, pseudo_reg_num))
        {
-         int gpnum = regnum - tdep->ax_regnum;
+         int gpnum = pseudo_reg_num - tdep->ax_regnum;
 
-         /* Read ...  */
-         regcache->raw_read (gpnum, raw_buf);
-         /* ... Modify ... (always little endian).  */
-         memcpy (raw_buf, buf, 2);
-         /* ... Write.  */
-         regcache->raw_write (gpnum, raw_buf);
+         pseudo_to_raw_part (next_frame, buf, gpnum, 0);
        }
-      else if (i386_byte_regnum_p (gdbarch, regnum))
+      else if (i386_byte_regnum_p (gdbarch, pseudo_reg_num))
        {
-         int gpnum = regnum - tdep->al_regnum;
+         int gpnum = pseudo_reg_num - tdep->al_regnum;
 
-         /* Read ...  We read both lower and upper registers.  */
-         regcache->raw_read (gpnum % 4, raw_buf);
-         /* ... Modify ... (always little endian).  */
-         if (gpnum >= 4)
-           memcpy (raw_buf + 1, buf, 1);
-         else
-           memcpy (raw_buf, buf, 1);
-         /* ... Write.  */
-         regcache->raw_write (gpnum % 4, raw_buf);
+         pseudo_to_raw_part (next_frame, buf, gpnum % 4, gpnum >= 4 ? 1 : 0);
        }
       else
        internal_error (_("invalid regnum"));
@@ -3712,12 +3634,6 @@ i386_ax_pseudo_register_collect (struct gdbarch *gdbarch,
       ax_reg_mask (ax, I387_BND0R_REGNUM (tdep) + regnum);
       return 0;
     }
-  else if (i386_k_regnum_p (gdbarch, regnum))
-    {
-      regnum -= tdep->k0_regnum;
-      ax_reg_mask (ax, tdep->k0_regnum + regnum);
-      return 0;
-    }
   else if (i386_zmm_regnum_p (gdbarch, regnum))
     {
       regnum -= tdep->zmm0_regnum;
@@ -3834,7 +3750,7 @@ i386_convert_register_p (struct gdbarch *gdbarch,
    return its contents in TO.  */
 
 static int
-i386_register_to_value (frame_info_ptr frame, int regnum,
+i386_register_to_value (const frame_info_ptr &frame, int regnum,
                        struct type *type, gdb_byte *to,
                        int *optimizedp, int *unavailablep)
 {
@@ -3854,10 +3770,10 @@ i386_register_to_value (frame_info_ptr frame, int regnum,
       gdb_assert (regnum != -1);
       gdb_assert (register_size (gdbarch, regnum) == 4);
 
-      if (!get_frame_register_bytes (frame, regnum, 0,
-                                    gdb::make_array_view (to,
-                                                       register_size (gdbarch,
-                                                                      regnum)),
+      auto to_view
+       = gdb::make_array_view (to, register_size (gdbarch, regnum));
+      frame_info_ptr next_frame = get_next_frame_sentinel_okay (frame);
+      if (!get_frame_register_bytes (next_frame, regnum, 0, to_view,
                                     optimizedp, unavailablep))
        return 0;
 
@@ -3874,7 +3790,7 @@ i386_register_to_value (frame_info_ptr frame, int regnum,
    REGNUM in frame FRAME.  */
 
 static void
-i386_value_to_register (frame_info_ptr frame, int regnum,
+i386_value_to_register (const frame_info_ptr &frame, int regnum,
                        struct type *type, const gdb_byte *from)
 {
   int len = type->length ();
@@ -3894,7 +3810,9 @@ i386_value_to_register (frame_info_ptr frame, int regnum,
       gdb_assert (regnum != -1);
       gdb_assert (register_size (get_frame_arch (frame), regnum) == 4);
 
-      put_frame_register (frame, regnum, from);
+      auto from_view = gdb::make_array_view (from, 4);
+      put_frame_register (get_next_frame_sentinel_okay (frame), regnum,
+                         from_view);
       regnum = i386_next_regnum (regnum);
       len -= 4;
       from += 4;
@@ -4026,7 +3944,7 @@ i386_iterate_over_regset_sections (struct gdbarch *gdbarch,
 /* Stuff for WIN32 PE style DLL's but is pretty generic really.  */
 
 CORE_ADDR
-i386_pe_skip_trampoline_code (frame_info_ptr frame,
+i386_pe_skip_trampoline_code (const frame_info_ptr &frame,
                              CORE_ADDR pc, char *name)
 {
   struct gdbarch *gdbarch = get_frame_arch (frame);
@@ -4057,7 +3975,7 @@ i386_pe_skip_trampoline_code (frame_info_ptr frame,
    routine.  */
 
 int
-i386_sigtramp_p (frame_info_ptr this_frame)
+i386_sigtramp_p (const frame_info_ptr &this_frame)
 {
   CORE_ADDR pc = get_frame_pc (this_frame);
   const char *name;
@@ -4093,7 +4011,7 @@ i386_print_insn (bfd_vma pc, struct disassemble_info *info)
    routine.  */
 
 static int
-i386_svr4_sigtramp_p (frame_info_ptr this_frame)
+i386_svr4_sigtramp_p (const frame_info_ptr &this_frame)
 {
   CORE_ADDR pc = get_frame_pc (this_frame);
   const char *name;
@@ -4108,7 +4026,7 @@ i386_svr4_sigtramp_p (frame_info_ptr this_frame)
    address of the associated sigcontext (ucontext) structure.  */
 
 static CORE_ADDR
-i386_svr4_sigcontext_addr (frame_info_ptr this_frame)
+i386_svr4_sigcontext_addr (const frame_info_ptr &this_frame)
 {
   struct gdbarch *gdbarch = get_frame_arch (this_frame);
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
@@ -4664,7 +4582,7 @@ i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
 /* Get the ARGIth function argument for the current function.  */
 
 static CORE_ADDR
-i386_fetch_pointer_argument (frame_info_ptr frame, int argi, 
+i386_fetch_pointer_argument (const frame_info_ptr &frame, int argi,
                             struct type *type)
 {
   struct gdbarch *gdbarch = get_frame_arch (frame);
@@ -5644,7 +5562,7 @@ i386_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
          ir.addr -= 1;
          goto no_support;
        }
-      for (regnum = X86_RECORD_REAX_REGNUM; 
+      for (regnum = X86_RECORD_REAX_REGNUM;
           regnum <= X86_RECORD_REDI_REGNUM;
           regnum++)
        I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
@@ -5965,7 +5883,7 @@ Do you want to stop the program?"),
          ir.addr -= 1;
          goto no_support;
        }
-      /* FALLTHROUGH */
+      [[fallthrough]];
     case 0x0fb2:    /* lss Gv */
     case 0x0fb4:    /* lfs Gv */
     case 0x0fb5:    /* lgs Gv */
@@ -6202,7 +6120,7 @@ Do you want to stop the program?"),
                                                  I386_SAVE_FPU_REGS))
                            return -1;
                        }
-                     /* Fall through */
+                     [[fallthrough]];
                    default:
                      if (record_full_arch_list_add_mem (addr64, 2))
                        return -1;
@@ -6741,7 +6659,7 @@ Do you want to stop the program?"),
          ir.addr -= 1;
          goto no_support;
        }
-      /* FALLTHROUGH */
+      [[fallthrough]];
     case 0xf5:    /* cmc */
     case 0xf8:    /* clc */
     case 0xf9:    /* stc */
@@ -6962,6 +6880,9 @@ Do you want to stop the program?"),
       goto no_support;
       break;
 
+    case 0x0f01f9:  /* rdtscp */
+      I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
+      [[fallthrough]];
     case 0x0f31:    /* rdtsc */
       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
@@ -7071,6 +6992,11 @@ Do you want to stop the program?"),
     case 0x0f01:
       if (i386_record_modrm (&ir))
        return -1;
+      if (ir.modrm == 0xf9)
+       {
+         opcode = (opcode << 8) | 0xf9;
+         goto reswitch;
+       }
       switch (ir.reg)
        {
        case 0:  /* sgdt */
@@ -7183,7 +7109,7 @@ Do you want to stop the program?"),
              else if (ir.rm == 1)
                break;
            }
-         /* Fall through.  */
+         [[fallthrough]];
        case 3:  /* lidt */
          if (ir.mod == 3)
            {
@@ -8238,17 +8164,85 @@ i386_floatformat_for_type (struct gdbarch *gdbarch,
        || strcmp (name, "_Float128") == 0
        || strcmp (name, "complex _Float128") == 0
        || strcmp (name, "complex(kind=16)") == 0
+       || strcmp (name, "COMPLEX(16)") == 0
        || strcmp (name, "complex*32") == 0
        || strcmp (name, "COMPLEX*32") == 0
        || strcmp (name, "quad complex") == 0
        || strcmp (name, "real(kind=16)") == 0
        || strcmp (name, "real*16") == 0
-       || strcmp (name, "REAL*16") == 0)
+       || strcmp (name, "REAL*16") == 0
+       || strcmp (name, "REAL(16)") == 0)
       return floatformats_ieee_quad;
 
   return default_floatformat_for_type (gdbarch, name, len);
 }
 
+/* Compute an XCR0 mask based on a target description.  */
+
+static uint64_t
+i386_xcr0_from_tdesc (const struct target_desc *tdesc)
+{
+  if (! tdesc_has_registers (tdesc))
+    return 0;
+
+  const struct tdesc_feature *feature_core;
+
+  const struct tdesc_feature *feature_sse, *feature_avx, *feature_mpx,
+                            *feature_avx512, *feature_pkeys;
+
+  /* Get core registers.  */
+  feature_core = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.core");
+  if (feature_core == NULL)
+    return 0;
+
+  /* Get SSE registers.  */
+  feature_sse = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.sse");
+
+  /* Try AVX registers.  */
+  feature_avx = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx");
+
+  /* Try MPX registers.  */
+  feature_mpx = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.mpx");
+
+  /* Try AVX512 registers.  */
+  feature_avx512 = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512");
+
+  /* Try PKEYS  */
+  feature_pkeys = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.pkeys");
+
+  /* The XCR0 bits.  */
+  uint64_t xcr0 = X86_XSTATE_X87;
+
+  if (feature_sse)
+    xcr0 |= X86_XSTATE_SSE;
+
+  if (feature_avx)
+    {
+      /* AVX register description requires SSE register description.  */
+      if (!feature_sse)
+       return 0;
+
+      xcr0 |= X86_XSTATE_AVX;
+    }
+
+  if (feature_mpx)
+    xcr0 |= X86_XSTATE_MPX_MASK;
+
+  if (feature_avx512)
+    {
+      /* AVX512 register description requires AVX register description.  */
+      if (!feature_avx)
+       return 0;
+
+      xcr0 |= X86_XSTATE_AVX512;
+    }
+
+  if (feature_pkeys)
+    xcr0 |= X86_XSTATE_PKRU;
+
+  return xcr0;
+}
+
 static int
 i386_validate_tdesc_p (i386_gdbarch_tdep *tdep,
                       struct tdesc_arch_data *tdesc_data)
@@ -8458,10 +8452,30 @@ i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   int bnd0_regnum;
   int num_bnd_cooked;
 
+  x86_xsave_layout xsave_layout = target_fetch_x86_xsave_layout ();
+
+  /* If the target did not provide an XSAVE layout but the target
+     description includes registers from the XSAVE extended region,
+     use a fallback XSAVE layout.  Specifically, this fallback layout
+     is used when writing out a local core dump for a remote
+     target.  */
+  if (xsave_layout.sizeof_xsave == 0)
+    xsave_layout
+      = i387_fallback_xsave_layout (i386_xcr0_from_tdesc (info.target_desc));
+
   /* If there is already a candidate, use it.  */
-  arches = gdbarch_list_lookup_by_info (arches, &info);
-  if (arches != NULL)
-    return arches->gdbarch;
+  for (arches = gdbarch_list_lookup_by_info (arches, &info);
+       arches != NULL;
+       arches = gdbarch_list_lookup_by_info (arches->next, &info))
+    {
+      /* Check that the XSAVE layout of ARCHES matches the layout for
+        the current target.  */
+      i386_gdbarch_tdep *other_tdep
+       = gdbarch_tdep<i386_gdbarch_tdep> (arches->gdbarch);
+
+      if (other_tdep->xsave_layout == xsave_layout)
+       return arches->gdbarch;
+    }
 
   /* Allocate space for the new architecture.  Assume i386 for now.  */
   gdbarch *gdbarch
@@ -8610,17 +8624,20 @@ i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_fetch_pointer_argument (gdbarch, i386_fetch_pointer_argument);
 
   /* Hook the function epilogue frame unwinder.  This unwinder is
-     appended to the list first, so that it supercedes the DWARF
+     appended to the list first, so that it supersedes the DWARF
      unwinder in function epilogues (where the DWARF unwinder
      currently fails).  */
   if (info.bfd_arch_info->bits_per_word == 32)
-    frame_unwind_append_unwinder (gdbarch, &i386_epilogue_frame_unwind);
+    frame_unwind_append_unwinder (gdbarch, &i386_epilogue_override_frame_unwind);
 
   /* Hook in the DWARF CFI frame unwinder.  This unwinder is appended
      to the list before the prologue-based unwinders, so that DWARF
      CFI info will be used if it is available.  */
   dwarf2_append_unwinders (gdbarch);
 
+  if (info.bfd_arch_info->bits_per_word == 32)
+    frame_unwind_append_unwinder (gdbarch, &i386_epilogue_frame_unwind);
+
   frame_base_set_default (gdbarch, &i386_frame_base);
 
   /* Pseudo registers may be changed by amd64_init_abi.  */
@@ -8713,6 +8730,7 @@ i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
       gdbarch_free (gdbarch);
       return NULL;
     }
+  tdep->xsave_layout = xsave_layout;
 
   num_bnd_cooked = (tdep->bnd0r_regnum > 0 ? I387_NUM_BND_REGS : 0);
 
@@ -8844,11 +8862,10 @@ i386_target_description (uint64_t xcr0, bool segments)
 static unsigned long
 i386_mpx_bd_base (void)
 {
-  struct regcache *rcache;
   ULONGEST ret;
   enum register_status regstatus;
 
-  rcache = get_current_regcache ();
+  regcache *rcache = get_thread_regcache (inferior_thread ());
   gdbarch *arch = rcache->arch ();
   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
 
@@ -9115,17 +9132,19 @@ is \"default\"."),
 
   /* Add "bound" command for the show mpx commands list.  */
 
-  add_cmd ("bound", no_class, i386_mpx_info_bounds,
+  cmd_list_element *c = add_cmd ("bound", no_class, i386_mpx_info_bounds,
           "Show the memory bounds for a given array/pointer storage\
  in the bound table.",
           &mpx_show_cmdlist);
+  deprecate_cmd (c, nullptr);
 
   /* Add "bound" command for the set mpx commands list.  */
 
-  add_cmd ("bound", no_class, i386_mpx_set_bounds,
+  c = add_cmd ("bound", no_class, i386_mpx_set_bounds,
           "Set the memory bounds for a given array/pointer storage\
  in the bound table.",
           &mpx_set_cmdlist);
+  deprecate_cmd (c, nullptr);
 
   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
                          i386_svr4_init_abi);