diff --git a/components/lwp/arch/arm/cortex-m3/lwp_rvds.S b/components/lwp/arch/arm/cortex-m3/lwp_rvds.S index ff79cfda4bece59cff3fcf30853b74c7372d4db0..49470ea03aa22b973345849f723b5b08b44a3118 100644 --- a/components/lwp/arch/arm/cortex-m3/lwp_rvds.S +++ b/components/lwp/arch/arm/cortex-m3/lwp_rvds.S @@ -34,33 +34,33 @@ IMPORT lwp_set_kernel_sp ;/* -; * void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr); +; * void lwp_user_entry(args, text, data); ; */ lwp_user_entry PROC EXPORT lwp_user_entry - PUSH {R0-R1} ; push text&data addr. + PUSH {R0-R3} ; push text&data addr. MOV R0, SP ; v1 = SP BL lwp_set_kernel_sp ; lwp_set_kernel_sp(v1) - POP {R0-R1} ; pop app address to R1. - ; set CPU to user-thread mode. MRS R2, CONTROL ORR R2, R2, #0x03 ; use PSP, user-thread mode. MSR CONTROL, R2 + POP {R0-R3} ; pop app address to R1. ; set data address. - MOV R9, R1 + MOV R9, R2 ; run app, only Thumb-mode. - ORR R0, R0, #0x01 - BX R0 + ORR R1, R1, #0x01 + BX R1 ; never reach here! ENDP + ;/* ; * void SVC_Handler(void); ; */ diff --git a/components/lwp/arch/arm/cortex-m4/lwp_rvds.S b/components/lwp/arch/arm/cortex-m4/lwp_rvds.S index 16e81710c11b246d2fd0b95d9e52e65d22ef9d50..42fad954ce000cd15544959e32290587fc6f6856 100644 --- a/components/lwp/arch/arm/cortex-m4/lwp_rvds.S +++ b/components/lwp/arch/arm/cortex-m4/lwp_rvds.S @@ -34,33 +34,33 @@ IMPORT lwp_set_kernel_sp ;/* -; * void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr); +; * void lwp_user_entry(args, text, data); ; */ lwp_user_entry PROC EXPORT lwp_user_entry - PUSH {R0-R1} ; push text&data addr. + PUSH {R0-R3} ; push text&data addr. MOV R0, SP ; v1 = SP BL lwp_set_kernel_sp ; lwp_set_kernel_sp(v1) - POP {R0-R1} ; pop app address to R1. - ; set CPU to user-thread mode. MRS R2, CONTROL ORR R2, R2, #0x03 ; use PSP, user-thread mode. MSR CONTROL, R2 + POP {R0-R3} ; pop app address to R1. ; set data address. - MOV R9, R1 + MOV R9, R2 ; run app, only Thumb-mode. - ORR R0, R0, #0x01 - BX R0 + ORR R1, R1, #0x01 + BX R1 ; never reach here! ENDP + ;/* ; * void SVC_Handler(void); ; */ diff --git a/components/lwp/arch/arm/cortex-m7/lwp_rvds.S b/components/lwp/arch/arm/cortex-m7/lwp_rvds.S index 16e81710c11b246d2fd0b95d9e52e65d22ef9d50..6c310a3632207dd0aa684b81da1a765668289514 100644 --- a/components/lwp/arch/arm/cortex-m7/lwp_rvds.S +++ b/components/lwp/arch/arm/cortex-m7/lwp_rvds.S @@ -34,29 +34,28 @@ IMPORT lwp_set_kernel_sp ;/* -; * void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr); +; * void lwp_user_entry(args, text, data); ; */ lwp_user_entry PROC EXPORT lwp_user_entry - PUSH {R0-R1} ; push text&data addr. + PUSH {R0-R3} ; push text&data addr. MOV R0, SP ; v1 = SP BL lwp_set_kernel_sp ; lwp_set_kernel_sp(v1) - POP {R0-R1} ; pop app address to R1. - ; set CPU to user-thread mode. MRS R2, CONTROL ORR R2, R2, #0x03 ; use PSP, user-thread mode. MSR CONTROL, R2 + POP {R0-R3} ; pop app address to R1. ; set data address. - MOV R9, R1 + MOV R9, R2 ; run app, only Thumb-mode. - ORR R0, R0, #0x01 - BX R0 + ORR R1, R1, #0x01 + BX R1 ; never reach here! ENDP diff --git a/components/lwp/lwp.c b/components/lwp/lwp.c index 13e2a8f2efb4fb67da63d8c06cf8b0b8877c3da2..7157f2725022626570ec717384fc7d274d0ecb67 100644 --- a/components/lwp/lwp.c +++ b/components/lwp/lwp.c @@ -25,10 +25,6 @@ #include #include -#ifdef RT_USING_FINSH - #include -#endif - #ifndef RT_USING_DFS #error "lwp need file system(RT_USING_DFS)" #endif @@ -42,7 +38,7 @@ #include extern rt_thread_t rt_current_thread; -extern void lwp_user_entry(const void *text, void *data); +extern void lwp_user_entry(void *args, const void *text, void *data); /** * RT-Thread light-weight process @@ -62,6 +58,43 @@ uint32_t *lwp_get_kernel_sp(void) return user_data->kernel_sp; } +static int lwp_argscopy(struct rt_lwp *lwp, int argc, char **argv) +{ + int size = sizeof(int)*3; /* store argc, argv, NULL */ + int *args; + char *str; + char **new_argv; + int i; + int len; + + for (i = 0; i < argc; i ++) + { + size += (rt_strlen(argv[i]) + 1); + } + size += (sizeof(int) * argc); + + args = (int*)rt_malloc(size); + if (args == RT_NULL) + return -1; + + str = (char*)((int)args + (argc + 3) * sizeof(int)); + new_argv = (char**)&args[2]; + args[0] = argc; + args[1] = (int)new_argv; + + for (i = 0; i < argc; i ++) + { + len = rt_strlen(argv[i]) + 1; + new_argv[i] = str; + rt_memcpy(str, argv[i], len); + str += len; + } + new_argv[i] = 0; + lwp->args = args; + + return 0; +} + static int lwp_load(const char *filename, struct rt_lwp *lwp, uint8_t *load_addr, size_t addr_size) { int fd; @@ -76,8 +109,6 @@ static int lwp_load(const char *filename, struct rt_lwp *lwp, uint8_t *load_addr /* check lwp control block */ RT_ASSERT(lwp != RT_NULL); - memset(lwp, 0x00, sizeof(struct rt_lwp)); - if (load_addr != RT_NULL) { lwp->lwp_type = LWP_TYPE_FIX_ADDR; @@ -279,6 +310,15 @@ static void lwp_cleanup(struct rt_thread *tid) dbg_log(DBG_LOG, "lwp free memory pages\n"); rt_lwp_mem_deinit(lwp); + /* cleanup fd table */ + while (lwp->fdt.maxfd > 0) + { + lwp->fdt.maxfd --; + close(lwp->fdt.maxfd); + } + rt_free(lwp->fdt.fds); + rt_free(lwp->args); + dbg_log(DBG_LOG, "lwp free: %p\n", lwp); rt_free(lwp); @@ -299,7 +339,7 @@ static void lwp_thread(void *parameter) tid->user_data = (rt_uint32_t)lwp; tid->cleanup = lwp_cleanup; - lwp_user_entry(lwp->text_entry, lwp->data); + lwp_user_entry(lwp->args, lwp->text_entry, lwp->data); } struct rt_lwp *rt_lwp_self(void) @@ -307,7 +347,7 @@ struct rt_lwp *rt_lwp_self(void) return (struct rt_lwp *)rt_thread_self()->user_data; } -int exec(char *filename) +int exec(char *filename, int argc, char **argv) { struct rt_lwp *lwp; int result; @@ -324,6 +364,12 @@ int exec(char *filename) dbg_log(DBG_INFO, "lwp malloc : %p, size: %d!\n", lwp, sizeof(struct rt_lwp)); rt_memset(lwp, 0, sizeof(*lwp)); + if (lwp_argscopy(lwp, argc, argv) != 0) + { + rt_free(lwp); + return -ENOMEM; + } + result = lwp_load(filename, lwp, RT_NULL, 0); if (result == RT_EOK) { @@ -348,6 +394,7 @@ int exec(char *filename) } } + rt_free(lwp->args); rt_free(lwp); return -RT_ERROR; diff --git a/components/lwp/lwp.h b/components/lwp/lwp.h index b1ce96455eb29269165b951b11f9a7a417d964cf..0b25cf69175b701e8ce317a85e83b7ab782c6d9c 100644 --- a/components/lwp/lwp.h +++ b/components/lwp/lwp.h @@ -52,6 +52,7 @@ struct rt_lwp uint32_t *kernel_sp; /**< kernel stack point */ struct dfs_fdtable fdt; + void *args; }; struct lwp_header