Syscall.cs 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Cpu;
  4. using Ryujinx.HLE.Exceptions;
  5. using Ryujinx.HLE.HOS.Kernel.Common;
  6. using Ryujinx.HLE.HOS.Kernel.Ipc;
  7. using Ryujinx.HLE.HOS.Kernel.Memory;
  8. using Ryujinx.HLE.HOS.Kernel.Process;
  9. using Ryujinx.HLE.HOS.Kernel.Threading;
  10. using Ryujinx.Horizon.Common;
  11. using System;
  12. using System.Threading;
  13. namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
  14. {
  15. [SvcImpl]
  16. class Syscall : ISyscallApi
  17. {
  18. private readonly KernelContext _context;
  19. public Syscall(KernelContext context)
  20. {
  21. _context = context;
  22. }
  23. // Process
  24. [Svc(0x24)]
  25. public Result GetProcessId(out ulong pid, int handle)
  26. {
  27. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  28. KProcess process = currentProcess.HandleTable.GetKProcess(handle);
  29. if (process == null)
  30. {
  31. KThread thread = currentProcess.HandleTable.GetKThread(handle);
  32. if (thread != null)
  33. {
  34. process = thread.Owner;
  35. }
  36. // TODO: KDebugEvent.
  37. }
  38. pid = process?.Pid ?? 0;
  39. return process != null
  40. ? Result.Success
  41. : KernelResult.InvalidHandle;
  42. }
  43. public Result CreateProcess(
  44. out int handle,
  45. ProcessCreationInfo info,
  46. ReadOnlySpan<uint> capabilities,
  47. IProcessContextFactory contextFactory,
  48. ThreadStart customThreadStart = null)
  49. {
  50. handle = 0;
  51. if ((info.Flags & ~ProcessCreationFlags.All) != 0)
  52. {
  53. return KernelResult.InvalidEnumValue;
  54. }
  55. // TODO: Address space check.
  56. if ((info.Flags & ProcessCreationFlags.PoolPartitionMask) > ProcessCreationFlags.PoolPartitionSystemNonSecure)
  57. {
  58. return KernelResult.InvalidEnumValue;
  59. }
  60. if ((info.CodeAddress & 0x1fffff) != 0)
  61. {
  62. return KernelResult.InvalidAddress;
  63. }
  64. if (info.CodePagesCount < 0 || info.SystemResourcePagesCount < 0)
  65. {
  66. return KernelResult.InvalidSize;
  67. }
  68. if (info.Flags.HasFlag(ProcessCreationFlags.OptimizeMemoryAllocation) &&
  69. !info.Flags.HasFlag(ProcessCreationFlags.IsApplication))
  70. {
  71. return KernelResult.InvalidThread;
  72. }
  73. KHandleTable handleTable = KernelStatic.GetCurrentProcess().HandleTable;
  74. KProcess process = new KProcess(_context);
  75. using var _ = new OnScopeExit(process.DecrementReferenceCount);
  76. KResourceLimit resourceLimit;
  77. if (info.ResourceLimitHandle != 0)
  78. {
  79. resourceLimit = handleTable.GetObject<KResourceLimit>(info.ResourceLimitHandle);
  80. if (resourceLimit == null)
  81. {
  82. return KernelResult.InvalidHandle;
  83. }
  84. }
  85. else
  86. {
  87. resourceLimit = _context.ResourceLimit;
  88. }
  89. MemoryRegion memRegion = (info.Flags & ProcessCreationFlags.PoolPartitionMask) switch
  90. {
  91. ProcessCreationFlags.PoolPartitionApplication => MemoryRegion.Application,
  92. ProcessCreationFlags.PoolPartitionApplet => MemoryRegion.Applet,
  93. ProcessCreationFlags.PoolPartitionSystem => MemoryRegion.Service,
  94. ProcessCreationFlags.PoolPartitionSystemNonSecure => MemoryRegion.NvServices,
  95. _ => MemoryRegion.NvServices
  96. };
  97. Result result = process.Initialize(
  98. info,
  99. capabilities,
  100. resourceLimit,
  101. memRegion,
  102. contextFactory,
  103. customThreadStart);
  104. if (result != Result.Success)
  105. {
  106. return result;
  107. }
  108. _context.Processes.TryAdd(process.Pid, process);
  109. return handleTable.GenerateHandle(process, out handle);
  110. }
  111. public Result StartProcess(int handle, int priority, int cpuCore, ulong mainThreadStackSize)
  112. {
  113. KProcess process = KernelStatic.GetCurrentProcess().HandleTable.GetObject<KProcess>(handle);
  114. if (process == null)
  115. {
  116. return KernelResult.InvalidHandle;
  117. }
  118. if ((uint)cpuCore >= KScheduler.CpuCoresCount || !process.IsCpuCoreAllowed(cpuCore))
  119. {
  120. return KernelResult.InvalidCpuCore;
  121. }
  122. if ((uint)priority >= KScheduler.PrioritiesCount || !process.IsPriorityAllowed(priority))
  123. {
  124. return KernelResult.InvalidPriority;
  125. }
  126. process.DefaultCpuCore = cpuCore;
  127. Result result = process.Start(priority, mainThreadStackSize);
  128. if (result != Result.Success)
  129. {
  130. return result;
  131. }
  132. process.IncrementReferenceCount();
  133. return Result.Success;
  134. }
  135. [Svc(0x5f)]
  136. public Result FlushProcessDataCache(int processHandle, ulong address, ulong size)
  137. {
  138. // FIXME: This needs to be implemented as ARMv7 doesn't have any way to do cache maintenance operations on EL0.
  139. // As we don't support (and don't actually need) to flush the cache, this is stubbed.
  140. return Result.Success;
  141. }
  142. // IPC
  143. [Svc(0x1f)]
  144. public Result ConnectToNamedPort(out int handle, [PointerSized] ulong namePtr)
  145. {
  146. handle = 0;
  147. if (!KernelTransfer.UserToKernelString(out string name, namePtr, 12))
  148. {
  149. return KernelResult.UserCopyFailed;
  150. }
  151. return ConnectToNamedPort(out handle, name);
  152. }
  153. public Result ConnectToNamedPort(out int handle, string name)
  154. {
  155. handle = 0;
  156. if (name.Length > 11)
  157. {
  158. return KernelResult.MaximumExceeded;
  159. }
  160. KAutoObject autoObj = KAutoObject.FindNamedObject(_context, name);
  161. if (autoObj is not KClientPort clientPort)
  162. {
  163. return KernelResult.NotFound;
  164. }
  165. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  166. Result result = currentProcess.HandleTable.ReserveHandle(out handle);
  167. if (result != Result.Success)
  168. {
  169. return result;
  170. }
  171. result = clientPort.Connect(out KClientSession clientSession);
  172. if (result != Result.Success)
  173. {
  174. currentProcess.HandleTable.CancelHandleReservation(handle);
  175. return result;
  176. }
  177. currentProcess.HandleTable.SetReservedHandleObj(handle, clientSession);
  178. clientSession.DecrementReferenceCount();
  179. return result;
  180. }
  181. [Svc(0x21)]
  182. public Result SendSyncRequest(int handle)
  183. {
  184. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  185. KClientSession session = currentProcess.HandleTable.GetObject<KClientSession>(handle);
  186. if (session == null)
  187. {
  188. return KernelResult.InvalidHandle;
  189. }
  190. return session.SendSyncRequest();
  191. }
  192. [Svc(0x22)]
  193. public Result SendSyncRequestWithUserBuffer(
  194. [PointerSized] ulong messagePtr,
  195. [PointerSized] ulong messageSize,
  196. int handle)
  197. {
  198. if (!PageAligned(messagePtr))
  199. {
  200. return KernelResult.InvalidAddress;
  201. }
  202. if (!PageAligned(messageSize) || messageSize == 0)
  203. {
  204. return KernelResult.InvalidSize;
  205. }
  206. if (messagePtr + messageSize <= messagePtr)
  207. {
  208. return KernelResult.InvalidMemState;
  209. }
  210. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  211. Result result = currentProcess.MemoryManager.BorrowIpcBuffer(messagePtr, messageSize);
  212. if (result != Result.Success)
  213. {
  214. return result;
  215. }
  216. KClientSession session = currentProcess.HandleTable.GetObject<KClientSession>(handle);
  217. if (session == null)
  218. {
  219. result = KernelResult.InvalidHandle;
  220. }
  221. else
  222. {
  223. result = session.SendSyncRequest(messagePtr, messageSize);
  224. }
  225. Result result2 = currentProcess.MemoryManager.UnborrowIpcBuffer(messagePtr, messageSize);
  226. if (result == Result.Success)
  227. {
  228. result = result2;
  229. }
  230. return result;
  231. }
  232. [Svc(0x23)]
  233. public Result SendAsyncRequestWithUserBuffer(
  234. out int doneEventHandle,
  235. [PointerSized] ulong messagePtr,
  236. [PointerSized] ulong messageSize,
  237. int handle)
  238. {
  239. doneEventHandle = 0;
  240. if (!PageAligned(messagePtr))
  241. {
  242. return KernelResult.InvalidAddress;
  243. }
  244. if (!PageAligned(messageSize) || messageSize == 0)
  245. {
  246. return KernelResult.InvalidSize;
  247. }
  248. if (messagePtr + messageSize <= messagePtr)
  249. {
  250. return KernelResult.InvalidMemState;
  251. }
  252. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  253. Result result = currentProcess.MemoryManager.BorrowIpcBuffer(messagePtr, messageSize);
  254. if (result != Result.Success)
  255. {
  256. return result;
  257. }
  258. KResourceLimit resourceLimit = currentProcess.ResourceLimit;
  259. if (resourceLimit != null && !resourceLimit.Reserve(LimitableResource.Event, 1))
  260. {
  261. currentProcess.MemoryManager.UnborrowIpcBuffer(messagePtr, messageSize);
  262. return KernelResult.ResLimitExceeded;
  263. }
  264. KClientSession session = currentProcess.HandleTable.GetObject<KClientSession>(handle);
  265. if (session == null)
  266. {
  267. result = KernelResult.InvalidHandle;
  268. }
  269. else
  270. {
  271. KEvent doneEvent = new KEvent(_context);
  272. result = currentProcess.HandleTable.GenerateHandle(doneEvent.ReadableEvent, out doneEventHandle);
  273. if (result == Result.Success)
  274. {
  275. result = session.SendAsyncRequest(doneEvent.WritableEvent, messagePtr, messageSize);
  276. if (result != Result.Success)
  277. {
  278. currentProcess.HandleTable.CloseHandle(doneEventHandle);
  279. }
  280. }
  281. }
  282. if (result != Result.Success)
  283. {
  284. resourceLimit?.Release(LimitableResource.Event, 1);
  285. currentProcess.MemoryManager.UnborrowIpcBuffer(messagePtr, messageSize);
  286. }
  287. return result;
  288. }
  289. [Svc(0x40)]
  290. public Result CreateSession(
  291. out int serverSessionHandle,
  292. out int clientSessionHandle,
  293. bool isLight,
  294. [PointerSized] ulong namePtr)
  295. {
  296. return CreateSession(out serverSessionHandle, out clientSessionHandle, isLight, null);
  297. }
  298. public Result CreateSession(
  299. out int serverSessionHandle,
  300. out int clientSessionHandle,
  301. bool isLight,
  302. string name)
  303. {
  304. serverSessionHandle = 0;
  305. clientSessionHandle = 0;
  306. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  307. KResourceLimit resourceLimit = currentProcess.ResourceLimit;
  308. if (resourceLimit != null && !resourceLimit.Reserve(LimitableResource.Session, 1))
  309. {
  310. return KernelResult.ResLimitExceeded;
  311. }
  312. Result result;
  313. if (isLight)
  314. {
  315. KLightSession session = new KLightSession(_context);
  316. result = currentProcess.HandleTable.GenerateHandle(session.ServerSession, out serverSessionHandle);
  317. if (result == Result.Success)
  318. {
  319. result = currentProcess.HandleTable.GenerateHandle(session.ClientSession, out clientSessionHandle);
  320. if (result != Result.Success)
  321. {
  322. currentProcess.HandleTable.CloseHandle(serverSessionHandle);
  323. serverSessionHandle = 0;
  324. }
  325. }
  326. session.ServerSession.DecrementReferenceCount();
  327. session.ClientSession.DecrementReferenceCount();
  328. }
  329. else
  330. {
  331. KSession session = new KSession(_context);
  332. result = currentProcess.HandleTable.GenerateHandle(session.ServerSession, out serverSessionHandle);
  333. if (result == Result.Success)
  334. {
  335. result = currentProcess.HandleTable.GenerateHandle(session.ClientSession, out clientSessionHandle);
  336. if (result != Result.Success)
  337. {
  338. currentProcess.HandleTable.CloseHandle(serverSessionHandle);
  339. serverSessionHandle = 0;
  340. }
  341. }
  342. session.ServerSession.DecrementReferenceCount();
  343. session.ClientSession.DecrementReferenceCount();
  344. }
  345. return result;
  346. }
  347. [Svc(0x41)]
  348. public Result AcceptSession(out int sessionHandle, int portHandle)
  349. {
  350. sessionHandle = 0;
  351. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  352. KServerPort serverPort = currentProcess.HandleTable.GetObject<KServerPort>(portHandle);
  353. if (serverPort == null)
  354. {
  355. return KernelResult.InvalidHandle;
  356. }
  357. Result result = currentProcess.HandleTable.ReserveHandle(out int handle);
  358. if (result != Result.Success)
  359. {
  360. return result;
  361. }
  362. KAutoObject session;
  363. if (serverPort.IsLight)
  364. {
  365. session = serverPort.AcceptIncomingLightConnection();
  366. }
  367. else
  368. {
  369. session = serverPort.AcceptIncomingConnection();
  370. }
  371. if (session != null)
  372. {
  373. currentProcess.HandleTable.SetReservedHandleObj(handle, session);
  374. session.DecrementReferenceCount();
  375. sessionHandle = handle;
  376. result = Result.Success;
  377. }
  378. else
  379. {
  380. currentProcess.HandleTable.CancelHandleReservation(handle);
  381. result = KernelResult.NotFound;
  382. }
  383. return result;
  384. }
  385. [Svc(0x43)]
  386. public Result ReplyAndReceive(
  387. out int handleIndex,
  388. [PointerSized] ulong handlesPtr,
  389. int handlesCount,
  390. int replyTargetHandle,
  391. long timeout)
  392. {
  393. handleIndex = 0;
  394. if ((uint)handlesCount > 0x40)
  395. {
  396. return KernelResult.MaximumExceeded;
  397. }
  398. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  399. ulong copySize = (ulong)((long)handlesCount * 4);
  400. if (!currentProcess.MemoryManager.InsideAddrSpace(handlesPtr, copySize))
  401. {
  402. return KernelResult.UserCopyFailed;
  403. }
  404. if (handlesPtr + copySize < handlesPtr)
  405. {
  406. return KernelResult.UserCopyFailed;
  407. }
  408. int[] handles = new int[handlesCount];
  409. if (!KernelTransfer.UserToKernelArray<int>(handlesPtr, handles))
  410. {
  411. return KernelResult.UserCopyFailed;
  412. }
  413. if (timeout > 0)
  414. {
  415. timeout += KTimeManager.DefaultTimeIncrementNanoseconds;
  416. }
  417. return ReplyAndReceive(out handleIndex, handles, replyTargetHandle, timeout);
  418. }
  419. public Result ReplyAndReceive(out int handleIndex, ReadOnlySpan<int> handles, int replyTargetHandle, long timeout)
  420. {
  421. handleIndex = 0;
  422. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  423. KSynchronizationObject[] syncObjs = new KSynchronizationObject[handles.Length];
  424. for (int index = 0; index < handles.Length; index++)
  425. {
  426. KSynchronizationObject obj = currentProcess.HandleTable.GetObject<KSynchronizationObject>(handles[index]);
  427. if (obj == null)
  428. {
  429. return KernelResult.InvalidHandle;
  430. }
  431. syncObjs[index] = obj;
  432. }
  433. Result result = Result.Success;
  434. if (replyTargetHandle != 0)
  435. {
  436. KServerSession replyTarget = currentProcess.HandleTable.GetObject<KServerSession>(replyTargetHandle);
  437. if (replyTarget == null)
  438. {
  439. result = KernelResult.InvalidHandle;
  440. }
  441. else
  442. {
  443. result = replyTarget.Reply();
  444. }
  445. }
  446. if (result == Result.Success)
  447. {
  448. if (timeout > 0)
  449. {
  450. timeout += KTimeManager.DefaultTimeIncrementNanoseconds;
  451. }
  452. while ((result = _context.Synchronization.WaitFor(syncObjs, timeout, out handleIndex)) == Result.Success)
  453. {
  454. KServerSession session = currentProcess.HandleTable.GetObject<KServerSession>(handles[handleIndex]);
  455. if (session == null)
  456. {
  457. break;
  458. }
  459. if ((result = session.Receive()) != KernelResult.NotFound)
  460. {
  461. break;
  462. }
  463. }
  464. }
  465. return result;
  466. }
  467. [Svc(0x44)]
  468. public Result ReplyAndReceiveWithUserBuffer(
  469. out int handleIndex,
  470. [PointerSized] ulong messagePtr,
  471. [PointerSized] ulong messageSize,
  472. [PointerSized] ulong handlesPtr,
  473. int handlesCount,
  474. int replyTargetHandle,
  475. long timeout)
  476. {
  477. handleIndex = 0;
  478. if ((uint)handlesCount > 0x40)
  479. {
  480. return KernelResult.MaximumExceeded;
  481. }
  482. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  483. ulong copySize = (ulong)((long)handlesCount * 4);
  484. if (!currentProcess.MemoryManager.InsideAddrSpace(handlesPtr, copySize))
  485. {
  486. return KernelResult.UserCopyFailed;
  487. }
  488. if (handlesPtr + copySize < handlesPtr)
  489. {
  490. return KernelResult.UserCopyFailed;
  491. }
  492. Result result = currentProcess.MemoryManager.BorrowIpcBuffer(messagePtr, messageSize);
  493. if (result != Result.Success)
  494. {
  495. return result;
  496. }
  497. int[] handles = new int[handlesCount];
  498. if (!KernelTransfer.UserToKernelArray<int>(handlesPtr, handles))
  499. {
  500. currentProcess.MemoryManager.UnborrowIpcBuffer(messagePtr, messageSize);
  501. return KernelResult.UserCopyFailed;
  502. }
  503. KSynchronizationObject[] syncObjs = new KSynchronizationObject[handlesCount];
  504. for (int index = 0; index < handlesCount; index++)
  505. {
  506. KSynchronizationObject obj = currentProcess.HandleTable.GetObject<KSynchronizationObject>(handles[index]);
  507. if (obj == null)
  508. {
  509. currentProcess.MemoryManager.UnborrowIpcBuffer(messagePtr, messageSize);
  510. return KernelResult.InvalidHandle;
  511. }
  512. syncObjs[index] = obj;
  513. }
  514. if (replyTargetHandle != 0)
  515. {
  516. KServerSession replyTarget = currentProcess.HandleTable.GetObject<KServerSession>(replyTargetHandle);
  517. if (replyTarget == null)
  518. {
  519. result = KernelResult.InvalidHandle;
  520. }
  521. else
  522. {
  523. result = replyTarget.Reply(messagePtr, messageSize);
  524. }
  525. }
  526. if (result == Result.Success)
  527. {
  528. if (timeout > 0)
  529. {
  530. timeout += KTimeManager.DefaultTimeIncrementNanoseconds;
  531. }
  532. while ((result = _context.Synchronization.WaitFor(syncObjs, timeout, out handleIndex)) == Result.Success)
  533. {
  534. KServerSession session = currentProcess.HandleTable.GetObject<KServerSession>(handles[handleIndex]);
  535. if (session == null)
  536. {
  537. break;
  538. }
  539. if ((result = session.Receive(messagePtr, messageSize)) != KernelResult.NotFound)
  540. {
  541. break;
  542. }
  543. }
  544. }
  545. currentProcess.MemoryManager.UnborrowIpcBuffer(messagePtr, messageSize);
  546. return result;
  547. }
  548. [Svc(0x70)]
  549. public Result CreatePort(
  550. out int serverPortHandle,
  551. out int clientPortHandle,
  552. int maxSessions,
  553. bool isLight,
  554. [PointerSized] ulong namePtr)
  555. {
  556. // The kernel doesn't use the name pointer, so we can just pass null as the name.
  557. return CreatePort(out serverPortHandle, out clientPortHandle, maxSessions, isLight, null);
  558. }
  559. public Result CreatePort(
  560. out int serverPortHandle,
  561. out int clientPortHandle,
  562. int maxSessions,
  563. bool isLight,
  564. string name)
  565. {
  566. serverPortHandle = clientPortHandle = 0;
  567. if (maxSessions < 1)
  568. {
  569. return KernelResult.MaximumExceeded;
  570. }
  571. KPort port = new KPort(_context, maxSessions, isLight, name);
  572. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  573. Result result = currentProcess.HandleTable.GenerateHandle(port.ClientPort, out clientPortHandle);
  574. if (result != Result.Success)
  575. {
  576. return result;
  577. }
  578. result = currentProcess.HandleTable.GenerateHandle(port.ServerPort, out serverPortHandle);
  579. if (result != Result.Success)
  580. {
  581. currentProcess.HandleTable.CloseHandle(clientPortHandle);
  582. }
  583. return result;
  584. }
  585. [Svc(0x71)]
  586. public Result ManageNamedPort(out int handle, [PointerSized] ulong namePtr, int maxSessions)
  587. {
  588. handle = 0;
  589. if (!KernelTransfer.UserToKernelString(out string name, namePtr, 12))
  590. {
  591. return KernelResult.UserCopyFailed;
  592. }
  593. if (name.Length > 11)
  594. {
  595. return KernelResult.MaximumExceeded;
  596. }
  597. return ManageNamedPort(out handle, name, maxSessions);
  598. }
  599. public Result ManageNamedPort(out int handle, string name, int maxSessions)
  600. {
  601. handle = 0;
  602. if (maxSessions < 0)
  603. {
  604. return KernelResult.MaximumExceeded;
  605. }
  606. if (maxSessions == 0)
  607. {
  608. return KAutoObject.RemoveName(_context, name);
  609. }
  610. KPort port = new KPort(_context, maxSessions, false, null);
  611. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  612. Result result = currentProcess.HandleTable.GenerateHandle(port.ServerPort, out handle);
  613. if (result != Result.Success)
  614. {
  615. return result;
  616. }
  617. result = port.ClientPort.SetName(name);
  618. if (result != Result.Success)
  619. {
  620. currentProcess.HandleTable.CloseHandle(handle);
  621. }
  622. return result;
  623. }
  624. [Svc(0x72)]
  625. public Result ConnectToPort(out int clientSessionHandle, int clientPortHandle)
  626. {
  627. clientSessionHandle = 0;
  628. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  629. KClientPort clientPort = currentProcess.HandleTable.GetObject<KClientPort>(clientPortHandle);
  630. if (clientPort == null)
  631. {
  632. return KernelResult.InvalidHandle;
  633. }
  634. Result result = currentProcess.HandleTable.ReserveHandle(out int handle);
  635. if (result != Result.Success)
  636. {
  637. return result;
  638. }
  639. KAutoObject session;
  640. if (clientPort.IsLight)
  641. {
  642. result = clientPort.ConnectLight(out KLightClientSession clientSession);
  643. session = clientSession;
  644. }
  645. else
  646. {
  647. result = clientPort.Connect(out KClientSession clientSession);
  648. session = clientSession;
  649. }
  650. if (result != Result.Success)
  651. {
  652. currentProcess.HandleTable.CancelHandleReservation(handle);
  653. return result;
  654. }
  655. currentProcess.HandleTable.SetReservedHandleObj(handle, session);
  656. session.DecrementReferenceCount();
  657. clientSessionHandle = handle;
  658. return result;
  659. }
  660. // Memory
  661. [Svc(1)]
  662. public Result SetHeapSize([PointerSized] out ulong address, [PointerSized] ulong size)
  663. {
  664. if ((size & 0xfffffffe001fffff) != 0)
  665. {
  666. address = 0;
  667. return KernelResult.InvalidSize;
  668. }
  669. KProcess process = KernelStatic.GetCurrentProcess();
  670. return process.MemoryManager.SetHeapSize(size, out address);
  671. }
  672. [Svc(2)]
  673. public Result SetMemoryPermission([PointerSized] ulong address, [PointerSized] ulong size, KMemoryPermission permission)
  674. {
  675. if (!PageAligned(address))
  676. {
  677. return KernelResult.InvalidAddress;
  678. }
  679. if (!PageAligned(size) || size == 0)
  680. {
  681. return KernelResult.InvalidSize;
  682. }
  683. if (address + size <= address)
  684. {
  685. return KernelResult.InvalidMemState;
  686. }
  687. if (permission != KMemoryPermission.None && (permission | KMemoryPermission.Write) != KMemoryPermission.ReadAndWrite)
  688. {
  689. return KernelResult.InvalidPermission;
  690. }
  691. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  692. if (!currentProcess.MemoryManager.InsideAddrSpace(address, size))
  693. {
  694. return KernelResult.InvalidMemState;
  695. }
  696. return currentProcess.MemoryManager.SetMemoryPermission(address, size, permission);
  697. }
  698. [Svc(3)]
  699. public Result SetMemoryAttribute(
  700. [PointerSized] ulong address,
  701. [PointerSized] ulong size,
  702. MemoryAttribute attributeMask,
  703. MemoryAttribute attributeValue)
  704. {
  705. if (!PageAligned(address))
  706. {
  707. return KernelResult.InvalidAddress;
  708. }
  709. if (!PageAligned(size) || size == 0)
  710. {
  711. return KernelResult.InvalidSize;
  712. }
  713. MemoryAttribute attributes = attributeMask | attributeValue;
  714. if (attributes != attributeMask ||
  715. (attributes | MemoryAttribute.Uncached) != MemoryAttribute.Uncached)
  716. {
  717. return KernelResult.InvalidCombination;
  718. }
  719. KProcess process = KernelStatic.GetCurrentProcess();
  720. if (!process.MemoryManager.InsideAddrSpace(address, size))
  721. {
  722. return KernelResult.InvalidMemState;
  723. }
  724. Result result = process.MemoryManager.SetMemoryAttribute(
  725. address,
  726. size,
  727. attributeMask,
  728. attributeValue);
  729. return result;
  730. }
  731. [Svc(4)]
  732. public Result MapMemory([PointerSized] ulong dst, [PointerSized] ulong src, [PointerSized] ulong size)
  733. {
  734. if (!PageAligned(src | dst))
  735. {
  736. return KernelResult.InvalidAddress;
  737. }
  738. if (!PageAligned(size) || size == 0)
  739. {
  740. return KernelResult.InvalidSize;
  741. }
  742. if (src + size <= src || dst + size <= dst)
  743. {
  744. return KernelResult.InvalidMemState;
  745. }
  746. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  747. if (!currentProcess.MemoryManager.InsideAddrSpace(src, size))
  748. {
  749. return KernelResult.InvalidMemState;
  750. }
  751. if (currentProcess.MemoryManager.OutsideStackRegion(dst, size) ||
  752. currentProcess.MemoryManager.InsideHeapRegion(dst, size) ||
  753. currentProcess.MemoryManager.InsideAliasRegion(dst, size))
  754. {
  755. return KernelResult.InvalidMemRange;
  756. }
  757. KProcess process = KernelStatic.GetCurrentProcess();
  758. return process.MemoryManager.Map(dst, src, size);
  759. }
  760. [Svc(5)]
  761. public Result UnmapMemory([PointerSized] ulong dst, [PointerSized] ulong src, [PointerSized] ulong size)
  762. {
  763. if (!PageAligned(src | dst))
  764. {
  765. return KernelResult.InvalidAddress;
  766. }
  767. if (!PageAligned(size) || size == 0)
  768. {
  769. return KernelResult.InvalidSize;
  770. }
  771. if (src + size <= src || dst + size <= dst)
  772. {
  773. return KernelResult.InvalidMemState;
  774. }
  775. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  776. if (!currentProcess.MemoryManager.InsideAddrSpace(src, size))
  777. {
  778. return KernelResult.InvalidMemState;
  779. }
  780. if (currentProcess.MemoryManager.OutsideStackRegion(dst, size) ||
  781. currentProcess.MemoryManager.InsideHeapRegion(dst, size) ||
  782. currentProcess.MemoryManager.InsideAliasRegion(dst, size))
  783. {
  784. return KernelResult.InvalidMemRange;
  785. }
  786. KProcess process = KernelStatic.GetCurrentProcess();
  787. return process.MemoryManager.Unmap(dst, src, size);
  788. }
  789. [Svc(6)]
  790. public Result QueryMemory([PointerSized] ulong infoPtr, [PointerSized] out ulong pageInfo, [PointerSized] ulong address)
  791. {
  792. Result result = QueryMemory(out MemoryInfo info, out pageInfo, address);
  793. if (result == Result.Success)
  794. {
  795. return KernelTransfer.KernelToUser(infoPtr, info)
  796. ? Result.Success
  797. : KernelResult.InvalidMemState;
  798. }
  799. return result;
  800. }
  801. public Result QueryMemory(out MemoryInfo info, out ulong pageInfo, ulong address)
  802. {
  803. KProcess process = KernelStatic.GetCurrentProcess();
  804. KMemoryInfo blockInfo = process.MemoryManager.QueryMemory(address);
  805. info = new MemoryInfo(
  806. blockInfo.Address,
  807. blockInfo.Size,
  808. blockInfo.State & MemoryState.UserMask,
  809. blockInfo.Attribute,
  810. blockInfo.Permission & KMemoryPermission.UserMask,
  811. blockInfo.IpcRefCount,
  812. blockInfo.DeviceRefCount);
  813. pageInfo = 0;
  814. return Result.Success;
  815. }
  816. [Svc(0x13)]
  817. public Result MapSharedMemory(int handle, [PointerSized] ulong address, [PointerSized] ulong size, KMemoryPermission permission)
  818. {
  819. if (!PageAligned(address))
  820. {
  821. return KernelResult.InvalidAddress;
  822. }
  823. if (!PageAligned(size) || size == 0)
  824. {
  825. return KernelResult.InvalidSize;
  826. }
  827. if (address + size <= address)
  828. {
  829. return KernelResult.InvalidMemState;
  830. }
  831. if ((permission | KMemoryPermission.Write) != KMemoryPermission.ReadAndWrite)
  832. {
  833. return KernelResult.InvalidPermission;
  834. }
  835. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  836. KSharedMemory sharedMemory = currentProcess.HandleTable.GetObject<KSharedMemory>(handle);
  837. if (sharedMemory == null)
  838. {
  839. return KernelResult.InvalidHandle;
  840. }
  841. if (currentProcess.MemoryManager.IsInvalidRegion(address, size) ||
  842. currentProcess.MemoryManager.InsideHeapRegion(address, size) ||
  843. currentProcess.MemoryManager.InsideAliasRegion(address, size))
  844. {
  845. return KernelResult.InvalidMemRange;
  846. }
  847. return sharedMemory.MapIntoProcess(
  848. currentProcess.MemoryManager,
  849. address,
  850. size,
  851. currentProcess,
  852. permission);
  853. }
  854. [Svc(0x14)]
  855. public Result UnmapSharedMemory(int handle, [PointerSized] ulong address, [PointerSized] ulong size)
  856. {
  857. if (!PageAligned(address))
  858. {
  859. return KernelResult.InvalidAddress;
  860. }
  861. if (!PageAligned(size) || size == 0)
  862. {
  863. return KernelResult.InvalidSize;
  864. }
  865. if (address + size <= address)
  866. {
  867. return KernelResult.InvalidMemState;
  868. }
  869. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  870. KSharedMemory sharedMemory = currentProcess.HandleTable.GetObject<KSharedMemory>(handle);
  871. if (sharedMemory == null)
  872. {
  873. return KernelResult.InvalidHandle;
  874. }
  875. if (currentProcess.MemoryManager.IsInvalidRegion(address, size) ||
  876. currentProcess.MemoryManager.InsideHeapRegion(address, size) ||
  877. currentProcess.MemoryManager.InsideAliasRegion(address, size))
  878. {
  879. return KernelResult.InvalidMemRange;
  880. }
  881. return sharedMemory.UnmapFromProcess(
  882. currentProcess.MemoryManager,
  883. address,
  884. size,
  885. currentProcess);
  886. }
  887. [Svc(0x15)]
  888. public Result CreateTransferMemory(out int handle, [PointerSized] ulong address, [PointerSized] ulong size, KMemoryPermission permission)
  889. {
  890. handle = 0;
  891. if (!PageAligned(address))
  892. {
  893. return KernelResult.InvalidAddress;
  894. }
  895. if (!PageAligned(size) || size == 0)
  896. {
  897. return KernelResult.InvalidSize;
  898. }
  899. if (address + size <= address)
  900. {
  901. return KernelResult.InvalidMemState;
  902. }
  903. if (permission > KMemoryPermission.ReadAndWrite || permission == KMemoryPermission.Write)
  904. {
  905. return KernelResult.InvalidPermission;
  906. }
  907. KProcess process = KernelStatic.GetCurrentProcess();
  908. KResourceLimit resourceLimit = process.ResourceLimit;
  909. if (resourceLimit != null && !resourceLimit.Reserve(LimitableResource.TransferMemory, 1))
  910. {
  911. return KernelResult.ResLimitExceeded;
  912. }
  913. void CleanUpForError()
  914. {
  915. resourceLimit?.Release(LimitableResource.TransferMemory, 1);
  916. }
  917. if (!process.MemoryManager.InsideAddrSpace(address, size))
  918. {
  919. CleanUpForError();
  920. return KernelResult.InvalidMemState;
  921. }
  922. KTransferMemory transferMemory = new KTransferMemory(_context);
  923. Result result = transferMemory.Initialize(address, size, permission);
  924. if (result != Result.Success)
  925. {
  926. CleanUpForError();
  927. return result;
  928. }
  929. result = process.HandleTable.GenerateHandle(transferMemory, out handle);
  930. transferMemory.DecrementReferenceCount();
  931. return result;
  932. }
  933. [Svc(0x51)]
  934. public Result MapTransferMemory(int handle, [PointerSized] ulong address, [PointerSized] ulong size, KMemoryPermission permission)
  935. {
  936. if (!PageAligned(address))
  937. {
  938. return KernelResult.InvalidAddress;
  939. }
  940. if (!PageAligned(size) || size == 0)
  941. {
  942. return KernelResult.InvalidSize;
  943. }
  944. if (address + size <= address)
  945. {
  946. return KernelResult.InvalidMemState;
  947. }
  948. if (permission > KMemoryPermission.ReadAndWrite || permission == KMemoryPermission.Write)
  949. {
  950. return KernelResult.InvalidPermission;
  951. }
  952. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  953. KTransferMemory transferMemory = currentProcess.HandleTable.GetObject<KTransferMemory>(handle);
  954. if (transferMemory == null)
  955. {
  956. return KernelResult.InvalidHandle;
  957. }
  958. if (currentProcess.MemoryManager.IsInvalidRegion(address, size) ||
  959. currentProcess.MemoryManager.InsideHeapRegion(address, size) ||
  960. currentProcess.MemoryManager.InsideAliasRegion(address, size))
  961. {
  962. return KernelResult.InvalidMemRange;
  963. }
  964. return transferMemory.MapIntoProcess(
  965. currentProcess.MemoryManager,
  966. address,
  967. size,
  968. currentProcess,
  969. permission);
  970. }
  971. [Svc(0x52)]
  972. public Result UnmapTransferMemory(int handle, [PointerSized] ulong address, [PointerSized] ulong size)
  973. {
  974. if (!PageAligned(address))
  975. {
  976. return KernelResult.InvalidAddress;
  977. }
  978. if (!PageAligned(size) || size == 0)
  979. {
  980. return KernelResult.InvalidSize;
  981. }
  982. if (address + size <= address)
  983. {
  984. return KernelResult.InvalidMemState;
  985. }
  986. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  987. KTransferMemory transferMemory = currentProcess.HandleTable.GetObject<KTransferMemory>(handle);
  988. if (transferMemory == null)
  989. {
  990. return KernelResult.InvalidHandle;
  991. }
  992. if (currentProcess.MemoryManager.IsInvalidRegion(address, size) ||
  993. currentProcess.MemoryManager.InsideHeapRegion(address, size) ||
  994. currentProcess.MemoryManager.InsideAliasRegion(address, size))
  995. {
  996. return KernelResult.InvalidMemRange;
  997. }
  998. return transferMemory.UnmapFromProcess(
  999. currentProcess.MemoryManager,
  1000. address,
  1001. size,
  1002. currentProcess);
  1003. }
  1004. [Svc(0x2c)]
  1005. public Result MapPhysicalMemory([PointerSized] ulong address, [PointerSized] ulong size)
  1006. {
  1007. if (!PageAligned(address))
  1008. {
  1009. return KernelResult.InvalidAddress;
  1010. }
  1011. if (!PageAligned(size) || size == 0)
  1012. {
  1013. return KernelResult.InvalidSize;
  1014. }
  1015. if (address + size <= address)
  1016. {
  1017. return KernelResult.InvalidMemRange;
  1018. }
  1019. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1020. if ((currentProcess.PersonalMmHeapPagesCount & 0xfffffffffffff) == 0)
  1021. {
  1022. return KernelResult.InvalidState;
  1023. }
  1024. if (!currentProcess.MemoryManager.InsideAddrSpace(address, size) ||
  1025. currentProcess.MemoryManager.OutsideAliasRegion(address, size))
  1026. {
  1027. return KernelResult.InvalidMemRange;
  1028. }
  1029. KProcess process = KernelStatic.GetCurrentProcess();
  1030. return process.MemoryManager.MapPhysicalMemory(address, size);
  1031. }
  1032. [Svc(0x2d)]
  1033. public Result UnmapPhysicalMemory([PointerSized] ulong address, [PointerSized] ulong size)
  1034. {
  1035. if (!PageAligned(address))
  1036. {
  1037. return KernelResult.InvalidAddress;
  1038. }
  1039. if (!PageAligned(size) || size == 0)
  1040. {
  1041. return KernelResult.InvalidSize;
  1042. }
  1043. if (address + size <= address)
  1044. {
  1045. return KernelResult.InvalidMemRange;
  1046. }
  1047. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1048. if ((currentProcess.PersonalMmHeapPagesCount & 0xfffffffffffff) == 0)
  1049. {
  1050. return KernelResult.InvalidState;
  1051. }
  1052. if (!currentProcess.MemoryManager.InsideAddrSpace(address, size) ||
  1053. currentProcess.MemoryManager.OutsideAliasRegion(address, size))
  1054. {
  1055. return KernelResult.InvalidMemRange;
  1056. }
  1057. KProcess process = KernelStatic.GetCurrentProcess();
  1058. return process.MemoryManager.UnmapPhysicalMemory(address, size);
  1059. }
  1060. [Svc(0x4b)]
  1061. public Result CreateCodeMemory(out int handle, [PointerSized] ulong address, [PointerSized] ulong size)
  1062. {
  1063. handle = 0;
  1064. if (!PageAligned(address))
  1065. {
  1066. return KernelResult.InvalidAddress;
  1067. }
  1068. if (!PageAligned(size) || size == 0)
  1069. {
  1070. return KernelResult.InvalidSize;
  1071. }
  1072. if (size + address <= address)
  1073. {
  1074. return KernelResult.InvalidMemState;
  1075. }
  1076. KCodeMemory codeMemory = new KCodeMemory(_context);
  1077. using var _ = new OnScopeExit(codeMemory.DecrementReferenceCount);
  1078. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1079. if (!currentProcess.MemoryManager.InsideAddrSpace(address, size))
  1080. {
  1081. return KernelResult.InvalidMemState;
  1082. }
  1083. Result result = codeMemory.Initialize(address, size);
  1084. if (result != Result.Success)
  1085. {
  1086. return result;
  1087. }
  1088. return currentProcess.HandleTable.GenerateHandle(codeMemory, out handle);
  1089. }
  1090. [Svc(0x4c)]
  1091. public Result ControlCodeMemory(
  1092. int handle,
  1093. CodeMemoryOperation op,
  1094. ulong address,
  1095. ulong size,
  1096. KMemoryPermission permission)
  1097. {
  1098. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1099. KCodeMemory codeMemory = currentProcess.HandleTable.GetObject<KCodeMemory>(handle);
  1100. // Newer versions of the kernel also returns an error here if the owner and process
  1101. // where the operation will happen are the same. We do not return an error here
  1102. // for homebrew because some of them requires this to be patched out to work (for JIT).
  1103. if (codeMemory == null || (!currentProcess.AllowCodeMemoryForJit && codeMemory.Owner == currentProcess))
  1104. {
  1105. return KernelResult.InvalidHandle;
  1106. }
  1107. switch (op)
  1108. {
  1109. case CodeMemoryOperation.Map:
  1110. if (!currentProcess.MemoryManager.CanContain(address, size, MemoryState.CodeWritable))
  1111. {
  1112. return KernelResult.InvalidMemRange;
  1113. }
  1114. if (permission != KMemoryPermission.ReadAndWrite)
  1115. {
  1116. return KernelResult.InvalidPermission;
  1117. }
  1118. return codeMemory.Map(address, size, permission);
  1119. case CodeMemoryOperation.MapToOwner:
  1120. if (!currentProcess.MemoryManager.CanContain(address, size, MemoryState.CodeReadOnly))
  1121. {
  1122. return KernelResult.InvalidMemRange;
  1123. }
  1124. if (permission != KMemoryPermission.Read && permission != KMemoryPermission.ReadAndExecute)
  1125. {
  1126. return KernelResult.InvalidPermission;
  1127. }
  1128. return codeMemory.MapToOwner(address, size, permission);
  1129. case CodeMemoryOperation.Unmap:
  1130. if (!currentProcess.MemoryManager.CanContain(address, size, MemoryState.CodeWritable))
  1131. {
  1132. return KernelResult.InvalidMemRange;
  1133. }
  1134. if (permission != KMemoryPermission.None)
  1135. {
  1136. return KernelResult.InvalidPermission;
  1137. }
  1138. return codeMemory.Unmap(address, size);
  1139. case CodeMemoryOperation.UnmapFromOwner:
  1140. if (!currentProcess.MemoryManager.CanContain(address, size, MemoryState.CodeReadOnly))
  1141. {
  1142. return KernelResult.InvalidMemRange;
  1143. }
  1144. if (permission != KMemoryPermission.None)
  1145. {
  1146. return KernelResult.InvalidPermission;
  1147. }
  1148. return codeMemory.UnmapFromOwner(address, size);
  1149. default: return KernelResult.InvalidEnumValue;
  1150. }
  1151. }
  1152. [Svc(0x73)]
  1153. public Result SetProcessMemoryPermission(
  1154. int handle,
  1155. [PointerSized] ulong src,
  1156. [PointerSized] ulong size,
  1157. KMemoryPermission permission)
  1158. {
  1159. if (!PageAligned(src))
  1160. {
  1161. return KernelResult.InvalidAddress;
  1162. }
  1163. if (!PageAligned(size) || size == 0)
  1164. {
  1165. return KernelResult.InvalidSize;
  1166. }
  1167. if (permission != KMemoryPermission.None &&
  1168. permission != KMemoryPermission.Read &&
  1169. permission != KMemoryPermission.ReadAndWrite &&
  1170. permission != KMemoryPermission.ReadAndExecute)
  1171. {
  1172. return KernelResult.InvalidPermission;
  1173. }
  1174. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1175. KProcess targetProcess = currentProcess.HandleTable.GetObject<KProcess>(handle);
  1176. if (targetProcess == null)
  1177. {
  1178. return KernelResult.InvalidHandle;
  1179. }
  1180. if (targetProcess.MemoryManager.OutsideAddrSpace(src, size))
  1181. {
  1182. return KernelResult.InvalidMemState;
  1183. }
  1184. return targetProcess.MemoryManager.SetProcessMemoryPermission(src, size, permission);
  1185. }
  1186. [Svc(0x74)]
  1187. public Result MapProcessMemory(
  1188. [PointerSized] ulong dst,
  1189. int handle,
  1190. ulong src,
  1191. [PointerSized] ulong size)
  1192. {
  1193. if (!PageAligned(src) || !PageAligned(dst))
  1194. {
  1195. return KernelResult.InvalidAddress;
  1196. }
  1197. if (!PageAligned(size) || size == 0)
  1198. {
  1199. return KernelResult.InvalidSize;
  1200. }
  1201. if (dst + size <= dst || src + size <= src)
  1202. {
  1203. return KernelResult.InvalidMemRange;
  1204. }
  1205. KProcess dstProcess = KernelStatic.GetCurrentProcess();
  1206. KProcess srcProcess = dstProcess.HandleTable.GetObject<KProcess>(handle);
  1207. if (srcProcess == null)
  1208. {
  1209. return KernelResult.InvalidHandle;
  1210. }
  1211. if (!srcProcess.MemoryManager.InsideAddrSpace(src, size) ||
  1212. !dstProcess.MemoryManager.CanContain(dst, size, MemoryState.ProcessMemory))
  1213. {
  1214. return KernelResult.InvalidMemRange;
  1215. }
  1216. KPageList pageList = new KPageList();
  1217. Result result = srcProcess.MemoryManager.GetPagesIfStateEquals(
  1218. src,
  1219. size,
  1220. MemoryState.MapProcessAllowed,
  1221. MemoryState.MapProcessAllowed,
  1222. KMemoryPermission.None,
  1223. KMemoryPermission.None,
  1224. MemoryAttribute.Mask,
  1225. MemoryAttribute.None,
  1226. pageList);
  1227. if (result != Result.Success)
  1228. {
  1229. return result;
  1230. }
  1231. return dstProcess.MemoryManager.MapPages(dst, pageList, MemoryState.ProcessMemory, KMemoryPermission.ReadAndWrite);
  1232. }
  1233. [Svc(0x75)]
  1234. public Result UnmapProcessMemory(
  1235. [PointerSized] ulong dst,
  1236. int handle,
  1237. ulong src,
  1238. [PointerSized] ulong size)
  1239. {
  1240. if (!PageAligned(src) || !PageAligned(dst))
  1241. {
  1242. return KernelResult.InvalidAddress;
  1243. }
  1244. if (!PageAligned(size) || size == 0)
  1245. {
  1246. return KernelResult.InvalidSize;
  1247. }
  1248. if (dst + size <= dst || src + size <= src)
  1249. {
  1250. return KernelResult.InvalidMemRange;
  1251. }
  1252. KProcess dstProcess = KernelStatic.GetCurrentProcess();
  1253. KProcess srcProcess = dstProcess.HandleTable.GetObject<KProcess>(handle);
  1254. if (srcProcess == null)
  1255. {
  1256. return KernelResult.InvalidHandle;
  1257. }
  1258. if (!srcProcess.MemoryManager.InsideAddrSpace(src, size) ||
  1259. !dstProcess.MemoryManager.CanContain(dst, size, MemoryState.ProcessMemory))
  1260. {
  1261. return KernelResult.InvalidMemRange;
  1262. }
  1263. Result result = dstProcess.MemoryManager.UnmapProcessMemory(dst, size, srcProcess.MemoryManager, src);
  1264. if (result != Result.Success)
  1265. {
  1266. return result;
  1267. }
  1268. return Result.Success;
  1269. }
  1270. [Svc(0x77)]
  1271. public Result MapProcessCodeMemory(int handle, ulong dst, ulong src, ulong size)
  1272. {
  1273. if (!PageAligned(dst) || !PageAligned(src))
  1274. {
  1275. return KernelResult.InvalidAddress;
  1276. }
  1277. if (!PageAligned(size) || size == 0)
  1278. {
  1279. return KernelResult.InvalidSize;
  1280. }
  1281. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1282. KProcess targetProcess = currentProcess.HandleTable.GetObject<KProcess>(handle);
  1283. if (targetProcess == null)
  1284. {
  1285. return KernelResult.InvalidHandle;
  1286. }
  1287. if (targetProcess.MemoryManager.OutsideAddrSpace(dst, size) ||
  1288. targetProcess.MemoryManager.OutsideAddrSpace(src, size) ||
  1289. targetProcess.MemoryManager.InsideAliasRegion(dst, size) ||
  1290. targetProcess.MemoryManager.InsideHeapRegion(dst, size))
  1291. {
  1292. return KernelResult.InvalidMemRange;
  1293. }
  1294. if (size + dst <= dst || size + src <= src)
  1295. {
  1296. return KernelResult.InvalidMemState;
  1297. }
  1298. return targetProcess.MemoryManager.MapProcessCodeMemory(dst, src, size);
  1299. }
  1300. [Svc(0x78)]
  1301. public Result UnmapProcessCodeMemory(int handle, ulong dst, ulong src, ulong size)
  1302. {
  1303. if (!PageAligned(dst) || !PageAligned(src))
  1304. {
  1305. return KernelResult.InvalidAddress;
  1306. }
  1307. if (!PageAligned(size) || size == 0)
  1308. {
  1309. return KernelResult.InvalidSize;
  1310. }
  1311. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1312. KProcess targetProcess = currentProcess.HandleTable.GetObject<KProcess>(handle);
  1313. if (targetProcess == null)
  1314. {
  1315. return KernelResult.InvalidHandle;
  1316. }
  1317. if (targetProcess.MemoryManager.OutsideAddrSpace(dst, size) ||
  1318. targetProcess.MemoryManager.OutsideAddrSpace(src, size) ||
  1319. targetProcess.MemoryManager.InsideAliasRegion(dst, size) ||
  1320. targetProcess.MemoryManager.InsideHeapRegion(dst, size))
  1321. {
  1322. return KernelResult.InvalidMemRange;
  1323. }
  1324. if (size + dst <= dst || size + src <= src)
  1325. {
  1326. return KernelResult.InvalidMemState;
  1327. }
  1328. return targetProcess.MemoryManager.UnmapProcessCodeMemory(dst, src, size);
  1329. }
  1330. private static bool PageAligned(ulong address)
  1331. {
  1332. return (address & (KPageTableBase.PageSize - 1)) == 0;
  1333. }
  1334. // System
  1335. [Svc(0x7b)]
  1336. public Result TerminateProcess(int handle)
  1337. {
  1338. KProcess process = KernelStatic.GetCurrentProcess();
  1339. process = process.HandleTable.GetObject<KProcess>(handle);
  1340. Result result;
  1341. if (process != null)
  1342. {
  1343. if (process == KernelStatic.GetCurrentProcess())
  1344. {
  1345. result = Result.Success;
  1346. process.DecrementToZeroWhileTerminatingCurrent();
  1347. }
  1348. else
  1349. {
  1350. result = process.Terminate();
  1351. process.DecrementReferenceCount();
  1352. }
  1353. }
  1354. else
  1355. {
  1356. result = KernelResult.InvalidHandle;
  1357. }
  1358. return result;
  1359. }
  1360. [Svc(7)]
  1361. public void ExitProcess()
  1362. {
  1363. KernelStatic.GetCurrentProcess().TerminateCurrentProcess();
  1364. }
  1365. [Svc(0x11)]
  1366. public Result SignalEvent(int handle)
  1367. {
  1368. KProcess process = KernelStatic.GetCurrentProcess();
  1369. KWritableEvent writableEvent = process.HandleTable.GetObject<KWritableEvent>(handle);
  1370. Result result;
  1371. if (writableEvent != null)
  1372. {
  1373. writableEvent.Signal();
  1374. result = Result.Success;
  1375. }
  1376. else
  1377. {
  1378. result = KernelResult.InvalidHandle;
  1379. }
  1380. return result;
  1381. }
  1382. [Svc(0x12)]
  1383. public Result ClearEvent(int handle)
  1384. {
  1385. Result result;
  1386. KProcess process = KernelStatic.GetCurrentProcess();
  1387. KWritableEvent writableEvent = process.HandleTable.GetObject<KWritableEvent>(handle);
  1388. if (writableEvent == null)
  1389. {
  1390. KReadableEvent readableEvent = process.HandleTable.GetObject<KReadableEvent>(handle);
  1391. result = readableEvent?.Clear() ?? KernelResult.InvalidHandle;
  1392. }
  1393. else
  1394. {
  1395. result = writableEvent.Clear();
  1396. }
  1397. return result;
  1398. }
  1399. [Svc(0x16)]
  1400. public Result CloseHandle(int handle)
  1401. {
  1402. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1403. return currentProcess.HandleTable.CloseHandle(handle) ? Result.Success : KernelResult.InvalidHandle;
  1404. }
  1405. [Svc(0x17)]
  1406. public Result ResetSignal(int handle)
  1407. {
  1408. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1409. KReadableEvent readableEvent = currentProcess.HandleTable.GetObject<KReadableEvent>(handle);
  1410. Result result;
  1411. if (readableEvent != null)
  1412. {
  1413. result = readableEvent.ClearIfSignaled();
  1414. }
  1415. else
  1416. {
  1417. KProcess process = currentProcess.HandleTable.GetKProcess(handle);
  1418. if (process != null)
  1419. {
  1420. result = process.ClearIfNotExited();
  1421. }
  1422. else
  1423. {
  1424. result = KernelResult.InvalidHandle;
  1425. }
  1426. }
  1427. return result;
  1428. }
  1429. [Svc(0x1e)]
  1430. public ulong GetSystemTick()
  1431. {
  1432. return _context.TickSource.Counter;
  1433. }
  1434. [Svc(0x26)]
  1435. public void Break(ulong reason)
  1436. {
  1437. KThread currentThread = KernelStatic.GetCurrentThread();
  1438. if ((reason & (1UL << 31)) == 0)
  1439. {
  1440. currentThread.PrintGuestStackTrace();
  1441. currentThread.PrintGuestRegisterPrintout();
  1442. // As the process is exiting, this is probably caused by emulation termination.
  1443. if (currentThread.Owner.State == ProcessState.Exiting)
  1444. {
  1445. return;
  1446. }
  1447. // TODO: Debug events.
  1448. currentThread.Owner.TerminateCurrentProcess();
  1449. throw new GuestBrokeExecutionException();
  1450. }
  1451. else
  1452. {
  1453. Logger.Debug?.Print(LogClass.KernelSvc, "Debugger triggered.");
  1454. }
  1455. }
  1456. [Svc(0x27)]
  1457. public void OutputDebugString([PointerSized] ulong strPtr, [PointerSized] ulong size)
  1458. {
  1459. KProcess process = KernelStatic.GetCurrentProcess();
  1460. string str = MemoryHelper.ReadAsciiString(process.CpuMemory, strPtr, (long)size);
  1461. Logger.Warning?.Print(LogClass.KernelSvc, str);
  1462. }
  1463. [Svc(0x29)]
  1464. public Result GetInfo(out ulong value, InfoType id, int handle, long subId)
  1465. {
  1466. value = 0;
  1467. switch (id)
  1468. {
  1469. case InfoType.CoreMask:
  1470. case InfoType.PriorityMask:
  1471. case InfoType.AliasRegionAddress:
  1472. case InfoType.AliasRegionSize:
  1473. case InfoType.HeapRegionAddress:
  1474. case InfoType.HeapRegionSize:
  1475. case InfoType.TotalMemorySize:
  1476. case InfoType.UsedMemorySize:
  1477. case InfoType.AslrRegionAddress:
  1478. case InfoType.AslrRegionSize:
  1479. case InfoType.StackRegionAddress:
  1480. case InfoType.StackRegionSize:
  1481. case InfoType.SystemResourceSizeTotal:
  1482. case InfoType.SystemResourceSizeUsed:
  1483. case InfoType.ProgramId:
  1484. case InfoType.UserExceptionContextAddress:
  1485. case InfoType.TotalNonSystemMemorySize:
  1486. case InfoType.UsedNonSystemMemorySize:
  1487. case InfoType.IsApplication:
  1488. case InfoType.FreeThreadCount:
  1489. {
  1490. if (subId != 0)
  1491. {
  1492. return KernelResult.InvalidCombination;
  1493. }
  1494. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1495. KProcess process = currentProcess.HandleTable.GetKProcess(handle);
  1496. if (process == null)
  1497. {
  1498. return KernelResult.InvalidHandle;
  1499. }
  1500. switch (id)
  1501. {
  1502. case InfoType.CoreMask: value = process.Capabilities.AllowedCpuCoresMask; break;
  1503. case InfoType.PriorityMask: value = process.Capabilities.AllowedThreadPriosMask; break;
  1504. case InfoType.AliasRegionAddress: value = process.MemoryManager.AliasRegionStart; break;
  1505. case InfoType.AliasRegionSize:
  1506. value = (process.MemoryManager.AliasRegionEnd -
  1507. process.MemoryManager.AliasRegionStart); break;
  1508. case InfoType.HeapRegionAddress: value = process.MemoryManager.HeapRegionStart; break;
  1509. case InfoType.HeapRegionSize:
  1510. value = (process.MemoryManager.HeapRegionEnd -
  1511. process.MemoryManager.HeapRegionStart); break;
  1512. case InfoType.TotalMemorySize: value = process.GetMemoryCapacity(); break;
  1513. case InfoType.UsedMemorySize: value = process.GetMemoryUsage(); break;
  1514. case InfoType.AslrRegionAddress: value = process.MemoryManager.GetAddrSpaceBaseAddr(); break;
  1515. case InfoType.AslrRegionSize: value = process.MemoryManager.GetAddrSpaceSize(); break;
  1516. case InfoType.StackRegionAddress: value = process.MemoryManager.StackRegionStart; break;
  1517. case InfoType.StackRegionSize:
  1518. value = (process.MemoryManager.StackRegionEnd -
  1519. process.MemoryManager.StackRegionStart); break;
  1520. case InfoType.SystemResourceSizeTotal: value = process.PersonalMmHeapPagesCount * KPageTableBase.PageSize; break;
  1521. case InfoType.SystemResourceSizeUsed:
  1522. if (process.PersonalMmHeapPagesCount != 0)
  1523. {
  1524. value = process.MemoryManager.GetMmUsedPages() * KPageTableBase.PageSize;
  1525. }
  1526. break;
  1527. case InfoType.ProgramId: value = process.TitleId; break;
  1528. case InfoType.UserExceptionContextAddress: value = process.UserExceptionContextAddress; break;
  1529. case InfoType.TotalNonSystemMemorySize: value = process.GetMemoryCapacityWithoutPersonalMmHeap(); break;
  1530. case InfoType.UsedNonSystemMemorySize: value = process.GetMemoryUsageWithoutPersonalMmHeap(); break;
  1531. case InfoType.IsApplication: value = process.IsApplication ? 1UL : 0UL; break;
  1532. case InfoType.FreeThreadCount:
  1533. if (process.ResourceLimit != null)
  1534. {
  1535. value = (ulong)(process.ResourceLimit.GetLimitValue(LimitableResource.Thread) -
  1536. process.ResourceLimit.GetCurrentValue(LimitableResource.Thread));
  1537. }
  1538. else
  1539. {
  1540. value = 0;
  1541. }
  1542. break;
  1543. }
  1544. break;
  1545. }
  1546. case InfoType.DebuggerAttached:
  1547. {
  1548. if (handle != 0)
  1549. {
  1550. return KernelResult.InvalidHandle;
  1551. }
  1552. if (subId != 0)
  1553. {
  1554. return KernelResult.InvalidCombination;
  1555. }
  1556. value = KernelStatic.GetCurrentProcess().Debug ? 1UL : 0UL;
  1557. break;
  1558. }
  1559. case InfoType.ResourceLimit:
  1560. {
  1561. if (handle != 0)
  1562. {
  1563. return KernelResult.InvalidHandle;
  1564. }
  1565. if (subId != 0)
  1566. {
  1567. return KernelResult.InvalidCombination;
  1568. }
  1569. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1570. if (currentProcess.ResourceLimit != null)
  1571. {
  1572. KHandleTable handleTable = currentProcess.HandleTable;
  1573. KResourceLimit resourceLimit = currentProcess.ResourceLimit;
  1574. Result result = handleTable.GenerateHandle(resourceLimit, out int resLimHandle);
  1575. if (result != Result.Success)
  1576. {
  1577. return result;
  1578. }
  1579. value = (uint)resLimHandle;
  1580. }
  1581. break;
  1582. }
  1583. case InfoType.IdleTickCount:
  1584. {
  1585. if (handle != 0)
  1586. {
  1587. return KernelResult.InvalidHandle;
  1588. }
  1589. int currentCore = KernelStatic.GetCurrentThread().CurrentCore;
  1590. if (subId != -1 && subId != currentCore)
  1591. {
  1592. return KernelResult.InvalidCombination;
  1593. }
  1594. value = (ulong)KTimeManager.ConvertHostTicksToTicks(_context.Schedulers[currentCore].TotalIdleTimeTicks);
  1595. break;
  1596. }
  1597. case InfoType.RandomEntropy:
  1598. {
  1599. if (handle != 0)
  1600. {
  1601. return KernelResult.InvalidHandle;
  1602. }
  1603. if ((ulong)subId > 3)
  1604. {
  1605. return KernelResult.InvalidCombination;
  1606. }
  1607. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1608. value = currentProcess.RandomEntropy[subId];
  1609. break;
  1610. }
  1611. case InfoType.ThreadTickCount:
  1612. {
  1613. if (subId < -1 || subId > 3)
  1614. {
  1615. return KernelResult.InvalidCombination;
  1616. }
  1617. KThread thread = KernelStatic.GetCurrentProcess().HandleTable.GetKThread(handle);
  1618. if (thread == null)
  1619. {
  1620. return KernelResult.InvalidHandle;
  1621. }
  1622. KThread currentThread = KernelStatic.GetCurrentThread();
  1623. int currentCore = currentThread.CurrentCore;
  1624. if (subId != -1 && subId != currentCore)
  1625. {
  1626. return Result.Success;
  1627. }
  1628. KScheduler scheduler = _context.Schedulers[currentCore];
  1629. long timeDelta = PerformanceCounter.ElapsedTicks - scheduler.LastContextSwitchTime;
  1630. if (subId != -1)
  1631. {
  1632. value = (ulong)KTimeManager.ConvertHostTicksToTicks(timeDelta);
  1633. }
  1634. else
  1635. {
  1636. long totalTimeRunning = thread.TotalTimeRunning;
  1637. if (thread == currentThread)
  1638. {
  1639. totalTimeRunning += timeDelta;
  1640. }
  1641. value = (ulong)KTimeManager.ConvertHostTicksToTicks(totalTimeRunning);
  1642. }
  1643. break;
  1644. }
  1645. case InfoType.MesosphereCurrentProcess:
  1646. {
  1647. if (handle != 0)
  1648. {
  1649. return KernelResult.InvalidHandle;
  1650. }
  1651. if ((ulong)subId != 0)
  1652. {
  1653. return KernelResult.InvalidCombination;
  1654. }
  1655. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1656. KHandleTable handleTable = currentProcess.HandleTable;
  1657. Result result = handleTable.GenerateHandle(currentProcess, out int outHandle);
  1658. if (result != Result.Success)
  1659. {
  1660. return result;
  1661. }
  1662. value = (ulong)outHandle;
  1663. break;
  1664. }
  1665. default: return KernelResult.InvalidEnumValue;
  1666. }
  1667. return Result.Success;
  1668. }
  1669. [Svc(0x45)]
  1670. public Result CreateEvent(out int wEventHandle, out int rEventHandle)
  1671. {
  1672. KEvent Event = new KEvent(_context);
  1673. KProcess process = KernelStatic.GetCurrentProcess();
  1674. Result result = process.HandleTable.GenerateHandle(Event.WritableEvent, out wEventHandle);
  1675. if (result == Result.Success)
  1676. {
  1677. result = process.HandleTable.GenerateHandle(Event.ReadableEvent, out rEventHandle);
  1678. if (result != Result.Success)
  1679. {
  1680. process.HandleTable.CloseHandle(wEventHandle);
  1681. }
  1682. }
  1683. else
  1684. {
  1685. rEventHandle = 0;
  1686. }
  1687. return result;
  1688. }
  1689. [Svc(0x65)]
  1690. public Result GetProcessList(out int count, [PointerSized] ulong address, int maxCount)
  1691. {
  1692. count = 0;
  1693. if ((maxCount >> 28) != 0)
  1694. {
  1695. return KernelResult.MaximumExceeded;
  1696. }
  1697. if (maxCount != 0)
  1698. {
  1699. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1700. ulong copySize = (ulong)maxCount * 8;
  1701. if (address + copySize <= address)
  1702. {
  1703. return KernelResult.InvalidMemState;
  1704. }
  1705. if (currentProcess.MemoryManager.OutsideAddrSpace(address, copySize))
  1706. {
  1707. return KernelResult.InvalidMemState;
  1708. }
  1709. }
  1710. int copyCount = 0;
  1711. lock (_context.Processes)
  1712. {
  1713. foreach (KProcess process in _context.Processes.Values)
  1714. {
  1715. if (copyCount < maxCount)
  1716. {
  1717. if (!KernelTransfer.KernelToUser(address + (ulong)copyCount * 8, process.Pid))
  1718. {
  1719. return KernelResult.UserCopyFailed;
  1720. }
  1721. }
  1722. copyCount++;
  1723. }
  1724. }
  1725. count = copyCount;
  1726. return Result.Success;
  1727. }
  1728. [Svc(0x6f)]
  1729. public Result GetSystemInfo(out long value, uint id, int handle, long subId)
  1730. {
  1731. value = 0;
  1732. if (id > 2)
  1733. {
  1734. return KernelResult.InvalidEnumValue;
  1735. }
  1736. if (handle != 0)
  1737. {
  1738. return KernelResult.InvalidHandle;
  1739. }
  1740. if (id < 2)
  1741. {
  1742. if ((ulong)subId > 3)
  1743. {
  1744. return KernelResult.InvalidCombination;
  1745. }
  1746. KMemoryRegionManager region = _context.MemoryManager.MemoryRegions[subId];
  1747. switch (id)
  1748. {
  1749. // Memory region capacity.
  1750. case 0: value = (long)region.Size; break;
  1751. // Memory region free space.
  1752. case 1:
  1753. {
  1754. ulong freePagesCount = region.GetFreePages();
  1755. value = (long)(freePagesCount * KPageTableBase.PageSize);
  1756. break;
  1757. }
  1758. }
  1759. }
  1760. else /* if (Id == 2) */
  1761. {
  1762. if ((ulong)subId > 1)
  1763. {
  1764. return KernelResult.InvalidCombination;
  1765. }
  1766. switch (subId)
  1767. {
  1768. case 0: value = _context.PrivilegedProcessLowestId; break;
  1769. case 1: value = _context.PrivilegedProcessHighestId; break;
  1770. }
  1771. }
  1772. return Result.Success;
  1773. }
  1774. [Svc(0x30)]
  1775. public Result GetResourceLimitLimitValue(out long limitValue, int handle, LimitableResource resource)
  1776. {
  1777. limitValue = 0;
  1778. if (resource >= LimitableResource.Count)
  1779. {
  1780. return KernelResult.InvalidEnumValue;
  1781. }
  1782. KResourceLimit resourceLimit = KernelStatic.GetCurrentProcess().HandleTable.GetObject<KResourceLimit>(handle);
  1783. if (resourceLimit == null)
  1784. {
  1785. return KernelResult.InvalidHandle;
  1786. }
  1787. limitValue = resourceLimit.GetLimitValue(resource);
  1788. return Result.Success;
  1789. }
  1790. [Svc(0x31)]
  1791. public Result GetResourceLimitCurrentValue(out long limitValue, int handle, LimitableResource resource)
  1792. {
  1793. limitValue = 0;
  1794. if (resource >= LimitableResource.Count)
  1795. {
  1796. return KernelResult.InvalidEnumValue;
  1797. }
  1798. KResourceLimit resourceLimit = KernelStatic.GetCurrentProcess().HandleTable.GetObject<KResourceLimit>(handle);
  1799. if (resourceLimit == null)
  1800. {
  1801. return KernelResult.InvalidHandle;
  1802. }
  1803. limitValue = resourceLimit.GetCurrentValue(resource);
  1804. return Result.Success;
  1805. }
  1806. [Svc(0x37)]
  1807. public Result GetResourceLimitPeakValue(out long peak, int handle, LimitableResource resource)
  1808. {
  1809. peak = 0;
  1810. if (resource >= LimitableResource.Count)
  1811. {
  1812. return KernelResult.InvalidEnumValue;
  1813. }
  1814. KResourceLimit resourceLimit = KernelStatic.GetCurrentProcess().HandleTable.GetObject<KResourceLimit>(handle);
  1815. if (resourceLimit == null)
  1816. {
  1817. return KernelResult.InvalidHandle;
  1818. }
  1819. peak = resourceLimit.GetPeakValue(resource);
  1820. return Result.Success;
  1821. }
  1822. [Svc(0x7d)]
  1823. public Result CreateResourceLimit(out int handle)
  1824. {
  1825. KResourceLimit limit = new KResourceLimit(_context);
  1826. KProcess process = KernelStatic.GetCurrentProcess();
  1827. return process.HandleTable.GenerateHandle(limit, out handle);
  1828. }
  1829. [Svc(0x7e)]
  1830. public Result SetResourceLimitLimitValue(int handle, LimitableResource resource, long limitValue)
  1831. {
  1832. if (resource >= LimitableResource.Count)
  1833. {
  1834. return KernelResult.InvalidEnumValue;
  1835. }
  1836. KResourceLimit resourceLimit = KernelStatic.GetCurrentProcess().HandleTable.GetObject<KResourceLimit>(handle);
  1837. if (resourceLimit == null)
  1838. {
  1839. return KernelResult.InvalidHandle;
  1840. }
  1841. return resourceLimit.SetLimitValue(resource, limitValue);
  1842. }
  1843. // Thread
  1844. [Svc(8)]
  1845. public Result CreateThread(
  1846. out int handle,
  1847. [PointerSized] ulong entrypoint,
  1848. [PointerSized] ulong argsPtr,
  1849. [PointerSized] ulong stackTop,
  1850. int priority,
  1851. int cpuCore)
  1852. {
  1853. return CreateThread(out handle, entrypoint, argsPtr, stackTop, priority, cpuCore, null);
  1854. }
  1855. public Result CreateThread(
  1856. out int handle,
  1857. ulong entrypoint,
  1858. ulong argsPtr,
  1859. ulong stackTop,
  1860. int priority,
  1861. int cpuCore,
  1862. ThreadStart customThreadStart)
  1863. {
  1864. handle = 0;
  1865. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  1866. if (cpuCore == -2)
  1867. {
  1868. cpuCore = currentProcess.DefaultCpuCore;
  1869. }
  1870. if ((uint)cpuCore >= KScheduler.CpuCoresCount || !currentProcess.IsCpuCoreAllowed(cpuCore))
  1871. {
  1872. return KernelResult.InvalidCpuCore;
  1873. }
  1874. if ((uint)priority >= KScheduler.PrioritiesCount || !currentProcess.IsPriorityAllowed(priority))
  1875. {
  1876. return KernelResult.InvalidPriority;
  1877. }
  1878. long timeout = KTimeManager.ConvertMillisecondsToNanoseconds(100);
  1879. if (currentProcess.ResourceLimit != null &&
  1880. !currentProcess.ResourceLimit.Reserve(LimitableResource.Thread, 1, timeout))
  1881. {
  1882. return KernelResult.ResLimitExceeded;
  1883. }
  1884. KThread thread = new KThread(_context);
  1885. Result result = currentProcess.InitializeThread(
  1886. thread,
  1887. entrypoint,
  1888. argsPtr,
  1889. stackTop,
  1890. priority,
  1891. cpuCore,
  1892. customThreadStart);
  1893. if (result == Result.Success)
  1894. {
  1895. KProcess process = KernelStatic.GetCurrentProcess();
  1896. result = process.HandleTable.GenerateHandle(thread, out handle);
  1897. }
  1898. else
  1899. {
  1900. currentProcess.ResourceLimit?.Release(LimitableResource.Thread, 1);
  1901. }
  1902. thread.DecrementReferenceCount();
  1903. return result;
  1904. }
  1905. [Svc(9)]
  1906. public Result StartThread(int handle)
  1907. {
  1908. KProcess process = KernelStatic.GetCurrentProcess();
  1909. KThread thread = process.HandleTable.GetKThread(handle);
  1910. if (thread != null)
  1911. {
  1912. thread.IncrementReferenceCount();
  1913. Result result = thread.Start();
  1914. if (result == Result.Success)
  1915. {
  1916. thread.IncrementReferenceCount();
  1917. }
  1918. thread.DecrementReferenceCount();
  1919. return result;
  1920. }
  1921. else
  1922. {
  1923. return KernelResult.InvalidHandle;
  1924. }
  1925. }
  1926. [Svc(0xa)]
  1927. public void ExitThread()
  1928. {
  1929. KThread currentThread = KernelStatic.GetCurrentThread();
  1930. currentThread.Exit();
  1931. }
  1932. [Svc(0xb)]
  1933. public void SleepThread(long timeout)
  1934. {
  1935. if (timeout < 1)
  1936. {
  1937. switch (timeout)
  1938. {
  1939. case 0: KScheduler.Yield(_context); break;
  1940. case -1: KScheduler.YieldWithLoadBalancing(_context); break;
  1941. case -2: KScheduler.YieldToAnyThread(_context); break;
  1942. }
  1943. }
  1944. else
  1945. {
  1946. KernelStatic.GetCurrentThread().Sleep(timeout + KTimeManager.DefaultTimeIncrementNanoseconds);
  1947. }
  1948. }
  1949. [Svc(0xc)]
  1950. public Result GetThreadPriority(out int priority, int handle)
  1951. {
  1952. KProcess process = KernelStatic.GetCurrentProcess();
  1953. KThread thread = process.HandleTable.GetKThread(handle);
  1954. if (thread != null)
  1955. {
  1956. priority = thread.DynamicPriority;
  1957. return Result.Success;
  1958. }
  1959. else
  1960. {
  1961. priority = 0;
  1962. return KernelResult.InvalidHandle;
  1963. }
  1964. }
  1965. [Svc(0xd)]
  1966. public Result SetThreadPriority(int handle, int priority)
  1967. {
  1968. // TODO: NPDM check.
  1969. KProcess process = KernelStatic.GetCurrentProcess();
  1970. KThread thread = process.HandleTable.GetKThread(handle);
  1971. if (thread == null)
  1972. {
  1973. return KernelResult.InvalidHandle;
  1974. }
  1975. thread.SetPriority(priority);
  1976. return Result.Success;
  1977. }
  1978. [Svc(0xe)]
  1979. public Result GetThreadCoreMask(out int preferredCore, out ulong affinityMask, int handle)
  1980. {
  1981. KProcess process = KernelStatic.GetCurrentProcess();
  1982. KThread thread = process.HandleTable.GetKThread(handle);
  1983. if (thread != null)
  1984. {
  1985. preferredCore = thread.PreferredCore;
  1986. affinityMask = thread.AffinityMask;
  1987. return Result.Success;
  1988. }
  1989. else
  1990. {
  1991. preferredCore = 0;
  1992. affinityMask = 0;
  1993. return KernelResult.InvalidHandle;
  1994. }
  1995. }
  1996. [Svc(0xf)]
  1997. public Result SetThreadCoreMask(int handle, int preferredCore, ulong affinityMask)
  1998. {
  1999. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  2000. if (preferredCore == -2)
  2001. {
  2002. preferredCore = currentProcess.DefaultCpuCore;
  2003. affinityMask = 1UL << preferredCore;
  2004. }
  2005. else
  2006. {
  2007. if ((currentProcess.Capabilities.AllowedCpuCoresMask | affinityMask) !=
  2008. currentProcess.Capabilities.AllowedCpuCoresMask)
  2009. {
  2010. return KernelResult.InvalidCpuCore;
  2011. }
  2012. if (affinityMask == 0)
  2013. {
  2014. return KernelResult.InvalidCombination;
  2015. }
  2016. if ((uint)preferredCore > 3)
  2017. {
  2018. if ((preferredCore | 2) != -1)
  2019. {
  2020. return KernelResult.InvalidCpuCore;
  2021. }
  2022. }
  2023. else if ((affinityMask & (1UL << preferredCore)) == 0)
  2024. {
  2025. return KernelResult.InvalidCombination;
  2026. }
  2027. }
  2028. KProcess process = KernelStatic.GetCurrentProcess();
  2029. KThread thread = process.HandleTable.GetKThread(handle);
  2030. if (thread == null)
  2031. {
  2032. return KernelResult.InvalidHandle;
  2033. }
  2034. return thread.SetCoreAndAffinityMask(preferredCore, affinityMask);
  2035. }
  2036. [Svc(0x10)]
  2037. public int GetCurrentProcessorNumber()
  2038. {
  2039. return KernelStatic.GetCurrentThread().CurrentCore;
  2040. }
  2041. [Svc(0x25)]
  2042. public Result GetThreadId(out ulong threadUid, int handle)
  2043. {
  2044. KProcess process = KernelStatic.GetCurrentProcess();
  2045. KThread thread = process.HandleTable.GetKThread(handle);
  2046. if (thread != null)
  2047. {
  2048. threadUid = thread.ThreadUid;
  2049. return Result.Success;
  2050. }
  2051. else
  2052. {
  2053. threadUid = 0;
  2054. return KernelResult.InvalidHandle;
  2055. }
  2056. }
  2057. [Svc(0x32)]
  2058. public Result SetThreadActivity(int handle, bool pause)
  2059. {
  2060. KProcess process = KernelStatic.GetCurrentProcess();
  2061. KThread thread = process.HandleTable.GetObject<KThread>(handle);
  2062. if (thread == null)
  2063. {
  2064. return KernelResult.InvalidHandle;
  2065. }
  2066. if (thread.Owner != process)
  2067. {
  2068. return KernelResult.InvalidHandle;
  2069. }
  2070. if (thread == KernelStatic.GetCurrentThread())
  2071. {
  2072. return KernelResult.InvalidThread;
  2073. }
  2074. return thread.SetActivity(pause);
  2075. }
  2076. [Svc(0x33)]
  2077. public Result GetThreadContext3([PointerSized] ulong address, int handle)
  2078. {
  2079. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  2080. KThread currentThread = KernelStatic.GetCurrentThread();
  2081. KThread thread = currentProcess.HandleTable.GetObject<KThread>(handle);
  2082. if (thread == null)
  2083. {
  2084. return KernelResult.InvalidHandle;
  2085. }
  2086. if (thread.Owner != currentProcess)
  2087. {
  2088. return KernelResult.InvalidHandle;
  2089. }
  2090. if (currentThread == thread)
  2091. {
  2092. return KernelResult.InvalidThread;
  2093. }
  2094. Result result = thread.GetThreadContext3(out ThreadContext context);
  2095. if (result == Result.Success)
  2096. {
  2097. return KernelTransfer.KernelToUser(address, context)
  2098. ? Result.Success
  2099. : KernelResult.InvalidMemState;
  2100. }
  2101. return result;
  2102. }
  2103. // Thread synchronization
  2104. [Svc(0x18)]
  2105. public Result WaitSynchronization(out int handleIndex, [PointerSized] ulong handlesPtr, int handlesCount, long timeout)
  2106. {
  2107. handleIndex = 0;
  2108. if ((uint)handlesCount > KThread.MaxWaitSyncObjects)
  2109. {
  2110. return KernelResult.MaximumExceeded;
  2111. }
  2112. KThread currentThread = KernelStatic.GetCurrentThread();
  2113. if (handlesCount != 0)
  2114. {
  2115. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  2116. if (currentProcess.MemoryManager.AddrSpaceStart > handlesPtr)
  2117. {
  2118. return KernelResult.UserCopyFailed;
  2119. }
  2120. long handlesSize = handlesCount * 4;
  2121. if (handlesPtr + (ulong)handlesSize <= handlesPtr)
  2122. {
  2123. return KernelResult.UserCopyFailed;
  2124. }
  2125. if (handlesPtr + (ulong)handlesSize - 1 > currentProcess.MemoryManager.AddrSpaceEnd - 1)
  2126. {
  2127. return KernelResult.UserCopyFailed;
  2128. }
  2129. Span<int> handles = new Span<int>(currentThread.WaitSyncHandles).Slice(0, handlesCount);
  2130. if (!KernelTransfer.UserToKernelArray(handlesPtr, handles))
  2131. {
  2132. return KernelResult.UserCopyFailed;
  2133. }
  2134. return WaitSynchronization(out handleIndex, handles, timeout);
  2135. }
  2136. return WaitSynchronization(out handleIndex, ReadOnlySpan<int>.Empty, timeout);
  2137. }
  2138. public Result WaitSynchronization(out int handleIndex, ReadOnlySpan<int> handles, long timeout)
  2139. {
  2140. handleIndex = 0;
  2141. if ((uint)handles.Length > KThread.MaxWaitSyncObjects)
  2142. {
  2143. return KernelResult.MaximumExceeded;
  2144. }
  2145. KThread currentThread = KernelStatic.GetCurrentThread();
  2146. var syncObjs = new Span<KSynchronizationObject>(currentThread.WaitSyncObjects).Slice(0, handles.Length);
  2147. if (handles.Length != 0)
  2148. {
  2149. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  2150. int processedHandles = 0;
  2151. for (; processedHandles < handles.Length; processedHandles++)
  2152. {
  2153. KSynchronizationObject syncObj = currentProcess.HandleTable.GetObject<KSynchronizationObject>(handles[processedHandles]);
  2154. if (syncObj == null)
  2155. {
  2156. break;
  2157. }
  2158. syncObjs[processedHandles] = syncObj;
  2159. syncObj.IncrementReferenceCount();
  2160. }
  2161. if (processedHandles != handles.Length)
  2162. {
  2163. // One or more handles are invalid.
  2164. for (int index = 0; index < processedHandles; index++)
  2165. {
  2166. currentThread.WaitSyncObjects[index].DecrementReferenceCount();
  2167. }
  2168. return KernelResult.InvalidHandle;
  2169. }
  2170. }
  2171. if (timeout > 0)
  2172. {
  2173. timeout += KTimeManager.DefaultTimeIncrementNanoseconds;
  2174. }
  2175. Result result = _context.Synchronization.WaitFor(syncObjs, timeout, out handleIndex);
  2176. if (result == KernelResult.PortRemoteClosed)
  2177. {
  2178. result = Result.Success;
  2179. }
  2180. for (int index = 0; index < handles.Length; index++)
  2181. {
  2182. currentThread.WaitSyncObjects[index].DecrementReferenceCount();
  2183. }
  2184. return result;
  2185. }
  2186. [Svc(0x19)]
  2187. public Result CancelSynchronization(int handle)
  2188. {
  2189. KProcess process = KernelStatic.GetCurrentProcess();
  2190. KThread thread = process.HandleTable.GetKThread(handle);
  2191. if (thread == null)
  2192. {
  2193. return KernelResult.InvalidHandle;
  2194. }
  2195. thread.CancelSynchronization();
  2196. return Result.Success;
  2197. }
  2198. [Svc(0x1a)]
  2199. public Result ArbitrateLock(int ownerHandle, [PointerSized] ulong mutexAddress, int requesterHandle)
  2200. {
  2201. if (IsPointingInsideKernel(mutexAddress))
  2202. {
  2203. return KernelResult.InvalidMemState;
  2204. }
  2205. if (IsAddressNotWordAligned(mutexAddress))
  2206. {
  2207. return KernelResult.InvalidAddress;
  2208. }
  2209. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  2210. return currentProcess.AddressArbiter.ArbitrateLock(ownerHandle, mutexAddress, requesterHandle);
  2211. }
  2212. [Svc(0x1b)]
  2213. public Result ArbitrateUnlock([PointerSized] ulong mutexAddress)
  2214. {
  2215. if (IsPointingInsideKernel(mutexAddress))
  2216. {
  2217. return KernelResult.InvalidMemState;
  2218. }
  2219. if (IsAddressNotWordAligned(mutexAddress))
  2220. {
  2221. return KernelResult.InvalidAddress;
  2222. }
  2223. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  2224. return currentProcess.AddressArbiter.ArbitrateUnlock(mutexAddress);
  2225. }
  2226. [Svc(0x1c)]
  2227. public Result WaitProcessWideKeyAtomic(
  2228. [PointerSized] ulong mutexAddress,
  2229. [PointerSized] ulong condVarAddress,
  2230. int handle,
  2231. long timeout)
  2232. {
  2233. if (IsPointingInsideKernel(mutexAddress))
  2234. {
  2235. return KernelResult.InvalidMemState;
  2236. }
  2237. if (IsAddressNotWordAligned(mutexAddress))
  2238. {
  2239. return KernelResult.InvalidAddress;
  2240. }
  2241. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  2242. if (timeout > 0)
  2243. {
  2244. timeout += KTimeManager.DefaultTimeIncrementNanoseconds;
  2245. }
  2246. return currentProcess.AddressArbiter.WaitProcessWideKeyAtomic(
  2247. mutexAddress,
  2248. condVarAddress,
  2249. handle,
  2250. timeout);
  2251. }
  2252. [Svc(0x1d)]
  2253. public Result SignalProcessWideKey([PointerSized] ulong address, int count)
  2254. {
  2255. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  2256. currentProcess.AddressArbiter.SignalProcessWideKey(address, count);
  2257. return Result.Success;
  2258. }
  2259. [Svc(0x34)]
  2260. public Result WaitForAddress([PointerSized] ulong address, ArbitrationType type, int value, long timeout)
  2261. {
  2262. if (IsPointingInsideKernel(address))
  2263. {
  2264. return KernelResult.InvalidMemState;
  2265. }
  2266. if (IsAddressNotWordAligned(address))
  2267. {
  2268. return KernelResult.InvalidAddress;
  2269. }
  2270. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  2271. if (timeout > 0)
  2272. {
  2273. timeout += KTimeManager.DefaultTimeIncrementNanoseconds;
  2274. }
  2275. return type switch
  2276. {
  2277. ArbitrationType.WaitIfLessThan
  2278. => currentProcess.AddressArbiter.WaitForAddressIfLessThan(address, value, false, timeout),
  2279. ArbitrationType.DecrementAndWaitIfLessThan
  2280. => currentProcess.AddressArbiter.WaitForAddressIfLessThan(address, value, true, timeout),
  2281. ArbitrationType.WaitIfEqual
  2282. => currentProcess.AddressArbiter.WaitForAddressIfEqual(address, value, timeout),
  2283. _ => KernelResult.InvalidEnumValue,
  2284. };
  2285. }
  2286. [Svc(0x35)]
  2287. public Result SignalToAddress([PointerSized] ulong address, SignalType type, int value, int count)
  2288. {
  2289. if (IsPointingInsideKernel(address))
  2290. {
  2291. return KernelResult.InvalidMemState;
  2292. }
  2293. if (IsAddressNotWordAligned(address))
  2294. {
  2295. return KernelResult.InvalidAddress;
  2296. }
  2297. KProcess currentProcess = KernelStatic.GetCurrentProcess();
  2298. return type switch
  2299. {
  2300. SignalType.Signal
  2301. => currentProcess.AddressArbiter.Signal(address, count),
  2302. SignalType.SignalAndIncrementIfEqual
  2303. => currentProcess.AddressArbiter.SignalAndIncrementIfEqual(address, value, count),
  2304. SignalType.SignalAndModifyIfEqual
  2305. => currentProcess.AddressArbiter.SignalAndModifyIfEqual(address, value, count),
  2306. _ => KernelResult.InvalidEnumValue
  2307. };
  2308. }
  2309. [Svc(0x36)]
  2310. public Result SynchronizePreemptionState()
  2311. {
  2312. KernelStatic.GetCurrentThread().SynchronizePreemptionState();
  2313. return Result.Success;
  2314. }
  2315. private static bool IsPointingInsideKernel(ulong address)
  2316. {
  2317. return (address + 0x1000000000) < 0xffffff000;
  2318. }
  2319. private static bool IsAddressNotWordAligned(ulong address)
  2320. {
  2321. return (address & 3) != 0;
  2322. }
  2323. }
  2324. }