summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Subv2016-11-30 22:50:13 -0500
committerGravatar Subv2016-11-30 23:12:35 -0500
commit009b15b3aa9858930f461d825f7dd030fc963801 (patch)
tree060b6a82ad6c093b1832cd9e96a4fdacf29448b5 /src
parentIPC/HLE: Associate the ClientSessions with their parent port's HLE interface ... (diff)
downloadyuzu-009b15b3aa9858930f461d825f7dd030fc963801.tar.gz
yuzu-009b15b3aa9858930f461d825f7dd030fc963801.tar.xz
yuzu-009b15b3aa9858930f461d825f7dd030fc963801.zip
A bit of a redesign.
Sessions and Ports are now detached from each other. HLE services are handled by means of a SessionRequestHandler class, Interface now inherits from this class. The File and Directory classes are no longer kernel objects, but SessionRequestHandlers instead, bound to a ServerSession when requested. File::OpenLinkFile now creates a new session pair and binds the File instance to it.
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/kernel/client_port.cpp14
-rw-r--r--src/core/hle/kernel/client_port.h12
-rw-r--r--src/core/hle/kernel/client_session.cpp16
-rw-r--r--src/core/hle/kernel/client_session.h5
-rw-r--r--src/core/hle/kernel/server_session.cpp25
-rw-r--r--src/core/hle/kernel/server_session.h167
-rw-r--r--src/core/hle/service/fs/archive.cpp35
-rw-r--r--src/core/hle/service/fs/archive.h20
-rw-r--r--src/core/hle/service/fs/fs_user.cpp18
-rw-r--r--src/core/hle/service/service.cpp17
-rw-r--r--src/core/hle/service/service.h179
-rw-r--r--src/core/hle/service/srv.cpp10
-rw-r--r--src/core/hle/svc.cpp11
13 files changed, 266 insertions, 263 deletions
diff --git a/src/core/hle/kernel/client_port.cpp b/src/core/hle/kernel/client_port.cpp
index 0ac36cd12..de67688c9 100644
--- a/src/core/hle/kernel/client_port.cpp
+++ b/src/core/hle/kernel/client_port.cpp
@@ -14,21 +14,7 @@ namespace Kernel {
14ClientPort::ClientPort() {} 14ClientPort::ClientPort() {}
15ClientPort::~ClientPort() {} 15ClientPort::~ClientPort() {}
16 16
17Kernel::SharedPtr<ClientPort> ClientPort::CreateForHLE(u32 max_sessions, std::shared_ptr<Service::Interface> hle_interface) {
18 SharedPtr<ClientPort> client_port(new ClientPort);
19 client_port->max_sessions = max_sessions;
20 client_port->active_sessions = 0;
21 client_port->name = hle_interface->GetPortName();
22 client_port->hle_interface = std::move(hle_interface);
23
24 return client_port;
25}
26
27void ClientPort::AddWaitingSession(SharedPtr<ServerSession> server_session) { 17void ClientPort::AddWaitingSession(SharedPtr<ServerSession> server_session) {
28 // A port that has an associated HLE interface doesn't have a server port.
29 if (hle_interface != nullptr)
30 return;
31
32 server_port->pending_sessions.push_back(server_session); 18 server_port->pending_sessions.push_back(server_session);
33 // Wake the threads waiting on the ServerPort 19 // Wake the threads waiting on the ServerPort
34 server_port->WakeupAllWaitingThreads(); 20 server_port->WakeupAllWaitingThreads();
diff --git a/src/core/hle/kernel/client_port.h b/src/core/hle/kernel/client_port.h
index 52308f13f..7a53c93b8 100644
--- a/src/core/hle/kernel/client_port.h
+++ b/src/core/hle/kernel/client_port.h
@@ -23,14 +23,6 @@ public:
23 friend class ServerPort; 23 friend class ServerPort;
24 24
25 /** 25 /**
26 * Creates a serverless ClientPort that represents a bridge between the HLE implementation of a service/port and the emulated application.
27 * @param max_sessions Maximum number of sessions that this port is able to handle concurrently.
28 * @param hle_interface Interface object that implements the commands of the service.
29 * @returns ClientPort for the given HLE interface.
30 */
31 static Kernel::SharedPtr<ClientPort> CreateForHLE(u32 max_sessions, std::shared_ptr<Service::Interface> hle_interface);
32
33 /**
34 * Adds the specified server session to the queue of pending sessions of the associated ServerPort 26 * Adds the specified server session to the queue of pending sessions of the associated ServerPort
35 * @param server_session Server session to add to the queue 27 * @param server_session Server session to add to the queue
36 */ 28 */
@@ -44,12 +36,10 @@ public:
44 return HANDLE_TYPE; 36 return HANDLE_TYPE;
45 } 37 }
46 38
47 SharedPtr<ServerPort> server_port = nullptr; ///< ServerPort associated with this client port. 39 SharedPtr<ServerPort> server_port; ///< ServerPort associated with this client port.
48 u32 max_sessions; ///< Maximum number of simultaneous sessions the port can have 40 u32 max_sessions; ///< Maximum number of simultaneous sessions the port can have
49 u32 active_sessions; ///< Number of currently open sessions to this port 41 u32 active_sessions; ///< Number of currently open sessions to this port
50 std::string name; ///< Name of client port (optional) 42 std::string name; ///< Name of client port (optional)
51 std::shared_ptr<Service::Interface> hle_interface = nullptr; ///< HLE implementation of this port's request handler
52
53private: 43private:
54 ClientPort(); 44 ClientPort();
55 ~ClientPort() override; 45 ~ClientPort() override;
diff --git a/src/core/hle/kernel/client_session.cpp b/src/core/hle/kernel/client_session.cpp
index 22fa2ff03..31ea8045a 100644
--- a/src/core/hle/kernel/client_session.cpp
+++ b/src/core/hle/kernel/client_session.cpp
@@ -15,29 +15,17 @@ namespace Kernel {
15ClientSession::ClientSession() {} 15ClientSession::ClientSession() {}
16ClientSession::~ClientSession() {} 16ClientSession::~ClientSession() {}
17 17
18ResultVal<SharedPtr<ClientSession>> ClientSession::Create(SharedPtr<ServerSession> server_session, SharedPtr<ClientPort> client_port, std::string name) { 18ResultVal<SharedPtr<ClientSession>> ClientSession::Create(SharedPtr<ServerSession> server_session, std::string name) {
19 SharedPtr<ClientSession> client_session(new ClientSession); 19 SharedPtr<ClientSession> client_session(new ClientSession);
20 20
21 client_session->name = std::move(name); 21 client_session->name = std::move(name);
22 client_session->server_session = server_session; 22 client_session->server_session = server_session;
23 client_session->client_port = client_port;
24 client_session->hle_helper = client_port->hle_interface;
25
26 return MakeResult<SharedPtr<ClientSession>>(std::move(client_session)); 23 return MakeResult<SharedPtr<ClientSession>>(std::move(client_session));
27} 24}
28 25
29ResultCode ClientSession::HandleSyncRequest() { 26ResultCode ClientSession::HandleSyncRequest() {
30 // Signal the server session that new data is available 27 // Signal the server session that new data is available
31 ResultCode result = server_session->HandleSyncRequest(); 28 return server_session->HandleSyncRequest();
32
33 if (result.IsError())
34 return result;
35
36 // If this ClientSession has an associated HLE helper, forward the request to it.
37 if (hle_helper != nullptr)
38 result = hle_helper->HandleSyncRequest(server_session);
39
40 return result;
41} 29}
42 30
43} // namespace 31} // namespace
diff --git a/src/core/hle/kernel/client_session.h b/src/core/hle/kernel/client_session.h
index c2fc0d7dd..a951ea4d6 100644
--- a/src/core/hle/kernel/client_session.h
+++ b/src/core/hle/kernel/client_session.h
@@ -25,11 +25,10 @@ public:
25 /** 25 /**
26 * Creates a client session. 26 * Creates a client session.
27 * @param server_session The server session associated with this client session 27 * @param server_session The server session associated with this client session
28 * @param client_port The client port which this session is connected to
29 * @param name Optional name of client session 28 * @param name Optional name of client session
30 * @return The created client session 29 * @return The created client session
31 */ 30 */
32 static ResultVal<SharedPtr<ClientSession>> Create(SharedPtr<ServerSession> server_session, SharedPtr<ClientPort> client_port, std::string name = "Unknown"); 31 static ResultVal<SharedPtr<ClientSession>> Create(SharedPtr<ServerSession> server_session, std::string name = "Unknown");
33 32
34 std::string GetTypeName() const override { return "ClientSession"; } 33 std::string GetTypeName() const override { return "ClientSession"; }
35 std::string GetName() const override { return name; } 34 std::string GetName() const override { return name; }
@@ -45,8 +44,6 @@ public:
45 44
46 std::string name; ///< Name of client port (optional) 45 std::string name; ///< Name of client port (optional)
47 SharedPtr<ServerSession> server_session; ///< The server session associated with this client session. 46 SharedPtr<ServerSession> server_session; ///< The server session associated with this client session.
48 SharedPtr<ClientPort> client_port; ///< The client port which this session is connected to.
49 std::shared_ptr<Service::Interface> hle_helper = nullptr; ///< HLE implementation of this port's request handler
50 47
51private: 48private:
52 ClientSession(); 49 ClientSession();
diff --git a/src/core/hle/kernel/server_session.cpp b/src/core/hle/kernel/server_session.cpp
index 720c0eb94..200a7b815 100644
--- a/src/core/hle/kernel/server_session.cpp
+++ b/src/core/hle/kernel/server_session.cpp
@@ -14,11 +14,12 @@ namespace Kernel {
14ServerSession::ServerSession() {} 14ServerSession::ServerSession() {}
15ServerSession::~ServerSession() {} 15ServerSession::~ServerSession() {}
16 16
17ResultVal<SharedPtr<ServerSession>> ServerSession::Create(std::string name) { 17ResultVal<SharedPtr<ServerSession>> ServerSession::Create(std::string name, std::shared_ptr<Service::SessionRequestHandler> hle_handler) {
18 SharedPtr<ServerSession> server_session(new ServerSession); 18 SharedPtr<ServerSession> server_session(new ServerSession);
19 19
20 server_session->name = std::move(name); 20 server_session->name = std::move(name);
21 server_session->signaled = false; 21 server_session->signaled = false;
22 server_session->hle_handler = hle_handler;
22 23
23 return MakeResult<SharedPtr<ServerSession>>(std::move(server_session)); 24 return MakeResult<SharedPtr<ServerSession>>(std::move(server_session));
24} 25}
@@ -34,23 +35,21 @@ void ServerSession::Acquire() {
34 35
35ResultCode ServerSession::HandleSyncRequest() { 36ResultCode ServerSession::HandleSyncRequest() {
36 // The ServerSession received a sync request, this means that there's new data available 37 // The ServerSession received a sync request, this means that there's new data available
37 // from one of its ClientSessions, so wake up any threads that may be waiting on a svcReplyAndReceive or similar. 38 // from its ClientSession, so wake up any threads that may be waiting on a svcReplyAndReceive or similar.
39
40 // If this ServerSession has an associated HLE handler, forward the request to it.
41 if (hle_handler != nullptr)
42 return hle_handler->HandleSyncRequest(SharedPtr<ServerSession>(this));
43
44 // If this ServerSession does not have an HLE implementation, just wake up the threads waiting on it.
38 signaled = true; 45 signaled = true;
39 WakeupAllWaitingThreads(); 46 WakeupAllWaitingThreads();
40 return RESULT_SUCCESS; 47 return RESULT_SUCCESS;
41} 48}
42 49
43SharedPtr<ClientSession> ServerSession::CreateClientSession() { 50std::tuple<SharedPtr<ServerSession>, SharedPtr<ClientSession>> ServerSession::CreateSessionPair(const std::string& name, std::shared_ptr<Service::SessionRequestHandler> hle_handler) {
44 // In Citra, some types of ServerSessions (File and Directory sessions) are not created as a pair of Server-Client sessions, 51 auto server_session = ServerSession::Create(name + "Server", hle_handler).MoveFrom();
45 // but are instead created as a single ServerSession, which then hands over a ClientSession on demand (When opening the File or Directory). 52 auto client_session = ClientSession::Create(server_session, name + "Client").MoveFrom();
46 // The real kernel (Or more specifically, the real FS service) does create the pair of Sessions at the same time (via svcCreateSession), and simply
47 // stores the ClientSession until it is needed.
48 return ClientSession::Create(SharedPtr<ServerSession>(this), nullptr, name + "Client").MoveFrom();
49}
50
51std::tuple<SharedPtr<ServerSession>, SharedPtr<ClientSession>> ServerSession::CreateSessionPair(SharedPtr<ClientPort> client_port, const std::string& name) {
52 auto server_session = ServerSession::Create(name + "Server").MoveFrom();
53 auto client_session = ClientSession::Create(server_session, client_port, name + "Client").MoveFrom();
54 53
55 return std::make_tuple(server_session, client_session); 54 return std::make_tuple(server_session, client_session);
56} 55}
diff --git a/src/core/hle/kernel/server_session.h b/src/core/hle/kernel/server_session.h
index 510b0a150..86fe641c0 100644
--- a/src/core/hle/kernel/server_session.h
+++ b/src/core/hle/kernel/server_session.h
@@ -10,158 +10,11 @@
10#include "core/hle/kernel/kernel.h" 10#include "core/hle/kernel/kernel.h"
11#include "core/hle/kernel/thread.h" 11#include "core/hle/kernel/thread.h"
12#include "core/hle/result.h" 12#include "core/hle/result.h"
13#include "core/hle/service/service.h"
13#include "core/memory.h" 14#include "core/memory.h"
14 15
15namespace IPC {
16
17enum DescriptorType : u32 {
18 // Buffer related desciptors types (mask : 0x0F)
19 StaticBuffer = 0x02,
20 PXIBuffer = 0x04,
21 MappedBuffer = 0x08,
22 // Handle related descriptors types (mask : 0x30, but need to check for buffer related
23 // descriptors first )
24 CopyHandle = 0x00,
25 MoveHandle = 0x10,
26 CallingPid = 0x20,
27};
28
29/**
30 * @brief Creates a command header to be used for IPC
31 * @param command_id ID of the command to create a header for.
32 * @param normal_params Size of the normal parameters in words. Up to 63.
33 * @param translate_params_size Size of the translate parameters in words. Up to 63.
34 * @return The created IPC header.
35 *
36 * Normal parameters are sent directly to the process while the translate parameters might go
37 * through modifications and checks by the kernel.
38 * The translate parameters are described by headers generated with the IPC::*Desc functions.
39 *
40 * @note While #normal_params is equivalent to the number of normal parameters,
41 * #translate_params_size includes the size occupied by the translate parameters headers.
42 */
43constexpr u32 MakeHeader(u16 command_id, unsigned int normal_params,
44 unsigned int translate_params_size) {
45 return (u32(command_id) << 16) | ((u32(normal_params) & 0x3F) << 6) |
46 (u32(translate_params_size) & 0x3F);
47}
48
49union Header {
50 u32 raw;
51 BitField<0, 6, u32> translate_params_size;
52 BitField<6, 6, u32> normal_params;
53 BitField<16, 16, u32> command_id;
54};
55
56inline Header ParseHeader(u32 header) {
57 return {header};
58}
59
60constexpr u32 MoveHandleDesc(u32 num_handles = 1) {
61 return MoveHandle | ((num_handles - 1) << 26);
62}
63
64constexpr u32 CopyHandleDesc(u32 num_handles = 1) {
65 return CopyHandle | ((num_handles - 1) << 26);
66}
67
68constexpr u32 CallingPidDesc() {
69 return CallingPid;
70}
71
72constexpr bool isHandleDescriptor(u32 descriptor) {
73 return (descriptor & 0xF) == 0x0;
74}
75
76constexpr u32 HandleNumberFromDesc(u32 handle_descriptor) {
77 return (handle_descriptor >> 26) + 1;
78}
79
80constexpr u32 StaticBufferDesc(u32 size, u8 buffer_id) {
81 return StaticBuffer | (size << 14) | ((buffer_id & 0xF) << 10);
82}
83
84union StaticBufferDescInfo {
85 u32 raw;
86 BitField<10, 4, u32> buffer_id;
87 BitField<14, 18, u32> size;
88};
89
90inline StaticBufferDescInfo ParseStaticBufferDesc(const u32 desc) {
91 return {desc};
92}
93
94/**
95 * @brief Creates a header describing a buffer to be sent over PXI.
96 * @param size Size of the buffer. Max 0x00FFFFFF.
97 * @param buffer_id The Id of the buffer. Max 0xF.
98 * @param is_read_only true if the buffer is read-only. If false, the buffer is considered to have
99 * read-write access.
100 * @return The created PXI buffer header.
101 *
102 * The next value is a phys-address of a table located in the BASE memregion.
103 */
104inline u32 PXIBufferDesc(u32 size, unsigned buffer_id, bool is_read_only) {
105 u32 type = PXIBuffer;
106 if (is_read_only)
107 type |= 0x2;
108 return type | (size << 8) | ((buffer_id & 0xF) << 4);
109}
110
111enum MappedBufferPermissions {
112 R = 1,
113 W = 2,
114 RW = R | W,
115};
116
117constexpr u32 MappedBufferDesc(u32 size, MappedBufferPermissions perms) {
118 return MappedBuffer | (size << 4) | (u32(perms) << 1);
119}
120
121union MappedBufferDescInfo {
122 u32 raw;
123 BitField<4, 28, u32> size;
124 BitField<1, 2, MappedBufferPermissions> perms;
125};
126
127inline MappedBufferDescInfo ParseMappedBufferDesc(const u32 desc) {
128 return {desc};
129}
130
131inline DescriptorType GetDescriptorType(u32 descriptor) {
132 // Note: Those checks must be done in this order
133 if (isHandleDescriptor(descriptor))
134 return (DescriptorType)(descriptor & 0x30);
135
136 // handle the fact that the following descriptors can have rights
137 if (descriptor & MappedBuffer)
138 return MappedBuffer;
139
140 if (descriptor & PXIBuffer)
141 return PXIBuffer;
142
143 return StaticBuffer;
144}
145
146} // namespace IPC
147
148namespace Kernel { 16namespace Kernel {
149 17
150static const int kCommandHeaderOffset = 0x80; ///< Offset into command buffer of header
151
152/**
153 * Returns a pointer to the command buffer in the current thread's TLS
154 * TODO(Subv): This is not entirely correct, the command buffer should be copied from
155 * the thread's TLS to an intermediate buffer in kernel memory, and then copied again to
156 * the service handler process' memory.
157 * @param offset Optional offset into command buffer
158 * @return Pointer to command buffer
159 */
160inline u32* GetCommandBuffer(const int offset = 0) {
161 return (u32*)Memory::GetPointer(GetCurrentThread()->GetTLSAddress() + kCommandHeaderOffset +
162 offset);
163}
164
165class ClientSession; 18class ClientSession;
166class ClientPort; 19class ClientPort;
167 20
@@ -183,11 +36,13 @@ public:
183 ~ServerSession() override; 36 ~ServerSession() override;
184 37
185 /** 38 /**
186 * Creates a server session. 39 * Creates a server session. The server session can have an optional HLE handler,
187 * @param name Optional name of the server session 40 * which will be invoked to handle the IPC requests that this session receives.
41 * @param name Optional name of the server session.
42 * @param hle_handler Optional HLE handler for this server session.
188 * @return The created server session 43 * @return The created server session
189 */ 44 */
190 static ResultVal<SharedPtr<ServerSession>> Create(std::string name = "Unknown"); 45 static ResultVal<SharedPtr<ServerSession>> Create(std::string name = "Unknown", std::shared_ptr<Service::SessionRequestHandler> hle_handler = nullptr);
191 46
192 std::string GetTypeName() const override { return "ServerSession"; } 47 std::string GetTypeName() const override { return "ServerSession"; }
193 48
@@ -196,17 +51,10 @@ public:
196 51
197 /** 52 /**
198 * Creates a pair of ServerSession and an associated ClientSession. 53 * Creates a pair of ServerSession and an associated ClientSession.
199 * @param client_port ClientPort to which the sessions are connected
200 * @param name Optional name of the ports 54 * @param name Optional name of the ports
201 * @return The created session tuple 55 * @return The created session tuple
202 */ 56 */
203 static std::tuple<SharedPtr<ServerSession>, SharedPtr<ClientSession>> CreateSessionPair(SharedPtr<ClientPort> client_port, const std::string& name = "Unknown"); 57 static std::tuple<SharedPtr<ServerSession>, SharedPtr<ClientSession>> CreateSessionPair(const std::string& name = "Unknown", std::shared_ptr<Service::SessionRequestHandler> hle_handler = nullptr);
204
205 /**
206 * Creates a portless ClientSession and associates it with this ServerSession.
207 * @returns ClientSession The newly created ClientSession.
208 */
209 SharedPtr<ClientSession> CreateClientSession();
210 58
211 /** 59 /**
212 * Handle a sync request from the emulated application. 60 * Handle a sync request from the emulated application.
@@ -221,5 +69,6 @@ public:
221 69
222 std::string name; ///< The name of this session (optional) 70 std::string name; ///< The name of this session (optional)
223 bool signaled; ///< Whether there's new data available to this ServerSession 71 bool signaled; ///< Whether there's new data available to this ServerSession
72 std::shared_ptr<Service::SessionRequestHandler> hle_handler; ///< This session's HLE request handler (optional)
224}; 73};
225} 74}
diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp
index da009df91..e40483c72 100644
--- a/src/core/hle/service/fs/archive.cpp
+++ b/src/core/hle/service/fs/archive.cpp
@@ -23,10 +23,11 @@
23#include "core/file_sys/directory_backend.h" 23#include "core/file_sys/directory_backend.h"
24#include "core/file_sys/file_backend.h" 24#include "core/file_sys/file_backend.h"
25#include "core/hle/hle.h" 25#include "core/hle/hle.h"
26#include "core/hle/kernel/client_session.h"
26#include "core/hle/result.h" 27#include "core/hle/result.h"
28#include "core/hle/service/service.h"
27#include "core/hle/service/fs/archive.h" 29#include "core/hle/service/fs/archive.h"
28#include "core/hle/service/fs/fs_user.h" 30#include "core/hle/service/fs/fs_user.h"
29#include "core/hle/service/service.h"
30#include "core/memory.h" 31#include "core/memory.h"
31 32
32// Specializes std::hash for ArchiveIdCode, so that we can use it in std::unordered_map. 33// Specializes std::hash for ArchiveIdCode, so that we can use it in std::unordered_map.
@@ -92,11 +93,10 @@ File::File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path&
92 93
93File::~File() {} 94File::~File() {}
94 95
95ResultCode File::HandleSyncRequest() { 96ResultCode File::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
96 u32* cmd_buff = Kernel::GetCommandBuffer(); 97 u32* cmd_buff = Kernel::GetCommandBuffer();
97 FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]); 98 FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]);
98 switch (cmd) { 99 switch (cmd) {
99
100 // Read from file... 100 // Read from file...
101 case FileCommand::Read: { 101 case FileCommand::Read: {
102 u64 offset = cmd_buff[1] | ((u64)cmd_buff[2]) << 32; 102 u64 offset = cmd_buff[1] | ((u64)cmd_buff[2]) << 32;
@@ -170,9 +170,11 @@ ResultCode File::HandleSyncRequest() {
170 break; 170 break;
171 } 171 }
172 172
173 case FileCommand::OpenLinkFile: { 173 case FileCommand::OpenLinkFile:
174 {
174 LOG_WARNING(Service_FS, "(STUBBED) File command OpenLinkFile %s", GetName().c_str()); 175 LOG_WARNING(Service_FS, "(STUBBED) File command OpenLinkFile %s", GetName().c_str());
175 cmd_buff[3] = Kernel::g_handle_table.Create(this).ValueOr(INVALID_HANDLE); 176 auto sessions = Kernel::ServerSession::CreateSessionPair(GetName(), shared_from_this());
177 cmd_buff[3] = Kernel::g_handle_table.Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions)).ValueOr(INVALID_HANDLE);
176 break; 178 break;
177 } 179 }
178 180
@@ -193,10 +195,10 @@ ResultCode File::HandleSyncRequest() {
193 LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd); 195 LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd);
194 ResultCode error = UnimplementedFunction(ErrorModule::FS); 196 ResultCode error = UnimplementedFunction(ErrorModule::FS);
195 cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that. 197 cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
196 return ServerSession::HandleSyncRequest(); 198 return RESULT_SUCCESS;
197 } 199 }
198 cmd_buff[1] = RESULT_SUCCESS.raw; // No error 200 cmd_buff[1] = RESULT_SUCCESS.raw; // No error
199 return ServerSession::HandleSyncRequest(); 201 return RESULT_SUCCESS;
200} 202}
201 203
202Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, 204Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
@@ -205,11 +207,10 @@ Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
205 207
206Directory::~Directory() {} 208Directory::~Directory() {}
207 209
208ResultCode Directory::HandleSyncRequest() { 210ResultCode Directory::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
209 u32* cmd_buff = Kernel::GetCommandBuffer(); 211 u32* cmd_buff = Kernel::GetCommandBuffer();
210 DirectoryCommand cmd = static_cast<DirectoryCommand>(cmd_buff[0]); 212 DirectoryCommand cmd = static_cast<DirectoryCommand>(cmd_buff[0]);
211 switch (cmd) { 213 switch (cmd) {
212
213 // Read from directory... 214 // Read from directory...
214 case DirectoryCommand::Read: { 215 case DirectoryCommand::Read: {
215 u32 count = cmd_buff[1]; 216 u32 count = cmd_buff[1];
@@ -236,10 +237,10 @@ ResultCode Directory::HandleSyncRequest() {
236 LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd); 237 LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd);
237 ResultCode error = UnimplementedFunction(ErrorModule::FS); 238 ResultCode error = UnimplementedFunction(ErrorModule::FS);
238 cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that. 239 cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
239 return ServerSession::HandleSyncRequest(); 240 return RESULT_SUCCESS;
240 } 241 }
241 cmd_buff[1] = RESULT_SUCCESS.raw; // No error 242 cmd_buff[1] = RESULT_SUCCESS.raw; // No error
242 return ServerSession::HandleSyncRequest(); 243 return RESULT_SUCCESS;
243} 244}
244 245
245//////////////////////////////////////////////////////////////////////////////////////////////////// 246////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -306,7 +307,7 @@ ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factor
306 return RESULT_SUCCESS; 307 return RESULT_SUCCESS;
307} 308}
308 309
309ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_handle, 310ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
310 const FileSys::Path& path, 311 const FileSys::Path& path,
311 const FileSys::Mode mode) { 312 const FileSys::Mode mode) {
312 ArchiveBackend* archive = GetArchive(archive_handle); 313 ArchiveBackend* archive = GetArchive(archive_handle);
@@ -317,8 +318,8 @@ ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_han
317 if (backend.Failed()) 318 if (backend.Failed())
318 return backend.Code(); 319 return backend.Code();
319 320
320 auto file = Kernel::SharedPtr<File>(new File(backend.MoveFrom(), path)); 321 auto file = std::shared_ptr<File>(new File(backend.MoveFrom(), path));
321 return MakeResult<Kernel::SharedPtr<File>>(std::move(file)); 322 return MakeResult<std::shared_ptr<File>>(std::move(file));
322} 323}
323 324
324ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) { 325ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
@@ -397,7 +398,7 @@ ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
397 } 398 }
398} 399}
399 400
400ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle, 401ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
401 const FileSys::Path& path) { 402 const FileSys::Path& path) {
402 ArchiveBackend* archive = GetArchive(archive_handle); 403 ArchiveBackend* archive = GetArchive(archive_handle);
403 if (archive == nullptr) 404 if (archive == nullptr)
@@ -407,8 +408,8 @@ ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle a
407 if (backend.Failed()) 408 if (backend.Failed())
408 return backend.Code(); 409 return backend.Code();
409 410
410 auto directory = Kernel::SharedPtr<Directory>(new Directory(backend.MoveFrom(), path)); 411 auto directory = std::shared_ptr<Directory>(new Directory(backend.MoveFrom(), path));
411 return MakeResult<Kernel::SharedPtr<Directory>>(std::move(directory)); 412 return MakeResult<std::shared_ptr<Directory>>(std::move(directory));
412} 413}
413 414
414ResultVal<u64> GetFreeBytesInArchive(ArchiveHandle archive_handle) { 415ResultVal<u64> GetFreeBytesInArchive(ArchiveHandle archive_handle) {
diff --git a/src/core/hle/service/fs/archive.h b/src/core/hle/service/fs/archive.h
index 22e659c40..29527ef48 100644
--- a/src/core/hle/service/fs/archive.h
+++ b/src/core/hle/service/fs/archive.h
@@ -41,32 +41,32 @@ enum class MediaType : u32 { NAND = 0, SDMC = 1 };
41 41
42typedef u64 ArchiveHandle; 42typedef u64 ArchiveHandle;
43 43
44class File : public Kernel::ServerSession { 44class File : public SessionRequestHandler, public std::enable_shared_from_this<File> {
45public: 45public:
46 File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path); 46 File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path);
47 ~File(); 47 ~File();
48 48
49 std::string GetName() const override { 49 std::string GetName() const {
50 return "Path: " + path.DebugStr(); 50 return "Path: " + path.DebugStr();
51 } 51 }
52 52
53 ResultCode HandleSyncRequest() override; 53 ResultCode HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
54 54
55 FileSys::Path path; ///< Path of the file 55 FileSys::Path path; ///< Path of the file
56 u32 priority; ///< Priority of the file. TODO(Subv): Find out what this means 56 u32 priority; ///< Priority of the file. TODO(Subv): Find out what this means
57 std::unique_ptr<FileSys::FileBackend> backend; ///< File backend interface 57 std::unique_ptr<FileSys::FileBackend> backend; ///< File backend interface
58}; 58};
59 59
60class Directory : public Kernel::ServerSession { 60class Directory : public SessionRequestHandler {
61public: 61public:
62 Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, const FileSys::Path& path); 62 Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, const FileSys::Path& path);
63 ~Directory(); 63 ~Directory();
64 64
65 std::string GetName() const override { 65 std::string GetName() const {
66 return "Directory: " + path.DebugStr(); 66 return "Directory: " + path.DebugStr();
67 } 67 }
68 68
69 ResultCode HandleSyncRequest() override; 69 ResultCode HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
70 70
71 FileSys::Path path; ///< Path of the directory 71 FileSys::Path path; ///< Path of the directory
72 std::unique_ptr<FileSys::DirectoryBackend> backend; ///< File backend interface 72 std::unique_ptr<FileSys::DirectoryBackend> backend; ///< File backend interface
@@ -99,9 +99,9 @@ ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factor
99 * @param archive_handle Handle to an open Archive object 99 * @param archive_handle Handle to an open Archive object
100 * @param path Path to the File inside of the Archive 100 * @param path Path to the File inside of the Archive
101 * @param mode Mode under which to open the File 101 * @param mode Mode under which to open the File
102 * @return The opened File object as a Session 102 * @return The opened File object
103 */ 103 */
104ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_handle, 104ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
105 const FileSys::Path& path, 105 const FileSys::Path& path,
106 const FileSys::Mode mode); 106 const FileSys::Mode mode);
107 107
@@ -178,9 +178,9 @@ ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
178 * Open a Directory from an Archive 178 * Open a Directory from an Archive
179 * @param archive_handle Handle to an open Archive object 179 * @param archive_handle Handle to an open Archive object
180 * @param path Path to the Directory inside of the Archive 180 * @param path Path to the Directory inside of the Archive
181 * @return The opened Directory object as a Session 181 * @return The opened Directory object
182 */ 182 */
183ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle, 183ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
184 const FileSys::Path& path); 184 const FileSys::Path& path);
185 185
186/** 186/**
diff --git a/src/core/hle/service/fs/fs_user.cpp b/src/core/hle/service/fs/fs_user.cpp
index bb78091f9..a29bce22a 100644
--- a/src/core/hle/service/fs/fs_user.cpp
+++ b/src/core/hle/service/fs/fs_user.cpp
@@ -68,10 +68,12 @@ static void OpenFile(Service::Interface* self) {
68 LOG_DEBUG(Service_FS, "path=%s, mode=%u attrs=%u", file_path.DebugStr().c_str(), mode.hex, 68 LOG_DEBUG(Service_FS, "path=%s, mode=%u attrs=%u", file_path.DebugStr().c_str(), mode.hex,
69 attributes); 69 attributes);
70 70
71 ResultVal<SharedPtr<File>> file_res = OpenFileFromArchive(archive_handle, file_path, mode); 71 ResultVal<std::shared_ptr<File>> file_res = OpenFileFromArchive(archive_handle, file_path, mode);
72 cmd_buff[1] = file_res.Code().raw; 72 cmd_buff[1] = file_res.Code().raw;
73 if (file_res.Succeeded()) { 73 if (file_res.Succeeded()) {
74 cmd_buff[3] = Kernel::g_handle_table.Create((*file_res)->CreateClientSession()).MoveFrom(); 74 std::shared_ptr<File> file = *file_res;
75 auto sessions = ServerSession::CreateSessionPair(file->GetName(), file);
76 cmd_buff[3] = Kernel::g_handle_table.Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions)).MoveFrom();
75 } else { 77 } else {
76 cmd_buff[3] = 0; 78 cmd_buff[3] = 0;
77 LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str()); 79 LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str());
@@ -128,10 +130,12 @@ static void OpenFileDirectly(Service::Interface* self) {
128 } 130 }
129 SCOPE_EXIT({ CloseArchive(*archive_handle); }); 131 SCOPE_EXIT({ CloseArchive(*archive_handle); });
130 132
131 ResultVal<SharedPtr<File>> file_res = OpenFileFromArchive(*archive_handle, file_path, mode); 133 ResultVal<std::shared_ptr<File>> file_res = OpenFileFromArchive(*archive_handle, file_path, mode);
132 cmd_buff[1] = file_res.Code().raw; 134 cmd_buff[1] = file_res.Code().raw;
133 if (file_res.Succeeded()) { 135 if (file_res.Succeeded()) {
134 cmd_buff[3] = Kernel::g_handle_table.Create((*file_res)->CreateClientSession()).MoveFrom(); 136 std::shared_ptr<File> file = *file_res;
137 auto sessions = ServerSession::CreateSessionPair(file->GetName(), file);
138 cmd_buff[3] = Kernel::g_handle_table.Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions)).MoveFrom();
135 } else { 139 } else {
136 cmd_buff[3] = 0; 140 cmd_buff[3] = 0;
137 LOG_ERROR(Service_FS, "failed to get a handle for file %s mode=%u attributes=%u", 141 LOG_ERROR(Service_FS, "failed to get a handle for file %s mode=%u attributes=%u",
@@ -389,10 +393,12 @@ static void OpenDirectory(Service::Interface* self) {
389 LOG_DEBUG(Service_FS, "type=%u size=%u data=%s", static_cast<u32>(dirname_type), dirname_size, 393 LOG_DEBUG(Service_FS, "type=%u size=%u data=%s", static_cast<u32>(dirname_type), dirname_size,
390 dir_path.DebugStr().c_str()); 394 dir_path.DebugStr().c_str());
391 395
392 ResultVal<SharedPtr<Directory>> dir_res = OpenDirectoryFromArchive(archive_handle, dir_path); 396 ResultVal<std::shared_ptr<Directory>> dir_res = OpenDirectoryFromArchive(archive_handle, dir_path);
393 cmd_buff[1] = dir_res.Code().raw; 397 cmd_buff[1] = dir_res.Code().raw;
394 if (dir_res.Succeeded()) { 398 if (dir_res.Succeeded()) {
395 cmd_buff[3] = Kernel::g_handle_table.Create((*dir_res)->CreateClientSession()).MoveFrom(); 399 std::shared_ptr<Directory> directory = *dir_res;
400 auto sessions = ServerSession::CreateSessionPair(directory->GetName(), directory);
401 cmd_buff[3] = Kernel::g_handle_table.Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions)).MoveFrom();
396 } else { 402 } else {
397 LOG_ERROR(Service_FS, "failed to get a handle for directory type=%d size=%d data=%s", 403 LOG_ERROR(Service_FS, "failed to get a handle for directory type=%d size=%d data=%s",
398 dirname_type, dirname_size, dir_path.DebugStr().c_str()); 404 dirname_type, dirname_size, dir_path.DebugStr().c_str());
diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp
index 56e4f8734..c90802455 100644
--- a/src/core/hle/service/service.cpp
+++ b/src/core/hle/service/service.cpp
@@ -4,6 +4,9 @@
4 4
5#include "common/logging/log.h" 5#include "common/logging/log.h"
6#include "common/string_util.h" 6#include "common/string_util.h"
7
8#include "core/hle/kernel/server_port.h"
9#include "core/hle/service/service.h"
7#include "core/hle/service/ac_u.h" 10#include "core/hle/service/ac_u.h"
8#include "core/hle/service/act_a.h" 11#include "core/hle/service/act_a.h"
9#include "core/hle/service/act_u.h" 12#include "core/hle/service/act_u.h"
@@ -41,8 +44,8 @@
41 44
42namespace Service { 45namespace Service {
43 46
44std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports; 47std::unordered_map<std::string, std::tuple<Kernel::SharedPtr<Kernel::ClientPort>, std::shared_ptr<Interface>>> g_kernel_named_ports;
45std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_srv_services; 48std::unordered_map<std::string, std::tuple<Kernel::SharedPtr<Kernel::ClientPort>, std::shared_ptr<Interface>>> g_srv_services;
46 49
47/** 50/**
48 * Creates a function string for logging, complete with the name (or header code, depending 51 * Creates a function string for logging, complete with the name (or header code, depending
@@ -99,13 +102,15 @@ void Interface::Register(const FunctionInfo* functions, size_t n) {
99// Module interface 102// Module interface
100 103
101static void AddNamedPort(Interface* interface_) { 104static void AddNamedPort(Interface* interface_) {
102 auto client_port = Kernel::ClientPort::CreateForHLE(interface_->GetMaxSessions(), std::shared_ptr<Interface>(interface_)); 105 auto ports = Kernel::ServerPort::CreatePortPair(interface_->GetMaxSessions(), interface_->GetPortName());
103 g_kernel_named_ports.emplace(interface_->GetPortName(), client_port); 106 auto client_port = std::get<Kernel::SharedPtr<Kernel::ClientPort>>(ports);
107 g_kernel_named_ports.emplace(interface_->GetPortName(), std::make_tuple(client_port, std::shared_ptr<Interface>(interface_)));
104} 108}
105 109
106void AddService(Interface* interface_) { 110void AddService(Interface* interface_) {
107 auto client_port = Kernel::ClientPort::CreateForHLE(interface_->GetMaxSessions(), std::shared_ptr<Interface>(interface_)); 111 auto ports = Kernel::ServerPort::CreatePortPair(interface_->GetMaxSessions(), interface_->GetPortName());
108 g_srv_services.emplace(interface_->GetPortName(), client_port); 112 auto client_port = std::get<Kernel::SharedPtr<Kernel::ClientPort>>(ports);
113 g_srv_services.emplace(interface_->GetPortName(), std::make_tuple(client_port, std::shared_ptr<Interface>(interface_)));
109} 114}
110 115
111/// Initialize ServiceManager 116/// Initialize ServiceManager
diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h
index e2d04450a..dd268f39c 100644
--- a/src/core/hle/service/service.h
+++ b/src/core/hle/service/service.h
@@ -10,8 +10,163 @@
10#include <boost/container/flat_map.hpp> 10#include <boost/container/flat_map.hpp>
11#include "common/common_types.h" 11#include "common/common_types.h"
12#include "core/hle/kernel/client_port.h" 12#include "core/hle/kernel/client_port.h"
13#include "core/hle/kernel/server_session.h" 13#include "core/hle/kernel/thread.h"
14#include "core/hle/result.h" 14#include "core/hle/result.h"
15#include "core/memory.h"
16
17namespace Kernel {
18class ServerSession;
19
20// TODO(Subv): Move these declarations out of here
21static const int kCommandHeaderOffset = 0x80; ///< Offset into command buffer of header
22
23/**
24 * Returns a pointer to the command buffer in the current thread's TLS
25 * TODO(Subv): This is not entirely correct, the command buffer should be copied from
26 * the thread's TLS to an intermediate buffer in kernel memory, and then copied again to
27 * the service handler process' memory.
28 * @param offset Optional offset into command buffer
29 * @return Pointer to command buffer
30 */
31inline u32* GetCommandBuffer(const int offset = 0) {
32 return (u32*)Memory::GetPointer(GetCurrentThread()->GetTLSAddress() + kCommandHeaderOffset +
33 offset);
34}
35}
36
37// TODO(Subv): Move this namespace out of here
38namespace IPC {
39
40enum DescriptorType : u32 {
41 // Buffer related desciptors types (mask : 0x0F)
42 StaticBuffer = 0x02,
43 PXIBuffer = 0x04,
44 MappedBuffer = 0x08,
45 // Handle related descriptors types (mask : 0x30, but need to check for buffer related
46 // descriptors first )
47 CopyHandle = 0x00,
48 MoveHandle = 0x10,
49 CallingPid = 0x20,
50};
51
52/**
53* @brief Creates a command header to be used for IPC
54* @param command_id ID of the command to create a header for.
55* @param normal_params Size of the normal parameters in words. Up to 63.
56* @param translate_params_size Size of the translate parameters in words. Up to 63.
57* @return The created IPC header.
58*
59* Normal parameters are sent directly to the process while the translate parameters might go
60* through modifications and checks by the kernel.
61* The translate parameters are described by headers generated with the IPC::*Desc functions.
62*
63* @note While #normal_params is equivalent to the number of normal parameters,
64* #translate_params_size includes the size occupied by the translate parameters headers.
65*/
66constexpr u32 MakeHeader(u16 command_id, unsigned int normal_params,
67 unsigned int translate_params_size) {
68 return (u32(command_id) << 16) | ((u32(normal_params) & 0x3F) << 6) |
69 (u32(translate_params_size) & 0x3F);
70}
71
72union Header {
73 u32 raw;
74 BitField<0, 6, u32> translate_params_size;
75 BitField<6, 6, u32> normal_params;
76 BitField<16, 16, u32> command_id;
77};
78
79inline Header ParseHeader(u32 header) {
80 return{ header };
81}
82
83constexpr u32 MoveHandleDesc(u32 num_handles = 1) {
84 return MoveHandle | ((num_handles - 1) << 26);
85}
86
87constexpr u32 CopyHandleDesc(u32 num_handles = 1) {
88 return CopyHandle | ((num_handles - 1) << 26);
89}
90
91constexpr u32 CallingPidDesc() {
92 return CallingPid;
93}
94
95constexpr bool isHandleDescriptor(u32 descriptor) {
96 return (descriptor & 0xF) == 0x0;
97}
98
99constexpr u32 HandleNumberFromDesc(u32 handle_descriptor) {
100 return (handle_descriptor >> 26) + 1;
101}
102
103constexpr u32 StaticBufferDesc(u32 size, u8 buffer_id) {
104 return StaticBuffer | (size << 14) | ((buffer_id & 0xF) << 10);
105}
106
107union StaticBufferDescInfo {
108 u32 raw;
109 BitField<10, 4, u32> buffer_id;
110 BitField<14, 18, u32> size;
111};
112
113inline StaticBufferDescInfo ParseStaticBufferDesc(const u32 desc) {
114 return{ desc };
115}
116
117/**
118* @brief Creates a header describing a buffer to be sent over PXI.
119* @param size Size of the buffer. Max 0x00FFFFFF.
120* @param buffer_id The Id of the buffer. Max 0xF.
121* @param is_read_only true if the buffer is read-only. If false, the buffer is considered to have
122* read-write access.
123* @return The created PXI buffer header.
124*
125* The next value is a phys-address of a table located in the BASE memregion.
126*/
127inline u32 PXIBufferDesc(u32 size, unsigned buffer_id, bool is_read_only) {
128 u32 type = PXIBuffer;
129 if (is_read_only)
130 type |= 0x2;
131 return type | (size << 8) | ((buffer_id & 0xF) << 4);
132}
133
134enum MappedBufferPermissions {
135 R = 1,
136 W = 2,
137 RW = R | W,
138};
139
140constexpr u32 MappedBufferDesc(u32 size, MappedBufferPermissions perms) {
141 return MappedBuffer | (size << 4) | (u32(perms) << 1);
142}
143
144union MappedBufferDescInfo {
145 u32 raw;
146 BitField<4, 28, u32> size;
147 BitField<1, 2, MappedBufferPermissions> perms;
148};
149
150inline MappedBufferDescInfo ParseMappedBufferDesc(const u32 desc) {
151 return{ desc };
152}
153
154inline DescriptorType GetDescriptorType(u32 descriptor) {
155 // Note: Those checks must be done in this order
156 if (isHandleDescriptor(descriptor))
157 return (DescriptorType)(descriptor & 0x30);
158
159 // handle the fact that the following descriptors can have rights
160 if (descriptor & MappedBuffer)
161 return MappedBuffer;
162
163 if (descriptor & PXIBuffer)
164 return PXIBuffer;
165
166 return StaticBuffer;
167}
168
169} // namespace IPC
15 170
16//////////////////////////////////////////////////////////////////////////////////////////////////// 171////////////////////////////////////////////////////////////////////////////////////////////////////
17// Namespace Service 172// Namespace Service
@@ -21,8 +176,22 @@ namespace Service {
21static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters) 176static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
22static const u32 DefaultMaxSessions = 10; ///< Arbitrary default number of maximum connections to an HLE port 177static const u32 DefaultMaxSessions = 10; ///< Arbitrary default number of maximum connections to an HLE port
23 178
179/// TODO(Subv): Write documentation for this class
180class SessionRequestHandler {
181public:
182 /**
183 * Dispatches and handles a sync request from the emulated application.
184 * @param server_session The ServerSession that was triggered for this sync request,
185 * it should be used to differentiate which client (As in ClientSession) we're answering to.
186 * TODO(Subv): Make a HandleSyncRequestParent function that is called from the outside and does { ReturnIfError(Translate()); HandleSyncRequest(); }
187 * The Translate() function would copy the command buffer from the ServerSession thread's TLS into a temporary buffer, and pass it to HandleSyncRequest.
188 * TODO(Subv): HandleSyncRequest's return type should be void.
189 */
190 virtual ResultCode HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) = 0;
191};
192
24/// Interface to a CTROS service 193/// Interface to a CTROS service
25class Interface { 194class Interface : public SessionRequestHandler {
26public: 195public:
27 std::string GetName() const { 196 std::string GetName() const {
28 return GetPortName(); 197 return GetPortName();
@@ -56,7 +225,7 @@ public:
56 return "[UNKNOWN SERVICE PORT]"; 225 return "[UNKNOWN SERVICE PORT]";
57 } 226 }
58 227
59 ResultCode HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session); 228 ResultCode HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
60 229
61protected: 230protected:
62 /** 231 /**
@@ -88,9 +257,9 @@ void Init();
88void Shutdown(); 257void Shutdown();
89 258
90/// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC. 259/// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC.
91extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports; 260extern std::unordered_map<std::string, std::tuple<Kernel::SharedPtr<Kernel::ClientPort>, std::shared_ptr<Interface>>> g_kernel_named_ports;
92/// Map of services registered with the "srv:" service, retrieved using GetServiceHandle. 261/// Map of services registered with the "srv:" service, retrieved using GetServiceHandle.
93extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_srv_services; 262extern std::unordered_map<std::string, std::tuple<Kernel::SharedPtr<Kernel::ClientPort>, std::shared_ptr<Interface>>> g_srv_services;
94 263
95/// Adds a service to the services table 264/// Adds a service to the services table
96void AddService(Interface* interface_); 265void AddService(Interface* interface_);
diff --git a/src/core/hle/service/srv.cpp b/src/core/hle/service/srv.cpp
index eb2e06041..6731afc22 100644
--- a/src/core/hle/service/srv.cpp
+++ b/src/core/hle/service/srv.cpp
@@ -10,6 +10,7 @@
10#include "core/hle/kernel/client_session.h" 10#include "core/hle/kernel/client_session.h"
11#include "core/hle/kernel/event.h" 11#include "core/hle/kernel/event.h"
12#include "core/hle/service/srv.h" 12#include "core/hle/service/srv.h"
13#include "core/hle/kernel/server_session.h"
13 14
14//////////////////////////////////////////////////////////////////////////////////////////////////// 15////////////////////////////////////////////////////////////////////////////////////////////////////
15// Namespace SRV 16// Namespace SRV
@@ -85,13 +86,18 @@ static void GetServiceHandle(Service::Interface* self) {
85 auto it = Service::g_srv_services.find(port_name); 86 auto it = Service::g_srv_services.find(port_name);
86 87
87 if (it != Service::g_srv_services.end()) { 88 if (it != Service::g_srv_services.end()) {
88 auto client_port = it->second; 89 auto client_port = std::get<Kernel::SharedPtr<Kernel::ClientPort>>(it->second);
90 // The hle_handler will be nullptr if this port was registered by the emulated
91 // application by means of srv:RegisterService.
92 auto hle_handler = std::get<std::shared_ptr<Service::Interface>>(it->second);
89 93
90 // Create a new session pair 94 // Create a new session pair
91 auto sessions = Kernel::ServerSession::CreateSessionPair(client_port, port_name); 95 auto sessions = Kernel::ServerSession::CreateSessionPair(port_name, hle_handler);
92 auto client_session = std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions); 96 auto client_session = std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions);
93 auto server_session = std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions); 97 auto server_session = std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions);
94 98
99 // TODO(Subv): Wait the current thread until the ServerPort calls AcceptSession.
100
95 // Add the server session to the port's queue 101 // Add the server session to the port's queue
96 client_port->AddWaitingSession(server_session); 102 client_port->AddWaitingSession(server_session);
97 103
diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp
index be03e53bc..6d990b5f2 100644
--- a/src/core/hle/svc.cpp
+++ b/src/core/hle/svc.cpp
@@ -21,6 +21,7 @@
21#include "core/hle/kernel/resource_limit.h" 21#include "core/hle/kernel/resource_limit.h"
22#include "core/hle/kernel/semaphore.h" 22#include "core/hle/kernel/semaphore.h"
23#include "core/hle/kernel/server_port.h" 23#include "core/hle/kernel/server_port.h"
24#include "core/hle/kernel/server_session.h"
24#include "core/hle/kernel/shared_memory.h" 25#include "core/hle/kernel/shared_memory.h"
25#include "core/hle/kernel/thread.h" 26#include "core/hle/kernel/thread.h"
26#include "core/hle/kernel/timer.h" 27#include "core/hle/kernel/timer.h"
@@ -223,13 +224,18 @@ static ResultCode ConnectToPort(Handle* out_handle, const char* port_name) {
223 return ERR_NOT_FOUND; 224 return ERR_NOT_FOUND;
224 } 225 }
225 226
226 auto client_port = it->second; 227 auto client_port = std::get<Kernel::SharedPtr<Kernel::ClientPort>>(it->second);
228 // The hle_handler will be nullptr if this port was registered by the emulated
229 // application by means of svcCreatePort with a defined name.
230 auto hle_handler = std::get<std::shared_ptr<Service::Interface>>(it->second);
227 231
228 // Create a new session pair 232 // Create a new session pair
229 auto sessions = Kernel::ServerSession::CreateSessionPair(client_port, port_name); 233 auto sessions = Kernel::ServerSession::CreateSessionPair(port_name, hle_handler);
230 auto client_session = std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions); 234 auto client_session = std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions);
231 auto server_session = std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions); 235 auto server_session = std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions);
232 236
237 // TODO(Subv): Wait the current thread until the ServerPort calls AcceptSession.
238
233 // Add the server session to the port's queue 239 // Add the server session to the port's queue
234 client_port->AddWaitingSession(server_session); 240 client_port->AddWaitingSession(server_session);
235 241
@@ -247,6 +253,7 @@ static ResultCode SendSyncRequest(Handle handle) {
247 253
248 LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s)", handle, session->GetName().c_str()); 254 LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s)", handle, session->GetName().c_str());
249 255
256 // TODO(Subv): Wait the current thread and reschedule if this request is not going to be handled by HLE code.
250 return session->HandleSyncRequest(); 257 return session->HandleSyncRequest();
251} 258}
252 259