From 4d86863f2cd85672df0094b10f6c21f9f44f4562 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 10 Feb 2021 04:22:19 -0800 Subject: [PATCH] htc: ObserverThread (mostly), system now boots + works with htc in bg --- .../source/boot2/boot2_api.cpp | 4 +- .../source/htc/server/htc_htcmisc_impl.cpp | 11 +++++ .../source/htc/server/htc_htcmisc_impl.hpp | 7 ++- .../source/htc/server/htc_observer.cpp | 45 ++++++++++++++++++- .../libstratosphere/source/usb/usb_device.cpp | 2 +- stratosphere/htc/source/htc_main.cpp | 21 +++++++++ 6 files changed, 83 insertions(+), 7 deletions(-) diff --git a/libraries/libstratosphere/source/boot2/boot2_api.cpp b/libraries/libstratosphere/source/boot2/boot2_api.cpp index 2c085abdd..baa539d61 100644 --- a/libraries/libstratosphere/source/boot2/boot2_api.cpp +++ b/libraries/libstratosphere/source/boot2/boot2_api.cpp @@ -34,7 +34,7 @@ namespace ams::boot2 { constexpr size_t NumPreSdCardLaunchPrograms = util::size(PreSdCardLaunchPrograms); constexpr ncm::SystemProgramId AdditionalLaunchPrograms[] = { - ncm::SystemProgramId::Tma, /* tma */ + ncm::SystemProgramId::Htc, /* htc */ /* TODO: should we do boot!use_htc_gen2, with default to on in custom settings? */ ncm::SystemProgramId::Am, /* am */ ncm::SystemProgramId::NvServices, /* nvservices */ ncm::SystemProgramId::NvnFlinger, /* nvnflinger */ @@ -80,7 +80,7 @@ namespace ams::boot2 { constexpr size_t NumAdditionalLaunchPrograms = util::size(AdditionalLaunchPrograms); constexpr ncm::SystemProgramId AdditionalMaintenanceLaunchPrograms[] = { - ncm::SystemProgramId::Tma, /* tma */ + ncm::SystemProgramId::Htc, /* htc */ ncm::SystemProgramId::Am, /* am */ ncm::SystemProgramId::NvServices, /* nvservices */ ncm::SystemProgramId::NvnFlinger, /* nvnflinger */ diff --git a/libraries/libstratosphere/source/htc/server/htc_htcmisc_impl.cpp b/libraries/libstratosphere/source/htc/server/htc_htcmisc_impl.cpp index e2ed1d157..5bd78d167 100644 --- a/libraries/libstratosphere/source/htc/server/htc_htcmisc_impl.cpp +++ b/libraries/libstratosphere/source/htc/server/htc_htcmisc_impl.cpp @@ -195,4 +195,15 @@ namespace ams::htc::server { } } + os::EventType *HtcmiscImpl::GetConnectionEvent() const { + return m_connection_event.GetBase(); + } + + bool HtcmiscImpl::IsConnected() const { + /* Lock ourselves. */ + std::scoped_lock lk(m_connection_mutex); + + return m_connected; + } + } diff --git a/libraries/libstratosphere/source/htc/server/htc_htcmisc_impl.hpp b/libraries/libstratosphere/source/htc/server/htc_htcmisc_impl.hpp index c012a9078..3317bda4c 100644 --- a/libraries/libstratosphere/source/htc/server/htc_htcmisc_impl.hpp +++ b/libraries/libstratosphere/source/htc/server/htc_htcmisc_impl.hpp @@ -33,11 +33,11 @@ namespace ams::htc::server { os::ThreadType m_server_thread; os::Event m_cancel_event; bool m_cancelled; - os::Event m_connection_event; + mutable os::Event m_connection_event; bool m_client_connected; bool m_server_connected; bool m_connected; - os::SdkMutex m_connection_mutex; + mutable os::SdkMutex m_connection_mutex; private: static void ClientThreadEntry(void *arg) { static_cast(arg)->ClientThread(); } static void ServerThreadEntry(void *arg) { static_cast(arg)->ServerThread(); } @@ -47,6 +47,9 @@ namespace ams::htc::server { public: HtcmiscImpl(htclow::HtclowManager *htclow_manager); ~HtcmiscImpl(); + + os::EventType *GetConnectionEvent() const; + bool IsConnected() const; private: void SetClientConnectionEvent(bool en); void SetServerConnectionEvent(bool en); diff --git a/libraries/libstratosphere/source/htc/server/htc_observer.cpp b/libraries/libstratosphere/source/htc/server/htc_observer.cpp index 4e1c6622b..58fc965bb 100644 --- a/libraries/libstratosphere/source/htc/server/htc_observer.cpp +++ b/libraries/libstratosphere/source/htc/server/htc_observer.cpp @@ -29,7 +29,7 @@ namespace ams::htc::server { m_is_service_available(false) { /* Initialize htcs library. */ - AMS_ABORT("htcs::impl::HtcsManagerHolder::AddReference();"); + /* TODO: AMS_ABORT("htcs::impl::HtcsManagerHolder::AddReference();"); */ /* Update our event state. */ this->UpdateEvent(); @@ -69,7 +69,48 @@ namespace ams::htc::server { } void Observer::ObserverThreadBody() { - AMS_ABORT("Observer::ObserverThreadBody"); + /* When we're done observing, clear our state. */ + ON_SCOPE_EXIT { + m_connected = false; + m_is_service_available = false; + this->UpdateEvent(); + }; + + /* Get the events we're waiting on. */ + os::EventType * const stop_event = m_stop_event.GetBase(); + os::EventType * const conn_event = m_misc_impl.GetConnectionEvent(); + os::EventType * const htcs_event = nullptr /* TODO: htcs::impl::HtcsManagerHolder::GetHtcsManager()->GetServiceAvailabilityEvent() */; + + /* Loop until we're asked to stop. */ + while (!m_stopped) { + /* Wait for an event to be signaled. */ + const auto index = os::WaitAny(stop_event, conn_event /*, htcs_event */); + switch (index) { + case 0: + /* Stop event, just break out of the loop. */ + os::ClearEvent(stop_event); + break; + case 1: + /* Connection event, update our connection status. */ + os::ClearEvent(conn_event); + m_connected = m_misc_impl.IsConnected(); + break; + case 2: + /* Htcs event, update our service status. */ + os::ClearEvent(htcs_event); + m_is_service_available = false /* TODO: htcs::impl::HtcsManagerHolder::GetHtcsManager()->IsServiceAvailable() */; + break; + AMS_UNREACHABLE_DEFAULT_CASE(); + } + + /* If the event was our stop event, break. */ + if (index == 0) { + break; + } + + /* Update event status. */ + this->UpdateEvent(); + } } } diff --git a/libraries/libstratosphere/source/usb/usb_device.cpp b/libraries/libstratosphere/source/usb/usb_device.cpp index cbc52439c..8eda9f6be 100644 --- a/libraries/libstratosphere/source/usb/usb_device.cpp +++ b/libraries/libstratosphere/source/usb/usb_device.cpp @@ -274,7 +274,7 @@ namespace ams::usb { Result DsInterface::Finalize() { /* Validate that we have a service. */ - R_ABORT_UNLESS(m_interface != nullptr); + AMS_ABORT_UNLESS(m_interface != nullptr); /* We must be disabled. */ R_UNLESS(!m_client->m_is_enabled, usb::ResultResourceBusy()); diff --git a/stratosphere/htc/source/htc_main.cpp b/stratosphere/htc/source/htc_main.cpp index 92b39ac7e..d3ebba403 100644 --- a/stratosphere/htc/source/htc_main.cpp +++ b/stratosphere/htc/source/htc_main.cpp @@ -42,6 +42,25 @@ namespace ams { using namespace ams; +#define AMS_HTC_USE_FATAL_ERROR 1 + +#if AMS_HTC_USE_FATAL_ERROR + +extern "C" { + + /* Exception handling. */ + alignas(16) u8 __nx_exception_stack[ams::os::MemoryPageSize]; + u64 __nx_exception_stack_size = sizeof(__nx_exception_stack); + void __libnx_exception_handler(ThreadExceptionDump *ctx); + +} + +void __libnx_exception_handler(ThreadExceptionDump *ctx) { + ams::CrashHandler(ctx); +} + +#endif + namespace ams::htc { namespace { @@ -87,6 +106,8 @@ void __appInit(void) { sm::DoWithSession([&]() { R_ABORT_UNLESS(setsysInitialize()); + R_ABORT_UNLESS(setcalInitialize()); + R_ABORT_UNLESS(pscmInitialize()); R_ABORT_UNLESS(fsInitialize()); });