forked from xuos/xiuos
132 lines
3.5 KiB
C
132 lines
3.5 KiB
C
/*
|
|
* Copyright (c) 2020 AIIT XUOS Lab
|
|
* XiUOS is licensed under Mulan PSL v2.
|
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
* You may obtain a copy of Mulan PSL v2 at:
|
|
* http://license.coscl.org.cn/MulanPSL2
|
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
* See the Mulan PSL v2 for more details.
|
|
*/
|
|
#include "usyscall.h"
|
|
#include "libmem.h"
|
|
|
|
int spawn(struct Session* session, int fd, ipc_read_fn ipc_read, ipc_fsize_fn ipc_fsize, char* name, char** argv)
|
|
{
|
|
/* read elf image */
|
|
int file_size = ipc_fsize(session, fd);
|
|
void* img = malloc(file_size);
|
|
int read_len = 0;
|
|
while (read_len < file_size) {
|
|
int cur_read_len = file_size - read_len < 4096 ? file_size - read_len : 4096;
|
|
if (cur_read_len < 0) {
|
|
return -1;
|
|
}
|
|
read_len += ipc_read(session, fd, (char*)((uintptr_t)img + read_len), read_len, cur_read_len);
|
|
}
|
|
/* sys call */
|
|
int ret = syscall(SYSCALL_SPAWN, (uintptr_t)img, (uintptr_t)name, (uintptr_t)argv, 0);
|
|
free(img);
|
|
return ret;
|
|
}
|
|
|
|
int thread(void* entry, char* name, char** argv)
|
|
{
|
|
return syscall(SYSCALL_THREAD, (uintptr_t)entry, (uintptr_t)name, (uintptr_t)argv, 0);
|
|
}
|
|
|
|
void exit(int status)
|
|
{
|
|
syscall(SYSCALL_EXIT, (uintptr_t)status, 0, 0, 0);
|
|
}
|
|
|
|
int yield(task_yield_reason reason)
|
|
{
|
|
return syscall(SYSCALL_YIELD, (uintptr_t)reason, 0, 0, 0);
|
|
}
|
|
|
|
int kill(int pid)
|
|
{
|
|
return syscall(SYSCALL_KILL, (intptr_t)pid, 0, 0, 0);
|
|
}
|
|
|
|
int register_server(char* name)
|
|
{
|
|
return syscall(SYSCALL_SERVER, (intptr_t)name, 0, 0, 0);
|
|
}
|
|
|
|
int session(char* path, int capacity, struct Session* user_session)
|
|
{
|
|
return syscall(SYSCALL_SESSION, (intptr_t)path, (intptr_t)capacity, (intptr_t)user_session, 0);
|
|
}
|
|
|
|
int poll_session(struct Session* userland_session_arr, int arr_capacity)
|
|
{
|
|
return syscall(SYSCALL_POLL_SESSION, (intptr_t)userland_session_arr, (intptr_t)arr_capacity, 0, 0);
|
|
}
|
|
|
|
int close_session(struct Session* session)
|
|
{
|
|
return syscall(SYSCALL_CLOSE_SESSION, (intptr_t)session, 0, 0, 0);
|
|
}
|
|
|
|
int get_memblock_info(sys_state_info* info)
|
|
{
|
|
return syscall(SYSCALL_SYS_STATE, SYS_STATE_MEMBLOCK_INFO, (intptr_t)info, 0, 0);
|
|
}
|
|
|
|
int set_priority(sys_state_info* info)
|
|
{
|
|
return syscall(SYSCALL_SYS_STATE, SYS_STATE_SET_TASK_PRIORITY, (intptr_t)info, 0, 0);
|
|
}
|
|
|
|
int task_heap_base()
|
|
{
|
|
return syscall(SYSCALL_SYS_STATE, SYS_STATE_GET_HEAP_BASE, 0, 0, 0);
|
|
}
|
|
|
|
int show_task()
|
|
{
|
|
return syscall(SYSCALL_SYS_STATE, SYS_STATE_SHOW_TASKS, 0, 0, 0);
|
|
}
|
|
|
|
int show_mem()
|
|
{
|
|
return syscall(SYSCALL_SYS_STATE, SYS_STATE_SHOW_MEM_INFO, 0, 0, 0);
|
|
}
|
|
|
|
int show_cpu()
|
|
{
|
|
return syscall(SYSCALL_SYS_STATE, SYS_STATE_SHOW_CPU_INFO, 0, 0, 0);
|
|
}
|
|
|
|
int mmap(uintptr_t vaddr, uintptr_t paddr, int len, bool is_dev)
|
|
{
|
|
return syscall(SYSCALL_MMAP, vaddr, paddr, (intptr_t)len, (intptr_t)is_dev);
|
|
}
|
|
|
|
int register_irq(int irq, int opcode)
|
|
{
|
|
return syscall(SYSCALL_REGISTER_IRQ, (intptr_t)irq, (intptr_t)opcode, 0, 0);
|
|
}
|
|
|
|
int semaphore_new(int val)
|
|
{
|
|
return syscall(SYSCALL_SEMAPHORE, (intptr_t)SYS_SEM_NEW, (intptr_t)val, 0, 0);
|
|
}
|
|
|
|
bool semaphore_free(int sem_id)
|
|
{
|
|
return syscall(SYSCALL_SEMAPHORE, (intptr_t)SYS_SEM_FREE, (intptr_t)sem_id, 0, 0);
|
|
}
|
|
|
|
bool semaphore_wait(int sem_id)
|
|
{
|
|
return syscall(SYSCALL_SEMAPHORE, (intptr_t)SYS_SEM_WAIT, (intptr_t)sem_id, 0, 0);
|
|
}
|
|
|
|
bool semaphore_signal(int sem_id)
|
|
{
|
|
return syscall(SYSCALL_SEMAPHORE, (intptr_t)SYS_SEM_SIGNAL, (intptr_t)sem_id, 0, 0);
|
|
} |