sysupdater: implement (untested) rest of the api

This commit is contained in:
Michael Scire 2020-06-26 19:22:50 -07:00 committed by SciresM
parent f223c27bb0
commit 73a6aeed15
22 changed files with 957 additions and 57 deletions

View file

@ -29,6 +29,7 @@
#include <stratosphere/os/os_condition_variable.hpp>
#include <stratosphere/os/os_sdk_mutex.hpp>
#include <stratosphere/os/os_rw_lock.hpp>
#include <stratosphere/os/os_transfer_memory.hpp>
#include <stratosphere/os/os_semaphore.hpp>
#include <stratosphere/os/os_event.hpp>
#include <stratosphere/os/os_system_event.hpp>

View file

@ -70,6 +70,11 @@ namespace ams::os {
return h;
}
void Detach() {
const Handle h = this->Move();
AMS_UNUSED(h);
}
void Reset(Handle h) {
ManagedHandle(h).Swap(*this);
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_transfer_memory_types.hpp>
#include <stratosphere/os/os_transfer_memory_api.hpp>
namespace ams::os {
class TransferMemory {
NON_COPYABLE(TransferMemory);
NON_MOVEABLE(TransferMemory);
private:
TransferMemoryType tmem;
public:
constexpr TransferMemory() : tmem{ .state = TransferMemoryType::State_NotInitialized } {
/* ... */
}
TransferMemory(void *address, size_t size, MemoryPermission perm) {
R_ABORT_UNLESS(CreateTransferMemory(std::addressof(this->tmem), address, size, perm));
}
TransferMemory(size_t size, Handle handle, bool managed) {
this->Attach(size, handle, managed);
}
~TransferMemory() {
if (this->tmem.state == TransferMemoryType::State_NotInitialized) {
return;
}
DestroyTransferMemory(std::addressof(this->tmem));
}
void Attach(size_t size, Handle handle, bool managed) {
AttachTransferMemory(std::addressof(this->tmem), size, handle, managed);
}
Handle Detach() {
return DetachTransferMemory(std::addressof(this->tmem));
}
Result Map(void **out, MemoryPermission owner_perm) {
return MapTransferMemory(out, std::addressof(this->tmem), owner_perm);
}
void Unmap() {
UnmapTransferMemory(std::addressof(this->tmem));
}
operator TransferMemoryType &() {
return this->tmem;
}
operator const TransferMemoryType &() const {
return this->tmem;
}
TransferMemoryType *GetBase() {
return std::addressof(this->tmem);
}
};
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_memory_permission.hpp>
namespace ams::os {
struct TransferMemoryType;
Result CreateTransferMemory(TransferMemoryType *tmem, void *address, size_t size, MemoryPermission perm);
Result AttachTransferMemory(TransferMemoryType *tmem, size_t size, Handle handle, bool managed);
Handle DetachTransferMemory(TransferMemoryType *tmem);
void DestroyTransferMemory(TransferMemoryType *tmem);
Result MapTransferMemory(void **out, TransferMemoryType *tmem, MemoryPermission owner_perm);
void UnmapTransferMemory(TransferMemoryType *tmem);
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/impl/os_internal_critical_section.hpp>
namespace ams::os {
struct TransferMemoryType {
enum State {
State_NotInitialized = 0,
State_Created = 1,
State_Mapped = 2,
State_Detached = 3,
};
u8 state;
bool handle_managed;
bool allocated;
void *address;
size_t size;
Handle handle;
mutable impl::InternalCriticalSectionStorage cs_transfer_memory;
};
static_assert(std::is_trivial<TransferMemoryType>::value);
}

View file

@ -23,6 +23,7 @@
#include <stratosphere/settings/factory/settings_device_certificate.hpp>
#include <stratosphere/settings/system/settings_error_report.hpp>
#include <stratosphere/settings/system/settings_firmware_version.hpp>
#include <stratosphere/settings/system/settings_platform_region.hpp>
#include <stratosphere/settings/system/settings_product_model.hpp>
#include <stratosphere/settings/system/settings_region.hpp>
#include <stratosphere/settings/system/settings_serial_number.hpp>

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/settings/settings_types.hpp>
namespace ams::settings::system {
enum PlatformRegion {
PlatformRegion_Invalid = 0,
PlatformRegion_Global = 1,
PlatformRegion_China = 2,
};
PlatformRegion GetPlatformRegion();
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::os::impl {
class TransferMemoryImpl {
public:
static Result Create(Handle *out, void *address, size_t size, MemoryPermission perm);
static void Close(Handle handle);
static Result Map(void **out, Handle handle, void *address, size_t size, MemoryPermission owner_perm);
static void Unmap(Handle handle, void *address, size_t size);
};
}

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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>
#include "os_transfer_memory_impl.hpp"
namespace ams::os::impl {
namespace {
svc::MemoryPermission ConvertToSvcMemoryPermission(os::MemoryPermission perm) {
switch (perm) {
case os::MemoryPermission_None: return svc::MemoryPermission_None;
case os::MemoryPermission_ReadOnly: return svc::MemoryPermission_Read;
case os::MemoryPermission_WriteOnly: return svc::MemoryPermission_Write;
case os::MemoryPermission_ReadWrite: return svc::MemoryPermission_ReadWrite;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
}
Result TransferMemoryImpl::Create(Handle *out, void *address, size_t size, MemoryPermission perm) {
/* Convert memory permission. */
auto svc_perm = ConvertToSvcMemoryPermission(perm);
/* Create the memory. */
svc::Handle handle;
R_TRY_CATCH(svc::CreateTransferMemory(std::addressof(handle), reinterpret_cast<uintptr_t>(address), size, svc_perm)) {
R_CONVERT(svc::ResultOutOfHandles, os::ResultOutOfHandles())
R_CONVERT(svc::ResultOutOfResource, os::ResultOutOfTransferMemory())
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
*out = handle;
return ResultSuccess();
}
void TransferMemoryImpl::Close(Handle handle) {
R_ABORT_UNLESS(svc::CloseHandle(handle));
}
Result TransferMemoryImpl::Map(void **out, Handle handle, void *address, size_t size, MemoryPermission owner_perm) {
AMS_ASSERT(address != nullptr);
/* Convert memory permission. */
auto svc_owner_perm = ConvertToSvcMemoryPermission(owner_perm);
/* Map the memory. */
R_TRY_CATCH(svc::MapTransferMemory(handle, reinterpret_cast<uintptr_t>(address), size, svc_owner_perm)) {
R_CONVERT(svc::ResultInvalidHandle, os::ResultInvalidHandle())
R_CONVERT(svc::ResultInvalidSize, os::ResultInvalidTransferMemorySize())
R_CONVERT(svc::ResultInvalidState, os::ResultInvalidTransferMemoryState())
R_CONVERT(svc::ResultInvalidCurrentMemory, os::ResultInvalidCurrentMemoryState())
R_CONVERT(svc::ResultInvalidMemoryRegion, os::ResultInvalidCurrentMemoryState())
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
*out = address;
return ResultSuccess();
}
void TransferMemoryImpl::Unmap(Handle handle, void *address, size_t size) {
R_ABORT_UNLESS(svc::UnmapTransferMemory(handle, reinterpret_cast<uintptr_t>(address), size));
}
}

View file

@ -0,0 +1,185 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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>
#include "impl/os_thread_manager.hpp"
#include "impl/os_transfer_memory_impl.hpp"
namespace ams::os {
namespace {
Result MapTransferMemoryWithAddressUnsafe(TransferMemoryType *tmem, void *address, os::MemoryPermission owner_perm) {
/* Map the transfer memory. */
void *mapped_address = nullptr;
R_TRY(impl::TransferMemoryImpl::Map(std::addressof(mapped_address), tmem->handle, address, tmem->size, owner_perm));
/* Set fields now that we've mapped. */
tmem->address = mapped_address;
tmem->state = TransferMemoryType::State_Mapped;
return ResultSuccess();
}
inline void SetupTransferMemoryType(TransferMemoryType *tmem, size_t size, Handle handle, bool managed) {
/* Set members. */
tmem->handle = handle;
tmem->size = size;
tmem->address = nullptr;
tmem->allocated = false;
/* Set managed. */
tmem->handle_managed = managed;
/* Create the critical section. */
new (GetPointer(tmem->cs_transfer_memory)) impl::InternalCriticalSection;
}
}
Result CreateTransferMemory(TransferMemoryType *tmem, void *address, size_t size, MemoryPermission perm) {
/* Validate pre-conditions. */
AMS_ASSERT(size > 0);
AMS_ASSERT(util::IsAligned(size, os::MemoryPageSize));
AMS_ASSERT(address != nullptr);
AMS_ASSERT(util::IsAligned(reinterpret_cast<uintptr_t>(address), os::MemoryPageSize));
/* Create the memory. */
Handle handle;
R_TRY(impl::TransferMemoryImpl::Create(std::addressof(handle), address, size, perm));
/* Setup the object. */
SetupTransferMemoryType(tmem, size, handle, true);
return ResultSuccess();
}
Result AttachTransferMemory(TransferMemoryType *tmem, size_t size, Handle handle, bool managed) {
AMS_ASSERT(size > 0);
AMS_ASSERT(util::IsAligned(size, os::MemoryPageSize));
AMS_ASSERT(handle != svc::InvalidHandle);
/* Setup the object. */
SetupTransferMemoryType(tmem, size, handle, managed);
return ResultSuccess();
}
Handle DetachTransferMemory(TransferMemoryType *tmem) {
AMS_ASSERT(tmem->state == TransferMemoryType::State_Created);
/* Set state to detached. */
tmem->state = TransferMemoryType::State_Detached;
/* Clear handle. */
Handle handle = tmem->handle;
tmem->handle = svc::InvalidHandle;
tmem->handle_managed = false;
return handle;
}
void DestroyTransferMemory(TransferMemoryType *tmem) {
/* Unmap the transfer memory, if required. */
if (tmem->state == TransferMemoryType::State_Mapped) {
UnmapTransferMemory(tmem);
}
/* Check the state is valid. */
AMS_ASSERT(tmem->state == TransferMemoryType::State_Created || tmem->state == TransferMemoryType::State_Detached);
/* Set state to not initialized. */
tmem->state = TransferMemoryType::State_NotInitialized;
/* Close the handle, if it's managed. */
if (tmem->handle_managed) {
impl::TransferMemoryImpl::Close(tmem->handle);
}
tmem->handle_managed = false;
/* Clear members. */
tmem->address = nullptr;
tmem->size = 0;
tmem->handle = svc::InvalidHandle;
/* Destroy the critical section. */
GetReference(tmem->cs_transfer_memory).~InternalCriticalSection();
}
Result MapTransferMemory(void **out, TransferMemoryType *tmem, MemoryPermission owner_perm) {
/* Lock the current thread, and then the transfer memory. */
std::scoped_lock thread_lk(GetReference(impl::GetCurrentThread()->cs_thread));
std::scoped_lock lk(GetReference(tmem->cs_transfer_memory));
/* Ensure we're in a mappable state. */
AMS_ASSERT(tmem->state == TransferMemoryType::State_Created);
/* Try to map up to 64 times. */
for (int i = 0; i < 64; ++i) {
/* Reserve space to map the memory. */
/* TODO: os::AslrSpaceManager */
void *map_address = ::virtmemReserve(tmem->size);
R_UNLESS(map_address != nullptr, os::ResultOutOfAddressSpace());
/* Mark allocated. */
tmem->allocated = true;
auto alloc_guard = SCOPE_GUARD { tmem->allocated = false; };
/* Try to map. */
R_TRY_CATCH(MapTransferMemoryWithAddressUnsafe(tmem, map_address, owner_perm)) {
/* If we failed to map at the address, retry. */
R_CATCH(os::ResultInvalidCurrentMemoryState) { continue; }
} R_END_TRY_CATCH;
/* TODO: Check guard space via aslr manager. */
if (false /* !impl::GetAslrSpaceManager()->CheckGuardSpace(reinterpret_cast<uintptr_t>(tmem->address), tmem->size) */) {
impl::TransferMemoryImpl::Unmap(tmem->handle, tmem->address, tmem->size);
continue;
}
/* We mapped successfully. */
alloc_guard.Cancel();
*out = tmem->address;
return ResultSuccess();
}
/* We failed to map. */
return os::ResultOutOfAddressSpace();
}
void UnmapTransferMemory(TransferMemoryType *tmem) {
/* Lock the memory. */
std::scoped_lock lk(GetReference(tmem->cs_transfer_memory));
/* If the memory isn't mapped, we can't unmap it. */
if (tmem->state != TransferMemoryType::State_Mapped) {
return;
}
/* Unmap the memory. */
impl::TransferMemoryImpl::Unmap(tmem->handle, tmem->address, tmem->size);
/* Unmapped memory is necessarily not allocated. */
if (tmem->allocated) {
tmem->allocated = false;
}
/* Clear the address. */
tmem->address = nullptr;
tmem->state = TransferMemoryType::State_Created;
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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>
#include "settings_platform_region_impl.hpp"
namespace ams::settings::impl {
Result GetPlatformRegion(s32 *out) {
static_assert(sizeof(*out) == sizeof(::SetSysPlatformRegion));
return ::setsysGetPlatformRegion(reinterpret_cast<::SetSysPlatformRegion *>(out));
}
}

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::settings::impl {
Result GetPlatformRegion(s32 *out);
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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>
#include "impl/settings_platform_region_impl.hpp"
namespace ams::settings::system {
PlatformRegion GetPlatformRegion() {
if (hos::GetVersion() >= hos::Version_9_0_0) {
s32 region = 0;
R_ABORT_UNLESS(settings::impl::GetPlatformRegion(std::addressof(region)));
return static_cast<PlatformRegion>(region);
} else {
return PlatformRegion_Global;
}
}
}