svc_info.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/core_timing.h"
  5. #include "core/hle/kernel/k_process.h"
  6. #include "core/hle/kernel/k_resource_limit.h"
  7. #include "core/hle/kernel/svc.h"
  8. namespace Kernel::Svc {
  9. /// Gets system/memory information for the current process
  10. Result GetInfo(Core::System& system, u64* result, InfoType info_id_type, Handle handle,
  11. u64 info_sub_id) {
  12. LOG_TRACE(Kernel_SVC, "called info_id=0x{:X}, info_sub_id=0x{:X}, handle=0x{:08X}",
  13. info_id_type, info_sub_id, handle);
  14. u32 info_id = static_cast<u32>(info_id_type);
  15. switch (info_id_type) {
  16. case InfoType::CoreMask:
  17. case InfoType::PriorityMask:
  18. case InfoType::AliasRegionAddress:
  19. case InfoType::AliasRegionSize:
  20. case InfoType::HeapRegionAddress:
  21. case InfoType::HeapRegionSize:
  22. case InfoType::AslrRegionAddress:
  23. case InfoType::AslrRegionSize:
  24. case InfoType::StackRegionAddress:
  25. case InfoType::StackRegionSize:
  26. case InfoType::TotalMemorySize:
  27. case InfoType::UsedMemorySize:
  28. case InfoType::SystemResourceSizeTotal:
  29. case InfoType::SystemResourceSizeUsed:
  30. case InfoType::ProgramId:
  31. case InfoType::UserExceptionContextAddress:
  32. case InfoType::TotalNonSystemMemorySize:
  33. case InfoType::UsedNonSystemMemorySize:
  34. case InfoType::IsApplication:
  35. case InfoType::FreeThreadCount: {
  36. R_UNLESS(info_sub_id == 0, ResultInvalidEnumValue);
  37. const auto& handle_table = GetCurrentProcess(system.Kernel()).GetHandleTable();
  38. KScopedAutoObject process = handle_table.GetObject<KProcess>(handle);
  39. R_UNLESS(process.IsNotNull(), ResultInvalidHandle);
  40. switch (info_id_type) {
  41. case InfoType::CoreMask:
  42. *result = process->GetCoreMask();
  43. R_SUCCEED();
  44. case InfoType::PriorityMask:
  45. *result = process->GetPriorityMask();
  46. R_SUCCEED();
  47. case InfoType::AliasRegionAddress:
  48. *result = GetInteger(process->GetPageTable().GetAliasRegionStart());
  49. R_SUCCEED();
  50. case InfoType::AliasRegionSize:
  51. *result = process->GetPageTable().GetAliasRegionSize();
  52. R_SUCCEED();
  53. case InfoType::HeapRegionAddress:
  54. *result = GetInteger(process->GetPageTable().GetHeapRegionStart());
  55. R_SUCCEED();
  56. case InfoType::HeapRegionSize:
  57. *result = process->GetPageTable().GetHeapRegionSize();
  58. R_SUCCEED();
  59. case InfoType::AslrRegionAddress:
  60. *result = GetInteger(process->GetPageTable().GetAliasCodeRegionStart());
  61. R_SUCCEED();
  62. case InfoType::AslrRegionSize:
  63. *result = process->GetPageTable().GetAliasCodeRegionSize();
  64. R_SUCCEED();
  65. case InfoType::StackRegionAddress:
  66. *result = GetInteger(process->GetPageTable().GetStackRegionStart());
  67. R_SUCCEED();
  68. case InfoType::StackRegionSize:
  69. *result = process->GetPageTable().GetStackRegionSize();
  70. R_SUCCEED();
  71. case InfoType::TotalMemorySize:
  72. *result = process->GetTotalUserPhysicalMemorySize();
  73. R_SUCCEED();
  74. case InfoType::UsedMemorySize:
  75. *result = process->GetUsedUserPhysicalMemorySize();
  76. R_SUCCEED();
  77. case InfoType::SystemResourceSizeTotal:
  78. *result = process->GetTotalSystemResourceSize();
  79. R_SUCCEED();
  80. case InfoType::SystemResourceSizeUsed:
  81. *result = process->GetUsedSystemResourceSize();
  82. R_SUCCEED();
  83. case InfoType::ProgramId:
  84. *result = process->GetProgramId();
  85. R_SUCCEED();
  86. case InfoType::UserExceptionContextAddress:
  87. *result = GetInteger(process->GetProcessLocalRegionAddress());
  88. R_SUCCEED();
  89. case InfoType::TotalNonSystemMemorySize:
  90. *result = process->GetTotalNonSystemUserPhysicalMemorySize();
  91. R_SUCCEED();
  92. case InfoType::UsedNonSystemMemorySize:
  93. *result = process->GetUsedNonSystemUserPhysicalMemorySize();
  94. R_SUCCEED();
  95. case InfoType::IsApplication:
  96. LOG_WARNING(Kernel_SVC, "(STUBBED) Assuming process is application");
  97. *result = process->IsApplication();
  98. R_SUCCEED();
  99. case InfoType::FreeThreadCount:
  100. if (KResourceLimit* resource_limit = process->GetResourceLimit();
  101. resource_limit != nullptr) {
  102. const auto current_value =
  103. resource_limit->GetCurrentValue(Svc::LimitableResource::ThreadCountMax);
  104. const auto limit_value =
  105. resource_limit->GetLimitValue(Svc::LimitableResource::ThreadCountMax);
  106. *result = limit_value - current_value;
  107. } else {
  108. *result = 0;
  109. }
  110. R_SUCCEED();
  111. default:
  112. break;
  113. }
  114. LOG_ERROR(Kernel_SVC, "Unimplemented svcGetInfo id=0x{:016X}", info_id);
  115. R_THROW(ResultInvalidEnumValue);
  116. }
  117. case InfoType::DebuggerAttached:
  118. *result = 0;
  119. R_SUCCEED();
  120. case InfoType::ResourceLimit: {
  121. R_UNLESS(handle == 0, ResultInvalidHandle);
  122. R_UNLESS(info_sub_id == 0, ResultInvalidCombination);
  123. KProcess* const current_process = GetCurrentProcessPointer(system.Kernel());
  124. KHandleTable& handle_table = current_process->GetHandleTable();
  125. const auto resource_limit = current_process->GetResourceLimit();
  126. if (!resource_limit) {
  127. *result = Svc::InvalidHandle;
  128. // Yes, the kernel considers this a successful operation.
  129. R_SUCCEED();
  130. }
  131. Handle resource_handle{};
  132. R_TRY(handle_table.Add(std::addressof(resource_handle), resource_limit));
  133. *result = resource_handle;
  134. R_SUCCEED();
  135. }
  136. case InfoType::RandomEntropy:
  137. R_UNLESS(handle == 0, ResultInvalidHandle);
  138. R_UNLESS(info_sub_id < 4, ResultInvalidCombination);
  139. *result = GetCurrentProcess(system.Kernel()).GetRandomEntropy(info_sub_id);
  140. R_SUCCEED();
  141. case InfoType::InitialProcessIdRange:
  142. LOG_WARNING(Kernel_SVC,
  143. "(STUBBED) Attempted to query privileged process id bounds, returned 0");
  144. *result = 0;
  145. R_SUCCEED();
  146. case InfoType::ThreadTickCount: {
  147. constexpr u64 num_cpus = 4;
  148. if (info_sub_id != 0xFFFFFFFFFFFFFFFF && info_sub_id >= num_cpus) {
  149. LOG_ERROR(Kernel_SVC, "Core count is out of range, expected {} but got {}", num_cpus,
  150. info_sub_id);
  151. R_THROW(ResultInvalidCombination);
  152. }
  153. KScopedAutoObject thread = GetCurrentProcess(system.Kernel())
  154. .GetHandleTable()
  155. .GetObject<KThread>(static_cast<Handle>(handle));
  156. if (thread.IsNull()) {
  157. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}",
  158. static_cast<Handle>(handle));
  159. R_THROW(ResultInvalidHandle);
  160. }
  161. const auto& core_timing = system.CoreTiming();
  162. const auto& scheduler = *system.Kernel().CurrentScheduler();
  163. const auto* const current_thread = GetCurrentThreadPointer(system.Kernel());
  164. const bool same_thread = current_thread == thread.GetPointerUnsafe();
  165. const u64 prev_ctx_ticks = scheduler.GetLastContextSwitchTime();
  166. u64 out_ticks = 0;
  167. if (same_thread && info_sub_id == 0xFFFFFFFFFFFFFFFF) {
  168. const u64 thread_ticks = current_thread->GetCpuTime();
  169. out_ticks = thread_ticks + (core_timing.GetClockTicks() - prev_ctx_ticks);
  170. } else if (same_thread && info_sub_id == system.Kernel().CurrentPhysicalCoreIndex()) {
  171. out_ticks = core_timing.GetClockTicks() - prev_ctx_ticks;
  172. }
  173. *result = out_ticks;
  174. R_SUCCEED();
  175. }
  176. case InfoType::IdleTickCount: {
  177. // Verify the input handle is invalid.
  178. R_UNLESS(handle == InvalidHandle, ResultInvalidHandle);
  179. // Verify the requested core is valid.
  180. const bool core_valid =
  181. (info_sub_id == 0xFFFFFFFFFFFFFFFF) ||
  182. (info_sub_id == static_cast<u64>(system.Kernel().CurrentPhysicalCoreIndex()));
  183. R_UNLESS(core_valid, ResultInvalidCombination);
  184. // Get the idle tick count.
  185. *result = system.Kernel().CurrentScheduler()->GetIdleThread()->GetCpuTime();
  186. R_SUCCEED();
  187. }
  188. case InfoType::MesosphereCurrentProcess: {
  189. // Verify the input handle is invalid.
  190. R_UNLESS(handle == InvalidHandle, ResultInvalidHandle);
  191. // Verify the sub-type is valid.
  192. R_UNLESS(info_sub_id == 0, ResultInvalidCombination);
  193. // Get the handle table.
  194. KProcess* current_process = GetCurrentProcessPointer(system.Kernel());
  195. KHandleTable& handle_table = current_process->GetHandleTable();
  196. // Get a new handle for the current process.
  197. Handle tmp;
  198. R_TRY(handle_table.Add(std::addressof(tmp), current_process));
  199. // Set the output.
  200. *result = tmp;
  201. // We succeeded.
  202. R_SUCCEED();
  203. }
  204. default:
  205. LOG_ERROR(Kernel_SVC, "Unimplemented svcGetInfo id=0x{:016X}", info_id);
  206. R_THROW(ResultInvalidEnumValue);
  207. }
  208. }
  209. Result GetSystemInfo(Core::System& system, uint64_t* out, SystemInfoType info_type, Handle handle,
  210. uint64_t info_subtype) {
  211. UNIMPLEMENTED();
  212. R_THROW(ResultNotImplemented);
  213. }
  214. Result GetInfo64(Core::System& system, uint64_t* out, InfoType info_type, Handle handle,
  215. uint64_t info_subtype) {
  216. R_RETURN(GetInfo(system, out, info_type, handle, info_subtype));
  217. }
  218. Result GetSystemInfo64(Core::System& system, uint64_t* out, SystemInfoType info_type, Handle handle,
  219. uint64_t info_subtype) {
  220. R_RETURN(GetSystemInfo(system, out, info_type, handle, info_subtype));
  221. }
  222. Result GetInfo64From32(Core::System& system, uint64_t* out, InfoType info_type, Handle handle,
  223. uint64_t info_subtype) {
  224. R_RETURN(GetInfo(system, out, info_type, handle, info_subtype));
  225. }
  226. Result GetSystemInfo64From32(Core::System& system, uint64_t* out, SystemInfoType info_type,
  227. Handle handle, uint64_t info_subtype) {
  228. R_RETURN(GetSystemInfo(system, out, info_type, handle, info_subtype));
  229. }
  230. } // namespace Kernel::Svc