public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] x86: Merge kernel_thread()
@ 2009-11-13 19:54 Brian Gerst
  2009-11-13 19:54 ` [PATCH 1/4] x86, 64-bit: Move kernel_thread to C Brian Gerst
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Brian Gerst @ 2009-11-13 19:54 UTC (permalink / raw)
  To: Ingo Molnar; +Cc: x86, linux-kernel

This patch series merges 32/64-bit implementations of kernel_thread,
and cleans up related code.

 arch/x86/kernel/entry_32.S       |    8 +----
 arch/x86/kernel/entry_64.S       |   49 ++-----------------------------------
 arch/x86/kernel/process.c        |   30 +++++++++++++++++++++++
 arch/x86/kernel/process_32.c     |   33 -------------------------
 arch/x86/kernel/process_64.c     |    7 ++---
 arch/x86/kernel/x8664_ksyms_64.c |    2 -
 6 files changed, 38 insertions(+), 91 deletions(-)

--
Brian Gerst

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] x86, 64-bit: Move kernel_thread to C
  2009-11-13 19:54 [PATCH 0/4] x86: Merge kernel_thread() Brian Gerst
@ 2009-11-13 19:54 ` Brian Gerst
  2009-11-13 19:54 ` [PATCH 2/4] x86: Merge 32/64-bit kernel_thread Brian Gerst
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Brian Gerst @ 2009-11-13 19:54 UTC (permalink / raw)
  To: Ingo Molnar; +Cc: x86, linux-kernel

Prepare for merging with 32-bit.

Signed-off-by: Brian Gerst <brgerst@gmail.com>
---
 arch/x86/kernel/entry_64.S       |   49 ++-----------------------------------
 arch/x86/kernel/process_64.c     |   31 ++++++++++++++++++++++-
 arch/x86/kernel/x8664_ksyms_64.c |    2 -
 3 files changed, 32 insertions(+), 50 deletions(-)

diff --git a/arch/x86/kernel/entry_64.S b/arch/x86/kernel/entry_64.S
index b5c061f..64919a8 100644
--- a/arch/x86/kernel/entry_64.S
+++ b/arch/x86/kernel/entry_64.S
@@ -1158,63 +1158,20 @@ bad_gs:
 	jmp  2b
 	.previous
 
-/*
- * Create a kernel thread.
- *
- * C extern interface:
- *	extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
- *
- * asm input arguments:
- *	rdi: fn, rsi: arg, rdx: flags
- */
-ENTRY(kernel_thread)
-	CFI_STARTPROC
-	FAKE_STACK_FRAME $child_rip
-	SAVE_ALL
-
-	# rdi: flags, rsi: usp, rdx: will be &pt_regs
-	movq %rdx,%rdi
-	orq  kernel_thread_flags(%rip),%rdi
-	movq $-1, %rsi
-	movq %rsp, %rdx
-
-	xorl %r8d,%r8d
-	xorl %r9d,%r9d
-
-	# clone now
-	call do_fork
-	movq %rax,RAX(%rsp)
-	xorl %edi,%edi
-
-	/*
-	 * It isn't worth to check for reschedule here,
-	 * so internally to the x86_64 port you can rely on kernel_thread()
-	 * not to reschedule the child before returning, this avoids the need
-	 * of hacks for example to fork off the per-CPU idle tasks.
-	 * [Hopefully no generic code relies on the reschedule -AK]
-	 */
-	RESTORE_ALL
-	UNFAKE_STACK_FRAME
-	ret
-	CFI_ENDPROC
-END(kernel_thread)
-
-ENTRY(child_rip)
+ENTRY(kernel_thread_helper)
 	pushq $0		# fake return address
 	CFI_STARTPROC
 	/*
 	 * Here we are in the child and the registers are set as they were
 	 * at kernel_thread() invocation in the parent.
 	 */
-	movq %rdi, %rax
-	movq %rsi, %rdi
-	call *%rax
+	call *%rsi
 	# exit
 	mov %eax, %edi
 	call do_exit
 	ud2			# padding for call trace
 	CFI_ENDPROC
-END(child_rip)
+END(kernel_thread_helper)
 
 /*
  * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index eb62cbc..cf1d59d 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -58,8 +58,6 @@ asmlinkage extern void ret_from_fork(void);
 DEFINE_PER_CPU(unsigned long, old_rsp);
 static DEFINE_PER_CPU(unsigned char, is_idle);
 
-unsigned long kernel_thread_flags = CLONE_VM | CLONE_UNTRACED;
-
 static ATOMIC_NOTIFIER_HEAD(idle_notifier);
 
 void idle_notifier_register(struct notifier_block *n)
@@ -231,6 +229,35 @@ void show_regs(struct pt_regs *regs)
 	show_trace(NULL, regs, (void *)(regs + 1), regs->bp);
 }
 
+/*
+ * This gets run with %si containing the
+ * function to call, and %di containing
+ * the "args".
+ */
+extern void kernel_thread_helper(void);
+
+/*
+ * Create a kernel thread
+ */
+int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
+{
+	struct pt_regs regs;
+
+	memset(&regs, 0, sizeof(regs));
+
+	regs.si = (unsigned long) fn;
+	regs.di = (unsigned long) arg;
+
+	regs.orig_ax = -1;
+	regs.ip = (unsigned long) kernel_thread_helper;
+	regs.cs = __KERNEL_CS;
+	regs.flags = X86_EFLAGS_IF;
+
+	/* Ok, create the new process.. */
+	return do_fork(flags | CLONE_VM | CLONE_UNTRACED, ~0UL, &regs, 0, NULL, NULL);
+}
+EXPORT_SYMBOL(kernel_thread);
+
 void release_thread(struct task_struct *dead_task)
 {
 	if (dead_task->mm) {
diff --git a/arch/x86/kernel/x8664_ksyms_64.c b/arch/x86/kernel/x8664_ksyms_64.c
index 3909e3b..f99bc0c 100644
--- a/arch/x86/kernel/x8664_ksyms_64.c
+++ b/arch/x86/kernel/x8664_ksyms_64.c
@@ -17,8 +17,6 @@
 EXPORT_SYMBOL(mcount);
 #endif
 
-EXPORT_SYMBOL(kernel_thread);
-
 EXPORT_SYMBOL(__get_user_1);
 EXPORT_SYMBOL(__get_user_2);
 EXPORT_SYMBOL(__get_user_4);
-- 
1.6.2.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] x86: Merge 32/64-bit kernel_thread
  2009-11-13 19:54 [PATCH 0/4] x86: Merge kernel_thread() Brian Gerst
  2009-11-13 19:54 ` [PATCH 1/4] x86, 64-bit: Move kernel_thread to C Brian Gerst
