Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::resources::BridgeHandler Class Reference

A handler to manage resource commands over shared memory. More...

#include <resource_bridge.h>

Inheritance diagram for MaxOS::processes::resources::BridgeHandler:
MaxOS::memory::allocator::BumpAllocator MaxOS::memory::allocator::MemoryAllocator

Public Member Functions

 BridgeHandler (string const &shared_name, Process *owner_process)
 Constructs a handler to manage resource commands over shared memory.
 
size_t allocate_id ()
 Allocates an ID to identify a resource on the bridge.
 
int64_t send_to_bridge (size_t id, KPI::ServiceResourceCommand command, const void *buffer, size_t size, size_t flags)
 Send a command and its associated data to the endpoint associated with this resource.
 
- Public Member Functions inherited from MaxOS::memory::allocator::BumpAllocator
 BumpAllocator ()
 Creates a new bump memory allocator.
 
virtual ~BumpAllocator ()
 Destructs a Bump memory allocator.
 
void setup_region (uintptr_t address, size_t length) final
 Setup a region to be bump memory managed, creates a free chunk represent the whole region.
 
void * allocate (size_t size) final
 Allocates a block of memory.
 
void unallocate (void *pointer) final
 Frees a block of memory.
 
size_t amount_used () final
 Returns the amount of memory used.
 
size_t align (size_t size) final
 Aligns the size to the chunk alignment.
 
- Public Member Functions inherited from MaxOS::memory::allocator::MemoryAllocator
 MemoryAllocator ()
 
virtual ~MemoryAllocator ()
 

Detailed Description

A handler to manage resource commands over shared memory.

Definition at line 32 of file resource_bridge.h.

Constructor & Destructor Documentation

◆ BridgeHandler()

BridgeHandler::BridgeHandler ( string const &  shared_name,
Process *  owner_process 
)

Constructs a handler to manage resource commands over shared memory.

Definition at line 21 of file resource_bridge.cpp.

22: m_owner_process(owner_process)
23{
24
25 // Get the shared memory region
26 auto shared_memory = (SharedMemory*)GlobalResourceRegistry::get_registry(resource_type_t::SHARED_MEMORY)->get_resource(shared_name);
27 ASSERT(shared_memory != nullptr, "Shared memory resource not found\n");
28
29 // Copy into the kernel
30 ASSERT(shared_memory->size() == SERVICE_SHARED_MEM_SIZE, "Shared memory resource size mismatch\n");
31 auto address = MemoryManager::s_kernel_memory_manager->vmm()->load_shared_memory(shared_memory->physical_address(), SERVICE_SHARED_MEM_SIZE);
32
33 // Setup the message region
34 m_message_ring = (service_message_ring_t*)address;
35
36 // Setup the data region
37 m_data_region = (uintptr_t)address + MESSAGE_SIZE;
38 setup_region(m_data_region, DATA_SIZE);
39}
static MemoryManager * s_kernel_memory_manager
The memory manager for any kernel processes and all kernel allocations.
void * load_shared_memory(const string &name)
Load shared memory into the VMM's address space.
Definition virtual.cpp:487
void setup_region(uintptr_t address, size_t length) final
Setup a region to be bump memory managed, creates a free chunk represent the whole region.
Definition bump.cpp:122
A block memory that is mapped into multiple processes.
Definition ipc.h:45
virtual Resource * get_resource(const string &name)
Gets a resource from the registry by name.
Definition resource.cpp:52
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
Definition resource.cpp:161

References MaxOS::processes::resources::GlobalResourceRegistry::get_registry(), MaxOS::processes::resources::BaseResourceRegistry::get_resource(), MaxOS::memory::VirtualMemoryManager::load_shared_memory(), MaxOS::memory::MemoryManager::s_kernel_memory_manager, MaxOS::memory::allocator::BumpAllocator::setup_region(), and MaxOS::memory::BumpMemoryManager::vmm().

Member Function Documentation

◆ allocate_id()

size_t BridgeHandler::allocate_id ( )

Allocates an ID to identify a resource on the bridge.

Returns
The ID that the resource can use

Definition at line 122 of file resource_bridge.cpp.

