Kernel: Implement deletion of SMO objects

This commit is contained in:
Bananymous 2024-05-31 12:27:56 +03:00
parent 0501f3bd99
commit 8bfacb0091
7 changed files with 38 additions and 1 deletions

View File

@ -21,6 +21,7 @@ namespace Kernel
static SharedMemoryObjectManager& get(); static SharedMemoryObjectManager& get();
BAN::ErrorOr<Key> create_object(size_t size, PageTable::flags_t); BAN::ErrorOr<Key> create_object(size_t size, PageTable::flags_t);
BAN::ErrorOr<void> delete_object(Key);
BAN::ErrorOr<BAN::UniqPtr<SharedMemoryObject>> map_object(Key, PageTable&, AddressRange); BAN::ErrorOr<BAN::UniqPtr<SharedMemoryObject>> map_object(Key, PageTable&, AddressRange);
private: private:
@ -29,6 +30,8 @@ namespace Kernel
private: private:
struct Object : public BAN::RefCounted<Object> struct Object : public BAN::RefCounted<Object>
{ {
~Object();
size_t size; size_t size;
PageTable::flags_t flags; PageTable::flags_t flags;
BAN::Vector<paddr_t> paddrs; BAN::Vector<paddr_t> paddrs;

View File

@ -159,6 +159,7 @@ namespace Kernel
BAN::ErrorOr<long> sys_msync(void* addr, size_t len, int flags); BAN::ErrorOr<long> sys_msync(void* addr, size_t len, int flags);
BAN::ErrorOr<long> sys_smo_create(size_t len, int prot); BAN::ErrorOr<long> sys_smo_create(size_t len, int prot);
BAN::ErrorOr<long> sys_smo_delete(SharedMemoryObjectManager::Key);
BAN::ErrorOr<long> sys_smo_map(SharedMemoryObjectManager::Key); BAN::ErrorOr<long> sys_smo_map(SharedMemoryObjectManager::Key);
BAN::ErrorOr<long> sys_tty_ctrl(int fildes, int command, int flags); BAN::ErrorOr<long> sys_tty_ctrl(int fildes, int command, int flags);

View File

@ -21,6 +21,13 @@ namespace Kernel
return *s_instance; return *s_instance;
} }
SharedMemoryObjectManager::Object::~Object()
{
for (auto paddr : paddrs)
if (paddr)
Heap::get().release_page(paddr);
}
BAN::ErrorOr<SharedMemoryObjectManager::Key> SharedMemoryObjectManager::create_object(size_t size, PageTable::flags_t flags) BAN::ErrorOr<SharedMemoryObjectManager::Key> SharedMemoryObjectManager::create_object(size_t size, PageTable::flags_t flags)
{ {
ASSERT(size % PAGE_SIZE == 0); ASSERT(size % PAGE_SIZE == 0);
@ -43,6 +50,18 @@ namespace Kernel
return key; return key;
} }
BAN::ErrorOr<void> SharedMemoryObjectManager::delete_object(Key key)
{
LockGuard _(m_mutex);
auto it = m_objects.find(key);
if (it == m_objects.end())
return BAN::Error::from_errno(ENOENT);
m_objects.remove(it);
return {};
}
BAN::ErrorOr<BAN::UniqPtr<SharedMemoryObject>> SharedMemoryObjectManager::map_object(Key key, PageTable& page_table, AddressRange address_range) BAN::ErrorOr<BAN::UniqPtr<SharedMemoryObject>> SharedMemoryObjectManager::map_object(Key key, PageTable& page_table, AddressRange address_range)
{ {
LockGuard _(m_mutex); LockGuard _(m_mutex);

View File

@ -1418,6 +1418,12 @@ namespace Kernel
return TRY(SharedMemoryObjectManager::get().create_object(len, page_flags)); return TRY(SharedMemoryObjectManager::get().create_object(len, page_flags));
} }
BAN::ErrorOr<long> Process::sys_smo_delete(SharedMemoryObjectManager::Key key)
{
TRY(SharedMemoryObjectManager::get().delete_object(key));
return 0;
}
BAN::ErrorOr<long> Process::sys_smo_map(SharedMemoryObjectManager::Key key) BAN::ErrorOr<long> Process::sys_smo_map(SharedMemoryObjectManager::Key key)
{ {
auto region = TRY(SharedMemoryObjectManager::get().map_object(key, page_table(), { .start = 0x400000, .end = KERNEL_OFFSET })); auto region = TRY(SharedMemoryObjectManager::get().map_object(key, page_table(), { .start = 0x400000, .end = KERNEL_OFFSET }));

View File

@ -38,7 +38,9 @@ int load_keymap(const char* path);
// Create shared memory object and return its key or -1 on error // Create shared memory object and return its key or -1 on error
long smo_create(size_t size, int prot); long smo_create(size_t size, int prot);
// Map shared memory object defined by its key and return address or null on error // Delete shared memory object such that it will be no longer accessible with smo_map(). Existing mappings are still valid
int smo_delete(long key);
// Map shared memory object defined by its key and return address or null on error. Mappings can be unmapped using munmap()
void* smo_map(long key); void* smo_map(long key);
__END_DECLS __END_DECLS

View File

@ -75,6 +75,7 @@ __BEGIN_DECLS
O(SYS_PSELECT, pselect) \ O(SYS_PSELECT, pselect) \
O(SYS_TRUNCATE, truncate) \ O(SYS_TRUNCATE, truncate) \
O(SYS_SMO_CREATE, smo_create) \ O(SYS_SMO_CREATE, smo_create) \
O(SYS_SMO_DELETE, smo_delete) \
O(SYS_SMO_MAP, smo_map) \ O(SYS_SMO_MAP, smo_map) \
enum Syscall enum Syscall

View File

@ -22,6 +22,11 @@ long smo_create(size_t size, int prot)
return syscall(SYS_SMO_CREATE, size, prot); return syscall(SYS_SMO_CREATE, size, prot);
} }
int smo_delete(long key)
{
return syscall(SYS_SMO_DELETE, key);
}
void* smo_map(long key) void* smo_map(long key)
{ {
long ret = syscall(SYS_SMO_MAP, key); long ret = syscall(SYS_SMO_MAP, key);