You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ma...@apache.org on 2022/10/31 03:47:06 UTC

[incubator-nuttx] 03/03: sched/spawn: Support task_spawnattr_[set|get]stackaddr

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

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

commit 64e7833cbc95500c91432a1e9cde0d2847104f96
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Thu Oct 20 03:27:21 2022 +0800

    sched/spawn: Support task_spawnattr_[set|get]stackaddr
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 Documentation/components/binfmt.rst |  3 +++
 binfmt/binfmt_dumpmodule.c          |  3 +++
 binfmt/binfmt_exec.c                |  7 +++++++
 binfmt/binfmt_execmodule.c          |  9 +++++++--
 drivers/serial/serial.c             |  2 +-
 include/nuttx/binfmt/binfmt.h       |  4 ++++
 include/nuttx/kthread.h             |  4 ++--
 include/nuttx/sched.h               |  4 ++--
 include/sched.h                     |  3 +++
 libs/libc/spawn/lib_psa_init.c      |  4 ++++
 sched/init/nx_bringup.c             |  4 ++--
 sched/sched/sched.h                 |  2 +-
 sched/task/task_create.c            | 37 ++++++++++++++++++++++++-------------
 sched/task/task_spawn.c             |  7 +++++--
 14 files changed, 68 insertions(+), 25 deletions(-)

diff --git a/Documentation/components/binfmt.rst b/Documentation/components/binfmt.rst
index 53426c58dd..8ff5173fc0 100644
--- a/Documentation/components/binfmt.rst
+++ b/Documentation/components/binfmt.rst
@@ -134,6 +134,9 @@ pointer to a write-able instance of :c:struct:`binfmt_s`.
 
       uint8_t priority;                    /* Task execution priority */
       size_t stacksize;                    /* Size of the stack in bytes (unallocated) */
+    #ifndef CONFIG_BUILD_KERNEL
+      FAR void *stackaddr;                 /* Task stack address */
+    #endif
     };
 
   Where the types ``binfmt_ctor_t`` and ``binfmt_dtor_t`` define the type
diff --git a/binfmt/binfmt_dumpmodule.c b/binfmt/binfmt_dumpmodule.c
index ff67da522a..b4ddf0a886 100644
--- a/binfmt/binfmt_dumpmodule.c
+++ b/binfmt/binfmt_dumpmodule.c
@@ -70,6 +70,9 @@ int binfmt_dumpmodule(FAR const struct binary_s *bin)
       binfo("  addrenv:   %p\n", bin->addrenv);
 #endif
       binfo("  stacksize: %zd\n", bin->stacksize);
+#ifndef CONFIG_BUILD_KERNEL
+      binfo("  stackaddr: %p\n", bin->stackaddr);
+#endif
       binfo("  unload:    %p\n", bin->unload);
     }
 
diff --git a/binfmt/binfmt_exec.c b/binfmt/binfmt_exec.c
index dd51d7e53a..ab361d5887 100644
--- a/binfmt/binfmt_exec.c
+++ b/binfmt/binfmt_exec.c
@@ -110,6 +110,13 @@ int exec_spawn(FAR const char *filename, FAR char * const *argv,
         {
           bin->stacksize = attr->stacksize;
         }
+
+#ifndef CONFIG_BUILD_KERNEL
+      if (attr->stackaddr != NULL)
+        {
+          bin->stackaddr = attr->stackaddr;
+        }
+#endif
     }
 
   /* Disable pre-emption so that the executed module does
diff --git a/binfmt/binfmt_execmodule.c b/binfmt/binfmt_execmodule.c
index e6347240b9..ed4a7046f6 100644
--- a/binfmt/binfmt_execmodule.c
+++ b/binfmt/binfmt_execmodule.c
@@ -120,6 +120,7 @@ int exec_module(FAR const struct binary_s *binp,
   save_addrenv_t oldenv;
   FAR void *vheap;
 #endif
+  FAR void *stackaddr = NULL;
   pid_t pid;
   int ret;
 
@@ -189,14 +190,18 @@ int exec_module(FAR const struct binary_s *binp,
 
   /* Initialize the task */
 
