svc.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cinttypes>
  6. #include <iterator>
  7. #include <mutex>
  8. #include <vector>
  9. #include "common/alignment.h"
  10. #include "common/assert.h"
  11. #include "common/logging/log.h"
  12. #include "common/microprofile.h"
  13. #include "common/string_util.h"
  14. #include "core/arm/exclusive_monitor.h"
  15. #include "core/core.h"
  16. #include "core/core_cpu.h"
  17. #include "core/core_timing.h"
  18. #include "core/hle/kernel/address_arbiter.h"
  19. #include "core/hle/kernel/client_port.h"
  20. #include "core/hle/kernel/client_session.h"
  21. #include "core/hle/kernel/event.h"
  22. #include "core/hle/kernel/handle_table.h"
  23. #include "core/hle/kernel/kernel.h"
  24. #include "core/hle/kernel/mutex.h"
  25. #include "core/hle/kernel/process.h"
  26. #include "core/hle/kernel/resource_limit.h"
  27. #include "core/hle/kernel/scheduler.h"
  28. #include "core/hle/kernel/shared_memory.h"
  29. #include "core/hle/kernel/svc.h"
  30. #include "core/hle/kernel/svc_wrap.h"
  31. #include "core/hle/kernel/thread.h"
  32. #include "core/hle/lock.h"
  33. #include "core/hle/result.h"
  34. #include "core/hle/service/service.h"
  35. namespace Kernel {
  36. namespace {
  37. // Checks if address + size is greater than the given address
  38. // This can return false if the size causes an overflow of a 64-bit type
  39. // or if the given size is zero.
  40. constexpr bool IsValidAddressRange(VAddr address, u64 size) {
  41. return address + size > address;
  42. }
  43. // Checks if a given address range lies within a larger address range.
  44. constexpr bool IsInsideAddressRange(VAddr address, u64 size, VAddr address_range_begin,
  45. VAddr address_range_end) {
  46. const VAddr end_address = address + size - 1;
  47. return address_range_begin <= address && end_address <= address_range_end - 1;
  48. }
  49. bool IsInsideAddressSpace(const VMManager& vm, VAddr address, u64 size) {
  50. return IsInsideAddressRange(address, size, vm.GetAddressSpaceBaseAddress(),
  51. vm.GetAddressSpaceEndAddress());
  52. }
  53. bool IsInsideNewMapRegion(const VMManager& vm, VAddr address, u64 size) {
  54. return IsInsideAddressRange(address, size, vm.GetNewMapRegionBaseAddress(),
  55. vm.GetNewMapRegionEndAddress());
  56. }
  57. // Helper function that performs the common sanity checks for svcMapMemory
  58. // and svcUnmapMemory. This is doable, as both functions perform their sanitizing
  59. // in the same order.
  60. ResultCode MapUnmapMemorySanityChecks(const VMManager& vm_manager, VAddr dst_addr, VAddr src_addr,
  61. u64 size) {
  62. if (!Common::Is4KBAligned(dst_addr) || !Common::Is4KBAligned(src_addr)) {
  63. return ERR_INVALID_ADDRESS;
  64. }
  65. if (size == 0 || !Common::Is4KBAligned(size)) {
  66. return ERR_INVALID_SIZE;
  67. }
  68. if (!IsValidAddressRange(dst_addr, size)) {
  69. return ERR_INVALID_ADDRESS_STATE;
  70. }
  71. if (!IsValidAddressRange(src_addr, size)) {
  72. return ERR_INVALID_ADDRESS_STATE;
  73. }
  74. if (!IsInsideAddressSpace(vm_manager, src_addr, size)) {
  75. return ERR_INVALID_ADDRESS_STATE;
  76. }
  77. if (!IsInsideNewMapRegion(vm_manager, dst_addr, size)) {
  78. return ERR_INVALID_MEMORY_RANGE;
  79. }
  80. const VAddr dst_end_address = dst_addr + size;
  81. if (dst_end_address > vm_manager.GetHeapRegionBaseAddress() &&
  82. vm_manager.GetHeapRegionEndAddress() > dst_addr) {
  83. return ERR_INVALID_MEMORY_RANGE;
  84. }
  85. if (dst_end_address > vm_manager.GetMapRegionBaseAddress() &&
  86. vm_manager.GetMapRegionEndAddress() > dst_addr) {
  87. return ERR_INVALID_MEMORY_RANGE;
  88. }
  89. return RESULT_SUCCESS;
  90. }
  91. } // Anonymous namespace
  92. /// Set the process heap to a given Size. It can both extend and shrink the heap.
  93. static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) {
  94. LOG_TRACE(Kernel_SVC, "called, heap_size=0x{:X}", heap_size);
  95. // Size must be a multiple of 0x200000 (2MB) and be equal to or less than 4GB.
  96. if ((heap_size & 0xFFFFFFFE001FFFFF) != 0) {
  97. return ERR_INVALID_SIZE;
  98. }
  99. auto& process = *Core::CurrentProcess();
  100. const VAddr heap_base = process.VMManager().GetHeapRegionBaseAddress();
  101. CASCADE_RESULT(*heap_addr,
  102. process.HeapAllocate(heap_base, heap_size, VMAPermission::ReadWrite));
  103. return RESULT_SUCCESS;
  104. }
  105. static ResultCode SetMemoryAttribute(VAddr addr, u64 size, u32 state0, u32 state1) {
  106. LOG_WARNING(Kernel_SVC,
  107. "(STUBBED) called, addr=0x{:X}, size=0x{:X}, state0=0x{:X}, state1=0x{:X}", addr,
  108. size, state0, state1);
  109. return RESULT_SUCCESS;
  110. }
  111. /// Maps a memory range into a different range.
  112. static ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
  113. LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr,
  114. src_addr, size);
  115. auto* const current_process = Core::CurrentProcess();
  116. const auto& vm_manager = current_process->VMManager();
  117. const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size);
  118. if (result != RESULT_SUCCESS) {
  119. return result;
  120. }
  121. return current_process->MirrorMemory(dst_addr, src_addr, size);
  122. }
  123. /// Unmaps a region that was previously mapped with svcMapMemory
  124. static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
  125. LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr,
  126. src_addr, size);
  127. auto* const current_process = Core::CurrentProcess();
  128. const auto& vm_manager = current_process->VMManager();
  129. const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size);
  130. if (result != RESULT_SUCCESS) {
  131. return result;
  132. }
  133. return current_process->UnmapMemory(dst_addr, src_addr, size);
  134. }
  135. /// Connect to an OS service given the port name, returns the handle to the port to out
  136. static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address) {
  137. if (!Memory::IsValidVirtualAddress(port_name_address)) {
  138. return ERR_NOT_FOUND;
  139. }
  140. static constexpr std::size_t PortNameMaxLength = 11;
  141. // Read 1 char beyond the max allowed port name to detect names that are too long.
  142. std::string port_name = Memory::ReadCString(port_name_address, PortNameMaxLength + 1);
  143. if (port_name.size() > PortNameMaxLength) {
  144. return ERR_PORT_NAME_TOO_LONG;
  145. }
  146. LOG_TRACE(Kernel_SVC, "called port_name={}", port_name);
  147. auto& kernel = Core::System::GetInstance().Kernel();
  148. auto it = kernel.FindNamedPort(port_name);
  149. if (!kernel.IsValidNamedPort(it)) {
  150. LOG_WARNING(Kernel_SVC, "tried to connect to unknown port: {}", port_name);
  151. return ERR_NOT_FOUND;
  152. }
  153. auto client_port = it->second;
  154. SharedPtr<ClientSession> client_session;
  155. CASCADE_RESULT(client_session, client_port->Connect());
  156. // Return the client session
  157. auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  158. CASCADE_RESULT(*out_handle, handle_table.Create(client_session));
  159. return RESULT_SUCCESS;
  160. }
  161. /// Makes a blocking IPC call to an OS service.
  162. static ResultCode SendSyncRequest(Handle handle) {
  163. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  164. SharedPtr<ClientSession> session = handle_table.Get<ClientSession>(handle);
  165. if (!session) {
  166. LOG_ERROR(Kernel_SVC, "called with invalid handle=0x{:08X}", handle);
  167. return ERR_INVALID_HANDLE;
  168. }
  169. LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({})", handle, session->GetName());
  170. Core::System::GetInstance().PrepareReschedule();
  171. // TODO(Subv): svcSendSyncRequest should put the caller thread to sleep while the server
  172. // responds and cause a reschedule.
  173. return session->SendSyncRequest(GetCurrentThread());
  174. }
  175. /// Get the ID for the specified thread.
  176. static ResultCode GetThreadId(u32* thread_id, Handle thread_handle) {
  177. LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
  178. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  179. const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  180. if (!thread) {
  181. return ERR_INVALID_HANDLE;
  182. }
  183. *thread_id = thread->GetThreadID();
  184. return RESULT_SUCCESS;
  185. }
  186. /// Get the ID of the specified process
  187. static ResultCode GetProcessId(u32* process_id, Handle process_handle) {
  188. LOG_TRACE(Kernel_SVC, "called process=0x{:08X}", process_handle);
  189. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  190. const SharedPtr<Process> process = handle_table.Get<Process>(process_handle);
  191. if (!process) {
  192. return ERR_INVALID_HANDLE;
  193. }
  194. *process_id = process->GetProcessID();
  195. return RESULT_SUCCESS;
  196. }
  197. /// Default thread wakeup callback for WaitSynchronization
  198. static bool DefaultThreadWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thread> thread,
  199. SharedPtr<WaitObject> object, std::size_t index) {
  200. ASSERT(thread->GetStatus() == ThreadStatus::WaitSynchAny);
  201. if (reason == ThreadWakeupReason::Timeout) {
  202. thread->SetWaitSynchronizationResult(RESULT_TIMEOUT);
  203. return true;
  204. }
  205. ASSERT(reason == ThreadWakeupReason::Signal);
  206. thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
  207. thread->SetWaitSynchronizationOutput(static_cast<u32>(index));
  208. return true;
  209. };
  210. /// Wait for the given handles to synchronize, timeout after the specified nanoseconds
  211. static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 handle_count,
  212. s64 nano_seconds) {
  213. LOG_TRACE(Kernel_SVC, "called handles_address=0x{:X}, handle_count={}, nano_seconds={}",
  214. handles_address, handle_count, nano_seconds);
  215. if (!Memory::IsValidVirtualAddress(handles_address))
  216. return ERR_INVALID_POINTER;
  217. static constexpr u64 MaxHandles = 0x40;
  218. if (handle_count > MaxHandles)
  219. return ResultCode(ErrorModule::Kernel, ErrCodes::TooLarge);
  220. auto* const thread = GetCurrentThread();
  221. using ObjectPtr = Thread::ThreadWaitObjects::value_type;
  222. Thread::ThreadWaitObjects objects(handle_count);
  223. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  224. for (u64 i = 0; i < handle_count; ++i) {
  225. const Handle handle = Memory::Read32(handles_address + i * sizeof(Handle));
  226. const auto object = handle_table.Get<WaitObject>(handle);
  227. if (object == nullptr) {
  228. return ERR_INVALID_HANDLE;
  229. }
  230. objects[i] = object;
  231. }
  232. // Find the first object that is acquirable in the provided list of objects
  233. auto itr = std::find_if(objects.begin(), objects.end(), [thread](const ObjectPtr& object) {
  234. return !object->ShouldWait(thread);
  235. });
  236. if (itr != objects.end()) {
  237. // We found a ready object, acquire it and set the result value
  238. WaitObject* object = itr->get();
  239. object->Acquire(thread);
  240. *index = static_cast<s32>(std::distance(objects.begin(), itr));
  241. return RESULT_SUCCESS;
  242. }
  243. // No objects were ready to be acquired, prepare to suspend the thread.
  244. // If a timeout value of 0 was provided, just return the Timeout error code instead of
  245. // suspending the thread.
  246. if (nano_seconds == 0)
  247. return RESULT_TIMEOUT;
  248. for (auto& object : objects)
  249. object->AddWaitingThread(thread);
  250. thread->SetWaitObjects(std::move(objects));
  251. thread->SetStatus(ThreadStatus::WaitSynchAny);
  252. // Create an event to wake the thread up after the specified nanosecond delay has passed
  253. thread->WakeAfterDelay(nano_seconds);
  254. thread->SetWakeupCallback(DefaultThreadWakeupCallback);
  255. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  256. return RESULT_TIMEOUT;
  257. }
  258. /// Resumes a thread waiting on WaitSynchronization
  259. static ResultCode CancelSynchronization(Handle thread_handle) {
  260. LOG_TRACE(Kernel_SVC, "called thread=0x{:X}", thread_handle);
  261. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  262. const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  263. if (!thread) {
  264. return ERR_INVALID_HANDLE;
  265. }
  266. ASSERT(thread->GetStatus() == ThreadStatus::WaitSynchAny);
  267. thread->SetWaitSynchronizationResult(
  268. ResultCode(ErrorModule::Kernel, ErrCodes::SynchronizationCanceled));
  269. thread->ResumeFromWait();
  270. return RESULT_SUCCESS;
  271. }
  272. /// Attempts to locks a mutex, creating it if it does not already exist
  273. static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr,
  274. Handle requesting_thread_handle) {
  275. LOG_TRACE(Kernel_SVC,
  276. "called holding_thread_handle=0x{:08X}, mutex_addr=0x{:X}, "
  277. "requesting_current_thread_handle=0x{:08X}",
  278. holding_thread_handle, mutex_addr, requesting_thread_handle);
  279. if (Memory::IsKernelVirtualAddress(mutex_addr)) {
  280. return ERR_INVALID_ADDRESS_STATE;
  281. }
  282. if (!Common::IsWordAligned(mutex_addr)) {
  283. return ERR_INVALID_ADDRESS;
  284. }
  285. auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  286. return Mutex::TryAcquire(handle_table, mutex_addr, holding_thread_handle,
  287. requesting_thread_handle);
  288. }
  289. /// Unlock a mutex
  290. static ResultCode ArbitrateUnlock(VAddr mutex_addr) {
  291. LOG_TRACE(Kernel_SVC, "called mutex_addr=0x{:X}", mutex_addr);
  292. if (Memory::IsKernelVirtualAddress(mutex_addr)) {
  293. return ERR_INVALID_ADDRESS_STATE;
  294. }
  295. if (!Common::IsWordAligned(mutex_addr)) {
  296. return ERR_INVALID_ADDRESS;
  297. }
  298. return Mutex::Release(mutex_addr);
  299. }
  300. enum class BreakType : u32 {
  301. Panic = 0,
  302. AssertionFailed = 1,
  303. PreNROLoad = 3,
  304. PostNROLoad = 4,
  305. PreNROUnload = 5,
  306. PostNROUnload = 6,
  307. };
  308. struct BreakReason {
  309. union {
  310. u32 raw;
  311. BitField<0, 30, BreakType> break_type;
  312. BitField<31, 1, u32> signal_debugger;
  313. };
  314. };
  315. /// Break program execution
  316. static void Break(u32 reason, u64 info1, u64 info2) {
  317. BreakReason break_reason{reason};
  318. bool has_dumped_buffer{};
  319. const auto handle_debug_buffer = [&](VAddr addr, u64 sz) {
  320. if (sz == 0 || addr == 0 || has_dumped_buffer) {
  321. return;
  322. }
  323. // This typically is an error code so we're going to assume this is the case
  324. if (sz == sizeof(u32)) {
  325. LOG_CRITICAL(Debug_Emulated, "debug_buffer_err_code={:X}", Memory::Read32(addr));
  326. } else {
  327. // We don't know what's in here so we'll hexdump it
  328. std::vector<u8> debug_buffer(sz);
  329. Memory::ReadBlock(addr, debug_buffer.data(), sz);
  330. std::string hexdump;
  331. for (std::size_t i = 0; i < debug_buffer.size(); i++) {
  332. hexdump += fmt::format("{:02X} ", debug_buffer[i]);
  333. if (i != 0 && i % 16 == 0) {
  334. hexdump += '\n';
  335. }
  336. }
  337. LOG_CRITICAL(Debug_Emulated, "debug_buffer=\n{}", hexdump);
  338. }
  339. has_dumped_buffer = true;
  340. };
  341. switch (break_reason.break_type) {
  342. case BreakType::Panic:
  343. LOG_CRITICAL(Debug_Emulated, "Signalling debugger, PANIC! info1=0x{:016X}, info2=0x{:016X}",
  344. info1, info2);
  345. handle_debug_buffer(info1, info2);
  346. break;
  347. case BreakType::AssertionFailed:
  348. LOG_CRITICAL(Debug_Emulated,
  349. "Signalling debugger, Assertion failed! info1=0x{:016X}, info2=0x{:016X}",
  350. info1, info2);
  351. handle_debug_buffer(info1, info2);
  352. break;
  353. case BreakType::PreNROLoad:
  354. LOG_WARNING(
  355. Debug_Emulated,
  356. "Signalling debugger, Attempting to load an NRO at 0x{:016X} with size 0x{:016X}",
  357. info1, info2);
  358. break;
  359. case BreakType::PostNROLoad:
  360. LOG_WARNING(Debug_Emulated,
  361. "Signalling debugger, Loaded an NRO at 0x{:016X} with size 0x{:016X}", info1,
  362. info2);
  363. break;
  364. case BreakType::PreNROUnload:
  365. LOG_WARNING(
  366. Debug_Emulated,
  367. "Signalling debugger, Attempting to unload an NRO at 0x{:016X} with size 0x{:016X}",
  368. info1, info2);
  369. break;
  370. case BreakType::PostNROUnload:
  371. LOG_WARNING(Debug_Emulated,
  372. "Signalling debugger, Unloaded an NRO at 0x{:016X} with size 0x{:016X}", info1,
  373. info2);
  374. break;
  375. default:
  376. LOG_WARNING(
  377. Debug_Emulated,
  378. "Signalling debugger, Unknown break reason {}, info1=0x{:016X}, info2=0x{:016X}",
  379. static_cast<u32>(break_reason.break_type.Value()), info1, info2);
  380. handle_debug_buffer(info1, info2);
  381. break;
  382. }
  383. if (!break_reason.signal_debugger) {
  384. LOG_CRITICAL(
  385. Debug_Emulated,
  386. "Emulated program broke execution! reason=0x{:016X}, info1=0x{:016X}, info2=0x{:016X}",
  387. reason, info1, info2);
  388. handle_debug_buffer(info1, info2);
  389. ASSERT(false);
  390. Core::CurrentProcess()->PrepareForTermination();
  391. // Kill the current thread
  392. GetCurrentThread()->Stop();
  393. Core::System::GetInstance().PrepareReschedule();
  394. }
  395. }
  396. /// Used to output a message on a debug hardware unit - does nothing on a retail unit
  397. static void OutputDebugString(VAddr address, u64 len) {
  398. if (len == 0) {
  399. return;
  400. }
  401. std::string str(len, '\0');
  402. Memory::ReadBlock(address, str.data(), str.size());
  403. LOG_DEBUG(Debug_Emulated, "{}", str);
  404. }
  405. /// Gets system/memory information for the current process
  406. static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) {
  407. LOG_TRACE(Kernel_SVC, "called info_id=0x{:X}, info_sub_id=0x{:X}, handle=0x{:08X}", info_id,
  408. info_sub_id, handle);
  409. enum class GetInfoType : u64 {
  410. // 1.0.0+
  411. AllowedCpuIdBitmask = 0,
  412. AllowedThreadPrioBitmask = 1,
  413. MapRegionBaseAddr = 2,
  414. MapRegionSize = 3,
  415. HeapRegionBaseAddr = 4,
  416. HeapRegionSize = 5,
  417. TotalMemoryUsage = 6,
  418. TotalHeapUsage = 7,
  419. IsCurrentProcessBeingDebugged = 8,
  420. ResourceHandleLimit = 9,
  421. IdleTickCount = 10,
  422. RandomEntropy = 11,
  423. PerformanceCounter = 0xF0000002,
  424. // 2.0.0+
  425. ASLRRegionBaseAddr = 12,
  426. ASLRRegionSize = 13,
  427. NewMapRegionBaseAddr = 14,
  428. NewMapRegionSize = 15,
  429. // 3.0.0+
  430. IsVirtualAddressMemoryEnabled = 16,
  431. PersonalMmHeapUsage = 17,
  432. TitleId = 18,
  433. // 4.0.0+
  434. PrivilegedProcessId = 19,
  435. // 5.0.0+
  436. UserExceptionContextAddr = 20,
  437. ThreadTickCount = 0xF0000002,
  438. };
  439. const auto* current_process = Core::CurrentProcess();
  440. const auto& vm_manager = current_process->VMManager();
  441. switch (static_cast<GetInfoType>(info_id)) {
  442. case GetInfoType::AllowedCpuIdBitmask:
  443. *result = current_process->GetAllowedProcessorMask();
  444. break;
  445. case GetInfoType::AllowedThreadPrioBitmask:
  446. *result = current_process->GetAllowedThreadPriorityMask();
  447. break;
  448. case GetInfoType::MapRegionBaseAddr:
  449. *result = vm_manager.GetMapRegionBaseAddress();
  450. break;
  451. case GetInfoType::MapRegionSize:
  452. *result = vm_manager.GetMapRegionSize();
  453. break;
  454. case GetInfoType::HeapRegionBaseAddr:
  455. *result = vm_manager.GetHeapRegionBaseAddress();
  456. break;
  457. case GetInfoType::HeapRegionSize:
  458. *result = vm_manager.GetHeapRegionSize();
  459. break;
  460. case GetInfoType::TotalMemoryUsage:
  461. *result = vm_manager.GetTotalMemoryUsage();
  462. break;
  463. case GetInfoType::TotalHeapUsage:
  464. *result = vm_manager.GetTotalHeapUsage();
  465. break;
  466. case GetInfoType::IsCurrentProcessBeingDebugged:
  467. *result = 0;
  468. break;
  469. case GetInfoType::RandomEntropy:
  470. *result = 0;
  471. break;
  472. case GetInfoType::ASLRRegionBaseAddr:
  473. *result = vm_manager.GetASLRRegionBaseAddress();
  474. break;
  475. case GetInfoType::ASLRRegionSize:
  476. *result = vm_manager.GetASLRRegionSize();
  477. break;
  478. case GetInfoType::NewMapRegionBaseAddr:
  479. *result = vm_manager.GetNewMapRegionBaseAddress();
  480. break;
  481. case GetInfoType::NewMapRegionSize:
  482. *result = vm_manager.GetNewMapRegionSize();
  483. break;
  484. case GetInfoType::IsVirtualAddressMemoryEnabled:
  485. *result = current_process->IsVirtualMemoryEnabled();
  486. break;
  487. case GetInfoType::TitleId:
  488. *result = current_process->GetTitleID();
  489. break;
  490. case GetInfoType::PrivilegedProcessId:
  491. LOG_WARNING(Kernel_SVC,
  492. "(STUBBED) Attempted to query privileged process id bounds, returned 0");
  493. *result = 0;
  494. break;
  495. case GetInfoType::UserExceptionContextAddr:
  496. LOG_WARNING(Kernel_SVC,
  497. "(STUBBED) Attempted to query user exception context address, returned 0");
  498. *result = 0;
  499. break;
  500. case GetInfoType::ThreadTickCount: {
  501. constexpr u64 num_cpus = 4;
  502. if (info_sub_id != 0xFFFFFFFFFFFFFFFF && info_sub_id >= num_cpus) {
  503. return ERR_INVALID_COMBINATION_KERNEL;
  504. }
  505. const auto thread =
  506. current_process->GetHandleTable().Get<Thread>(static_cast<Handle>(handle));
  507. if (!thread) {
  508. return ERR_INVALID_HANDLE;
  509. }
  510. const auto& system = Core::System::GetInstance();
  511. const auto& scheduler = system.CurrentScheduler();
  512. const auto* const current_thread = scheduler.GetCurrentThread();
  513. const bool same_thread = current_thread == thread;
  514. const u64 prev_ctx_ticks = scheduler.GetLastContextSwitchTicks();
  515. u64 out_ticks = 0;
  516. if (same_thread && info_sub_id == 0xFFFFFFFFFFFFFFFF) {
  517. const u64 thread_ticks = current_thread->GetTotalCPUTimeTicks();
  518. out_ticks = thread_ticks + (CoreTiming::GetTicks() - prev_ctx_ticks);
  519. } else if (same_thread && info_sub_id == system.CurrentCoreIndex()) {
  520. out_ticks = CoreTiming::GetTicks() - prev_ctx_ticks;
  521. }
  522. *result = out_ticks;
  523. break;
  524. }
  525. default:
  526. UNIMPLEMENTED();
  527. }
  528. return RESULT_SUCCESS;
  529. }
  530. /// Sets the thread activity
  531. static ResultCode SetThreadActivity(Handle handle, u32 unknown) {
  532. LOG_WARNING(Kernel_SVC, "(STUBBED) called, handle=0x{:08X}, unknown=0x{:08X}", handle, unknown);
  533. return RESULT_SUCCESS;
  534. }
  535. /// Gets the thread context
  536. static ResultCode GetThreadContext(VAddr thread_context, Handle handle) {
  537. LOG_DEBUG(Kernel_SVC, "called, context=0x{:08X}, thread=0x{:X}", thread_context, handle);
  538. const auto* current_process = Core::CurrentProcess();
  539. const SharedPtr<Thread> thread = current_process->GetHandleTable().Get<Thread>(handle);
  540. if (!thread) {
  541. return ERR_INVALID_HANDLE;
  542. }
  543. if (thread->GetOwnerProcess() != current_process) {
  544. return ERR_INVALID_HANDLE;
  545. }
  546. if (thread == GetCurrentThread()) {
  547. return ERR_ALREADY_REGISTERED;
  548. }
  549. Core::ARM_Interface::ThreadContext ctx = thread->GetContext();
  550. // Mask away mode bits, interrupt bits, IL bit, and other reserved bits.
  551. ctx.pstate &= 0xFF0FFE20;
  552. // If 64-bit, we can just write the context registers directly and we're good.
  553. // However, if 32-bit, we have to ensure some registers are zeroed out.
  554. if (!current_process->Is64BitProcess()) {
  555. std::fill(ctx.cpu_registers.begin() + 15, ctx.cpu_registers.end(), 0);
  556. std::fill(ctx.vector_registers.begin() + 16, ctx.vector_registers.end(), u128{});
  557. }
  558. Memory::WriteBlock(thread_context, &ctx, sizeof(ctx));
  559. return RESULT_SUCCESS;
  560. }
  561. /// Gets the priority for the specified thread
  562. static ResultCode GetThreadPriority(u32* priority, Handle handle) {
  563. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  564. const SharedPtr<Thread> thread = handle_table.Get<Thread>(handle);
  565. if (!thread) {
  566. return ERR_INVALID_HANDLE;
  567. }
  568. *priority = thread->GetPriority();
  569. return RESULT_SUCCESS;
  570. }
  571. /// Sets the priority for the specified thread
  572. static ResultCode SetThreadPriority(Handle handle, u32 priority) {
  573. if (priority > THREADPRIO_LOWEST) {
  574. return ERR_INVALID_THREAD_PRIORITY;
  575. }
  576. const auto* const current_process = Core::CurrentProcess();
  577. // Note: The kernel uses the current process's resource limit instead of
  578. // the one from the thread owner's resource limit.
  579. const ResourceLimit& resource_limit = current_process->GetResourceLimit();
  580. if (resource_limit.GetMaxResourceValue(ResourceType::Priority) > priority) {
  581. return ERR_INVALID_THREAD_PRIORITY;
  582. }
  583. SharedPtr<Thread> thread = current_process->GetHandleTable().Get<Thread>(handle);
  584. if (!thread) {
  585. return ERR_INVALID_HANDLE;
  586. }
  587. thread->SetPriority(priority);
  588. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  589. return RESULT_SUCCESS;
  590. }
  591. /// Get which CPU core is executing the current thread
  592. static u32 GetCurrentProcessorNumber() {
  593. LOG_TRACE(Kernel_SVC, "called");
  594. return GetCurrentThread()->GetProcessorID();
  595. }
  596. static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size,
  597. u32 permissions) {
  598. LOG_TRACE(Kernel_SVC,
  599. "called, shared_memory_handle=0x{:X}, addr=0x{:X}, size=0x{:X}, permissions=0x{:08X}",
  600. shared_memory_handle, addr, size, permissions);
  601. if (!Common::Is4KBAligned(addr)) {
  602. return ERR_INVALID_ADDRESS;
  603. }
  604. if (size == 0 || !Common::Is4KBAligned(size)) {
  605. return ERR_INVALID_SIZE;
  606. }
  607. if (!IsValidAddressRange(addr, size)) {
  608. return ERR_INVALID_ADDRESS_STATE;
  609. }
  610. const auto permissions_type = static_cast<MemoryPermission>(permissions);
  611. if (permissions_type != MemoryPermission::Read &&
  612. permissions_type != MemoryPermission::ReadWrite) {
  613. LOG_ERROR(Kernel_SVC, "Invalid permissions=0x{:08X}", permissions);
  614. return ERR_INVALID_MEMORY_PERMISSIONS;
  615. }
  616. auto* const current_process = Core::CurrentProcess();
  617. auto shared_memory = current_process->GetHandleTable().Get<SharedMemory>(shared_memory_handle);
  618. if (!shared_memory) {
  619. return ERR_INVALID_HANDLE;
  620. }
  621. const auto& vm_manager = current_process->VMManager();
  622. if (!vm_manager.IsWithinASLRRegion(addr, size)) {
  623. return ERR_INVALID_MEMORY_RANGE;
  624. }
  625. return shared_memory->Map(current_process, addr, permissions_type, MemoryPermission::DontCare);
  626. }
  627. static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size) {
  628. LOG_WARNING(Kernel_SVC, "called, shared_memory_handle=0x{:08X}, addr=0x{:X}, size=0x{:X}",
  629. shared_memory_handle, addr, size);
  630. if (!Common::Is4KBAligned(addr)) {
  631. return ERR_INVALID_ADDRESS;
  632. }
  633. if (size == 0 || !Common::Is4KBAligned(size)) {
  634. return ERR_INVALID_SIZE;
  635. }
  636. if (!IsValidAddressRange(addr, size)) {
  637. return ERR_INVALID_ADDRESS_STATE;
  638. }
  639. auto* const current_process = Core::CurrentProcess();
  640. auto shared_memory = current_process->GetHandleTable().Get<SharedMemory>(shared_memory_handle);
  641. if (!shared_memory) {
  642. return ERR_INVALID_HANDLE;
  643. }
  644. const auto& vm_manager = current_process->VMManager();
  645. if (!vm_manager.IsWithinASLRRegion(addr, size)) {
  646. return ERR_INVALID_MEMORY_RANGE;
  647. }
  648. return shared_memory->Unmap(current_process, addr);
  649. }
  650. /// Query process memory
  651. static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* /*page_info*/,
  652. Handle process_handle, u64 addr) {
  653. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  654. SharedPtr<Process> process = handle_table.Get<Process>(process_handle);
  655. if (!process) {
  656. return ERR_INVALID_HANDLE;
  657. }
  658. auto vma = process->VMManager().FindVMA(addr);
  659. memory_info->attributes = 0;
  660. if (vma == process->VMManager().vma_map.end()) {
  661. memory_info->base_address = 0;
  662. memory_info->permission = static_cast<u32>(VMAPermission::None);
  663. memory_info->size = 0;
  664. memory_info->type = static_cast<u32>(MemoryState::Unmapped);
  665. } else {
  666. memory_info->base_address = vma->second.base;
  667. memory_info->permission = static_cast<u32>(vma->second.permissions);
  668. memory_info->size = vma->second.size;
  669. memory_info->type = static_cast<u32>(vma->second.meminfo_state);
  670. }
  671. LOG_TRACE(Kernel_SVC, "called process=0x{:08X} addr={:X}", process_handle, addr);
  672. return RESULT_SUCCESS;
  673. }
  674. /// Query memory
  675. static ResultCode QueryMemory(MemoryInfo* memory_info, PageInfo* page_info, VAddr addr) {
  676. LOG_TRACE(Kernel_SVC, "called, addr={:X}", addr);
  677. return QueryProcessMemory(memory_info, page_info, CurrentProcess, addr);
  678. }
  679. /// Exits the current process
  680. static void ExitProcess() {
  681. auto* current_process = Core::CurrentProcess();
  682. LOG_INFO(Kernel_SVC, "Process {} exiting", current_process->GetProcessID());
  683. ASSERT_MSG(current_process->GetStatus() == ProcessStatus::Running,
  684. "Process has already exited");
  685. current_process->PrepareForTermination();
  686. // Kill the current thread
  687. GetCurrentThread()->Stop();
  688. Core::System::GetInstance().PrepareReschedule();
  689. }
  690. /// Creates a new thread
  691. static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, VAddr stack_top,
  692. u32 priority, s32 processor_id) {
  693. if (priority > THREADPRIO_LOWEST) {
  694. return ERR_INVALID_THREAD_PRIORITY;
  695. }
  696. auto* const current_process = Core::CurrentProcess();
  697. const ResourceLimit& resource_limit = current_process->GetResourceLimit();
  698. if (resource_limit.GetMaxResourceValue(ResourceType::Priority) > priority) {
  699. return ERR_INVALID_THREAD_PRIORITY;
  700. }
  701. if (processor_id == THREADPROCESSORID_DEFAULT) {
  702. // Set the target CPU to the one specified in the process' exheader.
  703. processor_id = current_process->GetDefaultProcessorID();
  704. ASSERT(processor_id != THREADPROCESSORID_DEFAULT);
  705. }
  706. switch (processor_id) {
  707. case THREADPROCESSORID_0:
  708. case THREADPROCESSORID_1:
  709. case THREADPROCESSORID_2:
  710. case THREADPROCESSORID_3:
  711. break;
  712. default:
  713. LOG_ERROR(Kernel_SVC, "Invalid thread processor ID: {}", processor_id);
  714. return ERR_INVALID_PROCESSOR_ID;
  715. }
  716. const std::string name = fmt::format("thread-{:X}", entry_point);
  717. auto& kernel = Core::System::GetInstance().Kernel();
  718. CASCADE_RESULT(SharedPtr<Thread> thread,
  719. Thread::Create(kernel, name, entry_point, priority, arg, processor_id, stack_top,
  720. *current_process));
  721. const auto new_guest_handle = current_process->GetHandleTable().Create(thread);
  722. if (new_guest_handle.Failed()) {
  723. return new_guest_handle.Code();
  724. }
  725. thread->SetGuestHandle(*new_guest_handle);
  726. *out_handle = *new_guest_handle;
  727. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  728. LOG_TRACE(Kernel_SVC,
  729. "called entrypoint=0x{:08X} ({}), arg=0x{:08X}, stacktop=0x{:08X}, "
  730. "threadpriority=0x{:08X}, processorid=0x{:08X} : created handle=0x{:08X}",
  731. entry_point, name, arg, stack_top, priority, processor_id, *out_handle);
  732. return RESULT_SUCCESS;
  733. }
  734. /// Starts the thread for the provided handle
  735. static ResultCode StartThread(Handle thread_handle) {
  736. LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
  737. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  738. const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  739. if (!thread) {
  740. return ERR_INVALID_HANDLE;
  741. }
  742. ASSERT(thread->GetStatus() == ThreadStatus::Dormant);
  743. thread->ResumeFromWait();
  744. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  745. return RESULT_SUCCESS;
  746. }
  747. /// Called when a thread exits
  748. static void ExitThread() {
  749. LOG_TRACE(Kernel_SVC, "called, pc=0x{:08X}", Core::CurrentArmInterface().GetPC());
  750. ExitCurrentThread();
  751. Core::System::GetInstance().PrepareReschedule();
  752. }
  753. /// Sleep the current thread
  754. static void SleepThread(s64 nanoseconds) {
  755. LOG_TRACE(Kernel_SVC, "called nanoseconds={}", nanoseconds);
  756. // Don't attempt to yield execution if there are no available threads to run,
  757. // this way we avoid a useless reschedule to the idle thread.
  758. if (nanoseconds == 0 && !Core::System::GetInstance().CurrentScheduler().HaveReadyThreads())
  759. return;
  760. // Sleep current thread and check for next thread to schedule
  761. WaitCurrentThread_Sleep();
  762. // Create an event to wake the thread up after the specified nanosecond delay has passed
  763. GetCurrentThread()->WakeAfterDelay(nanoseconds);
  764. Core::System::GetInstance().PrepareReschedule();
  765. }
  766. /// Wait process wide key atomic
  767. static ResultCode WaitProcessWideKeyAtomic(VAddr mutex_addr, VAddr condition_variable_addr,
  768. Handle thread_handle, s64 nano_seconds) {
  769. LOG_TRACE(
  770. Kernel_SVC,
  771. "called mutex_addr={:X}, condition_variable_addr={:X}, thread_handle=0x{:08X}, timeout={}",
  772. mutex_addr, condition_variable_addr, thread_handle, nano_seconds);
  773. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  774. SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  775. ASSERT(thread);
  776. CASCADE_CODE(Mutex::Release(mutex_addr));
  777. SharedPtr<Thread> current_thread = GetCurrentThread();
  778. current_thread->SetCondVarWaitAddress(condition_variable_addr);
  779. current_thread->SetMutexWaitAddress(mutex_addr);
  780. current_thread->SetWaitHandle(thread_handle);
  781. current_thread->SetStatus(ThreadStatus::WaitMutex);
  782. current_thread->InvalidateWakeupCallback();
  783. current_thread->WakeAfterDelay(nano_seconds);
  784. // Note: Deliberately don't attempt to inherit the lock owner's priority.
  785. Core::System::GetInstance().CpuCore(current_thread->GetProcessorID()).PrepareReschedule();
  786. return RESULT_SUCCESS;
  787. }
  788. /// Signal process wide key
  789. static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target) {
  790. LOG_TRACE(Kernel_SVC, "called, condition_variable_addr=0x{:X}, target=0x{:08X}",
  791. condition_variable_addr, target);
  792. const auto RetrieveWaitingThreads = [](std::size_t core_index,
  793. std::vector<SharedPtr<Thread>>& waiting_threads,
  794. VAddr condvar_addr) {
  795. const auto& scheduler = Core::System::GetInstance().Scheduler(core_index);
  796. const auto& thread_list = scheduler.GetThreadList();
  797. for (const auto& thread : thread_list) {
  798. if (thread->GetCondVarWaitAddress() == condvar_addr)
  799. waiting_threads.push_back(thread);
  800. }
  801. };
  802. // Retrieve a list of all threads that are waiting for this condition variable.
  803. std::vector<SharedPtr<Thread>> waiting_threads;
  804. RetrieveWaitingThreads(0, waiting_threads, condition_variable_addr);
  805. RetrieveWaitingThreads(1, waiting_threads, condition_variable_addr);
  806. RetrieveWaitingThreads(2, waiting_threads, condition_variable_addr);
  807. RetrieveWaitingThreads(3, waiting_threads, condition_variable_addr);
  808. // Sort them by priority, such that the highest priority ones come first.
  809. std::sort(waiting_threads.begin(), waiting_threads.end(),
  810. [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) {
  811. return lhs->GetPriority() < rhs->GetPriority();
  812. });
  813. // Only process up to 'target' threads, unless 'target' is -1, in which case process
  814. // them all.
  815. std::size_t last = waiting_threads.size();
  816. if (target != -1)
  817. last = target;
  818. // If there are no threads waiting on this condition variable, just exit
  819. if (last > waiting_threads.size())
  820. return RESULT_SUCCESS;
  821. for (std::size_t index = 0; index < last; ++index) {
  822. auto& thread = waiting_threads[index];
  823. ASSERT(thread->GetCondVarWaitAddress() == condition_variable_addr);
  824. std::size_t current_core = Core::System::GetInstance().CurrentCoreIndex();
  825. auto& monitor = Core::System::GetInstance().Monitor();
  826. // Atomically read the value of the mutex.
  827. u32 mutex_val = 0;
  828. do {
  829. monitor.SetExclusive(current_core, thread->GetMutexWaitAddress());
  830. // If the mutex is not yet acquired, acquire it.
  831. mutex_val = Memory::Read32(thread->GetMutexWaitAddress());
  832. if (mutex_val != 0) {
  833. monitor.ClearExclusive();
  834. break;
  835. }
  836. } while (!monitor.ExclusiveWrite32(current_core, thread->GetMutexWaitAddress(),
  837. thread->GetWaitHandle()));
  838. if (mutex_val == 0) {
  839. // We were able to acquire the mutex, resume this thread.
  840. ASSERT(thread->GetStatus() == ThreadStatus::WaitMutex);
  841. thread->ResumeFromWait();
  842. auto* const lock_owner = thread->GetLockOwner();
  843. if (lock_owner != nullptr) {
  844. lock_owner->RemoveMutexWaiter(thread);
  845. }
  846. thread->SetLockOwner(nullptr);
  847. thread->SetMutexWaitAddress(0);
  848. thread->SetCondVarWaitAddress(0);
  849. thread->SetWaitHandle(0);
  850. } else {
  851. // Atomically signal that the mutex now has a waiting thread.
  852. do {
  853. monitor.SetExclusive(current_core, thread->GetMutexWaitAddress());
  854. // Ensure that the mutex value is still what we expect.
  855. u32 value = Memory::Read32(thread->GetMutexWaitAddress());
  856. // TODO(Subv): When this happens, the kernel just clears the exclusive state and
  857. // retries the initial read for this thread.
  858. ASSERT_MSG(mutex_val == value, "Unhandled synchronization primitive case");
  859. } while (!monitor.ExclusiveWrite32(current_core, thread->GetMutexWaitAddress(),
  860. mutex_val | Mutex::MutexHasWaitersFlag));
  861. // The mutex is already owned by some other thread, make this thread wait on it.
  862. const Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask);
  863. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  864. auto owner = handle_table.Get<Thread>(owner_handle);
  865. ASSERT(owner);
  866. ASSERT(thread->GetStatus() == ThreadStatus::WaitMutex);
  867. thread->InvalidateWakeupCallback();
  868. owner->AddMutexWaiter(thread);
  869. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  870. }
  871. }
  872. return RESULT_SUCCESS;
  873. }
  874. // Wait for an address (via Address Arbiter)
  875. static ResultCode WaitForAddress(VAddr address, u32 type, s32 value, s64 timeout) {
  876. LOG_WARNING(Kernel_SVC, "called, address=0x{:X}, type=0x{:X}, value=0x{:X}, timeout={}",
  877. address, type, value, timeout);
  878. // If the passed address is a kernel virtual address, return invalid memory state.
  879. if (Memory::IsKernelVirtualAddress(address)) {
  880. return ERR_INVALID_ADDRESS_STATE;
  881. }
  882. // If the address is not properly aligned to 4 bytes, return invalid address.
  883. if (address % sizeof(u32) != 0) {
  884. return ERR_INVALID_ADDRESS;
  885. }
  886. switch (static_cast<AddressArbiter::ArbitrationType>(type)) {
  887. case AddressArbiter::ArbitrationType::WaitIfLessThan:
  888. return AddressArbiter::WaitForAddressIfLessThan(address, value, timeout, false);
  889. case AddressArbiter::ArbitrationType::DecrementAndWaitIfLessThan:
  890. return AddressArbiter::WaitForAddressIfLessThan(address, value, timeout, true);
  891. case AddressArbiter::ArbitrationType::WaitIfEqual:
  892. return AddressArbiter::WaitForAddressIfEqual(address, value, timeout);
  893. default:
  894. return ERR_INVALID_ENUM_VALUE;
  895. }
  896. }
  897. // Signals to an address (via Address Arbiter)
  898. static ResultCode SignalToAddress(VAddr address, u32 type, s32 value, s32 num_to_wake) {
  899. LOG_WARNING(Kernel_SVC, "called, address=0x{:X}, type=0x{:X}, value=0x{:X}, num_to_wake=0x{:X}",
  900. address, type, value, num_to_wake);
  901. // If the passed address is a kernel virtual address, return invalid memory state.
  902. if (Memory::IsKernelVirtualAddress(address)) {
  903. return ERR_INVALID_ADDRESS_STATE;
  904. }
  905. // If the address is not properly aligned to 4 bytes, return invalid address.
  906. if (address % sizeof(u32) != 0) {
  907. return ERR_INVALID_ADDRESS;
  908. }
  909. switch (static_cast<AddressArbiter::SignalType>(type)) {
  910. case AddressArbiter::SignalType::Signal:
  911. return AddressArbiter::SignalToAddress(address, num_to_wake);
  912. case AddressArbiter::SignalType::IncrementAndSignalIfEqual:
  913. return AddressArbiter::IncrementAndSignalToAddressIfEqual(address, value, num_to_wake);
  914. case AddressArbiter::SignalType::ModifyByWaitingCountAndSignalIfEqual:
  915. return AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(address, value,
  916. num_to_wake);
  917. default:
  918. return ERR_INVALID_ENUM_VALUE;
  919. }
  920. }
  921. /// This returns the total CPU ticks elapsed since the CPU was powered-on
  922. static u64 GetSystemTick() {
  923. const u64 result{CoreTiming::GetTicks()};
  924. // Advance time to defeat dumb games that busy-wait for the frame to end.
  925. CoreTiming::AddTicks(400);
  926. return result;
  927. }
  928. /// Close a handle
  929. static ResultCode CloseHandle(Handle handle) {
  930. LOG_TRACE(Kernel_SVC, "Closing handle 0x{:08X}", handle);
  931. auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  932. return handle_table.Close(handle);
  933. }
  934. /// Reset an event
  935. static ResultCode ResetSignal(Handle handle) {
  936. LOG_WARNING(Kernel_SVC, "(STUBBED) called handle 0x{:08X}", handle);
  937. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  938. auto event = handle_table.Get<Event>(handle);
  939. ASSERT(event != nullptr);
  940. event->Clear();
  941. return RESULT_SUCCESS;
  942. }
  943. /// Creates a TransferMemory object
  944. static ResultCode CreateTransferMemory(Handle* handle, VAddr addr, u64 size, u32 permissions) {
  945. LOG_WARNING(Kernel_SVC, "(STUBBED) called addr=0x{:X}, size=0x{:X}, perms=0x{:08X}", addr, size,
  946. permissions);
  947. *handle = 0;
  948. return RESULT_SUCCESS;
  949. }
  950. static ResultCode GetThreadCoreMask(Handle thread_handle, u32* core, u64* mask) {
  951. LOG_TRACE(Kernel_SVC, "called, handle=0x{:08X}", thread_handle);
  952. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  953. const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  954. if (!thread) {
  955. return ERR_INVALID_HANDLE;
  956. }
  957. *core = thread->GetIdealCore();
  958. *mask = thread->GetAffinityMask();
  959. return RESULT_SUCCESS;
  960. }
  961. static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) {
  962. LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, mask=0x{:16X}, core=0x{:X}", thread_handle,
  963. mask, core);
  964. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  965. const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  966. if (!thread) {
  967. return ERR_INVALID_HANDLE;
  968. }
  969. if (core == static_cast<u32>(THREADPROCESSORID_DEFAULT)) {
  970. const u8 default_processor_id = thread->GetOwnerProcess()->GetDefaultProcessorID();
  971. ASSERT(default_processor_id != static_cast<u8>(THREADPROCESSORID_DEFAULT));
  972. // Set the target CPU to the one specified in the process' exheader.
  973. core = default_processor_id;
  974. mask = 1ULL << core;
  975. }
  976. if (mask == 0) {
  977. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidCombination);
  978. }
  979. /// This value is used to only change the affinity mask without changing the current ideal core.
  980. static constexpr u32 OnlyChangeMask = static_cast<u32>(-3);
  981. if (core == OnlyChangeMask) {
  982. core = thread->GetIdealCore();
  983. } else if (core >= Core::NUM_CPU_CORES && core != static_cast<u32>(-1)) {
  984. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidProcessorId);
  985. }
  986. // Error out if the input core isn't enabled in the input mask.
  987. if (core < Core::NUM_CPU_CORES && (mask & (1ull << core)) == 0) {
  988. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidCombination);
  989. }
  990. thread->ChangeCore(core, mask);
  991. return RESULT_SUCCESS;
  992. }
  993. static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permissions,
  994. u32 remote_permissions) {
  995. LOG_TRACE(Kernel_SVC, "called, size=0x{:X}, localPerms=0x{:08X}, remotePerms=0x{:08X}", size,
  996. local_permissions, remote_permissions);
  997. // Size must be a multiple of 4KB and be less than or equal to
  998. // approx. 8 GB (actually (1GB - 512B) * 8)
  999. if (size == 0 || (size & 0xFFFFFFFE00000FFF) != 0) {
  1000. return ERR_INVALID_SIZE;
  1001. }
  1002. const auto local_perms = static_cast<MemoryPermission>(local_permissions);
  1003. if (local_perms != MemoryPermission::Read && local_perms != MemoryPermission::ReadWrite) {
  1004. return ERR_INVALID_MEMORY_PERMISSIONS;
  1005. }
  1006. const auto remote_perms = static_cast<MemoryPermission>(remote_permissions);
  1007. if (remote_perms != MemoryPermission::Read && remote_perms != MemoryPermission::ReadWrite &&
  1008. remote_perms != MemoryPermission::DontCare) {
  1009. return ERR_INVALID_MEMORY_PERMISSIONS;
  1010. }
  1011. auto& kernel = Core::System::GetInstance().Kernel();
  1012. auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1013. auto shared_mem_handle =
  1014. SharedMemory::Create(kernel, handle_table.Get<Process>(KernelHandle::CurrentProcess), size,
  1015. local_perms, remote_perms);
  1016. CASCADE_RESULT(*handle, handle_table.Create(shared_mem_handle));
  1017. return RESULT_SUCCESS;
  1018. }
  1019. static ResultCode ClearEvent(Handle handle) {
  1020. LOG_TRACE(Kernel_SVC, "called, event=0x{:08X}", handle);
  1021. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1022. SharedPtr<Event> evt = handle_table.Get<Event>(handle);
  1023. if (evt == nullptr) {
  1024. return ERR_INVALID_HANDLE;
  1025. }
  1026. evt->Clear();
  1027. return RESULT_SUCCESS;
  1028. }
  1029. static ResultCode GetProcessInfo(u64* out, Handle process_handle, u32 type) {
  1030. LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, type=0x{:X}", process_handle, type);
  1031. // This function currently only allows retrieving a process' status.
  1032. enum class InfoType {
  1033. Status,
  1034. };
  1035. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1036. const auto process = handle_table.Get<Process>(process_handle);
  1037. if (!process) {
  1038. return ERR_INVALID_HANDLE;
  1039. }
  1040. const auto info_type = static_cast<InfoType>(type);
  1041. if (info_type != InfoType::Status) {
  1042. return ERR_INVALID_ENUM_VALUE;
  1043. }
  1044. *out = static_cast<u64>(process->GetStatus());
  1045. return RESULT_SUCCESS;
  1046. }
  1047. namespace {
  1048. struct FunctionDef {
  1049. using Func = void();
  1050. u32 id;
  1051. Func* func;
  1052. const char* name;
  1053. };
  1054. } // namespace
  1055. static const FunctionDef SVC_Table[] = {
  1056. {0x00, nullptr, "Unknown"},
  1057. {0x01, SvcWrap<SetHeapSize>, "SetHeapSize"},
  1058. {0x02, nullptr, "SetMemoryPermission"},
  1059. {0x03, SvcWrap<SetMemoryAttribute>, "SetMemoryAttribute"},
  1060. {0x04, SvcWrap<MapMemory>, "MapMemory"},
  1061. {0x05, SvcWrap<UnmapMemory>, "UnmapMemory"},
  1062. {0x06, SvcWrap<QueryMemory>, "QueryMemory"},
  1063. {0x07, SvcWrap<ExitProcess>, "ExitProcess"},
  1064. {0x08, SvcWrap<CreateThread>, "CreateThread"},
  1065. {0x09, SvcWrap<StartThread>, "StartThread"},
  1066. {0x0A, SvcWrap<ExitThread>, "ExitThread"},
  1067. {0x0B, SvcWrap<SleepThread>, "SleepThread"},
  1068. {0x0C, SvcWrap<GetThreadPriority>, "GetThreadPriority"},
  1069. {0x0D, SvcWrap<SetThreadPriority>, "SetThreadPriority"},
  1070. {0x0E, SvcWrap<GetThreadCoreMask>, "GetThreadCoreMask"},
  1071. {0x0F, SvcWrap<SetThreadCoreMask>, "SetThreadCoreMask"},
  1072. {0x10, SvcWrap<GetCurrentProcessorNumber>, "GetCurrentProcessorNumber"},
  1073. {0x11, nullptr, "SignalEvent"},
  1074. {0x12, SvcWrap<ClearEvent>, "ClearEvent"},
  1075. {0x13, SvcWrap<MapSharedMemory>, "MapSharedMemory"},
  1076. {0x14, SvcWrap<UnmapSharedMemory>, "UnmapSharedMemory"},
  1077. {0x15, SvcWrap<CreateTransferMemory>, "CreateTransferMemory"},
  1078. {0x16, SvcWrap<CloseHandle>, "CloseHandle"},
  1079. {0x17, SvcWrap<ResetSignal>, "ResetSignal"},
  1080. {0x18, SvcWrap<WaitSynchronization>, "WaitSynchronization"},
  1081. {0x19, SvcWrap<CancelSynchronization>, "CancelSynchronization"},
  1082. {0x1A, SvcWrap<ArbitrateLock>, "ArbitrateLock"},
  1083. {0x1B, SvcWrap<ArbitrateUnlock>, "ArbitrateUnlock"},
  1084. {0x1C, SvcWrap<WaitProcessWideKeyAtomic>, "WaitProcessWideKeyAtomic"},
  1085. {0x1D, SvcWrap<SignalProcessWideKey>, "SignalProcessWideKey"},
  1086. {0x1E, SvcWrap<GetSystemTick>, "GetSystemTick"},
  1087. {0x1F, SvcWrap<ConnectToNamedPort>, "ConnectToNamedPort"},
  1088. {0x20, nullptr, "SendSyncRequestLight"},
  1089. {0x21, SvcWrap<SendSyncRequest>, "SendSyncRequest"},
  1090. {0x22, nullptr, "SendSyncRequestWithUserBuffer"},
  1091. {0x23, nullptr, "SendAsyncRequestWithUserBuffer"},
  1092. {0x24, SvcWrap<GetProcessId>, "GetProcessId"},
  1093. {0x25, SvcWrap<GetThreadId>, "GetThreadId"},
  1094. {0x26, SvcWrap<Break>, "Break"},
  1095. {0x27, SvcWrap<OutputDebugString>, "OutputDebugString"},
  1096. {0x28, nullptr, "ReturnFromException"},
  1097. {0x29, SvcWrap<GetInfo>, "GetInfo"},
  1098. {0x2A, nullptr, "FlushEntireDataCache"},
  1099. {0x2B, nullptr, "FlushDataCache"},
  1100. {0x2C, nullptr, "MapPhysicalMemory"},
  1101. {0x2D, nullptr, "UnmapPhysicalMemory"},
  1102. {0x2E, nullptr, "GetFutureThreadInfo"},
  1103. {0x2F, nullptr, "GetLastThreadInfo"},
  1104. {0x30, nullptr, "GetResourceLimitLimitValue"},
  1105. {0x31, nullptr, "GetResourceLimitCurrentValue"},
  1106. {0x32, SvcWrap<SetThreadActivity>, "SetThreadActivity"},
  1107. {0x33, SvcWrap<GetThreadContext>, "GetThreadContext"},
  1108. {0x34, SvcWrap<WaitForAddress>, "WaitForAddress"},
  1109. {0x35, SvcWrap<SignalToAddress>, "SignalToAddress"},
  1110. {0x36, nullptr, "Unknown"},
  1111. {0x37, nullptr, "Unknown"},
  1112. {0x38, nullptr, "Unknown"},
  1113. {0x39, nullptr, "Unknown"},
  1114. {0x3A, nullptr, "Unknown"},
  1115. {0x3B, nullptr, "Unknown"},
  1116. {0x3C, nullptr, "DumpInfo"},
  1117. {0x3D, nullptr, "DumpInfoNew"},
  1118. {0x3E, nullptr, "Unknown"},
  1119. {0x3F, nullptr, "Unknown"},
  1120. {0x40, nullptr, "CreateSession"},
  1121. {0x41, nullptr, "AcceptSession"},
  1122. {0x42, nullptr, "ReplyAndReceiveLight"},
  1123. {0x43, nullptr, "ReplyAndReceive"},
  1124. {0x44, nullptr, "ReplyAndReceiveWithUserBuffer"},
  1125. {0x45, nullptr, "CreateEvent"},
  1126. {0x46, nullptr, "Unknown"},
  1127. {0x47, nullptr, "Unknown"},
  1128. {0x48, nullptr, "MapPhysicalMemoryUnsafe"},
  1129. {0x49, nullptr, "UnmapPhysicalMemoryUnsafe"},
  1130. {0x4A, nullptr, "SetUnsafeLimit"},
  1131. {0x4B, nullptr, "CreateCodeMemory"},
  1132. {0x4C, nullptr, "ControlCodeMemory"},
  1133. {0x4D, nullptr, "SleepSystem"},
  1134. {0x4E, nullptr, "ReadWriteRegister"},
  1135. {0x4F, nullptr, "SetProcessActivity"},
  1136. {0x50, SvcWrap<CreateSharedMemory>, "CreateSharedMemory"},
  1137. {0x51, nullptr, "MapTransferMemory"},
  1138. {0x52, nullptr, "UnmapTransferMemory"},
  1139. {0x53, nullptr, "CreateInterruptEvent"},
  1140. {0x54, nullptr, "QueryPhysicalAddress"},
  1141. {0x55, nullptr, "QueryIoMapping"},
  1142. {0x56, nullptr, "CreateDeviceAddressSpace"},
  1143. {0x57, nullptr, "AttachDeviceAddressSpace"},
  1144. {0x58, nullptr, "DetachDeviceAddressSpace"},
  1145. {0x59, nullptr, "MapDeviceAddressSpaceByForce"},
  1146. {0x5A, nullptr, "MapDeviceAddressSpaceAligned"},
  1147. {0x5B, nullptr, "MapDeviceAddressSpace"},
  1148. {0x5C, nullptr, "UnmapDeviceAddressSpace"},
  1149. {0x5D, nullptr, "InvalidateProcessDataCache"},
  1150. {0x5E, nullptr, "StoreProcessDataCache"},
  1151. {0x5F, nullptr, "FlushProcessDataCache"},
  1152. {0x60, nullptr, "DebugActiveProcess"},
  1153. {0x61, nullptr, "BreakDebugProcess"},
  1154. {0x62, nullptr, "TerminateDebugProcess"},
  1155. {0x63, nullptr, "GetDebugEvent"},
  1156. {0x64, nullptr, "ContinueDebugEvent"},
  1157. {0x65, nullptr, "GetProcessList"},
  1158. {0x66, nullptr, "GetThreadList"},
  1159. {0x67, nullptr, "GetDebugThreadContext"},
  1160. {0x68, nullptr, "SetDebugThreadContext"},
  1161. {0x69, nullptr, "QueryDebugProcessMemory"},
  1162. {0x6A, nullptr, "ReadDebugProcessMemory"},
  1163. {0x6B, nullptr, "WriteDebugProcessMemory"},
  1164. {0x6C, nullptr, "SetHardwareBreakPoint"},
  1165. {0x6D, nullptr, "GetDebugThreadParam"},
  1166. {0x6E, nullptr, "Unknown"},
  1167. {0x6F, nullptr, "GetSystemInfo"},
  1168. {0x70, nullptr, "CreatePort"},
  1169. {0x71, nullptr, "ManageNamedPort"},
  1170. {0x72, nullptr, "ConnectToPort"},
  1171. {0x73, nullptr, "SetProcessMemoryPermission"},
  1172. {0x74, nullptr, "MapProcessMemory"},
  1173. {0x75, nullptr, "UnmapProcessMemory"},
  1174. {0x76, nullptr, "QueryProcessMemory"},
  1175. {0x77, nullptr, "MapProcessCodeMemory"},
  1176. {0x78, nullptr, "UnmapProcessCodeMemory"},
  1177. {0x79, nullptr, "CreateProcess"},
  1178. {0x7A, nullptr, "StartProcess"},
  1179. {0x7B, nullptr, "TerminateProcess"},
  1180. {0x7C, SvcWrap<GetProcessInfo>, "GetProcessInfo"},
  1181. {0x7D, nullptr, "CreateResourceLimit"},
  1182. {0x7E, nullptr, "SetResourceLimitLimitValue"},
  1183. {0x7F, nullptr, "CallSecureMonitor"},
  1184. };
  1185. static const FunctionDef* GetSVCInfo(u32 func_num) {
  1186. if (func_num >= std::size(SVC_Table)) {
  1187. LOG_ERROR(Kernel_SVC, "Unknown svc=0x{:02X}", func_num);
  1188. return nullptr;
  1189. }
  1190. return &SVC_Table[func_num];
  1191. }
  1192. MICROPROFILE_DEFINE(Kernel_SVC, "Kernel", "SVC", MP_RGB(70, 200, 70));
  1193. void CallSVC(u32 immediate) {
  1194. MICROPROFILE_SCOPE(Kernel_SVC);
  1195. // Lock the global kernel mutex when we enter the kernel HLE.
  1196. std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
  1197. const FunctionDef* info = GetSVCInfo(immediate);
  1198. if (info) {
  1199. if (info->func) {
  1200. info->func();
  1201. } else {
  1202. LOG_CRITICAL(Kernel_SVC, "Unimplemented SVC function {}(..)", info->name);
  1203. }
  1204. } else {
  1205. LOG_CRITICAL(Kernel_SVC, "Unknown SVC function 0x{:X}", immediate);
  1206. }
  1207. }
  1208. } // namespace Kernel