Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::system::SyscallManager Class Reference

Provides an API for userspace applications to interact with the kernel. More...

#include <syscalls.h>

Inheritance diagram for MaxOS::system::SyscallManager:
MaxOS::hardwarecommunication::InterruptHandler

Public Member Functions

 SyscallManager ()
 Construct a new Syscall Manager object and register the syscall handlers. Registers to interrupt 0x80.
 
cpu_status_thandle_interrupt (cpu_status_t *esp) final
 Loads the args from the registers and delegates the syscall to the relevant handler if defined.
 
void set_syscall_handler (::syscore::SyscallType syscall, syscall_func_t handler)
 Loads a syscall handler into the manager.
 
void remove_syscall_handler (::syscore::SyscallType syscall)
 Removes a syscall handler from the manager.
 

Static Public Member Functions

static syscall_args_tsyscall_close_process (syscall_args_t *args)
 System call to close a process.
 
static syscall_args_tsyscall_klog (syscall_args_t *args)
 System call to log a message to the kernel log.
 
static syscall_args_tsyscall_allocate_memory (syscall_args_t *args)
 System call to allocate a block of memory.
 
static syscall_args_tsyscall_free_memory (syscall_args_t *args)
 System call to free a block of memory.
 
static syscall_args_tsyscall_resource_create (syscall_args_t *args)
 System call to create a resource.
 
static syscall_args_tsyscall_resource_open (syscall_args_t *args)
 System call to open a resource.
 
static syscall_args_tsyscall_resource_close (syscall_args_t *args)
 System call to close a resource.
 
static syscall_args_tsyscall_resource_write (syscall_args_t *args)
 System call to write to a resource.
 
static syscall_args_tsyscall_resource_read (syscall_args_t *args)
 System call to read from a resource.
 
static syscall_args_tsyscall_thread_yield (syscall_args_t *args)
 System call to yield the current process.
 
static syscall_args_tsyscall_thread_sleep (syscall_args_t *args)
 System call to sleep the current process.
 
static syscall_args_tsyscall_thread_close (syscall_args_t *args)
 System call to close a thread.
 

Detailed Description

Provides an API for userspace applications to interact with the kernel.

Todo:
Very c style, should be made class based that automatically registers

Definition at line 58 of file syscalls.h.

Constructor & Destructor Documentation

◆ SyscallManager()

SyscallManager::SyscallManager ( )

Construct a new Syscall Manager object and register the syscall handlers. Registers to interrupt 0x80.

Definition at line 23 of file syscalls.cpp.

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}
static Logger INFO()
Gets active logger set to info level.
Definition logger.cpp:171
Handles a certain interrupt number.
Definition interrupts.h:30
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
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

References MaxOS::Logger::INFO(), set_syscall_handler(), syscall_allocate_memory(), syscall_close_process(), syscall_free_memory(), syscall_klog(), syscall_resource_close(), syscall_resource_create(), syscall_resource_open(), syscall_resource_read(), syscall_resource_write(), syscall_thread_close(), syscall_thread_sleep(), and syscall_thread_yield().

Member Function Documentation

◆ handle_interrupt()

cpu_status_t * SyscallManager::handle_interrupt ( cpu_status_t status)
finalvirtual

Loads the args from the registers and delegates the syscall to the relevant handler if defined.

Parameters
statusThe cpu state
Returns
The cpu state (may be modified with return value in RAX or a new state by the handler)

Reimplemented from MaxOS::hardwarecommunication::InterruptHandler.

Definition at line 52 of file syscalls.cpp.

52 {
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}
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
The arguments passed to a syscall (simplified representation of values in registers)
Definition syscalls.h:36

References MaxOS::Logger::ERROR().

◆ remove_syscall_handler()

void SyscallManager::remove_syscall_handler ( ::syscore::SyscallType  syscall)

Removes a syscall handler from the manager.

Parameters
syscallThe syscall ID number

Definition at line 105 of file syscalls.cpp.

105 {
106
107 m_syscall_handlers[(uint8_t) syscall] = nullptr;
108}

◆ set_syscall_handler()

void SyscallManager::set_syscall_handler ( ::syscore::SyscallType  syscall,
syscall_func_t  handler 
)

Loads a syscall handler into the manager.

Parameters
syscallThe syscall ID number
handlerThe handler to set

Definition at line 95 of file syscalls.cpp.

95 {
96
97 m_syscall_handlers[(uint8_t) syscall] = handler;
98}

Referenced by SyscallManager().

◆ syscall_allocate_memory()

syscall_args_t * SyscallManager::syscall_allocate_memory ( syscall_args_t args)
static

System call to allocate a block of memory.

Parameters
argsArg0 = size
Returns
The address of the memory block or null if failed

Definition at line 169 of file syscalls.cpp.

169 {
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}
static void * malloc(size_t size)
Allocates a block of memory in the current USERSPACE heap.

References MaxOS::memory::MemoryManager::malloc().

Referenced by SyscallManager().

◆ syscall_close_process()

syscall_args_t * SyscallManager::syscall_close_process ( system::syscall_args_t args)
static

System call to close a process.

Parameters
argsArg0 = pid Arg1 = exit code
Returns
Nothing

Definition at line 117 of file syscalls.cpp.

117 {
118
119 // Get the args
120 uint64_t pid = args->arg0;
121 int exit_code = (int) args->arg1;
122
123 // Close the process
124 Process* process = pid == 0 ? GlobalScheduler::current_process() : GlobalScheduler::get_process(pid);
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}
The global scheduler that manages all processes and threads across all cores.
Definition scheduler.h:26
static Scheduler * core_scheduler()
Gets the scheduler for the currently executing core.
static GlobalScheduler * system_scheduler()
Gets the system scheduler.
Definition scheduler.cpp:50
static Process * current_process()
Gets the process 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
struct PACKED MaxOS::system::CPUStatus cpu_status_t
Alias for CPUStatus struct.

