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_t * handle_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 (::MaxOS::KPI::SyscallType syscall, syscall_func_t handler)
 Loads a syscall handler into the manager.
 
void remove_syscall_handler (::MaxOS::KPI::SyscallType syscall)
 Removes a syscall handler from the manager.
 

Static Public Member Functions

static syscall_args_t * syscall_klog (syscall_args_t *args)
 System call to log a message to the kernel log.
 
static syscall_args_t * syscall_allocate_memory (syscall_args_t *args)
 System call to allocate a block of memory.
 
static syscall_args_t * syscall_free_memory (syscall_args_t *args)
 System call to free a block of memory.
 
static syscall_args_t * syscall_resource_create (syscall_args_t *args)
 System call to create a resource.
 
static syscall_args_t * syscall_resource_open (syscall_args_t *args)
 System call to open a resource.
 
static syscall_args_t * syscall_resource_close (syscall_args_t *args)
 System call to close a resource.
 
static syscall_args_t * syscall_resource_write (syscall_args_t *args)
 System call to write to a resource.
 
static syscall_args_t * syscall_resource_read (syscall_args_t *args)
 System call to read from a resource.
 
static syscall_args_t * syscall_resource_registry_create (syscall_args_t *args)
 System call to create a new resource registry.
 

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 59 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 24 of file syscalls.cpp.

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}
static LogRecord INFO()
Gets the active logger for info logs.
Definition logger.cpp:195
Handles a certain interrupt number.
Definition interrupts.h:44
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
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
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

References MaxOS::Logger::INFO(), set_syscall_handler(), syscall_allocate_memory(), syscall_free_memory(), syscall_klog(), syscall_resource_close(), syscall_resource_create(), syscall_resource_open(), syscall_resource_read(), syscall_resource_registry_create(), and syscall_resource_write().

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 55 of file syscalls.cpp.

55 {
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}
static LogRecord ERROR()
Gets the active logger for error logs.
Definition logger.cpp:222
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

References MaxOS::system::SyscallArguments::arg0, MaxOS::system::SyscallArguments::arg1, MaxOS::system::SyscallArguments::arg2, MaxOS::system::SyscallArguments::arg3, MaxOS::system::SyscallArguments::arg4, MaxOS::system::SyscallArguments::arg5, MaxOS::Logger::ERROR(), MaxOS::system::SyscallArguments::return_state, and MaxOS::system::SyscallArguments::return_value.

◆ remove_syscall_handler()

void SyscallManager::remove_syscall_handler ( ::MaxOS::KPI::SyscallType  syscall)

Removes a syscall handler from the manager.

Parameters
syscallThe syscall ID number

Definition at line 108 of file syscalls.cpp.

108 {
109
110 m_syscall_handlers[(uint8_t) syscall] = nullptr;
111}

◆ set_syscall_handler()

void SyscallManager::set_syscall_handler ( ::MaxOS::KPI::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 98 of file syscalls.cpp.

98 {
99
100 m_syscall_handlers[(uint8_t) syscall] = handler;
101}

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 142 of file syscalls.cpp.

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

References MaxOS::system::SyscallArguments::arg0, MaxOS::memory::MemoryManager::malloc(), and MaxOS::system::SyscallArguments::return_value.

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 160 of file syscalls.cpp.

160 {
161
162 // Free the memory
163 void* address = (void*) args->arg0;
164 MemoryManager::free(address);
165
166 return args;
167}
static void free(void *pointer)
Frees a block of memory using the current memory manager.

References MaxOS::system::SyscallArguments::arg0, and 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 120 of file syscalls.cpp.

120 {
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}
static LogRecord Out()
Gets the active logger.
Definition logger.cpp:176

References MaxOS::system::SyscallArguments::arg0, 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 216 of file syscalls.cpp.

216 {
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}
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
void close_resource(uint64_t handle, size_t flags)
Un registers the resource and then closes it.
Definition resource.cpp:262

References MaxOS::system::SyscallArguments::arg0, MaxOS::system::SyscallArguments::arg1, MaxOS::processes::resources::ResourceManager::close_resource(), MaxOS::processes::GlobalScheduler::current_process(), and MaxOS::processes::Process::resource_manager.

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 175 of file syscalls.cpp.

175 {
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}
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
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
Definition resource.cpp:161
KPI::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.
Definition resource.h:25

References MaxOS::system::SyscallArguments::arg0, MaxOS::system::SyscallArguments::arg1, MaxOS::system::SyscallArguments::arg2, MaxOS::system::SyscallArguments::arg3, MaxOS::processes::resources::BaseResourceRegistry::create_resource(), MaxOS::processes::resources::GlobalResourceRegistry::get_registry(), and MaxOS::system::SyscallArguments::return_value.

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 197 of file syscalls.cpp.

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

References MaxOS::system::SyscallArguments::arg0, MaxOS::system::SyscallArguments::arg1, MaxOS::system::SyscallArguments::arg2, MaxOS::processes::GlobalScheduler::current_process(), MaxOS::processes::resources::ResourceManager::open_resource(), MaxOS::processes::Process::resource_manager, and MaxOS::system::SyscallArguments::return_value.

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 259 of file syscalls.cpp.

259 {
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}
Resource * get_resource(uint64_t handle)
Gets a resource based on the handle id.
Definition resource.cpp:282

References MaxOS::system::SyscallArguments::arg0, MaxOS::system::SyscallArguments::arg1, MaxOS::system::SyscallArguments::arg2, MaxOS::system::SyscallArguments::arg3, MaxOS::processes::GlobalScheduler::current_process(), MaxOS::processes::resources::ResourceManager::get_resource(), MaxOS::processes::Process::resource_manager, and MaxOS::system::SyscallArguments::return_value.

Referenced by SyscallManager().

◆ syscall_resource_registry_create()

syscall_args_t * SyscallManager::syscall_resource_registry_create ( syscall_args_t *  args)
static

System call to create a new resource registry.

Todo:
resource type system
Parameters
argsArg0 = Name of the shared memory region Arg1 = Resource Type
Returns
1 for success 0 for failure

Definition at line 287 of file syscalls.cpp.

287 {
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}
A generic wrapper for a registry of resources that exist on a userspace server.

References MaxOS::system::SyscallArguments::arg0, MaxOS::system::SyscallArguments::arg1, and MaxOS::system::SyscallArguments::return_value.

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 233 of file syscalls.cpp.

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

References MaxOS::system::SyscallArguments::arg0, MaxOS::system::SyscallArguments::arg1, MaxOS::system::SyscallArguments::arg2, MaxOS::system::SyscallArguments::arg3, MaxOS::processes::GlobalScheduler::current_process(), MaxOS::processes::resources::ResourceManager::get_resource(), MaxOS::processes::Process::resource_manager, and MaxOS::system::SyscallArguments::return_value.

Referenced by SyscallManager().


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