summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_thread_local_page.cpp
blob: 1952c00831c5be760b98da871d53ba380c75254d (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
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/scope_exit.h"
#include "core/core.h"

#include "core/hle/kernel/k_memory_block.h"
#include "core/hle/kernel/k_page_buffer.h"
#include "core/hle/kernel/k_page_table.h"
#include "core/hle/kernel/k_process.h"
#include "core/hle/kernel/k_thread_local_page.h"
#include "core/hle/kernel/kernel.h"

namespace Kernel {

Result KThreadLocalPage::Initialize(KernelCore& kernel, KProcess* process) {
    // Set that this process owns us.
    m_owner = process;
    m_kernel = std::addressof(kernel);

    // Allocate a new page.
    KPageBuffer* page_buf = KPageBuffer::Allocate(kernel);
    R_UNLESS(page_buf != nullptr, ResultOutOfMemory);
    auto page_buf_guard = SCOPE_GUARD {
        KPageBuffer::Free(kernel, page_buf);
    };

    // Map the address in.
    const auto phys_addr = kernel.System().DeviceMemory().GetPhysicalAddr(page_buf);
    R_TRY(m_owner->GetPageTable().MapPages(std::addressof(m_virt_addr), 1, PageSize, phys_addr,
                                           KMemoryState::ThreadLocal,
                                           KMemoryPermission::UserReadWrite));

    // We succeeded.
    page_buf_guard.Cancel();

    return ResultSuccess;
}

Result KThreadLocalPage::Finalize() {
    // Get the physical address of the page.
    KPhysicalAddress phys_addr{};
    ASSERT(m_owner->GetPageTable().GetPhysicalAddress(std::addressof(phys_addr), m_virt_addr));

    // Unmap the page.
    R_TRY(m_owner->GetPageTable().UnmapPages(this->GetAddress(), 1, KMemoryState::ThreadLocal));

    // Free the page.
    KPageBuffer::Free(*m_kernel, KPageBuffer::FromPhysicalAddress(m_kernel->System(), phys_addr));

    return ResultSuccess;
}

KProcessAddress KThreadLocalPage::Reserve() {
    for (size_t i = 0; i < m_is_region_free.size(); i++) {
        if (m_is_region_free[i]) {
            m_is_region_free[i] = false;
            return this->GetRegionAddress(i);
        }
    }

    return 0;
}

void KThreadLocalPage::Release(KProcessAddress addr) {
    m_is_region_free[this->GetRegionIndex(addr)] = true;
}

} // namespace Kernel