diff options
43 files changed, 1406 insertions, 256 deletions
diff --git a/src/common/bit_field.h b/src/common/bit_field.h index 7433c39ba..902e668e3 100644 --- a/src/common/bit_field.h +++ b/src/common/bit_field.h @@ -34,6 +34,7 @@ #include <limits> #include <type_traits> #include "common/common_funcs.h" +#include "common/swap.h" /* * Abstract bitfield class @@ -108,7 +109,7 @@ * symptoms. */ #pragma pack(1) -template <std::size_t Position, std::size_t Bits, typename T> +template <std::size_t Position, std::size_t Bits, typename T, typename EndianTag = LETag> struct BitField { private: // UnderlyingType is T for non-enum types and the underlying type of T if @@ -121,6 +122,8 @@ private: // We store the value as the unsigned type to avoid undefined behaviour on value shifting using StorageType = std::make_unsigned_t<UnderlyingType>; + using StorageTypeWithEndian = typename AddEndian<StorageType, EndianTag>::type; + public: /// Constants to allow limited introspection of fields if needed static constexpr std::size_t position = Position; @@ -170,7 +173,7 @@ public: } constexpr FORCE_INLINE void Assign(const T& value) { - storage = (storage & ~mask) | FormatValue(value); + storage = (static_cast<StorageType>(storage) & ~mask) | FormatValue(value); } constexpr T Value() const { @@ -182,7 +185,7 @@ public: } private: - StorageType storage; + StorageTypeWithEndian storage; static_assert(bits + position <= 8 * sizeof(T), "Bitfield out of range"); @@ -193,3 +196,6 @@ private: static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable in a BitField"); }; #pragma pack() + +template <std::size_t Position, std::size_t Bits, typename T> +using BitFieldBE = BitField<Position, Bits, T, BETag>; diff --git a/src/common/swap.h b/src/common/swap.h index 0e219747f..b3eab1324 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -17,6 +17,8 @@ #pragma once +#include <type_traits> + #if defined(_MSC_VER) #include <cstdlib> #elif defined(__linux__) @@ -170,7 +172,7 @@ struct swap_struct_t { using swapped_t = swap_struct_t; protected: - T value = T(); + T value; static T swap(T v) { return F::swap(v); @@ -605,52 +607,154 @@ struct swap_double_t { } }; -#if COMMON_LITTLE_ENDIAN -using u16_le = u16; -using u32_le = u32; -using u64_le = u64; +template <typename T> +struct swap_enum_t { + static_assert(std::is_enum_v<T>); + using base = std::underlying_type_t<T>; + +public: + swap_enum_t() = default; + swap_enum_t(const T& v) : value(swap(v)) {} + + swap_enum_t& operator=(const T& v) { + value = swap(v); + return *this; + } + + operator T() const { + return swap(value); + } + + explicit operator base() const { + return static_cast<base>(swap(value)); + } -using s16_le = s16; -using s32_le = s32; -using s64_le = s64; +protected: + T value{}; + // clang-format off + using swap_t = std::conditional_t< + std::is_same_v<base, u16>, swap_16_t<u16>, std::conditional_t< + std::is_same_v<base, s16>, swap_16_t<s16>, std::conditional_t< + std::is_same_v<base, u32>, swap_32_t<u32>, std::conditional_t< + std::is_same_v<base, s32>, swap_32_t<s32>, std::conditional_t< + std::is_same_v<base, u64>, swap_64_t<u64>, std::conditional_t< + std::is_same_v<base, s64>, swap_64_t<s64>, void>>>>>>; + // clang-format on + static T swap(T x) { + return static_cast<T>(swap_t::swap(static_cast<base>(x))); + } +}; -using float_le = float; -using double_le = double; +struct SwapTag {}; // Use the different endianness from the system +struct KeepTag {}; // Use the same endianness as the system -using u64_be = swap_struct_t<u64, swap_64_t<u64>>; -using s64_be = swap_struct_t<s64, swap_64_t<s64>>; +template <typename T, typename Tag> +struct AddEndian; -using u32_be = swap_struct_t<u32, swap_32_t<u32>>; -using s32_be = swap_struct_t<s32, swap_32_t<s32>>; +// KeepTag specializations -using u16_be = swap_struct_t<u16, swap_16_t<u16>>; -using s16_be = swap_struct_t<s16, swap_16_t<s16>>; +template <typename T> +struct AddEndian<T, KeepTag> { + using type = T; +}; -using float_be = swap_struct_t<float, swap_float_t<float>>; -using double_be = swap_struct_t<double, swap_double_t<double>>; -#else +// SwapTag specializations + +template <> +struct AddEndian<u8, SwapTag> { + using type = u8; +}; + +template <> +struct AddEndian<u16, SwapTag> { + using type = swap_struct_t<u16, swap_16_t<u16>>; +}; + +template <> +struct AddEndian<u32, SwapTag> { + using type = swap_struct_t<u32, swap_32_t<u32>>; +}; -using u64_le = swap_struct_t<u64, swap_64_t<u64>>; -using s64_le = swap_struct_t<s64, swap_64_t<s64>>; +template <> +struct AddEndian<u64, SwapTag> { + using type = swap_struct_t<u64, swap_64_t<u64>>; +}; + +template <> +struct AddEndian<s8, SwapTag> { + using type = s8; +}; -using u32_le = swap_struct_t<u32, swap_32_t<u32>>; -using s32_le = swap_struct_t<s32, swap_32_t<s32>>; +template <> +struct AddEndian<s16, SwapTag> { + using type = swap_struct_t<s16, swap_16_t<s16>>; +}; -using u16_le = swap_struct_t<u16, swap_16_t<u16>>; -using s16_le = swap_struct_t<s16, swap_16_t<s16>>; +template <> +struct AddEndian<s32, SwapTag> { + using type = swap_struct_t<s32, swap_32_t<s32>>; +}; + +template <> +struct AddEndian<s64, SwapTag> { + using type = swap_struct_t<s64, swap_64_t<s64>>; +}; + +template <> +struct AddEndian<float, SwapTag> { + using type = swap_struct_t<float, swap_float_t<float>>; +}; + +template <> +struct AddEndian<double, SwapTag> { + using type = swap_struct_t<double, swap_double_t<double>>; +}; + +template <typename T> +struct AddEndian<T, SwapTag> { + static_assert(std::is_enum_v<T>); + using type = swap_enum_t<T>; +}; -using float_le = swap_struct_t<float, swap_float_t<float>>; -using double_le = swap_struct_t<double, swap_double_t<double>>; +// Alias LETag/BETag as KeepTag/SwapTag depending on the system +#if COMMON_LITTLE_ENDIAN -using u16_be = u16; -using u32_be = u32; -using u64_be = u64; +using LETag = KeepTag; +using BETag = SwapTag; -using s16_be = s16; -using s32_be = s32; -using s64_be = s64; +#else -using float_be = float; -using double_be = double; +using BETag = KeepTag; +using LETag = SwapTag; #endif + +// Aliases for LE types +using u16_le = AddEndian<u16, LETag>::type; +using u32_le = AddEndian<u32, LETag>::type; +using u64_le = AddEndian<u64, LETag>::type; + +using s16_le = AddEndian<s16, LETag>::type; +using s32_le = AddEndian<s32, LETag>::type; +using s64_le = AddEndian<s64, LETag>::type; + +template <typename T> +using enum_le = std::enable_if_t<std::is_enum_v<T>, typename AddEndian<T, LETag>::type>; + +using float_le = AddEndian<float, LETag>::type; +using double_le = AddEndian<double, LETag>::type; + +// Aliases for BE types +using u16_be = AddEndian<u16, BETag>::type; +using u32_be = AddEndian<u32, BETag>::type; +using u64_be = AddEndian<u64, BETag>::type; + +using s16_be = AddEndian<s16, BETag>::type; +using s32_be = AddEndian<s32, BETag>::type; +using s64_be = AddEndian<s64, BETag>::type; + +template <typename T> +using enum_be = std::enable_if_t<std::is_enum_v<T>, typename AddEndian<T, BETag>::type>; + +using float_be = AddEndian<float, BETag>::type; +using double_be = AddEndian<double, BETag>::type; diff --git a/src/common/uint128.cpp b/src/common/uint128.cpp index 2238a52c5..32bf56730 100644 --- a/src/common/uint128.cpp +++ b/src/common/uint128.cpp @@ -1,3 +1,7 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + #ifdef _MSC_VER #include <intrin.h> diff --git a/src/common/uint128.h b/src/common/uint128.h index 52e6b46eb..a3be2a2cb 100644 --- a/src/common/uint128.h +++ b/src/common/uint128.h @@ -1,3 +1,8 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once #include <utility> #include "common/common_types.h" diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 6319414ba..bbbe60896 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -31,6 +31,8 @@ add_library(core STATIC file_sys/bis_factory.h file_sys/card_image.cpp file_sys/card_image.h + file_sys/cheat_engine.cpp + file_sys/cheat_engine.h file_sys/content_archive.cpp file_sys/content_archive.h file_sys/control_metadata.cpp @@ -107,6 +109,8 @@ add_library(core STATIC hle/kernel/client_port.h hle/kernel/client_session.cpp hle/kernel/client_session.h + hle/kernel/code_set.cpp + hle/kernel/code_set.h hle/kernel/errors.h hle/kernel/handle_table.cpp hle/kernel/handle_table.h diff --git a/src/core/core.cpp b/src/core/core.cpp index 89b3fb418..a88e332be 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -32,6 +32,7 @@ #include "core/perf_stats.h" #include "core/settings.h" #include "core/telemetry_session.h" +#include "file_sys/cheat_engine.h" #include "frontend/applets/profile_select.h" #include "frontend/applets/software_keyboard.h" #include "frontend/applets/web_browser.h" @@ -205,6 +206,7 @@ struct System::Impl { GDBStub::Shutdown(); Service::Shutdown(); service_manager.reset(); + cheat_engine.reset(); telemetry_session.reset(); gpu_core.reset(); @@ -255,6 +257,8 @@ struct System::Impl { CpuCoreManager cpu_core_manager; bool is_powered_on = false; + std::unique_ptr<FileSys::CheatEngine> cheat_engine; + /// Frontend applets std::unique_ptr<Core::Frontend::ProfileSelectApplet> profile_selector; std::unique_ptr<Core::Frontend::SoftwareKeyboardApplet> software_keyboard; @@ -453,6 +457,13 @@ Tegra::DebugContext* System::GetGPUDebugContext() const { return impl->debug_context.get(); } +void System::RegisterCheatList(const std::vector<FileSys::CheatList>& list, + const std::string& build_id, VAddr code_region_start, + VAddr code_region_end) { + impl->cheat_engine = + std::make_unique<FileSys::CheatEngine>(list, build_id, code_region_start, code_region_end); +} + void System::SetFilesystem(std::shared_ptr<FileSys::VfsFilesystem> vfs) { impl->virtual_filesystem = std::move(vfs); } diff --git a/src/core/core.h b/src/core/core.h index ba76a41d8..4d83b93cc 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -20,6 +20,7 @@ class WebBrowserApplet; } // namespace Core::Frontend namespace FileSys { +class CheatList; class VfsFilesystem; } // namespace FileSys @@ -253,6 +254,9 @@ public: std::shared_ptr<FileSys::VfsFilesystem> GetFilesystem() const; + void RegisterCheatList(const std::vector<FileSys::CheatList>& list, const std::string& build_id, + VAddr code_region_start, VAddr code_region_end); + void SetProfileSelector(std::unique_ptr<Frontend::ProfileSelectApplet> applet); const Frontend::ProfileSelectApplet& GetProfileSelector() const; diff --git a/src/core/file_sys/cheat_engine.cpp b/src/core/file_sys/cheat_engine.cpp new file mode 100644 index 000000000..09ca9d705 --- /dev/null +++ b/src/core/file_sys/cheat_engine.cpp @@ -0,0 +1,493 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include <locale> +#include "common/hex_util.h" +#include "common/microprofile.h" +#include "common/swap.h" +#include "core/core.h" +#include "core/core_timing.h" +#include "core/core_timing_util.h" +#include "core/file_sys/cheat_engine.h" +#include "core/hle/kernel/process.h" +#include "core/hle/service/hid/controllers/controller_base.h" +#include "core/hle/service/hid/controllers/npad.h" +#include "core/hle/service/hid/hid.h" +#include "core/hle/service/sm/sm.h" + +namespace FileSys { + +constexpr u64 CHEAT_ENGINE_TICKS = Core::Timing::BASE_CLOCK_RATE / 60; +constexpr u32 KEYPAD_BITMASK = 0x3FFFFFF; + +u64 Cheat::Address() const { + u64 out; + std::memcpy(&out, raw.data(), sizeof(u64)); + return Common::swap64(out) & 0xFFFFFFFFFF; +} + +u64 Cheat::ValueWidth(u64 offset) const { + return Value(offset, width); +} + +u64 Cheat::Value(u64 offset, u64 width) const { + u64 out; + std::memcpy(&out, raw.data() + offset, sizeof(u64)); + out = Common::swap64(out); + if (width == 8) + return out; + return out & ((1ull << (width * CHAR_BIT)) - 1); +} + +u32 Cheat::KeypadValue() const { + u32 out; + std::memcpy(&out, raw.data(), sizeof(u32)); + return Common::swap32(out) & 0x0FFFFFFF; +} + +void CheatList::SetMemoryParameters(VAddr main_begin, VAddr heap_begin, VAddr main_end, + VAddr heap_end, MemoryWriter writer, MemoryReader reader) { + this->main_region_begin = main_begin; + this->main_region_end = main_end; + this->heap_region_begin = heap_begin; + this->heap_region_end = heap_end; + this->writer = writer; + this->reader = reader; +} + +MICROPROFILE_DEFINE(Cheat_Engine, "Add-Ons", "Cheat Engine", MP_RGB(70, 200, 70)); + +void CheatList::Execute() { + MICROPROFILE_SCOPE(Cheat_Engine); + + std::fill(scratch.begin(), scratch.end(), 0); + in_standard = false; + for (std::size_t i = 0; i < master_list.size(); ++i) { + LOG_DEBUG(Common_Filesystem, "Executing block #{:08X} ({})", i, master_list[i].first); + current_block = i; + ExecuteBlock(master_list[i].second); + } + + in_standard = true; + for (std::size_t i = 0; i < standard_list.size(); ++i) { + LOG_DEBUG(Common_Filesystem, "Executing block #{:08X} ({})", i, standard_list[i].first); + current_block = i; + ExecuteBlock(standard_list[i].second); + } +} + +CheatList::CheatList(ProgramSegment master, ProgramSegment standard) + : master_list(master), standard_list(standard) {} + +bool CheatList::EvaluateConditional(const Cheat& cheat) const { + using ComparisonFunction = bool (*)(u64, u64); + constexpr std::array<ComparisonFunction, 6> comparison_functions{ + [](u64 a, u64 b) { return a > b; }, [](u64 a, u64 b) { return a >= b; }, + [](u64 a, u64 b) { return a < b; }, [](u64 a, u64 b) { return a <= b; }, + [](u64 a, u64 b) { return a == b; }, [](u64 a, u64 b) { return a != b; }, + }; + + if (cheat.type == CodeType::ConditionalInput) { + const auto applet_resource = Core::System::GetInstance() + .ServiceManager() + .GetService<Service::HID::Hid>("hid") + ->GetAppletResource(); + if (applet_resource == nullptr) { + LOG_WARNING( + Common_Filesystem, + "Attempted to evaluate input conditional, but applet resource is not initialized!"); + return false; + } + + const auto press_state = + applet_resource + ->GetController<Service::HID::Controller_NPad>(Service::HID::HidController::NPad) + .GetAndResetPressState(); + return ((press_state & cheat.KeypadValue()) & KEYPAD_BITMASK) != 0; + } + + ASSERT(cheat.type == CodeType::Conditional); + + const auto offset = + cheat.memory_type == MemoryType::MainNSO ? main_region_begin : heap_region_begin; + ASSERT(static_cast<u8>(cheat.comparison_op.Value()) < 6); + auto* function = comparison_functions[static_cast<u8>(cheat.comparison_op.Value())]; + const auto addr = cheat.Address() + offset; + + return function(reader(cheat.width, SanitizeAddress(addr)), cheat.ValueWidth(8)); +} + +void CheatList::ProcessBlockPairs(const Block& block) { + block_pairs.clear(); + + u64 scope = 0; + std::map<u64, u64> pairs; + + for (std::size_t i = 0; i < block.size(); ++i) { + const auto& cheat = block[i]; + + switch (cheat.type) { + case CodeType::Conditional: + case CodeType::ConditionalInput: + pairs.insert_or_assign(scope, i); + ++scope; + break; + case CodeType::EndConditional: { + --scope; + const auto idx = pairs.at(scope); + block_pairs.insert_or_assign(idx, i); + break; + } + case CodeType::Loop: { + if (cheat.end_of_loop) { + --scope; + const auto idx = pairs.at(scope); + block_pairs.insert_or_assign(idx, i); + } else { + pairs.insert_or_assign(scope, i); + ++scope; + } + break; + } + } + } +} + +void CheatList::WriteImmediate(const Cheat& cheat) { + const auto offset = + cheat.memory_type == MemoryType::MainNSO ? main_region_begin : heap_region_begin; + const auto& register_3 = scratch.at(cheat.register_3); + + const auto addr = cheat.Address() + offset + register_3; + LOG_DEBUG(Common_Filesystem, "writing value={:016X} to addr={:016X}", addr, + cheat.Value(8, cheat.width)); + writer(cheat.width, SanitizeAddress(addr), cheat.ValueWidth(8)); +} + +void CheatList::BeginConditional(const Cheat& cheat) { + if (EvaluateConditional(cheat)) { + return; + } + + const auto iter = block_pairs.find(current_index); + ASSERT(iter != block_pairs.end()); + current_index = iter->second - 1; +} + +void CheatList::EndConditional(const Cheat& cheat) { + LOG_DEBUG(Common_Filesystem, "Ending conditional block."); +} + +void CheatList::Loop(const Cheat& cheat) { + if (cheat.end_of_loop.Value()) + ASSERT(!cheat.end_of_loop.Value()); + + auto& register_3 = scratch.at(cheat.register_3); + const auto iter = block_pairs.find(current_index); + ASSERT(iter != block_pairs.end()); + ASSERT(iter->first < iter->second); + + for (int i = cheat.Value(4, 4); i >= 0; --i) { + register_3 = i; + for (std::size_t c = iter->first + 1; c < iter->second; ++c) { + current_index = c; + ExecuteSingleCheat( + (in_standard ? standard_list : master_list)[current_block].second[c]); + } + } + + current_index = iter->second; +} + +void CheatList::LoadImmediate(const Cheat& cheat) { + auto& register_3 = scratch.at(cheat.register_3); + + LOG_DEBUG(Common_Filesystem, "setting register={:01X} equal to value={:016X}", cheat.register_3, + cheat.Value(4, 8)); + register_3 = cheat.Value(4, 8); +} + +void CheatList::LoadIndexed(const Cheat& cheat) { + const auto offset = + cheat.memory_type == MemoryType::MainNSO ? main_region_begin : heap_region_begin; + auto& register_3 = scratch.at(cheat.register_3); + + const auto addr = (cheat.load_from_register.Value() ? register_3 : offset) + cheat.Address(); + LOG_DEBUG(Common_Filesystem, "writing indexed value to register={:01X}, addr={:016X}", + cheat.register_3, addr); + register_3 = reader(cheat.width, SanitizeAddress(addr)); +} + +void CheatList::StoreIndexed(const Cheat& cheat) { + const auto& register_3 = scratch.at(cheat.register_3); + + const auto addr = + register_3 + (cheat.add_additional_register.Value() ? scratch.at(cheat.register_6) : 0); + LOG_DEBUG(Common_Filesystem, "writing value={:016X} to addr={:016X}", + cheat.Value(4, cheat.width), addr); + writer(cheat.width, SanitizeAddress(addr), cheat.ValueWidth(4)); +} + +void CheatList::RegisterArithmetic(const Cheat& cheat) { + using ArithmeticFunction = u64 (*)(u64, u64); + constexpr std::array<ArithmeticFunction, 5> arithmetic_functions{ + [](u64 a, u64 b) { return a + b; }, [](u64 a, u64 b) { return a - b; }, + [](u64 a, u64 b) { return a * b; }, [](u64 a, u64 b) { return a << b; }, + [](u64 a, u64 b) { return a >> b; }, + }; + + using ArithmeticOverflowCheck = bool (*)(u64, u64); + constexpr std::array<ArithmeticOverflowCheck, 5> arithmetic_overflow_checks{ + [](u64 a, u64 b) { return a > (std::numeric_limits<u64>::max() - b); }, // a + b + [](u64 a, u64 b) { return a > (std::numeric_limits<u64>::max() + b); }, // a - b + [](u64 a, u64 b) { return a > (std::numeric_limits<u64>::max() / b); }, // a * b + [](u64 a, u64 b) { return b >= 64 || (a & ~((1ull << (64 - b)) - 1)) != 0; }, // a << b + [](u64 a, u64 b) { return b >= 64 || (a & ((1ull << b) - 1)) != 0; }, // a >> b + }; + + static_assert(sizeof(arithmetic_functions) == sizeof(arithmetic_overflow_checks), + "Missing or have extra arithmetic overflow checks compared to functions!"); + + auto& register_3 = scratch.at(cheat.register_3); + + ASSERT(static_cast<u8>(cheat.arithmetic_op.Value()) < 5); + auto* function = arithmetic_functions[static_cast<u8>(cheat.arithmetic_op.Value())]; + auto* overflow_function = + arithmetic_overflow_checks[static_cast<u8>(cheat.arithmetic_op.Value())]; + LOG_DEBUG(Common_Filesystem, "performing arithmetic with register={:01X}, value={:016X}", + cheat.register_3, cheat.ValueWidth(4)); + + if (overflow_function(register_3, cheat.ValueWidth(4))) { + LOG_WARNING(Common_Filesystem, + "overflow will occur when performing arithmetic operation={:02X} with operands " + "a={:016X}, b={:016X}!", + static_cast<u8>(cheat.arithmetic_op.Value()), register_3, cheat.ValueWidth(4)); + } + + register_3 = function(register_3, cheat.ValueWidth(4)); +} + +void CheatList::BeginConditionalInput(const Cheat& cheat) { + if (EvaluateConditional(cheat)) + return; + + const auto iter = block_pairs.find(current_index); + ASSERT(iter != block_pairs.end()); + current_index = iter->second - 1; +} + +VAddr CheatList::SanitizeAddress(VAddr in) const { + if ((in < main_region_begin || in >= main_region_end) && + (in < heap_region_begin || in >= heap_region_end)) { + LOG_ERROR(Common_Filesystem, + "Cheat attempting to access memory at invalid address={:016X}, if this persists, " + "the cheat may be incorrect. However, this may be normal early in execution if " + "the game has not properly set up yet.", + in); + return 0; ///< Invalid addresses will hard crash + } + + return in; +} + +void CheatList::ExecuteSingleCheat(const Cheat& cheat) { + using CheatOperationFunction = void (CheatList::*)(const Cheat&); + constexpr std::array<CheatOperationFunction, 9> cheat_operation_functions{ + &CheatList::WriteImmediate, &CheatList::BeginConditional, + &CheatList::EndConditional, &CheatList::Loop, + &CheatList::LoadImmediate, &CheatList::LoadIndexed, + &CheatList::StoreIndexed, &CheatList::RegisterArithmetic, + &CheatList::BeginConditionalInput, + }; + + const auto index = static_cast<u8>(cheat.type.Value()); + ASSERT(index < sizeof(cheat_operation_functions)); + const auto op = cheat_operation_functions[index]; + (this->*op)(cheat); +} + +void CheatList::ExecuteBlock(const Block& block) { + encountered_loops.clear(); + + ProcessBlockPairs(block); + for (std::size_t i = 0; i < block.size(); ++i) { + current_index = i; + ExecuteSingleCheat(block[i]); + i = current_index; + } +} + +CheatParser::~CheatParser() = default; + +CheatList CheatParser::MakeCheatList(CheatList::ProgramSegment master, + CheatList::ProgramSegment standard) const { + return {master, standard}; +} + +TextCheatParser::~TextCheatParser() = default; + +CheatList TextCheatParser::Parse(const std::vector<u8>& data) const { + std::stringstream ss; + ss.write(reinterpret_cast<const char*>(data.data()), data.size()); + + std::vector<std::string> lines; + std::string stream_line; + while (std::getline(ss, stream_line)) { + // Remove a trailing \r + if (!stream_line.empty() && stream_line.back() == '\r') + stream_line.pop_back(); + lines.push_back(std::move(stream_line)); + } + + CheatList::ProgramSegment master_list; + CheatList::ProgramSegment standard_list; + + for (std::size_t i = 0; i < lines.size(); ++i) { + auto line = lines[i]; + + if (!line.empty() && (line[0] == '[' || line[0] == '{')) { + const auto master = line[0] == '{'; + const auto begin = master ? line.find('{') : line.find('['); + const auto end = master ? line.rfind('}') : line.rfind(']'); + + ASSERT(begin != std::string::npos && end != std::string::npos); + + const std::string patch_name{line.begin() + begin + 1, line.begin() + end}; + CheatList::Block block{}; + + while (i < lines.size() - 1) { + line = lines[++i]; + if (!line.empty() && (line[0] == '[' || line[0] == '{')) { + --i; + break; + } + + if (line.size() < 8) + continue; + + Cheat out{}; + out.raw = ParseSingleLineCheat(line); + block.push_back(out); + } + + (master ? master_list : standard_list).emplace_back(patch_name, block); + } + } + + return MakeCheatList(master_list, standard_list); +} + +std::array<u8, 16> TextCheatParser::ParseSingleLineCheat(const std::string& line) const { + std::array<u8, 16> out{}; + + if (line.size() < 8) + return out; + + const auto word1 = Common::HexStringToArray<sizeof(u32)>(std::string_view{line.data(), 8}); + std::memcpy(out.data(), word1.data(), sizeof(u32)); + + if (line.size() < 17 || line[8] != ' ') + return out; + + const auto word2 = Common::HexStringToArray<sizeof(u32)>(std::string_view{line.data() + 9, 8}); + std::memcpy(out.data() + sizeof(u32), word2.data(), sizeof(u32)); + + if (line.size() < 26 || line[17] != ' ') { + // Perform shifting in case value is truncated early. + const auto type = static_cast<CodeType>((out[0] & 0xF0) >> 4); + if (type == CodeType::Loop || type == CodeType::LoadImmediate || + type == CodeType::StoreIndexed || type == CodeType::RegisterArithmetic) { + std::memcpy(out.data() + 8, out.data() + 4, sizeof(u32)); + std::memset(out.data() + 4, 0, sizeof(u32)); + } + + return out; + } + + const auto word3 = Common::HexStringToArray<sizeof(u32)>(std::string_view{line.data() + 18, 8}); + std::memcpy(out.data() + 2 * sizeof(u32), word3.data(), sizeof(u32)); + + if (line.size() < 35 || line[26] != ' ') { + // Perform shifting in case value is truncated early. + const auto type = static_cast<CodeType>((out[0] & 0xF0) >> 4); + if (type == CodeType::WriteImmediate || type == CodeType::Conditional) { + std::memcpy(out.data() + 12, out.data() + 8, sizeof(u32)); + std::memset(out.data() + 8, 0, sizeof(u32)); + } + + return out; + } + + const auto word4 = Common::HexStringToArray<sizeof(u32)>(std::string_view{line.data() + 27, 8}); + std::memcpy(out.data() + 3 * sizeof(u32), word4.data(), sizeof(u32)); + + return out; +} + +u64 MemoryReadImpl(u32 width, VAddr addr) { + switch (width) { + case 1: + return Memory::Read8(addr); + case 2: + return Memory::Read16(addr); + case 4: + return Memory::Read32(addr); + case 8: + return Memory::Read64(addr); + default: + UNREACHABLE(); + return 0; + } +} + +void MemoryWriteImpl(u32 width, VAddr addr, u64 value) { + switch (width) { + case 1: + Memory::Write8(addr, static_cast<u8>(value)); + break; + case 2: + Memory::Write16(addr, static_cast<u16>(value)); + break; + case 4: + Memory::Write32(addr, static_cast<u32>(value)); + break; + case 8: + Memory::Write64(addr, value); + break; + default: + UNREACHABLE(); + } +} + +CheatEngine::CheatEngine(std::vector<CheatList> cheats, const std::string& build_id, + VAddr code_region_start, VAddr code_region_end) + : cheats(std::move(cheats)) { + auto& core_timing{Core::System::GetInstance().CoreTiming()}; + event = core_timing.RegisterEvent( + "CheatEngine::FrameCallback::" + build_id, + [this](u64 userdata, s64 cycles_late) { FrameCallback(userdata, cycles_late); }); + core_timing.ScheduleEvent(CHEAT_ENGINE_TICKS, event); + + const auto& vm_manager = Core::System::GetInstance().CurrentProcess()->VMManager(); + for (auto& list : this->cheats) { + list.SetMemoryParameters(code_region_start, vm_manager.GetHeapRegionBaseAddress(), + code_region_end, vm_manager.GetHeapRegionEndAddress(), + &MemoryWriteImpl, &MemoryReadImpl); + } +} + +CheatEngine::~CheatEngine() { + auto& core_timing{Core::System::GetInstance().CoreTiming()}; + core_timing.UnscheduleEvent(event, 0); +} + +void CheatEngine::FrameCallback(u64 userdata, int cycles_late) { + for (auto& list : cheats) + list.Execute(); + + auto& core_timing{Core::System::GetInstance().CoreTiming()}; + core_timing.ScheduleEvent(CHEAT_ENGINE_TICKS - cycles_late, event); +} + +} // namespace FileSys diff --git a/src/core/file_sys/cheat_engine.h b/src/core/file_sys/cheat_engine.h new file mode 100644 index 000000000..7ed69a2c8 --- /dev/null +++ b/src/core/file_sys/cheat_engine.h @@ -0,0 +1,227 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <map> +#include <set> +#include <vector> +#include <queue> +#include "common/bit_field.h" +#include "common/common_types.h" + +namespace Core::Timing { +struct EventType; +} + +namespace FileSys { + +enum class CodeType : u32 { + // 0TMR00AA AAAAAAAA YYYYYYYY YYYYYYYY + // Writes a T sized value Y to the address A added to the value of register R in memory domain M + WriteImmediate = 0, + + // 1TMC00AA AAAAAAAA YYYYYYYY YYYYYYYY + // Compares the T sized value Y to the value at address A in memory domain M using the + // conditional function C. If success, continues execution. If failure, jumps to the matching + // EndConditional statement. + Conditional = 1, + + // 20000000 + // Terminates a Conditional or ConditionalInput block. + EndConditional = 2, + + // 300R0000 VVVVVVVV + // Starts looping V times, storing the current count in register R. + // Loop block is terminated with a matching 310R0000. + Loop = 3, + + // 400R0000 VVVVVVVV VVVVVVVV + // Sets the value of register R to the value V. + LoadImmediate = 4, + + // 5TMRI0AA AAAAAAAA + // Sets the value of register R to the value of width T at address A in memory domain M, with + // the current value of R added to the address if I == 1. + LoadIndexed = 5, + + // 6T0RIFG0 VVVVVVVV VVVVVVVV + // Writes the value V of width T to the memory address stored in register R. Adds the value of + // register G to the final calculation if F is nonzero. Increments the value of register R by T + // after operation if I is nonzero. + StoreIndexed = 6, + + // 7T0RA000 VVVVVVVV + // Performs the arithmetic operation A on the value in register R and the value V of width T, + // storing the result in register R. + RegisterArithmetic = 7, + + // 8KKKKKKK + // Checks to see if any of the buttons defined by the bitmask K are pressed. If any are, + // execution continues. If none are, execution skips to the next EndConditional command. + ConditionalInput = 8, +}; + +enum class MemoryType : u32 { + // Addressed relative to start of main NSO + MainNSO = 0, + + // Addressed relative to start of heap + Heap = 1, +}; + +enum class ArithmeticOp : u32 { + Add = 0, + Sub = 1, + Mult = 2, + LShift = 3, + RShift = 4, +}; + +enum class ComparisonOp : u32 { + GreaterThan = 1, + GreaterThanEqual = 2, + LessThan = 3, + LessThanEqual = 4, + Equal = 5, + Inequal = 6, +}; + +union Cheat { + std::array<u8, 16> raw; + + BitField<4, 4, CodeType> type; + BitField<0, 4, u32> width; // Can be 1, 2, 4, or 8. Measured in bytes. + BitField<0, 4, u32> end_of_loop; + BitField<12, 4, MemoryType> memory_type; + BitField<8, 4, u32> register_3; + BitField<8, 4, ComparisonOp> comparison_op; + BitField<20, 4, u32> load_from_register; + BitField<20, 4, u32> increment_register; + BitField<20, 4, ArithmeticOp> arithmetic_op; + BitField<16, 4, u32> add_additional_register; + BitField<28, 4, u32> register_6; + + u64 Address() const; + u64 ValueWidth(u64 offset) const; + u64 Value(u64 offset, u64 width) const; + u32 KeypadValue() const; +}; + +class CheatParser; + +// Represents a full collection of cheats for a game. The Execute function should be called every +// interval that all cheats should be executed. Clients should not directly instantiate this class +// (hence private constructor), they should instead receive an instance from CheatParser, which +// guarantees the list is always in an acceptable state. +class CheatList { +public: + friend class CheatParser; + + using Block = std::vector<Cheat>; + using ProgramSegment = std::vector<std::pair<std::string, Block>>; + + // (width in bytes, address, value) + using MemoryWriter = void (*)(u32, VAddr, u64); + // (width in bytes, address) -> value + using MemoryReader = u64 (*)(u32, VAddr); + + void SetMemoryParameters(VAddr main_begin, VAddr heap_begin, VAddr main_end, VAddr heap_end, + MemoryWriter writer, MemoryReader reader); + + void Execute(); + +private: + CheatList(ProgramSegment master, ProgramSegment standard); + + void ProcessBlockPairs(const Block& block); + void ExecuteSingleCheat(const Cheat& cheat); + + void ExecuteBlock(const Block& block); + + bool EvaluateConditional(const Cheat& cheat) const; + + // Individual cheat operations + void WriteImmediate(const Cheat& cheat); + void BeginConditional(const Cheat& cheat); + void EndConditional(const Cheat& cheat); + void Loop(const Cheat& cheat); + void LoadImmediate(const Cheat& cheat); + void LoadIndexed(const Cheat& cheat); + void StoreIndexed(const Cheat& cheat); + void RegisterArithmetic(const Cheat& cheat); + void BeginConditionalInput(const Cheat& cheat); + + VAddr SanitizeAddress(VAddr in) const; + + // Master Codes are defined as codes that cannot be disabled and are run prior to all + // others. + ProgramSegment master_list; + // All other codes + ProgramSegment standard_list; + + bool in_standard = false; + + // 16 (0x0-0xF) scratch registers that can be used by cheats + std::array<u64, 16> scratch{}; + + MemoryWriter writer = nullptr; + MemoryReader reader = nullptr; + + u64 main_region_begin{}; + u64 heap_region_begin{}; + u64 main_region_end{}; + u64 heap_region_end{}; + + u64 current_block{}; + // The current index of the cheat within the current Block + u64 current_index{}; + + // The 'stack' of the program. When a conditional or loop statement is encountered, its index is + // pushed onto this queue. When a end block is encountered, the condition is checked. + std::map<u64, u64> block_pairs; + + std::set<u64> encountered_loops; +}; + +// Intermediary class that parses a text file or other disk format for storing cheats into a +// CheatList object, that can be used for execution. +class CheatParser { +public: + virtual ~CheatParser(); + + virtual CheatList Parse(const std::vector<u8>& data) const = 0; + +protected: + CheatList MakeCheatList(CheatList::ProgramSegment master, + CheatList::ProgramSegment standard) const; +}; + +// CheatParser implementation that parses text files +class TextCheatParser final : public CheatParser { +public: + ~TextCheatParser() override; + + CheatList Parse(const std::vector<u8>& data) const override; + +private: + std::array<u8, 16> ParseSingleLineCheat(const std::string& line) const; +}; + +// Class that encapsulates a CheatList and manages its interaction with memory and CoreTiming +class CheatEngine final { +public: + CheatEngine(std::vector<CheatList> cheats, const std::string& build_id, VAddr code_region_start, + VAddr code_region_end); + ~CheatEngine(); + +private: + void FrameCallback(u64 userdata, int cycles_late); + + Core::Timing::EventType* event; + + std::vector<CheatList> cheats; +}; + +} // namespace FileSys diff --git a/src/core/file_sys/content_archive.h b/src/core/file_sys/content_archive.h index 5d4d05c82..15b9e6624 100644 --- a/src/core/file_sys/content_archive.h +++ b/src/core/file_sys/content_archive.h @@ -24,13 +24,26 @@ namespace FileSys { union NCASectionHeader; +/// Describes the type of content within an NCA archive. enum class NCAContentType : u8 { + /// Executable-related data Program = 0, + + /// Metadata. Meta = 1, + + /// Access control data. Control = 2, + + /// Information related to the game manual + /// e.g. Legal information, etc. Manual = 3, + + /// System data. Data = 4, - Data_Unknown5 = 5, ///< Seems to be used on some system archives + + /// Data that can be accessed by applications. + PublicData = 5, }; enum class NCASectionCryptoType : u8 { diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 61706966e..2b09e5d35 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -7,6 +7,7 @@ #include <cstddef> #include <cstring> +#include "common/file_util.h" #include "common/hex_util.h" #include "common/logging/log.h" #include "core/file_sys/content_archive.h" @@ -232,6 +233,57 @@ bool PatchManager::HasNSOPatch(const std::array<u8, 32>& build_id_) const { return !CollectPatches(patch_dirs, build_id).empty(); } +static std::optional<CheatList> ReadCheatFileFromFolder(u64 title_id, + const std::array<u8, 0x20>& build_id_, + const VirtualDir& base_path, bool upper) { + const auto build_id_raw = Common::HexArrayToString(build_id_, upper); + const auto build_id = build_id_raw.substr(0, sizeof(u64) * 2); + const auto file = base_path->GetFile(fmt::format("{}.txt", build_id)); + + if (file == nullptr) { + LOG_INFO(Common_Filesystem, "No cheats file found for title_id={:016X}, build_id={}", + title_id, build_id); + return std::nullopt; + } + + std::vector<u8> data(file->GetSize()); + if (file->Read(data.data(), data.size()) != data.size()) { + LOG_INFO(Common_Filesystem, "Failed to read cheats file for title_id={:016X}, build_id={}", + title_id, build_id); + return std::nullopt; + } + + TextCheatParser parser; + return parser.Parse(data); +} + +std::vector<CheatList> PatchManager::CreateCheatList(const std::array<u8, 32>& build_id_) const { + std::vector<CheatList> out; + + const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id); + auto patch_dirs = load_dir->GetSubdirectories(); + std::sort(patch_dirs.begin(), patch_dirs.end(), + [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); }); + + out.reserve(patch_dirs.size()); + for (const auto& subdir : patch_dirs) { + auto cheats_dir = subdir->GetSubdirectory("cheats"); + if (cheats_dir != nullptr) { + auto res = ReadCheatFileFromFolder(title_id, build_id_, cheats_dir, true); + if (res.has_value()) { + out.push_back(std::move(*res)); + continue; + } + + res = ReadCheatFileFromFolder(title_id, build_id_, cheats_dir, false); + if (res.has_value()) + out.push_back(std::move(*res)); + } + } + + return out; +} + static void ApplyLayeredFS(VirtualFile& romfs, u64 title_id, ContentRecordType type) { const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id); if ((type != ContentRecordType::Program && type != ContentRecordType::Data) || @@ -403,6 +455,8 @@ std::map<std::string, std::string, std::less<>> PatchManager::GetPatchVersionNam } if (IsDirValidAndNonEmpty(mod->GetSubdirectory("romfs"))) AppendCommaIfNotEmpty(types, "LayeredFS"); + if (IsDirValidAndNonEmpty(mod->GetSubdirectory("cheats"))) + AppendCommaIfNotEmpty(types, "Cheats"); if (types.empty()) continue; diff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.h index b8a1652fd..3e3ac6aca 100644 --- a/src/core/file_sys/patch_manager.h +++ b/src/core/file_sys/patch_manager.h @@ -8,6 +8,7 @@ #include <memory> #include <string> #include "common/common_types.h" +#include "core/file_sys/cheat_engine.h" #include "core/file_sys/nca_metadata.h" #include "core/file_sys/vfs.h" @@ -45,6 +46,9 @@ public: // Used to prevent expensive copies in NSO loader. bool HasNSOPatch(const std::array<u8, 0x20>& build_id) const; + // Creates a CheatList object with all + std::vector<CheatList> CreateCheatList(const std::array<u8, 0x20>& build_id) const; + // Currently tracked RomFS patches: // - Game Updates // - LayeredFS diff --git a/src/core/file_sys/registered_cache.cpp b/src/core/file_sys/registered_cache.cpp index 128199063..1c6bacace 100644 --- a/src/core/file_sys/registered_cache.cpp +++ b/src/core/file_sys/registered_cache.cpp @@ -94,7 +94,7 @@ static ContentRecordType GetCRTypeFromNCAType(NCAContentType type) { case NCAContentType::Control: return ContentRecordType::Control; case NCAContentType::Data: - case NCAContentType::Data_Unknown5: + case NCAContentType::PublicData: return ContentRecordType::Data; case NCAContentType::Manual: // TODO(DarkLordZach): Peek at NCA contents to differentiate Manual and Legal. diff --git a/src/core/hle/ipc.h b/src/core/hle/ipc.h index 455d1f346..fae54bcc7 100644 --- a/src/core/hle/ipc.h +++ b/src/core/hle/ipc.h @@ -39,10 +39,10 @@ struct CommandHeader { union { u32_le raw_low; BitField<0, 16, CommandType> type; - BitField<16, 4, u32_le> num_buf_x_descriptors; - BitField<20, 4, u32_le> num_buf_a_descriptors; - BitField<24, 4, u32_le> num_buf_b_descriptors; - BitField<28, 4, u32_le> num_buf_w_descriptors; + BitField<16, 4, u32> num_buf_x_descriptors; + BitField<20, 4, u32> num_buf_a_descriptors; + BitField<24, 4, u32> num_buf_b_descriptors; + BitField<28, 4, u32> num_buf_w_descriptors; }; enum class BufferDescriptorCFlag : u32 { @@ -53,28 +53,28 @@ struct CommandHeader { union { u32_le raw_high; - BitField<0, 10, u32_le> data_size; + BitField<0, 10, u32> data_size; BitField<10, 4, BufferDescriptorCFlag> buf_c_descriptor_flags; - BitField<31, 1, u32_le> enable_handle_descriptor; + BitField<31, 1, u32> enable_handle_descriptor; }; }; static_assert(sizeof(CommandHeader) == 8, "CommandHeader size is incorrect"); union HandleDescriptorHeader { u32_le raw_high; - BitField<0, 1, u32_le> send_current_pid; - BitField<1, 4, u32_le> num_handles_to_copy; - BitField<5, 4, u32_le> num_handles_to_move; + BitField<0, 1, u32> send_current_pid; + BitField<1, 4, u32> num_handles_to_copy; + BitField<5, 4, u32> num_handles_to_move; }; static_assert(sizeof(HandleDescriptorHeader) == 4, "HandleDescriptorHeader size is incorrect"); struct BufferDescriptorX { union { - BitField<0, 6, u32_le> counter_bits_0_5; - BitField<6, 3, u32_le> address_bits_36_38; - BitField<9, 3, u32_le> counter_bits_9_11; - BitField<12, 4, u32_le> address_bits_32_35; - BitField<16, 16, u32_le> size; + BitField<0, 6, u32> counter_bits_0_5; + BitField<6, 3, u32> address_bits_36_38; + BitField<9, 3, u32> counter_bits_9_11; + BitField<12, 4, u32> address_bits_32_35; + BitField<16, 16, u32> size; }; u32_le address_bits_0_31; @@ -103,10 +103,10 @@ struct BufferDescriptorABW { u32_le address_bits_0_31; union { - BitField<0, 2, u32_le> flags; - BitField<2, 3, u32_le> address_bits_36_38; - BitField<24, 4, u32_le> size_bits_32_35; - BitField<28, 4, u32_le> address_bits_32_35; + BitField<0, 2, u32> flags; + BitField<2, 3, u32> address_bits_36_38; + BitField<24, 4, u32> size_bits_32_35; + BitField<28, 4, u32> address_bits_32_35; }; VAddr Address() const { @@ -128,8 +128,8 @@ struct BufferDescriptorC { u32_le address_bits_0_31; union { - BitField<0, 16, u32_le> address_bits_32_47; - BitField<16, 16, u32_le> size; + BitField<0, 16, u32> address_bits_32_47; + BitField<16, 16, u32> size; }; VAddr Address() const { @@ -167,8 +167,8 @@ struct DomainMessageHeader { struct { union { BitField<0, 8, CommandType> command; - BitField<8, 8, u32_le> input_object_count; - BitField<16, 16, u32_le> size; + BitField<8, 8, u32> input_object_count; + BitField<16, 16, u32> size; }; u32_le object_id; INSERT_PADDING_WORDS(2); diff --git a/src/core/hle/kernel/code_set.cpp b/src/core/hle/kernel/code_set.cpp new file mode 100644 index 000000000..1f434e9af --- /dev/null +++ b/src/core/hle/kernel/code_set.cpp @@ -0,0 +1,12 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/hle/kernel/code_set.h" + +namespace Kernel { + +CodeSet::CodeSet() = default; +CodeSet::~CodeSet() = default; + +} // namespace Kernel diff --git a/src/core/hle/kernel/code_set.h b/src/core/hle/kernel/code_set.h new file mode 100644 index 000000000..834fd23d2 --- /dev/null +++ b/src/core/hle/kernel/code_set.h @@ -0,0 +1,90 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <cstddef> +#include <memory> +#include <vector> + +#include "common/common_types.h" + +namespace Kernel { + +/** + * Represents executable data that may be loaded into a kernel process. + * + * A code set consists of three basic segments: + * - A code (AKA text) segment, + * - A read-only data segment (rodata) + * - A data segment + * + * The code segment is the portion of the object file that contains + * executable instructions. + * + * The read-only data segment in the portion of the object file that + * contains (as one would expect) read-only data, such as fixed constant + * values and data structures. + * + * The data segment is similar to the read-only data segment -- it contains + * variables and data structures that have predefined values, however, + * entities within this segment can be modified. + */ +struct CodeSet final { + /// A single segment within a code set. + struct Segment final { + /// The byte offset that this segment is located at. + std::size_t offset = 0; + + /// The address to map this segment to. + VAddr addr = 0; + + /// The size of this segment in bytes. + u32 size = 0; + }; + + explicit CodeSet(); + ~CodeSet(); + + CodeSet(const CodeSet&) = delete; + CodeSet& operator=(const CodeSet&) = delete; + + CodeSet(CodeSet&&) = default; + CodeSet& operator=(CodeSet&&) = default; + + Segment& CodeSegment() { + return segments[0]; + } + + const Segment& CodeSegment() const { + return segments[0]; + } + + Segment& RODataSegment() { + return segments[1]; + } + + const Segment& RODataSegment() const { + return segments[1]; + } + + Segment& DataSegment() { + return segments[2]; + } + + const Segment& DataSegment() const { + return segments[2]; + } + + /// The overall data that backs this code set. + std::shared_ptr<std::vector<u8>> memory; + + /// The segments that comprise this code set. + std::array<Segment, 3> segments; + + /// The entry point address for this code set. + VAddr entrypoint = 0; +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index 0743670ad..98e87313b 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp @@ -2,7 +2,6 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include <map> #include <utility> #include <vector> @@ -10,8 +9,11 @@ #include "core/core.h" #include "core/hle/kernel/errors.h" #include "core/hle/kernel/handle_table.h" +#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/mutex.h" #include "core/hle/kernel/object.h" +#include "core/hle/kernel/process.h" +#include "core/hle/kernel/scheduler.h" #include "core/hle/kernel/thread.h" #include "core/hle/result.h" #include "core/memory.h" @@ -57,41 +59,47 @@ static void TransferMutexOwnership(VAddr mutex_addr, SharedPtr<Thread> current_t } } -ResultCode Mutex::TryAcquire(HandleTable& handle_table, VAddr address, Handle holding_thread_handle, +Mutex::Mutex(Core::System& system) : system{system} {} +Mutex::~Mutex() = default; + +ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle, Handle requesting_thread_handle) { // The mutex address must be 4-byte aligned if ((address % sizeof(u32)) != 0) { return ERR_INVALID_ADDRESS; } + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); + Thread* const current_thread = system.CurrentScheduler().GetCurrentThread(); SharedPtr<Thread> holding_thread = handle_table.Get<Thread>(holding_thread_handle); SharedPtr<Thread> requesting_thread = handle_table.Get<Thread>(requesting_thread_handle); // TODO(Subv): It is currently unknown if it is possible to lock a mutex in behalf of another // thread. - ASSERT(requesting_thread == GetCurrentThread()); + ASSERT(requesting_thread == current_thread); - u32 addr_value = Memory::Read32(address); + const u32 addr_value = Memory::Read32(address); // If the mutex isn't being held, just return success. if (addr_value != (holding_thread_handle | Mutex::MutexHasWaitersFlag)) { return RESULT_SUCCESS; } - if (holding_thread == nullptr) + if (holding_thread == nullptr) { return ERR_INVALID_HANDLE; + } // Wait until the mutex is released - GetCurrentThread()->SetMutexWaitAddress(address); - GetCurrentThread()->SetWaitHandle(requesting_thread_handle); + current_thread->SetMutexWaitAddress(address); + current_thread->SetWaitHandle(requesting_thread_handle); - GetCurrentThread()->SetStatus(ThreadStatus::WaitMutex); - GetCurrentThread()->InvalidateWakeupCallback(); + current_thread->SetStatus(ThreadStatus::WaitMutex); + current_thread->InvalidateWakeupCallback(); // Update the lock holder thread's priority to prevent priority inversion. - holding_thread->AddMutexWaiter(GetCurrentThread()); + holding_thread->AddMutexWaiter(current_thread); - Core::System::GetInstance().PrepareReschedule(); + system.PrepareReschedule(); return RESULT_SUCCESS; } @@ -102,7 +110,8 @@ ResultCode Mutex::Release(VAddr address) { return ERR_INVALID_ADDRESS; } - auto [thread, num_waiters] = GetHighestPriorityMutexWaitingThread(GetCurrentThread(), address); + auto* const current_thread = system.CurrentScheduler().GetCurrentThread(); + auto [thread, num_waiters] = GetHighestPriorityMutexWaitingThread(current_thread, address); // There are no more threads waiting for the mutex, release it completely. if (thread == nullptr) { @@ -111,7 +120,7 @@ ResultCode Mutex::Release(VAddr address) { } // Transfer the ownership of the mutex from the previous owner to the new one. - TransferMutexOwnership(address, GetCurrentThread(), thread); + TransferMutexOwnership(address, current_thread, thread); u32 mutex_value = thread->GetWaitHandle(); diff --git a/src/core/hle/kernel/mutex.h b/src/core/hle/kernel/mutex.h index 81e62d497..b904de2e8 100644 --- a/src/core/hle/kernel/mutex.h +++ b/src/core/hle/kernel/mutex.h @@ -5,32 +5,34 @@ #pragma once #include "common/common_types.h" -#include "core/hle/kernel/object.h" union ResultCode; -namespace Kernel { +namespace Core { +class System; +} -class HandleTable; -class Thread; +namespace Kernel { class Mutex final { public: + explicit Mutex(Core::System& system); + ~Mutex(); + /// Flag that indicates that a mutex still has threads waiting for it. static constexpr u32 MutexHasWaitersFlag = 0x40000000; /// Mask of the bits in a mutex address value that contain the mutex owner. static constexpr u32 MutexOwnerMask = 0xBFFFFFFF; /// Attempts to acquire a mutex at the specified address. - static ResultCode TryAcquire(HandleTable& handle_table, VAddr address, - Handle holding_thread_handle, Handle requesting_thread_handle); + ResultCode TryAcquire(VAddr address, Handle holding_thread_handle, + Handle requesting_thread_handle); /// Releases the mutex at the specified address. - static ResultCode Release(VAddr address); + ResultCode Release(VAddr address); private: - Mutex() = default; - ~Mutex() = default; + Core::System& system; }; } // namespace Kernel diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 65c51003d..0d782e4ba 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -9,6 +9,7 @@ #include "common/logging/log.h" #include "core/core.h" #include "core/file_sys/program_metadata.h" +#include "core/hle/kernel/code_set.h" #include "core/hle/kernel/errors.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" @@ -50,9 +51,6 @@ void SetupMainThread(Process& owner_process, KernelCore& kernel, VAddr entry_poi } } // Anonymous namespace -CodeSet::CodeSet() = default; -CodeSet::~CodeSet() = default; - SharedPtr<Process> Process::Create(Core::System& system, std::string&& name) { auto& kernel = system.Kernel(); @@ -212,7 +210,7 @@ void Process::FreeTLSSlot(VAddr tls_address) { } void Process::LoadModule(CodeSet module_, VAddr base_addr) { - const auto MapSegment = [&](CodeSet::Segment& segment, VMAPermission permissions, + const auto MapSegment = [&](const CodeSet::Segment& segment, VMAPermission permissions, MemoryState memory_state) { const auto vma = vm_manager .MapMemoryBlock(segment.addr + base_addr, module_.memory, @@ -222,16 +220,17 @@ void Process::LoadModule(CodeSet module_, VAddr base_addr) { }; // Map CodeSet segments - MapSegment(module_.CodeSegment(), VMAPermission::ReadExecute, MemoryState::CodeStatic); - MapSegment(module_.RODataSegment(), VMAPermission::Read, MemoryState::CodeMutable); - MapSegment(module_.DataSegment(), VMAPermission::ReadWrite, MemoryState::CodeMutable); + MapSegment(module_.CodeSegment(), VMAPermission::ReadExecute, MemoryState::Code); + MapSegment(module_.RODataSegment(), VMAPermission::Read, MemoryState::CodeData); + MapSegment(module_.DataSegment(), VMAPermission::ReadWrite, MemoryState::CodeData); // Clear instruction cache in CPU JIT system.InvalidateCpuInstructionCaches(); } Process::Process(Core::System& system) - : WaitObject{system.Kernel()}, address_arbiter{system}, system{system} {} + : WaitObject{system.Kernel()}, address_arbiter{system}, mutex{system}, system{system} {} + Process::~Process() = default; void Process::Acquire(Thread* thread) { diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index 47ffd4ad3..1bd7bf5c1 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -7,13 +7,13 @@ #include <array> #include <bitset> #include <cstddef> -#include <memory> #include <string> #include <vector> #include <boost/container/static_vector.hpp> #include "common/common_types.h" #include "core/hle/kernel/address_arbiter.h" #include "core/hle/kernel/handle_table.h" +#include "core/hle/kernel/mutex.h" #include "core/hle/kernel/process_capability.h" #include "core/hle/kernel/vm_manager.h" #include "core/hle/kernel/wait_object.h" @@ -33,6 +33,8 @@ class KernelCore; class ResourceLimit; class Thread; +struct CodeSet; + struct AddressMapping { // Address and size must be page-aligned VAddr address; @@ -65,46 +67,6 @@ enum class ProcessStatus { DebugBreak, }; -struct CodeSet final { - struct Segment { - std::size_t offset = 0; - VAddr addr = 0; - u32 size = 0; - }; - - explicit CodeSet(); - ~CodeSet(); - - Segment& CodeSegment() { - return segments[0]; - } - - const Segment& CodeSegment() const { - return segments[0]; - } - - Segment& RODataSegment() { - return segments[1]; - } - - const Segment& RODataSegment() const { - return segments[1]; - } - - Segment& DataSegment() { - return segments[2]; - } - - const Segment& DataSegment() const { - return segments[2]; - } - - std::shared_ptr<std::vector<u8>> memory; - - std::array<Segment, 3> segments; - VAddr entrypoint = 0; -}; - class Process final : public WaitObject { public: enum : u64 { @@ -165,6 +127,16 @@ public: return address_arbiter; } + /// Gets a reference to the process' mutex lock. + Mutex& GetMutex() { + return mutex; + } + + /// Gets a const reference to the process' mutex lock + const Mutex& GetMutex() const { + return mutex; + } + /// Gets the current status of the process ProcessStatus GetStatus() const { return status; @@ -327,6 +299,11 @@ private: /// Per-process address arbiter. AddressArbiter address_arbiter; + /// The per-process mutex lock instance used for handling various + /// forms of services, such as lock arbitration, and condition + /// variable related facilities. + Mutex mutex; + /// Random values for svcGetInfo RandomEntropy std::array<u64, RANDOM_ENTROPY_SIZE> random_entropy; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 047fa0c19..a6a17efe7 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -551,9 +551,9 @@ static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr, return ERR_INVALID_ADDRESS; } - auto& handle_table = Core::CurrentProcess()->GetHandleTable(); - return Mutex::TryAcquire(handle_table, mutex_addr, holding_thread_handle, - requesting_thread_handle); + auto* const current_process = Core::System::GetInstance().Kernel().CurrentProcess(); + return current_process->GetMutex().TryAcquire(mutex_addr, holding_thread_handle, + requesting_thread_handle); } /// Unlock a mutex @@ -571,7 +571,8 @@ static ResultCode ArbitrateUnlock(VAddr mutex_addr) { return ERR_INVALID_ADDRESS; } - return Mutex::Release(mutex_addr); + auto* const current_process = Core::System::GetInstance().Kernel().CurrentProcess(); + return current_process->GetMutex().Release(mutex_addr); } enum class BreakType : u32 { @@ -1340,11 +1341,15 @@ static ResultCode WaitProcessWideKeyAtomic(VAddr mutex_addr, VAddr condition_var "called mutex_addr={:X}, condition_variable_addr={:X}, thread_handle=0x{:08X}, timeout={}", mutex_addr, condition_variable_addr, thread_handle, nano_seconds); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + auto* const current_process = Core::System::GetInstance().Kernel().CurrentProcess(); + const auto& handle_table = current_process->GetHandleTable(); SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle); ASSERT(thread); - CASCADE_CODE(Mutex::Release(mutex_addr)); + const auto release_result = current_process->GetMutex().Release(mutex_addr); + if (release_result.IsError()) { + return release_result; + } SharedPtr<Thread> current_thread = GetCurrentThread(); current_thread->SetCondVarWaitAddress(condition_variable_addr); diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index 3def3e52c..22bf55ce7 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp @@ -20,16 +20,16 @@ namespace Kernel { namespace { const char* GetMemoryStateName(MemoryState state) { static constexpr const char* names[] = { - "Unmapped", "Io", - "Normal", "CodeStatic", - "CodeMutable", "Heap", - "Shared", "Unknown1", - "ModuleCodeStatic", "ModuleCodeMutable", - "IpcBuffer0", "Stack", - "ThreadLocal", "TransferMemoryIsolated", - "TransferMemory", "ProcessMemory", - "Inaccessible", "IpcBuffer1", - "IpcBuffer3", "KernelStack", + "Unmapped", "Io", + "Normal", "Code", + "CodeData", "Heap", + "Shared", "Unknown1", + "ModuleCode", "ModuleCodeData", + "IpcBuffer0", "Stack", + "ThreadLocal", "TransferMemoryIsolated", + "TransferMemory", "ProcessMemory", + "Inaccessible", "IpcBuffer1", + "IpcBuffer3", "KernelStack", }; return names[ToSvcMemoryState(state)]; diff --git a/src/core/hle/kernel/vm_manager.h b/src/core/hle/kernel/vm_manager.h index b96980f8f..7cdff6094 100644 --- a/src/core/hle/kernel/vm_manager.h +++ b/src/core/hle/kernel/vm_manager.h @@ -165,12 +165,12 @@ enum class MemoryState : u32 { Unmapped = 0x00, Io = 0x01 | FlagMapped, Normal = 0x02 | FlagMapped | FlagQueryPhysicalAddressAllowed, - CodeStatic = 0x03 | CodeFlags | FlagMapProcess, - CodeMutable = 0x04 | CodeFlags | FlagMapProcess | FlagCodeMemory, + Code = 0x03 | CodeFlags | FlagMapProcess, + CodeData = 0x04 | DataFlags | FlagMapProcess | FlagCodeMemory, Heap = 0x05 | DataFlags | FlagCodeMemory, Shared = 0x06 | FlagMapped | FlagMemoryPoolAllocated, - ModuleCodeStatic = 0x08 | CodeFlags | FlagModule | FlagMapProcess, - ModuleCodeMutable = 0x09 | DataFlags | FlagModule | FlagMapProcess | FlagCodeMemory, + ModuleCode = 0x08 | CodeFlags | FlagModule | FlagMapProcess, + ModuleCodeData = 0x09 | DataFlags | FlagModule | FlagMapProcess | FlagCodeMemory, IpcBuffer0 = 0x0A | FlagMapped | FlagQueryPhysicalAddressAllowed | FlagMemoryPoolAllocated | IPCFlags | FlagSharedDevice | FlagSharedDeviceAligned, @@ -617,6 +617,9 @@ private: VAddr new_map_region_base = 0; VAddr new_map_region_end = 0; + VAddr main_code_region_base = 0; + VAddr main_code_region_end = 0; + VAddr tls_io_region_base = 0; VAddr tls_io_region_end = 0; diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index c750d70ac..9c44e27c6 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -215,7 +215,21 @@ IDisplayController::IDisplayController() : ServiceFramework("IDisplayController" IDisplayController::~IDisplayController() = default; -IDebugFunctions::IDebugFunctions() : ServiceFramework("IDebugFunctions") {} +IDebugFunctions::IDebugFunctions() : ServiceFramework{"IDebugFunctions"} { + // clang-format off + static const FunctionInfo functions[] = { + {0, nullptr, "NotifyMessageToHomeMenuForDebug"}, + {1, nullptr, "OpenMainApplication"}, + {10, nullptr, "EmulateButtonEvent"}, + {20, nullptr, "InvalidateTransitionLayer"}, + {30, nullptr, "RequestLaunchApplicationWithUserAndArgumentForDebug"}, + {40, nullptr, "GetAppletResourceUsageInfo"}, + }; + // clang-format on + + RegisterHandlers(functions); +} + IDebugFunctions::~IDebugFunctions() = default; ISelfController::ISelfController(std::shared_ptr<NVFlinger::NVFlinger> nvflinger) diff --git a/src/core/hle/service/hid/controllers/debug_pad.h b/src/core/hle/service/hid/controllers/debug_pad.h index 929035034..e584b92ec 100644 --- a/src/core/hle/service/hid/controllers/debug_pad.h +++ b/src/core/hle/service/hid/controllers/debug_pad.h @@ -41,20 +41,20 @@ private: struct PadState { union { u32_le raw{}; - BitField<0, 1, u32_le> a; - BitField<1, 1, u32_le> b; - BitField<2, 1, u32_le> x; - BitField<3, 1, u32_le> y; - BitField<4, 1, u32_le> l; - BitField<5, 1, u32_le> r; - BitField<6, 1, u32_le> zl; - BitField<7, 1, u32_le> zr; - BitField<8, 1, u32_le> plus; - BitField<9, 1, u32_le> minus; - BitField<10, 1, u32_le> d_left; - BitField<11, 1, u32_le> d_up; - BitField<12, 1, u32_le> d_right; - BitField<13, 1, u32_le> d_down; + BitField<0, 1, u32> a; + BitField<1, 1, u32> b; + BitField<2, 1, u32> x; + BitField<3, 1, u32> y; + BitField<4, 1, u32> l; + BitField<5, 1, u32> r; + BitField<6, 1, u32> zl; + BitField<7, 1, u32> zr; + BitField<8, 1, u32> plus; + BitField<9, 1, u32> minus; + BitField<10, 1, u32> d_left; + BitField<11, 1, u32> d_up; + BitField<12, 1, u32> d_right; + BitField<13, 1, u32> d_down; }; }; static_assert(sizeof(PadState) == 0x4, "PadState is an invalid size"); @@ -62,7 +62,7 @@ private: struct Attributes { union { u32_le raw{}; - BitField<0, 1, u32_le> connected; + BitField<0, 1, u32> connected; }; }; static_assert(sizeof(Attributes) == 0x4, "Attributes is an invalid size"); diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h index 18c7a94e6..4ff50b3cd 100644 --- a/src/core/hle/service/hid/controllers/npad.h +++ b/src/core/hle/service/hid/controllers/npad.h @@ -39,13 +39,13 @@ public: union { u32_le raw{}; - BitField<0, 1, u32_le> pro_controller; - BitField<1, 1, u32_le> handheld; - BitField<2, 1, u32_le> joycon_dual; - BitField<3, 1, u32_le> joycon_left; - BitField<4, 1, u32_le> joycon_right; + BitField<0, 1, u32> pro_controller; + BitField<1, 1, u32> handheld; + BitField<2, 1, u32> joycon_dual; + BitField<3, 1, u32> joycon_left; + BitField<4, 1, u32> joycon_right; - BitField<6, 1, u32_le> pokeball; // TODO(ogniK): Confirm when possible + BitField<6, 1, u32> pokeball; // TODO(ogniK): Confirm when possible }; }; static_assert(sizeof(NPadType) == 4, "NPadType is an invalid size"); @@ -150,43 +150,43 @@ private: union { u64_le raw{}; // Button states - BitField<0, 1, u64_le> a; - BitField<1, 1, u64_le> b; - BitField<2, 1, u64_le> x; - BitField<3, 1, u64_le> y; - BitField<4, 1, u64_le> l_stick; - BitField<5, 1, u64_le> r_stick; - BitField<6, 1, u64_le> l; - BitField<7, 1, u64_le> r; - BitField<8, 1, u64_le> zl; - BitField<9, 1, u64_le> zr; - BitField<10, 1, u64_le> plus; - BitField<11, 1, u64_le> minus; + BitField<0, 1, u64> a; + BitField<1, 1, u64> b; + BitField<2, 1, u64> x; + BitField<3, 1, u64> y; + BitField<4, 1, u64> l_stick; + BitField<5, 1, u64> r_stick; + BitField<6, 1, u64> l; + BitField<7, 1, u64> r; + BitField<8, 1, u64> zl; + BitField<9, 1, u64> zr; + BitField<10, 1, u64> plus; + BitField<11, 1, u64> minus; // D-Pad - BitField<12, 1, u64_le> d_left; - BitField<13, 1, u64_le> d_up; - BitField<14, 1, u64_le> d_right; - BitField<15, 1, u64_le> d_down; + BitField<12, 1, u64> d_left; + BitField<13, 1, u64> d_up; + BitField<14, 1, u64> d_right; + BitField<15, 1, u64> d_down; // Left JoyStick - BitField<16, 1, u64_le> l_stick_left; - BitField<17, 1, u64_le> l_stick_up; - BitField<18, 1, u64_le> l_stick_right; - BitField<19, 1, u64_le> l_stick_down; + BitField<16, 1, u64> l_stick_left; + BitField<17, 1, u64> l_stick_up; + BitField<18, 1, u64> l_stick_right; + BitField<19, 1, u64> l_stick_down; // Right JoyStick - BitField<20, 1, u64_le> r_stick_left; - BitField<21, 1, u64_le> r_stick_up; - BitField<22, 1, u64_le> r_stick_right; - BitField<23, 1, u64_le> r_stick_down; + BitField<20, 1, u64> r_stick_left; + BitField<21, 1, u64> r_stick_up; + BitField<22, 1, u64> r_stick_right; + BitField<23, 1, u64> r_stick_down; // Not always active? - BitField<24, 1, u64_le> left_sl; - BitField<25, 1, u64_le> left_sr; + BitField<24, 1, u64> left_sl; + BitField<25, 1, u64> left_sr; - BitField<26, 1, u64_le> right_sl; - BitField<27, 1, u64_le> right_sr; + BitField<26, 1, u64> right_sl; + BitField<27, 1, u64> right_sr; }; }; static_assert(sizeof(ControllerPadState) == 8, "ControllerPadState is an invalid size"); @@ -200,12 +200,12 @@ private: struct ConnectionState { union { u32_le raw{}; - BitField<0, 1, u32_le> IsConnected; - BitField<1, 1, u32_le> IsWired; - BitField<2, 1, u32_le> IsLeftJoyConnected; - BitField<3, 1, u32_le> IsLeftJoyWired; - BitField<4, 1, u32_le> IsRightJoyConnected; - BitField<5, 1, u32_le> IsRightJoyWired; + BitField<0, 1, u32> IsConnected; + BitField<1, 1, u32> IsWired; + BitField<2, 1, u32> IsLeftJoyConnected; + BitField<3, 1, u32> IsLeftJoyWired; + BitField<4, 1, u32> IsRightJoyConnected; + BitField<5, 1, u32> IsRightJoyWired; }; }; static_assert(sizeof(ConnectionState) == 4, "ConnectionState is an invalid size"); @@ -240,23 +240,23 @@ private: struct NPadProperties { union { s64_le raw{}; - BitField<11, 1, s64_le> is_vertical; - BitField<12, 1, s64_le> is_horizontal; - BitField<13, 1, s64_le> use_plus; - BitField<14, 1, s64_le> use_minus; + BitField<11, 1, s64> is_vertical; + BitField<12, 1, s64> is_horizontal; + BitField<13, 1, s64> use_plus; + BitField<14, 1, s64> use_minus; }; }; struct NPadDevice { union { u32_le raw{}; - BitField<0, 1, s32_le> pro_controller; - BitField<1, 1, s32_le> handheld; - BitField<2, 1, s32_le> handheld_left; - BitField<3, 1, s32_le> handheld_right; - BitField<4, 1, s32_le> joycon_left; - BitField<5, 1, s32_le> joycon_right; - BitField<6, 1, s32_le> pokeball; + BitField<0, 1, s32> pro_controller; + BitField<1, 1, s32> handheld; + BitField<2, 1, s32> handheld_left; + BitField<3, 1, s32> handheld_right; + BitField<4, 1, s32> joycon_left; + BitField<5, 1, s32> joycon_right; + BitField<6, 1, s32> pokeball; }; }; diff --git a/src/core/hle/service/hid/controllers/touchscreen.h b/src/core/hle/service/hid/controllers/touchscreen.h index 012b6e0dd..76fc340e9 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.h +++ b/src/core/hle/service/hid/controllers/touchscreen.h @@ -33,8 +33,8 @@ private: struct Attributes { union { u32 raw{}; - BitField<0, 1, u32_le> start_touch; - BitField<1, 1, u32_le> end_touch; + BitField<0, 1, u32> start_touch; + BitField<1, 1, u32> end_touch; }; }; static_assert(sizeof(Attributes) == 0x4, "Attributes is an invalid size"); diff --git a/src/core/hle/service/hid/hid.h b/src/core/hle/service/hid/hid.h index 7cc58db4c..498602de5 100644 --- a/src/core/hle/service/hid/hid.h +++ b/src/core/hle/service/hid/hid.h @@ -4,6 +4,9 @@ #pragma once +#include "core/hle/service/hid/controllers/controller_base.h" +#include "core/hle/service/service.h" + #include "controllers/controller_base.h" #include "core/hle/service/service.h" diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp index 9df7ac50f..d65693fc7 100644 --- a/src/core/hle/service/ldr/ldr.cpp +++ b/src/core/hle/service/ldr/ldr.cpp @@ -319,15 +319,14 @@ public: } ASSERT(vm_manager - .MirrorMemory(*map_address, nro_addr, nro_size, - Kernel::MemoryState::ModuleCodeStatic) + .MirrorMemory(*map_address, nro_addr, nro_size, Kernel::MemoryState::ModuleCode) .IsSuccess()); ASSERT(vm_manager.UnmapRange(nro_addr, nro_size).IsSuccess()); if (bss_size > 0) { ASSERT(vm_manager .MirrorMemory(*map_address + nro_size, bss_addr, bss_size, - Kernel::MemoryState::ModuleCodeStatic) + Kernel::MemoryState::ModuleCode) .IsSuccess()); ASSERT(vm_manager.UnmapRange(bss_addr, bss_size).IsSuccess()); } @@ -388,8 +387,7 @@ public: const auto& nro_size = iter->second.size; ASSERT(vm_manager - .MirrorMemory(heap_addr, mapped_addr, nro_size, - Kernel::MemoryState::ModuleCodeStatic) + .MirrorMemory(heap_addr, mapped_addr, nro_size, Kernel::MemoryState::ModuleCode) .IsSuccess()); ASSERT(vm_manager.UnmapRange(mapped_addr, nro_size).IsSuccess()); diff --git a/src/core/hle/service/lm/lm.cpp b/src/core/hle/service/lm/lm.cpp index 1f462e087..2a61593e2 100644 --- a/src/core/hle/service/lm/lm.cpp +++ b/src/core/hle/service/lm/lm.cpp @@ -42,7 +42,7 @@ private: union { BitField<0, 16, Flags> flags; BitField<16, 8, Severity> severity; - BitField<24, 8, u32_le> verbosity; + BitField<24, 8, u32> verbosity; }; u32_le payload_size; diff --git a/src/core/hle/service/nvdrv/devices/nvdevice.h b/src/core/hle/service/nvdrv/devices/nvdevice.h index 0f02a1a18..4f6042b00 100644 --- a/src/core/hle/service/nvdrv/devices/nvdevice.h +++ b/src/core/hle/service/nvdrv/devices/nvdevice.h @@ -19,11 +19,11 @@ public: virtual ~nvdevice() = default; union Ioctl { u32_le raw; - BitField<0, 8, u32_le> cmd; - BitField<8, 8, u32_le> group; - BitField<16, 14, u32_le> length; - BitField<30, 1, u32_le> is_in; - BitField<31, 1, u32_le> is_out; + BitField<0, 8, u32> cmd; + BitField<8, 8, u32> group; + BitField<16, 14, u32> length; + BitField<30, 1, u32> is_in; + BitField<31, 1, u32> is_out; }; /** diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 6057c7f26..8b1920f22 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -9,6 +9,7 @@ #include "common/common_types.h" #include "common/file_util.h" #include "common/logging/log.h" +#include "core/hle/kernel/code_set.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/vm_manager.h" #include "core/loader/elf.h" diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index 4fad0c0dd..5de02a94b 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -14,6 +14,7 @@ #include "core/file_sys/romfs_factory.h" #include "core/file_sys/vfs_offset.h" #include "core/gdbstub/gdbstub.h" +#include "core/hle/kernel/code_set.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/vm_manager.h" #include "core/hle/service/filesystem/filesystem.h" diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 6ded0b707..0eb9fd7f7 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -7,10 +7,13 @@ #include <lz4.h> #include "common/common_funcs.h" #include "common/file_util.h" +#include "common/hex_util.h" #include "common/logging/log.h" #include "common/swap.h" +#include "core/core.h" #include "core/file_sys/patch_manager.h" #include "core/gdbstub/gdbstub.h" +#include "core/hle/kernel/code_set.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/vm_manager.h" #include "core/loader/nso.h" @@ -164,6 +167,16 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process, std::memcpy(program_image.data(), pi_header.data() + 0x100, program_image.size()); } + // Apply cheats if they exist and the program has a valid title ID + if (pm) { + const auto cheats = pm->CreateCheatList(nso_header.build_id); + if (!cheats.empty()) { + Core::System::GetInstance().RegisterCheatList( + cheats, Common::HexArrayToString(nso_header.build_id), load_base, + load_base + program_image.size()); + } + } + // Load codeset for current process codeset.memory = std::make_shared<std::vector<u8>>(std::move(program_image)); process.LoadModule(std::move(codeset), load_base); diff --git a/src/core/memory.h b/src/core/memory.h index 3f60d868c..1d38cdca8 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -6,9 +6,6 @@ #include <cstddef> #include <string> -#include <tuple> -#include <vector> -#include <boost/icl/interval_map.hpp> #include "common/common_types.h" namespace Common { diff --git a/src/input_common/sdl/sdl.h b/src/input_common/sdl/sdl.h index 02a8d2e2c..d7f24c68a 100644 --- a/src/input_common/sdl/sdl.h +++ b/src/input_common/sdl/sdl.h @@ -24,17 +24,19 @@ namespace InputCommon::SDL { class State { public: - /// Unresisters SDL device factories and shut them down. + using Pollers = std::vector<std::unique_ptr<Polling::DevicePoller>>; + + /// Unregisters SDL device factories and shut them down. virtual ~State() = default; - virtual std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> GetPollers( - InputCommon::Polling::DeviceType type) = 0; + virtual Pollers GetPollers(Polling::DeviceType type) = 0; }; class NullState : public State { public: - std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> GetPollers( - InputCommon::Polling::DeviceType type) override {} + Pollers GetPollers(Polling::DeviceType type) override { + return {}; + } }; std::unique_ptr<State> Init(); diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index 6e8376549..b132d77f5 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -650,9 +650,9 @@ private: }; } // namespace Polling -std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> SDLState::GetPollers( - InputCommon::Polling::DeviceType type) { - std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> pollers; +SDLState::Pollers SDLState::GetPollers(InputCommon::Polling::DeviceType type) { + Pollers pollers; + switch (type) { case InputCommon::Polling::DeviceType::Analog: pollers.emplace_back(std::make_unique<Polling::SDLAnalogPoller>(*this)); @@ -660,8 +660,9 @@ std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> SDLState::GetPo case InputCommon::Polling::DeviceType::Button: pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this)); break; - return pollers; } + + return pollers; } } // namespace SDL diff --git a/src/input_common/sdl/sdl_impl.h b/src/input_common/sdl/sdl_impl.h index fec82fbe6..2579741d6 100644 --- a/src/input_common/sdl/sdl_impl.h +++ b/src/input_common/sdl/sdl_impl.h @@ -25,7 +25,7 @@ public: /// Initializes and registers SDL device factories SDLState(); - /// Unresisters SDL device factories and shut them down. + /// Unregisters SDL device factories and shut them down. ~SDLState() override; /// Handle SDL_Events for joysticks from SDL_PollEvent @@ -35,8 +35,7 @@ public: std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port); /// Get all DevicePoller that use the SDL backend for a specific device type - std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> GetPollers( - InputCommon::Polling::DeviceType type) override; + Pollers GetPollers(Polling::DeviceType type) override; /// Used by the Pollers during config std::atomic<bool> polling = false; diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 37f09ce5f..d0284bdf4 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -1,4 +1,5 @@ add_executable(tests + common/bit_field.cpp common/param_package.cpp common/ring_buffer.cpp core/arm/arm_test_common.cpp diff --git a/src/tests/common/bit_field.cpp b/src/tests/common/bit_field.cpp new file mode 100644 index 000000000..8ca1889f9 --- /dev/null +++ b/src/tests/common/bit_field.cpp @@ -0,0 +1,90 @@ +// Copyright 2019 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include <array> +#include <cstring> +#include <type_traits> +#include <catch2/catch.hpp> +#include "common/bit_field.h" + +TEST_CASE("BitField", "[common]") { + enum class TestEnum : u32 { + A = 0b10111101, + B = 0b10101110, + C = 0b00001111, + }; + + union LEBitField { + u32_le raw; + BitField<0, 6, u32> a; + BitField<6, 4, s32> b; + BitField<10, 8, TestEnum> c; + BitField<18, 14, u32> d; + } le_bitfield; + + union BEBitField { + u32_be raw; + BitFieldBE<0, 6, u32> a; + BitFieldBE<6, 4, s32> b; + BitFieldBE<10, 8, TestEnum> c; + BitFieldBE<18, 14, u32> d; + } be_bitfield; + + static_assert(sizeof(LEBitField) == sizeof(u32)); + static_assert(sizeof(BEBitField) == sizeof(u32)); + static_assert(std::is_trivially_copyable_v<LEBitField>); + static_assert(std::is_trivially_copyable_v<BEBitField>); + + std::array<u8, 4> raw{{ + 0b01101100, + 0b11110110, + 0b10111010, + 0b11101100, + }}; + + std::memcpy(&le_bitfield, &raw, sizeof(raw)); + std::memcpy(&be_bitfield, &raw, sizeof(raw)); + + // bit fields: 11101100101110'10111101'1001'101100 + REQUIRE(le_bitfield.raw == 0b11101100'10111010'11110110'01101100); + REQUIRE(le_bitfield.a == 0b101100); + REQUIRE(le_bitfield.b == -7); // 1001 as two's complement + REQUIRE(le_bitfield.c == TestEnum::A); + REQUIRE(le_bitfield.d == 0b11101100101110); + + le_bitfield.a.Assign(0b000111); + le_bitfield.b.Assign(-1); + le_bitfield.c.Assign(TestEnum::C); + le_bitfield.d.Assign(0b01010101010101); + std::memcpy(&raw, &le_bitfield, sizeof(raw)); + // bit fields: 01010101010101'00001111'1111'000111 + REQUIRE(le_bitfield.raw == 0b01010101'01010100'00111111'11000111); + REQUIRE(raw == std::array<u8, 4>{{ + 0b11000111, + 0b00111111, + 0b01010100, + 0b01010101, + }}); + + // bit fields: 01101100111101'10101110'1011'101100 + REQUIRE(be_bitfield.raw == 0b01101100'11110110'10111010'11101100); + REQUIRE(be_bitfield.a == 0b101100); + REQUIRE(be_bitfield.b == -5); // 1011 as two's complement + REQUIRE(be_bitfield.c == TestEnum::B); + REQUIRE(be_bitfield.d == 0b01101100111101); + + be_bitfield.a.Assign(0b000111); + be_bitfield.b.Assign(-1); + be_bitfield.c.Assign(TestEnum::C); + be_bitfield.d.Assign(0b01010101010101); + std::memcpy(&raw, &be_bitfield, sizeof(raw)); + // bit fields: 01010101010101'00001111'1111'000111 + REQUIRE(be_bitfield.raw == 0b01010101'01010100'00111111'11000111); + REQUIRE(raw == std::array<u8, 4>{{ + 0b01010101, + 0b01010100, + 0b00111111, + 0b11000111, + }}); +} diff --git a/src/video_core/rasterizer_cache.h b/src/video_core/rasterizer_cache.h index ecd9986a0..9fc9f3056 100644 --- a/src/video_core/rasterizer_cache.h +++ b/src/video_core/rasterizer_cache.h @@ -132,7 +132,7 @@ protected: } /// Register an object into the cache - void Register(const T& object) { + virtual void Register(const T& object) { std::lock_guard<std::recursive_mutex> lock{mutex}; object->SetIsRegistered(true); @@ -142,7 +142,7 @@ protected: } /// Unregisters an object from the cache - void Unregister(const T& object) { + virtual void Unregister(const T& object) { std::lock_guard<std::recursive_mutex> lock{mutex}; object->SetIsRegistered(false); diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp index 39dcf71ce..0235317c0 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp @@ -933,7 +933,7 @@ Surface RasterizerCacheOpenGL::GetSurface(const SurfaceParams& params, bool pres // If surface parameters changed and we care about keeping the previous data, recreate // the surface from the old one Surface new_surface{RecreateSurface(surface, params)}; - UnregisterSurface(surface); + Unregister(surface); Register(new_surface); if (new_surface->IsUploaded()) { RegisterReinterpretSurface(new_surface); @@ -941,7 +941,7 @@ Surface RasterizerCacheOpenGL::GetSurface(const SurfaceParams& params, bool pres return new_surface; } else { // Delete the old surface before creating a new one to prevent collisions. - UnregisterSurface(surface); + Unregister(surface); } } @@ -1295,12 +1295,12 @@ static bool IsReinterpretInvalidSecond(const Surface render_surface, bool RasterizerCacheOpenGL::PartialReinterpretSurface(Surface triggering_surface, Surface intersect) { if (IsReinterpretInvalid(triggering_surface, intersect)) { - UnregisterSurface(intersect); + Unregister(intersect); return false; } if (!LayerFitReinterpretSurface(*this, triggering_surface, intersect)) { if (IsReinterpretInvalidSecond(triggering_surface, intersect)) { - UnregisterSurface(intersect); + Unregister(intersect); return false; } FlushObject(intersect); diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h index 0efcafd07..c644271d0 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h +++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h @@ -538,13 +538,17 @@ private: return nullptr; } + void Register(const Surface& object) { + RasterizerCache<Surface>::Register(object); + } + /// Unregisters an object from the cache - void UnregisterSurface(const Surface& object) { + void Unregister(const Surface& object) { if (object->IsReinterpreted()) { auto interval = GetReinterpretInterval(object); reinterpreted_surfaces.erase(interval); } - Unregister(object); + RasterizerCache<Surface>::Unregister(object); } }; |