diff options
Diffstat (limited to 'src/core')
27 files changed, 638 insertions, 311 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index b1a746727..6e8d11919 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -247,6 +247,9 @@ add_library(core STATIC hle/kernel/k_trace.h hle/kernel/k_transfer_memory.cpp hle/kernel/k_transfer_memory.h + hle/kernel/k_worker_task.h + hle/kernel/k_worker_task_manager.cpp + hle/kernel/k_worker_task_manager.h hle/kernel/k_writable_event.cpp hle/kernel/k_writable_event.h hle/kernel/kernel.cpp diff --git a/src/core/hid/emulated_console.cpp b/src/core/hid/emulated_console.cpp index 08f8af551..eef0ff493 100644 --- a/src/core/hid/emulated_console.cpp +++ b/src/core/hid/emulated_console.cpp @@ -158,7 +158,7 @@ void EmulatedConsole::SetMotion(const Common::Input::CallbackStatus& callback) { auto& motion = console.motion_state; motion.accel = emulated.GetAcceleration(); motion.gyro = emulated.GetGyroscope(); - motion.rotation = emulated.GetGyroscope(); + motion.rotation = emulated.GetRotations(); motion.orientation = emulated.GetOrientation(); motion.quaternion = emulated.GetQuaternion(); motion.gyro_bias = emulated.GetGyroBias(); diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp index 52a56ef1a..d12037b11 100644 --- a/src/core/hid/emulated_controller.cpp +++ b/src/core/hid/emulated_controller.cpp @@ -145,7 +145,7 @@ void EmulatedController::LoadDevices() { motion_devices.begin(), Common::Input::CreateDevice<Common::Input::InputDevice>); std::transform(trigger_params.begin(), trigger_params.end(), trigger_devices.begin(), Common::Input::CreateDevice<Common::Input::InputDevice>); - std::transform(battery_params.begin(), battery_params.begin(), battery_devices.end(), + std::transform(battery_params.begin(), battery_params.end(), battery_devices.begin(), Common::Input::CreateDevice<Common::Input::InputDevice>); std::transform(output_params.begin(), output_params.end(), output_devices.begin(), Common::Input::CreateDevice<Common::Input::OutputDevice>); @@ -351,6 +351,19 @@ void EmulatedController::DisableConfiguration() { } } +void EmulatedController::EnableSystemButtons() { + system_buttons_enabled = true; +} + +void EmulatedController::DisableSystemButtons() { + system_buttons_enabled = false; +} + +void EmulatedController::ResetSystemButtons() { + controller.home_button_state.home.Assign(false); + controller.capture_button_state.capture.Assign(false); +} + bool EmulatedController::IsConfiguring() const { return is_configuring; } @@ -600,7 +613,16 @@ void EmulatedController::SetButton(const Common::Input::CallbackStatus& callback controller.npad_button_state.right_sr.Assign(current_status.value); break; case Settings::NativeButton::Home: + if (!system_buttons_enabled) { + break; + } + controller.home_button_state.home.Assign(current_status.value); + break; case Settings::NativeButton::Screenshot: + if (!system_buttons_enabled) { + break; + } + controller.capture_button_state.capture.Assign(current_status.value); break; } } @@ -1081,6 +1103,20 @@ BatteryValues EmulatedController::GetBatteryValues() const { return controller.battery_values; } +HomeButtonState EmulatedController::GetHomeButtons() const { + if (is_configuring) { + return {}; + } + return controller.home_button_state; +} + +CaptureButtonState EmulatedController::GetCaptureButtons() const { + if (is_configuring) { + return {}; + } + return controller.capture_button_state; +} + NpadButtonState EmulatedController::GetNpadButtons() const { if (is_configuring) { return {}; diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h index c0994ab4d..a63a83cce 100644 --- a/src/core/hid/emulated_controller.h +++ b/src/core/hid/emulated_controller.h @@ -101,6 +101,8 @@ struct ControllerStatus { VibrationValues vibration_values{}; // Data for HID serices + HomeButtonState home_button_state{}; + CaptureButtonState capture_button_state{}; NpadButtonState npad_button_state{}; DebugPadButton debug_pad_button_state{}; AnalogSticks analog_stick_state{}; @@ -198,6 +200,15 @@ public: /// Returns the emulated controller into normal mode, allowing the modification of the HID state void DisableConfiguration(); + /// Enables Home and Screenshot buttons + void EnableSystemButtons(); + + /// Disables Home and Screenshot buttons + void DisableSystemButtons(); + + /// Sets Home and Screenshot buttons to false + void ResetSystemButtons(); + /// Returns true if the emulated controller is in configuring mode bool IsConfiguring() const; @@ -261,7 +272,13 @@ public: /// Returns the latest battery status from the controller with parameters BatteryValues GetBatteryValues() const; - /// Returns the latest status of button input for the npad service + /// Returns the latest status of button input for the hid::HomeButton service + HomeButtonState GetHomeButtons() const; + + /// Returns the latest status of button input for the hid::CaptureButton service + CaptureButtonState GetCaptureButtons() const; + + /// Returns the latest status of button input for the hid::Npad service NpadButtonState GetNpadButtons() const; /// Returns the latest status of button input for the debug pad service @@ -383,6 +400,7 @@ private: NpadStyleTag supported_style_tag{NpadStyleSet::All}; bool is_connected{false}; bool is_configuring{false}; + bool system_buttons_enabled{true}; f32 motion_sensitivity{0.01f}; bool force_update_motion{false}; diff --git a/src/core/hid/hid_types.h b/src/core/hid/hid_types.h index 4eca68533..778b328b9 100644 --- a/src/core/hid/hid_types.h +++ b/src/core/hid/hid_types.h @@ -378,6 +378,26 @@ struct LedPattern { }; }; +struct HomeButtonState { + union { + u64 raw{}; + + // Buttons + BitField<0, 1, u64> home; + }; +}; +static_assert(sizeof(HomeButtonState) == 0x8, "HomeButtonState has incorrect size."); + +struct CaptureButtonState { + union { + u64 raw{}; + + // Buttons + BitField<0, 1, u64> capture; + }; +}; +static_assert(sizeof(CaptureButtonState) == 0x8, "CaptureButtonState has incorrect size."); + struct NpadButtonState { union { NpadButton raw{}; diff --git a/src/core/hid/input_converter.cpp b/src/core/hid/input_converter.cpp index f5acff6e0..860aab400 100644 --- a/src/core/hid/input_converter.cpp +++ b/src/core/hid/input_converter.cpp @@ -114,7 +114,7 @@ Common::Input::MotionStatus TransformToMotion(const Common::Input::CallbackStatu if (TransformToButton(callback).value) { std::random_device device; std::mt19937 gen(device()); - std::uniform_int_distribution<s16> distribution(-1000, 1000); + std::uniform_int_distribution<s16> distribution(-5000, 5000); status.accel.x.raw_value = static_cast<f32>(distribution(gen)) * 0.001f; status.accel.y.raw_value = static_cast<f32>(distribution(gen)) * 0.001f; status.accel.z.raw_value = static_cast<f32>(distribution(gen)) * 0.001f; diff --git a/src/core/hid/motion_input.cpp b/src/core/hid/motion_input.cpp index 43152492e..6e126be19 100644 --- a/src/core/hid/motion_input.cpp +++ b/src/core/hid/motion_input.cpp @@ -10,7 +10,7 @@ namespace Core::HID { MotionInput::MotionInput() { // Initialize PID constants with default values SetPID(0.3f, 0.005f, 0.0f); - SetGyroThreshold(0.001f); + SetGyroThreshold(0.00005f); } void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) { diff --git a/src/core/hle/kernel/k_memory_block.h b/src/core/hle/kernel/k_memory_block.h index 9e51c33ce..dcca47f1b 100644 --- a/src/core/hle/kernel/k_memory_block.h +++ b/src/core/hle/kernel/k_memory_block.h @@ -70,12 +70,12 @@ enum class KMemoryState : u32 { ThreadLocal = static_cast<u32>(Svc::MemoryState::ThreadLocal) | FlagMapped | FlagReferenceCounted, - Transferred = static_cast<u32>(Svc::MemoryState::Transferred) | FlagsMisc | - FlagCanAlignedDeviceMap | FlagCanChangeAttribute | FlagCanUseIpc | - FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, + Transfered = static_cast<u32>(Svc::MemoryState::Transferred) | FlagsMisc | + FlagCanAlignedDeviceMap | FlagCanChangeAttribute | FlagCanUseIpc | + FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, - SharedTransferred = static_cast<u32>(Svc::MemoryState::SharedTransferred) | FlagsMisc | - FlagCanAlignedDeviceMap | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, + SharedTransfered = static_cast<u32>(Svc::MemoryState::SharedTransferred) | FlagsMisc | + FlagCanAlignedDeviceMap | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, SharedCode = static_cast<u32>(Svc::MemoryState::SharedCode) | FlagMapped | FlagReferenceCounted | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, @@ -93,6 +93,8 @@ enum class KMemoryState : u32 { GeneratedCode = static_cast<u32>(Svc::MemoryState::GeneratedCode) | FlagMapped | FlagReferenceCounted | FlagCanDebug, CodeOut = static_cast<u32>(Svc::MemoryState::CodeOut) | FlagMapped | FlagReferenceCounted, + + Coverage = static_cast<u32>(Svc::MemoryState::Coverage) | FlagMapped, }; DECLARE_ENUM_FLAG_OPERATORS(KMemoryState); @@ -108,8 +110,8 @@ static_assert(static_cast<u32>(KMemoryState::AliasCodeData) == 0x03FFBD09); static_assert(static_cast<u32>(KMemoryState::Ipc) == 0x005C3C0A); static_assert(static_cast<u32>(KMemoryState::Stack) == 0x005C3C0B); static_assert(static_cast<u32>(KMemoryState::ThreadLocal) == 0x0040200C); -static_assert(static_cast<u32>(KMemoryState::Transferred) == 0x015C3C0D); -static_assert(static_cast<u32>(KMemoryState::SharedTransferred) == 0x005C380E); +static_assert(static_cast<u32>(KMemoryState::Transfered) == 0x015C3C0D); +static_assert(static_cast<u32>(KMemoryState::SharedTransfered) == 0x005C380E); static_assert(static_cast<u32>(KMemoryState::SharedCode) == 0x0040380F); static_assert(static_cast<u32>(KMemoryState::Inaccessible) == 0x00000010); static_assert(static_cast<u32>(KMemoryState::NonSecureIpc) == 0x005C3811); @@ -117,6 +119,7 @@ static_assert(static_cast<u32>(KMemoryState::NonDeviceIpc) == 0x004C2812); static_assert(static_cast<u32>(KMemoryState::Kernel) == 0x00002013); static_assert(static_cast<u32>(KMemoryState::GeneratedCode) == 0x00402214); static_assert(static_cast<u32>(KMemoryState::CodeOut) == 0x00402015); +static_assert(static_cast<u32>(KMemoryState::Coverage) == 0x00002016); enum class KMemoryPermission : u8 { None = 0, @@ -155,7 +158,13 @@ enum class KMemoryPermission : u8 { DECLARE_ENUM_FLAG_OPERATORS(KMemoryPermission); constexpr KMemoryPermission ConvertToKMemoryPermission(Svc::MemoryPermission perm) { - return static_cast<KMemoryPermission>(perm); + return static_cast<KMemoryPermission>( + (static_cast<KMemoryPermission>(perm) & KMemoryPermission::UserMask) | + KMemoryPermission::KernelRead | + ((static_cast<KMemoryPermission>(perm) & KMemoryPermission::UserWrite) + << KMemoryPermission::KernelShift) | + (perm == Svc::MemoryPermission::None ? KMemoryPermission::NotMapped + : KMemoryPermission::None)); } enum class KMemoryAttribute : u8 { @@ -169,6 +178,8 @@ enum class KMemoryAttribute : u8 { DeviceShared = static_cast<u8>(Svc::MemoryAttribute::DeviceShared), Uncached = static_cast<u8>(Svc::MemoryAttribute::Uncached), + SetMask = Uncached, + IpcAndDeviceMapped = IpcLocked | DeviceShared, LockedAndIpcLocked = Locked | IpcLocked, DeviceSharedAndUncached = DeviceShared | Uncached @@ -215,6 +226,15 @@ struct KMemoryInfo { constexpr VAddr GetLastAddress() const { return GetEndAddress() - 1; } + constexpr KMemoryState GetState() const { + return state; + } + constexpr KMemoryAttribute GetAttribute() const { + return attribute; + } + constexpr KMemoryPermission GetPermission() const { + return perm; + } }; class KMemoryBlock final { diff --git a/src/core/hle/kernel/k_memory_manager.cpp b/src/core/hle/kernel/k_memory_manager.cpp index 0166df0a5..1b44541b1 100644 --- a/src/core/hle/kernel/k_memory_manager.cpp +++ b/src/core/hle/kernel/k_memory_manager.cpp @@ -8,12 +8,16 @@ #include "common/assert.h" #include "common/common_types.h" #include "common/scope_exit.h" +#include "core/core.h" +#include "core/device_memory.h" #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_page_linked_list.h" #include "core/hle/kernel/svc_results.h" namespace Kernel { +KMemoryManager::KMemoryManager(Core::System& system_) : system{system_} {} + std::size_t KMemoryManager::Impl::Initialize(Pool new_pool, u64 start_address, u64 end_address) { const auto size{end_address - start_address}; @@ -81,7 +85,7 @@ VAddr KMemoryManager::AllocateAndOpenContinuous(std::size_t num_pages, std::size } ResultCode KMemoryManager::Allocate(KPageLinkedList& page_list, std::size_t num_pages, Pool pool, - Direction dir) { + Direction dir, u32 heap_fill_value) { ASSERT(page_list.GetNumPages() == 0); // Early return if we're allocating no pages @@ -139,6 +143,12 @@ ResultCode KMemoryManager::Allocate(KPageLinkedList& page_list, std::size_t num_ } } + // Clear allocated memory. + for (const auto& it : page_list.Nodes()) { + std::memset(system.DeviceMemory().GetPointer(it.GetAddress()), heap_fill_value, + it.GetSize()); + } + // Only succeed if we allocated as many pages as we wanted if (num_pages) { return ResultOutOfMemory; @@ -146,11 +156,12 @@ ResultCode KMemoryManager::Allocate(KPageLinkedList& page_list, std::size_t num_ // We succeeded! group_guard.Cancel(); + return ResultSuccess; } ResultCode KMemoryManager::Free(KPageLinkedList& page_list, std::size_t num_pages, Pool pool, - Direction dir) { + Direction dir, u32 heap_fill_value) { // Early return if we're freeing no pages if (!num_pages) { return ResultSuccess; diff --git a/src/core/hle/kernel/k_memory_manager.h b/src/core/hle/kernel/k_memory_manager.h index 39badc5f1..abd6c8ace 100644 --- a/src/core/hle/kernel/k_memory_manager.h +++ b/src/core/hle/kernel/k_memory_manager.h @@ -12,6 +12,10 @@ #include "core/hle/kernel/k_page_heap.h" #include "core/hle/result.h" +namespace Core { +class System; +} + namespace Kernel { class KPageLinkedList; @@ -42,7 +46,7 @@ public: Mask = (0xF << Shift), }; - KMemoryManager() = default; + explicit KMemoryManager(Core::System& system_); constexpr std::size_t GetSize(Pool pool) const { return managers[static_cast<std::size_t>(pool)].GetSize(); @@ -51,10 +55,10 @@ public: void InitializeManager(Pool pool, u64 start_address, u64 end_address); VAddr AllocateAndOpenContinuous(size_t num_pages, size_t align_pages, u32 option); - ResultCode Allocate(KPageLinkedList& page_list, std::size_t num_pages, Pool pool, - Direction dir = Direction::FromFront); - ResultCode Free(KPageLinkedList& page_list, std::size_t num_pages, Pool pool, - Direction dir = Direction::FromFront); + ResultCode Allocate(KPageLinkedList& page_list, std::size_t num_pages, Pool pool, Direction dir, + u32 heap_fill_value = 0); + ResultCode Free(KPageLinkedList& page_list, std::size_t num_pages, Pool pool, Direction dir, + u32 heap_fill_value = 0); static constexpr std::size_t MaxManagerCount = 10; @@ -129,6 +133,7 @@ private: }; private: + Core::System& system; std::array<std::mutex, static_cast<std::size_t>(Pool::Count)> pool_locks; std::array<Impl, MaxManagerCount> managers; }; diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp index 4da509224..b650ea31d 100644 --- a/src/core/hle/kernel/k_page_table.cpp +++ b/src/core/hle/kernel/k_page_table.cpp @@ -289,8 +289,8 @@ ResultCode KPageTable::MapProcessCode(VAddr addr, std::size_t num_pages, KMemory } KPageLinkedList page_linked_list; - CASCADE_CODE( - system.Kernel().MemoryManager().Allocate(page_linked_list, num_pages, memory_pool)); + CASCADE_CODE(system.Kernel().MemoryManager().Allocate(page_linked_list, num_pages, memory_pool, + allocation_option)); CASCADE_CODE(Operate(addr, num_pages, page_linked_list, OperationType::MapGroup)); block_manager->Update(addr, num_pages, state, perm); @@ -298,16 +298,16 @@ ResultCode KPageTable::MapProcessCode(VAddr addr, std::size_t num_pages, KMemory return ResultSuccess; } -ResultCode KPageTable::MapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size) { +ResultCode KPageTable::MapCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size) { std::lock_guard lock{page_table_lock}; const std::size_t num_pages{size / PageSize}; KMemoryState state{}; KMemoryPermission perm{}; - CASCADE_CODE(CheckMemoryState(&state, &perm, nullptr, src_addr, size, KMemoryState::All, - KMemoryState::Normal, KMemoryPermission::All, - KMemoryPermission::ReadAndWrite, KMemoryAttribute::Mask, + CASCADE_CODE(CheckMemoryState(&state, &perm, nullptr, nullptr, src_addr, size, + KMemoryState::All, KMemoryState::Normal, KMemoryPermission::All, + KMemoryPermission::UserReadWrite, KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped)); if (IsRegionMapped(dst_addr, size)) { @@ -335,7 +335,7 @@ ResultCode KPageTable::MapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, std: return ResultSuccess; } -ResultCode KPageTable::UnmapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size) { +ResultCode KPageTable::UnmapCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size) { std::lock_guard lock{page_table_lock}; if (!size) { @@ -344,14 +344,14 @@ ResultCode KPageTable::UnmapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, st const std::size_t num_pages{size / PageSize}; - CASCADE_CODE(CheckMemoryState(nullptr, nullptr, nullptr, src_addr, size, KMemoryState::All, - KMemoryState::Normal, KMemoryPermission::None, + CASCADE_CODE(CheckMemoryState(nullptr, nullptr, nullptr, nullptr, src_addr, size, + KMemoryState::All, KMemoryState::Normal, KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::Mask, KMemoryAttribute::Locked, KMemoryAttribute::IpcAndDeviceMapped)); KMemoryState state{}; CASCADE_CODE(CheckMemoryState( - &state, nullptr, nullptr, dst_addr, PageSize, KMemoryState::FlagCanCodeAlias, + &state, nullptr, nullptr, nullptr, dst_addr, PageSize, KMemoryState::FlagCanCodeAlias, KMemoryState::FlagCanCodeAlias, KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped)); CASCADE_CODE(CheckMemoryState(dst_addr, size, KMemoryState::All, state, KMemoryPermission::None, @@ -361,7 +361,7 @@ ResultCode KPageTable::UnmapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, st block_manager->Update(dst_addr, num_pages, KMemoryState::Free); block_manager->Update(src_addr, num_pages, KMemoryState::Normal, - KMemoryPermission::ReadAndWrite); + KMemoryPermission::UserReadWrite); system.InvalidateCpuInstructionCacheRange(dst_addr, size); @@ -416,7 +416,7 @@ void KPageTable::MapPhysicalMemory(KPageLinkedList& page_linked_list, VAddr star } const std::size_t num_pages{std::min(src_num_pages, dst_num_pages)}; - Operate(dst_addr, num_pages, KMemoryPermission::ReadAndWrite, OperationType::Map, + Operate(dst_addr, num_pages, KMemoryPermission::UserReadWrite, OperationType::Map, map_addr); dst_addr += num_pages * PageSize; @@ -457,8 +457,8 @@ ResultCode KPageTable::MapPhysicalMemory(VAddr addr, std::size_t size) { KPageLinkedList page_linked_list; - CASCADE_CODE( - system.Kernel().MemoryManager().Allocate(page_linked_list, remaining_pages, memory_pool)); + CASCADE_CODE(system.Kernel().MemoryManager().Allocate(page_linked_list, remaining_pages, + memory_pool, allocation_option)); // We succeeded, so commit the memory reservation. memory_reservation.Commit(); @@ -470,7 +470,7 @@ ResultCode KPageTable::MapPhysicalMemory(VAddr addr, std::size_t size) { const std::size_t num_pages{size / PageSize}; block_manager->Update(addr, num_pages, KMemoryState::Free, KMemoryPermission::None, KMemoryAttribute::None, KMemoryState::Normal, - KMemoryPermission::ReadAndWrite, KMemoryAttribute::None); + KMemoryPermission::UserReadWrite, KMemoryAttribute::None); return ResultSuccess; } @@ -541,7 +541,8 @@ ResultCode KPageTable::UnmapMemory(VAddr addr, std::size_t size) { } const std::size_t num_pages{size / PageSize}; - system.Kernel().MemoryManager().Free(page_linked_list, num_pages, memory_pool); + system.Kernel().MemoryManager().Free(page_linked_list, num_pages, memory_pool, + allocation_option); block_manager->Update(addr, num_pages, KMemoryState::Free); @@ -553,8 +554,8 @@ ResultCode KPageTable::Map(VAddr dst_addr, VAddr src_addr, std::size_t size) { KMemoryState src_state{}; CASCADE_CODE(CheckMemoryState( - &src_state, nullptr, nullptr, src_addr, size, KMemoryState::FlagCanAlias, - KMemoryState::FlagCanAlias, KMemoryPermission::All, KMemoryPermission::ReadAndWrite, + &src_state, nullptr, nullptr, nullptr, src_addr, size, KMemoryState::FlagCanAlias, + KMemoryState::FlagCanAlias, KMemoryPermission::All, KMemoryPermission::UserReadWrite, KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped)); if (IsRegionMapped(dst_addr, size)) { @@ -568,13 +569,13 @@ ResultCode KPageTable::Map(VAddr dst_addr, VAddr src_addr, std::size_t size) { { auto block_guard = detail::ScopeExit([&] { - Operate(src_addr, num_pages, KMemoryPermission::ReadAndWrite, + Operate(src_addr, num_pages, KMemoryPermission::UserReadWrite, OperationType::ChangePermissions); }); CASCADE_CODE(Operate(src_addr, num_pages, KMemoryPermission::None, OperationType::ChangePermissions)); - CASCADE_CODE(MapPages(dst_addr, page_linked_list, KMemoryPermission::ReadAndWrite)); + CASCADE_CODE(MapPages(dst_addr, page_linked_list, KMemoryPermission::UserReadWrite)); block_guard.Cancel(); } @@ -582,7 +583,7 @@ ResultCode KPageTable::Map(VAddr dst_addr, VAddr src_addr, std::size_t size) { block_manager->Update(src_addr, num_pages, src_state, KMemoryPermission::None, KMemoryAttribute::Locked); block_manager->Update(dst_addr, num_pages, KMemoryState::Stack, - KMemoryPermission::ReadAndWrite); + KMemoryPermission::UserReadWrite); return ResultSuccess; } @@ -592,13 +593,13 @@ ResultCode KPageTable::Unmap(VAddr dst_addr, VAddr src_addr, std::size_t size) { KMemoryState src_state{}; CASCADE_CODE(CheckMemoryState( - &src_state, nullptr, nullptr, src_addr, size, KMemoryState::FlagCanAlias, + &src_state, nullptr, nullptr, nullptr, src_addr, size, KMemoryState::FlagCanAlias, KMemoryState::FlagCanAlias, KMemoryPermission::All, KMemoryPermission::None, KMemoryAttribute::Mask, KMemoryAttribute::Locked, KMemoryAttribute::IpcAndDeviceMapped)); KMemoryPermission dst_perm{}; - CASCADE_CODE(CheckMemoryState(nullptr, &dst_perm, nullptr, dst_addr, size, KMemoryState::All, - KMemoryState::Stack, KMemoryPermission::None, + CASCADE_CODE(CheckMemoryState(nullptr, &dst_perm, nullptr, nullptr, dst_addr, size, + KMemoryState::All, KMemoryState::Stack, KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped)); @@ -617,13 +618,13 @@ ResultCode KPageTable::Unmap(VAddr dst_addr, VAddr src_addr, std::size_t size) { auto block_guard = detail::ScopeExit([&] { MapPages(dst_addr, dst_pages, dst_perm); }); CASCADE_CODE(Operate(dst_addr, num_pages, KMemoryPermission::None, OperationType::Unmap)); - CASCADE_CODE(Operate(src_addr, num_pages, KMemoryPermission::ReadAndWrite, + CASCADE_CODE(Operate(src_addr, num_pages, KMemoryPermission::UserReadWrite, OperationType::ChangePermissions)); block_guard.Cancel(); } - block_manager->Update(src_addr, num_pages, src_state, KMemoryPermission::ReadAndWrite); + block_manager->Update(src_addr, num_pages, src_state, KMemoryPermission::UserReadWrite); block_manager->Update(dst_addr, num_pages, KMemoryState::Free); return ResultSuccess; @@ -713,50 +714,61 @@ ResultCode KPageTable::UnmapPages(VAddr addr, KPageLinkedList& page_linked_list, } ResultCode KPageTable::SetProcessMemoryPermission(VAddr addr, std::size_t size, - KMemoryPermission perm) { + Svc::MemoryPermission svc_perm) { + const size_t num_pages = size / PageSize; + // Lock the table. std::lock_guard lock{page_table_lock}; - KMemoryState prev_state{}; - KMemoryPermission prev_perm{}; - - CASCADE_CODE(CheckMemoryState( - &prev_state, &prev_perm, nullptr, addr, size, KMemoryState::FlagCode, - KMemoryState::FlagCode, KMemoryPermission::None, KMemoryPermission::None, - KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped)); - - KMemoryState state{prev_state}; + // Verify we can change the memory permission. + KMemoryState old_state; + KMemoryPermission old_perm; + size_t num_allocator_blocks; + R_TRY(this->CheckMemoryState(std::addressof(old_state), std::addressof(old_perm), nullptr, + std::addressof(num_allocator_blocks), addr, size, + KMemoryState::FlagCode, KMemoryState::FlagCode, + KMemoryPermission::None, KMemoryPermission::None, + KMemoryAttribute::All, KMemoryAttribute::None)); - // Ensure state is mutable if permission allows write - if ((perm & KMemoryPermission::Write) != KMemoryPermission::None) { - if (prev_state == KMemoryState::Code) { - state = KMemoryState::CodeData; - } else if (prev_state == KMemoryState::AliasCode) { - state = KMemoryState::AliasCodeData; - } else { + // Determine new perm/state. + const KMemoryPermission new_perm = ConvertToKMemoryPermission(svc_perm); + KMemoryState new_state = old_state; + const bool is_w = (new_perm & KMemoryPermission::UserWrite) == KMemoryPermission::UserWrite; + const bool is_x = (new_perm & KMemoryPermission::UserExecute) == KMemoryPermission::UserExecute; + const bool was_x = + (old_perm & KMemoryPermission::UserExecute) == KMemoryPermission::UserExecute; + ASSERT(!(is_w && is_x)); + + if (is_w) { + switch (old_state) { + case KMemoryState::Code: + new_state = KMemoryState::CodeData; + break; + case KMemoryState::AliasCode: + new_state = KMemoryState::AliasCodeData; + break; + default: UNREACHABLE(); } } - // Return early if there is nothing to change - if (state == prev_state && perm == prev_perm) { - return ResultSuccess; - } + // Succeed if there's nothing to do. + R_SUCCEED_IF(old_perm == new_perm && old_state == new_state); + + // Perform mapping operation. + const auto operation = + was_x ? OperationType::ChangePermissionsAndRefresh : OperationType::ChangePermissions; + R_TRY(Operate(addr, num_pages, new_perm, operation)); - if ((prev_perm & KMemoryPermission::Execute) != (perm & KMemoryPermission::Execute)) { + // Update the blocks. + block_manager->Update(addr, num_pages, new_state, new_perm, KMemoryAttribute::None); + + // Ensure cache coherency, if we're setting pages as executable. + if (is_x) { // Memory execution state is changing, invalidate CPU cache range system.InvalidateCpuInstructionCacheRange(addr, size); } - const std::size_t num_pages{size / PageSize}; - const OperationType operation{(perm & KMemoryPermission::Execute) != KMemoryPermission::None - ? OperationType::ChangePermissionsAndRefresh - : OperationType::ChangePermissions}; - - CASCADE_CODE(Operate(addr, num_pages, perm, operation)); - - block_manager->Update(addr, num_pages, state, perm); - return ResultSuccess; } @@ -782,10 +794,10 @@ ResultCode KPageTable::ReserveTransferMemory(VAddr addr, std::size_t size, KMemo KMemoryAttribute attribute{}; CASCADE_CODE(CheckMemoryState( - &state, nullptr, &attribute, addr, size, + &state, nullptr, &attribute, nullptr, addr, size, KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted, KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted, KMemoryPermission::All, - KMemoryPermission::ReadAndWrite, KMemoryAttribute::Mask, KMemoryAttribute::None, + KMemoryPermission::UserReadWrite, KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped)); block_manager->Update(addr, size / PageSize, state, perm, attribute | KMemoryAttribute::Locked); @@ -799,13 +811,13 @@ ResultCode KPageTable::ResetTransferMemory(VAddr addr, std::size_t size) { KMemoryState state{}; CASCADE_CODE( - CheckMemoryState(&state, nullptr, nullptr, addr, size, + CheckMemoryState(&state, nullptr, nullptr, nullptr, addr, size, KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted, KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted, KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::Mask, KMemoryAttribute::Locked, KMemoryAttribute::IpcAndDeviceMapped)); - block_manager->Update(addr, size / PageSize, state, KMemoryPermission::ReadAndWrite); + block_manager->Update(addr, size / PageSize, state, KMemoryPermission::UserReadWrite); return ResultSuccess; } @@ -820,7 +832,7 @@ ResultCode KPageTable::SetMemoryPermission(VAddr addr, std::size_t size, KMemoryState old_state; KMemoryPermission old_perm; R_TRY(this->CheckMemoryState( - std::addressof(old_state), std::addressof(old_perm), nullptr, addr, size, + std::addressof(old_state), std::addressof(old_perm), nullptr, nullptr, addr, size, KMemoryState::FlagCanReprotect, KMemoryState::FlagCanReprotect, KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::All, KMemoryAttribute::None)); @@ -837,24 +849,36 @@ ResultCode KPageTable::SetMemoryPermission(VAddr addr, std::size_t size, return ResultSuccess; } -ResultCode KPageTable::SetMemoryAttribute(VAddr addr, std::size_t size, KMemoryAttribute mask, - KMemoryAttribute value) { - std::lock_guard lock{page_table_lock}; +ResultCode KPageTable::SetMemoryAttribute(VAddr addr, std::size_t size, u32 mask, u32 attr) { + const size_t num_pages = size / PageSize; + ASSERT((static_cast<KMemoryAttribute>(mask) | KMemoryAttribute::SetMask) == + KMemoryAttribute::SetMask); - KMemoryState state{}; - KMemoryPermission perm{}; - KMemoryAttribute attribute{}; + // Lock the table. + std::lock_guard lock{page_table_lock}; - CASCADE_CODE(CheckMemoryState( - &state, &perm, &attribute, addr, size, KMemoryState::FlagCanChangeAttribute, + // Verify we can change the memory attribute. + KMemoryState old_state; + KMemoryPermission old_perm; + KMemoryAttribute old_attr; + size_t num_allocator_blocks; + constexpr auto AttributeTestMask = + ~(KMemoryAttribute::SetMask | KMemoryAttribute::DeviceShared); + R_TRY(this->CheckMemoryState( + std::addressof(old_state), std::addressof(old_perm), std::addressof(old_attr), + std::addressof(num_allocator_blocks), addr, size, KMemoryState::FlagCanChangeAttribute, KMemoryState::FlagCanChangeAttribute, KMemoryPermission::None, KMemoryPermission::None, - KMemoryAttribute::LockedAndIpcLocked, KMemoryAttribute::None, - KMemoryAttribute::DeviceSharedAndUncached)); + AttributeTestMask, KMemoryAttribute::None, ~AttributeTestMask)); + + // Determine the new attribute. + const auto new_attr = ((old_attr & static_cast<KMemoryAttribute>(~mask)) | + static_cast<KMemoryAttribute>(attr & mask)); - attribute = attribute & ~mask; - attribute = attribute | (mask & value); + // Perform operation. + this->Operate(addr, num_pages, old_perm, OperationType::ChangePermissionsAndRefresh); - block_manager->Update(addr, size / PageSize, state, perm, attribute); + // Update the blocks. + block_manager->Update(addr, num_pages, old_state, old_perm, new_attr); return ResultSuccess; } @@ -894,7 +918,7 @@ ResultCode KPageTable::SetHeapSize(VAddr* out, std::size_t size) { R_TRY(this->CheckMemoryState(std::addressof(num_allocator_blocks), heap_region_start + size, GetHeapSize() - size, KMemoryState::All, KMemoryState::Normal, - KMemoryPermission::All, KMemoryPermission::ReadAndWrite, + KMemoryPermission::All, KMemoryPermission::UserReadWrite, KMemoryAttribute::All, KMemoryAttribute::None)); // Unmap the end of the heap. @@ -937,7 +961,7 @@ ResultCode KPageTable::SetHeapSize(VAddr* out, std::size_t size) { // Allocate pages for the heap extension. KPageLinkedList page_linked_list; R_TRY(system.Kernel().MemoryManager().Allocate(page_linked_list, allocation_size / PageSize, - memory_pool)); + memory_pool, allocation_option)); // Map the pages. { @@ -969,7 +993,7 @@ ResultCode KPageTable::SetHeapSize(VAddr* out, std::size_t size) { // Apply the memory block update. block_manager->Update(current_heap_end, num_pages, KMemoryState::Normal, - KMemoryPermission::ReadAndWrite, KMemoryAttribute::None); + KMemoryPermission::UserReadWrite, KMemoryAttribute::None); // Update the current heap end. current_heap_end = heap_region_start + size; @@ -1004,8 +1028,8 @@ ResultVal<VAddr> KPageTable::AllocateAndMapMemory(std::size_t needed_num_pages, CASCADE_CODE(Operate(addr, needed_num_pages, perm, OperationType::Map, map_addr)); } else { KPageLinkedList page_group; - CASCADE_CODE( - system.Kernel().MemoryManager().Allocate(page_group, needed_num_pages, memory_pool)); + CASCADE_CODE(system.Kernel().MemoryManager().Allocate(page_group, needed_num_pages, + memory_pool, allocation_option)); CASCADE_CODE(Operate(addr, needed_num_pages, page_group, OperationType::MapGroup)); } @@ -1019,7 +1043,7 @@ ResultCode KPageTable::LockForDeviceAddressSpace(VAddr addr, std::size_t size) { KMemoryPermission perm{}; if (const ResultCode result{CheckMemoryState( - nullptr, &perm, nullptr, addr, size, KMemoryState::FlagCanChangeAttribute, + nullptr, &perm, nullptr, nullptr, addr, size, KMemoryState::FlagCanChangeAttribute, KMemoryState::FlagCanChangeAttribute, KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::LockedAndIpcLocked, KMemoryAttribute::None, KMemoryAttribute::DeviceSharedAndUncached)}; @@ -1042,7 +1066,7 @@ ResultCode KPageTable::UnlockForDeviceAddressSpace(VAddr addr, std::size_t size) KMemoryPermission perm{}; if (const ResultCode result{CheckMemoryState( - nullptr, &perm, nullptr, addr, size, KMemoryState::FlagCanChangeAttribute, + nullptr, &perm, nullptr, nullptr, addr, size, KMemoryState::FlagCanChangeAttribute, KMemoryState::FlagCanChangeAttribute, KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::LockedAndIpcLocked, KMemoryAttribute::None, KMemoryAttribute::DeviceSharedAndUncached)}; @@ -1068,7 +1092,7 @@ ResultCode KPageTable::LockForCodeMemory(VAddr addr, std::size_t size) { KMemoryPermission old_perm{}; if (const ResultCode result{CheckMemoryState( - nullptr, &old_perm, nullptr, addr, size, KMemoryState::FlagCanCodeMemory, + nullptr, &old_perm, nullptr, nullptr, addr, size, KMemoryState::FlagCanCodeMemory, KMemoryState::FlagCanCodeMemory, KMemoryPermission::All, KMemoryPermission::UserReadWrite, KMemoryAttribute::All, KMemoryAttribute::None)}; result.IsError()) { @@ -1095,7 +1119,7 @@ ResultCode KPageTable::UnlockForCodeMemory(VAddr addr, std::size_t size) { KMemoryPermission old_perm{}; if (const ResultCode result{CheckMemoryState( - nullptr, &old_perm, nullptr, addr, size, KMemoryState::FlagCanCodeMemory, + nullptr, &old_perm, nullptr, nullptr, addr, size, KMemoryState::FlagCanCodeMemory, KMemoryState::FlagCanCodeMemory, KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::All, KMemoryAttribute::Locked)}; result.IsError()) { @@ -1225,18 +1249,19 @@ constexpr VAddr KPageTable::GetRegionAddress(KMemoryState state) const { return alias_region_start; case KMemoryState::Stack: return stack_region_start; - case KMemoryState::Io: case KMemoryState::Static: case KMemoryState::ThreadLocal: return kernel_map_region_start; + case KMemoryState::Io: case KMemoryState::Shared: case KMemoryState::AliasCode: case KMemoryState::AliasCodeData: - case KMemoryState::Transferred: - case KMemoryState::SharedTransferred: + case KMemoryState::Transfered: + case KMemoryState::SharedTransfered: case KMemoryState::SharedCode: case KMemoryState::GeneratedCode: case KMemoryState::CodeOut: + case KMemoryState::Coverage: return alias_code_region_start; case KMemoryState::Code: case KMemoryState::CodeData: @@ -1260,18 +1285,19 @@ constexpr std::size_t KPageTable::GetRegionSize(KMemoryState state) const { return alias_region_end - alias_region_start; case KMemoryState::Stack: return stack_region_end - stack_region_start; - case KMemoryState::Io: case KMemoryState::Static: case KMemoryState::ThreadLocal: return kernel_map_region_end - kernel_map_region_start; + case KMemoryState::Io: case KMemoryState::Shared: case KMemoryState::AliasCode: case KMemoryState::AliasCodeData: - case KMemoryState::Transferred: - case KMemoryState::SharedTransferred: + case KMemoryState::Transfered: + case KMemoryState::SharedTransfered: case KMemoryState::SharedCode: case KMemoryState::GeneratedCode: case KMemoryState::CodeOut: + case KMemoryState::Coverage: return alias_code_region_end - alias_code_region_start; case KMemoryState::Code: case KMemoryState::CodeData: @@ -1283,15 +1309,18 @@ constexpr std::size_t KPageTable::GetRegionSize(KMemoryState state) const { } bool KPageTable::CanContain(VAddr addr, std::size_t size, KMemoryState state) const { - const VAddr end{addr + size}; - const VAddr last{end - 1}; - const VAddr region_start{GetRegionAddress(state)}; - const std::size_t region_size{GetRegionSize(state)}; - const bool is_in_region{region_start <= addr && addr < end && - last <= region_start + region_size - 1}; - const bool is_in_heap{!(end <= heap_region_start || heap_region_end <= addr)}; - const bool is_in_alias{!(end <= alias_region_start || alias_region_end <= addr)}; - + const VAddr end = addr + size; + const VAddr last = end - 1; + + const VAddr region_start = this->GetRegionAddress(state); + const size_t region_size = this->GetRegionSize(state); + + const bool is_in_region = + region_start <= addr && addr < end && last <= region_start + region_size - 1; + const bool is_in_heap = !(end <= heap_region_start || heap_region_end <= addr || + heap_region_start == heap_region_end); + const bool is_in_alias = !(end <= alias_region_start || alias_region_end <= addr || + alias_region_start == alias_region_end); switch (state) { case KMemoryState::Free: case KMemoryState::Kernel: @@ -1305,11 +1334,12 @@ bool KPageTable::CanContain(VAddr addr, std::size_t size, KMemoryState state) co case KMemoryState::AliasCodeData: case KMemoryState::Stack: case KMemoryState::ThreadLocal: - case KMemoryState::Transferred: - case KMemoryState::SharedTransferred: + case KMemoryState::Transfered: + case KMemoryState::SharedTransfered: case KMemoryState::SharedCode: case KMemoryState::GeneratedCode: case KMemoryState::CodeOut: + case KMemoryState::Coverage: return is_in_region && !is_in_heap && !is_in_alias; case KMemoryState::Normal: ASSERT(is_in_heap); @@ -1324,100 +1354,91 @@ bool KPageTable::CanContain(VAddr addr, std::size_t size, KMemoryState state) co } } -constexpr ResultCode KPageTable::CheckMemoryState(const KMemoryInfo& info, KMemoryState state_mask, - KMemoryState state, KMemoryPermission perm_mask, - KMemoryPermission perm, - KMemoryAttribute attr_mask, - KMemoryAttribute attr) const { - // Validate the states match expectation - if ((info.state & state_mask) != state) { - return ResultInvalidCurrentMemory; - } - if ((info.perm & perm_mask) != perm) { - return ResultInvalidCurrentMemory; - } - if ((info.attribute & attr_mask) != attr) { - return ResultInvalidCurrentMemory; - } +ResultCode KPageTable::CheckMemoryState(const KMemoryInfo& info, KMemoryState state_mask, + KMemoryState state, KMemoryPermission perm_mask, + KMemoryPermission perm, KMemoryAttribute attr_mask, + KMemoryAttribute attr) const { + // Validate the states match expectation. + R_UNLESS((info.state & state_mask) == state, ResultInvalidCurrentMemory); + R_UNLESS((info.perm & perm_mask) == perm, ResultInvalidCurrentMemory); + R_UNLESS((info.attribute & attr_mask) == attr, ResultInvalidCurrentMemory); return ResultSuccess; } -ResultCode KPageTable::CheckMemoryState(KMemoryState* out_state, KMemoryPermission* out_perm, - KMemoryAttribute* out_attr, VAddr addr, std::size_t size, - KMemoryState state_mask, KMemoryState state, - KMemoryPermission perm_mask, KMemoryPermission perm, - KMemoryAttribute attr_mask, KMemoryAttribute attr, - KMemoryAttribute ignore_attr) { - std::lock_guard lock{page_table_lock}; +ResultCode KPageTable::CheckMemoryStateContiguous(std::size_t* out_blocks_needed, VAddr addr, + std::size_t size, KMemoryState state_mask, + KMemoryState state, KMemoryPermission perm_mask, + KMemoryPermission perm, + KMemoryAttribute attr_mask, + KMemoryAttribute attr) const { + ASSERT(this->IsLockedByCurrentThread()); - // Get information about the first block - const VAddr last_addr{addr + size - 1}; - KMemoryBlockManager::const_iterator it{block_manager->FindIterator(addr)}; - KMemoryInfo info{it->GetMemoryInfo()}; + // Get information about the first block. + const VAddr last_addr = addr + size - 1; + KMemoryBlockManager::const_iterator it = block_manager->FindIterator(addr); + KMemoryInfo info = it->GetMemoryInfo(); - // Validate all blocks in the range have correct state - const KMemoryState first_state{info.state}; - const KMemoryPermission first_perm{info.perm}; - const KMemoryAttribute first_attr{info.attribute}; + // If the start address isn't aligned, we need a block. + const size_t blocks_for_start_align = + (Common::AlignDown(addr, PageSize) != info.GetAddress()) ? 1 : 0; while (true) { - // Validate the current block - if (!(info.state == first_state)) { - return ResultInvalidCurrentMemory; - } - if (!(info.perm == first_perm)) { - return ResultInvalidCurrentMemory; - } - if (!((info.attribute | static_cast<KMemoryAttribute>(ignore_attr)) == - (first_attr | static_cast<KMemoryAttribute>(ignore_attr)))) { - return ResultInvalidCurrentMemory; - } - - // Validate against the provided masks - CASCADE_CODE(CheckMemoryState(info, state_mask, state, perm_mask, perm, attr_mask, attr)); + // Validate against the provided masks. + R_TRY(this->CheckMemoryState(info, state_mask, state, perm_mask, perm, attr_mask, attr)); - // Break once we're done + // Break once we're done. if (last_addr <= info.GetLastAddress()) { break; } - // Advance our iterator + // Advance our iterator. it++; ASSERT(it != block_manager->cend()); info = it->GetMemoryInfo(); } - // Write output state - if (out_state) { - *out_state = first_state; - } - if (out_perm) { - *out_perm = first_perm; - } - if (out_attr) { - *out_attr = first_attr & static_cast<KMemoryAttribute>(~ignore_attr); + // If the end address isn't aligned, we need a block. + const size_t blocks_for_end_align = + (Common::AlignUp(addr + size, PageSize) != info.GetEndAddress()) ? 1 : 0; + + if (out_blocks_needed != nullptr) { + *out_blocks_needed = blocks_for_start_align + blocks_for_end_align; } return ResultSuccess; } -ResultCode KPageTable::CheckMemoryState(size_t* out_blocks_needed, VAddr addr, size_t size, - KMemoryState state_mask, KMemoryState state, - KMemoryPermission perm_mask, KMemoryPermission perm, - KMemoryAttribute attr_mask, KMemoryAttribute attr) const { +ResultCode KPageTable::CheckMemoryState(KMemoryState* out_state, KMemoryPermission* out_perm, + KMemoryAttribute* out_attr, std::size_t* out_blocks_needed, + VAddr addr, std::size_t size, KMemoryState state_mask, + KMemoryState state, KMemoryPermission perm_mask, + KMemoryPermission perm, KMemoryAttribute attr_mask, + KMemoryAttribute attr, KMemoryAttribute ignore_attr) const { + ASSERT(this->IsLockedByCurrentThread()); + // Get information about the first block. const VAddr last_addr = addr + size - 1; - KMemoryBlockManager::const_iterator it{block_manager->FindIterator(addr)}; + KMemoryBlockManager::const_iterator it = block_manager->FindIterator(addr); KMemoryInfo info = it->GetMemoryInfo(); // If the start address isn't aligned, we need a block. const size_t blocks_for_start_align = (Common::AlignDown(addr, PageSize) != info.GetAddress()) ? 1 : 0; + // Validate all blocks in the range have correct state. + const KMemoryState first_state = info.state; + const KMemoryPermission first_perm = info.perm; + const KMemoryAttribute first_attr = info.attribute; while (true) { + // Validate the current block. + R_UNLESS(info.state == first_state, ResultInvalidCurrentMemory); + R_UNLESS(info.perm == first_perm, ResultInvalidCurrentMemory); + R_UNLESS((info.attribute | ignore_attr) == (first_attr | ignore_attr), + ResultInvalidCurrentMemory); + // Validate against the provided masks. - R_TRY(CheckMemoryState(info, state_mask, state, perm_mask, perm, attr_mask, attr)); + R_TRY(this->CheckMemoryState(info, state_mask, state, perm_mask, perm, attr_mask, attr)); // Break once we're done. if (last_addr <= info.GetLastAddress()) { @@ -1426,6 +1447,7 @@ ResultCode KPageTable::CheckMemoryState(size_t* out_blocks_needed, VAddr addr, s // Advance our iterator. it++; + ASSERT(it != block_manager->cend()); info = it->GetMemoryInfo(); } @@ -1433,10 +1455,19 @@ ResultCode KPageTable::CheckMemoryState(size_t* out_blocks_needed, VAddr addr, s const size_t blocks_for_end_align = (Common::AlignUp(addr + size, PageSize) != info.GetEndAddress()) ? 1 : 0; + // Write output state. + if (out_state != nullptr) { + *out_state = first_state; + } + if (out_perm != nullptr) { + *out_perm = first_perm; + } + if (out_attr != nullptr) { + *out_attr = static_cast<KMemoryAttribute>(first_attr & ~ignore_attr); + } if (out_blocks_needed != nullptr) { *out_blocks_needed = blocks_for_start_align + blocks_for_end_align; } - return ResultSuccess; } diff --git a/src/core/hle/kernel/k_page_table.h b/src/core/hle/kernel/k_page_table.h index 564410dca..f67986e91 100644 --- a/src/core/hle/kernel/k_page_table.h +++ b/src/core/hle/kernel/k_page_table.h @@ -31,8 +31,8 @@ public: KMemoryManager::Pool pool); ResultCode MapProcessCode(VAddr addr, std::size_t pages_count, KMemoryState state, KMemoryPermission perm); - ResultCode MapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size); - ResultCode UnmapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size); + ResultCode MapCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size); + ResultCode UnmapCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size); ResultCode UnmapProcessMemory(VAddr dst_addr, std::size_t size, KPageTable& src_page_table, VAddr src_addr); ResultCode MapPhysicalMemory(VAddr addr, std::size_t size); @@ -43,13 +43,13 @@ public: ResultCode MapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state, KMemoryPermission perm); ResultCode UnmapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state); - ResultCode SetProcessMemoryPermission(VAddr addr, std::size_t size, KMemoryPermission perm); + ResultCode SetProcessMemoryPermission(VAddr addr, std::size_t size, + Svc::MemoryPermission svc_perm); KMemoryInfo QueryInfo(VAddr addr); ResultCode ReserveTransferMemory(VAddr addr, std::size_t size, KMemoryPermission perm); ResultCode ResetTransferMemory(VAddr addr, std::size_t size); ResultCode SetMemoryPermission(VAddr addr, std::size_t size, Svc::MemoryPermission perm); - ResultCode SetMemoryAttribute(VAddr addr, std::size_t size, KMemoryAttribute mask, - KMemoryAttribute value); + ResultCode SetMemoryAttribute(VAddr addr, std::size_t size, u32 mask, u32 attr); ResultCode SetMaxHeapSize(std::size_t size); ResultCode SetHeapSize(VAddr* out, std::size_t size); ResultVal<VAddr> AllocateAndMapMemory(std::size_t needed_num_pages, std::size_t align, @@ -102,28 +102,50 @@ private: constexpr VAddr GetRegionAddress(KMemoryState state) const; constexpr std::size_t GetRegionSize(KMemoryState state) const; - constexpr ResultCode CheckMemoryState(const KMemoryInfo& info, KMemoryState state_mask, + ResultCode CheckMemoryStateContiguous(std::size_t* out_blocks_needed, VAddr addr, + std::size_t size, KMemoryState state_mask, KMemoryState state, KMemoryPermission perm_mask, KMemoryPermission perm, KMemoryAttribute attr_mask, KMemoryAttribute attr) const; + ResultCode CheckMemoryStateContiguous(VAddr addr, std::size_t size, KMemoryState state_mask, + KMemoryState state, KMemoryPermission perm_mask, + KMemoryPermission perm, KMemoryAttribute attr_mask, + KMemoryAttribute attr) const { + return this->CheckMemoryStateContiguous(nullptr, addr, size, state_mask, state, perm_mask, + perm, attr_mask, attr); + } + + ResultCode CheckMemoryState(const KMemoryInfo& info, KMemoryState state_mask, + KMemoryState state, KMemoryPermission perm_mask, + KMemoryPermission perm, KMemoryAttribute attr_mask, + KMemoryAttribute attr) const; ResultCode CheckMemoryState(KMemoryState* out_state, KMemoryPermission* out_perm, - KMemoryAttribute* out_attr, VAddr addr, std::size_t size, + KMemoryAttribute* out_attr, std::size_t* out_blocks_needed, + VAddr addr, std::size_t size, KMemoryState state_mask, + KMemoryState state, KMemoryPermission perm_mask, + KMemoryPermission perm, KMemoryAttribute attr_mask, + KMemoryAttribute attr, + KMemoryAttribute ignore_attr = DefaultMemoryIgnoreAttr) const; + ResultCode CheckMemoryState(std::size_t* out_blocks_needed, VAddr addr, std::size_t size, KMemoryState state_mask, KMemoryState state, KMemoryPermission perm_mask, KMemoryPermission perm, KMemoryAttribute attr_mask, KMemoryAttribute attr, - KMemoryAttribute ignore_attr = DefaultMemoryIgnoreAttr); - ResultCode CheckMemoryState(VAddr addr, std::size_t size, KMemoryState state_mask, + KMemoryAttribute ignore_attr = DefaultMemoryIgnoreAttr) const { + return CheckMemoryState(nullptr, nullptr, nullptr, out_blocks_needed, addr, size, + state_mask, state, perm_mask, perm, attr_mask, attr, ignore_attr); + } + ResultCode CheckMemoryState(VAddr addr, size_t size, KMemoryState state_mask, KMemoryState state, KMemoryPermission perm_mask, KMemoryPermission perm, KMemoryAttribute attr_mask, KMemoryAttribute attr, - KMemoryAttribute ignore_attr = DefaultMemoryIgnoreAttr) { - return CheckMemoryState(nullptr, nullptr, nullptr, addr, size, state_mask, state, perm_mask, - perm, attr_mask, attr, ignore_attr); + KMemoryAttribute ignore_attr = DefaultMemoryIgnoreAttr) const { + return this->CheckMemoryState(nullptr, addr, size, state_mask, state, perm_mask, perm, + attr_mask, attr, ignore_attr); + } + + bool IsLockedByCurrentThread() const { + return true; } - ResultCode CheckMemoryState(size_t* out_blocks_needed, VAddr addr, size_t size, - KMemoryState state_mask, KMemoryState state, - KMemoryPermission perm_mask, KMemoryPermission perm, - KMemoryAttribute attr_mask, KMemoryAttribute attr) const; std::recursive_mutex page_table_lock; std::unique_ptr<KMemoryBlockManager> block_manager; @@ -281,6 +303,7 @@ private: bool is_aslr_enabled{}; KMemoryManager::Pool memory_pool{KMemoryManager::Pool::Application}; + KMemoryManager::Direction allocation_option{KMemoryManager::Direction::FromFront}; Common::PageTable page_table_impl; diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp index bf98a51e2..265ac6fa1 100644 --- a/src/core/hle/kernel/k_process.cpp +++ b/src/core/hle/kernel/k_process.cpp @@ -149,6 +149,10 @@ ResultCode KProcess::Initialize(KProcess* process, Core::System& system, std::st return ResultSuccess; } +void KProcess::DoWorkerTaskImpl() { + UNIMPLEMENTED(); +} + KResourceLimit* KProcess::GetResourceLimit() const { return resource_limit; } @@ -477,7 +481,7 @@ void KProcess::Finalize() { } // Perform inherited finalization. - KAutoObjectWithSlabHeapAndContainer<KProcess, KSynchronizationObject>::Finalize(); + KAutoObjectWithSlabHeapAndContainer<KProcess, KWorkerTask>::Finalize(); } /** @@ -509,7 +513,7 @@ VAddr KProcess::CreateTLSRegion() { const VAddr tls_page_addr{page_table ->AllocateAndMapMemory(1, PageSize, true, start, size / PageSize, KMemoryState::ThreadLocal, - KMemoryPermission::ReadAndWrite, + KMemoryPermission::UserReadWrite, tls_map_addr) .ValueOr(0)}; @@ -541,16 +545,16 @@ void KProcess::FreeTLSRegion(VAddr tls_address) { void KProcess::LoadModule(CodeSet code_set, VAddr base_addr) { const auto ReprotectSegment = [&](const CodeSet::Segment& segment, - KMemoryPermission permission) { + Svc::MemoryPermission permission) { page_table->SetProcessMemoryPermission(segment.addr + base_addr, segment.size, permission); }; kernel.System().Memory().WriteBlock(*this, base_addr, code_set.memory.data(), code_set.memory.size()); - ReprotectSegment(code_set.CodeSegment(), KMemoryPermission::ReadAndExecute); - ReprotectSegment(code_set.RODataSegment(), KMemoryPermission::Read); - ReprotectSegment(code_set.DataSegment(), KMemoryPermission::ReadAndWrite); + ReprotectSegment(code_set.CodeSegment(), Svc::MemoryPermission::ReadExecute); + ReprotectSegment(code_set.RODataSegment(), Svc::MemoryPermission::Read); + ReprotectSegment(code_set.DataSegment(), Svc::MemoryPermission::ReadWrite); } bool KProcess::IsSignaled() const { @@ -587,7 +591,7 @@ ResultCode KProcess::AllocateMainThreadStack(std::size_t stack_size) { CASCADE_RESULT(main_thread_stack_top, page_table->AllocateAndMapMemory( main_thread_stack_size / PageSize, PageSize, false, start, size / PageSize, - KMemoryState::Stack, KMemoryPermission::ReadAndWrite)); + KMemoryState::Stack, KMemoryPermission::UserReadWrite)); main_thread_stack_top += main_thread_stack_size; diff --git a/src/core/hle/kernel/k_process.h b/src/core/hle/kernel/k_process.h index e7c8b5838..c2a672021 100644 --- a/src/core/hle/kernel/k_process.h +++ b/src/core/hle/kernel/k_process.h @@ -15,6 +15,7 @@ #include "core/hle/kernel/k_condition_variable.h" #include "core/hle/kernel/k_handle_table.h" #include "core/hle/kernel/k_synchronization_object.h" +#include "core/hle/kernel/k_worker_task.h" #include "core/hle/kernel/process_capability.h" #include "core/hle/kernel/slab_helpers.h" #include "core/hle/result.h" @@ -62,8 +63,7 @@ enum class ProcessStatus { DebugBreak, }; -class KProcess final - : public KAutoObjectWithSlabHeapAndContainer<KProcess, KSynchronizationObject> { +class KProcess final : public KAutoObjectWithSlabHeapAndContainer<KProcess, KWorkerTask> { KERNEL_AUTOOBJECT_TRAITS(KProcess, KSynchronizationObject); public: @@ -345,6 +345,8 @@ public: bool IsSignaled() const override; + void DoWorkerTaskImpl(); + void PinCurrentThread(s32 core_id); void UnpinCurrentThread(s32 core_id); void UnpinThread(KThread* thread); diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index 31cec990e..f900b2e7a 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -49,8 +49,6 @@ void KScheduler::RescheduleCores(KernelCore& kernel, u64 cores_pending_reschedul if (!must_context_switch || core != current_core) { auto& phys_core = kernel.PhysicalCore(core); phys_core.Interrupt(); - } else { - must_context_switch = true; } cores_pending_reschedule &= ~(1ULL << core); } diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp index 71e029a3f..7a5e6fc08 100644 --- a/src/core/hle/kernel/k_thread.cpp +++ b/src/core/hle/kernel/k_thread.cpp @@ -30,6 +30,7 @@ #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_thread_queue.h" +#include "core/hle/kernel/k_worker_task_manager.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/time_manager.h" @@ -332,7 +333,7 @@ void KThread::Finalize() { } // Perform inherited finalization. - KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>::Finalize(); + KSynchronizationObject::Finalize(); } bool KThread::IsSignaled() const { @@ -376,11 +377,28 @@ void KThread::StartTermination() { // Register terminated dpc flag. RegisterDpc(DpcFlag::Terminated); +} + +void KThread::FinishTermination() { + // Ensure that the thread is not executing on any core. + if (parent != nullptr) { + for (std::size_t i = 0; i < static_cast<std::size_t>(Core::Hardware::NUM_CPU_CORES); ++i) { + KThread* core_thread{}; + do { + core_thread = kernel.Scheduler(i).GetCurrentThread(); + } while (core_thread == this); + } + } // Close the thread. this->Close(); } +void KThread::DoWorkerTaskImpl() { + // Finish the termination that was begun by Exit(). + this->FinishTermination(); +} + void KThread::Pin(s32 current_core) { ASSERT(kernel.GlobalSchedulerContext().IsLocked()); @@ -417,12 +435,7 @@ void KThread::Pin(s32 current_core) { static_cast<u32>(ThreadState::SuspendShift))); // Update our state. - const ThreadState old_state = thread_state; - thread_state = static_cast<ThreadState>(GetSuspendFlags() | - static_cast<u32>(old_state & ThreadState::Mask)); - if (thread_state != old_state) { - KScheduler::OnThreadStateChanged(kernel, this, old_state); - } + UpdateState(); } // TODO(bunnei): Update our SVC access permissions. @@ -463,20 +476,13 @@ void KThread::Unpin() { } // Allow performing thread suspension (if termination hasn't been requested). - { + if (!IsTerminationRequested()) { // Update our allow flags. - if (!IsTerminationRequested()) { - suspend_allowed_flags |= (1 << (static_cast<u32>(SuspendType::Thread) + - static_cast<u32>(ThreadState::SuspendShift))); - } + suspend_allowed_flags |= (1 << (static_cast<u32>(SuspendType::Thread) + + static_cast<u32>(ThreadState::SuspendShift))); // Update our state. - const ThreadState old_state = thread_state; - thread_state = static_cast<ThreadState>(GetSuspendFlags() | - static_cast<u32>(old_state & ThreadState::Mask)); - if (thread_state != old_state) { - KScheduler::OnThreadStateChanged(kernel, this, old_state); - } + UpdateState(); } // TODO(bunnei): Update our SVC access permissions. @@ -689,12 +695,7 @@ void KThread::Resume(SuspendType type) { ~(1u << (static_cast<u32>(ThreadState::SuspendShift) + static_cast<u32>(type))); // Update our state. - const ThreadState old_state = thread_state; - thread_state = static_cast<ThreadState>(GetSuspendFlags() | - static_cast<u32>(old_state & ThreadState::Mask)); - if (thread_state != old_state) { - KScheduler::OnThreadStateChanged(kernel, this, old_state); - } + this->UpdateState(); } void KThread::WaitCancel() { @@ -721,19 +722,22 @@ void KThread::TrySuspend() { ASSERT(GetNumKernelWaiters() == 0); // Perform the suspend. - Suspend(); + this->UpdateState(); } -void KThread::Suspend() { +void KThread::UpdateState() { ASSERT(kernel.GlobalSchedulerContext().IsLocked()); - ASSERT(IsSuspendRequested()); // Set our suspend flags in state. const auto old_state = thread_state; - thread_state = static_cast<ThreadState>(GetSuspendFlags()) | (old_state & ThreadState::Mask); + const auto new_state = + static_cast<ThreadState>(this->GetSuspendFlags()) | (old_state & ThreadState::Mask); + thread_state = new_state; // Note the state change in scheduler. - KScheduler::OnThreadStateChanged(kernel, this, old_state); + if (new_state != old_state) { + KScheduler::OnThreadStateChanged(kernel, this, old_state); + } } void KThread::Continue() { @@ -998,13 +1002,16 @@ ResultCode KThread::Run() { // If the current thread has been asked to suspend, suspend it and retry. if (GetCurrentThread(kernel).IsSuspended()) { - GetCurrentThread(kernel).Suspend(); + GetCurrentThread(kernel).UpdateState(); continue; } // If we're not a kernel thread and we've been asked to suspend, suspend ourselves. - if (IsUserThread() && IsSuspended()) { - Suspend(); + if (KProcess* owner = this->GetOwnerProcess(); owner != nullptr) { + if (IsUserThread() && IsSuspended()) { + this->UpdateState(); + } + owner->IncrementThreadCount(); } // Set our state and finish. @@ -1031,9 +1038,16 @@ void KThread::Exit() { // Disallow all suspension. suspend_allowed_flags = 0; + this->UpdateState(); + + // Disallow all suspension. + suspend_allowed_flags = 0; // Start termination. StartTermination(); + + // Register the thread as a work task. + KWorkerTaskManager::AddTask(kernel, KWorkerTaskManager::WorkerType::Exit, this); } } diff --git a/src/core/hle/kernel/k_thread.h b/src/core/hle/kernel/k_thread.h index 83dfde69b..cc427f6cf 100644 --- a/src/core/hle/kernel/k_thread.h +++ b/src/core/hle/kernel/k_thread.h @@ -19,6 +19,7 @@ #include "core/hle/kernel/k_light_lock.h" #include "core/hle/kernel/k_spin_lock.h" #include "core/hle/kernel/k_synchronization_object.h" +#include "core/hle/kernel/k_worker_task.h" #include "core/hle/kernel/slab_helpers.h" #include "core/hle/kernel/svc_common.h" #include "core/hle/kernel/svc_types.h" @@ -100,7 +101,7 @@ enum class ThreadWaitReasonForDebugging : u32 { [[nodiscard]] KThread& GetCurrentThread(KernelCore& kernel); [[nodiscard]] s32 GetCurrentCoreId(KernelCore& kernel); -class KThread final : public KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>, +class KThread final : public KAutoObjectWithSlabHeapAndContainer<KThread, KWorkerTask>, public boost::intrusive::list_base_hook<> { KERNEL_AUTOOBJECT_TRAITS(KThread, KSynchronizationObject); @@ -192,9 +193,9 @@ public: void TrySuspend(); - void Continue(); + void UpdateState(); - void Suspend(); + void Continue(); constexpr void SetSyncedIndex(s32 index) { synced_index = index; @@ -385,6 +386,8 @@ public: void OnTimer(); + void DoWorkerTaskImpl(); + static void PostDestroy(uintptr_t arg); [[nodiscard]] static ResultCode InitializeDummyThread(KThread* thread); @@ -679,6 +682,8 @@ private: void StartTermination(); + void FinishTermination(); + [[nodiscard]] ResultCode Initialize(KThreadFunction func, uintptr_t arg, VAddr user_stack_top, s32 prio, s32 virt_core, KProcess* owner, ThreadType type); diff --git a/src/core/hle/kernel/k_worker_task.h b/src/core/hle/kernel/k_worker_task.h new file mode 100644 index 000000000..b7794c6a8 --- /dev/null +++ b/src/core/hle/kernel/k_worker_task.h @@ -0,0 +1,18 @@ +// Copyright 2022 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/kernel/k_synchronization_object.h" + +namespace Kernel { + +class KWorkerTask : public KSynchronizationObject { +public: + explicit KWorkerTask(KernelCore& kernel_); + + void DoWorkerTask(); +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_worker_task_manager.cpp b/src/core/hle/kernel/k_worker_task_manager.cpp new file mode 100644 index 000000000..785e08111 --- /dev/null +++ b/src/core/hle/kernel/k_worker_task_manager.cpp @@ -0,0 +1,42 @@ +// Copyright 2022 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "core/hle/kernel/k_process.h" +#include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/k_worker_task.h" +#include "core/hle/kernel/k_worker_task_manager.h" +#include "core/hle/kernel/kernel.h" + +namespace Kernel { + +KWorkerTask::KWorkerTask(KernelCore& kernel_) : KSynchronizationObject{kernel_} {} + +void KWorkerTask::DoWorkerTask() { + if (auto* const thread = this->DynamicCast<KThread*>(); thread != nullptr) { + return thread->DoWorkerTaskImpl(); + } else { + auto* const process = this->DynamicCast<KProcess*>(); + ASSERT(process != nullptr); + + return process->DoWorkerTaskImpl(); + } +} + +KWorkerTaskManager::KWorkerTaskManager() : m_waiting_thread(1, "yuzu:KWorkerTaskManager") {} + +void KWorkerTaskManager::AddTask(KernelCore& kernel, WorkerType type, KWorkerTask* task) { + ASSERT(type <= WorkerType::Count); + kernel.WorkerTaskManager().AddTask(kernel, task); +} + +void KWorkerTaskManager::AddTask(KernelCore& kernel, KWorkerTask* task) { + KScopedSchedulerLock sl(kernel); + m_waiting_thread.QueueWork([task]() { + // Do the task. + task->DoWorkerTask(); + }); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_worker_task_manager.h b/src/core/hle/kernel/k_worker_task_manager.h new file mode 100644 index 000000000..43d1bfcec --- /dev/null +++ b/src/core/hle/kernel/k_worker_task_manager.h @@ -0,0 +1,33 @@ +// Copyright 2022 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" +#include "common/thread_worker.h" + +namespace Kernel { + +class KernelCore; +class KWorkerTask; + +class KWorkerTaskManager final { +public: + enum class WorkerType : u32 { + Exit, + Count, + }; + + KWorkerTaskManager(); + + static void AddTask(KernelCore& kernel_, WorkerType type, KWorkerTask* task); + +private: + void AddTask(KernelCore& kernel, KWorkerTask* task); + +private: + Common::ThreadWorker m_waiting_thread; +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 1225e1fba..887c1fd27 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -37,6 +37,7 @@ #include "core/hle/kernel/k_shared_memory.h" #include "core/hle/kernel/k_slab_heap.h" #include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/k_worker_task_manager.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" #include "core/hle/kernel/service_thread.h" @@ -51,7 +52,8 @@ namespace Kernel { struct KernelCore::Impl { explicit Impl(Core::System& system_, KernelCore& kernel_) - : time_manager{system_}, object_list_container{kernel_}, system{system_} {} + : time_manager{system_}, object_list_container{kernel_}, + service_threads_manager{1, "yuzu:ServiceThreadsManager"}, system{system_} {} void SetMulticore(bool is_multi) { is_multicore = is_multi; @@ -121,7 +123,7 @@ struct KernelCore::Impl { object_list_container.Finalize(); // Ensures all service threads gracefully shutdown. - service_threads.clear(); + ClearServiceThreads(); next_object_id = 0; next_kernel_process_id = KProcess::InitialKIPIDMin; @@ -629,7 +631,7 @@ struct KernelCore::Impl { const auto application_pool = memory_layout.GetKernelApplicationPoolRegionPhysicalExtents(); // Initialize memory managers - memory_manager = std::make_unique<KMemoryManager>(); + memory_manager = std::make_unique<KMemoryManager>(system); memory_manager->InitializeManager(KMemoryManager::Pool::Application, application_pool.GetAddress(), application_pool.GetEndAddress()); @@ -704,6 +706,27 @@ struct KernelCore::Impl { return port; } + std::weak_ptr<Kernel::ServiceThread> CreateServiceThread(KernelCore& kernel, + const std::string& name) { + auto service_thread = std::make_shared<Kernel::ServiceThread>(kernel, 1, name); + + service_threads_manager.QueueWork( + [this, service_thread]() { service_threads.emplace(service_thread); }); + + return service_thread; + } + + void ReleaseServiceThread(std::weak_ptr<Kernel::ServiceThread> service_thread) { + if (auto strong_ptr = service_thread.lock()) { + service_threads_manager.QueueWork( + [this, strong_ptr{std::move(strong_ptr)}]() { service_threads.erase(strong_ptr); }); + } + } + + void ClearServiceThreads() { + service_threads_manager.QueueWork([this]() { service_threads.clear(); }); + } + std::mutex server_ports_lock; std::mutex server_sessions_lock; std::mutex registered_objects_lock; @@ -759,6 +782,7 @@ struct KernelCore::Impl { // Threads used for services std::unordered_set<std::shared_ptr<Kernel::ServiceThread>> service_threads; + Common::ThreadWorker service_threads_manager; std::array<KThread*, Core::Hardware::NUM_CPU_CORES> suspend_threads; std::array<Core::CPUInterruptHandler, Core::Hardware::NUM_CPU_CORES> interrupts{}; @@ -774,6 +798,8 @@ struct KernelCore::Impl { std::array<u64, Core::Hardware::NUM_CPU_CORES> svc_ticks{}; + KWorkerTaskManager worker_task_manager; + // System context Core::System& system; }; @@ -1099,15 +1125,11 @@ void KernelCore::ExitSVCProfile() { } std::weak_ptr<Kernel::ServiceThread> KernelCore::CreateServiceThread(const std::string& name) { - auto service_thread = std::make_shared<Kernel::ServiceThread>(*this, 1, name); - impl->service_threads.emplace(service_thread); - return service_thread; + return impl->CreateServiceThread(*this, name); } void KernelCore::ReleaseServiceThread(std::weak_ptr<Kernel::ServiceThread> service_thread) { - if (auto strong_ptr = service_thread.lock()) { - impl->service_threads.erase(strong_ptr); - } + impl->ReleaseServiceThread(service_thread); } Init::KSlabResourceCounts& KernelCore::SlabResourceCounts() { @@ -1118,6 +1140,14 @@ const Init::KSlabResourceCounts& KernelCore::SlabResourceCounts() const { return impl->slab_resource_counts; } +KWorkerTaskManager& KernelCore::WorkerTaskManager() { + return impl->worker_task_manager; +} + +const KWorkerTaskManager& KernelCore::WorkerTaskManager() const { + return impl->worker_task_manager; +} + bool KernelCore::IsPhantomModeForSingleCore() const { return impl->IsPhantomModeForSingleCore(); } diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index b9b423908..0e04fc3bb 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -52,6 +52,7 @@ class KSharedMemory; class KSharedMemoryInfo; class KThread; class KTransferMemory; +class KWorkerTaskManager; class KWritableEvent; class KCodeMemory; class PhysicalCore; @@ -343,6 +344,12 @@ public: /// Gets the current slab resource counts. const Init::KSlabResourceCounts& SlabResourceCounts() const; + /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks. + KWorkerTaskManager& WorkerTaskManager(); + + /// Gets the current worker task manager, used for dispatching KThread/KProcess tasks. + const KWorkerTaskManager& WorkerTaskManager() const; + private: friend class KProcess; friend class KThread; diff --git a/src/core/hle/kernel/physical_core.cpp b/src/core/hle/kernel/physical_core.cpp index 7f02d9471..7477668e4 100644 --- a/src/core/hle/kernel/physical_core.cpp +++ b/src/core/hle/kernel/physical_core.cpp @@ -16,17 +16,25 @@ namespace Kernel { PhysicalCore::PhysicalCore(std::size_t core_index_, Core::System& system_, KScheduler& scheduler_, Core::CPUInterrupts& interrupts_) : core_index{core_index_}, system{system_}, scheduler{scheduler_}, - interrupts{interrupts_}, guard{std::make_unique<Common::SpinLock>()} {} + interrupts{interrupts_}, guard{std::make_unique<Common::SpinLock>()} { +#ifdef ARCHITECTURE_x86_64 + // TODO(bunnei): Initialization relies on a core being available. We may later replace this with + // a 32-bit instance of Dynarmic. This should be abstracted out to a CPU manager. + auto& kernel = system.Kernel(); + arm_interface = std::make_unique<Core::ARM_Dynarmic_64>( + system, interrupts, kernel.IsMulticore(), kernel.GetExclusiveMonitor(), core_index); +#else +#error Platform not supported yet. +#endif +} PhysicalCore::~PhysicalCore() = default; void PhysicalCore::Initialize([[maybe_unused]] bool is_64_bit) { #ifdef ARCHITECTURE_x86_64 auto& kernel = system.Kernel(); - if (is_64_bit) { - arm_interface = std::make_unique<Core::ARM_Dynarmic_64>( - system, interrupts, kernel.IsMulticore(), kernel.GetExclusiveMonitor(), core_index); - } else { + if (!is_64_bit) { + // We already initialized a 64-bit core, replace with a 32-bit one. arm_interface = std::make_unique<Core::ARM_Dynarmic_32>( system, interrupts, kernel.IsMulticore(), kernel.GetExclusiveMonitor(), core_index); } diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 250ef9042..c7f5140f4 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -168,6 +168,9 @@ constexpr bool IsValidSetMemoryPermission(MemoryPermission perm) { static ResultCode SetMemoryPermission(Core::System& system, VAddr address, u64 size, MemoryPermission perm) { + LOG_DEBUG(Kernel_SVC, "called, address=0x{:016X}, size=0x{:X}, perm=0x{:08X", address, size, + perm); + // Validate address / size. R_UNLESS(Common::IsAligned(address, PageSize), ResultInvalidAddress); R_UNLESS(Common::IsAligned(size, PageSize), ResultInvalidSize); @@ -186,46 +189,33 @@ static ResultCode SetMemoryPermission(Core::System& system, VAddr address, u64 s } static ResultCode SetMemoryAttribute(Core::System& system, VAddr address, u64 size, u32 mask, - u32 attribute) { + u32 attr) { LOG_DEBUG(Kernel_SVC, "called, address=0x{:016X}, size=0x{:X}, mask=0x{:08X}, attribute=0x{:08X}", address, - size, mask, attribute); - - if (!Common::Is4KBAligned(address)) { - LOG_ERROR(Kernel_SVC, "Address not page aligned (0x{:016X})", address); - return ResultInvalidAddress; - } + size, mask, attr); - if (size == 0 || !Common::Is4KBAligned(size)) { - LOG_ERROR(Kernel_SVC, "Invalid size (0x{:X}). Size must be non-zero and page aligned.", - size); - return ResultInvalidAddress; - } - - if (!IsValidAddressRange(address, size)) { - LOG_ERROR(Kernel_SVC, "Address range overflowed (Address: 0x{:016X}, Size: 0x{:016X})", - address, size); - return ResultInvalidCurrentMemory; - } + // Validate address / size. + R_UNLESS(Common::IsAligned(address, PageSize), ResultInvalidAddress); + R_UNLESS(Common::IsAligned(size, PageSize), ResultInvalidSize); + R_UNLESS(size > 0, ResultInvalidSize); + R_UNLESS((address < address + size), ResultInvalidCurrentMemory); - const auto attributes{static_cast<MemoryAttribute>(mask | attribute)}; - if (attributes != static_cast<MemoryAttribute>(mask) || - (attributes | MemoryAttribute::Uncached) != MemoryAttribute::Uncached) { - LOG_ERROR(Kernel_SVC, - "Memory attribute doesn't match the given mask (Attribute: 0x{:X}, Mask: {:X}", - attribute, mask); - return ResultInvalidCombination; - } + // Validate the attribute and mask. + constexpr u32 SupportedMask = static_cast<u32>(MemoryAttribute::Uncached); + R_UNLESS((mask | attr) == mask, ResultInvalidCombination); + R_UNLESS((mask | attr | SupportedMask) == SupportedMask, ResultInvalidCombination); + // Validate that the region is in range for the current process. auto& page_table{system.Kernel().CurrentProcess()->PageTable()}; + R_UNLESS(page_table.Contains(address, size), ResultInvalidCurrentMemory); - return page_table.SetMemoryAttribute(address, size, static_cast<KMemoryAttribute>(mask), - static_cast<KMemoryAttribute>(attribute)); + // Set the memory attribute. + return page_table.SetMemoryAttribute(address, size, mask, attr); } static ResultCode SetMemoryAttribute32(Core::System& system, u32 address, u32 size, u32 mask, - u32 attribute) { - return SetMemoryAttribute(system, address, size, mask, attribute); + u32 attr) { + return SetMemoryAttribute(system, address, size, mask, attr); } /// Maps a memory range into a different range. @@ -1319,6 +1309,8 @@ static ResultCode SetProcessMemoryPermission(Core::System& system, Handle proces R_UNLESS(Common::IsAligned(size, PageSize), ResultInvalidSize); R_UNLESS(size > 0, ResultInvalidSize); R_UNLESS((address < address + size), ResultInvalidCurrentMemory); + R_UNLESS(address == static_cast<uintptr_t>(address), ResultInvalidCurrentMemory); + R_UNLESS(size == static_cast<size_t>(size), ResultInvalidCurrentMemory); // Validate the memory permission. R_UNLESS(IsValidProcessMemoryPermission(perm), ResultInvalidNewMemoryPermission); @@ -1333,7 +1325,7 @@ static ResultCode SetProcessMemoryPermission(Core::System& system, Handle proces R_UNLESS(page_table.Contains(address, size), ResultInvalidCurrentMemory); // Set the memory permission. - return page_table.SetProcessMemoryPermission(address, size, ConvertToKMemoryPermission(perm)); + return page_table.SetProcessMemoryPermission(address, size, perm); } static ResultCode MapProcessMemory(Core::System& system, VAddr dst_address, Handle process_handle, @@ -1636,7 +1628,7 @@ static ResultCode MapProcessCodeMemory(Core::System& system, Handle process_hand return ResultInvalidMemoryRegion; } - return page_table.MapProcessCodeMemory(dst_address, src_address, size); + return page_table.MapCodeMemory(dst_address, src_address, size); } static ResultCode UnmapProcessCodeMemory(Core::System& system, Handle process_handle, @@ -1704,7 +1696,7 @@ static ResultCode UnmapProcessCodeMemory(Core::System& system, Handle process_ha return ResultInvalidMemoryRegion; } - return page_table.UnmapProcessCodeMemory(dst_address, src_address, size); + return page_table.UnmapCodeMemory(dst_address, src_address, size); } /// Exits the current process diff --git a/src/core/hle/kernel/svc_types.h b/src/core/hle/kernel/svc_types.h index ec463b97c..365e22e4e 100644 --- a/src/core/hle/kernel/svc_types.h +++ b/src/core/hle/kernel/svc_types.h @@ -32,6 +32,7 @@ enum class MemoryState : u32 { Kernel = 0x13, GeneratedCode = 0x14, CodeOut = 0x15, + Coverage = 0x16, }; DECLARE_ENUM_FLAG_OPERATORS(MemoryState); diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index 6e12381fb..a2bf7defb 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -37,7 +37,8 @@ namespace Service::HID { // Period time is obtained by measuring the number of samples in a second on HW using a homebrew constexpr auto pad_update_ns = std::chrono::nanoseconds{4 * 1000 * 1000}; // (4ms, 250Hz) constexpr auto mouse_keyboard_update_ns = std::chrono::nanoseconds{8 * 1000 * 1000}; // (8ms, 125Hz) -constexpr auto motion_update_ns = std::chrono::nanoseconds{5 * 1000 * 1000}; // (5ms, 200Hz) +// TODO: Correct update rate for motion is 5ms. Check why some games don't behave at that speed +constexpr auto motion_update_ns = std::chrono::nanoseconds{10 * 1000 * 1000}; // (10ms, 100Hz) constexpr std::size_t SHARED_MEMORY_SIZE = 0x40000; IAppletResource::IAppletResource(Core::System& system_, @@ -1175,7 +1176,8 @@ void Hid::SetNpadAnalogStickUseCenterClamp(Kernel::HLERequestContext& ctx) { const auto parameters{rp.PopRaw<Parameters>()}; - applet_resource->GetController<Controller_NPad>(HidController::NPad) + GetAppletResource() + ->GetController<Controller_NPad>(HidController::NPad) .SetAnalogStickUseCenterClamp(parameters.analog_stick_use_center_clamp); LOG_WARNING(Service_HID, diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp index 3782703d2..9fc7bb1b1 100644 --- a/src/core/hle/service/ldr/ldr.cpp +++ b/src/core/hle/service/ldr/ldr.cpp @@ -14,6 +14,7 @@ #include "core/hle/kernel/k_page_table.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/svc_results.h" +#include "core/hle/kernel/svc_types.h" #include "core/hle/service/ldr/ldr.h" #include "core/hle/service/service.h" #include "core/loader/nro.h" @@ -325,7 +326,7 @@ public: for (std::size_t retry = 0; retry < MAXIMUM_MAP_RETRIES; retry++) { auto& page_table{process->PageTable()}; const VAddr addr{GetRandomMapRegion(page_table, size)}; - const ResultCode result{page_table.MapProcessCodeMemory(addr, baseAddress, size)}; + const ResultCode result{page_table.MapCodeMemory(addr, baseAddress, size)}; if (result == Kernel::ResultInvalidCurrentMemory) { continue; @@ -351,12 +352,12 @@ public: if (bss_size) { auto block_guard = detail::ScopeExit([&] { - page_table.UnmapProcessCodeMemory(addr + nro_size, bss_addr, bss_size); - page_table.UnmapProcessCodeMemory(addr, nro_addr, nro_size); + page_table.UnmapCodeMemory(addr + nro_size, bss_addr, bss_size); + page_table.UnmapCodeMemory(addr, nro_addr, nro_size); }); const ResultCode result{ - page_table.MapProcessCodeMemory(addr + nro_size, bss_addr, bss_size)}; + page_table.MapCodeMemory(addr + nro_size, bss_addr, bss_size)}; if (result == Kernel::ResultInvalidCurrentMemory) { continue; @@ -397,12 +398,12 @@ public: nro_header.segment_headers[DATA_INDEX].memory_size); CASCADE_CODE(process->PageTable().SetProcessMemoryPermission( - text_start, ro_start - text_start, Kernel::KMemoryPermission::ReadAndExecute)); + text_start, ro_start - text_start, Kernel::Svc::MemoryPermission::ReadExecute)); CASCADE_CODE(process->PageTable().SetProcessMemoryPermission( - ro_start, data_start - ro_start, Kernel::KMemoryPermission::Read)); + ro_start, data_start - ro_start, Kernel::Svc::MemoryPermission::Read)); return process->PageTable().SetProcessMemoryPermission( - data_start, bss_end_addr - data_start, Kernel::KMemoryPermission::ReadAndWrite); + data_start, bss_end_addr - data_start, Kernel::Svc::MemoryPermission::ReadWrite); } void LoadModule(Kernel::HLERequestContext& ctx) { @@ -530,16 +531,19 @@ public: ResultCode UnmapNro(const NROInfo& info) { // Each region must be unmapped separately to validate memory state auto& page_table{system.CurrentProcess()->PageTable()}; - CASCADE_CODE(page_table.UnmapProcessCodeMemory(info.nro_address + info.text_size + - info.ro_size + info.data_size, - info.bss_address, info.bss_size)); - CASCADE_CODE(page_table.UnmapProcessCodeMemory( - info.nro_address + info.text_size + info.ro_size, - info.src_addr + info.text_size + info.ro_size, info.data_size)); - CASCADE_CODE(page_table.UnmapProcessCodeMemory( - info.nro_address + info.text_size, info.src_addr + info.text_size, info.ro_size)); - CASCADE_CODE( - page_table.UnmapProcessCodeMemory(info.nro_address, info.src_addr, info.text_size)); + + if (info.bss_size != 0) { + CASCADE_CODE(page_table.UnmapCodeMemory(info.nro_address + info.text_size + + info.ro_size + info.data_size, + info.bss_address, info.bss_size)); + } + + CASCADE_CODE(page_table.UnmapCodeMemory(info.nro_address + info.text_size + info.ro_size, + info.src_addr + info.text_size + info.ro_size, + info.data_size)); + CASCADE_CODE(page_table.UnmapCodeMemory(info.nro_address + info.text_size, + info.src_addr + info.text_size, info.ro_size)); + CASCADE_CODE(page_table.UnmapCodeMemory(info.nro_address, info.src_addr, info.text_size)); return ResultSuccess; } |