Expand description
§pw_kernel User Space API
§Core Concepts
§Objects
Objects are the basic building block of functionality exposed to user space. They are polymorphic and may be one of a limited set of types:
- Channel
- Wait Group
- TODO: https://pwbug.dev/496975012 - add other objects.
§Handles
All system calls reference objects through a u32 handle which indexes into a process-local handle table.
§Signals and Waiting
Every kernel object has a set of signals that can be pending and waited upon. The exact meaning and semantics of each signal vary between kernel objects. The signal types are:
Signals::READABLE: Object is readable.Signals::WRITABLE: Object is writable.Signals::ERROR: Object is in an error state.Signals::USER: User defined signal. Useful for out of band signaling between peers on a Channel.
Any kernel object can be waited for signals to assert using the
object_wait() syscall. Multiple objects can be waited on simultaneously
using a Wait Group object.
§Open Questions
- What are the multi-threaded semantics of waiting.
§Object and Handle Creation
Initially only statically defined and allocated objects and handles are supported. The creation of these will be driven through build time configuration and the necessary code will be generated to allocate kernel data structures as well as expose handle definitions to user space processes.
§Object Types
§Channel
A channel is a unidirectional connection between two asymmetric peers: an initiator and a handler. A channel allows the initiator peer to send a buffer of data to the handler peer and wait for it’s response. A channel can have a maximum of one transaction pending at a time and is designed to not require intermediate kernel buffers.
Both a synchronous and asynchronous API is offered to the initiator while the handler side API is strictly non-blocking. All data copies between peers happen during the system call.
The flow of a transaction is as follows:
- The initiator starts the transaction by providing send and receive
buffers to one of the two transact system calls (
channel_transact()orchannel_async_transact()). This has the additional side effect of clearingSignals::READABLEandSignals::WRITABLEon the initiator. - The handler’s
Signals::READABLEwill become asserted. - The handler can now read the message in multiple calls to
channel_read()causing the kernel to copy the data from the initiator’s send buffer to the buffer provided tochannel_read(). - The handler completes the transaction by calling
channel_respond()and providing a response. The kernel will immediately copy the response from the handler’s buffer to the initiator’s receive buffer. There is no built in mechanism for the handler to signal an error to the initiator. This is left to the higher level protocol used to communicate over the channel. This will clearSignals::READABLEandSignals::WRITABLEon the handler and raiseSignals::READABLEon the initiator.
The handler’s only ways of communicating with the initiator are by
- responding to an initiated transaction
- raising
Signals::USERon the initiator by callingobject_set_peer_user_signal()
§Initiator Signals
Signals::WRITABLEindicates there is no pending transaction and one can be started. Cleared on transaction initiation.Signals::READABLEindicates the handler has responded to the pending transaction. Cleared on transaction initiation.Signals::ERRORindicates pending transaction has an error. Cleared when the initiator is waited on.Signals::USERindicates the handler callsobject_set_peer_user_signal(). Cleared when the handler callsobject_set_peer_user_signal()withset=false, or automatically by the kernel if the initiator handler terminates. Preserved on initiator process termination.
§Handler Signals
Signals::READABLEindicates there is a pending transaction. Cleared when the handler callschannel_respond().Signals::WRITABLEindicates there is a pending transaction. Cleared when the handler callschannel_respond().Signals::ERRORindicates a pending transaction error. No error states are defined at the moment. In the future an error may be raised when the remote peer closes.Signals::USERindicates the initiator callsobject_set_peer_user_signal(). Cleared when the initiator callsobject_set_peer_user_signal()withset=false, or automatically by the kernel if the initiator process terminates. Preserved on handler process termination.
§Wait Group
Wait groups provide a mechanism for waiting on multiple handles at once.
Handles can be added to and removed from a wait group with wait_group_add()
and wait_group_remove(). In addition to a set of signals to wait on,
an arbitrary user_data is provided to wait_group_add(). This value
is returned by object_wait(), unmodified by the kernel, when the wait group is
waited on.
§Open questions:
- How is the wait group’s member list allocated in there kernel. To support
a fully statically allocated kernel one of two approaches are being
considered:
- an object (or possibly handle) may only be in a single wait group at one time. This allows a wait group to maintain an intrusive list of objects with the list element storage being stored in the object.
- wait queues are statically sized at compile time and adding more objects than there is space for will return an error.
§Interrupt
Interrupt objects provide a mechanism for handling hardware interrupts.
A single Interrupt object can be configured to handle multiple interrupt
sources (IRQs), up to a maximum of 16 per object.
Each interrupt source handled by an Interrupt object is mapped to a unique
signal bit within the higher 16 bits of Signals. These signals
range from Signals::INTERRUPT_A (bit 16) to Signals::INTERRUPT_O (bit 30).
When a interrupt occurs for a source handled by an Interrupt object, the kernel
masks the interrupt and then signals the corresponding INTERRUPT_ bit on the
object. Userspace threads can wait on the Interrupt object using the
object_wait() syscall.
Upon waking from object_wait(), the returned Signals mask indicates which
interrupt(s) have triggered. After the userspace handler has serviced the
interrupt(s), it must call the interrupt_ack() syscall to allow the
interrupt(s) to be triggered again. This syscall must be provided with a
Signals mask containing the bits for the interrupts that have been
handled.
interrupt_ack() accomplishes two things:
- It clears the specified signal bits on the Interrupt object, allowing it to receive new signals for those interrupts.
- It signals to the underlying hardware interrupt controller (e.g., PLIC or NVIC) that the associated IRQ(s) have been acknowledged, re-enabling them at the hardware level.
§Futex
In design
§System Calls
The C ABI system calls listed here are not intended to be called directly by user space code and instead be accessed through language idiomatic wrapper libraries.
§Generic Syscalls
§Channel Initiator Syscalls
channel_transact()channel_async_transact()channel_async_transact_complete()channel_async_cancel()
§Channel Handler Syscalls
§Wait Group Syscalls
Structs§
- Signals
- A set of object signals
- SysCall
Return Value - Wait
Return - Return value from the
object_wait()syscall.
Enums§
- Exit
Status - Exit status of a process or thread.
- SysCall
Id
Traits§
Functions§
- channel_
async_ ⚠cancel - Cancels a pending transaction on a channel
- channel_
async_ ⚠transact - Perform an asynchronous channel transaction
- channel_
async_ ⚠transact_ complete - Completes an asynchronous channel transaction
- channel_
read ⚠ - Perform a non-blocking read from a pending transaction.
- channel_
respond ⚠ - Respond to and complete a pending transaction
- channel_
transact ⚠ - Perform a synchronous channel transaction
- interrupt_
ack ⚠ - Acknowledges the signaled interrupts allowing them to be signaled again.
- object_
set_ ⚠peer_ user_ signal - Set or clear
Signals::USERon the paired peer. - object_
wait ⚠ - Wait on a single object
- wait_
group_ ⚠add - Adds an object to a wait group
- wait_
group_ ⚠remove - Removes an object from a wait group