@ 2009-11-13 19:54 ` Brian Gerst
  2009-11-13 19:54 ` [PATCH 3/4] x86, 32-bit: Don't push args on stack in kernel_thread_helper Brian Gerst
  2009-11-13 19:54 ` [PATCH 4/4] x86, 64-bit: Use user_mode() to determine new stack pointer in copy_thread() Brian Gerst
  3 siblings, 0 replies; 5+ messages in thread
From: Brian Gerst @ 2009-11-13 19:54 UTC (permalink / raw)
  To: Ingo Molnar; +Cc: x86, linux-kernel

Signed-off-by: Brian Gerst <brgerst@gmail.com>
---
 arch/x86/kernel/entry_32.S   |    6 +++---
 arch/x86/kernel/process.c    |   34 ++++++++++++++++++++++++++++++++++
 arch/x86/kernel/process_32.c |   33 ---------------------------------
 arch/x86/kernel/process_64.c |   29 -----------------------------
 4 files changed, 37 insertions(+), 65 deletions(-)

diff --git a/arch/x86/kernel/entry_32.S b/arch/x86/kernel/entry_32.S
index c097e7d..2778ae3 100644
--- a/arch/x86/kernel/entry_32.S
+++ b/arch/x86/kernel/entry_32.S
@@ -984,10 +984,10 @@ END(spurious_interrupt_bug)
 ENTRY(kernel_thread_helper)
 	pushl $0		# fake return address for unwinder
 	CFI_STARTPROC
-	movl %edx,%eax
-	push %edx
+	movl %edi,%eax
+	push %edi
 	CFI_ADJUST_CFA_OFFSET 4
-	call *%ebx
+	call *%esi
 	push %eax
 	CFI_ADJUST_CFA_OFFSET 4
 	call do_exit
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index 5284cd2..71a22c9 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -247,6 +247,40 @@ int sys_vfork(struct pt_regs *regs)
 		       NULL, NULL);
 }
 
+extern void kernel_thread_helper(void);
+
+/*
+ * Create a kernel thread
+ */
+int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
+{
+	struct pt_regs regs;
+	unsigned long newsp;
+
+	memset(&regs, 0, sizeof(regs));
+
+	regs.si = (unsigned long) fn;
+	regs.di = (unsigned long) arg;
+
+#ifdef CONFIG_X86_32
+	regs.ds = __USER_DS;
+	regs.es = __USER_DS;
+	regs.fs = __KERNEL_PERCPU;
+	regs.gs = __KERNEL_STACK_CANARY;
+	newsp = 0;
+#else
+	newsp = ~0UL;
+#endif
+
+	regs.orig_ax = -1;
+	regs.ip = (unsigned long) kernel_thread_helper;
+	regs.cs = __KERNEL_CS | get_kernel_rpl();
+	regs.flags = X86_EFLAGS_IF | 2;
+
+	/* Ok, create the new process.. */
+	return do_fork(flags | CLONE_VM | CLONE_UNTRACED, newsp, &regs, 0, NULL, NULL);
+}
+EXPORT_SYMBOL(kernel_thread);
 
 /*
  * Idle related variables and functions
diff --git a/arch/x86/kernel/process_32.c b/arch/x86/kernel/process_32.c
index 4cf7956..8afe621 100644
--- a/arch/x86/kernel/process_32.c
+++ b/arch/x86/kernel/process_32.c
@@ -191,39 +191,6 @@ void show_regs(struct pt_regs *regs)
 	show_trace(NULL, regs, &regs->sp, regs->bp);
 }
 
-/*
- * This gets run with %bx containing the
- * function to call, and %dx containing
- * the "args".
- */
-extern void kernel_thread_helper(void);
-
-/*
- * Create a kernel thread
- */
-int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
-{
-	struct pt_regs regs;
-
-	memset(&regs, 0, sizeof(regs));
-
-	regs.bx = (unsigned long) fn;
-	regs.dx = (unsigned long) arg;
-
-	regs.ds = __USER_DS;
-	regs.es = __USER_DS;
-	regs.fs = __KERNEL_PERCPU;
-	regs.gs = __KERNEL_STACK_CANARY;
-	regs.orig_ax = -1;
-	regs.ip = (unsigned long) kernel_thread_helper;
-	regs.cs = __KERNEL_CS | get_kernel_rpl();
-	regs.flags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
-
-	/* Ok, create the new process.. */
-	return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
-}
-EXPORT_SYMBOL(kernel_thread);
-
 void release_thread(struct task_struct *dead_task)
 {
 	BUG_ON(dead_task->mm);
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index cf1d59d..8151a82 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -229,35 +229,6 @@ void show_regs(struct pt_regs *regs)
 	show_trace(NULL, regs, (void *)(regs + 1), regs->bp);
 }
 
-/*
- * This gets run with %si containing the
- * function to call, and %di containing
- * the "args".
- */
-extern void kernel_thread_helper(void);
-
-/*
- * Create a kernel thread
- */
-int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
-{
-	struct pt_regs regs;
-
-	memset(&regs, 0, sizeof(regs));
-
-	regs.si = (unsigned long) fn;
-	regs.di = (unsigned long) arg;
-
-	regs.orig_ax = -1;
-	regs.ip = (unsigned long) kernel_thread_helper;
-	regs.cs = __KERNEL_CS;
-	regs.flags = X86_EFLAGS_IF;
-
-	/* Ok, create the new process.. */
-	return do_fork(flags | CLONE_VM | CLONE_UNTRACED, ~0UL, &regs, 0, NULL, NULL);
-}
-EXPORT_SYMBOL(kernel_thread);
-
 void release_thread(struct task_struct *dead_task)
 {
 	if (dead_task->mm) {
-- 
1.6.2.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] x86, 32-bit: Don't push args on stack in kernel_thread_helper
  2009-11-13 19:54 [PATCH 0/4] x86: Merge kernel_thread() Brian Gerst
  2009-11-13 19:54 ` [PATCH 1/4] x86, 64-bit: Move kernel_thread to C Brian Gerst
  2009-11-13 19:54 ` [PATCH 2/4] x86: Merge 32/64-bit kernel_thread Brian Gerst
@ 2009-11-13 19:54 ` Brian Gerst
  2009-11-13 19:54 ` [PATCH 4/4] x86, 64-bit: Use user_mode() to determine new stack pointer in copy_thread() Brian Gerst
  3 siblings, 0 replies; 5+ messages in thread
From: Brian Gerst @ 2009-11-13 19:54 UTC (permalink / raw)
  To: Ingo Molnar; +Cc: x86, linux-kernel

regparm(3) is the normal calling convention now.

Signed-off-by: Brian Gerst <brgerst@gmail.com>
---
 arch/x86/kernel/entry_32.S |    4 ----
 1 files changed, 0 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kernel/entry_32.S b/arch/x86/kernel/entry_32.S
index 2778ae3..96e9c32 100644
--- a/arch/x86/kernel/entry_32.S
+++ b/arch/x86/kernel/entry_32.S
@@ -985,11 +985,7 @@ ENTRY(kernel_thread_helper)
 	pushl $0		# fake return address for unwinder
 	CFI_STARTPROC
 	movl %edi,%eax
-	push %edi
-	CFI_ADJUST_CFA_OFFSET 4
 	call *%esi
-	push %eax
-	CFI_ADJUST_CFA_OFFSET 4
 	call do_exit
 	ud2			# padding for call trace
 	CFI_ENDPROC
-- 
1.6.2.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] x86, 64-bit: Use user_mode() to determine new stack pointer in copy_thread()
  2009-11-13 19:54 [PATCH 0/4] x86: Merge kernel_thread() Brian Gerst
                   ` (2 preceding siblings ...)
  2009-11-13 19:54 ` [PATCH 3/4] x86, 32-bit: Don't push args on stack in kernel_thread_helper Brian Gerst
@ 2009-11-13 19:54 ` Brian Gerst
  3 siblings, 0 replies; 5+ messages in thread
