SayoriOS 0.3.3

syscalls.c
См. документацию.
1
10 #include "sys/syscalls.h"
11 #include "io/ports.h"
12 #include "io/tty.h"
13 #include "user/env.h"
14 #include "sys/file_descriptors.h"
15 #include <kernel.h>
16 #include <sys/trigger.h>
17
18 syscall_fn_t* calls_table[NUM_CALLS] = {0};
19
25 void syscall_handler(volatile registers_t regs) {
26 // qemu_log("syscall: %d", regs.eax);
27
28 if (regs.eax >= NUM_CALLS) {
29 qemu_err("Invalid system call: %d!", regs.eax);
30
31 __asm__ volatile("movl %0, %%eax" :: "r"(0));
32 return;
33 }
34
35 syscall_fn_t* entry_point = (syscall_fn_t*)calls_table[regs.eax];
36
37 regs.eax = entry_point(regs.ebx, regs.ecx, regs.edx);
38
39 // TODO: Just place result into eax, I know how to do it!
40
41 __asm__ volatile("movl %0, %%eax" :: "r"(regs.eax));
42 }
43
44 size_t syscall_env(struct env* position) {
45 memcpy(position, &system_environment, sizeof(env_t));
46
47 return 0;
48 }
49
50 size_t syscall_memory_alloc(size_t size, size_t align, void** out) {
51 void* allocated = kcalloc(size, align);
52
53 *out = allocated;
54
55 return 0;
56 }
57
58 size_t syscall_memory_realloc(void* memory, size_t size, void** out) {
59 void* r = krealloc(memory, size);
60
61 *out = r;
62
63 return 0;
64 }
65
66 size_t syscall_memory_free(void* memory) {
67 kfree(memory);
68
69 return 0;
70 }
71
72 size_t syscall_tty_write(char* text) {
73 tty_puts(text);
74 return 0;
75 }
76
77 int syscall_trigger_reg(int type,trigger_cmd_t handler){
78 int init = RegTrigger(type,handler);
79 qemu_warn("[SysCall] [Triggers] REG -> I:%x | T:%x",init, type);
80 return init;
81 }
82
83 size_t syscall_trigger_on(int index){
84 qemu_warn("[SysCall] [Triggers] ON -> I:%x",index);
85 OnTrigger(index);
86 return 1;
87 }
88
89 size_t syscall_trigger_off(int index){
90 qemu_warn("[SysCall] [Triggers] OFF -> I:%x",index);
91 OffTrigger(index);
92 return 1;
93 }
94
95 size_t syscall_trigger_del(int index){
96 qemu_warn("[SysCall] [Triggers] Delete -> I:%x",index);
97 DeleteTrigger(index);
98 return 1;
99 }
100
101 size_t syscall_getkey() {
102 return getCharRaw();
103 }
104
105 size_t syscall_get_timer_ticks() {
106 return getTicks();
107 }
108
109 size_t syscall_sleep(uint32_t millis) {
110 sleep_ms(millis);
111
112 return 0;
113 }
114
115 size_t syscall_datetime(sayori_time_t* out_time) {
116 *out_time = get_time();
117
118 return 0;
119 }
120
121 size_t syscall_exit(uint32_t status) {
122 process_t* proc = get_current_proc();
123
124 qemu_log("Exit requested (status %d) by PID %d\n", status, proc->pid);
125
126 if(proc->pid == 0) {
127 qemu_warn("Request cancelled because PID == 0");
128 return 0;
129 }
130
131 blyat_fire();
132 }
133
140 void init_syscalls(void){
141 register_interrupt_handler(0x50, &syscall_handler);
142
143 calls_table[0] = (syscall_fn_t *)syscall_env;
144 calls_table[1] = (syscall_fn_t *)syscall_memory_alloc;
145 calls_table[2] = (syscall_fn_t *)syscall_memory_free;
146 calls_table[3] = (syscall_fn_t *)syscall_tty_write;
147 calls_table[4] = (syscall_fn_t *)file_descriptor_allocate;
148 calls_table[5] = (syscall_fn_t *)file_descriptor_read;
149 calls_table[6] = (syscall_fn_t *)file_descriptor_close;
150 calls_table[7] = (syscall_fn_t *)file_descriptor_seek;
151 calls_table[8] = (syscall_fn_t *)file_descriptor_tell;
152 calls_table[9] = (syscall_fn_t *)syscall_trigger_reg;
153 calls_table[10] = (syscall_fn_t *)syscall_trigger_on;
154 calls_table[11] = (syscall_fn_t *)syscall_trigger_off;
155 calls_table[12] = (syscall_fn_t *)syscall_trigger_del;
156 calls_table[13] = (syscall_fn_t *)syscall_getkey;
157 calls_table[14] = (syscall_fn_t *)syscall_get_timer_ticks;
158 calls_table[15] = (syscall_fn_t *)syscall_sleep;
159 calls_table[16] = (syscall_fn_t *)syscall_datetime;
160 calls_table[17] = (syscall_fn_t *)syscall_exit;
161 calls_table[18] = (syscall_fn_t *)syscall_memory_realloc;
162 calls_table[19] = (syscall_fn_t *)file_descriptor_write;
163
164 qemu_ok("System calls initialized!");
165 }
get_time
sayori_time_t get_time()
Считывает время и передает в удобной структуре
Definition: cmos.c:144
memcpy
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
get_current_proc
volatile process_t * get_current_proc(void)
Получить текущий обработчик процесса
Definition: scheduler.c:125
env
Definition: env.h:3
registers
Definition: common.h:59
sayori_time
Definition: cmos.h:5
getTicks
size_t getTicks()
Получить количество тиков
Definition: timer.c:26
sleep_ms
void sleep_ms(uint32_t milliseconds)
Ожидание по миллисекундам
Definition: timer.c:68
init_syscalls
void init_syscalls(void)
Инициализация системных вызовов
Definition: syscalls.c:140
syscall_handler
void syscall_handler(volatile registers_t regs)
Обработчик системных вызовов
Definition: syscalls.c:25
DeleteTrigger
void DeleteTrigger(int index)
Удаление триггера
Definition: trigger.c:56
OffTrigger
void OffTrigger(int index)
Выключить триггер
Definition: trigger.c:81
OnTrigger
void OnTrigger(int index)
Включить триггер
Definition: trigger.c:69
RegTrigger
int RegTrigger(int type, trigger_cmd_t handler)
Регистрация триггера
Definition: trigger.c:35