summaryrefslogtreecommitdiffstats
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/CMakeLists.txt19
-rw-r--r--src/core/arm/dyncom/arm_dyncom_interpreter.cpp4
-rw-r--r--src/core/arm/dyncom/arm_dyncom_trans.h2
-rw-r--r--src/core/arm/skyeye_common/armstate.cpp2
-rw-r--r--src/core/core.cpp2
-rw-r--r--src/core/gdbstub/gdbstub.cpp20
-rw-r--r--src/core/gdbstub/gdbstub.h11
-rw-r--r--src/core/hle/ipc.h (renamed from src/core/hle/kernel/session.h)96
-rw-r--r--src/core/hle/kernel/address_arbiter.cpp2
-rw-r--r--src/core/hle/kernel/client_port.cpp32
-rw-r--r--src/core/hle/kernel/client_port.h13
-rw-r--r--src/core/hle/kernel/client_session.cpp40
-rw-r--r--src/core/hle/kernel/client_session.h65
-rw-r--r--src/core/hle/kernel/kernel.cpp59
-rw-r--r--src/core/hle/kernel/kernel.h45
-rw-r--r--src/core/hle/kernel/server_port.cpp4
-rw-r--r--src/core/hle/kernel/server_port.h14
-rw-r--r--src/core/hle/kernel/server_session.cpp79
-rw-r--r--src/core/hle/kernel/server_session.h94
-rw-r--r--src/core/hle/kernel/thread.cpp101
-rw-r--r--src/core/hle/kernel/thread.h44
-rw-r--r--src/core/hle/kernel/timer.cpp4
-rw-r--r--src/core/hle/result.h1
-rw-r--r--src/core/hle/service/act/act.cpp18
-rw-r--r--src/core/hle/service/act/act.h14
-rw-r--r--src/core/hle/service/act/act_a.cpp (renamed from src/core/hle/service/act_a.cpp)3
-rw-r--r--src/core/hle/service/act/act_a.h (renamed from src/core/hle/service/act_a.h)0
-rw-r--r--src/core/hle/service/act/act_u.cpp (renamed from src/core/hle/service/act_u.cpp)3
-rw-r--r--src/core/hle/service/act/act_u.h (renamed from src/core/hle/service/act_u.h)0
-rw-r--r--src/core/hle/service/apt/apt.h3
-rw-r--r--src/core/hle/service/apt/apt_a.cpp2
-rw-r--r--src/core/hle/service/apt/apt_s.cpp2
-rw-r--r--src/core/hle/service/apt/apt_u.cpp2
-rw-r--r--src/core/hle/service/cecd/cecd.cpp10
-rw-r--r--src/core/hle/service/cecd/cecd_ndm.cpp23
-rw-r--r--src/core/hle/service/cecd/cecd_ndm.h22
-rw-r--r--src/core/hle/service/cecd/cecd_s.cpp26
-rw-r--r--src/core/hle/service/cecd/cecd_s.h4
-rw-r--r--src/core/hle/service/cecd/cecd_u.cpp3
-rw-r--r--src/core/hle/service/cecd/cecd_u.h4
-rw-r--r--src/core/hle/service/dlp/dlp.h2
-rw-r--r--src/core/hle/service/fs/archive.cpp60
-rw-r--r--src/core/hle/service/fs/archive.h32
-rw-r--r--src/core/hle/service/fs/fs_user.cpp33
-rw-r--r--src/core/hle/service/hid/hid.cpp3
-rw-r--r--src/core/hle/service/service.cpp48
-rw-r--r--src/core/hle/service/service.h88
-rw-r--r--src/core/hle/service/soc_u.cpp2
-rw-r--r--src/core/hle/service/srv.cpp14
-rw-r--r--src/core/hle/service/srv.h2
-rw-r--r--src/core/hle/svc.cpp206
-rw-r--r--src/core/memory.cpp10
52 files changed, 976 insertions, 416 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index e26677079..af224166a 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -38,6 +38,7 @@ set(SRCS
hle/applets/swkbd.cpp
hle/kernel/address_arbiter.cpp
hle/kernel/client_port.cpp
+ hle/kernel/client_session.cpp
hle/kernel/event.cpp
hle/kernel/kernel.cpp
hle/kernel/memory.cpp
@@ -46,14 +47,15 @@ set(SRCS
hle/kernel/resource_limit.cpp
hle/kernel/semaphore.cpp
hle/kernel/server_port.cpp
- hle/kernel/session.cpp
+ hle/kernel/server_session.cpp
hle/kernel/shared_memory.cpp
hle/kernel/thread.cpp
hle/kernel/timer.cpp
hle/kernel/vm_manager.cpp
hle/service/ac_u.cpp
- hle/service/act_a.cpp
- hle/service/act_u.cpp
+ hle/service/act/act.cpp
+ hle/service/act/act_a.cpp
+ hle/service/act/act_u.cpp
hle/service/am/am.cpp
hle/service/am/am_app.cpp
hle/service/am/am_net.cpp
@@ -73,6 +75,7 @@ set(SRCS
hle/service/cam/cam_s.cpp
hle/service/cam/cam_u.cpp
hle/service/cecd/cecd.cpp
+ hle/service/cecd/cecd_ndm.cpp
hle/service/cecd/cecd_s.cpp
hle/service/cecd/cecd_u.cpp
hle/service/cfg/cfg.cpp
@@ -194,12 +197,14 @@ set(HEADERS
hle/config_mem.h
hle/function_wrappers.h
hle/hle.h
+ hle/ipc.h
hle/applets/applet.h
hle/applets/erreula.h
hle/applets/mii_selector.h
hle/applets/swkbd.h
hle/kernel/address_arbiter.h
hle/kernel/client_port.h
+ hle/kernel/client_session.h
hle/kernel/event.h
hle/kernel/kernel.h
hle/kernel/memory.h
@@ -208,15 +213,16 @@ set(HEADERS
hle/kernel/resource_limit.h
hle/kernel/semaphore.h
hle/kernel/server_port.h
- hle/kernel/session.h
+ hle/kernel/server_session.h
hle/kernel/shared_memory.h
hle/kernel/thread.h
hle/kernel/timer.h
hle/kernel/vm_manager.h
hle/result.h
hle/service/ac_u.h
- hle/service/act_a.h
- hle/service/act_u.h
+ hle/service/act/act.h
+ hle/service/act/act_a.h
+ hle/service/act/act_u.h
hle/service/am/am.h
hle/service/am/am_app.h
hle/service/am/am_net.h
@@ -236,6 +242,7 @@ set(HEADERS
hle/service/cam/cam_s.h
hle/service/cam/cam_u.h
hle/service/cecd/cecd.h
+ hle/service/cecd/cecd_ndm.h
hle/service/cecd/cecd_s.h
hle/service/cecd/cecd_u.h
hle/service/cfg/cfg.h
diff --git a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
index 7b8616702..67c45640a 100644
--- a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
+++ b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
@@ -953,7 +953,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
#define GDB_BP_CHECK \
cpu->Cpsr &= ~(1 << 5); \
cpu->Cpsr |= cpu->TFlag << 5; \
- if (GDBStub::g_server_enabled) { \
+ if (GDBStub::IsServerEnabled()) { \
if (GDBStub::IsMemoryBreak() || (breakpoint_data.type != GDBStub::BreakpointType::None && \
PC == breakpoint_data.address)) { \
GDBStub::Break(); \
@@ -1649,7 +1649,7 @@ DISPATCH : {
}
// Find breakpoint if one exists within the block
- if (GDBStub::g_server_enabled && GDBStub::IsConnected()) {
+ if (GDBStub::IsConnected()) {
breakpoint_data =
GDBStub::GetNextBreakpointFromAddress(cpu->Reg[15], GDBStub::BreakpointType::Execute);
}
diff --git a/src/core/arm/dyncom/arm_dyncom_trans.h b/src/core/arm/dyncom/arm_dyncom_trans.h
index b1ec90662..632ff2cd6 100644
--- a/src/core/arm/dyncom/arm_dyncom_trans.h
+++ b/src/core/arm/dyncom/arm_dyncom_trans.h
@@ -1,3 +1,5 @@
+#pragma once
+
#include <cstddef>
#include "common/common_types.h"
diff --git a/src/core/arm/skyeye_common/armstate.cpp b/src/core/arm/skyeye_common/armstate.cpp
index 1465b074e..c36b0208f 100644
--- a/src/core/arm/skyeye_common/armstate.cpp
+++ b/src/core/arm/skyeye_common/armstate.cpp
@@ -182,7 +182,7 @@ void ARMul_State::ResetMPCoreCP15Registers() {
}
static void CheckMemoryBreakpoint(u32 address, GDBStub::BreakpointType type) {
- if (GDBStub::g_server_enabled && GDBStub::CheckBreakpoint(address, type)) {
+ if (GDBStub::IsServerEnabled() && GDBStub::CheckBreakpoint(address, type)) {
LOG_DEBUG(Debug, "Found memory breakpoint @ %08x", address);
GDBStub::Break(true);
}
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 49ac8be6e..6efa18159 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -22,7 +22,7 @@ std::unique_ptr<ARM_Interface> g_sys_core; ///< ARM11 system (OS) core
/// Run the core CPU loop
void RunLoop(int tight_loop) {
- if (GDBStub::g_server_enabled) {
+ if (GDBStub::IsServerEnabled()) {
GDBStub::HandlePacket();
// If the loop is halted and we want to step, use a tiny (1) number of instructions to
diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index 21d941363..1303bafc1 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -5,6 +5,7 @@
// Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
#include <algorithm>
+#include <atomic>
#include <climits>
#include <csignal>
#include <cstdarg>
@@ -130,7 +131,10 @@ static u16 gdbstub_port = 24689;
static bool halt_loop = true;
static bool step_loop = false;
-std::atomic<bool> g_server_enabled(false);
+
+// If set to false, the server will never be started and no
+// gdbstub-related functions will be executed.
+static std::atomic<bool> server_enabled(false);
#ifdef _WIN32
WSADATA InitData;
@@ -902,7 +906,7 @@ void SetServerPort(u16 port) {
void ToggleServer(bool status) {
if (status) {
- g_server_enabled = status;
+ server_enabled = status;
// Start server
if (!IsConnected() && Core::g_sys_core != nullptr) {
@@ -914,12 +918,12 @@ void ToggleServer(bool status) {
Shutdown();
}
- g_server_enabled = status;
+ server_enabled = status;
}
}
static void Init(u16 port) {
- if (!g_server_enabled) {
+ if (!server_enabled) {
// Set the halt loop to false in case the user enabled the gdbstub mid-execution.
// This way the CPU can still execute normally.
halt_loop = false;
@@ -998,7 +1002,7 @@ void Init() {
}
void Shutdown() {
- if (!g_server_enabled) {
+ if (!server_enabled) {
return;
}
@@ -1015,8 +1019,12 @@ void Shutdown() {
LOG_INFO(Debug_GDBStub, "GDB stopped.");
}
+bool IsServerEnabled() {
+ return server_enabled;
+}
+
bool IsConnected() {
- return g_server_enabled && gdbserver_socket != -1;
+ return IsServerEnabled() && gdbserver_socket != -1;
}
bool GetCpuHaltFlag() {
diff --git a/src/core/gdbstub/gdbstub.h b/src/core/gdbstub/gdbstub.h
index a7483bb10..38177e32c 100644
--- a/src/core/gdbstub/gdbstub.h
+++ b/src/core/gdbstub/gdbstub.h
@@ -5,7 +5,7 @@
// Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
#pragma once
-#include <atomic>
+
#include "common/common_types.h"
namespace GDBStub {
@@ -24,10 +24,6 @@ struct BreakpointAddress {
BreakpointType type;
};
-/// If set to false, the server will never be started and no gdbstub-related functions will be
-/// executed.
-extern std::atomic<bool> g_server_enabled;
-
/**
* Set the port the gdbstub should use to listen for connections.
*
@@ -36,7 +32,7 @@ extern std::atomic<bool> g_server_enabled;
void SetServerPort(u16 port);
/**
- * Set the g_server_enabled flag and start or stop the server if possible.
+ * Starts or stops the server if possible.
*
* @param status Set the server to enabled or disabled.
*/
@@ -48,6 +44,9 @@ void Init();
/// Stop gdbstub server.
void Shutdown();
+/// Checks if the gdbstub server is enabled.
+bool IsServerEnabled();
+
/// Returns true if there is an active socket connection.
bool IsConnected();
diff --git a/src/core/hle/kernel/session.h b/src/core/hle/ipc.h
index ec025f732..4e094faa7 100644
--- a/src/core/hle/kernel/session.h
+++ b/src/core/hle/ipc.h
@@ -1,17 +1,31 @@
-// Copyright 2014 Citra Emulator Project
+// Copyright 2016 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
-#include <string>
-#include "common/assert.h"
#include "common/common_types.h"
-#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/thread.h"
-#include "core/hle/result.h"
#include "core/memory.h"
+namespace Kernel {
+
+static const int kCommandHeaderOffset = 0x80; ///< Offset into command buffer of header
+
+/**
+ * Returns a pointer to the command buffer in the current thread's TLS
+ * TODO(Subv): This is not entirely correct, the command buffer should be copied from
+ * the thread's TLS to an intermediate buffer in kernel memory, and then copied again to
+ * the service handler process' memory.
+ * @param offset Optional offset into command buffer
+ * @return Pointer to command buffer
+ */
+inline u32* GetCommandBuffer(const int offset = 0) {
+ return (u32*)Memory::GetPointer(GetCurrentThread()->GetTLSAddress() + kCommandHeaderOffset +
+ offset);
+}
+}
+
namespace IPC {
enum DescriptorType : u32 {
@@ -144,75 +158,3 @@ inline DescriptorType GetDescriptorType(u32 descriptor) {
}
} // namespace IPC
-
-namespace Kernel {
-
-static const int kCommandHeaderOffset = 0x80; ///< Offset into command buffer of header
-
-/**
- * Returns a pointer to the command buffer in the current thread's TLS
- * TODO(Subv): This is not entirely correct, the command buffer should be copied from
- * the thread's TLS to an intermediate buffer in kernel memory, and then copied again to
- * the service handler process' memory.
- * @param offset Optional offset into command buffer
- * @return Pointer to command buffer
- */
-inline u32* GetCommandBuffer(const int offset = 0) {
- return (u32*)Memory::GetPointer(GetCurrentThread()->GetTLSAddress() + kCommandHeaderOffset +
- offset);
-}
-
-/**
- * Kernel object representing the client endpoint of an IPC session. Sessions are the basic CTR-OS
- * primitive for communication between different processes, and are used to implement service calls
- * to the various system services.
- *
- * To make a service call, the client must write the command header and parameters to the buffer
- * located at offset 0x80 of the TLS (Thread-Local Storage) area, then execute a SendSyncRequest
- * SVC call with its Session handle. The kernel will read the command header, using it to marshall
- * the parameters to the process at the server endpoint of the session. After the server replies to
- * the request, the response is marshalled back to the caller's TLS buffer and control is
- * transferred back to it.
- *
- * In Citra, only the client endpoint is currently implemented and only HLE calls, where the IPC
- * request is answered by C++ code in the emulator, are supported. When SendSyncRequest is called
- * with the session handle, this class's SyncRequest method is called, which should read the TLS
- * buffer and emulate the call accordingly. Since the code can directly read the emulated memory,
- * no parameter marshalling is done.
- *
- * In the long term, this should be turned into the full-fledged IPC mechanism implemented by
- * CTR-OS so that IPC calls can be optionally handled by the real implementations of processes, as
- * opposed to HLE simulations.
- */
-class Session : public WaitObject {
-public:
- Session();
- ~Session() override;
-
- std::string GetTypeName() const override {
- return "Session";
- }
-
- static const HandleType HANDLE_TYPE = HandleType::Session;
- HandleType GetHandleType() const override {
- return HANDLE_TYPE;
- }
-
- /**
- * Handles a synchronous call to this session using HLE emulation. Emulated <-> emulated calls
- * aren't supported yet.
- */
- virtual ResultVal<bool> SyncRequest() = 0;
-
- // TODO(bunnei): These functions exist to satisfy a hardware test with a Session object
- // passed into WaitSynchronization. Figure out the meaning of them.
-
- bool ShouldWait() override {
- return true;
- }
-
- void Acquire() override {
- ASSERT_MSG(!ShouldWait(), "object unavailable!");
- }
-};
-}
diff --git a/src/core/hle/kernel/address_arbiter.cpp b/src/core/hle/kernel/address_arbiter.cpp
index 37eec4c84..b5a0cc3a3 100644
--- a/src/core/hle/kernel/address_arbiter.cpp
+++ b/src/core/hle/kernel/address_arbiter.cpp
@@ -79,8 +79,6 @@ ResultCode AddressArbiter::ArbitrateAddress(ArbitrationType type, VAddr address,
ErrorSummary::WrongArgument, ErrorLevel::Usage);
}
- HLE::Reschedule(__func__);
-
// The calls that use a timeout seem to always return a Timeout error even if they did not put
// the thread to sleep
if (type == ArbitrationType::WaitIfLessThanWithTimeout ||
diff --git a/src/core/hle/kernel/client_port.cpp b/src/core/hle/kernel/client_port.cpp
index aedc6f989..22645f4ec 100644
--- a/src/core/hle/kernel/client_port.cpp
+++ b/src/core/hle/kernel/client_port.cpp
@@ -4,12 +4,44 @@
#include "common/assert.h"
#include "core/hle/kernel/client_port.h"
+#include "core/hle/kernel/client_session.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/server_port.h"
+#include "core/hle/kernel/server_session.h"
namespace Kernel {
ClientPort::ClientPort() {}
ClientPort::~ClientPort() {}
+ResultVal<SharedPtr<ClientSession>> ClientPort::Connect() {
+ // Note: Threads do not wait for the server endpoint to call
+ // AcceptSession before returning from this call.
+
+ if (active_sessions >= max_sessions) {
+ // TODO(Subv): Return an error code in this situation after session disconnection is
+ // implemented.
+ /*return ResultCode(ErrorDescription::MaxConnectionsReached,
+ ErrorModule::OS, ErrorSummary::WouldBlock,
+ ErrorLevel::Temporary);*/
+ }
+ active_sessions++;
+
+ // Create a new session pair, let the created sessions inherit the parent port's HLE handler.
+ auto sessions =
+ ServerSession::CreateSessionPair(server_port->GetName(), server_port->hle_handler);
+ auto client_session = std::get<SharedPtr<ClientSession>>(sessions);
+ auto server_session = std::get<SharedPtr<ServerSession>>(sessions);
+
+ if (server_port->hle_handler)
+ server_port->hle_handler->ClientConnected(server_session);
+
+ server_port->pending_sessions.push_back(std::move(server_session));
+
+ // Wake the threads waiting on the ServerPort
+ server_port->WakeupAllWaitingThreads();
+
+ return MakeResult<SharedPtr<ClientSession>>(std::move(client_session));
+}
+
} // namespace
diff --git a/src/core/hle/kernel/client_port.h b/src/core/hle/kernel/client_port.h
index d28147718..511490c7c 100644
--- a/src/core/hle/kernel/client_port.h
+++ b/src/core/hle/kernel/client_port.h
@@ -11,8 +11,9 @@
namespace Kernel {
class ServerPort;
+class ClientSession;
-class ClientPort : public Object {
+class ClientPort final : public Object {
public:
friend class ServerPort;
std::string GetTypeName() const override {
@@ -27,12 +28,20 @@ public:
return HANDLE_TYPE;
}
+ /**
+ * 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<SharedPtr<ClientSession>> Connect();
+
SharedPtr<ServerPort> server_port; ///< ServerPort associated with this client port.
u32 max_sessions; ///< Maximum number of simultaneous sessions the port can have
u32 active_sessions; ///< Number of currently open sessions to this port
std::string name; ///< Name of client port (optional)
-protected:
+private:
ClientPort();
~ClientPort() override;
};
diff --git a/src/core/hle/kernel/client_session.cpp b/src/core/hle/kernel/client_session.cpp
new file mode 100644
index 000000000..0331386ec
--- /dev/null
+++ b/src/core/hle/kernel/client_session.cpp
@@ -0,0 +1,40 @@
+// Copyright 2016 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/client_session.h"
+#include "core/hle/kernel/server_session.h"
+
+namespace Kernel {
+
+ClientSession::ClientSession() = default;
+ClientSession::~ClientSession() {
+ // This destructor will be called automatically when the last ClientSession handle is closed by
+ // the emulated application.
+
+ if (server_session->hle_handler)
+ server_session->hle_handler->ClientDisconnected(server_session);
+
+ // TODO(Subv): If the session is still open, set the connection status to 2 (Closed by client),
+ // wake up all the ServerSession's waiting threads and set the WaitSynchronization result to
+ // 0xC920181A.
+}
+
+ResultVal<SharedPtr<ClientSession>> ClientSession::Create(ServerSession* server_session,
+ std::string name) {
+ SharedPtr<ClientSession> client_session(new ClientSession);
+
+ client_session->name = std::move(name);
+ client_session->server_session = server_session;
+ client_session->session_status = SessionStatus::Open;
+ return MakeResult<SharedPtr<ClientSession>>(std::move(client_session));
+}
+
+ResultCode ClientSession::SendSyncRequest() {
+ // Signal the server session that new data is available
+ return server_session->HandleSyncRequest();
+}
+
+} // namespace
diff --git a/src/core/hle/kernel/client_session.h b/src/core/hle/kernel/client_session.h
new file mode 100644
index 000000000..ed468dec6
--- /dev/null
+++ b/src/core/hle/kernel/client_session.h
@@ -0,0 +1,65 @@
+// 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/kernel.h"
+
+namespace Kernel {
+
+class ServerSession;
+
+enum class SessionStatus {
+ Open = 1,
+ ClosedByClient = 2,
+ ClosedBYServer = 3,
+};
+
+class ClientSession final : public Object {
+public:
+ friend class ServerSession;
+
+ std::string GetTypeName() const override {
+ return "ClientSession";
+ }
+
+ std::string GetName() const override {
+ return name;
+ }
+
+ static const HandleType HANDLE_TYPE = HandleType::ClientSession;
+ HandleType GetHandleType() const override {
+ return HANDLE_TYPE;
+ }
+
+ /**
+ * Sends an SyncRequest from the current emulated thread.
+ * @return ResultCode of the operation.
+ */
+ ResultCode SendSyncRequest();
+
+ std::string name; ///< Name of client port (optional)
+ ServerSession* server_session; ///< The server session associated with this client session.
+ SessionStatus session_status; ///< The session's current status.
+
+private:
+ ClientSession();
+ ~ClientSession() override;
+
+ /**
+ * Creates a client session.
+ * @param server_session The server session associated with this client session
+ * @param name Optional name of client session
+ * @return The created client session
+ */
+ static ResultVal<SharedPtr<ClientSession>> Create(ServerSession* server_session,
+ std::string name = "Unknown");
+};
+
+} // namespace
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index 0c8752670..1db8e102f 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include <algorithm>
+#include <boost/range/algorithm_ext/erase.hpp>
#include "common/assert.h"
#include "common/logging/log.h"
#include "core/hle/config_mem.h"
@@ -31,13 +32,61 @@ void WaitObject::RemoveWaitingThread(Thread* thread) {
waiting_threads.erase(itr);
}
-void WaitObject::WakeupAllWaitingThreads() {
- for (auto thread : waiting_threads)
- thread->ResumeFromWait();
+SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() {
+ // Remove the threads that are ready or already running from our waitlist
+ boost::range::remove_erase_if(waiting_threads, [](const SharedPtr<Thread>& thread) {
+ return thread->status == THREADSTATUS_RUNNING || thread->status == THREADSTATUS_READY ||
+ thread->status == THREADSTATUS_DEAD;
+ });
+
+ // TODO(Subv): This call should be performed inside the loop below to check if an object can be
+ // acquired by a particular thread. This is useful for things like recursive locking of Mutexes.
+ if (ShouldWait())
+ return nullptr;
+
+ Thread* candidate = nullptr;
+ s32 candidate_priority = THREADPRIO_LOWEST + 1;
+
+ for (const auto& thread : waiting_threads) {
+ if (thread->current_priority >= candidate_priority)
+ continue;
- waiting_threads.clear();
+ bool ready_to_run =
+ std::none_of(thread->wait_objects.begin(), thread->wait_objects.end(),
+ [](const SharedPtr<WaitObject>& object) { return object->ShouldWait(); });
+ if (ready_to_run) {
+ candidate = thread.get();
+ candidate_priority = thread->current_priority;
+ }
+ }
+
+ return candidate;
+}
- HLE::Reschedule(__func__);
+void WaitObject::WakeupAllWaitingThreads() {
+ while (auto thread = GetHighestPriorityReadyThread()) {
+ if (!thread->IsSleepingOnWaitAll()) {
+ Acquire();
+ // Set the output index of the WaitSynchronizationN call to the index of this object.
+ if (thread->wait_set_output) {
+ thread->SetWaitSynchronizationOutput(thread->GetWaitObjectIndex(this));
+ thread->wait_set_output = false;
+ }
+ } else {
+ for (auto& object : thread->wait_objects) {
+ object->Acquire();
+ object->RemoveWaitingThread(thread.get());
+ }
+ // Note: This case doesn't update the output index of WaitSynchronizationN.
+ // Clear the thread's waitlist
+ thread->wait_objects.clear();
+ }
+
+ thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
+ thread->ResumeFromWait();
+ // Note: Removing the thread from the object's waitlist will be
+ // done by GetHighestPriorityReadyThread.
+ }
}
const std::vector<SharedPtr<Thread>>& WaitObject::GetWaitingThreads() const {
diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h
index 231cf7b75..1adcf6c71 100644
--- a/src/core/hle/kernel/kernel.h
+++ b/src/core/hle/kernel/kernel.h
@@ -31,22 +31,21 @@ enum KernelHandle : Handle {
};
enum class HandleType : u32 {
- Unknown = 0,
-
- Session = 2,
- Event = 3,
- Mutex = 4,
- SharedMemory = 5,
- Redirection = 6,
- Thread = 7,
- Process = 8,
- AddressArbiter = 9,
- Semaphore = 10,
- Timer = 11,
- ResourceLimit = 12,
- CodeSet = 13,
- ClientPort = 14,
- ServerPort = 15,
+ Unknown,
+ Event,
+ Mutex,
+ SharedMemory,
+ Thread,
+ Process,
+ AddressArbiter,
+ Semaphore,
+ Timer,
+ ResourceLimit,
+ CodeSet,
+ ClientPort,
+ ServerPort,
+ ClientSession,
+ ServerSession,
};
enum {
@@ -82,23 +81,23 @@ public:
*/
bool IsWaitable() const {
switch (GetHandleType()) {
- case HandleType::Session:
- case HandleType::ServerPort:
case HandleType::Event:
case HandleType::Mutex:
case HandleType::Thread:
case HandleType::Semaphore:
case HandleType::Timer:
+ case HandleType::ServerPort:
+ case HandleType::ServerSession:
return true;
case HandleType::Unknown:
case HandleType::SharedMemory:
- case HandleType::Redirection:
case HandleType::Process:
case HandleType::AddressArbiter:
case HandleType::ResourceLimit:
case HandleType::CodeSet:
case HandleType::ClientPort:
+ case HandleType::ClientSession:
return false;
}
}
@@ -152,9 +151,15 @@ public:
*/
void RemoveWaitingThread(Thread* thread);
- /// Wake up all threads waiting on this object
+ /**
+ * Wake up all threads waiting on this object that can be awoken, in priority order,
+ * and set the synchronization result and output of the thread.
+ */
void WakeupAllWaitingThreads();
+ /// Obtains the highest priority thread that is ready to run from this object's waiting list.
+ SharedPtr<Thread> GetHighestPriorityReadyThread();
+
/// Get a const reference to the waiting threads list for debug use
const std::vector<SharedPtr<Thread>>& GetWaitingThreads() const;
diff --git a/src/core/hle/kernel/server_port.cpp b/src/core/hle/kernel/server_port.cpp
index 8e3ec8a14..6c19aa7c0 100644
--- a/src/core/hle/kernel/server_port.cpp
+++ b/src/core/hle/kernel/server_port.cpp
@@ -24,12 +24,14 @@ void ServerPort::Acquire() {
}
std::tuple<SharedPtr<ServerPort>, SharedPtr<ClientPort>> ServerPort::CreatePortPair(
- u32 max_sessions, std::string name) {
+ u32 max_sessions, std::string name,
+ std::shared_ptr<Service::SessionRequestHandler> hle_handler) {
SharedPtr<ServerPort> server_port(new ServerPort);
SharedPtr<ClientPort> client_port(new ClientPort);
server_port->name = name + "_Server";
+ server_port->hle_handler = std::move(hle_handler);
client_port->name = name + "_Client";
client_port->server_port = server_port;
client_port->max_sessions = max_sessions;
diff --git a/src/core/hle/kernel/server_port.h b/src/core/hle/kernel/server_port.h
index fa9448ca0..b0f8df62c 100644
--- a/src/core/hle/kernel/server_port.h
+++ b/src/core/hle/kernel/server_port.h
@@ -4,11 +4,16 @@
#pragma once
+#include <memory>
#include <string>
#include <tuple>
#include "common/common_types.h"
#include "core/hle/kernel/kernel.h"
+namespace Service {
+class SessionRequestHandler;
+}
+
namespace Kernel {
class ClientPort;
@@ -19,10 +24,13 @@ public:
* Creates a pair of ServerPort and an associated ClientPort.
* @param max_sessions Maximum number of sessions to the port
* @param name Optional name of the ports
+ * @param hle_handler Optional HLE handler template for the port,
+ * ServerSessions crated from this port will inherit a reference to this handler.
* @return The created port tuple
*/
static std::tuple<SharedPtr<ServerPort>, SharedPtr<ClientPort>> CreatePortPair(
- u32 max_sessions, std::string name = "UnknownPort");
+ u32 max_sessions, std::string name = "UnknownPort",
+ std::shared_ptr<Service::SessionRequestHandler> hle_handler = nullptr);
std::string GetTypeName() const override {
return "ServerPort";
@@ -41,6 +49,10 @@ public:
std::vector<SharedPtr<WaitObject>>
pending_sessions; ///< ServerSessions waiting to be accepted by the port
+ /// This session's HLE request handler template (optional)
+ /// ServerSessions created from this port inherit a reference to this handler.
+ std::shared_ptr<Service::SessionRequestHandler> hle_handler;
+
bool ShouldWait() override;
void Acquire() override;
diff --git a/src/core/hle/kernel/server_session.cpp b/src/core/hle/kernel/server_session.cpp
new file mode 100644
index 000000000..146458c1c
--- /dev/null
+++ b/src/core/hle/kernel/server_session.cpp
@@ -0,0 +1,79 @@
+// Copyright 2016 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <tuple>
+
+#include "core/hle/kernel/client_session.h"
+#include "core/hle/kernel/server_session.h"
+#include "core/hle/kernel/thread.h"
+
+namespace Kernel {
+
+ServerSession::ServerSession() = default;
+ServerSession::~ServerSession() {
+ // This destructor will be called automatically when the last ServerSession handle is closed by
+ // the emulated application.
+ // TODO(Subv): Reduce the ClientPort's connection count,
+ // if the session is still open, set the connection status to 3 (Closed by server),
+}
+
+ResultVal<SharedPtr<ServerSession>> ServerSession::Create(
+ std::string name, std::shared_ptr<Service::SessionRequestHandler> hle_handler) {
+ SharedPtr<ServerSession> server_session(new ServerSession);
+
+ server_session->name = std::move(name);
+ server_session->signaled = false;
+ server_session->hle_handler = std::move(hle_handler);
+
+ return MakeResult<SharedPtr<ServerSession>>(std::move(server_session));
+}
+
+bool ServerSession::ShouldWait() {
+ return !signaled;
+}
+
+void ServerSession::Acquire() {
+ ASSERT_MSG(!ShouldWait(), "object unavailable!");
+ signaled = false;
+}
+
+ResultCode ServerSession::HandleSyncRequest() {
+ // The ServerSession received a sync request, this means that there's new data available
+ // from its ClientSession, so wake up any threads that may be waiting on a svcReplyAndReceive or
+ // similar.
+
+ // If this ServerSession has an associated HLE handler, forward the request to it.
+ if (hle_handler != nullptr) {
+ // Attempt to translate the incoming request's command buffer.
+ ResultCode result = TranslateHLERequest(this);
+ if (result.IsError())
+ return result;
+ hle_handler->HandleSyncRequest(SharedPtr<ServerSession>(this));
+ // TODO(Subv): Translate the response command buffer.
+ }
+
+ // If this ServerSession does not have an HLE implementation, just wake up the threads waiting
+ // on it.
+ signaled = true;
+ WakeupAllWaitingThreads();
+ return RESULT_SUCCESS;
+}
+
+ServerSession::SessionPair ServerSession::CreateSessionPair(
+ const std::string& name, std::shared_ptr<Service::SessionRequestHandler> hle_handler) {
+ auto server_session =
+ ServerSession::Create(name + "_Server", std::move(hle_handler)).MoveFrom();
+ // We keep a non-owning pointer to the ServerSession in the ClientSession because we don't want
+ // to prevent the ServerSession's destructor from being called when the emulated
+ // application closes the last ServerSession handle.
+ auto client_session = ClientSession::Create(server_session.get(), name + "_Client").MoveFrom();
+
+ return std::make_tuple(std::move(server_session), std::move(client_session));
+}
+
+ResultCode TranslateHLERequest(ServerSession* server_session) {
+ // TODO(Subv): Implement this function once multiple concurrent processes are supported.
+ return RESULT_SUCCESS;
+}
+}
diff --git a/src/core/hle/kernel/server_session.h b/src/core/hle/kernel/server_session.h
new file mode 100644
index 000000000..458284a5d
--- /dev/null
+++ b/src/core/hle/kernel/server_session.h
@@ -0,0 +1,94 @@
+// Copyright 2014 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <string>
+#include "common/assert.h"
+#include "common/common_types.h"
+#include "core/hle/kernel/kernel.h"
+#include "core/hle/kernel/thread.h"
+#include "core/hle/result.h"
+#include "core/hle/service/service.h"
+#include "core/memory.h"
+
+namespace Kernel {
+
+class ClientSession;
+
+/**
+ * Kernel object representing the server endpoint of an IPC session. Sessions are the basic CTR-OS
+ * primitive for communication between different processes, and are used to implement service calls
+ * to the various system services.
+ *
+ * To make a service call, the client must write the command header and parameters to the buffer
+ * located at offset 0x80 of the TLS (Thread-Local Storage) area, then execute a SendSyncRequest
+ * SVC call with its ClientSession handle. The kernel will read the command header, using it to
+ * marshall the parameters to the process at the server endpoint of the session.
+ * After the server replies to the request, the response is marshalled back to the caller's
+ * TLS buffer and control is transferred back to it.
+ */
+class ServerSession final : public WaitObject {
+public:
+ std::string GetTypeName() const override {
+ return "ServerSession";
+ }
+
+ static const HandleType HANDLE_TYPE = HandleType::ServerSession;
+ HandleType GetHandleType() const override {
+ return HANDLE_TYPE;
+ }
+
+ using SessionPair = std::tuple<SharedPtr<ServerSession>, SharedPtr<ClientSession>>;
+
+ /**
+ * Creates a pair of ServerSession and an associated ClientSession.
+ * @param name Optional name of the ports
+ * @return The created session tuple
+ */
+ static SessionPair CreateSessionPair(
+ const std::string& name = "Unknown",
+ std::shared_ptr<Service::SessionRequestHandler> hle_handler = nullptr);
+
+ /**
+ * Handle a sync request from the emulated application.
+ * @returns ResultCode from the operation.
+ */
+ ResultCode HandleSyncRequest();
+
+ bool ShouldWait() override;
+
+ void Acquire() override;
+
+ std::string name; ///< The name of this session (optional)
+ bool signaled; ///< Whether there's new data available to this ServerSession
+ std::shared_ptr<Service::SessionRequestHandler>
+ hle_handler; ///< This session's HLE request handler (optional)
+
+private:
+ ServerSession();
+ ~ServerSession() override;
+
+ /**
+ * Creates a server session. The server session can have an optional HLE handler,
+ * which will be invoked to handle the IPC requests that this session receives.
+ * @param name Optional name of the server session.
+ * @param hle_handler Optional HLE handler for this server session.
+ * @return The created server session
+ */
+ static ResultVal<SharedPtr<ServerSession>> Create(
+ std::string name = "Unknown",
+ std::shared_ptr<Service::SessionRequestHandler> hle_handler = nullptr);
+};
+
+/**
+ * Performs command buffer translation for an HLE IPC request.
+ * The command buffer from the ServerSession thread's TLS is copied into a
+ * buffer and all descriptors in the buffer are processed.
+ * TODO(Subv): Implement this function, currently we do not support multiple processes running at
+ * once, but once that is implemented we'll need to properly translate all descriptors
+ * in the command buffer.
+ */
+ResultCode TranslateHLERequest(ServerSession* server_session);
+}
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index 84d6d24c6..4bbc08516 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -120,8 +120,6 @@ void Thread::Stop() {
u32 tls_slot =
((tls_address - Memory::TLS_AREA_VADDR) % Memory::PAGE_SIZE) / Memory::TLS_ENTRY_SIZE;
Kernel::g_current_process->tls_slots[tls_page].reset(tls_slot);
-
- HLE::Reschedule(__func__);
}
Thread* ArbitrateHighestPriorityThread(u32 address) {
@@ -181,50 +179,6 @@ static void PriorityBoostStarvedThreads() {
}
/**
- * Gets the registers for timeout parameter of the next WaitSynchronization call.
- * @param thread a pointer to the thread that is ready to call WaitSynchronization
- * @returns a tuple of two register pointers to low and high part of the timeout parameter
- */
-static std::tuple<u32*, u32*> GetWaitSynchTimeoutParameterRegister(Thread* thread) {
- bool thumb_mode = (thread->context.cpsr & TBIT) != 0;
- u16 thumb_inst = Memory::Read16(thread->context.pc & 0xFFFFFFFE);
- u32 inst = Memory::Read32(thread->context.pc & 0xFFFFFFFC) & 0x0FFFFFFF;
-
- if ((thumb_mode && thumb_inst == 0xDF24) || (!thumb_mode && inst == 0x0F000024)) {
- // svc #0x24 (WaitSynchronization1)
- return std::make_tuple(&thread->context.cpu_registers[2],
- &thread->context.cpu_registers[3]);
- } else if ((thumb_mode && thumb_inst == 0xDF25) || (!thumb_mode && inst == 0x0F000025)) {
- // svc #0x25 (WaitSynchronizationN)
- return std::make_tuple(&thread->context.cpu_registers[0],
- &thread->context.cpu_registers[4]);
- }
-
- UNREACHABLE();
-}
-
-/**
- * Updates the WaitSynchronization timeout parameter according to the difference
- * between ticks of the last WaitSynchronization call and the incoming one.
- * @param timeout_low a pointer to the register for the low part of the timeout parameter
- * @param timeout_high a pointer to the register for the high part of the timeout parameter
- * @param last_tick tick of the last WaitSynchronization call
- */
-static void UpdateTimeoutParameter(u32* timeout_low, u32* timeout_high, u64 last_tick) {
- s64 timeout = ((s64)*timeout_high << 32) | *timeout_low;
-
- if (timeout != -1) {
- timeout -= cyclesToUs(CoreTiming::GetTicks() - last_tick) * 1000; // in nanoseconds
-
- if (timeout < 0)
- timeout = 0;
-
- *timeout_low = timeout & 0xFFFFFFFF;
- *timeout_high = timeout >> 32;
- }
-}
-
-/**
* Switches the CPU's active thread context to that of the specified thread
* @param new_thread The thread to switch to
*/
@@ -254,32 +208,6 @@ static void SwitchContext(Thread* new_thread) {
current_thread = new_thread;
- // If the thread was waited by a svcWaitSynch call, step back PC by one instruction to rerun
- // the SVC when the thread wakes up. This is necessary to ensure that the thread can acquire
- // the requested wait object(s) before continuing.
- if (new_thread->waitsynch_waited) {
- // CPSR flag indicates CPU mode
- bool thumb_mode = (new_thread->context.cpsr & TBIT) != 0;
-
- // SVC instruction is 2 bytes for THUMB, 4 bytes for ARM
- new_thread->context.pc -= thumb_mode ? 2 : 4;
-
- // Get the register for timeout parameter
- u32 *timeout_low, *timeout_high;
- std::tie(timeout_low, timeout_high) = GetWaitSynchTimeoutParameterRegister(new_thread);
-
- // Update the timeout parameter
- UpdateTimeoutParameter(timeout_low, timeout_high, new_thread->last_running_ticks);
- }
-
- // Clean up the thread's wait_objects, they'll be restored if needed during
- // the svcWaitSynchronization call
- for (size_t i = 0; i < new_thread->wait_objects.size(); ++i) {
- SharedPtr<WaitObject> object = new_thread->wait_objects[i];
- object->RemoveWaitingThread(new_thread);
- }
- new_thread->wait_objects.clear();
-
ready_queue.remove(new_thread->current_priority, new_thread);
new_thread->status = THREADSTATUS_RUNNING;
@@ -319,17 +247,13 @@ static Thread* PopNextReadyThread() {
void WaitCurrentThread_Sleep() {
Thread* thread = GetCurrentThread();
thread->status = THREADSTATUS_WAIT_SLEEP;
-
- HLE::Reschedule(__func__);
}
void WaitCurrentThread_WaitSynchronization(std::vector<SharedPtr<WaitObject>> wait_objects,
- bool wait_set_output, bool wait_all) {
+ bool wait_set_output) {
Thread* thread = GetCurrentThread();
thread->wait_set_output = wait_set_output;
- thread->wait_all = wait_all;
thread->wait_objects = std::move(wait_objects);
- thread->waitsynch_waited = true;
thread->status = THREADSTATUS_WAIT_SYNCH;
}
@@ -351,15 +275,15 @@ static void ThreadWakeupCallback(u64 thread_handle, int cycles_late) {
return;
}
- thread->waitsynch_waited = false;
-
if (thread->status == THREADSTATUS_WAIT_SYNCH || thread->status == THREADSTATUS_WAIT_ARB) {
+ thread->wait_set_output = false;
+ // Remove the thread from each of its waiting objects' waitlists
+ for (auto& object : thread->wait_objects)
+ object->RemoveWaitingThread(thread.get());
+ thread->wait_objects.clear();
thread->SetWaitSynchronizationResult(ResultCode(ErrorDescription::Timeout, ErrorModule::OS,
ErrorSummary::StatusChanged,
ErrorLevel::Info));
-
- if (thread->wait_set_output)
- thread->SetWaitSynchronizationOutput(-1);
}
thread->ResumeFromWait();
@@ -399,6 +323,7 @@ void Thread::ResumeFromWait() {
ready_queue.push_back(current_priority, this);
status = THREADSTATUS_READY;
+ HLE::Reschedule(__func__);
}
/**
@@ -494,13 +419,11 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
thread->last_running_ticks = CoreTiming::GetTicks();
thread->processor_id = processor_id;
thread->wait_set_output = false;
- thread->wait_all = false;
thread->wait_objects.clear();
thread->wait_address = 0;
thread->name = std::move(name);
thread->callback_handle = wakeup_callback_handle_table.Create(thread).MoveFrom();
thread->owner_process = g_current_process;
- thread->waitsynch_waited = false;
// Find the next available TLS index, and mark it as used
auto& tls_slots = Kernel::g_current_process->tls_slots;
@@ -555,8 +478,6 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
ready_queue.push_back(thread->current_priority, thread.get());
thread->status = THREADSTATUS_READY;
- HLE::Reschedule(__func__);
-
return MakeResult<SharedPtr<Thread>>(std::move(thread));
}
@@ -619,14 +540,6 @@ void Reschedule() {
HLE::DoneRescheduling();
- // Don't bother switching to the same thread.
- // But if the thread was waiting on objects, we still need to switch it
- // to perform PC modification, change state to RUNNING, etc.
- // This occurs in the case when an object the thread is waiting on immediately wakes up
- // the current thread before Reschedule() is called.
- if (next == cur && (next == nullptr || next->waitsynch_waited == false))
- return;
-
if (cur && next) {
LOG_TRACE(Kernel, "context switch %u -> %u", cur->GetObjectId(), next->GetObjectId());
} else if (cur) {
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h
index e0ffcea8a..238359fc5 100644
--- a/src/core/hle/kernel/thread.h
+++ b/src/core/hle/kernel/thread.h
@@ -5,7 +5,9 @@
#pragma once
#include <string>
+#include <unordered_map>
#include <vector>
+#include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp>
#include "common/common_types.h"
#include "core/core.h"
@@ -125,6 +127,16 @@ public:
void SetWaitSynchronizationOutput(s32 output);
/**
+ * Retrieves the index that this particular object occupies in the list of objects
+ * that the thread passed to WaitSynchronizationN.
+ * It is used to set the output value of WaitSynchronizationN when the thread is awakened.
+ * @param object Object to query the index of.
+ */
+ s32 GetWaitObjectIndex(const WaitObject* object) const {
+ return wait_objects_index.at(object->GetObjectId());
+ }
+
+ /**
* Stops a thread, invalidating it from further use
*/
void Stop();
@@ -137,6 +149,15 @@ public:
return tls_address;
}
+ /**
+ * Returns whether this thread is waiting for all the objects in
+ * its wait list to become ready, as a result of a WaitSynchronizationN call
+ * with wait_all = true, or a ReplyAndReceive call.
+ */
+ bool IsSleepingOnWaitAll() const {
+ return !wait_objects.empty();
+ }
+
Core::ThreadContext context;
u32 thread_id;
@@ -154,16 +175,22 @@ public:
VAddr tls_address; ///< Virtual address of the Thread Local Storage of the thread
- bool waitsynch_waited; ///< Set to true if the last svcWaitSynch call caused the thread to wait
-
/// Mutexes currently held by this thread, which will be released when it exits.
boost::container::flat_set<SharedPtr<Mutex>> held_mutexes;
- SharedPtr<Process> owner_process; ///< Process that owns this thread
- std::vector<SharedPtr<WaitObject>> wait_objects; ///< Objects that the thread is waiting on
- VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address
- bool wait_all; ///< True if the thread is waiting on all objects before resuming
- bool wait_set_output; ///< True if the output parameter should be set on thread wakeup
+ SharedPtr<Process> owner_process; ///< Process that owns this thread
+
+ /// Objects that the thread is waiting on.
+ /// This is only populated when the thread should wait for all the objects to become ready.
+ std::vector<SharedPtr<WaitObject>> wait_objects;
+
+ /// Mapping of Object ids to their position in the last waitlist that this object waited on.
+ boost::container::flat_map<int, s32> wait_objects_index;
+
+ VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address
+
+ /// True if the WaitSynchronizationN output parameter should be set on thread wakeup.
+ bool wait_set_output;
std::string name;
@@ -215,10 +242,9 @@ void WaitCurrentThread_Sleep();
* @param wait_objects Kernel objects that we are waiting on
* @param wait_set_output If true, set the output parameter on thread wakeup (for
* WaitSynchronizationN only)
- * @param wait_all If true, wait on all objects before resuming (for WaitSynchronizationN only)
*/
void WaitCurrentThread_WaitSynchronization(std::vector<SharedPtr<WaitObject>> wait_objects,
- bool wait_set_output, bool wait_all);
+ bool wait_set_output);
/**
* Waits the current thread from an ArbitrateAddress call
diff --git a/src/core/hle/kernel/timer.cpp b/src/core/hle/kernel/timer.cpp
index eac181f4e..b50cf520d 100644
--- a/src/core/hle/kernel/timer.cpp
+++ b/src/core/hle/kernel/timer.cpp
@@ -60,14 +60,10 @@ void Timer::Set(s64 initial, s64 interval) {
u64 initial_microseconds = initial / 1000;
CoreTiming::ScheduleEvent(usToCycles(initial_microseconds), timer_callback_event_type,
callback_handle);
-
- HLE::Reschedule(__func__);
}
void Timer::Cancel() {
CoreTiming::UnscheduleEvent(timer_callback_event_type, callback_handle);
-
- HLE::Reschedule(__func__);
}
void Timer::Clear() {
diff --git a/src/core/hle/result.h b/src/core/hle/result.h
index 8d29117a8..53864a3a7 100644
--- a/src/core/hle/result.h
+++ b/src/core/hle/result.h
@@ -18,6 +18,7 @@ enum class ErrorDescription : u32 {
Success = 0,
WrongPermission = 46,
OS_InvalidBufferDescriptor = 48,
+ MaxConnectionsReached = 52,
WrongAddress = 53,
FS_ArchiveNotMounted = 101,
FS_FileNotFound = 112,
diff --git a/src/core/hle/service/act/act.cpp b/src/core/hle/service/act/act.cpp
new file mode 100644
index 000000000..9600036c0
--- /dev/null
+++ b/src/core/hle/service/act/act.cpp
@@ -0,0 +1,18 @@
+// Copyright 2016 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include "core/hle/service/act/act.h"
+#include "core/hle/service/act/act_a.h"
+#include "core/hle/service/act/act_u.h"
+
+namespace Service {
+namespace ACT {
+
+void Init() {
+ AddService(new ACT_A);
+ AddService(new ACT_U);
+}
+
+} // namespace ACT
+} // namespace Service
diff --git a/src/core/hle/service/act/act.h b/src/core/hle/service/act/act.h
new file mode 100644
index 000000000..1425291aa
--- /dev/null
+++ b/src/core/hle/service/act/act.h
@@ -0,0 +1,14 @@
+// Copyright 2016 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+namespace Service {
+namespace ACT {
+
+/// Initializes all ACT services
+void Init();
+
+} // namespace ACT
+} // namespace Service
diff --git a/src/core/hle/service/act_a.cpp b/src/core/hle/service/act/act_a.cpp
index 9880aafff..5c523368f 100644
--- a/src/core/hle/service/act_a.cpp
+++ b/src/core/hle/service/act/act_a.cpp
@@ -2,7 +2,8 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
-#include "core/hle/service/act_a.h"
+#include "core/hle/service/act/act.h"
+#include "core/hle/service/act/act_a.h"
namespace Service {
namespace ACT {
diff --git a/src/core/hle/service/act_a.h b/src/core/hle/service/act/act_a.h
index e3adb03e5..e3adb03e5 100644
--- a/src/core/hle/service/act_a.h
+++ b/src/core/hle/service/act/act_a.h
diff --git a/src/core/hle/service/act_u.cpp b/src/core/hle/service/act/act_u.cpp
index b4f69c57d..cf98aa1d6 100644
--- a/src/core/hle/service/act_u.cpp
+++ b/src/core/hle/service/act/act_u.cpp
@@ -2,7 +2,8 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
-#include "core/hle/service/act_u.h"
+#include "core/hle/service/act/act.h"
+#include "core/hle/service/act/act_u.h"
namespace Service {
namespace ACT {
diff --git a/src/core/hle/service/act_u.h b/src/core/hle/service/act/act_u.h
index 9d8538fbf..9d8538fbf 100644
--- a/src/core/hle/service/act_u.h
+++ b/src/core/hle/service/act/act_u.h
diff --git a/src/core/hle/service/apt/apt.h b/src/core/hle/service/apt/apt.h
index e6a8be870..80325361f 100644
--- a/src/core/hle/service/apt/apt.h
+++ b/src/core/hle/service/apt/apt.h
@@ -14,6 +14,9 @@ class Interface;
namespace APT {
+/// Each APT service can only have up to 2 sessions connected at the same time.
+static const u32 MaxAPTSessions = 2;
+
/// Holds information about the parameters used in Send/Glance/ReceiveParameter
struct MessageParameter {
u32 sender_id = 0;
diff --git a/src/core/hle/service/apt/apt_a.cpp b/src/core/hle/service/apt/apt_a.cpp
index 6e35e1d29..62dc2d61d 100644
--- a/src/core/hle/service/apt/apt_a.cpp
+++ b/src/core/hle/service/apt/apt_a.cpp
@@ -102,7 +102,7 @@ const Interface::FunctionInfo FunctionTable[] = {
{0x01050100, nullptr, "IsTitleAllowed"},
};
-APT_A_Interface::APT_A_Interface() {
+APT_A_Interface::APT_A_Interface() : Interface(MaxAPTSessions) {
Register(FunctionTable);
}
diff --git a/src/core/hle/service/apt/apt_s.cpp b/src/core/hle/service/apt/apt_s.cpp
index 84019e6e5..effd23dce 100644
--- a/src/core/hle/service/apt/apt_s.cpp
+++ b/src/core/hle/service/apt/apt_s.cpp
@@ -102,7 +102,7 @@ const Interface::FunctionInfo FunctionTable[] = {
{0x01050100, nullptr, "IsTitleAllowed"},
};
-APT_S_Interface::APT_S_Interface() {
+APT_S_Interface::APT_S_Interface() : Interface(MaxAPTSessions) {
Register(FunctionTable);
}
diff --git a/src/core/hle/service/apt/apt_u.cpp b/src/core/hle/service/apt/apt_u.cpp
index a731c39f6..e06084a1e 100644
--- a/src/core/hle/service/apt/apt_u.cpp
+++ b/src/core/hle/service/apt/apt_u.cpp
@@ -99,7 +99,7 @@ const Interface::FunctionInfo FunctionTable[] = {
{0x01020000, CheckNew3DS, "CheckNew3DS"},
};
-APT_U_Interface::APT_U_Interface() {
+APT_U_Interface::APT_U_Interface() : Interface(MaxAPTSessions) {
Register(FunctionTable);
}
diff --git a/src/core/hle/service/cecd/cecd.cpp b/src/core/hle/service/cecd/cecd.cpp
index 515b344e6..eb04273db 100644
--- a/src/core/hle/service/cecd/cecd.cpp
+++ b/src/core/hle/service/cecd/cecd.cpp
@@ -5,6 +5,7 @@
#include "common/logging/log.h"
#include "core/hle/kernel/event.h"
#include "core/hle/service/cecd/cecd.h"
+#include "core/hle/service/cecd/cecd_ndm.h"
#include "core/hle/service/cecd/cecd_s.h"
#include "core/hle/service/cecd/cecd_u.h"
#include "core/hle/service/service.h"
@@ -43,12 +44,13 @@ void GetChangeStateEventHandle(Service::Interface* self) {
}
void Init() {
- AddService(new CECD_S_Interface);
- AddService(new CECD_U_Interface);
+ AddService(new CECD_NDM);
+ AddService(new CECD_S);
+ AddService(new CECD_U);
- cecinfo_event = Kernel::Event::Create(Kernel::ResetType::OneShot, "CECD_U::cecinfo_event");
+ cecinfo_event = Kernel::Event::Create(Kernel::ResetType::OneShot, "CECD::cecinfo_event");
change_state_event =
- Kernel::Event::Create(Kernel::ResetType::OneShot, "CECD_U::change_state_event");
+ Kernel::Event::Create(Kernel::ResetType::OneShot, "CECD::change_state_event");
}
void Shutdown() {
diff --git a/src/core/hle/service/cecd/cecd_ndm.cpp b/src/core/hle/service/cecd/cecd_ndm.cpp
new file mode 100644
index 000000000..7baf93750
--- /dev/null
+++ b/src/core/hle/service/cecd/cecd_ndm.cpp
@@ -0,0 +1,23 @@
+// Copyright 2016 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include "core/hle/service/cecd/cecd.h"
+#include "core/hle/service/cecd/cecd_ndm.h"
+
+namespace Service {
+namespace CECD {
+
+static const Interface::FunctionInfo FunctionTable[] = {
+ {0x00010000, nullptr, "Initialize"},
+ {0x00020000, nullptr, "Deinitialize"},
+ {0x00030000, nullptr, "ResumeDaemon"},
+ {0x00040040, nullptr, "SuspendDaemon"},
+};
+
+CECD_NDM::CECD_NDM() {
+ Register(FunctionTable);
+}
+
+} // namespace CECD
+} // namespace Service
diff --git a/src/core/hle/service/cecd/cecd_ndm.h b/src/core/hle/service/cecd/cecd_ndm.h
new file mode 100644
index 000000000..2e2e50ada
--- /dev/null
+++ b/src/core/hle/service/cecd/cecd_ndm.h
@@ -0,0 +1,22 @@
+// Copyright 2016 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include "core/hle/service/service.h"
+
+namespace Service {
+namespace CECD {
+
+class CECD_NDM : public Interface {
+public:
+ CECD_NDM();
+
+ std::string GetPortName() const override {
+ return "cecd:ndm";
+ }
+};
+
+} // namespace CECD
+} // namespace Service
diff --git a/src/core/hle/service/cecd/cecd_s.cpp b/src/core/hle/service/cecd/cecd_s.cpp
index 7477b9320..eacda7d41 100644
--- a/src/core/hle/service/cecd/cecd_s.cpp
+++ b/src/core/hle/service/cecd/cecd_s.cpp
@@ -2,16 +2,34 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include "core/hle/service/cecd/cecd.h"
#include "core/hle/service/cecd/cecd_s.h"
namespace Service {
namespace CECD {
-// Empty arrays are illegal -- commented out until an entry is added.
-// const Interface::FunctionInfo FunctionTable[] = { };
+static const Interface::FunctionInfo FunctionTable[] = {
+ // cecd:u shared commands
+ {0x000100C2, nullptr, "OpenRawFile"},
+ {0x00020042, nullptr, "ReadRawFile"},
+ {0x00030104, nullptr, "ReadMessage"},
+ {0x00040106, nullptr, "ReadMessageWithHMAC"},
+ {0x00050042, nullptr, "WriteRawFile"},
+ {0x00060104, nullptr, "WriteMessage"},
+ {0x00070106, nullptr, "WriteMessageWithHMAC"},
+ {0x00080102, nullptr, "Delete"},
+ {0x000A00C4, nullptr, "GetSystemInfo"},
+ {0x000B0040, nullptr, "RunCommand"},
+ {0x000C0040, nullptr, "RunCommandAlt"},
+ {0x000E0000, GetCecStateAbbreviated, "GetCecStateAbbreviated"},
+ {0x000F0000, GetCecInfoEventHandle, "GetCecInfoEventHandle"},
+ {0x00100000, GetChangeStateEventHandle, "GetChangeStateEventHandle"},
+ {0x00110104, nullptr, "OpenAndWrite"},
+ {0x00120104, nullptr, "OpenAndRead"},
+};
-CECD_S_Interface::CECD_S_Interface() {
- // Register(FunctionTable);
+CECD_S::CECD_S() {
+ Register(FunctionTable);
}
} // namespace CECD
diff --git a/src/core/hle/service/cecd/cecd_s.h b/src/core/hle/service/cecd/cecd_s.h
index df5c01849..ab6c6789a 100644
--- a/src/core/hle/service/cecd/cecd_s.h
+++ b/src/core/hle/service/cecd/cecd_s.h
@@ -9,9 +9,9 @@
namespace Service {
namespace CECD {
-class CECD_S_Interface : public Interface {
+class CECD_S : public Interface {
public:
- CECD_S_Interface();
+ CECD_S();
std::string GetPortName() const override {
return "cecd:s";
diff --git a/src/core/hle/service/cecd/cecd_u.cpp b/src/core/hle/service/cecd/cecd_u.cpp
index 7d98ba6e9..3ed864f0b 100644
--- a/src/core/hle/service/cecd/cecd_u.cpp
+++ b/src/core/hle/service/cecd/cecd_u.cpp
@@ -9,6 +9,7 @@ namespace Service {
namespace CECD {
static const Interface::FunctionInfo FunctionTable[] = {
+ // cecd:u shared commands
{0x000100C2, nullptr, "OpenRawFile"},
{0x00020042, nullptr, "ReadRawFile"},
{0x00030104, nullptr, "ReadMessage"},
@@ -27,7 +28,7 @@ static const Interface::FunctionInfo FunctionTable[] = {
{0x00120104, nullptr, "OpenAndRead"},
};
-CECD_U_Interface::CECD_U_Interface() {
+CECD_U::CECD_U() {
Register(FunctionTable);
}
diff --git a/src/core/hle/service/cecd/cecd_u.h b/src/core/hle/service/cecd/cecd_u.h
index 394030ffc..16e874ff5 100644
--- a/src/core/hle/service/cecd/cecd_u.h
+++ b/src/core/hle/service/cecd/cecd_u.h
@@ -9,9 +9,9 @@
namespace Service {
namespace CECD {
-class CECD_U_Interface : public Interface {
+class CECD_U : public Interface {
public:
- CECD_U_Interface();
+ CECD_U();
std::string GetPortName() const override {
return "cecd:u";
diff --git a/src/core/hle/service/dlp/dlp.h b/src/core/hle/service/dlp/dlp.h
index ec2fe46e8..3185fe322 100644
--- a/src/core/hle/service/dlp/dlp.h
+++ b/src/core/hle/service/dlp/dlp.h
@@ -2,6 +2,8 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#pragma once
+
namespace Service {
namespace DLP {
diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp
index bef75f5df..09205e4b2 100644
--- a/src/core/hle/service/fs/archive.cpp
+++ b/src/core/hle/service/fs/archive.cpp
@@ -24,6 +24,7 @@
#include "core/file_sys/directory_backend.h"
#include "core/file_sys/file_backend.h"
#include "core/hle/hle.h"
+#include "core/hle/kernel/client_session.h"
#include "core/hle/result.h"
#include "core/hle/service/fs/archive.h"
#include "core/hle/service/fs/fs_user.h"
@@ -93,7 +94,7 @@ File::File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path&
File::~File() {}
-ResultVal<bool> File::SyncRequest() {
+void File::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
u32* cmd_buff = Kernel::GetCommandBuffer();
FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]);
switch (cmd) {
@@ -103,8 +104,8 @@ ResultVal<bool> File::SyncRequest() {
u64 offset = cmd_buff[1] | ((u64)cmd_buff[2]) << 32;
u32 length = cmd_buff[3];
u32 address = cmd_buff[5];
- LOG_TRACE(Service_FS, "Read %s %s: offset=0x%llx length=%d address=0x%x",
- GetTypeName().c_str(), GetName().c_str(), offset, length, address);
+ LOG_TRACE(Service_FS, "Read %s: offset=0x%llx length=%d address=0x%x", GetName().c_str(),
+ offset, length, address);
if (offset + length > backend->GetSize()) {
LOG_ERROR(Service_FS,
@@ -116,7 +117,7 @@ ResultVal<bool> File::SyncRequest() {
ResultVal<size_t> read = backend->Read(offset, data.size(), data.data());
if (read.Failed()) {
cmd_buff[1] = read.Code().raw;
- return read.Code();
+ return;
}
Memory::WriteBlock(address, data.data(), *read);
cmd_buff[2] = static_cast<u32>(*read);
@@ -129,22 +130,22 @@ ResultVal<bool> File::SyncRequest() {
u32 length = cmd_buff[3];
u32 flush = cmd_buff[4];
u32 address = cmd_buff[6];
- LOG_TRACE(Service_FS, "Write %s %s: offset=0x%llx length=%d address=0x%x, flush=0x%x",
- GetTypeName().c_str(), GetName().c_str(), offset, length, address, flush);
+ LOG_TRACE(Service_FS, "Write %s: offset=0x%llx length=%d address=0x%x, flush=0x%x",
+ GetName().c_str(), offset, length, address, flush);
std::vector<u8> data(length);
Memory::ReadBlock(address, data.data(), data.size());
ResultVal<size_t> written = backend->Write(offset, data.size(), flush != 0, data.data());
if (written.Failed()) {
cmd_buff[1] = written.Code().raw;
- return written.Code();
+ return;
}
cmd_buff[2] = static_cast<u32>(*written);
break;
}
case FileCommand::GetSize: {
- LOG_TRACE(Service_FS, "GetSize %s %s", GetTypeName().c_str(), GetName().c_str());
+ LOG_TRACE(Service_FS, "GetSize %s", GetName().c_str());
u64 size = backend->GetSize();
cmd_buff[2] = (u32)size;
cmd_buff[3] = size >> 32;
@@ -153,14 +154,13 @@ ResultVal<bool> File::SyncRequest() {
case FileCommand::SetSize: {
u64 size = cmd_buff[1] | ((u64)cmd_buff[2] << 32);
- LOG_TRACE(Service_FS, "SetSize %s %s size=%llu", GetTypeName().c_str(), GetName().c_str(),
- size);
+ LOG_TRACE(Service_FS, "SetSize %s size=%llu", GetName().c_str(), size);
backend->SetSize(size);
break;
}
case FileCommand::Close: {
- LOG_TRACE(Service_FS, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
+ LOG_TRACE(Service_FS, "Close %s", GetName().c_str());
backend->Close();
break;
}
@@ -173,7 +173,11 @@ ResultVal<bool> File::SyncRequest() {
case FileCommand::OpenLinkFile: {
LOG_WARNING(Service_FS, "(STUBBED) File command OpenLinkFile %s", GetName().c_str());
- cmd_buff[3] = Kernel::g_handle_table.Create(this).ValueOr(INVALID_HANDLE);
+ auto sessions = Kernel::ServerSession::CreateSessionPair(GetName(), shared_from_this());
+ ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
+ cmd_buff[3] = Kernel::g_handle_table
+ .Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions))
+ .ValueOr(INVALID_HANDLE);
break;
}
@@ -194,10 +198,9 @@ ResultVal<bool> File::SyncRequest() {
LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd);
ResultCode error = UnimplementedFunction(ErrorModule::FS);
cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
- return error;
+ return;
}
cmd_buff[1] = RESULT_SUCCESS.raw; // No error
- return MakeResult<bool>(false);
}
Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
@@ -206,18 +209,16 @@ Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
Directory::~Directory() {}
-ResultVal<bool> Directory::SyncRequest() {
+void Directory::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
u32* cmd_buff = Kernel::GetCommandBuffer();
DirectoryCommand cmd = static_cast<DirectoryCommand>(cmd_buff[0]);
switch (cmd) {
-
// Read from directory...
case DirectoryCommand::Read: {
u32 count = cmd_buff[1];
u32 address = cmd_buff[3];
std::vector<FileSys::Entry> entries(count);
- LOG_TRACE(Service_FS, "Read %s %s: count=%d", GetTypeName().c_str(), GetName().c_str(),
- count);
+ LOG_TRACE(Service_FS, "Read %s: count=%d", GetName().c_str(), count);
// Number of entries actually read
u32 read = backend->Read(entries.size(), entries.data());
@@ -227,7 +228,7 @@ ResultVal<bool> Directory::SyncRequest() {
}
case DirectoryCommand::Close: {
- LOG_TRACE(Service_FS, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
+ LOG_TRACE(Service_FS, "Close %s", GetName().c_str());
backend->Close();
break;
}
@@ -237,10 +238,9 @@ ResultVal<bool> Directory::SyncRequest() {
LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd);
ResultCode error = UnimplementedFunction(ErrorModule::FS);
cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
- return MakeResult<bool>(false);
+ return;
}
cmd_buff[1] = RESULT_SUCCESS.raw; // No error
- return MakeResult<bool>(false);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -307,9 +307,9 @@ ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factor
return RESULT_SUCCESS;
}
-ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
- const FileSys::Path& path,
- const FileSys::Mode mode) {
+ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
+ const FileSys::Path& path,
+ const FileSys::Mode mode) {
ArchiveBackend* archive = GetArchive(archive_handle);
if (archive == nullptr)
return ERR_INVALID_ARCHIVE_HANDLE;
@@ -318,8 +318,8 @@ ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_han
if (backend.Failed())
return backend.Code();
- auto file = Kernel::SharedPtr<File>(new File(backend.MoveFrom(), path));
- return MakeResult<Kernel::SharedPtr<File>>(std::move(file));
+ auto file = std::shared_ptr<File>(new File(backend.MoveFrom(), path));
+ return MakeResult<std::shared_ptr<File>>(std::move(file));
}
ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
@@ -398,8 +398,8 @@ ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
}
}
-ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
- const FileSys::Path& path) {
+ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
+ const FileSys::Path& path) {
ArchiveBackend* archive = GetArchive(archive_handle);
if (archive == nullptr)
return ERR_INVALID_ARCHIVE_HANDLE;
@@ -408,8 +408,8 @@ ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle a
if (backend.Failed())
return backend.Code();
- auto directory = Kernel::SharedPtr<Directory>(new Directory(backend.MoveFrom(), path));
- return MakeResult<Kernel::SharedPtr<Directory>>(std::move(directory));
+ auto directory = std::shared_ptr<Directory>(new Directory(backend.MoveFrom(), path));
+ return MakeResult<std::shared_ptr<Directory>>(std::move(directory));
}
ResultVal<u64> GetFreeBytesInArchive(ArchiveHandle archive_handle) {
diff --git a/src/core/hle/service/fs/archive.h b/src/core/hle/service/fs/archive.h
index 87089bd92..7ba62ede0 100644
--- a/src/core/hle/service/fs/archive.h
+++ b/src/core/hle/service/fs/archive.h
@@ -8,7 +8,7 @@
#include <string>
#include "common/common_types.h"
#include "core/file_sys/archive_backend.h"
-#include "core/hle/kernel/session.h"
+#include "core/hle/kernel/server_session.h"
#include "core/hle/result.h"
namespace FileSys {
@@ -43,33 +43,37 @@ enum class MediaType : u32 { NAND = 0, SDMC = 1, GameCard = 2 };
typedef u64 ArchiveHandle;
-class File : public Kernel::Session {
+class File final : public SessionRequestHandler, public std::enable_shared_from_this<File> {
public:
File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path);
~File();
- std::string GetName() const override {
+ std::string GetName() const {
return "Path: " + path.DebugStr();
}
- ResultVal<bool> SyncRequest() override;
FileSys::Path path; ///< Path of the file
u32 priority; ///< Priority of the file. TODO(Subv): Find out what this means
std::unique_ptr<FileSys::FileBackend> backend; ///< File backend interface
+
+protected:
+ void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
};
-class Directory : public Kernel::Session {
+class Directory final : public SessionRequestHandler {
public:
Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, const FileSys::Path& path);
~Directory();
- std::string GetName() const override {
+ std::string GetName() const {
return "Directory: " + path.DebugStr();
}
- ResultVal<bool> SyncRequest() override;
FileSys::Path path; ///< Path of the directory
std::unique_ptr<FileSys::DirectoryBackend> backend; ///< File backend interface
+
+protected:
+ void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
};
/**
@@ -99,11 +103,11 @@ ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factor
* @param archive_handle Handle to an open Archive object
* @param path Path to the File inside of the Archive
* @param mode Mode under which to open the File
- * @return The opened File object as a Session
+ * @return The opened File object
*/
-ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
- const FileSys::Path& path,
- const FileSys::Mode mode);
+ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
+ const FileSys::Path& path,
+ const FileSys::Mode mode);
/**
* Delete a File from an Archive
@@ -178,10 +182,10 @@ ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
* Open a Directory from an Archive
* @param archive_handle Handle to an open Archive object
* @param path Path to the Directory inside of the Archive
- * @return The opened Directory object as a Session
+ * @return The opened Directory object
*/
-ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
- const FileSys::Path& path);
+ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
+ const FileSys::Path& path);
/**
* Get the free space in an Archive
diff --git a/src/core/hle/service/fs/fs_user.cpp b/src/core/hle/service/fs/fs_user.cpp
index d6ab5b065..337da1387 100644
--- a/src/core/hle/service/fs/fs_user.cpp
+++ b/src/core/hle/service/fs/fs_user.cpp
@@ -8,6 +8,7 @@
#include "common/logging/log.h"
#include "common/scope_exit.h"
#include "common/string_util.h"
+#include "core/hle/kernel/client_session.h"
#include "core/hle/result.h"
#include "core/hle/service/fs/archive.h"
#include "core/hle/service/fs/fs_user.h"
@@ -17,7 +18,7 @@
// Namespace FS_User
using Kernel::SharedPtr;
-using Kernel::Session;
+using Kernel::ServerSession;
namespace Service {
namespace FS {
@@ -67,10 +68,16 @@ static void OpenFile(Service::Interface* self) {
LOG_DEBUG(Service_FS, "path=%s, mode=%u attrs=%u", file_path.DebugStr().c_str(), mode.hex,
attributes);
- ResultVal<SharedPtr<File>> file_res = OpenFileFromArchive(archive_handle, file_path, mode);
+ ResultVal<std::shared_ptr<File>> file_res =
+ OpenFileFromArchive(archive_handle, file_path, mode);
cmd_buff[1] = file_res.Code().raw;
if (file_res.Succeeded()) {
- cmd_buff[3] = Kernel::g_handle_table.Create(*file_res).MoveFrom();
+ std::shared_ptr<File> file = *file_res;
+ auto sessions = ServerSession::CreateSessionPair(file->GetName(), file);
+ file->ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
+ cmd_buff[3] = Kernel::g_handle_table
+ .Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions))
+ .MoveFrom();
} else {
cmd_buff[3] = 0;
LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str());
@@ -127,10 +134,16 @@ static void OpenFileDirectly(Service::Interface* self) {
}
SCOPE_EXIT({ CloseArchive(*archive_handle); });
- ResultVal<SharedPtr<File>> file_res = OpenFileFromArchive(*archive_handle, file_path, mode);
+ ResultVal<std::shared_ptr<File>> file_res =
+ OpenFileFromArchive(*archive_handle, file_path, mode);
cmd_buff[1] = file_res.Code().raw;
if (file_res.Succeeded()) {
- cmd_buff[3] = Kernel::g_handle_table.Create(*file_res).MoveFrom();
+ std::shared_ptr<File> file = *file_res;
+ auto sessions = ServerSession::CreateSessionPair(file->GetName(), file);
+ file->ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
+ cmd_buff[3] = Kernel::g_handle_table
+ .Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions))
+ .MoveFrom();
} else {
cmd_buff[3] = 0;
LOG_ERROR(Service_FS, "failed to get a handle for file %s mode=%u attributes=%u",
@@ -388,10 +401,16 @@ static void OpenDirectory(Service::Interface* self) {
LOG_DEBUG(Service_FS, "type=%u size=%u data=%s", static_cast<u32>(dirname_type), dirname_size,
dir_path.DebugStr().c_str());
- ResultVal<SharedPtr<Directory>> dir_res = OpenDirectoryFromArchive(archive_handle, dir_path);
+ ResultVal<std::shared_ptr<Directory>> dir_res =
+ OpenDirectoryFromArchive(archive_handle, dir_path);
cmd_buff[1] = dir_res.Code().raw;
if (dir_res.Succeeded()) {
- cmd_buff[3] = Kernel::g_handle_table.Create(*dir_res).MoveFrom();
+ std::shared_ptr<Directory> directory = *dir_res;
+ auto sessions = ServerSession::CreateSessionPair(directory->GetName(), directory);
+ directory->ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
+ cmd_buff[3] = Kernel::g_handle_table
+ .Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions))
+ .MoveFrom();
} else {
LOG_ERROR(Service_FS, "failed to get a handle for directory type=%d size=%d data=%s",
dirname_type, dirname_size, dir_path.DebugStr().c_str());
diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp
index 99baded11..18a1b6a16 100644
--- a/src/core/hle/service/hid/hid.cpp
+++ b/src/core/hle/service/hid/hid.cpp
@@ -37,7 +37,8 @@ static int enable_gyroscope_count = 0; // positive means enabled
static PadState GetCirclePadDirectionState(s16 circle_pad_x, s16 circle_pad_y) {
// 30 degree and 60 degree are angular thresholds for directions
- constexpr float TAN30 = 0.577350269, TAN60 = 1 / TAN30;
+ constexpr float TAN30 = 0.577350269f;
+ constexpr float TAN60 = 1 / TAN30;
// a circle pad radius greater than 40 will trigger circle pad direction
constexpr int CIRCLE_PAD_THRESHOLD_SQUARE = 40 * 40;
PadState state;
diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp
index effecc043..25a7aeea8 100644
--- a/src/core/hle/service/service.cpp
+++ b/src/core/hle/service/service.cpp
@@ -2,11 +2,14 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include <boost/range/algorithm_ext/erase.hpp>
+
#include "common/logging/log.h"
#include "common/string_util.h"
+
+#include "core/hle/kernel/server_port.h"
#include "core/hle/service/ac_u.h"
-#include "core/hle/service/act_a.h"
-#include "core/hle/service/act_u.h"
+#include "core/hle/service/act/act.h"
#include "core/hle/service/am/am.h"
#include "core/hle/service/apt/apt.h"
#include "core/hle/service/boss/boss.h"
@@ -44,8 +47,8 @@
namespace Service {
-std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_kernel_named_ports;
-std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_srv_services;
+std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports;
+std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_srv_services;
/**
* Creates a function string for logging, complete with the name (or header code, depending
@@ -64,7 +67,23 @@ static std::string MakeFunctionString(const char* name, const char* port_name,
return function_string;
}
-ResultVal<bool> Interface::SyncRequest() {
+void SessionRequestHandler::ClientConnected(
+ Kernel::SharedPtr<Kernel::ServerSession> server_session) {
+ connected_sessions.push_back(server_session);
+}
+
+void SessionRequestHandler::ClientDisconnected(
+ Kernel::SharedPtr<Kernel::ServerSession> server_session) {
+ boost::range::remove_erase(connected_sessions, server_session);
+}
+
+Interface::Interface(u32 max_sessions) : max_sessions(max_sessions) {}
+Interface::~Interface() = default;
+
+void Interface::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
+ // TODO(Subv): Make use of the server_session in the HLE service handlers to distinguish which
+ // session triggered each command.
+
u32* cmd_buff = Kernel::GetCommandBuffer();
auto itr = m_functions.find(cmd_buff[0]);
@@ -78,14 +97,12 @@ ResultVal<bool> Interface::SyncRequest() {
// TODO(bunnei): Hack - ignore error
cmd_buff[1] = 0;
- return MakeResult<bool>(false);
+ return;
}
LOG_TRACE(Service, "%s",
MakeFunctionString(itr->second.name, GetPortName().c_str(), cmd_buff).c_str());
itr->second.func(this);
-
- return MakeResult<bool>(false); // TODO: Implement return from actual function
}
void Interface::Register(const FunctionInfo* functions, size_t n) {
@@ -100,11 +117,19 @@ void Interface::Register(const FunctionInfo* functions, size_t n) {
// Module interface
static void AddNamedPort(Interface* interface_) {
- g_kernel_named_ports.emplace(interface_->GetPortName(), interface_);
+ auto ports =
+ Kernel::ServerPort::CreatePortPair(interface_->GetMaxSessions(), interface_->GetPortName(),
+ std::shared_ptr<Interface>(interface_));
+ auto client_port = std::get<Kernel::SharedPtr<Kernel::ClientPort>>(ports);
+ g_kernel_named_ports.emplace(interface_->GetPortName(), std::move(client_port));
}
void AddService(Interface* interface_) {
- g_srv_services.emplace(interface_->GetPortName(), interface_);
+ auto ports =
+ Kernel::ServerPort::CreatePortPair(interface_->GetMaxSessions(), interface_->GetPortName(),
+ std::shared_ptr<Interface>(interface_));
+ auto client_port = std::get<Kernel::SharedPtr<Kernel::ClientPort>>(ports);
+ g_srv_services.emplace(interface_->GetPortName(), std::move(client_port));
}
/// Initialize ServiceManager
@@ -113,6 +138,7 @@ void Init() {
AddNamedPort(new ERR::ERR_F);
FS::ArchiveInit();
+ ACT::Init();
AM::Init();
APT::Init();
BOSS::Init();
@@ -132,8 +158,6 @@ void Init() {
QTM::Init();
AddService(new AC::AC_U);
- AddService(new ACT::ACT_A);
- AddService(new ACT::ACT_U);
AddService(new CSND::CSND_SND);
AddService(new DSP_DSP::Interface);
AddService(new GSP::GSP_GPU);
diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h
index 29daacfc4..a7ba7688f 100644
--- a/src/core/hle/service/service.h
+++ b/src/core/hle/service/service.h
@@ -9,8 +9,15 @@
#include <unordered_map>
#include <boost/container/flat_map.hpp>
#include "common/common_types.h"
-#include "core/hle/kernel/session.h"
+#include "core/hle/ipc.h"
+#include "core/hle/kernel/client_port.h"
+#include "core/hle/kernel/thread.h"
#include "core/hle/result.h"
+#include "core/memory.h"
+
+namespace Kernel {
+class ServerSession;
+}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Namespace Service
@@ -18,14 +25,63 @@
namespace Service {
static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
+/// Arbitrary default number of maximum connections to an HLE service.
+static const u32 DefaultMaxSessions = 10;
+
+/**
+ * Interface implemented by HLE Session handlers.
+ * This can be provided to a ServerSession in order to hook into several relevant events
+ * (such as a new connection or a SyncRequest) so they can be implemented in the emulator.
+ */
+class SessionRequestHandler {
+public:
+ /**
+ * Handles a sync request from the emulated application.
+ * @param server_session The ServerSession that was triggered for this sync request,
+ * it should be used to differentiate which client (As in ClientSession) we're answering to.
+ * TODO(Subv): Use a wrapper structure to hold all the information relevant to
+ * this request (ServerSession, Originator thread, Translated command buffer, etc).
+ * @returns ResultCode the result code of the translate operation.
+ */
+ virtual void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) = 0;
+
+ /**
+ * Signals that a client has just connected to this HLE handler and keeps the
+ * associated ServerSession alive for the duration of the connection.
+ * @param server_session Owning pointer to the ServerSession associated with the connection.
+ */
+ void ClientConnected(Kernel::SharedPtr<Kernel::ServerSession> server_session);
+
+ /**
+ * Signals that a client has just disconnected from this HLE handler and releases the
+ * associated ServerSession.
+ * @param server_session ServerSession associated with the connection.
+ */
+ void ClientDisconnected(Kernel::SharedPtr<Kernel::ServerSession> server_session);
-/// Interface to a CTROS service
-class Interface : public Kernel::Session {
- // TODO(yuriks): An "Interface" being a Kernel::Object is mostly non-sense. Interface should be
- // just something that encapsulates a session and acts as a helper to implement service
- // processes.
+protected:
+ /// List of sessions that are connected to this handler.
+ /// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list
+ // for the duration of the connection.
+ std::vector<Kernel::SharedPtr<Kernel::ServerSession>> connected_sessions;
+};
+
+/**
+ * Framework for implementing HLE service handlers which dispatch incoming SyncRequests based on a
+ * table mapping header ids to handler functions.
+ */
+class Interface : public SessionRequestHandler {
public:
- std::string GetName() const override {
+ /**
+ * Creates an HLE interface with the specified max sessions.
+ * @param max_sessions Maximum number of sessions that can be
+ * connected to this service at the same time.
+ */
+ Interface(u32 max_sessions = DefaultMaxSessions);
+
+ virtual ~Interface();
+
+ std::string GetName() const {
return GetPortName();
}
@@ -33,6 +89,15 @@ public:
version.raw = raw_version;
}
+ /**
+ * Gets the maximum allowed number of sessions that can be connected to this service
+ * at the same time.
+ * @returns The maximum number of connections allowed.
+ */
+ u32 GetMaxSessions() const {
+ return max_sessions;
+ }
+
typedef void (*Function)(Interface*);
struct FunctionInfo {
@@ -49,9 +114,9 @@ public:
return "[UNKNOWN SERVICE PORT]";
}
- ResultVal<bool> SyncRequest() override;
-
protected:
+ void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
+
/**
* Registers the functions in the service
*/
@@ -71,6 +136,7 @@ protected:
} version = {};
private:
+ u32 max_sessions; ///< Maximum number of concurrent sessions that this service can handle.
boost::container::flat_map<u32, FunctionInfo> m_functions;
};
@@ -81,9 +147,9 @@ void Init();
void Shutdown();
/// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC.
-extern std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_kernel_named_ports;
+extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports;
/// Map of services registered with the "srv:" service, retrieved using GetServiceHandle.
-extern std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_srv_services;
+extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_srv_services;
/// Adds a service to the services table
void AddService(Interface* interface_);
diff --git a/src/core/hle/service/soc_u.cpp b/src/core/hle/service/soc_u.cpp
index fd251fc0a..c3918cdd0 100644
--- a/src/core/hle/service/soc_u.cpp
+++ b/src/core/hle/service/soc_u.cpp
@@ -11,7 +11,7 @@
#include "common/common_types.h"
#include "common/logging/log.h"
#include "common/scope_exit.h"
-#include "core/hle/kernel/session.h"
+#include "core/hle/kernel/server_session.h"
#include "core/hle/result.h"
#include "core/hle/service/soc_u.h"
#include "core/memory.h"
diff --git a/src/core/hle/service/srv.cpp b/src/core/hle/service/srv.cpp
index f8df38c42..3bd787147 100644
--- a/src/core/hle/service/srv.cpp
+++ b/src/core/hle/service/srv.cpp
@@ -2,9 +2,13 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include <tuple>
+
#include "common/common_types.h"
#include "common/logging/log.h"
+#include "core/hle/kernel/client_session.h"
#include "core/hle/kernel/event.h"
+#include "core/hle/kernel/server_session.h"
#include "core/hle/service/srv.h"
namespace Service {
@@ -79,7 +83,15 @@ static void GetServiceHandle(Interface* self) {
auto it = Service::g_srv_services.find(port_name);
if (it != Service::g_srv_services.end()) {
- cmd_buff[3] = Kernel::g_handle_table.Create(it->second).MoveFrom();
+ auto client_port = it->second;
+
+ auto client_session = client_port->Connect();
+ res = client_session.Code();
+
+ if (client_session.Succeeded()) {
+ // Return the client session
+ cmd_buff[3] = Kernel::g_handle_table.Create(*client_session).MoveFrom();
+ }
LOG_TRACE(Service_SRV, "called port=%s, handle=0x%08X", port_name.c_str(), cmd_buff[3]);
} else {
LOG_ERROR(Service_SRV, "(UNIMPLEMENTED) called port=%s", port_name.c_str());
diff --git a/src/core/hle/service/srv.h b/src/core/hle/service/srv.h
index 6041ca42d..d3a9de879 100644
--- a/src/core/hle/service/srv.h
+++ b/src/core/hle/service/srv.h
@@ -2,6 +2,8 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#pragma once
+
#include "core/hle/service/service.h"
namespace Service {
diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp
index c6b80dc50..ef25acc4a 100644
--- a/src/core/hle/svc.cpp
+++ b/src/core/hle/svc.cpp
@@ -13,6 +13,7 @@
#include "core/hle/function_wrappers.h"
#include "core/hle/kernel/address_arbiter.h"
#include "core/hle/kernel/client_port.h"
+#include "core/hle/kernel/client_session.h"
#include "core/hle/kernel/event.h"
#include "core/hle/kernel/memory.h"
#include "core/hle/kernel/mutex.h"
@@ -20,6 +21,7 @@
#include "core/hle/kernel/resource_limit.h"
#include "core/hle/kernel/semaphore.h"
#include "core/hle/kernel/server_port.h"
+#include "core/hle/kernel/server_session.h"
#include "core/hle/kernel/shared_memory.h"
#include "core/hle/kernel/thread.h"
#include "core/hle/kernel/timer.h"
@@ -41,6 +43,9 @@ const ResultCode ERR_PORT_NAME_TOO_LONG(ErrorDescription(30), ErrorModule::OS,
ErrorSummary::InvalidArgument,
ErrorLevel::Usage); // 0xE0E0181E
+const ResultCode ERR_SYNC_TIMEOUT(ErrorDescription::Timeout, ErrorModule::OS,
+ ErrorSummary::StatusChanged, ErrorLevel::Info);
+
const ResultCode ERR_MISALIGNED_ADDRESS{// 0xE0E01BF1
ErrorDescription::MisalignedAddress, ErrorModule::OS,
ErrorSummary::InvalidArgument, ErrorLevel::Usage};
@@ -222,20 +227,29 @@ static ResultCode ConnectToPort(Handle* out_handle, const char* port_name) {
return ERR_NOT_FOUND;
}
- CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(it->second));
+ auto client_port = it->second;
+
+ SharedPtr<Kernel::ClientSession> client_session;
+ CASCADE_RESULT(client_session, client_port->Connect());
+
+ // Return the client session
+ CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(client_session));
return RESULT_SUCCESS;
}
-/// Synchronize to an OS service
+/// Makes a blocking IPC call to an OS service.
static ResultCode SendSyncRequest(Handle handle) {
- SharedPtr<Kernel::Session> session = Kernel::g_handle_table.Get<Kernel::Session>(handle);
+ SharedPtr<Kernel::ClientSession> session =
+ Kernel::g_handle_table.Get<Kernel::ClientSession>(handle);
if (session == nullptr) {
return ERR_INVALID_HANDLE;
}
LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s)", handle, session->GetName().c_str());
- return session->SyncRequest().Code();
+ // TODO(Subv): svcSendSyncRequest should put the caller thread to sleep while the server
+ // responds and cause a reschedule.
+ return session->SendSyncRequest();
}
/// Close a handle
@@ -249,27 +263,30 @@ static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
auto object = Kernel::g_handle_table.GetWaitObject(handle);
Kernel::Thread* thread = Kernel::GetCurrentThread();
- thread->waitsynch_waited = false;
-
if (object == nullptr)
return ERR_INVALID_HANDLE;
LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle,
object->GetTypeName().c_str(), object->GetName().c_str(), nano_seconds);
- HLE::Reschedule(__func__);
-
- // Check for next thread to schedule
if (object->ShouldWait()) {
+ if (nano_seconds == 0)
+ return ERR_SYNC_TIMEOUT;
+
object->AddWaitingThread(thread);
- Kernel::WaitCurrentThread_WaitSynchronization({object}, false, false);
+ // TODO(Subv): Perform things like update the mutex lock owner's priority to
+ // prevent priority inversion. Currently this is done in Mutex::ShouldWait,
+ // but it should be moved to a function that is called from here.
+ thread->status = THREADSTATUS_WAIT_SYNCH;
// Create an event to wake the thread up after the specified nanosecond delay has passed
thread->WakeAfterDelay(nano_seconds);
- // NOTE: output of this SVC will be set later depending on how the thread resumes
- return HLE::RESULT_INVALID;
+ // Note: The output of this SVC will be set to RESULT_SUCCESS if the thread
+ // resumes due to a signal in its wait objects.
+ // Otherwise we retain the default value of timeout.
+ return ERR_SYNC_TIMEOUT;
}
object->Acquire();
@@ -280,11 +297,7 @@ static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
/// Wait for the given handles to synchronize, timeout after the specified nanoseconds
static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count, bool wait_all,
s64 nano_seconds) {
- bool wait_thread = !wait_all;
- int handle_index = 0;
Kernel::Thread* thread = Kernel::GetCurrentThread();
- bool was_waiting = thread->waitsynch_waited;
- thread->waitsynch_waited = false;
// Check if 'handles' is invalid
if (handles == nullptr)
@@ -300,90 +313,113 @@ static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_cou
return ResultCode(ErrorDescription::OutOfRange, ErrorModule::OS,
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
- // If 'handle_count' is non-zero, iterate through each handle and wait the current thread if
- // necessary
- if (handle_count != 0) {
- bool selected = false; // True once an object has been selected
-
- Kernel::SharedPtr<Kernel::WaitObject> wait_object;
-
- for (int i = 0; i < handle_count; ++i) {
- auto object = Kernel::g_handle_table.GetWaitObject(handles[i]);
- if (object == nullptr)
- return ERR_INVALID_HANDLE;
-
- // Check if the current thread should wait on this object...
- if (object->ShouldWait()) {
-
- // Check we are waiting on all objects...
- if (wait_all)
- // Wait the thread
- wait_thread = true;
- } else {
- // Do not wait on this object, check if this object should be selected...
- if (!wait_all && (!selected || (wait_object == object && was_waiting))) {
- // Do not wait the thread
- wait_thread = false;
- handle_index = i;
- wait_object = object;
- selected = true;
- }
- }
- }
- } else {
- // If no handles were passed in, put the thread to sleep only when 'wait_all' is false
- // NOTE: This should deadlock the current thread if no timeout was specified
- if (!wait_all) {
- wait_thread = true;
- }
+ using ObjectPtr = Kernel::SharedPtr<Kernel::WaitObject>;
+ std::vector<ObjectPtr> objects(handle_count);
+
+ for (int i = 0; i < handle_count; ++i) {
+ auto object = Kernel::g_handle_table.GetWaitObject(handles[i]);
+ if (object == nullptr)
+ return ERR_INVALID_HANDLE;
+ objects[i] = object;
}
- SCOPE_EXIT({
- HLE::Reschedule("WaitSynchronizationN");
- }); // Reschedule after putting the threads to sleep.
+ // Clear the mapping of wait object indices.
+ // We don't want any lingering state in this map.
+ // It will be repopulated later in the wait_all = false case.
+ thread->wait_objects_index.clear();
+
+ if (wait_all) {
+ bool all_available =
+ std::all_of(objects.begin(), objects.end(),
+ [](const ObjectPtr& object) { return !object->ShouldWait(); });
+ if (all_available) {
+ // We can acquire all objects right now, do so.
+ for (auto& object : objects)
+ object->Acquire();
+ // Note: In this case, the `out` parameter is not set,
+ // and retains whatever value it had before.
+ return RESULT_SUCCESS;
+ }
+
+ // Not all objects were available right now, prepare to suspend the thread.
- // If thread should wait, then set its state to waiting
- if (wait_thread) {
+ // If a timeout value of 0 was provided, just return the Timeout error code instead of
+ // suspending the thread.
+ if (nano_seconds == 0)
+ return ERR_SYNC_TIMEOUT;
- // Actually wait the current thread on each object if we decided to wait...
- std::vector<SharedPtr<Kernel::WaitObject>> wait_objects;
- wait_objects.reserve(handle_count);
+ // Put the thread to sleep
+ thread->status = THREADSTATUS_WAIT_SYNCH;
- for (int i = 0; i < handle_count; ++i) {
- auto object = Kernel::g_handle_table.GetWaitObject(handles[i]);
- object->AddWaitingThread(Kernel::GetCurrentThread());
- wait_objects.push_back(object);
+ // Add the thread to each of the objects' waiting threads.
+ for (auto& object : objects) {
+ object->AddWaitingThread(thread);
+ // TODO(Subv): Perform things like update the mutex lock owner's priority to
+ // prevent priority inversion. Currently this is done in Mutex::ShouldWait,
+ // but it should be moved to a function that is called from here.
}
- Kernel::WaitCurrentThread_WaitSynchronization(std::move(wait_objects), true, wait_all);
+ // Set the thread's waitlist to the list of objects passed to WaitSynchronizationN
+ thread->wait_objects = std::move(objects);
// Create an event to wake the thread up after the specified nanosecond delay has passed
- Kernel::GetCurrentThread()->WakeAfterDelay(nano_seconds);
-
- // NOTE: output of this SVC will be set later depending on how the thread resumes
- return HLE::RESULT_INVALID;
- }
+ thread->WakeAfterDelay(nano_seconds);
- // Acquire objects if we did not wait...
- for (int i = 0; i < handle_count; ++i) {
- auto object = Kernel::g_handle_table.GetWaitObject(handles[i]);
+ // This value gets set to -1 by default in this case, it is not modified after this.
+ *out = -1;
+ // Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to
+ // a signal in one of its wait objects.
+ return ERR_SYNC_TIMEOUT;
+ } else {
+ // Find the first object that is acquirable in the provided list of objects
+ auto itr = std::find_if(objects.begin(), objects.end(),
+ [](const ObjectPtr& object) { return !object->ShouldWait(); });
- // Acquire the object if it is not waiting...
- if (!object->ShouldWait()) {
+ if (itr != objects.end()) {
+ // We found a ready object, acquire it and set the result value
+ Kernel::WaitObject* object = itr->get();
object->Acquire();
+ *out = std::distance(objects.begin(), itr);
+ return RESULT_SUCCESS;
+ }
+
+ // No objects were ready to be acquired, prepare to suspend the thread.
+
+ // If a timeout value of 0 was provided, just return the Timeout error code instead of
+ // suspending the thread.
+ if (nano_seconds == 0)
+ return ERR_SYNC_TIMEOUT;
+
+ // Put the thread to sleep
+ thread->status = THREADSTATUS_WAIT_SYNCH;
- // If this was the first non-waiting object and 'wait_all' is false, don't acquire
- // any other objects
- if (!wait_all)
- break;
+ // Clear the thread's waitlist, we won't use it for wait_all = false
+ thread->wait_objects.clear();
+
+ // Add the thread to each of the objects' waiting threads.
+ for (size_t i = 0; i < objects.size(); ++i) {
+ Kernel::WaitObject* object = objects[i].get();
+ // Set the index of this object in the mapping of Objects -> index for this thread.
+ thread->wait_objects_index[object->GetObjectId()] = static_cast<int>(i);
+ object->AddWaitingThread(thread);
+ // TODO(Subv): Perform things like update the mutex lock owner's priority to
+ // prevent priority inversion. Currently this is done in Mutex::ShouldWait,
+ // but it should be moved to a function that is called from here.
}
- }
- // TODO(bunnei): If 'wait_all' is true, this is probably wrong. However, real hardware does
- // not seem to set it to any meaningful value.
- *out = handle_count != 0 ? (wait_all ? -1 : handle_index) : 0;
+ // Note: If no handles and no timeout were given, then the thread will deadlock, this is
+ // consistent with hardware behavior.
- return RESULT_SUCCESS;
+ // Create an event to wake the thread up after the specified nanosecond delay has passed
+ thread->WakeAfterDelay(nano_seconds);
+
+ // Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a
+ // signal in one of its wait objects.
+ // Otherwise we retain the default value of timeout, and -1 in the out parameter
+ thread->wait_set_output = true;
+ *out = -1;
+ return ERR_SYNC_TIMEOUT;
+ }
}
/// Create an address arbiter (to allocate access to shared resources)
@@ -1148,6 +1184,8 @@ void CallSVC(u32 immediate) {
if (info) {
if (info->func) {
info->func();
+ // TODO(Subv): Not all service functions should cause a reschedule in all cases.
+ HLE::Reschedule(__func__);
} else {
LOG_ERROR(Kernel_SVC, "unimplemented SVC function %s(..)", info->name);
}
diff --git a/src/core/memory.cpp b/src/core/memory.cpp
index 65e4bba85..d058dc844 100644
--- a/src/core/memory.cpp
+++ b/src/core/memory.cpp
@@ -357,14 +357,24 @@ void RasterizerMarkRegionCached(PAddr start, u32 size, int count_delta) {
}
}
+static void RoundToPages(PAddr& start, u32& size) {
+ PAddr start_rounded_down = start & ~PAGE_MASK;
+ PAddr end_rounded_up = ((start + size) + PAGE_MASK) & ~PAGE_MASK;
+
+ start = start_rounded_down;
+ size = end_rounded_up - start_rounded_down;
+}
+
void RasterizerFlushRegion(PAddr start, u32 size) {
if (VideoCore::g_renderer != nullptr) {
+ RoundToPages(start, size);
VideoCore::g_renderer->Rasterizer()->FlushRegion(start, size);
}
}
void RasterizerFlushAndInvalidateRegion(PAddr start, u32 size) {
if (VideoCore::g_renderer != nullptr) {
+ RoundToPages(start, size);
VideoCore::g_renderer->Rasterizer()->FlushAndInvalidateRegion(start, size);
}
}