diff --git a/app/src/main/cpp/skyline/common/base.h b/app/src/main/cpp/skyline/common/base.h index eb8bfa4e..68aab28f 100644 --- a/app/src/main/cpp/skyline/common/base.h +++ b/app/src/main/cpp/skyline/common/base.h @@ -42,6 +42,12 @@ namespace skyline { constexpr i64 NsInSecond{1000000000}; //!< The amount of nanoseconds in a second constexpr i64 NsInMillisecond{1000000}; //!< The amount of nanoseconds in a millisecond constexpr i64 NsInDay{86400000000000UL}; //!< The amount of nanoseconds in a day + + constexpr size_t AddressSpaceSize{1ULL << 39}; //!< The size of the host CPU AS in bytes + constexpr size_t PageSize{0x1000}; //!< The size of a host page + constexpr size_t PageSizeBits{12}; //!< log2(PageSize) + + static_assert(PageSize == PAGE_SIZE); } namespace util { diff --git a/app/src/main/cpp/skyline/common/utils.h b/app/src/main/cpp/skyline/common/utils.h index e9dd44f7..3fe316f4 100644 --- a/app/src/main/cpp/skyline/common/utils.h +++ b/app/src/main/cpp/skyline/common/utils.h @@ -115,7 +115,7 @@ namespace skyline::util { template requires IsPointerOrUnsignedIntegral constexpr bool IsPageAligned(TypeVal value) { - return IsAligned(value, PAGE_SIZE); + return IsAligned(value, constant::PageSize); } template diff --git a/app/src/main/cpp/skyline/gpu/buffer.cpp b/app/src/main/cpp/skyline/gpu/buffer.cpp index 51a2f152..2d4a42cd 100644 --- a/app/src/main/cpp/skyline/gpu/buffer.cpp +++ b/app/src/main/cpp/skyline/gpu/buffer.cpp @@ -9,8 +9,8 @@ namespace skyline::gpu { void Buffer::SetupGuestMappings() { - u8 *alignedData{util::AlignDown(guest->data(), PAGE_SIZE)}; - size_t alignedSize{static_cast(util::AlignUp(guest->data() + guest->size(), PAGE_SIZE) - alignedData)}; + u8 *alignedData{util::AlignDown(guest->data(), constant::PageSize)}; + size_t alignedSize{static_cast(util::AlignUp(guest->data() + guest->size(), constant::PageSize) - alignedData)}; alignedMirror = gpu.state.process->memory.CreateMirror(span{alignedData, alignedSize}); mirror = alignedMirror.subspan(static_cast(guest->data() - alignedData), guest->size()); diff --git a/app/src/main/cpp/skyline/gpu/buffer_manager.h b/app/src/main/cpp/skyline/gpu/buffer_manager.h index 9341e5a5..6ae7197a 100644 --- a/app/src/main/cpp/skyline/gpu/buffer_manager.h +++ b/app/src/main/cpp/skyline/gpu/buffer_manager.h @@ -16,10 +16,8 @@ namespace skyline::gpu { std::mutex mutex; //!< Synchronizes access to the buffer mappings std::vector> bufferMappings; //!< A sorted vector of all buffer mappings - static constexpr size_t AddressSpaceSize{1ULL << 39}; //!< The size of the guest CPU AS in bytes - static constexpr size_t PageSizeBits{12}; //!< The size of a single page of the guest CPU AS as a power of two (4 KiB == 1 << 12) static constexpr size_t L2EntryGranularity{19}; //!< The amount of AS (in bytes) a single L2 PTE covers (512 KiB == 1 << 19) - SegmentTable bufferTable; //!< A page table of all buffer mappings for O(1) lookups on full matches + SegmentTable bufferTable; //!< A page table of all buffer mappings for O(1) lookups on full matches /** * @brief A wrapper around a Buffer which locks it with the specified ContextTag diff --git a/app/src/main/cpp/skyline/gpu/texture/texture.cpp b/app/src/main/cpp/skyline/gpu/texture/texture.cpp index cdef42b5..2c2c7554 100644 --- a/app/src/main/cpp/skyline/gpu/texture/texture.cpp +++ b/app/src/main/cpp/skyline/gpu/texture/texture.cpp @@ -116,8 +116,8 @@ namespace skyline::gpu { auto &mappings{guest->mappings}; if (mappings.size() == 1) { auto mapping{mappings.front()}; - u8 *alignedData{util::AlignDown(mapping.data(), PAGE_SIZE)}; - size_t alignedSize{static_cast(util::AlignUp(mapping.data() + mapping.size(), PAGE_SIZE) - alignedData)}; + u8 *alignedData{util::AlignDown(mapping.data(), constant::PageSize)}; + size_t alignedSize{static_cast(util::AlignUp(mapping.data() + mapping.size(), constant::PageSize) - alignedData)}; alignedMirror = gpu.state.process->memory.CreateMirror(span{alignedData, alignedSize}); mirror = alignedMirror.subspan(static_cast(mapping.data() - alignedData), mapping.size()); @@ -125,7 +125,7 @@ namespace skyline::gpu { std::vector> alignedMappings; const auto &frontMapping{mappings.front()}; - u8 *alignedData{util::AlignDown(frontMapping.data(), PAGE_SIZE)}; + u8 *alignedData{util::AlignDown(frontMapping.data(), constant::PageSize)}; alignedMappings.emplace_back(alignedData, (frontMapping.data() + frontMapping.size()) - alignedData); size_t totalSize{frontMapping.size()}; @@ -137,7 +137,7 @@ namespace skyline::gpu { const auto &backMapping{mappings.back()}; totalSize += backMapping.size(); - alignedMappings.emplace_back(backMapping.data(), util::AlignUp(backMapping.size(), PAGE_SIZE)); + alignedMappings.emplace_back(backMapping.data(), util::AlignUp(backMapping.size(), constant::PageSize)); alignedMirror = gpu.state.process->memory.CreateMirrors(alignedMappings); mirror = alignedMirror.subspan(static_cast(frontMapping.data() - alignedData), totalSize); diff --git a/app/src/main/cpp/skyline/kernel/memory.cpp b/app/src/main/cpp/skyline/kernel/memory.cpp index a3297262..9cd2075f 100644 --- a/app/src/main/cpp/skyline/kernel/memory.cpp +++ b/app/src/main/cpp/skyline/kernel/memory.cpp @@ -266,6 +266,6 @@ namespace skyline::kernel { size_t MemoryManager::GetSystemResourceUsage() { std::shared_lock lock(mutex); constexpr size_t KMemoryBlockSize{0x40}; - return std::min(static_cast(state.process->npdm.meta.systemResourceSize), util::AlignUp(chunks.size() * KMemoryBlockSize, PAGE_SIZE)); + return std::min(static_cast(state.process->npdm.meta.systemResourceSize), util::AlignUp(chunks.size() * KMemoryBlockSize, constant::PageSize)); } } diff --git a/app/src/main/cpp/skyline/kernel/types/KProcess.h b/app/src/main/cpp/skyline/kernel/types/KProcess.h index 049812af..162e50e5 100644 --- a/app/src/main/cpp/skyline/kernel/types/KProcess.h +++ b/app/src/main/cpp/skyline/kernel/types/KProcess.h @@ -12,7 +12,7 @@ namespace skyline { namespace constant { constexpr u16 TlsSlotSize{0x200}; //!< The size of a single TLS slot - constexpr u8 TlsSlots{PAGE_SIZE / TlsSlotSize}; //!< The amount of TLS slots in a single page + constexpr u8 TlsSlots{constant::PageSize / TlsSlotSize}; //!< The amount of TLS slots in a single page constexpr KHandle BaseHandleIndex{0xD000}; //!< The index of the base handle } diff --git a/app/src/main/cpp/skyline/loader/nso.cpp b/app/src/main/cpp/skyline/loader/nso.cpp index 16da304d..5d603662 100644 --- a/app/src/main/cpp/skyline/loader/nso.cpp +++ b/app/src/main/cpp/skyline/loader/nso.cpp @@ -38,18 +38,18 @@ namespace skyline::loader { Executable executable{}; executable.text.contents = GetSegment(backing, header.text, header.flags.textCompressed ? header.textCompressedSize : 0); - executable.text.contents.resize(util::AlignUp(executable.text.contents.size(), PAGE_SIZE)); + executable.text.contents.resize(util::AlignUp(executable.text.contents.size(), constant::PageSize)); executable.text.offset = header.text.memoryOffset; executable.ro.contents = GetSegment(backing, header.ro, header.flags.roCompressed ? header.roCompressedSize : 0); - executable.ro.contents.resize(util::AlignUp(executable.ro.contents.size(), PAGE_SIZE)); + executable.ro.contents.resize(util::AlignUp(executable.ro.contents.size(), constant::PageSize)); executable.ro.offset = header.ro.memoryOffset; executable.data.contents = GetSegment(backing, header.data, header.flags.dataCompressed ? header.dataCompressedSize : 0); executable.data.offset = header.data.memoryOffset; // Data and BSS are aligned together - executable.bssSize = util::AlignUp(executable.data.contents.size() + header.bssSize, PAGE_SIZE) - executable.data.contents.size(); + executable.bssSize = util::AlignUp(executable.data.contents.size() + header.bssSize, constant::PageSize) - executable.data.contents.size(); if (header.dynsym.offset + header.dynsym.size <= header.ro.decompressedSize && header.dynstr.offset + header.dynstr.size <= header.ro.decompressedSize) { executable.dynsym = {header.dynsym.offset, header.dynsym.size}; diff --git a/app/src/main/cpp/skyline/nce.cpp b/app/src/main/cpp/skyline/nce.cpp index 851333a6..64b02d84 100644 --- a/app/src/main/cpp/skyline/nce.cpp +++ b/app/src/main/cpp/skyline/nce.cpp @@ -243,7 +243,7 @@ namespace skyline::nce { offsets.push_back(instructionOffset); } } - return {util::AlignUp(size * sizeof(u32), PAGE_SIZE), offsets}; + return {util::AlignUp(size * sizeof(u32), constant::PageSize), offsets}; } void NCE::PatchCode(std::vector &text, u32 *patch, size_t patchSize, const std::vector &offsets) { @@ -413,7 +413,7 @@ namespace skyline::nce { auto reprotectIntervalsWithFunction = [&intervals](auto getProtection) { for (auto region : intervals) { - region = region.Align(PAGE_SIZE); + region = region.Align(constant::PageSize); mprotect(region.start, region.Size(), getProtection(region)); } }; @@ -472,7 +472,7 @@ namespace skyline::nce { std::scoped_lock lock(trapMutex); // Retrieve any callbacks for the page that was faulted - auto[entries, intervals]{trapMap.GetAlignedRecursiveRange(address)}; + auto[entries, intervals]{trapMap.GetAlignedRecursiveRange(address)}; if (entries.empty()) return false; // There's no callbacks associated with this page @@ -543,10 +543,10 @@ namespace skyline::nce { TRACE_EVENT("host", "NCE::PageOutRegions"); std::scoped_lock lock{trapMutex}; for (auto region : handle->intervals) { - auto freeStart{util::AlignUp(region.start, PAGE_SIZE)}, freeEnd{util::AlignDown(region.end, PAGE_SIZE)}; // We want to avoid the first and last page as they may contain unrelated data + auto freeStart{util::AlignUp(region.start, constant::PageSize)}, freeEnd{util::AlignDown(region.end, constant::PageSize)}; // We want to avoid the first and last page as they may contain unrelated data ssize_t freeSize{freeEnd - freeStart}; - constexpr ssize_t MinimumPageoutSize{PAGE_SIZE}; //!< The minimum size to page out, we don't want to page out small intervals for performance reasons + constexpr ssize_t MinimumPageoutSize{constant::PageSize}; //!< The minimum size to page out, we don't want to page out small intervals for performance reasons if (freeSize > MinimumPageoutSize) state.process->memory.FreeMemory(span{freeStart, static_cast(freeSize)}); } diff --git a/app/src/main/cpp/skyline/services/nvdrv/core/nvmap.cpp b/app/src/main/cpp/skyline/services/nvdrv/core/nvmap.cpp index 47286b7c..95aa29e2 100644 --- a/app/src/main/cpp/skyline/services/nvdrv/core/nvmap.cpp +++ b/app/src/main/cpp/skyline/services/nvdrv/core/nvmap.cpp @@ -22,7 +22,7 @@ namespace skyline::service::nvdrv::core { flags = pFlags; kind = pKind; - align = pAlign < PAGE_SIZE ? PAGE_SIZE : pAlign; + align = pAlign < constant::PageSize ? constant::PageSize : pAlign; // This flag is only applicable for handles with an address passed if (pAddress) @@ -30,7 +30,7 @@ namespace skyline::service::nvdrv::core { else throw exception("Mapping nvmap handles without a CPU side address is unimplemented!"); - size = util::AlignUp(size, PAGE_SIZE); + size = util::AlignUp(size, constant::PageSize); alignedSize = util::AlignUp(size, align); address = pAddress; @@ -56,7 +56,7 @@ namespace skyline::service::nvdrv::core { return PosixResult::Success; } - NvMap::NvMap(const DeviceState &state) : state(state), smmuAllocator(PAGE_SIZE) {} + NvMap::NvMap(const DeviceState &state) : state(state), smmuAllocator(soc::SmmuPageSize) {} void NvMap::AddHandle(std::shared_ptr handleDesc) { std::scoped_lock lock(handlesLock); diff --git a/app/src/main/cpp/skyline/services/nvdrv/devices/nvmap.cpp b/app/src/main/cpp/skyline/services/nvdrv/devices/nvmap.cpp index e77e4f66..78f62109 100644 --- a/app/src/main/cpp/skyline/services/nvdrv/devices/nvmap.cpp +++ b/app/src/main/cpp/skyline/services/nvdrv/devices/nvmap.cpp @@ -9,7 +9,7 @@ namespace skyline::service::nvdrv::device { NvMap::NvMap(const DeviceState &state, Driver &driver, Core &core, const SessionContext &ctx) : NvDevice(state, driver, core, ctx) {} PosixResult NvMap::Create(In size, Out handle) { - auto handleDesc{core.nvMap.CreateHandle(util::AlignUp(static_cast(size), PAGE_SIZE))}; + auto handleDesc{core.nvMap.CreateHandle(util::AlignUp(static_cast(size), constant::PageSize))}; if (handleDesc) { (*handleDesc)->origSize = size; // Orig size is the unaligned size handle = (*handleDesc)->id; @@ -51,8 +51,8 @@ namespace skyline::service::nvdrv::device { return PosixResult::InvalidArgument; // Force page size alignment at a minimum - if (align < PAGE_SIZE) [[unlikely]] - align = PAGE_SIZE; + if (align < constant::PageSize) [[unlikely]] + align = constant::PageSize; auto handleDesc{core.nvMap.GetHandle(handle)}; if (!handleDesc) [[unlikely]]