2020-02-08 19:53:27 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-02-08 19:53:27 +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 <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern::svc {
|
|
|
|
|
|
|
|
/* ============================= Common ============================= */
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-07-14 09:45:06 +00:00
|
|
|
Result SignalEvent(ams::svc::Handle event_handle) {
|
|
|
|
/* Get the current handle table. */
|
|
|
|
auto &handle_table = GetCurrentProcess().GetHandleTable();
|
2020-02-08 19:53:27 +00:00
|
|
|
|
2020-07-14 09:45:06 +00:00
|
|
|
/* Get the writable event. */
|
2021-09-17 22:31:25 +00:00
|
|
|
KScopedAutoObject event = handle_table.GetObject<KEvent>(event_handle);
|
|
|
|
R_UNLESS(event.IsNotNull(), svc::ResultInvalidHandle());
|
2020-07-14 09:45:06 +00:00
|
|
|
|
2021-09-17 22:31:25 +00:00
|
|
|
return event->Signal();
|
2020-07-14 09:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ClearEvent(ams::svc::Handle event_handle) {
|
|
|
|
/* Get the current handle table. */
|
|
|
|
auto &handle_table = GetCurrentProcess().GetHandleTable();
|
|
|
|
|
|
|
|
/* Try to clear the writable event. */
|
|
|
|
{
|
2021-09-17 22:31:25 +00:00
|
|
|
KScopedAutoObject event = handle_table.GetObject<KEvent>(event_handle);
|
|
|
|
if (event.IsNotNull()) {
|
|
|
|
return event->Clear();
|
2020-07-14 09:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to clear the readable event. */
|
|
|
|
{
|
|
|
|
KScopedAutoObject readable_event = handle_table.GetObject<KReadableEvent>(event_handle);
|
|
|
|
if (readable_event.IsNotNull()) {
|
2021-10-25 03:41:38 +00:00
|
|
|
if (auto * const interrupt_event = readable_event->DynamicCast<KInterruptEvent *>(); interrupt_event != nullptr) {
|
|
|
|
return interrupt_event->Clear();
|
|
|
|
} else {
|
|
|
|
return readable_event->Clear();
|
|
|
|
}
|
2020-07-14 09:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc::ResultInvalidHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result CreateEvent(ams::svc::Handle *out_write, ams::svc::Handle *out_read) {
|
|
|
|
/* Get the current process and handle table. */
|
|
|
|
auto &process = GetCurrentProcess();
|
|
|
|
auto &handle_table = process.GetHandleTable();
|
|
|
|
|
2021-09-18 05:01:58 +00:00
|
|
|
/* Declare the event we're going to allocate. */
|
|
|
|
KEvent *event;
|
|
|
|
|
2020-07-14 09:45:06 +00:00
|
|
|
/* Reserve a new event from the process resource limit. */
|
|
|
|
KScopedResourceReservation event_reservation(std::addressof(process), ams::svc::LimitableResource_EventCountMax);
|
2021-09-18 05:01:58 +00:00
|
|
|
if (event_reservation.Succeeded()) {
|
|
|
|
/* Allocate an event normally. */
|
|
|
|
event = KEvent::Create();
|
|
|
|
} else {
|
|
|
|
/* We couldn't reserve an event. Check that we support dynamically expanding the resource limit. */
|
|
|
|
R_UNLESS(process.GetResourceLimit() == std::addressof(Kernel::GetSystemResourceLimit()), svc::ResultLimitReached());
|
|
|
|
R_UNLESS(KTargetSystem::IsDynamicResourceLimitsEnabled(), svc::ResultLimitReached());
|
|
|
|
|
|
|
|
/* Try to allocate an event from unused slab memory. */
|
|
|
|
event = KEvent::CreateFromUnusedSlabMemory();
|
|
|
|
R_UNLESS(event != nullptr, svc::ResultLimitReached());
|
|
|
|
|
|
|
|
/* We successfully allocated an event, so add the object we allocated to the resource limit. */
|
|
|
|
Kernel::GetSystemResourceLimit().Add(ams::svc::LimitableResource_EventCountMax, 1);
|
|
|
|
}
|
2020-07-14 09:45:06 +00:00
|
|
|
|
2021-09-18 05:01:58 +00:00
|
|
|
/* Check that we successfully created an event. */
|
2020-07-14 09:45:06 +00:00
|
|
|
R_UNLESS(event != nullptr, svc::ResultOutOfResource());
|
|
|
|
|
|
|
|
/* Initialize the event. */
|
|
|
|
event->Initialize();
|
|
|
|
|
2020-07-20 03:06:21 +00:00
|
|
|
/* Commit the event reservation. */
|
|
|
|
event_reservation.Commit();
|
|
|
|
|
2020-07-14 09:45:06 +00:00
|
|
|
/* Ensure that we clean up the event (and its only references are handle table) on function end. */
|
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
event->GetReadableEvent().Close();
|
2021-09-17 22:31:25 +00:00
|
|
|
event->Close();
|
2020-07-14 09:45:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Register the event. */
|
2020-12-01 21:49:30 +00:00
|
|
|
KEvent::Register(event);
|
2020-07-14 09:45:06 +00:00
|
|
|
|
2021-09-17 22:31:25 +00:00
|
|
|
/* Add the event to the handle table. */
|
|
|
|
R_TRY(handle_table.Add(out_write, event));
|
2020-07-14 09:45:06 +00:00
|
|
|
|
|
|
|
/* Ensure that we maintaing a clean handle state on exit. */
|
|
|
|
auto handle_guard = SCOPE_GUARD { handle_table.Remove(*out_write); };
|
|
|
|
|
|
|
|
/* Add the readable event to the handle table. */
|
|
|
|
R_TRY(handle_table.Add(out_read, std::addressof(event->GetReadableEvent())));
|
|
|
|
|
|
|
|
/* We succeeded! */
|
|
|
|
handle_guard.Cancel();
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
2020-02-08 19:53:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================= 64 ABI ============================= */
|
|
|
|
|
|
|
|
Result SignalEvent64(ams::svc::Handle event_handle) {
|
2020-07-14 09:45:06 +00:00
|
|
|
return SignalEvent(event_handle);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ClearEvent64(ams::svc::Handle event_handle) {
|
2020-07-14 09:45:06 +00:00
|
|
|
return ClearEvent(event_handle);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result CreateEvent64(ams::svc::Handle *out_write_handle, ams::svc::Handle *out_read_handle) {
|
2020-07-14 09:45:06 +00:00
|
|
|
return CreateEvent(out_write_handle, out_read_handle);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================= 64From32 ABI ============================= */
|
|
|
|
|
|
|
|
Result SignalEvent64From32(ams::svc::Handle event_handle) {
|
2020-07-14 09:45:06 +00:00
|
|
|
return SignalEvent(event_handle);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ClearEvent64From32(ams::svc::Handle event_handle) {
|
2020-07-14 09:45:06 +00:00
|
|
|
return ClearEvent(event_handle);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result CreateEvent64From32(ams::svc::Handle *out_write_handle, ams::svc::Handle *out_read_handle) {
|
2020-07-14 09:45:06 +00:00
|
|
|
return CreateEvent(out_write_handle, out_read_handle);
|
2020-02-08 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 19:56:13 +00:00
|
|
|
}
|