Convert amd64-linux target descriptions
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-x86-low.c
index 2535959339418b8711a080c536fc4eacfa747e7d..f09871ac36fc9fad0451056daa36b547dbe32743 100644 (file)
@@ -1,6 +1,6 @@
 /* GNU/Linux/x86-64 specific low level interface, for the remote server
    for GDB.
-   Copyright (C) 2002-2016 Free Software Foundation, Inc.
+   Copyright (C) 2002-2017 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -133,6 +133,11 @@ static const int x86_64_regmap[] =
   -1,
   -1, -1, -1, -1, -1, -1, -1, -1,
   ORIG_RAX * 8,
+#ifdef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
+  21 * 8,  22 * 8,
+#else
+  -1, -1,
+#endif
   -1, -1, -1, -1,                      /* MPX registers BND0 ... BND3.  */
   -1, -1,                              /* MPX registers BNDCFGU, BNDSTATUS.  */
   -1, -1, -1, -1, -1, -1, -1, -1,       /* xmm16 ... xmm31 (AVX512)  */
@@ -143,7 +148,8 @@ static const int x86_64_regmap[] =
   -1, -1, -1, -1, -1, -1, -1, -1,       /* zmm0 ... zmm31 (AVX512)  */
   -1, -1, -1, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1,
-  -1, -1, -1, -1, -1, -1, -1, -1
+  -1, -1, -1, -1, -1, -1, -1, -1,
+  -1                                   /* pkru  */
 };
 
 #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0]))
@@ -186,7 +192,7 @@ is_64bit_tdesc (void)
 /* Called by libthread_db.  */
 
 ps_err_e
-ps_get_thread_area (const struct ps_prochandle *ph,
+ps_get_thread_area (struct ps_prochandle *ph,
                    lwpid_t lwpid, int idx, void **base)
 {
 #ifdef __x86_64__
@@ -306,6 +312,20 @@ x86_fill_gregset (struct regcache *regcache, void *buf)
       for (i = 0; i < X86_64_NUM_REGS; i++)
        if (x86_64_regmap[i] != -1)
          collect_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
+
+#ifndef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
+      {
+        unsigned long base;
+        int lwpid = lwpid_of (current_thread);
+
+        collect_register_by_name (regcache, "fs_base", &base);
+        ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_SET_FS);
+
+        collect_register_by_name (regcache, "gs_base", &base);
+        ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_SET_GS);
+      }
+#endif
+
       return;
     }
 
@@ -332,6 +352,19 @@ x86_store_gregset (struct regcache *regcache, const void *buf)
       for (i = 0; i < X86_64_NUM_REGS; i++)
        if (x86_64_regmap[i] != -1)
          supply_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
+
+#ifndef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
+      {
+        unsigned long base;
+        int lwpid = lwpid_of (current_thread);
+
+        if (ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_GET_FS) == 0)
+          supply_register_by_name (regcache, "fs_base", &base);
+
+        if (ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_GET_GS) == 0)
+          supply_register_by_name (regcache, "gs_base", &base);
+      }
+#endif
       return;
     }
 #endif
@@ -427,13 +460,15 @@ x86_get_pc (struct regcache *regcache)
 
   if (use_64bit)
     {
-      unsigned long pc;
+      uint64_t pc;
+
       collect_register_by_name (regcache, "rip", &pc);
       return (CORE_ADDR) pc;
     }
   else
     {
-      unsigned int pc;
+      uint32_t pc;
+
       collect_register_by_name (regcache, "eip", &pc);
       return (CORE_ADDR) pc;
     }
@@ -446,12 +481,14 @@ x86_set_pc (struct regcache *regcache, CORE_ADDR pc)
 
   if (use_64bit)
     {
-      unsigned long newpc = pc;
+      uint64_t newpc = pc;
+
       supply_register_by_name (regcache, "rip", &newpc);
     }
   else
     {
-      unsigned int newpc = pc;
+      uint32_t newpc = pc;
+
       supply_register_by_name (regcache, "eip", &newpc);
     }
 }
