diff options
30 files changed, 304 insertions, 64 deletions
diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 6cf1886cf..1cac1d0c9 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -23,6 +23,11 @@ void SessionRequestHandler::ClientDisconnected(SharedPtr<ServerSession> server_s boost::range::remove_erase(connected_sessions, server_session); } +HLERequestContext::HLERequestContext(SharedPtr<ServerSession> session) + : session(std::move(session)) { + cmd_buf[0] = 0; +} + HLERequestContext::~HLERequestContext() = default; SharedPtr<Object> HLERequestContext::GetIncomingHandle(u32 id_from_cmdbuf) const { diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index cbb109d8f..35795fc1d 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -84,6 +84,7 @@ protected: */ class HLERequestContext { public: + HLERequestContext(SharedPtr<ServerSession> session); ~HLERequestContext(); /// Returns a pointer to the IPC command buffer for this request. @@ -118,14 +119,14 @@ public: */ void ClearIncomingObjects(); -private: - friend class Service::ServiceFrameworkBase; - + /// Populates this context with data from the requesting process/thread. ResultCode PopulateFromIncomingCommandBuffer(const u32_le* src_cmdbuf, Process& src_process, HandleTable& src_table); + /// Writes data from this context back to the requesting process/thread. ResultCode WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process, HandleTable& dst_table) const; +private: std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf; SharedPtr<ServerSession> session; // TODO(yuriks): Check common usage of this and optimize size accordingly diff --git a/src/core/hle/kernel/memory.cpp b/src/core/hle/kernel/memory.cpp index 804f23b1c..496d07cb5 100644 --- a/src/core/hle/kernel/memory.cpp +++ b/src/core/hle/kernel/memory.cpp @@ -166,7 +166,7 @@ void HandleSpecialMapping(VMManager& address_space, const AddressMapping& mappin auto vma = address_space .MapBackingMemory(mapping.address, target_pointer + offset_into_region, mapping.size, memory_state) - .MoveFrom(); + .Unwrap(); address_space.Reprotect(vma, mapping.read_only ? VMAPermission::Read : VMAPermission::ReadWrite); } @@ -176,14 +176,14 @@ void MapSharedPages(VMManager& address_space) { .MapBackingMemory(Memory::CONFIG_MEMORY_VADDR, reinterpret_cast<u8*>(&ConfigMem::config_mem), Memory::CONFIG_MEMORY_SIZE, MemoryState::Shared) - .MoveFrom(); + .Unwrap(); address_space.Reprotect(cfg_mem_vma, VMAPermission::Read); auto shared_page_vma = address_space .MapBackingMemory(Memory::SHARED_PAGE_VADDR, reinterpret_cast<u8*>(&SharedPage::shared_page), Memory::SHARED_PAGE_SIZE, MemoryState::Shared) - .MoveFrom(); + .Unwrap(); address_space.Reprotect(shared_page_vma, VMAPermission::Read); } diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 1c31ec950..522ad2333 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -151,6 +151,8 @@ void Process::Run(s32 main_thread_priority, u32 stack_size) { } VAddr Process::GetLinearHeapAreaAddress() const { + // Starting from system version 8.0.0 a new linear heap layout is supported to allow usage of + // the extra RAM in the n3DS. return kernel_version < 0x22C ? Memory::LINEAR_HEAP_VADDR : Memory::NEW_LINEAR_HEAP_VADDR; } diff --git a/src/core/hle/kernel/server_session.cpp b/src/core/hle/kernel/server_session.cpp index 2dc709bc9..d197137c3 100644 --- a/src/core/hle/kernel/server_session.cpp +++ b/src/core/hle/kernel/server_session.cpp @@ -71,7 +71,7 @@ ResultCode ServerSession::HandleSyncRequest() { ServerSession::SessionPair ServerSession::CreateSessionPair(const std::string& name, SharedPtr<ClientPort> port) { - auto server_session = ServerSession::Create(name + "_Server").MoveFrom(); + auto server_session = ServerSession::Create(name + "_Server").Unwrap(); SharedPtr<ClientSession> client_session(new ClientSession); client_session->name = name + "_Client"; diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 75ce626f8..f5f2eb2f7 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -389,7 +389,7 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point, thread->wait_objects.clear(); thread->wait_address = 0; thread->name = std::move(name); - thread->callback_handle = wakeup_callback_handle_table.Create(thread).MoveFrom(); + thread->callback_handle = wakeup_callback_handle_table.Create(thread).Unwrap(); thread->owner_process = g_current_process; // Find the next available TLS index, and mark it as used @@ -484,7 +484,7 @@ SharedPtr<Thread> SetupMainThread(u32 entry_point, s32 priority) { auto thread_res = Thread::Create("main", entry_point, priority, 0, THREADPROCESSORID_0, Memory::HEAP_VADDR_END); - SharedPtr<Thread> thread = thread_res.MoveFrom(); + SharedPtr<Thread> thread = std::move(thread_res).Unwrap(); thread->context.fpscr = FPSCR_DEFAULT_NAN | FPSCR_FLUSH_TO_ZERO | FPSCR_ROUND_TOZERO | FPSCR_IXC; // 0x03C00010 diff --git a/src/core/hle/kernel/timer.cpp b/src/core/hle/kernel/timer.cpp index 6f2cf3b02..d7ec93672 100644 --- a/src/core/hle/kernel/timer.cpp +++ b/src/core/hle/kernel/timer.cpp @@ -30,7 +30,7 @@ SharedPtr<Timer> Timer::Create(ResetType reset_type, std::string name) { timer->name = std::move(name); timer->initial_delay = 0; timer->interval_delay = 0; - timer->callback_handle = timer_callback_handle_table.Create(timer).MoveFrom(); + timer->callback_handle = timer_callback_handle_table.Create(timer).Unwrap(); return timer; } diff --git a/src/core/hle/result.h b/src/core/hle/result.h index 5f2cdbb96..47b6e2b23 100644 --- a/src/core/hle/result.h +++ b/src/core/hle/result.h @@ -388,13 +388,14 @@ public: } /// Asserts that the result succeeded and returns a reference to it. - T& Unwrap() { + T& Unwrap() & { ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal"); return **this; } - T&& MoveFrom() { - return std::move(Unwrap()); + T&& Unwrap() && { + ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal"); + return std::move(**this); } private: diff --git a/src/core/hle/service/apt/apt.cpp b/src/core/hle/service/apt/apt.cpp index 4c587e3c8..25e7b777d 100644 --- a/src/core/hle/service/apt/apt.cpp +++ b/src/core/hle/service/apt/apt.cpp @@ -55,8 +55,8 @@ void Initialize(Service::Interface* self) { u32 flags = rp.Pop<u32>(); IPC::RequestBuilder rb = rp.MakeBuilder(1, 3); rb.Push(RESULT_SUCCESS); - rb.PushCopyHandles(Kernel::g_handle_table.Create(notification_event).MoveFrom(), - Kernel::g_handle_table.Create(parameter_event).MoveFrom()); + rb.PushCopyHandles(Kernel::g_handle_table.Create(notification_event).Unwrap(), + Kernel::g_handle_table.Create(parameter_event).Unwrap()); // TODO(bunnei): Check if these events are cleared every time Initialize is called. notification_event->Clear(); @@ -93,7 +93,7 @@ void GetSharedFont(Service::Interface* self) { // allocated, the real APT service calculates this address by scanning the entire address space // (using svcQueryMemory) and searches for an allocation of the same size as the Shared Font. rb.Push(target_address); - rb.PushCopyHandles(Kernel::g_handle_table.Create(shared_font_mem).MoveFrom()); + rb.PushCopyHandles(Kernel::g_handle_table.Create(shared_font_mem).Unwrap()); } void NotifyToWait(Service::Interface* self) { @@ -115,7 +115,7 @@ void GetLockHandle(Service::Interface* self) { rb.Push(RESULT_SUCCESS); // No error rb.Push(applet_attributes); // Applet Attributes, this value is passed to Enable. rb.Push<u32>(0); // Least significant bit = power button state - Kernel::Handle handle_copy = Kernel::g_handle_table.Create(lock).MoveFrom(); + Kernel::Handle handle_copy = Kernel::g_handle_table.Create(lock).Unwrap(); rb.PushCopyHandles(handle_copy); LOG_WARNING(Service_APT, "(STUBBED) called handle=0x%08X applet_attributes=0x%08X", handle_copy, @@ -231,7 +231,7 @@ void ReceiveParameter(Service::Interface* self) { rb.Push(static_cast<u32>(next_parameter.buffer.size())); // Parameter buffer size rb.PushMoveHandles((next_parameter.object != nullptr) - ? Kernel::g_handle_table.Create(next_parameter.object).MoveFrom() + ? Kernel::g_handle_table.Create(next_parameter.object).Unwrap() : 0); rb.PushStaticBuffer(buffer, static_cast<u32>(next_parameter.buffer.size()), 0); @@ -261,7 +261,7 @@ void GlanceParameter(Service::Interface* self) { rb.Push(static_cast<u32>(next_parameter.buffer.size())); // Parameter buffer size rb.PushCopyHandles((next_parameter.object != nullptr) - ? Kernel::g_handle_table.Create(next_parameter.object).MoveFrom() + ? Kernel::g_handle_table.Create(next_parameter.object).Unwrap() : 0); rb.PushStaticBuffer(buffer, static_cast<u32>(next_parameter.buffer.size()), 0); diff --git a/src/core/hle/service/cam/cam.cpp b/src/core/hle/service/cam/cam.cpp index 7394c844f..c9f9e9d95 100644 --- a/src/core/hle/service/cam/cam.cpp +++ b/src/core/hle/service/cam/cam.cpp @@ -347,7 +347,7 @@ void GetVsyncInterruptEvent(Service::Interface* self) { int port = *port_select.begin(); rb.Push(RESULT_SUCCESS); rb.PushCopyHandles( - Kernel::g_handle_table.Create(ports[port].vsync_interrupt_event).MoveFrom()); + Kernel::g_handle_table.Create(ports[port].vsync_interrupt_event).Unwrap()); } else { LOG_ERROR(Service_CAM, "invalid port_select=%u", port_select.m_val); rb.Push(ERROR_INVALID_ENUM_VALUE); @@ -366,7 +366,7 @@ void GetBufferErrorInterruptEvent(Service::Interface* self) { int port = *port_select.begin(); rb.Push(RESULT_SUCCESS); rb.PushCopyHandles( - Kernel::g_handle_table.Create(ports[port].buffer_error_interrupt_event).MoveFrom()); + Kernel::g_handle_table.Create(ports[port].buffer_error_interrupt_event).Unwrap()); } else { LOG_ERROR(Service_CAM, "invalid port_select=%u", port_select.m_val); rb.Push(ERROR_INVALID_ENUM_VALUE); @@ -400,7 +400,7 @@ void SetReceiving(Service::Interface* self) { } rb.Push(RESULT_SUCCESS); - rb.PushCopyHandles(Kernel::g_handle_table.Create(port.completion_event).MoveFrom()); + rb.PushCopyHandles(Kernel::g_handle_table.Create(port.completion_event).Unwrap()); } else { LOG_ERROR(Service_CAM, "invalid port_select=%u", port_select.m_val); rb.Push(ERROR_INVALID_ENUM_VALUE); diff --git a/src/core/hle/service/cecd/cecd.cpp b/src/core/hle/service/cecd/cecd.cpp index bd9814244..421006a9e 100644 --- a/src/core/hle/service/cecd/cecd.cpp +++ b/src/core/hle/service/cecd/cecd.cpp @@ -31,8 +31,8 @@ void GetCecStateAbbreviated(Service::Interface* self) { void GetCecInfoEventHandle(Service::Interface* self) { u32* cmd_buff = Kernel::GetCommandBuffer(); - cmd_buff[1] = RESULT_SUCCESS.raw; // No error - cmd_buff[3] = Kernel::g_handle_table.Create(cecinfo_event).MoveFrom(); // Event handle + cmd_buff[1] = RESULT_SUCCESS.raw; // No error + cmd_buff[3] = Kernel::g_handle_table.Create(cecinfo_event).Unwrap(); // Event handle LOG_WARNING(Service_CECD, "(STUBBED) called"); } @@ -40,8 +40,8 @@ void GetCecInfoEventHandle(Service::Interface* self) { void GetChangeStateEventHandle(Service::Interface* self) { u32* cmd_buff = Kernel::GetCommandBuffer(); - cmd_buff[1] = RESULT_SUCCESS.raw; // No error - cmd_buff[3] = Kernel::g_handle_table.Create(change_state_event).MoveFrom(); // Event handle + cmd_buff[1] = RESULT_SUCCESS.raw; // No error + cmd_buff[3] = Kernel::g_handle_table.Create(change_state_event).Unwrap(); // Event handle LOG_WARNING(Service_CECD, "(STUBBED) called"); } diff --git a/src/core/hle/service/cfg/cfg.cpp b/src/core/hle/service/cfg/cfg.cpp index 5a7878b31..6624f1711 100644 --- a/src/core/hle/service/cfg/cfg.cpp +++ b/src/core/hle/service/cfg/cfg.cpp @@ -406,7 +406,7 @@ ResultCode UpdateConfigNANDSavegame() { auto config_result = Service::FS::OpenFileFromArchive(cfg_system_save_data_archive, path, mode); ASSERT_MSG(config_result.Succeeded(), "could not open file"); - auto config = config_result.MoveFrom(); + auto config = std::move(config_result).Unwrap(); config->backend->Write(0, CONFIG_SAVEFILE_SIZE, 1, cfg_config_file_buffer.data()); return RESULT_SUCCESS; @@ -560,7 +560,7 @@ ResultCode LoadConfigNANDSaveFile() { // Read the file if it already exists if (config_result.Succeeded()) { - auto config = config_result.MoveFrom(); + auto config = std::move(config_result).Unwrap(); config->backend->Read(0, CONFIG_SAVEFILE_SIZE, cfg_config_file_buffer.data()); return RESULT_SUCCESS; } diff --git a/src/core/hle/service/csnd_snd.cpp b/src/core/hle/service/csnd_snd.cpp index 1455f20ca..9471ec1ef 100644 --- a/src/core/hle/service/csnd_snd.cpp +++ b/src/core/hle/service/csnd_snd.cpp @@ -51,8 +51,8 @@ static void Initialize(Interface* self) { cmd_buff[1] = RESULT_SUCCESS.raw; cmd_buff[2] = IPC::CopyHandleDesc(2); - cmd_buff[3] = Kernel::g_handle_table.Create(mutex).MoveFrom(); - cmd_buff[4] = Kernel::g_handle_table.Create(shared_memory).MoveFrom(); + cmd_buff[3] = Kernel::g_handle_table.Create(mutex).Unwrap(); + cmd_buff[4] = Kernel::g_handle_table.Create(shared_memory).Unwrap(); LOG_WARNING(Service_CSND, "(STUBBED) called"); } diff --git a/src/core/hle/service/dsp_dsp.cpp b/src/core/hle/service/dsp_dsp.cpp index 363066d14..7d746054f 100644 --- a/src/core/hle/service/dsp_dsp.cpp +++ b/src/core/hle/service/dsp_dsp.cpp @@ -168,7 +168,7 @@ static void GetSemaphoreEventHandle(Service::Interface* self) { cmd_buff[0] = IPC::MakeHeader(0x16, 1, 2); cmd_buff[1] = RESULT_SUCCESS.raw; // No error // cmd_buff[2] not set - cmd_buff[3] = Kernel::g_handle_table.Create(semaphore_event).MoveFrom(); // Event handle + cmd_buff[3] = Kernel::g_handle_table.Create(semaphore_event).Unwrap(); // Event handle LOG_WARNING(Service_DSP, "(STUBBED) called"); } diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp index 3605ef175..033fbc9aa 100644 --- a/src/core/hle/service/fs/archive.cpp +++ b/src/core/hle/service/fs/archive.cpp @@ -311,7 +311,7 @@ ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handl if (backend.Failed()) return backend.Code(); - auto file = std::shared_ptr<File>(new File(backend.MoveFrom(), path)); + auto file = std::shared_ptr<File>(new File(std::move(backend).Unwrap(), path)); return MakeResult<std::shared_ptr<File>>(std::move(file)); } @@ -401,7 +401,7 @@ ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle arc if (backend.Failed()) return backend.Code(); - auto directory = std::shared_ptr<Directory>(new Directory(backend.MoveFrom(), path)); + auto directory = std::shared_ptr<Directory>(new Directory(std::move(backend).Unwrap(), path)); return MakeResult<std::shared_ptr<Directory>>(std::move(directory)); } diff --git a/src/core/hle/service/fs/fs_user.cpp b/src/core/hle/service/fs/fs_user.cpp index 34e1783ec..b9eab7838 100644 --- a/src/core/hle/service/fs/fs_user.cpp +++ b/src/core/hle/service/fs/fs_user.cpp @@ -87,7 +87,7 @@ static void OpenFile(Service::Interface* self) { file->ClientConnected(std::get<SharedPtr<ServerSession>>(sessions)); rb.PushMoveHandles( - Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions)).MoveFrom()); + Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions)).Unwrap()); } else { rb.PushMoveHandles(0); LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str()); @@ -153,7 +153,7 @@ static void OpenFileDirectly(Service::Interface* self) { file->ClientConnected(std::get<SharedPtr<ServerSession>>(sessions)); cmd_buff[3] = - Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions)).MoveFrom(); + Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions)).Unwrap(); } else { cmd_buff[3] = 0; LOG_ERROR(Service_FS, "failed to get a handle for file %s mode=%u attributes=%u", @@ -420,7 +420,7 @@ static void OpenDirectory(Service::Interface* self) { directory->ClientConnected(std::get<SharedPtr<ServerSession>>(sessions)); cmd_buff[3] = - Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions)).MoveFrom(); + Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions)).Unwrap(); } else { LOG_ERROR(Service_FS, "failed to get a handle for directory type=%d size=%d data=%s", dirname_type, dirname_size, dir_path.DebugStr().c_str()); diff --git a/src/core/hle/service/gsp_gpu.cpp b/src/core/hle/service/gsp_gpu.cpp index 6ff0f4812..bc964ec60 100644 --- a/src/core/hle/service/gsp_gpu.cpp +++ b/src/core/hle/service/gsp_gpu.cpp @@ -389,8 +389,8 @@ static void RegisterInterruptRelayQueue(Interface* self) { } else { cmd_buff[1] = RESULT_SUCCESS.raw; } - cmd_buff[2] = g_thread_id++; // Thread ID - cmd_buff[4] = Kernel::g_handle_table.Create(g_shared_memory).MoveFrom(); // GSP shared memory + cmd_buff[2] = g_thread_id++; // Thread ID + cmd_buff[4] = Kernel::g_handle_table.Create(g_shared_memory).Unwrap(); // GSP shared memory g_interrupt_event->Signal(); // TODO(bunnei): Is this correct? diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index 5255f6dc8..2014b8461 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -253,12 +253,12 @@ void GetIPCHandles(Service::Interface* self) { cmd_buff[1] = 0; // No error cmd_buff[2] = 0x14000000; // IPC Command Structure translate-header // TODO(yuriks): Return error from SendSyncRequest is this fails (part of IPC marshalling) - cmd_buff[3] = Kernel::g_handle_table.Create(Service::HID::shared_mem).MoveFrom(); - cmd_buff[4] = Kernel::g_handle_table.Create(Service::HID::event_pad_or_touch_1).MoveFrom(); - cmd_buff[5] = Kernel::g_handle_table.Create(Service::HID::event_pad_or_touch_2).MoveFrom(); - cmd_buff[6] = Kernel::g_handle_table.Create(Service::HID::event_accelerometer).MoveFrom(); - cmd_buff[7] = Kernel::g_handle_table.Create(Service::HID::event_gyroscope).MoveFrom(); - cmd_buff[8] = Kernel::g_handle_table.Create(Service::HID::event_debug_pad).MoveFrom(); + cmd_buff[3] = Kernel::g_handle_table.Create(Service::HID::shared_mem).Unwrap(); + cmd_buff[4] = Kernel::g_handle_table.Create(Service::HID::event_pad_or_touch_1).Unwrap(); + cmd_buff[5] = Kernel::g_handle_table.Create(Service::HID::event_pad_or_touch_2).Unwrap(); + cmd_buff[6] = Kernel::g_handle_table.Create(Service::HID::event_accelerometer).Unwrap(); + cmd_buff[7] = Kernel::g_handle_table.Create(Service::HID::event_gyroscope).Unwrap(); + cmd_buff[8] = Kernel::g_handle_table.Create(Service::HID::event_debug_pad).Unwrap(); } void EnableAccelerometer(Service::Interface* self) { diff --git a/src/core/hle/service/ir/ir_rst.cpp b/src/core/hle/service/ir/ir_rst.cpp index 0de698003..837413f93 100644 --- a/src/core/hle/service/ir/ir_rst.cpp +++ b/src/core/hle/service/ir/ir_rst.cpp @@ -145,8 +145,8 @@ static void GetHandles(Interface* self) { IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x01, 0, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 3); rb.Push(RESULT_SUCCESS); - rb.PushMoveHandles(Kernel::g_handle_table.Create(Service::IR::shared_memory).MoveFrom(), - Kernel::g_handle_table.Create(Service::IR::update_event).MoveFrom()); + rb.PushMoveHandles(Kernel::g_handle_table.Create(Service::IR::shared_memory).Unwrap(), + Kernel::g_handle_table.Create(Service::IR::update_event).Unwrap()); } /** diff --git a/src/core/hle/service/ir/ir_user.cpp b/src/core/hle/service/ir/ir_user.cpp index fdecdce64..fbdf7a465 100644 --- a/src/core/hle/service/ir/ir_user.cpp +++ b/src/core/hle/service/ir/ir_user.cpp @@ -337,7 +337,7 @@ void GetReceiveEvent(Interface* self) { IPC::RequestBuilder rb(Kernel::GetCommandBuffer(), 0x0A, 1, 2); rb.Push(RESULT_SUCCESS); - rb.PushCopyHandles(Kernel::g_handle_table.Create(Service::IR::receive_event).MoveFrom()); + rb.PushCopyHandles(Kernel::g_handle_table.Create(Service::IR::receive_event).Unwrap()); LOG_INFO(Service_IR, "called"); } @@ -354,7 +354,7 @@ void GetSendEvent(Interface* self) { IPC::RequestBuilder rb(Kernel::GetCommandBuffer(), 0x0B, 1, 2); rb.Push(RESULT_SUCCESS); - rb.PushCopyHandles(Kernel::g_handle_table.Create(Service::IR::send_event).MoveFrom()); + rb.PushCopyHandles(Kernel::g_handle_table.Create(Service::IR::send_event).Unwrap()); LOG_INFO(Service_IR, "called"); } @@ -394,7 +394,7 @@ static void GetConnectionStatusEvent(Interface* self) { IPC::RequestBuilder rb(Kernel::GetCommandBuffer(), 0x0C, 1, 2); rb.Push(RESULT_SUCCESS); - rb.PushCopyHandles(Kernel::g_handle_table.Create(Service::IR::conn_status_event).MoveFrom()); + rb.PushCopyHandles(Kernel::g_handle_table.Create(Service::IR::conn_status_event).Unwrap()); LOG_INFO(Service_IR, "called"); } diff --git a/src/core/hle/service/mic_u.cpp b/src/core/hle/service/mic_u.cpp index 35212b59b..23e1ff094 100644 --- a/src/core/hle/service/mic_u.cpp +++ b/src/core/hle/service/mic_u.cpp @@ -160,7 +160,7 @@ static void IsSampling(Interface* self) { static void GetBufferFullEvent(Interface* self) { u32* cmd_buff = Kernel::GetCommandBuffer(); cmd_buff[1] = RESULT_SUCCESS.raw; // No error - cmd_buff[3] = Kernel::g_handle_table.Create(buffer_full_event).MoveFrom(); + cmd_buff[3] = Kernel::g_handle_table.Create(buffer_full_event).Unwrap(); LOG_WARNING(Service_MIC, "(STUBBED) called"); } diff --git a/src/core/hle/service/nfc/nfc.cpp b/src/core/hle/service/nfc/nfc.cpp index b44a9f668..cb09ed0b7 100644 --- a/src/core/hle/service/nfc/nfc.cpp +++ b/src/core/hle/service/nfc/nfc.cpp @@ -95,7 +95,7 @@ void GetTagInRangeEvent(Interface* self) { cmd_buff[0] = IPC::MakeHeader(0xB, 1, 2); cmd_buff[1] = RESULT_SUCCESS.raw; cmd_buff[2] = IPC::CopyHandleDesc(); - cmd_buff[3] = Kernel::g_handle_table.Create(tag_in_range_event).MoveFrom(); + cmd_buff[3] = Kernel::g_handle_table.Create(tag_in_range_event).Unwrap(); LOG_WARNING(Service_NFC, "(STUBBED) called"); } @@ -105,7 +105,7 @@ void GetTagOutOfRangeEvent(Interface* self) { cmd_buff[0] = IPC::MakeHeader(0xC, 1, 2); cmd_buff[1] = RESULT_SUCCESS.raw; cmd_buff[2] = IPC::CopyHandleDesc(); - cmd_buff[3] = Kernel::g_handle_table.Create(tag_out_of_range_event).MoveFrom(); + cmd_buff[3] = Kernel::g_handle_table.Create(tag_out_of_range_event).Unwrap(); LOG_WARNING(Service_NFC, "(STUBBED) called"); } diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp index e92900d48..a7149c9e8 100644 --- a/src/core/hle/service/nwm/nwm_uds.cpp +++ b/src/core/hle/service/nwm/nwm_uds.cpp @@ -190,7 +190,7 @@ static void InitializeWithVersion(Interface* self) { IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); rb.Push(RESULT_SUCCESS); - rb.PushCopyHandles(Kernel::g_handle_table.Create(connection_status_event).MoveFrom()); + rb.PushCopyHandles(Kernel::g_handle_table.Create(connection_status_event).Unwrap()); LOG_DEBUG(Service_NWM, "called sharedmem_size=0x%08X, version=0x%08X, sharedmem_handle=0x%08X", sharedmem_size, version, sharedmem_handle); @@ -265,7 +265,7 @@ static void Bind(Interface* self) { IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); rb.Push(RESULT_SUCCESS); - rb.PushCopyHandles(Kernel::g_handle_table.Create(event).MoveFrom()); + rb.PushCopyHandles(Kernel::g_handle_table.Create(event).Unwrap()); } /** @@ -543,6 +543,42 @@ static void BeaconBroadcastCallback(u64 userdata, int cycles_late) { beacon_broadcast_event, 0); } +/* + * Returns an available index in the nodes array for the + * currently-hosted UDS network. + */ +static u32 GetNextAvailableNodeId() { + ASSERT_MSG(connection_status.status == static_cast<u32>(NetworkStatus::ConnectedAsHost), + "Can not accept clients if we're not hosting a network"); + + for (unsigned index = 0; index < connection_status.max_nodes; ++index) { + if ((connection_status.node_bitmask & (1 << index)) == 0) + return index; + } + + // Any connection attempts to an already full network should have been refused. + ASSERT_MSG(false, "No available connection slots in the network"); +} + +/* + * Called when a client connects to an UDS network we're hosting, + * updates the connection status and signals the update event. + * @param network_node_id Network Node Id of the connecting client. + */ +void OnClientConnected(u16 network_node_id) { + ASSERT_MSG(connection_status.status == static_cast<u32>(NetworkStatus::ConnectedAsHost), + "Can not accept clients if we're not hosting a network"); + ASSERT_MSG(connection_status.total_nodes < connection_status.max_nodes, + "Can not accept connections on a full network"); + + u32 node_id = GetNextAvailableNodeId(); + connection_status.node_bitmask |= 1 << node_id; + connection_status.changed_nodes |= 1 << node_id; + connection_status.nodes[node_id] = network_node_id; + connection_status.total_nodes++; + connection_status_event->Signal(); +} + const Interface::FunctionInfo FunctionTable[] = { {0x00010442, nullptr, "Initialize (deprecated)"}, {0x00020000, nullptr, "Scrap"}, diff --git a/src/core/hle/service/ptm/ptm.cpp b/src/core/hle/service/ptm/ptm.cpp index 39382ef09..a0b959797 100644 --- a/src/core/hle/service/ptm/ptm.cpp +++ b/src/core/hle/service/ptm/ptm.cpp @@ -152,7 +152,7 @@ void Init() { auto gamecoin_result = Service::FS::OpenFileFromArchive(*archive_result, gamecoin_path, open_mode); if (gamecoin_result.Succeeded()) { - auto gamecoin = gamecoin_result.MoveFrom(); + auto gamecoin = std::move(gamecoin_result).Unwrap(); gamecoin->backend->Write(0, sizeof(GameCoin), true, reinterpret_cast<const u8*>(&default_game_coin)); gamecoin->backend->Close(); diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index 791a65c19..aad950e50 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -173,8 +173,7 @@ void ServiceFrameworkBase::HandleSyncRequest(SharedPtr<ServerSession> server_ses // TODO(yuriks): The kernel should be the one handling this as part of translation after // everything else is migrated - Kernel::HLERequestContext context; - context.session = std::move(server_session); + Kernel::HLERequestContext context(std::move(server_session)); context.PopulateFromIncomingCommandBuffer(cmd_buf, *Kernel::g_current_process, Kernel::g_handle_table); @@ -207,7 +206,7 @@ void AddService(Interface* interface_) { auto server_port = SM::g_service_manager ->RegisterService(interface_->GetPortName(), interface_->GetMaxSessions()) - .MoveFrom(); + .Unwrap(); server_port->SetHleHandler(std::shared_ptr<Interface>(interface_)); } diff --git a/src/core/hle/service/sm/srv.cpp b/src/core/hle/service/sm/srv.cpp index 74a1256e0..352941e69 100644 --- a/src/core/hle/service/sm/srv.cpp +++ b/src/core/hle/service/sm/srv.cpp @@ -113,13 +113,13 @@ void SRV::GetServiceHandle(Kernel::HLERequestContext& ctx) { (*session)->GetObjectId()); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); rb.Push(session.Code()); - rb.PushObjects(session.MoveFrom()); + rb.PushObjects(std::move(session).Unwrap()); } else if (session.Code() == Kernel::ERR_MAX_CONNECTIONS_REACHED && return_port_on_failure) { LOG_WARNING(Service_SRV, "called service=%s -> ERR_MAX_CONNECTIONS_REACHED, *port*=%u", name.c_str(), (*client_port)->GetObjectId()); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); rb.Push(ERR_MAX_CONNECTIONS_REACHED); - rb.PushObjects(client_port.MoveFrom()); + rb.PushObjects(std::move(client_port).Unwrap()); } else { LOG_ERROR(Service_SRV, "called service=%s -> error 0x%08X", name.c_str(), session.Code()); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); diff --git a/src/core/hle/service/y2r_u.cpp b/src/core/hle/service/y2r_u.cpp index bb7bf2d67..e73971d5f 100644 --- a/src/core/hle/service/y2r_u.cpp +++ b/src/core/hle/service/y2r_u.cpp @@ -275,7 +275,7 @@ static void GetTransferEndEvent(Interface* self) { cmd_buff[0] = IPC::MakeHeader(0xF, 2, 0); cmd_buff[1] = RESULT_SUCCESS.raw; - cmd_buff[3] = Kernel::g_handle_table.Create(completion_event).MoveFrom(); + cmd_buff[3] = Kernel::g_handle_table.Create(completion_event).Unwrap(); LOG_DEBUG(Service_Y2R, "called"); } diff --git a/src/core/memory.h b/src/core/memory.h index 802aa465e..71fb278ad 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -55,8 +55,10 @@ enum : PAddr { /// Main FCRAM FCRAM_PADDR = 0x20000000, - FCRAM_SIZE = 0x08000000, ///< FCRAM size (128MB) + FCRAM_SIZE = 0x08000000, ///< FCRAM size on the Old 3DS (128MB) + FCRAM_N3DS_SIZE = 0x10000000, ///< FCRAM size on the New 3DS (256MB) FCRAM_PADDR_END = FCRAM_PADDR + FCRAM_SIZE, + FCRAM_N3DS_PADDR_END = FCRAM_PADDR + FCRAM_N3DS_SIZE, }; /// Virtual user-space memory regions diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 00d7c636a..a14df325a 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -1,8 +1,9 @@ set(SRCS - glad.cpp - tests.cpp common/param_package.cpp core/file_sys/path_parser.cpp + core/hle/kernel/hle_ipc.cpp + glad.cpp + tests.cpp ) set(HEADERS diff --git a/src/tests/core/hle/kernel/hle_ipc.cpp b/src/tests/core/hle/kernel/hle_ipc.cpp new file mode 100644 index 000000000..e07a28c5b --- /dev/null +++ b/src/tests/core/hle/kernel/hle_ipc.cpp @@ -0,0 +1,193 @@ +// Copyright 2017 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include <catch.hpp> +#include "core/hle/ipc.h" +#include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/client_session.h" +#include "core/hle/kernel/event.h" +#include "core/hle/kernel/handle_table.h" +#include "core/hle/kernel/hle_ipc.h" +#include "core/hle/kernel/process.h" +#include "core/hle/kernel/server_session.h" + +namespace Kernel { + +static SharedPtr<Object> MakeObject() { + return Event::Create(ResetType::OneShot); +} + +TEST_CASE("HLERequestContext::PopoulateFromIncomingCommandBuffer", "[core][kernel]") { + auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair()); + HLERequestContext context(std::move(session)); + + auto process = Process::Create(CodeSet::Create("", 0)); + HandleTable handle_table; + + SECTION("works with empty cmdbuf") { + const u32_le input[]{ + IPC::MakeHeader(0x1234, 0, 0), + }; + + context.PopulateFromIncomingCommandBuffer(input, *process, handle_table); + + REQUIRE(context.CommandBuffer()[0] == 0x12340000); + } + + SECTION("translates regular params") { + const u32_le input[]{ + IPC::MakeHeader(0, 3, 0), 0x12345678, 0x21122112, 0xAABBCCDD, + }; + + context.PopulateFromIncomingCommandBuffer(input, *process, handle_table); + + auto* output = context.CommandBuffer(); + REQUIRE(output[1] == 0x12345678); + REQUIRE(output[2] == 0x21122112); + REQUIRE(output[3] == 0xAABBCCDD); + } + + SECTION("translates move handles") { + auto a = MakeObject(); + Handle a_handle = handle_table.Create(a).Unwrap(); + const u32_le input[]{ + IPC::MakeHeader(0, 0, 2), IPC::MoveHandleDesc(1), a_handle, + }; + + context.PopulateFromIncomingCommandBuffer(input, *process, handle_table); + + auto* output = context.CommandBuffer(); + REQUIRE(context.GetIncomingHandle(output[2]) == a); + REQUIRE(handle_table.GetGeneric(a_handle) == nullptr); + } + + SECTION("translates copy handles") { + auto a = MakeObject(); + Handle a_handle = handle_table.Create(a).Unwrap(); + const u32_le input[]{ + IPC::MakeHeader(0, 0, 2), IPC::CopyHandleDesc(1), a_handle, + }; + + context.PopulateFromIncomingCommandBuffer(input, *process, handle_table); + + auto* output = context.CommandBuffer(); + REQUIRE(context.GetIncomingHandle(output[2]) == a); + REQUIRE(handle_table.GetGeneric(a_handle) == a); + } + + SECTION("translates multi-handle descriptors") { + auto a = MakeObject(); + auto b = MakeObject(); + auto c = MakeObject(); + const u32_le input[]{ + IPC::MakeHeader(0, 0, 5), IPC::MoveHandleDesc(2), + handle_table.Create(a).Unwrap(), handle_table.Create(b).Unwrap(), + IPC::MoveHandleDesc(1), handle_table.Create(c).Unwrap(), + }; + + context.PopulateFromIncomingCommandBuffer(input, *process, handle_table); + + auto* output = context.CommandBuffer(); + REQUIRE(context.GetIncomingHandle(output[2]) == a); + REQUIRE(context.GetIncomingHandle(output[3]) == b); + REQUIRE(context.GetIncomingHandle(output[5]) == c); + } + + SECTION("translates CallingPid descriptors") { + const u32_le input[]{ + IPC::MakeHeader(0, 0, 2), IPC::CallingPidDesc(), 0x98989898, + }; + + context.PopulateFromIncomingCommandBuffer(input, *process, handle_table); + + REQUIRE(context.CommandBuffer()[2] == process->process_id); + } + + SECTION("translates mixed params") { + auto a = MakeObject(); + const u32_le input[]{ + IPC::MakeHeader(0, 2, 4), + 0x12345678, + 0xABCDEF00, + IPC::MoveHandleDesc(1), + handle_table.Create(a).Unwrap(), + IPC::CallingPidDesc(), + 0, + }; + + context.PopulateFromIncomingCommandBuffer(input, *process, handle_table); + + auto* output = context.CommandBuffer(); + REQUIRE(output[1] == 0x12345678); + REQUIRE(output[2] == 0xABCDEF00); + REQUIRE(context.GetIncomingHandle(output[4]) == a); + REQUIRE(output[6] == process->process_id); + } +} + +TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") { + auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair()); + HLERequestContext context(std::move(session)); + + auto process = Process::Create(CodeSet::Create("", 0)); + HandleTable handle_table; + auto* input = context.CommandBuffer(); + u32_le output[IPC::COMMAND_BUFFER_LENGTH]; + + SECTION("works with empty cmdbuf") { + input[0] = IPC::MakeHeader(0x1234, 0, 0); + + context.WriteToOutgoingCommandBuffer(output, *process, handle_table); + + REQUIRE(output[0] == 0x12340000); + } + + SECTION("translates regular params") { + input[0] = IPC::MakeHeader(0, 3, 0); + input[1] = 0x12345678; + input[2] = 0x21122112; + input[3] = 0xAABBCCDD; + + context.WriteToOutgoingCommandBuffer(output, *process, handle_table); + + REQUIRE(output[1] == 0x12345678); + REQUIRE(output[2] == 0x21122112); + REQUIRE(output[3] == 0xAABBCCDD); + } + + SECTION("translates move/copy handles") { + auto a = MakeObject(); + auto b = MakeObject(); + input[0] = IPC::MakeHeader(0, 0, 4); + input[1] = IPC::MoveHandleDesc(1); + input[2] = context.AddOutgoingHandle(a); + input[3] = IPC::CopyHandleDesc(1); + input[4] = context.AddOutgoingHandle(b); + + context.WriteToOutgoingCommandBuffer(output, *process, handle_table); + + REQUIRE(handle_table.GetGeneric(output[2]) == a); + REQUIRE(handle_table.GetGeneric(output[4]) == b); + } + + SECTION("translates multi-handle descriptors") { + auto a = MakeObject(); + auto b = MakeObject(); + auto c = MakeObject(); + input[0] = IPC::MakeHeader(0, 0, 5); + input[1] = IPC::MoveHandleDesc(2); + input[2] = context.AddOutgoingHandle(a); + input[3] = context.AddOutgoingHandle(b); + input[4] = IPC::CopyHandleDesc(1); + input[5] = context.AddOutgoingHandle(c); + + context.WriteToOutgoingCommandBuffer(output, *process, handle_table); + + REQUIRE(handle_table.GetGeneric(output[2]) == a); + REQUIRE(handle_table.GetGeneric(output[3]) == b); + REQUIRE(handle_table.GetGeneric(output[5]) == c); + } +} + +} // namespace Kernel |