From: Brian Gerst @ 2009-11-13 19:54 UTC (permalink / raw)
  To: Ingo Molnar; +Cc: x86, linux-kernel

Use user_mode() instead of a magic value for sp to determine when returning
to kernel mode.  This simplifies kernel_thread().

Signed-off-by: Brian Gerst <brgerst@gmail.com>
---
 arch/x86/kernel/process.c    |    6 +-----
 arch/x86/kernel/process_64.c |    5 +++--
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index 71a22c9..4ebe3e4 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -255,7 +255,6 @@ extern void kernel_thread_helper(void);
 int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
 {
 	struct pt_regs regs;
-	unsigned long newsp;
 
 	memset(&regs, 0, sizeof(regs));
 
@@ -267,9 +266,6 @@ int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
 	regs.es = __USER_DS;
 	regs.fs = __KERNEL_PERCPU;
 	regs.gs = __KERNEL_STACK_CANARY;
-	newsp = 0;
-#else
-	newsp = ~0UL;
 #endif
 
 	regs.orig_ax = -1;
@@ -278,7 +274,7 @@ int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
 	regs.flags = X86_EFLAGS_IF | 2;
 
 	/* Ok, create the new process.. */
-	return do_fork(flags | CLONE_VM | CLONE_UNTRACED, newsp, &regs, 0, NULL, NULL);
+	return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
 }
 EXPORT_SYMBOL(kernel_thread);
 
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index 8151a82..a9be0d5 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -283,8 +283,9 @@ int copy_thread(unsigned long clone_flags, unsigned long sp,
 	*childregs = *regs;
 
 	childregs->ax = 0;
-	childregs->sp = sp;
-	if (sp == ~0UL)
+	if (user_mode(regs))
+		childregs->sp = sp;
+	else
 		childregs->sp = (unsigned long)childregs;
 
 	p->thread.sp = (unsigned long) childregs;
-- 
1.6.2.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-11-13 19:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-13 19:54 [PATCH 0/4] x86: Merge kernel_thread() Brian Gerst
2009-11-13 19:54 ` [PATCH 1/4] x86, 64-bit: Move kernel_thread to C Brian Gerst
2009-11-13 19:54 ` [PATCH 2/4] x86: Merge 32/64-bit kernel_thread Brian Gerst
2009-11-13 19:54 ` [PATCH 3/4] x86, 32-bit: Don't push args on stack in kernel_thread_helper Brian Gerst
2009-11-13 19:54 ` [PATCH 4/4] x86, 64-bit: Use user_mode() to determine new stack pointer in copy_thread() Brian Gerst

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox