2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::sf::hipc {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-10-04 21:54:13 +00:00
|
|
|
ALWAYS_INLINE Result ReceiveImpl(os::NativeHandle session_handle, void *message_buf, size_t message_buf_size) {
|
2019-12-09 11:57:37 +00:00
|
|
|
s32 unused_index;
|
2021-10-04 21:54:13 +00:00
|
|
|
if (message_buf == svc::GetThreadLocalRegion()->message_buffer) {
|
2020-02-23 07:05:14 +00:00
|
|
|
/* Consider: AMS_ABORT_UNLESS(message_buf_size == TlsMessageBufferSize); */
|
2021-10-04 21:54:13 +00:00
|
|
|
return svc::ReplyAndReceive(&unused_index, &session_handle, 1, svc::InvalidHandle, std::numeric_limits<u64>::max());
|
2019-12-09 11:57:37 +00:00
|
|
|
} else {
|
2021-10-04 21:54:13 +00:00
|
|
|
return svc::ReplyAndReceiveWithUserBuffer(&unused_index, reinterpret_cast<uintptr_t>(message_buf), message_buf_size, &session_handle, 1, svc::InvalidHandle, std::numeric_limits<u64>::max());
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 21:54:13 +00:00
|
|
|
ALWAYS_INLINE Result ReplyImpl(os::NativeHandle session_handle, void *message_buf, size_t message_buf_size) {
|
2019-12-09 11:57:37 +00:00
|
|
|
s32 unused_index;
|
2021-10-04 21:54:13 +00:00
|
|
|
if (message_buf == svc::GetThreadLocalRegion()->message_buffer) {
|
2020-02-23 07:05:14 +00:00
|
|
|
/* Consider: AMS_ABORT_UNLESS(message_buf_size == TlsMessageBufferSize); */
|
2021-10-04 21:54:13 +00:00
|
|
|
return svc::ReplyAndReceive(&unused_index, &session_handle, 0, session_handle, 0);
|
2019-12-09 11:57:37 +00:00
|
|
|
} else {
|
2021-10-04 21:54:13 +00:00
|
|
|
return svc::ReplyAndReceiveWithUserBuffer(&unused_index, reinterpret_cast<uintptr_t>(message_buf), message_buf_size, &session_handle, 0, session_handle, 0);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-04 21:54:13 +00:00
|
|
|
void AttachMultiWaitHolderForAccept(os::MultiWaitHolderType *holder, os::NativeHandle port) {
|
2021-10-01 02:00:47 +00:00
|
|
|
return os::InitializeMultiWaitHolder(holder, port);
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:54:13 +00:00
|
|
|
void AttachMultiWaitHolderForReply(os::MultiWaitHolderType *holder, os::NativeHandle request) {
|
2021-10-01 02:00:47 +00:00
|
|
|
return os::InitializeMultiWaitHolder(holder, request);
|
2020-04-08 09:21:35 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:54:13 +00:00
|
|
|
Result Receive(ReceiveResult *out_recv_result, os::NativeHandle session_handle, const cmif::PointerAndSize &message_buffer) {
|
2019-12-09 11:57:37 +00:00
|
|
|
R_TRY_CATCH(ReceiveImpl(session_handle, message_buffer.GetPointer(), message_buffer.GetSize())) {
|
|
|
|
R_CATCH(svc::ResultSessionClosed) {
|
|
|
|
*out_recv_result = ReceiveResult::Closed;
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
R_CATCH(svc::ResultReceiveListBroken) {
|
|
|
|
*out_recv_result = ReceiveResult::NeedsRetry;
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
*out_recv_result = ReceiveResult::Success;
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2021-10-04 21:54:13 +00:00
|
|
|
Result Receive(bool *out_closed, os::NativeHandle session_handle, const cmif::PointerAndSize &message_buffer) {
|
2019-12-09 11:57:37 +00:00
|
|
|
R_TRY_CATCH(ReceiveImpl(session_handle, message_buffer.GetPointer(), message_buffer.GetSize())) {
|
|
|
|
R_CATCH(svc::ResultSessionClosed) {
|
|
|
|
*out_closed = true;
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
*out_closed = false;
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2021-10-04 21:54:13 +00:00
|
|
|
Result Reply(os::NativeHandle session_handle, const cmif::PointerAndSize &message_buffer) {
|
2019-12-09 11:57:37 +00:00
|
|
|
R_TRY_CATCH(ReplyImpl(session_handle, message_buffer.GetPointer(), message_buffer.GetSize())) {
|
|
|
|
R_CONVERT(svc::ResultTimedOut, ResultSuccess())
|
|
|
|
R_CONVERT(svc::ResultSessionClosed, ResultSuccess())
|
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
/* ReplyImpl should *always* return an error. */
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(false);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:54:13 +00:00
|
|
|
Result CreateSession(os::NativeHandle *out_server_handle, os::NativeHandle *out_client_handle) {
|
|
|
|
R_TRY_CATCH(svc::CreateSession(out_server_handle, out_client_handle, 0, 0)) {
|
2019-12-09 11:57:37 +00:00
|
|
|
R_CONVERT(svc::ResultOutOfResource, sf::hipc::ResultOutOfSessions());
|
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|