This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit bb9b58bddea2d7e188edbecfbbf38f420c2d5ad9
Author: Gregory Nutt <[email protected]>
AuthorDate: Mon Jun 29 08:26:29 2020 -0600

    libc: Move pthread_create to user space
    
    Signed-off-by: Huang Qi <[email protected]>
    Change-Id: I5c447d94077debc79158686935f288e4c8e51e01
---
 arch/arm/src/armv6-m/arm_svcall.c                  | 12 ++--
 arch/arm/src/armv6-m/svcall.h                      | 21 +++---
 arch/arm/src/armv7-a/arm_syscall.c                 | 12 ++--
 arch/arm/src/armv7-a/svcall.h                      | 20 +++---
 arch/arm/src/armv7-m/arm_svcall.c                  | 12 ++--
 arch/arm/src/armv7-m/svcall.h                      | 20 +++---
 arch/arm/src/armv7-r/arm_syscall.c                 |  6 +-
 arch/arm/src/armv7-r/svcall.h                      | 20 +++---
 arch/arm/src/armv8-m/arm_svcall.c                  | 10 +--
 arch/arm/src/armv8-m/svcall.h                      | 20 +++---
 arch/arm/src/common/arm_pthread_start.c            | 11 +--
 arch/or1k/src/common/up_pthread_start.c            |  6 +-
 arch/risc-v/src/common/riscv_pthread_start.c       |  9 ++-
 arch/risc-v/src/rv64gc/riscv_swint.c               | 12 ++--
 arch/risc-v/src/rv64gc/svcall.h                    | 21 +++---
 .../imxrt/imxrt1050-evk/kernel/imxrt_userspace.c   |  3 -
 .../imxrt/imxrt1060-evk/kernel/imxrt_userspace.c   |  3 -
 .../lc823450-xgevk/kernel/lc823450_userspace.c     |  3 -
 .../lpc4088-devkit/kernel/lpc17_40_userspace.c     |  3 -
 .../lpc4088-quickstart/kernel/lpc17_40_userspace.c |  3 -
 .../open1788/kernel/lpc17_40_userspace.c           |  3 -
 .../pnev5180b/kernel/lpc17_40_userspace.c          |  3 -
 .../lpc43xx/bambino-200e/kernel/lpc43_userspace.c  |  3 -
 boards/arm/sam34/sam3u-ek/kernel/sam_userspace.c   |  3 -
 .../samv7/same70-xplained/kernel/sam_userspace.c   |  3 -
 .../arm/samv7/samv71-xult/kernel/sam_userspace.c   |  3 -
 .../stm32/clicker2-stm32/kernel/stm32_userspace.c  |  3 -
 .../stm32/mikroe-stm32f4/kernel/stm32_userspace.c  |  3 -
 .../olimex-stm32-p407/kernel/stm32_userspace.c     |  3 -
 .../arm/stm32/omnibusf4/kernel/stm32_userspace.c   |  3 -
 .../stm32/stm3240g-eval/kernel/stm32_userspace.c   |  3 -
 .../stm32f4discovery/kernel/stm32_userspace.c      |  3 -
 .../stm32f746g-disco/kernel/stm32_userspace.c      |  3 -
 .../stm32f769i-disco/kernel/stm32_userspace.c      |  3 -
 .../stm32h7/nucleo-h743zi/kernel/stm32_userspace.c |  3 -
 .../stm32h747i-disco/kernel/stm32_userspace.c      |  3 -
 .../stm32l476vg-disco/kernel/stm32l4_userspace.c   |  3 -
 .../stm32l4r9ai-disco/kernel/stm32l4_userspace.c   |  3 -
 boards/arm/tiva/lm3s6965-ek/kernel/lm_userspace.c  |  3 -
 .../risc-v/k210/maix-bit/kernel/k210_userspace.c   |  3 -
 include/nuttx/arch.h                               |  6 +-
 include/nuttx/pthread.h                            | 30 +++++++-
 include/nuttx/sched.h                              |  1 +
 include/nuttx/userspace.h                          | 26 +------
 include/pthread.h                                  |  2 +
 include/sys/syscall_lookup.h                       |  2 +-
 libs/libc/pthread/Make.defs                        | 14 ++--
 .../{pthread_startup.c => pthread_create.c}        | 80 +++++++++++-----------
 sched/pthread/pthread_create.c                     | 50 ++++++++------
 syscall/syscall.csv                                |  2 +-
 50 files changed, 235 insertions(+), 265 deletions(-)

diff --git a/arch/arm/src/armv6-m/arm_svcall.c 
b/arch/arm/src/armv6-m/arm_svcall.c
index d7b5430..0339ccc 100644
--- a/arch/arm/src/armv6-m/arm_svcall.c
+++ b/arch/arm/src/armv6-m/arm_svcall.c
@@ -310,22 +310,22 @@ int arm_svcall(int irq, FAR void *context, FAR void *arg)
        *   R2 = arg
        */
 
-#if defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_DISABLE_PTHREAD)
+#if !defined(CONFIG_BUILD_FLAT) && !defined(CONFIG_DISABLE_PTHREAD)
       case SYS_pthread_start:
         {
           /* Set up to return to the user-space pthread start-up function in
            * unprivileged mode.
            */
 
-          regs[REG_PC]         = (uint32_t)USERSPACE->pthread_startup;
+          regs[REG_PC]         = (uint32_t)regs[REG_R1]; /* startup */
           regs[REG_EXC_RETURN] = EXC_RETURN_UNPRIVTHR;
 
-          /* Change the parameter ordering to match the expectation of struct
-           * userpace_s pthread_startup:
+          /* Change the parameter ordering to match the expectation of the
+           * user space pthread_startup:
            */
 
-          regs[REG_R0]         = regs[REG_R1]; /* pthread entry */
-          regs[REG_R1]         = regs[REG_R2]; /* arg */
+          regs[REG_R0]         = regs[REG_R2]; /* pthread entry */
+          regs[REG_R1]         = regs[REG_R3]; /* arg */
         }
         break;
 #endif
diff --git a/arch/arm/src/armv6-m/svcall.h b/arch/arm/src/armv6-m/svcall.h
index 7f2f45f..a790f58 100644
--- a/arch/arm/src/armv6-m/svcall.h
+++ b/arch/arm/src/armv6-m/svcall.h
@@ -89,6 +89,7 @@
 
 #define SYS_syscall_return        (3)
 
+#ifndef CONFIG_BUILD_FLAT
 #ifdef CONFIG_BUILD_PROTECTED
 /* SYS call 4:
  *
@@ -98,15 +99,6 @@
 
 #define SYS_task_start            (4)
 
-/* SYS call 5:
- *
- * void up_pthread_start(pthread_startroutine_t entrypt,
- *                       pthread_addr_t arg)
- *        noreturn_function
- */
-
-#define SYS_pthread_start         (5)
-
 /* SYS call 6:
  *
  * void signal_handler(_sa_sigaction_t sighand, int signo,
@@ -124,6 +116,17 @@
 #define SYS_signal_handler_return (7)
 
 #endif /* CONFIG_BUILD_PROTECTED */
