Blame SOURCES/gdb-x86_64-i386-syscall-restart.patch

7bc85d
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
7bc85d
From: Fedora GDB patches <invalid@email.com>
7bc85d
Date: Fri, 27 Oct 2017 21:07:50 +0200
7bc85d
Subject: gdb-x86_64-i386-syscall-restart.patch
7bc85d
7bc85d
;; Fix syscall restarts for amd64->i386 biarch.
7bc85d
;;=push+jan
7bc85d
7bc85d
http://sourceware.org/ml/gdb-patches/2009-11/msg00592.html
7bc85d
Subject: [patch] Fix syscall restarts for amd64->i386 biarch
7bc85d
7bc85d
Hi,
7bc85d
7bc85d
tested only on recent Linux kernels, it should apply also on vanilla ones.
7bc85d
There were various changes of the kernels behavior in the past.
7bc85d
7bc85d
FSF GDB HEAD state:
7bc85d
kernel debugger inferior state
7bc85d
x86_64 x86_64   x86_64   PASS
7bc85d
x86_64 x86_64   i386     FAIL without this patch, PASS with this patch
7bc85d
x86_64 i386     i386     PASS on recent kernels
7bc85d
                         (FAIL: kernel-2.6.31.5-127.fc12.x86_64 - Fedora 12)
7bc85d
                         (PASS: kernel-2.6.32-0.55.rc8.git1.fc13.x86_64)
7bc85d
i386   i386     i386     PASS
7bc85d
7bc85d
Currently gdb.base/interrupt.exp fails on amd64 host running under
7bc85d
--target_board unix/-m32 with:
7bc85d
	continue
7bc85d
	Continuing.
7bc85d
	Unknown error 512
7bc85d
7bc85d
<linux/errno.h>:
7bc85d
/*
7bc85d
 * These should never be seen by user programs.  To return one of ERESTART*
7bc85d
 * codes, signal_pending() MUST be set.  Note that ptrace can observe these
7bc85d
 * at syscall exit tracing, but they will never be left for the debugged user
7bc85d
 * process to see.
7bc85d
 */
7bc85d
7bc85d
"Unknown error 512" printed above is printed by the inferior itself, not by GDB.
7bc85d
7bc85d
It is because GDB reads it as 0xfffffffffffffe00 but writes it back as
7bc85d
0xfffffe00.
7bc85d
+      /* Sign-extend %eax as during return from a syscall it is being checked
7bc85d
+	 for -ERESTART* values -512 being above 0xfffffffffffffe00; tested by
7bc85d
+	 interrupt.exp.  */
7bc85d
7bc85d
Quote of Roland McGrath from IRC:
7bc85d
7bc85d
roland: in the user_regset model, there are 64-bit user_regset flavors and
7bc85d
32-bit user_regset flavors, so at the kabi level the (kernel) caller can say
7bc85d
what it means: calls on the 32-bit user_regset flavor will behave as if on
7bc85d
a 32-bit kernel/userland.  in ptrace, there is no way for x86_64 ptrace calls
7bc85d
to say "i think of the inferior as being 32 bits, so act accordingly" (tho ppc
7bc85d
and/or sparc have ptr
7bc85d
roland: ace requests that do that iirc)
7bc85d
roland: ergo 64-bit ptrace callers must either save/restore full 64-bits so
7bc85d
the kernel's sign-extension choices are preserved, or else grok magic ways to
7bc85d
expand stored 32-bit register contents to 64-bit values to stuff via 64-bit
7bc85d
ptrace
7bc85d
[...]
7bc85d
roland: there is a "32-bit-flavored task", but it's not really true that it
7bc85d
has 32-bit registers.  there is no 32-bit-only userland condition.  any task
7bc85d
can always ljmp to the 64-bit code segment and run 64-bit insns including
7bc85d
a 64-bit syscall
7bc85d
roland: so a 64-bit debugger should see and be able to fiddle the full
7bc85d
registers.  it can even change cs via ptrace to force the inferior into
7bc85d
running 32 or 64 bit code.
7bc85d
7bc85d
Saving whole 64bits for i386 targets on x86_64 hosts does not much match the
7bc85d
GDB architecture as `struct type' for these registers still should be 32bit
7bc85d
etc.   Therefore provided just this exception.
7bc85d
7bc85d
The problem is reproducible only if one does an inferior call during the
7bc85d
interruption to do full inferior save/restore from GDB regcache.
7bc85d
7bc85d
Regression tested on {x86_64,x86_64-m32,i686}-fedora12-linux-gnu.
7bc85d
7bc85d
Thanks,
7bc85d
Jan
7bc85d
7bc85d
gdb/
7bc85d
2009-11-29  Jan Kratochvil  <jan.kratochvil@redhat.com>
7bc85d
7bc85d
	* amd64-nat.c (amd64_collect_native_gregset): Do not pre-clear %eax.
7bc85d
	Sign extend it afterwards.
7bc85d
7bc85d
diff --git a/gdb/amd64-nat.c b/gdb/amd64-nat.c
7bc85d
--- a/gdb/amd64-nat.c
7bc85d
+++ b/gdb/amd64-nat.c
7bc85d
@@ -135,9 +135,9 @@ amd64_collect_native_gregset (const struct regcache *regcache,
7bc85d
     {
7bc85d
       num_regs = amd64_native_gregset32_num_regs;
7bc85d
 
7bc85d
-      /* Make sure %eax, %ebx, %ecx, %edx, %esi, %edi, %ebp, %esp and
7bc85d
+      /* Make sure %ebx, %ecx, %edx, %esi, %edi, %ebp, %esp and
7bc85d
          %eip get zero-extended to 64 bits.  */
7bc85d
-      for (i = 0; i <= I386_EIP_REGNUM; i++)
7bc85d
+      for (i = I386_ECX_REGNUM; i <= I386_EIP_REGNUM; i++)
7bc85d
 	{
7bc85d
 	  if (regnum == -1 || regnum == i)
7bc85d
 	    memset (regs + amd64_native_gregset_reg_offset (gdbarch, i), 0, 8);
7bc85d
@@ -163,4 +163,20 @@ amd64_collect_native_gregset (const struct regcache *regcache,
7bc85d
 	    regcache->raw_collect (i, regs + offset);
7bc85d
 	}
7bc85d
     }
7bc85d
+
7bc85d
+  if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
7bc85d
+    {
7bc85d
+      /* Sign-extend %eax as during return from a syscall it is being checked
7bc85d
+	 for -ERESTART* values -512 being above 0xfffffffffffffe00; tested by
7bc85d
+	 interrupt.exp.  */
7bc85d
+
7bc85d
+      int i = I386_EAX_REGNUM;
7bc85d
+
7bc85d
+      if (regnum == -1 || regnum == i)
7bc85d
+	{
7bc85d
+	  void *ptr = regs + amd64_native_gregset_reg_offset (gdbarch, i);
7bc85d
+
7bc85d
+	  *(int64_t *) ptr = *(int32_t *) ptr;
7bc85d
+	}
7bc85d
+    }
7bc85d
 }