mirror of https://github.com/0Nera/BMOSP.git
95 lines
2.3 KiB
C
95 lines
2.3 KiB
C
/**
|
||
* system.h
|
||
* Системные вызовы
|
||
*
|
||
* Заголовочный файл содержащий заготовку для работы с системными вызовами
|
||
*
|
||
*/
|
||
|
||
#ifndef SYSTEM_H
|
||
#define SYSTEM_H
|
||
|
||
#include <types.h>
|
||
|
||
static void *(*alloc)(uint64_t size);
|
||
static void (*free)(void *ptr);
|
||
static void (*fb_printf)(char *str, ...);
|
||
static module_info_t *(*get_module)(char *module_id);
|
||
static framebuffer_t (*alloc_framebuffer)( );
|
||
static void (*free_framebuffer)(framebuffer_t *frame);
|
||
static void (*exit)(int code);
|
||
static int (*get_error)( );
|
||
static sys_info_t *(*get_info)( );
|
||
static uint64_t (*new_thread)(uint64_t func);
|
||
static int (*delete_thread)(uint64_t thread_id);
|
||
static time_t (*get_time)( );
|
||
static uint64_t offset;
|
||
|
||
#include <modstd.h>
|
||
|
||
static inline void init_env(env_t *loader_env) {
|
||
offset = loader_env->offset;
|
||
fb_printf = loader_env->fb_printf;
|
||
alloc = loader_env->alloc;
|
||
free = loader_env->free;
|
||
get_module = loader_env->get_module;
|
||
alloc_framebuffer = loader_env->alloc_framebuffer;
|
||
free_framebuffer = loader_env->free_framebuffer;
|
||
exit = loader_env->exit;
|
||
get_error = loader_env->get_error;
|
||
get_info = loader_env->get_info;
|
||
new_thread = loader_env->new_thread;
|
||
delete_thread = loader_env->delete_thread;
|
||
get_time = loader_env->get_time;
|
||
}
|
||
|
||
static void *realloc(void *addr, size_t size) {
|
||
if (size == 0) {
|
||
free(addr);
|
||
return NULL;
|
||
}
|
||
|
||
if (addr == NULL) { return alloc(size); }
|
||
|
||
void *new_addr = alloc(size);
|
||
|
||
if (new_addr == NULL) { return NULL; }
|
||
|
||
memcpy(new_addr, addr, size);
|
||
free(addr);
|
||
|
||
return new_addr;
|
||
}
|
||
|
||
static inline void outb(uint16_t port, uint8_t val) {
|
||
asm volatile("outb %0, %1" : : "a"(val), "Nd"(port));
|
||
}
|
||
|
||
static inline uint8_t inb(uint16_t port) {
|
||
uint8_t ret;
|
||
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port));
|
||
return ret;
|
||
}
|
||
|
||
static inline void outw(uint16_t port, uint16_t val) {
|
||
asm volatile("outw %0, %1" : : "a"(val), "Nd"(port));
|
||
}
|
||
|
||
static inline uint16_t inw(uint16_t port) {
|
||
uint16_t ret;
|
||
asm volatile("inw %1, %0" : "=a"(ret) : "Nd"(port));
|
||
return ret;
|
||
}
|
||
|
||
static inline uint32_t inl(uint16_t port) {
|
||
uint32_t data;
|
||
asm volatile("inl %1, %0" : "=a"(data) : "Nd"(port));
|
||
return data;
|
||
}
|
||
|
||
static inline void outl(uint16_t port, uint32_t data) {
|
||
asm volatile("outl %0, %1" : : "a"(data), "Nd"(port));
|
||
}
|
||
|
||
#endif // system.h
|