References MaxOS::processes::GlobalScheduler::core_scheduler(), MaxOS::processes::GlobalScheduler::current_process(), MaxOS::processes::GlobalScheduler::get_process(), and MaxOS::processes::GlobalScheduler::system_scheduler().

Referenced by SyscallManager().

◆ syscall_free_memory()

syscall_args_t * SyscallManager::syscall_free_memory ( syscall_args_t args)
static

System call to free a block of memory.

Parameters
argsArg0 = address
Returns
Nothing

Definition at line 186 of file syscalls.cpp.

186 {
187
188 // Free the memory
189 void* address = (void*) args->arg0;
190 MemoryManager::free(address);
191
192 return args;
193}
static void free(void *pointer)
Frees a block of memory using the current memory manager.

References MaxOS::memory::MemoryManager::free().

Referenced by SyscallManager().

◆ syscall_klog()

syscall_args_t * SyscallManager::syscall_klog ( syscall_args_t args)
static

System call to log a message to the kernel log.

Parameters
argsArg0 = message
Returns
The same args structure

Definition at line 141 of file syscalls.cpp.

141 {
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}
static Logger & Out()
Gets the active logger.
Definition logger.cpp:149

References MaxOS::Logger::INFO(), and MaxOS::Logger::Out().

Referenced by SyscallManager().

◆ syscall_resource_close()

syscall_args_t * SyscallManager::syscall_resource_close ( syscall_args_t args)
static

System call to close a resource.

Parameters
argsArg0 = Handle Arg1 = Flags
Returns
Nothing

Definition at line 240 of file syscalls.cpp.

240 {
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}

References MaxOS::processes::GlobalScheduler::current_process().

Referenced by SyscallManager().

◆ syscall_resource_create()

syscall_args_t * SyscallManager::syscall_resource_create ( syscall_args_t args)
static

System call to create a resource.

Parameters
argsArg0 = ResourceType Arg1 = Name Arg2 = Flags
Returns
1 if resource was created sucessfully, 0 otherwise

Definition at line 201 of file syscalls.cpp.

201 {
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}
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
Definition resource.cpp:217

References MaxOS::processes::GlobalResourceRegistry::get_registry().

Referenced by SyscallManager().

◆ syscall_resource_open()

syscall_args_t * SyscallManager::syscall_resource_open ( syscall_args_t args)
static

System call to open a resource.

Parameters
argsArg0 = ResourceType Arg1 = Name Arg2 = Flags
Returns
The handle of the resource or 0 if failed

Definition at line 222 of file syscalls.cpp.

222 {
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}

References MaxOS::processes::GlobalScheduler::current_process().

Referenced by SyscallManager().

◆ syscall_resource_read()

syscall_args_t * SyscallManager::syscall_resource_read ( syscall_args_t args)
static

System call to read from a resource.

Parameters
argsArg0 = Handle Arg1 = Buffer Arg2 = Size Arg3 = Flags
Returns
The number of bytes read or 0 if failed

Definition at line 283 of file syscalls.cpp.

283 {
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}

References MaxOS::processes::GlobalScheduler::current_process().

Referenced by SyscallManager().

◆ syscall_resource_write()

syscall_args_t * SyscallManager::syscall_resource_write ( syscall_args_t args)
static

System call to write to a resource.

Parameters
argsArg0 = Handle Arg1 = Buffer Arg2 = Size Arg3 = Flags
Returns
The number of bytes written or 0 if failed

Definition at line 257 of file syscalls.cpp.

257 {
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}

References MaxOS::processes::GlobalScheduler::current_process().

Referenced by SyscallManager().

◆ syscall_thread_close()

syscall_args_t * SyscallManager::syscall_thread_close ( syscall_args_t args)
static

System call to close a thread.

Parameters
argsArg0 = tid Arg1 = exit code
Returns
Nothing

Definition at line 342 of file syscalls.cpp.

342 {
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
349 Thread* thread = tid == 0 ? GlobalScheduler::current_thread() : GlobalScheduler::get_thread(tid);
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 Thread * current_thread()
Gets the thread on the currently executing core.
The execution context of a sub-process thread.
Definition process.h:49

References MaxOS::processes::GlobalScheduler::core_scheduler(), MaxOS::processes::GlobalScheduler::current_thread(), and MaxOS::processes::GlobalScheduler::get_thread().

Referenced by SyscallManager().

◆ syscall_thread_sleep()

syscall_args_t * SyscallManager::syscall_thread_sleep ( syscall_args_t args)
static

System call to sleep the current process.

Parameters
argsArg0 = milliseconds
Returns
Nothing

Definition at line 323 of file syscalls.cpp.

323 {
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}
static system::cpu_status_t * yield(system::cpu_status_t *current)
Pass execution to the next thread.
Definition scheduler.cpp:79

References MaxOS::processes::GlobalScheduler::current_thread(), and MaxOS::processes::GlobalScheduler::yield().

Referenced by SyscallManager().

◆ syscall_thread_yield()

syscall_args_t * SyscallManager::syscall_thread_yield ( syscall_args_t args)
static

System call to yield the current process.

Parameters
argsNothing
Returns
Nothing

Definition at line 309 of file syscalls.cpp.

309 {
310
311 // Yield
312 args->return_state = GlobalScheduler::yield(args->return_state);
313
314 return args;
315}

References MaxOS::processes::GlobalScheduler::yield().

Referenced by SyscallManager().


The documentation for this class was generated from the following files: