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
|
|
|
#include <vector>
|
|
|
|
|
2014-07-05 06:55:39 +02:00
|
|
|
#include "common/common_types.h"
|
2018-08-02 04:40:00 +02:00
|
|
|
#include "core/hle/kernel/object.h"
|
2016-04-18 04:07:52 +02:00
|
|
|
#include "core/hle/kernel/process.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;
|
|
|
|
|
2014-07-05 16:22:03 +02:00
|
|
|
/// Permissions for mapped shared memory blocks
|
|
|
|
enum class MemoryPermission : u32 {
|
2016-09-18 02:38:01 +02:00
|
|
|
None = 0,
|
|
|
|
Read = (1u << 0),
|
|
|
|
Write = (1u << 1),
|
|
|
|
ReadWrite = (Read | Write),
|
|
|
|
Execute = (1u << 2),
|
|
|
|
ReadExecute = (Read | Execute),
|
|
|
|
WriteExecute = (Write | Execute),
|
2014-11-08 03:53:18 +01:00
|
|
|
ReadWriteExecute = (Read | Write | Execute),
|
2016-09-18 02:38:01 +02:00
|
|
|
DontCare = (1u << 28)
|
2014-07-05 16:22:03 +02:00
|
|
|
};
|
|
|
|
|
2015-01-27 05:40:21 +01:00
|
|
|
class SharedMemory final : public Object {
|
2015-01-11 06:43:29 +01:00
|
|
|
public:
|
|
|
|
/**
|
2016-05-09 00:10:53 +02:00
|
|
|
* Creates a shared memory object.
|
2018-08-28 18:30:33 +02:00
|
|
|
* @param kernel The kernel instance to create a shared memory instance under.
|
2016-04-18 04:07:52 +02:00
|
|
|
* @param owner_process Process that created this shared memory object.
|
2015-05-11 00:47:07 +02:00
|
|
|
* @param size Size of the memory block. Must be page-aligned.
|
|
|
|
* @param permissions Permission restrictions applied to the process which created the block.
|
2016-09-18 02:38:01 +02:00
|
|
|
* @param other_permissions Permission restrictions applied to other processes mapping the
|
|
|
|
* block.
|
2016-04-18 04:07:52 +02:00
|
|
|
* @param address The address from which to map the Shared Memory.
|
2016-09-18 02:38:01 +02:00
|
|
|
* @param region If the address is 0, the shared memory will be allocated in this region of the
|
|
|
|
* linear heap.
|
2015-05-11 00:47:07 +02:00
|
|
|
* @param name Optional object name, used for debugging purposes.
|
2015-01-11 06:43:29 +01:00
|
|
|
*/
|
2018-12-10 19:42:01 +01:00
|
|
|
static SharedPtr<SharedMemory> Create(KernelCore& kernel, Process* owner_process, u64 size,
|
|
|
|
MemoryPermission permissions,
|
2016-09-18 02:38:01 +02:00
|
|
|
MemoryPermission other_permissions, VAddr address = 0,
|
|
|
|
MemoryRegion region = MemoryRegion::BASE,
|
|
|
|
std::string name = "Unknown");
|
2015-01-11 06:43:29 +01:00
|
|
|
|
2016-05-09 00:10:53 +02:00
|
|
|
/**
|
|
|
|
* Creates a shared memory object from a block of memory managed by an HLE applet.
|
2018-08-28 18:30:33 +02:00
|
|
|
* @param kernel The kernel instance to create a shared memory instance under.
|
2016-05-09 00:10:53 +02:00
|
|
|
* @param heap_block Heap block of the HLE applet.
|
|
|
|
* @param offset The offset into the heap block that the SharedMemory will map.
|
|
|
|
* @param size Size of the memory block. Must be page-aligned.
|
|
|
|
* @param permissions Permission restrictions applied to the process which created the block.
|
2016-09-18 02:38:01 +02:00
|
|
|
* @param other_permissions Permission restrictions applied to other processes mapping the
|
|
|
|
* block.
|
2016-05-09 00:10:53 +02:00
|
|
|
* @param name Optional object name, used for debugging purposes.
|
|
|
|
*/
|
2018-08-28 18:30:33 +02:00
|
|
|
static SharedPtr<SharedMemory> CreateForApplet(KernelCore& kernel,
|
|
|
|
std::shared_ptr<std::vector<u8>> heap_block,
|
2018-11-19 14:56:15 +01:00
|
|
|
std::size_t offset, u64 size,
|
2016-09-18 02:38:01 +02:00
|
|
|
MemoryPermission permissions,
|
|
|
|
MemoryPermission other_permissions,
|
|
|
|
std::string name = "Unknown Applet");
|
|
|
|
|
|
|
|
std::string GetTypeName() const override {
|
|
|
|
return "SharedMemory";
|
|
|
|
}
|
|
|
|
std::string GetName() const override {
|
|
|
|
return name;
|
|
|
|
}
|
2015-01-11 06:43:29 +01:00
|
|
|
|
2019-04-11 22:30:52 +02:00
|
|
|
static constexpr HandleType HANDLE_TYPE = HandleType::SharedMemory;
|
2016-09-18 02:38:01 +02:00
|
|
|
HandleType GetHandleType() const override {
|
|
|
|
return HANDLE_TYPE;
|
|
|
|
}
|
2015-01-11 06:43:29 +01:00
|
|
|
|
2018-11-19 14:29:25 +01:00
|
|
|
/// Gets the size of the underlying memory block in bytes.
|
|
|
|
u64 GetSize() const {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-04-18 04:58:51 +02:00
|
|
|
/**
|
|
|
|
* Converts the specified MemoryPermission into the equivalent VMAPermission.
|
|
|
|
* @param permission The MemoryPermission to convert.
|
|
|
|
*/
|
|
|
|
static VMAPermission ConvertPermissions(MemoryPermission permission);
|
|
|
|
|
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
|
|
|
|
* @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
|
|
|
|
* @param permissions Memory block map permissions (specified by SVC field)
|
|
|
|
* @param other_permissions Memory block map other permissions (specified by SVC field)
|
|
|
|
*/
|
2018-11-19 15:05:04 +01:00
|
|
|
ResultCode Map(Process& target_process, VAddr address, MemoryPermission permissions,
|
2016-09-18 02:38:01 +02:00
|
|
|
MemoryPermission other_permissions);
|
2015-01-11 06:43:29 +01:00
|
|
|
|
2015-12-31 15:46:32 +01:00
|
|
|
/**
|
|
|
|
* Unmaps a shared memory block from the specified address in system memory
|
2019-03-29 23:01:38 +01:00
|
|
|
*
|
2018-11-19 15:05:04 +01:00
|
|
|
* @param target_process Process from which to unmap the memory block.
|
2019-03-29 23:01:38 +01:00
|
|
|
* @param address Address in system memory where the shared memory block is mapped.
|
|
|
|
* @param unmap_size The amount of bytes to unmap from this shared memory instance.
|
|
|
|
*
|
2015-12-31 15:46:32 +01:00
|
|
|
* @return Result code of the unmap operation
|
2019-03-29 23:01:38 +01:00
|
|
|
*
|
|
|
|
* @pre The given size to unmap must be the same size as the amount of memory managed by
|
|
|
|
* the SharedMemory instance itself, otherwise ERR_INVALID_SIZE will be returned.
|
2015-12-31 15:46:32 +01:00
|
|
|
*/
|
2019-03-29 23:01:38 +01:00
|
|
|
ResultCode Unmap(Process& target_process, VAddr address, u64 unmap_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
|
|
|
*/
|
2018-11-19 14:50:28 +01:00
|
|
|
u8* GetPointer(std::size_t offset = 0);
|
2015-01-11 06:43:29 +01:00
|
|
|
|
2018-11-19 15:00:32 +01:00
|
|
|
/**
|
|
|
|
* Gets a constant pointer to the shared memory block
|
|
|
|
* @param offset Offset from the start of the shared memory block to get pointer
|
|
|
|
* @return A constant pointer to the shared memory block from the specified offset
|
|
|
|
*/
|
|
|
|
const u8* GetPointer(std::size_t offset = 0) const;
|
|
|
|
|
2018-11-19 14:29:25 +01:00
|
|
|
private:
|
|
|
|
explicit SharedMemory(KernelCore& kernel);
|
|
|
|
~SharedMemory() override;
|
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
/// Backing memory for this shared memory block.
|
|
|
|
std::shared_ptr<std::vector<u8>> backing_block;
|
|
|
|
/// Offset into the backing block for this shared memory.
|
2018-11-19 14:29:25 +01:00
|
|
|
std::size_t backing_block_offset = 0;
|
2015-05-11 00:47:07 +02:00
|
|
|
/// Size of the memory block. Page-aligned.
|
2018-11-19 14:29:25 +01:00
|
|
|
u64 size = 0;
|
2015-05-11 00:47:07 +02:00
|
|
|
/// Permission restrictions applied to the process which created the block.
|
2018-11-19 14:29:25 +01:00
|
|
|
MemoryPermission permissions{};
|
2015-05-11 00:47:07 +02:00
|
|
|
/// Permission restrictions applied to other processes mapping the block.
|
2018-11-19 14:29:25 +01:00
|
|
|
MemoryPermission other_permissions{};
|
|
|
|
/// Process that created this shared memory block.
|
2018-12-10 19:42:01 +01:00
|
|
|
Process* owner_process;
|
2018-11-19 14:29:25 +01:00
|
|
|
/// Address of shared memory block in the owner process if specified.
|
|
|
|
VAddr base_address = 0;
|
2015-05-11 00:47:07 +02:00
|
|
|
/// Name of shared memory object.
|
|
|
|
std::string name;
|
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
|