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

Manages the creation, retention and destruction of resources representing processes. More...

#include <scheduler_resources.h>

Inheritance diagram for MaxOS::processes::resources::ProcessResourceRegistry:
MaxOS::processes::resources::ResourceRegistry< ProcessResource > MaxOS::processes::resources::BaseResourceRegistry

Public Member Functions

Resource * get_resource (const string &name) final
 The.
 
Resource * create_resource (const string &name, size_t flags, uintptr_t data) final
 Opens an executable.
 
- Public Member Functions inherited from MaxOS::processes::resources::ResourceRegistry< ProcessResource >
 ResourceRegistry (resource_type_t type)
 Constructor for ResourceRegistry of a specific type.
 
Resource * create_resource (const string &name, size_t flags, uintptr_t data) override
 Create a new resource of the specific type.
 
- Public Member Functions inherited from MaxOS::processes::resources::BaseResourceRegistry
 BaseResourceRegistry (resource_type_t type)
 Constructs a new BaseResourceRegistry object and registers it globally.
 
 ~BaseResourceRegistry ()
 Destructor for BaseResourceRegistry, removes it from the global list.
 
resource_type_t type ()
 Gets the type of resources this registry handles.
 
bool register_resource (Resource *resource)
 Registers a resource in the registry.
 
void close_resource (Resource *resource, size_t flags)
 Close the resource provided, if it exists in this registry.
 

Additional Inherited Members

- Protected Attributes inherited from MaxOS::processes::resources::BaseResourceRegistry
common::Map< string, Resource * > m_resources
 The map of resource names to resource instances.
 
common::Map< string, uint64_t > m_resource_uses
 The map of resource names to how many processes are using them.
 
common::Spinlock m_lock
 The lock to make the registry threadsafe.
 
resource_type_t m_type
 The resource type that this registry manages.
 

Detailed Description

Manages the creation, retention and destruction of resources representing processes.

Definition at line 93 of file scheduler_resources.h.

Constructor & Destructor Documentation

◆ ProcessResourceRegistry()

ProcessResourceRegistry::ProcessResourceRegistry ( )

Definition at line 347 of file scheduler_resources.cpp.

348: ResourceRegistry<ProcessResource>(resource_type_t::PROCESS)
349{
350
351}
A resource registry for a specific resource type.
Definition resource.h:61

Member Function Documentation

◆ create_resource()

Resource * ProcessResourceRegistry::create_resource ( const string &  name,
size_t  flags,
uintptr_t  data 
)
finalvirtual

Opens an executable.

Parameters
nameThe path to the executable to open
flagstd
Returns
The Process resource created from the executable or nullptr if failed to open/execute
Todo:
Process needs to own the Elf pointer and free it in destructor (do this when moving ELF as a superclass)

Reimplemented from MaxOS::processes::resources::BaseResourceRegistry.

Definition at line 362 of file scheduler_resources.cpp.

362 {
363
364 // Get the command
365 auto command = (execute_command_t*)data;
366 if (!command)
367 return nullptr;
368
369 // Kernel copy (@todo copy the data page into the new process so dont get unmapped error and also thus dont need a copy)
370 auto exec_buffer = buffer_t(command->file_data_size);
371 exec_buffer.copy_from(command->file_data, command->file_data_size);
372
373 // Copy the args into userspace (@todo bad, two copies)
374 uint64_t argc = command->args_count;
375 size_t arg_size = argc * sizeof(void*);
376 void* argv = MemoryManager::kmalloc(arg_size);
377 for (int i = 0; i < argc; i++) {
378
379 // Copy each argument
380 size_t len = strlen(((char**)command->args)[i]) + 1;
381 ((char**)argv)[i] = (char*) MemoryManager::kmalloc(len);
382 memcpy((void*) ((char**)argv)[i], (void*) ((char**)command->args)[i], len);
383 }
384
385 // Load the elf
386 auto elf = new ELF64((uintptr_t)exec_buffer.raw());
387 if (!elf->is_valid())
388 return nullptr;
389
390 // Create the processs
391 auto process = new Process(name, argv, argc, elf);
392 GlobalScheduler::system_scheduler() -> add_process(process);
393
394 // Create the resource
395 auto resource = new ProcessResource(name, flags, resource_type_t::PROCESS);
396 resource->process = process;
397
398 // Clean up
399 // for (int i = 0; i < argc; i++)
400 // delete ((char**)argv)[i];
401 // delete argv;
402
403
404 // Switch back to the caller proc
405 MemoryManager::switch_active_memory_manager(GlobalScheduler::current_process()->memory_manager);
406 return resource;
407}
static void * kmalloc(size_t size)
Allocates a block of memory in the KERNEL space.
Handles the loading and parsing of 64-bit ELF files.
Definition elf.h:217
static GlobalScheduler * system_scheduler()
Gets the system scheduler.
Definition scheduler.cpp:55
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:94
A wrapper for a Process which exposes Process operations as Resource operations.

References MaxOS::processes::GlobalScheduler::current_process(), MaxOS::memory::MemoryManager::kmalloc(), and MaxOS::processes::GlobalScheduler::system_scheduler().

◆ get_resource()

Resource * ProcessResourceRegistry::get_resource ( const string &  name)
finalvirtual

The.

Parameters
name
Returns
Todo:
'this' is a bit mess

Reimplemented from MaxOS::processes::resources::BaseResourceRegistry.

Definition at line 417 of file scheduler_resources.cpp.

417 {
418
419 // Resource already opened
420 auto resource = BaseResourceRegistry::get_resource(name);
421 if(resource != nullptr)
422 return resource;
423
424 // Get the process
425 uint64_t pid = name.to_int();
426 auto process = name == "this" ? GlobalScheduler::current_process() : GlobalScheduler::get_process(pid);
427
428 // Self refrence may already be open
429 resource = BaseResourceRegistry::get_resource(string(process -> pid()));
430 if(resource != nullptr)
431 return resource;
432
433 // Create the resource
434 auto process_resource = new ProcessResource(string(process -> pid()), 0, resource_type_t::PROCESS);
435 process_resource->process = process;
436
437 register_resource(process_resource);
438 return process_resource;
439}
The global scheduler that manages all processes and threads across all cores.
Definition scheduler.h:28
virtual Resource * get_resource(const string &name)
Gets a resource from the registry by name.
Definition resource.cpp:52
bool register_resource(Resource *resource)
Registers a resource in the registry.
Definition resource.cpp:76

References MaxOS::processes::GlobalScheduler::current_process(), MaxOS::processes::GlobalScheduler::get_process(), MaxOS::processes::resources::BaseResourceRegistry::get_resource(), and MaxOS::processes::resources::BaseResourceRegistry::register_resource().


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