HelenOS sources
This source file includes following definitions.
- program_create
- program_create_from_image
- program_create_loader
- program_ready
- sys_program_spawn_loader
#include <main/uinit.h>
#include <proc/thread.h>
#include <proc/task.h>
#include <mm/as.h>
#include <stdlib.h>
#include <arch.h>
#include <adt/list.h>
#include <ipc/ipc.h>
#include <ipc/ipcrsc.h>
#include <security/perm.h>
#include <lib/elf_load.h>
#include <str.h>
#include <str_error.h>
#include <log.h>
#include <syscall/copy.h>
#include <proc/program.h>
#include <userspace.h>
void *program_loader = NULL;
errno_t program_create(as_t *as, uspace_addr_t entry_addr, char *name, program_t *prg)
{
uinit_arg_t *kernel_uarg = malloc(sizeof(uinit_arg_t));
if (!kernel_uarg)
return ENOMEM;
prg->loader_status = EOK;
prg->task = task_create(as, name);
if (!prg->task) {
free(kernel_uarg);
return ELIMIT;
}
uintptr_t virt = (uintptr_t) AS_AREA_ANY;
uintptr_t bound = USER_ADDRESS_SPACE_END - (STACK_SIZE_USER - 1);
bound -= PAGE_SIZE;
as_area_t *area = as_area_create(as,
AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
AS_AREA_LATE_RESERVE, STACK_SIZE_USER, AS_AREA_ATTR_NONE,
&anon_backend, NULL, &virt, bound);
if (!area) {
free(kernel_uarg);
task_release(prg->task);
prg->task = NULL;
return ENOMEM;
}
kernel_uarg->pc = entry_addr;
kernel_uarg->sp = arch_get_initial_sp(virt, STACK_SIZE_USER);
prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
THREAD_FLAG_USPACE, "uinit");
if (!prg->main_thread) {
free(kernel_uarg);
as_area_destroy(as, virt);
task_release(prg->task);
prg->task = NULL;
return ELIMIT;
}
return EOK;
}
errno_t program_create_from_image(void *image_addr, size_t image_size, char *name, program_t *prg)
{
as_t *as = as_create(0);
if (!as)
return ENOMEM;
prg->loader_status = elf_load((elf_header_t *) image_addr, as);
if (prg->loader_status != EOK) {
as_release(as);
prg->task = NULL;
prg->main_thread = NULL;
return ENOTSUP;
}
errno_t rc = program_create(as, ((elf_header_t *) image_addr)->e_entry,
name, prg);
if (rc == EOK) {
prg->task->debug_sections = calloc(1, sizeof(debug_sections_t));
if (prg->task->debug_sections != NULL)
*prg->task->debug_sections = get_debug_sections(image_addr, image_size);
}
return rc;
}
errno_t program_create_loader(program_t *prg, char *name)
{
as_t *as = as_create(0);
if (!as)
return ENOMEM;
void *loader = program_loader;
if (!loader) {
as_release(as);
log(LF_OTHER, LVL_ERROR,
"Cannot spawn loader as none was registered");
return ENOENT;
}
prg->loader_status = elf_load((elf_header_t *) program_loader, as);
if (prg->loader_status != EOK) {
as_release(as);
log(LF_OTHER, LVL_ERROR, "Cannot spawn loader (%s)",
str_error(prg->loader_status));
return prg->loader_status;
}
return program_create(as, ((elf_header_t *) program_loader)->e_entry,
name, prg);
}
void program_ready(program_t *prg)
{
thread_start(prg->main_thread);
thread_detach(prg->main_thread);
prg->main_thread = NULL;
}
sys_errno_t sys_program_spawn_loader(uspace_ptr_char uspace_name, size_t name_len)
{
if (name_len > TASK_NAME_BUFLEN - 1)
name_len = TASK_NAME_BUFLEN - 1;
char namebuf[TASK_NAME_BUFLEN];
errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
if (rc != EOK)
return (sys_errno_t) rc;
namebuf[name_len] = 0;
program_t prg;
rc = program_create_loader(&prg, namebuf);
if (rc != EOK)
return rc;
perm_set(prg.task, perm_get(TASK));
program_ready(&prg);
task_release(prg.task);
return EOK;
}
HelenOS homepage, sources at GitHub