summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_shared_memory.h
blob: bfd01b61997cde2e58244e33227022b5064af53c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-FileCopyrightText: 2014 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <optional>
#include <string>

#include "common/common_types.h"
#include "core/device_memory.h"
#include "core/hle/kernel/k_memory_block.h"
#include "core/hle/kernel/k_page_group.h"
#include "core/hle/kernel/k_process.h"
#include "core/hle/kernel/slab_helpers.h"
#include "core/hle/result.h"

namespace Kernel {

class KernelCore;

class KSharedMemory final
    : public KAutoObjectWithSlabHeapAndContainer<KSharedMemory, KAutoObjectWithList> {
    KERNEL_AUTOOBJECT_TRAITS(KSharedMemory, KAutoObject);

public:
    explicit KSharedMemory(KernelCore& kernel_);
    ~KSharedMemory() override;

    Result Initialize(Core::DeviceMemory& device_memory_, KProcess* owner_process_,
                      Svc::MemoryPermission owner_permission_,
                      Svc::MemoryPermission user_permission_, std::size_t size_);

    /**
     * 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
     * @param address Address in system memory to map shared memory block to
     * @param map_size Size of the shared memory block to map
     * @param permissions Memory block map permissions (specified by SVC field)
     */
    Result Map(KProcess& target_process, VAddr address, std::size_t map_size,
               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 unmap_size Size of the shared memory block to unmap
     */
    Result Unmap(KProcess& target_process, VAddr address, std::size_t unmap_size);

    /**
     * Gets a pointer to the shared memory block
     * @param offset Offset from the start of the shared memory block to get pointer
     * @return A pointer to the shared memory block from the specified offset
     */
    u8* GetPointer(std::size_t offset = 0) {
        return device_memory->GetPointer<u8>(physical_address + offset);
    }

    /**
     * Gets a pointer to the shared memory block
     * @param offset Offset from the start of the shared memory block to get pointer
     * @return A pointer to the shared memory block from the specified offset
     */
    const u8* GetPointer(std::size_t offset = 0) const {
        return device_memory->GetPointer<u8>(physical_address + offset);
    }

    void Finalize() override;

    bool IsInitialized() const override {
        return is_initialized;
    }
    static void PostDestroy([[maybe_unused]] uintptr_t arg) {}

private:
    Core::DeviceMemory* device_memory{};
    KProcess* owner_process{};
    std::optional<KPageGroup> page_group{};
    Svc::MemoryPermission owner_permission{};
    Svc::MemoryPermission user_permission{};
    PAddr physical_address{};
    std::size_t size{};
    KResourceLimit* resource_limit{};
    bool is_initialized{};
};

} // namespace Kernel