diff options
Diffstat (limited to 'src/core/hle/kernel')
| -rw-r--r-- | src/core/hle/kernel/address_arbiter.cpp | 87 | ||||
| -rw-r--r-- | src/core/hle/kernel/address_arbiter.h | 36 | ||||
| -rw-r--r-- | src/core/hle/kernel/archive.cpp | 157 | ||||
| -rw-r--r-- | src/core/hle/kernel/archive.h | 38 | ||||
| -rw-r--r-- | src/core/hle/kernel/kernel.cpp | 3 | ||||
| -rw-r--r-- | src/core/hle/kernel/kernel.h | 3 | ||||
| -rw-r--r-- | src/core/hle/kernel/shared_memory.cpp | 105 | ||||
| -rw-r--r-- | src/core/hle/kernel/shared_memory.h | 48 | ||||
| -rw-r--r-- | src/core/hle/kernel/thread.cpp | 37 | ||||
| -rw-r--r-- | src/core/hle/kernel/thread.h | 7 |
10 files changed, 520 insertions, 1 deletions
diff --git a/src/core/hle/kernel/address_arbiter.cpp b/src/core/hle/kernel/address_arbiter.cpp new file mode 100644 index 000000000..61717bbe4 --- /dev/null +++ b/src/core/hle/kernel/address_arbiter.cpp @@ -0,0 +1,87 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/common_types.h" + +#include "core/mem_map.h" + +#include "core/hle/hle.h" +#include "core/hle/kernel/address_arbiter.h" +#include "core/hle/kernel/thread.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Kernel namespace + +namespace Kernel { + +class AddressArbiter : public Object { +public: + const char* GetTypeName() const { return "Arbiter"; } + const char* GetName() const { return name.c_str(); } + + static Kernel::HandleType GetStaticHandleType() { return HandleType::AddressArbiter; } + Kernel::HandleType GetHandleType() const { return HandleType::AddressArbiter; } + + std::string name; ///< Name of address arbiter object (optional) + + /** + * Wait for kernel object to synchronize + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result WaitSynchronization(bool* wait) { + // TODO(bunnei): ImplementMe + ERROR_LOG(OSHLE, "(UNIMPLEMENTED)"); + return 0; + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Arbitrate an address +Result ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s32 value) { + switch (type) { + + // Signal thread(s) waiting for arbitrate address... + case ArbitrationType::Signal: + // Negative value means resume all threads + if (value < 0) { + ArbitrateAllThreads(handle, address); + } else { + // Resume first N threads + for(int i = 0; i < value; i++) + ArbitrateHighestPriorityThread(handle, address); + } + HLE::Reschedule(__func__); + + // Wait current thread (acquire the arbiter)... + case ArbitrationType::WaitIfLessThan: + if ((s32)Memory::Read32(address) <= value) { + Kernel::WaitCurrentThread(WAITTYPE_ARB, handle); + HLE::Reschedule(__func__); + } + + default: + ERROR_LOG(KERNEL, "unknown type=%d", type); + return -1; + } + return 0; +} + +/// Create an address arbiter +AddressArbiter* CreateAddressArbiter(Handle& handle, const std::string& name) { + AddressArbiter* address_arbiter = new AddressArbiter; + handle = Kernel::g_object_pool.Create(address_arbiter); + address_arbiter->name = name; + return address_arbiter; +} + +/// Create an address arbiter +Handle CreateAddressArbiter(const std::string& name) { + Handle handle; + CreateAddressArbiter(handle, name); + return handle; +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/address_arbiter.h b/src/core/hle/kernel/address_arbiter.h new file mode 100644 index 000000000..a483fe466 --- /dev/null +++ b/src/core/hle/kernel/address_arbiter.h @@ -0,0 +1,36 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/hle/kernel/kernel.h" + +// Address arbiters are an underlying kernel synchronization object that can be created/used via +// supervisor calls (SVCs). They function as sort of a global lock. Typically, games/other CTR +// applications use them as an underlying mechanism to implement thread-safe barriers, events, and +// semphores. + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Kernel namespace + +namespace Kernel { + +/// Address arbitration types +enum class ArbitrationType : u32 { + Signal, + WaitIfLessThan, + DecrementAndWaitIfLessThan, + WaitIfLessThanWithTimeout, + DecrementAndWaitIfLessThanWithTimeout, +}; + +/// Arbitrate an address +Result ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s32 value); + +/// Create an address arbiter +Handle CreateAddressArbiter(const std::string& name = "Unknown"); + +} // namespace FileSys diff --git a/src/core/hle/kernel/archive.cpp b/src/core/hle/kernel/archive.cpp new file mode 100644 index 000000000..76b2520da --- /dev/null +++ b/src/core/hle/kernel/archive.cpp @@ -0,0 +1,157 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/common_types.h" + +#include "core/file_sys/archive.h" +#include "core/hle/service/service.h" +#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/archive.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Kernel namespace + +namespace Kernel { + +// Command to access archive file +enum class FileCommand : u32 { + Dummy1 = 0x000100C6, + Control = 0x040100C4, + OpenSubFile = 0x08010100, + Read = 0x080200C2, + Write = 0x08030102, + GetSize = 0x08040000, + SetSize = 0x08050080, + GetAttributes = 0x08060000, + SetAttributes = 0x08070040, + Close = 0x08080000, + Flush = 0x08090000, +}; + +class Archive : public Object { +public: + const char* GetTypeName() const { return "Archive"; } + const char* GetName() const { return name.c_str(); } + + static Kernel::HandleType GetStaticHandleType() { return HandleType::Archive; } + Kernel::HandleType GetHandleType() const { return HandleType::Archive; } + + std::string name; ///< Name of archive (optional) + FileSys::Archive* backend; ///< Archive backend interface + + /** + * Synchronize kernel object + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result SyncRequest(bool* wait) { + u32* cmd_buff = Service::GetCommandBuffer(); + FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]); + switch (cmd) { + + // Read from archive... + case FileCommand::Read: + { + u64 offset = cmd_buff[1] | ((u64) cmd_buff[2]) << 32; + u32 length = cmd_buff[3]; + u32 address = cmd_buff[5]; + cmd_buff[2] = backend->Read(offset, length, Memory::GetPointer(address)); + break; + } + + // Unknown command... + default: + ERROR_LOG(KERNEL, "Unknown command=0x%08X!", cmd); + return -1; + } + cmd_buff[1] = 0; // No error + return 0; + } + + /** + * Wait for kernel object to synchronize + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result WaitSynchronization(bool* wait) { + // TODO(bunnei): ImplementMe + ERROR_LOG(OSHLE, "(UNIMPLEMENTED)"); + return 0; + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +std::map<FileSys::Archive::IdCode, Handle> g_archive_map; ///< Map of file archives by IdCode + +/** + * Opens an archive + * @param id_code IdCode of the archive to open + * @return Handle to archive if it exists, otherwise a null handle (0) + */ +Handle OpenArchive(FileSys::Archive::IdCode id_code) { + auto itr = g_archive_map.find(id_code); + if (itr == g_archive_map.end()) { + return 0; + } + return itr->second; +} + +/** + * Mounts an archive + * @param archive Pointer to the archive to mount + * @return Result of operation, 0 on success, otherwise error code + */ +Result MountArchive(Archive* archive) { + FileSys::Archive::IdCode id_code = archive->backend->GetIdCode(); + if (0 != OpenArchive(id_code)) { + ERROR_LOG(KERNEL, "Cannot mount two archives with the same ID code! (%d)", (int) id_code); + return -1; + } + g_archive_map[id_code] = archive->GetHandle(); + INFO_LOG(KERNEL, "Mounted archive %s", archive->GetName()); + return 0; +} + +/** + * Creates an Archive + * @param handle Handle to newly created archive object + * @param backend File system backend interface to the archive + * @param name Optional name of Archive + * @return Newly created Archive object + */ +Archive* CreateArchive(Handle& handle, FileSys::Archive* backend, const std::string& name) { + Archive* archive = new Archive; + handle = Kernel::g_object_pool.Create(archive); + archive->name = name; + archive->backend = backend; + + MountArchive(archive); + + return archive; +} + +/** + * Creates an Archive + * @param backend File system backend interface to the archive + * @param name Optional name of Archive + * @return Handle to newly created Archive object + */ +Handle CreateArchive(FileSys::Archive* backend, const std::string& name) { + Handle handle; + Archive* archive = CreateArchive(handle, backend, name); + return handle; +} + +/// Initialize archives +void ArchiveInit() { + g_archive_map.clear(); +} + +/// Shutdown archives +void ArchiveShutdown() { + g_archive_map.clear(); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/archive.h b/src/core/hle/kernel/archive.h new file mode 100644 index 000000000..3758e7061 --- /dev/null +++ b/src/core/hle/kernel/archive.h @@ -0,0 +1,38 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/hle/kernel/kernel.h" +#include "core/file_sys/archive.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Kernel namespace + +namespace Kernel { + +/** + * Opens an archive + * @param id_code IdCode of the archive to open + * @return Handle to archive if it exists, otherwise a null handle (0) + */ +Handle OpenArchive(FileSys::Archive::IdCode id_code); + +/** + * Creates an Archive + * @param backend File system backend interface to the archive + * @param name Optional name of Archive + * @return Handle to newly created Archive object + */ +Handle CreateArchive(FileSys::Archive* backend, const std::string& name); + +/// Initialize archives +void ArchiveInit(); + +/// Shutdown archives +void ArchiveShutdown(); + +} // namespace FileSys diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index cda183add..7d9bd261e 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -9,6 +9,7 @@ #include "core/core.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/thread.h" +#include "core/hle/kernel/archive.h" namespace Kernel { @@ -133,11 +134,13 @@ Object* ObjectPool::CreateByIDType(int type) { /// Initialize the kernel void Init() { Kernel::ThreadingInit(); + Kernel::ArchiveInit(); } /// Shutdown the kernel void Shutdown() { Kernel::ThreadingShutdown(); + Kernel::ArchiveShutdown(); g_object_pool.Clear(); // Free all kernel objects } diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 3f15da0ac..d9afcdd25 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -26,9 +26,10 @@ enum class HandleType : u32 { Redirection = 6, Thread = 7, Process = 8, - Arbiter = 9, + AddressArbiter = 9, File = 10, Semaphore = 11, + Archive = 12, }; enum { diff --git a/src/core/hle/kernel/shared_memory.cpp b/src/core/hle/kernel/shared_memory.cpp new file mode 100644 index 000000000..52823048f --- /dev/null +++ b/src/core/hle/kernel/shared_memory.cpp @@ -0,0 +1,105 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/common.h" + +#include "core/mem_map.h" +#include "core/hle/kernel/shared_memory.h" + +namespace Kernel { + +class SharedMemory : public Object { +public: + const char* GetTypeName() const { return "SharedMemory"; } + + static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::SharedMemory; } + Kernel::HandleType GetHandleType() const { return Kernel::HandleType::SharedMemory; } + + /** + * Wait for kernel object to synchronize + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result WaitSynchronization(bool* wait) { + // TODO(bunnei): ImplementMe + ERROR_LOG(OSHLE, "(UNIMPLEMENTED)"); + return 0; + } + + u32 base_address; ///< Address of shared memory block in RAM + MemoryPermission permissions; ///< Permissions of shared memory block (SVC field) + MemoryPermission other_permissions; ///< Other permissions of shared memory block (SVC field) + std::string name; ///< Name of shared memory object (optional) +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/** + * Creates a shared memory object + * @param handle Handle of newly created shared memory object + * @param name Name of shared memory object + * @return Pointer to newly created shared memory object + */ +SharedMemory* CreateSharedMemory(Handle& handle, const std::string& name) { + SharedMemory* shared_memory = new SharedMemory; + handle = Kernel::g_object_pool.Create(shared_memory); + shared_memory->name = name; + return shared_memory; +} + +/** + * Creates a shared memory object + * @param name Optional name of shared memory object + * @return Handle of newly created shared memory object + */ +Handle CreateSharedMemory(const std::string& name) { + Handle handle; + CreateSharedMemory(handle, name); + return handle; +} + +/** + * Maps a shared memory block to an address in system memory + * @param handle Shared memory block handle + * @param address Address in system memory to map shared memory block to + * @param permissions Memory block map permissions (specified by SVC field) + * @param other_permissions Memory block map other permissions (specified by SVC field) + * @return Result of operation, 0 on success, otherwise error code + */ +Result MapSharedMemory(u32 handle, u32 address, MemoryPermission permissions, + MemoryPermission other_permissions) { + + if (address < Memory::SHARED_MEMORY_VADDR || address >= Memory::SHARED_MEMORY_VADDR_END) { + ERROR_LOG(KERNEL, "cannot map handle=0x%08X, address=0x%08X outside of shared mem bounds!", + handle); + return -1; + } + SharedMemory* shared_memory = Kernel::g_object_pool.GetFast<SharedMemory>(handle); + _assert_msg_(KERNEL, (shared_memory != nullptr), "handle 0x%08X is not valid!", handle); + + shared_memory->base_address = address; + shared_memory->permissions = permissions; + shared_memory->other_permissions = other_permissions; + + return 0; +} + +/** + * Gets a pointer to the shared memory block + * @param handle Shared memory block handle + * @param offset Offset from the start of the shared memory block to get pointer + * @return Pointer to the shared memory block from the specified offset + */ +u8* GetSharedMemoryPointer(Handle handle, u32 offset) { + SharedMemory* shared_memory = Kernel::g_object_pool.GetFast<SharedMemory>(handle); + _assert_msg_(KERNEL, (shared_memory != nullptr), "handle 0x%08X is not valid!", handle); + + if (0 != shared_memory->base_address) + return Memory::GetPointer(shared_memory->base_address + offset); + + ERROR_LOG(KERNEL, "memory block handle=0x%08X not mapped!", handle); + return nullptr; +} + +} // namespace diff --git a/src/core/hle/kernel/shared_memory.h b/src/core/hle/kernel/shared_memory.h new file mode 100644 index 000000000..5312b8854 --- /dev/null +++ b/src/core/hle/kernel/shared_memory.h @@ -0,0 +1,48 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/hle/kernel/kernel.h" + +namespace Kernel { + +/// Permissions for mapped shared memory blocks +enum class MemoryPermission : u32 { + None = 0, + Read = (1u << 0), + Write = (1u << 1), + ReadWrite = (Read | Write), + DontCare = (1u << 28) +}; + +/** + * Creates a shared memory object + * @param name Optional name of shared memory object + * @return Handle of newly created shared memory object + */ +Handle CreateSharedMemory(const std::string& name="Unknown"); + +/** + * Maps a shared memory block to an address in system memory + * @param handle Shared memory block handle + * @param address Address in system memory to map shared memory block to + * @param permissions Memory block map permissions (specified by SVC field) + * @param other_permissions Memory block map other permissions (specified by SVC field) + * @return Result of operation, 0 on success, otherwise error code + */ +Result MapSharedMemory(u32 handle, u32 address, MemoryPermission permissions, + MemoryPermission other_permissions); + +/** + * Gets a pointer to the shared memory block + * @param handle Shared memory block handle + * @param offset Offset from the start of the shared memory block to get pointer + * @return Pointer to the shared memory block from the specified offset + */ +u8* GetSharedMemoryPointer(Handle handle, u32 offset); + +} // namespace diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index ab5a5559e..86bbf29d0 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -188,6 +188,43 @@ void ChangeThreadState(Thread* t, ThreadStatus new_status) { } } +/// Arbitrate the highest priority thread that is waiting +Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address) { + Handle highest_priority_thread = 0; + s32 priority = THREADPRIO_LOWEST; + + // Iterate through threads, find highest priority thread that is waiting to be arbitrated... + for (const auto& handle : g_thread_queue) { + + // TODO(bunnei): Verify arbiter address... + if (!VerifyWait(handle, WAITTYPE_ARB, arbiter)) + continue; + + Thread* thread = g_object_pool.GetFast<Thread>(handle); + if(thread->current_priority <= priority) { + highest_priority_thread = handle; + priority = thread->current_priority; + } + } + // If a thread was arbitrated, resume it + if (0 != highest_priority_thread) + ResumeThreadFromWait(highest_priority_thread); + + return highest_priority_thread; +} + +/// Arbitrate all threads currently waiting +void ArbitrateAllThreads(u32 arbiter, u32 address) { + + // Iterate through threads, find highest priority thread that is waiting to be arbitrated... + for (const auto& handle : g_thread_queue) { + + // TODO(bunnei): Verify arbiter address... + if (VerifyWait(handle, WAITTYPE_ARB, arbiter)) + ResumeThreadFromWait(handle); + } +} + /// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields) void CallThread(Thread* t) { // Stop waiting diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 04914ba90..f2bfdfa1a 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -39,6 +39,7 @@ enum WaitType { WAITTYPE_VBLANK, WAITTYPE_MUTEX, WAITTYPE_SYNCH, + WAITTYPE_ARB, }; namespace Kernel { @@ -59,6 +60,12 @@ void StopThread(Handle thread, const char* reason); /// Resumes a thread from waiting by marking it as "ready" void ResumeThreadFromWait(Handle handle); +/// Arbitrate the highest priority thread that is waiting +Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address); + +/// Arbitrate all threads currently waiting... +void ArbitrateAllThreads(u32 arbiter, u32 address); + /// Gets the current thread handle Handle GetCurrentThreadHandle(); |
