Merge pull request #6266 from bunnei/kautoobject-refactor
Kernel Rework: Migrate kernel objects to KAutoObjectmaster
commit
faa067f175
@ -1,47 +0,0 @@
|
|||||||
// Copyright 2016 Citra Emulator Project
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#include "core/hle/kernel/client_port.h"
|
|
||||||
#include "core/hle/kernel/client_session.h"
|
|
||||||
#include "core/hle/kernel/hle_ipc.h"
|
|
||||||
#include "core/hle/kernel/object.h"
|
|
||||||
#include "core/hle/kernel/server_port.h"
|
|
||||||
#include "core/hle/kernel/session.h"
|
|
||||||
#include "core/hle/kernel/svc_results.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
ClientPort::ClientPort(KernelCore& kernel) : Object{kernel} {}
|
|
||||||
ClientPort::~ClientPort() = default;
|
|
||||||
|
|
||||||
std::shared_ptr<ServerPort> ClientPort::GetServerPort() const {
|
|
||||||
return server_port;
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultVal<std::shared_ptr<ClientSession>> ClientPort::Connect() {
|
|
||||||
if (active_sessions >= max_sessions) {
|
|
||||||
return ResultMaxConnectionsReached;
|
|
||||||
}
|
|
||||||
active_sessions++;
|
|
||||||
|
|
||||||
auto [client, server] = Kernel::Session::Create(kernel, name);
|
|
||||||
|
|
||||||
if (server_port->HasHLEHandler()) {
|
|
||||||
server_port->GetHLEHandler()->ClientConnected(std::move(server));
|
|
||||||
} else {
|
|
||||||
server_port->AppendPendingSession(std::move(server));
|
|
||||||
}
|
|
||||||
|
|
||||||
return MakeResult(std::move(client));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClientPort::ConnectionClosed() {
|
|
||||||
if (active_sessions == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
--active_sessions;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -1,63 +0,0 @@
|
|||||||
// Copyright 2016 Citra Emulator Project
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "common/common_types.h"
|
|
||||||
#include "core/hle/kernel/object.h"
|
|
||||||
#include "core/hle/result.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
class ClientSession;
|
|
||||||
class KernelCore;
|
|
||||||
class ServerPort;
|
|
||||||
|
|
||||||
class ClientPort final : public Object {
|
|
||||||
public:
|
|
||||||
explicit ClientPort(KernelCore& kernel);
|
|
||||||
~ClientPort() override;
|
|
||||||
|
|
||||||
friend class ServerPort;
|
|
||||||
std::string GetTypeName() const override {
|
|
||||||
return "ClientPort";
|
|
||||||
}
|
|
||||||
std::string GetName() const override {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr HandleType HANDLE_TYPE = HandleType::ClientPort;
|
|
||||||
HandleType GetHandleType() const override {
|
|
||||||
return HANDLE_TYPE;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<ServerPort> GetServerPort() const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new Session pair, adds the created ServerSession to the associated ServerPort's
|
|
||||||
* list of pending sessions, and signals the ServerPort, causing any threads
|
|
||||||
* waiting on it to awake.
|
|
||||||
* @returns ClientSession The client endpoint of the created Session pair, or error code.
|
|
||||||
*/
|
|
||||||
ResultVal<std::shared_ptr<ClientSession>> Connect();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Signifies that a previously active connection has been closed,
|
|
||||||
* decreasing the total number of active connections to this port.
|
|
||||||
*/
|
|
||||||
void ConnectionClosed();
|
|
||||||
|
|
||||||
void Finalize() override {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::shared_ptr<ServerPort> server_port; ///< ServerPort associated with this client port.
|
|
||||||
u32 max_sessions = 0; ///< Maximum number of simultaneous sessions the port can have
|
|
||||||
u32 active_sessions = 0; ///< Number of currently open sessions to this port
|
|
||||||
std::string name; ///< Name of client port (optional)
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -1,53 +0,0 @@
|
|||||||
// Copyright 2019 yuzu emulator team
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#include "core/hle/kernel/client_session.h"
|
|
||||||
#include "core/hle/kernel/hle_ipc.h"
|
|
||||||
#include "core/hle/kernel/k_thread.h"
|
|
||||||
#include "core/hle/kernel/server_session.h"
|
|
||||||
#include "core/hle/kernel/session.h"
|
|
||||||
#include "core/hle/kernel/svc_results.h"
|
|
||||||
#include "core/hle/result.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
ClientSession::ClientSession(KernelCore& kernel) : KSynchronizationObject{kernel} {}
|
|
||||||
|
|
||||||
ClientSession::~ClientSession() {
|
|
||||||
// This destructor will be called automatically when the last ClientSession handle is closed by
|
|
||||||
// the emulated application.
|
|
||||||
if (parent->Server()) {
|
|
||||||
parent->Server()->ClientDisconnected();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ClientSession::IsSignaled() const {
|
|
||||||
UNIMPLEMENTED();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultVal<std::shared_ptr<ClientSession>> ClientSession::Create(KernelCore& kernel,
|
|
||||||
std::shared_ptr<Session> parent,
|
|
||||||
std::string name) {
|
|
||||||
std::shared_ptr<ClientSession> client_session{std::make_shared<ClientSession>(kernel)};
|
|
||||||
|
|
||||||
client_session->name = std::move(name);
|
|
||||||
client_session->parent = std::move(parent);
|
|
||||||
|
|
||||||
return MakeResult(std::move(client_session));
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultCode ClientSession::SendSyncRequest(std::shared_ptr<KThread> thread,
|
|
||||||
Core::Memory::Memory& memory,
|
|
||||||
Core::Timing::CoreTiming& core_timing) {
|
|
||||||
// Keep ServerSession alive until we're done working with it.
|
|
||||||
if (!parent->Server()) {
|
|
||||||
return ResultSessionClosedByRemote;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Signal the server session that new data is available
|
|
||||||
return parent->Server()->HandleSyncRequest(std::move(thread), memory, core_timing);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -1,68 +0,0 @@
|
|||||||
// Copyright 2019 yuzu emulator team
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "core/hle/kernel/k_synchronization_object.h"
|
|
||||||
#include "core/hle/result.h"
|
|
||||||
|
|
||||||
union ResultCode;
|
|
||||||
|
|
||||||
namespace Core::Memory {
|
|
||||||
class Memory;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace Core::Timing {
|
|
||||||
class CoreTiming;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
class KernelCore;
|
|
||||||
class Session;
|
|
||||||
class KThread;
|
|
||||||
|
|
||||||
class ClientSession final : public KSynchronizationObject {
|
|
||||||
public:
|
|
||||||
explicit ClientSession(KernelCore& kernel);
|
|
||||||
~ClientSession() override;
|
|
||||||
|
|
||||||
friend class Session;
|
|
||||||
|
|
||||||
std::string GetTypeName() const override {
|
|
||||||
return "ClientSession";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string GetName() const override {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr HandleType HANDLE_TYPE = HandleType::ClientSession;
|
|
||||||
HandleType GetHandleType() const override {
|
|
||||||
return HANDLE_TYPE;
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultCode SendSyncRequest(std::shared_ptr<KThread> thread, Core::Memory::Memory& memory,
|
|
||||||
Core::Timing::CoreTiming& core_timing);
|
|
||||||
|
|
||||||
bool IsSignaled() const override;
|
|
||||||
|
|
||||||
void Finalize() override {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
static ResultVal<std::shared_ptr<ClientSession>> Create(KernelCore& kernel,
|
|
||||||
std::shared_ptr<Session> parent,
|
|
||||||
std::string name = "Unknown");
|
|
||||||
|
|
||||||
/// The parent session, which links to the server endpoint.
|
|
||||||
std::shared_ptr<Session> parent;
|
|
||||||
|
|
||||||
/// Name of the client session (optional)
|
|
||||||
std::string name;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -1,131 +0,0 @@
|
|||||||
// Copyright 2014 Citra Emulator Project
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#include <utility>
|
|
||||||
#include "common/assert.h"
|
|
||||||
#include "common/logging/log.h"
|
|
||||||
#include "core/core.h"
|
|
||||||
#include "core/hle/kernel/handle_table.h"
|
|
||||||
#include "core/hle/kernel/k_scheduler.h"
|
|
||||||
#include "core/hle/kernel/k_thread.h"
|
|
||||||
#include "core/hle/kernel/kernel.h"
|
|
||||||
#include "core/hle/kernel/process.h"
|
|
||||||
#include "core/hle/kernel/svc_results.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
namespace {
|
|
||||||
constexpr u16 GetSlot(Handle handle) {
|
|
||||||
return static_cast<u16>(handle >> 15);
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr u16 GetGeneration(Handle handle) {
|
|
||||||
return static_cast<u16>(handle & 0x7FFF);
|
|
||||||
}
|
|
||||||
} // Anonymous namespace
|
|
||||||
|
|
||||||
HandleTable::HandleTable(KernelCore& kernel) : kernel{kernel} {
|
|
||||||
Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
HandleTable::~HandleTable() = default;
|
|
||||||
|
|
||||||
ResultCode HandleTable::SetSize(s32 handle_table_size) {
|
|
||||||
if (static_cast<u32>(handle_table_size) > MAX_COUNT) {
|
|
||||||
LOG_ERROR(Kernel, "Handle table size {} is greater than {}", handle_table_size, MAX_COUNT);
|
|
||||||
return ResultOutOfMemory;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Values less than or equal to zero indicate to use the maximum allowable
|
|
||||||
// size for the handle table in the actual kernel, so we ignore the given
|
|
||||||
// value in that case, since we assume this by default unless this function
|
|
||||||
// is called.
|
|
||||||
if (handle_table_size > 0) {
|
|
||||||
table_size = static_cast<u16>(handle_table_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
return RESULT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultVal<Handle> HandleTable::Create(std::shared_ptr<Object> obj) {
|
|
||||||
DEBUG_ASSERT(obj != nullptr);
|
|
||||||
|
|
||||||
const u16 slot = next_free_slot;
|
|
||||||
if (slot >= table_size) {
|
|
||||||
LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use.");
|
|
||||||
return ResultHandleTableFull;
|
|
||||||
}
|
|
||||||
next_free_slot = generations[slot];
|
|
||||||
|
|
||||||
const u16 generation = next_generation++;
|
|
||||||
|
|
||||||
// Overflow count so it fits in the 15 bits dedicated to the generation in the handle.
|
|
||||||
// Horizon OS uses zero to represent an invalid handle, so skip to 1.
|
|
||||||
if (next_generation >= (1 << 15)) {
|
|
||||||
next_generation = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
generations[slot] = generation;
|
|
||||||
objects[slot] = std::move(obj);
|
|
||||||
|
|
||||||
Handle handle = generation | (slot << 15);
|
|
||||||
return MakeResult<Handle>(handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultVal<Handle> HandleTable::Duplicate(Handle handle) {
|
|
||||||
std::shared_ptr<Object> object = GetGeneric(handle);
|
|
||||||
if (object == nullptr) {
|
|
||||||
LOG_ERROR(Kernel, "Tried to duplicate invalid handle: {:08X}", handle);
|
|
||||||
return ResultInvalidHandle;
|
|
||||||
}
|
|
||||||
return Create(std::move(object));
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultCode HandleTable::Close(Handle handle) {
|
|
||||||
if (!IsValid(handle)) {
|
|
||||||
LOG_ERROR(Kernel, "Handle is not valid! handle={:08X}", handle);
|
|
||||||
return ResultInvalidHandle;
|
|
||||||
}
|
|
||||||
|
|
||||||
const u16 slot = GetSlot(handle);
|
|
||||||
|
|
||||||
if (objects[slot].use_count() == 1) {
|
|
||||||
objects[slot]->Finalize();
|
|
||||||
}
|
|
||||||
|
|
||||||
objects[slot] = nullptr;
|
|
||||||
|
|
||||||
generations[slot] = next_free_slot;
|
|
||||||
next_free_slot = slot;
|
|
||||||
return RESULT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HandleTable::IsValid(Handle handle) const {
|
|
||||||
const std::size_t slot = GetSlot(handle);
|
|
||||||
const u16 generation = GetGeneration(handle);
|
|
||||||
|
|
||||||
return slot < table_size && objects[slot] != nullptr && generations[slot] == generation;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Object> HandleTable::GetGeneric(Handle handle) const {
|
|
||||||
if (handle == CurrentThread) {
|
|
||||||
return SharedFrom(kernel.CurrentScheduler()->GetCurrentThread());
|
|
||||||
} else if (handle == CurrentProcess) {
|
|
||||||
return SharedFrom(kernel.CurrentProcess());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!IsValid(handle)) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return objects[GetSlot(handle)];
|
|
||||||
}
|
|
||||||
|
|
||||||
void HandleTable::Clear() {
|
|
||||||
for (u16 i = 0; i < table_size; ++i) {
|
|
||||||
generations[i] = static_cast<u16>(i + 1);
|
|
||||||
objects[i] = nullptr;
|
|
||||||
}
|
|
||||||
next_free_slot = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -1,144 +0,0 @@
|
|||||||
// Copyright 2014 Citra Emulator Project
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <cstddef>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "common/common_types.h"
|
|
||||||
#include "core/hle/kernel/object.h"
|
|
||||||
#include "core/hle/result.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
class KernelCore;
|
|
||||||
|
|
||||||
enum KernelHandle : Handle {
|
|
||||||
InvalidHandle = 0,
|
|
||||||
CurrentThread = 0xFFFF8000,
|
|
||||||
CurrentProcess = 0xFFFF8001,
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class allows the creation of Handles, which are references to objects that can be tested
|
|
||||||
* for validity and looked up. Here they are used to pass references to kernel objects to/from the
|
|
||||||
* emulated process. it has been designed so that it follows the same handle format and has
|
|
||||||
* approximately the same restrictions as the handle manager in the CTR-OS.
|
|
||||||
*
|
|
||||||
* Handles contain two sub-fields: a slot index (bits 31:15) and a generation value (bits 14:0).
|
|
||||||
* The slot index is used to index into the arrays in this class to access the data corresponding
|
|
||||||
* to the Handle.
|
|
||||||
*
|
|
||||||
* To prevent accidental use of a freed Handle whose slot has already been reused, a global counter
|
|
||||||
* is kept and incremented every time a Handle is created. This is the Handle's "generation". The
|
|
||||||
* value of the counter is stored into the Handle as well as in the handle table (in the
|
|
||||||
* "generations" array). When looking up a handle, the Handle's generation must match with the
|
|
||||||
* value stored on the class, otherwise the Handle is considered invalid.
|
|
||||||
*
|
|
||||||
* To find free slots when allocating a Handle without needing to scan the entire object array, the
|
|
||||||
* generations field of unallocated slots is re-purposed as a linked list of indices to free slots.
|
|
||||||
* When a Handle is created, an index is popped off the list and used for the new Handle. When it
|
|
||||||
* is destroyed, it is again pushed onto the list to be re-used by the next allocation. It is
|
|
||||||
* likely that this allocation strategy differs from the one used in CTR-OS, but this hasn't been
|
|
||||||
* verified and isn't likely to cause any problems.
|
|
||||||
*/
|
|
||||||
class HandleTable final : NonCopyable {
|
|
||||||
public:
|
|
||||||
/// This is the maximum limit of handles allowed per process in Horizon
|
|
||||||
static constexpr std::size_t MAX_COUNT = 1024;
|
|
||||||
|
|
||||||
explicit HandleTable(KernelCore& kernel);
|
|
||||||
~HandleTable();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the number of handles that may be in use at one time
|
|
||||||
* for this handle table.
|
|
||||||
*
|
|
||||||
* @param handle_table_size The desired size to limit the handle table to.
|
|
||||||
*
|
|
||||||
* @returns an error code indicating if initialization was successful.
|
|
||||||
* If initialization was not successful, then ERR_OUT_OF_MEMORY
|
|
||||||
* will be returned.
|
|
||||||
*
|
|
||||||
* @pre handle_table_size must be within the range [0, 1024]
|
|
||||||
*/
|
|
||||||
ResultCode SetSize(s32 handle_table_size);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Allocates a handle for the given object.
|
|
||||||
* @return The created Handle or one of the following errors:
|
|
||||||
* - `ERR_HANDLE_TABLE_FULL`: the maximum number of handles has been exceeded.
|
|
||||||
*/
|
|
||||||
ResultVal<Handle> Create(std::shared_ptr<Object> obj);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a new handle that points to the same object as the passed in handle.
|
|
||||||
* @return The duplicated Handle or one of the following errors:
|
|
||||||
* - `ERR_INVALID_HANDLE`: an invalid handle was passed in.
|
|
||||||
* - Any errors returned by `Create()`.
|
|
||||||
*/
|
|
||||||
ResultVal<Handle> Duplicate(Handle handle);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Closes a handle, removing it from the table and decreasing the object's ref-count.
|
|
||||||
* @return `RESULT_SUCCESS` or one of the following errors:
|
|
||||||
* - `ERR_INVALID_HANDLE`: an invalid handle was passed in.
|
|
||||||
*/
|
|
||||||
ResultCode Close(Handle handle);
|
|
||||||
|
|
||||||
/// Checks if a handle is valid and points to an existing object.
|
|
||||||
bool IsValid(Handle handle) const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Looks up a handle.
|
|
||||||
* @return Pointer to the looked-up object, or `nullptr` if the handle is not valid.
|
|
||||||
*/
|
|
||||||
std::shared_ptr<Object> GetGeneric(Handle handle) const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Looks up a handle while verifying its type.
|
|
||||||
* @return Pointer to the looked-up object, or `nullptr` if the handle is not valid or its
|
|
||||||
* type differs from the requested one.
|
|
||||||
*/
|
|
||||||
template <class T>
|
|
||||||
std::shared_ptr<T> Get(Handle handle) const {
|
|
||||||
return DynamicObjectCast<T>(GetGeneric(handle));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Closes all handles held in this table.
|
|
||||||
void Clear();
|
|
||||||
|
|
||||||
private:
|
|
||||||
/// Stores the Object referenced by the handle or null if the slot is empty.
|
|
||||||
std::array<std::shared_ptr<Object>, MAX_COUNT> objects;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of `next_generation` when the handle was created, used to check for validity. For
|
|
||||||
* empty slots, contains the index of the next free slot in the list.
|
|
||||||
*/
|
|
||||||
std::array<u16, MAX_COUNT> generations;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The limited size of the handle table. This can be specified by process
|
|
||||||
* capabilities in order to restrict the overall number of handles that
|
|
||||||
* can be created in a process instance
|
|
||||||
*/
|
|
||||||
u16 table_size = static_cast<u16>(MAX_COUNT);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Global counter of the number of created handles. Stored in `generations` when a handle is
|
|
||||||
* created, and wraps around to 1 when it hits 0x8000.
|
|
||||||
*/
|
|
||||||
u16 next_generation = 1;
|
|
||||||
|
|
||||||
/// Head of the free slots linked list.
|
|
||||||
u16 next_free_slot = 0;
|
|
||||||
|
|
||||||
/// Underlying kernel instance that this handle table operates under.
|
|
||||||
KernelCore& kernel;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -0,0 +1,192 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include "common/alignment.h"
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "common/common_funcs.h"
|
||||||
|
#include "common/common_types.h"
|
||||||
|
#include "core/core.h"
|
||||||
|
#include "core/hardware_properties.h"
|
||||||
|
#include "core/hle/kernel/init/init_slab_setup.h"
|
||||||
|
#include "core/hle/kernel/k_event.h"
|
||||||
|
#include "core/hle/kernel/k_memory_layout.h"
|
||||||
|
#include "core/hle/kernel/k_memory_manager.h"
|
||||||
|
#include "core/hle/kernel/k_port.h"
|
||||||
|
#include "core/hle/kernel/k_process.h"
|
||||||
|
#include "core/hle/kernel/k_resource_limit.h"
|
||||||
|
#include "core/hle/kernel/k_session.h"
|
||||||
|
#include "core/hle/kernel/k_shared_memory.h"
|
||||||
|
#include "core/hle/kernel/k_system_control.h"
|
||||||
|
#include "core/hle/kernel/k_thread.h"
|
||||||
|
#include "core/hle/kernel/k_transfer_memory.h"
|
||||||
|
#include "core/hle/kernel/memory_types.h"
|
||||||
|
#include "core/memory.h"
|
||||||
|
|
||||||
|
namespace Kernel::Init {
|
||||||
|
|
||||||
|
#define SLAB_COUNT(CLASS) kernel.SlabResourceCounts().num_##CLASS
|
||||||
|
|
||||||
|
#define FOREACH_SLAB_TYPE(HANDLER, ...) \
|
||||||
|
HANDLER(KProcess, (SLAB_COUNT(KProcess)), ##__VA_ARGS__) \
|
||||||
|
HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \
|
||||||
|
HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \
|
||||||
|
HANDLER(KPort, (SLAB_COUNT(KPort)), ##__VA_ARGS__) \
|
||||||
|
HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) \
|
||||||
|
HANDLER(KTransferMemory, (SLAB_COUNT(KTransferMemory)), ##__VA_ARGS__) \
|
||||||
|
HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) \
|
||||||
|
HANDLER(KResourceLimit, (SLAB_COUNT(KResourceLimit)), ##__VA_ARGS__)
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
#define DEFINE_SLAB_TYPE_ENUM_MEMBER(NAME, COUNT, ...) KSlabType_##NAME,
|
||||||
|
|
||||||
|
enum KSlabType : u32 {
|
||||||
|
FOREACH_SLAB_TYPE(DEFINE_SLAB_TYPE_ENUM_MEMBER) KSlabType_Count,
|
||||||
|
};
|
||||||
|
|
||||||
|
#undef DEFINE_SLAB_TYPE_ENUM_MEMBER
|
||||||
|
|
||||||
|
// Constexpr counts.
|
||||||
|
constexpr size_t SlabCountKProcess = 80;
|
||||||
|
constexpr size_t SlabCountKThread = 800;
|
||||||
|
constexpr size_t SlabCountKEvent = 700;
|
||||||
|
constexpr size_t SlabCountKInterruptEvent = 100;
|
||||||
|
constexpr size_t SlabCountKPort = 256 + 0x20; // Extra 0x20 ports over Nintendo for homebrew.
|
||||||
|
constexpr size_t SlabCountKSharedMemory = 80;
|
||||||
|
constexpr size_t SlabCountKTransferMemory = 200;
|
||||||
|
constexpr size_t SlabCountKCodeMemory = 10;
|
||||||
|
constexpr size_t SlabCountKDeviceAddressSpace = 300;
|
||||||
|
constexpr size_t SlabCountKSession = 933;
|
||||||
|
constexpr size_t SlabCountKLightSession = 100;
|
||||||
|
constexpr size_t SlabCountKObjectName = 7;
|
||||||
|
constexpr size_t SlabCountKResourceLimit = 5;
|
||||||
|
constexpr size_t SlabCountKDebug = Core::Hardware::NUM_CPU_CORES;
|
||||||
|
constexpr size_t SlabCountKAlpha = 1;
|
||||||
|
constexpr size_t SlabCountKBeta = 6;
|
||||||
|
|
||||||
|
constexpr size_t SlabCountExtraKThread = 160;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
VAddr InitializeSlabHeap(Core::System& system, KMemoryLayout& memory_layout, VAddr address,
|
||||||
|
size_t num_objects) {
|
||||||
|
const size_t size = Common::AlignUp(sizeof(T) * num_objects, alignof(void*));
|
||||||
|
VAddr start = Common::AlignUp(address, alignof(T));
|
||||||
|
|
||||||
|
if (size > 0) {
|
||||||
|
const KMemoryRegion* region = memory_layout.FindVirtual(start + size - 1);
|
||||||
|
ASSERT(region != nullptr);
|
||||||
|
ASSERT(region->IsDerivedFrom(KMemoryRegionType_KernelSlab));
|
||||||
|
T::InitializeSlabHeap(system.Kernel(), system.Memory().GetKernelBuffer(start, size), size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return start + size;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
KSlabResourceCounts KSlabResourceCounts::CreateDefault() {
|
||||||
|
return {
|
||||||
|
.num_KProcess = SlabCountKProcess,
|
||||||
|
.num_KThread = SlabCountKThread,
|
||||||
|
.num_KEvent = SlabCountKEvent,
|
||||||
|
.num_KInterruptEvent = SlabCountKInterruptEvent,
|
||||||
|
.num_KPort = SlabCountKPort,
|
||||||
|
.num_KSharedMemory = SlabCountKSharedMemory,
|
||||||
|
.num_KTransferMemory = SlabCountKTransferMemory,
|
||||||
|
.num_KCodeMemory = SlabCountKCodeMemory,
|
||||||
|
.num_KDeviceAddressSpace = SlabCountKDeviceAddressSpace,
|
||||||
|
.num_KSession = SlabCountKSession,
|
||||||
|
.num_KLightSession = SlabCountKLightSession,
|
||||||
|
.num_KObjectName = SlabCountKObjectName,
|
||||||
|
.num_KResourceLimit = SlabCountKResourceLimit,
|
||||||
|
.num_KDebug = SlabCountKDebug,
|
||||||
|
.num_KAlpha = SlabCountKAlpha,
|
||||||
|
.num_KBeta = SlabCountKBeta,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeSlabResourceCounts(KernelCore& kernel) {
|
||||||
|
kernel.SlabResourceCounts() = KSlabResourceCounts::CreateDefault();
|
||||||
|
if (KSystemControl::Init::ShouldIncreaseThreadResourceLimit()) {
|
||||||
|
kernel.SlabResourceCounts().num_KThread += SlabCountExtraKThread;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t CalculateTotalSlabHeapSize(const KernelCore& kernel) {
|
||||||
|
size_t size = 0;
|
||||||
|
|
||||||
|
#define ADD_SLAB_SIZE(NAME, COUNT, ...) \
|
||||||
|
{ \
|
||||||
|
size += alignof(NAME); \
|
||||||
|
size += Common::AlignUp(sizeof(NAME) * (COUNT), alignof(void*)); \
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add the size required for each slab.
|
||||||
|
FOREACH_SLAB_TYPE(ADD_SLAB_SIZE)
|
||||||
|
|
||||||
|
#undef ADD_SLAB_SIZE
|
||||||
|
|
||||||
|
// Add the reserved size.
|
||||||
|
size += KernelSlabHeapGapsSize;
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) {
|
||||||
|
auto& kernel = system.Kernel();
|
||||||
|
|
||||||
|
// Get the start of the slab region, since that's where we'll be working.
|
||||||
|
VAddr address = memory_layout.GetSlabRegionAddress();
|
||||||
|
|
||||||
|
// Initialize slab type array to be in sorted order.
|
||||||
|
std::array<KSlabType, KSlabType_Count> slab_types;
|
||||||
|
for (size_t i = 0; i < slab_types.size(); i++) {
|
||||||
|
slab_types[i] = static_cast<KSlabType>(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// N shuffles the slab type array with the following simple algorithm.
|
||||||
|
for (size_t i = 0; i < slab_types.size(); i++) {
|
||||||
|
const size_t rnd = KSystemControl::GenerateRandomRange(0, slab_types.size() - 1);
|
||||||
|
std::swap(slab_types[i], slab_types[rnd]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create an array to represent the gaps between the slabs.
|
||||||
|
const size_t total_gap_size = KernelSlabHeapGapsSize;
|
||||||
|
std::array<size_t, slab_types.size()> slab_gaps;
|
||||||
|
for (size_t i = 0; i < slab_gaps.size(); i++) {
|
||||||
|
// Note: This is an off-by-one error from Nintendo's intention, because GenerateRandomRange
|
||||||
|
// is inclusive. However, Nintendo also has the off-by-one error, and it's "harmless", so we
|
||||||
|
// will include it ourselves.
|
||||||
|
slab_gaps[i] = KSystemControl::GenerateRandomRange(0, total_gap_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort the array, so that we can treat differences between values as offsets to the starts of
|
||||||
|
// slabs.
|
||||||
|
for (size_t i = 1; i < slab_gaps.size(); i++) {
|
||||||
|
for (size_t j = i; j > 0 && slab_gaps[j - 1] > slab_gaps[j]; j--) {
|
||||||
|
std::swap(slab_gaps[j], slab_gaps[j - 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < slab_types.size(); i++) {
|
||||||
|
// Add the random gap to the address.
|
||||||
|
address += (i == 0) ? slab_gaps[0] : slab_gaps[i] - slab_gaps[i - 1];
|
||||||
|
|
||||||
|
#define INITIALIZE_SLAB_HEAP(NAME, COUNT, ...) \
|
||||||
|
case KSlabType_##NAME: \
|
||||||
|
address = InitializeSlabHeap<NAME>(system, memory_layout, address, COUNT); \
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Initialize the slabheap.
|
||||||
|
switch (slab_types[i]) {
|
||||||
|
// For each of the slab types, we want to initialize that heap.
|
||||||
|
FOREACH_SLAB_TYPE(INITIALIZE_SLAB_HEAP)
|
||||||
|
// If we somehow get an invalid type, abort.
|
||||||
|
default:
|
||||||
|
UNREACHABLE();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel::Init
|
@ -0,0 +1,43 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace Core {
|
||||||
|
class System;
|
||||||
|
} // namespace Core
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
class KernelCore;
|
||||||
|
class KMemoryLayout;
|
||||||
|
} // namespace Kernel
|
||||||
|
|
||||||
|
namespace Kernel::Init {
|
||||||
|
|
||||||
|
struct KSlabResourceCounts {
|
||||||
|
static KSlabResourceCounts CreateDefault();
|
||||||
|
|
||||||
|
size_t num_KProcess;
|
||||||
|
size_t num_KThread;
|
||||||
|
size_t num_KEvent;
|
||||||
|
size_t num_KInterruptEvent;
|
||||||
|
size_t num_KPort;
|
||||||
|
size_t num_KSharedMemory;
|
||||||
|
size_t num_KTransferMemory;
|
||||||
|
size_t num_KCodeMemory;
|
||||||
|
size_t num_KDeviceAddressSpace;
|
||||||
|
size_t num_KSession;
|
||||||
|
size_t num_KLightSession;
|
||||||
|
size_t num_KObjectName;
|
||||||
|
size_t num_KResourceLimit;
|
||||||
|
size_t num_KDebug;
|
||||||
|
size_t num_KAlpha;
|
||||||
|
size_t num_KBeta;
|
||||||
|
};
|
||||||
|
|
||||||
|
void InitializeSlabResourceCounts(KernelCore& kernel);
|
||||||
|
size_t CalculateTotalSlabHeapSize(const KernelCore& kernel);
|
||||||
|
void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout);
|
||||||
|
|
||||||
|
} // namespace Kernel::Init
|
@ -0,0 +1,14 @@
|
|||||||
|
// Copyright 2021 yuzu Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include "core/hle/kernel/k_auto_object.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
KAutoObject* KAutoObject::Create(KAutoObject* obj) {
|
||||||
|
obj->m_ref_count = 1;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,306 @@
|
|||||||
|
// Copyright 2021 yuzu Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "common/common_funcs.h"
|
||||||
|
#include "common/common_types.h"
|
||||||
|
#include "common/intrusive_red_black_tree.h"
|
||||||
|
#include "core/hle/kernel/k_class_token.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KernelCore;
|
||||||
|
class KProcess;
|
||||||
|
|
||||||
|
#define KERNEL_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \
|
||||||
|
YUZU_NON_COPYABLE(CLASS); \
|
||||||
|
YUZU_NON_MOVEABLE(CLASS); \
|
||||||
|
\
|
||||||
|
private: \
|
||||||
|
friend class ::Kernel::KClassTokenGenerator; \
|
||||||
|
static constexpr inline auto ObjectType = ::Kernel::KClassTokenGenerator::ObjectType::CLASS; \
|
||||||
|
static constexpr inline const char* const TypeName = #CLASS; \
|
||||||
|
static constexpr inline ClassTokenType ClassToken() { \
|
||||||
|
return ::Kernel::ClassToken<CLASS>; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
public: \
|
||||||
|
using BaseClass = BASE_CLASS; \
|
||||||
|
static constexpr TypeObj GetStaticTypeObj() { \
|
||||||
|
constexpr ClassTokenType Token = ClassToken(); \
|
||||||
|
return TypeObj(TypeName, Token); \
|
||||||
|
} \
|
||||||
|
static constexpr const char* GetStaticTypeName() { \
|
||||||
|
return TypeName; \
|
||||||
|
} \
|
||||||
|
virtual TypeObj GetTypeObj() const { \
|
||||||
|
return GetStaticTypeObj(); \
|
||||||
|
} \
|
||||||
|
virtual const char* GetTypeName() const { \
|
||||||
|
return GetStaticTypeName(); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
private: \
|
||||||
|
constexpr bool operator!=(const TypeObj& rhs)
|
||||||
|
|
||||||
|
class KAutoObject {
|
||||||
|
protected:
|
||||||
|
class TypeObj {
|
||||||
|
public:
|
||||||
|
constexpr explicit TypeObj(const char* n, ClassTokenType tok)
|
||||||
|
: m_name(n), m_class_token(tok) {}
|
||||||
|
|
||||||
|
constexpr const char* GetName() const {
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
constexpr ClassTokenType GetClassToken() const {
|
||||||
|
return m_class_token;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator==(const TypeObj& rhs) const {
|
||||||
|
return this->GetClassToken() == rhs.GetClassToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator!=(const TypeObj& rhs) const {
|
||||||
|
return this->GetClassToken() != rhs.GetClassToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool IsDerivedFrom(const TypeObj& rhs) const {
|
||||||
|
return (this->GetClassToken() | rhs.GetClassToken()) == this->GetClassToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const char* m_name;
|
||||||
|
ClassTokenType m_class_token;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
KERNEL_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject);
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit KAutoObject(KernelCore& kernel_) : kernel(kernel_) {}
|
||||||
|
virtual ~KAutoObject() = default;
|
||||||
|
|
||||||
|
static KAutoObject* Create(KAutoObject* ptr);
|
||||||
|
|
||||||
|
// Destroy is responsible for destroying the auto object's resources when ref_count hits zero.
|
||||||
|
virtual void Destroy() {
|
||||||
|
UNIMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finalize is responsible for cleaning up resource, but does not destroy the object.
|
||||||
|
virtual void Finalize() {}
|
||||||
|
|
||||||
|
virtual KProcess* GetOwner() const {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 GetReferenceCount() const {
|
||||||
|
return m_ref_count.load();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsDerivedFrom(const TypeObj& rhs) const {
|
||||||
|
return this->GetTypeObj().IsDerivedFrom(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsDerivedFrom(const KAutoObject& rhs) const {
|
||||||
|
return this->IsDerivedFrom(rhs.GetTypeObj());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Derived>
|
||||||
|
Derived DynamicCast() {
|
||||||
|
static_assert(std::is_pointer_v<Derived>);
|
||||||
|
using DerivedType = std::remove_pointer_t<Derived>;
|
||||||
|
|
||||||
|
if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) {
|
||||||
|
return static_cast<Derived>(this);
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Derived>
|
||||||
|
const Derived DynamicCast() const {
|
||||||
|
static_assert(std::is_pointer_v<Derived>);
|
||||||
|
using DerivedType = std::remove_pointer_t<Derived>;
|
||||||
|
|
||||||
|
if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) {
|
||||||
|
return static_cast<Derived>(this);
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Open() {
|
||||||
|
// Atomically increment the reference count, only if it's positive.
|
||||||
|
u32 cur_ref_count = m_ref_count.load(std::memory_order_acquire);
|
||||||
|
do {
|
||||||
|
if (cur_ref_count == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ASSERT(cur_ref_count < cur_ref_count + 1);
|
||||||
|
} while (!m_ref_count.compare_exchange_weak(cur_ref_count, cur_ref_count + 1,
|
||||||
|
std::memory_order_relaxed));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Close() {
|
||||||
|
// Atomically decrement the reference count, not allowing it to become negative.
|
||||||
|
u32 cur_ref_count = m_ref_count.load(std::memory_order_acquire);
|
||||||
|
do {
|
||||||
|
ASSERT(cur_ref_count > 0);
|
||||||
|
} while (!m_ref_count.compare_exchange_weak(cur_ref_count, cur_ref_count - 1,
|
||||||
|
std::memory_order_relaxed));
|
||||||
|
|
||||||
|
// If ref count hits zero, destroy the object.
|
||||||
|
if (cur_ref_count - 1 == 0) {
|
||||||
|
this->Destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
KernelCore& kernel;
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::atomic<u32> m_ref_count{};
|
||||||
|
};
|
||||||
|
|
||||||
|
class KAutoObjectWithListContainer;
|
||||||
|
|
||||||
|
class KAutoObjectWithList : public KAutoObject {
|
||||||
|
public:
|
||||||
|
explicit KAutoObjectWithList(KernelCore& kernel_) : KAutoObject(kernel_), kernel(kernel_) {}
|
||||||
|
|
||||||
|
static int Compare(const KAutoObjectWithList& lhs, const KAutoObjectWithList& rhs) {
|
||||||
|
const u64 lid = lhs.GetId();
|
||||||
|
const u64 rid = rhs.GetId();
|
||||||
|
|
||||||
|
if (lid < rid) {
|
||||||
|
return -1;
|
||||||
|
} else if (lid > rid) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual u64 GetId() const {
|
||||||
|
return reinterpret_cast<u64>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual const std::string& GetName() const {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class KAutoObjectWithListContainer;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Common::IntrusiveRedBlackTreeNode list_node;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
KernelCore& kernel;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class KScopedAutoObject {
|
||||||
|
YUZU_NON_COPYABLE(KScopedAutoObject);
|
||||||
|
|
||||||
|
public:
|
||||||
|
constexpr KScopedAutoObject() = default;
|
||||||
|
|
||||||
|
constexpr KScopedAutoObject(T* o) : m_obj(o) {
|
||||||
|
if (m_obj != nullptr) {
|
||||||
|
m_obj->Open();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~KScopedAutoObject() {
|
||||||
|
if (m_obj != nullptr) {
|
||||||
|
m_obj->Close();
|
||||||
|
}
|
||||||
|
m_obj = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
requires(std::derived_from<T, U> ||
|
||||||
|
std::derived_from<U, T>) constexpr KScopedAutoObject(KScopedAutoObject<U>&& rhs) {
|
||||||
|
if constexpr (std::derived_from<U, T>) {
|
||||||
|
// Upcast.
|
||||||
|
m_obj = rhs.m_obj;
|
||||||
|
rhs.m_obj = nullptr;
|
||||||
|
} else {
|
||||||
|
// Downcast.
|
||||||
|
T* derived = nullptr;
|
||||||
|
if (rhs.m_obj != nullptr) {
|
||||||
|
derived = rhs.m_obj->template DynamicCast<T*>();
|
||||||
|
if (derived == nullptr) {
|
||||||
|
rhs.m_obj->Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_obj = derived;
|
||||||
|
rhs.m_obj = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr KScopedAutoObject<T>& operator=(KScopedAutoObject<T>&& rhs) {
|
||||||
|
rhs.Swap(*this);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr T* operator->() {
|
||||||
|
return m_obj;
|
||||||
|
}
|
||||||
|
constexpr T& operator*() {
|
||||||
|
return *m_obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void Reset(T* o) {
|
||||||
|
KScopedAutoObject(o).Swap(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr T* GetPointerUnsafe() {
|
||||||
|
return m_obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr T* GetPointerUnsafe() const {
|
||||||
|
return m_obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr T* ReleasePointerUnsafe() {
|
||||||
|
T* ret = m_obj;
|
||||||
|
m_obj = nullptr;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool IsNull() const {
|
||||||
|
return m_obj == nullptr;
|
||||||
|
}
|
||||||
|
constexpr bool IsNotNull() const {
|
||||||
|
return m_obj != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template <typename U>
|
||||||
|
friend class KScopedAutoObject;
|
||||||
|
|
||||||
|
private:
|
||||||
|
T* m_obj{};
|
||||||
|
|
||||||
|
private:
|
||||||
|
constexpr void Swap(KScopedAutoObject& rhs) noexcept {
|
||||||
|
std::swap(m_obj, rhs.m_obj);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,28 @@
|
|||||||
|
// Copyright 2021 yuzu Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include "core/hle/kernel/k_auto_object_container.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
void KAutoObjectWithListContainer::Register(KAutoObjectWithList* obj) {
|
||||||
|
KScopedLightLock lk(m_lock);
|
||||||
|
|
||||||
|
m_object_list.insert(*obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
void KAutoObjectWithListContainer::Unregister(KAutoObjectWithList* obj) {
|
||||||
|
KScopedLightLock lk(m_lock);
|
||||||
|
|
||||||
|
m_object_list.erase(m_object_list.iterator_to(*obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t KAutoObjectWithListContainer::GetOwnedCount(KProcess* owner) {
|
||||||
|
KScopedLightLock lk(m_lock);
|
||||||
|
|
||||||
|
return std::count_if(m_object_list.begin(), m_object_list.end(),
|
||||||
|
[&](const auto& obj) { return obj.GetOwner() == owner; });
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,70 @@
|
|||||||
|
// Copyright 2021 yuzu Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "common/common_funcs.h"
|
||||||
|
#include "common/common_types.h"
|
||||||
|
#include "common/intrusive_red_black_tree.h"
|
||||||
|
#include "core/hle/kernel/k_auto_object.h"
|
||||||
|
#include "core/hle/kernel/k_light_lock.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KernelCore;
|
||||||
|
class KProcess;
|
||||||
|
|
||||||
|
class KAutoObjectWithListContainer {
|
||||||
|
YUZU_NON_COPYABLE(KAutoObjectWithListContainer);
|
||||||
|
YUZU_NON_MOVEABLE(KAutoObjectWithListContainer);
|
||||||
|
|
||||||
|
public:
|
||||||
|
using ListType = Common::IntrusiveRedBlackTreeMemberTraits<
|
||||||
|
&KAutoObjectWithList::list_node>::TreeType<KAutoObjectWithList>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
class ListAccessor : public KScopedLightLock {
|
||||||
|
public:
|
||||||
|
explicit ListAccessor(KAutoObjectWithListContainer* container)
|
||||||
|
: KScopedLightLock(container->m_lock), m_list(container->m_object_list) {}
|
||||||
|
explicit ListAccessor(KAutoObjectWithListContainer& container)
|
||||||
|
: KScopedLightLock(container.m_lock), m_list(container.m_object_list) {}
|
||||||
|
|
||||||
|
typename ListType::iterator begin() const {
|
||||||
|
return m_list.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
typename ListType::iterator end() const {
|
||||||
|
return m_list.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
typename ListType::iterator find(typename ListType::const_reference ref) const {
|
||||||
|
return m_list.find(ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
ListType& m_list;
|
||||||
|
};
|
||||||
|
|
||||||
|
friend class ListAccessor;
|
||||||
|
|
||||||
|
public:
|
||||||
|
KAutoObjectWithListContainer(KernelCore& kernel) : m_lock(kernel), m_object_list() {}
|
||||||
|
|
||||||
|
void Initialize() {}
|
||||||
|
void Finalize() {}
|
||||||
|
|
||||||
|
void Register(KAutoObjectWithList* obj);
|
||||||
|
void Unregister(KAutoObjectWithList* obj);
|
||||||
|
size_t GetOwnedCount(KProcess* owner);
|
||||||
|
|
||||||
|
private:
|
||||||
|
KLightLock m_lock;
|
||||||
|
ListType m_object_list;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,133 @@
|
|||||||
|
// Copyright 2021 yuzu Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include "core/hle/kernel/k_auto_object.h"
|
||||||
|
#include "core/hle/kernel/k_class_token.h"
|
||||||
|
#include "core/hle/kernel/k_client_port.h"
|
||||||
|
#include "core/hle/kernel/k_client_session.h"
|
||||||
|
#include "core/hle/kernel/k_event.h"
|
||||||
|
#include "core/hle/kernel/k_port.h"
|
||||||
|
#include "core/hle/kernel/k_process.h"
|
||||||
|
#include "core/hle/kernel/k_readable_event.h"
|
||||||
|
#include "core/hle/kernel/k_resource_limit.h"
|
||||||
|
#include "core/hle/kernel/k_server_port.h"
|
||||||
|
#include "core/hle/kernel/k_server_session.h"
|
||||||
|
#include "core/hle/kernel/k_session.h"
|
||||||
|
#include "core/hle/kernel/k_shared_memory.h"
|
||||||
|
#include "core/hle/kernel/k_synchronization_object.h"
|
||||||
|
#include "core/hle/kernel/k_thread.h"
|
||||||
|
#include "core/hle/kernel/k_transfer_memory.h"
|
||||||
|
#include "core/hle/kernel/k_writable_event.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
// Ensure that we generate correct class tokens for all types.
|
||||||
|
|
||||||
|
// Ensure that the absolute token values are correct.
|
||||||
|
static_assert(ClassToken<KAutoObject> == 0b00000000'00000000);
|
||||||
|
static_assert(ClassToken<KSynchronizationObject> == 0b00000000'00000001);
|
||||||
|
static_assert(ClassToken<KReadableEvent> == 0b00000000'00000011);
|
||||||
|
// static_assert(ClassToken<KInterruptEvent> == 0b00000111'00000011);
|
||||||
|
// static_assert(ClassToken<KDebug> == 0b00001011'00000001);
|
||||||
|
static_assert(ClassToken<KThread> == 0b00010011'00000001);
|
||||||
|
static_assert(ClassToken<KServerPort> == 0b00100011'00000001);
|
||||||
|
static_assert(ClassToken<KServerSession> == 0b01000011'00000001);
|
||||||
|
static_assert(ClassToken<KClientPort> == 0b10000011'00000001);
|
||||||
|
static_assert(ClassToken<KClientSession> == 0b00001101'00000000);
|
||||||
|
static_assert(ClassToken<KProcess> == 0b00010101'00000001);
|
||||||
|
static_assert(ClassToken<KResourceLimit> == 0b00100101'00000000);
|
||||||
|
// static_assert(ClassToken<KLightSession> == 0b01000101'00000000);
|
||||||
|
static_assert(ClassToken<KPort> == 0b10000101'00000000);
|
||||||
|
static_assert(ClassToken<KSession> == 0b00011001'00000000);
|
||||||
|
static_assert(ClassToken<KSharedMemory> == 0b00101001'00000000);
|
||||||
|
static_assert(ClassToken<KEvent> == 0b01001001'00000000);
|
||||||
|
static_assert(ClassToken<KWritableEvent> == 0b10001001'00000000);
|
||||||
|
// static_assert(ClassToken<KLightClientSession> == 0b00110001'00000000);
|
||||||
|
// static_assert(ClassToken<KLightServerSession> == 0b01010001'00000000);
|
||||||
|
static_assert(ClassToken<KTransferMemory> == 0b10010001'00000000);
|
||||||
|
// static_assert(ClassToken<KDeviceAddressSpace> == 0b01100001'00000000);
|
||||||
|
// static_assert(ClassToken<KSessionRequest> == 0b10100001'00000000);
|
||||||
|
// static_assert(ClassToken<KCodeMemory> == 0b11000001'00000000);
|
||||||
|
|
||||||
|
// Ensure that the token hierarchy is correct.
|
||||||
|
|
||||||
|
// Base classes
|
||||||
|
static_assert(ClassToken<KAutoObject> == (0b00000000));
|
||||||
|
static_assert(ClassToken<KSynchronizationObject> == (0b00000001 | ClassToken<KAutoObject>));
|
||||||
|
static_assert(ClassToken<KReadableEvent> == (0b00000010 | ClassToken<KSynchronizationObject>));
|
||||||
|
|
||||||
|
// Final classes
|
||||||
|
// static_assert(ClassToken<KInterruptEvent> == ((0b00000111 << 8) | ClassToken<KReadableEvent>));
|
||||||
|
// static_assert(ClassToken<KDebug> == ((0b00001011 << 8) | ClassToken<KSynchronizationObject>));
|
||||||
|
static_assert(ClassToken<KThread> == ((0b00010011 << 8) | ClassToken<KSynchronizationObject>));
|
||||||
|
static_assert(ClassToken<KServerPort> == ((0b00100011 << 8) | ClassToken<KSynchronizationObject>));
|
||||||
|
static_assert(ClassToken<KServerSession> ==
|
||||||
|
((0b01000011 << 8) | ClassToken<KSynchronizationObject>));
|
||||||
|
static_assert(ClassToken<KClientPort> == ((0b10000011 << 8) | ClassToken<KSynchronizationObject>));
|
||||||
|
static_assert(ClassToken<KClientSession> == ((0b00001101 << 8) | ClassToken<KAutoObject>));
|
||||||
|
static_assert(ClassToken<KProcess> == ((0b00010101 << 8) | ClassToken<KSynchronizationObject>));
|
||||||
|
static_assert(ClassToken<KResourceLimit> == ((0b00100101 << 8) | ClassToken<KAutoObject>));
|
||||||
|
// static_assert(ClassToken<KLightSession> == ((0b01000101 << 8) | ClassToken<KAutoObject>));
|
||||||
|
static_assert(ClassToken<KPort> == ((0b10000101 << 8) | ClassToken<KAutoObject>));
|
||||||
|
static_assert(ClassToken<KSession> == ((0b00011001 << 8) | ClassToken<KAutoObject>));
|
||||||
|
static_assert(ClassToken<KSharedMemory> == ((0b00101001 << 8) | ClassToken<KAutoObject>));
|
||||||
|
static_assert(ClassToken<KEvent> == ((0b01001001 << 8) | ClassToken<KAutoObject>));
|
||||||
|
static_assert(ClassToken<KWritableEvent> == ((0b10001001 << 8) | ClassToken<KAutoObject>));
|
||||||
|
// static_assert(ClassToken<KLightClientSession> == ((0b00110001 << 8) | ClassToken<KAutoObject>));
|
||||||
|
// static_assert(ClassToken<KLightServerSession> == ((0b01010001 << 8) | ClassToken<KAutoObject>));
|
||||||
|
static_assert(ClassToken<KTransferMemory> == ((0b10010001 << 8) | ClassToken<KAutoObject>));
|
||||||
|
// static_assert(ClassToken<KDeviceAddressSpace> == ((0b01100001 << 8) | ClassToken<KAutoObject>));
|
||||||
|
// static_assert(ClassToken<KSessionRequest> == ((0b10100001 << 8) | ClassToken<KAutoObject>));
|
||||||
|
// static_assert(ClassToken<KCodeMemory> == ((0b11000001 << 8) | ClassToken<KAutoObject>));
|
||||||
|
|
||||||
|
// Ensure that the token hierarchy reflects the class hierarchy.
|
||||||
|
|
||||||
|
// Base classes.
|
||||||
|
static_assert(!std::is_final<KSynchronizationObject>::value &&
|
||||||
|
std::is_base_of<KAutoObject, KSynchronizationObject>::value);
|
||||||
|
static_assert(!std::is_final<KReadableEvent>::value &&
|
||||||
|
std::is_base_of<KSynchronizationObject, KReadableEvent>::value);
|
||||||
|
|
||||||
|
// Final classes
|
||||||
|
// static_assert(std::is_final<KInterruptEvent>::value &&
|
||||||
|
// std::is_base_of<KReadableEvent, KInterruptEvent>::value);
|
||||||
|
// static_assert(std::is_final<KDebug>::value &&
|
||||||
|
// std::is_base_of<KSynchronizationObject, KDebug>::value);
|
||||||
|
static_assert(std::is_final<KThread>::value &&
|
||||||
|
std::is_base_of<KSynchronizationObject, KThread>::value);
|
||||||
|
static_assert(std::is_final<KServerPort>::value &&
|
||||||
|
std::is_base_of<KSynchronizationObject, KServerPort>::value);
|
||||||
|
static_assert(std::is_final<KServerSession>::value &&
|
||||||
|
std::is_base_of<KSynchronizationObject, KServerSession>::value);
|
||||||
|
static_assert(std::is_final<KClientPort>::value &&
|
||||||
|
std::is_base_of<KSynchronizationObject, KClientPort>::value);
|
||||||
|
static_assert(std::is_final<KClientSession>::value &&
|
||||||
|
std::is_base_of<KAutoObject, KClientSession>::value);
|
||||||
|
static_assert(std::is_final<KProcess>::value &&
|
||||||
|
std::is_base_of<KSynchronizationObject, KProcess>::value);
|
||||||
|
static_assert(std::is_final<KResourceLimit>::value &&
|
||||||
|
std::is_base_of<KAutoObject, KResourceLimit>::value);
|
||||||
|
// static_assert(std::is_final<KLightSession>::value &&
|
||||||
|
// std::is_base_of<KAutoObject, KLightSession>::value);
|
||||||
|
static_assert(std::is_final<KPort>::value && std::is_base_of<KAutoObject, KPort>::value);
|
||||||
|
static_assert(std::is_final<KSession>::value && std::is_base_of<KAutoObject, KSession>::value);
|
||||||
|
static_assert(std::is_final<KSharedMemory>::value &&
|
||||||
|
std::is_base_of<KAutoObject, KSharedMemory>::value);
|
||||||
|
static_assert(std::is_final<KEvent>::value && std::is_base_of<KAutoObject, KEvent>::value);
|
||||||
|
static_assert(std::is_final<KWritableEvent>::value &&
|
||||||
|
std::is_base_of<KAutoObject, KWritableEvent>::value);
|
||||||
|
// static_assert(std::is_final<KLightClientSession>::value &&
|
||||||
|
// std::is_base_of<KAutoObject, KLightClientSession>::value);
|
||||||
|
// static_assert(std::is_final<KLightServerSession>::value &&
|
||||||
|
// std::is_base_of<KAutoObject, KLightServerSession>::value);
|
||||||
|
static_assert(std::is_final<KTransferMemory>::value &&
|
||||||
|
std::is_base_of<KAutoObject, KTransferMemory>::value);
|
||||||
|
// static_assert(std::is_final<KDeviceAddressSpace>::value &&
|
||||||
|
// std::is_base_of<KAutoObject, KDeviceAddressSpace>::value);
|
||||||
|
// static_assert(std::is_final<KSessionRequest>::value &&
|
||||||
|
// std::is_base_of<KAutoObject, KSessionRequest>::value);
|
||||||
|
// static_assert(std::is_final<KCodeMemory>::value &&
|
||||||
|
// std::is_base_of<KAutoObject, KCodeMemory>::value);
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,131 @@
|
|||||||
|
// Copyright 2021 yuzu Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "common/bit_util.h"
|
||||||
|
#include "common/common_types.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KAutoObject;
|
||||||
|
|
||||||
|
class KClassTokenGenerator {
|
||||||
|
public:
|
||||||
|
using TokenBaseType = u16;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static constexpr size_t BaseClassBits = 8;
|
||||||
|
static constexpr size_t FinalClassBits = (sizeof(TokenBaseType) * CHAR_BIT) - BaseClassBits;
|
||||||
|
// One bit per base class.
|
||||||
|
static constexpr size_t NumBaseClasses = BaseClassBits;
|
||||||
|
// Final classes are permutations of three bits.
|
||||||
|
static constexpr size_t NumFinalClasses = [] {
|
||||||
|
TokenBaseType index = 0;
|
||||||
|
for (size_t i = 0; i < FinalClassBits; i++) {
|
||||||
|
for (size_t j = i + 1; j < FinalClassBits; j++) {
|
||||||
|
for (size_t k = j + 1; k < FinalClassBits; k++) {
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}();
|
||||||
|
|
||||||
|
private:
|
||||||
|
template <TokenBaseType Index>
|
||||||
|
static constexpr inline TokenBaseType BaseClassToken = 1U << Index;
|
||||||
|
|
||||||
|
template <TokenBaseType Index>
|
||||||
|
static constexpr inline TokenBaseType FinalClassToken = [] {
|
||||||
|
TokenBaseType index = 0;
|
||||||
|
for (size_t i = 0; i < FinalClassBits; i++) {
|
||||||
|
for (size_t j = i + 1; j < FinalClassBits; j++) {
|
||||||
|
for (size_t k = j + 1; k < FinalClassBits; k++) {
|
||||||
|
if ((index++) == Index) {
|
||||||
|
return static_cast<TokenBaseType>(((1ULL << i) | (1ULL << j) | (1ULL << k))
|
||||||
|
<< BaseClassBits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}();
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline TokenBaseType GetClassToken() {
|
||||||
|
static_assert(std::is_base_of<KAutoObject, T>::value);
|
||||||
|
if constexpr (std::is_same<T, KAutoObject>::value) {
|
||||||
|
static_assert(T::ObjectType == ObjectType::KAutoObject);
|
||||||
|
return 0;
|
||||||
|
} else if constexpr (!std::is_final<T>::value) {
|
||||||
|
static_assert(ObjectType::BaseClassesStart <= T::ObjectType &&
|
||||||
|
T::ObjectType < ObjectType::BaseClassesEnd);
|
||||||
|
constexpr auto ClassIndex = static_cast<TokenBaseType>(T::ObjectType) -
|
||||||
|
static_cast<TokenBaseType>(ObjectType::BaseClassesStart);
|
||||||
|
return BaseClassToken<ClassIndex> | GetClassToken<typename T::BaseClass>();
|
||||||
|
} else if constexpr (ObjectType::FinalClassesStart <= T::ObjectType &&
|
||||||
|
T::ObjectType < ObjectType::FinalClassesEnd) {
|
||||||
|
constexpr auto ClassIndex = static_cast<TokenBaseType>(T::ObjectType) -
|
||||||
|
static_cast<TokenBaseType>(ObjectType::FinalClassesStart);
|
||||||
|
return FinalClassToken<ClassIndex> | GetClassToken<typename T::BaseClass>();
|
||||||
|
} else {
|
||||||
|
static_assert(!std::is_same<T, T>::value, "GetClassToken: Invalid Type");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum class ObjectType {
|
||||||
|
KAutoObject,
|
||||||
|
|
||||||
|
BaseClassesStart,
|
||||||
|
|
||||||
|
KSynchronizationObject = BaseClassesStart,
|
||||||
|
KReadableEvent,
|
||||||
|
|
||||||
|
BaseClassesEnd,
|
||||||
|
|
||||||
|
FinalClassesStart = BaseClassesEnd,
|
||||||
|
|
||||||
|
KInterruptEvent = FinalClassesStart,
|
||||||
|
KDebug,
|
||||||
|
KThread,
|
||||||
|
KServerPort,
|
||||||
|
KServerSession,
|
||||||
|
KClientPort,
|
||||||
|
KClientSession,
|
||||||
|
KProcess,
|
||||||
|
KResourceLimit,
|
||||||
|
KLightSession,
|
||||||
|
KPort,
|
||||||
|
KSession,
|
||||||
|
KSharedMemory,
|
||||||
|
KEvent,
|
||||||
|
KWritableEvent,
|
||||||
|
KLightClientSession,
|
||||||
|
KLightServerSession,
|
||||||
|
KTransferMemory,
|
||||||
|
KDeviceAddressSpace,
|
||||||
|
KSessionRequest,
|
||||||
|
KCodeMemory,
|
||||||
|
|
||||||
|
// NOTE: True order for these has not been determined yet.
|
||||||
|
KAlpha,
|
||||||
|
KBeta,
|
||||||
|
|
||||||
|
FinalClassesEnd = FinalClassesStart + NumFinalClasses,
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline TokenBaseType ClassToken = GetClassToken<T>();
|
||||||
|
};
|
||||||
|
|
||||||
|
using ClassTokenType = KClassTokenGenerator::TokenBaseType;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr inline ClassTokenType ClassToken = KClassTokenGenerator::ClassToken<T>;
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,125 @@
|
|||||||
|
// Copyright 2021 Citra Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include "common/scope_exit.h"
|
||||||
|
#include "core/hle/kernel/hle_ipc.h"
|
||||||
|
#include "core/hle/kernel/k_client_port.h"
|
||||||
|
#include "core/hle/kernel/k_port.h"
|
||||||
|
#include "core/hle/kernel/k_scheduler.h"
|
||||||
|
#include "core/hle/kernel/k_scoped_resource_reservation.h"
|
||||||
|
#include "core/hle/kernel/k_session.h"
|
||||||
|
#include "core/hle/kernel/svc_results.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
KClientPort::KClientPort(KernelCore& kernel) : KSynchronizationObject{kernel} {}
|
||||||
|
KClientPort::~KClientPort() = default;
|
||||||
|
|
||||||
|
void KClientPort::Initialize(KPort* parent_, s32 max_sessions_, std::string&& name_) {
|
||||||
|
// Set member variables.
|
||||||
|
num_sessions = 0;
|
||||||
|
peak_sessions = 0;
|
||||||
|
parent = parent_;
|
||||||
|
max_sessions = max_sessions_;
|
||||||
|
name = std::move(name_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void KClientPort::OnSessionFinalized() {
|
||||||
|
KScopedSchedulerLock sl{kernel};
|
||||||
|
|
||||||
|
const auto prev = num_sessions--;
|
||||||
|
if (prev == max_sessions) {
|
||||||
|
this->NotifyAvailable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void KClientPort::OnServerClosed() {}
|
||||||
|
|
||||||
|
bool KClientPort::IsLight() const {
|
||||||
|
return this->GetParent()->IsLight();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KClientPort::IsServerClosed() const {
|
||||||
|
return this->GetParent()->IsServerClosed();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KClientPort::Destroy() {
|
||||||
|
// Note with our parent that we're closed.
|
||||||
|
parent->OnClientClosed();
|
||||||
|
|
||||||
|
// Close our reference to our parent.
|
||||||
|
parent->Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KClientPort::IsSignaled() const {
|
||||||
|
return num_sessions < max_sessions;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResultCode KClientPort::CreateSession(KClientSession** out) {
|
||||||
|
// Reserve a new session from the resource limit.
|
||||||
|
KScopedResourceReservation session_reservation(kernel.CurrentProcess()->GetResourceLimit(),
|
||||||
|
LimitableResource::Sessions);
|
||||||
|
R_UNLESS(session_reservation.Succeeded(), ResultLimitReached);
|
||||||
|
|
||||||
|
// Update the session counts.
|
||||||
|
{
|
||||||
|
// Atomically increment the number of sessions.
|
||||||
|
s32 new_sessions;
|
||||||
|
{
|
||||||
|
const auto max = max_sessions;
|
||||||
|
auto cur_sessions = num_sessions.load(std::memory_order_acquire);
|
||||||
|
do {
|
||||||
|
R_UNLESS(cur_sessions < max, ResultOutOfSessions);
|
||||||
|
new_sessions = cur_sessions + 1;
|
||||||
|
} while (!num_sessions.compare_exchange_weak(cur_sessions, new_sessions,
|
||||||
|
std::memory_order_relaxed));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Atomically update the peak session tracking.
|
||||||
|
{
|
||||||
|
auto peak = peak_sessions.load(std::memory_order_acquire);
|
||||||
|
do {
|
||||||
|
if (peak >= new_sessions) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (!peak_sessions.compare_exchange_weak(peak, new_sessions,
|
||||||
|
std::memory_order_relaxed));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new session.
|
||||||
|
KSession* session = KSession::Create(kernel);
|
||||||
|
if (session == nullptr) {
|
||||||
|
/* Decrement the session count. */
|
||||||
|
const auto prev = num_sessions--;
|
||||||
|
if (prev == max_sessions) {
|
||||||
|
this->NotifyAvailable();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResultOutOfResource;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the session.
|
||||||
|
session->Initialize(this, parent->GetName());
|
||||||
|
|
||||||
|
// Commit the session reservation.
|
||||||
|
session_reservation.Commit();
|
||||||
|
|
||||||
|
// Register the session.
|
||||||
|
KSession::Register(kernel, session);
|
||||||
|
auto session_guard = SCOPE_GUARD({
|
||||||
|
session->GetClientSession().Close();
|
||||||
|
session->GetServerSession().Close();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Enqueue the session with our parent.
|
||||||
|
R_TRY(parent->EnqueueSession(std::addressof(session->GetServerSession())));
|
||||||
|
|
||||||
|
// We succeeded, so set the output.
|
||||||
|
session_guard.Cancel();
|
||||||
|
*out = std::addressof(session->GetClientSession());
|
||||||
|
return RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,61 @@
|
|||||||
|
// Copyright 2016 Citra Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "common/common_types.h"
|
||||||
|
#include "core/hle/kernel/k_synchronization_object.h"
|
||||||
|
#include "core/hle/result.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KClientSession;
|
||||||
|
class KernelCore;
|
||||||
|
class KPort;
|
||||||
|
|
||||||
|
class KClientPort final : public KSynchronizationObject {
|
||||||
|
KERNEL_AUTOOBJECT_TRAITS(KClientPort, KSynchronizationObject);
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit KClientPort(KernelCore& kernel);
|
||||||
|
virtual ~KClientPort() override;
|
||||||
|
|
||||||
|
void Initialize(KPort* parent_, s32 max_sessions_, std::string&& name_);
|
||||||
|
void OnSessionFinalized();
|
||||||
|
void OnServerClosed();
|
||||||
|
|
||||||
|
const KPort* GetParent() const {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
s32 GetNumSessions() const {
|
||||||
|
return num_sessions;
|
||||||
|
}
|
||||||
|
s32 GetPeakSessions() const {
|
||||||
|
return peak_sessions;
|
||||||
|
}
|
||||||
|
s32 GetMaxSessions() const {
|
||||||
|
return max_sessions;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsLight() const;
|
||||||
|
bool IsServerClosed() const;
|
||||||
|
|
||||||
|
// Overridden virtual functions.
|
||||||
|
virtual void Destroy() override;
|
||||||
|
virtual bool IsSignaled() const override;
|
||||||
|
|
||||||
|
ResultCode CreateSession(KClientSession** out);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::atomic<s32> num_sessions{};
|
||||||
|
std::atomic<s32> peak_sessions{};
|
||||||
|
s32 max_sessions{};
|
||||||
|
KPort* parent{};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,31 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include "core/hle/kernel/hle_ipc.h"
|
||||||
|
#include "core/hle/kernel/k_client_session.h"
|
||||||
|
#include "core/hle/kernel/k_server_session.h"
|
||||||
|
#include "core/hle/kernel/k_session.h"
|
||||||
|
#include "core/hle/kernel/k_thread.h"
|
||||||
|
#include "core/hle/kernel/svc_results.h"
|
||||||
|
#include "core/hle/result.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
KClientSession::KClientSession(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel} {}
|
||||||
|
KClientSession::~KClientSession() = default;
|
||||||
|
|
||||||
|
void KClientSession::Destroy() {
|
||||||
|
parent->OnClientClosed();
|
||||||
|
parent->Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KClientSession::OnServerClosed() {}
|
||||||
|
|
||||||
|
ResultCode KClientSession::SendSyncRequest(KThread* thread, Core::Memory::Memory& memory,
|
||||||
|
Core::Timing::CoreTiming& core_timing) {
|
||||||
|
// Signal the server session that new data is available
|
||||||
|
return parent->GetServerSession().HandleSyncRequest(thread, memory, core_timing);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,61 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "core/hle/kernel/k_auto_object.h"
|
||||||
|
#include "core/hle/kernel/k_synchronization_object.h"
|
||||||
|
#include "core/hle/kernel/slab_helpers.h"
|
||||||
|
#include "core/hle/result.h"
|
||||||
|
|
||||||
|
union ResultCode;
|
||||||
|
|
||||||
|
namespace Core::Memory {
|
||||||
|
class Memory;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Core::Timing {
|
||||||
|
class CoreTiming;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KernelCore;
|
||||||
|
class KSession;
|
||||||
|
class KThread;
|
||||||
|
|
||||||
|
class KClientSession final
|
||||||
|
: public KAutoObjectWithSlabHeapAndContainer<KClientSession, KAutoObjectWithList> {
|
||||||
|
KERNEL_AUTOOBJECT_TRAITS(KClientSession, KAutoObject);
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit KClientSession(KernelCore& kernel);
|
||||||
|
virtual ~KClientSession();
|
||||||
|
|
||||||
|
void Initialize(KSession* parent_, std::string&& name_) {
|
||||||
|
// Set member variables.
|
||||||
|
parent = parent_;
|
||||||
|
name = std::move(name_);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void Destroy() override;
|
||||||
|
static void PostDestroy([[maybe_unused]] uintptr_t arg) {}
|
||||||
|
|
||||||
|
KSession* GetParent() const {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResultCode SendSyncRequest(KThread* thread, Core::Memory::Memory& memory,
|
||||||
|
Core::Timing::CoreTiming& core_timing);
|
||||||
|
|
||||||
|
void OnServerClosed();
|
||||||
|
|
||||||
|
private:
|
||||||
|
KSession* parent{};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,135 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include "core/hle/kernel/k_handle_table.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
KHandleTable::KHandleTable(KernelCore& kernel_) : kernel{kernel_} {}
|
||||||
|
KHandleTable ::~KHandleTable() = default;
|
||||||
|
|
||||||
|
ResultCode KHandleTable::Finalize() {
|
||||||
|
// Get the table and clear our record of it.
|
||||||
|
u16 saved_table_size = 0;
|
||||||
|
{
|
||||||
|
KScopedSpinLock lk(m_lock);
|
||||||
|
|
||||||
|
std::swap(m_table_size, saved_table_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close and free all entries.
|
||||||
|
for (size_t i = 0; i < saved_table_size; i++) {
|
||||||
|
if (KAutoObject* obj = m_objects[i]; obj != nullptr) {
|
||||||
|
obj->Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KHandleTable::Remove(Handle handle) {
|
||||||
|
// Don't allow removal of a pseudo-handle.
|
||||||
|
if (Svc::IsPseudoHandle(handle)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles must not have reserved bits set.
|
||||||
|
const auto handle_pack = HandlePack(handle);
|
||||||
|
if (handle_pack.reserved != 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the object and free the entry.
|
||||||
|
KAutoObject* obj = nullptr;
|
||||||
|
{
|
||||||
|
KScopedSpinLock lk(m_lock);
|
||||||
|
|
||||||
|
if (this->IsValidHandle(handle)) {
|
||||||
|
const auto index = handle_pack.index;
|
||||||
|
|
||||||
|
obj = m_objects[index];
|
||||||
|
this->FreeEntry(index);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the object.
|
||||||
|
obj->Close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResultCode KHandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) {
|
||||||
|
KScopedSpinLock lk(m_lock);
|
||||||
|
|
||||||
|
// Never exceed our capacity.
|
||||||
|
R_UNLESS(m_count < m_table_size, ResultOutOfHandles);
|
||||||
|
|
||||||
|
// Allocate entry, set output handle.
|
||||||
|
{
|
||||||
|
const auto linear_id = this->AllocateLinearId();
|
||||||
|
const auto index = this->AllocateEntry();
|
||||||
|
|
||||||
|
m_entry_infos[index].info = {.linear_id = linear_id, .type = type};
|
||||||
|
m_objects[index] = obj;
|
||||||
|
|
||||||
|
obj->Open();
|
||||||
|
|
||||||
|
*out_handle = EncodeHandle(static_cast<u16>(index), linear_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResultCode KHandleTable::Reserve(Handle* out_handle) {
|
||||||
|
KScopedSpinLock lk(m_lock);
|
||||||
|
|
||||||
|
// Never exceed our capacity.
|
||||||
|
R_UNLESS(m_count < m_table_size, ResultOutOfHandles);
|
||||||
|
|
||||||
|
*out_handle = EncodeHandle(static_cast<u16>(this->AllocateEntry()), this->AllocateLinearId());
|
||||||
|
return RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void KHandleTable::Unreserve(Handle handle) {
|
||||||
|
KScopedSpinLock lk(m_lock);
|
||||||
|
|
||||||
|
// Unpack the handle.
|
||||||
|
const auto handle_pack = HandlePack(handle);
|
||||||
|
const auto index = handle_pack.index;
|
||||||
|
const auto linear_id = handle_pack.linear_id;
|
||||||
|
const auto reserved = handle_pack.reserved;
|
||||||
|
ASSERT(reserved == 0);
|
||||||
|
ASSERT(linear_id != 0);
|
||||||
|
|
||||||
|
if (index < m_table_size) {
|
||||||
|
// NOTE: This code does not check the linear id.
|
||||||
|
ASSERT(m_objects[index] == nullptr);
|
||||||
|
this->FreeEntry(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void KHandleTable::Register(Handle handle, KAutoObject* obj, u16 type) {
|
||||||
|
KScopedSpinLock lk(m_lock);
|
||||||
|
|
||||||
|
// Unpack the handle.
|
||||||
|
const auto handle_pack = HandlePack(handle);
|
||||||
|
const auto index = handle_pack.index;
|
||||||
|
const auto linear_id = handle_pack.linear_id;
|
||||||
|
const auto reserved = handle_pack.reserved;
|
||||||
|
ASSERT(reserved == 0);
|
||||||
|
ASSERT(linear_id != 0);
|
||||||
|
|
||||||
|
if (index < m_table_size) {
|
||||||
|
// Set the entry.
|
||||||
|
ASSERT(m_objects[index] == nullptr);
|
||||||
|
|
||||||
|
m_entry_infos[index].info = {.linear_id = static_cast<u16>(linear_id), .type = type};
|
||||||
|
m_objects[index] = obj;
|
||||||
|
|
||||||
|
obj->Open();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,310 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "common/bit_field.h"
|
||||||
|
#include "common/bit_util.h"
|
||||||
|
#include "common/common_types.h"
|
||||||
|
#include "core/hle/kernel/k_auto_object.h"
|
||||||
|
#include "core/hle/kernel/k_spin_lock.h"
|
||||||
|
#include "core/hle/kernel/k_thread.h"
|
||||||
|
#include "core/hle/kernel/kernel.h"
|
||||||
|
#include "core/hle/kernel/svc_common.h"
|
||||||
|
#include "core/hle/kernel/svc_results.h"
|
||||||
|
#include "core/hle/result.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KernelCore;
|
||||||
|
|
||||||
|
class KHandleTable {
|
||||||
|
YUZU_NON_COPYABLE(KHandleTable);
|
||||||
|
YUZU_NON_MOVEABLE(KHandleTable);
|
||||||
|
|
||||||
|
public:
|
||||||
|
static constexpr size_t MaxTableSize = 1024;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit KHandleTable(KernelCore& kernel_);
|
||||||
|
~KHandleTable();
|
||||||
|
|
||||||
|
ResultCode Initialize(s32 size) {
|
||||||
|
R_UNLESS(size <= static_cast<s32>(MaxTableSize), ResultOutOfMemory);
|
||||||
|
|
||||||
|
// Initialize all fields.
|
||||||
|
m_max_count = 0;
|
||||||
|
m_table_size = static_cast<u16>((size <= 0) ? MaxTableSize : size);
|
||||||
|
m_next_linear_id = MinLinearId;
|
||||||
|
m_count = 0;
|
||||||
|
m_free_head_index = -1;
|
||||||
|
|
||||||
|
// Free all entries.
|
||||||
|
for (s32 i = 0; i < static_cast<s32>(m_table_size); ++i) {
|
||||||
|
m_objects[i] = nullptr;
|
||||||
|
m_entry_infos[i].next_free_index = i - 1;
|
||||||
|
m_free_head_index = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t GetTableSize() const {
|
||||||
|
return m_table_size;
|
||||||
|
}
|
||||||
|
size_t GetCount() const {
|
||||||
|
return m_count;
|
||||||
|
}
|
||||||
|
size_t GetMaxCount() const {
|
||||||
|
return m_max_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResultCode Finalize();
|
||||||
|
bool Remove(Handle handle);
|
||||||
|
|
||||||
|
template <typename T = KAutoObject>
|
||||||
|
KScopedAutoObject<T> GetObjectWithoutPseudoHandle(Handle handle) const {
|
||||||
|
// Lock and look up in table.
|
||||||
|
KScopedSpinLock lk(m_lock);
|
||||||
|
|
||||||
|
if constexpr (std::is_same_v<T, KAutoObject>) {
|
||||||
|
return this->GetObjectImpl(handle);
|
||||||
|
} else {
|
||||||
|
if (auto* obj = this->GetObjectImpl(handle); obj != nullptr) {
|
||||||
|
return obj->DynamicCast<T*>();
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T = KAutoObject>
|
||||||
|
KScopedAutoObject<T> GetObject(Handle handle) const {
|
||||||
|
// Handle pseudo-handles.
|
||||||
|
if constexpr (std::derived_from<KProcess, T>) {
|
||||||
|
if (handle == Svc::PseudoHandle::CurrentProcess) {
|
||||||
|
auto* const cur_process = kernel.CurrentProcess();
|
||||||
|
ASSERT(cur_process != nullptr);
|
||||||
|
return cur_process;
|
||||||
|
}
|
||||||
|
} else if constexpr (std::derived_from<KThread, T>) {
|
||||||
|
if (handle == Svc::PseudoHandle::CurrentThread) {
|
||||||
|
auto* const cur_thread = GetCurrentThreadPointer(kernel);
|
||||||
|
ASSERT(cur_thread != nullptr);
|
||||||
|
return cur_thread;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this->template GetObjectWithoutPseudoHandle<T>(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
ResultCode Reserve(Handle* out_handle);
|
||||||
|
void Unreserve(Handle handle);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
ResultCode Add(Handle* out_handle, T* obj) {
|
||||||
|
static_assert(std::is_base_of_v<KAutoObject, T>);
|
||||||
|
return this->Add(out_handle, obj, obj->GetTypeObj().GetClassToken());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void Register(Handle handle, T* obj) {
|
||||||
|
static_assert(std::is_base_of_v<KAutoObject, T>);
|
||||||
|
return this->Register(handle, obj, obj->GetTypeObj().GetClassToken());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool GetMultipleObjects(T** out, const Handle* handles, size_t num_handles) const {
|
||||||
|
// Try to convert and open all the handles.
|
||||||
|
size_t num_opened;
|
||||||
|
{
|
||||||
|
// Lock the table.
|
||||||
|
KScopedSpinLock lk(m_lock);
|
||||||
|
for (num_opened = 0; num_opened < num_handles; num_opened++) {
|
||||||
|
// Get the current handle.
|
||||||
|
const auto cur_handle = handles[num_opened];
|
||||||
|
|
||||||
|
// Get the object for the current handle.
|
||||||
|
KAutoObject* cur_object = this->GetObjectImpl(cur_handle);
|
||||||
|
if (cur_object == nullptr) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cast the current object to the desired type.
|
||||||
|
T* cur_t = cur_object->DynamicCast<T*>();
|
||||||
|
if (cur_t == nullptr) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open a reference to the current object.
|
||||||
|
cur_t->Open();
|
||||||
|
out[num_opened] = cur_t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we converted every object, succeed.
|
||||||
|
if (num_opened == num_handles) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we didn't convert entry object, close the ones we opened.
|
||||||
|
for (size_t i = 0; i < num_opened; i++) {
|
||||||
|
out[i]->Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
ResultCode Add(Handle* out_handle, KAutoObject* obj, u16 type);
|
||||||
|
void Register(Handle handle, KAutoObject* obj, u16 type);
|
||||||
|
|
||||||
|
s32 AllocateEntry() {
|
||||||
|
ASSERT(m_count < m_table_size);
|
||||||
|
|
||||||
|
const auto index = m_free_head_index;
|
||||||
|
|
||||||
|
m_free_head_index = m_entry_infos[index].GetNextFreeIndex();
|
||||||
|
|
||||||
|
m_max_count = std::max(m_max_count, ++m_count);
|
||||||
|
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FreeEntry(s32 index) {
|
||||||
|
ASSERT(m_count > 0);
|
||||||
|
|
||||||
|
m_objects[index] = nullptr;
|
||||||
|
m_entry_infos[index].next_free_index = m_free_head_index;
|
||||||
|
|
||||||
|
m_free_head_index = index;
|
||||||
|
|
||||||
|
--m_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
u16 AllocateLinearId() {
|
||||||
|
const u16 id = m_next_linear_id++;
|
||||||
|
if (m_next_linear_id > MaxLinearId) {
|
||||||
|
m_next_linear_id = MinLinearId;
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsValidHandle(Handle handle) const {
|
||||||
|
// Unpack the handle.
|
||||||
|
const auto handle_pack = HandlePack(handle);
|
||||||
|
const auto raw_value = handle_pack.raw;
|
||||||
|
const auto index = handle_pack.index;
|
||||||
|
const auto linear_id = handle_pack.linear_id;
|
||||||
|
const auto reserved = handle_pack.reserved;
|
||||||
|
ASSERT(reserved == 0);
|
||||||
|
|
||||||
|
// Validate our indexing information.
|
||||||
|
if (raw_value == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (linear_id == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (index >= m_table_size) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that there's an object, and our serial id is correct.
|
||||||
|
if (m_objects[index] == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (m_entry_infos[index].GetLinearId() != linear_id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
KAutoObject* GetObjectImpl(Handle handle) const {
|
||||||
|
// Handles must not have reserved bits set.
|
||||||
|
const auto handle_pack = HandlePack(handle);
|
||||||
|
if (handle_pack.reserved != 0) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this->IsValidHandle(handle)) {
|
||||||
|
return m_objects[handle_pack.index];
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KAutoObject* GetObjectByIndexImpl(Handle* out_handle, size_t index) const {
|
||||||
|
|
||||||
|
// Index must be in bounds.
|
||||||
|
if (index >= m_table_size) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure entry has an object.
|
||||||
|
if (KAutoObject* obj = m_objects[index]; obj != nullptr) {
|
||||||
|
*out_handle = EncodeHandle(static_cast<u16>(index), m_entry_infos[index].GetLinearId());
|
||||||
|
return obj;
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
union HandlePack {
|
||||||
|
HandlePack() = default;
|
||||||
|
HandlePack(Handle handle) : raw{static_cast<u32>(handle)} {}
|
||||||
|
|
||||||
|
u32 raw;
|
||||||
|
BitField<0, 15, u32> index;
|
||||||
|
BitField<15, 15, u32> linear_id;
|
||||||
|
BitField<30, 2, u32> reserved;
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr u16 MinLinearId = 1;
|
||||||
|
static constexpr u16 MaxLinearId = 0x7FFF;
|
||||||
|
|
||||||
|
static constexpr Handle EncodeHandle(u16 index, u16 linear_id) {
|
||||||
|
HandlePack handle{};
|
||||||
|
handle.index.Assign(index);
|
||||||
|
handle.linear_id.Assign(linear_id);
|
||||||
|
handle.reserved.Assign(0);
|
||||||
|
return handle.raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
union EntryInfo {
|
||||||
|
struct {
|
||||||
|
u16 linear_id;
|
||||||
|
u16 type;
|
||||||
|
} info;
|
||||||
|
s32 next_free_index;
|
||||||
|
|
||||||
|
constexpr u16 GetLinearId() const {
|
||||||
|
return info.linear_id;
|
||||||
|
}
|
||||||
|
constexpr u16 GetType() const {
|
||||||
|
return info.type;
|
||||||
|
}
|
||||||
|
constexpr s32 GetNextFreeIndex() const {
|
||||||
|
return next_free_index;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::array<EntryInfo, MaxTableSize> m_entry_infos{};
|
||||||
|
std::array<KAutoObject*, MaxTableSize> m_objects{};
|
||||||
|
s32 m_free_head_index{-1};
|
||||||
|
u16 m_table_size{};
|
||||||
|
u16 m_max_count{};
|
||||||
|
u16 m_next_linear_id{MinLinearId};
|
||||||
|
u16 m_count{};
|
||||||
|
mutable KSpinLock m_lock;
|
||||||
|
KernelCore& kernel;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,238 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <boost/intrusive/list.hpp>
|
||||||
|
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "core/hle/kernel/slab_helpers.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KernelCore;
|
||||||
|
|
||||||
|
class KLinkedListNode : public boost::intrusive::list_base_hook<>,
|
||||||
|
public KSlabAllocated<KLinkedListNode> {
|
||||||
|
|
||||||
|
public:
|
||||||
|
KLinkedListNode() = default;
|
||||||
|
|
||||||
|
void Initialize(void* it) {
|
||||||
|
m_item = it;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* GetItem() const {
|
||||||
|
return m_item;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void* m_item = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class KLinkedList : private boost::intrusive::list<KLinkedListNode> {
|
||||||
|
private:
|
||||||
|
using BaseList = boost::intrusive::list<KLinkedListNode>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
template <bool Const>
|
||||||
|
class Iterator;
|
||||||
|
|
||||||
|
using value_type = T;
|
||||||
|
using size_type = size_t;
|
||||||
|
using difference_type = ptrdiff_t;
|
||||||
|
using pointer = value_type*;
|
||||||
|
using const_pointer = const value_type*;
|
||||||
|
using reference = value_type&;
|
||||||
|
using const_reference = const value_type&;
|
||||||
|
using iterator = Iterator<false>;
|
||||||
|
using const_iterator = Iterator<true>;
|
||||||
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
||||||
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||||
|
|
||||||
|
template <bool Const>
|
||||||
|
class Iterator {
|
||||||
|
private:
|
||||||
|
using BaseIterator = BaseList::iterator;
|
||||||
|
friend class KLinkedList;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using iterator_category = std::bidirectional_iterator_tag;
|
||||||
|
using value_type = typename KLinkedList::value_type;
|
||||||
|
using difference_type = typename KLinkedList::difference_type;
|
||||||
|
using pointer = std::conditional_t<Const, KLinkedList::const_pointer, KLinkedList::pointer>;
|
||||||
|
using reference =
|
||||||
|
std::conditional_t<Const, KLinkedList::const_reference, KLinkedList::reference>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit Iterator(BaseIterator it) : m_base_it(it) {}
|
||||||
|
|
||||||
|
pointer GetItem() const {
|
||||||
|
return static_cast<pointer>(m_base_it->GetItem());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const Iterator& rhs) const {
|
||||||
|
return m_base_it == rhs.m_base_it;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const Iterator& rhs) const {
|
||||||
|
return !(*this == rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
pointer operator->() const {
|
||||||
|
return this->GetItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
reference operator*() const {
|
||||||
|
return *this->GetItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator& operator++() {
|
||||||
|
++m_base_it;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator& operator--() {
|
||||||
|
--m_base_it;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator operator++(int) {
|
||||||
|
const Iterator it{*this};
|
||||||
|
++(*this);
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator operator--(int) {
|
||||||
|
const Iterator it{*this};
|
||||||
|
--(*this);
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator Iterator<true>() const {
|
||||||
|
return Iterator<true>(m_base_it);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
BaseIterator m_base_it;
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
constexpr KLinkedList(KernelCore& kernel_) : BaseList(), kernel{kernel_} {}
|
||||||
|
|
||||||
|
~KLinkedList() {
|
||||||
|
// Erase all elements.
|
||||||
|
for (auto it = this->begin(); it != this->end(); it = this->erase(kernel, it)) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure we succeeded.
|
||||||
|
ASSERT(this->empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterator accessors.
|
||||||
|
iterator begin() {
|
||||||
|
return iterator(BaseList::begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
const_iterator begin() const {
|
||||||
|
return const_iterator(BaseList::begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator end() {
|
||||||
|
return iterator(BaseList::end());
|
||||||
|
}
|
||||||
|
|
||||||
|
const_iterator end() const {
|
||||||
|
return const_iterator(BaseList::end());
|
||||||
|
}
|
||||||
|
|
||||||
|
const_iterator cbegin() const {
|
||||||
|
return this->begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
const_iterator cend() const {
|
||||||
|
return this->end();
|
||||||
|
}
|
||||||
|
|
||||||
|
reverse_iterator rbegin() {
|
||||||
|
return reverse_iterator(this->end());
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reverse_iterator rbegin() const {
|
||||||
|
return const_reverse_iterator(this->end());
|
||||||
|
}
|
||||||
|
|
||||||
|
reverse_iterator rend() {
|
||||||
|
return reverse_iterator(this->begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reverse_iterator rend() const {
|
||||||
|
return const_reverse_iterator(this->begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reverse_iterator crbegin() const {
|
||||||
|
return this->rbegin();
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reverse_iterator crend() const {
|
||||||
|
return this->rend();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Content management.
|
||||||
|
using BaseList::empty;
|
||||||
|
using BaseList::size;
|
||||||
|
|
||||||
|
reference back() {
|
||||||
|
return *(--this->end());
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reference back() const {
|
||||||
|
return *(--this->end());
|
||||||
|
}
|
||||||
|
|
||||||
|
reference front() {
|
||||||
|
return *this->begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reference front() const {
|
||||||
|
return *this->begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator insert(const_iterator pos, reference ref) {
|
||||||
|
KLinkedListNode* node = KLinkedListNode::Allocate(kernel);
|
||||||
|
ASSERT(node != nullptr);
|
||||||
|
node->Initialize(std::addressof(ref));
|
||||||
|
return iterator(BaseList::insert(pos.m_base_it, *node));
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_back(reference ref) {
|
||||||
|
this->insert(this->end(), ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_front(reference ref) {
|
||||||
|
this->insert(this->begin(), ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pop_back() {
|
||||||
|
this->erase(--this->end());
|
||||||
|
}
|
||||||
|
|
||||||
|
void pop_front() {
|
||||||
|
this->erase(this->begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator erase(KernelCore& kernel, const iterator pos) {
|
||||||
|
KLinkedListNode* freed_node = std::addressof(*pos.m_base_it);
|
||||||
|
iterator ret = iterator(BaseList::erase(pos.m_base_it));
|
||||||
|
KLinkedListNode::Free(kernel, freed_node);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
KernelCore& kernel;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,68 @@
|
|||||||
|
// Copyright 2021 yuzu Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include "core/hle/kernel/hle_ipc.h"
|
||||||
|
#include "core/hle/kernel/k_port.h"
|
||||||
|
#include "core/hle/kernel/k_scheduler.h"
|
||||||
|
#include "core/hle/kernel/svc_results.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
KPort::KPort(KernelCore& kernel)
|
||||||
|
: KAutoObjectWithSlabHeapAndContainer{kernel}, server{kernel}, client{kernel} {}
|
||||||
|
|
||||||
|
KPort::~KPort() = default;
|
||||||
|
|
||||||
|
void KPort::Initialize(s32 max_sessions_, bool is_light_, const std::string& name_) {
|
||||||
|
// Open a new reference count to the initialized port.
|
||||||
|
Open();
|
||||||
|
|
||||||
|
// Create and initialize our server/client pair.
|
||||||
|
KAutoObject::Create(std::addressof(server));
|
||||||
|
KAutoObject::Create(std::addressof(client));
|
||||||
|
server.Initialize(this, name_ + ":Server");
|
||||||
|
client.Initialize(this, max_sessions_, name_ + ":Client");
|
||||||
|
|
||||||
|
// Set our member variables.
|
||||||
|
is_light = is_light_;
|
||||||
|
name = name_;
|
||||||
|
state = State::Normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
void KPort::OnClientClosed() {
|
||||||
|
KScopedSchedulerLock sl{kernel};
|
||||||
|
|
||||||
|
if (state == State::Normal) {
|
||||||
|
state = State::ClientClosed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void KPort::OnServerClosed() {
|
||||||
|
KScopedSchedulerLock sl{kernel};
|
||||||
|
|
||||||
|
if (state == State::Normal) {
|
||||||
|
state = State::ServerClosed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KPort::IsServerClosed() const {
|
||||||
|
KScopedSchedulerLock sl{kernel};
|
||||||
|
return state == State::ServerClosed;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResultCode KPort::EnqueueSession(KServerSession* session) {
|
||||||
|
KScopedSchedulerLock sl{kernel};
|
||||||
|
|
||||||
|
R_UNLESS(state == State::Normal, ResultPortClosed);
|
||||||
|
|
||||||
|
if (server.HasHLEHandler()) {
|
||||||
|
server.GetHLEHandler()->ClientConnected(session);
|
||||||
|
} else {
|
||||||
|
server.EnqueueSession(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
return RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,69 @@
|
|||||||
|
// Copyright 2021 yuzu Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "common/common_types.h"
|
||||||
|
#include "core/hle/kernel/k_client_port.h"
|
||||||
|
#include "core/hle/kernel/k_server_port.h"
|
||||||
|
#include "core/hle/kernel/slab_helpers.h"
|
||||||
|
#include "core/hle/result.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KServerSession;
|
||||||
|
|
||||||
|
class KPort final : public KAutoObjectWithSlabHeapAndContainer<KPort, KAutoObjectWithList> {
|
||||||
|
KERNEL_AUTOOBJECT_TRAITS(KPort, KAutoObject);
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit KPort(KernelCore& kernel);
|
||||||
|
virtual ~KPort();
|
||||||
|
|
||||||
|
static void PostDestroy([[maybe_unused]] uintptr_t arg) {}
|
||||||
|
|
||||||
|
void Initialize(s32 max_sessions_, bool is_light_, const std::string& name_);
|
||||||
|
void OnClientClosed();
|
||||||
|
void OnServerClosed();
|
||||||
|
|
||||||
|
bool IsLight() const {
|
||||||
|
return is_light;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsServerClosed() const;
|
||||||
|
|
||||||
|
ResultCode EnqueueSession(KServerSession* session);
|
||||||
|
|
||||||
|
KClientPort& GetClientPort() {
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
KServerPort& GetServerPort() {
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
const KClientPort& GetClientPort() const {
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
const KServerPort& GetServerPort() const {
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
enum class State : u8 {
|
||||||
|
Invalid = 0,
|
||||||
|
Normal = 1,
|
||||||
|
ClientClosed = 2,
|
||||||
|
ServerClosed = 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
KServerPort server;
|
||||||
|
KClientPort client;
|
||||||
|
State state{State::Invalid};
|
||||||
|
bool is_light{};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,104 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "core/hle/kernel/k_client_port.h"
|
||||||
|
#include "core/hle/kernel/k_port.h"
|
||||||
|
#include "core/hle/kernel/k_scheduler.h"
|
||||||
|
#include "core/hle/kernel/k_server_port.h"
|
||||||
|
#include "core/hle/kernel/k_server_session.h"
|
||||||
|
#include "core/hle/kernel/k_thread.h"
|
||||||
|
#include "core/hle/kernel/svc_results.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
KServerPort::KServerPort(KernelCore& kernel) : KSynchronizationObject{kernel} {}
|
||||||
|
KServerPort::~KServerPort() = default;
|
||||||
|
|
||||||
|
void KServerPort::Initialize(KPort* parent_, std::string&& name_) {
|
||||||
|
// Set member variables.
|
||||||
|
parent = parent_;
|
||||||
|
name = std::move(name_);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KServerPort::IsLight() const {
|
||||||
|
return this->GetParent()->IsLight();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KServerPort::CleanupSessions() {
|
||||||
|
// Ensure our preconditions are met.
|
||||||
|
if (this->IsLight()) {
|
||||||
|
UNIMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup the session list.
|
||||||
|
while (true) {
|
||||||
|
// Get the last session in the list
|
||||||
|
KServerSession* session = nullptr;
|
||||||
|
{
|
||||||
|
KScopedSchedulerLock sl{kernel};
|
||||||
|
if (!session_list.empty()) {
|
||||||
|
session = std::addressof(session_list.front());
|
||||||
|
session_list.pop_front();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the session.
|
||||||
|
if (session != nullptr) {
|
||||||
|
session->Close();
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void KServerPort::Destroy() {
|
||||||
|
// Note with our parent that we're closed.
|
||||||
|
parent->OnServerClosed();
|
||||||
|
|
||||||
|
// Perform necessary cleanup of our session lists.
|
||||||
|
this->CleanupSessions();
|
||||||
|
|
||||||
|
// Close our reference to our parent.
|
||||||
|
parent->Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KServerPort::IsSignaled() const {
|
||||||
|
if (this->IsLight()) {
|
||||||
|
UNIMPLEMENTED();
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return !session_list.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void KServerPort::EnqueueSession(KServerSession* session) {
|
||||||
|
ASSERT(!this->IsLight());
|
||||||
|
|
||||||
|
KScopedSchedulerLock sl{kernel};
|
||||||
|
|
||||||
|
// Add the session to our queue.
|
||||||
|
session_list.push_back(*session);
|
||||||
|
if (session_list.size() == 1) {
|
||||||
|
this->NotifyAvailable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KServerSession* KServerPort::AcceptSession() {
|
||||||
|
ASSERT(!this->IsLight());
|
||||||
|
|
||||||
|
KScopedSchedulerLock sl{kernel};
|
||||||
|
|
||||||
|
// Return the first session in the list.
|
||||||
|
if (session_list.empty()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
KServerSession* session = std::addressof(session_list.front());
|
||||||
|
session_list.pop_front();
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,80 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <boost/intrusive/list.hpp>
|
||||||
|
|
||||||
|
#include "common/common_types.h"
|
||||||
|
#include "core/hle/kernel/k_server_session.h"
|
||||||
|
#include "core/hle/kernel/k_synchronization_object.h"
|
||||||
|
#include "core/hle/result.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KernelCore;
|
||||||
|
class KPort;
|
||||||
|
class SessionRequestHandler;
|
||||||
|
|
||||||
|
class KServerPort final : public KSynchronizationObject {
|
||||||
|
KERNEL_AUTOOBJECT_TRAITS(KServerPort, KSynchronizationObject);
|
||||||
|
|
||||||
|
private:
|
||||||
|
using SessionList = boost::intrusive::list<KServerSession>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit KServerPort(KernelCore& kernel);
|
||||||
|
virtual ~KServerPort() override;
|
||||||
|
|
||||||
|
using HLEHandler = std::shared_ptr<SessionRequestHandler>;
|
||||||
|
|
||||||
|
void Initialize(KPort* parent_, std::string&& name_);
|
||||||
|
|
||||||
|
/// Whether or not this server port has an HLE handler available.
|
||||||
|
bool HasHLEHandler() const {
|
||||||
|
return hle_handler != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the HLE handler for this port.
|
||||||
|
HLEHandler GetHLEHandler() const {
|
||||||
|
return hle_handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the HLE handler template for the port. ServerSessions crated by connecting to this port
|
||||||
|
* will inherit a reference to this handler.
|
||||||
|
*/
|
||||||
|
void SetHleHandler(HLEHandler hle_handler_) {
|
||||||
|
hle_handler = std::move(hle_handler_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnqueueSession(KServerSession* pending_session);
|
||||||
|
|
||||||
|
KServerSession* AcceptSession();
|
||||||
|
|
||||||
|
const KPort* GetParent() const {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsLight() const;
|
||||||
|
|
||||||
|
// Overridden virtual functions.
|
||||||
|
virtual void Destroy() override;
|
||||||
|
virtual bool IsSignaled() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void CleanupSessions();
|
||||||
|
|
||||||
|
private:
|
||||||
|
SessionList session_list;
|
||||||
|
HLEHandler hle_handler;
|
||||||
|
KPort* parent{};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,85 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "core/hle/kernel/k_client_port.h"
|
||||||
|
#include "core/hle/kernel/k_client_session.h"
|
||||||
|
#include "core/hle/kernel/k_scoped_resource_reservation.h"
|
||||||
|
#include "core/hle/kernel/k_server_session.h"
|
||||||
|
#include "core/hle/kernel/k_session.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
KSession::KSession(KernelCore& kernel)
|
||||||
|
: KAutoObjectWithSlabHeapAndContainer{kernel}, server{kernel}, client{kernel} {}
|
||||||
|
KSession::~KSession() = default;
|
||||||
|
|
||||||
|
void KSession::Initialize(KClientPort* port_, const std::string& name_) {
|
||||||
|
// Increment reference count.
|
||||||
|
// Because reference count is one on creation, this will result
|
||||||
|
// in a reference count of two. Thus, when both server and client are closed
|
||||||
|
// this object will be destroyed.
|
||||||
|
Open();
|
||||||
|
|
||||||
|
// Create our sub sessions.
|
||||||
|
KAutoObject::Create(std::addressof(server));
|
||||||
|
KAutoObject::Create(std::addressof(client));
|
||||||
|
|
||||||
|
// Initialize our sub sessions.
|
||||||
|
server.Initialize(this, name_ + ":Server");
|
||||||
|
client.Initialize(this, name_ + ":Client");
|
||||||
|
|
||||||
|
// Set state and name.
|
||||||
|
SetState(State::Normal);
|
||||||
|
name = name_;
|
||||||
|
|
||||||
|
// Set our owner process.
|
||||||
|
process = kernel.CurrentProcess();
|
||||||
|
process->Open();
|
||||||
|
|
||||||
|
// Set our port.
|
||||||
|
port = port_;
|
||||||
|
if (port != nullptr) {
|
||||||
|
port->Open();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark initialized.
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void KSession::Finalize() {
|
||||||
|
if (port == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
port->OnSessionFinalized();
|
||||||
|
port->Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KSession::OnServerClosed() {
|
||||||
|
if (GetState() != State::Normal) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SetState(State::ServerClosed);
|
||||||
|
client.OnServerClosed();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KSession::OnClientClosed() {
|
||||||
|
if (GetState() != State::Normal) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SetState(State::ClientClosed);
|
||||||
|
server.OnClientClosed();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KSession::PostDestroy(uintptr_t arg) {
|
||||||
|
// Release the session count resource the owner process holds.
|
||||||
|
KProcess* owner = reinterpret_cast<KProcess*>(arg);
|
||||||
|
owner->GetResourceLimit()->Release(LimitableResource::Sessions, 1);
|
||||||
|
owner->Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,96 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "core/hle/kernel/k_client_session.h"
|
||||||
|
#include "core/hle/kernel/k_server_session.h"
|
||||||
|
#include "core/hle/kernel/slab_helpers.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KSession final : public KAutoObjectWithSlabHeapAndContainer<KSession, KAutoObjectWithList> {
|
||||||
|
KERNEL_AUTOOBJECT_TRAITS(KSession, KAutoObject);
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit KSession(KernelCore& kernel);
|
||||||
|
virtual ~KSession() override;
|
||||||
|
|
||||||
|
void Initialize(KClientPort* port_, const std::string& name_);
|
||||||
|
|
||||||
|
virtual void Finalize() override;
|
||||||
|
|
||||||
|
virtual bool IsInitialized() const override {
|
||||||
|
return initialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual uintptr_t GetPostDestroyArgument() const override {
|
||||||
|
return reinterpret_cast<uintptr_t>(process);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PostDestroy(uintptr_t arg);
|
||||||
|
|
||||||
|
void OnServerClosed();
|
||||||
|
|
||||||
|
void OnClientClosed();
|
||||||
|
|
||||||
|
bool IsServerClosed() const {
|
||||||
|
return this->GetState() != State::Normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsClientClosed() const {
|
||||||
|
return this->GetState() != State::Normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
KClientSession& GetClientSession() {
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
KServerSession& GetServerSession() {
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
const KClientSession& GetClientSession() const {
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
const KServerSession& GetServerSession() const {
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
const KClientPort* GetParent() const {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
enum class State : u8 {
|
||||||
|
Invalid = 0,
|
||||||
|
Normal = 1,
|
||||||
|
ClientClosed = 2,
|
||||||
|
ServerClosed = 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
void SetState(State state) {
|
||||||
|
atomic_state = static_cast<u8>(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
State GetState() const {
|
||||||
|
return static_cast<State>(atomic_state.load(std::memory_order_relaxed));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
KServerSession server;
|
||||||
|
KClientSession client;
|
||||||
|
std::atomic<std::underlying_type_t<State>> atomic_state{
|
||||||
|
static_cast<std::underlying_type_t<State>>(State::Invalid)};
|
||||||
|
KClientPort* port{};
|
||||||
|
KProcess* process{};
|
||||||
|
bool initialized{};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,45 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include "core/hle/kernel/k_process.h"
|
||||||
|
#include "core/hle/kernel/k_resource_limit.h"
|
||||||
|
#include "core/hle/kernel/k_transfer_memory.h"
|
||||||
|
#include "core/hle/kernel/kernel.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
KTransferMemory::KTransferMemory(KernelCore& kernel)
|
||||||
|
: KAutoObjectWithSlabHeapAndContainer{kernel} {}
|
||||||
|
|
||||||
|
KTransferMemory::~KTransferMemory() = default;
|
||||||
|
|
||||||
|
ResultCode KTransferMemory::Initialize(VAddr address_, std::size_t size_,
|
||||||
|
Svc::MemoryPermission owner_perm_) {
|
||||||
|
// Set members.
|
||||||
|
owner = kernel.CurrentProcess();
|
||||||
|
|
||||||
|
// TODO(bunnei): Lock for transfer memory
|
||||||
|
|
||||||
|
// Set remaining tracking members.
|
||||||
|
owner->Open();
|
||||||
|
owner_perm = owner_perm_;
|
||||||
|
address = address_;
|
||||||
|
size = size_;
|
||||||
|
is_initialized = true;
|
||||||
|
|
||||||
|
return RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void KTransferMemory::Finalize() {
|
||||||
|
// Perform inherited finalization.
|
||||||
|
KAutoObjectWithSlabHeapAndContainer<KTransferMemory, KAutoObjectWithList>::Finalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KTransferMemory::PostDestroy(uintptr_t arg) {
|
||||||
|
KProcess* owner = reinterpret_cast<KProcess*>(arg);
|
||||||
|
owner->GetResourceLimit()->Release(LimitableResource::TransferMemory, 1);
|
||||||
|
owner->Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -0,0 +1,66 @@
|
|||||||
|
// Copyright 2021 yuzu emulator team
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "core/hle/kernel/slab_helpers.h"
|
||||||
|
#include "core/hle/kernel/svc_types.h"
|
||||||
|
#include "core/hle/result.h"
|
||||||
|
|
||||||
|
union ResultCode;
|
||||||
|
|
||||||
|
namespace Core::Memory {
|
||||||
|
class Memory;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class KernelCore;
|
||||||
|
class KProcess;
|
||||||
|
|
||||||
|
class KTransferMemory final
|
||||||
|
: public KAutoObjectWithSlabHeapAndContainer<KTransferMemory, KAutoObjectWithList> {
|
||||||
|
KERNEL_AUTOOBJECT_TRAITS(KTransferMemory, KAutoObject);
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit KTransferMemory(KernelCore& kernel);
|
||||||
|
virtual ~KTransferMemory() override;
|
||||||
|
|
||||||
|
ResultCode Initialize(VAddr address_, std::size_t size_, Svc::MemoryPermission owner_perm_);
|
||||||
|
|
||||||
|
virtual void Finalize() override;
|
||||||
|
|
||||||
|
virtual bool IsInitialized() const override {
|
||||||
|
return is_initialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual uintptr_t GetPostDestroyArgument() const override {
|
||||||
|
return reinterpret_cast<uintptr_t>(owner);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PostDestroy(uintptr_t arg);
|
||||||
|
|
||||||
|
KProcess* GetOwner() const {
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
VAddr GetSourceAddress() const {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t GetSize() const {
|
||||||
|
return is_initialized ? size * PageSize : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
KProcess* owner{};
|
||||||
|
VAddr address{};
|
||||||
|
Svc::MemoryPermission owner_perm{};
|
||||||
|
size_t size{};
|
||||||
|
bool is_initialized{};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
@ -1,42 +0,0 @@
|
|||||||
// Copyright 2018 Citra Emulator Project
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#include "common/assert.h"
|
|
||||||
#include "core/hle/kernel/kernel.h"
|
|
||||||
#include "core/hle/kernel/object.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
Object::Object(KernelCore& kernel_)
|
|
||||||
: kernel{kernel_}, object_id{kernel_.CreateNewObjectID()}, name{"[UNKNOWN KERNEL OBJECT]"} {}
|
|
||||||
Object::Object(KernelCore& kernel_, std::string&& name_)
|
|
||||||
: kernel{kernel_}, object_id{kernel_.CreateNewObjectID()}, name{std::move(name_)} {}
|
|
||||||
Object::~Object() = default;
|
|
||||||
|
|
||||||
bool Object::IsWaitable() const {
|
|
||||||
switch (GetHandleType()) {
|
|
||||||
case HandleType::ReadableEvent:
|
|
||||||
case HandleType::Thread:
|
|
||||||
case HandleType::Process:
|
|
||||||
case HandleType::ServerPort:
|
|
||||||
case HandleType::ServerSession:
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case HandleType::Unknown:
|
|
||||||
case HandleType::Event:
|
|
||||||
case HandleType::WritableEvent:
|
|
||||||
case HandleType::SharedMemory:
|
|
||||||
case HandleType::TransferMemory:
|
|
||||||
case HandleType::ResourceLimit:
|
|
||||||
case HandleType::ClientPort:
|
|
||||||
case HandleType::ClientSession:
|
|
||||||
case HandleType::Session:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
UNREACHABLE();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -1,96 +0,0 @@
|
|||||||
// Copyright 2018 Citra Emulator Project
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "common/common_types.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
class KernelCore;
|
|
||||||
|
|
||||||
using Handle = u32;
|
|
||||||
|
|
||||||
enum class HandleType : u32 {
|
|
||||||
Unknown,
|
|
||||||
Event,
|
|
||||||
WritableEvent,
|
|
||||||
ReadableEvent,
|
|
||||||
SharedMemory,
|
|
||||||
TransferMemory,
|
|
||||||
Thread,
|
|
||||||
Process,
|
|
||||||
ResourceLimit,
|
|
||||||
ClientPort,
|
|
||||||
ServerPort,
|
|
||||||
ClientSession,
|
|
||||||
ServerSession,
|
|
||||||
Session,
|
|
||||||
};
|
|
||||||
|
|
||||||
class Object : NonCopyable, public std::enable_shared_from_this<Object> {
|
|
||||||
public:
|
|
||||||
explicit Object(KernelCore& kernel_);
|
|
||||||
explicit Object(KernelCore& kernel_, std::string&& name_);
|
|
||||||
virtual ~Object();
|
|
||||||
|
|
||||||
/// Returns a unique identifier for the object. For debugging purposes only.
|
|
||||||
u32 GetObjectId() const {
|
|
||||||
return object_id.load(std::memory_order_relaxed);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual std::string GetTypeName() const {
|
|
||||||
return "[BAD KERNEL OBJECT TYPE]";
|
|
||||||
}
|
|
||||||
virtual std::string GetName() const {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
virtual HandleType GetHandleType() const = 0;
|
|
||||||
|
|
||||||
void Close() {
|
|
||||||
// TODO(bunnei): This is a placeholder to decrement the reference count, which we will use
|
|
||||||
// when we implement KAutoObject instead of using shared_ptr.
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a thread can wait on the object
|
|
||||||
* @return True if a thread can wait on the object, otherwise false
|
|
||||||
*/
|
|
||||||
bool IsWaitable() const;
|
|
||||||
|
|
||||||
virtual void Finalize() = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
/// The kernel instance this object was created under.
|
|
||||||
KernelCore& kernel;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::atomic<u32> object_id{0};
|
|
||||||
std::string name;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
std::shared_ptr<T> SharedFrom(T* raw) {
|
|
||||||
if (raw == nullptr)
|
|
||||||
return nullptr;
|
|
||||||
return std::static_pointer_cast<T>(raw->shared_from_this());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attempts to downcast the given Object pointer to a pointer to T.
|
|
||||||
* @return Derived pointer to the object, or `nullptr` if `object` isn't of type T.
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
inline std::shared_ptr<T> DynamicObjectCast(std::shared_ptr<Object> object) {
|
|
||||||
if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) {
|
|
||||||
return std::static_pointer_cast<T>(object);
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -1,54 +0,0 @@
|
|||||||
// Copyright 2016 Citra Emulator Project
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#include <tuple>
|
|
||||||
#include "common/assert.h"
|
|
||||||
#include "core/hle/kernel/client_port.h"
|
|
||||||
#include "core/hle/kernel/k_thread.h"
|
|
||||||
#include "core/hle/kernel/object.h"
|
|
||||||
#include "core/hle/kernel/server_port.h"
|
|
||||||
#include "core/hle/kernel/server_session.h"
|
|
||||||
#include "core/hle/kernel/svc_results.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
ServerPort::ServerPort(KernelCore& kernel) : KSynchronizationObject{kernel} {}
|
|
||||||
ServerPort::~ServerPort() = default;
|
|
||||||
|
|
||||||
ResultVal<std::shared_ptr<ServerSession>> ServerPort::Accept() {
|
|
||||||
if (pending_sessions.empty()) {
|
|
||||||
return ResultNotFound;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto session = std::move(pending_sessions.back());
|
|
||||||
pending_sessions.pop_back();
|
|
||||||
return MakeResult(std::move(session));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ServerPort::AppendPendingSession(std::shared_ptr<ServerSession> pending_session) {
|
|
||||||
pending_sessions.push_back(std::move(pending_session));
|
|
||||||
if (pending_sessions.size() == 1) {
|
|
||||||
NotifyAvailable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ServerPort::IsSignaled() const {
|
|
||||||
return !pending_sessions.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
ServerPort::PortPair ServerPort::CreatePortPair(KernelCore& kernel, u32 max_sessions,
|
|
||||||
std::string name) {
|
|
||||||
std::shared_ptr<ServerPort> server_port = std::make_shared<ServerPort>(kernel);
|
|
||||||
std::shared_ptr<ClientPort> client_port = std::make_shared<ClientPort>(kernel);
|
|
||||||
|
|
||||||
server_port->name = name + "_Server";
|
|
||||||
client_port->name = name + "_Client";
|
|
||||||
client_port->server_port = server_port;
|
|
||||||
client_port->max_sessions = max_sessions;
|
|
||||||
client_port->active_sessions = 0;
|
|
||||||
|
|
||||||
return std::make_pair(std::move(server_port), std::move(client_port));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -1,98 +0,0 @@
|
|||||||
// Copyright 2016 Citra Emulator Project
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
|
||||||
#include "common/common_types.h"
|
|
||||||
#include "core/hle/kernel/k_synchronization_object.h"
|
|
||||||
#include "core/hle/kernel/object.h"
|
|
||||||
#include "core/hle/result.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
class ClientPort;
|
|
||||||
class KernelCore;
|
|
||||||
class ServerSession;
|
|
||||||
class SessionRequestHandler;
|
|
||||||
|
|
||||||
class ServerPort final : public KSynchronizationObject {
|
|
||||||
public:
|
|
||||||
explicit ServerPort(KernelCore& kernel);
|
|
||||||
~ServerPort() override;
|
|
||||||
|
|
||||||
using HLEHandler = std::shared_ptr<SessionRequestHandler>;
|
|
||||||
using PortPair = std::pair<std::shared_ptr<ServerPort>, std::shared_ptr<ClientPort>>;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a pair of ServerPort and an associated ClientPort.
|
|
||||||
*
|
|
||||||
* @param kernel The kernel instance to create the port pair under.
|
|
||||||
* @param max_sessions Maximum number of sessions to the port
|
|
||||||
* @param name Optional name of the ports
|
|
||||||
* @return The created port tuple
|
|
||||||
*/
|
|
||||||
static PortPair CreatePortPair(KernelCore& kernel, u32 max_sessions,
|
|
||||||
std::string name = "UnknownPort");
|
|
||||||
|
|
||||||
std::string GetTypeName() const override {
|
|
||||||
return "ServerPort";
|
|
||||||
}
|
|
||||||
std::string GetName() const override {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr HandleType HANDLE_TYPE = HandleType::ServerPort;
|
|
||||||
HandleType GetHandleType() const override {
|
|
||||||
return HANDLE_TYPE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Accepts a pending incoming connection on this port. If there are no pending sessions, will
|
|
||||||
* return ERR_NO_PENDING_SESSIONS.
|
|
||||||
*/
|
|
||||||
ResultVal<std::shared_ptr<ServerSession>> Accept();
|
|
||||||
|
|
||||||
/// Whether or not this server port has an HLE handler available.
|
|
||||||
bool HasHLEHandler() const {
|
|
||||||
return hle_handler != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the HLE handler for this port.
|
|
||||||
HLEHandler GetHLEHandler() const {
|
|
||||||
return hle_handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the HLE handler template for the port. ServerSessions crated by connecting to this port
|
|
||||||
* will inherit a reference to this handler.
|
|
||||||
*/
|
|
||||||
void SetHleHandler(HLEHandler hle_handler_) {
|
|
||||||
hle_handler = std::move(hle_handler_);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Appends a ServerSession to the collection of ServerSessions
|
|
||||||
/// waiting to be accepted by this port.
|
|
||||||
void AppendPendingSession(std::shared_ptr<ServerSession> pending_session);
|
|
||||||
|
|
||||||
bool IsSignaled() const override;
|
|
||||||
|
|
||||||
void Finalize() override {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
/// ServerSessions waiting to be accepted by the port
|
|
||||||
std::vector<std::shared_ptr<ServerSession>> pending_sessions;
|
|
||||||
|
|
||||||
/// This session's HLE request handler template (optional)
|
|
||||||
/// ServerSessions created from this port inherit a reference to this handler.
|
|
||||||
HLEHandler hle_handler;
|
|
||||||
|
|
||||||
/// Name of the port (optional)
|
|
||||||
std::string name;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -1,41 +0,0 @@
|
|||||||
// Copyright 2019 yuzu emulator team
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#include "common/assert.h"
|
|
||||||
#include "core/hle/kernel/client_session.h"
|
|
||||||
#include "core/hle/kernel/k_scoped_resource_reservation.h"
|
|
||||||
#include "core/hle/kernel/server_session.h"
|
|
||||||
#include "core/hle/kernel/session.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
Session::Session(KernelCore& kernel) : KSynchronizationObject{kernel} {}
|
|
||||||
Session::~Session() {
|
|
||||||
// Release reserved resource when the Session pair was created.
|
|
||||||
kernel.GetSystemResourceLimit()->Release(LimitableResource::Sessions, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
Session::SessionPair Session::Create(KernelCore& kernel, std::string name) {
|
|
||||||
// Reserve a new session from the resource limit.
|
|
||||||
KScopedResourceReservation session_reservation(kernel.GetSystemResourceLimit(),
|
|
||||||
LimitableResource::Sessions);
|
|
||||||
ASSERT(session_reservation.Succeeded());
|
|
||||||
auto session{std::make_shared<Session>(kernel)};
|
|
||||||
auto client_session{Kernel::ClientSession::Create(kernel, session, name + "_Client").Unwrap()};
|
|
||||||
auto server_session{Kernel::ServerSession::Create(kernel, session, name + "_Server").Unwrap()};
|
|
||||||
|
|
||||||
session->name = std::move(name);
|
|
||||||
session->client = client_session;
|
|
||||||
session->server = server_session;
|
|
||||||
|
|
||||||
session_reservation.Commit();
|
|
||||||
return std::make_pair(std::move(client_session), std::move(server_session));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Session::IsSignaled() const {
|
|
||||||
UNIMPLEMENTED();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -1,64 +0,0 @@
|
|||||||
// Copyright 2019 yuzu emulator team
|
|
||||||
// Licensed under GPLv2 or any later version
|
|
||||||
// Refer to the license.txt file included.
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "core/hle/kernel/k_synchronization_object.h"
|
|
||||||
|
|
||||||
namespace Kernel {
|
|
||||||
|
|
||||||
class ClientSession;
|
|
||||||
class ServerSession;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parent structure to link the client and server endpoints of a session with their associated
|
|
||||||
* client port.
|
|
||||||
*/
|
|
||||||
class Session final : public KSynchronizationObject {
|
|
||||||
public:
|
|
||||||
explicit Session(KernelCore& kernel);
|
|
||||||
~Session() override;
|
|
||||||
|
|
||||||
using SessionPair = std::pair<std::shared_ptr<ClientSession>, std::shared_ptr<ServerSession>>;
|
|
||||||
|
|
||||||
static SessionPair Create(KernelCore& kernel, std::string name = "Unknown");
|
|
||||||
|
|
||||||
std::string GetName() const override {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr HandleType HANDLE_TYPE = HandleType::Session;
|
|
||||||
HandleType GetHandleType() const override {
|
|
||||||
return HANDLE_TYPE;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsSignaled() const override;
|
|
||||||
|
|
||||||
void Finalize() override {}
|
|
||||||
|
|
||||||
std::shared_ptr<ClientSession> Client() {
|
|
||||||
if (auto result{client.lock()}) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<ServerSession> Server() {
|
|
||||||
if (auto result{server.lock()}) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::string name;
|
|
||||||
std::weak_ptr<ClientSession> client;
|
|
||||||
std::weak_ptr<ServerSession> server;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Kernel
|
|
@ -0,0 +1,148 @@
|
|||||||
|
// Copyright 2021 yuzu Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
#include "common/assert.h"
|
||||||
|
#include "common/common_funcs.h"
|
||||||
|
#include "common/common_types.h"
|
||||||
|
#include "common/intrusive_red_black_tree.h"
|
||||||
|
#include "core/hle/kernel/k_auto_object.h"
|
||||||
|
#include "core/hle/kernel/k_auto_object_container.h"
|
||||||
|
#include "core/hle/kernel/k_light_lock.h"
|
||||||
|
#include "core/hle/kernel/k_slab_heap.h"
|
||||||
|
#include "core/hle/kernel/kernel.h"
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
template <class Derived>
|
||||||
|
class KSlabAllocated {
|
||||||
|
public:
|
||||||
|
constexpr KSlabAllocated() = default;
|
||||||
|
|
||||||
|
size_t GetSlabIndex(KernelCore& kernel) const {
|
||||||
|
return kernel.SlabHeap<Derived>().GetIndex(static_cast<const Derived*>(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
static void InitializeSlabHeap(KernelCore& kernel, void* memory, size_t memory_size) {
|
||||||
|
kernel.SlabHeap<Derived>().Initialize(memory, memory_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Derived* Allocate(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().Allocate();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Free(KernelCore& kernel, Derived* obj) {
|
||||||
|
kernel.SlabHeap<Derived>().Free(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t GetObjectSize(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().GetObjectSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t GetSlabHeapSize(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().GetSlabHeapSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t GetPeakIndex(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().GetPeakIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
static uintptr_t GetSlabHeapAddress(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().GetSlabHeapAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t GetNumRemaining(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().GetNumRemaining();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Derived, typename Base>
|
||||||
|
class KAutoObjectWithSlabHeapAndContainer : public Base {
|
||||||
|
static_assert(std::is_base_of<KAutoObjectWithList, Base>::value);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static Derived* Allocate(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().AllocateWithKernel(kernel);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Free(KernelCore& kernel, Derived* obj) {
|
||||||
|
kernel.SlabHeap<Derived>().Free(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
KAutoObjectWithSlabHeapAndContainer(KernelCore& kernel_) : Base(kernel_), kernel(kernel_) {}
|
||||||
|
virtual ~KAutoObjectWithSlabHeapAndContainer() {}
|
||||||
|
|
||||||
|
virtual void Destroy() override {
|
||||||
|
const bool is_initialized = this->IsInitialized();
|
||||||
|
uintptr_t arg = 0;
|
||||||
|
if (is_initialized) {
|
||||||
|
kernel.ObjectListContainer().Unregister(this);
|
||||||
|
arg = this->GetPostDestroyArgument();
|
||||||
|
this->Finalize();
|
||||||
|
}
|
||||||
|
Free(kernel, static_cast<Derived*>(this));
|
||||||
|
if (is_initialized) {
|
||||||
|
Derived::PostDestroy(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool IsInitialized() const {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
virtual uintptr_t GetPostDestroyArgument() const {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t GetSlabIndex() const {
|
||||||
|
return SlabHeap<Derived>(kernel).GetObjectIndex(static_cast<const Derived*>(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
static void InitializeSlabHeap(KernelCore& kernel, void* memory, size_t memory_size) {
|
||||||
|
kernel.SlabHeap<Derived>().Initialize(memory, memory_size);
|
||||||
|
kernel.ObjectListContainer().Initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
static Derived* Create(KernelCore& kernel) {
|
||||||
|
Derived* obj = Allocate(kernel);
|
||||||
|
if (obj != nullptr) {
|
||||||
|
KAutoObject::Create(obj);
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Register(KernelCore& kernel, Derived* obj) {
|
||||||
|
return kernel.ObjectListContainer().Register(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t GetObjectSize(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().GetObjectSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t GetSlabHeapSize(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().GetSlabHeapSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t GetPeakIndex(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().GetPeakIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
static uintptr_t GetSlabHeapAddress(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().GetSlabHeapAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t GetNumRemaining(KernelCore& kernel) {
|
||||||
|
return kernel.SlabHeap<Derived>().GetNumRemaining();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
KernelCore& kernel;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Kernel
|
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue