summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_code_memory.h
blob: 5b260b38539381da821a8d7f80bb89b607f86b61 (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
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <optional>

#include "common/common_types.h"
#include "core/device_memory.h"
#include "core/hle/kernel/k_auto_object.h"
#include "core/hle/kernel/k_light_lock.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/kernel/svc_types.h"
#include "core/hle/result.h"

namespace Kernel {

enum class CodeMemoryOperation : u32 {
    Map = 0,
    MapToOwner = 1,
    Unmap = 2,
    UnmapFromOwner = 3,
};

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

public:
    explicit KCodeMemory(KernelCore& kernel_);

    Result Initialize(Core::DeviceMemory& device_memory, VAddr address, size_t size);
    void Finalize() override;

    Result Map(VAddr address, size_t size);
    Result Unmap(VAddr address, size_t size);
    Result MapToOwner(VAddr address, size_t size, Svc::MemoryPermission perm);
    Result UnmapFromOwner(VAddr address, size_t size);

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

    KProcess* GetOwner() const override {
        return m_owner;
    }
    VAddr GetSourceAddress() const {
        return m_address;
    }
    size_t GetSize() const {
        return m_is_initialized ? m_page_group->GetNumPages() * PageSize : 0;
    }

private:
    std::optional<KPageGroup> m_page_group{};
    KProcess* m_owner{};
    VAddr m_address{};
    KLightLock m_lock;
    bool m_is_initialized{};
    bool m_is_owner_mapped{};
    bool m_is_mapped{};
};

} // namespace Kernel