Skip to contentSkip to conversation

xtensa: conversion to generic kernel_thread and friends

PATCH In review Max Filippov 4 participants 9 messages Oct 29, 2012

Subsystems (2)
lore
Share & tools
lore

Max Filippov

author maintainer
#0 lore raw

v2 cover xtensa: conversion to generic kernel_thread and friends

Hi.

This is v2 of xtensa conversion to generic kernel_thread and friends series, for inclusion into git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
tree.

Changes v1 -> v2:
- rebase on top of czankel/xtensa-linux dev branch; a fix to copy_thread from that branch on which kernel_thread conversion depends is also included;
- move pt_regs memset from copy_thread to start_thread;
- drop {set,clear}_child_tid manipulation;
- don't look at regs argument to copy_thread;
- move zero newsp logic from xtensa_clone to copy_thread.

Chris Zankel (1):
xtensa: reset windowbase/windowstart when cloning the VM

Max Filippov (3):
xtensa: switch to generic kernel_thread()
xtensa: switch to generic kernel_execve()
xtensa: switch to generic sys_execve()

 arch/xtensa/Kconfig                   |    2 +
 arch/xtensa/include/asm/processor.h   |    4 +-
 arch/xtensa/include/asm/syscall.h     |    2 +-
 arch/xtensa/include/asm/unistd.h      |    1 +
 arch/xtensa/include/uapi/asm/unistd.h |    2 +-
 arch/xtensa/kernel/entry.S            |   57 ++++-----------
 arch/xtensa/kernel/process.c          |  128 ++++++++++++++++++---------------
 arch/xtensa/kernel/xtensa_ksyms.c     |    1 -
 8 files changed, 90 insertions(+), 107 deletions(-)
Signature · 1 line
1.7.7.6
author maintainer
#1 lore raw

PATCH v2 1/4 xtensa: reset windowbase/windowstart when cloning the VM

From: Chris Zankel <[email protected]>

When we copy a user thread with CLONE_VM, we also have to reset windowbase and windowstart to start a pristine stack frame. Otherwise, overflows can happen using the address 0 as the stack pointer. Also add a special case for vfork, which continues on the parent stack until it calls execve. Because this could be a call8, we need to spill the stack pointer of the previus frame (if still 'live' in the register file).

---
 arch/xtensa/kernel/process.c |   26 +++++++++++++++++++++++++-
 1 files changed, 25 insertions(+), 1 deletions(-)
