ams: support building unit test programs on windows/linux/macos
This commit is contained in:
@@ -117,7 +117,7 @@ namespace ams::mem::impl::heap {
|
||||
if (real_size == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
AMS_ASSERT(cls < TlsHeapStatic::NumClassInfo);
|
||||
AMS_ASSERT(static_cast<u32>(cls) < TlsHeapStatic::NumClassInfo);
|
||||
return m_tls_heap_central->CacheSmallMemory(cls, align);
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ namespace ams::mem::impl::heap {
|
||||
if (!util::IsAligned(reinterpret_cast<uintptr_t>(ptr), MinimumAlignment)) {
|
||||
return 0;
|
||||
}
|
||||
AMS_ASSERT(cls < TlsHeapStatic::NumClassInfo);
|
||||
AMS_ASSERT(static_cast<u32>(cls) < TlsHeapStatic::NumClassInfo);
|
||||
return TlsHeapStatic::GetChunkSize(cls);
|
||||
} else if (ptr != nullptr) {
|
||||
return m_tls_heap_central->GetAllocationSize(ptr);
|
||||
@@ -151,7 +151,7 @@ namespace ams::mem::impl::heap {
|
||||
|
||||
const auto cls = m_tls_heap_central->GetClassFromPointer(ptr);
|
||||
if (cls >= 0) {
|
||||
AMS_ASSERT(cls < TlsHeapStatic::NumClassInfo);
|
||||
AMS_ASSERT(static_cast<u32>(cls) < TlsHeapStatic::NumClassInfo);
|
||||
if (cls) {
|
||||
return m_tls_heap_central->UncacheSmallMemory(ptr);
|
||||
} else {
|
||||
@@ -266,26 +266,22 @@ namespace ams::mem::impl::heap {
|
||||
return m_tls_heap_central->WalkAllocatedPointers(callback, user_data);
|
||||
}
|
||||
|
||||
errno_t CentralHeap::QueryV(int query, std::va_list vl) {
|
||||
return this->QueryVImpl(query, std::addressof(vl));
|
||||
}
|
||||
|
||||
errno_t CentralHeap::Query(int query, ...) {
|
||||
std::va_list vl;
|
||||
va_start(vl, query);
|
||||
auto err = this->QueryVImpl(query, std::addressof(vl));
|
||||
auto err = this->QueryV(query, vl);
|
||||
va_end(vl);
|
||||
return err;
|
||||
}
|
||||
|
||||
errno_t CentralHeap::QueryVImpl(int _query, std::va_list *vl_ptr) {
|
||||
errno_t CentralHeap::QueryV(int _query, std::va_list vl) {
|
||||
const AllocQuery query = static_cast<AllocQuery>(_query);
|
||||
switch (query) {
|
||||
case AllocQuery_Dump:
|
||||
case AllocQuery_DumpJson:
|
||||
{
|
||||
auto dump_mode = static_cast<DumpMode>(va_arg(*vl_ptr, int));
|
||||
auto fd = va_arg(*vl_ptr, int);
|
||||
auto dump_mode = static_cast<DumpMode>(va_arg(vl, int));
|
||||
auto fd = va_arg(vl, int);
|
||||
if (m_tls_heap_central) {
|
||||
m_tls_heap_central->Dump(dump_mode, fd, query == AllocQuery_DumpJson);
|
||||
}
|
||||
@@ -293,7 +289,7 @@ namespace ams::mem::impl::heap {
|
||||
}
|
||||
case AllocQuery_PageSize:
|
||||
{
|
||||
size_t *out = va_arg(*vl_ptr, size_t *);
|
||||
size_t *out = va_arg(vl, size_t *);
|
||||
if (out) {
|
||||
*out = PageSize;
|
||||
}
|
||||
@@ -304,7 +300,7 @@ namespace ams::mem::impl::heap {
|
||||
case AllocQuery_SystemSize:
|
||||
case AllocQuery_MaxAllocatableSize:
|
||||
{
|
||||
size_t *out = va_arg(*vl_ptr, size_t *);
|
||||
size_t *out = va_arg(vl, size_t *);
|
||||
if (!out) {
|
||||
return 0;
|
||||
}
|
||||
@@ -333,7 +329,7 @@ namespace ams::mem::impl::heap {
|
||||
}
|
||||
case AllocQuery_IsClean:
|
||||
{
|
||||
int *out = va_arg(*vl_ptr, int *);
|
||||
int *out = va_arg(vl, int *);
|
||||
if (out) {
|
||||
*out = !m_tls_heap_central || m_tls_heap_central->IsClean();
|
||||
}
|
||||
@@ -341,7 +337,7 @@ namespace ams::mem::impl::heap {
|
||||
}
|
||||
case AllocQuery_HeapHash:
|
||||
{
|
||||
HeapHash *out = va_arg(*vl_ptr, HeapHash *);
|
||||
HeapHash *out = va_arg(vl, HeapHash *);
|
||||
if (out) {
|
||||
if (m_tls_heap_central) {
|
||||
m_tls_heap_central->CalculateHeapHash(out);
|
||||
@@ -358,37 +354,37 @@ namespace ams::mem::impl::heap {
|
||||
case AllocQuery_SetColor:
|
||||
{
|
||||
/* NOTE: Nintendo does not check that the ptr is not null for this query, even though they do for other queries. */
|
||||
void *ptr = va_arg(*vl_ptr, void *);
|
||||
int color = va_arg(*vl_ptr, int);
|
||||
void *ptr = va_arg(vl, void *);
|
||||
int color = va_arg(vl, int);
|
||||
return m_tls_heap_central->SetColor(ptr, color);
|
||||
}
|
||||
case AllocQuery_GetColor:
|
||||
{
|
||||
/* NOTE: Nintendo does not check that the ptr is not null for this query, even though they do for other queries. */
|
||||
void *ptr = va_arg(*vl_ptr, void *);
|
||||
int *out = va_arg(*vl_ptr, int *);
|
||||
void *ptr = va_arg(vl, void *);
|
||||
int *out = va_arg(vl, int *);
|
||||
return m_tls_heap_central->GetColor(ptr, out);
|
||||
}
|
||||
case AllocQuery_SetName:
|
||||
{
|
||||
/* NOTE: Nintendo does not check that the ptr is not null for this query, even though they do for other queries. */
|
||||
void *ptr = va_arg(*vl_ptr, void *);
|
||||
const char *name = va_arg(*vl_ptr, const char *);
|
||||
void *ptr = va_arg(vl, void *);
|
||||
const char *name = va_arg(vl, const char *);
|
||||
return m_tls_heap_central->SetName(ptr, name);
|
||||
}
|
||||
case AllocQuery_GetName:
|
||||
{
|
||||
/* NOTE: Nintendo does not check that the ptr is not null for this query, even though they do for other queries. */
|
||||
void *ptr = va_arg(*vl_ptr, void *);
|
||||
char *dst = va_arg(*vl_ptr, char *);
|
||||
size_t dst_size = va_arg(*vl_ptr, size_t);
|
||||
void *ptr = va_arg(vl, void *);
|
||||
char *dst = va_arg(vl, char *);
|
||||
size_t dst_size = va_arg(vl, size_t);
|
||||
return m_tls_heap_central->GetName(ptr, dst, dst_size);
|
||||
}
|
||||
case AllocQuery_FreeSizeMapped:
|
||||
case AllocQuery_MaxAllocatableSizeMapped:
|
||||
{
|
||||
/* NOTE: Nintendo does not check that the ptr is not null for this query, even though they do for other queries. */
|
||||
size_t *out = va_arg(*vl_ptr, size_t *);
|
||||
size_t *out = va_arg(vl, size_t *);
|
||||
size_t free_size;
|
||||
size_t max_allocatable_size;
|
||||
auto err = m_tls_heap_central->GetMappedMemStats(std::addressof(free_size), std::addressof(max_allocatable_size));
|
||||
|
||||
@@ -122,7 +122,7 @@ namespace ams::mem::impl::heap {
|
||||
}
|
||||
|
||||
if (const size_t cls = TlsHeapStatic::GetClassFromSize(size); cls != 0) {
|
||||
AMS_ASSERT(cls < TlsHeapStatic::NumClassInfo);
|
||||
AMS_ASSERT(static_cast<u32>(cls) < TlsHeapStatic::NumClassInfo);
|
||||
return tls_heap_cache->m_central->CacheSmallMemory(cls);
|
||||
} else {
|
||||
/* If allocating a huge size, release our cache. */
|
||||
@@ -141,7 +141,7 @@ namespace ams::mem::impl::heap {
|
||||
}
|
||||
|
||||
if (size_t cls = TlsHeapStatic::GetClassFromSize(size); cls != 0) {
|
||||
AMS_ASSERT(cls < TlsHeapStatic::NumClassInfo);
|
||||
AMS_ASSERT(static_cast<u32>(cls) < TlsHeapStatic::NumClassInfo);
|
||||
/* Allocate a chunk. */
|
||||
void *ptr = tls_heap_cache->m_small_mem_lists[cls];
|
||||
if (ptr == nullptr) {
|
||||
@@ -208,7 +208,7 @@ namespace ams::mem::impl::heap {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
AMS_ASSERT(cls < TlsHeapStatic::NumClassInfo);
|
||||
AMS_ASSERT(static_cast<u32>(cls) < TlsHeapStatic::NumClassInfo);
|
||||
return tls_heap_cache->m_central->CacheSmallMemory(cls, align);
|
||||
} else {
|
||||
/* If allocating a huge size, release our cache. */
|
||||
@@ -243,7 +243,7 @@ namespace ams::mem::impl::heap {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
AMS_ASSERT(cls < TlsHeapStatic::NumClassInfo);
|
||||
AMS_ASSERT(static_cast<u32>(cls) < TlsHeapStatic::NumClassInfo);
|
||||
|
||||
/* Allocate a chunk. */
|
||||
void *ptr = tls_heap_cache->m_small_mem_lists[cls];
|
||||
@@ -294,7 +294,7 @@ namespace ams::mem::impl::heap {
|
||||
return tls_heap_cache->m_central->UncacheLargeMemory(ptr);
|
||||
}
|
||||
|
||||
AMS_ASSERT(cls < TlsHeapStatic::NumClassInfo);
|
||||
AMS_ASSERT(static_cast<u32>(cls) < TlsHeapStatic::NumClassInfo);
|
||||
|
||||
if (cls >= 0) {
|
||||
return tls_heap_cache->m_central->UncacheSmallMemory(ptr);
|
||||
@@ -312,7 +312,7 @@ namespace ams::mem::impl::heap {
|
||||
return tls_heap_cache->m_central->UncacheLargeMemory(ptr);
|
||||
}
|
||||
|
||||
AMS_ASSERT(cls < TlsHeapStatic::NumClassInfo);
|
||||
AMS_ASSERT(static_cast<u32>(cls) < TlsHeapStatic::NumClassInfo);
|
||||
|
||||
if (cls >= 0) {
|
||||
*reinterpret_cast<void **>(ptr) = tls_heap_cache->m_small_mem_lists[cls];
|
||||
|
||||
@@ -394,7 +394,7 @@ namespace ams::mem::impl::heap {
|
||||
this->AddToFreeBlockList(span);
|
||||
|
||||
m_num_threads = 1;
|
||||
m_static_thread_quota = std::min((m_span_table.total_pages * TlsHeapStatic::PageSize) / sizeof(void *), 2_MB);
|
||||
m_static_thread_quota = std::min<size_t>((m_span_table.total_pages * TlsHeapStatic::PageSize) / sizeof(void *), 2_MB);
|
||||
m_dynamic_thread_quota = m_static_thread_quota;
|
||||
m_use_virtual_memory = use_virtual_memory;
|
||||
|
||||
@@ -940,10 +940,10 @@ namespace ams::mem::impl::heap {
|
||||
m_physical_page_flags[i] = 2;
|
||||
}
|
||||
} else {
|
||||
const void *set_flag = ::memchr(std::addressof(m_physical_page_flags[i]), 1, idx_end - i);
|
||||
const void *set_flag = util::Memchr(std::addressof(m_physical_page_flags[i]), 1, idx_end - i);
|
||||
if (set_flag) {
|
||||
const uintptr_t set_idx = reinterpret_cast<const u8 *>(set_flag) - m_physical_page_flags;
|
||||
const void *lst_flag = ::memrchr(std::addressof(m_physical_page_flags[set_idx]), 1, idx_end - set_idx);
|
||||
const void *lst_flag = util::Memrchr(std::addressof(m_physical_page_flags[set_idx]), 1, idx_end - set_idx);
|
||||
const uintptr_t lst_idx = (lst_flag) ? (reinterpret_cast<const u8 *>(lst_flag) - m_physical_page_flags + 1) : idx_end;
|
||||
std::memset(std::addressof(m_physical_page_flags[set_idx]), 2, lst_idx - set_idx);
|
||||
}
|
||||
|
||||
@@ -20,27 +20,9 @@ namespace ams::mem::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
constinit os::SdkMutex g_virt_mem_enabled_lock;
|
||||
constinit bool g_virt_mem_enabled_detected = false;
|
||||
constinit bool g_virt_mem_enabled = false;
|
||||
|
||||
void EnsureVirtualAddressMemoryDetected() {
|
||||
if (AMS_LIKELY(g_virt_mem_enabled_detected)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::scoped_lock lk(g_virt_mem_enabled_lock);
|
||||
|
||||
if (AMS_UNLIKELY(g_virt_mem_enabled_detected)) {
|
||||
return;
|
||||
}
|
||||
|
||||
g_virt_mem_enabled = os::IsVirtualAddressMemoryEnabled();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE bool IsVirtualAddressMemoryEnabled() {
|
||||
EnsureVirtualAddressMemoryDetected();
|
||||
return g_virt_mem_enabled;
|
||||
AMS_FUNCTION_LOCAL_STATIC(bool, s_virt_mem_enabled, os::IsVirtualAddressMemoryEnabled());
|
||||
return s_virt_mem_enabled;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE errno_t ConvertResult(Result result) {
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright (c) 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 <sys/mman.h>
|
||||
#include "mem_impl_platform.hpp"
|
||||
|
||||
namespace ams::mem::impl {
|
||||
|
||||
errno_t virtual_alloc(void **ptr, size_t size) {
|
||||
/* Ensure size is non-zero. */
|
||||
if (size == 0) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Allocate virtual memory. */
|
||||
if (const auto address = ::mmap(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); address != MAP_FAILED) {
|
||||
*ptr = reinterpret_cast<void *>(address);
|
||||
return 0;
|
||||
} else {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
errno_t virtual_free(void *ptr, size_t size) {
|
||||
/* Ensure pointer/size aren't zero. */
|
||||
if (ptr == nullptr || size == 0) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Free the memory. */
|
||||
if (::munmap(ptr, size) == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
errno_t physical_alloc(void *ptr, size_t size, Prot prot) {
|
||||
/* Ensure pointer isn't zero. */
|
||||
if (ptr == nullptr) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Convert the protection. */
|
||||
int native_prot;
|
||||
switch (util::ToUnderlying(prot)) {
|
||||
case Prot_none:
|
||||
native_prot = PROT_NONE;
|
||||
break;
|
||||
case Prot_read:
|
||||
native_prot = PROT_READ;
|
||||
break;
|
||||
case Prot_write:
|
||||
case Prot_write | Prot_read:
|
||||
native_prot = PROT_READ | PROT_WRITE;
|
||||
break;
|
||||
case Prot_exec:
|
||||
native_prot = PROT_EXEC;
|
||||
break;
|
||||
case Prot_exec | Prot_read:
|
||||
native_prot = PROT_READ | PROT_EXEC;
|
||||
break;
|
||||
case Prot_exec | Prot_write:
|
||||
case Prot_exec | Prot_write | Prot_read:
|
||||
native_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
|
||||
break;
|
||||
default:
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if (::mprotect(ptr, size, native_prot)) {
|
||||
return 0;
|
||||
} else {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
errno_t physical_free(void *ptr, size_t size) {
|
||||
/* Detect empty allocation. */
|
||||
const uintptr_t aligned_start = util::AlignUp(reinterpret_cast<uintptr_t>(ptr), os::MemoryPageSize);
|
||||
const uintptr_t aligned_end = util::AlignDown(reinterpret_cast<uintptr_t>(ptr) + size, os::MemoryPageSize);
|
||||
const size_t aligned_size = aligned_end - aligned_start;
|
||||
if (aligned_end <= aligned_start) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (::mprotect(reinterpret_cast<void *>(aligned_start), aligned_size, PROT_NONE)) {
|
||||
return 0;
|
||||
} else {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
size_t strlcpy(char *dst, const char *src, size_t size) {
|
||||
const size_t src_size = std::strlen(src);
|
||||
if (src_size >= size) {
|
||||
if (size) {
|
||||
std::memcpy(dst, src, size - 1);
|
||||
dst[size - 1] = 0;
|
||||
}
|
||||
} else {
|
||||
std::memcpy(dst, src, src_size + 1);
|
||||
}
|
||||
return src_size;
|
||||
}
|
||||
|
||||
errno_t gen_random(void *dst, size_t dst_size) {
|
||||
/* TODO: CryptGenRandom? */
|
||||
os::GenerateRandomBytes(dst, dst_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno_t epochtime(s64 *dst) {
|
||||
/* TODO: Something more sane than this. */
|
||||
auto ts = os::ConvertToTimeSpan(os::GetSystemTick());
|
||||
*dst = (ts.GetNanoSeconds() / INT64_C(100)) + INT64_C(0x8A09F909AE60000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno_t getcpu(s32 *out) {
|
||||
if (const auto core = sched_getcpu(); core >= 0) {
|
||||
*out = core;
|
||||
return 0;
|
||||
} else {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* Copyright (c) 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 <sys/mman.h>
|
||||
#include "mem_impl_platform.hpp"
|
||||
|
||||
#if defined(ATMOSPHERE_ARCH_X64)
|
||||
#include <cpuid.h>
|
||||
#endif
|
||||
|
||||
namespace ams::mem::impl {
|
||||
|
||||
errno_t virtual_alloc(void **ptr, size_t size) {
|
||||
/* Ensure size is non-zero. */
|
||||
if (size == 0) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Allocate virtual memory. */
|
||||
if (const auto address = ::mmap(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); address != MAP_FAILED) {
|
||||
*ptr = reinterpret_cast<void *>(address);
|
||||
return 0;
|
||||
} else {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
errno_t virtual_free(void *ptr, size_t size) {
|
||||
/* Ensure pointer/size aren't zero. */
|
||||
if (ptr == nullptr || size == 0) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Free the memory. */
|
||||
if (::munmap(ptr, size) == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
errno_t physical_alloc(void *ptr, size_t size, Prot prot) {
|
||||
/* Ensure pointer isn't zero. */
|
||||
if (ptr == nullptr) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Convert the protection. */
|
||||
int native_prot;
|
||||
switch (util::ToUnderlying(prot)) {
|
||||
case Prot_none:
|
||||
native_prot = PROT_NONE;
|
||||
break;
|
||||
case Prot_read:
|
||||
native_prot = PROT_READ;
|
||||
break;
|
||||
case Prot_write:
|
||||
case Prot_write | Prot_read:
|
||||
native_prot = PROT_READ | PROT_WRITE;
|
||||
break;
|
||||
case Prot_exec:
|
||||
native_prot = PROT_EXEC;
|
||||
break;
|
||||
case Prot_exec | Prot_read:
|
||||
native_prot = PROT_READ | PROT_EXEC;
|
||||
break;
|
||||
case Prot_exec | Prot_write:
|
||||
case Prot_exec | Prot_write | Prot_read:
|
||||
native_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
|
||||
break;
|
||||
default:
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if (::mprotect(ptr, size, native_prot)) {
|
||||
return 0;
|
||||
} else {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
errno_t physical_free(void *ptr, size_t size) {
|
||||
/* Detect empty allocation. */
|
||||
const uintptr_t aligned_start = util::AlignUp(reinterpret_cast<uintptr_t>(ptr), os::MemoryPageSize);
|
||||
const uintptr_t aligned_end = util::AlignDown(reinterpret_cast<uintptr_t>(ptr) + size, os::MemoryPageSize);
|
||||
const size_t aligned_size = aligned_end - aligned_start;
|
||||
if (aligned_end <= aligned_start) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (::mprotect(reinterpret_cast<void *>(aligned_start), aligned_size, PROT_NONE)) {
|
||||
return 0;
|
||||
} else {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
size_t strlcpy(char *dst, const char *src, size_t size) {
|
||||
const size_t src_size = std::strlen(src);
|
||||
if (src_size >= size) {
|
||||
if (size) {
|
||||
std::memcpy(dst, src, size - 1);
|
||||
dst[size - 1] = 0;
|
||||
}
|
||||
} else {
|
||||
std::memcpy(dst, src, src_size + 1);
|
||||
}
|
||||
return src_size;
|
||||
}
|
||||
|
||||
errno_t gen_random(void *dst, size_t dst_size) {
|
||||
/* TODO: CryptGenRandom? */
|
||||
os::GenerateRandomBytes(dst, dst_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno_t epochtime(s64 *dst) {
|
||||
/* TODO: Something more sane than this. */
|
||||
auto ts = os::ConvertToTimeSpan(os::GetSystemTick());
|
||||
*dst = (ts.GetNanoSeconds() / INT64_C(100)) + INT64_C(0x8A09F909AE60000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno_t getcpu(s32 *out) {
|
||||
if (__builtin_available(macOS 11.0, *)) {
|
||||
/* On macOS 11.0+, we can use the exposed API they added. */
|
||||
size_t cpu_number = 0;
|
||||
const auto res = pthread_cpu_number_np(std::addressof(cpu_number));
|
||||
AMS_ASSERT(res == 0);
|
||||
AMS_UNUSED(res);
|
||||
|
||||
*out = static_cast<s32>(cpu_number);
|
||||
return 0;
|
||||
} else {
|
||||
#if defined(ATMOSPHERE_ARCH_X64)
|
||||
{
|
||||
uint32_t cpu_info[4];
|
||||
__cpuid_count(1, 0, cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]);
|
||||
|
||||
s32 core;
|
||||
if ((cpu_info[3] & (1 << 9)) == 0) {
|
||||
core = 0;
|
||||
} else {
|
||||
core = static_cast<s32>(cpu_info[1] >> 24);
|
||||
}
|
||||
|
||||
*out = core;
|
||||
return 0;
|
||||
}
|
||||
#elif defined(ATMOSPHERE_ARCH_ARM64)
|
||||
{
|
||||
/* Read from TSD, per https://github.com/apple-oss-distributions/xnu/blob/e6231be02a03711ca404e5121a151b24afbff733/libsyscall/os/tsd.h#L68 */
|
||||
uint64_t p;
|
||||
__asm__ __volatile__ ("mrs %0, TPIDR_EL0" : "=r" (p));
|
||||
*out = static_cast<s32>(p & 0xF);
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
AMS_ABORT("Unknown architecture to get current cpu on fallback path on macOS");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright (c) 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 <VersionHelpers.h>
|
||||
#include "mem_impl_platform.hpp"
|
||||
|
||||
namespace ams::mem::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
errno_t ConvertGetLastError(DWORD error, errno_t fallback = EIO) {
|
||||
/* TODO: Implement this? */
|
||||
AMS_UNUSED(error);
|
||||
return fallback;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
errno_t virtual_alloc(void **ptr, size_t size) {
|
||||
/* Ensure size is non-zero. */
|
||||
if (size == 0) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Allocate virtual memory. */
|
||||
if (void *mem = ::VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE); mem != nullptr) {
|
||||
*ptr = mem;
|
||||
return 0;
|
||||
} else {
|
||||
return ConvertGetLastError(::GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
errno_t virtual_free(void *ptr, size_t size) {
|
||||
/* Ensure pointer/size aren't zero. */
|
||||
if (ptr == nullptr || size == 0) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Free the memory. */
|
||||
if (::VirtualFree(ptr, 0, MEM_RELEASE)) {
|
||||
return 0;
|
||||
} else {
|
||||
return ConvertGetLastError(::GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
errno_t physical_alloc(void *ptr, size_t size, Prot prot) {
|
||||
/* Ensure pointer isn't zero. */
|
||||
if (ptr == nullptr) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
/* Convert the protection. */
|
||||
DWORD fl_protect;
|
||||
switch (util::ToUnderlying(prot)) {
|
||||
case Prot_none:
|
||||
fl_protect = PAGE_NOACCESS;
|
||||
break;
|
||||
case Prot_read:
|
||||
fl_protect = PAGE_READONLY;
|
||||
break;
|
||||
case Prot_write:
|
||||
case Prot_write | Prot_read:
|
||||
fl_protect = PAGE_READWRITE;
|
||||
break;
|
||||
case Prot_exec:
|
||||
fl_protect = PAGE_EXECUTE;
|
||||
break;
|
||||
case Prot_exec | Prot_read:
|
||||
fl_protect = PAGE_EXECUTE_READ;
|
||||
break;
|
||||
case Prot_exec | Prot_write:
|
||||
case Prot_exec | Prot_write | Prot_read:
|
||||
fl_protect = PAGE_EXECUTE_READWRITE;
|
||||
break;
|
||||
default:
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if (::VirtualAlloc(ptr, size, MEM_COMMIT, fl_protect)) {
|
||||
return 0;
|
||||
} else {
|
||||
return ConvertGetLastError(::GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
errno_t physical_free(void *ptr, size_t size) {
|
||||
/* Detect empty allocation. */
|
||||
const uintptr_t aligned_start = util::AlignUp(reinterpret_cast<uintptr_t>(ptr), os::MemoryPageSize);
|
||||
const uintptr_t aligned_end = util::AlignDown(reinterpret_cast<uintptr_t>(ptr) + size, os::MemoryPageSize);
|
||||
const size_t aligned_size = aligned_end - aligned_start;
|
||||
if (aligned_end <= aligned_start) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (::VirtualFree(reinterpret_cast<LPVOID>(aligned_start), aligned_size, MEM_DECOMMIT)) {
|
||||
return 0;
|
||||
} else {
|
||||
return ConvertGetLastError(::GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
size_t strlcpy(char *dst, const char *src, size_t size) {
|
||||
const size_t src_size = std::strlen(src);
|
||||
if (src_size >= size) {
|
||||
if (size) {
|
||||
std::memcpy(dst, src, size - 1);
|
||||
dst[size - 1] = 0;
|
||||
}
|
||||
} else {
|
||||
std::memcpy(dst, src, src_size + 1);
|
||||
}
|
||||
return src_size;
|
||||
}
|
||||
|
||||
errno_t gen_random(void *dst, size_t dst_size) {
|
||||
/* TODO: CryptGenRandom? */
|
||||
os::GenerateRandomBytes(dst, dst_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno_t epochtime(s64 *dst) {
|
||||
/* TODO: Something more sane than this. */
|
||||
auto ts = os::ConvertToTimeSpan(os::GetSystemTick());
|
||||
*dst = (ts.GetNanoSeconds() / INT64_C(100)) + INT64_C(0x8A09F909AE60000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno_t getcpu(s32 *out) {
|
||||
AMS_FUNCTION_LOCAL_STATIC(bool, s_has_processor_ex, ::IsWindows7OrGreater());
|
||||
|
||||
if (s_has_processor_ex) {
|
||||
PROCESSOR_NUMBER pnum;
|
||||
::GetCurrentProcessorNumberEx(std::addressof(pnum));
|
||||
|
||||
*out = (pnum.Group << 8) | pnum.Number;
|
||||
} else {
|
||||
*out = ::GetCurrentProcessorNumber();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user