@@ -624,14 +661,14 @@ x86_debug_reg_state (pid_t pid)
    as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit
    conversion in-place ourselves.  */
 
-/* Convert a native/host siginfo object, into/from the siginfo in the
+/* Convert a ptrace/host siginfo object, into/from the siginfo in the
    layout of the inferiors' architecture.  Returns true if any
    conversion was done; false otherwise.  If DIRECTION is 1, then copy
-   from INF to NATIVE.  If DIRECTION is 0, copy from NATIVE to
+   from INF to PTRACE.  If DIRECTION is 0, copy from PTRACE to
    INF.  */
 
 static int
-x86_siginfo_fixup (siginfo_t *native, gdb_byte *inf, int direction)
+x86_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction)
 {
 #ifdef __x86_64__
   unsigned int machine;
@@ -640,11 +677,11 @@ x86_siginfo_fixup (siginfo_t *native, gdb_byte *inf, int direction)
 
   /* Is the inferior 32-bit?  If so, then fixup the siginfo object.  */
   if (!is_64bit_tdesc ())
-      return amd64_linux_siginfo_fixup_common (native, inf, direction,
+      return amd64_linux_siginfo_fixup_common (ptrace, inf, direction,
                                               FIXUP_32);
   /* No fixup for native x32 GDB.  */
   else if (!is_elf64 && sizeof (void *) == 8)
-    return amd64_linux_siginfo_fixup_common (native, inf, direction,
+    return amd64_linux_siginfo_fixup_common (ptrace, inf, direction,
                                             FIXUP_X32);
 #endif
 
@@ -720,7 +757,7 @@ x86_linux_read_description (void)
        {
          have_ptrace_getfpxregs = 0;
          have_ptrace_getregset = 0;
-         return tdesc_i386_mmx_linux;
+         return i386_linux_read_description (X86_XSTATE_X87);
        }
       else
        have_ptrace_getfpxregs = 1;
@@ -772,7 +809,7 @@ x86_linux_read_description (void)
 
   /* Check the native XCR0 only if PTRACE_GETREGSET is available.  */
   xcr0_features = (have_ptrace_getregset
-         && (xcr0 & X86_XSTATE_ALL_MASK));
+                  && (xcr0 & X86_XSTATE_ALL_MASK));
 
   if (xcr0_features)
     x86_xcr0 = xcr0;
@@ -780,77 +817,30 @@ x86_linux_read_description (void)
   if (machine == EM_X86_64)
     {
 #ifdef __x86_64__
-      if (is_elf64)
-       {
-         if (xcr0_features)
-           {
-             switch (xcr0 & X86_XSTATE_ALL_MASK)
-               {
-               case X86_XSTATE_AVX512_MASK:
-                 return tdesc_amd64_avx512_linux;
-
-               case X86_XSTATE_AVX_MPX_MASK:
-                 return tdesc_amd64_avx_mpx_linux;
-
-               case X86_XSTATE_MPX_MASK:
-                 return tdesc_amd64_mpx_linux;
+      const target_desc *tdesc = NULL;
 
-               case X86_XSTATE_AVX_MASK:
-                 return tdesc_amd64_avx_linux;
-
-               default:
-                 return tdesc_amd64_linux;
-               }
-           }
-         else
-           return tdesc_amd64_linux;
-       }
-      else
+      if (xcr0_features)
        {
-         if (xcr0_features)
-           {
-             switch (xcr0 & X86_XSTATE_ALL_MASK)
-               {
-               case X86_XSTATE_AVX512_MASK:
-                 return tdesc_x32_avx512_linux;
-
-               case X86_XSTATE_MPX_MASK: /* No MPX on x32.  */
-               case X86_XSTATE_AVX_MASK:
-                 return tdesc_x32_avx_linux;
-
-               default:
-                 return tdesc_x32_linux;
-               }
-           }
-         else
-           return tdesc_x32_linux;
+         tdesc = amd64_linux_read_description (xcr0 & X86_XSTATE_ALL_MASK,
+                                               !is_elf64);
        }
+
+      if (tdesc == NULL)
+       tdesc = amd64_linux_read_description (X86_XSTATE_SSE_MASK, !is_elf64);
+      return tdesc;
 #endif
     }
   else
     {
-      if (xcr0_features)
-       {
-         switch (xcr0 & X86_XSTATE_ALL_MASK)
-           {
-           case (X86_XSTATE_AVX512_MASK):
-             return tdesc_i386_avx512_linux;
-
-           case (X86_XSTATE_MPX_MASK):
-             return tdesc_i386_mpx_linux;
+      const target_desc *tdesc = NULL;
 
-           case (X86_XSTATE_AVX_MPX_MASK):
-             return tdesc_i386_avx_mpx_linux;
+      if (xcr0_features)
+         tdesc = i386_linux_read_description (xcr0 & X86_XSTATE_ALL_MASK);
 
-           case (X86_XSTATE_AVX_MASK):
-             return tdesc_i386_avx_linux;
+      if (tdesc == NULL)
+       tdesc = i386_linux_read_description (X86_XSTATE_SSE);
 
-           default:
-             return tdesc_i386_linux;
-           }
-       }
-      else
-       return tdesc_i386_linux;
+      return tdesc;
     }
 
   gdb_assert_not_reached ("failed to return tdesc");
@@ -991,25 +981,19 @@ x86_arch_setup (void)
    code.  This should only be called if LWP got a SYSCALL_SIGTRAP.  */
 
 static void
-x86_get_syscall_trapinfo (struct regcache *regcache, int *sysno, int *sysret)
+x86_get_syscall_trapinfo (struct regcache *regcache, int *sysno)
 {
   int use_64bit = register_size (regcache->tdesc, 0) == 8;
 
   if (use_64bit)
     {
       long l_sysno;
-      long l_sysret;
 
       collect_register_by_name (regcache, "orig_rax", &l_sysno);
-      collect_register_by_name (regcache, "rax", &l_sysret);
       *sysno = (int) l_sysno;
-      *sysret = (int) l_sysret;
     }
   else
-    {
-      collect_register_by_name (regcache, "orig_eax", sysno);
-      collect_register_by_name (regcache, "eax", sysret);
-    }
+    collect_register_by_name (regcache, "orig_eax", sysno);
 }
 
 static int
@@ -1026,7 +1010,7 @@ append_insns (CORE_ADDR *to, size_t len, const unsigned char *buf)
 }
 
 static int
-push_opcode (unsigned char *buf, char *op)
+push_opcode (unsigned char *buf, const char *op)
 {
   unsigned char *buf_org = buf;
 
@@ -1094,10 +1078,10 @@ amd64_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr,
   buf[i++] = 0x41; buf[i++] = 0x51; /* push %r9 */
   buf[i++] = 0x41; buf[i++] = 0x50; /* push %r8 */
   buf[i++] = 0x9c; /* pushfq */
-  buf[i++] = 0x48; /* movl <addr>,%rdi */
+  buf[i++] = 0x48; /* movabs <addr>,%rdi */
   buf[i++] = 0xbf;
-  *((unsigned long *)(buf + i)) = (unsigned long) tpaddr;
-  i += sizeof (unsigned long);
+  memcpy (buf + i, &tpaddr, 8);
+  i += 8;
   buf[i++] = 0x57; /* push %rdi */
   append_insns (&buildaddr, i, buf);
 
@@ -1844,6 +1828,8 @@ amd64_emit_call (CORE_ADDR fn)
   else
     {
       int offset32 = offset64; /* we know we can't overflow here.  */
+
+      buf[i++] = 0xe8; /* call <reladdr> */
       memcpy (buf + i, &offset32, 4);
       i += 4;
     }
@@ -2854,33 +2840,13 @@ x86_get_ipa_tdesc_idx (void)
   const struct target_desc *tdesc = regcache->tdesc;
 
 #ifdef __x86_64__
-  if (tdesc == tdesc_amd64_linux || tdesc == tdesc_amd64_linux_no_xml
-      || tdesc == tdesc_x32_linux)
-    return X86_TDESC_SSE;
-  if (tdesc == tdesc_amd64_avx_linux || tdesc == tdesc_x32_avx_linux)
-    return X86_TDESC_AVX;
-  if (tdesc == tdesc_amd64_mpx_linux)
-    return X86_TDESC_MPX;
-  if (tdesc == tdesc_amd64_avx_mpx_linux)
-    return X86_TDESC_AVX_MPX;
-  if (tdesc == tdesc_amd64_avx512_linux || tdesc == tdesc_x32_avx512_linux)
-    return X86_TDESC_AVX512;
+  return amd64_get_ipa_tdesc_idx (tdesc);
 #endif
 
-  if (tdesc == tdesc_i386_mmx_linux)
-    return X86_TDESC_MMX;
-  if (tdesc == tdesc_i386_linux || tdesc == tdesc_i386_linux_no_xml)
+  if (tdesc == tdesc_i386_linux_no_xml)
     return X86_TDESC_SSE;
-  if (tdesc == tdesc_i386_avx_linux)
-    return X86_TDESC_AVX;
-  if (tdesc == tdesc_i386_mpx_linux)
-    return X86_TDESC_MPX;
-  if (tdesc == tdesc_i386_avx_mpx_linux)
-    return X86_TDESC_AVX_MPX;
-  if (tdesc == tdesc_i386_avx512_linux)
-    return X86_TDESC_AVX512;
 
-  return 0;
+  return i386_get_ipa_tdesc_idx (tdesc);
 }
 
 /* This is initialized assuming an amd64 target.
@@ -2934,29 +2900,20 @@ initialize_low_arch (void)
 {
   /* Initialize the Linux target descriptions.  */
 #ifdef __x86_64__
-  init_registers_amd64_linux ();
-  init_registers_amd64_avx_linux ();
-  init_registers_amd64_avx512_linux ();
-  init_registers_amd64_mpx_linux ();
-  init_registers_amd64_avx_mpx_linux ();
-
-  init_registers_x32_linux ();
-  init_registers_x32_avx_linux ();
-  init_registers_x32_avx512_linux ();
-
   tdesc_amd64_linux_no_xml = XNEW (struct target_desc);
-  copy_target_description (tdesc_amd64_linux_no_xml, tdesc_amd64_linux);
+  copy_target_description (tdesc_amd64_linux_no_xml,
+                          amd64_linux_read_description (X86_XSTATE_SSE_MASK,
+                                                        false));
   tdesc_amd64_linux_no_xml->xmltarget = xmltarget_amd64_linux_no_xml;
 #endif
-  init_registers_i386_linux ();
-  init_registers_i386_mmx_linux ();
-  init_registers_i386_avx_linux ();
-  init_registers_i386_avx512_linux ();
-  init_registers_i386_mpx_linux ();
-  init_registers_i386_avx_mpx_linux ();
+
+#if GDB_SELF_TEST
+  initialize_low_tdesc ();
+#endif
 
   tdesc_i386_linux_no_xml = XNEW (struct target_desc);
-  copy_target_description (tdesc_i386_linux_no_xml, tdesc_i386_linux);
+  copy_target_description (tdesc_i386_linux_no_xml,
+                          i386_linux_read_description (X86_XSTATE_SSE_MASK));
   tdesc_i386_linux_no_xml->xmltarget = xmltarget_i386_linux_no_xml;
 
   initialize_regsets_info (&x86_regsets_info);
This page took 0.030905 seconds and 4 git commands to generate.