svc_process.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "core/core.h"
  4. #include "core/hle/kernel/k_process.h"
  5. #include "core/hle/kernel/svc.h"
  6. namespace Kernel::Svc {
  7. /// Exits the current process
  8. void ExitProcess(Core::System& system) {
  9. auto* current_process = GetCurrentProcessPointer(system.Kernel());
  10. LOG_INFO(Kernel_SVC, "Process {} exiting", current_process->GetProcessID());
  11. ASSERT_MSG(current_process->GetState() == KProcess::State::Running,
  12. "Process has already exited");
  13. system.Exit();
  14. }
  15. /// Gets the ID of the specified process or a specified thread's owning process.
  16. Result GetProcessId(Core::System& system, u64* out_process_id, Handle handle) {
  17. LOG_DEBUG(Kernel_SVC, "called handle=0x{:08X}", handle);
  18. // Get the object from the handle table.
  19. KScopedAutoObject obj = GetCurrentProcess(system.Kernel())
  20. .GetHandleTable()
  21. .GetObject<KAutoObject>(static_cast<Handle>(handle));
  22. R_UNLESS(obj.IsNotNull(), ResultInvalidHandle);
  23. // Get the process from the object.
  24. KProcess* process = nullptr;
  25. if (KProcess* p = obj->DynamicCast<KProcess*>(); p != nullptr) {
  26. // The object is a process, so we can use it directly.
  27. process = p;
  28. } else if (KThread* t = obj->DynamicCast<KThread*>(); t != nullptr) {
  29. // The object is a thread, so we want to use its parent.
  30. process = reinterpret_cast<KThread*>(obj.GetPointerUnsafe())->GetOwnerProcess();
  31. } else {
  32. // TODO(bunnei): This should also handle debug objects before returning.
  33. UNIMPLEMENTED_MSG("Debug objects not implemented");
  34. }
  35. // Make sure the target process exists.
  36. R_UNLESS(process != nullptr, ResultInvalidHandle);
  37. // Get the process id.
  38. *out_process_id = process->GetId();
  39. return ResultSuccess;
  40. }
  41. Result GetProcessList(Core::System& system, s32* out_num_processes, VAddr out_process_ids,
  42. int32_t out_process_ids_size) {
  43. LOG_DEBUG(Kernel_SVC, "called. out_process_ids=0x{:016X}, out_process_ids_size={}",
  44. out_process_ids, out_process_ids_size);
  45. // If the supplied size is negative or greater than INT32_MAX / sizeof(u64), bail.
  46. if ((out_process_ids_size & 0xF0000000) != 0) {
  47. LOG_ERROR(Kernel_SVC,
  48. "Supplied size outside [0, 0x0FFFFFFF] range. out_process_ids_size={}",
  49. out_process_ids_size);
  50. return ResultOutOfRange;
  51. }
  52. auto& kernel = system.Kernel();
  53. const auto total_copy_size = out_process_ids_size * sizeof(u64);
  54. if (out_process_ids_size > 0 && !GetCurrentProcess(kernel).PageTable().IsInsideAddressSpace(
  55. out_process_ids, total_copy_size)) {
  56. LOG_ERROR(Kernel_SVC, "Address range outside address space. begin=0x{:016X}, end=0x{:016X}",
  57. out_process_ids, out_process_ids + total_copy_size);
  58. return ResultInvalidCurrentMemory;
  59. }
  60. auto& memory = system.Memory();
  61. const auto& process_list = kernel.GetProcessList();
  62. const auto num_processes = process_list.size();
  63. const auto copy_amount =
  64. std::min(static_cast<std::size_t>(out_process_ids_size), num_processes);
  65. for (std::size_t i = 0; i < copy_amount; ++i) {
  66. memory.Write64(out_process_ids, process_list[i]->GetProcessID());
  67. out_process_ids += sizeof(u64);
  68. }
  69. *out_num_processes = static_cast<u32>(num_processes);
  70. return ResultSuccess;
  71. }
  72. Result GetProcessInfo(Core::System& system, s64* out, Handle process_handle,
  73. ProcessInfoType info_type) {
  74. LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, type=0x{:X}", process_handle, info_type);
  75. const auto& handle_table = GetCurrentProcess(system.Kernel()).GetHandleTable();
  76. KScopedAutoObject process = handle_table.GetObject<KProcess>(process_handle);
  77. if (process.IsNull()) {
  78. LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}",
  79. process_handle);
  80. return ResultInvalidHandle;
  81. }
  82. if (info_type != ProcessInfoType::ProcessState) {
  83. LOG_ERROR(Kernel_SVC, "Expected info_type to be ProcessState but got {} instead",
  84. info_type);
  85. return ResultInvalidEnumValue;
  86. }
  87. *out = static_cast<s64>(process->GetState());
  88. return ResultSuccess;
  89. }
  90. Result CreateProcess(Core::System& system, Handle* out_handle, uint64_t parameters, uint64_t caps,
  91. int32_t num_caps) {
  92. UNIMPLEMENTED();
  93. R_THROW(ResultNotImplemented);
  94. }
  95. Result StartProcess(Core::System& system, Handle process_handle, int32_t priority, int32_t core_id,
  96. uint64_t main_thread_stack_size) {
  97. UNIMPLEMENTED();
  98. R_THROW(ResultNotImplemented);
  99. }
  100. Result TerminateProcess(Core::System& system, Handle process_handle) {
  101. UNIMPLEMENTED();
  102. R_THROW(ResultNotImplemented);
  103. }
  104. void ExitProcess64(Core::System& system) {
  105. ExitProcess(system);
  106. }
  107. Result GetProcessId64(Core::System& system, uint64_t* out_process_id, Handle process_handle) {
  108. R_RETURN(GetProcessId(system, out_process_id, process_handle));
  109. }
  110. Result GetProcessList64(Core::System& system, int32_t* out_num_processes, uint64_t out_process_ids,
  111. int32_t max_out_count) {
  112. R_RETURN(GetProcessList(system, out_num_processes, out_process_ids, max_out_count));
  113. }
  114. Result CreateProcess64(Core::System& system, Handle* out_handle, uint64_t parameters, uint64_t caps,
  115. int32_t num_caps) {
  116. R_RETURN(CreateProcess(system, out_handle, parameters, caps, num_caps));
  117. }
  118. Result StartProcess64(Core::System& system, Handle process_handle, int32_t priority,
  119. int32_t core_id, uint64_t main_thread_stack_size) {
  120. R_RETURN(StartProcess(system, process_handle, priority, core_id, main_thread_stack_size));
  121. }
  122. Result TerminateProcess64(Core::System& system, Handle process_handle) {
  123. R_RETURN(TerminateProcess(system, process_handle));
  124. }
  125. Result GetProcessInfo64(Core::System& system, int64_t* out_info, Handle process_handle,
  126. ProcessInfoType info_type) {
  127. R_RETURN(GetProcessInfo(system, out_info, process_handle, info_type));
  128. }
  129. void ExitProcess64From32(Core::System& system) {
  130. ExitProcess(system);
  131. }
  132. Result GetProcessId64From32(Core::System& system, uint64_t* out_process_id, Handle process_handle) {
  133. R_RETURN(GetProcessId(system, out_process_id, process_handle));
  134. }
  135. Result GetProcessList64From32(Core::System& system, int32_t* out_num_processes,
  136. uint32_t out_process_ids, int32_t max_out_count) {
  137. R_RETURN(GetProcessList(system, out_num_processes, out_process_ids, max_out_count));
  138. }
  139. Result CreateProcess64From32(Core::System& system, Handle* out_handle, uint32_t parameters,
  140. uint32_t caps, int32_t num_caps) {
  141. R_RETURN(CreateProcess(system, out_handle, parameters, caps, num_caps));
  142. }
  143. Result StartProcess64From32(Core::System& system, Handle process_handle, int32_t priority,
  144. int32_t core_id, uint64_t main_thread_stack_size) {
  145. R_RETURN(StartProcess(system, process_handle, priority, core_id, main_thread_stack_size));
  146. }
  147. Result TerminateProcess64From32(Core::System& system, Handle process_handle) {
  148. R_RETURN(TerminateProcess(system, process_handle));
  149. }
  150. Result GetProcessInfo64From32(Core::System& system, int64_t* out_info, Handle process_handle,
  151. ProcessInfoType info_type) {
  152. R_RETURN(GetProcessInfo(system, out_info, process_handle, info_type));
  153. }
  154. } // namespace Kernel::Svc