kernel.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include "common/assert.h"
  6. #include "common/logging/log.h"
  7. #include "core/hle/config_mem.h"
  8. #include "core/hle/kernel/kernel.h"
  9. #include "core/hle/kernel/memory.h"
  10. #include "core/hle/kernel/process.h"
  11. #include "core/hle/kernel/resource_limit.h"
  12. #include "core/hle/kernel/thread.h"
  13. #include "core/hle/kernel/timer.h"
  14. #include "core/hle/shared_page.h"
  15. namespace Kernel {
  16. unsigned int Object::next_object_id;
  17. HandleTable g_handle_table;
  18. void WaitObject::AddWaitingThread(SharedPtr<Thread> thread) {
  19. auto itr = std::find(waiting_threads.begin(), waiting_threads.end(), thread);
  20. if (itr == waiting_threads.end())
  21. waiting_threads.push_back(std::move(thread));
  22. }
  23. void WaitObject::RemoveWaitingThread(Thread* thread) {
  24. auto itr = std::find(waiting_threads.begin(), waiting_threads.end(), thread);
  25. if (itr != waiting_threads.end())
  26. waiting_threads.erase(itr);
  27. }
  28. void WaitObject::WakeupAllWaitingThreads() {
  29. for (auto thread : waiting_threads)
  30. thread->ResumeFromWait();
  31. waiting_threads.clear();
  32. HLE::Reschedule(__func__);
  33. }
  34. const std::vector<SharedPtr<Thread>>& WaitObject::GetWaitingThreads() const {
  35. return waiting_threads;
  36. }
  37. HandleTable::HandleTable() {
  38. next_generation = 1;
  39. Clear();
  40. }
  41. ResultVal<Handle> HandleTable::Create(SharedPtr<Object> obj) {
  42. DEBUG_ASSERT(obj != nullptr);
  43. u16 slot = next_free_slot;
  44. if (slot >= generations.size()) {
  45. LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use.");
  46. return ERR_OUT_OF_HANDLES;
  47. }
  48. next_free_slot = generations[slot];
  49. u16 generation = next_generation++;
  50. // Overflow count so it fits in the 15 bits dedicated to the generation in the handle.
  51. // CTR-OS doesn't use generation 0, so skip straight to 1.
  52. if (next_generation >= (1 << 15))
  53. next_generation = 1;
  54. generations[slot] = generation;
  55. objects[slot] = std::move(obj);
  56. Handle handle = generation | (slot << 15);
  57. return MakeResult<Handle>(handle);
  58. }
  59. ResultVal<Handle> HandleTable::Duplicate(Handle handle) {
  60. SharedPtr<Object> object = GetGeneric(handle);
  61. if (object == nullptr) {
  62. LOG_ERROR(Kernel, "Tried to duplicate invalid handle: %08X", handle);
  63. return ERR_INVALID_HANDLE;
  64. }
  65. return Create(std::move(object));
  66. }
  67. ResultCode HandleTable::Close(Handle handle) {
  68. if (!IsValid(handle))
  69. return ERR_INVALID_HANDLE;
  70. u16 slot = GetSlot(handle);
  71. objects[slot] = nullptr;
  72. generations[slot] = next_free_slot;
  73. next_free_slot = slot;
  74. return RESULT_SUCCESS;
  75. }
  76. bool HandleTable::IsValid(Handle handle) const {
  77. size_t slot = GetSlot(handle);
  78. u16 generation = GetGeneration(handle);
  79. return slot < MAX_COUNT && objects[slot] != nullptr && generations[slot] == generation;
  80. }
  81. SharedPtr<Object> HandleTable::GetGeneric(Handle handle) const {
  82. if (handle == CurrentThread) {
  83. return GetCurrentThread();
  84. } else if (handle == CurrentProcess) {
  85. return g_current_process;
  86. }
  87. if (!IsValid(handle)) {
  88. return nullptr;
  89. }
  90. return objects[GetSlot(handle)];
  91. }
  92. void HandleTable::Clear() {
  93. for (u16 i = 0; i < MAX_COUNT; ++i) {
  94. generations[i] = i + 1;
  95. objects[i] = nullptr;
  96. }
  97. next_free_slot = 0;
  98. }
  99. /// Initialize the kernel
  100. void Init() {
  101. ConfigMem::Init();
  102. SharedPage::Init();
  103. // TODO(yuriks): The memory type parameter needs to be determined by the ExHeader field instead
  104. // For now it defaults to the one with a largest allocation to the app
  105. Kernel::MemoryInit(2); // Allocates 96MB to the application
  106. Kernel::ResourceLimitsInit();
  107. Kernel::ThreadingInit();
  108. Kernel::TimersInit();
  109. Object::next_object_id = 0;
  110. // TODO(Subv): Start the process ids from 10 for now, as lower PIDs are
  111. // reserved for low-level services
  112. Process::next_process_id = 10;
  113. }
  114. /// Shutdown the kernel
  115. void Shutdown() {
  116. g_handle_table.Clear(); // Free all kernel objects
  117. Kernel::ThreadingShutdown();
  118. g_current_process = nullptr;
  119. Kernel::TimersShutdown();
  120. Kernel::ResourceLimitsShutdown();
  121. Kernel::MemoryShutdown();
  122. }
  123. } // namespace