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]

PATCH: Add x32 arch_prctl support


Hi,

X32 kernel interface stores 64-bit value for ARCH_GET_FS and ARCH_GET_GS.
But user interface of ARCH_GET_FS and ARCH_GET_GS in arch_prctl and ptrace
only expects 32-bit value.  This patch adds x32 arch_prctl.c and
ptrace.c to allocate a temporary 64-bit value for system call and copy
the lower 32-bit to user.  Tested on Linux/x32.  OK to install?

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..8aa0ebf
--- /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 32bit base address of segment register %fs
+   and %gs as unsigned 64bit value via ARCH_GET_FS and ARCH_GET_GS, we
+   use a local unsigned 64bit variable to hold the base address and copy
+   it to ADDR after arch_prctl return.  */
+
+int
+__arch_prctl (int code, unsigned long *addr)
+{
+  int res;
+  unsigned long long base_addr;
+  unsigned long *addr_saved;
+
+  switch (code)
+    {
+    case ARCH_GET_FS:
+    case ARCH_GET_GS:
+      addr_saved = addr;
+      addr = (unsigned long *) &base_addr;
+      break;
+
+    default:
+      break;
+    }
+
+  res = INLINE_SYSCALL (arch_prctl, 2, code, addr);
+  if (res == 0)
+    switch (code)
+      {
+      case ARCH_GET_FS:
+      case ARCH_GET_GS:
+	*addr_saved = (unsigned long) base_addr;
+	break;
+
+      default:
+	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..09bdac9
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/ptrace.c
@@ -0,0 +1,98 @@
+/* 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>
+
+#ifndef PTRACE_ARCH_PRCTL
+#define PTRACE_ARCH_PRCTL      30
+#endif
+
+/* Since x32 ptrace stores 32bit base address of segment register %fs
+   and %gs as unsigned 64bit value via ARCH_GET_FS and ARCH_GET_GS with
+   PTRACE_ARCH_PRCTL, we use a local unsigned 64bit variable to hold
+   the base address and copy it to ADDR after ptrace return.  */
+
+long
+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]