svc_info.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. if (info_sub_id != 0) {
  37. LOG_ERROR(Kernel_SVC, "Info sub id is non zero! info_id={}, info_sub_id={}", info_id,
  38. info_sub_id);
  39. return ResultInvalidEnumValue;
  40. }
  41. const auto& handle_table = GetCurrentProcess(system.Kernel()).GetHandleTable();
  42. KScopedAutoObject process = handle_table.GetObject<KProcess>(handle);
  43. if (process.IsNull()) {
  44. LOG_ERROR(Kernel_SVC, "Process is not valid! info_id={}, info_sub_id={}, handle={:08X}",
  45. info_id, info_sub_id, handle);
  46. return ResultInvalidHandle;
  47. }
  48. switch (info_id_type) {
  49. case InfoType::CoreMask:
  50. *result = process->GetCoreMask();
  51. return ResultSuccess;
  52. case InfoType::PriorityMask:
  53. *result = process->GetPriorityMask();
  54. return ResultSuccess;
  55. case InfoType::AliasRegionAddress:
  56. *result = process->PageTable().GetAliasRegionStart();
  57. return ResultSuccess;
  58. case InfoType::AliasRegionSize:
  59. *result = process->PageTable().GetAliasRegionSize();
  60. return ResultSuccess;
  61. case InfoType::HeapRegionAddress:
  62. *result = process->PageTable().GetHeapRegionStart();
  63. return ResultSuccess;
  64. case InfoType::HeapRegionSize:
  65. *result = process->PageTable().GetHeapRegionSize();
  66. return ResultSuccess;
  67. case InfoType::AslrRegionAddress:
  68. *result = process->PageTable().GetAliasCodeRegionStart();
  69. return ResultSuccess;
  70. case InfoType::AslrRegionSize:
  71. *result = process->PageTable().GetAliasCodeRegionSize();
  72. return ResultSuccess;
  73. case InfoType::StackRegionAddress:
  74. *result = process->PageTable().GetStackRegionStart();
  75. return ResultSuccess;
  76. case InfoType::StackRegionSize:
  77. *result = process->PageTable().GetStackRegionSize();
  78. return ResultSuccess;
  79. case InfoType::TotalMemorySize:
  80. *result = process->GetTotalPhysicalMemoryAvailable();
  81. return ResultSuccess;
  82. case InfoType::UsedMemorySize:
  83. *result = process->GetTotalPhysicalMemoryUsed();
  84. return ResultSuccess;
  85. case InfoType::SystemResourceSizeTotal:
  86. *result = process->GetSystemResourceSize();
  87. return ResultSuccess;
  88. case InfoType::SystemResourceSizeUsed:
  89. LOG_WARNING(Kernel_SVC, "(STUBBED) Attempted to query system resource usage");
  90. *result = process->GetSystemResourceUsage();
  91. return ResultSuccess;
  92. case InfoType::ProgramId:
  93. *result = process->GetProgramID();
  94. return ResultSuccess;
  95. case InfoType::UserExceptionContextAddress:
  96. *result = process->GetProcessLocalRegionAddress();
  97. return ResultSuccess;
  98. case InfoType::TotalNonSystemMemorySize:
  99. *result = process->GetTotalPhysicalMemoryAvailableWithoutSystemResource();
  100. return ResultSuccess;
  101. case InfoType::UsedNonSystemMemorySize:
  102. *result = process->GetTotalPhysicalMemoryUsedWithoutSystemResource();
  103. return ResultSuccess;
  104. case InfoType::FreeThreadCount:
  105. *result = process->GetFreeThreadCount();
  106. return ResultSuccess;
  107. default:
  108. break;
  109. }
  110. LOG_ERROR(Kernel_SVC, "Unimplemented svcGetInfo id=0x{:016X}", info_id);
  111. return ResultInvalidEnumValue;
  112. }
  113. case InfoType::DebuggerAttached:
  114. *result = 0;
  115. return ResultSuccess;
  116. case InfoType::ResourceLimit: {
  117. if (handle != 0) {
  118. LOG_ERROR(Kernel, "Handle is non zero! handle={:08X}", handle);
  119. return ResultInvalidHandle;
  120. }
  121. if (info_sub_id != 0) {
  122. LOG_ERROR(Kernel, "Info sub id is non zero! info_id={}, info_sub_id={}", info_id,
  123. info_sub_id);
  124. return ResultInvalidCombination;
  125. }
  126. KProcess* const current_process = GetCurrentProcessPointer(system.Kernel());
  127. KHandleTable& handle_table = current_process->GetHandleTable();
  128. const auto resource_limit = current_process->GetResourceLimit();
  129. if (!resource_limit) {
  130. *result = Svc::InvalidHandle;
  131. // Yes, the kernel considers this a successful operation.
  132. return ResultSuccess;
  133. }
  134. Handle resource_handle{};
  135. R_TRY(handle_table.Add(&resource_handle, resource_limit));
  136. *result = resource_handle;
  137. return ResultSuccess;
  138. }
  139. case InfoType::RandomEntropy:
  140. if (handle != 0) {
  141. LOG_ERROR(Kernel_SVC, "Process Handle is non zero, expected 0 result but got {:016X}",
  142. handle);
  143. return ResultInvalidHandle;
  144. }
  145. if (info_sub_id >= KProcess::RANDOM_ENTROPY_SIZE) {
  146. LOG_ERROR(Kernel_SVC, "Entropy size is out of range, expected {} but got {}",
  147. KProcess::RANDOM_ENTROPY_SIZE, info_sub_id);
  148. return ResultInvalidCombination;
  149. }
  150. *result = GetCurrentProcess(system.Kernel()).GetRandomEntropy(info_sub_id);
  151. return ResultSuccess;
  152. case InfoType::InitialProcessIdRange:
  153. LOG_WARNING(Kernel_SVC,
  154. "(STUBBED) Attempted to query privileged process id bounds, returned 0");
  155. *result = 0;
  156. return ResultSuccess;
  157. case InfoType::ThreadTickCount: {
  158. constexpr u64 num_cpus = 4;
  159. if (info_sub_id != 0xFFFFFFFFFFFFFFFF && info_sub_id >= num_cpus) {
  160. LOG_ERROR(Kernel_SVC, "Core count is out of range, expected {} but got {}", num_cpus,
  161. info_sub_id);
  162. return ResultInvalidCombination;
  163. }
  164. KScopedAutoObject thread = GetCurrentProcess(system.Kernel())
  165. .GetHandleTable()
  166. .GetObject<KThread>(static_cast<Handle>(handle));
  167. if (thread.IsNull()) {
  168. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}",
  169. static_cast<Handle>(handle));
  170. return ResultInvalidHandle;
  171. }
  172. const auto& core_timing = system.CoreTiming();
  173. const auto& scheduler = *system.Kernel().CurrentScheduler();
  174. const auto* const current_thread = GetCurrentThreadPointer(system.Kernel());
  175. const bool same_thread = current_thread == thread.GetPointerUnsafe();
  176. const u64 prev_ctx_ticks = scheduler.GetLastContextSwitchTime();
  177. u64 out_ticks = 0;
  178. if (same_thread && info_sub_id == 0xFFFFFFFFFFFFFFFF) {
  179. const u64 thread_ticks = current_thread->GetCpuTime();
  180. out_ticks = thread_ticks + (core_timing.GetCPUTicks() - prev_ctx_ticks);
  181. } else if (same_thread && info_sub_id == system.Kernel().CurrentPhysicalCoreIndex()) {
  182. out_ticks = core_timing.GetCPUTicks() - prev_ctx_ticks;
  183. }
  184. *result = out_ticks;
  185. return ResultSuccess;
  186. }
  187. case InfoType::IdleTickCount: {
  188. // Verify the input handle is invalid.
  189. R_UNLESS(handle == InvalidHandle, ResultInvalidHandle);
  190. // Verify the requested core is valid.
  191. const bool core_valid =
  192. (info_sub_id == 0xFFFFFFFFFFFFFFFF) ||
  193. (info_sub_id == static_cast<u64>(system.Kernel().CurrentPhysicalCoreIndex()));
  194. R_UNLESS(core_valid, ResultInvalidCombination);
  195. // Get the idle tick count.
  196. *result = system.Kernel().CurrentScheduler()->GetIdleThread()->GetCpuTime();
  197. return ResultSuccess;
  198. }
  199. case InfoType::MesosphereCurrentProcess: {
  200. // Verify the input handle is invalid.
  201. R_UNLESS(handle == InvalidHandle, ResultInvalidHandle);
  202. // Verify the sub-type is valid.
  203. R_UNLESS(info_sub_id == 0, ResultInvalidCombination);
  204. // Get the handle table.
  205. KProcess* current_process = GetCurrentProcessPointer(system.Kernel());
  206. KHandleTable& handle_table = current_process->GetHandleTable();
  207. // Get a new handle for the current process.
  208. Handle tmp;
  209. R_TRY(handle_table.Add(&tmp, current_process));
  210. // Set the output.
  211. *result = tmp;
  212. // We succeeded.
  213. return ResultSuccess;
  214. }
  215. default:
  216. LOG_ERROR(Kernel_SVC, "Unimplemented svcGetInfo id=0x{:016X}", info_id);
  217. return ResultInvalidEnumValue;
  218. }
  219. }
  220. Result GetSystemInfo(Core::System& system, uint64_t* out, SystemInfoType info_type, Handle handle,
  221. uint64_t info_subtype) {
  222. UNIMPLEMENTED();
  223. R_THROW(ResultNotImplemented);
  224. }
  225. Result GetInfo64(Core::System& system, uint64_t* out, InfoType info_type, Handle handle,
  226. uint64_t info_subtype) {
  227. R_RETURN(GetInfo(system, out, info_type, handle, info_subtype));
  228. }
  229. Result GetSystemInfo64(Core::System& system, uint64_t* out, SystemInfoType info_type, Handle handle,
  230. uint64_t info_subtype) {
  231. R_RETURN(GetSystemInfo(system, out, info_type, handle, info_subtype));
  232. }
  233. Result GetInfo64From32(Core::System& system, uint64_t* out, InfoType info_type, Handle handle,
  234. uint64_t info_subtype) {
  235. R_RETURN(GetInfo(system, out, info_type, handle, info_subtype));
  236. }
  237. Result GetSystemInfo64From32(Core::System& system, uint64_t* out, SystemInfoType info_type,
  238. Handle handle, uint64_t info_subtype) {
  239. R_RETURN(GetSystemInfo(system, out, info_type, handle, info_subtype));
  240. }
  241. } // namespace Kernel::Svc