Syscall.cs 93 KB

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