给lwp传递参数

This commit is contained in:
heyuanjie 2018-06-29 15:32:53 +08:00
parent 0f3820f1b6
commit 7d3af8e9ff
5 changed files with 77 additions and 30 deletions

View File

@ -34,33 +34,33 @@
IMPORT lwp_set_kernel_sp 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 lwp_user_entry PROC
EXPORT lwp_user_entry EXPORT lwp_user_entry
PUSH {R0-R1} ; push text&data addr. PUSH {R0-R3} ; push text&data addr.
MOV R0, SP ; v1 = SP MOV R0, SP ; v1 = SP
BL lwp_set_kernel_sp ; lwp_set_kernel_sp(v1) BL lwp_set_kernel_sp ; lwp_set_kernel_sp(v1)
POP {R0-R1} ; pop app address to R1.
; set CPU to user-thread mode. ; set CPU to user-thread mode.
MRS R2, CONTROL MRS R2, CONTROL
ORR R2, R2, #0x03 ; use PSP, user-thread mode. ORR R2, R2, #0x03 ; use PSP, user-thread mode.
MSR CONTROL, R2 MSR CONTROL, R2
POP {R0-R3} ; pop app address to R1.
; set data address. ; set data address.
MOV R9, R1 MOV R9, R2
; run app, only Thumb-mode. ; run app, only Thumb-mode.
ORR R0, R0, #0x01 ORR R1, R1, #0x01
BX R0 BX R1
; never reach here! ; never reach here!
ENDP ENDP
;/* ;/*
; * void SVC_Handler(void); ; * void SVC_Handler(void);
; */ ; */

View File

@ -34,33 +34,33 @@
IMPORT lwp_set_kernel_sp 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 lwp_user_entry PROC
EXPORT lwp_user_entry EXPORT lwp_user_entry
PUSH {R0-R1} ; push text&data addr. PUSH {R0-R3} ; push text&data addr.
MOV R0, SP ; v1 = SP MOV R0, SP ; v1 = SP
BL lwp_set_kernel_sp ; lwp_set_kernel_sp(v1) BL lwp_set_kernel_sp ; lwp_set_kernel_sp(v1)
POP {R0-R1} ; pop app address to R1.
; set CPU to user-thread mode. ; set CPU to user-thread mode.
MRS R2, CONTROL MRS R2, CONTROL
ORR R2, R2, #0x03 ; use PSP, user-thread mode. ORR R2, R2, #0x03 ; use PSP, user-thread mode.
MSR CONTROL, R2 MSR CONTROL, R2
POP {R0-R3} ; pop app address to R1.
; set data address. ; set data address.
MOV R9, R1 MOV R9, R2
; run app, only Thumb-mode. ; run app, only Thumb-mode.
ORR R0, R0, #0x01 ORR R1, R1, #0x01
BX R0 BX R1
; never reach here! ; never reach here!
ENDP ENDP
;/* ;/*
; * void SVC_Handler(void); ; * void SVC_Handler(void);
; */ ; */

View File

@ -34,29 +34,28 @@
IMPORT lwp_set_kernel_sp 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 lwp_user_entry PROC
EXPORT lwp_user_entry EXPORT lwp_user_entry
PUSH {R0-R1} ; push text&data addr. PUSH {R0-R3} ; push text&data addr.
MOV R0, SP ; v1 = SP MOV R0, SP ; v1 = SP
BL lwp_set_kernel_sp ; lwp_set_kernel_sp(v1) BL lwp_set_kernel_sp ; lwp_set_kernel_sp(v1)
POP {R0-R1} ; pop app address to R1.
; set CPU to user-thread mode. ; set CPU to user-thread mode.
MRS R2, CONTROL MRS R2, CONTROL
ORR R2, R2, #0x03 ; use PSP, user-thread mode. ORR R2, R2, #0x03 ; use PSP, user-thread mode.
MSR CONTROL, R2 MSR CONTROL, R2
POP {R0-R3} ; pop app address to R1.
; set data address. ; set data address.
MOV R9, R1 MOV R9, R2
; run app, only Thumb-mode. ; run app, only Thumb-mode.
ORR R0, R0, #0x01 ORR R1, R1, #0x01
BX R0 BX R1
; never reach here! ; never reach here!
ENDP ENDP

View File

@ -25,10 +25,6 @@
#include <rthw.h> #include <rthw.h>
#include <dfs_posix.h> #include <dfs_posix.h>
#ifdef RT_USING_FINSH
#include <finsh.h>
#endif
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
#error "lwp need file system(RT_USING_DFS)" #error "lwp need file system(RT_USING_DFS)"
#endif #endif
@ -42,7 +38,7 @@
#include <rtdbg.h> #include <rtdbg.h>
extern rt_thread_t rt_current_thread; 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 * RT-Thread light-weight process
@ -62,6 +58,43 @@ uint32_t *lwp_get_kernel_sp(void)
return user_data->kernel_sp; 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) static int lwp_load(const char *filename, struct rt_lwp *lwp, uint8_t *load_addr, size_t addr_size)
{ {
int fd; 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 */ /* check lwp control block */
RT_ASSERT(lwp != RT_NULL); RT_ASSERT(lwp != RT_NULL);
memset(lwp, 0x00, sizeof(struct rt_lwp));
if (load_addr != RT_NULL) if (load_addr != RT_NULL)
{ {
lwp->lwp_type = LWP_TYPE_FIX_ADDR; 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"); dbg_log(DBG_LOG, "lwp free memory pages\n");
rt_lwp_mem_deinit(lwp); 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); dbg_log(DBG_LOG, "lwp free: %p\n", lwp);
rt_free(lwp); rt_free(lwp);
@ -299,7 +339,7 @@ static void lwp_thread(void *parameter)
tid->user_data = (rt_uint32_t)lwp; tid->user_data = (rt_uint32_t)lwp;
tid->cleanup = lwp_cleanup; 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) 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; 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; struct rt_lwp *lwp;
int result; 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)); dbg_log(DBG_INFO, "lwp malloc : %p, size: %d!\n", lwp, sizeof(struct rt_lwp));
rt_memset(lwp, 0, sizeof(*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); result = lwp_load(filename, lwp, RT_NULL, 0);
if (result == RT_EOK) if (result == RT_EOK)
{ {
@ -348,6 +394,7 @@ int exec(char *filename)
} }
} }
rt_free(lwp->args);
rt_free(lwp); rt_free(lwp);
return -RT_ERROR; return -RT_ERROR;

View File

@ -52,6 +52,7 @@ struct rt_lwp
uint32_t *kernel_sp; /**< kernel stack point */ uint32_t *kernel_sp; /**< kernel stack point */
struct dfs_fdtable fdt; struct dfs_fdtable fdt;
void *args;
}; };
struct lwp_header struct lwp_header