+
+/* SYS call 5:
+ *
+ * void up_pthread_start(pthread_startroutine_t startup,
+ *                       pthread_startroutine_t entrypt, pthread_addr_t arg)
+ *        noreturn_function
+ */
+
+#define SYS_pthread_start         (5)
+
+#endif /* !CONFIG_BUILD_FLAT */
 #endif /* CONFIG_LIB_SYSCALL */
 
 /****************************************************************************
diff --git a/arch/arm/src/armv7-a/arm_syscall.c 
b/arch/arm/src/armv7-a/arm_syscall.c
index ea6b31e..e71a6b0 100644
--- a/arch/arm/src/armv7-a/arm_syscall.c
+++ b/arch/arm/src/armv7-a/arm_syscall.c
@@ -290,19 +290,21 @@ uint32_t *arm_syscall(uint32_t *regs)
        *   R2 = arg
        */
 
-#if defined(CONFIG_BUILD_KERNEL) && !defined(CONFIG_DISABLE_PTHREAD)
+#if !defined(CONFIG_BUILD_FLAT) && !defined(CONFIG_DISABLE_PTHREAD)
       case SYS_pthread_start:
         {
           /* Set up to return to the user-space pthread start-up function in
            * unprivileged mode. We need:
            *
-           *   R0   = arg
-           *   PC   = entrypt
+           *   R0   = entrypt
+           *   R1   = arg
+           *   PC   = startup
            *   CSPR = user mode
            */
 
-          regs[REG_PC]   = regs[REG_R1];
-          regs[REG_R0]   = regs[REG_R2];
+          regs[REG_PC]   = regs[REG_R0];
+          regs[REG_R0]   = regs[REG_R1];
+          regs[REG_R1]   = regs[REG_R2];
 
           cpsr           = regs[REG_CPSR] & ~PSR_MODE_MASK;
           regs[REG_CPSR] = cpsr | PSR_MODE_USR;
diff --git a/arch/arm/src/armv7-a/svcall.h b/arch/arm/src/armv7-a/svcall.h
index f0bb700..27dc875 100644
--- a/arch/arm/src/armv7-a/svcall.h
+++ b/arch/arm/src/armv7-a/svcall.h
@@ -65,6 +65,7 @@
 
 #define SYS_syscall_return        (0)
 
+#ifndef CONFIG_BUILD_FLAT
 #ifdef CONFIG_BUILD_KERNEL
 /* SYS call 1:
  *
@@ -81,14 +82,6 @@
 
 #define SYS_task_start            (2)
 
-/* SYS call 3:
- *
- * void up_pthread_start(pthread_startroutine_t entrypt, pthread_addr_t arg)
- *        noreturn_function
- */
-
-#define SYS_pthread_start         (3)
-
 /* SYS call 4:
  *
  * void signal_handler(_sa_sigaction_t sighand,
@@ -105,6 +98,17 @@
 
 #define SYS_signal_handler_return (5)
 
+#endif /* !CONFIG_BUILD_FLAT */
+
+/* SYS call 3:
+ *
+ * void up_pthread_start(pthread_startroutine_t startup,
+ *                       pthread_startroutine_t entrypt, pthread_addr_t arg)
+ *        noreturn_function
+ */
+
+#define SYS_pthread_start         (3)
+
 #endif /* CONFIG_BUILD_KERNEL */
 
 /****************************************************************************
diff --git a/arch/arm/src/armv7-m/arm_svcall.c 
b/arch/arm/src/armv7-m/arm_svcall.c
index 2112ea7..6119ee6 100644
--- a/arch/arm/src/armv7-m/arm_svcall.c
+++ b/arch/arm/src/armv7-m/arm_svcall.c
@@ -324,22 +324,22 @@ int arm_svcall(int irq, FAR void *context, FAR void *arg)
        *   R2 = arg
        */
 
-#if defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_DISABLE_PTHREAD)
+#if !defined(CONFIG_BUILD_FLAT) && !defined(CONFIG_DISABLE_PTHREAD)
       case SYS_pthread_start:
         {
           /* Set up to return to the user-space pthread start-up function in
            * unprivileged mode.
            */
 
-          regs[REG_PC]         = (uint32_t)USERSPACE->pthread_startup & ~1;
+          regs[REG_PC]         = (uint32_t)regs[REG_R1] & ~1;  /* startup */
           regs[REG_EXC_RETURN] = EXC_RETURN_UNPRIVTHR;
 
-          /* Change the parameter ordering to match the expectation of struct
-           * userpace_s pthread_startup:
+          /* Change the parameter ordering to match the expectation of the
+           * user space pthread_startup:
            */
 
-          regs[REG_R0]         = regs[REG_R1]; /* pthread entry */
-          regs[REG_R1]         = regs[REG_R2]; /* arg */
+          regs[REG_R0]         = regs[REG_R2]; /* pthread entry */
+          regs[REG_R1]         = regs[REG_R3]; /* arg */
         }
         break;
 #endif
diff --git a/arch/arm/src/armv7-m/svcall.h b/arch/arm/src/armv7-m/svcall.h
index 9f3b977..98a574e 100644
--- a/arch/arm/src/armv7-m/svcall.h
+++ b/arch/arm/src/armv7-m/svcall.h
@@ -81,6 +81,7 @@
 
 #define SYS_switch_context        (2)
 
+#ifndef CONFIG_BUILD_FLAT
 #ifdef CONFIG_LIB_SYSCALL
 /* SYS call 3:
  *
@@ -98,14 +99,6 @@
 
 #define SYS_task_start            (4)
 
-/* SYS call 5:
- *
- * void up_pthread_start(pthread_startroutine_t entrypt, pthread_addr_t arg)
- *        noreturn_function
- */
-
-#define SYS_pthread_start         (5)
-
 /* SYS call 6:
  *
  * void signal_handler(_sa_sigaction_t sighand,
@@ -123,6 +116,17 @@
 #define SYS_signal_handler_return (7)
 
 #endif /* CONFIG_BUILD_PROTECTED */
+
+/* SYS call 5:
+ *
+ * void up_pthread_start((pthread_startroutine_t startup,
+ *                        pthread_startroutine_t entrypt, pthread_addr_t arg)
+ *        noreturn_function
+ */
+
+#define SYS_pthread_start         (5)
+
+#endif /* !CONFIG_BUILD_FLAT */
 #endif /* CONFIG_LIB_SYSCALL */
 
 /****************************************************************************
diff --git a/arch/arm/src/armv7-r/arm_syscall.c 
b/arch/arm/src/armv7-r/arm_syscall.c
index ff49468..ea827bf 100644
--- a/arch/arm/src/armv7-r/arm_syscall.c
+++ b/arch/arm/src/armv7-r/arm_syscall.c
@@ -285,19 +285,21 @@ uint32_t *arm_syscall(uint32_t *regs)
        *   R2 = arg
        */
 
-#if defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_DISABLE_PTHREAD)
+#if !defined(CONFIG_BUILD_FLAT) && !defined(CONFIG_DISABLE_PTHREAD)
       case SYS_pthread_start:
         {
           /* Set up to return to the user-space pthread start-up function in
            * unprivileged mode. We need:
            *
-           *   R0   = arg
+           *   R0   = startup
+           *   R1   = arg
            *   PC   = entrypt
            *   CSPR = user mode
            */
 
           regs[REG_PC]   = regs[REG_R1];
           regs[REG_R0]   = regs[REG_R2];
+          regs[REG_R1]   = regs[REG_R3];
 
           cpsr           = regs[REG_CPSR] & ~PSR_MODE_MASK;
           regs[REG_CPSR] = cpsr | PSR_MODE_USR;
diff --git a/arch/arm/src/armv7-r/svcall.h b/arch/arm/src/armv7-r/svcall.h
index ffd8737..2470da7 100644
--- a/arch/arm/src/armv7-r/svcall.h
+++ b/arch/arm/src/armv7-r/svcall.h
@@ -66,6 +66,7 @@
 
 #define SYS_syscall_return        (0)
 
+#ifndef CONFIG_BUILD_FLAT
 #ifdef CONFIG_BUILD_PROTECTED
 /* SYS call 1:
  *
@@ -82,14 +83,6 @@
 
 #define SYS_task_start            (2)
 
-/* SYS call 3:
- *
- * void up_pthread_start(pthread_startroutine_t entrypt, pthread_addr_t arg)
- *        noreturn_function
- */
-
-#define SYS_pthread_start         (3)
-
 /* SYS call 4:
  *
  * void signal_handler(_sa_sigaction_t sighand, int signo,
@@ -108,6 +101,17 @@
 
 #endif /* CONFIG_BUILD_PROTECTED */
 
+/* SYS call 3:
+ *
+ * void up_pthread_start(pthread_startroutine_t startup,
+ *                       pthread_startroutine_t entrypt, pthread_addr_t arg)
+ *        noreturn_function
+ */
+
+#define SYS_pthread_start         (3)
+
+#endif /* !CONFIG_BUILD_FLAT */
+
 /****************************************************************************
  * Inline Functions
  ****************************************************************************/
diff --git a/arch/arm/src/armv8-m/arm_svcall.c 
b/arch/arm/src/armv8-m/arm_svcall.c
index ed28937..f0757c7 100644
--- a/arch/arm/src/armv8-m/arm_svcall.c
+++ b/arch/arm/src/armv8-m/arm_svcall.c
@@ -323,21 +323,21 @@ int arm_svcall(int irq, FAR void *context, FAR void *arg)
        *   R2 = arg
        */
 
-#if defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_DISABLE_PTHREAD)
+#if !defined(CONFIG_BUILD_FLAT) && !defined(CONFIG_DISABLE_PTHREAD)
       case SYS_pthread_start:
         {
           /* Set up to return to the user-space pthread start-up function in
            * unprivileged mode.
            */
 
-          regs[REG_PC]         = (uint32_t)USERSPACE->pthread_startup & ~1;
+          regs[REG_PC]         = (uint32_t)regs[REG_R1] & ~1;  /* startup */
           regs[REG_EXC_RETURN] = EXC_RETURN_UNPRIVTHR;
 
-          /* Change the parameter ordering to match the expectation of struct
-           * userpace_s pthread_startup:
+          /* Change the parameter ordering to match the expectation of the
+           * user space pthread_startup:
            */
 
-          regs[REG_R0]         = regs[REG_R1]; /* pthread entry */
+          regs[REG_R0]         = regs[REG_R2]; /* pthread entry */
           regs[REG_R1]         = regs[REG_R2]; /* arg */
         }
         break;
diff --git a/arch/arm/src/armv8-m/svcall.h b/arch/arm/src/armv8-m/svcall.h
index 77a1f92..7213084 100644
--- a/arch/arm/src/armv8-m/svcall.h
+++ b/arch/arm/src/armv8-m/svcall.h
@@ -88,6 +88,7 @@
 
 #define SYS_syscall_return        (3)
 
+#ifndef CONFIG_BUILD_FLAT
 #ifdef CONFIG_BUILD_PROTECTED
 /* SYS call 4:
  *
@@ -97,14 +98,6 @@
 
 #define SYS_task_start            (4)
 
-/* SYS call 5:
- *
- * void up_pthread_start(pthread_startroutine_t entrypt, pthread_addr_t arg)
- *        noreturn_function
- */
-
-#define SYS_pthread_start         (5)
-
 /* SYS call 6:
  *
  * void signal_handler(_sa_sigaction_t sighand,
@@ -122,6 +115,17 @@
 #define SYS_signal_handler_return (7)
 
 #endif /* CONFIG_BUILD_PROTECTED */
+
+/* SYS call 5:
+ *
+ * void up_pthread_start(pthread_startroutine_t startup,
+ *                       pthread_startroutine_t entrypt, pthread_addr_t arg)
+ *        noreturn_function
+ */
+
+#define SYS_pthread_start         (5)
+
+#endif /* !CONFIG_BUILD_FLAT */
 #endif /* CONFIG_LIB_SYSCALL */
 
 /****************************************************************************
diff --git a/arch/arm/src/common/arm_pthread_start.c 
b/arch/arm/src/common/arm_pthread_start.c
index f96c4a5..7e7e8e7 100644
--- a/arch/arm/src/common/arm_pthread_start.c
+++ b/arch/arm/src/common/arm_pthread_start.c
@@ -47,9 +47,10 @@
  *   pthread.
  *
  *   Normally the a user-mode start-up stub will also execute before the
- *   pthread actually starts.  See libc/pthread/pthread_startup.c
+ *   pthread actually starts.  See libc/pthread/pthread_create.c
  *
  * Input Parameters:
+ *   startup - The user-space pthread startup function
  *   entrypt - The user-space address of the pthread entry point
  *   arg     - Standard argument for the pthread entry point
  *
@@ -60,11 +61,13 @@
  *
  ****************************************************************************/
 
-void up_pthread_start(pthread_startroutine_t entrypt, pthread_addr_t arg)
+void up_pthread_start(pthread_trampoline_t startup,
+                      pthread_startroutine_t entrypt, pthread_addr_t arg)
 {
-  /* Let sys_call2() do all of the work */
+  /* Let sys_call3() do all of the work */
 
-  sys_call2(SYS_pthread_start, (uintptr_t)entrypt, (uintptr_t)arg);
+  sys_call3(SYS_pthread_start, (uintptr_t)startup, (uintptr_t)entrypt,
+            (uintptr_t)arg);
 
   PANIC();
 }
diff --git a/arch/or1k/src/common/up_pthread_start.c 
b/arch/or1k/src/common/up_pthread_start.c
index f32c4bb..8edbd06 100644
--- a/arch/or1k/src/common/up_pthread_start.c
+++ b/arch/or1k/src/common/up_pthread_start.c
@@ -47,9 +47,10 @@
  *   pthread.
  *
  *   Normally the a user-mode start-up stub will also execute before the
- *   pthread actually starts.  See libc/pthread/pthread_startup.c
+ *   pthread actually starts.  See libc/pthread/pthread_create.c
  *
  * Input Parameters:
+ *   startup - The user-space pthread startup function
  *   entrypt - The user-space address of the pthread entry point
  *   arg     - Standard argument for the pthread entry point
  *
@@ -66,7 +67,8 @@ void up_pthread_start(pthread_startroutine_t entrypt, 
pthread_addr_t arg)
 
   sinfo("entry %p arg %p\n", entrypt, arg);
 
-  sys_call2(SYS_pthread_start, (uintptr_t)entrypt, (uintptr_t)arg);
+  sys_call3(SYS_pthread_start, (uintptr_t)startup, (uintptr_t)entrypt,
+            (uintptr_t)arg);
 
   PANIC();
 }
diff --git a/arch/risc-v/src/common/riscv_pthread_start.c 
b/arch/risc-v/src/common/riscv_pthread_start.c
index f66b174..723f7ca 100644
--- a/arch/risc-v/src/common/riscv_pthread_start.c
+++ b/arch/risc-v/src/common/riscv_pthread_start.c
@@ -47,9 +47,10 @@
  *   pthread.
  *
  *   Normally the a user-mode start-up stub will also execute before the
- *   pthread actually starts.  See libc/pthread/pthread_startup.c
+ *   pthread actually starts.  See libc/pthread/pthread_create.c
  *
  * Input Parameters:
+ *   startup - The user-space pthread startup function
  *   entrypt - The user-space address of the pthread entry point
  *   arg     - Standard argument for the pthread entry point
  *
@@ -60,11 +61,13 @@
  *
  ****************************************************************************/
 
-void up_pthread_start(pthread_startroutine_t entrypt, pthread_addr_t arg)
+void up_pthread_start(pthread_trampoline_t startup,
+                      pthread_startroutine_t entrypt, pthread_addr_t arg)
 {
   /* Let sys_call2() do all of the work */
 
-  sys_call2(SYS_pthread_start, (uintptr_t)entrypt, (uintptr_t)arg);
+  sys_call3(SYS_pthread_start, (uintptr_t)startup, (uintptr_t)entrypt,
+            (uintptr_t)arg);
 
   PANIC();
 }
diff --git a/arch/risc-v/src/rv64gc/riscv_swint.c 
b/arch/risc-v/src/rv64gc/riscv_swint.c
index 23de0e7..7e6d90b 100644
--- a/arch/risc-v/src/rv64gc/riscv_swint.c
+++ b/arch/risc-v/src/rv64gc/riscv_swint.c
@@ -294,21 +294,21 @@ int riscv_swint(int irq, FAR void *context, FAR void *arg)
        *   R2 = arg
        */
 
-#if defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_DISABLE_PTHREAD)
+#if !defined(CONFIG_BUILD_FLAT) && !defined(CONFIG_DISABLE_PTHREAD)
       case SYS_pthread_start:
         {
           /* Set up to return to the user-space pthread start-up function in
            * unprivileged mode.
            */
 
-          regs[REG_EPC]      = (uintptr_t)USERSPACE->pthread_startup & ~1;
+          regs[REG_EPC]      = (uintptr_t)regs[REG_A1] & ~1;  /* startup */
 
-          /* Change the parameter ordering to match the expectation of struct
-           * userpace_s pthread_startup:
+          /* Change the parameter ordering to match the expectation of the
+           * user space pthread_startup:
            */
 
-          regs[REG_A0]       = regs[REG_A1];  /* pthread entry */
-          regs[REG_A1]       = regs[REG_A2];  /* arg */
+          regs[REG_A0]       = regs[REG_A2];  /* pthread entry */
+          regs[REG_A1]       = regs[REG_A3];  /* arg */
           regs[REG_INT_CTX] &= ~MSTATUS_MPPM; /* User mode */
         }
         break;
diff --git a/arch/risc-v/src/rv64gc/svcall.h b/arch/risc-v/src/rv64gc/svcall.h
index 690f562..80c8c15 100644
--- a/arch/risc-v/src/rv64gc/svcall.h
+++ b/arch/risc-v/src/rv64gc/svcall.h
@@ -82,6 +82,7 @@
 
 #define SYS_switch_context        (2)
 
+#ifndef CONFIG_BUILD_FLAT
 #ifdef CONFIG_LIB_SYSCALL
 /* SYS call 3:
  *
@@ -98,15 +99,6 @@
  */
 
 #define SYS_task_start            (4)
-
-/* SYS call 5:
- *
- * void up_pthread_start(pthread_startroutine_t entrypt, pthread_addr_t arg)
- *        noreturn_function
- */
-
-#define SYS_pthread_start         (5)
-
 /* SYS call 6:
  *
  * void signal_handler(_sa_sigaction_t sighand, int signo,
@@ -123,6 +115,17 @@
 #define SYS_signal_handler_return (7)
 
 #endif /* CONFIG_BUILD_PROTECTED */
+
+/* SYS call 5:
+ *
+ * void up_pthread_start(pthread_startroutine_t startup,
+ *                       pthread_startroutine_t entrypt, pthread_addr_t arg)
+ *        noreturn_function
+ */
+
+#define SYS_pthread_start         (5)
+
+#endif /* !CONFIG_BUILD_FLAT */
 #endif /* CONFIG_LIB_SYSCALL */
 
 #endif /* __ARCH_RISCV_SRC_RV64GC_SVCALL_H */
diff --git a/boards/arm/imxrt/imxrt1050-evk/kernel/imxrt_userspace.c 
b/boards/arm/imxrt/imxrt1050-evk/kernel/imxrt_userspace.c
index 2654b3b..0f0ee11 100644
--- a/boards/arm/imxrt/imxrt1050-evk/kernel/imxrt_userspace.c
+++ b/boards/arm/imxrt/imxrt1050-evk/kernel/imxrt_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/imxrt/imxrt1060-evk/kernel/imxrt_userspace.c 
b/boards/arm/imxrt/imxrt1060-evk/kernel/imxrt_userspace.c
index 3a706f3..e2557c7 100644
--- a/boards/arm/imxrt/imxrt1060-evk/kernel/imxrt_userspace.c
+++ b/boards/arm/imxrt/imxrt1060-evk/kernel/imxrt_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/lc823450/lc823450-xgevk/kernel/lc823450_userspace.c 
b/boards/arm/lc823450/lc823450-xgevk/kernel/lc823450_userspace.c
index c6ce154..7eedb98 100644
--- a/boards/arm/lc823450/lc823450-xgevk/kernel/lc823450_userspace.c
+++ b/boards/arm/lc823450/lc823450-xgevk/kernel/lc823450_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/lpc17xx_40xx/lpc4088-devkit/kernel/lpc17_40_userspace.c 
b/boards/arm/lpc17xx_40xx/lpc4088-devkit/kernel/lpc17_40_userspace.c
index 4298a0f..2af4c85 100644
--- a/boards/arm/lpc17xx_40xx/lpc4088-devkit/kernel/lpc17_40_userspace.c
+++ b/boards/arm/lpc17xx_40xx/lpc4088-devkit/kernel/lpc17_40_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git 
a/boards/arm/lpc17xx_40xx/lpc4088-quickstart/kernel/lpc17_40_userspace.c 
b/boards/arm/lpc17xx_40xx/lpc4088-quickstart/kernel/lpc17_40_userspace.c
index 2af012c..4f5c33c 100644
--- a/boards/arm/lpc17xx_40xx/lpc4088-quickstart/kernel/lpc17_40_userspace.c
+++ b/boards/arm/lpc17xx_40xx/lpc4088-quickstart/kernel/lpc17_40_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/lpc17xx_40xx/open1788/kernel/lpc17_40_userspace.c 
b/boards/arm/lpc17xx_40xx/open1788/kernel/lpc17_40_userspace.c
index a09ab2d..09450ec 100644
--- a/boards/arm/lpc17xx_40xx/open1788/kernel/lpc17_40_userspace.c
+++ b/boards/arm/lpc17xx_40xx/open1788/kernel/lpc17_40_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/lpc17xx_40xx/pnev5180b/kernel/lpc17_40_userspace.c 
b/boards/arm/lpc17xx_40xx/pnev5180b/kernel/lpc17_40_userspace.c
index 21b4c18..8a8451c 100644
--- a/boards/arm/lpc17xx_40xx/pnev5180b/kernel/lpc17_40_userspace.c
+++ b/boards/arm/lpc17xx_40xx/pnev5180b/kernel/lpc17_40_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/lpc43xx/bambino-200e/kernel/lpc43_userspace.c 
b/boards/arm/lpc43xx/bambino-200e/kernel/lpc43_userspace.c
index 72600e0..e59fd01 100644
--- a/boards/arm/lpc43xx/bambino-200e/kernel/lpc43_userspace.c
+++ b/boards/arm/lpc43xx/bambino-200e/kernel/lpc43_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/sam34/sam3u-ek/kernel/sam_userspace.c 
b/boards/arm/sam34/sam3u-ek/kernel/sam_userspace.c
index 55fa413..da2e8d8 100644
--- a/boards/arm/sam34/sam3u-ek/kernel/sam_userspace.c
+++ b/boards/arm/sam34/sam3u-ek/kernel/sam_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/samv7/same70-xplained/kernel/sam_userspace.c 
b/boards/arm/samv7/same70-xplained/kernel/sam_userspace.c
index e77b252..2cd4003 100644
--- a/boards/arm/samv7/same70-xplained/kernel/sam_userspace.c
+++ b/boards/arm/samv7/same70-xplained/kernel/sam_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/samv7/samv71-xult/kernel/sam_userspace.c 
b/boards/arm/samv7/samv71-xult/kernel/sam_userspace.c
index a63cc7c..01be17d 100644
--- a/boards/arm/samv7/samv71-xult/kernel/sam_userspace.c
+++ b/boards/arm/samv7/samv71-xult/kernel/sam_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32/clicker2-stm32/kernel/stm32_userspace.c 
b/boards/arm/stm32/clicker2-stm32/kernel/stm32_userspace.c
index 791543d..2a2521c 100644
--- a/boards/arm/stm32/clicker2-stm32/kernel/stm32_userspace.c
+++ b/boards/arm/stm32/clicker2-stm32/kernel/stm32_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32/mikroe-stm32f4/kernel/stm32_userspace.c 
b/boards/arm/stm32/mikroe-stm32f4/kernel/stm32_userspace.c
index fb63a4a..25af38e 100644
--- a/boards/arm/stm32/mikroe-stm32f4/kernel/stm32_userspace.c
+++ b/boards/arm/stm32/mikroe-stm32f4/kernel/stm32_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32/olimex-stm32-p407/kernel/stm32_userspace.c 
b/boards/arm/stm32/olimex-stm32-p407/kernel/stm32_userspace.c
index a6d3a3f..efe32d8 100644
--- a/boards/arm/stm32/olimex-stm32-p407/kernel/stm32_userspace.c
+++ b/boards/arm/stm32/olimex-stm32-p407/kernel/stm32_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32/omnibusf4/kernel/stm32_userspace.c 
b/boards/arm/stm32/omnibusf4/kernel/stm32_userspace.c
index a3ffc8d..08c02ae 100644
--- a/boards/arm/stm32/omnibusf4/kernel/stm32_userspace.c
+++ b/boards/arm/stm32/omnibusf4/kernel/stm32_userspace.c
@@ -112,9 +112,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32/stm3240g-eval/kernel/stm32_userspace.c 
b/boards/arm/stm32/stm3240g-eval/kernel/stm32_userspace.c
index 2faeb54..4eafb03 100644
--- a/boards/arm/stm32/stm3240g-eval/kernel/stm32_userspace.c
+++ b/boards/arm/stm32/stm3240g-eval/kernel/stm32_userspace.c
@@ -94,9 +94,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32/stm32f4discovery/kernel/stm32_userspace.c 
b/boards/arm/stm32/stm32f4discovery/kernel/stm32_userspace.c
index 1ec35a3..ff60953 100644
--- a/boards/arm/stm32/stm32f4discovery/kernel/stm32_userspace.c
+++ b/boards/arm/stm32/stm32f4discovery/kernel/stm32_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32f7/stm32f746g-disco/kernel/stm32_userspace.c 
b/boards/arm/stm32f7/stm32f746g-disco/kernel/stm32_userspace.c
index 6061bb2..0ee9fa1 100644
--- a/boards/arm/stm32f7/stm32f746g-disco/kernel/stm32_userspace.c
+++ b/boards/arm/stm32f7/stm32f746g-disco/kernel/stm32_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32f7/stm32f769i-disco/kernel/stm32_userspace.c 
b/boards/arm/stm32f7/stm32f769i-disco/kernel/stm32_userspace.c
index 4a66cd7..f85466e 100644
--- a/boards/arm/stm32f7/stm32f769i-disco/kernel/stm32_userspace.c
+++ b/boards/arm/stm32f7/stm32f769i-disco/kernel/stm32_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32h7/nucleo-h743zi/kernel/stm32_userspace.c 
b/boards/arm/stm32h7/nucleo-h743zi/kernel/stm32_userspace.c
index d696786..54fb39e 100644
--- a/boards/arm/stm32h7/nucleo-h743zi/kernel/stm32_userspace.c
+++ b/boards/arm/stm32h7/nucleo-h743zi/kernel/stm32_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32h7/stm32h747i-disco/kernel/stm32_userspace.c 
b/boards/arm/stm32h7/stm32h747i-disco/kernel/stm32_userspace.c
index 84079d2..4dfafe8 100644
--- a/boards/arm/stm32h7/stm32h747i-disco/kernel/stm32_userspace.c
+++ b/boards/arm/stm32h7/stm32h747i-disco/kernel/stm32_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32l4/stm32l476vg-disco/kernel/stm32l4_userspace.c 
b/boards/arm/stm32l4/stm32l476vg-disco/kernel/stm32l4_userspace.c
index f274947..548d619 100644
--- a/boards/arm/stm32l4/stm32l476vg-disco/kernel/stm32l4_userspace.c
+++ b/boards/arm/stm32l4/stm32l476vg-disco/kernel/stm32l4_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/stm32l4/stm32l4r9ai-disco/kernel/stm32l4_userspace.c 
b/boards/arm/stm32l4/stm32l4r9ai-disco/kernel/stm32l4_userspace.c
index 802c1a3..ae474c6 100644
--- a/boards/arm/stm32l4/stm32l4r9ai-disco/kernel/stm32l4_userspace.c
+++ b/boards/arm/stm32l4/stm32l4r9ai-disco/kernel/stm32l4_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/arm/tiva/lm3s6965-ek/kernel/lm_userspace.c 
b/boards/arm/tiva/lm3s6965-ek/kernel/lm_userspace.c
index 6873dc1..1e6ae7b 100644
--- a/boards/arm/tiva/lm3s6965-ek/kernel/lm_userspace.c
+++ b/boards/arm/tiva/lm3s6965-ek/kernel/lm_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/boards/risc-v/k210/maix-bit/kernel/k210_userspace.c 
b/boards/risc-v/k210/maix-bit/kernel/k210_userspace.c
index 0933520..f7ff8e5 100644
--- a/boards/risc-v/k210/maix-bit/kernel/k210_userspace.c
+++ b/boards/risc-v/k210/maix-bit/kernel/k210_userspace.c
@@ -95,9 +95,6 @@ const struct userspace_s userspace __attribute__ ((section 
(".userspace"))) =
   /* Task/thread startup routines */
 
   .task_startup     = nxtask_startup,
-#ifndef CONFIG_DISABLE_PTHREAD
-  .pthread_startup  = pthread_startup,
-#endif
 
   /* Signal handler trampoline */
 
diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h
index 9bf8dd5..d3ef3d6 100644
--- a/include/nuttx/arch.h
+++ b/include/nuttx/arch.h
@@ -569,9 +569,10 @@ void up_task_start(main_t taskentry, int argc, FAR char 
*argv[])
  *   pthread by calling this function.
  *
  *   Normally the a user-mode start-up stub will also execute before the
- *   pthread actually starts.  See libc/pthread/pthread_startup.c
+ *   pthread actually starts.  See libc/pthread/pthread_create.c
  *
  * Input Parameters:
+ *   startup - The user-space pthread startup function
  *   entrypt - The user-space address of the pthread entry point
  *   arg     - Standard argument for the pthread entry point
  *
@@ -584,7 +585,8 @@ void up_task_start(main_t taskentry, int argc, FAR char 
*argv[])
 
 #if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__) && \
     !defined(CONFIG_DISABLE_PTHREAD)
-void up_pthread_start(pthread_startroutine_t entrypt, pthread_addr_t arg)
+void up_pthread_start(pthread_trampoline_t startup,
+                      pthread_startroutine_t entrypt, pthread_addr_t arg);
        noreturn_function;
 #endif
 
diff --git a/include/nuttx/pthread.h b/include/nuttx/pthread.h
index e92b207..8dc21d9 100644
--- a/include/nuttx/pthread.h
+++ b/include/nuttx/pthread.h
@@ -100,7 +100,7 @@
 #endif
 
 /****************************************************************************
- * Public Data
+ * Public Types
  ****************************************************************************/
 
 #ifdef __cplusplus
@@ -111,6 +111,10 @@ extern "C"
 #define EXTERN extern
 #endif
 
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
 /* Default pthread attributes.  This global can only be shared within the
  * kernel- or within the user- address space.
  */
@@ -121,6 +125,30 @@ EXTERN const pthread_attr_t g_default_pthread_attr;
  * Public Function Prototypes
  ****************************************************************************/
 
+/****************************************************************************
+ * Name:  nx_pthread_create
+ *
+ * Description:
+ *   This function creates and activates a new thread with specified
+ *   attributes.
+ *
+ * Input Parameters:
+ *    trampoline
+ *    thread
+ *    attr
+ *    start_routine
+ *    arg
+ *
+ * Returned Value:
+ *   OK (0) on success; a (non-negated) errno value on failure. The errno
+ *   variable is not set.
+ *
+ ****************************************************************************/
+
+int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
+                      FAR const pthread_attr_t *attr,
+                      pthread_startroutine_t entry, pthread_addr_t arg);
+
 #undef EXTERN
 #ifdef __cplusplus
 }
diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 4f7f647..f40a2cd 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -770,6 +770,7 @@ struct pthread_tcb_s
 
   /* Task Management Fields 
*****************************************************/
 
+  pthread_trampoline_t trampoline;       /* User-space pthread startup 
function */
   pthread_addr_t arg;                    /* Startup argument                   
 */
   FAR void *joininfo;                    /* Detach-able info to support join   
 */
 };
diff --git a/include/nuttx/userspace.h b/include/nuttx/userspace.h
index 23cb033..4c9811a 100644
--- a/include/nuttx/userspace.h
+++ b/include/nuttx/userspace.h
@@ -100,13 +100,9 @@ struct userspace_s
 
   FAR struct mm_heap_s *us_heap;
 
-  /* Task/thread startup routines */
+  /* Task startup routine */
 
   CODE void (*task_startup)(main_t entrypt, int argc, FAR char *argv[]);
-#ifndef CONFIG_DISABLE_PTHREAD
-  CODE void (*pthread_startup)(pthread_startroutine_t entrypt,
-    pthread_addr_t arg);
-#endif
 
   /* Signal handler trampoline */
 
@@ -136,26 +132,6 @@ extern "C"
  * Public Function Prototypes
  ****************************************************************************/
 
-/****************************************************************************
- * Name: pthread_startup
- *
- * Description:
- *   This function is the user-space, pthread startup function.  It is called
- *   from up_pthread_start() in user-mode.
- *
- * Input Parameters:
- *   entrypt - The user-space address of the pthread entry point
- *   arg     - Standard argument for the pthread entry point
- *
- * Returned Value:
- *   None.  This function does not return.
- *
- ****************************************************************************/
-
-#if !defined(__KERNEL__) && !defined(CONFIG_DISABLE_PTHREAD)
-void pthread_startup(pthread_startroutine_t entrypt, pthread_addr_t arg);
-#endif
-
 #undef EXTERN
 #ifdef __cplusplus
 }
