summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_page_group.h
blob: b32909f05a606d77d6c28508d23b247d7a55c78f (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <list>

#include "common/alignment.h"
#include "common/assert.h"
#include "common/common_types.h"
#include "core/hle/kernel/memory_types.h"
#include "core/hle/result.h"

namespace Kernel {

class KBlockInfoManager;
class KernelCore;
class KPageGroup;

class KBlockInfo {
public:
    constexpr explicit KBlockInfo() : m_next(nullptr) {}

    constexpr void Initialize(KPhysicalAddress addr, size_t np) {
        ASSERT(Common::IsAligned(GetInteger(addr), PageSize));
        ASSERT(static_cast<u32>(np) == np);

        m_page_index = static_cast<u32>(addr / PageSize);
        m_num_pages = static_cast<u32>(np);
    }

    constexpr KPhysicalAddress GetAddress() const {
        return m_page_index * PageSize;
    }
    constexpr size_t GetNumPages() const {
        return m_num_pages;
    }
    constexpr size_t GetSize() const {
        return this->GetNumPages() * PageSize;
    }
    constexpr KPhysicalAddress GetEndAddress() const {
        return (m_page_index + m_num_pages) * PageSize;
    }
    constexpr KPhysicalAddress GetLastAddress() const {
        return this->GetEndAddress() - 1;
    }

    constexpr KBlockInfo* GetNext() const {
        return m_next;
    }

    constexpr bool IsEquivalentTo(const KBlockInfo& rhs) const {
        return m_page_index == rhs.m_page_index && m_num_pages == rhs.m_num_pages;
    }

    constexpr bool operator==(const KBlockInfo& rhs) const {
        return this->IsEquivalentTo(rhs);
    }

    constexpr bool operator!=(const KBlockInfo& rhs) const {
        return !(*this == rhs);
    }

    constexpr bool IsStrictlyBefore(KPhysicalAddress addr) const {
        const KPhysicalAddress end = this->GetEndAddress();

        if (m_page_index != 0 && end == 0) {
            return false;
        }

        return end < addr;
    }

    constexpr bool operator<(KPhysicalAddress addr) const {
        return this->IsStrictlyBefore(addr);
    }

    constexpr bool TryConcatenate(KPhysicalAddress addr, size_t np) {
        if (addr != 0 && addr == this->GetEndAddress()) {
            m_num_pages += static_cast<u32>(np);
            return true;
        }
        return false;
    }

private:
    constexpr void SetNext(KBlockInfo* next) {
        m_next = next;
    }

private:
    friend class KPageGroup;

    KBlockInfo* m_next{};
    u32 m_page_index{};
    u32 m_num_pages{};
};
static_assert(sizeof(KBlockInfo) <= 0x10);

class KPageGroup {
public:
    class Iterator {
    public:
        using iterator_category = std::forward_iterator_tag;
        using value_type = const KBlockInfo;
        using difference_type = std::ptrdiff_t;
        using pointer = value_type*;
        using reference = value_type&;

        constexpr explicit Iterator(pointer n) : m_node(n) {}

        constexpr bool operator==(const Iterator& rhs) const {
            return m_node == rhs.m_node;
        }
        constexpr bool operator!=(const Iterator& rhs) const {
            return !(*this == rhs);
        }

        constexpr pointer operator->() const {
            return m_node;
        }
        constexpr reference operator*() const {
            return *m_node;
        }

        constexpr Iterator& operator++() {
            m_node = m_node->GetNext();
            return *this;
        }

        constexpr Iterator operator++(int) {
            const Iterator it{*this};
            ++(*this);
            return it;
        }

    private:
        pointer m_node{};
    };

    explicit KPageGroup(KernelCore& kernel, KBlockInfoManager* m)
        : m_kernel{kernel}, m_manager{m} {}
    ~KPageGroup() {
        this->Finalize();
    }

    void CloseAndReset();
    void Finalize();

    Iterator begin() const {
        return Iterator{m_first_block};
    }
    Iterator end() const {
        return Iterator{nullptr};
    }
    bool empty() const {
        return m_first_block == nullptr;
    }

    Result AddBlock(KPhysicalAddress addr, size_t num_pages);
    void Open() const;
    void OpenFirst() const;
    void Close() const;

    size_t GetNumPages() const;

    bool IsEquivalentTo(const KPageGroup& rhs) const;

    bool operator==(const KPageGroup& rhs) const {
        return this->IsEquivalentTo(rhs);
    }

    bool operator!=(const KPageGroup& rhs) const {
        return !(*this == rhs);
    }

private:
    KernelCore& m_kernel;
    KBlockInfo* m_first_block{};
    KBlockInfo* m_last_block{};
    KBlockInfoManager* m_manager{};
};

class KScopedPageGroup {
public:
    explicit KScopedPageGroup(const KPageGroup* gp) : m_pg(gp) {
        if (m_pg) {
            m_pg->Open();
        }
    }
    explicit KScopedPageGroup(const KPageGroup& gp) : KScopedPageGroup(std::addressof(gp)) {}
    ~KScopedPageGroup() {
        if (m_pg) {
            m_pg->Close();
        }
    }

    void CancelClose() {
        m_pg = nullptr;
    }

private:
    const KPageGroup* m_pg{};
};

} // namespace Kernel