linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org
Cc: mingo@kernel.org, tglx@linutronix.de, hpa@zytor.com,
	torvalds@linux-foundation.org, akpm@linux-foundation.org,
	hjl.tools@gmail.com, Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: [PATCH 27/30] x32: Handle process creation
Date: Sun, 19 Feb 2012 16:08:05 -0800	[thread overview]
Message-ID: <1329696488-16970-28-git-send-email-hpa@zytor.com> (raw)
In-Reply-To: <1329696488-16970-1-git-send-email-hpa@zytor.com>

From: "H. Peter Anvin" <hpa@zytor.com>

Allow an x32 process to be started.

Originally-by: H. J. Lu <hjl.tools@gmail.com>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
---
 arch/x86/include/asm/compat.h    |   26 ++++++++++++++++++++++++--
 arch/x86/include/asm/elf.h       |   25 +++++++++++++++++++++----
 arch/x86/kernel/cpu/perf_event.c |    4 +++-
 arch/x86/kernel/entry_64.S       |   15 +++++++++++++++
 arch/x86/kernel/process_64.c     |   23 ++++++++++++++++-------
 5 files changed, 79 insertions(+), 14 deletions(-)

diff --git a/arch/x86/include/asm/compat.h b/arch/x86/include/asm/compat.h
index 7938b84..e7f68b4 100644
--- a/arch/x86/include/asm/compat.h
+++ b/arch/x86/include/asm/compat.h
@@ -6,6 +6,7 @@
  */
 #include <linux/types.h>
 #include <linux/sched.h>
+#include <asm/processor.h>
 #include <asm/user32.h>
 #include <asm/unistd.h>
 
@@ -187,7 +188,20 @@ struct compat_shmid64_ds {
 /*
  * The type of struct elf_prstatus.pr_reg in compatible core dumps.
  */
+#ifdef CONFIG_X86_X32_ABI
+typedef struct user_regs_struct compat_elf_gregset_t;
+
+#define PR_REG_SIZE(S) (test_thread_flag(TIF_IA32) ? 68 : 216)
+#define PRSTATUS_SIZE(S) (test_thread_flag(TIF_IA32) ? 144 : 296)
+#define SET_PR_FPVALID(S,V) \
+  do { *(int *) (((void *) &((S)->pr_reg)) + PR_REG_SIZE(0)) = (V); } \
+  while (0)
+
+#define COMPAT_USE_64BIT_TIME \
+	(!!(task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT))
+#else
 typedef struct user_regs_struct32 compat_elf_gregset_t;
+#endif
 
 /*
  * A pointer passed in from user mode. This should not
@@ -209,8 +223,16 @@ static inline compat_uptr_t ptr_to_compat(void __user *uptr)
 
 static inline void __user *arch_compat_alloc_user_space(long len)
 {
-	struct pt_regs *regs = task_pt_regs(current);
-	return (void __user *)regs->sp - len;
+	compat_uptr_t sp;
+
+	if (test_thread_flag(TIF_IA32)) {
+		sp = task_pt_regs(current)->sp;
+	} else {
+		/* -128 for the x32 ABI redzone */
+		sp = percpu_read(old_rsp) - 128;
+	}
+
+	return (void __user *)round_down(sp - len, 16);
 }
 
 static inline bool is_compat_task(void)
diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h
index 410fa6a..83aabea 100644
--- a/arch/x86/include/asm/elf.h
+++ b/arch/x86/include/asm/elf.h
@@ -156,7 +156,12 @@ do {						\
 #define elf_check_arch(x)			\
 	((x)->e_machine == EM_X86_64)
 
-#define compat_elf_check_arch(x)	elf_check_arch_ia32(x)
+#define compat_elf_check_arch(x)		\
+	(elf_check_arch_ia32(x) || (x)->e_machine == EM_X86_64)
+
+#if __USER32_DS != __USER_DS
+# error "The following code assumes __USER32_DS == __USER_DS"
+#endif
 
 static inline void elf_common_init(struct thread_struct *t,
 				   struct pt_regs *regs, const u16 ds)
@@ -179,8 +184,9 @@ static inline void elf_common_init(struct thread_struct *t,
 void start_thread_ia32(struct pt_regs *regs, u32 new_ip, u32 new_sp);
 #define compat_start_thread start_thread_ia32
 
-void set_personality_ia32(void);
-#define COMPAT_SET_PERSONALITY(ex) set_personality_ia32()
+void set_personality_ia32(bool);
+#define COMPAT_SET_PERSONALITY(ex)			\
+	set_personality_ia32((ex).e_machine == EM_X86_64)
 
 #define COMPAT_ELF_PLATFORM			("i686")
 
@@ -296,9 +302,20 @@ do {									\
 			    (unsigned long)current->mm->context.vdso);	\
 } while (0)
 
+#define ARCH_DLINFO_X32							\
+do {									\
+	if (vdso_enabled)						\
+		NEW_AUX_ENT(AT_SYSINFO_EHDR,				\
+			    (unsigned long)current->mm->context.vdso);	\
+} while (0)
+
 #define AT_SYSINFO		32
 
-#define COMPAT_ARCH_DLINFO	ARCH_DLINFO_IA32(sysctl_vsyscall32)
+#define COMPAT_ARCH_DLINFO						\
+if (test_thread_flag(TIF_X32))						\
+	ARCH_DLINFO_X32;						\
+else									\
+	ARCH_DLINFO_IA32(sysctl_vsyscall32)
 
 #define COMPAT_ELF_ET_DYN_BASE	(TASK_UNMAPPED_BASE + 0x1000000)
 
diff --git a/arch/x86/kernel/cpu/perf_event.c b/arch/x86/kernel/cpu/perf_event.c
index 5adce10..63c0e05 100644
--- a/arch/x86/kernel/cpu/perf_event.c
+++ b/arch/x86/kernel/cpu/perf_event.c
@@ -28,7 +28,6 @@
 #include <asm/apic.h>
 #include <asm/stacktrace.h>
 #include <asm/nmi.h>
-#include <asm/compat.h>
 #include <asm/smp.h>
 #include <asm/alternative.h>
 
@@ -1595,6 +1594,9 @@ perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
 }
 
 #ifdef CONFIG_COMPAT
+
+#include <asm/compat.h>
+
 static inline int
 perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
 {
diff --git a/arch/x86/kernel/entry_64.S b/arch/x86/kernel/entry_64.S
index 53dc821..9e036f0 100644
--- a/arch/x86/kernel/entry_64.S
+++ b/arch/x86/kernel/entry_64.S
@@ -763,6 +763,21 @@ ENTRY(stub_x32_rt_sigreturn)
 	CFI_ENDPROC
 END(stub_x32_rt_sigreturn)
 
+ENTRY(stub_x32_execve)
+	CFI_STARTPROC
+	addq $8, %rsp
+	PARTIAL_FRAME 0
+	SAVE_REST
+	FIXUP_TOP_OF_STACK %r11
+	movq %rsp, %rcx
+	call sys32_execve
+	RESTORE_TOP_OF_STACK %r11
+	movq %rax,RAX(%rsp)
+	RESTORE_REST
+	jmp int_ret_from_sys_call
+	CFI_ENDPROC
+END(stub_x32_execve)
+
 #endif
 
 /*
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index 5fe2fba..a0701da 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -364,7 +364,9 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
 void start_thread_ia32(struct pt_regs *regs, u32 new_ip, u32 new_sp)
 {
 	start_thread_common(regs, new_ip, new_sp,
-			    __USER32_CS, __USER32_DS, __USER32_DS);
+			    test_thread_flag(TIF_X32)
+			    ? __USER_CS : __USER32_CS,
+			    __USER_DS, __USER_DS);
 }
 #endif
 
@@ -508,6 +510,7 @@ void set_personality_64bit(void)
 
 	/* Make sure to be in 64bit mode */
 	clear_thread_flag(TIF_IA32);
+	clear_thread_flag(TIF_X32);
 	clear_thread_flag(TIF_ADDR32);
 	clear_thread_flag(TIF_X32);
 
@@ -522,22 +525,28 @@ void set_personality_64bit(void)
 	current->personality &= ~READ_IMPLIES_EXEC;
 }
 
