提交 bca65f30 编写于 作者: B Bernard Xiong

[lwP] Add light weight process component.

上级 682da9b2
......@@ -26,6 +26,8 @@ source "$RTT_DIR/components/drivers/Kconfig"
source "$RTT_DIR/components/libc/Kconfig"
source "$RTT_DIR/components/lwp/Kconfig"
source "$RTT_DIR/components/net/Kconfig"
source "$RTT_DIR/components/vbus/Kconfig"
......
......@@ -95,6 +95,12 @@ struct dirent
char d_name[DFS_PATH_MAX]; /* The null-terminated file name */
};
struct dfs_fdtable
{
uint32_t maxfd;
struct dfs_fd **fds;
};
/* Initialization of dfs */
int dfs_init(void);
......@@ -110,6 +116,8 @@ struct dfs_fd *fd_get(int fd);
void fd_put(struct dfs_fd *fd);
int fd_is_open(const char *pathname);
struct dfs_fdtable* dfs_fdtable_get(void);
#ifdef __cplusplus
}
#endif
......
......@@ -21,12 +21,16 @@
* Date Author Notes
* 2005-02-22 Bernard The first version.
* 2017-12-11 Bernard Use rt_free to instead of free in fd_is_open().
* 2018-03-20 Heyuanjie dynamic allocation FD
*/
#include <dfs.h>
#include <dfs_fs.h>
#include <dfs_file.h>
#include "dfs_private.h"
#ifdef RT_USING_LWP
#include <lwp.h>
#endif
/* Global variables */
const struct dfs_filesystem_ops *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
......@@ -39,7 +43,8 @@ static struct rt_mutex fslock;
char working_directory[DFS_PATH_MAX] = {"/"};
#endif
struct dfs_fd fd_table[DFS_FD_MAX];
static struct dfs_fdtable _fdtab;
static int fd_alloc(struct dfs_fdtable *fdt, int startfd);
/**
* @addtogroup DFS
......@@ -57,7 +62,7 @@ int dfs_init(void)
/* clear filesystem table */
memset(filesystem_table, 0, sizeof(filesystem_table));
/* clean fd table */
memset(fd_table, 0, sizeof(fd_table));
memset(&_fdtab, 0, sizeof(_fdtab));
/* create device filesystem lock */
rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);
......@@ -90,9 +95,13 @@ INIT_PREV_EXPORT(dfs_init);
*/
void dfs_lock(void)
{
rt_err_t result;
rt_err_t result = -RT_EBUSY;
while (result == -RT_EBUSY)
{
result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
}
result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
if (result != RT_EOK)
{
RT_ASSERT(0);
......@@ -109,6 +118,54 @@ void dfs_unlock(void)
rt_mutex_release(&fslock);
}
static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
{
int idx;
/* find an empty fd entry */
for (idx = startfd; idx < fdt->maxfd; idx++)
{
if (fdt->fds[idx] == RT_NULL)
break;
if (fdt->fds[idx]->ref_count == 0)
break;
}
/* allocate a larger FD container */
if (idx == fdt->maxfd && fdt->maxfd < DFS_FD_MAX)
{
int cnt, index;
struct dfs_fd **fds;
/* increase the number of FD with 4 step length */
cnt = fdt->maxfd + 4;
cnt = cnt > DFS_FD_MAX? DFS_FD_MAX : cnt;
fds = rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *));
if (fds == NULL) goto __out; /* return fdt->maxfd */
/* clean the new allocated fds */
for (index = fdt->maxfd; index < cnt; index ++)
{
fds[index] = NULL;
}
fdt->fds = fds;
fdt->maxfd = cnt;
}
/* allocate 'struct dfs_fd' */
if (idx < fdt->maxfd &&fdt->fds[idx] == RT_NULL)
{
fdt->fds[idx] = rt_malloc(sizeof(struct dfs_fd));
if (fdt->fds[idx] == RT_NULL)
idx = fdt->maxfd;
}
__out:
return idx;
}
/**
* @ingroup Fd
* This function will allocate a file descriptor.
......@@ -119,21 +176,23 @@ int fd_new(void)
{
struct dfs_fd *d;
int idx;
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
/* lock filesystem */
dfs_lock();
/* find an empty fd entry */
for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++);
idx = fd_alloc(fdt, 0);
/* can't find an empty fd entry */
if (idx == DFS_FD_MAX)
if (idx == fdt->maxfd)
{
idx = -(1 + DFS_FD_OFFSET);
goto __result;
}
d = &(fd_table[idx]);
d = fdt->fds[idx];
d->ref_count = 1;
d->magic = DFS_FD_MAGIC;
......@@ -154,13 +213,15 @@ __result:
struct dfs_fd *fd_get(int fd)
{
struct dfs_fd *d;
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
fd = fd - DFS_FD_OFFSET;
if (fd < 0 || fd >= DFS_FD_MAX)
if (fd < 0 || fd >= fdt->maxfd)
return NULL;
dfs_lock();
d = &fd_table[fd];
d = fdt->fds[fd];
/* check dfs_fd valid or not */
if (d->magic != DFS_FD_MAGIC)
......@@ -186,12 +247,25 @@ void fd_put(struct dfs_fd *fd)
RT_ASSERT(fd != NULL);
dfs_lock();
fd->ref_count --;
/* clear this fd entry */
if (fd->ref_count == 0)
{
memset(fd, 0, sizeof(struct dfs_fd));
int index;
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
for (index = 0; index < fdt->maxfd; index ++)
{
if (fdt->fds[index] == fd)
{
rt_free(fd);
fdt->fds[index] = 0;
break;
}
}
}
dfs_unlock();
}
......@@ -211,7 +285,9 @@ int fd_is_open(const char *pathname)
unsigned int index;
struct dfs_filesystem *fs;
struct dfs_fd *fd;
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
fullpath = dfs_normalize_path(NULL, pathname);
if (fullpath != NULL)
{
......@@ -233,11 +309,10 @@ int fd_is_open(const char *pathname)
dfs_lock();
for (index = 0; index < DFS_FD_MAX; index++)
for (index = 0; index < fdt->maxfd; index++)
{
fd = &(fd_table[index]);
if (fd->fops == NULL)
continue;
fd = fdt->fds[index];
if (fd == NULL) continue;
if (fd->fops == fs->ops->fops && strcmp(fd->path, mountpath) == 0)
{
......@@ -414,17 +489,43 @@ up_one:
return fullpath;
}
RTM_EXPORT(dfs_normalize_path);
/**
* This function will get the file descriptor table of current process.
*/
struct dfs_fdtable* dfs_fdtable_get(void)
{
struct dfs_fdtable *fdt;
#ifdef RT_USING_LWP
struct rt_lwp *lwp;
lwp = (struct rt_lwp *)rt_thread_self()->user_data;
if (lwp)
fdt = &lwp->fdt;
else
fdt = &_fdtab;
#else
fdt = &_fdtab;
#endif
return fdt;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
int list_fd(void)
{
int index;
struct dfs_fdtable *fd_table;
fd_table = dfs_fdtable_get();
if (!fd_table) return -1;
rt_enter_critical();
for (index = 0; index < DFS_FD_MAX; index ++)
for (index = 0; index < fd_table->maxfd; index ++)
{
struct dfs_fd *fd = &(fd_table[index]);
struct dfs_fd *fd = fd_table->fds[index];
if (fd->fops)
{
......
config RT_USING_LWP
bool "Using light-weight process"
select RT_USING_DFS
depends on ARCH_ARM_CORTEX_M
default n
help
The lwP is a light weight process running in user mode.
Import('rtconfig')
from building import *
cwd = GetCurrentDir()
src = []
CPPPATH = []
support_arch = {"arm": ["cortex-m3", "cortex-m4", "cortex-m7"]}
platform_file = {'armcc': 'rvds.S', 'gcc': 'gcc.S', 'iar': 'iar.S'}
if rtconfig.PLATFORM in platform_file.keys(): # support platforms
if rtconfig.ARCH in support_arch.keys() and rtconfig.CPU in support_arch[rtconfig.ARCH]:
# arch/arm/cortex-m7/lwp_gcc.S
asm_path = 'arch/' + rtconfig.ARCH + '/' + rtconfig.CPU + '/*_' + platform_file[rtconfig.PLATFORM]
src = Glob('*.c') + Glob(asm_path)
CPPPATH = [cwd]
group = DefineGroup('lwP', src, depend = ['RT_USING_LWP'], CPPPATH = CPPPATH)
Return('group')
/*
* File : lwp_gcc.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
.cpu cortex-m3
.syntax unified
.thumb
.text
/*
* void* lwp_get_sys_api(rt_uint32_t number);
*/
.global lwp_get_sys_api
.global lwp_get_kernel_sp
.global lwp_set_kernel_sp
/*
* void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr);
*/
.global lwp_user_entry
.type lwp_user_entry, % function
lwp_user_entry:
PUSH {R0 - R1} /* 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
/* set data address. */
MOV R9, R1
/* run app, only Thumb-mode. */
ORR R0, R0, #0x01
BX R0
/*
* void SVC_Handler(void);
*/
.global SVC_Handler
.type SVC_Handler, % function
SVC_Handler:
PUSH {LR}
/* get user SP. */
TST LR, #0x4
ITE EQ
MRSEQ R1, MSP
MRSNE R1, PSP
PUSH {R1} /* push app SP. */
MOV R2, R1
STMFD R2!, {R4 - R11} /* push app R4-R11 to app stack , and R1 not change. */
/* get SVC number. */
LDR R0, [R1, #24] /* get the app LR. */
LDRB R0, [R0, #-2] /* get the SVC No. from instruction. */
/* get kernel system API */
BL lwp_get_sys_api
/* if(api == NULL) return; */
CMP R0, #0
POPEQ {R1}
POPEQ {LR}
BXEQ LR
/* push api */
PUSH {R0}
/* get kernel SP to R0. */
BL lwp_get_kernel_sp
POP {R2} /* pop api to R2. */
POP {R1} /* pop app SP to R1. */
/* copy R1(app SP) to R0(server SP). */
LDMFD R1, {R4 - R11} /* pop exception_stack_frame to r4 - r11 register */
STMFD R0!, {R4 - R11} /* push exception_stack_frame to server SP. */
POP {LR}
PUSH {LR}
/* save app SP. */
PUSH {R0 - R3}
SUB R0, R1, #0x20 /* keep {R4 - R11} */
BL lwp_set_kernel_sp
POP {R0 - R3}
/* set to thread-privilege mode. */
MRS R3, CONTROL
BIC R3, R3, #0x01
ORR R3, R3, #0x02
MSR CONTROL, R3
/* call api. */
LDR R3, = svc_exit
STR R3, [R0, #20] /* update LR */
STR R2, [R0, #24] /* update api to PC */
MSR PSP, R0 /* update stack pointer */
POP {LR} /* 0xFFFFFFED */
ORR LR, LR, #0x10
BX LR
/*
* void svc_exit(void);
*/
.global svc_exit
.type svc_exit, % function
svc_exit:
/* get user SP. */
PUSH {R0} /* push result to SP. */
BL lwp_get_kernel_sp
LDMFD R0!, {R4 - R11} /* pop app {R4 - R11} */
ADD R0, R0, #16 /* skip R0-R3 */
LDMFD R0!, {R12, LR}
LDMFD R0!, {R1} /* pop PC to R1 */
LDMFD R0!, {R2} /* pop PSR to R2 */
/* align to 2 words */
ADD R0, R0, #0x07
BIC R0, R0, #0x07
PUSH {R0} /* push user-SP to SP */
/* save server SP. */
ADD R0, SP, #0x08 /* [user-SP, result] */
PUSH {R1 - R2, LR}
BL lwp_set_kernel_sp
POP {R1 - R2, LR}
POP {R3} /* pop user-SP to R3 */
POP {R0} /* restore API result. */
MSR APSR, R2 /* restore PSR */
MSR PSP, R3 /* restore app stack pointer */
/* restore to PSP & thread-unprivilege mode. */
MRS R2, CONTROL
ORR R2, R2, #0x03
MSR CONTROL, R2
/* return to lwp. */
ORR R1, R1, #0x01 /* only Thumb-mode. */
BX R1 /* return to user app. */
;/*
; * File : lwp_iar.S
; * This file is part of RT-Thread RTOS
; * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
; *
; * This program is free software; you can redistribute it and/or modify
; * it under the terms of the GNU General Public License as published by
; * the Free Software Foundation; either version 2 of the License, or
; * (at your option) any later version.
; *
; * This program is distributed in the hope that it will be useful,
; * but WITHOUT ANY WARRANTY; without even the implied warranty of
; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; * GNU General Public License for more details.
; *
; * You should have received a copy of the GNU General Public License along
; * with this program; if not, write to the Free Software Foundation, Inc.,
; * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
; *
; * Change Logs:
; * Date Author Notes
; */
SECTION .text:CODE(2)
THUMB
REQUIRE8
PRESERVE8
;/*
; * void* lwp_get_sys_api(rt_uint32_t number);
; */
IMPORT lwp_get_sys_api
IMPORT lwp_get_kernel_sp
IMPORT lwp_set_kernel_sp
;/*
; * void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr);
; */
EXPORT lwp_user_entry
lwp_user_entry:
PUSH {R0-R1} ; 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
; set data address.
MOV R9, R1
; run app, only Thumb-mode.
ORR R0, R0, #0x01
BX R0
;/*
; * void SVC_Handler(void);
; */
EXPORT SVC_Handler
SVC_Handler:
PUSH {LR}
; get user SP.
TST LR, #0x4
ITE EQ
MRSEQ R1, MSP
MRSNE R1, PSP
PUSH {R1} ; push app SP.
MOV R2, R1
STMFD R2!, {R4 - R11} ; push app R4-R11 to app stack , and R1 not change.
; get SVC number.
LDR R0, [R1, #24] ; get the app LR.
LDRB R0, [R0, #-2] ; get the SVC No. from instruction.
; get kernel system API
BL lwp_get_sys_api
; if(api == NULL) return;
CMP R0, #0
ITTT EQ
POPEQ {R1}
POPEQ {LR}
BXEQ LR
; push api
PUSH {R0}
; get kernel SP to R0.
BL lwp_get_kernel_sp
POP {R2} ; pop api to R2.
POP {R1} ; pop app SP to R1.
; copy R1(app SP) to R0(server SP).
LDMFD R1, {R4 - R11} ; pop exception_stack_frame to r4 - r11 register
STMFD R0!, {R4 - R11} ; push exception_stack_frame to server SP.
POP {LR}
PUSH {LR}
; save app SP.
PUSH {R0 - R3}
SUB R0, R1, #0x20 ; keep {R4 - R11}
BL lwp_set_kernel_sp
POP {R0 - R3}
; set to thread-privilege mode.
MRS R3, CONTROL
BIC R3, R3, #0x01
ORR R3, R3, #0x02
MSR CONTROL, R3
; call api.
LDR R3, =svc_exit
STR R3, [R0, #20] ; update LR
STR R2, [R0, #24] ; update api to PC
MSR PSP, R0 ; update stack pointer
POP {LR} ; 0xFFFFFFED
ORR LR, LR, #0x10
BX LR
;/*
; * void svc_exit(void);
; */
EXPORT svc_exit
svc_exit:
; get user SP.
PUSH {R0} ; push result to SP.
BL lwp_get_kernel_sp
LDMFD R0!, {R4 - R11} ; pop app {R4 - R11}
ADD R0, R0, #16 ; skip R0-R3
LDMFD R0!, {R12, LR} ;
LDMFD R0!, {R1} ; pop PC to R1
LDMFD R0!, {R2} ; pop PSR to R2
; align to 2 words
ADD R0, R0, #0x07
BIC R0, R0, #0x07
PUSH {R0} ; push user-SP to SP
; save server SP.
ADD R0, SP, #0x08 ; [user-SP, result]
PUSH {R1 - R2, LR}
BL lwp_set_kernel_sp
POP {R1 - R2, LR}
POP {R3} ; pop user-SP to R3
POP {R0} ; restore API result.
MSR APSR, R2 ; restore PSR
MSR PSP, R3 ; restore app stack pointer
; restore to PSP & thread-unprivilege mode.
MRS R2, CONTROL
ORR R2, R2, #0x03
MSR CONTROL, R2
; return to lwp.
ORR R1, R1, #0x01 ; only Thumb-mode.
BX R1 ; return to user app.
END
;/*
; * File : lwp_rvds.S
; * This file is part of RT-Thread RTOS
; * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
; *
; * This program is free software; you can redistribute it and/or modify
; * it under the terms of the GNU General Public License as published by
; * the Free Software Foundation; either version 2 of the License, or
; * (at your option) any later version.
; *
; * This program is distributed in the hope that it will be useful,
; * but WITHOUT ANY WARRANTY; without even the implied warranty of
; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; * GNU General Public License for more details.
; *
; * You should have received a copy of the GNU General Public License along
; * with this program; if not, write to the Free Software Foundation, Inc.,
; * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
; *
; * Change Logs:
; * Date Author Notes
; */
AREA |.text|, CODE, READONLY, ALIGN=2
THUMB
REQUIRE8
PRESERVE8
;/*
; * void* lwp_get_sys_api(rt_uint32_t number);
; */
IMPORT lwp_get_sys_api
IMPORT lwp_get_kernel_sp
IMPORT lwp_set_kernel_sp
;/*
; * void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr);
; */
lwp_user_entry PROC
EXPORT lwp_user_entry
PUSH {R0-R1} ; 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
; set data address.
MOV R9, R1
; run app, only Thumb-mode.
ORR R0, R0, #0x01
BX R0
; never reach here!
ENDP
;/*
; * void SVC_Handler(void);
; */
SVC_Handler PROC
EXPORT SVC_Handler
PUSH {LR}
; get user SP.
TST LR, #0x4
ITE EQ
MRSEQ R1, MSP
MRSNE R1, PSP
PUSH {R1} ; push app SP.
MOV R2, R1
STMFD R2!, {R4 - R11} ; push app R4-R11 to app stack , and R1 not change.
; get SVC number.
LDR R0, [R1, #24] ; get the app LR.
LDRB R0, [R0, #-2] ; get the SVC No. from instruction.
; get kernel system API
BL lwp_get_sys_api
; if(api == NULL) return;
CMP R0, #0
POPEQ {R1}
POPEQ {LR}
BXEQ LR
; push api
PUSH {R0}
; get kernel SP to R0.
BL lwp_get_kernel_sp
POP {R2} ; pop api to R2.
POP {R1} ; pop app SP to R1.
; copy R1(app SP) to R0(server SP).
LDMFD R1, {R4 - R11} ; pop exception_stack_frame to r4 - r11 register
STMFD R0!, {R4 - R11} ; push exception_stack_frame to server SP.
POP {LR}
PUSH {LR}
; save app SP.
PUSH {R0 - R3}
SUB R0, R1, #0x20 ; keep {R4 - R11}
BL lwp_set_kernel_sp
POP {R0 - R3}
; set to thread-privilege mode.
MRS R3, CONTROL
BIC R3, R3, #0x01
ORR R3, R3, #0x02
MSR CONTROL, R3
; call api.
LDR R3, =svc_exit
STR R3, [R0, #20] ; update LR
STR R2, [R0, #24] ; update api to PC
MSR PSP, R0 ; update stack pointer
POP {LR} ; 0xFFFFFFED
ORR LR, LR, #0x10
BX LR
ENDP
;/*
; * void svc_exit(void);
; */
svc_exit PROC
EXPORT svc_exit
; get user SP.
PUSH {R0} ; push result to SP.
BL lwp_get_kernel_sp
LDMFD R0!, {R4 - R11} ; pop app {R4 - R11}
ADD R0, R0, #16 ; skip R0-R3
LDMFD R0!, {R12, LR} ;
LDMFD R0!, {R1} ; pop PC to R1
LDMFD R0!, {R2} ; pop PSR to R2
; align to 2 words
ADD R0, R0, #0x07
BIC R0, R0, #0x07
PUSH {R0} ; push user-SP to SP
; save server SP.
ADD R0, SP, #0x08 ; [user-SP, result]
PUSH {R1 - R2, LR}
BL lwp_set_kernel_sp
POP {R1 - R2, LR}
POP {R3} ; pop user-SP to R3
POP {R0} ; restore API result.
MSR APSR, R2 ; restore PSR
MSR PSP, R3 ; restore app stack pointer
; restore to PSP & thread-unprivilege mode.
MRS R2, CONTROL
ORR R2, R2, #0x03
MSR CONTROL, R2
; return to lwp.
ORR R1, R1, #0x01 ; only Thumb-mode.
BX R1 ; return to user app.
ENDP
ALIGN
END
/*
* File : lwp_gcc.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
.cpu cortex-m4
.syntax unified
.thumb
.text
/*
* void* lwp_get_sys_api(rt_uint32_t number);
*/
.global lwp_get_sys_api
.global lwp_get_kernel_sp
.global lwp_set_kernel_sp
/*
* void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr);
*/
.global lwp_user_entry
.type lwp_user_entry, % function
lwp_user_entry:
PUSH {R0 - R1} /* 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
/* set data address. */
MOV R9, R1
/* run app, only Thumb-mode. */
ORR R0, R0, #0x01
BX R0
/*
* void SVC_Handler(void);
*/
.global SVC_Handler
.type SVC_Handler, % function
SVC_Handler:
PUSH {LR}
/* get user SP. */
TST LR, #0x4
ITE EQ
MRSEQ R1, MSP
MRSNE R1, PSP
PUSH {R1} /* push app SP. */
MOV R2, R1
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
TST LR, #0x10
VSTMDBEQ R2!, {D8 - D15}
#endif
STMFD R2!, {R4 - R11} /* push app R4-R11 to app stack , and R1 not change. */
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
MOV R4, #0x00 /* flag = 0 */
TST LR, #0x10 /* if(!EXC_RETURN[4]) */
MOVEQ R4, #0x01 /* flag = 1 */
STMFD R2!, {R4} /* push flag */
#endif
/* get SVC number. */
LDR R0, [R1, #24] /* get the app LR. */
LDRB R0, [R0, #-2] /* get the SVC No. from instruction. */
/* get kernel system API */
BL lwp_get_sys_api
/* if(api == NULL) return; */
CMP R0, #0
POPEQ {R1}
POPEQ {LR}
BXEQ LR
/* push api */
PUSH {R0}
/* get kernel SP to R0. */
BL lwp_get_kernel_sp
POP {R2} /* pop api to R2. */
POP {R1} /* pop app SP to R1. */
/* copy R1(app SP) to R0(server SP). */
LDMFD R1, {R4 - R11} /* pop exception_stack_frame to r4 - r11 register */
STMFD R0!, {R4 - R11} /* push exception_stack_frame to server SP. */
POP {LR}
PUSH {LR}
/* save app SP. */
PUSH {R0 - R3}
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
TST LR, #0x10
SUBEQ R0, R1, #0x64 /* keep {R4 - R11}, {D8-D15}, FLAG */
SUBNE R0, R1, #0x24 /* keep {R4 - R11}, FLAG */
#else
SUB R0, R1, #0x20 /* keep {R4 - R11} */
#endif
BL lwp_set_kernel_sp
POP {R0 - R3}
/* set to thread-privilege mode. */
MRS R3, CONTROL
BIC R3, R3, #0x01
ORR R3, R3, #0x02
MSR CONTROL, R3
/* call api. */
LDR R3, = svc_exit
STR R3, [R0, #20] /* update LR */
STR R2, [R0, #24] /* update api to PC */
MSR PSP, R0 /* update stack pointer */
POP {LR} /* 0xFFFFFFED */
ORR LR, LR, #0x10
BX LR
/*
* void svc_exit(void);
*/
.global svc_exit
.type svc_exit, % function
svc_exit:
/* get user SP. */
PUSH {R0} /* push result to SP. */
BL lwp_get_kernel_sp
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
LDMFD R0!, {R3} /* pop flag */
#endif
LDMFD R0!, {R4 - R11} /* pop app {R4 - R11} */
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
CMP R3, #0 /* if(flag_r3 != 0) */
VLDMIANE R0!, {D8 - D15} /* pop FPU register s16~s31 */
#endif
ADD R0, R0, #16 /* skip R0-R3 */
LDMFD R0!, {R12, LR}
LDMFD R0!, {R1} /* pop PC to R1 */
LDMFD R0!, {R2} /* pop PSR to R2 */
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
CMP R3, #0 /* if(flag_r3 != 0) */
VLDMIANE R0!, {D0 - D7} /* pop FPU register s16~s31 */
LDMIANE R0!, {R3}
VMSRNE FPSCR, R3
#endif
/* align to 2 words */
ADD R0, R0, #0x07
BIC R0, R0, #0x07
PUSH {R0} /* push user-SP to SP */
/* save server SP. */
ADD R0, SP, #0x08 /* [user-SP, result] */
PUSH {R1 - R2, LR}
BL lwp_set_kernel_sp
POP {R1 - R2, LR}
POP {R3} /* pop user-SP to R3 */
POP {R0} /* restore API result. */
MSR APSR, R2 /* restore PSR */
MSR PSP, R3 /* restore app stack pointer */
/* restore to PSP & thread-unprivilege mode. */
MRS R2, CONTROL
ORR R2, R2, #0x03
MSR CONTROL, R2
/* return to lwp. */
ORR R1, R1, #0x01 /* only Thumb-mode. */
BX R1 /* return to user app. */
;/*
; * File : lwp_iar.S
; * This file is part of RT-Thread RTOS
; * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
; *
; * This program is free software; you can redistribute it and/or modify
; * it under the terms of the GNU General Public License as published by
; * the Free Software Foundation; either version 2 of the License, or
; * (at your option) any later version.
; *
; * This program is distributed in the hope that it will be useful,
; * but WITHOUT ANY WARRANTY; without even the implied warranty of
; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; * GNU General Public License for more details.
; *
; * You should have received a copy of the GNU General Public License along
; * with this program; if not, write to the Free Software Foundation, Inc.,
; * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
; *
; * Change Logs:
; * Date Author Notes
; */
SECTION .text:CODE(2)
THUMB
REQUIRE8
PRESERVE8
;/*
; * void* lwp_get_sys_api(rt_uint32_t number);
; */
IMPORT lwp_get_sys_api
IMPORT lwp_get_kernel_sp
IMPORT lwp_set_kernel_sp
;/*
; * void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr);
; */
EXPORT lwp_user_entry
lwp_user_entry:
PUSH {R0-R1} ; 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
; set data address.
MOV R9, R1
; run app, only Thumb-mode.
ORR R0, R0, #0x01
BX R0
;/*
; * void SVC_Handler(void);
; */
EXPORT SVC_Handler
SVC_Handler:
PUSH {LR}
; get user SP.
TST LR, #0x4
ITE EQ
MRSEQ R1, MSP
MRSNE R1, PSP
PUSH {R1} ; push app SP.
MOV R2, R1
#if defined ( __ARMVFP__ )
TST LR, #0x10
IT EQ
VSTMDBEQ R2!, {D8 - D15}
#endif
STMFD R2!, {R4 - R11} ; push app R4-R11 to app stack , and R1 not change.
#if defined ( __ARMVFP__ )
MOV R4, #0x00 ; flag = 0
TST LR, #0x10 ; if(!EXC_RETURN[4])
IT EQ
MOVEQ R4, #0x01 ; flag = 1
STMFD R2!, {R4} ; push flag
#endif
; get SVC number.
LDR R0, [R1, #24] ; get the app LR.
LDRB R0, [R0, #-2] ; get the SVC No. from instruction.
; get kernel system API
BL lwp_get_sys_api
; if(api == NULL) return;
CMP R0, #0
ITTT EQ
POPEQ {R1}
POPEQ {LR}
BXEQ LR
; push api
PUSH {R0}
; get kernel SP to R0.
BL lwp_get_kernel_sp
POP {R2} ; pop api to R2.
POP {R1} ; pop app SP to R1.
; copy R1(app SP) to R0(server SP).
LDMFD R1, {R4 - R11} ; pop exception_stack_frame to r4 - r11 register
STMFD R0!, {R4 - R11} ; push exception_stack_frame to server SP.
POP {LR}
PUSH {LR}
; save app SP.
PUSH {R0 - R3}
#if defined ( __ARMVFP__ )
TST LR, #0x10
ITE EQ
SUBEQ R0, R1, #0x64 ; keep {R4 - R11}, {D8-D15}, FLAG
SUBNE R0, R1, #0x24 ; keep {R4 - R11}, FLAG
#else
SUB R0, R1, #0x20 ; keep {R4 - R11}
#endif
BL lwp_set_kernel_sp
POP {R0 - R3}
; set to thread-privilege mode.
MRS R3, CONTROL
BIC R3, R3, #0x01
ORR R3, R3, #0x02
MSR CONTROL, R3
; call api.
LDR R3, =svc_exit
STR R3, [R0, #20] ; update LR
STR R2, [R0, #24] ; update api to PC
MSR PSP, R0 ; update stack pointer
POP {LR} ; 0xFFFFFFED
ORR LR, LR, #0x10
BX LR
;/*
; * void svc_exit(void);
; */
EXPORT svc_exit
svc_exit:
; get user SP.
PUSH {R0} ; push result to SP.
BL lwp_get_kernel_sp
#if defined ( __ARMVFP__ )
LDMFD R0!, {R3} ; pop flag
#endif
LDMFD R0!, {R4 - R11} ; pop app {R4 - R11}
#if defined ( __ARMVFP__ )
CMP R3, #0 ; if(flag_r3 != 0)
IT NE
VLDMIANE R0!, {D8 - D15} ; pop FPU register s16~s31
#endif
ADD R0, R0, #16 ; skip R0-R3
LDMFD R0!, {R12, LR} ;
LDMFD R0!, {R1} ; pop PC to R1
LDMFD R0!, {R2} ; pop PSR to R2
#if defined ( __ARMVFP__ )
CMP R3, #0 ; if(flag_r3 != 0)
ITTT NE
VLDMIANE R0!, {D0 - D7} ; pop FPU register s16~s31
LDMIANE R0!, {R3}
VMSRNE FPSCR, R3
#endif
; align to 2 words
ADD R0, R0, #0x07
BIC R0, R0, #0x07
PUSH {R0} ; push user-SP to SP
; save server SP.
ADD R0, SP, #0x08 ; [user-SP, result]
PUSH {R1 - R2, LR}
BL lwp_set_kernel_sp
POP {R1 - R2, LR}
POP {R3} ; pop user-SP to R3
POP {R0} ; restore API result.
MSR APSR, R2 ; restore PSR
MSR PSP, R3 ; restore app stack pointer
; restore to PSP & thread-unprivilege mode.
MRS R2, CONTROL
ORR R2, R2, #0x03
MSR CONTROL, R2
; return to lwp.
ORR R1, R1, #0x01 ; only Thumb-mode.
BX R1 ; return to user app.
END
;/*
; * File : lwp_rvds.S
; * This file is part of RT-Thread RTOS
; * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
; *
; * This program is free software; you can redistribute it and/or modify
; * it under the terms of the GNU General Public License as published by
; * the Free Software Foundation; either version 2 of the License, or
; * (at your option) any later version.
; *
; * This program is distributed in the hope that it will be useful,
; * but WITHOUT ANY WARRANTY; without even the implied warranty of
; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; * GNU General Public License for more details.
; *
; * You should have received a copy of the GNU General Public License along
; * with this program; if not, write to the Free Software Foundation, Inc.,
; * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
; *
; * Change Logs:
; * Date Author Notes
; */
AREA |.text|, CODE, READONLY, ALIGN=2
THUMB
REQUIRE8
PRESERVE8
;/*
; * void* lwp_get_sys_api(rt_uint32_t number);
; */
IMPORT lwp_get_sys_api
IMPORT lwp_get_kernel_sp
IMPORT lwp_set_kernel_sp
;/*
; * void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr);
; */
lwp_user_entry PROC
EXPORT lwp_user_entry
PUSH {R0-R1} ; 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
; set data address.
MOV R9, R1
; run app, only Thumb-mode.
ORR R0, R0, #0x01
BX R0
; never reach here!
ENDP
;/*
; * void SVC_Handler(void);
; */
SVC_Handler PROC
EXPORT SVC_Handler
PUSH {LR}
; get user SP.
TST LR, #0x4
ITE EQ
MRSEQ R1, MSP
MRSNE R1, PSP
PUSH {R1} ; push app SP.
MOV R2, R1
IF {FPU} != "SoftVFP"
TST LR, #0x10
VSTMFDEQ R2!, {D8 - D15}
ENDIF
STMFD R2!, {R4 - R11} ; push app R4-R11 to app stack , and R1 not change.
IF {FPU} != "SoftVFP"
MOV R4, #0x00 ; flag = 0
TST LR, #0x10 ; if(!EXC_RETURN[4])
MOVEQ R4, #0x01 ; flag = 1
STMFD R2!, {R4} ; push flag
ENDIF
; get SVC number.
LDR R0, [R1, #24] ; get the app LR.
LDRB R0, [R0, #-2] ; get the SVC No. from instruction.
; get kernel system API
BL lwp_get_sys_api
; if(api == NULL) return;
CMP R0, #0
POPEQ {R1}
POPEQ {LR}
BXEQ LR
; push api
PUSH {R0}
; get kernel SP to R0.
BL lwp_get_kernel_sp
POP {R2} ; pop api to R2.
POP {R1} ; pop app SP to R1.
; copy R1(app SP) to R0(server SP).
LDMFD R1, {R4 - R11} ; pop exception_stack_frame to r4 - r11 register
STMFD R0!, {R4 - R11} ; push exception_stack_frame to server SP.
POP {LR}
PUSH {LR}
; save app SP.
PUSH {R0 - R3}
IF {FPU} != "SoftVFP"
TST LR, #0x10
SUBEQ R0, R1, #0x64 ; keep {R4 - R11}, {D8-D15}, FLAG
SUBNE R0, R1, #0x24 ; keep {R4 - R11}, FLAG
ELSE
SUB R0, R1, #0x20 ; keep {R4 - R11}
ENDIF
BL lwp_set_kernel_sp
POP {R0 - R3}
; set to thread-privilege mode.
MRS R3, CONTROL
BIC R3, R3, #0x01
ORR R3, R3, #0x02
MSR CONTROL, R3
; call api.
LDR R3, =svc_exit
STR R3, [R0, #20] ; update LR
STR R2, [R0, #24] ; update api to PC
MSR PSP, R0 ; update stack pointer
POP {LR} ; 0xFFFFFFED
ORR LR, LR, #0x10
BX LR
ENDP
;/*
; * void svc_exit(void);
; */
svc_exit PROC
EXPORT svc_exit
; get user SP.
PUSH {R0} ; push result to SP.
BL lwp_get_kernel_sp
IF {FPU} != "SoftVFP"
LDMFD R0!, {R3} ; pop flag
ENDIF
LDMFD R0!, {R4 - R11} ; pop app {R4 - R11}
IF {FPU} != "SoftVFP"
CMP R3, #0 ; if(flag_r3 != 0)
VLDMFDNE R0!, {D8 - D15} ; pop FPU register s16~s31
ENDIF
ADD R0, R0, #16 ; skip R0-R3
LDMFD R0!, {R12, LR} ;
LDMFD R0!, {R1} ; pop PC to R1
LDMFD R0!, {R2} ; pop PSR to R2
IF {FPU} != "SoftVFP"
CMP R3, #0 ; if(flag_r3 != 0)
VLDMFDNE R0!, {D0 - D7} ; pop FPU register s16~s31
LDMFDNE R0!, {R3}
VMSRNE FPSCR, R3
ENDIF
; align to 2 words
ADD R0, R0, #0x07
BIC R0, R0, #0x07
PUSH {R0} ; push user-SP to SP
; save server SP.
ADD R0, SP, #0x08 ; [user-SP, result]
PUSH {R1 - R2, LR}
BL lwp_set_kernel_sp
POP {R1 - R2, LR}
POP {R3} ; pop user-SP to R3
POP {R0} ; restore API result.
MSR APSR, R2 ; restore PSR
MSR PSP, R3 ; restore app stack pointer
; restore to PSP & thread-unprivilege mode.
MRS R2, CONTROL
ORR R2, R2, #0x03
MSR CONTROL, R2
; return to lwp.
ORR R1, R1, #0x01 ; only Thumb-mode.
BX R1 ; return to user app.
ENDP
ALIGN
END
/*
* File : lwp_gcc.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
.cpu cortex-m4
.syntax unified
.thumb
.text
/*
* void* lwp_get_sys_api(rt_uint32_t number);
*/
.global lwp_get_sys_api
.global lwp_get_kernel_sp
.global lwp_set_kernel_sp
/*
* void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr);
*/
.global lwp_user_entry
.type lwp_user_entry, % function
lwp_user_entry:
PUSH {R0 - R1} /* 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
/* set data address. */
MOV R9, R1
/* run app, only Thumb-mode. */
ORR R0, R0, #0x01
BX R0
/*
* void SVC_Handler(void);
*/
.global SVC_Handler
.type SVC_Handler, % function
SVC_Handler:
PUSH {LR}
/* get user SP. */
TST LR, #0x4
ITE EQ
MRSEQ R1, MSP
MRSNE R1, PSP
PUSH {R1} /* push app SP. */
MOV R2, R1
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
TST LR, #0x10
VSTMDBEQ R2!, {D8 - D15}
#endif
STMFD R2!, {R4 - R11} /* push app R4-R11 to app stack , and R1 not change. */
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
MOV R4, #0x00 /* flag = 0 */
TST LR, #0x10 /* if(!EXC_RETURN[4]) */
MOVEQ R4, #0x01 /* flag = 1 */
STMFD R2!, {R4} /* push flag */
#endif
/* get SVC number. */
LDR R0, [R1, #24] /* get the app LR. */
LDRB R0, [R0, #-2] /* get the SVC No. from instruction. */
/* get kernel system API */
BL lwp_get_sys_api
/* if(api == NULL) return; */
CMP R0, #0
POPEQ {R1}
POPEQ {LR}
BXEQ LR
/* push api */
PUSH {R0}
/* get kernel SP to R0. */
BL lwp_get_kernel_sp
POP {R2} /* pop api to R2. */
POP {R1} /* pop app SP to R1. */
/* copy R1(app SP) to R0(server SP). */
LDMFD R1, {R4 - R11} /* pop exception_stack_frame to r4 - r11 register */
STMFD R0!, {R4 - R11} /* push exception_stack_frame to server SP. */
POP {LR}
PUSH {LR}
/* save app SP. */
PUSH {R0 - R3}
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
TST LR, #0x10
SUBEQ R0, R1, #0x64 /* keep {R4 - R11}, {D8-D15}, FLAG */
SUBNE R0, R1, #0x24 /* keep {R4 - R11}, FLAG */
#else
SUB R0, R1, #0x20 /* keep {R4 - R11} */
#endif
BL lwp_set_kernel_sp
POP {R0 - R3}
/* set to thread-privilege mode. */
MRS R3, CONTROL
BIC R3, R3, #0x01
ORR R3, R3, #0x02
MSR CONTROL, R3
/* call api. */
LDR R3, = svc_exit
STR R3, [R0, #20] /* update LR */
STR R2, [R0, #24] /* update api to PC */
MSR PSP, R0 /* update stack pointer */
POP {LR} /* 0xFFFFFFED */
ORR LR, LR, #0x10
BX LR
/*
* void svc_exit(void);
*/
.global svc_exit
.type svc_exit, % function
svc_exit:
/* get user SP. */
PUSH {R0} /* push result to SP. */
BL lwp_get_kernel_sp
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
LDMFD R0!, {R3} /* pop flag */
#endif
LDMFD R0!, {R4 - R11} /* pop app {R4 - R11} */
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
CMP R3, #0 /* if(flag_r3 != 0) */
VLDMIANE R0!, {D8 - D15} /* pop FPU register s16~s31 */
#endif
ADD R0, R0, #16 /* skip R0-R3 */
LDMFD R0!, {R12, LR}
LDMFD R0!, {R1} /* pop PC to R1 */
LDMFD R0!, {R2} /* pop PSR to R2 */
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
CMP R3, #0 /* if(flag_r3 != 0) */
VLDMIANE R0!, {D0 - D7} /* pop FPU register s16~s31 */
LDMIANE R0!, {R3}
VMSRNE FPSCR, R3
#endif
/* align to 2 words */
ADD R0, R0, #0x07
BIC R0, R0, #0x07
PUSH {R0} /* push user-SP to SP */
/* save server SP. */
ADD R0, SP, #0x08 /* [user-SP, result] */
PUSH {R1 - R2, LR}
BL lwp_set_kernel_sp
POP {R1 - R2, LR}
POP {R3} /* pop user-SP to R3 */
POP {R0} /* restore API result. */
MSR APSR, R2 /* restore PSR */
MSR PSP, R3 /* restore app stack pointer */
/* restore to PSP & thread-unprivilege mode. */
MRS R2, CONTROL
ORR R2, R2, #0x03
MSR CONTROL, R2
/* return to lwp. */
ORR R1, R1, #0x01 /* only Thumb-mode. */
BX R1 /* return to user app. */
;/*
; * File : lwp_iar.S
; * This file is part of RT-Thread RTOS
; * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
; *
; * This program is free software; you can redistribute it and/or modify
; * it under the terms of the GNU General Public License as published by
; * the Free Software Foundation; either version 2 of the License, or
; * (at your option) any later version.
; *
; * This program is distributed in the hope that it will be useful,
; * but WITHOUT ANY WARRANTY; without even the implied warranty of
; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; * GNU General Public License for more details.
; *
; * You should have received a copy of the GNU General Public License along
; * with this program; if not, write to the Free Software Foundation, Inc.,
; * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
; *
; * Change Logs:
; * Date Author Notes
; */
SECTION .text:CODE(2)
THUMB
REQUIRE8
PRESERVE8
;/*
; * void* lwp_get_sys_api(rt_uint32_t number);
; */
IMPORT lwp_get_sys_api
IMPORT lwp_get_kernel_sp
IMPORT lwp_set_kernel_sp
;/*
; * void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr);
; */
EXPORT lwp_user_entry
lwp_user_entry:
PUSH {R0-R1} ; 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
; set data address.
MOV R9, R1
; run app, only Thumb-mode.
ORR R0, R0, #0x01
BX R0
;/*
; * void SVC_Handler(void);
; */
EXPORT SVC_Handler
SVC_Handler:
PUSH {LR}
; get user SP.
TST LR, #0x4
ITE EQ
MRSEQ R1, MSP
MRSNE R1, PSP
PUSH {R1} ; push app SP.
MOV R2, R1
#if defined ( __ARMVFP__ )
TST LR, #0x10
IT EQ
VSTMDBEQ R2!, {D8 - D15}
#endif
STMFD R2!, {R4 - R11} ; push app R4-R11 to app stack , and R1 not change.
#if defined ( __ARMVFP__ )
MOV R4, #0x00 ; flag = 0
TST LR, #0x10 ; if(!EXC_RETURN[4])
IT EQ
MOVEQ R4, #0x01 ; flag = 1
STMFD R2!, {R4} ; push flag
#endif
; get SVC number.
LDR R0, [R1, #24] ; get the app LR.
LDRB R0, [R0, #-2] ; get the SVC No. from instruction.
; get kernel system API
BL lwp_get_sys_api
; if(api == NULL) return;
CMP R0, #0
ITTT EQ
POPEQ {R1}
POPEQ {LR}
BXEQ LR
; push api
PUSH {R0}
; get kernel SP to R0.
BL lwp_get_kernel_sp
POP {R2} ; pop api to R2.
POP {R1} ; pop app SP to R1.
; copy R1(app SP) to R0(server SP).
LDMFD R1, {R4 - R11} ; pop exception_stack_frame to r4 - r11 register
STMFD R0!, {R4 - R11} ; push exception_stack_frame to server SP.
POP {LR}
PUSH {LR}
; save app SP.
PUSH {R0 - R3}
#if defined ( __ARMVFP__ )
TST LR, #0x10
ITE EQ
SUBEQ R0, R1, #0x64 ; keep {R4 - R11}, {D8-D15}, FLAG
SUBNE R0, R1, #0x24 ; keep {R4 - R11}, FLAG
#else
SUB R0, R1, #0x20 ; keep {R4 - R11}
#endif
BL lwp_set_kernel_sp
POP {R0 - R3}
; set to thread-privilege mode.
MRS R3, CONTROL
BIC R3, R3, #0x01
ORR R3, R3, #0x02
MSR CONTROL, R3
; call api.
LDR R3, =svc_exit
STR R3, [R0, #20] ; update LR
STR R2, [R0, #24] ; update api to PC
MSR PSP, R0 ; update stack pointer
POP {LR} ; 0xFFFFFFED
ORR LR, LR, #0x10
BX LR
;/*
; * void svc_exit(void);
; */
EXPORT svc_exit
svc_exit:
; get user SP.
PUSH {R0} ; push result to SP.
BL lwp_get_kernel_sp
#if defined ( __ARMVFP__ )
LDMFD R0!, {R3} ; pop flag
#endif
LDMFD R0!, {R4 - R11} ; pop app {R4 - R11}
#if defined ( __ARMVFP__ )
CMP R3, #0 ; if(flag_r3 != 0)
IT NE
VLDMIANE R0!, {D8 - D15} ; pop FPU register s16~s31
#endif
ADD R0, R0, #16 ; skip R0-R3
LDMFD R0!, {R12, LR} ;
LDMFD R0!, {R1} ; pop PC to R1
LDMFD R0!, {R2} ; pop PSR to R2
#if defined ( __ARMVFP__ )
CMP R3, #0 ; if(flag_r3 != 0)
ITTT NE
VLDMIANE R0!, {D0 - D7} ; pop FPU register s16~s31
LDMIANE R0!, {R3}
VMSRNE FPSCR, R3
#endif
; align to 2 words
ADD R0, R0, #0x07
BIC R0, R0, #0x07
PUSH {R0} ; push user-SP to SP
; save server SP.
ADD R0, SP, #0x08 ; [user-SP, result]
PUSH {R1 - R2, LR}
BL lwp_set_kernel_sp
POP {R1 - R2, LR}
POP {R3} ; pop user-SP to R3
POP {R0} ; restore API result.
MSR APSR, R2 ; restore PSR
MSR PSP, R3 ; restore app stack pointer
; restore to PSP & thread-unprivilege mode.
MRS R2, CONTROL
ORR R2, R2, #0x03
MSR CONTROL, R2
; return to lwp.
ORR R1, R1, #0x01 ; only Thumb-mode.
BX R1 ; return to user app.
END
;/*
; * File : lwp_rvds.S
; * This file is part of RT-Thread RTOS
; * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
; *
; * This program is free software; you can redistribute it and/or modify
; * it under the terms of the GNU General Public License as published by
; * the Free Software Foundation; either version 2 of the License, or
; * (at your option) any later version.
; *
; * This program is distributed in the hope that it will be useful,
; * but WITHOUT ANY WARRANTY; without even the implied warranty of
; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; * GNU General Public License for more details.
; *
; * You should have received a copy of the GNU General Public License along
; * with this program; if not, write to the Free Software Foundation, Inc.,
; * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
; *
; * Change Logs:
; * Date Author Notes
; */
AREA |.text|, CODE, READONLY, ALIGN=2
THUMB
REQUIRE8
PRESERVE8
;/*
; * void* lwp_get_sys_api(rt_uint32_t number);
; */
IMPORT lwp_get_sys_api
IMPORT lwp_get_kernel_sp
IMPORT lwp_set_kernel_sp
;/*
; * void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr);
; */
lwp_user_entry PROC
EXPORT lwp_user_entry
PUSH {R0-R1} ; 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
; set data address.
MOV R9, R1
; run app, only Thumb-mode.
ORR R0, R0, #0x01
BX R0
; never reach here!
ENDP
;/*
; * void SVC_Handler(void);
; */
SVC_Handler PROC
EXPORT SVC_Handler
PUSH {LR}
; get user SP.
TST LR, #0x4
ITE EQ
MRSEQ R1, MSP
MRSNE R1, PSP
PUSH {R1} ; push app SP.
MOV R2, R1
IF {FPU} != "SoftVFP"
TST LR, #0x10
VSTMFDEQ R2!, {D8 - D15}
ENDIF
STMFD R2!, {R4 - R11} ; push app R4-R11 to app stack , and R1 not change.
IF {FPU} != "SoftVFP"
MOV R4, #0x00 ; flag = 0
TST LR, #0x10 ; if(!EXC_RETURN[4])
MOVEQ R4, #0x01 ; flag = 1
STMFD R2!, {R4} ; push flag
ENDIF
; get SVC number.
LDR R0, [R1, #24] ; get the app LR.
LDRB R0, [R0, #-2] ; get the SVC No. from instruction.
; get kernel system API
BL lwp_get_sys_api
; if(api == NULL) return;
CMP R0, #0
POPEQ {R1}
POPEQ {LR}
BXEQ LR
; push api
PUSH {R0}
; get kernel SP to R0.
BL lwp_get_kernel_sp
POP {R2} ; pop api to R2.
POP {R1} ; pop app SP to R1.
; copy R1(app SP) to R0(server SP).
LDMFD R1, {R4 - R11} ; pop exception_stack_frame to r4 - r11 register
STMFD R0!, {R4 - R11} ; push exception_stack_frame to server SP.
POP {LR}
PUSH {LR}
; save app SP.
PUSH {R0 - R3}
IF {FPU} != "SoftVFP"
TST LR, #0x10
SUBEQ R0, R1, #0x64 ; keep {R4 - R11}, {D8-D15}, FLAG
SUBNE R0, R1, #0x24 ; keep {R4 - R11}, FLAG
ELSE
SUB R0, R1, #0x20 ; keep {R4 - R11}
ENDIF
BL lwp_set_kernel_sp
POP {R0 - R3}
; set to thread-privilege mode.
MRS R3, CONTROL
BIC R3, R3, #0x01
ORR R3, R3, #0x02
MSR CONTROL, R3
; call api.
LDR R3, =svc_exit
STR R3, [R0, #20] ; update LR
STR R2, [R0, #24] ; update api to PC
MSR PSP, R0 ; update stack pointer
POP {LR} ; 0xFFFFFFED
ORR LR, LR, #0x10
BX LR
ENDP
;/*
; * void svc_exit(void);
; */
svc_exit PROC
EXPORT svc_exit
; get user SP.
PUSH {R0} ; push result to SP.
BL lwp_get_kernel_sp
IF {FPU} != "SoftVFP"
LDMFD R0!, {R3} ; pop flag
ENDIF
LDMFD R0!, {R4 - R11} ; pop app {R4 - R11}
IF {FPU} != "SoftVFP"
CMP R3, #0 ; if(flag_r3 != 0)
VLDMFDNE R0!, {D8 - D15} ; pop FPU register s16~s31
ENDIF
ADD R0, R0, #16 ; skip R0-R3
LDMFD R0!, {R12, LR} ;
LDMFD R0!, {R1} ; pop PC to R1
LDMFD R0!, {R2} ; pop PSR to R2
IF {FPU} != "SoftVFP"
CMP R3, #0 ; if(flag_r3 != 0)
VLDMFDNE R0!, {D0 - D7} ; pop FPU register s16~s31
LDMFDNE R0!, {R3}
VMSRNE FPSCR, R3
ENDIF
; align to 2 words
ADD R0, R0, #0x07
BIC R0, R0, #0x07
PUSH {R0} ; push user-SP to SP
; save server SP.
ADD R0, SP, #0x08 ; [user-SP, result]
PUSH {R1 - R2, LR}
BL lwp_set_kernel_sp
POP {R1 - R2, LR}
POP {R3} ; pop user-SP to R3
POP {R0} ; restore API result.
MSR APSR, R2 ; restore PSR
MSR PSP, R3 ; restore app stack pointer
; restore to PSP & thread-unprivilege mode.
MRS R2, CONTROL
ORR R2, R2, #0x03
MSR CONTROL, R2
; return to lwp.
ORR R1, R1, #0x01 ; only Thumb-mode.
BX R1 ; return to user app.
ENDP
ALIGN
END
/*
* File : clock.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
#include <rtthread.h>
#include <rthw.h>
#include <dfs_posix.h>
#ifdef RT_USING_FINSH
#include <finsh.h>
#endif
#ifndef RT_USING_DFS
#error "lwp need file system(RT_USING_DFS)"
#endif
#include "lwp.h"
#define DBG_ENABLE
#define DBG_SECTION_NAME "[LWP]"
#define DBG_COLOR
#define DBG_LEVEL DBG_LOG
#include <rtdbg.h>
extern rt_thread_t rt_current_thread;
extern void lwp_user_entry(const void *text, void *data);
/**
* RT-Thread light-weight process
*/
void lwp_set_kernel_sp(uint32_t *sp)
{
struct rt_lwp *user_data;
user_data = (struct rt_lwp *)rt_current_thread->user_data;
user_data->kernel_sp = sp;
}
uint32_t *lwp_get_kernel_sp(void)
{
struct rt_lwp *user_data;
user_data = (struct rt_lwp *)rt_current_thread->user_data;
return user_data->kernel_sp;
}
static int lwp_load(const char *filename, struct rt_lwp *lwp, uint8_t *load_addr, size_t addr_size)
{
int fd;
uint8_t *ptr;
int result = RT_EOK;
int nbytes;
struct lwp_header header;
struct lwp_chunk chunk;
/* check file name */
RT_ASSERT(filename != RT_NULL);
/* 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;
ptr = load_addr;
}
else
{
lwp->lwp_type = LWP_TYPE_DYN_ADDR;
ptr = RT_NULL;
}
/* open lwp */
fd = open(filename, 0, O_RDONLY);
if (fd < 0)
{
dbg_log(DBG_ERROR, "open file:%s failed!\n", filename);
result = -RT_ENOSYS;
goto _exit;
}
/* read lwp header */
nbytes = read(fd, &header, sizeof(struct lwp_header));
if (nbytes != sizeof(struct lwp_header))
{
dbg_log(DBG_ERROR, "read lwp header return error size: %d!\n", nbytes);
result = -RT_EIO;
goto _exit;
}
/* check file header */
if (header.magic != LWP_MAGIC)
{
dbg_log(DBG_ERROR, "erro header magic number: 0x%02X\n", header.magic);
result = -RT_EINVAL;
goto _exit;
}
/* read text chunk info */
nbytes = read(fd, &chunk, sizeof(struct lwp_chunk));
if (nbytes != sizeof(struct lwp_chunk))
{
dbg_log(DBG_ERROR, "read text chunk info failed!\n");
result = -RT_EIO;
goto _exit;
}
dbg_log(DBG_LOG, "chunk name: %s, total len %d, data %d, need space %d!\n",
"text", /*chunk.name*/ chunk.total_len, chunk.data_len, chunk.data_len_space);
/* load text */
{
lwp->text_size = RT_ALIGN(chunk.data_len_space, 4);
if (load_addr)
lwp->text_entry = ptr;
else
{
#ifdef RT_USING_CACHE
lwp->text_entry = (rt_uint8_t *)rt_malloc_align(lwp->text_size, RT_CPU_CACHE_LINE_SZ);
#else
lwp->text_entry = (rt_uint8_t *)rt_malloc(lwp->text_size);
#endif
if (lwp->text_entry == RT_NULL)
{
dbg_log(DBG_ERROR, "alloc text memory faild!\n");
result = -RT_ENOMEM;
goto _exit;
}
else
{
dbg_log(DBG_LOG, "lwp text malloc : %p, size: %d!\n", lwp->text_entry, lwp->text_size);
}
}
dbg_log(DBG_INFO, "load text %d => (0x%08x, 0x%08x)\n", lwp->text_size, (uint32_t)lwp->text_entry, (uint32_t)lwp->text_entry + lwp->text_size);
nbytes = read(fd, lwp->text_entry, chunk.data_len);
if (nbytes != chunk.data_len)
{
dbg_log(DBG_ERROR, "read text region from file failed!\n");
result = -RT_EIO;
goto _exit;
}
#ifdef RT_USING_CACHE
else
{
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, lwp->text_entry, lwp->text_size);
rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, lwp->text_entry, lwp->text_size);
}
#endif
if (ptr != RT_NULL) ptr += nbytes;
/* skip text hole */
if ((chunk.total_len - sizeof(struct lwp_chunk) - chunk.data_len))
{
dbg_log(DBG_LOG, "skip text hole %d!\n", (chunk.total_len - sizeof(struct lwp_chunk) - chunk.data_len));
lseek(fd, (chunk.total_len - sizeof(struct lwp_chunk) - chunk.data_len), SEEK_CUR);
}
}
/* load data */
nbytes = read(fd, &chunk, sizeof(struct lwp_chunk));
if (nbytes != sizeof(struct lwp_chunk))
{
dbg_log(DBG_ERROR, "read data chunk info failed!\n");
result = -RT_EIO;
goto _exit;
}
dbg_log(DBG_LOG, "chunk name: %s, total len %d, data %d, need space %d!\n",
chunk.name, chunk.total_len, chunk.data_len, chunk.data_len_space);
{
lwp->data_size = RT_ALIGN(chunk.data_len_space, 4);
if (load_addr)
lwp->data = ptr;
else
{
lwp->data = rt_malloc(lwp->data_size);
if (lwp->data == RT_NULL)
{
dbg_log(DBG_ERROR, "alloc data memory faild!\n");
result = -RT_ENOMEM;
goto _exit;
}
else
{
dbg_log(DBG_LOG, "lwp data malloc : %p, size: %d!\n", lwp->data, lwp->data_size);
rt_memset(lwp->data, 0, lwp->data_size);
}
}
dbg_log(DBG_INFO, "load data %d => (0x%08x, 0x%08x)\n", lwp->data_size, (uint32_t)lwp->data, (uint32_t)lwp->data + lwp->data_size);
nbytes = read(fd, lwp->data, chunk.data_len);
if (nbytes != chunk.data_len)
{
dbg_log(DBG_ERROR, "read data region from file failed!\n");
result = -RT_ERROR;
goto _exit;
}
}
_exit:
if (fd >= 0)
close(fd);
if (result != RT_EOK)
{
if (lwp->lwp_type == LWP_TYPE_DYN_ADDR)
{
dbg_log(DBG_ERROR, "lwp dynamic load faild, %d\n", result);
if (lwp->text_entry)
{
dbg_log(DBG_LOG, "lwp text free: %p\n", lwp->text_entry);
#ifdef RT_USING_CACHE
rt_free_align(lwp->text_entry);
#else
rt_free(lwp->text_entry);
#endif
}
if (lwp->data)
{
dbg_log(DBG_LOG, "lwp data free: %p\n", lwp->data);
rt_free(lwp->data);
}
}
}
return result;
}
static void lwp_cleanup(struct rt_thread *tid)
{
struct rt_lwp *lwp;
dbg_log(DBG_INFO, "thread: %s, stack_addr: %08X\n", tid->name, tid->stack_addr);
lwp = (struct rt_lwp *)tid->user_data;
if (lwp->lwp_type == LWP_TYPE_DYN_ADDR)
{
dbg_log(DBG_INFO, "dynamic lwp\n");
if (lwp->text_entry)
{
dbg_log(DBG_LOG, "lwp text free: %p\n", lwp->text_entry);
#ifdef RT_USING_CACHE
rt_free_align(lwp->text_entry);
#else
rt_free(lwp->text_entry);
#endif
}
if (lwp->data)
{
dbg_log(DBG_LOG, "lwp data free: %p\n", lwp->data);
rt_free(lwp->data);
}
}
dbg_log(DBG_LOG, "lwp free memory pages\n");
rt_lwp_mem_deinit(lwp);
dbg_log(DBG_LOG, "lwp free: %p\n", lwp);
rt_free(lwp);
/* TODO: cleanup fd table */
}
static void lwp_thread(void *parameter)
{
volatile uint32_t tmp;
rt_thread_t tid;
struct rt_lwp *lwp;
rt_kprintf("%08x %08x\n", &tmp, tmp);
lwp = (struct rt_lwp *)parameter;
rt_lwp_mem_init(lwp);
tid = rt_thread_self();
tid->user_data = (rt_uint32_t)lwp;
tid->cleanup = lwp_cleanup;
lwp_user_entry(lwp->text_entry, lwp->data);
}
struct rt_lwp *rt_lwp_self(void)
{
return (struct rt_lwp *)rt_thread_self()->user_data;
}
int exec(char *filename)
{
struct rt_lwp *lwp;
int result;
if (filename == RT_NULL)
return -RT_ERROR;
lwp = (struct rt_lwp *)rt_malloc(sizeof(struct rt_lwp));
if (lwp == RT_NULL)
{
dbg_log(DBG_ERROR, "lwp struct out of memory!\n");
return -RT_ENOMEM;
}
dbg_log(DBG_INFO, "lwp malloc : %p, size: %d!\n", lwp, sizeof(struct rt_lwp));
rt_memset(lwp, 0, sizeof(*lwp));
result = lwp_load(filename, lwp, RT_NULL, 0);
if (result == RT_EOK)
{
rt_thread_t tid;
tid = rt_thread_create("user", lwp_thread, (void *)lwp,
1024 * 4, 2, 200);
if (tid != RT_NULL)
{
dbg_log(DBG_LOG, "lwp kernel => (0x%08x, 0x%08x)\n", (rt_uint32_t)tid->stack_addr, (rt_uint32_t)tid->stack_addr + tid->stack_size);
rt_thread_startup(tid);
return RT_EOK;
}
else
{
#ifdef RT_USING_CACHE
rt_free_align(lwp->text_entry);
#else
rt_free(lwp->text_entry);
#endif
rt_free(lwp->data);
}
}
rt_free(lwp);
return -RT_ERROR;
}
FINSH_FUNCTION_EXPORT(exec, loader a user app &run);
int _exec(int argc, char **argv)
{
if (argc != 2)
return -RT_ERROR;
return exec(argv[1]);
}
MSH_CMD_EXPORT_ALIAS(_exec, exec, loader a user app &run);
/*
* File : lwp.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
#ifndef __LWP_H__
#define __LWP_H__
#define LWP_MAGIC 0x5A
#define LWP_TYPE_FIX_ADDR 0x01
#define LWP_TYPE_DYN_ADDR 0x02
#define LWP_ARG_MAX 8
#include <stdint.h>
#include <rtthread.h>
#include <dfs.h>
#include <lwp_memheap.h>
struct rt_lwp
{
uint8_t lwp_type;
uint8_t heap_cnt;
uint8_t reserv[2];
rt_list_t hlist; /**< headp list */
uint8_t *text_entry;
uint32_t text_size;
uint8_t *data;
uint32_t data_size;
uint32_t *kernel_sp; /**< kernel stack point */
struct dfs_fdtable fdt;
};
struct lwp_header
{
uint8_t magic;
uint8_t compress_encrypt_algo;
uint16_t reserved;
uint32_t crc32;
};
struct lwp_chunk
{
uint32_t total_len;
char name[4];
uint32_t data_len;
uint32_t data_len_space;
};
extern struct rt_lwp *rt_lwp_self(void);
extern void rt_lwp_mem_init(struct rt_lwp *lwp);
extern void rt_lwp_mem_deinit(struct rt_lwp *lwp);
extern void *rt_lwp_mem_malloc(rt_uint32_t size);
extern void rt_lwp_mem_free(void *addr);
extern void *rt_lwp_mem_realloc(void *rmem, rt_size_t newsize);
#endif
/*
* File : lwp_mem.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2012, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-03-24 Tanek the first version
*/
#include <stdio.h>
#include <rtthread.h>
#include <lwp.h>
#define DBG_ENABLE
#define DBG_SECTION_NAME "[LWPMEM]"
#define DBG_COLOR
#define DBG_LEVEL DBG_LOG
#include <rtdbg.h>
// todo: remove repleat code
#define RT_MEMHEAP_SIZE RT_ALIGN(sizeof(struct rt_lwp_memheap_item), RT_ALIGN_SIZE)
#define MEMITEM_SIZE(item) ((rt_uint32_t)item->next - (rt_uint32_t)item - RT_MEMHEAP_SIZE)
#ifndef LWP_MEM_PAGE_SIZE
#define LWP_MEM_PAGE_SIZE (4 * 1024)
#endif
#ifndef LWP_MEM_MAX_PAGE_COUNT
#define LWP_MEM_MAX_PAGE_COUNT (256 * 4)
#endif
static void *rt_lwp_malloc_page(struct rt_lwp *lwp, rt_size_t npages)
{
void *chunk;
char name[6];
struct rt_lwp_memheap *lwp_heap;
rt_size_t page_cnt;
RT_ASSERT(lwp != RT_NULL);
page_cnt = lwp->heap_cnt + npages;
if (page_cnt > LWP_MEM_MAX_PAGE_COUNT)
{
dbg_log(DBG_ERROR, "alloc new page failed, lwp memory size out of limited: %d\n", page_cnt);
return RT_NULL;
}
lwp_heap = rt_malloc(sizeof(struct rt_lwp_memheap));
if (lwp_heap == RT_NULL)
{
dbg_log(DBG_ERROR, "alloc new page head failed, out of memory : %d\n", page_cnt);
return RT_NULL;
}
chunk = rt_malloc(npages * LWP_MEM_PAGE_SIZE);
if (chunk == RT_NULL)
{
dbg_log(DBG_ERROR, "alloc new page buffer failed, out of memory : %d\n", page_cnt);
rt_free(lwp_heap);
return RT_NULL;
}
dbg_log(DBG_LOG, "lwp alloc page: %d\n", npages);
sprintf(name, "lwp%02x", lwp->heap_cnt);
rt_lwp_memheap_init(lwp_heap, name, chunk, npages * LWP_MEM_PAGE_SIZE);
rt_list_insert_before(&lwp->hlist, &lwp_heap->mlist);
lwp->heap_cnt += npages;
return chunk;
}
static void rt_lwp_free_page(struct rt_lwp *lwp, struct rt_lwp_memheap *lwp_heap)
{
rt_size_t npages;
RT_ASSERT(lwp != RT_NULL);
RT_ASSERT(lwp_heap != RT_NULL);
RT_ASSERT(lwp_heap->start_addr != RT_NULL);
npages = lwp_heap->pool_size / LWP_MEM_PAGE_SIZE;
lwp->heap_cnt -= npages;
dbg_log(DBG_LOG, "lwp free page: %d\n", npages);
rt_list_remove(&lwp_heap->mlist);
rt_free(lwp_heap->start_addr);
rt_free(lwp_heap);
}
void rt_lwp_mem_init(struct rt_lwp *lwp)
{
RT_ASSERT(lwp != RT_NULL);
rt_list_init(&lwp->hlist);
}
void rt_lwp_mem_deinit(struct rt_lwp *lwp)
{
struct rt_list_node *node;
RT_ASSERT(lwp != RT_NULL);
for (node = lwp->hlist.next; node != &(lwp->hlist); node = node->next)
{
struct rt_lwp_memheap *lwp_heap;
lwp_heap = rt_list_entry(node, struct rt_lwp_memheap, mlist);
RT_ASSERT(lwp_heap != RT_NULL);
rt_lwp_free_page(lwp, lwp_heap);
}
}
void *rt_lwp_mem_malloc(rt_uint32_t size)
{
struct rt_lwp *lwp;
struct rt_list_node *node;
void *addr = RT_NULL;
rt_uint32_t npages;
if (size == 0)
return RT_NULL;
lwp = rt_lwp_self();
RT_ASSERT(lwp != RT_NULL);
for (node = lwp->hlist.next; node != &(lwp->hlist); node = node->next)
{
struct rt_lwp_memheap *lwp_heap;
lwp_heap = rt_list_entry(node, struct rt_lwp_memheap, mlist);
addr = rt_lwp_memheap_alloc(lwp_heap, size);
if (addr != RT_NULL)
{
dbg_log(DBG_LOG, "lwp alloc 0x%x/%d\n", addr, size);
return addr;
}
}
npages = (size + rt_lwp_memheap_unavailable_size_get() + LWP_MEM_PAGE_SIZE) / LWP_MEM_PAGE_SIZE;
if (RT_NULL != rt_lwp_malloc_page(lwp, npages))
return rt_lwp_mem_malloc(size);
else
return RT_NULL;
}
void rt_lwp_mem_free(void *addr)
{
struct rt_lwp_memheap_item *header_ptr;
struct rt_lwp_memheap *lwp_heap;
if (addr == RT_NULL)
return ;
/* get memory item */
header_ptr = (struct rt_lwp_memheap_item *)((rt_uint8_t *)addr - RT_MEMHEAP_SIZE);
RT_ASSERT(header_ptr);
lwp_heap = header_ptr->pool_ptr;
RT_ASSERT(lwp_heap);
dbg_log(DBG_LOG, "lwp free 0x%x\n", addr);
rt_lwp_memheap_free((void *)addr);
if (rt_lwp_memheap_is_empty(lwp_heap))
{
rt_lwp_free_page(rt_lwp_self(), lwp_heap);
}
}
void *rt_lwp_mem_realloc(void *rmem, rt_size_t newsize)
{
void *new_ptr;
struct rt_lwp_memheap_item *header_ptr;
if (rmem == RT_NULL)
return rt_lwp_mem_malloc(newsize);
if (newsize == 0)
{
rt_lwp_mem_free(rmem);
return RT_NULL;
}
/* get old memory item */
header_ptr = (struct rt_lwp_memheap_item *)
((rt_uint8_t *)rmem - RT_MEMHEAP_SIZE);
new_ptr = rt_lwp_memheap_realloc(header_ptr->pool_ptr, rmem, newsize);
if (new_ptr == RT_NULL)
{
/* allocate memory block from other memheap */
new_ptr = rt_lwp_mem_malloc(newsize);
if (new_ptr != RT_NULL && rmem != RT_NULL)
{
rt_size_t oldsize;
/* get the size of old memory block */
oldsize = MEMITEM_SIZE(header_ptr);
if (newsize > oldsize)
rt_memcpy(new_ptr, rmem, oldsize);
else
rt_memcpy(new_ptr, rmem, newsize);
dbg_log(DBG_LOG, "lwp realloc with memcpy 0x%x -> 0x%x/%d\n", rmem, new_ptr, newsize);
rt_lwp_mem_free(rmem);
}
}
dbg_log(DBG_LOG, "lwp realloc in same address 0x%x/%d\n", rmem, newsize);
return new_ptr;
}
/*
* File : lwp_mem.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
#ifndef __LWP_MEM_H__
#define __LWP_MEM_H__
extern void rt_lwp_mem_init(struct rt_lwp *lwp);
extern void rt_lwp_mem_deinit(struct rt_lwp *lwp);
extern void *rt_lwp_mem_malloc(rt_uint32_t size);
extern void rt_lwp_mem_free(void *addr);
extern void *rt_lwp_mem_realloc(void *rmem, rt_size_t newsize);
#endif
此差异已折叠。
/*
* File : lwp_memheap.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
#ifndef __LWP_MEMHEAP_H__
#define __LWP_MEMHEAP_H__
#include <stdint.h>
#include <rtthread.h>
/**
* memory item on the heap
*/
struct rt_lwp_memheap_item
{
rt_uint32_t magic; /**< magic number for memheap */
struct rt_lwp_memheap *pool_ptr; /**< point of pool */
struct rt_lwp_memheap_item *next; /**< next memheap item */
struct rt_lwp_memheap_item *prev; /**< prev memheap item */
struct rt_lwp_memheap_item *next_free; /**< next free memheap item */
struct rt_lwp_memheap_item *prev_free; /**< prev free memheap item */
};
/**
* Base structure of memory heap object
*/
struct rt_lwp_memheap
{
struct rt_object parent; /**< inherit from rt_object */
void *start_addr; /**< pool start address and size */
rt_uint32_t pool_size; /**< pool size */
rt_uint32_t available_size; /**< available size */
rt_uint32_t max_used_size; /**< maximum allocated size */
struct rt_lwp_memheap_item *block_list; /**< used block list */
struct rt_lwp_memheap_item *free_list; /**< free block list */
struct rt_lwp_memheap_item free_header; /**< free block list header */
struct rt_semaphore lock; /**< semaphore lock */
rt_list_t mlist;
};
extern rt_err_t rt_lwp_memheap_init(struct rt_lwp_memheap *memheap, const char *name, void *start_addr, rt_uint32_t size);
extern void *rt_lwp_memheap_alloc(struct rt_lwp_memheap *heap, rt_uint32_t size);
extern void rt_lwp_memheap_free(void *ptr);
extern void *rt_lwp_memheap_realloc(struct rt_lwp_memheap *heap, void *ptr, rt_size_t newsize);
extern rt_bool_t rt_lwp_memheap_is_empty(struct rt_lwp_memheap *memheap);
extern rt_bool_t rt_lwp_memheap_unavailable_size_get(void);
#endif
/*
* File : lwp_syscall.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
/* RT-Thread System call */
#include <lwp.h>
#include <lwp_mem.h>
#include <lwp_syscall.h>
#define DBG_ENABLE
#define DBG_SECTION_NAME "[LWP_CALL]"
#define DBG_COLOR
#define DBG_LEVEL DBG_WARNING
#include <rtdbg.h>
/* thread/process */
void sys_exit(int value)
{
/* TODO: handle the return_value */
dbg_log(DBG_LOG, "enter sys_exit\n");
rt_thread_delete(rt_thread_self());
rt_schedule();
return;
}
/* syscall: "read" ret: "ssize_t" args: "int" "void *" "size_t" */
ssize_t sys_read(int fd, void *buf, size_t nbyte)
{
return read(fd, buf, nbyte);
}
/* syscall: "write" ret: "ssize_t" args: "int" "const void *" "size_t" */
ssize_t sys_write(int fd, const void *buf, size_t nbyte)
{
return write(fd, buf, nbyte);
}
/* syscall: "lseek" ret: "off_t" args: "int" "off_t" "int" */
off_t sys_lseek(int fd, off_t offset, int whence)
{
return lseek(fd, offset, whence);
}
/* syscall: "open" ret: "int" args: "const char *" "int" "..." */
int sys_open(const char *name, int mode, ...)
{
return open(name, mode, 0);
}
/* syscall: "close" ret: "int" args: "int" */
int sys_close(int fd)
{
return close(fd);
}
/* syscall: "ioctl" ret: "int" args: "int" "u_long" "..." */
int sys_ioctl(int fd, unsigned long cmd, void* data)
{
return ioctl(fd, cmd, data);
}
/* syscall: "nanosleep" ret: "int" args: "const struct timespec *" "struct timespec *" */
int sys_nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
rt_tick_t tick;
dbg_log(DBG_LOG, "sys_nanosleep\n");
tick = rqtp->tv_sec * RT_TICK_PER_SECOND + (rqtp->tv_nsec * RT_TICK_PER_SECOND)/ 1000000000;
rt_thread_delay(tick);
if (rmtp)
{
tick = rt_tick_get() - tick;
/* get the passed time */
rmtp->tv_sec = tick/RT_TICK_PER_SECOND;
rmtp->tv_nsec = (tick%RT_TICK_PER_SECOND) * (1000000000/RT_TICK_PER_SECOND);
}
return 0;
}
/* syscall: "getpriority" ret: "int" args: "int" "id_t" */
int sys_getpriority(int which, id_t who)
{
if (which == PRIO_PROCESS)
{
rt_thread_t tid;
tid = rt_thread_self();
if (who == (id_t)tid || who == 0xff)
{
return tid->current_priority;
}
}
return 0xff;
}
/* syscall: "setpriority" ret: "int" args: "int" "id_t" "int" */
int sys_setpriority(int which, id_t who, int prio)
{
if (which == PRIO_PROCESS)
{
rt_thread_t tid;
tid = rt_thread_self();
if ((who == (id_t)tid || who == 0xff) && (prio >= 0 && prio < RT_THREAD_PRIORITY_MAX))
{
rt_thread_control(tid, RT_THREAD_CTRL_CHANGE_PRIORITY, &prio);
return 0;
}
}
return -1;
}
/* syscall: "gettimeofday" ret: "int" args: "struct timeval *" "struct timezone *" */
int sys_gettimeofday(struct timeval *tp, struct timezone *tzp)
{
if (tp)
{
tp->tv_sec = rt_tick_get() / RT_TICK_PER_SECOND;
tp->tv_usec = (rt_tick_get() % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND);
}
return 0;
}
/* syscall: "settimeofday" ret: "int" args: "const struct timeval *" "const struct timezone *" */
int sys_settimeofday(const struct timeval *tv, const struct timezone *tzp)
{
return 0;
}
/* syscall: "msgget" ret: "int" args: "key_t" "int" */
int sys_msgget(key_t key, int msgflg)
{
return -1;
}
/* syscall: "msgsnd" ret: "int" args: "int" "const void *" "size_t" "int" */
int sys_msgsend(int msqid, const void *msgp, size_t msgsz, int msgflg)
{
return -1;
}
/* syscall: "msgrcv" ret: "int" args: "int" "void *" "size_t" "long" "int" */
int sys_msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
{
return -1;
}
/* syscall: "sys_log" ret: "int" args: "const char*" "size" */
int sys_log(const char* log, int size)
{
rt_device_t console = rt_console_get_device();
if (console) rt_device_write(console, -1, log, size);
return 0;
}
void *sys_malloc(size_t size)
{
return rt_lwp_mem_malloc(size);
}
void sys_free(void *addr)
{
rt_lwp_mem_free(addr);
}
void *sys_realloc(void *rmem, size_t newsize)
{
return rt_lwp_mem_realloc(rmem, newsize);
}
const static void* func_table[] =
{
(void *)sys_exit, // 0x01
(void *)sys_read, // 0x02
(void *)sys_write, // 0x03
(void *)sys_lseek, // 0x04
(void *)sys_open, // 0x05
(void *)sys_close, // 0x06
(void *)sys_ioctl, // 0x07
(void *)sys_nanosleep, // 0x08
(void *)sys_getpriority, // 0x09
(void *)sys_setpriority, // 0x0a
(void *)sys_gettimeofday, // 0x0b
(void *)sys_settimeofday, // 0x0c
(void *)sys_malloc, // 0x0d
(void *)sys_free, // 0x0e
};
const void *lwp_get_sys_api(rt_uint32_t number)
{
const void *func = RT_NULL;
if (number == 0xff)
{
func = (void *)sys_log;
}
else
{
number -= 1;
if (number < sizeof(func_table)/sizeof(func_table[0]))
{
func = func_table[number];
}
}
return func;
}
/*
* File : lwp_syscall.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2006-03-18 Bernard the first version
* 2006-04-25 Bernard add rt_hw_context_switch_interrupt declaration
* 2006-09-24 Bernard add rt_hw_context_switch_to declaration
* 2012-12-29 Bernard add rt_hw_exception_install declaration
* 2017-10-17 Hichard add some micros
*/
#ifndef __LWP_SYSCALL_H__
#define __LWP_SYSCALL_H__
#include <stdint.h>
#include <rtthread.h>
#include <dfs_posix.h>
#include <sys/time.h>
#include <sys/types.h>
typedef long suseconds_t; /* microseconds (signed) */
typedef long key_t; /* IPC key (for Sys V IPC) */
typedef uint32_t id_t; /* may contain pid, uid or gid */
/*
* Process priority specifications to get/setpriority.
*/
#define PRIO_MIN (-20)
#define PRIO_MAX 20
#define PRIO_PROCESS 0 /* only support lwp process */
#define PRIO_PGRP 1
#define PRIO_USER 2
#define TIMEVAL_TO_TIMESPEC(tv, ts) { \
(ts)->tv_sec = (tv)->tv_sec; \
(ts)->tv_nsec = (tv)->tv_usec * 1000; \
}
#define TIMESPEC_TO_TIMEVAL(tv, ts) { \
(tv)->tv_sec = (ts)->tv_sec; \
(tv)->tv_usec = (ts)->tv_nsec / 1000; \
}
void sys_exit(int value);
ssize_t sys_read(int fd, void *buf, size_t nbyte);
ssize_t sys_write(int fd, const void *buf, size_t nbyte);
off_t sys_lseek(int fd, off_t offset, int whence);
int sys_open(const char *name, int mode, ...);
int sys_close(int fd);
int sys_nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
int sys_getpriority(int which, id_t who);
int sys_setpriority(int which, id_t who, int prio);
int sys_gettimeofday(struct timeval *tp, struct timezone *tzp);
int sys_settimeofday(const struct timeval *tv, const struct timezone *tzp);
int sys_msgget(key_t key, int msgflg);
int sys_msgsend(int msqid, const void *msgp, size_t msgsz, int msgflg);
int sys_msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
int sys_log(const char* log, int size);
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册