diff --git a/include/pthread.h b/include/pthread.h
index 1474f93..fb8a458 100644
--- a/include/pthread.h
+++ b/include/pthread.h
@@ -225,6 +225,8 @@ typedef FAR void *pthread_addr_t;
 typedef CODE pthread_addr_t (*pthread_startroutine_t)(pthread_addr_t);
 typedef pthread_startroutine_t pthread_func_t;
 
+typedef void (*pthread_trampoline_t)(pthread_startroutine_t, pthread_addr_t);
+
 struct pthread_attr_s
 {
   uint8_t priority;            /* Priority of the pthread */
diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h
index ba5c04f..c3f3dea 100644
--- a/include/sys/syscall_lookup.h
+++ b/include/sys/syscall_lookup.h
@@ -305,7 +305,7 @@ SYSCALL_LOOKUP(telldir,                    1)
   SYSCALL_LOOKUP(pthread_cond_broadcast,   1)
   SYSCALL_LOOKUP(pthread_cond_signal,      1)
   SYSCALL_LOOKUP(pthread_cond_wait,        2)
-  SYSCALL_LOOKUP(pthread_create,           4)
+  SYSCALL_LOOKUP(nx_pthread_create,        5)
   SYSCALL_LOOKUP(pthread_detach,           1)
   SYSCALL_LOOKUP(pthread_exit,             1)
   SYSCALL_LOOKUP(pthread_getschedparam,    3)
diff --git a/libs/libc/pthread/Make.defs b/libs/libc/pthread/Make.defs
index 3f62e4e..21497bf 100644
--- a/libs/libc/pthread/Make.defs
+++ b/libs/libc/pthread/Make.defs
@@ -38,19 +38,20 @@ CSRCS += pthread_barrierattr_init.c 
pthread_barrierattr_destroy.c
 CSRCS += pthread_barrierattr_getpshared.c pthread_barrierattr_setpshared.c
 CSRCS += pthread_barrierinit.c pthread_barrierdestroy.c pthread_barrierwait.c
 CSRCS += pthread_condattr_init.c pthread_condattr_destroy.c
+CSRCS += pthread_condattr_setclock.c pthread_condattr_getclock.c
 CSRCS += pthread_condinit.c pthread_conddestroy.c pthread_condtimedwait.c
+CSRCS += pthread_create.c
+CSRCS += pthread_get_stackaddr_np.c pthread_get_stacksize_np.c
 CSRCS += pthread_mutexattr_init.c pthread_mutexattr_destroy.c
 CSRCS += pthread_mutexattr_getpshared.c pthread_mutexattr_setpshared.c
 CSRCS += pthread_mutexattr_setprotocol.c pthread_mutexattr_getprotocol.c
 CSRCS += pthread_mutexattr_settype.c pthread_mutexattr_gettype.c
 CSRCS += pthread_mutexattr_setrobust.c pthread_mutexattr_getrobust.c
 CSRCS += pthread_mutex_lock.c
+CSRCS += pthread_once.c pthread_yield.c
+CSRCS += pthread_rwlock.c pthread_rwlock_rdlock.c pthread_rwlock_wrlock.c
 CSRCS += pthread_setcancelstate.c pthread_setcanceltype.c
 CSRCS += pthread_testcancel.c
-CSRCS += pthread_rwlock.c pthread_rwlock_rdlock.c pthread_rwlock_wrlock.c
-CSRCS += pthread_once.c pthread_yield.c
-CSRCS += pthread_get_stackaddr_np.c pthread_get_stacksize_np.c
-CSRCS += pthread_condattr_setclock.c pthread_condattr_getclock.c
 
 ifeq ($(CONFIG_SMP),y)
 CSRCS += pthread_attr_getaffinity.c pthread_attr_setaffinity.c
@@ -60,14 +61,9 @@ ifeq ($(CONFIG_PTHREAD_SPINLOCKS),y)
 CSRCS += pthread_spinlock.c
 endif
 
-ifeq ($(CONFIG_BUILD_PROTECTED),y)
-CSRCS += pthread_startup.c
-endif
-
 endif # CONFIG_DISABLE_PTHREAD
 
 # Add the pthread directory to the build
 
 DEPPATH += --dep-path pthread
 VPATH += :pthread
-
diff --git a/libs/libc/pthread/pthread_startup.c 
b/libs/libc/pthread/pthread_create.c
similarity index 57%
rename from libs/libc/pthread/pthread_startup.c
rename to libs/libc/pthread/pthread_create.c
index 53be032..e81c63e 100644
--- a/libs/libc/pthread/pthread_startup.c
+++ b/libs/libc/pthread/pthread_create.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * libs/libc/pthread/pthread_startup.c
+ * libs/libc/pthread/pthread_create.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -24,70 +24,68 @@
 
 #include <nuttx/config.h>
 
-#include <pthread.h>
-#include <assert.h>
+#include <debug.h>
 
-#include <nuttx/userspace.h>
-
-#if !defined(CONFIG_BUILD_FLAT) && !defined(__KERNEL__)
-
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
+#include <nuttx/pthread.h>
 
 /****************************************************************************
- * Private Type Declarations
+ * Private Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Public Data
+ * Name: pthread_startup
+ *
+ * Description:
+ *   This function is the user space pthread startup function.  Its purpose
+ *   is to catch the return from the pthread main function so that
+ *   pthread_exit() can be called from user space
+ *
+ * Input Parameters:
+ *   entry - The user-space address of the pthread entry point
+ *   arg   - Standard argument for the pthread entry point
+ *
+ * Returned Value:
+ *   None.  This function does not return.
+ *
  ****************************************************************************/
 
-/****************************************************************************
- * Private Data
- ****************************************************************************/
+static void pthread_startup(pthread_startroutine_t entry,
+                            pthread_addr_t arg)
+{
+  DEBUGASSERT(entry != NULL);
 
-/****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
+  /* Pass control to the thread entry point.  Handle any returned value. */
 
-/****************************************************************************
- * Private Functions
- ****************************************************************************/
+  pthread_exit(entry(arg));
+}
 
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Name: pthread_startup
+ * Name:  pthread_create
  *
  * Description:
- *   This function is the user-space, pthread startup function.  It is called
- *   from up_pthread_start() in user-mode.
+ *   This function creates and activates a new thread with specified
+ *   attributes.  It is simply a wrapper around the nx_pthread_create system
+ *   call.
  *
  * Input Parameters:
- *   entrypt - The user-space address of the pthread entry point
- *   arg     - Standard argument for the pthread entry point
+ *    thread
+ *    attr
+ *    pthread_entry
+ *    arg
  *
  * Returned Value:
- *   None.  This function does not return.
+ *   OK (0) on success; a (non-negated) errno value on failure. The errno
+ *   variable is not set.
  *
  ****************************************************************************/
 
-void pthread_startup(pthread_startroutine_t entrypt, pthread_addr_t arg)
+int pthread_create(FAR pthread_t *thread, FAR const pthread_attr_t *attr,
+                   pthread_startroutine_t pthread_entry, pthread_addr_t arg)
 {
-  pthread_addr_t exit_status;
-
-  DEBUGASSERT(entrypt);
-
-  /* Pass control to the thread entry point. */
-
-  exit_status = entrypt(arg);
-
-  /* The pthread has returned */
-
-  pthread_exit(exit_status);
+  return nx_pthread_create(pthread_startup, thread, attr, pthread_entry,
+                           arg);
 }
-
-#endif /* !CONFIG_BUILD_FLAT && !__KERNEL__ */
diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c
index 1c4af3b..22cd008 100644
--- a/sched/pthread/pthread_create.c
+++ b/sched/pthread/pthread_create.c
@@ -64,19 +64,20 @@ const pthread_attr_t g_default_pthread_attr = 
PTHREAD_ATTR_INITIALIZER;
  ****************************************************************************/
 
 /****************************************************************************
- * Name: pthread_argsetup
+ * Name: pthread_tcb_setup
  *
  * Description:
- *   This functions sets up parameters in the Task Control Block (TCB) in
+ *   This function sets up parameters in the Task Control Block (TCB) in
  *   preparation for starting a new thread.
  *
- *   pthread_argsetup() is called from nxtask_init() and nxtask_start() to
+ *   pthread_tcb_setup() is called from nxtask_init() and nxtask_start() to
  *   create a new task (with arguments cloned via strdup) or pthread_create()
  *   which has one argument passed by value (distinguished by the pthread
  *   boolean argument).
  *
  * Input Parameters:
  *   tcb        - Address of the new task's TCB
+ *   trampoline - User space pthread startup function
  *   arg        - The argument to provide to the pthread on startup.
  *
  * Returned Value:
@@ -84,21 +85,23 @@ const pthread_attr_t g_default_pthread_attr = 
PTHREAD_ATTR_INITIALIZER;
  *
  ****************************************************************************/
 
-static inline void pthread_argsetup(FAR struct pthread_tcb_s *tcb,
-                                    pthread_addr_t arg)
+static inline void pthread_tcb_setup(FAR struct pthread_tcb_s *ptcb,
+                                     pthread_trampoline_t trampoline,
+                                     pthread_addr_t arg)
 {
 #if CONFIG_TASK_NAME_SIZE > 0
   /* Copy the pthread name into the TCB */
 
-  snprintf(tcb->cmn.name, CONFIG_TASK_NAME_SIZE,
-           "pt-%p", tcb->cmn.entry.pthread);
+  snprintf(ptcb->cmn.name, CONFIG_TASK_NAME_SIZE,
+           "pt-%p", ptcb->cmn.entry.pthread);
 #endif /* CONFIG_TASK_NAME_SIZE */
 
   /* For pthreads, args are strictly pass-by-value; that actual
    * type wrapped by pthread_addr_t is unknown.
    */
 
-  tcb->arg = arg;
+  ptcb->trampoline = trampoline;
+  ptcb->arg        = arg;
 }
 
 /****************************************************************************
@@ -150,9 +153,8 @@ static void pthread_start(void)
   FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)this_task();
   FAR struct task_group_s *group = ptcb->cmn.group;
   FAR struct join_s *pjoin = (FAR struct join_s *)ptcb->joininfo;
-  pthread_addr_t exit_status;
 
-  DEBUGASSERT(group && pjoin);
+  DEBUGASSERT(group != NULL && pjoin != NULL);
 
   /* Successfully spawned, add the pjoin to our data set. */
 
@@ -180,16 +182,18 @@ static void pthread_start(void)
    * to switch to user-mode before calling into the pthread.
    */
 
+  DEBUGASSERT(ptcb->trampoline != NULL && ptcb->cmn.entry.pthread != NULL);
+
 #ifdef CONFIG_BUILD_FLAT
-  exit_status = (*ptcb->cmn.entry.pthread)(ptcb->arg);
+  ptcb->trampoline(ptcb->cmn.entry.pthread, ptcb->arg);
 #else
-  up_pthread_start(ptcb->cmn.entry.pthread, ptcb->arg);
-  exit_status = NULL;
+  up_pthread_start(ptcb->trampoline, ptcb->cmn.entry.pthread, ptcb->arg);
 #endif
 
-  /* The thread has returned (should never happen in the kernel mode case) */
+  /* The thread has returned (should never happen) */
 
-  pthread_exit(exit_status);
+  DEBUGPANIC();
+  pthread_exit(NULL);
 }
 
 /****************************************************************************
@@ -197,13 +201,14 @@ static void pthread_start(void)
  ****************************************************************************/
 
 /****************************************************************************
- * Name:  pthread_create
+ * Name:  nx_pthread_create
  *
  * Description:
- *   This function creates and activates a new thread with a specified
+ *   This function creates and activates a new thread with specified
  *   attributes.
  *
  * Input Parameters:
+ *    trampoline
  *    thread
  *    attr
  *    start_routine
@@ -215,8 +220,9 @@ static void pthread_start(void)
  *
  ****************************************************************************/
 
-int pthread_create(FAR pthread_t *thread, FAR const pthread_attr_t *attr,
-                   pthread_startroutine_t start_routine, pthread_addr_t arg)
+int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
+                      FAR const pthread_attr_t *attr,
+                      pthread_startroutine_t entry, pthread_addr_t arg)
 {
   FAR struct pthread_tcb_s *ptcb;
   FAR struct tls_info_s *info;
@@ -228,6 +234,8 @@ int pthread_create(FAR pthread_t *thread, FAR const 
pthread_attr_t *attr,
   int ret;
   bool group_joined = false;
 
+  DEBUGASSERT(trampoline != NULL);
+
   /* If attributes were not supplied, use the default attributes */
 
   if (!attr)
@@ -415,7 +423,7 @@ int pthread_create(FAR pthread_t *thread, FAR const 
pthread_attr_t *attr,
   /* Initialize the task control block */
 
   ret = pthread_setup_scheduler(ptcb, param.sched_priority, pthread_start,
-                                start_routine);
+                                entry);
   if (ret != OK)
     {
       errcode = EBUSY;
@@ -440,7 +448,7 @@ int pthread_create(FAR pthread_t *thread, FAR const 
pthread_attr_t *attr,
    * passed by value
    */
 
-  pthread_argsetup(ptcb, arg);
+  pthread_tcb_setup(ptcb, trampoline, arg);
 
   /* Join the parent's task group */
 
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index c8b6807..a9c4658 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -66,6 +66,7 @@
 "munmap","sys/mman.h","defined(CONFIG_FS_RAMMAP)","int","FAR void *","size_t"
 "nx_mkfifo","nuttx/fs/fs.h","defined(CONFIG_PIPES) && CONFIG_DEV_FIFO_SIZE > 
0","int","FAR const char *","mode_t","size_t"
 "nx_pipe","nuttx/fs/fs.h","defined(CONFIG_PIPES) && CONFIG_DEV_PIPE_SIZE > 
0","int","int [2]|FAR int *","size_t","int"
+"nx_pthread_create","nuttx/pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_trampoline_t","FAR
 pthread_t *","FAR const pthread_attr_t 
*","pthread_startroutine_t","pthread_addr_t"
 "nx_task_spawn","nuttx/spawn.h","defined(CONFIG_LIB_SYSCALL) && 
!defined(CONFIG_BUILD_KERNEL)","int","FAR const struct spawn_syscall_parms_s *"
 "nx_vsyslog","nuttx/syslog/syslog.h","","int","int","FAR const IPTR char 
*","FAR va_list *"
 "nxsched_get_stackinfo","nuttx/sched.h","","int","pid_t","FAR struct 
stackinfo_s *"
@@ -88,7 +89,6 @@
 
"pthread_cond_clockwait","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR
 pthread_cond_t *","FAR pthread_mutex_t *","clockid_t","FAR const struct 
timespec *"
 
"pthread_cond_signal","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR 
pthread_cond_t *"
 "pthread_cond_wait","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR 
pthread_cond_t *","FAR pthread_mutex_t *"
-"pthread_create","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR 
pthread_t *","FAR const pthread_attr_t 
*","pthread_startroutine_t","pthread_addr_t"
 
"pthread_detach","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t"
 
"pthread_exit","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","noreturn","pthread_addr_t"
 "pthread_getaffinity_np","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && 
defined(CONFIG_SMP)","int","pthread_t","size_t","FAR cpu_set_t*"

Reply via email to