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
|
|
|
|
|
2015-07-12 23:10:37 +02:00
|
|
|
#include <string>
|
2014-07-05 06:55:39 +02:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "core/hle/kernel/kernel.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 {
|
|
|
|
|
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.
|
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
|
|
|
*/
|
2016-09-18 02:38:01 +02:00
|
|
|
static SharedPtr<SharedMemory> Create(SharedPtr<Process> owner_process, u32 size,
|
|
|
|
MemoryPermission permissions,
|
|
|
|
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.
|
|
|
|
* @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.
|
|
|
|
*/
|
2016-09-18 02:38:01 +02:00
|
|
|
static SharedPtr<SharedMemory> CreateForApplet(std::shared_ptr<std::vector<u8>> heap_block,
|
|
|
|
u32 offset, u32 size,
|
|
|
|
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
|
|
|
|
|
|
|
static const 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
|
|
|
|
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)
|
|
|
|
*/
|
2016-09-18 02:38:01 +02:00
|
|
|
ResultCode Map(Process* target_process, VAddr address, MemoryPermission permissions,
|
|
|
|
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
|
2016-04-18 04:07:52 +02:00
|
|
|
* @param target_process Process from which to umap the memory block.
|
2015-12-31 15:46:32 +01:00
|
|
|
* @param address Address in system memory where the shared memory block is mapped
|
|
|
|
* @return Result code of the unmap operation
|
|
|
|
*/
|
2016-04-18 04:07:52 +02:00
|
|
|
ResultCode Unmap(Process* target_process, VAddr address);
|
2015-12-31 15:46:32 +01:00
|
|
|
|
2015-01-11 06:43:29 +01:00
|
|
|
/**
|
|
|
|
* Gets a pointer to the shared memory block
|
|
|
|
* @param offset Offset from the start of the shared memory block to get pointer
|
|
|
|
* @return Pointer to the shared memory block from the specified offset
|
|
|
|
*/
|
2015-05-11 00:47:07 +02:00
|
|
|
u8* GetPointer(u32 offset = 0);
|
2015-01-11 06:43:29 +01:00
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
/// Process that created this shared memory block.
|
|
|
|
SharedPtr<Process> owner_process;
|
|
|
|
/// Address of shared memory block in the owner process if specified.
|
2015-05-11 00:47:07 +02:00
|
|
|
VAddr base_address;
|
2016-09-18 02:38:01 +02:00
|
|
|
/// Physical address of the shared memory block in the linear heap if no address was specified
|
|
|
|
/// during creation.
|
2016-04-18 04:07:52 +02:00
|
|
|
PAddr linear_heap_phys_address;
|
|
|
|
/// Backing memory for this shared memory block.
|
|
|
|
std::shared_ptr<std::vector<u8>> backing_block;
|
|
|
|
/// Offset into the backing block for this shared memory.
|
2017-09-27 01:26:09 +02:00
|
|
|
size_t backing_block_offset;
|
2015-05-11 00:47:07 +02:00
|
|
|
/// Size of the memory block. Page-aligned.
|
|
|
|
u32 size;
|
|
|
|
/// Permission restrictions applied to the process which created the block.
|
|
|
|
MemoryPermission permissions;
|
|
|
|
/// Permission restrictions applied to other processes mapping the block.
|
|
|
|
MemoryPermission other_permissions;
|
|
|
|
/// Name of shared memory object.
|
|
|
|
std::string name;
|
2015-01-11 06:43:29 +01:00
|
|
|
|
|
|
|
private:
|
2015-02-01 01:56:59 +01:00
|
|
|
SharedMemory();
|
|
|
|
~SharedMemory() override;
|
2015-01-11 06:43:29 +01:00
|
|
|
};
|
2014-07-05 06:55:39 +02:00
|
|
|
|
|
|
|
} // namespace
|