arch/xtensa/kernel/process.c +25 -1
index 1908f66..341c151 100644
@@ -220,8 +220,32 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 	if (user_mode(regs)) {
 
 		childregs->areg[1] = usp;
+
+		/* When sharing memory with the parent thread, the child
+		   usually starts on a pristine stack, so we have to reset
+		   windowbase, windowstart and wmask.
+		   (Note that such a new thread is required to always create
+		   an initial call4 frame)
+		   The exception is vfork, where the new thread continues to
+		   run on the parent's stack until it calls execve. This could
+		   be a call8 or call12, which requires a legal stack frame
+		   of the previous caller for the overflow handlers to work.
+		   (Note that it's always legal to overflow live registers).
+		   In this case, ensure to spill at least the stack pointer
+		   of that frame. */
+
 		if (clone_flags & CLONE_VM) {
-			childregs->wmask = 1;	/* can't share live windows */
+			/* check that caller window is live and same stack */
+			int len = childregs->wmask & ~0xf;
+			if (regs->areg[1] == usp && len != 0) {
+				int callinc = (regs->areg[0] >> 30) & 3;
+				int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
+				put_user(regs->areg[caller_ars+1],
+					 (unsigned __user*)(usp - 12));
+			}
+			childregs->wmask = 1;
+			childregs->windowstart = 1;
+			childregs->windowbase = 0;
 		} else {
 			int len = childregs->wmask & ~0xf;
 			memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
Signature · 1 line
1.7.7.6
author maintainer
#3 lore raw

PATCH v2 2/4 xtensa: switch to generic kernel_thread()

In reply to Max Filippov
---
 arch/xtensa/Kconfig                 |    1 +
 arch/xtensa/include/asm/processor.h |    4 +-
 arch/xtensa/kernel/entry.S          |   41 ++++++-------------
 arch/xtensa/kernel/process.c        |   77 +++++++++++++++++++++--------------
 arch/xtensa/kernel/xtensa_ksyms.c   |    1 -
 5 files changed, 61 insertions(+), 63 deletions(-)
arch/xtensa/Kconfig +1 -0
index 75bb9a5..1033a1a 100644
@@ -13,6 +13,7 @@ config XTENSA
 	select GENERIC_CPU_DEVICES
 	select MODULES_USE_ELF_RELA
 	select GENERIC_PCI_IOMAP
+	select GENERIC_KERNEL_THREAD
 	select ARCH_WANT_OPTIONAL_GPIOLIB
 	select OF
 	select OF_EARLY_FLATTREE
arch/xtensa/include/asm/processor.h +1 -3
index 5d6c536..c1fc2d4 100644
@@ -152,6 +152,7 @@ struct thread_struct {
 
 /* Clearing a0 terminates the backtrace. */
 #define start_thread(regs, new_pc, new_sp) \
+	memset(regs, 0, sizeof(*regs)); \
 	regs->pc = new_pc; \
 	regs->ps = USER_PS_VALUE; \
 	regs->areg[1] = new_sp; \
@@ -168,9 +169,6 @@ struct mm_struct;
 /* Free all resources held by a thread. */
 #define release_thread(thread) do { } while(0)
 
-/* Create a kernel thread without removing it from tasklists */
-extern int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags);
-
 /* Copy and release all segment info associated with a VM */
 #define copy_segments(p, mm)	do { } while(0)
 #define release_segments(mm)	do { } while(0)
arch/xtensa/kernel/entry.S +13 -28

50 lines not on this page · raw message

arch/xtensa/kernel/process.c +46 -31

130 lines not on this page · raw message

arch/xtensa/kernel/xtensa_ksyms.c +0 -1
index a8b9f1f..afe058b 100644
@@ -43,7 +43,6 @@ EXPORT_SYMBOL(__strncpy_user);
 EXPORT_SYMBOL(clear_page);
 EXPORT_SYMBOL(copy_page);
 
-EXPORT_SYMBOL(kernel_thread);
 EXPORT_SYMBOL(empty_zero_page);
 
 /*
Signature · 1 line
1.7.7.6
author maintainer
#4 lore raw

PATCH v2 3/4 xtensa: switch to generic kernel_execve()

In reply to Max Filippov
---
 arch/xtensa/Kconfig        |    1 +
 arch/xtensa/kernel/entry.S |   18 +-----------------
 2 files changed, 2 insertions(+), 17 deletions(-)
arch/xtensa/Kconfig +1 -0
index 1033a1a..e957454 100644
@@ -14,6 +14,7 @@ config XTENSA
 	select MODULES_USE_ELF_RELA
 	select GENERIC_PCI_IOMAP
 	select GENERIC_KERNEL_THREAD
+	select GENERIC_KERNEL_EXECVE
 	select ARCH_WANT_OPTIONAL_GPIOLIB
 	select OF
 	select OF_EARLY_FLATTREE
arch/xtensa/kernel/entry.S +1 -17
index 0fc994f..7da2c02 100644
@@ -1833,22 +1833,6 @@ ENTRY(system_call)
 
 
 /*
- * Do a system call from kernel instead of calling sys_execve, so we end up
- * with proper pt_regs.
- *
- * int kernel_execve(const char *fname, char *const argv[], charg *const envp[])
- * a2                        a2               a3                  a4
- */
-
-ENTRY(kernel_execve)
-	entry	a1, 16
-	mov	a6, a2			# arg0 is in a6
-	movi	a2, __NR_execve
-	syscall
-
-	retw
-
-/*
  * Task switch.
  *
  * struct task*  _switch_to (struct task* prev, struct task* next)
@@ -1940,6 +1924,6 @@ ENTRY(ret_from_kernel_thread)
 	call4	schedule_tail
 	mov	a6, a3
 	callx4	a2
-	call4	do_exit
+	j	common_exception_return
 
 ENDPROC(ret_from_kernel_thread)
Signature · 1 line
1.7.7.6
author maintainer
#5 lore raw

PATCH v2 4/4 xtensa: switch to generic sys_execve()

In reply to Max Filippov
---
 arch/xtensa/include/asm/syscall.h     |    2 +-
 arch/xtensa/include/asm/unistd.h      |    1 +
 arch/xtensa/include/uapi/asm/unistd.h |    2 +-
 arch/xtensa/kernel/process.c          |   25 -------------------------
 4 files changed, 3 insertions(+), 27 deletions(-)
arch/xtensa/include/asm/syscall.h +1 -1
index c1dacca..124aeee 100644
@@ -10,7 +10,7 @@
 
 struct pt_regs;
 struct sigaction;
-asmlinkage long xtensa_execve(char*, char**, char**, struct pt_regs*);
+asmlinkage long sys_execve(char*, char**, char**, struct pt_regs*);
 asmlinkage long xtensa_clone(unsigned long, unsigned long, struct pt_regs*);
 asmlinkage long xtensa_ptrace(long, long, long, long);
 asmlinkage long xtensa_sigreturn(struct pt_regs*);
arch/xtensa/include/asm/unistd.h +1 -0
index d9fa52d..f4e6eaa 100644
@@ -1,6 +1,7 @@
 #ifndef _XTENSA_UNISTD_H
 #define _XTENSA_UNISTD_H
 
+#define __ARCH_WANT_SYS_EXECVE
 #include <uapi/asm/unistd.h>
 
 /*
arch/xtensa/include/uapi/asm/unistd.h +1 -1
index 1fd157f..9f36d0e 100644
@@ -262,7 +262,7 @@ __SYSCALL(115, sys_sendmmsg, 4)
 #define __NR_clone 				116
 __SYSCALL(116, xtensa_clone, 5)
 #define __NR_execve 				117
-__SYSCALL(117, xtensa_execve, 3)
+__SYSCALL(117, sys_execve, 3)
 #define __NR_exit 				118
 __SYSCALL(118, sys_exit, 1)
 #define __NR_exit_group 			119
arch/xtensa/kernel/process.c +0 -25
index be52fe4..09ae7bf 100644
@@ -373,28 +373,3 @@ long xtensa_clone(unsigned long clone_flags, unsigned long newsp,
 {
         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
 }
-
-/*
- * xtensa_execve() executes a new program.
- */
-
-asmlinkage
-long xtensa_execve(const char __user *name,
-		   const char __user *const __user *argv,
-                   const char __user *const __user *envp,
-                   long a3, long a4, long a5,
-                   struct pt_regs *regs)
-{
-	long error;
-	struct filename *filename;
-
-	filename = getname(name);
-	error = PTR_ERR(filename);
-	if (IS_ERR(filename))
-		goto out;
-	error = do_execve(filename->name, argv, envp, regs);
-	putname(filename);
-out:
-	return error;
-}
-
Signature · 1 line
1.7.7.6
Acked-by on v2 1/4
#2 lore raw

RE: [PATCH v2 1/4] xtensa: reset windowbase/windowstart when cloning the VM

In reply to Max Filippov
Quoted 58 lines · Max Filippov

From: Chris Zankel <[email protected]>

When we copy a user thread with CLONE_VM, we also have to reset windowbase and windowstart to start a pristine stack frame. Otherwise, overflows can happen using the address 0 as the stack pointer. Also add a special case for vfork, which continues on the parent stack until it calls execve. Because this could be a call8, we need to spill the stack pointer of the previus frame (if still 'live' in
the register file).

---
 arch/xtensa/kernel/process.c |   26 +++++++++++++++++++++++++-
 1 files changed, 25 insertions(+), 1 deletions(-)
arch/xtensa/kernel/process.c +3 -0
index 1908f66..341c151 100644
@@ -220,8 +220,32 @@ int copy_thread(unsigned long
 clone_flags, unsigned long usp,
      if (user_mode(regs)) {
 
              childregs->areg[1] = usp;
+
+             /* When sharing memory with the parent thread, the child
+                usually starts on a pristine stack, so we

have to reset

arch/xtensa/kernel/process.c +2 -0
+                windowbase, windowstart and wmask.
+                (Note that such a new thread is required to

always create

arch/xtensa/kernel/process.c +2 -0
+                an initial call4 frame)
+                The exception is vfork, where the new thread

continues to
+ run on the parent's stack until it calls
execve. This could
+ be a call8 or call12, which requires a legal
stack frame
+ of the previous caller for the overflow
handlers to work.
+ (Note that it's always legal to overflow
live registers).
+ In this case, ensure to spill at least the
stack pointer

arch/xtensa/kernel/process.c +2 -1
+                of that frame. */
+
              if (clone_flags & CLONE_VM) {
-                     childregs->wmask = 1;   /* can't share

live windows */
+ /* check that caller window is live and
same stack */

arch/xtensa/kernel/process.c +4 -0
+                     int len = childregs->wmask & ~0xf;
+                     if (regs->areg[1] == usp && len != 0) {
+                             int callinc = (regs->areg[0] >> 30) & 3;
+                             int caller_ars =

XCHAL_NUM_AREGS - callinc * 4;

arch/xtensa/kernel/process.c int copy_thread(unsigned long clone_flags, unsigned long usp,

Quoted 10 lines
arch/xtensa/kernel/process.c +6 -1
+                             put_user(regs->areg[caller_ars+1],
+                                      (unsigned __user*)(usp - 12));
+                     }
+                     childregs->wmask = 1;
+                     childregs->windowstart = 1;
+                     childregs->windowbase = 0;
              } else {
                      int len = childregs->wmask & ~0xf;
                      memcpy(&childregs->areg[XCHAL_NUM_AREGS
- len/4],
maintainer
#6 lore raw
In reply to Max Filippov

Hi Max,

Has anything changed from the version that's already in linux-next? I wanted to push that version upstream today.

Thanks,
-Chris

Quoted 32 lines · Max Filippov

Hi.

This is v2 of xtensa conversion to generic kernel_thread and friends series, for inclusion into git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
tree.

Changes v1 -> v2:
- rebase on top of czankel/xtensa-linux dev branch; a fix to copy_thread from that branch on which kernel_thread conversion depends is also included;
- move pt_regs memset from copy_thread to start_thread;
- drop {set,clear}_child_tid manipulation;
- don't look at regs argument to copy_thread;
- move zero newsp logic from xtensa_clone to copy_thread.

Chris Zankel (1):
xtensa: reset windowbase/windowstart when cloning the VM

Max Filippov (3):
xtensa: switch to generic kernel_thread()
xtensa: switch to generic kernel_execve()
xtensa: switch to generic sys_execve()

  arch/xtensa/Kconfig                   |    2 +
  arch/xtensa/include/asm/processor.h   |    4 +-
  arch/xtensa/include/asm/syscall.h     |    2 +-
  arch/xtensa/include/asm/unistd.h      |    1 +
  arch/xtensa/include/uapi/asm/unistd.h |    2 +-
  arch/xtensa/kernel/entry.S            |   57 ++++-----------
  arch/xtensa/kernel/process.c          |  128 ++++++++++++++++++---------------
  arch/xtensa/kernel/xtensa_ksyms.c     |    1 -
  8 files changed, 90 insertions(+), 107 deletions(-)
author maintainer
#7 lore raw
Quoted 4 lines · czankel

Hi Max,

Has anything changed from the version that's already in linux-next? I wanted to push that version upstream today.

It's the same. I sent self-contained series to Al in case it blocks whatever.

Quoted 38 lines · Max Filippov
Quoted 37 lines

Hi.

This is v2 of xtensa conversion to generic kernel_thread and friends series,
for inclusion into
git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
tree.

Changes v1 -> v2:
- rebase on top of czankel/xtensa-linux dev branch; a fix to copy_thread from
that branch on which kernel_thread conversion depends is also included;
- move pt_regs memset from copy_thread to start_thread;
- drop {set,clear}_child_tid manipulation;
- don't look at regs argument to copy_thread;
- move zero newsp logic from xtensa_clone to copy_thread.

Chris Zankel (1):
xtensa: reset windowbase/windowstart when cloning the VM

Max Filippov (3):
xtensa: switch to generic kernel_thread()
xtensa: switch to generic kernel_execve()
xtensa: switch to generic sys_execve()

  arch/xtensa/Kconfig                   |    2 +
  arch/xtensa/include/asm/processor.h   |    4 +-
  arch/xtensa/include/asm/syscall.h     |    2 +-
  arch/xtensa/include/asm/unistd.h      |    1 +
  arch/xtensa/include/uapi/asm/unistd.h |    2 +-
  arch/xtensa/kernel/entry.S            |   57 ++++-----------
  arch/xtensa/kernel/process.c          |  128
++++++++++++++++++---------------
  arch/xtensa/kernel/xtensa_ksyms.c     |    1 -
  8 files changed, 90 insertions(+), 107 deletions(-)
Signature · 2 lines
Thanks.
-- Max
#8 lore raw
Quoted 7 lines · Max Filippov
Quoted 4 lines · czankel

Hi Max,

Has anything changed from the version that's already in linux-next? I wanted to push that version upstream today.

It's the same. I sent self-contained series to Al in case it blocks whatever.

I'd very much prefer to pull from xtensa tree, to avoid the PITA with merge conflicts later. It's your current #for_next, right? And there's not much besides those commits in there, so I'm grabbing that one...