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 MaxOS;
13using namespace MaxOS::KPI;
14using namespace MaxOS::common;
15using namespace MaxOS::hardwarecommunication;
16using namespace MaxOS::system;
17using namespace MaxOS::processes;
18using namespace MaxOS::processes::resources;
19using namespace MaxOS::memory;
20
25: InterruptHandler(0x80)
26{
27
28 // Register the handlers
29 Logger::INFO() << "Setting up Syscalls \n";
30
31 set_syscall_handler(SyscallType::KLOG, syscall_klog);
32
33 set_syscall_handler(SyscallType::ALLOCATE_MEMORY, syscall_allocate_memory);
34 set_syscall_handler(SyscallType::FREE_MEMORY, syscall_free_memory);
35
36 set_syscall_handler(SyscallType::RESOURCE_CREATE, syscall_resource_create);
37 set_syscall_handler(SyscallType::RESOURCE_OPEN, syscall_resource_open);
38 set_syscall_handler(SyscallType::RESOURCE_CLOSE, syscall_resource_close);
39 set_syscall_handler(SyscallType::RESOURCE_WRITE, syscall_resource_write);
40 set_syscall_handler(SyscallType::RESOURCE_READ, syscall_resource_read);
41
42 set_syscall_handler(SyscallType::RESOURCE_REGISTRY_CREATE, syscall_resource_registry_create);
43
44
45}
46
47SyscallManager::~SyscallManager() = default;
48
56
57 // Get the args from the cpu state
58 syscall_args_t args;
59 args.arg0 = status->rdi;
60 args.arg1 = status->rsi;
61 args.arg2 = status->rdx;
62 args.arg3 = status->r10;
63 args.arg4 = status->r8;
64 args.arg5 = status->r9;
65 args.return_value = 0;
66 args.return_state = status;
67
68 // Call the handler
69 uint64_t syscall = status->rax;
70 if (m_syscall_handlers[syscall] != nullptr)
71 args = *(m_syscall_handlers[syscall](&args));
72 else
73 Logger::ERROR() << "Syscall " << syscall << " not found\n";
74
75 // If there is a specific return state, use that
76 if (args.return_state != status)
77 return args.return_state;
78
79 // Update the cpu state
80 status->rdi = args.arg0;
81 status->rsi = args.arg1;
82 status->rdx = args.arg2;
83 status->r10 = args.arg3;
84 status->r8 = args.arg4;
85 status->r9 = args.arg5;
86 status->rax = args.return_value;
87
88 // Return the status
89 return status;
90}
91
98void SyscallManager::set_syscall_handler(SyscallType syscall, syscall_func_t handler) {
99
100 m_syscall_handlers[(uint8_t) syscall] = handler;
101}
102
108void SyscallManager::remove_syscall_handler(SyscallType syscall) {
109
110 m_syscall_handlers[(uint8_t) syscall] = nullptr;
111}
112
113
121
122 // Ensure a message was provided
123 char* message = (char*) args->arg0;
124 if(!message)
125 return args;
126
127 // If the first two characters are %h then no header
128 if (message[0] == '%' && message[1] == 'h')
129 Logger::Out() << message + 2;
130 else
131 Logger::INFO() << message;
132
133 return args;
134}
135
143
144
145 // Malloc the memory
146 size_t size = args->arg0;
147 void* address = MemoryManager::malloc(size);
148
149 // Return the address
150 args->return_value = (uint64_t)address;
151 return args;
152}
153
161
162 // Free the memory
163 void* address = (void*) args->arg0;
164 MemoryManager::free(address);
165
166 return args;
167}
168
176
177 // Parse params
178 auto type = (resource_type_t)args->arg0;
179 auto name = (char*)args->arg1;
180 auto flags = (size_t)args->arg2;
181 auto data = (uintptr_t)args->arg3;
182
183 // Try to create the resource
184 auto resource = GlobalResourceRegistry::get_registry(type)->create_resource(name, flags, data);
185
186 // Handle response
187 args->return_value = resource ? 1 : 0;
188 return args;
189}
190
198
199 // Parse params
200 auto type = (resource_type_t)args->arg0;
201 auto name = (char*)args->arg1;
202 auto flags = (size_t)args->arg2;
203
204 // Open the resource
206
207 return args;
208}
209
217
218 // Parse params
219 auto handle = (uint64_t )args->arg0;
220 auto flags = (size_t)args->arg1;
221
222 // Close the resource
224 return args;
225}
226
234
235 // Parse params
236 auto handle = (uint64_t )args->arg0;
237 auto buffer = (void*)args->arg1;
238 auto size = (size_t)args->arg2;
239 auto flags = (size_t)args->arg3;
240
241 // Open the resource
243 if(!resource){
244 args->return_value = 0;
245 return args;
246 }
247
248 // Write to the resource
249 args->return_value = resource->write(buffer,size,flags);
250 return args;
251}
252
260
261 // Parse params
262 auto handle = (uint64_t)args->arg0;
263 auto buffer = (void*)args->arg1;
264 auto size = (size_t)args->arg2;
265 auto flags = (size_t)args->arg3;
266
267 // Open the resource
269 if(!resource){
270 args->return_value = 0;
271 return args;
272 }
273
274 // Write to the resource
275 args->return_value = resource->read(buffer,size,flags);
276 return args;
277}
278
288
289 // Parse params
290 auto name = (char*)args->arg0;
291 auto type = (size_t)args->arg1;
292
293 // Create the registry
294 auto registery = new BridgeResourceRegistry(name, type);
295
296 args->return_value = registery ? 1 : 0;
297 return args;
298}
static LogRecord ERROR()
Gets the active logger for error logs.
Definition logger.cpp:222
static LogRecord INFO()
Gets the active logger for info logs.
Definition logger.cpp:195
static LogRecord Out()
Gets the active logger.
Definition logger.cpp:176
Handles a certain interrupt number.
Definition interrupts.h:44
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 Process * current_process()
Gets the process on the currently executing core.
resources::ResourceManager resource_manager
The manger for resources used by this process.
Definition process.h:122
virtual Resource * create_resource(const string &name, size_t flags, uintptr_t data)
Try to create a new resource with the specified name, will fail if the name is in use.
Definition resource.cpp:134
A generic wrapper for a registry of resources that exist on a userspace server.
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
Definition resource.cpp:161
Resource * get_resource(uint64_t handle)
Gets a resource based on the handle id.
Definition resource.cpp:282
void close_resource(uint64_t handle, size_t flags)
Un registers the resource and then closes it.
Definition resource.cpp:262
uint64_t open_resource(resource_type_t type, const string &name, size_t flags)
Registers a resource with the resource manager and then opens it.
Definition resource.cpp:236
static syscall_args_t * syscall_klog(syscall_args_t *args)
System call to log a message to the kernel log.
Definition syscalls.cpp:120
static syscall_args_t * syscall_resource_create(syscall_args_t *args)
System call to create a resource.
Definition syscalls.cpp:175
static syscall_args_t * syscall_resource_close(syscall_args_t *args)
System call to close a resource.
Definition syscalls.cpp:216
static syscall_args_t * syscall_resource_open(syscall_args_t *args)
System call to open a resource.
Definition syscalls.cpp:197
static syscall_args_t * syscall_resource_registry_create(syscall_args_t *args)
System call to create a new resource registry.
Definition syscalls.cpp:287
SyscallManager()
Construct a new Syscall Manager object and register the syscall handlers. Registers to interrupt 0x80...
Definition syscalls.cpp:24
void set_syscall_handler(::MaxOS::KPI::SyscallType syscall, syscall_func_t handler)
Loads a syscall handler into the manager.
Definition syscalls.cpp:98
static syscall_args_t * syscall_free_memory(syscall_args_t *args)
System call to free a block of memory.
Definition syscalls.cpp:160
static syscall_args_t * syscall_resource_write(syscall_args_t *args)
System call to write to a resource.
Definition syscalls.cpp:233
void remove_syscall_handler(::MaxOS::KPI::SyscallType syscall)
Removes a syscall handler from the manager.
Definition syscalls.cpp:108
static syscall_args_t * syscall_allocate_memory(syscall_args_t *args)
System call to allocate a block of memory.
Definition syscalls.cpp:142
static syscall_args_t * syscall_resource_read(syscall_args_t *args)
System call to read from a resource.
Definition syscalls.cpp:259
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...
KPI::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.
Definition resource.h:25
The arguments passed to a syscall (simplified representation of values in registers)
Definition syscalls.h:37
uint64_t arg0
First argument (in rdi)
Definition syscalls.h:39
uint64_t arg1
Second argument (in rsi)
Definition syscalls.h:40
uint64_t return_value
The return value of the syscall (in rax)
Definition syscalls.h:45
uint64_t arg4
Fifth argument (in r8)
Definition syscalls.h:43
uint64_t arg3
Fourth argument (in rcx)
Definition syscalls.h:42
uint64_t arg2
Third argument (in rdx)
Definition syscalls.h:41
cpu_status_t * return_state
The CPU state to return to after the syscall.
Definition syscalls.h:46
uint64_t arg5
Sixth argument (in r9)
Definition syscalls.h:44
Defines the SyscallManager class for handling system calls from userspace applications.