2023-01-13 18:05:20 +00:00
|
|
|
#pragma once
|
2023-07-09 18:32:42 +00:00
|
|
|
#include "arch/MMU.h"
|
2023-06-17 23:48:36 +00:00
|
|
|
#include <luna/LinkedList.h>
|
2023-01-13 18:05:20 +00:00
|
|
|
#include <luna/OwnedPtr.h>
|
|
|
|
#include <luna/Result.h>
|
2023-08-02 20:39:07 +00:00
|
|
|
#include <sys/types.h>
|
2023-01-13 18:05:20 +00:00
|
|
|
|
2023-06-17 23:48:36 +00:00
|
|
|
class VMRegion : LinkedListNode<VMRegion>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
u64 start;
|
|
|
|
u64 end;
|
|
|
|
usize count;
|
|
|
|
bool used { true };
|
|
|
|
bool persistent { false };
|
2023-08-02 20:19:06 +00:00
|
|
|
int flags { 0 };
|
|
|
|
int prot { 0 };
|
|
|
|
u64 shmid;
|
2023-08-02 20:39:07 +00:00
|
|
|
off_t offset { 0 };
|
2023-08-02 20:19:06 +00:00
|
|
|
|
|
|
|
void cleanup_shared();
|
2023-08-02 20:39:07 +00:00
|
|
|
void sync_shared();
|
2023-06-17 23:48:36 +00:00
|
|
|
};
|
|
|
|
|
2023-07-09 18:38:04 +00:00
|
|
|
class AddressSpace
|
2023-01-13 18:05:20 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-07-09 18:38:04 +00:00
|
|
|
AddressSpace();
|
|
|
|
~AddressSpace();
|
2023-01-13 18:05:20 +00:00
|
|
|
|
2023-07-09 18:38:04 +00:00
|
|
|
AddressSpace& operator=(AddressSpace&& other);
|
2023-07-09 18:32:42 +00:00
|
|
|
|
2023-08-02 20:39:07 +00:00
|
|
|
Result<u64> alloc_region(usize count, int prot, int flags, off_t offset, u64 shmid = 0, bool persistent = false);
|
2023-06-17 23:48:36 +00:00
|
|
|
|
2023-08-02 20:39:07 +00:00
|
|
|
Result<bool> test_and_alloc_region(u64 address, usize count, int prot, int flags, off_t offset, u64 shmid = 0,
|
2023-08-02 20:19:06 +00:00
|
|
|
bool persistent = false)
|
2023-06-17 23:48:36 +00:00
|
|
|
{
|
2023-08-02 20:39:07 +00:00
|
|
|
return set_region(address, count, true, prot, flags, offset, shmid, persistent);
|
2023-06-17 23:48:36 +00:00
|
|
|
}
|
2023-01-13 18:05:20 +00:00
|
|
|
|
2023-06-17 23:48:36 +00:00
|
|
|
Result<bool> free_region(u64 address, usize count)
|
|
|
|
{
|
2023-08-02 20:39:07 +00:00
|
|
|
return set_region(address, count, false, 0, 0, 0, 0, false);
|
2023-06-17 23:48:36 +00:00
|
|
|
}
|
2023-01-13 18:05:20 +00:00
|
|
|
|
2023-08-02 20:39:07 +00:00
|
|
|
Result<void> sync_regions(u64 address, usize count);
|
|
|
|
|
2023-07-09 18:38:04 +00:00
|
|
|
static Result<OwnedPtr<AddressSpace>> try_create();
|
2023-01-13 18:05:20 +00:00
|
|
|
|
2023-07-09 18:38:04 +00:00
|
|
|
Result<OwnedPtr<AddressSpace>> clone();
|
2023-03-18 22:45:48 +00:00
|
|
|
|
2023-07-09 18:32:42 +00:00
|
|
|
PageDirectory* page_directory() const
|
|
|
|
{
|
|
|
|
return m_directory;
|
|
|
|
}
|
|
|
|
|
2023-01-13 18:05:20 +00:00
|
|
|
private:
|
2023-08-02 20:39:07 +00:00
|
|
|
Result<bool> set_region(u64 address, usize count, bool used, int prot, int flags, off_t offset, u64 shmid,
|
|
|
|
bool persistent);
|
2023-06-17 23:48:36 +00:00
|
|
|
Result<void> create_default_region();
|
|
|
|
Result<void> create_null_region();
|
|
|
|
void try_merge_region_with_neighbors(VMRegion* region);
|
|
|
|
void merge_contiguous_regions(VMRegion* a, VMRegion* b);
|
|
|
|
Result<VMRegion*> split_region(VMRegion* parent, u64 boundary);
|
2023-07-09 18:38:04 +00:00
|
|
|
|
2023-06-17 23:48:36 +00:00
|
|
|
LinkedList<VMRegion> m_regions;
|
2023-07-09 18:32:42 +00:00
|
|
|
PageDirectory* m_directory;
|
2023-01-13 18:05:20 +00:00
|
|
|
};
|