svc.cpp 69 KB

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