122 {
123 return __atomic_fetch_add(&m_next_id, 1, __ATOMIC_RELAXED);
124}

◆ send_to_bridge()

int64_t BridgeHandler::send_to_bridge ( size_t  id,
KPI::ServiceResourceCommand  command,
const void *  buffer,
size_t  size,
size_t  flags 
)

Send a command and its associated data to the endpoint associated with this resource.

Parameters
idThe id of the resource
commandThe command to execute
bufferThe data to pass along with the command
sizeThe size of the data buffer
flagsFlags associated with the command
Todo:
Get slot data before freeing

Definition at line 139 of file resource_bridge.cpp.

139 {
140
141 // Copy data into shared region
142 bool is_read = command == ServiceResourceCommand::R_READ; // || command == BridgeResourceCommand::R_READ_ATTR;
143 bool is_page_mapped = size >= MAX_COPY_SIZE;
144 size_t offset = store_data(buffer, size, is_read);
145
146 // Construct the message
147 auto slot = aquire_slot();
148 __atomic_store_n(&slot->sending_pid, GlobalScheduler::current_process()->pid(), __ATOMIC_RELAXED);
149 __atomic_store_n(&slot->resource_id, id, __ATOMIC_RELAXED);
150 __atomic_store_n(&slot->flags, flags, __ATOMIC_RELAXED);
151 __atomic_store_n(&slot->command, (uint8_t)command, __ATOMIC_RELAXED);
152 __atomic_store_n(&slot->data_size, size, __ATOMIC_RELAXED);
153 __atomic_store_n(&slot->data_offset, offset, __ATOMIC_RELAXED);
154 __atomic_store_n(&slot->data_page_mapped, is_page_mapped, __ATOMIC_RELAXED);
155 __atomic_store_n(&slot->response, 0, __ATOMIC_RELAXED);
156 __atomic_store_n(&slot->state, (uint8_t)ServiceMessageSlotState::REQUEST, __ATOMIC_RELEASE);
157
158 // Wait for a response
159 while (__atomic_load_n(&slot->state, __ATOMIC_ACQUIRE) == (uint8_t)ServiceMessageSlotState::REQUEST)
161
162 // Read request
163 if (is_read && !is_page_mapped)
164 memcpy((void*)buffer, (void*)m_data_region + offset, size);
165
166 // Free resources
167 if (size != 0) {
168 if (is_page_mapped) {
170 MemoryManager::switch_active_memory_manager(m_owner_process->memory_manager);
171 m_owner_process->memory_manager->vmm()->unload_range_from_process((void*)offset, size);
172 MemoryManager::switch_active_memory_manager(caller);
173 }
174 else {
175 m_lock.lock();
176 unallocate((void*)m_data_region + offset);
177 m_lock.unlock();
178 }
179 }
180
181 // Get the response and then release the slot
182 int64_t response = __atomic_load_n(&slot->response, __ATOMIC_RELAXED);
183 __atomic_store_n(&slot->state, (uint8_t)ServiceMessageSlotState::FREE,__ATOMIC_RELEASE);
184
185 return response;
186}
Handles memory allocation and deallocation.
void unload_range_from_process(const void *start_address, size_t size)
Unload a shared range of pages from this vmm.
Definition virtual.cpp:602
void unallocate(void *pointer) final
Frees a block of memory.
Definition bump.cpp:212
static Process * current_process()
Gets the process on the currently executing core.
static Thread * current_thread()
Gets the thread on the currently executing core.
uint64_t pid() const
Gets the pid of the process.
Definition process.cpp:429
memory::MemoryManager * memory_manager
The manager for memory used by this process.
Definition process.h:121
void yield()
Pass execution to the next ready thread.
Definition process.cpp:103

References MaxOS::processes::GlobalScheduler::current_process(), MaxOS::processes::GlobalScheduler::current_thread(), MaxOS::processes::Process::memory_manager, MaxOS::processes::Process::pid(), MaxOS::memory::allocator::BumpAllocator::unallocate(), MaxOS::memory::VirtualMemoryManager::unload_range_from_process(), MaxOS::memory::BumpMemoryManager::vmm(), and MaxOS::processes::Thread::yield().


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