Page History

SystemCalls

lukas edited this page on 12 Dec 2022

Clone this wiki locally
You can clone HTTP or SSH.

System call

Fundamentals

All system calls used for communication with the kernel are initialized using the sysenter x86 instruction. There are a total of 4 parameters which can be passed to the system call handler in the kernel and these parameters will then be used to pass data to the handler. Information about the system call is communicated using the cpu according to the following table:

Register Useage description
STACK return address before calling sysenter push the value EIP should take when the system call is completed and the task can be resumed. (the last thing the kernel does is call the ret instruction)
EAX syscall function the numeric function id of the system call. This value serves as an index for different handlers for different system calls in the kernel
EBX parameter 0 the first parameter the system call handler will receive
ECX parameter 1 the second parameter the system call handler will receive
EDX parameter 2 the third parameter the system call handler will receive
ESI parameter 3 the fourth parameter the system call handler will receive
EDI return stack the value ESP should take before returning from the system call. Keep in mind the return address should be on top of this return stack value.

When returning to the service from the system call, the kernel will set EAX to an appropriate return value.

System call functions

There are several different system call functions which are differentiated using the EAX (syscall function) register when calling sysenter.

Function 0: exit task

System call function 0 is used internally in the kernel to signal a task exiting successfully.

Function 1: Install provider function

parameter name description
0 Function name The string Id of the intended name of the new function
1 Function address the virtual address (of the calling service) of the function's entry point
return value function id the id of the installed function which can be used to call this function using the request system call

The Install provider system call lets a service install a new public function. This function can be called from any other service using the request system call.

Function 2: Request system call

parameter name description
0 target service the service id of the targeted service
1 target function the function id of the requested function
2 parameter 0 the first parameter passed to the handler function of the request
3 parameter 1 the second parameter passed to the handler function of the request

The Request system call will call a public function of a certain target service with 2 custom parameters. The call will only be resumed whenever the request has finished processing and a result is available.

Function 3: IO in system call

parameter name description
0 port the targeted IO port
1 size the targeted IO port width

The IO in system call lets services access the normally hidden function in which uses the IO-port of the processor to communicate with external equipment. The size determines the operator width of this operation and must be either 1, 2 or 4 depending on the intended result and represents the number of bytes read. Other values will be treated as invalid and will not call any in-instruction. The result of this operation will be the return value of the system call.

Function 4: IO out system call

parameter name description
0 port the targeted IO port
1 size the targeted IO port width

The IO out system call lets services access the normally hidden function out which uses the IO-port of the processor to communicate with external equipment. The size determines the operator width of this operation and must be either 1, 2 or 4 depending on the intended result and represents the number of bytes written. Other values will be treated as invalid and will not call any out-instruction.

Function 5: Load program from initrd system call

parameter name description
0 program name a string ID describing the program name to be loaded

The Load program from initrd system call fetches a ELF file from the initial ramdisk, prepares its content as a new service and calls its main / initialize function. Whenever this system call is called, the loadInitrd kernel event will also be fired.

Function 5: Retrieve Service ID system call

parameter name description
0 service name a string ID pointing to the service's name

The Retrieve Service ID system call searches the list of installed services and returns its id (its index) to be used in cunjunction with other system calls to uniquely identify a service.

Function 6: Retrieve Service function ID system call

parameter name description
0 service ID the target service's service Id, obtained through a Retrieve Service ID system call
1 function name a string id pointing to the name of the function to be searched for

The Retrieve Service function ID system call searches the list of registered functions of the service identified by the service ID-Parameter and returns the corresponding id of the function matching the function name.

Function 7: Subscribe to interrrupt system call

This function is soon to be deprecated to be replaced by events fired by the kernel

parameter name description
0 interrupt number a index in the range (0 - 255) describing the interrupt to be subscribed to
1 handler function a pointer to the handler function (virtual address of calling service)

The Subscribe to interrrupt system call adds a subscription to an interrupt described by the interrupt number parameter. Whenever the specified interrupt is triggered, all subscriptions shall be added to the processing queue by the kernel.

Function 8: Create event system call

parameter name description
0 event name a string ID describing the event

The Create event system call registers a new event for the calling service, described by its event name. Using a Fire event system call, all functions subscribed to this event using the Subscribe event system call will be called.

Function 9: Retrieve event ID system call

parameter name description
0 target service ID the service ID of the service which provides the event
1 event name a string Id containing the service name to be retrieved

The Retrieve event ID system call searches the events the targeted service has already registered using a Create event system call and returns the resulting event's ID.

Function 10: Fire event system call

parameter name description
0 event id the id of the event returned by a Create event system call

The Fire event system call calls all handlers that have previously been installed using a Subscribe event system call associated with the calling service.

Function 11: Subscribe event system call

parameter name description
0 service ID the id of the service providing the event, if 0: reference kernel events
1 event id the id of the targeted event
2 handler a pointer to the handling function for the event

The Subscribe event system call adds a new handler to the list of subscriptions of a certain event which will be fired by a certain service.

Function 12: Get service id system call

parameter name description
0 service name a string ID for the service

The Get service id system call returns the ID of a requested service given its name.

Function 13: Insert string system call

parameter name description
0 string a pointer to a null-terminated string in the service's virtual memory

The Insert string system call copies a string pointed to by the caller into the kernel's memory and returns the determined string ID.

Function 14: Read string length system call

parameter name description
0 string ID a string ID of the string whose length is desired

The Read string length system call retrieves a string given its ID from the kernel string map and returns its length.

Function 15: Read string system call

parameter name description
0 string ID a string ID of the string whose length is desired
1 data a pointer to the desired location where the string data should be transferred

The Read string system call retrieves a string by its string ID from the string map and copies it into the data-field for the calling service.

Function 16: Discard string system call

parameter name description
0 string ID the ID of the string which should be discarded

The Discard string system call purges the desired string from the kernel string map and frees its associated memory.

Function 17: Request memory system call

parameter name description
0 page count the number of 4096-byte pages requested
1 target address the preferred address of the memory in the service's virtual memory, a free slot will be chosen if this field is 0
2 physical address the preferred physical address of the allocated memory, a free slot will be chosen if this field is 0

The Request memory system call reserves page count physical pages for a service and maps it to a target address. The new virtual address of the allocated memory will be returned.