-void set_personality_ia32(void)
+void set_personality_ia32(bool x32)
 {
 	/* inherit personality from parent */
 
 	/* Make sure to be in 32bit mode */
-	set_thread_flag(TIF_IA32);
 	set_thread_flag(TIF_ADDR32);
-	clear_thread_flag(TIF_X32);
-	current->personality |= force_personality32;
 
 	/* Mark the associated mm as containing 32-bit tasks. */
 	if (current->mm)
 		current->mm->context.ia32_compat = 1;
 
-	/* Prepare the first "return" to user space */
-	current_thread_info()->status |= TS_COMPAT;
+	if (x32) {
+		clear_thread_flag(TIF_IA32);
+		set_thread_flag(TIF_X32);
+		current->personality &= ~READ_IMPLIES_EXEC;
+	} else {
+		set_thread_flag(TIF_IA32);
+		clear_thread_flag(TIF_X32);
+		current->personality |= force_personality32;
+		/* Prepare the first "return" to user space */
+		current_thread_info()->status |= TS_COMPAT;
+	}
 }
 
 unsigned long get_wchan(struct task_struct *p)
-- 
1.7.6.5


  parent reply	other threads:[~2012-02-20  0:21 UTC|newest]

Thread overview: 106+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-20  0:07 [PATCH 00/30] RFC: x32 support H. Peter Anvin
2012-02-20  0:07 ` [PATCH 01/30] x86: Factor out TIF_IA32 from 32-bit address space H. Peter Anvin
2012-02-20  0:07 ` [PATCH 02/30] x86-64: Use explicit sizes in sigcontext.h, prepare for x32 H. Peter Anvin
2012-02-20  0:51   ` Linus Torvalds
2012-02-20  0:56     ` H. Peter Anvin
2012-02-22 12:22       ` Arnd Bergmann
2012-02-22 18:14         ` H. Peter Anvin
2012-02-22 21:23           ` Arnd Bergmann
2012-02-22 21:55             ` H. Peter Anvin
2012-02-24  2:33             ` H. Peter Anvin
2012-03-06  5:25               ` H. Peter Anvin
2012-02-20  0:07 ` [PATCH 03/30] sysinfo: Move struct sysinfo to a separate header file H. Peter Anvin
2012-02-20  0:07 ` [PATCH 04/30] posix_types: Introduce __kernel_[u]long_t H. Peter Anvin
2012-02-20  0:07 ` [PATCH 05/30] x32: Create posix_types_x32.h H. Peter Anvin
2012-02-20  0:07 ` [PATCH 06/30] sysinfo: Use explicit types in <linux/sysinfo.h> H. Peter Anvin
2012-02-20 12:12   ` Geert Uytterhoeven
2012-02-20 17:29     ` H. Peter Anvin
2012-02-20 20:01       ` Geert Uytterhoeven
2012-02-20 20:44         ` H. Peter Anvin
2012-02-20  0:07 ` [PATCH 07/30] compat: Introduce COMPAT_USE_64BIT_TIME H. Peter Anvin
2012-02-20  0:07 ` [PATCH 08/30] compat: Use COMPAT_USE_64BIT_TIME in the lp driver H. Peter Anvin
2012-02-20  0:56   ` Linus Torvalds
2012-02-20  0:59     ` H. Peter Anvin
2012-02-20  2:22     ` [PATCH 0/7] COMPAT_USE_64BIT_TIME v2 H. Peter Anvin
2012-02-20  2:22       ` [PATCH 1/7] compat: Introduce COMPAT_USE_64BIT_TIME H. Peter Anvin
2012-02-20  2:22       ` [PATCH 2/7] compat: Add helper functions to read/write struct timeval, timespec H. Peter Anvin
2012-02-20  2:22       ` [PATCH 3/7] compat: Handle COMPAT_USE_64BIT_TIME in the lp driver H. Peter Anvin
2012-02-20  2:22       ` [PATCH 4/7] compat: Use COMPAT_USE_64BIT_TIME in the input subsystem H. Peter Anvin
2012-02-20  2:22       ` [PATCH 5/7] compat: Use COMPAT_USE_64BIT_TIME in the Bluetooth subsystem H. Peter Anvin
2012-02-20  2:22       ` [PATCH 6/7] compat: Use COMPAT_USE_64BIT_TIME in net/compat.c H. Peter Anvin
2012-02-20  2:22       ` [PATCH 7/7] compat: Handle COMPAT_USE_64BIT_TIME in net/socket.c H. Peter Anvin
2012-02-20  2:42       ` [PATCH 0/7] COMPAT_USE_64BIT_TIME v2 H. Peter Anvin
2012-02-20  6:22         ` H. Peter Anvin
2012-02-20  0:07 ` [PATCH 09/30] compat: Use COMPAT_USE_64BIT_TIME in the input subsystem H. Peter Anvin
2012-02-20  0:07 ` [PATCH 10/30] compat: Use COMPAT_USE_64BIT_TIME in the Bluetooth subsystem H. Peter Anvin
2012-02-21 19:05   ` Gustavo Padovan
2012-02-21 19:15     ` H. Peter Anvin
2012-02-22 13:47     ` Jiri Kosina
2012-02-22 14:45       ` Gustavo Padovan
2012-02-20  0:07 ` [PATCH 11/30] aio: Use __kernel_ulong_t to define aio_context_t H. Peter Anvin
2012-02-20  0:07 ` [PATCH 12/30] compat: Create compat_sys_p{read,write}v64 H. Peter Anvin
2012-02-20  0:07 ` [PATCH 13/30] elf: Allow core dump-related fields to be overridden H. Peter Anvin
2012-02-20  0:07 ` [PATCH 14/30] x86-64: Add prototype for old_rsp to a header file H. Peter Anvin
2012-02-20  0:07 ` [PATCH 15/30] x32: Add a thread flag for x32 processes H. Peter Anvin
2012-02-20  0:07 ` [PATCH 16/30] x86-64, ia32: Drop sys32_rt_sigprocmask H. Peter Anvin
2012-02-20  0:07 ` [PATCH 17/30] x32: Add x32 system calls to syscall/syscall_64.tbl H. Peter Anvin
2012-02-20  0:07 ` [PATCH 18/30] x32: Generate <asm/unistd_x32.h> H. Peter Anvin
2012-02-20  0:07 ` [PATCH 19/30] x32: Generate <asm/unistd_64_x32.h> H. Peter Anvin
2012-02-20  0:07 ` [PATCH 20/30] x86: Move some signal-handling definitions to a common header H. Peter Anvin
2012-02-20  0:07 ` [PATCH 21/30] x32: Export setup/restore_sigcontext from signal.c H. Peter Anvin
2012-02-20  0:08 ` [PATCH 22/30] x32: Add struct ucontext_x32 H. Peter Anvin
2012-02-20  0:08 ` [PATCH 23/30] x32: Add rt_sigframe_x32 H. Peter Anvin
2012-02-20  0:08 ` [PATCH 24/30] x32: Handle the x32 system call flag H. Peter Anvin
2012-02-20  0:08 ` [PATCH 25/30] x86: Add #ifdef CONFIG_COMPAT to <asm/sys_ia32.h> H. Peter Anvin
2012-02-20  0:08 ` [PATCH 26/30] x32: Signal-related system calls H. Peter Anvin
2012-02-20  0:08 ` H. Peter Anvin [this message]
2012-02-20  0:08 ` [PATCH 28/30] x32: If configured, add x32 system calls to system call tables H. Peter Anvin
2012-02-20  0:08 ` [PATCH 29/30] x32: Allow x32 to be configured H. Peter Anvin
2012-02-20  0:08 ` [PATCH 30/30] x32: Add x32 VDSO support H. Peter Anvin
2012-02-21  0:12   ` Andy Lutomirski
2012-02-21  3:58     ` H. Peter Anvin
2012-02-21 16:52       ` Andrew Lutomirski
2012-02-21 17:51         ` H. Peter Anvin
2012-02-21 18:54           ` Andrew Lutomirski
2012-02-21 19:03             ` H. Peter Anvin
2012-02-21 19:29               ` Andrew Lutomirski
2012-02-21 19:37                 ` H. Peter Anvin
2012-02-21 19:40                   ` Andrew Lutomirski
2012-02-21 19:49                     ` H. Peter Anvin
2012-02-21 19:51                       ` Andrew Lutomirski
2012-02-21 19:56                         ` H. Peter Anvin
2012-02-23  4:49     ` [tip:x86/x32] x32: Drop non-__vdso weak symbols from the x32 VDSO tip-bot for H. Peter Anvin
2012-02-23 10:55       ` Ingo Molnar
2012-02-23 14:36         ` H. Peter Anvin
2012-02-27 22:21 ` [tip:x86/x32] x32: Warn and disable rather than error if binutils too old tip-bot for H. Peter Anvin
2012-02-28  9:49 ` [tip:x86/x32] x86/x32: Fix the binutils auto-detect tip-bot for Ingo Molnar
2012-03-06  0:56 ` [tip:x86/x32] x32: Provide separate is_ia32_task() and is_x32_task () predicates tip-bot for H. Peter Anvin
2012-03-06  0:56 ` [tip:x86/x32] x32: Switch to a 64-bit clock_t tip-bot for H. Peter Anvin
2012-03-06  0:57 ` [tip:x86/x32] x32: Add ptrace for x32 tip-bot for H.J. Lu
2012-03-14  5:58 ` [tip:x86/x32] x32: Fix stupid ia32/ x32 inversion in the siginfo format tip-bot for H. Peter Anvin
2012-03-14 21:43 ` [tip:x86/x32] x32: Fix alignment fail in struct compat_siginfo tip-bot for H. Peter Anvin
2012-03-14 23:17   ` H.J. Lu
2012-03-14 23:19     ` H. Peter Anvin
2012-03-15  0:33       ` H.J. Lu
2012-03-14 23:33     ` Derek Fawcus
2012-03-14 23:44       ` H. Peter Anvin
2012-04-18 17:38   ` H.J. Lu
2012-04-18 17:47     ` H. Peter Anvin
2012-04-18 18:01       ` H.J. Lu
2012-04-18 18:10         ` H. Peter Anvin
2012-04-18 18:15           ` H.J. Lu
2012-04-18 18:21             ` H. Peter Anvin
2012-04-18 18:33               ` H.J. Lu
2012-04-18 19:03                 ` H. Peter Anvin
2012-04-18 21:24                 ` H. Peter Anvin
2012-04-18 21:49                   ` H.J. Lu
2012-04-18 21:58                     ` H. Peter Anvin
2012-04-18 22:58                       ` H.J. Lu
2012-04-18 22:55                   ` H.J. Lu
2012-04-18 23:48                     ` H. Peter Anvin
2012-04-18 23:54                       ` H.J. Lu
2012-04-19  0:09                         ` H. Peter Anvin
2012-04-19  0:14                           ` H.J. Lu
2012-04-23 23:49     ` [tip:x86/urgent] asm-generic: Allow overriding clock_t and add attributes to siginfo_t tip-bot for H. Peter Anvin
2012-04-23 23:50     ` [tip:x86/urgent] x32, siginfo: Provide proper overrides for x32 siginfo_t tip-bot for H. Peter Anvin
2012-04-24  1:19     ` tip-bot for H. Peter Anvin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1329696488-16970-28-git-send-email-hpa@zytor.com \
    --to=hpa@zytor.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=akpm@linux-foundation.org \
    --cc=hjl.tools@gmail.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).