kern: add SvcCreatePort, SvcConnectToPort

This commit is contained in:
Michael Scire 2020-07-14 02:24:26 -07:00 committed by SciresM
parent 9f79710cb7
commit 93be2ffcba
10 changed files with 338 additions and 11 deletions

View file

@ -47,7 +47,7 @@ namespace ams::kern::svc {
port->Initialize(max_sessions, false, 0);
/* Register the port. */
KPort::Register(port);
R_TRY(KPort::Register(port));
/* Register the handle in the table. */
handle_table.Register(*out_server_handle, std::addressof(port->GetServerPort()));
@ -77,6 +77,43 @@ namespace ams::kern::svc {
return ResultSuccess();
}
Result CreatePort(ams::svc::Handle *out_server, ams::svc::Handle *out_client, int32_t max_sessions, bool is_light, uintptr_t name) {
/* Ensure max sessions is valid. */
R_UNLESS(max_sessions > 0, svc::ResultOutOfRange());
/* Get the current handle table. */
auto &handle_table = GetCurrentProcess().GetHandleTable();
/* Create a new port. */
KPort *port = KPort::Create();
R_UNLESS(port != nullptr, svc::ResultOutOfResource());
/* Initialize the port. */
port->Initialize(max_sessions, is_light, name);
/* Ensure that we clean up the port (and its only references are handle table) on function end. */
ON_SCOPE_EXIT {
port->GetServerPort().Close();
port->GetClientPort().Close();
};
/* Register the port. */
R_TRY(KPort::Register(port));
/* Add the client to the handle table. */
R_TRY(handle_table.Add(out_client, std::addressof(port->GetClientPort())));
/* Ensure that we maintaing a clean handle state on exit. */
auto handle_guard = SCOPE_GUARD { handle_table.Remove(*out_client); };
/* Add the server to the handle table. */
R_TRY(handle_table.Add(out_server, std::addressof(port->GetServerPort())));
/* We succeeded! */
handle_guard.Cancel();
return ResultSuccess();
}
Result ConnectToNamedPort(ams::svc::Handle *out, KUserPointer<const char *> user_name) {
/* Copy the provided name from user memory to kernel memory. */
char name[KObjectName::NameLengthMax] = {};
@ -112,6 +149,39 @@ namespace ams::kern::svc {
return ResultSuccess();
}
Result ConnectToPort(ams::svc::Handle *out, ams::svc::Handle port) {
/* Get the current handle table. */
auto &handle_table = GetCurrentProcess().GetHandleTable();
/* Get the client port. */
KScopedAutoObject client_port = handle_table.GetObject<KClientPort>(port);
R_UNLESS(client_port.IsNotNull(), svc::ResultInvalidHandle());
/* Reserve a handle for the port. */
/* NOTE: Nintendo really does write directly to the output handle here. */
R_TRY(handle_table.Reserve(out));
auto handle_guard = SCOPE_GUARD { handle_table.Unreserve(*out); };
/* Create and register session. */
if (client_port->IsLight()) {
KLightClientSession *session;
R_TRY(client_port->CreateLightSession(std::addressof(session)));
handle_table.Register(*out, session);
session->Close();
} else {
KClientSession *session;
R_TRY(client_port->CreateSession(std::addressof(session)));
handle_table.Register(*out, session);
session->Close();
}
/* We succeeded. */
handle_guard.Cancel();
return ResultSuccess();
}
}
/* ============================= 64 ABI ============================= */
@ -121,7 +191,7 @@ namespace ams::kern::svc {
}
Result CreatePort64(ams::svc::Handle *out_server_handle, ams::svc::Handle *out_client_handle, int32_t max_sessions, bool is_light, ams::svc::Address name) {
MESOSPHERE_PANIC("Stubbed SvcCreatePort64 was called.");
return CreatePort(out_server_handle, out_client_handle, max_sessions, is_light, name);
}
Result ManageNamedPort64(ams::svc::Handle *out_server_handle, KUserPointer<const char *> name, int32_t max_sessions) {
@ -129,7 +199,7 @@ namespace ams::kern::svc {
}
Result ConnectToPort64(ams::svc::Handle *out_handle, ams::svc::Handle port) {
MESOSPHERE_PANIC("Stubbed SvcConnectToPort64 was called.");
return ConnectToPort(out_handle, port);
}
/* ============================= 64From32 ABI ============================= */
@ -139,7 +209,7 @@ namespace ams::kern::svc {
}
Result CreatePort64From32(ams::svc::Handle *out_server_handle, ams::svc::Handle *out_client_handle, int32_t max_sessions, bool is_light, ams::svc::Address name) {
MESOSPHERE_PANIC("Stubbed SvcCreatePort64From32 was called.");
return CreatePort(out_server_handle, out_client_handle, max_sessions, is_light, name);
}
Result ManageNamedPort64From32(ams::svc::Handle *out_server_handle, KUserPointer<const char *> name, int32_t max_sessions) {
@ -147,7 +217,7 @@ namespace ams::kern::svc {
}
Result ConnectToPort64From32(ams::svc::Handle *out_handle, ams::svc::Handle port) {
MESOSPHERE_PANIC("Stubbed SvcConnectToPort64From32 was called.");
return ConnectToPort(out_handle, port);
}
}