2014-07-05 06:55:39 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-11-19 09:49:13 +01:00
|
|
|
// Refer to the license.txt file included.
|
2014-07-05 06:55:39 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-07-31 14:06:09 +02:00
|
|
|
#include <memory>
|
2015-07-12 23:10:37 +02:00
|
|
|
#include <string>
|
2018-07-31 14:06:09 +02:00
|
|
|
|
2014-07-05 06:55:39 +02:00
|
|
|
#include "common/common_types.h"
|
2020-04-09 00:53:52 +02:00
|
|
|
#include "core/device_memory.h"
|
2021-02-13 02:02:51 +01:00
|
|
|
#include "core/hle/kernel/k_memory_block.h"
|
2021-02-13 02:26:01 +01:00
|
|
|
#include "core/hle/kernel/k_page_linked_list.h"
|
2021-04-24 07:04:28 +02:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2021-04-04 08:22:07 +02:00
|
|
|
#include "core/hle/kernel/slab_helpers.h"
|
2015-07-12 23:10:37 +02:00
|
|
|
#include "core/hle/result.h"
|
2014-07-05 06:55:39 +02:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2018-08-28 18:30:33 +02:00
|
|
|
class KernelCore;
|
|
|
|
|
2021-04-04 08:22:07 +02:00
|
|
|
class KSharedMemory final
|
|
|
|
: public KAutoObjectWithSlabHeapAndContainer<KSharedMemory, KAutoObjectWithList> {
|
|
|
|
KERNEL_AUTOOBJECT_TRAITS(KSharedMemory, KAutoObject);
|
|
|
|
|
2015-01-11 06:43:29 +01:00
|
|
|
public:
|
2021-04-04 08:22:07 +02:00
|
|
|
explicit KSharedMemory(KernelCore& kernel);
|
2021-02-06 08:14:31 +01:00
|
|
|
~KSharedMemory() override;
|
2019-11-25 02:15:51 +01:00
|
|
|
|
2021-04-04 08:22:07 +02:00
|
|
|
ResultCode Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_,
|
2021-04-24 07:04:28 +02:00
|
|
|
KProcess* owner_process_, KPageLinkedList&& page_list_,
|
2021-04-30 23:53:22 +02:00
|
|
|
Svc::MemoryPermission owner_permission_,
|
|
|
|
Svc::MemoryPermission user_permission_, PAddr physical_address_,
|
|
|
|
std::size_t size_, std::string name_);
|
2016-09-18 02:38:01 +02:00
|
|
|
|
2015-01-11 06:43:29 +01:00
|
|
|
/**
|
2016-04-18 04:07:52 +02:00
|
|
|
* Maps a shared memory block to an address in the target process' address space
|
2020-04-09 00:53:52 +02:00
|
|
|
* @param target_process Process on which to map the memory block
|
2015-01-11 06:43:29 +01:00
|
|
|
* @param address Address in system memory to map shared memory block to
|
2020-04-09 00:53:52 +02:00
|
|
|
* @param size Size of the shared memory block to map
|
2015-01-11 06:43:29 +01:00
|
|
|
* @param permissions Memory block map permissions (specified by SVC field)
|
2015-12-31 15:46:32 +01:00
|
|
|
*/
|
2021-04-24 07:04:28 +02:00
|
|
|
ResultCode Map(KProcess& target_process, VAddr address, std::size_t size,
|
2021-04-30 23:53:22 +02:00
|
|
|
Svc::MemoryPermission permissions);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unmaps a shared memory block from an address in the target process' address space
|
|
|
|
* @param target_process Process on which to unmap the memory block
|
|
|
|
* @param address Address in system memory to unmap shared memory block
|
|
|
|
* @param size Size of the shared memory block to unmap
|
|
|
|
*/
|
|
|
|
ResultCode Unmap(KProcess& target_process, VAddr address, std::size_t size);
|
2015-12-31 15:46:32 +01:00
|
|
|
|
2015-01-11 06:43:29 +01:00
|
|
|
/**
|
2018-01-20 08:48:02 +01:00
|
|
|
* Gets a pointer to the shared memory block
|
|
|
|
* @param offset Offset from the start of the shared memory block to get pointer
|
2018-11-19 15:00:32 +01:00
|
|
|
* @return A pointer to the shared memory block from the specified offset
|
2018-01-20 08:48:02 +01:00
|
|
|
*/
|
2020-04-09 00:53:52 +02:00
|
|
|
u8* GetPointer(std::size_t offset = 0) {
|
2021-04-04 08:22:07 +02:00
|
|
|
return device_memory->GetPointer(physical_address + offset);
|
2020-04-09 00:53:52 +02:00
|
|
|
}
|
2015-01-11 06:43:29 +01:00
|
|
|
|
2018-11-19 15:00:32 +01:00
|
|
|
/**
|
2020-04-09 00:53:52 +02:00
|
|
|
* Gets a pointer to the shared memory block
|
2018-11-19 15:00:32 +01:00
|
|
|
* @param offset Offset from the start of the shared memory block to get pointer
|
2020-04-09 00:53:52 +02:00
|
|
|
* @return A pointer to the shared memory block from the specified offset
|
2018-11-19 15:00:32 +01:00
|
|
|
*/
|
2020-04-09 00:53:52 +02:00
|
|
|
const u8* GetPointer(std::size_t offset = 0) const {
|
2021-04-04 08:22:07 +02:00
|
|
|
return device_memory->GetPointer(physical_address + offset);
|
2020-04-09 00:53:52 +02:00
|
|
|
}
|
2018-11-19 15:00:32 +01:00
|
|
|
|
2021-04-04 08:22:07 +02:00
|
|
|
virtual void Finalize() override;
|
|
|
|
|
|
|
|
virtual bool IsInitialized() const override {
|
|
|
|
return is_initialized;
|
|
|
|
}
|
|
|
|
static void PostDestroy([[maybe_unused]] uintptr_t arg) {}
|
2021-01-16 09:25:29 +01:00
|
|
|
|
2018-11-19 14:29:25 +01:00
|
|
|
private:
|
2021-04-04 08:22:07 +02:00
|
|
|
Core::DeviceMemory* device_memory;
|
2021-04-24 07:04:28 +02:00
|
|
|
KProcess* owner_process{};
|
2021-02-13 02:26:01 +01:00
|
|
|
KPageLinkedList page_list;
|
2021-04-30 23:53:22 +02:00
|
|
|
Svc::MemoryPermission owner_permission{};
|
|
|
|
Svc::MemoryPermission user_permission{};
|
2020-04-09 00:53:52 +02:00
|
|
|
PAddr physical_address{};
|
|
|
|
std::size_t size{};
|
2021-04-21 06:28:11 +02:00
|
|
|
KResourceLimit* resource_limit{};
|
2021-04-04 08:22:07 +02:00
|
|
|
bool is_initialized{};
|
2015-01-11 06:43:29 +01:00
|
|
|
};
|
2014-07-05 06:55:39 +02:00
|
|
|
|
2018-01-20 08:48:02 +01:00
|
|
|
} // namespace Kernel
|