2018-12-10 16:31:33 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2018-12-10 16:31:33 +08:00
|
|
|
*
|
2018-12-10 18:23:21 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-12-10 16:31:33 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2018-12-10 18:23:21 +08:00
|
|
|
* 2006-03-12 Bernard first version
|
|
|
|
* 2018-11-02 heyuanjie fix complie error in iar
|
2022-12-03 12:07:44 +08:00
|
|
|
* 2021-02-03 lizhirui add 64-bit arch support and riscv64 arch support
|
|
|
|
* 2021-08-26 linzhenxing add lwp_setcwd\lwp_getcwd
|
2023-04-22 23:59:11 +08:00
|
|
|
* 2023-02-20 wangxiaoyao inv icache before new app startup
|
2023-06-28 09:11:17 +08:00
|
|
|
* 2023-02-20 wangxiaoyao fix bug on foreground app switch
|
2023-10-21 20:14:45 +08:00
|
|
|
* 2023-10-16 Shell Support a new backtrace framework
|
2024-03-28 23:42:56 +08:00
|
|
|
* 2023-11-17 xqyjlj add process group and session support
|
|
|
|
* 2023-11-30 Shell add lwp_startup()
|
2018-12-10 16:31:33 +08:00
|
|
|
*/
|
|
|
|
|
2023-10-25 20:31:25 +08:00
|
|
|
#define DBG_TAG "lwp"
|
2024-05-17 13:19:00 +08:00
|
|
|
#define DBG_LVL DBG_INFO
|
2023-07-20 00:02:41 +08:00
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2018-12-10 16:31:33 +08:00
|
|
|
#include <rthw.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
|
2022-01-11 15:20:14 +08:00
|
|
|
#include <dfs_file.h>
|
|
|
|
#include <unistd.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <stdio.h> /* rename() */
|
|
|
|
#include <fcntl.h>
|
2022-01-11 15:20:14 +08:00
|
|
|
#include <sys/stat.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <sys/statfs.h> /* statfs() */
|
|
|
|
|
|
|
|
#include <lwp_elf.h>
|
2018-12-10 16:31:33 +08:00
|
|
|
|
|
|
|
#ifndef RT_USING_DFS
|
2022-12-03 12:07:44 +08:00
|
|
|
#error "lwp need file system(RT_USING_DFS)"
|
2018-12-10 16:31:33 +08:00
|
|
|
#endif
|
|
|
|
|
2023-10-25 20:31:25 +08:00
|
|
|
#include "lwp_internal.h"
|
2022-12-03 12:07:44 +08:00
|
|
|
#include "lwp_arch.h"
|
|
|
|
#include "lwp_arch_comm.h"
|
2023-07-20 00:02:41 +08:00
|
|
|
#include "lwp_signal.h"
|
|
|
|
#include "lwp_dbg.h"
|
2024-03-28 23:42:56 +08:00
|
|
|
#include <terminal/terminal.h>
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <lwp_user_mm.h>
|
2022-12-16 18:38:28 +08:00
|
|
|
#endif /* end of ARCH_MM_MMU */
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef O_DIRECTORY
|
|
|
|
#define O_DIRECTORY 0x200000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0x10000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static const char elf_magic[] = {0x7f, 'E', 'L', 'F'};
|
|
|
|
#ifdef DFS_USING_WORKDIR
|
|
|
|
extern char working_directory[];
|
|
|
|
#endif
|
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
static int lwp_component_init(void)
|
2023-10-25 20:31:25 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
if ((rc = lwp_tid_init()) != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_E("%s: lwp_component_init() failed", __func__);
|
|
|
|
}
|
|
|
|
else if ((rc = lwp_pid_init()) != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_E("%s: lwp_pid_init() failed", __func__);
|
|
|
|
}
|
2023-11-10 21:37:07 +08:00
|
|
|
else if ((rc = rt_channel_component_init()) != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_E("%s: rt_channel_component_init failed", __func__);
|
|
|
|
}
|
2024-03-28 23:42:56 +08:00
|
|
|
else if ((rc = lwp_futex_init()) != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_E("%s: lwp_futex_init() failed", __func__);
|
|
|
|
}
|
2023-10-25 20:31:25 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
INIT_COMPONENT_EXPORT(lwp_component_init);
|
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_weak int lwp_startup_debug_request(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LATENCY_TIMES (3)
|
|
|
|
#define LATENCY_IN_MSEC (128)
|
|
|
|
#define LWP_CONSOLE_PATH "CONSOLE=/dev/console"
|
|
|
|
const char *init_search_path[] = {
|
|
|
|
"/sbin/init",
|
|
|
|
"/bin/init",
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Startup process 0 and do the essential works
|
|
|
|
* This is the "Hello World" point of RT-Smart
|
|
|
|
*/
|
|
|
|
static int lwp_startup(void)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
const char *init_path;
|
|
|
|
char *argv[] = {0, "&"};
|
|
|
|
char *envp[] = {LWP_CONSOLE_PATH, 0};
|
|
|
|
|
2024-04-09 21:36:16 +08:00
|
|
|
#ifdef LWP_DEBUG_INIT
|
2024-03-28 23:42:56 +08:00
|
|
|
int command;
|
|
|
|
int countdown = LATENCY_TIMES;
|
|
|
|
while (countdown)
|
|
|
|
{
|
|
|
|
command = lwp_startup_debug_request();
|
|
|
|
if (command)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rt_kprintf("Press any key to stop init process startup ... %d\n", countdown);
|
|
|
|
countdown -= 1;
|
|
|
|
rt_thread_mdelay(LATENCY_IN_MSEC);
|
|
|
|
}
|
|
|
|
rt_kprintf("Starting init ...\n");
|
2024-04-09 21:36:16 +08:00
|
|
|
#endif /* LWP_DEBUG_INIT */
|
2024-03-28 23:42:56 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(init_search_path)/sizeof(init_search_path[0]); i++)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
init_path = init_search_path[i];
|
|
|
|
error = stat(init_path, &s);
|
|
|
|
if (error == 0)
|
|
|
|
{
|
|
|
|
argv[0] = (void *)init_path;
|
|
|
|
error = lwp_execve((void *)init_path, 0, sizeof(argv)/sizeof(argv[0]), argv, envp);
|
|
|
|
if (error < 0)
|
|
|
|
{
|
|
|
|
LOG_E("%s: failed to startup process 0 (init)\n"
|
|
|
|
"Switching to legacy mode...", __func__);
|
|
|
|
}
|
|
|
|
else if (error != 1)
|
|
|
|
{
|
|
|
|
LOG_E("%s: pid 1 is already allocated", __func__);
|
|
|
|
error = -EBUSY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_lwp_t p = lwp_from_pid_locked(1);
|
2024-04-15 18:24:19 +08:00
|
|
|
p->sig_protected = 1;
|
2024-03-28 23:42:56 +08:00
|
|
|
|
|
|
|
error = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
2024-04-15 18:24:19 +08:00
|
|
|
LOG_D("%s: init program not found\n"
|
2024-03-28 23:42:56 +08:00
|
|
|
"Switching to legacy mode...", __func__);
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
INIT_APP_EXPORT(lwp_startup);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
void lwp_setcwd(char *buf)
|
|
|
|
{
|
|
|
|
struct rt_lwp *lwp = RT_NULL;
|
|
|
|
|
|
|
|
if(strlen(buf) >= DFS_PATH_MAX)
|
|
|
|
{
|
|
|
|
rt_kprintf("buf too long!\n");
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwp = (struct rt_lwp *)rt_thread_self()->lwp;
|
|
|
|
if (lwp)
|
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_strncpy(lwp->working_directory, buf, DFS_PATH_MAX - 1);
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_strncpy(working_directory, buf, DFS_PATH_MAX - 1);
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *lwp_getcwd(void)
|
|
|
|
{
|
|
|
|
char *dir_buf = RT_NULL;
|
|
|
|
struct rt_lwp *lwp = RT_NULL;
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_thread_t thread = rt_thread_self();
|
|
|
|
|
|
|
|
if (thread)
|
|
|
|
{
|
|
|
|
lwp = (struct rt_lwp *)thread->lwp;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
if (lwp)
|
|
|
|
{
|
|
|
|
if(lwp->working_directory[0] != '/')
|
|
|
|
{
|
|
|
|
dir_buf = &working_directory[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dir_buf = &lwp->working_directory[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dir_buf = &working_directory[0];
|
|
|
|
|
|
|
|
return dir_buf;
|
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RT-Thread light-weight process
|
|
|
|
*/
|
|
|
|
void lwp_set_kernel_sp(uint32_t *sp)
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_thread_self()->kernel_sp = (rt_uint32_t *)sp;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t *lwp_get_kernel_sp(void)
|
|
|
|
{
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
return (uint32_t *)rt_thread_self()->sp;
|
|
|
|
#else
|
|
|
|
uint32_t* kernel_sp;
|
|
|
|
extern rt_uint32_t rt_interrupt_from_thread;
|
|
|
|
extern rt_uint32_t rt_thread_switch_interrupt_flag;
|
|
|
|
if (rt_thread_switch_interrupt_flag)
|
|
|
|
{
|
|
|
|
kernel_sp = (uint32_t *)((rt_thread_t)rt_container_of(rt_interrupt_from_thread, struct rt_thread, sp))->kernel_sp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
kernel_sp = (uint32_t *)rt_thread_self()->kernel_sp;
|
|
|
|
}
|
|
|
|
return kernel_sp;
|
|
|
|
#endif
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
struct process_aux *lwp_argscopy(struct rt_lwp *lwp, int argc, char **argv, char **envp)
|
|
|
|
{
|
|
|
|
int size = sizeof(size_t) * 5; /* store argc, argv, envp, aux, NULL */
|
|
|
|
int *args;
|
|
|
|
char *str;
|
|
|
|
char *str_k;
|
|
|
|
char **new_argve;
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
size_t *args_k;
|
|
|
|
struct process_aux *aux;
|
2023-10-17 13:07:59 +08:00
|
|
|
size_t prot = PROT_READ | PROT_WRITE;
|
|
|
|
size_t flags = MAP_FIXED | MAP_PRIVATE;
|
|
|
|
size_t zero = 0;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
size += (rt_strlen(argv[i]) + 1);
|
|
|
|
}
|
|
|
|
size += (sizeof(size_t) * argc);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
if (envp)
|
|
|
|
{
|
|
|
|
while (envp[i] != 0)
|
|
|
|
{
|
|
|
|
size += (rt_strlen(envp[i]) + 1);
|
|
|
|
size += sizeof(size_t);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for aux */
|
|
|
|
size += sizeof(struct process_aux);
|
|
|
|
|
|
|
|
if (size > ARCH_PAGE_SIZE)
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
args = lwp_mmap2(lwp, (void *)(USER_STACK_VEND), size, prot, flags, -1, 0);
|
|
|
|
if (args == RT_NULL || lwp_data_put(lwp, args, &zero, sizeof(zero)) != sizeof(zero))
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
args_k = (size_t *)lwp_v2p(lwp, args);
|
2022-12-03 12:07:44 +08:00
|
|
|
args_k = (size_t *)((size_t)args_k - PV_OFFSET);
|
|
|
|
|
|
|
|
/* argc, argv[], 0, envp[], 0 , aux[] */
|
|
|
|
str = (char *)((size_t)args + (argc + 2 + i + 1 + AUX_ARRAY_ITEMS_NR * 2 + 1) * sizeof(size_t));
|
|
|
|
str_k = (char *)((size_t)args_k + (argc + 2 + i + 1 + AUX_ARRAY_ITEMS_NR * 2 + 1) * sizeof(size_t));
|
|
|
|
|
|
|
|
new_argve = (char **)&args_k[1];
|
|
|
|
args_k[0] = argc;
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
len = rt_strlen(argv[i]) + 1;
|
|
|
|
new_argve[i] = str;
|
2023-09-15 15:57:08 +08:00
|
|
|
lwp_memcpy(str_k, argv[i], len);
|
2022-12-03 12:07:44 +08:00
|
|
|
str += len;
|
|
|
|
str_k += len;
|
|
|
|
}
|
|
|
|
new_argve[i] = 0;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
new_argve[i] = 0;
|
|
|
|
if (envp)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = 0; envp[j] != 0; j++)
|
|
|
|
{
|
|
|
|
len = rt_strlen(envp[j]) + 1;
|
|
|
|
new_argve[i] = str;
|
2023-09-15 15:57:08 +08:00
|
|
|
lwp_memcpy(str_k, envp[j], len);
|
2022-12-03 12:07:44 +08:00
|
|
|
str += len;
|
|
|
|
str_k += len;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
new_argve[i] = 0;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
|
|
|
|
/* aux */
|
|
|
|
aux = (struct process_aux *)(new_argve + i);
|
|
|
|
aux->item[0].key = AT_EXECFN;
|
|
|
|
aux->item[0].value = (size_t)(size_t)new_argve[0];
|
|
|
|
i += AUX_ARRAY_ITEMS_NR * 2;
|
|
|
|
new_argve[i] = 0;
|
|
|
|
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, args_k, size);
|
|
|
|
|
|
|
|
lwp->args = args;
|
|
|
|
|
|
|
|
return aux;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static struct process_aux *lwp_argscopy(struct rt_lwp *lwp, int argc, char **argv, char **envp)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
|
|
|
int size = sizeof(int) * 5; /* store argc, argv, envp, aux, NULL */
|
|
|
|
struct process_aux *aux;
|
|
|
|
#else
|
|
|
|
int size = sizeof(int) * 4; /* store argc, argv, envp, NULL */
|
|
|
|
#endif /* ARCH_MM_MMU */
|
2018-12-10 16:31:33 +08:00
|
|
|
int *args;
|
|
|
|
char *str;
|
2022-12-03 12:07:44 +08:00
|
|
|
char **new_argve;
|
2018-12-10 16:31:33 +08:00
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
for (i = 0; i < argc; i++)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
|
|
|
size += (rt_strlen(argv[i]) + 1);
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
size += (sizeof(int) * argc);
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
i = 0;
|
|
|
|
if (envp)
|
|
|
|
{
|
|
|
|
while (envp[i] != 0)
|
|
|
|
{
|
|
|
|
size += (rt_strlen(envp[i]) + 1);
|
|
|
|
size += sizeof(int);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ARCH_MM_MMU
|
|
|
|
/* for aux */
|
|
|
|
size += sizeof(struct process_aux);
|
|
|
|
|
|
|
|
args = (int *)rt_malloc(size);
|
2018-12-10 16:31:33 +08:00
|
|
|
if (args == RT_NULL)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* argc, argv[], 0, envp[], 0 */
|
|
|
|
str = (char *)((size_t)args + (argc + 2 + i + 1 + AUX_ARRAY_ITEMS_NR * 2 + 1) * sizeof(int));
|
|
|
|
#else
|
|
|
|
args = (int *)rt_malloc(size);
|
|
|
|
if (args == RT_NULL)
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
str = (char*)((int)args + (argc + 2 + i + 1) * sizeof(int));
|
|
|
|
#endif /* ARCH_MM_MMU */
|
|
|
|
|
|
|
|
new_argve = (char **)&args[1];
|
2018-12-10 16:31:33 +08:00
|
|
|
args[0] = argc;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
for (i = 0; i < argc; i++)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
|
|
|
len = rt_strlen(argv[i]) + 1;
|
2022-12-03 12:07:44 +08:00
|
|
|
new_argve[i] = str;
|
2023-09-15 15:57:08 +08:00
|
|
|
lwp_memcpy(str, argv[i], len);
|
2018-12-10 16:31:33 +08:00
|
|
|
str += len;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
new_argve[i] = 0;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
new_argve[i] = 0;
|
|
|
|
if (envp)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
for (j = 0; envp[j] != 0; j++)
|
|
|
|
{
|
|
|
|
len = rt_strlen(envp[j]) + 1;
|
|
|
|
new_argve[i] = str;
|
2023-09-15 15:57:08 +08:00
|
|
|
lwp_memcpy(str, envp[j], len);
|
2022-12-03 12:07:44 +08:00
|
|
|
str += len;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
new_argve[i] = 0;
|
|
|
|
}
|
|
|
|
#ifdef ARCH_MM_MMU
|
|
|
|
/* aux */
|
|
|
|
aux = (struct process_aux *)(new_argve + i);
|
|
|
|
aux->item[0].key = AT_EXECFN;
|
|
|
|
aux->item[0].value = (uint32_t)(size_t)new_argve[0];
|
|
|
|
i += AUX_ARRAY_ITEMS_NR * 2;
|
|
|
|
new_argve[i] = 0;
|
|
|
|
|
2018-12-10 16:31:33 +08:00
|
|
|
lwp->args = args;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return aux;
|
|
|
|
#else
|
|
|
|
lwp->args = args;
|
|
|
|
lwp->args_length = size;
|
|
|
|
|
|
|
|
return (struct process_aux *)(new_argve + i);
|
|
|
|
#endif /* ARCH_MM_MMU */
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ARCH_MM_MMU
|
|
|
|
#define check_off(voff, vlen) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (voff > vlen) \
|
|
|
|
{ \
|
|
|
|
result = -RT_ERROR; \
|
|
|
|
goto _exit; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define check_read(vrlen, vrlen_want) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (vrlen < vrlen_want) \
|
|
|
|
{ \
|
|
|
|
result = -RT_ERROR; \
|
|
|
|
goto _exit; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
static size_t load_fread(void *ptr, size_t size, size_t nmemb, int fd)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
size_t read_block = 0;
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
while (nmemb)
|
|
|
|
{
|
|
|
|
size_t count;
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
count = read(fd, ptr, size * nmemb) / size;
|
|
|
|
if (count < nmemb)
|
|
|
|
{
|
|
|
|
LOG_E("ERROR: file size error!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = (void *)((uint8_t *)ptr + (count * size));
|
|
|
|
nmemb -= count;
|
|
|
|
read_block += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return read_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
Elf_Word st_name;
|
|
|
|
Elf_Addr st_value;
|
|
|
|
Elf_Word st_size;
|
|
|
|
unsigned char st_info;
|
|
|
|
unsigned char st_other;
|
|
|
|
Elf_Half st_shndx;
|
|
|
|
} Elf_sym;
|
|
|
|
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
struct map_range
|
|
|
|
{
|
|
|
|
void *start;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void expand_map_range(struct map_range *m, void *start, size_t size)
|
|
|
|
{
|
|
|
|
if (!m->start)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
m->start = start;
|
|
|
|
m->size = size;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
void *end = (void *)((char*)start + size);
|
|
|
|
void *mend = (void *)((char*)m->start + m->size);
|
|
|
|
|
|
|
|
if (m->start > start)
|
|
|
|
{
|
|
|
|
m->start = start;
|
|
|
|
}
|
|
|
|
if (mend < end)
|
|
|
|
{
|
|
|
|
mend = end;
|
|
|
|
}
|
|
|
|
m->size = (char *)mend - (char *)m->start;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
static int map_range_ckeck(struct map_range *m1, struct map_range *m2)
|
|
|
|
{
|
|
|
|
void *m1_start = (void *)((size_t)m1->start & ~ARCH_PAGE_MASK);
|
|
|
|
void *m1_end = (void *)((((size_t)m1->start + m1->size) + ARCH_PAGE_MASK) & ~ARCH_PAGE_MASK);
|
|
|
|
void *m2_start = (void *)((size_t)m2->start & ~ARCH_PAGE_MASK);
|
|
|
|
void *m2_end = (void *)((((size_t)m2->start + m2->size) + ARCH_PAGE_MASK) & ~ARCH_PAGE_MASK);
|
|
|
|
|
|
|
|
if (m1->size)
|
|
|
|
{
|
|
|
|
if (m1_start < (void *)USER_LOAD_VADDR)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (m1_start > (void *)USER_STACK_VSTART)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (m1_end < (void *)USER_LOAD_VADDR)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (m1_end > (void *)USER_STACK_VSTART)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m2->size)
|
|
|
|
{
|
|
|
|
if (m2_start < (void *)USER_LOAD_VADDR)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (m2_start > (void *)USER_STACK_VSTART)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (m2_end < (void *)USER_LOAD_VADDR)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (m2_end > (void *)USER_STACK_VSTART)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((m1->size != 0) && (m2->size != 0))
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
if (m1_start < m2_start)
|
|
|
|
{
|
|
|
|
if (m1_end > m2_start)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* m2_start <= m1_start */
|
|
|
|
{
|
|
|
|
if (m2_end > m1_start)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
static int load_elf(int fd, int len, struct rt_lwp *lwp, uint8_t *load_addr, struct process_aux *aux)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t off = 0;
|
|
|
|
size_t load_off = 0;
|
|
|
|
char *p_section_str = 0;
|
|
|
|
Elf_sym *dynsym = 0;
|
|
|
|
Elf_Ehdr eheader;
|
|
|
|
Elf_Phdr pheader;
|
|
|
|
Elf_Shdr sheader;
|
|
|
|
int result = RT_EOK;
|
|
|
|
uint32_t magic;
|
|
|
|
size_t read_len;
|
|
|
|
void *got_start = 0;
|
|
|
|
size_t got_size = 0;
|
|
|
|
void *rel_dyn_start = 0;
|
|
|
|
size_t rel_dyn_size = 0;
|
|
|
|
size_t dynsym_off = 0;
|
|
|
|
size_t dynsym_size = 0;
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
struct map_range user_area[2] = {{NULL, 0}, {NULL, 0}}; /* 0 is text, 1 is data */
|
|
|
|
void *pa, *va;
|
|
|
|
void *va_self;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (len < sizeof eheader)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
LOG_E("len < sizeof eheader!");
|
|
|
|
return -RT_ERROR;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
read_len = load_fread(&magic, 1, sizeof magic, fd);
|
|
|
|
check_read(read_len, sizeof magic);
|
|
|
|
|
|
|
|
if (memcmp(elf_magic, &magic, 4) != 0)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
LOG_E("elf_magic not same, magic:0x%x!", magic);
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
lseek(fd, off, SEEK_SET);
|
|
|
|
read_len = load_fread(&eheader, 1, sizeof eheader, fd);
|
|
|
|
check_read(read_len, sizeof eheader);
|
|
|
|
|
|
|
|
#ifndef ARCH_CPU_64BIT
|
|
|
|
if (eheader.e_ident[4] != 1)
|
|
|
|
{ /* not 32bit */
|
|
|
|
LOG_E("elf not 32bit, %d!", eheader.e_ident[4]);
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (eheader.e_ident[4] != 2)
|
|
|
|
{ /* not 64bit */
|
|
|
|
LOG_E("elf not 64bit, %d!", eheader.e_ident[4]);
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (eheader.e_ident[6] != 1)
|
|
|
|
{ /* ver not 1 */
|
|
|
|
LOG_E("elf Version not 1,ver:%d!", eheader.e_ident[6]);
|
|
|
|
return -RT_ERROR;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if ((eheader.e_type != ET_DYN)
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
&& (eheader.e_type != ET_EXEC)
|
|
|
|
#endif
|
|
|
|
)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
/* not pie or exec elf */
|
|
|
|
LOG_E("elf type not pie or exec, type:%d!", eheader.e_type);
|
|
|
|
return -RT_ERROR;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
off = eheader.e_phoff;
|
|
|
|
for (i = 0; i < eheader.e_phnum; i++, off += sizeof pheader)
|
|
|
|
{
|
|
|
|
check_off(off, len);
|
|
|
|
lseek(fd, off, SEEK_SET);
|
|
|
|
read_len = load_fread(&pheader, 1, sizeof pheader, fd);
|
|
|
|
check_read(read_len, sizeof pheader);
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (pheader.p_type == PT_DYNAMIC)
|
|
|
|
{
|
|
|
|
/* load ld.so */
|
|
|
|
return 1; /* 1 means dynamic */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (eheader.e_entry != 0)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
if ((eheader.e_entry != USER_LOAD_VADDR)
|
|
|
|
&& (eheader.e_entry != LDSO_LOAD_VADDR))
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
/* the entry is invalidate */
|
|
|
|
LOG_E("elf entry is invalidate, entry:0x%x!", eheader.e_entry);
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ /* load aux */
|
|
|
|
uint8_t *process_header;
|
|
|
|
size_t process_header_size;
|
|
|
|
|
|
|
|
off = eheader.e_phoff;
|
|
|
|
process_header_size = eheader.e_phnum * sizeof pheader;
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
if (process_header_size > ARCH_PAGE_SIZE - sizeof(char[16]))
|
|
|
|
{
|
|
|
|
LOG_E("process_header_size too big, size:0x%x!", process_header_size);
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
va = (uint8_t *)lwp_map_user(lwp, (void *)(USER_VADDR_TOP - ARCH_PAGE_SIZE * 2), process_header_size, 0);
|
|
|
|
if (!va)
|
|
|
|
{
|
|
|
|
LOG_E("lwp map user failed!");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
pa = lwp_v2p(lwp, va);
|
2022-12-03 12:07:44 +08:00
|
|
|
process_header = (uint8_t *)pa - PV_OFFSET;
|
|
|
|
#else
|
|
|
|
process_header = (uint8_t *)rt_malloc(process_header_size + sizeof(char[16]));
|
|
|
|
if (!process_header)
|
|
|
|
{
|
|
|
|
LOG_E("process_header malloc failed, size:0x%x!", process_header_size + sizeof(char[16]));
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
check_off(off, len);
|
|
|
|
lseek(fd, off, SEEK_SET);
|
|
|
|
read_len = load_fread(process_header, 1, process_header_size, fd);
|
|
|
|
check_read(read_len, process_header_size);
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, process_header, process_header_size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
aux->item[1].key = AT_PAGESZ;
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
aux->item[1].value = ARCH_PAGE_SIZE;
|
2018-12-10 16:31:33 +08:00
|
|
|
#else
|
2022-12-03 12:07:44 +08:00
|
|
|
aux->item[1].value = RT_MM_PAGE_SIZE;
|
2018-12-10 16:31:33 +08:00
|
|
|
#endif
|
2022-12-03 12:07:44 +08:00
|
|
|
aux->item[2].key = AT_RANDOM;
|
|
|
|
{
|
|
|
|
uint32_t random_value = rt_tick_get();
|
|
|
|
uint8_t *random;
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
uint8_t *krandom;
|
|
|
|
|
|
|
|
random = (uint8_t *)(USER_VADDR_TOP - ARCH_PAGE_SIZE - sizeof(char[16]));
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
krandom = (uint8_t *)lwp_v2p(lwp, random);
|
2022-12-03 12:07:44 +08:00
|
|
|
krandom = (uint8_t *)krandom - PV_OFFSET;
|
|
|
|
rt_memcpy(krandom, &random_value, sizeof random_value);
|
|
|
|
#else
|
|
|
|
random = (uint8_t *)(process_header + process_header_size);
|
|
|
|
rt_memcpy(random, &random_value, sizeof random_value);
|
|
|
|
#endif
|
|
|
|
aux->item[2].value = (size_t)random;
|
|
|
|
}
|
|
|
|
aux->item[3].key = AT_PHDR;
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
aux->item[3].value = (size_t)va;
|
|
|
|
#else
|
|
|
|
aux->item[3].value = (size_t)process_header;
|
|
|
|
#endif
|
|
|
|
aux->item[4].key = AT_PHNUM;
|
|
|
|
aux->item[4].value = eheader.e_phnum;
|
|
|
|
aux->item[5].key = AT_PHENT;
|
|
|
|
aux->item[5].value = sizeof pheader;
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, aux, sizeof *aux);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (load_addr)
|
|
|
|
{
|
|
|
|
load_off = (size_t)load_addr;
|
|
|
|
}
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* map user */
|
|
|
|
off = eheader.e_shoff;
|
|
|
|
for (i = 0; i < eheader.e_shnum; i++, off += sizeof sheader)
|
|
|
|
{
|
|
|
|
check_off(off, len);
|
|
|
|
lseek(fd, off, SEEK_SET);
|
|
|
|
read_len = load_fread(&sheader, 1, sizeof sheader, fd);
|
|
|
|
check_read(read_len, sizeof sheader);
|
|
|
|
|
|
|
|
if ((sheader.sh_flags & SHF_ALLOC) == 0)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
continue;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
switch (sheader.sh_type)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
case SHT_PROGBITS:
|
|
|
|
if ((sheader.sh_flags & SHF_WRITE) == 0)
|
|
|
|
{
|
|
|
|
expand_map_range(&user_area[0], (void *)sheader.sh_addr, sheader.sh_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
expand_map_range(&user_area[1], (void *)sheader.sh_addr, sheader.sh_size);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SHT_NOBITS:
|
|
|
|
expand_map_range(&user_area[1], (void *)sheader.sh_addr, sheader.sh_size);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
expand_map_range(&user_area[1], (void *)sheader.sh_addr, sheader.sh_size);
|
|
|
|
break;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (user_area[0].size == 0)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
/* no code */
|
|
|
|
result = -RT_ERROR;
|
2018-12-10 16:31:33 +08:00
|
|
|
goto _exit;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
if (user_area[0].start == NULL)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
/* DYN */
|
|
|
|
load_off = USER_LOAD_VADDR;
|
|
|
|
user_area[0].start = (void *)((char*)user_area[0].start + load_off);
|
|
|
|
user_area[1].start = (void *)((char*)user_area[1].start + load_off);
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (map_range_ckeck(&user_area[0], &user_area[1]) != 0)
|
|
|
|
{
|
|
|
|
result = -RT_ERROR;
|
|
|
|
goto _exit;
|
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* text and data */
|
|
|
|
for (i = 0; i < 2; i++)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
if (user_area[i].size != 0)
|
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
va = lwp_map_user(lwp, user_area[i].start, user_area[i].size, (i == 0));
|
2022-12-03 12:07:44 +08:00
|
|
|
if (!va || (va != user_area[i].start))
|
|
|
|
{
|
|
|
|
result = -RT_ERROR;
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
lwp->text_size = user_area[0].size;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
#else
|
|
|
|
else
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
size_t start = -1UL;
|
|
|
|
size_t end = 0UL;
|
|
|
|
size_t total_size;
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
off = eheader.e_shoff;
|
|
|
|
for (i = 0; i < eheader.e_shnum; i++, off += sizeof sheader)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
check_off(off, len);
|
|
|
|
lseek(fd, off, SEEK_SET);
|
|
|
|
read_len = load_fread(&sheader, 1, sizeof sheader, fd);
|
|
|
|
check_read(read_len, sizeof sheader);
|
|
|
|
|
|
|
|
if ((sheader.sh_flags & SHF_ALLOC) == 0)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
continue;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
switch (sheader.sh_type)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
case SHT_PROGBITS:
|
|
|
|
case SHT_NOBITS:
|
|
|
|
if (start > sheader.sh_addr)
|
|
|
|
{
|
|
|
|
start = sheader.sh_addr;
|
|
|
|
}
|
|
|
|
if (sheader.sh_addr + sheader.sh_size > end)
|
|
|
|
{
|
|
|
|
end = sheader.sh_addr + sheader.sh_size;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
total_size = end - start;
|
|
|
|
|
|
|
|
#ifdef RT_USING_CACHE
|
|
|
|
load_off = (size_t)rt_malloc_align(total_size, RT_CPU_CACHE_LINE_SZ);
|
|
|
|
#else
|
|
|
|
load_off = (size_t)rt_malloc(total_size);
|
|
|
|
#endif
|
|
|
|
if (load_off == 0)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
LOG_E("alloc text memory faild!");
|
|
|
|
result = -RT_ENOMEM;
|
2018-12-10 16:31:33 +08:00
|
|
|
goto _exit;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_D("lwp text malloc : %p, size: %d!", (void *)load_off, lwp->text_size);
|
|
|
|
}
|
|
|
|
lwp->load_off = load_off; /* for free */
|
|
|
|
lwp->text_size = total_size;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
#endif
|
|
|
|
lwp->text_entry = (void *)(eheader.e_entry + load_off);
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
off = eheader.e_phoff;
|
|
|
|
for (i = 0; i < eheader.e_phnum; i++, off += sizeof pheader)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
check_off(off, len);
|
|
|
|
lseek(fd, off, SEEK_SET);
|
|
|
|
read_len = load_fread(&pheader, 1, sizeof pheader, fd);
|
|
|
|
check_read(read_len, sizeof pheader);
|
|
|
|
|
|
|
|
if (pheader.p_type == PT_LOAD)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
if (pheader.p_filesz > pheader.p_memsz)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
LOG_E("pheader.p_filesz > pheader.p_memsz, p_filesz:0x%x;p_memsz:0x%x!", pheader.p_filesz, pheader.p_memsz);
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
check_off(pheader.p_offset, len);
|
|
|
|
lseek(fd, pheader.p_offset, SEEK_SET);
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
uint32_t size = pheader.p_filesz;
|
|
|
|
size_t tmp_len = 0;
|
|
|
|
|
|
|
|
va = (void *)(pheader.p_vaddr + load_addr);
|
|
|
|
read_len = 0;
|
|
|
|
while (size)
|
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
pa = lwp_v2p(lwp, va);
|
2022-12-03 12:07:44 +08:00
|
|
|
va_self = (void *)((char *)pa - PV_OFFSET);
|
|
|
|
LOG_D("va_self = %p pa = %p", va_self, pa);
|
|
|
|
tmp_len = (size < ARCH_PAGE_SIZE) ? size : ARCH_PAGE_SIZE;
|
|
|
|
tmp_len = load_fread(va_self, 1, tmp_len, fd);
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, va_self, tmp_len);
|
|
|
|
read_len += tmp_len;
|
|
|
|
size -= tmp_len;
|
|
|
|
va = (void *)((char *)va + ARCH_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
read_len = load_fread((void*)(pheader.p_vaddr + load_off), 1, pheader.p_filesz, fd);
|
|
|
|
#endif
|
|
|
|
check_read(read_len, pheader.p_filesz);
|
|
|
|
|
|
|
|
if (pheader.p_filesz < pheader.p_memsz)
|
|
|
|
{
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2022-12-03 12:07:44 +08:00
|
|
|
uint32_t size = pheader.p_memsz - pheader.p_filesz;
|
|
|
|
uint32_t size_s;
|
|
|
|
uint32_t off;
|
|
|
|
|
|
|
|
off = pheader.p_filesz & ARCH_PAGE_MASK;
|
|
|
|
va = (void *)((pheader.p_vaddr + pheader.p_filesz + load_off) & ~ARCH_PAGE_MASK);
|
|
|
|
while (size)
|
|
|
|
{
|
|
|
|
size_s = (size < ARCH_PAGE_SIZE - off) ? size : ARCH_PAGE_SIZE - off;
|
2023-01-09 10:08:55 +08:00
|
|
|
pa = lwp_v2p(lwp, va);
|
2022-12-03 12:07:44 +08:00
|
|
|
va_self = (void *)((char *)pa - PV_OFFSET);
|
|
|
|
memset((void *)((char *)va_self + off), 0, size_s);
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, (void *)((char *)va_self + off), size_s);
|
|
|
|
off = 0;
|
|
|
|
size -= size_s;
|
|
|
|
va = (void *)((char *)va + ARCH_PAGE_SIZE);
|
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
#else
|
2022-12-03 12:07:44 +08:00
|
|
|
memset((uint8_t *)pheader.p_vaddr + pheader.p_filesz + load_off, 0, (size_t)(pheader.p_memsz - pheader.p_filesz));
|
2018-12-10 16:31:33 +08:00
|
|
|
#endif
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* relocate */
|
|
|
|
if (eheader.e_type == ET_DYN)
|
|
|
|
{
|
|
|
|
/* section info */
|
|
|
|
off = eheader.e_shoff;
|
|
|
|
/* find section string table */
|
|
|
|
check_off(off, len);
|
|
|
|
lseek(fd, off + (sizeof sheader) * eheader.e_shstrndx, SEEK_SET);
|
|
|
|
read_len = load_fread(&sheader, 1, sizeof sheader, fd);
|
|
|
|
check_read(read_len, sizeof sheader);
|
|
|
|
|
|
|
|
p_section_str = (char *)rt_malloc(sheader.sh_size);
|
|
|
|
if (!p_section_str)
|
|
|
|
{
|
|
|
|
LOG_E("out of memory!");
|
|
|
|
result = -ENOMEM;
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
check_off(sheader.sh_offset, len);
|
|
|
|
lseek(fd, sheader.sh_offset, SEEK_SET);
|
|
|
|
read_len = load_fread(p_section_str, 1, sheader.sh_size, fd);
|
|
|
|
check_read(read_len, sheader.sh_size);
|
|
|
|
|
|
|
|
check_off(off, len);
|
|
|
|
lseek(fd, off, SEEK_SET);
|
|
|
|
for (i = 0; i < eheader.e_shnum; i++, off += sizeof sheader)
|
|
|
|
{
|
|
|
|
read_len = load_fread(&sheader, 1, sizeof sheader, fd);
|
|
|
|
check_read(read_len, sizeof sheader);
|
|
|
|
|
|
|
|
if (strcmp(p_section_str + sheader.sh_name, ".got") == 0)
|
|
|
|
{
|
|
|
|
got_start = (void *)((uint8_t *)sheader.sh_addr + load_off);
|
|
|
|
got_size = (size_t)sheader.sh_size;
|
|
|
|
}
|
|
|
|
else if (strcmp(p_section_str + sheader.sh_name, ".rel.dyn") == 0)
|
|
|
|
{
|
|
|
|
rel_dyn_start = (void *)((uint8_t *)sheader.sh_addr + load_off);
|
|
|
|
rel_dyn_size = (size_t)sheader.sh_size;
|
|
|
|
}
|
|
|
|
else if (strcmp(p_section_str + sheader.sh_name, ".dynsym") == 0)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
dynsym_off = (size_t)sheader.sh_offset;
|
|
|
|
dynsym_size = (size_t)sheader.sh_size;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
/* reloc */
|
|
|
|
if (dynsym_size)
|
|
|
|
{
|
|
|
|
dynsym = rt_malloc(dynsym_size);
|
|
|
|
if (!dynsym)
|
|
|
|
{
|
|
|
|
LOG_E("ERROR: Malloc error!");
|
|
|
|
result = -ENOMEM;
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
check_off(dynsym_off, len);
|
|
|
|
lseek(fd, dynsym_off, SEEK_SET);
|
|
|
|
read_len = load_fread(dynsym, 1, dynsym_size, fd);
|
|
|
|
check_read(read_len, dynsym_size);
|
|
|
|
}
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2023-01-09 10:08:55 +08:00
|
|
|
arch_elf_reloc(lwp->aspace, (void *)load_off, rel_dyn_start, rel_dyn_size, got_start, got_size, dynsym);
|
2022-12-03 12:07:44 +08:00
|
|
|
#else
|
|
|
|
arch_elf_reloc((void *)load_off, rel_dyn_start, rel_dyn_size, got_start, got_size, dynsym);
|
|
|
|
|
|
|
|
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
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
LOG_D("lwp->text_entry = 0x%p", lwp->text_entry);
|
|
|
|
LOG_D("lwp->text_size = 0x%p", lwp->text_size);
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
_exit:
|
|
|
|
if (dynsym)
|
|
|
|
{
|
|
|
|
rt_free(dynsym);
|
|
|
|
}
|
|
|
|
if (p_section_str)
|
|
|
|
{
|
|
|
|
rt_free(p_section_str);
|
|
|
|
}
|
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_E("lwp load faild, %d", result);
|
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
return result;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
#endif /* ARCH_MM_MMU */
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_weak int lwp_load(const char *filename, struct rt_lwp *lwp, uint8_t *load_addr, size_t addr_size, struct process_aux *aux)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
uint8_t *ptr;
|
|
|
|
int ret = -1;
|
|
|
|
int len;
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
/* check file name */
|
|
|
|
RT_ASSERT(filename != RT_NULL);
|
|
|
|
/* check lwp control block */
|
|
|
|
RT_ASSERT(lwp != RT_NULL);
|
|
|
|
|
|
|
|
/* copy file name to process name */
|
|
|
|
rt_strncpy(lwp->cmd, filename, RT_NAME_MAX);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(filename, O_BINARY | O_RDONLY, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
LOG_E("ERROR: Can't open elf file %s!", filename);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
len = lseek(fd, 0, SEEK_END);
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
LOG_E("ERROR: File %s size error!", filename);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
ret = load_elf(fd, len, lwp, ptr, aux);
|
|
|
|
if ((ret != RT_EOK) && (ret != 1))
|
|
|
|
{
|
|
|
|
LOG_E("lwp load ret = %d", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (fd > 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:31:25 +08:00
|
|
|
/* lwp-thread clean up routine */
|
2022-12-03 12:07:44 +08:00
|
|
|
void lwp_cleanup(struct rt_thread *tid)
|
|
|
|
{
|
2018-12-10 16:31:33 +08:00
|
|
|
struct rt_lwp *lwp;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
if (tid == NULL)
|
|
|
|
{
|
2023-07-20 00:02:41 +08:00
|
|
|
LOG_I("%s: invalid parameter tid == NULL", __func__);
|
2022-12-03 12:07:44 +08:00
|
|
|
return;
|
|
|
|
}
|
2023-07-20 00:02:41 +08:00
|
|
|
else
|
|
|
|
LOG_D("cleanup thread: %s, stack_addr: 0x%x", tid->parent.name, tid->stack_addr);
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2023-10-25 20:31:25 +08:00
|
|
|
/**
|
|
|
|
* Brief: lwp thread cleanup
|
|
|
|
*
|
|
|
|
* Note: Critical Section
|
|
|
|
* - thread control block (RW. It's ensured that no one else can access tcb
|
|
|
|
* other than itself)
|
|
|
|
*/
|
2018-12-10 16:31:33 +08:00
|
|
|
lwp = (struct rt_lwp *)tid->lwp;
|
2023-07-20 00:02:41 +08:00
|
|
|
lwp_thread_signal_detach(&tid->signal);
|
|
|
|
|
|
|
|
/* tty will be release in lwp_ref_dec() if ref is cleared */
|
|
|
|
lwp_ref_dec(lwp);
|
2022-12-03 12:07:44 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
static void lwp_execve_setup_stdio(struct rt_lwp *lwp)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
struct dfs_fdtable *lwp_fdt;
|
2024-03-28 23:42:56 +08:00
|
|
|
struct dfs_file *cons_file;
|
|
|
|
int cons_fd;
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
lwp_fdt = &lwp->fdt;
|
2024-03-28 23:42:56 +08:00
|
|
|
|
|
|
|
/* open console */
|
|
|
|
cons_fd = open("/dev/console", O_RDWR);
|
|
|
|
if (cons_fd < 0)
|
|
|
|
{
|
|
|
|
LOG_E("%s: Cannot open console tty", __func__);
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
LOG_D("%s: open console as fd %d", __func__, cons_fd);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* init 4 fds */
|
|
|
|
lwp_fdt->fds = rt_calloc(4, sizeof(void *));
|
|
|
|
if (lwp_fdt->fds)
|
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
cons_file = fd_get(cons_fd);
|
2022-12-03 12:07:44 +08:00
|
|
|
lwp_fdt->maxfd = 4;
|
2024-03-28 23:42:56 +08:00
|
|
|
fdt_fd_associate_file(lwp_fdt, 0, cons_file);
|
|
|
|
fdt_fd_associate_file(lwp_fdt, 1, cons_file);
|
|
|
|
fdt_fd_associate_file(lwp_fdt, 2, cons_file);
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
close(cons_fd);
|
2022-12-03 12:07:44 +08:00
|
|
|
return;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
static void _lwp_thread_entry(void *parameter)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
|
|
|
rt_thread_t tid;
|
|
|
|
struct rt_lwp *lwp;
|
|
|
|
|
|
|
|
tid = rt_thread_self();
|
2022-12-03 12:07:44 +08:00
|
|
|
lwp = (struct rt_lwp *)tid->lwp;
|
2018-12-10 16:31:33 +08:00
|
|
|
tid->cleanup = lwp_cleanup;
|
2022-12-03 12:07:44 +08:00
|
|
|
tid->user_stack = RT_NULL;
|
|
|
|
|
|
|
|
if (lwp->debug)
|
|
|
|
{
|
2023-10-14 13:07:45 +08:00
|
|
|
lwp->bak_first_inst = *(uint32_t *)lwp->text_entry;
|
2022-12-03 12:07:44 +08:00
|
|
|
*(uint32_t *)lwp->text_entry = dbg_get_ins();
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, lwp->text_entry, sizeof(uint32_t));
|
|
|
|
icache_invalid_all();
|
|
|
|
}
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
/**
|
|
|
|
* without ASID support, it will be a special case when trying to run application
|
|
|
|
* and exit multiple times and a same page frame allocated to it bound to
|
|
|
|
* different text segment. Then we are in a situation where icache contains
|
|
|
|
* out-of-dated data and must be handle by the running core itself.
|
|
|
|
* with ASID support, this should be a rare case that ASID & page frame both
|
|
|
|
* identical to previous running application.
|
|
|
|
*
|
|
|
|
* For a new application loaded into memory, icache are seen as empty. And there
|
|
|
|
* should be nothing in the icache entry to match. So this icache invalidation
|
|
|
|
* operation should have barely influence.
|
|
|
|
*/
|
2023-02-20 13:48:00 +08:00
|
|
|
rt_hw_icache_invalidate_all();
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2023-04-22 23:59:11 +08:00
|
|
|
arch_start_umode(lwp->args, lwp->text_entry, (void *)USER_STACK_VEND, (char *)tid->stack_addr + tid->stack_size);
|
2022-12-03 12:07:44 +08:00
|
|
|
#else
|
|
|
|
arch_start_umode(lwp->args, lwp->text_entry, lwp->data_entry, (void *)((uint32_t)lwp->data_entry + lwp->data_size));
|
|
|
|
#endif /* ARCH_MM_MMU */
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
struct rt_lwp *lwp_self(void)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_thread_t tid;
|
|
|
|
|
|
|
|
tid = rt_thread_self();
|
|
|
|
if (tid)
|
|
|
|
{
|
|
|
|
return (struct rt_lwp *)tid->lwp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_NULL;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
2023-10-25 20:31:25 +08:00
|
|
|
rt_err_t lwp_children_register(struct rt_lwp *parent, struct rt_lwp *child)
|
|
|
|
{
|
|
|
|
/* lwp add to children link */
|
|
|
|
LWP_LOCK(parent);
|
|
|
|
child->sibling = parent->first_child;
|
|
|
|
parent->first_child = child;
|
|
|
|
child->parent = parent;
|
|
|
|
LWP_UNLOCK(parent);
|
|
|
|
|
|
|
|
LOG_D("%s(parent=%p, child=%p)", __func__, parent, child);
|
2023-11-02 20:23:11 +08:00
|
|
|
/* parent holds reference to child */
|
|
|
|
lwp_ref_inc(parent);
|
|
|
|
/* child holds reference to parent */
|
|
|
|
lwp_ref_inc(child);
|
2023-10-25 20:31:25 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t lwp_children_unregister(struct rt_lwp *parent, struct rt_lwp *child)
|
|
|
|
{
|
|
|
|
struct rt_lwp **lwp_node;
|
|
|
|
|
|
|
|
LWP_LOCK(parent);
|
|
|
|
/* detach from children link */
|
|
|
|
lwp_node = &parent->first_child;
|
|
|
|
while (*lwp_node != child)
|
|
|
|
{
|
|
|
|
RT_ASSERT(*lwp_node != RT_NULL);
|
|
|
|
lwp_node = &(*lwp_node)->sibling;
|
|
|
|
}
|
|
|
|
(*lwp_node) = child->sibling;
|
|
|
|
child->parent = RT_NULL;
|
|
|
|
LWP_UNLOCK(parent);
|
|
|
|
|
|
|
|
LOG_D("%s(parent=%p, child=%p)", __func__, parent, child);
|
2023-11-02 20:23:11 +08:00
|
|
|
lwp_ref_dec(child);
|
|
|
|
lwp_ref_dec(parent);
|
2023-10-25 20:31:25 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
pid_t lwp_execve(char *filename, int debug, int argc, char **argv, char **envp)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
|
|
|
int result;
|
2022-12-03 12:07:44 +08:00
|
|
|
struct rt_lwp *lwp;
|
|
|
|
char *thread_name;
|
|
|
|
struct process_aux *aux;
|
|
|
|
int tid = 0;
|
2018-12-10 16:31:33 +08:00
|
|
|
|
|
|
|
if (filename == RT_NULL)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-11-02 20:23:11 +08:00
|
|
|
return -EINVAL;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
|
2023-07-03 19:25:59 +08:00
|
|
|
if (access(filename, X_OK) != 0)
|
|
|
|
{
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
lwp = lwp_create(LWP_CREATE_FLAG_ALLOC_PID | LWP_CREATE_FLAG_NOTRACE_EXEC);
|
2018-12-10 16:31:33 +08:00
|
|
|
|
|
|
|
if (lwp == RT_NULL)
|
|
|
|
{
|
|
|
|
dbg_log(DBG_ERROR, "lwp struct out of memory!\n");
|
2023-11-02 20:23:11 +08:00
|
|
|
return -ENOMEM;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
LOG_D("lwp malloc : %p, size: %d!", lwp, sizeof(struct rt_lwp));
|
|
|
|
|
|
|
|
if ((tid = lwp_tid_get()) == 0)
|
|
|
|
{
|
|
|
|
lwp_ref_dec(lwp);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef ARCH_MM_MMU
|
2023-01-09 10:08:55 +08:00
|
|
|
if (lwp_user_space_init(lwp, 0) != 0)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
lwp_tid_put(tid);
|
|
|
|
lwp_ref_dec(lwp);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((aux = lwp_argscopy(lwp, argc, argv, envp)) == RT_NULL)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
lwp_tid_put(tid);
|
|
|
|
lwp_ref_dec(lwp);
|
2018-12-10 16:31:33 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
result = lwp_load(filename, lwp, RT_NULL, 0, aux);
|
|
|
|
#ifdef ARCH_MM_MMU
|
|
|
|
if (result == 1)
|
|
|
|
{
|
|
|
|
/* dynmaic */
|
|
|
|
lwp_unmap_user(lwp, (void *)(USER_VADDR_TOP - ARCH_PAGE_SIZE));
|
|
|
|
result = load_ldso(lwp, filename, argv, envp);
|
|
|
|
}
|
|
|
|
#endif /* ARCH_MM_MMU */
|
2018-12-10 16:31:33 +08:00
|
|
|
if (result == RT_EOK)
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_thread_t thread = RT_NULL;
|
|
|
|
rt_uint32_t priority = 25, tick = 200;
|
2018-12-10 16:31:33 +08:00
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
lwp_execve_setup_stdio(lwp);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
/* obtain the base name */
|
|
|
|
thread_name = strrchr(filename, '/');
|
|
|
|
thread_name = thread_name ? thread_name + 1 : filename;
|
|
|
|
#ifndef ARCH_MM_MMU
|
|
|
|
struct lwp_app_head *app_head = lwp->text_entry;
|
|
|
|
if (app_head->priority)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
priority = app_head->priority;
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
if (app_head->tick)
|
2018-12-10 16:31:33 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
tick = app_head->tick;
|
|
|
|
}
|
|
|
|
#endif /* not defined ARCH_MM_MMU */
|
|
|
|
thread = rt_thread_create(thread_name, _lwp_thread_entry, RT_NULL,
|
|
|
|
LWP_TASK_STACK_SIZE, priority, tick);
|
|
|
|
if (thread != RT_NULL)
|
|
|
|
{
|
|
|
|
struct rt_lwp *self_lwp;
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_session_t session;
|
|
|
|
rt_processgroup_t group;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
thread->tid = tid;
|
|
|
|
lwp_tid_set_thread(tid, thread);
|
|
|
|
LOG_D("lwp kernel => (0x%08x, 0x%08x)\n", (rt_size_t)thread->stack_addr,
|
|
|
|
(rt_size_t)thread->stack_addr + thread->stack_size);
|
|
|
|
self_lwp = lwp_self();
|
2024-03-28 23:42:56 +08:00
|
|
|
|
|
|
|
/* when create init, self_lwp == null */
|
|
|
|
if (self_lwp == RT_NULL && lwp_to_pid(lwp) != 1)
|
|
|
|
{
|
|
|
|
self_lwp = lwp_from_pid_and_lock(1);
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (self_lwp)
|
|
|
|
{
|
|
|
|
/* lwp add to children link */
|
2023-10-25 20:31:25 +08:00
|
|
|
lwp_children_register(self_lwp, lwp);
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2024-03-28 23:42:56 +08:00
|
|
|
|
|
|
|
session = RT_NULL;
|
|
|
|
group = RT_NULL;
|
|
|
|
|
|
|
|
group = lwp_pgrp_create(lwp);
|
|
|
|
if (group)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
lwp_pgrp_insert(group, lwp);
|
|
|
|
if (self_lwp == RT_NULL)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
session = lwp_session_create(lwp);
|
|
|
|
lwp_session_insert(session, group);
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
session = lwp_session_find(lwp_sid_get_byprocess(self_lwp));
|
|
|
|
lwp_session_insert(session, group);
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
}
|
2024-03-28 23:42:56 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
thread->lwp = lwp;
|
|
|
|
#ifndef ARCH_MM_MMU
|
|
|
|
struct lwp_app_head *app_head = (struct lwp_app_head*)lwp->text_entry;
|
|
|
|
thread->user_stack = app_head->stack_offset ?
|
|
|
|
(void *)(app_head->stack_offset -
|
|
|
|
app_head->data_offset +
|
|
|
|
(uint32_t)lwp->data_entry) : RT_NULL;
|
|
|
|
thread->user_stack_size = app_head->stack_size;
|
|
|
|
/* init data area */
|
|
|
|
rt_memset(lwp->data_entry, 0, lwp->data_size);
|
|
|
|
/* init user stack */
|
|
|
|
rt_memset(thread->user_stack, '#', thread->user_stack_size);
|
|
|
|
#endif /* not defined ARCH_MM_MMU */
|
|
|
|
rt_list_insert_after(&lwp->t_grp, &thread->sibling);
|
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
lwp->did_exec = RT_TRUE;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (debug && rt_dbg_ops)
|
|
|
|
{
|
|
|
|
lwp->debug = debug;
|
|
|
|
rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void*)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_thread_startup(thread);
|
|
|
|
return lwp_to_pid(lwp);
|
2018-12-10 16:31:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
lwp_tid_put(tid);
|
|
|
|
lwp_ref_dec(lwp);
|
2018-12-10 16:31:33 +08:00
|
|
|
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-08-16 15:38:59 +08:00
|
|
|
#ifdef RT_USING_MUSLLIBC
|
2022-12-03 12:07:44 +08:00
|
|
|
extern char **__environ;
|
|
|
|
#else
|
2022-12-10 22:16:42 +08:00
|
|
|
char **__environ = 0;
|
2022-12-03 12:07:44 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
pid_t exec(char *filename, int debug, int argc, char **argv)
|
|
|
|
{
|
2023-03-28 15:53:15 +08:00
|
|
|
setenv("OS", "RT-Thread", 1);
|
2022-12-03 12:07:44 +08:00
|
|
|
return lwp_execve(filename, debug, argc, argv, __environ);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ARCH_MM_MMU
|
|
|
|
void lwp_user_setting_save(rt_thread_t thread)
|
|
|
|
{
|
|
|
|
if (thread)
|
|
|
|
{
|
|
|
|
thread->thread_idr = arch_get_tidr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lwp_user_setting_restore(rt_thread_t thread)
|
|
|
|
{
|
|
|
|
if (!thread)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if !defined(ARCH_RISCV64)
|
|
|
|
/* tidr will be set in RESTORE_ALL in risc-v */
|
|
|
|
arch_set_tidr(thread->thread_idr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rt_dbg_ops)
|
|
|
|
{
|
|
|
|
struct rt_lwp *l = (struct rt_lwp *)thread->lwp;
|
|
|
|
|
|
|
|
if (l != 0)
|
|
|
|
{
|
|
|
|
rt_hw_set_process_id((size_t)l->pid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_set_process_id(0);
|
|
|
|
}
|
|
|
|
if (l && l->debug)
|
|
|
|
{
|
|
|
|
uint32_t step_type = 0;
|
|
|
|
|
|
|
|
step_type = dbg_step_type();
|
|
|
|
|
|
|
|
if ((step_type == 2) || (thread->step_exec && (step_type == 1)))
|
|
|
|
{
|
|
|
|
dbg_activate_step();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbg_deactivate_step();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* ARCH_MM_MMU */
|
2023-08-08 00:22:14 +08:00
|
|
|
|
|
|
|
void lwp_uthread_ctx_save(void *ctx)
|
|
|
|
{
|
|
|
|
rt_thread_t thread;
|
|
|
|
thread = rt_thread_self();
|
|
|
|
thread->user_ctx.ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lwp_uthread_ctx_restore(void)
|
|
|
|
{
|
|
|
|
rt_thread_t thread;
|
|
|
|
thread = rt_thread_self();
|
|
|
|
thread->user_ctx.ctx = RT_NULL;
|
|
|
|
}
|
2023-10-17 13:07:59 +08:00
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
rt_err_t lwp_backtrace_frame(rt_thread_t uthread, struct rt_hw_backtrace_frame *frame)
|
|
|
|
{
|
|
|
|
rt_err_t rc = -RT_ERROR;
|
|
|
|
long nesting = 0;
|
|
|
|
char **argv;
|
|
|
|
rt_lwp_t lwp;
|
|
|
|
|
2024-03-28 23:42:56 +08:00
|
|
|
if (uthread && uthread->lwp && rt_scheduler_is_available())
|
2023-10-21 20:14:45 +08:00
|
|
|
{
|
|
|
|
lwp = uthread->lwp;
|
|
|
|
argv = lwp_get_command_line_args(lwp);
|
|
|
|
if (argv)
|
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_kprintf("please use: addr2line -e %s -a -f", argv[0]);
|
2023-10-21 20:14:45 +08:00
|
|
|
lwp_free_command_line_args(argv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_kprintf("please use: addr2line -e %s -a -f", lwp->cmd);
|
2023-10-21 20:14:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (nesting < RT_BACKTRACE_LEVEL_MAX_NR)
|
|
|
|
{
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_kprintf(" 0x%lx", frame->pc);
|
2023-10-21 20:14:45 +08:00
|
|
|
if (rt_hw_backtrace_frame_unwind(uthread, frame))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nesting++;
|
|
|
|
}
|
2024-03-28 23:42:56 +08:00
|
|
|
rt_kprintf("\n");
|
2023-10-21 20:14:45 +08:00
|
|
|
rc = RT_EOK;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
void rt_update_process_times(void)
|
|
|
|
{
|
|
|
|
struct rt_thread *thread;
|
|
|
|
#ifdef RT_USING_SMP
|
|
|
|
struct rt_cpu* pcpu;
|
|
|
|
|
|
|
|
pcpu = rt_cpu_self();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
thread = rt_thread_self();
|
|
|
|
|
|
|
|
if (!IS_USER_MODE(thread))
|
|
|
|
{
|
|
|
|
thread->user_time += 1;
|
|
|
|
#ifdef RT_USING_SMP
|
|
|
|
pcpu->cpu_stat.user += 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
thread->system_time += 1;
|
|
|
|
#ifdef RT_USING_SMP
|
|
|
|
if (thread == pcpu->idle_thread)
|
|
|
|
{
|
|
|
|
pcpu->cpu_stat.idle += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pcpu->cpu_stat.system += 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|