Max OS 0.3
Loading...
Searching...
No Matches
syscalls.cpp
Go to the documentation of this file.
1
9#include <system/syscalls.h>
10#include <common/logger.h>
11
12using namespace syscore;
13using namespace MaxOS;
14using namespace MaxOS::common;
15using namespace MaxOS::hardwarecommunication;
16using namespace MaxOS::system;
17using namespace MaxOS::processes;
18using namespace MaxOS::memory;
19
24: InterruptHandler(0x80)
25{
26
27 // Register the handlers
28 Logger::INFO() << "Setting up Syscalls \n";
29 set_syscall_handler(SyscallType::CLOSE_PROCESS, syscall_close_process);
30 set_syscall_handler(SyscallType::KLOG, syscall_klog);
31 set_syscall_handler(SyscallType::ALLOCATE_MEMORY, syscall_allocate_memory);
32 set_syscall_handler(SyscallType::FREE_MEMORY, syscall_free_memory);
33 set_syscall_handler(SyscallType::RESOURCE_CREATE, syscall_resource_create);
34 set_syscall_handler(SyscallType::RESOURCE_OPEN, syscall_resource_open);
35 set_syscall_handler(SyscallType::RESOURCE_CLOSE, syscall_resource_close);
36 set_syscall_handler(SyscallType::RESOURCE_WRITE, syscall_resource_write);
37 set_syscall_handler(SyscallType::RESOURCE_READ, syscall_resource_read);
38 set_syscall_handler(SyscallType::THREAD_YIELD, syscall_thread_yield);
39 set_syscall_handler(SyscallType::THREAD_SLEEP, syscall_thread_sleep);
40 set_syscall_handler(SyscallType::THREAD_CLOSE, syscall_thread_close);
41
42}
43
44SyscallManager::~SyscallManager() = default;
45
53
54 // Get the args from the cpu state
56 args.arg0 = status->rdi;
57 args.arg1 = status->rsi;
58 args.arg2 = status->rdx;
59 args.arg3 = status->r10;
60 args.arg4 = status->r8;
61 args.arg5 = status->r9;
62 args.return_value = 0;
63 args.return_state = status;
64
65 // Call the handler
66 uint64_t syscall = status->rax;
67 if (m_syscall_handlers[syscall] != nullptr)
68 args = *(m_syscall_handlers[syscall](&args));
69 else
70 Logger::ERROR() << "Syscall " << syscall << " not found\n";
71
72 // If there is a specific return state, use that
73 if (args.return_state != status)
74 return args.return_state;
75
76 // Update the cpu state
77 status->rdi = args.arg0;
78 status->rsi = args.arg1;
79 status->rdx = args.arg2;
80 status->r10 = args.arg3;
81 status->r8 = args.arg4;
82 status->r9 = args.arg5;
83 status->rax = args.return_value;
84
85 // Return the status
86 return status;
87}
88
96
97 m_syscall_handlers[(uint8_t) syscall] = handler;
98}
99
106
107 m_syscall_handlers[(uint8_t) syscall] = nullptr;
108}
109
110
118
119 // Get the args
120 uint64_t pid = args->arg0;
121 int exit_code = (int) args->arg1;
122
123 // Close the process
126
127 // Schedule the next process
128 cpu_status_t* next_process = GlobalScheduler::core_scheduler()->schedule_next(args->return_state);
129 args->return_state = next_process;
130
131 // Done
132 return args;
133}
134
142
143 s_lock.lock();
144
145 // Ensure a message was provided
146 char* message = (char*) args->arg0;
147 if(!message){
148 s_lock.unlock();
149 return args;
150 }
151
152 // If the first two characters are %h then no header
153 if (message[0] == '%' && message[1] == 'h')
154 Logger::Out() << message + 2;
155 else
157
158 s_lock.unlock();
159
160 return args;
161}
162
170
171 // Malloc the memory
172 size_t size = args->arg0;
173 void* address = MemoryManager::malloc(size);
174
175 // Return the address
176 args->return_value = (uint64_t)address;
177 return args;
178}
179
187
188 // Free the memory
189 void* address = (void*) args->arg0;
190 MemoryManager::free(address);
191
192 return args;
193}
194
202
203 // Parse params
204 auto type = (ResourceType)args->arg0;
205 auto name = (char*)args->arg1;
206 auto flags = (size_t)args->arg2;
207
208 // Try to create the resource
209 auto resource = GlobalResourceRegistry::get_registry(type)->create_resource(name, flags);
210
211 // Handle response
212 args->return_value = resource ? 1 : 0;
213 return args;
214}
215
223
224 // Parse params
225 auto type = (ResourceType)args->arg0;
226 auto name = (char*)args->arg1;
227 auto flags = (size_t)args->arg2;
228
229 // Open the resource
230 args->return_value = GlobalScheduler::current_process()->resource_manager.open_resource(type, name, flags);
231 return args;
232}
233
241
242 // Parse params
243 auto handle = (uint64_t )args->arg0;
244 auto flags = (size_t)args->arg1;
245
246 // Close the resource
247 GlobalScheduler::current_process()->resource_manager.close_resource(handle, flags);
248 return args;
249}
250
258
259 // Parse params
260 auto handle = (uint64_t )args->arg0;
261 auto buffer = (void*)args->arg1;
262 auto size = (size_t)args->arg2;
263 auto flags = (size_t)args->arg3;
264
265 // Open the resource
266 auto resource = GlobalScheduler::current_process()->resource_manager.get_resource(handle);
267 if(!resource){
268 args->return_value = 0;
269 return args;
270 }
271
272 // Write to the resource
273 args->return_value = resource->write(buffer,size,flags);
274 return args;
275}
276
284
285 // Parse params
286 auto handle = (uint64_t )args->arg0;
287 auto buffer = (void*)args->arg1;
288 auto size = (size_t)args->arg2;
289 auto flags = (size_t)args->arg3;
290
291 // Open the resource
292 auto resource = GlobalScheduler::current_process()->resource_manager.get_resource(handle);
293 if(!resource){
294 args->return_value = 0;
295 return args;
296 }
297
298 // Write to the resource
299 args->return_value = resource->read(buffer,size,flags);
300 return args;
301}
302
310
311 // Yield
312 args->return_state = GlobalScheduler::yield(args->return_state);
313
314 return args;
315}
316
324
325 // Get the milliseconds
326 size_t milliseconds = args->arg0;
327
328 // Sleep the thread
330 args->return_state = GlobalScheduler::yield(args->return_state);
331
332 // Done
333 return args;
334}
335
343
344 // Get the args
345 uint64_t tid = args->arg0;
346 int exit_code = (int) args->arg1;
347
348 // Get the thread if it is 0 then it is the current thread
350 thread->thread_state = ThreadState::STOPPED;
351
352 // Schedule the next thread
353 if(tid == 0){
354 cpu_status_t* next_thread = GlobalScheduler::core_scheduler()->schedule_next(args->return_state);
355 args->return_state = next_thread;
356 }
357
358 // Done
359 return args;
360}
static Logger & Out()
Gets the active logger.
Definition logger.cpp:149
static Logger INFO()
Gets active logger set to info level.
Definition logger.cpp:171
static Logger ERROR()
Gets active logger set to ERROR level.
Definition logger.cpp:216
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Handles a certain interrupt number.
Definition interrupts.h:30
virtual void handle_interrupt()
Handles an interrupt.
static void free(void *pointer)
Frees a block of memory using the current memory manager.
static void * malloc(size_t size)
Allocates a block of memory in the current USERSPACE heap.
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
Definition resource.cpp:217
static Scheduler * core_scheduler()
Gets the scheduler for the currently executing core.
static Process * get_process(uint64_t pid)
Gets a process on the currently executing core by its PID.
static system::cpu_status_t * yield(system::cpu_status_t *current)
Pass execution to the next thread.
Definition scheduler.cpp:79
static GlobalScheduler * system_scheduler()
Gets the system scheduler.
Definition scheduler.cpp:50
static Process * current_process()
Gets the process on the currently executing core.
static Thread * get_thread(uint64_t tid)
Gets a thread on the currently executing core by its TID.
static Thread * current_thread()
Gets the thread on the currently executing core.
A process that can be scheduled by the Scheduler, wraps & manages threads as well as its own address ...
Definition process.h:85
The execution context of a sub-process thread.
Definition process.h:49
static syscall_args_t * syscall_thread_sleep(syscall_args_t *args)
System call to sleep the current process.
Definition syscalls.cpp:323
static syscall_args_t * syscall_klog(syscall_args_t *args)
System call to log a message to the kernel log.
Definition syscalls.cpp:141
static syscall_args_t * syscall_resource_create(syscall_args_t *args)
System call to create a resource.
Definition syscalls.cpp:201
static syscall_args_t * syscall_resource_close(syscall_args_t *args)
System call to close a resource.
Definition syscalls.cpp:240
static syscall_args_t * syscall_close_process(syscall_args_t *args)
System call to close a process.
Definition syscalls.cpp:117
static syscall_args_t * syscall_resource_open(syscall_args_t *args)
System call to open a resource.
Definition syscalls.cpp:222
static syscall_args_t * syscall_thread_close(syscall_args_t *args)
System call to close a thread.
Definition syscalls.cpp:342
static syscall_args_t * syscall_thread_yield(syscall_args_t *args)
System call to yield the current process.
Definition syscalls.cpp:309
SyscallManager()
Construct a new Syscall Manager object and register the syscall handlers. Registers to interrupt 0x80...
Definition syscalls.cpp:23
void remove_syscall_handler(::syscore::SyscallType syscall)
Removes a syscall handler from the manager.
Definition syscalls.cpp:105
void set_syscall_handler(::syscore::SyscallType syscall, syscall_func_t handler)
Loads a syscall handler into the manager.
Definition syscalls.cpp:95
static syscall_args_t * syscall_free_memory(syscall_args_t *args)
System call to free a block of memory.
Definition syscalls.cpp:186
static syscall_args_t * syscall_resource_write(syscall_args_t *args)
System call to write to a resource.
Definition syscalls.cpp:257
static syscall_args_t * syscall_allocate_memory(syscall_args_t *args)
System call to allocate a block of memory.
Definition syscalls.cpp:169
static syscall_args_t * syscall_resource_read(syscall_args_t *args)
System call to read from a resource.
Definition syscalls.cpp:283
struct PACKED MaxOS::system::CPUStatus cpu_status_t
Alias for CPUStatus struct.
Defines a Logger class for logging messages with different severity levels to multiple output streams...
The arguments passed to a syscall (simplified representation of values in registers)
Definition syscalls.h:36
Defines the SyscallManager class for handling system calls from userspace applications.