pm.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "core/core.h"
  5. #include "core/hle/ipc_helpers.h"
  6. #include "core/hle/kernel/k_process.h"
  7. #include "core/hle/kernel/kernel.h"
  8. #include "core/hle/service/pm/pm.h"
  9. #include "core/hle/service/service.h"
  10. namespace Service::PM {
  11. namespace {
  12. constexpr ResultCode ERROR_PROCESS_NOT_FOUND{ErrorModule::PM, 1};
  13. constexpr u64 NO_PROCESS_FOUND_PID{0};
  14. std::optional<Kernel::KProcess*> SearchProcessList(
  15. const std::vector<Kernel::KProcess*>& process_list,
  16. std::function<bool(Kernel::KProcess*)> predicate) {
  17. const auto iter = std::find_if(process_list.begin(), process_list.end(), predicate);
  18. if (iter == process_list.end()) {
  19. return std::nullopt;
  20. }
  21. return *iter;
  22. }
  23. void GetApplicationPidGeneric(Kernel::HLERequestContext& ctx,
  24. const std::vector<Kernel::KProcess*>& process_list) {
  25. const auto process = SearchProcessList(process_list, [](const auto& proc) {
  26. return proc->GetProcessID() == Kernel::KProcess::ProcessIDMin;
  27. });
  28. IPC::ResponseBuilder rb{ctx, 4};
  29. rb.Push(ResultSuccess);
  30. rb.Push(process.has_value() ? (*process)->GetProcessID() : NO_PROCESS_FOUND_PID);
  31. }
  32. } // Anonymous namespace
  33. class BootMode final : public ServiceFramework<BootMode> {
  34. public:
  35. explicit BootMode(Core::System& system_) : ServiceFramework{system_, "pm:bm"} {
  36. static const FunctionInfo functions[] = {
  37. {0, &BootMode::GetBootMode, "GetBootMode"},
  38. {1, &BootMode::SetMaintenanceBoot, "SetMaintenanceBoot"},
  39. };
  40. RegisterHandlers(functions);
  41. }
  42. private:
  43. void GetBootMode(Kernel::HLERequestContext& ctx) {
  44. LOG_DEBUG(Service_PM, "called");
  45. IPC::ResponseBuilder rb{ctx, 3};
  46. rb.Push(ResultSuccess);
  47. rb.PushEnum(boot_mode);
  48. }
  49. void SetMaintenanceBoot(Kernel::HLERequestContext& ctx) {
  50. LOG_DEBUG(Service_PM, "called");
  51. boot_mode = SystemBootMode::Maintenance;
  52. IPC::ResponseBuilder rb{ctx, 2};
  53. rb.Push(ResultSuccess);
  54. }
  55. SystemBootMode boot_mode = SystemBootMode::Normal;
  56. };
  57. class DebugMonitor final : public ServiceFramework<DebugMonitor> {
  58. public:
  59. explicit DebugMonitor(Core::System& system_)
  60. : ServiceFramework{system_, "pm:dmnt"}, kernel{system_.Kernel()} {
  61. // clang-format off
  62. static const FunctionInfo functions[] = {
  63. {0, nullptr, "GetJitDebugProcessIdList"},
  64. {1, nullptr, "StartProcess"},
  65. {2, &DebugMonitor::GetProcessId, "GetProcessId"},
  66. {3, nullptr, "HookToCreateProcess"},
  67. {4, &DebugMonitor::GetApplicationProcessId, "GetApplicationProcessId"},
  68. {5, nullptr, "HookToCreateApplicationProgress"},
  69. {6, nullptr, "ClearHook"},
  70. };
  71. // clang-format on
  72. RegisterHandlers(functions);
  73. }
  74. private:
  75. void GetProcessId(Kernel::HLERequestContext& ctx) {
  76. IPC::RequestParser rp{ctx};
  77. const auto title_id = rp.PopRaw<u64>();
  78. LOG_DEBUG(Service_PM, "called, title_id={:016X}", title_id);
  79. const auto process =
  80. SearchProcessList(kernel.GetProcessList(), [title_id](const auto& proc) {
  81. return proc->GetTitleID() == title_id;
  82. });
  83. if (!process.has_value()) {
  84. IPC::ResponseBuilder rb{ctx, 2};
  85. rb.Push(ERROR_PROCESS_NOT_FOUND);
  86. return;
  87. }
  88. IPC::ResponseBuilder rb{ctx, 4};
  89. rb.Push(ResultSuccess);
  90. rb.Push((*process)->GetProcessID());
  91. }
  92. void GetApplicationProcessId(Kernel::HLERequestContext& ctx) {
  93. LOG_DEBUG(Service_PM, "called");
  94. GetApplicationPidGeneric(ctx, kernel.GetProcessList());
  95. }
  96. const Kernel::KernelCore& kernel;
  97. };
  98. class Info final : public ServiceFramework<Info> {
  99. public:
  100. explicit Info(Core::System& system_, const std::vector<Kernel::KProcess*>& process_list_)
  101. : ServiceFramework{system_, "pm:info"}, process_list{process_list_} {
  102. static const FunctionInfo functions[] = {
  103. {0, &Info::GetTitleId, "GetTitleId"},
  104. };
  105. RegisterHandlers(functions);
  106. }
  107. private:
  108. void GetTitleId(Kernel::HLERequestContext& ctx) {
  109. IPC::RequestParser rp{ctx};
  110. const auto process_id = rp.PopRaw<u64>();
  111. LOG_DEBUG(Service_PM, "called, process_id={:016X}", process_id);
  112. const auto process = SearchProcessList(process_list, [process_id](const auto& proc) {
  113. return proc->GetProcessID() == process_id;
  114. });
  115. if (!process.has_value()) {
  116. IPC::ResponseBuilder rb{ctx, 2};
  117. rb.Push(ERROR_PROCESS_NOT_FOUND);
  118. return;
  119. }
  120. IPC::ResponseBuilder rb{ctx, 4};
  121. rb.Push(ResultSuccess);
  122. rb.Push((*process)->GetTitleID());
  123. }
  124. const std::vector<Kernel::KProcess*>& process_list;
  125. };
  126. class Shell final : public ServiceFramework<Shell> {
  127. public:
  128. explicit Shell(Core::System& system_)
  129. : ServiceFramework{system_, "pm:shell"}, kernel{system_.Kernel()} {
  130. // clang-format off
  131. static const FunctionInfo functions[] = {
  132. {0, nullptr, "LaunchProgram"},
  133. {1, nullptr, "TerminateProcess"},
  134. {2, nullptr, "TerminateProgram"},
  135. {3, nullptr, "GetProcessEventHandle"},
  136. {4, nullptr, "GetProcessEventInfo"},
  137. {5, nullptr, "NotifyBootFinished"},
  138. {6, &Shell::GetApplicationProcessIdForShell, "GetApplicationProcessIdForShell"},
  139. {7, nullptr, "BoostSystemMemoryResourceLimit"},
  140. {8, nullptr, "BoostApplicationThreadResourceLimit"},
  141. {9, nullptr, "GetBootFinishedEventHandle"},
  142. };
  143. // clang-format on
  144. RegisterHandlers(functions);
  145. }
  146. private:
  147. void GetApplicationProcessIdForShell(Kernel::HLERequestContext& ctx) {
  148. LOG_DEBUG(Service_PM, "called");
  149. GetApplicationPidGeneric(ctx, kernel.GetProcessList());
  150. }
  151. const Kernel::KernelCore& kernel;
  152. };
  153. void InstallInterfaces(Core::System& system) {
  154. std::make_shared<BootMode>(system)->InstallAsService(system.ServiceManager());
  155. std::make_shared<DebugMonitor>(system)->InstallAsService(system.ServiceManager());
  156. std::make_shared<Info>(system, system.Kernel().GetProcessList())
  157. ->InstallAsService(system.ServiceManager());
  158. std::make_shared<Shell>(system)->InstallAsService(system.ServiceManager());
  159. }
  160. } // namespace Service::PM