2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-06-15 01:03:30 +02:00
|
|
|
|
|
|
|
#include <tuple>
|
2018-07-19 01:02:47 +02:00
|
|
|
#include <utility>
|
2016-06-15 01:03:30 +02:00
|
|
|
|
2018-07-31 14:06:09 +02:00
|
|
|
#include "common/assert.h"
|
2019-01-01 00:09:41 +01:00
|
|
|
#include "common/common_types.h"
|
2018-07-31 14:06:09 +02:00
|
|
|
#include "common/logging/log.h"
|
2022-10-12 00:16:56 +02:00
|
|
|
#include "common/scope_exit.h"
|
|
|
|
#include "core/core.h"
|
2019-11-26 00:28:48 +01:00
|
|
|
#include "core/core_timing.h"
|
2018-01-24 00:03:09 +01:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
2017-06-05 06:52:19 +02:00
|
|
|
#include "core/hle/kernel/hle_ipc.h"
|
2021-04-22 06:43:25 +02:00
|
|
|
#include "core/hle/kernel/k_client_port.h"
|
2021-04-24 11:40:31 +02:00
|
|
|
#include "core/hle/kernel/k_handle_table.h"
|
2021-04-24 07:04:28 +02:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2020-12-03 03:08:35 +01:00
|
|
|
#include "core/hle/kernel/k_scheduler.h"
|
2021-06-05 04:26:48 +02:00
|
|
|
#include "core/hle/kernel/k_server_port.h"
|
2021-04-14 02:48:37 +02:00
|
|
|
#include "core/hle/kernel/k_server_session.h"
|
|
|
|
#include "core/hle/kernel/k_session.h"
|
2020-12-31 08:01:08 +01:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2022-10-12 00:16:56 +02:00
|
|
|
#include "core/hle/kernel/k_thread_queue.h"
|
2018-08-31 18:21:34 +02:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2019-11-26 21:19:15 +01:00
|
|
|
#include "core/memory.h"
|
2016-06-15 01:03:30 +02:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2022-10-12 00:16:56 +02:00
|
|
|
using ThreadQueueImplForKServerSessionRequest = KThreadQueue;
|
|
|
|
|
|
|
|
KServerSession::KServerSession(KernelCore& kernel_)
|
|
|
|
: KSynchronizationObject{kernel_}, m_lock{kernel_} {}
|
2020-12-15 09:41:48 +01:00
|
|
|
|
2022-03-12 02:17:41 +01:00
|
|
|
KServerSession::~KServerSession() = default;
|
2016-06-15 01:03:30 +02:00
|
|
|
|
2021-06-07 00:39:11 +02:00
|
|
|
void KServerSession::Initialize(KSession* parent_session_, std::string&& name_,
|
2021-06-05 04:26:48 +02:00
|
|
|
std::shared_ptr<SessionRequestManager> manager_) {
|
2021-04-14 02:48:37 +02:00
|
|
|
// Set member variables.
|
2021-06-07 00:39:11 +02:00
|
|
|
parent = parent_session_;
|
2021-04-14 02:48:37 +02:00
|
|
|
name = std::move(name_);
|
2022-10-12 00:16:56 +02:00
|
|
|
manager = manager_;
|
2016-06-15 01:03:30 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 02:48:37 +02:00
|
|
|
void KServerSession::Destroy() {
|
|
|
|
parent->OnServerClosed();
|
|
|
|
|
2022-10-12 00:16:56 +02:00
|
|
|
this->CleanupRequests();
|
|
|
|
|
2021-04-14 02:48:37 +02:00
|
|
|
parent->Close();
|
2022-03-11 09:13:21 +01:00
|
|
|
|
|
|
|
// Release host emulation members.
|
|
|
|
manager.reset();
|
2022-04-07 02:44:06 +02:00
|
|
|
|
|
|
|
// Ensure that the global list tracking server objects does not hold on to a reference.
|
|
|
|
kernel.UnregisterServerObject(this);
|
2020-02-11 22:36:39 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 02:48:37 +02:00
|
|
|
void KServerSession::OnClientClosed() {
|
2022-10-12 00:16:56 +02:00
|
|
|
if (manager && manager->HasSessionHandler()) {
|
2021-05-16 08:49:03 +02:00
|
|
|
manager->SessionHandler().ClientDisconnected(this);
|
2021-04-14 02:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KServerSession::IsSignaled() const {
|
2022-10-12 00:16:56 +02:00
|
|
|
ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(kernel));
|
2021-04-14 02:48:37 +02:00
|
|
|
|
|
|
|
// If the client is closed, we're always signaled.
|
|
|
|
if (parent->IsClientClosed()) {
|
|
|
|
return true;
|
2019-03-06 00:51:16 +01:00
|
|
|
}
|
2021-04-14 02:48:37 +02:00
|
|
|
|
|
|
|
// Otherwise, we're signaled if we have a request and aren't handling one.
|
2022-10-15 03:24:25 +02:00
|
|
|
return !m_request_list.empty() && m_current_request == nullptr;
|
2019-03-06 00:51:16 +01:00
|
|
|
}
|
|
|
|
|
2022-06-26 05:44:19 +02:00
|
|
|
Result KServerSession::QueueSyncRequest(KThread* thread, Core::Memory::Memory& memory) {
|
2019-11-26 00:28:48 +01:00
|
|
|
u32* cmd_buf{reinterpret_cast<u32*>(memory.GetPointer(thread->GetTLSAddress()))};
|
2021-04-14 02:48:37 +02:00
|
|
|
auto context = std::make_shared<HLERequestContext>(kernel, memory, this, thread);
|
2019-11-26 00:28:48 +01:00
|
|
|
|
|
|
|
context->PopulateFromIncomingCommandBuffer(kernel.CurrentProcess()->GetHandleTable(), cmd_buf);
|
2020-12-15 09:41:48 +01:00
|
|
|
|
2022-10-16 03:57:40 +02:00
|
|
|
return manager->QueueSyncRequest(parent, std::move(context));
|
2019-11-26 00:28:48 +01:00
|
|
|
}
|
|
|
|
|
2022-06-26 05:44:19 +02:00
|
|
|
Result KServerSession::CompleteSyncRequest(HLERequestContext& context) {
|
2022-10-16 03:57:40 +02:00
|
|
|
Result result = manager->CompleteSyncRequest(this, context);
|
2018-01-24 00:03:09 +01:00
|
|
|
|
2022-01-18 01:51:18 +01:00
|
|
|
// The calling thread is waiting for this request to complete, so wake it up.
|
|
|
|
context.GetThread().EndWait(result);
|
2017-01-05 05:23:17 +01:00
|
|
|
|
2019-11-26 00:28:48 +01:00
|
|
|
return result;
|
|
|
|
}
|
2016-06-15 01:03:30 +02:00
|
|
|
|
2022-10-15 03:24:25 +02:00
|
|
|
Result KServerSession::OnRequest(KSessionRequest* request) {
|
2022-10-12 00:16:56 +02:00
|
|
|
// Create the wait queue.
|
|
|
|
ThreadQueueImplForKServerSessionRequest wait_queue{kernel};
|
|
|
|
|
|
|
|
{
|
|
|
|
// Lock the scheduler.
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
|
|
|
// Ensure that we can handle new requests.
|
|
|
|
R_UNLESS(!parent->IsServerClosed(), ResultSessionClosed);
|
|
|
|
|
|
|
|
// Check that we're not terminating.
|
|
|
|
R_UNLESS(!GetCurrentThread(kernel).IsTerminationRequested(), ResultTerminationRequested);
|
|
|
|
|
|
|
|
if (manager) {
|
|
|
|
// HLE request.
|
|
|
|
auto& memory{kernel.System().Memory()};
|
|
|
|
this->QueueSyncRequest(GetCurrentThreadPointer(kernel), memory);
|
|
|
|
} else {
|
|
|
|
// Non-HLE request.
|
|
|
|
|
|
|
|
// Get whether we're empty.
|
2022-10-15 03:24:25 +02:00
|
|
|
const bool was_empty = m_request_list.empty();
|
2022-10-12 00:16:56 +02:00
|
|
|
|
2022-10-15 03:24:25 +02:00
|
|
|
// Add the request to the list.
|
|
|
|
request->Open();
|
|
|
|
m_request_list.push_back(*request);
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// If we were empty, signal.
|
|
|
|
if (was_empty) {
|
|
|
|
this->NotifyAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 03:24:25 +02:00
|
|
|
// If we have a request event, this is asynchronous, and we don't need to wait.
|
|
|
|
R_SUCCEED_IF(request->GetEvent() != nullptr);
|
|
|
|
|
2022-10-12 00:16:56 +02:00
|
|
|
// This is a synchronous request, so we should wait for our request to complete.
|
|
|
|
GetCurrentThread(kernel).SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::IPC);
|
|
|
|
GetCurrentThread(kernel).BeginWait(&wait_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetCurrentThread(kernel).GetWaitResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result KServerSession::SendReply() {
|
|
|
|
// Lock the session.
|
2022-10-15 03:24:25 +02:00
|
|
|
KScopedLightLock lk{m_lock};
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Get the request.
|
2022-10-15 03:24:25 +02:00
|
|
|
KSessionRequest* request;
|
2022-10-12 00:16:56 +02:00
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
|
|
|
// Get the current request.
|
2022-10-15 03:24:25 +02:00
|
|
|
request = m_current_request;
|
|
|
|
R_UNLESS(request != nullptr, ResultInvalidState);
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Clear the current request, since we're processing it.
|
2022-10-15 03:24:25 +02:00
|
|
|
m_current_request = nullptr;
|
|
|
|
if (!m_request_list.empty()) {
|
2022-10-12 00:16:56 +02:00
|
|
|
this->NotifyAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close reference to the request once we're done processing it.
|
2022-10-15 03:24:25 +02:00
|
|
|
SCOPE_EXIT({ request->Close(); });
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Extract relevant information from the request.
|
2022-10-15 03:24:25 +02:00
|
|
|
const uintptr_t client_message = request->GetAddress();
|
|
|
|
const size_t client_buffer_size = request->GetSize();
|
|
|
|
KThread* client_thread = request->GetThread();
|
|
|
|
KEvent* event = request->GetEvent();
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Check whether we're closed.
|
|
|
|
const bool closed = (client_thread == nullptr || parent->IsClientClosed());
|
|
|
|
|
|
|
|
Result result = ResultSuccess;
|
|
|
|
if (!closed) {
|
|
|
|
// If we're not closed, send the reply.
|
|
|
|
Core::Memory::Memory& memory{kernel.System().Memory()};
|
|
|
|
KThread* server_thread{GetCurrentThreadPointer(kernel)};
|
|
|
|
UNIMPLEMENTED_IF(server_thread->GetOwnerProcess() != client_thread->GetOwnerProcess());
|
|
|
|
|
|
|
|
auto* src_msg_buffer = memory.GetPointer(server_thread->GetTLSAddress());
|
2022-10-15 03:24:25 +02:00
|
|
|
auto* dst_msg_buffer = memory.GetPointer(client_message);
|
|
|
|
std::memcpy(dst_msg_buffer, src_msg_buffer, client_buffer_size);
|
2022-10-12 00:16:56 +02:00
|
|
|
} else {
|
|
|
|
result = ResultSessionClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select a result for the client.
|
|
|
|
Result client_result = result;
|
|
|
|
if (closed && R_SUCCEEDED(result)) {
|
|
|
|
result = ResultSessionClosed;
|
|
|
|
client_result = ResultSessionClosed;
|
|
|
|
} else {
|
|
|
|
result = ResultSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's a client thread, update it.
|
|
|
|
if (client_thread != nullptr) {
|
2022-10-15 03:24:25 +02:00
|
|
|
if (event != nullptr) {
|
|
|
|
// // Get the client process/page table.
|
|
|
|
// KProcess *client_process = client_thread->GetOwnerProcess();
|
|
|
|
// KPageTable *client_page_table = &client_process->PageTable();
|
|
|
|
|
|
|
|
// // If we need to, reply with an async error.
|
|
|
|
// if (R_FAILED(client_result)) {
|
|
|
|
// ReplyAsyncError(client_process, client_message, client_buffer_size,
|
|
|
|
// client_result);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Unlock the client buffer.
|
|
|
|
// // NOTE: Nintendo does not check the result of this.
|
|
|
|
// client_page_table->UnlockForIpcUserBuffer(client_message, client_buffer_size);
|
|
|
|
|
|
|
|
// Signal the event.
|
|
|
|
event->Signal();
|
|
|
|
} else {
|
|
|
|
// End the client thread's wait.
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
2022-10-12 00:16:56 +02:00
|
|
|
|
2022-10-15 03:24:25 +02:00
|
|
|
if (!client_thread->IsTerminationRequested()) {
|
|
|
|
client_thread->EndWait(client_result);
|
|
|
|
}
|
2022-10-12 00:16:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result KServerSession::ReceiveRequest() {
|
|
|
|
// Lock the session.
|
2022-10-15 03:24:25 +02:00
|
|
|
KScopedLightLock lk{m_lock};
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Get the request and client thread.
|
2022-10-15 03:24:25 +02:00
|
|
|
KSessionRequest* request;
|
2022-10-12 00:16:56 +02:00
|
|
|
KThread* client_thread;
|
|
|
|
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
|
|
|
// Ensure that we can service the request.
|
|
|
|
R_UNLESS(!parent->IsClientClosed(), ResultSessionClosed);
|
|
|
|
|
|
|
|
// Ensure we aren't already servicing a request.
|
2022-10-15 03:24:25 +02:00
|
|
|
R_UNLESS(m_current_request == nullptr, ResultNotFound);
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Ensure we have a request to service.
|
2022-10-15 03:24:25 +02:00
|
|
|
R_UNLESS(!m_request_list.empty(), ResultNotFound);
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Pop the first request from the list.
|
2022-10-15 03:24:25 +02:00
|
|
|
request = &m_request_list.front();
|
|
|
|
m_request_list.pop_front();
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Get the thread for the request.
|
2022-10-15 03:24:25 +02:00
|
|
|
client_thread = request->GetThread();
|
2022-10-12 00:16:56 +02:00
|
|
|
R_UNLESS(client_thread != nullptr, ResultSessionClosed);
|
|
|
|
|
|
|
|
// Open the client thread.
|
|
|
|
client_thread->Open();
|
|
|
|
}
|
|
|
|
|
2022-10-15 03:24:25 +02:00
|
|
|
SCOPE_EXIT({ client_thread->Close(); });
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Set the request as our current.
|
2022-10-15 03:24:25 +02:00
|
|
|
m_current_request = request;
|
|
|
|
|
|
|
|
// Get the client address.
|
|
|
|
uintptr_t client_message = request->GetAddress();
|
|
|
|
size_t client_buffer_size = request->GetSize();
|
|
|
|
// bool recv_list_broken = false;
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Receive the message.
|
|
|
|
Core::Memory::Memory& memory{kernel.System().Memory()};
|
|
|
|
KThread* server_thread{GetCurrentThreadPointer(kernel)};
|
|
|
|
UNIMPLEMENTED_IF(server_thread->GetOwnerProcess() != client_thread->GetOwnerProcess());
|
|
|
|
|
2022-10-15 03:24:25 +02:00
|
|
|
auto* src_msg_buffer = memory.GetPointer(client_message);
|
2022-10-12 00:16:56 +02:00
|
|
|
auto* dst_msg_buffer = memory.GetPointer(server_thread->GetTLSAddress());
|
2022-10-15 03:24:25 +02:00
|
|
|
std::memcpy(dst_msg_buffer, src_msg_buffer, client_buffer_size);
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// We succeeded.
|
|
|
|
return ResultSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KServerSession::CleanupRequests() {
|
|
|
|
KScopedLightLock lk(m_lock);
|
|
|
|
|
|
|
|
// Clean up any pending requests.
|
|
|
|
while (true) {
|
|
|
|
// Get the next request.
|
2022-10-15 03:24:25 +02:00
|
|
|
KSessionRequest* request = nullptr;
|
2022-10-12 00:16:56 +02:00
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
2022-10-15 03:24:25 +02:00
|
|
|
if (m_current_request) {
|
2022-10-12 00:16:56 +02:00
|
|
|
// Choose the current request if we have one.
|
2022-10-15 03:24:25 +02:00
|
|
|
request = m_current_request;
|
|
|
|
m_current_request = nullptr;
|
|
|
|
} else if (!m_request_list.empty()) {
|
2022-10-12 00:16:56 +02:00
|
|
|
// Pop the request from the front of the list.
|
2022-10-15 03:24:25 +02:00
|
|
|
request = &m_request_list.front();
|
|
|
|
m_request_list.pop_front();
|
2022-10-12 00:16:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no request, we're done.
|
2022-10-15 03:24:25 +02:00
|
|
|
if (request == nullptr) {
|
2022-10-12 00:16:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close a reference to the request once it's cleaned up.
|
2022-10-15 03:24:25 +02:00
|
|
|
SCOPE_EXIT({ request->Close(); });
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// Extract relevant information from the request.
|
|
|
|
// const uintptr_t client_message = request->GetAddress();
|
|
|
|
// const size_t client_buffer_size = request->GetSize();
|
2022-10-15 03:24:25 +02:00
|
|
|
KThread* client_thread = request->GetThread();
|
|
|
|
KEvent* event = request->GetEvent();
|
2022-10-12 00:16:56 +02:00
|
|
|
|
|
|
|
// KProcess *server_process = request->GetServerProcess();
|
|
|
|
// KProcess *client_process = (client_thread != nullptr) ?
|
|
|
|
// client_thread->GetOwnerProcess() : nullptr;
|
|
|
|
// KProcessPageTable *client_page_table = (client_process != nullptr) ?
|
|
|
|
// &client_process->GetPageTable() : nullptr;
|
|
|
|
|
|
|
|
// Cleanup the mappings.
|
|
|
|
// Result result = CleanupMap(request, server_process, client_page_table);
|
|
|
|
|
|
|
|
// If there's a client thread, update it.
|
|
|
|
if (client_thread != nullptr) {
|
2022-10-15 03:24:25 +02:00
|
|
|
if (event != nullptr) {
|
|
|
|
// // We need to reply async.
|
|
|
|
// ReplyAsyncError(client_process, client_message, client_buffer_size,
|
|
|
|
// (R_SUCCEEDED(result) ? ResultSessionClosed : result));
|
|
|
|
|
|
|
|
// // Unlock the client buffer.
|
|
|
|
// NOTE: Nintendo does not check the result of this.
|
|
|
|
// client_page_table->UnlockForIpcUserBuffer(client_message, client_buffer_size);
|
|
|
|
|
|
|
|
// Signal the event.
|
|
|
|
event->Signal();
|
|
|
|
} else {
|
|
|
|
// End the client thread's wait.
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
|
|
|
if (!client_thread->IsTerminationRequested()) {
|
|
|
|
client_thread->EndWait(ResultSessionClosed);
|
|
|
|
}
|
2022-10-12 00:16:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 01:03:30 +02:00
|
|
|
}
|
2019-11-26 00:28:48 +01:00
|
|
|
|
2017-06-21 00:33:28 +02:00
|
|
|
} // namespace Kernel
|