+#ifndef CONFIG_BUILD_KERNEL
+  stackaddr = binp->stackaddr;
+#endif
+
   if (argv && argv[0])
     {
-      ret = nxtask_init(tcb, argv[0], binp->priority, NULL,
+      ret = nxtask_init(tcb, argv[0], binp->priority, stackaddr,
                         binp->stacksize, binp->entrypt, &argv[1], envp);
     }
   else
     {
-      ret = nxtask_init(tcb, filename, binp->priority, NULL,
+      ret = nxtask_init(tcb, filename, binp->priority, stackaddr,
                         binp->stacksize, binp->entrypt, argv, envp);
     }
 
diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c
index dc9d6564a9..192b4d5b11 100644
--- a/drivers/serial/serial.c
+++ b/drivers/serial/serial.c
@@ -1603,7 +1603,7 @@ static void uart_launch_worker(void *arg)
 #ifdef CONFIG_TTY_LAUNCH_ENTRY
       nxtask_create(CONFIG_TTY_LAUNCH_ENTRYNAME,
                     CONFIG_TTY_LAUNCH_PRIORITY,
-                    CONFIG_TTY_LAUNCH_STACKSIZE,
+                    NULL, CONFIG_TTY_LAUNCH_STACKSIZE,
                     CONFIG_TTY_LAUNCH_ENTRYPOINT,
                     argv, NULL);
 #else
diff --git a/include/nuttx/binfmt/binfmt.h b/include/nuttx/binfmt/binfmt.h
index e730551628..d014d2dacb 100644
--- a/include/nuttx/binfmt/binfmt.h
+++ b/include/nuttx/binfmt/binfmt.h
@@ -98,6 +98,10 @@ struct binary_s
   uint8_t priority;                    /* Task execution priority */
   size_t stacksize;                    /* Size of the stack in bytes (unallocated) */
 
+#ifndef CONFIG_BUILD_KERNEL
+  FAR void *stackaddr;                 /* Task stack address */
+#endif
+
   /* Unload module callback */
 
   CODE int (*unload)(FAR struct binary_s *bin);
diff --git a/include/nuttx/kthread.h b/include/nuttx/kthread.h
index 6b71f41908..6b626376ac 100644
--- a/include/nuttx/kthread.h
+++ b/include/nuttx/kthread.h
@@ -60,7 +60,7 @@ extern "C"
  * Input Parameters:
  *   name       - Name of the new task
  *   priority   - Priority of the new task
- *   stack_ptr  - Stack buffer of the new task
+ *   stack_addr - Stack buffer of the new task
  *   stack_size - Stack size of the new task
  *   entry      - Entry point of a new task
  *   arg        - A pointer to an array of input parameters.  The array
@@ -75,7 +75,7 @@ extern "C"
  ****************************************************************************/
 
 int kthread_create_with_stack(FAR const char *name, int priority,
-                              FAR void *stack_ptr, int stack_size,
+                              FAR void *stack_addr, int stack_size,
                               main_t entry, FAR char * const argv[]);
 
 /****************************************************************************
diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index a9148eb3b4..8fe7e5d1d5 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -947,8 +947,8 @@ void nxtask_uninit(FAR struct task_tcb_s *tcb);
  *
  ****************************************************************************/
 
-int nxtask_create(FAR const char *name,
-                  int priority, int stack_size, main_t entry,
+int nxtask_create(FAR const char *name, int priority,
+                  FAR void *stack_addr, int stack_size, main_t entry,
                   FAR char * const argv[], FAR char * const envp[]);
 
 /****************************************************************************
diff --git a/include/sched.h b/include/sched.h
index e8fc0ed48e..480a8728d0 100644
--- a/include/sched.h
+++ b/include/sched.h
@@ -221,6 +221,9 @@ extern "C"
 #ifndef CONFIG_BUILD_KERNEL
 int    task_create(FAR const char *name, int priority, int stack_size,
                    main_t entry, FAR char * const argv[]);
+int    task_create_with_stack(FAR const char *name, int priority,
+                              FAR void *stack_addr, int stack_size,
+                              main_t entry, FAR char * const argv[]);
 #endif
 int    task_delete(pid_t pid);
 int    task_restart(pid_t pid);
diff --git a/libs/libc/spawn/lib_psa_init.c b/libs/libc/spawn/lib_psa_init.c
index c9fb53a5ce..68d31f8561 100644
--- a/libs/libc/spawn/lib_psa_init.c
+++ b/libs/libc/spawn/lib_psa_init.c
@@ -102,5 +102,9 @@ int posix_spawnattr_init(posix_spawnattr_t *attr)
 
   attr->stacksize           = CONFIG_TASK_SPAWN_DEFAULT_STACKSIZE;
 
+#ifndef CONFIG_BUILD_KERNEL
+  attr->stackaddr           = NULL;
+#endif
+
   return OK;
 }
diff --git a/sched/init/nx_bringup.c b/sched/init/nx_bringup.c
index 639ad91b5c..dbf6df0e0b 100644
--- a/sched/init/nx_bringup.c
+++ b/sched/init/nx_bringup.c
@@ -262,11 +262,11 @@ static inline void nx_start_application(void)
 #  ifdef CONFIG_BUILD_PROTECTED
   DEBUGASSERT(USERSPACE->us_entrypoint != NULL);
   ret = nxtask_create(CONFIG_INIT_ENTRYNAME, CONFIG_INIT_PRIORITY,
-                      CONFIG_INIT_STACKSIZE,
+                      NULL, CONFIG_INIT_STACKSIZE,
                       USERSPACE->us_entrypoint, argv, NULL);
 #  else
   ret = nxtask_create(CONFIG_INIT_ENTRYNAME, CONFIG_INIT_PRIORITY,
-                      CONFIG_INIT_STACKSIZE,
+                      NULL, CONFIG_INIT_STACKSIZE,
                       CONFIG_INIT_ENTRYPOINT, argv, NULL);
 #  endif
   DEBUGASSERT(ret > 0);
diff --git a/sched/sched/sched.h b/sched/sched/sched.h
index 37b7c51e5c..3a966b47d8 100644
--- a/sched/sched/sched.h
+++ b/sched/sched/sched.h
@@ -298,7 +298,7 @@ extern volatile spinlock_t g_cpu_tasklistlock;
  ****************************************************************************/
 
 int nxthread_create(FAR const char *name, uint8_t ttype, int priority,
-                    FAR void *stack_ptr, int stack_size, main_t entry,
+                    FAR void *stack_addr, int stack_size, main_t entry,
                     FAR char * const argv[], FAR char * const envp[]);
 
 /* Task list manipulation functions */
diff --git a/sched/task/task_create.c b/sched/task/task_create.c
index 0442a17355..0118abe6c2 100644
--- a/sched/task/task_create.c
+++ b/sched/task/task_create.c
@@ -55,7 +55,8 @@
  *   name       - Name of the new task
  *   ttype      - Type of the new task
  *   priority   - Priority of the new task
- *   stack_size - size (in bytes) of the stack needed
+ *   stack_addr - Address of the stack needed
+ *   stack_size - Size (in bytes) of the stack needed
  *   entry      - Entry point of a new task
  *   arg        - A pointer to an array of input parameters.  The array
  *                should be terminated with a NULL argv[] value. If no
@@ -71,7 +72,7 @@
  ****************************************************************************/
 
 int nxthread_create(FAR const char *name, uint8_t ttype, int priority,
-                    FAR void *stack_ptr, int stack_size, main_t entry,
+                    FAR void *stack_addr, int stack_size, main_t entry,
                     FAR char * const argv[], FAR char * const envp[])
 {
   FAR struct task_tcb_s *tcb;
@@ -93,7 +94,7 @@ int nxthread_create(FAR const char *name, uint8_t ttype, int priority,
 
   /* Initialize the task */
 
-  ret = nxtask_init(tcb, name, priority, stack_ptr, stack_size,
+  ret = nxtask_init(tcb, name, priority, stack_addr, stack_size,
                     entry, argv, envp);
   if (ret < OK)
     {
@@ -137,7 +138,8 @@ int nxthread_create(FAR const char *name, uint8_t ttype, int priority,
  * Input Parameters:
  *   name       - Name of the new task
  *   priority   - Priority of the new task
- *   stack_size - size (in bytes) of the stack needed
+ *   stack_addr - Address of the stack needed
+ *   stack_size - Size (in bytes) of the stack needed
  *   entry      - Entry point of a new task
  *   arg        - A pointer to an array of input parameters.  The array
  *                should be terminated with a NULL argv[] value. If no
@@ -152,11 +154,11 @@ int nxthread_create(FAR const char *name, uint8_t ttype, int priority,
  *
  ****************************************************************************/
 
-int nxtask_create(FAR const char *name,
-                  int priority, int stack_size, main_t entry,
+int nxtask_create(FAR const char *name, int priority,
+                  FAR void *stack_addr, int stack_size, main_t entry,
                   FAR char * const argv[], FAR char * const envp[])
 {
-  return nxthread_create(name, TCB_FLAG_TTYPE_TASK, priority, NULL,
+  return nxthread_create(name, TCB_FLAG_TTYPE_TASK, priority, stack_addr,
                          stack_size, entry, argv, envp ? envp : environ);
 }
 
@@ -193,10 +195,12 @@ int nxtask_create(FAR const char *name,
  ****************************************************************************/
 
 #ifndef CONFIG_BUILD_KERNEL
-int task_create(FAR const char *name, int priority,
-                int stack_size, main_t entry, FAR char * const argv[])
+int task_create_with_stack(FAR const char *name, int priority,
+                           FAR void *stack_addr, int stack_size,
+                           main_t entry, FAR char * const argv[])
 {
-  int ret = nxtask_create(name, priority, stack_size, entry, argv, NULL);
+  int ret = nxtask_create(name, priority, stack_addr,
+                          stack_size, entry, argv, NULL);
   if (ret < 0)
     {
       set_errno(-ret);
@@ -205,6 +209,13 @@ int task_create(FAR const char *name, int priority,
 
   return ret;
 }
+
+int task_create(FAR const char *name, int priority,
+                int stack_size, main_t entry, FAR char * const argv[])
+{
+  return task_create_with_stack(name, priority, NULL,
+                                stack_size, entry, argv);
+}
 #endif
 
 /****************************************************************************
@@ -218,7 +229,7 @@ int task_create(FAR const char *name, int priority,
  * Input Parameters:
  *   name       - Name of the new task
  *   priority   - Priority of the new task
- *   stack_ptr  - Stack buffer of the new task
+ *   stack_addr - Stack buffer of the new task
  *   stack_size - Stack size of the new task
  *   entry      - Entry point of a new task
  *   arg        - A pointer to an array of input parameters.  The array
@@ -233,11 +244,11 @@ int task_create(FAR const char *name, int priority,
  ****************************************************************************/
 
 int kthread_create_with_stack(FAR const char *name, int priority,
-                              FAR void *stack_ptr, int stack_size,
+                              FAR void *stack_addr, int stack_size,
                               main_t entry, FAR char * const argv[])
 {
   return nxthread_create(name, TCB_FLAG_TTYPE_KERNEL, priority,
-                         stack_ptr, stack_size, entry, argv, NULL);
+                         stack_addr, stack_size, entry, argv, NULL);
 }
 
 /****************************************************************************
diff --git a/sched/task/task_spawn.c b/sched/task/task_spawn.c
index 1dff3c758e..c30ff598b4 100644
--- a/sched/task/task_spawn.c
+++ b/sched/task/task_spawn.c
@@ -92,6 +92,7 @@ static int nxtask_spawn_exec(FAR pid_t *pidp, FAR const char *name,
                              main_t entry, FAR const posix_spawnattr_t *attr,
                              FAR char * const *argv, FAR char * const envp[])
 {
+  FAR void *stackaddr = NULL;
   size_t stacksize;
   int priority;
   int pid;
@@ -110,6 +111,7 @@ static int nxtask_spawn_exec(FAR pid_t *pidp, FAR const char *name,
     {
       priority  = attr->priority;
       stacksize = attr->stacksize;
+      stackaddr = attr->stackaddr;
     }
   else
     {
@@ -129,7 +131,8 @@ static int nxtask_spawn_exec(FAR pid_t *pidp, FAR const char *name,
 
   /* Start the task */
 
-  pid = nxtask_create(name, priority, stacksize, entry, argv, envp);
+  pid = nxtask_create(name, priority, stackaddr,
+                      stacksize, entry, argv, envp);
   if (pid < 0)
     {
       ret = pid;
@@ -405,7 +408,7 @@ int task_spawn(FAR const char *name, main_t entry,
    */
 
   proxy = nxtask_create("nxtask_spawn_proxy", param.sched_priority,
-                        CONFIG_POSIX_SPAWN_PROXY_STACKSIZE,
+                        NULL, CONFIG_POSIX_SPAWN_PROXY_STACKSIZE,
                         nxtask_spawn_proxy, NULL, NULL);
   if (proxy < 0)
     {