This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PATCH: Add x32 arch_prctl support


On Fri, May 18, 2012 at 11:35 AM, Roland McGrath <roland@hack.frob.com> wrote:
>
>> + ?unsigned long long base_addr;
>> + ?unsigned long *addr_saved;
>
> This is probably going to get a "might be used uninitialized" warning.

Yes.  Should I silence it?

> Anyway, I think it's cleaner to pass an 'unsigned long long int' (or
> uint64_t) to the syscall, since that's what it is in the kernel.
> Might as well just make it:

I thought about it and decided against it since it will require
special treatment for x32 when it is used.


>> +#include <errno.h>
>> +#include <sys/types.h>
>> +#define ptrace __redirect_ptrace
>> +#include <sys/ptrace.h>
>> +#undef ptrace
>> +#include <sys/syscall.h>
>> +#include <sysdep.h>
>
> If this funny business were warranted, it would need some comments.
> But it's not. ?Just use stdarg like linux/ptrace.c does.
>
>> +#ifndef PTRACE_ARCH_PRCTL
>> +#define PTRACE_ARCH_PRCTL ? ? ?30
>> +#endif
>
> This should never be required. ?Just add it to ptrace.h.

PTRACE_ARCH_PRCTL is x86 specific and is only used by GDB,
GDB has #ifdef to define it, which will never be removed.  If we add
it to ptrace.h, I have to add it a new ptrace for x86-64.  I don't think
we should do it.

>> +  switch ((int) request)
>> +    {
>
> There's no need to cast to int when you have a default case anyway.

I need it if PTRACE_ARCH_PRCTL isn't in x86-64 ptrace.h.

>
> But actually, PTRACE_ARCH_PRCTL should be entirely obsolete anyway.
> You should just drop it from the x32 kernel. ?Since many kernel
> versions ago, you can just use the fs_base and gs_base fields in
> user_regs_struct via the other ptrace calls.
>

That is true. However, since it is used in GDB today, I need it
to support it for x32.  Here is the patch.  Does it look OK?

Thanks.

-- 
H.J.
----
	* sysdeps/unix/sysv/linux/x86_64/x32/Makefile (sysdep_routines):
	Add arch_prctl.
	* sysdeps/unix/sysv/linux/x86_64/x32/arch_prctl.c: New file.
	* sysdeps/unix/sysv/linux/x86_64/x32/ptrace.c: Likewise.

diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/Makefile
b/sysdeps/unix/sysv/linux/x86_64/x32/Makefile
index 5f77df7..aa78238 100644
--- a/sysdeps/unix/sysv/linux/x86_64/x32/Makefile
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/Makefile
@@ -1,3 +1,7 @@
+ifeq ($(subdir),misc)
+sysdep_routines += arch_prctl
+endif
+
 ifeq ($(subdir),posix)
 sysdep_routines += getcpu sched_getcpu-static
 endif
diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/arch_prctl.c
b/sysdeps/unix/sysv/linux/x86_64/x32/arch_prctl.c
new file mode 100644
index 0000000..44c100b
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/arch_prctl.c
@@ -0,0 +1,64 @@
+/* arch_prctl call for Linux/x32.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <sysdep.h>
+
+/* Since x32 arch_prctl stores 32-bit base address of segment register %fs
+   and %gs as unsigned 64-bit value via ARCH_GET_FS and ARCH_GET_GS, we
+   use a local unsigned 64-bit variable to hold the base address and copy
+   it to ADDR after arch_prctl return.  */
+
+int
+__arch_prctl (int code, uintptr_t *addr)
+{
+  int res;
+  uint64_t addr64;
+  uintptr_t *addr_saved;
+
+  switch (code)
+    {
+    case ARCH_GET_FS:
+    case ARCH_GET_GS:
+      addr_saved = addr;
+      addr = (uintptr_t *) &addr64;
+      break;
+    }
+
+  res = INLINE_SYSCALL (arch_prctl, 2, code, addr);
+  if (res == 0)
+    switch (code)
+      {
+      case ARCH_GET_FS:
+      case ARCH_GET_GS:
+	 /* Check for a large value that overflows.  */
+	if ((uintptr_t) addr64 != addr64)
+	  {
+	    __set_errno (EOVERFLOW);
+	    return -1;
+	  }
+	*addr_saved = (uintptr_t) addr64;
+	break;
+      }
+
+  return res;
+}
+
+weak_alias (__arch_prctl, arch_prctl)
diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/ptrace.c
b/sysdeps/unix/sysv/linux/x86_64/x32/ptrace.c
new file mode 100644
index 0000000..c026307
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/ptrace.c
@@ -0,0 +1,107 @@
+/* ptrace call for Linux/x32.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <sys/types.h>
+#define ptrace __redirect_ptrace
+#include <sys/ptrace.h>
+#undef ptrace
+#include <sys/syscall.h>
+#include <sysdep.h>
+
+/* PTRACE_ARCH_PRCTL is specific to x86 kernel and is only used by GDB.  */
+#ifndef PTRACE_ARCH_PRCTL
+# define PTRACE_ARCH_PRCTL	30
+#endif
+
+/* Since x32 ptrace stores 32-bit base address of segment register %fs
+   and %gs as unsigned 64-bit value via ARCH_GET_FS and ARCH_GET_GS with
+   PTRACE_ARCH_PRCTL, we use a local unsigned 64-bit variable to hold
+   the base address and copy it to ADDR after ptrace return.
+
+   We don't use
+
+   long int ptrace (enum __ptrace_request __request, ...);
+
+   since x32 passes all 4 parameters in registers, which is the same
+   as the non-stdarg call.  The generated code is much simpler.
+   */
+
+long int
+ptrace (enum __ptrace_request request, pid_t pid, void *addr, void *data)
+{
+  long res, ret;
+  unsigned long long base_addr;
+  unsigned long *addr_saved;
+
+  switch ((int) request)
+    {
+    case PTRACE_PEEKTEXT:
+    case PTRACE_PEEKDATA:
+    case PTRACE_PEEKUSER:
+      /* Pass the address of return data to kernel.  */
+      data = &ret;
+      break;
+
+    case PTRACE_ARCH_PRCTL:
+      switch ((int) data)
+	{
+	case ARCH_GET_FS:
+	case ARCH_GET_GS:
+	  addr_saved = (unsigned long *) addr;
+	  addr = &base_addr;
+	  break;
+
+	default:
+	  break;
+	}
+      break;
+
+    default:
+      break;
+    }
+
+  res = INLINE_SYSCALL (ptrace, 4, request, pid, addr, data);
+  if (res >= 0)
+    switch ((int) request)
+      {
+      case PTRACE_PEEKTEXT:
+      case PTRACE_PEEKDATA:
+      case PTRACE_PEEKUSER:
+	__set_errno (0);
+	return ret;
+
+      case PTRACE_ARCH_PRCTL:
+	switch ((int) data)
+	  {
+	  case ARCH_GET_FS:
+	  case ARCH_GET_GS:
+	    *addr_saved = (unsigned long) base_addr;
+	    break;
+
+	  default:
+	    break;
+	  }
+	break;
+
+      default:
+	break;
+      }
+
+  return res;
+}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]