Compare commits

...

178 Commits

Author SHA1 Message Date
Michael Scire
997e4dd665 git subrepo push libraries
subrepo:
  subdir:   "libraries"
  merged:   "86c2eec8"
upstream:
  origin:   "https://github.com/Atmosphere-NX/Atmosphere-libs"
  branch:   "master"
  commit:   "86c2eec8"
git-subrepo:
  version:  "0.4.1"
  origin:   "???"
  commit:   "???"
2021-04-12 17:14:38 -07:00
Michael Scire
aa2d03d8e1 ams: bump to 0.19.1 2021-04-12 17:14:07 -07:00
Michael Scire
274c1deae4 git subrepo push libraries
subrepo:
  subdir:   "libraries"
  merged:   "68ddbc73"
upstream:
  origin:   "https://github.com/Atmosphere-NX/Atmosphere-libs"
  branch:   "master"
  commit:   "68ddbc73"
git-subrepo:
  version:  "0.4.1"
  origin:   "???"
  commit:   "???"
2021-04-12 17:13:43 -07:00
Michael Scire
d5bbf32a26 docs: add 0.19.1 changelog 2021-04-12 17:13:12 -07:00
Michael Scire
97a251b4b2 ncm: fix iteration of nested-subdirectory content files 2021-04-12 17:10:27 -07:00
Michael Scire
9d30917f4e boot2: migration also not launched in 12.0.0 2021-04-11 10:37:04 -07:00
Michael Scire
c67c29ebd5 ncm: fix random error when deleting content 2021-04-11 05:44:13 -07:00
Michael Scire
88dd414721 git subrepo push libraries
subrepo:
  subdir:   "libraries"
  merged:   "44279dbac"
upstream:
  origin:   "https://github.com/Atmosphere-NX/Atmosphere-libs"
  branch:   "master"
  commit:   "44279dbac"
git-subrepo:
  version:  "0.4.1"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "a04d8c2"
2021-04-11 04:02:17 -07:00
Michael Scire
0f6f13a1ac git subrepo push emummc
subrepo:
  subdir:   "emummc"
  merged:   "b355ee6a8"
upstream:
  origin:   "https://github.com/m4xw/emuMMC"
  branch:   "develop"
  commit:   "b355ee6a8"
git-subrepo:
  version:  "0.4.1"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "a04d8c2"
2021-04-11 03:58:13 -07:00
Michael Scire
b24784f5c1 git subrepo pull emummc
subrepo:
  subdir:   "emummc"
  merged:   "38ed90b6e"
upstream:
  origin:   "https://github.com/m4xw/emuMMC"
  branch:   "develop"
  commit:   "5a29e1b2c"
git-subrepo:
  version:  "0.4.1"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "a04d8c2"
2021-04-11 03:51:53 -07:00
Michael Scire
3b3082cf58 changelog: add note about extensions needing recompile 2021-04-11 03:42:16 -07:00
Michael Scire
38a2fdcd76 docs: add changelog for 0.19.0 2021-04-11 03:42:16 -07:00
Michael Scire
0ca2f962de usb: add usb3.0 patches for 12.0.0 2021-04-11 03:42:16 -07:00
Michael Scire
03a98635d8 fs: add (stubbed) OperateRangeWithBuffer implementation 2021-04-11 03:42:16 -07:00
Michael Scire
461e2ced6f erpt: add (stubbed, TODO after 0.19.0) support for 12.0.0 2021-04-11 03:42:16 -07:00
Michael Scire
b2d2f65b87 boot2: grc is no longer launched by boot2 on 12.0.0+ 2021-04-11 03:42:16 -07:00
Michael Scire
b2b0c50802 pgl: update to use tipc (untested) 2021-04-11 03:42:16 -07:00
Michael Scire
1118421fa6 strat: changes for sm tipc (boots 11.0.1, now) 2021-04-11 03:42:16 -07:00
Michael Scire
b1b3914ccf tipc: Result is first raw data word, not last 2021-04-11 03:42:16 -07:00
Michael Scire
9be8b32311 tipc/sm: various fixes for issues 2021-04-11 03:42:16 -07:00
Michael Scire
57c8bc432d sm: reimplement using tipc instead of cmif (probably broken, untested) 2021-04-11 03:42:16 -07:00
Michael Scire
58776f5ba8 tipc: server processor fixes (compiles!) 2021-04-11 03:42:16 -07:00
Michael Scire
affeeb2724 tipc: implement ServerManager processing logic 2021-04-11 03:42:16 -07:00
Michael Scire
822875ecf5 tipc: implement framework/server support logic (except for actual processing) 2021-04-11 03:42:16 -07:00
Michael Scire
e3a65b1405 tipc: fix deserialization of buffers 2021-04-11 03:42:16 -07:00
Michael Scire
20a7fa1588 tipc: hard-enforce boolean constraints in command processing generation 2021-04-11 03:42:16 -07:00
Michael Scire
315b7bdf22 tipc: implement service object interface generation 2021-04-11 03:42:16 -07:00
Michael Scire
ec988c5a99 tipc: first draft object allocation logic 2021-04-11 03:42:16 -07:00
Michael Scire
21b883a75c tipc: fix compilation issues with core serialization routines 2021-04-11 03:42:16 -07:00
Michael Scire
e93d71d932 tipc: tentative core serialization logic (missing imports, won't compile) 2021-04-11 03:42:16 -07:00
Michael Scire
dc6a0d7562 ams: add target firmware 12.0.0, fusee recognition 2021-04-11 03:42:16 -07:00
Michael Scire
1d2be0a2eb kern: mesosphere now implements kernel/sdk 12.3 2021-04-11 03:42:16 -07:00
Michael Scire
96937a611d kern: fuck the KPolice^H^H^H^H^H^HPageGroups 2021-04-11 03:42:16 -07:00
Michael Scire
dc7862882f kern: who needs __purecall? 2021-04-11 03:42:16 -07:00
Michael Scire
6faa3534bf kern: update pinning semantics for terminating threads 2021-04-11 03:42:16 -07:00
Michael Scire
afb1d68d06 kern: ensure handle table is finalized when deferring termination 2021-04-11 03:42:16 -07:00
Michael Scire
911e431d65 kern: simplify handle table registration for port/session 2021-04-11 03:42:16 -07:00
Michael Scire
ee91063bbb kern: update kdebug process management semantics 2021-04-11 03:42:16 -07:00
Michael Scire
cbdf33260e kern: update port/session state semantics 2021-04-11 03:42:16 -07:00
Michael Scire
c62a7381f8 kern: update KLightConditionVariable 2021-04-11 03:42:16 -07:00
Michael Scire
b4498734e4 kern: optimize KHandleTable to use indices instead of pointers 2021-04-11 03:42:16 -07:00
Michael Scire
4407237f5b kern: KAutoObject destruction is now scheduled for next dpc-time 2021-04-11 03:42:16 -07:00
Michael Scire
15956fcf9a kern: update for new slab resource counts/extents 2021-04-11 03:42:16 -07:00
Michael Scire
6a368d3d1a kern: reallocate pool distributions for 8GB units 2021-04-11 03:42:16 -07:00
Michael Scire
8e4be9aef9 kern: simplify global rng initialization 2021-04-11 03:42:16 -07:00
Michael Scire
0f8b7be2d2 kern: load initial process binary from user pool, rather than from pt heap 2021-04-11 03:42:16 -07:00
Michael Scire
a1e137cc1c kern: update Initialize0 for new changes 2021-04-11 03:42:16 -07:00
Michael Scire
504472af4e kern: update KConditionVariable::WaitForAddress/Wait 2021-04-11 03:42:16 -07:00
Michael Scire
19b253fd17 kern: trivially optimize userspace io memory write 2021-04-11 03:42:16 -07:00
Michael Scire
01f5c89902 kern: add bounds checking to KHandleTable::Register/Unreserve 2021-04-11 03:42:16 -07:00
Michael Scire
44ccbc2a7b kern: update set/way cache operations for new semantics 2021-04-11 03:42:16 -07:00
Michael Scire
6e4664ee05 kern: if a page table region is zero-size, nothing overlaps it 2021-04-11 03:42:16 -07:00
Michael Scire
85f9355184 kern: use KScopedLightLockPair helper for page table pair-locks 2021-04-11 03:42:16 -07:00
Michael Scire
60b5bd73b7 kern: track mapped ipc server memory in page table 2021-04-11 03:42:16 -07:00
Michael Scire
53e7aa0a20 kern: add KPageTableBase::Read/WriteDebugIoMemory 2021-04-11 03:42:16 -07:00
Michael Scire
561a16a348 kern: flush memory before reading in KPageTableBase::ReadDebugMemory 2021-04-11 03:42:16 -07:00
Michael Scire
1fce7b08b1 kern: update KMemoryBlockManagerUpdaterAllocator for new ctor/init semantics 2021-04-11 03:42:16 -07:00
Michael Scire
c216f92a91 kern: swap tpidr_el1/cntv_cval_el0 as scratch vs exception stack 2021-04-11 03:42:16 -07:00
Michael Scire
2f930c2d5f kern: support immortal processes 2021-04-11 03:42:16 -07:00
Michael Scire
256eb92f4c kern: update process/thread for new running/termination semantics 2021-04-11 03:42:16 -07:00
Michael Scire
ec1d9c4c49 kern: unconditionally set thread state when appropriate 2021-04-11 03:42:16 -07:00
Michael Scire
3356eddcba kern: update kernel waiter management rules 2021-04-11 03:42:16 -07:00
Michael Scire
f67d1b7026 kern: update KInterruptEvent to store core id 2021-04-11 03:42:16 -07:00
Michael Scire
e64fef109c kern: update pinned thread priority rules 2021-04-11 03:42:16 -07:00
Michael Scire
1b2cf173b3 kern: add new checks to SetThreadPriority/CoreMask 2021-04-11 03:42:16 -07:00
Michael Scire
2fb258ca7e kern: update KInitialPageTable/KInitialPageAllocator 2021-04-11 03:42:16 -07:00
Michael Scire
962cf97150 kern: KLinkedList no longer exists 2021-04-11 03:42:16 -07:00
Michael Scire
b3bd443636 svc: sanitize booleans in autogenerated abi stubs 2021-04-11 03:42:16 -07:00
Michael Scire
4b9e7c7d27 kern: bump svc limit to 192 from 128 2021-04-11 03:42:16 -07:00
hexkyz
46612156f4 exo: add new dram ID 2021-04-11 03:42:16 -07:00
Michael Scire
279bb863df fusee: add support for 12.0.0 kernel 2021-04-11 03:42:16 -07:00
Michael Scire
252ea97ca5 nogc: add patches for 12.0.0 2021-04-11 03:42:16 -07:00
Michael Scire
f89cfe41da emummc: update for 12.0.0 2021-04-11 03:42:16 -07:00
Easy World
e3e3679cfe Update dmnt_cheat_vm.cpp
* fix case fallthrough in function "LogOpcode"
* use same attribute name in one opcode case
2021-04-07 18:46:23 -07:00
Michael Scire
75a2052144 ncm: fix GameCardStorageRoot mount point (closes #1404) 2021-03-24 07:17:03 -07:00
Adubbz
5666c59657 ncm: Updated ListContentId for 11.0.0 2021-03-22 14:58:28 -07:00
Michael Scire
c99ce36d7d ams: convert to util::ConstructAt where appropriate 2021-03-21 20:36:49 -07:00
Michael Scire
d84dcb653d ams: prefer construct_at/destroy_at over placement new/explicit destructor 2021-03-21 20:30:40 -07:00
Michael Scire
aff0da9427 ams: remove TYPED_STORAGE() macro in favor of template 2021-03-21 18:47:30 -07:00
Michael Scire
8d9174b227 ams: bump version to 0.19.0.
Release (probably) not actually imminent, I just don't want to forget.
2021-03-21 13:16:49 -07:00
Michael Scire
c8404e8452 boot2: clean up pre-0.19.0 ams contents on upgrade 2021-03-21 13:16:30 -07:00
Michael Scire
79e4c82d7e ams: distribute sysmodules in single file as stratosphere.romfs 2021-03-21 13:16:30 -07:00
Adubbz
3afd9a737c daybreak: Added a warning when resetting to factory settings 2021-03-18 21:08:22 -07:00
Michael Scire
a7564cf303 kern: add extension InfoType for retrieving current process handle. 2021-03-17 17:48:30 -07:00
SciresM
5362ee9450 [tma2] [Ongoing] Continue implementing modules for tma2. (#1388)
* cs: add stub sysmodule to host command shell server

* cs: implement logic for main (linker error paradise, for now)

* cs: implement more of the system module's skeleton

* htcs: update client type names for libnx pr merge
2021-03-16 17:13:30 -07:00
Michael Scire
021d4c88fa kern: use fix usage of incorrect page table for UserBuffer ipc 2021-03-13 15:14:36 -08:00
Michael Scire
deb4aece9a kern: fix inverted conditional in KDebugBase::SetThreadContext 2021-03-11 12:53:43 -08:00
SciresM
a6729171d3 set.mitm: fake compatibility for usb!usb30_force_enabled on 9.0.0+ (#1391)
* set.mitm: fake compatibility for usb!usb30_force_enabled on 9.0.0+

* set.mitm: add value meaning comment for usb!usb30_force_enabled

* loader: pretend to be polite about patch ordering
2021-03-01 14:18:27 -08:00
Michael Scire
c9015581ca boot2: fix tma launch when htc is disabled 2021-02-26 08:08:05 -08:00
Michael Scire
35c816d62f htclow: fix ordering of channels, uninitialized bug in service json parse 2021-02-26 04:49:20 -08:00
Michael Scire
18031ae107 tio: fix wrong body size on optimized ListDirectory 2021-02-26 04:49:20 -08:00
Michael Scire
c7e4f963e8 fs: fix GetFileTimeStampRawForDebug 2021-02-26 04:49:20 -08:00
Michael Scire
97875c7d2f tio: fix bug in body receive 2021-02-26 04:49:20 -08:00
Michael Scire
0da3b2b273 tio: implement SdCardObserver (finishes sysmodule) 2021-02-26 04:49:20 -08:00
Michael Scire
3cbd99a709 tio: implement all command processor logic 2021-02-26 04:49:20 -08:00
Michael Scire
6ce2076d92 tio: implement server/dispatch logic. 2021-02-26 04:49:20 -08:00
Michael Scire
cee1ecd06f tio: add stub sysmodule to host target io server 2021-02-26 04:49:20 -08:00
Michael Scire
a739e3fb20 docs: remove deprecated hid mitm from settings template 2021-02-24 04:08:15 -08:00
Michael Scire
953246a175 htc: disable socket driver, needs design thought before we can turn it on for real. 2021-02-24 04:06:54 -08:00
Michael Scire
d8faa37de0 socket: fix config size calculations 2021-02-24 04:06:54 -08:00
Michael Scire
64c7c6b2a5 ams: implement socket api for htclow socket driver 2021-02-24 04:06:54 -08:00
Michael Scire
1c974a387c htc: implement socket driver (socket api not really impl'd yet) 2021-02-24 04:06:54 -08:00
Michael Scire
b5ab491603 htc: implement htcmisc service object commands 2021-02-24 04:06:54 -08:00
Michael Scire
8b32b9eadf kern: Increase reserved system memory, require mesosphere for htc/tma 2021-02-24 04:06:54 -08:00
Michael Scire
ce149f996c htc: configure usage via system setting 2021-02-24 04:06:54 -08:00
Michael Scire
0ec54ed492 htcs: fixes, echo server is now fully functional 2021-02-24 04:06:54 -08:00
Michael Scire
72de4d85f3 htcs: implement remaining client bindings 2021-02-24 04:06:54 -08:00
Michael Scire
d0673aa2fb htcs: implement client socket bindings 2021-02-24 04:06:54 -08:00
Michael Scire
f7fcb54622 htcs: implement virtual socket collection 2021-02-24 04:06:54 -08:00
Michael Scire
ec643789ab htcs: implement data channel manager 2021-02-24 04:06:54 -08:00
Michael Scire
70caadafd5 htcs: implement rpc tasks 2021-02-24 04:06:54 -08:00
Michael Scire
7667104961 htcs: hook up HtcsService to rpc client 2021-02-24 04:06:54 -08:00
Michael Scire
0c791f2279 htcs: fix magic template argument deduction, do Close/Connect/Bind 2021-02-24 04:06:54 -08:00
Michael Scire
f71943c03a htcs: declare all rpc tasks 2021-02-24 04:06:54 -08:00
Michael Scire
536e3e99a8 htcs: hook up CreateSocket/RpcClient Begin<>/End<> 2021-02-24 04:06:54 -08:00
Michael Scire
abff428212 htcs: hook up manager impl to (unimplemented) service 2021-02-24 04:06:54 -08:00
Michael Scire
1541985222 htcs: hook manager up to (unimplemented) manager impl 2021-02-24 04:06:54 -08:00
Michael Scire
61929d6e21 htcs: hook service objects up to (unimplemented) manager apis 2021-02-24 04:06:54 -08:00
Michael Scire
f0ef9fb918 htc: fixes for WriteFileLarge/sending over data channel 2021-02-24 04:06:54 -08:00
Michael Scire
7621bd4e13 htcfs: fix CreateFile packet header 2021-02-24 04:06:54 -08:00
Michael Scire
d20bceff75 htc: implement the remaining commands for htcfs 2021-02-24 04:06:54 -08:00
Michael Scire
1961cb1034 htc: ReadDirectoryLarge/data channel support 2021-02-24 04:06:54 -08:00
Michael Scire
9daec3a66a htc: Implement (almost) all host-directory commands 2021-02-24 04:06:54 -08:00
Michael Scire
e79417c37c htcfs: implement OpenDirectory/CloseDirectory 2021-02-24 04:06:54 -08:00
Michael Scire
b371487525 sf: optimize argument parsing for const LargeData & 2021-02-24 04:06:54 -08:00
Michael Scire
5c97469348 htc: implement htcfs protocol bringup 2021-02-24 04:06:54 -08:00
Michael Scire
99a38dce32 htc: fix event wait loops for rpc clients 2021-02-24 04:06:54 -08:00
Michael Scire
f28a410ba0 htc: fixes, can now enter ReadyState with wip starlink code 2021-02-24 04:06:54 -08:00
Michael Scire
5fc1981061 htc: fix htcfs sf definition 2021-02-24 04:06:54 -08:00
Michael Scire
1bd0094bee htc: finish last code for Main() 2021-02-24 04:06:54 -08:00
Michael Scire
870b45f208 htc: add htcfs server/service object skeletons 2021-02-24 04:06:54 -08:00
Michael Scire
9fbbb9fadb htclow: add Channel wrapper class 2021-02-24 04:06:54 -08:00
Michael Scire
dec06ff649 htc: add htcfs service api definitions 2021-02-24 04:06:54 -08:00
Michael Scire
b898241112 htcs: add hipc server/service object skeletons 2021-02-24 04:06:54 -08:00
Michael Scire
10255f7f51 htc: skeleton HtcsManagerImpl, implement HtcsMonitor 2021-02-24 04:06:54 -08:00
Michael Scire
cb5a706659 htcs: add sf interface info/types 2021-02-24 04:06:54 -08:00
Michael Scire
4d86863f2c htc: ObserverThread (mostly), system now boots + works with htc in bg 2021-02-24 04:06:54 -08:00
Michael Scire
79a3f442d6 htc: implement psc/pm loop 2021-02-24 04:06:54 -08:00
Michael Scire
7485a1968a htc: implement HtcmiscImpl::ServerThread/HtcmiscRpcServer::ReceiveThread 2021-02-24 04:06:54 -08:00
Michael Scire
3be005b638 htc: Implement RpcClient::ReceiveThread + SendThread 2021-02-24 04:06:54 -08:00
Michael Scire
d60b1abed0 htc: Implement HtcmiscImpl::ClientThread 2021-02-24 04:06:54 -08:00
Michael Scire
1867c31f63 htc: add RpcTaskQueue/RpcTaskIdFreeList 2021-02-24 04:06:54 -08:00
Michael Scire
82757cd1b4 htc: nullptr != false 2021-02-24 04:06:54 -08:00
Michael Scire
f5e98de1a3 htc: add RpcTaskTable 2021-02-24 04:06:54 -08:00
Michael Scire
0880cebc4d htc: implement htcmisc rpc tasks 2021-02-24 04:06:54 -08:00
Michael Scire
1f03b11dbc htc: skeleton constructors for htcmisc 2021-02-24 04:06:54 -08:00
Michael Scire
b925344c3b htc: implement remainder of Mux/Tasks 2021-02-24 04:06:54 -08:00
Michael Scire
42cf3f50d7 htc: implement mux side of connecting (and more) 2021-02-24 04:06:54 -08:00
Michael Scire
70aae4e27a htc: fix driver manager c/p error 2021-02-24 04:06:54 -08:00
Michael Scire
87165e0f08 htc: implement remaining htclow::HtclowManagerImpl funcs (mux impls pending) 2021-02-24 04:06:54 -08:00
Michael Scire
e20c2450ce htc: declare and begin impl of HtclowManagerImpl interface 2021-02-24 04:06:54 -08:00
Michael Scire
968ce12492 htc: optimize Mux::QuerySendPacket 2021-02-24 04:06:54 -08:00
Michael Scire
00ab210e66 mux: optimize many accesses to O(log(n)) vs Nintendo's O(log(n)^2) 2021-02-24 04:06:54 -08:00
Michael Scire
4cb6c63516 htc: implement HtclowDriver 2021-02-24 04:06:54 -08:00
Michael Scire
1963ae7ec0 htc: begin skeletoning types for HtcmiscImpl 2021-02-24 04:06:54 -08:00
Michael Scire
889f144b27 htc: hook up creation of the htc manager service object 2021-02-24 04:06:54 -08:00
Michael Scire
4408ad6a47 htc: module id names, skeleton rest of main 2021-02-24 04:06:54 -08:00
Michael Scire
4ed665bcd3 htc: implement remaining worker thread send logic (for channel mux) 2021-02-24 04:06:54 -08:00
Michael Scire
df3d62df84 htc: send logic for HtcctrlService, bugfixes (thanks @misson20000) 2021-02-24 04:06:54 -08:00
Michael Scire
0977ee72ca rapidjson: add customization point for allocation/asserts 2021-02-24 04:06:54 -08:00
Michael Scire
2cdfde6637 htc: add remaining worker receive thread logic 2021-02-24 04:06:54 -08:00
Michael Scire
6fc24d8883 htc: implement service channel parsing (ReceiveReadyPacket) 2021-02-24 04:06:54 -08:00
Michael Scire
4e9bc617bb rapidjson: import -master as ams::rapidjson 2021-02-24 04:06:54 -08:00
Michael Scire
679fec2ddc htc: implement much of worker receive logic 2021-02-24 04:06:54 -08:00
Michael Scire
8f85cc17dc htc: fix copy/paste error in usb driver 2021-02-24 04:06:54 -08:00
Michael Scire
e40eece74e htc: free ourselves from the tyranny of numerical enums 2021-02-24 04:06:54 -08:00
Michael Scire
2341f18edd htc: implement htclow listener thread 2021-02-24 04:06:54 -08:00
Michael Scire
c9c41e0e8d htc: actually use the usb driver 2021-02-24 04:06:54 -08:00
Michael Scire
c59388caf1 htc: implement complete usb driver 2021-02-24 04:06:54 -08:00
Michael Scire
c878123274 htc: implement (fixing linker errors) through HtclowManagerImpl::OpenDriver 2021-02-24 04:06:54 -08:00
Michael Scire
1687bf2e07 htclow: fix copyright headers, skeleton more manager types 2021-02-24 04:06:54 -08:00
Michael Scire
cf99f54a34 htc: skeleton much of the type hierarchy for htclow manager 2021-02-24 04:06:54 -08:00
Michael Scire
83c1c175ba htc: skeleton some more of main 2021-02-24 04:06:54 -08:00
Michael Scire
fc060d3777 htc: skeleton main file/sysmodule dir 2021-02-24 04:06:54 -08:00
Michael Scire
eb50e99748 kern: alleviate a little KPort pressure. 2021-02-23 14:31:24 -08:00
Michael Scire
287f4e6fa1 sm: fix abort on RegisterService while at port limit
closes #1382
2021-02-23 14:21:46 -08:00
Michael Scire
cbf3ba9b75 General system stability improvements to enhance the user's experience. 2021-02-21 23:50:01 -08:00
Michael Scire
95a6b0828f dmnt: set the debug process handle slightly more carefully 2021-02-20 17:37:59 -08:00
583 changed files with 51191 additions and 2770 deletions

View File

@@ -1,3 +1,9 @@
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>devkitPro)
endif
include $(DEVKITPRO)/devkitA64/base_tools
TOPTARGETS := all clean dist-no-debug dist TOPTARGETS := all clean dist-no-debug dist
AMSBRANCH := $(shell git symbolic-ref --short HEAD) AMSBRANCH := $(shell git symbolic-ref --short HEAD)
AMSHASH := $(shell git rev-parse --short HEAD) AMSHASH := $(shell git rev-parse --short HEAD)
@@ -56,18 +62,11 @@ dist-no-debug: all
mkdir atmosphere-$(AMSVER)/atmosphere mkdir atmosphere-$(AMSVER)/atmosphere
mkdir atmosphere-$(AMSVER)/sept mkdir atmosphere-$(AMSVER)/sept
mkdir atmosphere-$(AMSVER)/switch mkdir atmosphere-$(AMSVER)/switch
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000008
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/010000000000000D
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/010000000000002B
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000032
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000034
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000036
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000037
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/010000000000003C
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000042
mkdir -p atmosphere-$(AMSVER)/atmosphere/fatal_errors mkdir -p atmosphere-$(AMSVER)/atmosphere/fatal_errors
mkdir -p atmosphere-$(AMSVER)/atmosphere/config_templates mkdir -p atmosphere-$(AMSVER)/atmosphere/config_templates
mkdir -p atmosphere-$(AMSVER)/atmosphere/config mkdir -p atmosphere-$(AMSVER)/atmosphere/config
mkdir -p atmosphere-$(AMSVER)/atmosphere/flags
touch atmosphere-$(AMSVER)/atmosphere/flags/clean_stratosphere_for_0.19.0.flag
cp fusee/fusee-primary/fusee-primary.bin atmosphere-$(AMSVER)/atmosphere/reboot_payload.bin cp fusee/fusee-primary/fusee-primary.bin atmosphere-$(AMSVER)/atmosphere/reboot_payload.bin
cp fusee/fusee-mtc/fusee-mtc.bin atmosphere-$(AMSVER)/atmosphere/fusee-mtc.bin cp fusee/fusee-mtc/fusee-mtc.bin atmosphere-$(AMSVER)/atmosphere/fusee-mtc.bin
cp fusee/fusee-secondary/fusee-secondary-experimental.bin atmosphere-$(AMSVER)/atmosphere/fusee-secondary.bin cp fusee/fusee-secondary/fusee-secondary-experimental.bin atmosphere-$(AMSVER)/atmosphere/fusee-secondary.bin
@@ -84,19 +83,27 @@ dist-no-debug: all
cp config_templates/exosphere.ini atmosphere-$(AMSVER)/atmosphere/config_templates/exosphere.ini cp config_templates/exosphere.ini atmosphere-$(AMSVER)/atmosphere/config_templates/exosphere.ini
cp -r config_templates/kip_patches atmosphere-$(AMSVER)/atmosphere/kip_patches cp -r config_templates/kip_patches atmosphere-$(AMSVER)/atmosphere/kip_patches
cp -r config_templates/hbl_html atmosphere-$(AMSVER)/atmosphere/hbl_html cp -r config_templates/hbl_html atmosphere-$(AMSVER)/atmosphere/hbl_html
cp stratosphere/boot2/boot2.nsp atmosphere-$(AMSVER)/atmosphere/contents/0100000000000008/exefs.nsp mkdir -p atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000008
cp stratosphere/dmnt/dmnt.nsp atmosphere-$(AMSVER)/atmosphere/contents/010000000000000D/exefs.nsp mkdir -p atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000008
cp stratosphere/erpt/erpt.nsp atmosphere-$(AMSVER)/atmosphere/contents/010000000000002B/exefs.nsp mkdir -p atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/010000000000000D
cp stratosphere/eclct.stub/eclct.stub.nsp atmosphere-$(AMSVER)/atmosphere/contents/0100000000000032/exefs.nsp mkdir -p atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/010000000000002B
cp stratosphere/fatal/fatal.nsp atmosphere-$(AMSVER)/atmosphere/contents/0100000000000034/exefs.nsp mkdir -p atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000032
cp stratosphere/creport/creport.nsp atmosphere-$(AMSVER)/atmosphere/contents/0100000000000036/exefs.nsp mkdir -p atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000034
cp stratosphere/ro/ro.nsp atmosphere-$(AMSVER)/atmosphere/contents/0100000000000037/exefs.nsp mkdir -p atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000036
cp stratosphere/jpegdec/jpegdec.nsp atmosphere-$(AMSVER)/atmosphere/contents/010000000000003C/exefs.nsp mkdir -p atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000037
cp stratosphere/pgl/pgl.nsp atmosphere-$(AMSVER)/atmosphere/contents/0100000000000042/exefs.nsp mkdir -p atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/010000000000003C
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000032/flags mkdir -p atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000042
touch atmosphere-$(AMSVER)/atmosphere/contents/0100000000000032/flags/boot2.flag cp stratosphere/boot2/boot2.nsp atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000008/exefs.nsp
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000037/flags cp stratosphere/dmnt/dmnt.nsp atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/010000000000000D/exefs.nsp
touch atmosphere-$(AMSVER)/atmosphere/contents/0100000000000037/flags/boot2.flag cp stratosphere/erpt/erpt.nsp atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/010000000000002B/exefs.nsp
cp stratosphere/eclct.stub/eclct.stub.nsp atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000032/exefs.nsp
cp stratosphere/fatal/fatal.nsp atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000034/exefs.nsp
cp stratosphere/creport/creport.nsp atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000036/exefs.nsp
cp stratosphere/ro/ro.nsp atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000037/exefs.nsp
cp stratosphere/jpegdec/jpegdec.nsp atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/010000000000003C/exefs.nsp
cp stratosphere/pgl/pgl.nsp atmosphere-$(AMSVER)/stratosphere_romfs/atmosphere/contents/0100000000000042/exefs.nsp
@build_romfs atmosphere-$(AMSVER)/stratosphere_romfs atmosphere-$(AMSVER)/atmosphere/stratosphere.romfs
rm -r atmosphere-$(AMSVER)/stratosphere_romfs
cp troposphere/reboot_to_payload/reboot_to_payload.nro atmosphere-$(AMSVER)/switch/reboot_to_payload.nro cp troposphere/reboot_to_payload/reboot_to_payload.nro atmosphere-$(AMSVER)/switch/reboot_to_payload.nro
cp troposphere/daybreak/daybreak.nro atmosphere-$(AMSVER)/switch/daybreak.nro cp troposphere/daybreak/daybreak.nro atmosphere-$(AMSVER)/switch/daybreak.nro
cd atmosphere-$(AMSVER); zip -r ../atmosphere-$(AMSVER).zip ./*; cd ../; cd atmosphere-$(AMSVER); zip -r ../atmosphere-$(AMSVER).zip ./*; cd ../;

View File

@@ -1,9 +1,13 @@
; Disable uploading error reports to Nintendo
[eupld] [eupld]
; Disable uploading error reports to Nintendo
; upload_enabled = u8!0x0 ; upload_enabled = u8!0x0
[usb]
; Enable USB 3.0 superspeed for homebrew
; 0 = USB 3.0 support is system default (usually disabled), 1 = USB 3.0 support is enabled.
; usb30_force_enabled = u8!0x0
[ro]
; Control whether RO should ease its validation of NROs. ; Control whether RO should ease its validation of NROs.
; (note: this is normally not necessary, and ips patches can be used.) ; (note: this is normally not necessary, and ips patches can be used.)
[ro]
; ease_nro_restriction = u8!0x1 ; ease_nro_restriction = u8!0x1
; Atmosphere custom settings ; Atmosphere custom settings
[atmosphere] [atmosphere]
@@ -32,12 +36,6 @@
; NOTE: EXPERIMENTAL ; NOTE: EXPERIMENTAL
; If you do not know what you are doing, do not touch this yet. ; If you do not know what you are doing, do not touch this yet.
; fsmitm_redirect_saves_to_sd = u8!0x0 ; fsmitm_redirect_saves_to_sd = u8!0x0
; Controls whether to enable the deprecated hid mitm
; to fix compatibility with old homebrew.
; 0 = Do not enable, 1 = Enable.
; Please note this setting may be removed in a
; future release of Atmosphere.
; enable_deprecated_hid_mitm = u8!0x0
; Controls whether am sees system settings "DebugModeFlag" as ; Controls whether am sees system settings "DebugModeFlag" as
; enabled or disabled. ; enabled or disabled.
; 0 = Disabled (not debug mode), 1 = Enabled (debug mode) ; 0 = Disabled (not debug mode), 1 = Enabled (debug mode)
@@ -52,6 +50,9 @@
; Controls whether dns.mitm logs to the sd card for debugging ; Controls whether dns.mitm logs to the sd card for debugging
; 0 = Disabled, 1 = Enabled ; 0 = Disabled, 1 = Enabled
; enable_dns_mitm_debug_log = u8!0x0 ; enable_dns_mitm_debug_log = u8!0x0
; Controls whether htc is enabled
; 0 = Disabled, 1 = Enabled
; enable_htc = u8!0x0
[hbloader] [hbloader]
; Controls the size of the homebrew heap when running as applet. ; Controls the size of the homebrew heap when running as applet.
; If set to zero, all available applet memory is used as heap. ; If set to zero, all available applet memory is used as heap.

View File

@@ -1,4 +1,50 @@
# Changelog # Changelog
## 0.19.1
+ An issue was fixed that caused a fatal error when using official `migration` services to transfer data between consoles.
+ An issue was fixed in `ncm` that caused an error when the OS tried to enumerate installed SD card content.
+ Several issues were fixed, and usability and stability were improved.
## 0.19.0
+ Support was added for 12.0.0.
+ `mesosphère` was updated to reflect the latest official kernel behavior.
+ `sm`, `boot2`, `pgl` were updated to reflect the latest official behaviors.
+ **Please Note**: 12.0.0 added a new protocol for IPC ("tipc"), which has been freshly reimplemented in its entirety.
+ It is possible there may be as of yet unfound issues; if there are, please send the appropriate crash reports to SciresM (SciresM#0524 on discord).
+ Homebrew which uses atmosphere extensions (including the mitm API) will need to be re-compiled in order to function on 0.19.0.
+ I apologize for this, but it's unavoidable for technical reasons. If you're affected by this and mad about it, please contact SciresM to complain.
+ `erpt` was partially updated to reflect the latest official behaviors.
+ New features were added to erpt to track the activity of running applets, and to detect when a forced shutdown occurs.
+ These behaviors have been temporarily stubbed, as they are not necessary for 12.0.0 to run (and their outputs won't be saved anywhere).
+ A future atmosphère update will implement these behaviors, in the interest of reflecting official logic as faithfully as we can.
+ Atmosphère no longer uses the /contents/ folder for its own programs.
+ Atmosphère's system modules are now bundled together in the single file "stratosphere.romfs".
+ For those working on developing for atmosphère, executables inside the /contents/ directory will be preferred to those in "stratosphere.romfs".
+ **Please Note**: In order to facilitate this change (and the desired behavior), the first time you boot after extracting a release zip, atmosphère system modules inside /contents/ will be deleted.
+ This will have no impact on user programs (it only removes programs with specific program ids).
+ Improvements were made to mesosphere, including:
+ An extension InfoType was added for getting the current process handle, without having to spawn a thread and do IPC with oneself.
+ An issue was fixed in SvcSetDebugThreadContext.
+ An issue was fixed when doing IPC with user buffers.
+ Support was fixed for toggling the custom setting `usb!usb30_force_enabled` on 9.0.0+.
+ This was broken by Nintendo's introducing a dependency that made USB a requirement to launch before custom settings are parsed.
+ Since the fix, you can now toggle the setting (as you could prior to atmosphère 0.9.4), and it will work as expected.
+ **Please Note**: Enabling USB 3.0 often severely impacts wireless communications.
+ Because of this, the setting will default to off. If you experience issues with it enabled, consider disabling it.
+ A warning was added to daybreak when resetting the console to factory settings.
+ Substantial work was completed towards atmosphere's upcoming implementation of the host target connection protocol.
+ Once completed, users will be able to interact with a Switch running atmosphère via a PC application ("Starlink") currently under development.
+ Planned eventual features for connected consoles include a gdbstub, interacting with memory (for cheat development), streaming gameplay audio and video, and accessing the Switch's SD card filesystem.
+ Switch homebrew will also have access to a (configurable and sandboxed) filesystem on the host PC, while connected.
+ Towards this end, the following was accomplished:
+ The "htc" system module was reimplemented completely.
+ The system module which provides remote access to the SD card was reimplemented completely.
+ This is currently the active focus of atmosphère's development.
+ **Please Note**: Support is not yet completed, and users are disadvised from interacting with the related settings for the time being, unless they particularly know what they're doing.
+ A number of minor issues were fixed, including:
+ A bug was fixed in `dmnt` that could cause a fatal when launching certain games with cheats active.
+ An issue was fixed that could cause an abort in `sm` when using a large number of custom system modules.
+ An issue was fixed that prevented launching gamecards on 1.0.0.
+ Minor issues were fixed in the cheat virtual machine's behavior.
+ Several issues were fixed, and usability and stability were improved.
## 0.18.1 ## 0.18.1
+ A number of minor issues were fixed, including: + A number of minor issues were fixed, including:
+ The new `dns.mitm` module added in 0.18.0 no longer fatal errors when receiving port=nullptr. + The new `dns.mitm` module added in 0.18.0 no longer fatal errors when receiving port=nullptr.
@@ -41,7 +87,7 @@
+ This also substantially improves power drain when the system is shut off; consoles powered off from Atmosphere should now drain battery at the same reduced rate as original firmware. + This also substantially improves power drain when the system is shut off; consoles powered off from Atmosphere should now drain battery at the same reduced rate as original firmware.
+ A number of minor changes were made, including: + A number of minor changes were made, including:
+ A number of inconsistencies in the build system were fixed. + A number of inconsistencies in the build system were fixed.
+ Fow those building atmosphère at home, the `boot` sysmodule will no longer rebuild every time make is invoked. + For those building atmosphère at home, the `boot` sysmodule will no longer rebuild every time make is invoked.
+ This substantially improves build times during development iteration. + This substantially improves build times during development iteration.
+ `sm` was updated to more accurately reflect how official code manages request deferral. + `sm` was updated to more accurately reflect how official code manages request deferral.
+ `mesosphère` was updated to more accurately reflect official kernel management of the trace buffer. + `mesosphère` was updated to more accurately reflect official kernel management of the trace buffer.

View File

@@ -6,7 +6,7 @@
[subrepo] [subrepo]
remote = https://github.com/m4xw/emuMMC remote = https://github.com/m4xw/emuMMC
branch = develop branch = develop
commit = 5eed18eb527bbaa63aee5323c26de5b0cca6d28e commit = b355ee6a8f376faa615785419c7d73a8814d9d65
parent = 021b29d2dbc8ed0469bc822393e58c9f0d174d57 parent = b24784f5c13a142bd0cb5d7edb82691c71f4bd00
method = rebase method = rebase
cmdver = 0.4.1 cmdver = 0.4.1

View File

@@ -51,6 +51,8 @@
#include "offsets/1020_exfat.h" #include "offsets/1020_exfat.h"
#include "offsets/1100.h" #include "offsets/1100.h"
#include "offsets/1100_exfat.h" #include "offsets/1100_exfat.h"
#include "offsets/1200.h"
#include "offsets/1200_exfat.h"
#include "../utils/fatal.h" #include "../utils/fatal.h"
#define GET_OFFSET_STRUCT_NAME(vers) g_offsets##vers #define GET_OFFSET_STRUCT_NAME(vers) g_offsets##vers
@@ -113,6 +115,8 @@ DEFINE_OFFSET_STRUCT(_1020);
DEFINE_OFFSET_STRUCT(_1020_EXFAT); DEFINE_OFFSET_STRUCT(_1020_EXFAT);
DEFINE_OFFSET_STRUCT(_1100); DEFINE_OFFSET_STRUCT(_1100);
DEFINE_OFFSET_STRUCT(_1100_EXFAT); DEFINE_OFFSET_STRUCT(_1100_EXFAT);
DEFINE_OFFSET_STRUCT(_1200);
DEFINE_OFFSET_STRUCT(_1200_EXFAT);
const fs_offsets_t *get_fs_offsets(enum FS_VER version) { const fs_offsets_t *get_fs_offsets(enum FS_VER version) {
switch (version) { switch (version) {
@@ -186,6 +190,10 @@ const fs_offsets_t *get_fs_offsets(enum FS_VER version) {
return &(GET_OFFSET_STRUCT_NAME(_1100)); return &(GET_OFFSET_STRUCT_NAME(_1100));
case FS_VER_11_0_0_EXFAT: case FS_VER_11_0_0_EXFAT:
return &(GET_OFFSET_STRUCT_NAME(_1100_EXFAT)); return &(GET_OFFSET_STRUCT_NAME(_1100_EXFAT));
case FS_VER_12_0_0:
return &(GET_OFFSET_STRUCT_NAME(_1200));
case FS_VER_12_0_0_EXFAT:
return &(GET_OFFSET_STRUCT_NAME(_1200_EXFAT));
default: default:
fatal_abort(Fatal_UnknownVersion); fatal_abort(Fatal_UnknownVersion);
} }

View File

@@ -74,6 +74,9 @@ enum FS_VER
FS_VER_11_0_0, FS_VER_11_0_0,
FS_VER_11_0_0_EXFAT, FS_VER_11_0_0_EXFAT,
FS_VER_12_0_0,
FS_VER_12_0_0_EXFAT,
FS_VER_MAX, FS_VER_MAX,
}; };

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2019 m4xw <m4x@m4xw.net>
* Copyright (c) 2019 Atmosphere-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __FS_1200_H__
#define __FS_1200_H__
// Accessor vtable getters
#define FS_OFFSET_1200_SDMMC_ACCESSOR_GC 0x154FD0
#define FS_OFFSET_1200_SDMMC_ACCESSOR_SD 0x156DE0
#define FS_OFFSET_1200_SDMMC_ACCESSOR_NAND 0x155500
// Hooks
#define FS_OFFSET_1200_SDMMC_WRAPPER_READ 0x150970
#define FS_OFFSET_1200_SDMMC_WRAPPER_WRITE 0x150A30
#define FS_OFFSET_1200_RTLD 0x688
#define FS_OFFSET_1200_RTLD_DESTINATION ((uintptr_t)(INT64_C(-0x3C)))
#define FS_OFFSET_1200_CLKRST_SET_MIN_V_CLK_RATE 0x14FCC0
// Misc funcs
#define FS_OFFSET_1200_LOCK_MUTEX 0x29350
#define FS_OFFSET_1200_UNLOCK_MUTEX 0x293A0
#define FS_OFFSET_1200_SDMMC_WRAPPER_CONTROLLER_OPEN 0x150850
#define FS_OFFSET_1200_SDMMC_WRAPPER_CONTROLLER_CLOSE 0x1508E0
// Misc Data
#define FS_OFFSET_1200_SD_MUTEX 0xE3D3E8
#define FS_OFFSET_1200_NAND_MUTEX 0xE38768
#define FS_OFFSET_1200_ACTIVE_PARTITION 0xE387A8
#define FS_OFFSET_1200_SDMMC_DAS_HANDLE 0xE20DB0
// NOPs
#define FS_OFFSET_1200_SD_DAS_INIT 0x27244
// Nintendo Paths
#define FS_OFFSET_1200_NINTENDO_PATHS \
{ \
{.opcode_reg = 3, .adrp_offset = 0x0006E810, .add_rel_offset = 0x00000004}, \
{.opcode_reg = 3, .adrp_offset = 0x0007AEC0, .add_rel_offset = 0x00000004}, \
{.opcode_reg = 4, .adrp_offset = 0x00081254, .add_rel_offset = 0x00000004}, \
{.opcode_reg = 4, .adrp_offset = 0x00092850, .add_rel_offset = 0x00000004}, \
{.opcode_reg = 0, .adrp_offset = 0, .add_rel_offset = 0}, \
}
#endif // __FS_1200_H__

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2019 m4xw <m4x@m4xw.net>
* Copyright (c) 2019 Atmosphere-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __FS_1200_EXFAT_H__
#define __FS_1200_EXFAT_H__
// Accessor vtable getters
#define FS_OFFSET_1200_EXFAT_SDMMC_ACCESSOR_GC 0x154FD0
#define FS_OFFSET_1200_EXFAT_SDMMC_ACCESSOR_SD 0x156DE0
#define FS_OFFSET_1200_EXFAT_SDMMC_ACCESSOR_NAND 0x155500
// Hooks
#define FS_OFFSET_1200_EXFAT_SDMMC_WRAPPER_READ 0x150970
#define FS_OFFSET_1200_EXFAT_SDMMC_WRAPPER_WRITE 0x150A30
#define FS_OFFSET_1200_EXFAT_RTLD 0x688
#define FS_OFFSET_1200_EXFAT_RTLD_DESTINATION ((uintptr_t)(INT64_C(-0x3C)))
#define FS_OFFSET_1200_EXFAT_CLKRST_SET_MIN_V_CLK_RATE 0x14FCC0
// Misc funcs
#define FS_OFFSET_1200_EXFAT_LOCK_MUTEX 0x29350
#define FS_OFFSET_1200_EXFAT_UNLOCK_MUTEX 0x293A0
#define FS_OFFSET_1200_EXFAT_SDMMC_WRAPPER_CONTROLLER_OPEN 0x150850
#define FS_OFFSET_1200_EXFAT_SDMMC_WRAPPER_CONTROLLER_CLOSE 0x1508E0
// Misc Data
#define FS_OFFSET_1200_EXFAT_SD_MUTEX 0xE4B3E8
#define FS_OFFSET_1200_EXFAT_NAND_MUTEX 0xE46768
#define FS_OFFSET_1200_EXFAT_ACTIVE_PARTITION 0xE467A8
#define FS_OFFSET_1200_EXFAT_SDMMC_DAS_HANDLE 0xE2EDB0
// NOPs
#define FS_OFFSET_1200_EXFAT_SD_DAS_INIT 0x27244
// Nintendo Paths
#define FS_OFFSET_1200_EXFAT_NINTENDO_PATHS \
{ \
{.opcode_reg = 3, .adrp_offset = 0x0006E810, .add_rel_offset = 0x00000004}, \
{.opcode_reg = 3, .adrp_offset = 0x0007AEC0, .add_rel_offset = 0x00000004}, \
{.opcode_reg = 4, .adrp_offset = 0x00081254, .add_rel_offset = 0x00000004}, \
{.opcode_reg = 4, .adrp_offset = 0x00092850, .add_rel_offset = 0x00000004}, \
{.opcode_reg = 0, .adrp_offset = 0, .add_rel_offset = 0}, \
}
#endif // __FS_1200_EXFAT_H__

View File

@@ -328,13 +328,13 @@ uint64_t sdmmc_wrapper_controller_open(int mmc_id)
if (_this != NULL) if (_this != NULL)
{ {
// Lock eMMC xfer while SD card is being initialized by FS. // Lock eMMC xfer while SD card is being initialized by FS.
if (_this == sdmmc_accessor_get(FS_SDMMC_SD)) if (mmc_id == FS_SDMMC_SD)
mutex_lock_handler(FS_SDMMC_EMMC); // Recursive Mutex, handler will lock SD as well if custom_driver mutex_lock_handler(FS_SDMMC_EMMC); // Recursive Mutex, handler will lock SD as well if custom_driver
result = _this->vtab->sdmmc_accessor_controller_open(_this); result = _this->vtab->sdmmc_accessor_controller_open(_this);
// Unlock eMMC. // Unlock eMMC.
if (_this == sdmmc_accessor_get(FS_SDMMC_SD)) if (mmc_id == FS_SDMMC_SD)
mutex_unlock_handler(FS_SDMMC_EMMC); mutex_unlock_handler(FS_SDMMC_EMMC);
return result; return result;

View File

@@ -47,9 +47,9 @@ namespace ams::secmon::smc {
[fuse::DramId_IcosaSamsung4GB] = pkg1::MemorySize_4GB, [fuse::DramId_IcosaSamsung4GB] = pkg1::MemorySize_4GB,
[fuse::DramId_IcosaHynix4GB] = pkg1::MemorySize_4GB, [fuse::DramId_IcosaHynix4GB] = pkg1::MemorySize_4GB,
[fuse::DramId_IcosaMicron4GB] = pkg1::MemorySize_4GB, [fuse::DramId_IcosaMicron4GB] = pkg1::MemorySize_4GB,
[fuse::DramId_AulaHynix1y4GB] = pkg1::MemorySize_4GB, [fuse::DramId_IowaHynix1y4GB] = pkg1::MemorySize_4GB,
[fuse::DramId_IcosaSamsung6GB] = pkg1::MemorySize_6GB, [fuse::DramId_IcosaSamsung6GB] = pkg1::MemorySize_6GB,
[fuse::DramId_CopperHynix4GB] = pkg1::MemorySize_4GB, [fuse::DramId_HoagHynix1y4GB] = pkg1::MemorySize_4GB,
[fuse::DramId_CopperMicron4GB] = pkg1::MemorySize_4GB, [fuse::DramId_CopperMicron4GB] = pkg1::MemorySize_4GB,
[fuse::DramId_IowaX1X2Samsung4GB] = pkg1::MemorySize_4GB, [fuse::DramId_IowaX1X2Samsung4GB] = pkg1::MemorySize_4GB,
[fuse::DramId_IowaSansung4GB] = pkg1::MemorySize_4GB, [fuse::DramId_IowaSansung4GB] = pkg1::MemorySize_4GB,
@@ -286,6 +286,10 @@ namespace ams::secmon::smc {
/* Get the log configuration. */ /* Get the log configuration. */
args.r[1] = (static_cast<u64>(static_cast<u8>(secmon::GetLogPort())) << 32) | static_cast<u64>(secmon::GetLogBaudRate()); args.r[1] = (static_cast<u64>(static_cast<u8>(secmon::GetLogPort())) << 32) | static_cast<u64>(secmon::GetLogBaudRate());
break; break;
case ConfigItem::ExosphereForceEnableUsb30:
/* Get whether usb 3.0 should be force-enabled. */
args.r[1] = GetSecmonConfiguration().IsUsb30ForceEnabled();
break;
default: default:
return SmcResult::InvalidArgument; return SmcResult::InvalidArgument;
} }

View File

@@ -50,6 +50,7 @@ namespace ams::secmon::smc {
ExosphereEmummcType = 65007, ExosphereEmummcType = 65007,
ExospherePayloadAddress = 65008, ExospherePayloadAddress = 65008,
ExosphereLogConfiguration = 65009, ExosphereLogConfiguration = 65009,
ExosphereForceEnableUsb30 = 65010,
}; };
SmcResult SmcGetConfigUser(SmcArguments &args); SmcResult SmcGetConfigUser(SmcArguments &args);

View File

@@ -91,6 +91,9 @@ typedef enum {
FS_VER_11_0_0, FS_VER_11_0_0,
FS_VER_11_0_0_EXFAT, FS_VER_11_0_0_EXFAT,
FS_VER_12_0_0,
FS_VER_12_0_0_EXFAT,
FS_VER_MAX, FS_VER_MAX,
} emummc_fs_ver_t; } emummc_fs_ver_t;

View File

@@ -33,6 +33,7 @@
#define EXOSPHERE_FLAG_ENABLE_USERMODE_PMU_ACCESS (1 << 4u) #define EXOSPHERE_FLAG_ENABLE_USERMODE_PMU_ACCESS (1 << 4u)
#define EXOSPHERE_FLAG_BLANK_PRODINFO (1 << 5u) #define EXOSPHERE_FLAG_BLANK_PRODINFO (1 << 5u)
#define EXOSPHERE_FLAG_ALLOW_WRITING_TO_CAL_SYSMMC (1 << 6u) #define EXOSPHERE_FLAG_ALLOW_WRITING_TO_CAL_SYSMMC (1 << 6u)
#define EXOSPHERE_FLAG_FORCE_ENABLE_USB_30 (1 << 7u)
#define EXOSPHERE_LOG_FLAG_INVERTED (1 << 0u) #define EXOSPHERE_LOG_FLAG_INVERTED (1 << 0u)

View File

@@ -426,6 +426,9 @@ static const uint8_t g_fs_hashes[FS_VER_MAX][0x8] = {
"\xE3\x99\x15\x6E\x84\x4E\xB0\xAA", /* FS_VER_11_0_0 */ "\xE3\x99\x15\x6E\x84\x4E\xB0\xAA", /* FS_VER_11_0_0 */
"\x0B\xA1\x5B\xB3\x04\xB5\x05\x63", /* FS_VER_11_0_0_EXFAT */ "\x0B\xA1\x5B\xB3\x04\xB5\x05\x63", /* FS_VER_11_0_0_EXFAT */
"\xDC\x2A\x08\x49\x96\xBB\x3C\x01", /* FS_VER_12_0_0 */
"\xD5\xA5\xBF\x36\x64\x0C\x49\xEA", /* FS_VER_12_0_0_EXFAT */
}; };
kip1_header_t *apply_kip_ips_patches(kip1_header_t *kip, size_t kip_size, emummc_fs_ver_t *out_fs_ver) { kip1_header_t *apply_kip_ips_patches(kip1_header_t *kip, size_t kip_size, emummc_fs_ver_t *out_fs_ver) {

View File

@@ -568,6 +568,7 @@ static const instruction_t MAKE_KERNEL_PATCH_NAME(1000, proc_id_recv)[] = {0xA9B
*/ */
static const uint8_t MAKE_KERNEL_PATTERN_NAME(1100, proc_id_send)[] = {0xE0, 0x03, 0x15, 0xAA, 0xA8, 0x02, 0x40, 0xF9, 0x08, 0x1D, 0x40, 0xF9, 0x00, 0x01, 0x3F, 0xD6, 0x88, 0x4A, 0x3C, 0x8B, 0x09, 0xFC, 0x60, 0xD3, 0x00, 0x25, 0x00, 0x29}; static const uint8_t MAKE_KERNEL_PATTERN_NAME(1100, proc_id_send)[] = {0xE0, 0x03, 0x15, 0xAA, 0xA8, 0x02, 0x40, 0xF9, 0x08, 0x1D, 0x40, 0xF9, 0x00, 0x01, 0x3F, 0xD6, 0x88, 0x4A, 0x3C, 0x8B, 0x09, 0xFC, 0x60, 0xD3, 0x00, 0x25, 0x00, 0x29};
static const instruction_t MAKE_KERNEL_PATCH_NAME(1100, proc_id_send)[] = {0xA9BF2FEA, 0xF94043EB, 0x5280006A, 0xD37EF54A, 0xF86A696A, 0x92FFFFE9, 0x8A090148, 0xD2FFFFE9, 0x8A09014A, 0xD2FFFFC9, 0xEB09015F, 0x54000100, 0xA9BF27E8, 0xF94002A8, 0xF9401D08, 0xAA1503E0, 0xD63F0100, 0xA8C127E8, 0xAA0003E8, 0xA8C12FEA, 0xAA0803E0}; static const instruction_t MAKE_KERNEL_PATCH_NAME(1100, proc_id_send)[] = {0xA9BF2FEA, 0xF94043EB, 0x5280006A, 0xD37EF54A, 0xF86A696A, 0x92FFFFE9, 0x8A090148, 0xD2FFFFE9, 0x8A09014A, 0xD2FFFFC9, 0xEB09015F, 0x54000100, 0xA9BF27E8, 0xF94002A8, 0xF9401D08, 0xAA1503E0, 0xD63F0100, 0xA8C127E8, 0xAA0003E8, 0xA8C12FEA, 0xAA0803E0};
/* /*
stp x10, x11, [sp, #-0x10]! stp x10, x11, [sp, #-0x10]!
ldr x11, [sp, #0xE0] ldr x11, [sp, #0xE0]
@@ -596,6 +597,63 @@ static const instruction_t MAKE_KERNEL_PATCH_NAME(1100, proc_id_send)[] = {0xA9B
static const uint8_t MAKE_KERNEL_PATTERN_NAME(1100, proc_id_recv)[] = {0x08, 0x03, 0x40, 0xF9, 0xE0, 0x03, 0x18, 0xAA, 0x08, 0x1D, 0x40, 0xF9, 0x00, 0x01, 0x3F, 0xD6, 0xE8, 0x7F, 0x40, 0xF9, 0x09, 0xFC, 0x60, 0xD3, 0xEA, 0x5B, 0x40, 0xF9}; static const uint8_t MAKE_KERNEL_PATTERN_NAME(1100, proc_id_recv)[] = {0x08, 0x03, 0x40, 0xF9, 0xE0, 0x03, 0x18, 0xAA, 0x08, 0x1D, 0x40, 0xF9, 0x00, 0x01, 0x3F, 0xD6, 0xE8, 0x7F, 0x40, 0xF9, 0x09, 0xFC, 0x60, 0xD3, 0xEA, 0x5B, 0x40, 0xF9};
static const instruction_t MAKE_KERNEL_PATCH_NAME(1100, proc_id_recv)[] = {0xA9BF2FEA, 0xF94073EB, 0x5280006A, 0xD37EF54A, 0xF86A696A, 0x92FFFFE9, 0x8A090148, 0xD2FFFFE9, 0x8A09014A, 0xD2FFFFC9, 0xEB09015F, 0x54000100, 0xA9BF27E8, 0xF9400308, 0xF9401D08, 0xAA1803E0, 0xD63F0100, 0xA8C127E8, 0xAA0003E8, 0xA8C12FEA, 0xAA0803E0}; static const instruction_t MAKE_KERNEL_PATCH_NAME(1100, proc_id_recv)[] = {0xA9BF2FEA, 0xF94073EB, 0x5280006A, 0xD37EF54A, 0xF86A696A, 0x92FFFFE9, 0x8A090148, 0xD2FFFFE9, 0x8A09014A, 0xD2FFFFC9, 0xEB09015F, 0x54000100, 0xA9BF27E8, 0xF9400308, 0xF9401D08, 0xAA1803E0, 0xD63F0100, 0xA8C127E8, 0xAA0003E8, 0xA8C12FEA, 0xAA0803E0};
/*
stp x10, x11, [sp, #-0x10]!
ldr x11, [sp, #0x98]
mov w10, #3
lsl x10, x10, #2
ldr x10, [x11, x10]
mov x9, #0x0000ffffffffffff
and x8, x10, x9
mov x9, #0xffff000000000000
and x10, x10, x9
mov x9, #0xfffe000000000000
cmp x10, x9
beq #0x20
stp x8, x9, [sp, #-0x10]!
ldr x8, [x22]
ldr x8, [x8, #0x38]
mov x0, x22
blr x8
ldp x8, x9, [sp],#0x10
mov x8, x0
ldp x10, x11, [sp],#0x10
mov x0, x8
*/
static const uint8_t MAKE_KERNEL_PATTERN_NAME(1200, proc_id_send)[] = {0xE0, 0x03, 0x16, 0xAA, 0xC8, 0x02, 0x40, 0xF9, 0x08, 0x1D, 0x40, 0xF9, 0x00, 0x01, 0x3F, 0xD6, 0xA8, 0x4A, 0x3B, 0x8B, 0x09, 0xFC, 0x60, 0xD3, 0x00, 0x25, 0x00, 0x29};
static const instruction_t MAKE_KERNEL_PATCH_NAME(1200, proc_id_send)[] = {0xA9BF2FEA, 0xF9404FEB, 0x5280006A, 0xD37EF54A, 0xF86A696A, 0x92FFFFE9, 0x8A090148, 0xD2FFFFE9, 0x8A09014A, 0xD2FFFFC9, 0xEB09015F, 0x54000100, 0xA9BF27E8, 0xF94002C8, 0xF9401D08, 0xAA1603E0, 0xD63F0100, 0xA8C127E8, 0xAA0003E8, 0xA8C12FEA, 0xAA0803E0};
/*
stp x10, x11, [sp, #-0x10]!
ldr x11, [sp, #0xE0]
mov w10, #3
lsl x10, x10, #2
ldr x10, [x11, x10]
mov x9, #0x0000ffffffffffff
and x8, x10, x9
mov x9, #0xffff000000000000
and x10, x10, x9
mov x9, #0xfffe000000000000
cmp x10, x9
beq #0x20
stp x8, x9, [sp, #-0x10]!
ldr x8, [x28]
ldr x8, [x8, #0x38]
mov x0, x28
blr x8
ldp x8, x9, [sp],#0x10
mov x8, x0
ldp x10, x11, [sp],#0x10
mov x0, x8
*/
static const uint8_t MAKE_KERNEL_PATTERN_NAME(1200, proc_id_recv)[] = {0x88, 0x03, 0x40, 0xF9, 0xE0, 0x03, 0x1C, 0xAA, 0x08, 0x1D, 0x40, 0xF9, 0x00, 0x01, 0x3F, 0xD6, 0x08, 0x4B, 0x3A, 0x8B, 0x09, 0xFC, 0x60, 0xD3, 0x00, 0x25, 0x00, 0x29};
static const instruction_t MAKE_KERNEL_PATCH_NAME(1200, proc_id_recv)[] = {0xA9BF2FEA, 0xF94073EB, 0x5280006A, 0xD37EF54A, 0xF86A696A, 0x92FFFFE9, 0x8A090148, 0xD2FFFFE9, 0x8A09014A, 0xD2FFFFC9, 0xEB09015F, 0x54000100, 0xA9BF27E8, 0xF9400388, 0xF9401D08, 0xAA1C03E0, 0xD63F0100, 0xA8C127E8, 0xAA0003E8, 0xA8C12FEA, 0xAA0803E0};
/* svcControlCodeMemory Patches */ /* svcControlCodeMemory Patches */
/* b.eq -> nop */ /* b.eq -> nop */
static const instruction_t MAKE_KERNEL_PATCH_NAME(500, svc_control_codememory)[] = {MAKE_NOP}; static const instruction_t MAKE_KERNEL_PATCH_NAME(500, svc_control_codememory)[] = {MAKE_NOP};
@@ -605,6 +663,7 @@ static const instruction_t MAKE_KERNEL_PATCH_NAME(800, svc_control_codememory)[
static const instruction_t MAKE_KERNEL_PATCH_NAME(900, svc_control_codememory)[] = {MAKE_NOP}; static const instruction_t MAKE_KERNEL_PATCH_NAME(900, svc_control_codememory)[] = {MAKE_NOP};
static const instruction_t MAKE_KERNEL_PATCH_NAME(1000, svc_control_codememory)[] = {MAKE_NOP}; static const instruction_t MAKE_KERNEL_PATCH_NAME(1000, svc_control_codememory)[] = {MAKE_NOP};
static const instruction_t MAKE_KERNEL_PATCH_NAME(1100, svc_control_codememory)[] = {MAKE_NOP}; static const instruction_t MAKE_KERNEL_PATCH_NAME(1100, svc_control_codememory)[] = {MAKE_NOP};
static const instruction_t MAKE_KERNEL_PATCH_NAME(1200, svc_control_codememory)[] = {MAKE_NOP};
static const instruction_t MAKE_KERNEL_PATCH_NAME(500, system_memory_increase)[] = {0x52A3C008}; /* MOV W8, #0x1E000000 */ static const instruction_t MAKE_KERNEL_PATCH_NAME(500, system_memory_increase)[] = {0x52A3C008}; /* MOV W8, #0x1E000000 */
static const instruction_t MAKE_KERNEL_PATCH_NAME(600, system_memory_increase)[] = {0x52A3B008}; /* MOV W8, #0x1D800000 */ static const instruction_t MAKE_KERNEL_PATCH_NAME(600, system_memory_increase)[] = {0x52A3B008}; /* MOV W8, #0x1D800000 */
@@ -613,6 +672,7 @@ static const instruction_t MAKE_KERNEL_PATCH_NAME(800, system_memory_increase)[
static const instruction_t MAKE_KERNEL_PATCH_NAME(900, system_memory_increase)[] = {0x52A3B013}; /* MOV W19, #0x1D800000 */ static const instruction_t MAKE_KERNEL_PATCH_NAME(900, system_memory_increase)[] = {0x52A3B013}; /* MOV W19, #0x1D800000 */
static const instruction_t MAKE_KERNEL_PATCH_NAME(1000, system_memory_increase)[] = {0x52A3B013}; /* MOV W19, #0x1D800000 */ static const instruction_t MAKE_KERNEL_PATCH_NAME(1000, system_memory_increase)[] = {0x52A3B013}; /* MOV W19, #0x1D800000 */
static const instruction_t MAKE_KERNEL_PATCH_NAME(1100, system_memory_increase)[] = {0x52A3B015}; /* MOV W21, #0x1D800000 */ static const instruction_t MAKE_KERNEL_PATCH_NAME(1100, system_memory_increase)[] = {0x52A3B015}; /* MOV W21, #0x1D800000 */
static const instruction_t MAKE_KERNEL_PATCH_NAME(1200, system_memory_increase)[] = {0x52A3B015}; /* MOV W21, #0x1D800000 */
/* Hook Definitions. */ /* Hook Definitions. */
static const kernel_patch_t g_kernel_patches_100[] = { static const kernel_patch_t g_kernel_patches_100[] = {
@@ -935,6 +995,35 @@ static const kernel_patch_t g_kernel_patches_1101[] = {
} }
}; };
static const kernel_patch_t g_kernel_patches_1200[] = {
{ /* Send Message Process ID Patch. */
.pattern_size = 0x1C,
.pattern = MAKE_KERNEL_PATTERN_NAME(1200, proc_id_send),
.pattern_hook_offset = 0x0,
.payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(1200, proc_id_send))/sizeof(instruction_t),
.branch_back_offset = 0x10,
.payload = MAKE_KERNEL_PATCH_NAME(1200, proc_id_send)
},
{ /* Receive Message Process ID Patch. */
.pattern_size = 0x1C,
.pattern = MAKE_KERNEL_PATTERN_NAME(1200, proc_id_recv),
.pattern_hook_offset = 0x0,
.payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(1200, proc_id_recv))/sizeof(instruction_t),
.branch_back_offset = 0x10,
.payload = MAKE_KERNEL_PATCH_NAME(1200, proc_id_recv)
},
{ /* svcControlCodeMemory Patch. */
.payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(1200, svc_control_codememory))/sizeof(instruction_t),
.payload = MAKE_KERNEL_PATCH_NAME(1200, svc_control_codememory),
.patch_offset = 0x2FCB4,
},
{ /* System Memory Increase Patch. */
.payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(1200, system_memory_increase))/sizeof(instruction_t),
.payload = MAKE_KERNEL_PATCH_NAME(1200, system_memory_increase),
.patch_offset = 0x4809C,
}
};
#define KERNEL_PATCHES(vers) .num_patches = sizeof(g_kernel_patches_##vers)/sizeof(kernel_patch_t), .patches = g_kernel_patches_##vers, #define KERNEL_PATCHES(vers) .num_patches = sizeof(g_kernel_patches_##vers)/sizeof(kernel_patch_t), .patches = g_kernel_patches_##vers,
/* Kernel Infos. */ /* Kernel Infos. */
@@ -1038,6 +1127,15 @@ static const kernel_info_t g_kernel_infos[] = {
.embedded_ini_ptr = 0x180, .embedded_ini_ptr = 0x180,
.free_code_space_offset = 0x49EE8, .free_code_space_offset = 0x49EE8,
KERNEL_PATCHES(1101) KERNEL_PATCHES(1101)
},
{ /* 12.0.0. */
.hash = {0x8D, 0x4A, 0x1E, 0xFC, 0xCC, 0x6C, 0xFE, 0x6C, 0x45, 0x14, 0x13, 0xA1, 0x7F, 0xF6, 0xDF, 0xFD, 0x7E, 0x5D, 0xD1, 0x38, 0xCE, 0x86, 0x11, 0x8B, 0x58, 0x5F, 0x89, 0x67, 0x84, 0x48, 0xA8, 0x17, },
.hash_offset = 0x1C4,
.hash_size = 0x68000 - 0x1C4,
.embedded_ini_offset = 0x68000,
.embedded_ini_ptr = 0x180,
.free_code_space_offset = 0x48810,
KERNEL_PATCHES(1200)
} }
}; };

View File

@@ -256,6 +256,24 @@ static int stratosphere_ini_handler(void *user, const char *section, const char
return 1; return 1;
} }
static int system_settings_ini_handler(void *user, const char *section, const char *name, const char *value) {
uint32_t *flags = (uint32_t *)user;
if (strcmp(section, "usb") == 0) {
if (strcmp(name, "usb30_force_enabled") == 0) {
if (strcmp(value, "u8!0x1") == 0) {
*flags |= EXOSPHERE_FLAG_FORCE_ENABLE_USB_30;
} else if (strcmp(value, "u8!0x0") == 0) {
*flags &= ~(EXOSPHERE_FLAG_FORCE_ENABLE_USB_30);
}
} else {
return 0;
}
} else {
return 0;
}
return 1;
}
static bool is_nca_present(const char *nca_name) { static bool is_nca_present(const char *nca_name) {
char path[0x100]; char path[0x100];
snprintf(path, sizeof(path), "system:/contents/registered/%s.nca", nca_name); snprintf(path, sizeof(path), "system:/contents/registered/%s.nca", nca_name);
@@ -267,7 +285,9 @@ static bool is_nca_present(const char *nca_name) {
static uint32_t nxboot_get_specific_target_firmware(uint32_t target_firmware){ static uint32_t nxboot_get_specific_target_firmware(uint32_t target_firmware){
#define CHECK_NCA(NCA_ID, VERSION) do { if (is_nca_present(NCA_ID)) { return ATMOSPHERE_TARGET_FIRMWARE_##VERSION; } } while(0) #define CHECK_NCA(NCA_ID, VERSION) do { if (is_nca_present(NCA_ID)) { return ATMOSPHERE_TARGET_FIRMWARE_##VERSION; } } while(0)
if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_11_0_0) { if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_12_0_0) {
CHECK_NCA("bd4185843550fbba125b20787005d1d2", 12_0_0);
} else if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_11_0_0) {
CHECK_NCA("56211c7a5ed20a5332f5cdda67121e37", 11_0_1); CHECK_NCA("56211c7a5ed20a5332f5cdda67121e37", 11_0_1);
CHECK_NCA("594c90bcdbcccad6b062eadba0cd0e7e", 11_0_0); CHECK_NCA("594c90bcdbcccad6b062eadba0cd0e7e", 11_0_0);
} else if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_10_0_0) { } else if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_10_0_0) {
@@ -370,6 +390,8 @@ static uint32_t nxboot_get_target_firmware(const void *package1loader) {
return ATMOSPHERE_TARGET_FIRMWARE_10_0_0; return ATMOSPHERE_TARGET_FIRMWARE_10_0_0;
} else if (memcmp(package1loader_header->build_timestamp, "20201030", 8) == 0) { } else if (memcmp(package1loader_header->build_timestamp, "20201030", 8) == 0) {
return ATMOSPHERE_TARGET_FIRMWARE_11_0_0; return ATMOSPHERE_TARGET_FIRMWARE_11_0_0;
} else if (memcmp(package1loader_header->build_timestamp, "20210129", 8) == 0) {
return ATMOSPHERE_TARGET_FIRMWARE_12_0_0;
} else { } else {
fatal_error("[NXBOOT] Unable to identify package1!\n"); fatal_error("[NXBOOT] Unable to identify package1!\n");
} }
@@ -537,6 +559,15 @@ static void nxboot_configure_exosphere(uint32_t target_firmware, unsigned int ke
/* Apply lcd vendor. */ /* Apply lcd vendor. */
exo_cfg.lcd_vendor = display_get_lcd_vendor(); exo_cfg.lcd_vendor = display_get_lcd_vendor();
/* Read and parse system settings.ini to determine usb 3.0 enable. */
char *settings_ini = calloc(1, 0x20000);
if (read_from_file(settings_ini, 0x1FFFF, "atmosphere/config/system_settings.ini")) {
if (ini_parse_string(settings_ini, system_settings_ini_handler, &exo_cfg.flags[0]) < 0) {
fatal_error("[NXBOOT] Failed to parse system_settings.ini!\n");
}
}
free(settings_ini);
if ((exo_cfg.target_firmware < ATMOSPHERE_TARGET_FIRMWARE_MIN) || (exo_cfg.target_firmware > ATMOSPHERE_TARGET_FIRMWARE_MAX)) { if ((exo_cfg.target_firmware < ATMOSPHERE_TARGET_FIRMWARE_MIN) || (exo_cfg.target_firmware > ATMOSPHERE_TARGET_FIRMWARE_MAX)) {
fatal_error("[NXBOOT] Invalid Exosphere target firmware!\n"); fatal_error("[NXBOOT] Invalid Exosphere target firmware!\n");
} }
@@ -568,6 +599,10 @@ static void nxboot_configure_stratosphere(uint32_t target_firmware) {
if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_11_0_0 && !(fuse_get_reserved_odm(7) & ~0x00001FFF)) { if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_11_0_0 && !(fuse_get_reserved_odm(7) & ~0x00001FFF)) {
kip_patches_set_enable_nogc(); kip_patches_set_enable_nogc();
} }
/* NOTE: 12.0.0 added a new lotus firmware, but did not burn a fuse. */
/* This is literally undetectable using normal fuses.... */
/* C'est la vie. */
} }
} }

View File

@@ -6,7 +6,7 @@
[subrepo] [subrepo]
remote = https://github.com/Atmosphere-NX/Atmosphere-libs remote = https://github.com/Atmosphere-NX/Atmosphere-libs
branch = master branch = master
commit = bc08912dd31bb172467add8e24b4f0adac431939 commit = 86c2eec8e9e966a30c19692adb79faeda45c1940
parent = 71add1add8521e0c2115ec612c514400ac7ba688 parent = aa2d03d8e13bc5d3f34751b6105503a601dc958e
method = merge method = merge
cmdver = 0.4.1 cmdver = 0.4.1

View File

@@ -51,9 +51,9 @@ namespace ams::fuse {
DramId_IcosaSamsung4GB = 0, DramId_IcosaSamsung4GB = 0,
DramId_IcosaHynix4GB = 1, DramId_IcosaHynix4GB = 1,
DramId_IcosaMicron4GB = 2, DramId_IcosaMicron4GB = 2,
DramId_AulaHynix1y4GB = 3, DramId_IowaHynix1y4GB = 3,
DramId_IcosaSamsung6GB = 4, DramId_IcosaSamsung6GB = 4,
DramId_CopperHynix4GB = 5, DramId_HoagHynix1y4GB = 5,
DramId_CopperMicron4GB = 6, DramId_CopperMicron4GB = 6,
DramId_IowaX1X2Samsung4GB = 7, DramId_IowaX1X2Samsung4GB = 7,
DramId_IowaSansung4GB = 8, DramId_IowaSansung4GB = 8,

View File

@@ -29,6 +29,7 @@ namespace ams::secmon {
SecureMonitorConfigurationFlag_EnableUserModePerformanceCounterAccess = (1u << 4), SecureMonitorConfigurationFlag_EnableUserModePerformanceCounterAccess = (1u << 4),
SecureMonitorConfigurationFlag_ShouldUseBlankCalibrationBinary = (1u << 5), SecureMonitorConfigurationFlag_ShouldUseBlankCalibrationBinary = (1u << 5),
SecureMonitorConfigurationFlag_AllowWritingToCalibrationBinarySysmmc = (1u << 6), SecureMonitorConfigurationFlag_AllowWritingToCalibrationBinarySysmmc = (1u << 6),
SecureMonitorConfigurationFlag_ForceEnableUsb30 = (1u << 7),
SecureMonitorConfigurationFlag_Default = SecureMonitorConfigurationFlag_IsDevelopmentFunctionEnabledForKernel, SecureMonitorConfigurationFlag_Default = SecureMonitorConfigurationFlag_IsDevelopmentFunctionEnabledForKernel,
}; };
@@ -101,6 +102,7 @@ namespace ams::secmon {
constexpr bool EnableUserModePerformanceCounterAccess() const { return (this->flags[0] & SecureMonitorConfigurationFlag_EnableUserModePerformanceCounterAccess) != 0; } constexpr bool EnableUserModePerformanceCounterAccess() const { return (this->flags[0] & SecureMonitorConfigurationFlag_EnableUserModePerformanceCounterAccess) != 0; }
constexpr bool ShouldUseBlankCalibrationBinary() const { return (this->flags[0] & SecureMonitorConfigurationFlag_ShouldUseBlankCalibrationBinary) != 0; } constexpr bool ShouldUseBlankCalibrationBinary() const { return (this->flags[0] & SecureMonitorConfigurationFlag_ShouldUseBlankCalibrationBinary) != 0; }
constexpr bool AllowWritingToCalibrationBinarySysmmc() const { return (this->flags[0] & SecureMonitorConfigurationFlag_AllowWritingToCalibrationBinarySysmmc) != 0; } constexpr bool AllowWritingToCalibrationBinarySysmmc() const { return (this->flags[0] & SecureMonitorConfigurationFlag_AllowWritingToCalibrationBinarySysmmc) != 0; }
constexpr bool IsUsb30ForceEnabled() const { return (this->flags[0] & SecureMonitorConfigurationFlag_ForceEnableUsb30) != 0; }
constexpr bool IsDevelopmentFunctionEnabled(bool for_kern) const { return for_kern ? this->IsDevelopmentFunctionEnabledForKernel() : this->IsDevelopmentFunctionEnabledForUser(); } constexpr bool IsDevelopmentFunctionEnabled(bool for_kern) const { return for_kern ? this->IsDevelopmentFunctionEnabledForKernel() : this->IsDevelopmentFunctionEnabledForUser(); }
}; };

View File

@@ -38,37 +38,67 @@ namespace ams::kern::arch::arm64::init {
public: public:
class IPageAllocator { class IPageAllocator {
public: public:
virtual KPhysicalAddress Allocate() { return Null<KPhysicalAddress>; } virtual KPhysicalAddress Allocate(size_t size) = 0;
virtual void Free(KPhysicalAddress phys_addr) { /* Nothing to do here. */ (void)(phys_addr); } virtual void Free(KPhysicalAddress phys_addr, size_t size) = 0;
}; };
struct NoClear{};
private: private:
KPhysicalAddress m_l1_table; KPhysicalAddress m_l1_tables[2];
u32 m_num_entries[2];
public: public:
constexpr ALWAYS_INLINE KInitialPageTable(KPhysicalAddress l1, NoClear) : m_l1_table(l1) { /* ... */ } KInitialPageTable(KVirtualAddress start_address, KVirtualAddress end_address, IPageAllocator &allocator) {
/* Set tables. */
m_l1_tables[0] = AllocateNewPageTable(allocator);
m_l1_tables[1] = AllocateNewPageTable(allocator);
constexpr ALWAYS_INLINE KInitialPageTable(KPhysicalAddress l1) : KInitialPageTable(l1, NoClear{}) { /* Set counts. */
ClearNewPageTable(m_l1_table); m_num_entries[0] = MaxPageTableEntries;
m_num_entries[1] = ((end_address / L1BlockSize) & (MaxPageTableEntries - 1)) - ((start_address / L1BlockSize) & (MaxPageTableEntries - 1)) + 1;
} }
constexpr ALWAYS_INLINE uintptr_t GetL1TableAddress() const { KInitialPageTable() {
return GetInteger(m_l1_table); /* Set tables. */
m_l1_tables[0] = util::AlignDown(cpu::GetTtbr0El1(), PageSize);
m_l1_tables[1] = util::AlignDown(cpu::GetTtbr1El1(), PageSize);
/* Set counts. */
cpu::TranslationControlRegisterAccessor tcr;
m_num_entries[0] = tcr.GetT0Size() / L1BlockSize;
m_num_entries[1] = tcr.GetT1Size() / L1BlockSize;
/* Check counts. */
MESOSPHERE_INIT_ABORT_UNLESS(0 < m_num_entries[0] && m_num_entries[0] <= MaxPageTableEntries);
MESOSPHERE_INIT_ABORT_UNLESS(0 < m_num_entries[1] && m_num_entries[1] <= MaxPageTableEntries);
}
constexpr ALWAYS_INLINE uintptr_t GetTtbr0L1TableAddress() const {
return GetInteger(m_l1_tables[0]);
}
constexpr ALWAYS_INLINE uintptr_t GetTtbr1L1TableAddress() const {
return GetInteger(m_l1_tables[1]);
} }
private: private:
static constexpr ALWAYS_INLINE L1PageTableEntry *GetL1Entry(KPhysicalAddress _l1_table, KVirtualAddress address) { constexpr ALWAYS_INLINE L1PageTableEntry *GetL1Entry(KVirtualAddress address) const {
L1PageTableEntry *l1_table = reinterpret_cast<L1PageTableEntry *>(GetInteger(_l1_table)); const size_t index = (GetInteger(address) >> (BITSIZEOF(address) - 1)) & 1;
return l1_table + ((GetInteger(address) >> 30) & (MaxPageTableEntries - 1)); L1PageTableEntry *l1_table = reinterpret_cast<L1PageTableEntry *>(GetInteger(m_l1_tables[index]));
return l1_table + ((GetInteger(address) / L1BlockSize) & (m_num_entries[index] - 1));
} }
static constexpr ALWAYS_INLINE L2PageTableEntry *GetL2Entry(const L1PageTableEntry *entry, KVirtualAddress address) { static constexpr ALWAYS_INLINE L2PageTableEntry *GetL2Entry(const L1PageTableEntry *entry, KVirtualAddress address) {
L2PageTableEntry *l2_table = reinterpret_cast<L2PageTableEntry *>(GetInteger(entry->GetTable())); L2PageTableEntry *l2_table = reinterpret_cast<L2PageTableEntry *>(GetInteger(entry->GetTable()));
return l2_table + ((GetInteger(address) >> 21) & (MaxPageTableEntries - 1)); return l2_table + ((GetInteger(address) / L2BlockSize) & (MaxPageTableEntries - 1));
} }
static constexpr ALWAYS_INLINE L3PageTableEntry *GetL3Entry(const L2PageTableEntry *entry, KVirtualAddress address) { static constexpr ALWAYS_INLINE L3PageTableEntry *GetL3Entry(const L2PageTableEntry *entry, KVirtualAddress address) {
L3PageTableEntry *l3_table = reinterpret_cast<L3PageTableEntry *>(GetInteger(entry->GetTable())); L3PageTableEntry *l3_table = reinterpret_cast<L3PageTableEntry *>(GetInteger(entry->GetTable()));
return l3_table + ((GetInteger(address) >> 12) & (MaxPageTableEntries - 1)); return l3_table + ((GetInteger(address) / L3BlockSize) & (MaxPageTableEntries - 1));
}
static ALWAYS_INLINE KPhysicalAddress AllocateNewPageTable(IPageAllocator &allocator) {
auto address = allocator.Allocate(PageSize);
ClearNewPageTable(address);
return address;
} }
static ALWAYS_INLINE void ClearNewPageTable(KPhysicalAddress address) { static ALWAYS_INLINE void ClearNewPageTable(KPhysicalAddress address) {
@@ -83,7 +113,7 @@ namespace ams::kern::arch::arm64::init {
const KVirtualAddress end_virt_addr = virt_addr + size; const KVirtualAddress end_virt_addr = virt_addr + size;
size_t count = 0; size_t count = 0;
while (virt_addr < end_virt_addr) { while (virt_addr < end_virt_addr) {
L1PageTableEntry *l1_entry = GetL1Entry(m_l1_table, virt_addr); L1PageTableEntry *l1_entry = this->GetL1Entry(virt_addr);
/* If an L1 block is mapped or we're empty, advance by L1BlockSize. */ /* If an L1 block is mapped or we're empty, advance by L1BlockSize. */
if (l1_entry->IsBlock() || l1_entry->IsEmpty()) { if (l1_entry->IsBlock() || l1_entry->IsEmpty()) {
@@ -137,7 +167,7 @@ namespace ams::kern::arch::arm64::init {
const KVirtualAddress end_virt_addr = virt_addr + size; const KVirtualAddress end_virt_addr = virt_addr + size;
size_t count = 0; size_t count = 0;
while (virt_addr < end_virt_addr) { while (virt_addr < end_virt_addr) {
L1PageTableEntry *l1_entry = GetL1Entry(m_l1_table, virt_addr); L1PageTableEntry *l1_entry = this->GetL1Entry(virt_addr);
/* If an L1 block is mapped or we're empty, advance by L1BlockSize. */ /* If an L1 block is mapped or we're empty, advance by L1BlockSize. */
if (l1_entry->IsBlock() || l1_entry->IsEmpty()) { if (l1_entry->IsBlock() || l1_entry->IsEmpty()) {
@@ -194,7 +224,7 @@ namespace ams::kern::arch::arm64::init {
} }
PageTableEntry *GetMappingEntry(KVirtualAddress virt_addr, size_t block_size) { PageTableEntry *GetMappingEntry(KVirtualAddress virt_addr, size_t block_size) {
L1PageTableEntry *l1_entry = GetL1Entry(m_l1_table, virt_addr); L1PageTableEntry *l1_entry = this->GetL1Entry(virt_addr);
if (l1_entry->IsBlock()) { if (l1_entry->IsBlock()) {
MESOSPHERE_INIT_ABORT_UNLESS(block_size == L1BlockSize); MESOSPHERE_INIT_ABORT_UNLESS(block_size == L1BlockSize);
@@ -301,7 +331,7 @@ namespace ams::kern::arch::arm64::init {
/* Iteratively map pages until the requested region is mapped. */ /* Iteratively map pages until the requested region is mapped. */
while (size > 0) { while (size > 0) {
L1PageTableEntry *l1_entry = GetL1Entry(m_l1_table, virt_addr); L1PageTableEntry *l1_entry = this->GetL1Entry(virt_addr);
/* Can we make an L1 block? */ /* Can we make an L1 block? */
if (util::IsAligned(GetInteger(virt_addr), L1BlockSize) && util::IsAligned(GetInteger(phys_addr), L1BlockSize) && size >= L1BlockSize) { if (util::IsAligned(GetInteger(virt_addr), L1BlockSize) && util::IsAligned(GetInteger(phys_addr), L1BlockSize) && size >= L1BlockSize) {
@@ -316,7 +346,7 @@ namespace ams::kern::arch::arm64::init {
/* If we don't already have an L2 table, we need to make a new one. */ /* If we don't already have an L2 table, we need to make a new one. */
if (!l1_entry->IsTable()) { if (!l1_entry->IsTable()) {
KPhysicalAddress new_table = allocator.Allocate(); KPhysicalAddress new_table = AllocateNewPageTable(allocator);
ClearNewPageTable(new_table); ClearNewPageTable(new_table);
*l1_entry = L1PageTableEntry(PageTableEntry::TableTag{}, new_table, attr.IsPrivilegedExecuteNever()); *l1_entry = L1PageTableEntry(PageTableEntry::TableTag{}, new_table, attr.IsPrivilegedExecuteNever());
cpu::DataSynchronizationBarrierInnerShareable(); cpu::DataSynchronizationBarrierInnerShareable();
@@ -350,7 +380,7 @@ namespace ams::kern::arch::arm64::init {
/* If we don't already have an L3 table, we need to make a new one. */ /* If we don't already have an L3 table, we need to make a new one. */
if (!l2_entry->IsTable()) { if (!l2_entry->IsTable()) {
KPhysicalAddress new_table = allocator.Allocate(); KPhysicalAddress new_table = AllocateNewPageTable(allocator);
ClearNewPageTable(new_table); ClearNewPageTable(new_table);
*l2_entry = L2PageTableEntry(PageTableEntry::TableTag{}, new_table, attr.IsPrivilegedExecuteNever()); *l2_entry = L2PageTableEntry(PageTableEntry::TableTag{}, new_table, attr.IsPrivilegedExecuteNever());
cpu::DataSynchronizationBarrierInnerShareable(); cpu::DataSynchronizationBarrierInnerShareable();
@@ -382,7 +412,7 @@ namespace ams::kern::arch::arm64::init {
KPhysicalAddress GetPhysicalAddress(KVirtualAddress virt_addr) const { KPhysicalAddress GetPhysicalAddress(KVirtualAddress virt_addr) const {
/* Get the L1 entry. */ /* Get the L1 entry. */
const L1PageTableEntry *l1_entry = GetL1Entry(m_l1_table, virt_addr); const L1PageTableEntry *l1_entry = this->GetL1Entry(virt_addr);
if (l1_entry->IsBlock()) { if (l1_entry->IsBlock()) {
return l1_entry->GetBlock() + (GetInteger(virt_addr) & (L1BlockSize - 1)); return l1_entry->GetBlock() + (GetInteger(virt_addr) & (L1BlockSize - 1));
@@ -444,7 +474,7 @@ namespace ams::kern::arch::arm64::init {
}; };
while (virt_addr < end_virt_addr) { while (virt_addr < end_virt_addr) {
L1PageTableEntry *l1_entry = GetL1Entry(m_l1_table, virt_addr); L1PageTableEntry *l1_entry = this->GetL1Entry(virt_addr);
/* If an L1 block is mapped, update. */ /* If an L1 block is mapped, update. */
if (l1_entry->IsBlock()) { if (l1_entry->IsBlock()) {
@@ -485,7 +515,7 @@ namespace ams::kern::arch::arm64::init {
const KVirtualAddress end_virt_addr = virt_addr + size; const KVirtualAddress end_virt_addr = virt_addr + size;
while (virt_addr < end_virt_addr) { while (virt_addr < end_virt_addr) {
L1PageTableEntry *l1_entry = GetL1Entry(m_l1_table, virt_addr); L1PageTableEntry *l1_entry = this->GetL1Entry(virt_addr);
/* If an L1 block is mapped, the address isn't free. */ /* If an L1 block is mapped, the address isn't free. */
if (l1_entry->IsBlock()) { if (l1_entry->IsBlock()) {
@@ -534,7 +564,7 @@ namespace ams::kern::arch::arm64::init {
/* Iteratively reprotect pages until the requested region is reprotected. */ /* Iteratively reprotect pages until the requested region is reprotected. */
while (size > 0) { while (size > 0) {
L1PageTableEntry *l1_entry = GetL1Entry(m_l1_table, virt_addr); L1PageTableEntry *l1_entry = this->GetL1Entry(virt_addr);
/* Check if an L1 block is present. */ /* Check if an L1 block is present. */
if (l1_entry->IsBlock()) { if (l1_entry->IsBlock()) {
@@ -673,11 +703,18 @@ namespace ams::kern::arch::arm64::init {
}; };
class KInitialPageAllocator : public KInitialPageTable::IPageAllocator { class KInitialPageAllocator final : public KInitialPageTable::IPageAllocator {
private:
static constexpr inline size_t FreeUnitSize = BITSIZEOF(u64) * PageSize;
struct FreeListEntry {
FreeListEntry *next;
size_t size;
};
public: public:
struct State { struct State {
uintptr_t next_address; uintptr_t start_address;
uintptr_t free_bitmap; uintptr_t end_address;
FreeListEntry *free_head;
}; };
private: private:
State m_state; State m_state;
@@ -685,8 +722,8 @@ namespace ams::kern::arch::arm64::init {
constexpr ALWAYS_INLINE KInitialPageAllocator() : m_state{} { /* ... */ } constexpr ALWAYS_INLINE KInitialPageAllocator() : m_state{} { /* ... */ }
ALWAYS_INLINE void Initialize(uintptr_t address) { ALWAYS_INLINE void Initialize(uintptr_t address) {
m_state.next_address = address + BITSIZEOF(m_state.free_bitmap) * PageSize; m_state.start_address = address;
m_state.free_bitmap = ~uintptr_t(); m_state.end_address = address;
} }
ALWAYS_INLINE void InitializeFromState(uintptr_t state_val) { ALWAYS_INLINE void InitializeFromState(uintptr_t state_val) {
@@ -697,28 +734,134 @@ namespace ams::kern::arch::arm64::init {
*out = m_state; *out = m_state;
m_state = {}; m_state = {};
} }
public: private:
virtual KPhysicalAddress Allocate() override { bool CanAllocate(size_t align, size_t size) const {
MESOSPHERE_INIT_ABORT_UNLESS(m_state.next_address != Null<uintptr_t>); for (auto *cur = m_state.free_head; cur != nullptr; cur = cur->next) {
uintptr_t allocated = m_state.next_address; const uintptr_t cur_last = reinterpret_cast<uintptr_t>(cur) + cur->size - 1;
if (m_state.free_bitmap != 0) { const uintptr_t alloc_last = util::AlignUp(reinterpret_cast<uintptr_t>(cur), align) + size - 1;
u64 index; if (alloc_last <= cur_last) {
uintptr_t mask; return true;
do { }
index = KSystemControl::Init::GenerateRandomRange(0, BITSIZEOF(m_state.free_bitmap) - 1);
mask = (static_cast<uintptr_t>(1) << index);
} while ((m_state.free_bitmap & mask) == 0);
m_state.free_bitmap &= ~mask;
allocated = m_state.next_address - ((BITSIZEOF(m_state.free_bitmap) - index) * PageSize);
} else {
m_state.next_address += PageSize;
} }
return false;
ClearPhysicalMemory(allocated, PageSize);
return allocated;
} }
/* No need to override free. The default does nothing, and so would we. */ bool TryAllocate(uintptr_t address, size_t size) {
/* Try to allocate the region. */
auto **prev_next = std::addressof(m_state.free_head);
for (auto *cur = m_state.free_head; cur != nullptr; prev_next = std::addressof(cur->next), cur = cur->next) {
const uintptr_t cur_start = reinterpret_cast<uintptr_t>(cur);
const uintptr_t cur_last = cur_start + cur->size - 1;
if (cur_start <= address && address + size - 1 <= cur_last) {
auto *alloc = reinterpret_cast<FreeListEntry *>(address);
/* Perform fragmentation at front. */
if (cur != alloc) {
prev_next = std::addressof(cur->next);
*alloc = {
.next = cur->next,
.size = cur_start + cur->size - address,
};
*cur = {
.next = alloc,
.size = address - cur_start,
};
}
/* Perform fragmentation at tail. */
if (alloc->size != size) {
auto *next = reinterpret_cast<FreeListEntry *>(address + size);
*next = {
.next = alloc->next,
.size = alloc->size - size,
};
*alloc = {
.next = next,
.size = size,
};
}
*prev_next = alloc->next;
return true;
}
}
return false;
}
public:
KPhysicalAddress Allocate(size_t align, size_t size) {
/* Ensure that the free list is non-empty. */
while (!this->CanAllocate(align, size)) {
this->Free(m_state.end_address, FreeUnitSize);
m_state.end_address += FreeUnitSize;
}
/* Allocate a random address. */
const uintptr_t aligned_start = util::AlignUp(m_state.start_address, align);
const uintptr_t aligned_end = util::AlignDown(m_state.end_address, align);
const size_t ind_max = ((aligned_end - aligned_start) / align) - 1;
while (true) {
if (const uintptr_t random_address = aligned_start + (KSystemControl::Init::GenerateRandomRange(0, ind_max) * align); this->TryAllocate(random_address, size)) {
return random_address;
}
}
}
virtual KPhysicalAddress Allocate(size_t size) override {
return this->Allocate(size, size);
}
virtual void Free(KPhysicalAddress phys_addr, size_t size) override {
auto **prev_next = std::addressof(m_state.free_head);
auto *new_chunk = reinterpret_cast<FreeListEntry *>(GetInteger(phys_addr));
if (auto *cur = m_state.free_head; cur != nullptr) {
const uintptr_t new_start = reinterpret_cast<uintptr_t>(new_chunk);
const uintptr_t new_end = GetInteger(phys_addr) + size;
while (true) {
/* Attempt coalescing. */
const uintptr_t cur_start = reinterpret_cast<uintptr_t>(cur);
const uintptr_t cur_end = cur_start + cur->size;
if (new_start < new_end) {
if (new_end < cur_start) {
*new_chunk = {
.next = cur,
.size = size,
};
break;
} else if (new_end == cur_start) {
*new_chunk = {
.next = cur->next,
.size = cur->size + size,
};
break;
}
} else if (cur_end == new_start) {
cur->size += size;
return;
}
prev_next = std::addressof(cur->next);
if (cur->next != nullptr) {
cur = cur->next;
} else {
*new_chunk = {
.next = nullptr,
.size = size,
};
cur->next = new_chunk;
return;
}
}
} else {
*new_chunk = {
.next = nullptr,
.size = size,
};
}
*prev_next = new_chunk;
}
}; };
} }

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#define THREAD_STACK_PARAMETERS_SIZE 0x30
#define THREAD_STACK_PARAMETERS_SVC_PERMISSION 0x00
#define THREAD_STACK_PARAMETERS_CONTEXT 0x18
#define THREAD_STACK_PARAMETERS_CUR_THREAD 0x20
#define THREAD_STACK_PARAMETERS_DISABLE_COUNT 0x28
#define THREAD_STACK_PARAMETERS_DPC_FLAGS 0x2A
#define THREAD_STACK_PARAMETERS_CURRENT_SVC_ID 0x2B
#define THREAD_STACK_PARAMETERS_IS_CALLING_SVC 0x2C
#define THREAD_STACK_PARAMETERS_IS_IN_EXCEPTION_HANDLER 0x2D
#define THREAD_STACK_PARAMETERS_IS_PINNED 0x2E

View File

@@ -232,7 +232,7 @@ namespace ams::kern::arch::arm64::cpu {
} }
ALWAYS_INLINE void SetExceptionThreadStackTop(uintptr_t top) { ALWAYS_INLINE void SetExceptionThreadStackTop(uintptr_t top) {
SetTpidrEl1(top); cpu::SetCntvCvalEl0(top);
} }
ALWAYS_INLINE void SwitchThreadLocalRegion(uintptr_t tlr) { ALWAYS_INLINE void SwitchThreadLocalRegion(uintptr_t tlr) {

View File

@@ -74,6 +74,7 @@ namespace ams::kern::arch::arm64::cpu {
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CntkCtlEl1, cntkctl_el1) MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CntkCtlEl1, cntkctl_el1)
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CntpCtlEl0, cntp_ctl_el0) MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CntpCtlEl0, cntp_ctl_el0)
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CntpCvalEl0, cntp_cval_el0) MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CntpCvalEl0, cntp_cval_el0)
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CntvCvalEl0, cntv_cval_el0)
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(Daif, daif) MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(Daif, daif)
@@ -197,6 +198,11 @@ namespace ams::kern::arch::arm64::cpu {
public: public:
MESOSPHERE_CPU_SYSREG_ACCESSOR_CLASS_FUNCTIONS(TranslationControl, tcr_el1) MESOSPHERE_CPU_SYSREG_ACCESSOR_CLASS_FUNCTIONS(TranslationControl, tcr_el1)
constexpr ALWAYS_INLINE size_t GetT0Size() const {
const size_t shift_value = this->GetBits(0, 6);
return size_t(1) << (size_t(64) - shift_value);
}
constexpr ALWAYS_INLINE size_t GetT1Size() const { constexpr ALWAYS_INLINE size_t GetT1Size() const {
const size_t shift_value = this->GetBits(16, 6); const size_t shift_value = this->GetBits(16, 6);
return size_t(1) << (size_t(64) - shift_value); return size_t(1) << (size_t(64) - shift_value);

View File

@@ -85,7 +85,6 @@ namespace ams::kern::arch::arm64 {
NOINLINE Result BindHandler(KInterruptHandler *handler, s32 irq, s32 core_id, s32 priority, bool manual_clear, bool level); NOINLINE Result BindHandler(KInterruptHandler *handler, s32 irq, s32 core_id, s32 priority, bool manual_clear, bool level);
NOINLINE Result UnbindHandler(s32 irq, s32 core); NOINLINE Result UnbindHandler(s32 irq, s32 core);
NOINLINE Result ClearInterrupt(s32 irq);
NOINLINE Result ClearInterrupt(s32 irq, s32 core_id); NOINLINE Result ClearInterrupt(s32 irq, s32 core_id);
ALWAYS_INLINE void SendInterProcessorInterrupt(s32 irq, u64 core_mask) { ALWAYS_INLINE void SendInterProcessorInterrupt(s32 irq, u64 core_mask) {

View File

@@ -176,7 +176,7 @@ namespace ams::kern::arch::arm64 {
} }
NOINLINE Result InitializeForKernel(void *table, KVirtualAddress start, KVirtualAddress end); NOINLINE Result InitializeForKernel(void *table, KVirtualAddress start, KVirtualAddress end);
NOINLINE Result InitializeForProcess(u32 id, ams::svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_das_merge, bool from_back, KMemoryManager::Pool pool, KProcessAddress code_address, size_t code_size, KMemoryBlockSlabManager *mem_block_slab_manager, KBlockInfoManager *block_info_manager, KPageTableManager *pt_manager); NOINLINE Result InitializeForProcess(u32 id, ams::svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_das_merge, bool from_back, KMemoryManager::Pool pool, KProcessAddress code_address, size_t code_size, KMemoryBlockSlabManager *mem_block_slab_manager, KBlockInfoManager *block_info_manager, KPageTableManager *pt_manager, KResourceLimit *resource_limit);
Result Finalize(); Result Finalize();
private: private:
Result MapL1Blocks(KProcessAddress virt_addr, KPhysicalAddress phys_addr, size_t num_pages, PageTableEntry entry_template, bool disable_head_merge, PageLinkedList *page_list, bool reuse_ll); Result MapL1Blocks(KProcessAddress virt_addr, KPhysicalAddress phys_addr, size_t num_pages, PageTableEntry entry_template, bool disable_head_merge, PageLinkedList *page_list, bool reuse_ll);

View File

@@ -30,12 +30,16 @@ namespace ams::kern::arch::arm64 {
m_page_table.Activate(id); m_page_table.Activate(id);
} }
Result Initialize(u32 id, ams::svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_das_merge, bool from_back, KMemoryManager::Pool pool, KProcessAddress code_address, size_t code_size, KMemoryBlockSlabManager *mem_block_slab_manager, KBlockInfoManager *block_info_manager, KPageTableManager *pt_manager) { Result Initialize(u32 id, ams::svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_das_merge, bool from_back, KMemoryManager::Pool pool, KProcessAddress code_address, size_t code_size, KMemoryBlockSlabManager *mem_block_slab_manager, KBlockInfoManager *block_info_manager, KPageTableManager *pt_manager, KResourceLimit *resource_limit) {
return m_page_table.InitializeForProcess(id, as_type, enable_aslr, enable_das_merge, from_back, pool, code_address, code_size, mem_block_slab_manager, block_info_manager, pt_manager); return m_page_table.InitializeForProcess(id, as_type, enable_aslr, enable_das_merge, from_back, pool, code_address, code_size, mem_block_slab_manager, block_info_manager, pt_manager, resource_limit);
} }
void Finalize() { m_page_table.Finalize(); } void Finalize() { m_page_table.Finalize(); }
ALWAYS_INLINE KScopedLightLock AcquireDeviceMapLock() {
return m_page_table.AcquireDeviceMapLock();
}
Result SetMemoryPermission(KProcessAddress addr, size_t size, ams::svc::MemoryPermission perm) { Result SetMemoryPermission(KProcessAddress addr, size_t size, ams::svc::MemoryPermission perm) {
return m_page_table.SetMemoryPermission(addr, size, perm); return m_page_table.SetMemoryPermission(addr, size, perm);
} }
@@ -136,26 +140,42 @@ namespace ams::kern::arch::arm64 {
return m_page_table.ReadDebugMemory(buffer, address, size); return m_page_table.ReadDebugMemory(buffer, address, size);
} }
Result ReadDebugIoMemory(void *buffer, KProcessAddress address, size_t size) {
return m_page_table.ReadDebugIoMemory(buffer, address, size);
}
Result WriteDebugMemory(KProcessAddress address, const void *buffer, size_t size) { Result WriteDebugMemory(KProcessAddress address, const void *buffer, size_t size) {
return m_page_table.WriteDebugMemory(address, buffer, size); return m_page_table.WriteDebugMemory(address, buffer, size);
} }
Result LockForDeviceAddressSpace(KPageGroup *out, KProcessAddress address, size_t size, KMemoryPermission perm, bool is_aligned) { Result WriteDebugIoMemory(KProcessAddress address, const void *buffer, size_t size) {
return m_page_table.LockForDeviceAddressSpace(out, address, size, perm, is_aligned); return m_page_table.WriteDebugIoMemory(address, buffer, size);
}
Result LockForMapDeviceAddressSpace(KProcessAddress address, size_t size, KMemoryPermission perm, bool is_aligned) {
return m_page_table.LockForMapDeviceAddressSpace(address, size, perm, is_aligned);
}
Result LockForUnmapDeviceAddressSpace(KProcessAddress address, size_t size) {
return m_page_table.LockForUnmapDeviceAddressSpace(address, size);
} }
Result UnlockForDeviceAddressSpace(KProcessAddress address, size_t size) { Result UnlockForDeviceAddressSpace(KProcessAddress address, size_t size) {
return m_page_table.UnlockForDeviceAddressSpace(address, size); return m_page_table.UnlockForDeviceAddressSpace(address, size);
} }
Result MakePageGroupForUnmapDeviceAddressSpace(KPageGroup *out, KProcessAddress address, size_t size) {
return m_page_table.MakePageGroupForUnmapDeviceAddressSpace(out, address, size);
}
Result UnlockForDeviceAddressSpacePartialMap(KProcessAddress address, size_t size, size_t mapped_size) { Result UnlockForDeviceAddressSpacePartialMap(KProcessAddress address, size_t size, size_t mapped_size) {
return m_page_table.UnlockForDeviceAddressSpacePartialMap(address, size, mapped_size); return m_page_table.UnlockForDeviceAddressSpacePartialMap(address, size, mapped_size);
} }
Result OpenMemoryRangeForMapDeviceAddressSpace(KPageTableBase::MemoryRange *out, KProcessAddress address, size_t size, KMemoryPermission perm, bool is_aligned) {
return m_page_table.OpenMemoryRangeForMapDeviceAddressSpace(out, address, size, perm, is_aligned);
}
Result OpenMemoryRangeForUnmapDeviceAddressSpace(KPageTableBase::MemoryRange *out, KProcessAddress address, size_t size) {
return m_page_table.OpenMemoryRangeForUnmapDeviceAddressSpace(out, address, size);
}
Result LockForIpcUserBuffer(KPhysicalAddress *out, KProcessAddress address, size_t size) { Result LockForIpcUserBuffer(KPhysicalAddress *out, KProcessAddress address, size_t size) {
return m_page_table.LockForIpcUserBuffer(out, address, size); return m_page_table.LockForIpcUserBuffer(out, address, size);
} }
@@ -180,6 +200,10 @@ namespace ams::kern::arch::arm64 {
return m_page_table.UnlockForCodeMemory(address, size, pg); return m_page_table.UnlockForCodeMemory(address, size, pg);
} }
Result OpenMemoryRangeForProcessCacheOperation(KPageTableBase::MemoryRange *out, KProcessAddress address, size_t size) {
return m_page_table.OpenMemoryRangeForProcessCacheOperation(out, address, size);
}
Result CopyMemoryFromLinearToUser(KProcessAddress dst_addr, size_t size, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr) { Result CopyMemoryFromLinearToUser(KProcessAddress dst_addr, size_t size, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr) {
return m_page_table.CopyMemoryFromLinearToUser(dst_addr, size, src_addr, src_state_mask, src_state, src_test_perm, src_attr_mask, src_attr); return m_page_table.CopyMemoryFromLinearToUser(dst_addr, size, src_addr, src_state_mask, src_state, src_test_perm, src_attr_mask, src_attr);
} }
@@ -208,8 +232,8 @@ namespace ams::kern::arch::arm64 {
return m_page_table.SetupForIpc(out_dst_addr, size, src_addr, src_page_table.m_page_table, test_perm, dst_state, send); return m_page_table.SetupForIpc(out_dst_addr, size, src_addr, src_page_table.m_page_table, test_perm, dst_state, send);
} }
Result CleanupForIpcServer(KProcessAddress address, size_t size, KMemoryState dst_state, KProcess *server_process) { Result CleanupForIpcServer(KProcessAddress address, size_t size, KMemoryState dst_state) {
return m_page_table.CleanupForIpcServer(address, size, dst_state, server_process); return m_page_table.CleanupForIpcServer(address, size, dst_state);
} }
Result CleanupForIpcClient(KProcessAddress address, size_t size, KMemoryState dst_state) { Result CleanupForIpcClient(KProcessAddress address, size_t size, KMemoryState dst_state) {
@@ -232,6 +256,10 @@ namespace ams::kern::arch::arm64 {
return m_page_table.UnmapPhysicalMemoryUnsafe(address, size); return m_page_table.UnmapPhysicalMemoryUnsafe(address, size);
} }
Result UnmapProcessMemory(KProcessAddress dst_address, size_t size, KProcessPageTable &src_page_table, KProcessAddress src_address) {
return m_page_table.UnmapProcessMemory(dst_address, size, src_page_table.m_page_table, src_address);
}
void DumpMemoryBlocks() const { void DumpMemoryBlocks() const {
return m_page_table.DumpMemoryBlocks(); return m_page_table.DumpMemoryBlocks();
} }
@@ -290,6 +318,10 @@ namespace ams::kern::arch::arm64 {
KBlockInfoManager *GetBlockInfoManager() { KBlockInfoManager *GetBlockInfoManager() {
return m_page_table.GetBlockInfoManager(); return m_page_table.GetBlockInfoManager();
} }
KPageTableBase &GetBasePageTable() {
return m_page_table;
}
}; };
} }

View File

@@ -69,8 +69,8 @@ namespace ams::kern::board::nintendo::nx {
Result Attach(ams::svc::DeviceName device_name, u64 space_address, u64 space_size); Result Attach(ams::svc::DeviceName device_name, u64 space_address, u64 space_size);
Result Detach(ams::svc::DeviceName device_name); Result Detach(ams::svc::DeviceName device_name);
Result Map(size_t *out_mapped_size, const KPageGroup &pg, KDeviceVirtualAddress device_address, ams::svc::MemoryPermission device_perm, bool refresh_mappings); Result Map(size_t *out_mapped_size, KProcessPageTable *page_table, KProcessAddress process_address, size_t size, KDeviceVirtualAddress device_address, ams::svc::MemoryPermission device_perm, bool refresh_mappings);
Result Unmap(const KPageGroup &pg, KDeviceVirtualAddress device_address); Result Unmap(KProcessPageTable *page_table, KProcessAddress process_address, size_t size, KDeviceVirtualAddress device_address);
void Unmap(KDeviceVirtualAddress device_address, size_t size) { void Unmap(KDeviceVirtualAddress device_address, size_t size) {
return this->UnmapImpl(device_address, size, false); return this->UnmapImpl(device_address, size, false);
@@ -78,12 +78,11 @@ namespace ams::kern::board::nintendo::nx {
private: private:
Result MapDevicePage(size_t *out_mapped_size, s32 &num_pt, s32 max_pt, KPhysicalAddress phys_addr, u64 size, KDeviceVirtualAddress address, ams::svc::MemoryPermission device_perm); Result MapDevicePage(size_t *out_mapped_size, s32 &num_pt, s32 max_pt, KPhysicalAddress phys_addr, u64 size, KDeviceVirtualAddress address, ams::svc::MemoryPermission device_perm);
Result MapImpl(size_t *out_mapped_size, s32 &num_pt, s32 max_pt, const KPageGroup &pg, KDeviceVirtualAddress device_address, ams::svc::MemoryPermission device_perm); Result MapImpl(size_t *out_mapped_size, s32 &num_pt, s32 max_pt, KProcessPageTable *page_table, KProcessAddress process_address, size_t size, KDeviceVirtualAddress device_address, ams::svc::MemoryPermission device_perm, bool is_aligned);
void UnmapImpl(KDeviceVirtualAddress address, u64 size, bool force); void UnmapImpl(KDeviceVirtualAddress address, u64 size, bool force);
bool IsFree(KDeviceVirtualAddress address, u64 size) const; bool IsFree(KDeviceVirtualAddress address, u64 size) const;
Result MakePageGroup(KPageGroup *out, KDeviceVirtualAddress address, u64 size) const; bool Compare(KProcessPageTable *page_table, KProcessAddress process_address, size_t size, KDeviceVirtualAddress device_address) const;
bool Compare(const KPageGroup &pg, KDeviceVirtualAddress device_address) const;
public: public:
static void Initialize(); static void Initialize();

View File

@@ -25,6 +25,7 @@ namespace ams::kern::board::nintendo::nx {
/* Initialization. */ /* Initialization. */
static size_t GetIntendedMemorySize(); static size_t GetIntendedMemorySize();
static KPhysicalAddress GetKernelPhysicalBaseAddress(uintptr_t base_address); static KPhysicalAddress GetKernelPhysicalBaseAddress(uintptr_t base_address);
static KPhysicalAddress GetInitialProcessBinaryPhysicalAddress();
static bool ShouldIncreaseThreadResourceLimit(); static bool ShouldIncreaseThreadResourceLimit();
static void CpuOn(u64 core_id, uintptr_t entrypoint, uintptr_t arg); static void CpuOn(u64 core_id, uintptr_t entrypoint, uintptr_t arg);
static size_t GetApplicationPoolSize(); static size_t GetApplicationPoolSize();

View File

@@ -27,7 +27,7 @@ namespace ams::kern::init {
u32 rw_end_offset; u32 rw_end_offset;
u32 bss_offset; u32 bss_offset;
u32 bss_end_offset; u32 bss_end_offset;
u32 ini_load_offset; u32 resource_offset;
u32 dynamic_offset; u32 dynamic_offset;
u32 init_array_offset; u32 init_array_offset;
u32 init_array_end_offset; u32 init_array_end_offset;

View File

@@ -17,6 +17,7 @@
#include <vapours.hpp> #include <vapours.hpp>
#include <mesosphere/kern_build_config.hpp> #include <mesosphere/kern_build_config.hpp>
#include <mesosphere/svc/kern_svc_results.hpp> #include <mesosphere/svc/kern_svc_results.hpp>
#include <mesosphere/kern_select_assembly_offsets.h>
namespace ams::kern { namespace ams::kern {

View File

@@ -29,11 +29,12 @@ namespace ams::kern {
u32 reserved; u32 reserved;
}; };
NOINLINE void CopyInitialProcessBinaryToKernelMemory(); NOINLINE size_t CopyInitialProcessBinaryToKernelMemory();
NOINLINE void CreateAndRunInitialProcesses(); NOINLINE void CreateAndRunInitialProcesses();
u64 GetInitialProcessIdMin(); u64 GetInitialProcessIdMin();
u64 GetInitialProcessIdMax(); u64 GetInitialProcessIdMax();
KVirtualAddress GetInitialProcessBinaryAddress();
size_t GetInitialProcessesSecureMemorySize(); size_t GetInitialProcessesSecureMemorySize();
} }

View File

@@ -69,11 +69,12 @@ namespace ams::kern {
private: private:
MESOSPHERE_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject); MESOSPHERE_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject);
private: private:
KAutoObject *m_next_closed_object;
std::atomic<u32> m_ref_count; std::atomic<u32> m_ref_count;
public: public:
static KAutoObject *Create(KAutoObject *ptr); static KAutoObject *Create(KAutoObject *ptr);
public: public:
constexpr ALWAYS_INLINE explicit KAutoObject() : m_ref_count(0) { MESOSPHERE_ASSERT_THIS(); } constexpr ALWAYS_INLINE explicit KAutoObject() : m_next_closed_object(nullptr), m_ref_count(0) { MESOSPHERE_ASSERT_THIS(); }
virtual ~KAutoObject() { MESOSPHERE_ASSERT_THIS(); } virtual ~KAutoObject() { MESOSPHERE_ASSERT_THIS(); }
/* Destroy is responsible for destroying the auto object's resources when ref_count hits zero. */ /* Destroy is responsible for destroying the auto object's resources when ref_count hits zero. */
@@ -120,7 +121,7 @@ namespace ams::kern {
} }
} }
ALWAYS_INLINE bool Open() { NOINLINE bool Open() {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
/* Atomically increment the reference count, only if it's positive. */ /* Atomically increment the reference count, only if it's positive. */
@@ -136,7 +137,7 @@ namespace ams::kern {
return true; return true;
} }
ALWAYS_INLINE void Close() { NOINLINE void Close() {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
/* Atomically decrement the reference count, not allowing it to become negative. */ /* Atomically decrement the reference count, not allowing it to become negative. */
@@ -145,11 +146,19 @@ namespace ams::kern {
MESOSPHERE_ABORT_UNLESS(cur_ref_count > 0); MESOSPHERE_ABORT_UNLESS(cur_ref_count > 0);
} while (!m_ref_count.compare_exchange_weak(cur_ref_count, cur_ref_count - 1, std::memory_order_relaxed)); } while (!m_ref_count.compare_exchange_weak(cur_ref_count, cur_ref_count - 1, std::memory_order_relaxed));
/* If ref count hits zero, destroy the object. */ /* If ref count hits zero, schedule the object for destruction. */
if (cur_ref_count - 1 == 0) { if (cur_ref_count - 1 == 0) {
this->Destroy(); this->ScheduleDestruction();
} }
} }
private:
/* NOTE: This has to be defined *after* KThread is defined. */
/* Nintendo seems to handle this by defining Open/Close() in a cpp, but we'd like them to remain in headers. */
/* Implementation for this will be inside kern_k_thread.hpp, so it can be ALWAYS_INLINE. */
void ScheduleDestruction();
public:
/* Getter, for KThread. */
ALWAYS_INLINE KAutoObject *GetNextClosedObject() { return m_next_closed_object; }
}; };
class KAutoObjectWithListContainer; class KAutoObjectWithListContainer;
@@ -198,7 +207,7 @@ namespace ams::kern {
} }
} }
~KScopedAutoObject() { ALWAYS_INLINE ~KScopedAutoObject() {
if (m_obj != nullptr) { if (m_obj != nullptr) {
m_obj->Close(); m_obj->Close();
} }

View File

@@ -47,6 +47,7 @@ namespace ams::kern {
ALWAYS_INLINE s32 GetMaxSessions() const { return m_max_sessions; } ALWAYS_INLINE s32 GetMaxSessions() const { return m_max_sessions; }
bool IsLight() const; bool IsLight() const;
bool IsServerClosed() const;
/* Overridden virtual functions. */ /* Overridden virtual functions. */
virtual void Destroy() override; virtual void Destroy() override;

View File

@@ -23,7 +23,7 @@ namespace ams::kern {
class KCodeMemory final : public KAutoObjectWithSlabHeapAndContainer<KCodeMemory, KAutoObjectWithList> { class KCodeMemory final : public KAutoObjectWithSlabHeapAndContainer<KCodeMemory, KAutoObjectWithList> {
MESOSPHERE_AUTOOBJECT_TRAITS(KCodeMemory, KAutoObject); MESOSPHERE_AUTOOBJECT_TRAITS(KCodeMemory, KAutoObject);
private: private:
TYPED_STORAGE(KPageGroup) m_page_group; util::TypedStorage<KPageGroup> m_page_group;
KProcess *m_owner; KProcess *m_owner;
KProcessAddress m_address; KProcessAddress m_address;
KLightLock m_lock; KLightLock m_lock;

View File

@@ -36,7 +36,7 @@ namespace ams::kern {
void Signal(uintptr_t cv_key, s32 count); void Signal(uintptr_t cv_key, s32 count);
Result Wait(KProcessAddress addr, uintptr_t key, u32 value, s64 timeout); Result Wait(KProcessAddress addr, uintptr_t key, u32 value, s64 timeout);
private: private:
KThread *SignalImpl(KThread *thread); void SignalImpl(KThread *thread);
}; };
ALWAYS_INLINE void BeforeUpdatePriority(KConditionVariable::ThreadTree *tree, KThread *thread) { ALWAYS_INLINE void BeforeUpdatePriority(KConditionVariable::ThreadTree *tree, KThread *thread) {

View File

@@ -113,7 +113,7 @@ namespace ams::kern {
if (AMS_LIKELY(allocated != nullptr)) { if (AMS_LIKELY(allocated != nullptr)) {
/* Construct the object. */ /* Construct the object. */
new (allocated) T(); std::construct_at(allocated);
/* Update our tracking. */ /* Update our tracking. */
size_t used = m_used.fetch_add(1) + 1; size_t used = m_used.fetch_add(1) + 1;

View File

@@ -53,46 +53,29 @@ namespace ams::kern {
return pack.Get<HandleEncoded>(); return pack.Get<HandleEncoded>();
} }
class Entry { union EntryInfo {
private: struct {
union { u16 linear_id;
struct { u16 type;
u16 linear_id; } info;
u16 type; s32 next_free_index;
} info;
Entry *next_free_entry;
} m_meta;
KAutoObject *m_object;
public:
constexpr Entry() : m_meta(), m_object(nullptr) { /* ... */ }
constexpr ALWAYS_INLINE void SetFree(Entry *next) { constexpr ALWAYS_INLINE u16 GetLinearId() const { return info.linear_id; }
m_object = nullptr; constexpr ALWAYS_INLINE u16 GetType() const { return info.type; }
m_meta.next_free_entry = next; constexpr ALWAYS_INLINE s32 GetNextFreeIndex() const { return next_free_index; }
}
constexpr ALWAYS_INLINE void SetUsed(KAutoObject *obj, u16 linear_id, u16 type) {
m_object = obj;
m_meta.info = { linear_id, type };
}
constexpr ALWAYS_INLINE KAutoObject *GetObject() const { return m_object; }
constexpr ALWAYS_INLINE Entry *GetNextFreeEntry() const { return m_meta.next_free_entry; }
constexpr ALWAYS_INLINE u16 GetLinearId() const { return m_meta.info.linear_id; }
constexpr ALWAYS_INLINE u16 GetType() const { return m_meta.info.type; }
}; };
private: private:
mutable KSpinLock m_lock; EntryInfo m_entry_infos[MaxTableSize];
Entry *m_table; KAutoObject *m_objects[MaxTableSize];
Entry *m_free_head; s32 m_free_head_index;
Entry m_entries[MaxTableSize];
u16 m_table_size; u16 m_table_size;
u16 m_max_count; u16 m_max_count;
u16 m_next_linear_id; u16 m_next_linear_id;
u16 m_count; u16 m_count;
mutable KSpinLock m_lock;
public: public:
constexpr KHandleTable() : constexpr KHandleTable() :
m_lock(), m_table(nullptr), m_free_head(nullptr), m_entries(), m_table_size(0), m_max_count(0), m_next_linear_id(MinLinearId), m_count(0) m_entry_infos(), m_objects(), m_free_head_index(-1), m_table_size(0), m_max_count(0), m_next_linear_id(MinLinearId), m_count(0), m_lock()
{ MESOSPHERE_ASSERT_THIS(); } { MESOSPHERE_ASSERT_THIS(); }
constexpr NOINLINE Result Initialize(s32 size) { constexpr NOINLINE Result Initialize(s32 size) {
@@ -101,19 +84,18 @@ namespace ams::kern {
R_UNLESS(size <= static_cast<s32>(MaxTableSize), svc::ResultOutOfMemory()); R_UNLESS(size <= static_cast<s32>(MaxTableSize), svc::ResultOutOfMemory());
/* Initialize all fields. */ /* Initialize all fields. */
m_table = m_entries; m_max_count = 0;
m_table_size = (size <= 0) ? MaxTableSize : size; m_table_size = (size <= 0) ? MaxTableSize : size;
m_next_linear_id = MinLinearId; m_next_linear_id = MinLinearId;
m_count = 0; m_count = 0;
m_max_count = 0; m_free_head_index = -1;
/* Free all entries. */ /* Free all entries. */
for (size_t i = 0; i < static_cast<size_t>(m_table_size - 1); i++) { for (s32 i = 0; i < static_cast<s32>(m_table_size); ++i) {
m_entries[i].SetFree(std::addressof(m_entries[i + 1])); m_objects[i] = nullptr;
m_entry_infos[i].next_free_index = i - 1;
m_free_head_index = i;
} }
m_entries[m_table_size - 1].SetFree(nullptr);
m_free_head = std::addressof(m_entries[0]);
return ResultSuccess(); return ResultSuccess();
} }
@@ -134,7 +116,7 @@ namespace ams::kern {
if constexpr (std::is_same<T, KAutoObject>::value) { if constexpr (std::is_same<T, KAutoObject>::value) {
return this->GetObjectImpl(handle); return this->GetObjectImpl(handle);
} else { } else {
if (auto *obj = this->GetObjectImpl(handle); obj != nullptr) { if (auto *obj = this->GetObjectImpl(handle); AMS_LIKELY(obj != nullptr)) {
return obj->DynamicCast<T*>(); return obj->DynamicCast<T*>();
} else { } else {
return nullptr; return nullptr;
@@ -256,27 +238,29 @@ namespace ams::kern {
NOINLINE Result Add(ams::svc::Handle *out_handle, KAutoObject *obj, u16 type); NOINLINE Result Add(ams::svc::Handle *out_handle, KAutoObject *obj, u16 type);
NOINLINE void Register(ams::svc::Handle handle, KAutoObject *obj, u16 type); NOINLINE void Register(ams::svc::Handle handle, KAutoObject *obj, u16 type);
constexpr ALWAYS_INLINE Entry *AllocateEntry() { constexpr ALWAYS_INLINE s32 AllocateEntry() {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(m_count < m_table_size); MESOSPHERE_ASSERT(m_count < m_table_size);
Entry *entry = m_free_head; const auto index = m_free_head_index;
m_free_head = entry->GetNextFreeEntry();
m_count++; m_free_head_index = m_entry_infos[index].GetNextFreeIndex();
m_max_count = std::max(m_max_count, m_count);
return entry; m_max_count = std::max(m_max_count, ++m_count);
return index;
} }
constexpr ALWAYS_INLINE void FreeEntry(Entry *entry) { constexpr ALWAYS_INLINE void FreeEntry(s32 index) {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(m_count > 0); MESOSPHERE_ASSERT(m_count > 0);
entry->SetFree(m_free_head); m_objects[index] = nullptr;
m_free_head = entry; m_entry_infos[index].next_free_index = m_free_head_index;
m_count--; m_free_head_index = index;
--m_count;
} }
constexpr ALWAYS_INLINE u16 AllocateLinearId() { constexpr ALWAYS_INLINE u16 AllocateLinearId() {
@@ -287,13 +271,7 @@ namespace ams::kern {
return id; return id;
} }
constexpr ALWAYS_INLINE size_t GetEntryIndex(Entry *entry) { constexpr ALWAYS_INLINE bool IsValidHandle(ams::svc::Handle handle) const {
const size_t index = entry - m_table;
MESOSPHERE_ASSERT(index < m_table_size);
return index;
}
constexpr ALWAYS_INLINE Entry *FindEntry(ams::svc::Handle handle) const {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
/* Unpack the handle. */ /* Unpack the handle. */
@@ -306,38 +284,38 @@ namespace ams::kern {
MESOSPHERE_UNUSED(reserved); MESOSPHERE_UNUSED(reserved);
/* Validate our indexing information. */ /* Validate our indexing information. */
if (raw_value == 0) { if (AMS_UNLIKELY(raw_value == 0)) {
return nullptr; return false;
} }
if (linear_id == 0) { if (AMS_UNLIKELY(linear_id == 0)) {
return nullptr; return false;
} }
if (index >= m_table_size) { if (AMS_UNLIKELY(index >= m_table_size)) {
return nullptr; return false;
} }
/* Get the entry, and ensure our serial id is correct. */ /* Check that there's an object, and our serial id is correct. */
Entry *entry = std::addressof(m_table[index]); if (AMS_UNLIKELY(m_objects[index] == nullptr)) {
if (entry->GetObject() == nullptr) { return false;
return nullptr;
} }
if (entry->GetLinearId() != linear_id) { if (AMS_UNLIKELY(m_entry_infos[index].GetLinearId() != linear_id)) {
return nullptr; return false;
} }
return entry; return true;
} }
constexpr ALWAYS_INLINE KAutoObject *GetObjectImpl(ams::svc::Handle handle) const { constexpr ALWAYS_INLINE KAutoObject *GetObjectImpl(ams::svc::Handle handle) const {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
/* Handles must not have reserved bits set. */ /* Handles must not have reserved bits set. */
if (GetHandleBitPack(handle).Get<HandleReserved>() != 0) { const auto handle_pack = GetHandleBitPack(handle);
if (AMS_UNLIKELY(handle_pack.Get<HandleReserved>() != 0)) {
return nullptr; return nullptr;
} }
if (Entry *entry = this->FindEntry(handle); entry != nullptr) { if (AMS_LIKELY(this->IsValidHandle(handle))) {
return entry->GetObject(); return m_objects[handle_pack.Get<HandleIndex>()];
} else { } else {
return nullptr; return nullptr;
} }
@@ -347,18 +325,17 @@ namespace ams::kern {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
/* Index must be in bounds. */ /* Index must be in bounds. */
if (index >= m_table_size || m_table == nullptr) { if (AMS_UNLIKELY(index >= m_table_size)) {
return nullptr; return nullptr;
} }
/* Ensure entry has an object. */ /* Ensure entry has an object. */
Entry *entry = std::addressof(m_table[index]); if (KAutoObject *obj = m_objects[index]; obj != nullptr) {
if (entry->GetObject() == nullptr) { *out_handle = EncodeHandle(index, m_entry_infos[index].GetLinearId());
return obj;
} else {
return nullptr; return nullptr;
} }
*out_handle = EncodeHandle(index, entry->GetLinearId());
return entry->GetObject();
} }
}; };

View File

@@ -70,6 +70,7 @@ namespace ams::kern {
constexpr bool Is64Bit() const { return (m_flags & (1 << 3)); } constexpr bool Is64Bit() const { return (m_flags & (1 << 3)); }
constexpr bool Is64BitAddressSpace() const { return (m_flags & (1 << 4)); } constexpr bool Is64BitAddressSpace() const { return (m_flags & (1 << 4)); }
constexpr bool UsesSecureMemory() const { return (m_flags & (1 << 5)); } constexpr bool UsesSecureMemory() const { return (m_flags & (1 << 5)); }
constexpr bool IsImmortal() const { return (m_flags & (1 << 6)); }
constexpr u32 GetRxAddress() const { return m_rx_address; } constexpr u32 GetRxAddress() const { return m_rx_address; }
constexpr u32 GetRxSize() const { return m_rx_size; } constexpr u32 GetRxSize() const { return m_rx_size; }
@@ -90,45 +91,49 @@ namespace ams::kern {
class KInitialProcessReader { class KInitialProcessReader {
private: private:
KInitialProcessHeader *m_kip_header; KInitialProcessHeader m_kip_header;
public: public:
constexpr KInitialProcessReader() : m_kip_header() { /* ... */ } constexpr KInitialProcessReader() : m_kip_header() { /* ... */ }
constexpr const u32 *GetCapabilities() const { return m_kip_header->GetCapabilities(); } constexpr const u32 *GetCapabilities() const { return m_kip_header.GetCapabilities(); }
constexpr size_t GetNumCapabilities() const { return m_kip_header->GetNumCapabilities(); } constexpr size_t GetNumCapabilities() const { return m_kip_header.GetNumCapabilities(); }
constexpr size_t GetBinarySize() const { constexpr size_t GetBinarySize() const {
return sizeof(*m_kip_header) + m_kip_header->GetRxCompressedSize() + m_kip_header->GetRoCompressedSize() + m_kip_header->GetRwCompressedSize(); return m_kip_header.GetRxCompressedSize() + m_kip_header.GetRoCompressedSize() + m_kip_header.GetRwCompressedSize();
} }
constexpr size_t GetSize() const { constexpr size_t GetSize() const {
if (const size_t bss_size = m_kip_header->GetBssSize(); bss_size != 0) { if (const size_t bss_size = m_kip_header.GetBssSize(); bss_size != 0) {
return m_kip_header->GetBssAddress() + m_kip_header->GetBssSize(); return util::AlignUp(m_kip_header.GetBssAddress() + m_kip_header.GetBssSize(), PageSize);
} else { } else {
return m_kip_header->GetRwAddress() + m_kip_header->GetRwSize(); return util::AlignUp(m_kip_header.GetRwAddress() + m_kip_header.GetRwSize(), PageSize);
} }
} }
constexpr u8 GetPriority() const { return m_kip_header->GetPriority(); } constexpr u8 GetPriority() const { return m_kip_header.GetPriority(); }
constexpr u8 GetIdealCoreId() const { return m_kip_header->GetIdealCoreId(); } constexpr u8 GetIdealCoreId() const { return m_kip_header.GetIdealCoreId(); }
constexpr u32 GetAffinityMask() const { return m_kip_header->GetAffinityMask(); } constexpr u32 GetAffinityMask() const { return m_kip_header.GetAffinityMask(); }
constexpr u32 GetStackSize() const { return m_kip_header->GetStackSize(); } constexpr u32 GetStackSize() const { return m_kip_header.GetStackSize(); }
constexpr bool Is64Bit() const { return m_kip_header->Is64Bit(); } constexpr bool Is64Bit() const { return m_kip_header.Is64Bit(); }
constexpr bool Is64BitAddressSpace() const { return m_kip_header->Is64BitAddressSpace(); } constexpr bool Is64BitAddressSpace() const { return m_kip_header.Is64BitAddressSpace(); }
constexpr bool UsesSecureMemory() const { return m_kip_header->UsesSecureMemory(); } constexpr bool UsesSecureMemory() const { return m_kip_header.UsesSecureMemory(); }
constexpr bool IsImmortal() const { return m_kip_header.IsImmortal(); }
bool Attach(u8 *bin) { KVirtualAddress Attach(KVirtualAddress bin) {
if (KInitialProcessHeader *header = reinterpret_cast<KInitialProcessHeader *>(bin); header->IsValid()) { /* Copy the header. */
m_kip_header = header; m_kip_header = *GetPointer<const KInitialProcessHeader>(bin);
return true;
/* Check that it's valid. */
if (m_kip_header.IsValid()) {
return bin + sizeof(KInitialProcessHeader);
} else { } else {
return false; return Null<KVirtualAddress>;
} }
} }
Result MakeCreateProcessParameter(ams::svc::CreateProcessParameter *out, bool enable_aslr) const; Result MakeCreateProcessParameter(ams::svc::CreateProcessParameter *out, bool enable_aslr) const;
Result Load(KProcessAddress address, const ams::svc::CreateProcessParameter &params) const; Result Load(KProcessAddress address, const ams::svc::CreateProcessParameter &params, KProcessAddress src) const;
Result SetMemoryPermissions(KProcessPageTable &page_table, const ams::svc::CreateProcessParameter &params) const; Result SetMemoryPermissions(KProcessPageTable &page_table, const ams::svc::CreateProcessParameter &params) const;
}; };

View File

@@ -28,9 +28,10 @@ namespace ams::kern {
MESOSPHERE_AUTOOBJECT_TRAITS(KInterruptEvent, KReadableEvent); MESOSPHERE_AUTOOBJECT_TRAITS(KInterruptEvent, KReadableEvent);
private: private:
s32 m_interrupt_id; s32 m_interrupt_id;
s32 m_core_id;
bool m_is_initialized; bool m_is_initialized;
public: public:
constexpr KInterruptEvent() : m_interrupt_id(-1), m_is_initialized(false) { /* ... */ } constexpr KInterruptEvent() : m_interrupt_id(-1), m_core_id(-1), m_is_initialized(false) { /* ... */ }
virtual ~KInterruptEvent() { /* ... */ } virtual ~KInterruptEvent() { /* ... */ }
Result Initialize(int32_t interrupt_name, ams::svc::InterruptType type); Result Initialize(int32_t interrupt_name, ams::svc::InterruptType type);
@@ -58,9 +59,9 @@ namespace ams::kern {
virtual KInterruptTask *OnInterrupt(s32 interrupt_id) override; virtual KInterruptTask *OnInterrupt(s32 interrupt_id) override;
virtual void DoTask() override; virtual void DoTask() override;
void Unregister(s32 interrupt_id); void Unregister(s32 interrupt_id, s32 core_id);
public: public:
static Result Register(s32 interrupt_id, bool level, KInterruptEvent *event); static Result Register(s32 interrupt_id, s32 core_id, bool level, KInterruptEvent *event);
}; };
} }

View File

@@ -24,40 +24,59 @@ namespace ams::kern {
class KLightConditionVariable { class KLightConditionVariable {
private: private:
KThreadQueue m_thread_queue; KThread::WaiterList m_wait_list;
public: public:
constexpr ALWAYS_INLINE KLightConditionVariable() : m_thread_queue() { /* ... */ } constexpr ALWAYS_INLINE KLightConditionVariable() : m_wait_list() { /* ... */ }
private: private:
void WaitImpl(KLightLock *lock, s64 timeout) { void WaitImpl(KLightLock *lock, s64 timeout, bool allow_terminating_thread) {
KThread *owner = GetCurrentThreadPointer(); KThread *owner = GetCurrentThreadPointer();
KHardwareTimer *timer; KHardwareTimer *timer;
/* Sleep the thread. */ /* Sleep the thread. */
{ {
KScopedSchedulerLockAndSleep lk(&timer, owner, timeout); KScopedSchedulerLockAndSleep lk(&timer, owner, timeout);
lock->Unlock();
if (!m_thread_queue.SleepThread(owner)) { if (!allow_terminating_thread && owner->IsTerminationRequested()) {
lk.CancelSleep(); lk.CancelSleep();
return; return;
} }
lock->Unlock();
/* Set the thread as waiting. */
GetCurrentThread().SetState(KThread::ThreadState_Waiting);
/* Add the thread to the queue. */
m_wait_list.push_back(GetCurrentThread());
}
/* Remove the thread from the wait list. */
{
KScopedSchedulerLock sl;
m_wait_list.erase(m_wait_list.iterator_to(GetCurrentThread()));
} }
/* Cancel the task that the sleep setup. */ /* Cancel the task that the sleep setup. */
if (timer != nullptr) { if (timer != nullptr) {
timer->CancelTask(owner); timer->CancelTask(owner);
} }
/* Re-acquire the lock. */
lock->Lock();
} }
public: public:
void Wait(KLightLock *lock, s64 timeout = -1ll) { void Wait(KLightLock *lock, s64 timeout = -1ll, bool allow_terminating_thread = true) {
this->WaitImpl(lock, timeout); this->WaitImpl(lock, timeout, allow_terminating_thread);
lock->Lock();
} }
void Broadcast() { void Broadcast() {
KScopedSchedulerLock lk; KScopedSchedulerLock lk;
while (m_thread_queue.WakeupFrontThread() != nullptr) {
/* We want to signal all threads, and so should continue waking up until there's nothing to wake. */ /* Signal all threads. */
for (auto &thread : m_wait_list) {
thread.SetState(KThread::ThreadState_Runnable);
} }
} }

View File

@@ -1,236 +0,0 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <mesosphere/kern_common.hpp>
#include <mesosphere/kern_k_typed_address.hpp>
#include <mesosphere/kern_slab_helpers.hpp>
namespace ams::kern {
class KLinkedListNode : public util::IntrusiveListBaseNode<KLinkedListNode>, public KSlabAllocated<KLinkedListNode> {
private:
void *m_item;
public:
constexpr KLinkedListNode() : util::IntrusiveListBaseNode<KLinkedListNode>(), m_item(nullptr) { MESOSPHERE_ASSERT_THIS(); }
constexpr void Initialize(void *it) {
MESOSPHERE_ASSERT_THIS();
m_item = it;
}
constexpr void *GetItem() const {
return m_item;
}
};
static_assert(sizeof(KLinkedListNode) == sizeof(util::IntrusiveListNode) + sizeof(void *));
template<typename T>
class KLinkedList : private util::IntrusiveListBaseTraits<KLinkedListNode>::ListType {
private:
using BaseList = util::IntrusiveListBaseTraits<KLinkedListNode>::ListType;
public:
template<bool Const>
class Iterator;
using value_type = T;
using size_type = size_t;
using difference_type = ptrdiff_t;
using pointer = value_type *;
using const_pointer = const value_type *;
using reference = value_type &;
using const_reference = const value_type &;
using iterator = Iterator<false>;
using const_iterator = Iterator<true>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
template<bool Const>
class Iterator {
private:
using BaseIterator = BaseList::Iterator<Const>;
friend class KLinkedList;
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = typename KLinkedList::value_type;
using difference_type = typename KLinkedList::difference_type;
using pointer = typename std::conditional<Const, KLinkedList::const_pointer, KLinkedList::pointer>::type;
using reference = typename std::conditional<Const, KLinkedList::const_reference, KLinkedList::reference>::type;
private:
BaseIterator m_base_it;
public:
explicit Iterator(BaseIterator it) : m_base_it(it) { /* ... */ }
pointer GetItem() const {
return static_cast<pointer>(m_base_it->GetItem());
}
bool operator==(const Iterator &rhs) const {
return m_base_it == rhs.m_base_it;
}
bool operator!=(const Iterator &rhs) const {
return !(*this == rhs);
}
pointer operator->() const {
return this->GetItem();
}
reference operator*() const {
return *this->GetItem();
}
Iterator &operator++() {
++m_base_it;
return *this;
}
Iterator &operator--() {
--m_base_it;
return *this;
}
Iterator operator++(int) {
const Iterator it{*this};
++(*this);
return it;
}
Iterator operator--(int) {
const Iterator it{*this};
--(*this);
return it;
}
operator Iterator<true>() const {
return Iterator<true>(m_base_it);
}
};
public:
constexpr KLinkedList() : BaseList() { /* ... */ }
~KLinkedList() {
/* Erase all elements. */
for (auto it = this->begin(); it != this->end(); it = this->erase(it)) {
/* ... */
}
/* Ensure we succeeded. */
MESOSPHERE_ASSERT(this->empty());
}
/* Iterator accessors. */
iterator begin() {
return iterator(BaseList::begin());
}
const_iterator begin() const {
return const_iterator(BaseList::begin());
}
iterator end() {
return iterator(BaseList::end());
}
const_iterator end() const {
return const_iterator(BaseList::end());
}
const_iterator cbegin() const {
return this->begin();
}
const_iterator cend() const {
return this->end();
}
reverse_iterator rbegin() {
return reverse_iterator(this->end());
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator(this->end());
}
reverse_iterator rend() {
return reverse_iterator(this->begin());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(this->begin());
}
const_reverse_iterator crbegin() const {
return this->rbegin();
}
const_reverse_iterator crend() const {
return this->rend();
}
/* Content management. */
using BaseList::empty;
using BaseList::size;
reference back() {
return *(--this->end());
}
const_reference back() const {
return *(--this->end());
}
reference front() {
return *this->begin();
}
const_reference front() const {
return *this->begin();
}
iterator insert(const_iterator pos, reference ref) {
KLinkedListNode *node = KLinkedListNode::Allocate();
MESOSPHERE_ABORT_UNLESS(node != nullptr);
node->Initialize(std::addressof(ref));
return iterator(BaseList::insert(pos.m_base_it, *node));
}
void push_back(reference ref) {
this->insert(this->end(), ref);
}
void push_front(reference ref) {
this->insert(this->begin(), ref);
}
void pop_back() {
this->erase(--this->end());
}
void pop_front() {
this->erase(this->begin());
}
iterator erase(const iterator pos) {
KLinkedListNode *freed_node = std::addressof(*pos.m_base_it);
iterator ret = iterator(BaseList::erase(pos.m_base_it));
KLinkedListNode::Free(freed_node);
return ret;
}
};
}

View File

@@ -27,18 +27,8 @@ namespace ams::kern {
KMemoryBlock *m_blocks[MaxBlocks]; KMemoryBlock *m_blocks[MaxBlocks];
size_t m_index; size_t m_index;
KMemoryBlockSlabManager *m_slab_manager; KMemoryBlockSlabManager *m_slab_manager;
public: private:
constexpr explicit KMemoryBlockManagerUpdateAllocator(KMemoryBlockSlabManager *sm) : m_blocks(), m_index(MaxBlocks), m_slab_manager(sm) { /* ... */ } ALWAYS_INLINE Result Initialize(size_t num_blocks) {
~KMemoryBlockManagerUpdateAllocator() {
for (const auto &block : m_blocks) {
if (block != nullptr) {
m_slab_manager->Free(block);
}
}
}
Result Initialize(size_t num_blocks) {
/* Check num blocks. */ /* Check num blocks. */
MESOSPHERE_ASSERT(num_blocks <= MaxBlocks); MESOSPHERE_ASSERT(num_blocks <= MaxBlocks);
@@ -53,6 +43,18 @@ namespace ams::kern {
return ResultSuccess(); return ResultSuccess();
} }
public:
KMemoryBlockManagerUpdateAllocator(Result *out_result, KMemoryBlockSlabManager *sm, size_t num_blocks = MaxBlocks) : m_blocks(), m_index(MaxBlocks), m_slab_manager(sm) {
*out_result = this->Initialize(num_blocks);
}
~KMemoryBlockManagerUpdateAllocator() {
for (const auto &block : m_blocks) {
if (block != nullptr) {
m_slab_manager->Free(block);
}
}
}
KMemoryBlock *Allocate() { KMemoryBlock *Allocate() {
MESOSPHERE_ABORT_UNLESS(m_index < MaxBlocks); MESOSPHERE_ABORT_UNLESS(m_index < MaxBlocks);

View File

@@ -44,9 +44,8 @@ namespace ams::kern {
constexpr size_t KernelInitialPageHeapSize = 128_KB; constexpr size_t KernelInitialPageHeapSize = 128_KB;
constexpr size_t KernelSlabHeapDataSize = 5_MB; constexpr size_t KernelSlabHeapDataSize = 5_MB;
constexpr size_t KernelSlabHeapGapsSize = 2_MB - 64_KB; constexpr size_t KernelSlabHeapGapsSizeMax = 2_MB - 64_KB;
constexpr size_t KernelSlabHeapGapsSizeDeprecated = 2_MB; constexpr size_t KernelSlabHeapSize = KernelSlabHeapDataSize + KernelSlabHeapGapsSizeMax;
constexpr size_t KernelSlabHeapSize = KernelSlabHeapDataSize + KernelSlabHeapGapsSize;
/* NOTE: This is calculated from KThread slab counts, assuming KThread size <= 0x860. */ /* NOTE: This is calculated from KThread slab counts, assuming KThread size <= 0x860. */
constexpr size_t KernelSlabHeapAdditionalSize = 0x68000; constexpr size_t KernelSlabHeapAdditionalSize = 0x68000;

View File

@@ -75,7 +75,7 @@ namespace ams::kern {
KVirtualAddress AllocateBlock(s32 index, bool random) { return m_heap.AllocateBlock(index, random); } KVirtualAddress AllocateBlock(s32 index, bool random) { return m_heap.AllocateBlock(index, random); }
void Free(KVirtualAddress addr, size_t num_pages) { m_heap.Free(addr, num_pages); } void Free(KVirtualAddress addr, size_t num_pages) { m_heap.Free(addr, num_pages); }
void UpdateUsedHeapSize() { m_heap.UpdateUsedSize(); } void SetInitialUsedHeapSize(size_t reserved_size) { m_heap.SetInitialUsedSize(reserved_size); }
void InitializeOptimizedMemory() { std::memset(GetVoidPointer(m_management_region), 0, CalculateOptimizedProcessOverheadSize(m_heap.GetSize())); } void InitializeOptimizedMemory() { std::memset(GetVoidPointer(m_management_region), 0, CalculateOptimizedProcessOverheadSize(m_heap.GetSize())); }
@@ -168,6 +168,10 @@ namespace ams::kern {
return m_managers[KMemoryLayout::GetVirtualLinearRegion(address).GetAttributes()]; return m_managers[KMemoryLayout::GetVirtualLinearRegion(address).GetAttributes()];
} }
const Impl &GetManager(KVirtualAddress address) const {
return m_managers[KMemoryLayout::GetVirtualLinearRegion(address).GetAttributes()];
}
constexpr Impl *GetFirstManager(Pool pool, Direction dir) { constexpr Impl *GetFirstManager(Pool pool, Direction dir) {
return dir == Direction_FromBack ? m_pool_managers_tail[pool] : m_pool_managers_head[pool]; return dir == Direction_FromBack ? m_pool_managers_tail[pool] : m_pool_managers_head[pool];
} }
@@ -197,6 +201,10 @@ namespace ams::kern {
NOINLINE Result AllocateAndOpen(KPageGroup *out, size_t num_pages, u32 option); NOINLINE Result AllocateAndOpen(KPageGroup *out, size_t num_pages, u32 option);
NOINLINE Result AllocateAndOpenForProcess(KPageGroup *out, size_t num_pages, u32 option, u64 process_id, u8 fill_pattern); NOINLINE Result AllocateAndOpenForProcess(KPageGroup *out, size_t num_pages, u32 option, u64 process_id, u8 fill_pattern);
Pool GetPool(KVirtualAddress address) const {
return this->GetManager(address).GetPool();
}
void Open(KVirtualAddress address, size_t num_pages) { void Open(KVirtualAddress address, size_t num_pages) {
/* Repeatedly open references until we've done so for all pages. */ /* Repeatedly open references until we've done so for all pages. */
while (num_pages) { while (num_pages) {

View File

@@ -21,7 +21,8 @@ namespace ams::kern {
enum KMemoryRegionType : u32 {}; enum KMemoryRegionType : u32 {};
enum KMemoryRegionAttr : typename std::underlying_type<KMemoryRegionType>::type { enum KMemoryRegionAttr : typename std::underlying_type<KMemoryRegionType>::type {
KMemoryRegionAttr_CarveoutProtected = 0x04000000, KMemoryRegionAttr_CarveoutProtected = 0x02000000,
KMemoryRegionAttr_Uncached = 0x04000000,
KMemoryRegionAttr_DidKernelMap = 0x08000000, KMemoryRegionAttr_DidKernelMap = 0x08000000,
KMemoryRegionAttr_ShouldKernelMap = 0x10000000, KMemoryRegionAttr_ShouldKernelMap = 0x10000000,
KMemoryRegionAttr_UserReadOnly = 0x20000000, KMemoryRegionAttr_UserReadOnly = 0x20000000,
@@ -216,6 +217,10 @@ namespace ams::kern {
static_assert(KMemoryRegionType_VirtualDramKernelPtHeap .GetValue() == 0x2A); static_assert(KMemoryRegionType_VirtualDramKernelPtHeap .GetValue() == 0x2A);
static_assert(KMemoryRegionType_VirtualDramKernelTraceBuffer.GetValue() == 0x4A); static_assert(KMemoryRegionType_VirtualDramKernelTraceBuffer.GetValue() == 0x4A);
/* UNUSED: .DeriveSparse(2, 2, 0); */
constexpr inline const auto KMemoryRegionType_VirtualDramUnknownDebug = KMemoryRegionType_Dram.DeriveSparse(2, 2, 1);
static_assert(KMemoryRegionType_VirtualDramUnknownDebug.GetValue() == (0x52));
constexpr inline const auto KMemoryRegionType_VirtualDramKernelInitPt = KMemoryRegionType_VirtualDramHeapBase.Derive(3, 0); constexpr inline const auto KMemoryRegionType_VirtualDramKernelInitPt = KMemoryRegionType_VirtualDramHeapBase.Derive(3, 0);
constexpr inline const auto KMemoryRegionType_VirtualDramPoolManagement = KMemoryRegionType_VirtualDramHeapBase.Derive(3, 1); constexpr inline const auto KMemoryRegionType_VirtualDramPoolManagement = KMemoryRegionType_VirtualDramHeapBase.Derive(3, 1);
constexpr inline const auto KMemoryRegionType_VirtualDramUserPool = KMemoryRegionType_VirtualDramHeapBase.Derive(3, 2); constexpr inline const auto KMemoryRegionType_VirtualDramUserPool = KMemoryRegionType_VirtualDramHeapBase.Derive(3, 2);
@@ -292,6 +297,8 @@ namespace ams::kern {
return KMemoryRegionType_VirtualDramKernelTraceBuffer; return KMemoryRegionType_VirtualDramKernelTraceBuffer;
} else if (KMemoryRegionType_DramKernelPtHeap.IsAncestorOf(type_id)) { } else if (KMemoryRegionType_DramKernelPtHeap.IsAncestorOf(type_id)) {
return KMemoryRegionType_VirtualDramKernelPtHeap; return KMemoryRegionType_VirtualDramKernelPtHeap;
} else if ((type_id | KMemoryRegionAttr_ShouldKernelMap) == type_id) {
return KMemoryRegionType_VirtualDramUnknownDebug;
} else { } else {
return KMemoryRegionType_Dram; return KMemoryRegionType_Dram;
} }

View File

@@ -47,6 +47,9 @@ namespace ams::kern {
Derived *derived = obj->DynamicCast<Derived *>(); Derived *derived = obj->DynamicCast<Derived *>();
R_UNLESS(derived != nullptr, svc::ResultNotFound()); R_UNLESS(derived != nullptr, svc::ResultNotFound());
/* Check that the object is closed. */
R_UNLESS(derived->IsServerClosed(), svc::ResultInvalidState());
return Delete(obj.GetPointerUnsafe(), name); return Delete(obj.GetPointerUnsafe(), name);
} }

View File

@@ -125,7 +125,7 @@ namespace ams::kern {
private: private:
KVirtualAddress m_heap_address; KVirtualAddress m_heap_address;
size_t m_heap_size; size_t m_heap_size;
size_t m_used_size; size_t m_initial_used_size;
size_t m_num_blocks; size_t m_num_blocks;
Block m_blocks[NumMemoryBlockPageShifts]; Block m_blocks[NumMemoryBlockPageShifts];
private: private:
@@ -134,7 +134,7 @@ namespace ams::kern {
void FreeBlock(KVirtualAddress block, s32 index); void FreeBlock(KVirtualAddress block, s32 index);
public: public:
KPageHeap() : m_heap_address(), m_heap_size(), m_used_size(), m_num_blocks(), m_blocks() { /* ... */ } KPageHeap() : m_heap_address(), m_heap_size(), m_initial_used_size(), m_num_blocks(), m_blocks() { /* ... */ }
constexpr KVirtualAddress GetAddress() const { return m_heap_address; } constexpr KVirtualAddress GetAddress() const { return m_heap_address; }
constexpr size_t GetSize() const { return m_heap_size; } constexpr size_t GetSize() const { return m_heap_size; }
@@ -149,8 +149,13 @@ namespace ams::kern {
size_t GetFreeSize() const { return this->GetNumFreePages() * PageSize; } size_t GetFreeSize() const { return this->GetNumFreePages() * PageSize; }
void DumpFreeList() const; void DumpFreeList() const;
void UpdateUsedSize() { void SetInitialUsedSize(size_t reserved_size) {
m_used_size = m_heap_size - (this->GetNumFreePages() * PageSize); /* Check that the reserved size is valid. */
const size_t free_size = this->GetNumFreePages() * PageSize;
MESOSPHERE_ABORT_UNLESS(m_heap_size >= free_size + reserved_size);
/* Set the initial used size. */
m_initial_used_size = m_heap_size - free_size - reserved_size;
} }
KVirtualAddress AllocateBlock(s32 index, bool random); KVirtualAddress AllocateBlock(s32 index, bool random);

View File

@@ -47,12 +47,21 @@ namespace ams::kern {
static_assert(std::is_trivial<KPageProperties>::value); static_assert(std::is_trivial<KPageProperties>::value);
static_assert(sizeof(KPageProperties) == sizeof(u32)); static_assert(sizeof(KPageProperties) == sizeof(u32));
class KResourceLimit;
class KPageTableBase { class KPageTableBase {
NON_COPYABLE(KPageTableBase); NON_COPYABLE(KPageTableBase);
NON_MOVEABLE(KPageTableBase); NON_MOVEABLE(KPageTableBase);
public: public:
using TraversalEntry = KPageTableImpl::TraversalEntry; using TraversalEntry = KPageTableImpl::TraversalEntry;
using TraversalContext = KPageTableImpl::TraversalContext; using TraversalContext = KPageTableImpl::TraversalContext;
struct MemoryRange {
KVirtualAddress address;
size_t size;
void Close();
};
protected: protected:
enum MemoryFillValue { enum MemoryFillValue {
MemoryFillValue_Zero = 0, MemoryFillValue_Zero = 0,
@@ -150,8 +159,10 @@ namespace ams::kern {
size_t m_max_heap_size{}; size_t m_max_heap_size{};
size_t m_mapped_physical_memory_size{}; size_t m_mapped_physical_memory_size{};
size_t m_mapped_unsafe_physical_memory{}; size_t m_mapped_unsafe_physical_memory{};
size_t m_mapped_ipc_server_memory{};
mutable KLightLock m_general_lock{}; mutable KLightLock m_general_lock{};
mutable KLightLock m_map_physical_memory_lock{}; mutable KLightLock m_map_physical_memory_lock{};
KLightLock m_device_map_lock{};
KPageTableImpl m_impl{}; KPageTableImpl m_impl{};
KMemoryBlockManager m_memory_block_manager{}; KMemoryBlockManager m_memory_block_manager{};
u32 m_allocate_option{}; u32 m_allocate_option{};
@@ -161,6 +172,7 @@ namespace ams::kern {
bool m_enable_device_address_space_merge{}; bool m_enable_device_address_space_merge{};
KMemoryBlockSlabManager *m_memory_block_slab_manager{}; KMemoryBlockSlabManager *m_memory_block_slab_manager{};
KBlockInfoManager *m_block_info_manager{}; KBlockInfoManager *m_block_info_manager{};
KResourceLimit *m_resource_limit{};
const KMemoryRegion *m_cached_physical_linear_region{}; const KMemoryRegion *m_cached_physical_linear_region{};
const KMemoryRegion *m_cached_physical_heap_region{}; const KMemoryRegion *m_cached_physical_heap_region{};
const KMemoryRegion *m_cached_virtual_heap_region{}; const KMemoryRegion *m_cached_virtual_heap_region{};
@@ -171,7 +183,7 @@ namespace ams::kern {
constexpr KPageTableBase() { /* ... */ } constexpr KPageTableBase() { /* ... */ }
NOINLINE Result InitializeForKernel(bool is_64_bit, void *table, KVirtualAddress start, KVirtualAddress end); NOINLINE Result InitializeForKernel(bool is_64_bit, void *table, KVirtualAddress start, KVirtualAddress end);
NOINLINE Result InitializeForProcess(ams::svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_device_address_space_merge, bool from_back, KMemoryManager::Pool pool, void *table, KProcessAddress start, KProcessAddress end, KProcessAddress code_address, size_t code_size, KMemoryBlockSlabManager *mem_block_slab_manager, KBlockInfoManager *block_info_manager); NOINLINE Result InitializeForProcess(ams::svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_device_address_space_merge, bool from_back, KMemoryManager::Pool pool, void *table, KProcessAddress start, KProcessAddress end, KProcessAddress code_address, size_t code_size, KMemoryBlockSlabManager *mem_block_slab_manager, KBlockInfoManager *block_info_manager, KResourceLimit *resource_limit);
void Finalize(); void Finalize();
@@ -195,6 +207,10 @@ namespace ams::kern {
return this->CanContain(addr, size, KMemoryState_AliasCode); return this->CanContain(addr, size, KMemoryState_AliasCode);
} }
ALWAYS_INLINE KScopedLightLock AcquireDeviceMapLock() {
return KScopedLightLock(m_device_map_lock);
}
KProcessAddress GetRegionAddress(KMemoryState state) const; KProcessAddress GetRegionAddress(KMemoryState state) const;
size_t GetRegionSize(KMemoryState state) const; size_t GetRegionSize(KMemoryState state) const;
bool CanContain(KProcessAddress addr, size_t size, KMemoryState state) const; bool CanContain(KProcessAddress addr, size_t size, KMemoryState state) const;
@@ -286,16 +302,35 @@ namespace ams::kern {
Result MakePageGroup(KPageGroup &pg, KProcessAddress addr, size_t num_pages); Result MakePageGroup(KPageGroup &pg, KProcessAddress addr, size_t num_pages);
bool IsValidPageGroup(const KPageGroup &pg, KProcessAddress addr, size_t num_pages); bool IsValidPageGroup(const KPageGroup &pg, KProcessAddress addr, size_t num_pages);
Result GetContiguousMemoryRangeWithState(MemoryRange *out, KProcessAddress address, size_t size, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr);
NOINLINE Result MapPages(KProcessAddress *out_addr, size_t num_pages, size_t alignment, KPhysicalAddress phys_addr, bool is_pa_valid, KProcessAddress region_start, size_t region_num_pages, KMemoryState state, KMemoryPermission perm); NOINLINE Result MapPages(KProcessAddress *out_addr, size_t num_pages, size_t alignment, KPhysicalAddress phys_addr, bool is_pa_valid, KProcessAddress region_start, size_t region_num_pages, KMemoryState state, KMemoryPermission perm);
Result MapIoImpl(KProcessAddress *out, PageLinkedList *page_list, KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm);
Result ReadIoMemoryImpl(void *buffer, KPhysicalAddress phys_addr, size_t size);
Result WriteIoMemoryImpl(KPhysicalAddress phys_addr, const void *buffer, size_t size);
Result SetupForIpcClient(PageLinkedList *page_list, size_t *out_blocks_needed, KProcessAddress address, size_t size, KMemoryPermission test_perm, KMemoryState dst_state); Result SetupForIpcClient(PageLinkedList *page_list, size_t *out_blocks_needed, KProcessAddress address, size_t size, KMemoryPermission test_perm, KMemoryState dst_state);
Result SetupForIpcServer(KProcessAddress *out_addr, size_t size, KProcessAddress src_addr, KMemoryPermission test_perm, KMemoryState dst_state, KPageTableBase &src_page_table, bool send); Result SetupForIpcServer(KProcessAddress *out_addr, size_t size, KProcessAddress src_addr, KMemoryPermission test_perm, KMemoryState dst_state, KPageTableBase &src_page_table, bool send);
void CleanupForIpcClientOnServerSetupFailure(PageLinkedList *page_list, KProcessAddress address, size_t size, KMemoryPermission prot_perm); void CleanupForIpcClientOnServerSetupFailure(PageLinkedList *page_list, KProcessAddress address, size_t size, KMemoryPermission prot_perm);
size_t GetSize(KMemoryState state) const; size_t GetSize(KMemoryState state) const;
ALWAYS_INLINE bool GetPhysicalAddressLocked(KPhysicalAddress *out, KProcessAddress virt_addr) const {
/* Validate pre-conditions. */
MESOSPHERE_AUDIT(this->IsLockedByCurrentThread());
return this->GetImpl().GetPhysicalAddress(out, virt_addr);
}
public: public:
bool GetPhysicalAddress(KPhysicalAddress *out, KProcessAddress virt_addr) const { bool GetPhysicalAddress(KPhysicalAddress *out, KProcessAddress virt_addr) const {
return this->GetImpl().GetPhysicalAddress(out, virt_addr); /* Validate pre-conditions. */
MESOSPHERE_AUDIT(!this->IsLockedByCurrentThread());
/* Acquire exclusive access to the table while doing address translation. */
KScopedLightLock lk(m_general_lock);
return this->GetPhysicalAddressLocked(out, virt_addr);
} }
KBlockInfoManager *GetBlockInfoManager() const { return m_block_info_manager; } KBlockInfoManager *GetBlockInfoManager() const { return m_block_info_manager; }
@@ -341,14 +376,20 @@ namespace ams::kern {
Result InvalidateProcessDataCache(KProcessAddress address, size_t size); Result InvalidateProcessDataCache(KProcessAddress address, size_t size);
Result ReadDebugMemory(void *buffer, KProcessAddress address, size_t size); Result ReadDebugMemory(void *buffer, KProcessAddress address, size_t size);
Result ReadDebugIoMemory(void *buffer, KProcessAddress address, size_t size);
Result WriteDebugMemory(KProcessAddress address, const void *buffer, size_t size); Result WriteDebugMemory(KProcessAddress address, const void *buffer, size_t size);
Result WriteDebugIoMemory(KProcessAddress address, const void *buffer, size_t size);
Result LockForMapDeviceAddressSpace(KProcessAddress address, size_t size, KMemoryPermission perm, bool is_aligned);
Result LockForUnmapDeviceAddressSpace(KProcessAddress address, size_t size);
Result LockForDeviceAddressSpace(KPageGroup *out, KProcessAddress address, size_t size, KMemoryPermission perm, bool is_aligned);
Result UnlockForDeviceAddressSpace(KProcessAddress address, size_t size); Result UnlockForDeviceAddressSpace(KProcessAddress address, size_t size);
Result MakePageGroupForUnmapDeviceAddressSpace(KPageGroup *out, KProcessAddress address, size_t size);
Result UnlockForDeviceAddressSpacePartialMap(KProcessAddress address, size_t size, size_t mapped_size); Result UnlockForDeviceAddressSpacePartialMap(KProcessAddress address, size_t size, size_t mapped_size);
Result OpenMemoryRangeForMapDeviceAddressSpace(KPageTableBase::MemoryRange *out, KProcessAddress address, size_t size, KMemoryPermission perm, bool is_aligned);
Result OpenMemoryRangeForUnmapDeviceAddressSpace(MemoryRange *out, KProcessAddress address, size_t size);
Result LockForIpcUserBuffer(KPhysicalAddress *out, KProcessAddress address, size_t size); Result LockForIpcUserBuffer(KPhysicalAddress *out, KProcessAddress address, size_t size);
Result UnlockForIpcUserBuffer(KProcessAddress address, size_t size); Result UnlockForIpcUserBuffer(KProcessAddress address, size_t size);
@@ -357,6 +398,8 @@ namespace ams::kern {
Result LockForCodeMemory(KPageGroup *out, KProcessAddress address, size_t size); Result LockForCodeMemory(KPageGroup *out, KProcessAddress address, size_t size);
Result UnlockForCodeMemory(KProcessAddress address, size_t size, const KPageGroup &pg); Result UnlockForCodeMemory(KProcessAddress address, size_t size, const KPageGroup &pg);
Result OpenMemoryRangeForProcessCacheOperation(MemoryRange *out, KProcessAddress address, size_t size);
Result CopyMemoryFromLinearToUser(KProcessAddress dst_addr, size_t size, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr); Result CopyMemoryFromLinearToUser(KProcessAddress dst_addr, size_t size, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr);
Result CopyMemoryFromLinearToKernel(KProcessAddress dst_addr, size_t size, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr); Result CopyMemoryFromLinearToKernel(KProcessAddress dst_addr, size_t size, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr);
Result CopyMemoryFromUserToLinear(KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr); Result CopyMemoryFromUserToLinear(KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr);
@@ -365,7 +408,7 @@ namespace ams::kern {
Result CopyMemoryFromHeapToHeapWithoutCheckDestination(KPageTableBase &dst_page_table, KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr); Result CopyMemoryFromHeapToHeapWithoutCheckDestination(KPageTableBase &dst_page_table, KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr);
Result SetupForIpc(KProcessAddress *out_dst_addr, size_t size, KProcessAddress src_addr, KPageTableBase &src_page_table, KMemoryPermission test_perm, KMemoryState dst_state, bool send); Result SetupForIpc(KProcessAddress *out_dst_addr, size_t size, KProcessAddress src_addr, KPageTableBase &src_page_table, KMemoryPermission test_perm, KMemoryState dst_state, bool send);
Result CleanupForIpcServer(KProcessAddress address, size_t size, KMemoryState dst_state, KProcess *server_process); Result CleanupForIpcServer(KProcessAddress address, size_t size, KMemoryState dst_state);
Result CleanupForIpcClient(KProcessAddress address, size_t size, KMemoryState dst_state); Result CleanupForIpcClient(KProcessAddress address, size_t size, KMemoryState dst_state);
Result MapPhysicalMemory(KProcessAddress address, size_t size); Result MapPhysicalMemory(KProcessAddress address, size_t size);
@@ -374,6 +417,8 @@ namespace ams::kern {
Result MapPhysicalMemoryUnsafe(KProcessAddress address, size_t size); Result MapPhysicalMemoryUnsafe(KProcessAddress address, size_t size);
Result UnmapPhysicalMemoryUnsafe(KProcessAddress address, size_t size); Result UnmapPhysicalMemoryUnsafe(KProcessAddress address, size_t size);
Result UnmapProcessMemory(KProcessAddress dst_address, size_t size, KPageTableBase &src_pt, KProcessAddress src_address);
void DumpMemoryBlocksLocked() const { void DumpMemoryBlocksLocked() const {
MESOSPHERE_ASSERT(this->IsLockedByCurrentThread()); MESOSPHERE_ASSERT(this->IsLockedByCurrentThread());
m_memory_block_manager.DumpBlocks(); m_memory_block_manager.DumpBlocks();

View File

@@ -53,6 +53,11 @@ namespace ams::kern {
uintptr_t GetName() const { return m_name; } uintptr_t GetName() const { return m_name; }
bool IsLight() const { return m_is_light; } bool IsLight() const { return m_is_light; }
bool IsServerClosed() const {
KScopedSchedulerLock sl;
return m_state == State::ServerClosed;
}
Result EnqueueSession(KServerSession *session); Result EnqueueSession(KServerSession *session);
Result EnqueueSession(KLightServerSession *session); Result EnqueueSession(KLightServerSession *session);

View File

@@ -77,8 +77,7 @@ namespace ams::kern {
bool m_is_initialized{}; bool m_is_initialized{};
bool m_is_application{}; bool m_is_application{};
char m_name[13]{}; char m_name[13]{};
std::atomic<u16> m_num_threads{}; std::atomic<u16> m_num_running_threads{};
u16 m_peak_num_threads{};
u32 m_flags{}; u32 m_flags{};
KMemoryManager::Pool m_memory_pool{}; KMemoryManager::Pool m_memory_pool{};
s64 m_schedule_count{}; s64 m_schedule_count{};
@@ -99,7 +98,9 @@ namespace ams::kern {
SharedMemoryInfoList m_shared_memory_list{}; SharedMemoryInfoList m_shared_memory_list{};
BetaList m_beta_list{}; BetaList m_beta_list{};
bool m_is_suspended{}; bool m_is_suspended{};
bool m_is_immortal{};
bool m_is_jit_debug{}; bool m_is_jit_debug{};
bool m_is_handle_table_initialized{};
ams::svc::DebugEvent m_jit_debug_event_type{}; ams::svc::DebugEvent m_jit_debug_event_type{};
ams::svc::DebugException m_jit_debug_exception_type{}; ams::svc::DebugException m_jit_debug_exception_type{};
uintptr_t m_jit_debug_params[4]{}; uintptr_t m_jit_debug_params[4]{};
@@ -108,7 +109,6 @@ namespace ams::kern {
KThread *m_running_threads[cpu::NumCores]{}; KThread *m_running_threads[cpu::NumCores]{};
u64 m_running_thread_idle_counts[cpu::NumCores]{}; u64 m_running_thread_idle_counts[cpu::NumCores]{};
KThread *m_pinned_threads[cpu::NumCores]{}; KThread *m_pinned_threads[cpu::NumCores]{};
std::atomic<s32> m_num_created_threads{};
std::atomic<s64> m_cpu_time{}; std::atomic<s64> m_cpu_time{};
std::atomic<s64> m_num_process_switches{}; std::atomic<s64> m_num_process_switches{};
std::atomic<s64> m_num_thread_switches{}; std::atomic<s64> m_num_thread_switches{};
@@ -124,17 +124,17 @@ namespace ams::kern {
private: private:
Result Initialize(const ams::svc::CreateProcessParameter &params); Result Initialize(const ams::svc::CreateProcessParameter &params);
void StartTermination(); Result StartTermination();
void FinishTermination(); void FinishTermination();
void PinThread(s32 core_id, KThread *thread) { ALWAYS_INLINE void PinThread(s32 core_id, KThread *thread) {
MESOSPHERE_ASSERT(0 <= core_id && core_id < static_cast<s32>(cpu::NumCores)); MESOSPHERE_ASSERT(0 <= core_id && core_id < static_cast<s32>(cpu::NumCores));
MESOSPHERE_ASSERT(thread != nullptr); MESOSPHERE_ASSERT(thread != nullptr);
MESOSPHERE_ASSERT(m_pinned_threads[core_id] == nullptr); MESOSPHERE_ASSERT(m_pinned_threads[core_id] == nullptr);
m_pinned_threads[core_id] = thread; m_pinned_threads[core_id] = thread;
} }
void UnpinThread(s32 core_id, KThread *thread) { ALWAYS_INLINE void UnpinThread(s32 core_id, KThread *thread) {
MESOSPHERE_UNUSED(thread); MESOSPHERE_UNUSED(thread);
MESOSPHERE_ASSERT(0 <= core_id && core_id < static_cast<s32>(cpu::NumCores)); MESOSPHERE_ASSERT(0 <= core_id && core_id < static_cast<s32>(cpu::NumCores));
MESOSPHERE_ASSERT(thread != nullptr); MESOSPHERE_ASSERT(thread != nullptr);
@@ -145,7 +145,7 @@ namespace ams::kern {
KProcess() { /* ... */ } KProcess() { /* ... */ }
virtual ~KProcess() { /* ... */ } virtual ~KProcess() { /* ... */ }
Result Initialize(const ams::svc::CreateProcessParameter &params, const KPageGroup &pg, const u32 *caps, s32 num_caps, KResourceLimit *res_limit, KMemoryManager::Pool pool); Result Initialize(const ams::svc::CreateProcessParameter &params, const KPageGroup &pg, const u32 *caps, s32 num_caps, KResourceLimit *res_limit, KMemoryManager::Pool pool, bool immortal);
Result Initialize(const ams::svc::CreateProcessParameter &params, svc::KUserPointer<const u32 *> caps, s32 num_caps, KResourceLimit *res_limit, KMemoryManager::Pool pool); Result Initialize(const ams::svc::CreateProcessParameter &params, svc::KUserPointer<const u32 *> caps, s32 num_caps, KResourceLimit *res_limit, KMemoryManager::Pool pool);
void Exit(); void Exit();
@@ -285,8 +285,8 @@ namespace ams::kern {
constexpr s64 GetScheduledCount() const { return m_schedule_count; } constexpr s64 GetScheduledCount() const { return m_schedule_count; }
void IncrementScheduledCount() { ++m_schedule_count; } void IncrementScheduledCount() { ++m_schedule_count; }
void IncrementThreadCount(); void IncrementRunningThreadCount();
void DecrementThreadCount(); void DecrementRunningThreadCount();
size_t GetTotalSystemResourceSize() const { return m_system_resource_num_pages * PageSize; } size_t GetTotalSystemResourceSize() const { return m_system_resource_num_pages * PageSize; }
size_t GetUsedSystemResourceSize() const { size_t GetUsedSystemResourceSize() const {
@@ -340,6 +340,7 @@ namespace ams::kern {
void PinCurrentThread(); void PinCurrentThread();
void UnpinCurrentThread(); void UnpinCurrentThread();
void UnpinThread(KThread *thread);
Result SignalToAddress(KProcessAddress address) { Result SignalToAddress(KProcessAddress address) {
return m_cond_var.SignalToAddress(address); return m_cond_var.SignalToAddress(address);
@@ -405,6 +406,23 @@ namespace ams::kern {
this->NotifyAvailable(); this->NotifyAvailable();
} }
} }
ALWAYS_INLINE Result InitializeHandleTable(s32 size) {
/* Try to initialize the handle table. */
R_TRY(m_handle_table.Initialize(size));
/* We succeeded, so note that we did. */
m_is_handle_table_initialized = true;
return ResultSuccess();
}
ALWAYS_INLINE void FinalizeHandleTable() {
/* Finalize the table. */
m_handle_table.Finalize();
/* Note that the table is finalized. */
m_is_handle_table_initialized = false;
}
}; };
} }

View File

@@ -37,14 +37,22 @@ namespace ams::kern {
private: private:
KServerSession m_server; KServerSession m_server;
KClientSession m_client; KClientSession m_client;
State m_state; std::atomic<std::underlying_type<State>::type> m_atomic_state;
KClientPort *m_port; KClientPort *m_port;
uintptr_t m_name; uintptr_t m_name;
KProcess *m_process; KProcess *m_process;
bool m_initialized; bool m_initialized;
private:
ALWAYS_INLINE void SetState(State state) {
m_atomic_state = static_cast<u8>(state);
}
ALWAYS_INLINE State GetState() const {
return static_cast<State>(m_atomic_state.load());
}
public: public:
constexpr KSession() constexpr KSession()
: m_server(), m_client(), m_state(State::Invalid), m_port(), m_name(), m_process(), m_initialized() : m_server(), m_client(), m_atomic_state(static_cast<std::underlying_type<State>::type>(State::Invalid)), m_port(), m_name(), m_process(), m_initialized()
{ {
/* ... */ /* ... */
} }
@@ -62,8 +70,8 @@ namespace ams::kern {
void OnServerClosed(); void OnServerClosed();
void OnClientClosed(); void OnClientClosed();
bool IsServerClosed() const { return m_state != State::Normal; } bool IsServerClosed() const { return this->GetState() != State::Normal; }
bool IsClientClosed() const { return m_state != State::Normal; } bool IsClientClosed() const { return this->GetState() != State::Normal; }
Result OnRequest(KSessionRequest *request) { return m_server.OnRequest(request); } Result OnRequest(KSessionRequest *request) { return m_server.OnRequest(request); }

View File

@@ -216,7 +216,7 @@ namespace ams::kern {
T *Allocate() { T *Allocate() {
T *obj = reinterpret_cast<T *>(this->AllocateImpl()); T *obj = reinterpret_cast<T *>(this->AllocateImpl());
if (AMS_LIKELY(obj != nullptr)) { if (AMS_LIKELY(obj != nullptr)) {
new (obj) T(); std::construct_at(obj);
} }
return obj; return obj;
} }

View File

@@ -16,7 +16,6 @@
#pragma once #pragma once
#include <mesosphere/kern_k_auto_object.hpp> #include <mesosphere/kern_k_auto_object.hpp>
#include <mesosphere/kern_slab_helpers.hpp> #include <mesosphere/kern_slab_helpers.hpp>
#include <mesosphere/kern_k_linked_list.hpp>
namespace ams::kern { namespace ams::kern {
@@ -46,7 +45,7 @@ namespace ams::kern {
static Result Wait(s32 *out_index, KSynchronizationObject **objects, const s32 num_objects, s64 timeout); static Result Wait(s32 *out_index, KSynchronizationObject **objects, const s32 num_objects, s64 timeout);
public: public:
virtual void Finalize() override; virtual void Finalize() override;
virtual bool IsSignaled() const = 0; virtual bool IsSignaled() const { AMS_INFINITE_LOOP(); }
virtual void DumpWaiters(); virtual void DumpWaiters();
}; };

View File

@@ -77,22 +77,34 @@ namespace ams::kern {
}; };
enum DpcFlag : u32 { enum DpcFlag : u32 {
DpcFlag_Terminating = (1 << 0), DpcFlag_Terminating = (1 << 0),
DpcFlag_Terminated = (1 << 1), DpcFlag_Terminated = (1 << 1),
DpcFlag_PerformDestruction = (1 << 2),
}; };
struct StackParameters { struct StackParameters {
alignas(0x10) u8 svc_permission[0x10]; alignas(0x10) u8 svc_permission[0x18];
KThreadContext *context;
KThread *cur_thread;
s16 disable_count;
std::atomic<u8> dpc_flags; std::atomic<u8> dpc_flags;
u8 current_svc_id; u8 current_svc_id;
bool is_calling_svc; bool is_calling_svc;
bool is_in_exception_handler; bool is_in_exception_handler;
bool is_pinned; bool is_pinned;
s32 disable_count;
KThreadContext *context;
KThread *cur_thread;
}; };
static_assert(alignof(StackParameters) == 0x10); static_assert(alignof(StackParameters) == 0x10);
static_assert(sizeof(StackParameters) == THREAD_STACK_PARAMETERS_SIZE);
static_assert(__builtin_offsetof(StackParameters, svc_permission) == THREAD_STACK_PARAMETERS_SVC_PERMISSION);
static_assert(__builtin_offsetof(StackParameters, context) == THREAD_STACK_PARAMETERS_CONTEXT);
static_assert(__builtin_offsetof(StackParameters, cur_thread) == THREAD_STACK_PARAMETERS_CUR_THREAD);
static_assert(__builtin_offsetof(StackParameters, disable_count) == THREAD_STACK_PARAMETERS_DISABLE_COUNT);
static_assert(__builtin_offsetof(StackParameters, dpc_flags) == THREAD_STACK_PARAMETERS_DPC_FLAGS);
static_assert(__builtin_offsetof(StackParameters, current_svc_id) == THREAD_STACK_PARAMETERS_CURRENT_SVC_ID);
static_assert(__builtin_offsetof(StackParameters, is_calling_svc) == THREAD_STACK_PARAMETERS_IS_CALLING_SVC);
static_assert(__builtin_offsetof(StackParameters, is_in_exception_handler) == THREAD_STACK_PARAMETERS_IS_IN_EXCEPTION_HANDLER);
static_assert(__builtin_offsetof(StackParameters, is_pinned) == THREAD_STACK_PARAMETERS_IS_PINNED);
struct QueueEntry { struct QueueEntry {
private: private:
@@ -192,12 +204,14 @@ namespace ams::kern {
WaiterList m_pinned_waiter_list{}; WaiterList m_pinned_waiter_list{};
KThread *m_lock_owner{}; KThread *m_lock_owner{};
uintptr_t m_debug_params[3]{}; uintptr_t m_debug_params[3]{};
KAutoObject *m_closed_object{};
u32 m_address_key_value{}; u32 m_address_key_value{};
u32 m_suspend_request_flags{}; u32 m_suspend_request_flags{};
u32 m_suspend_allowed_flags{}; u32 m_suspend_allowed_flags{};
Result m_wait_result; Result m_wait_result;
Result m_debug_exception_result; Result m_debug_exception_result;
s32 m_base_priority{}; s32 m_base_priority{};
s32 m_base_priority_on_unpin{};
s32 m_physical_ideal_core_id{}; s32 m_physical_ideal_core_id{};
s32 m_virtual_ideal_core_id{}; s32 m_virtual_ideal_core_id{};
s32 m_num_kernel_waiters{}; s32 m_num_kernel_waiters{};
@@ -251,7 +265,7 @@ namespace ams::kern {
return *(reinterpret_cast<StackParameters *>(m_kernel_stack_top) - 1); return *(reinterpret_cast<StackParameters *>(m_kernel_stack_top) - 1);
} }
public: public:
ALWAYS_INLINE s32 GetDisableDispatchCount() const { ALWAYS_INLINE s16 GetDisableDispatchCount() const {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
return this->GetStackParameters().disable_count; return this->GetStackParameters().disable_count;
} }
@@ -312,15 +326,15 @@ namespace ams::kern {
} }
ALWAYS_INLINE void RegisterDpc(DpcFlag flag) { ALWAYS_INLINE void RegisterDpc(DpcFlag flag) {
this->GetStackParameters().dpc_flags |= flag; this->GetStackParameters().dpc_flags.fetch_or(flag);
} }
ALWAYS_INLINE void ClearDpc(DpcFlag flag) { ALWAYS_INLINE void ClearDpc(DpcFlag flag) {
this->GetStackParameters().dpc_flags &= ~flag; this->GetStackParameters().dpc_flags.fetch_and(~flag);
} }
ALWAYS_INLINE u8 GetDpc() const { ALWAYS_INLINE u8 GetDpc() const {
return this->GetStackParameters().dpc_flags; return this->GetStackParameters().dpc_flags.load();
} }
ALWAYS_INLINE bool HasDpc() const { ALWAYS_INLINE bool HasDpc() const {
@@ -328,13 +342,15 @@ namespace ams::kern {
return this->GetDpc() != 0; return this->GetDpc() != 0;
} }
private: private:
void Suspend(); void UpdateState();
ALWAYS_INLINE void AddWaiterImpl(KThread *thread); ALWAYS_INLINE void AddWaiterImpl(KThread *thread);
ALWAYS_INLINE void RemoveWaiterImpl(KThread *thread); ALWAYS_INLINE void RemoveWaiterImpl(KThread *thread);
ALWAYS_INLINE static void RestorePriority(KThread *thread); ALWAYS_INLINE static void RestorePriority(KThread *thread);
void StartTermination(); void StartTermination();
void FinishTermination(); void FinishTermination();
void IncreaseBasePriority(s32 priority);
public: public:
constexpr u64 GetThreadId() const { return m_thread_id; } constexpr u64 GetThreadId() const { return m_thread_id; }
@@ -477,6 +493,39 @@ namespace ams::kern {
void SetInterruptFlag() const { static_cast<ams::svc::ThreadLocalRegion *>(m_tls_heap_address)->interrupt_flag = 1; } void SetInterruptFlag() const { static_cast<ams::svc::ThreadLocalRegion *>(m_tls_heap_address)->interrupt_flag = 1; }
void ClearInterruptFlag() const { static_cast<ams::svc::ThreadLocalRegion *>(m_tls_heap_address)->interrupt_flag = 0; } void ClearInterruptFlag() const { static_cast<ams::svc::ThreadLocalRegion *>(m_tls_heap_address)->interrupt_flag = 0; }
ALWAYS_INLINE KAutoObject *GetClosedObject() { return m_closed_object; }
ALWAYS_INLINE void SetClosedObject(KAutoObject *object) {
MESOSPHERE_ASSERT(object != nullptr);
/* Set the object to destroy. */
m_closed_object = object;
/* Schedule destruction DPC. */
if ((this->GetStackParameters().dpc_flags.load(std::memory_order_relaxed) & DpcFlag_PerformDestruction) == 0) {
this->RegisterDpc(DpcFlag_PerformDestruction);
}
}
ALWAYS_INLINE void DestroyClosedObjects() {
/* Destroy all objects that have been closed. */
if (KAutoObject *cur = m_closed_object; cur != nullptr) {
do {
/* Set our closed object as the next to close. */
m_closed_object = cur->GetNextClosedObject();
/* Destroy the current object. */
cur->Destroy();
/* Advance. */
cur = m_closed_object;
} while (cur != nullptr);
/* Clear the pending DPC. */
this->ClearDpc(DpcFlag_PerformDestruction);
}
}
constexpr void SetDebugAttached() { m_debug_attached = true; } constexpr void SetDebugAttached() { m_debug_attached = true; }
constexpr bool IsAttachedToDebugger() const { return m_debug_attached; } constexpr bool IsAttachedToDebugger() const { return m_debug_attached; }
@@ -521,7 +570,7 @@ namespace ams::kern {
Result Run(); Result Run();
void Exit(); void Exit();
void Terminate(); Result Terminate();
ThreadState RequestTerminate(); ThreadState RequestTerminate();
Result Sleep(s64 timeout); Result Sleep(s64 timeout);
@@ -589,4 +638,14 @@ namespace ams::kern {
return GetCurrentThread().GetCurrentCore(); return GetCurrentThread().GetCurrentCore();
} }
ALWAYS_INLINE void KAutoObject::ScheduleDestruction() {
MESOSPHERE_ASSERT_THIS();
/* Set our object to destroy. */
m_next_closed_object = GetCurrentThread().GetClosedObject();
/* Set ourselves as the thread's next object to destroy. */
GetCurrentThread().SetClosedObject(this);
}
} }

View File

@@ -23,7 +23,7 @@ namespace ams::kern {
class KTransferMemory final : public KAutoObjectWithSlabHeapAndContainer<KTransferMemory, KAutoObjectWithList> { class KTransferMemory final : public KAutoObjectWithSlabHeapAndContainer<KTransferMemory, KAutoObjectWithList> {
MESOSPHERE_AUTOOBJECT_TRAITS(KTransferMemory, KAutoObject); MESOSPHERE_AUTOOBJECT_TRAITS(KTransferMemory, KAutoObject);
private: private:
TYPED_STORAGE(KPageGroup) m_page_group; util::TypedStorage<KPageGroup> m_page_group;
KProcess *m_owner; KProcess *m_owner;
KProcessAddress m_address; KProcessAddress m_address;
KLightLock m_lock; KLightLock m_lock;

View File

@@ -0,0 +1,26 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifdef ATMOSPHERE_ARCH_ARM64
#include <mesosphere/arch/arm64/kern_assembly_offsets.h>
#else
#error "Unknown architecture for CPU"
#endif

View File

@@ -20,7 +20,7 @@
namespace ams::kern::svc { namespace ams::kern::svc {
static constexpr size_t NumSupervisorCalls = 0x80; static constexpr size_t NumSupervisorCalls = 0xC0;
#define AMS_KERN_SVC_DECLARE_ENUM_ID(ID, RETURN_TYPE, NAME, ...) \ #define AMS_KERN_SVC_DECLARE_ENUM_ID(ID, RETURN_TYPE, NAME, ...) \
SvcId_##NAME = ID, SvcId_##NAME = ID,

View File

@@ -283,6 +283,16 @@ namespace ams::kern::arch::arm64::cpu {
} }
} }
void StoreDataCacheBySetWay(int level) {
PerformCacheOperationBySetWayImpl<false>(level, StoreDataCacheLineBySetWayImpl);
cpu::DataSynchronizationBarrier();
}
void FlushDataCacheBySetWay(int level) {
PerformCacheOperationBySetWayImpl<false>(level, FlushDataCacheLineBySetWayImpl);
cpu::DataSynchronizationBarrier();
}
void KCacheHelperInterruptHandler::ProcessOperation() { void KCacheHelperInterruptHandler::ProcessOperation() {
switch (m_operation) { switch (m_operation) {
case Operation::Idle: case Operation::Idle:
@@ -291,12 +301,10 @@ namespace ams::kern::arch::arm64::cpu {
InstructionMemoryBarrier(); InstructionMemoryBarrier();
break; break;
case Operation::StoreDataCache: case Operation::StoreDataCache:
PerformCacheOperationBySetWayLocal<false>(StoreDataCacheLineBySetWayImpl); StoreDataCacheBySetWay(0);
DataSynchronizationBarrier();
break; break;
case Operation::FlushDataCache: case Operation::FlushDataCache:
PerformCacheOperationBySetWayLocal<false>(FlushDataCacheLineBySetWayImpl); FlushDataCacheBySetWay(0);
DataSynchronizationBarrier();
break; break;
} }
@@ -374,7 +382,20 @@ namespace ams::kern::arch::arm64::cpu {
} }
void FlushEntireDataCache() { void FlushEntireDataCache() {
return PerformCacheOperationBySetWayShared<false>(FlushDataCacheLineBySetWayImpl); KScopedCoreMigrationDisable dm;
CacheLineIdRegisterAccessor clidr_el1;
const int levels_of_coherency = clidr_el1.GetLevelsOfCoherency();
/* Store cache from L2 up to the level of coherence (if there's an L3 cache or greater). */
for (int level = 2; level < levels_of_coherency; ++level) {
StoreDataCacheBySetWay(level - 1);
}
/* Flush cache from the level of coherence down to L2. */
for (int level = levels_of_coherency; level > 1; --level) {
FlushDataCacheBySetWay(level - 1);
}
} }
Result InvalidateDataCache(void *addr, size_t size) { Result InvalidateDataCache(void *addr, size_t size) {

View File

@@ -521,6 +521,11 @@ namespace ams::kern::arch::arm64 {
{ {
KScopedInterruptEnable ei; KScopedInterruptEnable ei;
/* Terminate the thread, if we should. */
if (GetCurrentThread().IsTerminationRequested()) {
GetCurrentThread().Exit();
}
HandleUserException(context, esr, far, afsr0, afsr1, data); HandleUserException(context, esr, far, afsr0, afsr1, data);
} }
} else { } else {

View File

@@ -239,14 +239,6 @@ namespace ams::kern::arch::arm64 {
} }
} }
Result KInterruptManager::ClearInterrupt(s32 irq) {
R_UNLESS(KInterruptController::IsGlobal(irq), svc::ResultOutOfRange());
KScopedInterruptDisable di;
KScopedSpinLock lk(this->GetGlobalInterruptLock());
return this->ClearGlobal(irq);
}
Result KInterruptManager::ClearInterrupt(s32 irq, s32 core_id) { Result KInterruptManager::ClearInterrupt(s32 irq, s32 core_id) {
MESOSPHERE_UNUSED(core_id); MESOSPHERE_UNUSED(core_id);

View File

@@ -181,7 +181,7 @@ namespace ams::kern::arch::arm64 {
return ResultSuccess(); return ResultSuccess();
} }
Result KPageTable::InitializeForProcess(u32 id, ams::svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_das_merge, bool from_back, KMemoryManager::Pool pool, KProcessAddress code_address, size_t code_size, KMemoryBlockSlabManager *mem_block_slab_manager, KBlockInfoManager *block_info_manager, KPageTableManager *pt_manager) { Result KPageTable::InitializeForProcess(u32 id, ams::svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_das_merge, bool from_back, KMemoryManager::Pool pool, KProcessAddress code_address, size_t code_size, KMemoryBlockSlabManager *mem_block_slab_manager, KBlockInfoManager *block_info_manager, KPageTableManager *pt_manager, KResourceLimit *resource_limit) {
/* The input ID isn't actually used. */ /* The input ID isn't actually used. */
MESOSPHERE_UNUSED(id); MESOSPHERE_UNUSED(id);
@@ -202,7 +202,7 @@ namespace ams::kern::arch::arm64 {
const size_t as_width = GetAddressSpaceWidth(as_type); const size_t as_width = GetAddressSpaceWidth(as_type);
const KProcessAddress as_start = 0; const KProcessAddress as_start = 0;
const KProcessAddress as_end = (1ul << as_width); const KProcessAddress as_end = (1ul << as_width);
R_TRY(KPageTableBase::InitializeForProcess(as_type, enable_aslr, enable_das_merge, from_back, pool, GetVoidPointer(new_table), as_start, as_end, code_address, code_size, mem_block_slab_manager, block_info_manager)); R_TRY(KPageTableBase::InitializeForProcess(as_type, enable_aslr, enable_das_merge, from_back, pool, GetVoidPointer(new_table), as_start, as_end, code_address, code_size, mem_block_slab_manager, block_info_manager, resource_limit));
/* We succeeded! */ /* We succeeded! */
table_guard.Cancel(); table_guard.Cancel();
@@ -556,13 +556,13 @@ namespace ams::kern::arch::arm64 {
/* If we're not forcing an unmap, separate pages immediately. */ /* If we're not forcing an unmap, separate pages immediately. */
if (!force) { if (!force) {
const size_t size = num_pages * PageSize; const size_t size = num_pages * PageSize;
R_TRY(this->SeparatePages(virt_addr, std::min(GetInteger(virt_addr) & -GetInteger(virt_addr), size), page_list, reuse_ll)); R_TRY(this->SeparatePages(virt_addr, std::min(util::GetAlignment(GetInteger(virt_addr)), size), page_list, reuse_ll));
if (num_pages > 1) { if (num_pages > 1) {
const auto end_page = virt_addr + size; const auto end_page = virt_addr + size;
const auto last_page = end_page - PageSize; const auto last_page = end_page - PageSize;
auto merge_guard = SCOPE_GUARD { this->MergePages(virt_addr, page_list); }; auto merge_guard = SCOPE_GUARD { this->MergePages(virt_addr, page_list); };
R_TRY(this->SeparatePages(last_page, std::min(GetInteger(end_page) & -GetInteger(end_page), size), page_list, reuse_ll)); R_TRY(this->SeparatePages(last_page, std::min(util::GetAlignment(GetInteger(end_page)), size), page_list, reuse_ll));
merge_guard.Cancel(); merge_guard.Cancel();
} }
} }
@@ -1194,13 +1194,13 @@ namespace ams::kern::arch::arm64 {
/* Separate pages before we change permissions. */ /* Separate pages before we change permissions. */
const size_t size = num_pages * PageSize; const size_t size = num_pages * PageSize;
R_TRY(this->SeparatePages(virt_addr, std::min(GetInteger(virt_addr) & -GetInteger(virt_addr), size), page_list, reuse_ll)); R_TRY(this->SeparatePages(virt_addr, std::min(util::GetAlignment(GetInteger(virt_addr)), size), page_list, reuse_ll));
if (num_pages > 1) { if (num_pages > 1) {
const auto end_page = virt_addr + size; const auto end_page = virt_addr + size;
const auto last_page = end_page - PageSize; const auto last_page = end_page - PageSize;
auto merge_guard = SCOPE_GUARD { this->MergePages(virt_addr, page_list); }; auto merge_guard = SCOPE_GUARD { this->MergePages(virt_addr, page_list); };
R_TRY(this->SeparatePages(last_page, std::min(GetInteger(end_page) & -GetInteger(end_page), size), page_list, reuse_ll)); R_TRY(this->SeparatePages(last_page, std::min(util::GetAlignment(GetInteger(end_page)), size), page_list, reuse_ll));
merge_guard.Cancel(); merge_guard.Cancel();
} }

View File

@@ -758,7 +758,6 @@ _ZN3ams4kern4arch5arm6415UserspaceAccess18WriteIoMemory32BitEPvPKvm:
mov x8, x30 mov x8, x30
1: /* Read the word from normal memory. */ 1: /* Read the word from normal memory. */
mov x30, x8
ldtr w9, [x5] ldtr w9, [x5]
/* Set our return address so that on read failure we continue. */ /* Set our return address so that on read failure we continue. */
@@ -769,7 +768,7 @@ _ZN3ams4kern4arch5arm6415UserspaceAccess18WriteIoMemory32BitEPvPKvm:
dsb sy dsb sy
2: /* Continue. */ 2: /* Continue. */
nop mov x30, x8
/* Advance. */ /* Advance. */
add x4, x4, #4 add x4, x4, #4
@@ -801,7 +800,6 @@ _ZN3ams4kern4arch5arm6415UserspaceAccess18WriteIoMemory16BitEPvPKvm:
mov x8, x30 mov x8, x30
1: /* Read the word from normal memory. */ 1: /* Read the word from normal memory. */
mov x30, x8
ldtrh w9, [x5] ldtrh w9, [x5]
/* Set our return address so that on read failure we continue. */ /* Set our return address so that on read failure we continue. */
@@ -812,7 +810,7 @@ _ZN3ams4kern4arch5arm6415UserspaceAccess18WriteIoMemory16BitEPvPKvm:
dsb sy dsb sy
2: /* Continue. */ 2: /* Continue. */
nop mov x30, x8
/* Advance. */ /* Advance. */
add x4, x4, #2 add x4, x4, #2
@@ -844,7 +842,6 @@ _ZN3ams4kern4arch5arm6415UserspaceAccess17WriteIoMemory8BitEPvPKvm:
mov x8, x30 mov x8, x30
1: /* Read the word from normal memory. */ 1: /* Read the word from normal memory. */
mov x30, x8
ldtrb w9, [x5] ldtrb w9, [x5]
/* Set our return address so that on read failure we continue. */ /* Set our return address so that on read failure we continue. */
@@ -855,7 +852,7 @@ _ZN3ams4kern4arch5arm6415UserspaceAccess17WriteIoMemory8BitEPvPKvm:
dsb sy dsb sy
2: /* Continue. */ 2: /* Continue. */
nop mov x30, x8
/* Advance. */ /* Advance. */
add x4, x4, #1 add x4, x4, #1

View File

@@ -13,6 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <mesosphere/kern_select_assembly_offsets.h>
/* ams::kern::svc::CallReturnFromException64(Result result) */ /* ams::kern::svc::CallReturnFromException64(Result result) */
.section .text._ZN3ams4kern3svc25CallReturnFromException64Ev, "ax", %progbits .section .text._ZN3ams4kern3svc25CallReturnFromException64Ev, "ax", %progbits
@@ -62,7 +63,7 @@ _ZN3ams4kern3svc14RestoreContextEm:
0: /* We should handle DPC. */ 0: /* We should handle DPC. */
/* Check the dpc flags. */ /* Check the dpc flags. */
ldrb w8, [sp, #(0x120 + 0x10)] ldrb w8, [sp, #(0x120 + THREAD_STACK_PARAMETERS_DPC_FLAGS)]
cbz w8, 1f cbz w8, 1f
/* We have DPC to do! */ /* We have DPC to do! */
@@ -82,7 +83,7 @@ _ZN3ams4kern3svc14RestoreContextEm:
1: /* We're done with DPC, and should return from the svc. */ 1: /* We're done with DPC, and should return from the svc. */
/* Clear our in-SVC note. */ /* Clear our in-SVC note. */
strb wzr, [sp, #(0x120 + 0x12)] strb wzr, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_CALLING_SVC)]
/* Restore registers. */ /* Restore registers. */
ldp x30, x8, [sp, #(8 * 30)] ldp x30, x8, [sp, #(8 * 30)]

View File

@@ -14,6 +14,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <mesosphere/kern_build_config.hpp> #include <mesosphere/kern_build_config.hpp>
#include <mesosphere/kern_select_assembly_offsets.h>
/* ams::kern::arch::arm64::SvcHandler64() */ /* ams::kern::arch::arm64::SvcHandler64() */
.section .text._ZN3ams4kern4arch5arm6412SvcHandler64Ev, "ax", %progbits .section .text._ZN3ams4kern4arch5arm6412SvcHandler64Ev, "ax", %progbits
@@ -32,7 +33,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
mrs x9, elr_el1 mrs x9, elr_el1
mrs x10, spsr_el1 mrs x10, spsr_el1
mrs x11, tpidr_el0 mrs x11, tpidr_el0
ldr x18, [sp, #(0x120 + 0x28)] ldr x18, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CUR_THREAD)]
/* Save callee-saved registers. */ /* Save callee-saved registers. */
stp x19, x20, [sp, #(8 * 19)] stp x19, x20, [sp, #(8 * 19)]
@@ -59,7 +60,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
/* Check the specific SVC permission bit for allowal. */ /* Check the specific SVC permission bit for allowal. */
mov x9, sp mov x9, sp
add x9, x9, x8, lsr#3 add x9, x9, x8, lsr#3
ldrb w9, [x9, #0x120] ldrb w9, [x9, #(0x120 + THREAD_STACK_PARAMETERS_SVC_PERMISSION)]
and x10, x8, #0x7 and x10, x8, #0x7
lsr x10, x9, x10 lsr x10, x9, x10
tst x10, #1 tst x10, #1
@@ -71,7 +72,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
cbz w10, 1f cbz w10, 1f
/* It might not, so check the stack params to see if we must not allow the SVC. */ /* It might not, so check the stack params to see if we must not allow the SVC. */
ldrb w10, [sp, #(0x120 + 0x14)] ldrb w10, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_PINNED)]
cbz w10, 3f cbz w10, 3f
1: /* We can call the SVC. */ 1: /* We can call the SVC. */
@@ -81,8 +82,8 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
/* Note that we're calling the SVC. */ /* Note that we're calling the SVC. */
mov w10, #1 mov w10, #1
strb w10, [sp, #(0x120 + 0x12)] strb w10, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_CALLING_SVC)]
strb w8, [sp, #(0x120 + 0x11)] strb w8, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CURRENT_SVC_ID)]
/* If we should, trace the svc entry. */ /* If we should, trace the svc entry. */
#if defined(MESOSPHERE_BUILD_FOR_TRACING) #if defined(MESOSPHERE_BUILD_FOR_TRACING)
@@ -109,7 +110,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
2: /* We completed the SVC, and we should handle DPC. */ 2: /* We completed the SVC, and we should handle DPC. */
/* Check the dpc flags. */ /* Check the dpc flags. */
ldrb w8, [sp, #(0x120 + 0x10)] ldrb w8, [sp, #(0x120 + THREAD_STACK_PARAMETERS_DPC_FLAGS)]
cbz w8, 4f cbz w8, 4f
/* We have DPC to do! */ /* We have DPC to do! */
@@ -179,7 +180,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
4: /* Return from SVC. */ 4: /* Return from SVC. */
/* Clear our in-SVC note. */ /* Clear our in-SVC note. */
strb wzr, [sp, #(0x120 + 0x12)] strb wzr, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_CALLING_SVC)]
/* If we should, trace the svc exit. */ /* If we should, trace the svc exit. */
#if defined(MESOSPHERE_BUILD_FOR_TRACING) #if defined(MESOSPHERE_BUILD_FOR_TRACING)
@@ -245,7 +246,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
mrs x17, elr_el1 mrs x17, elr_el1
mrs x20, spsr_el1 mrs x20, spsr_el1
mrs x19, tpidr_el0 mrs x19, tpidr_el0
ldr x18, [sp, #(0x120 + 0x28)] ldr x18, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CUR_THREAD)]
stp x17, x20, [sp, #(8 * 32)] stp x17, x20, [sp, #(8 * 32)]
str x19, [sp, #(8 * 34)] str x19, [sp, #(8 * 34)]
@@ -268,7 +269,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
/* Check the specific SVC permission bit for allowal. */ /* Check the specific SVC permission bit for allowal. */
mov x20, sp mov x20, sp
add x20, x20, x16, lsr#3 add x20, x20, x16, lsr#3
ldrb w20, [x20, #0x120] ldrb w20, [x20, #(0x120 + THREAD_STACK_PARAMETERS_SVC_PERMISSION)]
and x17, x16, #0x7 and x17, x16, #0x7
lsr x17, x20, x17 lsr x17, x20, x17
tst x17, #1 tst x17, #1
@@ -280,7 +281,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
cbz w15, 1f cbz w15, 1f
/* It might not, so check the stack params to see if we must not allow the SVC. */ /* It might not, so check the stack params to see if we must not allow the SVC. */
ldrb w15, [sp, #(0x120 + 0x14)] ldrb w15, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_PINNED)]
cbz w15, 3f cbz w15, 3f
1: /* We can call the SVC. */ 1: /* We can call the SVC. */
@@ -290,8 +291,8 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
/* Note that we're calling the SVC. */ /* Note that we're calling the SVC. */
mov w15, #1 mov w15, #1
strb w15, [sp, #(0x120 + 0x12)] strb w15, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_CALLING_SVC)]
strb w16, [sp, #(0x120 + 0x11)] strb w16, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CURRENT_SVC_ID)]
/* If we should, trace the svc entry. */ /* If we should, trace the svc entry. */
#if defined(MESOSPHERE_BUILD_FOR_TRACING) #if defined(MESOSPHERE_BUILD_FOR_TRACING)
@@ -318,7 +319,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
2: /* We completed the SVC, and we should handle DPC. */ 2: /* We completed the SVC, and we should handle DPC. */
/* Check the dpc flags. */ /* Check the dpc flags. */
ldrb w16, [sp, #(0x120 + 0x10)] ldrb w16, [sp, #(0x120 + THREAD_STACK_PARAMETERS_DPC_FLAGS)]
cbz w16, 4f cbz w16, 4f
/* We have DPC to do! */ /* We have DPC to do! */
@@ -376,7 +377,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
4: /* Return from SVC. */ 4: /* Return from SVC. */
/* Clear our in-SVC note. */ /* Clear our in-SVC note. */
strb wzr, [sp, #(0x120 + 0x12)] strb wzr, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_CALLING_SVC)]
/* If we should, trace the svc exit. */ /* If we should, trace the svc exit. */
#if defined(MESOSPHERE_BUILD_FOR_TRACING) #if defined(MESOSPHERE_BUILD_FOR_TRACING)

View File

@@ -216,6 +216,12 @@ namespace ams::kern::board::nintendo::nx {
return (m_value & (1u << n)); return (m_value & (1u << n));
} }
template<Bit... Bits>
constexpr ALWAYS_INLINE u32 SelectBits() const {
constexpr u32 Mask = ((1u << Bits) | ...);
return m_value & Mask;
}
constexpr ALWAYS_INLINE bool GetBit(Bit n) const { constexpr ALWAYS_INLINE bool GetBit(Bit n) const {
return this->SelectBit(n) != 0; return this->SelectBit(n) != 0;
} }
@@ -242,12 +248,14 @@ namespace ams::kern::board::nintendo::nx {
constexpr ALWAYS_INLINE bool IsNonSecure() const { return this->GetBit(Bit_NonSecure); } constexpr ALWAYS_INLINE bool IsNonSecure() const { return this->GetBit(Bit_NonSecure); }
constexpr ALWAYS_INLINE bool IsWriteable() const { return this->GetBit(Bit_Writeable); } constexpr ALWAYS_INLINE bool IsWriteable() const { return this->GetBit(Bit_Writeable); }
constexpr ALWAYS_INLINE bool IsReadable() const { return this->GetBit(Bit_Readable); } constexpr ALWAYS_INLINE bool IsReadable() const { return this->GetBit(Bit_Readable); }
constexpr ALWAYS_INLINE bool IsValid() const { return this->IsWriteable() || this->IsReadable(); } constexpr ALWAYS_INLINE bool IsValid() const { return this->SelectBits<Bit_Readable, Bit_Writeable>(); }
constexpr ALWAYS_INLINE u32 GetAttributes() const { return this->SelectBit(Bit_NonSecure) | this->SelectBit(Bit_Writeable) | this->SelectBit(Bit_Readable); } constexpr ALWAYS_INLINE u32 GetAttributes() const { return this->SelectBits<Bit_Readable, Bit_Writeable, Bit_NonSecure>(); }
constexpr ALWAYS_INLINE KPhysicalAddress GetPhysicalAddress() const { return (static_cast<u64>(m_value) << DevicePageBits) & PhysicalAddressMask; } constexpr ALWAYS_INLINE KPhysicalAddress GetPhysicalAddress() const { return (static_cast<u64>(m_value) << DevicePageBits) & PhysicalAddressMask; }
ALWAYS_INLINE void InvalidateAttributes() { this->SetValue(m_value & ~(0xCu << 28)); }
ALWAYS_INLINE void Invalidate() { this->SetValue(0); } ALWAYS_INLINE void Invalidate() { this->SetValue(0); }
}; };
@@ -526,7 +534,7 @@ namespace ams::kern::board::nintendo::nx {
#if defined(MESOSPHERE_ENABLE_MEMORY_CONTROLLER_INTERRUPT) #if defined(MESOSPHERE_ENABLE_MEMORY_CONTROLLER_INTERRUPT)
{ {
/* Clear the interrupt when we're done. */ /* Clear the interrupt when we're done. */
ON_SCOPE_EXIT { Kernel::GetInterruptManager().ClearInterrupt(KInterruptName_MemoryController); }; ON_SCOPE_EXIT { Kernel::GetInterruptManager().ClearInterrupt(KInterruptName_MemoryController, GetCurrentCoreId()); };
/* Get and clear the interrupt status. */ /* Get and clear the interrupt status. */
u32 int_status, err_status, err_adr; u32 int_status, err_status, err_adr;
@@ -847,7 +855,7 @@ namespace ams::kern::board::nintendo::nx {
} }
/* Forcibly unmap all pages. */ /* Forcibly unmap all pages. */
this->UnmapImpl(0, (1ul << DeviceVirtualAddressBits), true); this->UnmapImpl(0, (1ul << DeviceVirtualAddressBits), false);
/* Release all asids. */ /* Release all asids. */
for (size_t i = 0; i < TableCount; ++i) { for (size_t i = 0; i < TableCount; ++i) {
@@ -1117,12 +1125,11 @@ namespace ams::kern::board::nintendo::nx {
return ResultSuccess(); return ResultSuccess();
} }
Result KDevicePageTable::MapImpl(size_t *out_mapped_size, s32 &num_pt, s32 max_pt, const KPageGroup &pg, KDeviceVirtualAddress device_address, ams::svc::MemoryPermission device_perm) { Result KDevicePageTable::MapImpl(size_t *out_mapped_size, s32 &num_pt, s32 max_pt, KProcessPageTable *page_table, KProcessAddress process_address, size_t size, KDeviceVirtualAddress device_address, ams::svc::MemoryPermission device_perm, bool is_aligned) {
/* Clear the output size. */ /* Clear the output size. */
*out_mapped_size = 0; *out_mapped_size = 0;
/* Get the size, and validate the address. */ /* Get the size, and validate the address. */
const u64 size = pg.GetNumPages() * PageSize;
MESOSPHERE_ASSERT((device_address & ~DeviceVirtualAddressMask) == 0); MESOSPHERE_ASSERT((device_address & ~DeviceVirtualAddressMask) == 0);
MESOSPHERE_ASSERT(((device_address + size - 1) & ~DeviceVirtualAddressMask) == 0); MESOSPHERE_ASSERT(((device_address + size - 1) & ~DeviceVirtualAddressMask) == 0);
@@ -1130,28 +1137,33 @@ namespace ams::kern::board::nintendo::nx {
R_UNLESS(this->IsFree(device_address, size), svc::ResultInvalidCurrentMemory()); R_UNLESS(this->IsFree(device_address, size), svc::ResultInvalidCurrentMemory());
/* Ensure that if we fail, we unmap anything we mapped. */ /* Ensure that if we fail, we unmap anything we mapped. */
auto unmap_guard = SCOPE_GUARD { this->UnmapImpl(device_address, size, true); }; auto unmap_guard = SCOPE_GUARD { this->UnmapImpl(device_address, size, false); };
/* Iterate, mapping device pages. */ /* Iterate, mapping device pages. */
KDeviceVirtualAddress cur_addr = device_address; KDeviceVirtualAddress cur_addr = device_address;
for (auto it = pg.begin(); it != pg.end(); ++it) { while (true) {
/* Require that we be able to map the device page. */ /* Get the current contiguous range. */
R_UNLESS(IsHeapVirtualAddress(it->GetAddress()), svc::ResultInvalidCurrentMemory()); KPageTableBase::MemoryRange contig_range = {};
R_TRY(page_table->OpenMemoryRangeForMapDeviceAddressSpace(std::addressof(contig_range), process_address + *out_mapped_size, size - *out_mapped_size, ConvertToKMemoryPermission(device_perm), is_aligned));
/* Get the physical address for the page. */ /* Ensure we close the range when we're done. */
const KPhysicalAddress phys_addr = GetHeapPhysicalAddress(it->GetAddress()); ON_SCOPE_EXIT { contig_range.Close(); };
/* Map the device page. */ /* Map the device page. */
const u64 block_size = it->GetSize();
size_t mapped_size = 0; size_t mapped_size = 0;
R_TRY(this->MapDevicePage(std::addressof(mapped_size), num_pt, max_pt, phys_addr, block_size, cur_addr, device_perm)); R_TRY(this->MapDevicePage(std::addressof(mapped_size), num_pt, max_pt, GetHeapPhysicalAddress(contig_range.address), contig_range.size, cur_addr, device_perm));
/* Advance. */ /* Advance. */
cur_addr += block_size; cur_addr += contig_range.size;
*out_mapped_size += mapped_size; *out_mapped_size += mapped_size;
/* If we didn't map as much as we wanted, break. */ /* If we didn't map as much as we wanted, break. */
if (mapped_size < block_size) { if (mapped_size < contig_range.size) {
break;
}
/* Similarly, if we're done, break. */
if (*out_mapped_size >= size) {
break; break;
} }
} }
@@ -1186,8 +1198,6 @@ namespace ams::kern::board::nintendo::nx {
/* Check if there's nothing mapped at l1. */ /* Check if there's nothing mapped at l1. */
if (l1 == nullptr || !l1[l1_index].IsValid()) { if (l1 == nullptr || !l1[l1_index].IsValid()) {
MESOSPHERE_ASSERT(force);
const size_t remaining_in_entry = (PageTableSize / sizeof(PageTableEntry)) - l2_index; const size_t remaining_in_entry = (PageTableSize / sizeof(PageTableEntry)) - l2_index;
const size_t map_count = std::min<size_t>(remaining_in_entry, remaining / DevicePageSize); const size_t map_count = std::min<size_t>(remaining_in_entry, remaining / DevicePageSize);
@@ -1201,30 +1211,12 @@ namespace ams::kern::board::nintendo::nx {
const size_t remaining_in_entry = (PageTableSize / sizeof(PageTableEntry)) - l2_index; const size_t remaining_in_entry = (PageTableSize / sizeof(PageTableEntry)) - l2_index;
const size_t map_count = std::min<size_t>(remaining_in_entry, remaining / DevicePageSize); const size_t map_count = std::min<size_t>(remaining_in_entry, remaining / DevicePageSize);
size_t num_closed = 0; size_t num_closed = 0;
bool invalidated_tlb = false;
/* Invalidate the attributes of all entries. */
for (size_t i = 0; i < map_count; ++i) { for (size_t i = 0; i < map_count; ++i) {
if (l2[l2_index + i].IsValid()) { if (l2[l2_index + i].IsValid()) {
/* Get the physical address. */ l2[l2_index + i].InvalidateAttributes();
const KPhysicalAddress phys_addr = l2[l2_index + i].GetPhysicalAddress();
MESOSPHERE_ASSERT(IsHeapPhysicalAddress(phys_addr));
/* Invalidate the entry. */
l2[l2_index + i].Invalidate();
++num_closed; ++num_closed;
/* Try to add the page to the group. */
if (R_FAILED(pg.AddBlock(GetHeapVirtualAddress(phys_addr), DevicePageSize / PageSize))) {
/* If we can't add it for deferred close, close it now. */
cpu::StoreDataCache(std::addressof(l2[l2_index + i]), sizeof(PageTableEntry));
InvalidatePtc(GetPageTablePhysicalAddress(KVirtualAddress(std::addressof(l2[l2_index + i]))));
SmmuSynchronizationBarrier();
/* Close the page's reference. */
mm.Close(GetHeapVirtualAddress(phys_addr), 1);
}
} else {
MESOSPHERE_ASSERT(force);
} }
} }
cpu::StoreDataCache(std::addressof(l2[l2_index]), map_count * sizeof(PageTableEntry)); cpu::StoreDataCache(std::addressof(l2[l2_index]), map_count * sizeof(PageTableEntry));
@@ -1235,6 +1227,38 @@ namespace ams::kern::board::nintendo::nx {
} }
SmmuSynchronizationBarrier(); SmmuSynchronizationBarrier();
/* Close the memory manager's references to the pages. */
{
KPhysicalAddress contig_phys_addr = Null<KPhysicalAddress>;
size_t contig_count = 0;
for (size_t i = 0; i < map_count; ++i) {
/* Get the physical address. */
const KPhysicalAddress phys_addr = l2[l2_index + i].GetPhysicalAddress();
MESOSPHERE_ASSERT(IsHeapPhysicalAddress(phys_addr));
/* Fully invalidate the entry. */
l2[l2_index + i].Invalidate();
if (contig_count == 0) {
/* Ensure that our address/count is valid. */
contig_phys_addr = phys_addr;
contig_count = contig_phys_addr != Null<KPhysicalAddress> ? 1 : 0;
} else if (phys_addr == Null<KPhysicalAddress> || phys_addr != (contig_phys_addr + (contig_count * DevicePageSize))) {
/* If we're no longer contiguous, close the range we've been building. */
mm.Close(GetHeapVirtualAddress(contig_phys_addr), (contig_count * DevicePageSize) / PageSize);
contig_phys_addr = phys_addr;
contig_count = contig_phys_addr != Null<KPhysicalAddress> ? 1 : 0;
} else {
++contig_count;
}
}
if (contig_count > 0) {
mm.Close(GetHeapVirtualAddress(contig_phys_addr), (contig_count * DevicePageSize) / PageSize);
}
}
/* Close the pages. */ /* Close the pages. */
if (ptm.Close(KVirtualAddress(l2), num_closed)) { if (ptm.Close(KVirtualAddress(l2), num_closed)) {
/* Invalidate the l1 entry. */ /* Invalidate the l1 entry. */
@@ -1243,22 +1267,12 @@ namespace ams::kern::board::nintendo::nx {
/* Synchronize. */ /* Synchronize. */
InvalidatePtc(GetPageTablePhysicalAddress(KVirtualAddress(std::addressof(l1[l1_index])))); InvalidatePtc(GetPageTablePhysicalAddress(KVirtualAddress(std::addressof(l1[l1_index]))));
InvalidateTlbSection(m_table_asids[l0_index], address);
SmmuSynchronizationBarrier(); SmmuSynchronizationBarrier();
/* We invalidated the tlb. */
invalidated_tlb = true;
/* Free the l2 page. */ /* Free the l2 page. */
ptm.Free(KVirtualAddress(l2)); ptm.Free(KVirtualAddress(l2));
} }
/* Invalidate the tlb if we haven't already. */
if (!invalidated_tlb) {
InvalidateTlbSection(m_table_asids[l0_index], address);
SmmuSynchronizationBarrier();
}
/* Advance. */ /* Advance. */
address += map_count * DevicePageSize; address += map_count * DevicePageSize;
remaining -= map_count * DevicePageSize; remaining -= map_count * DevicePageSize;
@@ -1287,114 +1301,158 @@ namespace ams::kern::board::nintendo::nx {
remaining -= DeviceLargePageSize; remaining -= DeviceLargePageSize;
} }
} }
/* Close references to the pages in the group. */
pg.Close();
} }
Result KDevicePageTable::MakePageGroup(KPageGroup *out, KDeviceVirtualAddress address, u64 size) const { bool KDevicePageTable::Compare(KProcessPageTable *page_table, KProcessAddress process_address, size_t size, KDeviceVirtualAddress device_address) const {
MESOSPHERE_ASSERT((address & ~DeviceVirtualAddressMask) == 0); MESOSPHERE_ASSERT((device_address & ~DeviceVirtualAddressMask) == 0);
MESOSPHERE_ASSERT(((address + size - 1) & ~DeviceVirtualAddressMask) == 0); MESOSPHERE_ASSERT(((device_address + size - 1) & ~DeviceVirtualAddressMask) == 0);
/* We need to traverse the ranges that make up our mapping, to make sure they're all good. Start by getting a contiguous range. */
KPageTableBase::MemoryRange contig_range = {};
if (R_FAILED(page_table->OpenMemoryRangeForUnmapDeviceAddressSpace(std::addressof(contig_range), process_address, size))) {
return false;
}
/* Ensure that we close the range when we're done. */
bool range_open = true;
ON_SCOPE_EXIT { if (range_open) { contig_range.Close(); } };
/* Walk the directory. */ /* Walk the directory. */
u64 remaining = size; KProcessAddress cur_process_address = process_address;
bool first = true; size_t remaining_size = size;
u32 attr = 0; KPhysicalAddress cur_phys_address = GetHeapPhysicalAddress(contig_range.address);
while (remaining > 0) { size_t remaining_in_range = contig_range.size;
const size_t l0_index = (address / DeviceRegionSize); bool first = true;
const size_t l1_index = (address % DeviceRegionSize) / DeviceLargePageSize; u32 first_attr = 0;
const size_t l2_index = (address % DeviceLargePageSize) / DevicePageSize; while (remaining_size > 0) {
/* Convert the device address to a series of indices. */
const size_t l0_index = (device_address / DeviceRegionSize);
const size_t l1_index = (device_address % DeviceRegionSize) / DeviceLargePageSize;
const size_t l2_index = (device_address % DeviceLargePageSize) / DevicePageSize;
/* Get and validate l1. */ /* Get and validate l1. */
const PageDirectoryEntry *l1 = GetPointer<PageDirectoryEntry>(m_tables[l0_index]); const PageDirectoryEntry *l1 = GetPointer<PageDirectoryEntry>(m_tables[l0_index]);
R_UNLESS(l1 != nullptr, svc::ResultInvalidCurrentMemory()); if (!(l1 != nullptr && l1[l1_index].IsValid())) {
R_UNLESS(l1[l1_index].IsValid(), svc::ResultInvalidCurrentMemory()); return false;
}
if (l1[l1_index].IsTable()) { if (l1[l1_index].IsTable()) {
/* We're acting on an l2 entry. */ /* We're acting on an l2 entry. */
const PageTableEntry *l2 = GetPointer<PageTableEntry>(GetPageTableVirtualAddress(l1[l1_index].GetPhysicalAddress())); const PageTableEntry *l2 = GetPointer<PageTableEntry>(GetPageTableVirtualAddress(l1[l1_index].GetPhysicalAddress()));
/* Determine the number of pages to check. */
const size_t remaining_in_entry = (PageTableSize / sizeof(PageTableEntry)) - l2_index; const size_t remaining_in_entry = (PageTableSize / sizeof(PageTableEntry)) - l2_index;
const size_t map_count = std::min<size_t>(remaining_in_entry, remaining / DevicePageSize); const size_t map_count = std::min<size_t>(remaining_in_entry, remaining_size / DevicePageSize);
/* Check each page. */
for (size_t i = 0; i < map_count; ++i) { for (size_t i = 0; i < map_count; ++i) {
/* Ensure the l2 entry is valid. */ /* Ensure the l2 entry is valid. */
R_UNLESS(l2[l2_index + i].IsValid(), svc::ResultInvalidCurrentMemory()); if (!l2[l2_index + i].IsValid()) {
return false;
/* Get the physical address. */
const KPhysicalAddress phys_addr = l2[l2_index + i].GetPhysicalAddress();
MESOSPHERE_ASSERT(IsHeapPhysicalAddress(phys_addr));
/* Add to the group. */
R_TRY(out->AddBlock(GetHeapVirtualAddress(phys_addr), DevicePageSize / PageSize));
/* If this is our first entry, get the attribute. */
if (first) {
attr = l2[l2_index + i].GetAttributes();
first = false;
} else {
/* Validate the attributes match the first entry. */
R_UNLESS(l2[l2_index + i].GetAttributes() == attr, svc::ResultInvalidCurrentMemory());
} }
/* Check that the attributes match the first attributes we encountered. */
const u32 cur_attr = l2[l2_index + i].GetAttributes();
if (!first && cur_attr != first_attr) {
return false;
}
/* If there's nothing remaining in the range, refresh the range. */
if (remaining_in_range == 0) {
contig_range.Close();
range_open = false;
if (R_FAILED(page_table->OpenMemoryRangeForUnmapDeviceAddressSpace(std::addressof(contig_range), cur_process_address, remaining_size))) {
return false;
}
range_open = true;
cur_phys_address = GetHeapPhysicalAddress(contig_range.address);
remaining_in_range = contig_range.size;
}
/* Check that the physical address is expected. */
if (l2[l2_index + i].GetPhysicalAddress() != cur_phys_address) {
return false;
}
/* Advance. */
cur_phys_address += DevicePageSize;
cur_process_address += DevicePageSize;
remaining_size -= DevicePageSize;
remaining_in_range -= DevicePageSize;
first = false;
first_attr = cur_attr;
} }
/* Advance. */ /* Advance the device address. */
address += DevicePageSize * map_count; device_address += map_count * DevicePageSize;
remaining -= DevicePageSize * map_count;
} else { } else {
/* We're acting on an l1 entry. */ /* We're acting on an l1 entry. */
R_UNLESS(l2_index == 0, svc::ResultInvalidCurrentMemory()); if (!(l2_index == 0 && remaining_size >= DeviceLargePageSize)) {
R_UNLESS(remaining >= DeviceLargePageSize, svc::ResultInvalidCurrentMemory()); return false;
}
/* Get the physical address. */ /* Check that the attributes match the first attributes we encountered. */
const KPhysicalAddress phys_addr = l1[l1_index].GetPhysicalAddress(); const u32 cur_attr = l1[l1_index].GetAttributes();
MESOSPHERE_ASSERT(IsHeapPhysicalAddress(phys_addr)); if (!first && cur_attr != first_attr) {
return false;
}
/* Add to the group. */ /* If there's nothing remaining in the range, refresh the range. */
R_TRY(out->AddBlock(GetHeapVirtualAddress(phys_addr), DeviceLargePageSize / PageSize)); if (remaining_in_range == 0) {
contig_range.Close();
/* If this is our first entry, get the attribute. */ range_open = false;
if (first) { if (R_FAILED(page_table->OpenMemoryRangeForUnmapDeviceAddressSpace(std::addressof(contig_range), cur_process_address, remaining_size))) {
attr = l1[l1_index].GetAttributes(); return false;
first = false; }
} else { range_open = true;
/* Validate the attributes match the first entry. */
R_UNLESS(l1[l1_index].GetAttributes() == attr, svc::ResultInvalidCurrentMemory()); cur_phys_address = GetHeapPhysicalAddress(contig_range.address);
remaining_in_range = contig_range.size;
}
/* Check that the physical address is expected, and there's enough in the range. */
if (remaining_in_range < DeviceLargePageSize || l1[l1_index].GetPhysicalAddress() != cur_phys_address) {
return false;
} }
/* Advance. */ /* Advance. */
address += DeviceLargePageSize; cur_phys_address += DeviceLargePageSize;
remaining -= DeviceLargePageSize; cur_process_address += DeviceLargePageSize;
remaining_size -= DeviceLargePageSize;
remaining_in_range -= DeviceLargePageSize;
first = false;
first_attr = cur_attr;
/* Advance the device address. */
device_address += DeviceLargePageSize;
} }
} }
return ResultSuccess(); /* The range is valid! */
return true;
} }
bool KDevicePageTable::Compare(const KPageGroup &compare_pg, KDeviceVirtualAddress device_address) const { Result KDevicePageTable::Map(size_t *out_mapped_size, KProcessPageTable *page_table, KProcessAddress process_address, size_t size, KDeviceVirtualAddress device_address, ams::svc::MemoryPermission device_perm, bool refresh_mappings) {
/* Check whether the page group we expect for the virtual address matches the page group we're validating. */
KPageGroup calc_pg(std::addressof(Kernel::GetBlockInfoManager()));
return (R_SUCCEEDED(this->MakePageGroup(std::addressof(calc_pg), device_address, compare_pg.GetNumPages() * PageSize))) &&
calc_pg.IsEquivalentTo(compare_pg);
}
Result KDevicePageTable::Map(size_t *out_mapped_size, const KPageGroup &pg, KDeviceVirtualAddress device_address, ams::svc::MemoryPermission device_perm, bool refresh_mappings) {
/* Clear the output size. */ /* Clear the output size. */
*out_mapped_size = 0; *out_mapped_size = 0;
/* Map the pages. */ /* Map the pages. */
s32 num_pt = 0; s32 num_pt = 0;
return this->MapImpl(out_mapped_size, num_pt, refresh_mappings ? 1 : std::numeric_limits<s32>::max(), pg, device_address, device_perm); return this->MapImpl(out_mapped_size, num_pt, refresh_mappings ? 1 : std::numeric_limits<s32>::max(), page_table, process_address, size, device_address, device_perm, refresh_mappings);
} }
Result KDevicePageTable::Unmap(const KPageGroup &pg, KDeviceVirtualAddress device_address) { Result KDevicePageTable::Unmap(KProcessPageTable *page_table, KProcessAddress process_address, size_t size, KDeviceVirtualAddress device_address) {
/* Validate address/size. */ /* Validate address/size. */
const size_t size = pg.GetNumPages() * PageSize;
MESOSPHERE_ASSERT((device_address & ~DeviceVirtualAddressMask) == 0); MESOSPHERE_ASSERT((device_address & ~DeviceVirtualAddressMask) == 0);
MESOSPHERE_ASSERT(((device_address + size - 1) & ~DeviceVirtualAddressMask) == 0); MESOSPHERE_ASSERT(((device_address + size - 1) & ~DeviceVirtualAddressMask) == 0);
/* Ensure the page group is correct. */ /* Ensure the page group is correct. */
R_UNLESS(this->Compare(pg, device_address), svc::ResultInvalidCurrentMemory()); R_UNLESS(this->Compare(page_table, process_address, size, device_address), svc::ResultInvalidCurrentMemory());
/* Unmap the pages. */ /* Unmap the pages. */
this->UnmapImpl(device_address, size, false); this->UnmapImpl(device_address, size, false);

View File

@@ -492,7 +492,7 @@ namespace ams::kern::board::nintendo::nx {
/* Wait for a request. */ /* Wait for a request. */
{ {
KScopedLightLock lk(g_cv_lock); KScopedLightLock lk(g_cv_lock);
while (!(g_sleep_target_cores & target_core_mask)) { while ((g_sleep_target_cores & target_core_mask) == 0) {
g_cv.Wait(std::addressof(g_cv_lock)); g_cv.Wait(std::addressof(g_cv_lock));
} }
} }

View File

@@ -95,9 +95,10 @@ _ZN3ams4kern5board8nintendo2nx13KSleepManager15CpuSleepHandlerEmm:
mrs x2, tpidr_el1 mrs x2, tpidr_el1
stp x1, x2, [x0], #0x10 stp x1, x2, [x0], #0x10
/* Save the virtual resumption entrypoint. */ /* Save the virtual resumption entrypoint and cntv_cval_el0. */
adr x1, 77f adr x1, 77f
stp x1, xzr, [x0], #0x10 mrs x2, cntv_cval_el0
stp x1, x2, [x0], #0x10
/* Get the current core id. */ /* Get the current core id. */
mrs x0, mpidr_el1 mrs x0, mpidr_el1
@@ -245,12 +246,13 @@ _ZN3ams4kern5board8nintendo2nx13KSleepManager11ResumeEntryEm:
msr tcr_el1, x1 msr tcr_el1, x1
msr mair_el1, x2 msr mair_el1, x2
/* Get sctlr, tpidr, and the entrypoint. */ /* Get sctlr, tpidr, the entrypoint, and cntv_cval_el0. */
ldp x1, x2, [x0], #0x10 ldp x1, x2, [x0], #0x10
ldp x3, xzr, [x0], #0x10 ldp x3, x4, [x0], #0x10
/* Set the global context back into x18/tpidr. */ /* Set the global context back into x18/tpidr. */
msr tpidr_el1, x2 msr tpidr_el1, x2
msr cntv_cval_el0, x4
dsb sy dsb sy
isb isb

View File

@@ -21,7 +21,8 @@ namespace ams::kern::board::nintendo::nx {
namespace { namespace {
constexpr size_t SecureAlignment = 128_KB; constexpr uintptr_t DramPhysicalAddress = 0x80000000;
constexpr size_t SecureAlignment = 128_KB;
/* Global variables for panic. */ /* Global variables for panic. */
constinit bool g_call_smc_on_panic; constinit bool g_call_smc_on_panic;
@@ -41,8 +42,8 @@ namespace ams::kern::board::nintendo::nx {
/* Nintendo uses std::mt19937_t for randomness. */ /* Nintendo uses std::mt19937_t for randomness. */
/* To save space (and because mt19337_t isn't secure anyway), */ /* To save space (and because mt19337_t isn't secure anyway), */
/* We will use TinyMT. */ /* We will use TinyMT. */
bool g_initialized_random_generator; constinit bool g_initialized_random_generator;
util::TinyMT g_random_generator; constinit util::TinyMT g_random_generator;
constinit KSpinLock g_random_lock; constinit KSpinLock g_random_lock;
ALWAYS_INLINE size_t GetRealMemorySizeForInit() { ALWAYS_INLINE size_t GetRealMemorySizeForInit() {
@@ -89,13 +90,10 @@ namespace ams::kern::board::nintendo::nx {
return value; return value;
} }
void EnsureRandomGeneratorInitialized() { ALWAYS_INLINE u64 GenerateRandomU64FromSmc() {
if (AMS_UNLIKELY(!g_initialized_random_generator)) { u64 value;
u64 seed; smc::GenerateRandomBytes(std::addressof(value), sizeof(value));
smc::GenerateRandomBytes(&seed, sizeof(seed)); return value;
g_random_generator.Initialize(reinterpret_cast<u32*>(&seed), sizeof(seed) / sizeof(u32));
g_initialized_random_generator = true;
}
} }
ALWAYS_INLINE u64 GenerateRandomU64FromGenerator() { ALWAYS_INLINE u64 GenerateRandomU64FromGenerator() {
@@ -348,6 +346,10 @@ namespace ams::kern::board::nintendo::nx {
} }
} }
KPhysicalAddress KSystemControl::Init::GetInitialProcessBinaryPhysicalAddress() {
return GetKernelPhysicalBaseAddress(DramPhysicalAddress) + GetIntendedMemorySize() - KTraceBufferSize - InitialProcessBinarySizeMax;
}
bool KSystemControl::Init::ShouldIncreaseThreadResourceLimit() { bool KSystemControl::Init::ShouldIncreaseThreadResourceLimit() {
return GetKernelConfigurationForInit().Get<smc::KernelConfiguration::IncreaseThreadResourceLimit>(); return GetKernelConfigurationForInit().Get<smc::KernelConfiguration::IncreaseThreadResourceLimit>();
} }
@@ -368,7 +370,7 @@ namespace ams::kern::board::nintendo::nx {
case smc::MemoryArrangement_6GBForAppletDev: case smc::MemoryArrangement_6GBForAppletDev:
return 3285_MB; return 3285_MB;
case smc::MemoryArrangement_8GB: case smc::MemoryArrangement_8GB:
return 4916_MB; return 6964_MB;
} }
}(); }();
@@ -392,12 +394,12 @@ namespace ams::kern::board::nintendo::nx {
case smc::MemoryArrangement_6GBForAppletDev: case smc::MemoryArrangement_6GBForAppletDev:
return 2193_MB; return 2193_MB;
case smc::MemoryArrangement_8GB: case smc::MemoryArrangement_8GB:
return 2193_MB; return 562_MB;
} }
}(); }();
/* Return (possibly) adjusted size. */ /* Return (possibly) adjusted size. */
constexpr size_t ExtraSystemMemoryForAtmosphere = 33_MB; constexpr size_t ExtraSystemMemoryForAtmosphere = 40_MB;
return base_pool_size - ExtraSystemMemoryForAtmosphere - KTraceBufferSize; return base_pool_size - ExtraSystemMemoryForAtmosphere - KTraceBufferSize;
} }
@@ -434,6 +436,14 @@ namespace ams::kern::board::nintendo::nx {
/* System Initialization. */ /* System Initialization. */
void KSystemControl::InitializePhase1() { void KSystemControl::InitializePhase1() {
/* Initialize our random generator. */
{
u64 seed;
smc::GenerateRandomBytes(std::addressof(seed), sizeof(seed));
g_random_generator.Initialize(reinterpret_cast<u32*>(std::addressof(seed)), sizeof(seed) / sizeof(u32));
g_initialized_random_generator = true;
}
/* Set IsDebugMode. */ /* Set IsDebugMode. */
{ {
KTargetSystem::SetIsDebugMode(GetConfigBool(smc::ConfigItem::IsDebugMode)); KTargetSystem::SetIsDebugMode(GetConfigBool(smc::ConfigItem::IsDebugMode));
@@ -539,18 +549,23 @@ namespace ams::kern::board::nintendo::nx {
KScopedInterruptDisable intr_disable; KScopedInterruptDisable intr_disable;
KScopedSpinLock lk(g_random_lock); KScopedSpinLock lk(g_random_lock);
EnsureRandomGeneratorInitialized();
return GenerateUniformRange(min, max, GenerateRandomU64FromGenerator); if (AMS_LIKELY(g_initialized_random_generator)) {
return GenerateUniformRange(min, max, GenerateRandomU64FromGenerator);
} else {
return GenerateUniformRange(min, max, GenerateRandomU64FromSmc);
}
} }
u64 KSystemControl::GenerateRandomU64() { u64 KSystemControl::GenerateRandomU64() {
KScopedInterruptDisable intr_disable; KScopedInterruptDisable intr_disable;
KScopedSpinLock lk(g_random_lock); KScopedSpinLock lk(g_random_lock);
EnsureRandomGeneratorInitialized(); if (AMS_LIKELY(g_initialized_random_generator)) {
return GenerateRandomU64FromGenerator();
return GenerateRandomU64FromGenerator(); } else {
return GenerateRandomU64FromSmc();
}
} }
void KSystemControl::SleepSystem() { void KSystemControl::SleepSystem() {

View File

@@ -22,7 +22,6 @@ namespace ams::kern::init {
#define FOREACH_SLAB_TYPE(HANDLER, ...) \ #define FOREACH_SLAB_TYPE(HANDLER, ...) \
HANDLER(KProcess, (SLAB_COUNT(KProcess)), ## __VA_ARGS__) \ HANDLER(KProcess, (SLAB_COUNT(KProcess)), ## __VA_ARGS__) \
HANDLER(KThread, (SLAB_COUNT(KThread)), ## __VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ## __VA_ARGS__) \
HANDLER(KLinkedListNode, (SLAB_COUNT(KThread)), ## __VA_ARGS__) \
HANDLER(KEvent, (SLAB_COUNT(KEvent)), ## __VA_ARGS__) \ HANDLER(KEvent, (SLAB_COUNT(KEvent)), ## __VA_ARGS__) \
HANDLER(KInterruptEvent, (SLAB_COUNT(KInterruptEvent)), ## __VA_ARGS__) \ HANDLER(KInterruptEvent, (SLAB_COUNT(KInterruptEvent)), ## __VA_ARGS__) \
HANDLER(KInterruptEventTask, (SLAB_COUNT(KInterruptEvent)), ## __VA_ARGS__) \ HANDLER(KInterruptEventTask, (SLAB_COUNT(KInterruptEvent)), ## __VA_ARGS__) \
@@ -58,14 +57,14 @@ namespace ams::kern::init {
/* Constexpr counts. */ /* Constexpr counts. */
constexpr size_t SlabCountKProcess = 80; constexpr size_t SlabCountKProcess = 80;
constexpr size_t SlabCountKThread = 800; constexpr size_t SlabCountKThread = 800;
constexpr size_t SlabCountKEvent = 700; constexpr size_t SlabCountKEvent = 900;
constexpr size_t SlabCountKInterruptEvent = 100; constexpr size_t SlabCountKInterruptEvent = 100;
constexpr size_t SlabCountKPort = 256; constexpr size_t SlabCountKPort = 256 + 0x20 /* Extra 0x20 ports over Nintendo for homebrew. */;
constexpr size_t SlabCountKSharedMemory = 80; constexpr size_t SlabCountKSharedMemory = 80;
constexpr size_t SlabCountKTransferMemory = 200; constexpr size_t SlabCountKTransferMemory = 200;
constexpr size_t SlabCountKCodeMemory = 10; constexpr size_t SlabCountKCodeMemory = 10;
constexpr size_t SlabCountKDeviceAddressSpace = 300; constexpr size_t SlabCountKDeviceAddressSpace = 300;
constexpr size_t SlabCountKSession = 933; constexpr size_t SlabCountKSession = 1133;
constexpr size_t SlabCountKLightSession = 100; constexpr size_t SlabCountKLightSession = 100;
constexpr size_t SlabCountKObjectName = 7; constexpr size_t SlabCountKObjectName = 7;
constexpr size_t SlabCountKResourceLimit = 5; constexpr size_t SlabCountKResourceLimit = 5;
@@ -77,13 +76,13 @@ namespace ams::kern::init {
namespace test { namespace test {
constexpr size_t RequiredSizeForExtraThreadCount = SlabCountExtraKThread * (sizeof(KThread) + sizeof(KLinkedListNode) + (sizeof(KThreadLocalPage) / 8) + sizeof(KEventInfo)); constexpr size_t RequiredSizeForExtraThreadCount = SlabCountExtraKThread * (sizeof(KThread) + (sizeof(KThreadLocalPage) / 8) + sizeof(KEventInfo));
static_assert(RequiredSizeForExtraThreadCount <= KernelSlabHeapAdditionalSize); static_assert(RequiredSizeForExtraThreadCount <= KernelSlabHeapAdditionalSize);
} }
/* Global to hold our resource counts. */ /* Global to hold our resource counts. */
KSlabResourceCounts g_slab_resource_counts = { constinit KSlabResourceCounts g_slab_resource_counts = {
.num_KProcess = SlabCountKProcess, .num_KProcess = SlabCountKProcess,
.num_KThread = SlabCountKThread, .num_KThread = SlabCountKThread,
.num_KEvent = SlabCountKEvent, .num_KEvent = SlabCountKEvent,
@@ -132,7 +131,9 @@ namespace ams::kern::init {
} }
size_t CalculateSlabHeapGapSize() { size_t CalculateSlabHeapGapSize() {
return (kern::GetTargetFirmware() >= TargetFirmware_10_0_0) ? KernelSlabHeapGapsSize : KernelSlabHeapGapsSizeDeprecated; constexpr size_t KernelSlabHeapGapSize = 2_MB - 296_KB;
static_assert(KernelSlabHeapGapSize <= KernelSlabHeapGapsSizeMax);
return KernelSlabHeapGapSize;
} }
size_t CalculateTotalSlabHeapSize() { size_t CalculateTotalSlabHeapSize() {

View File

@@ -25,101 +25,219 @@ namespace ams::kern {
s32 priority; s32 priority;
}; };
KVirtualAddress GetInitialProcessBinaryAddress() { constinit KVirtualAddress g_initial_process_binary_address = Null<KVirtualAddress>;
const uintptr_t end_address = KMemoryLayout::GetPageTableHeapRegion().GetEndAddress(); constinit InitialProcessBinaryHeader g_initial_process_binary_header = {};
MESOSPHERE_ABORT_UNLESS(end_address != 0); constinit size_t g_initial_process_secure_memory_size = 0;
return end_address - InitialProcessBinarySizeMax; constinit u64 g_initial_process_id_min = std::numeric_limits<u64>::max();
} constinit u64 g_initial_process_id_max = std::numeric_limits<u64>::min();
void LoadInitialProcessBinaryHeader(InitialProcessBinaryHeader *header) { void LoadInitialProcessBinaryHeader() {
if (header->magic != InitialProcessBinaryMagic) { if (g_initial_process_binary_header.magic != InitialProcessBinaryMagic) {
*header = *GetPointer<InitialProcessBinaryHeader>(GetInitialProcessBinaryAddress()); /* Get the virtual address for the image. */
} const KVirtualAddress virt_addr = GetInitialProcessBinaryAddress();
MESOSPHERE_ABORT_UNLESS(header->magic == InitialProcessBinaryMagic); /* Copy and validate the header. */
MESOSPHERE_ABORT_UNLESS(header->num_processes <= init::GetSlabResourceCounts().num_KProcess); g_initial_process_binary_header = *GetPointer<InitialProcessBinaryHeader>(virt_addr);
} MESOSPHERE_ABORT_UNLESS(g_initial_process_binary_header.magic == InitialProcessBinaryMagic);
MESOSPHERE_ABORT_UNLESS(g_initial_process_binary_header.num_processes <= init::GetSlabResourceCounts().num_KProcess);
size_t GetProcessesSecureMemorySize(KVirtualAddress binary_address, const InitialProcessBinaryHeader &header) { /* Set the image address. */
u8 *current = GetPointer<u8>(binary_address + sizeof(InitialProcessBinaryHeader)); g_initial_process_binary_address = virt_addr;
const u8 * const end = GetPointer<u8>(binary_address + header.size - sizeof(KInitialProcessHeader));
size_t size = 0; /* Process/calculate the secure memory size. */
const size_t num_processes = header.num_processes; KVirtualAddress current = g_initial_process_binary_address + sizeof(InitialProcessBinaryHeader);
for (size_t i = 0; i < num_processes; i++) { const KVirtualAddress end = g_initial_process_binary_address + g_initial_process_binary_header.size;
/* Validate that we can read the current KIP. */ const size_t num_processes = g_initial_process_binary_header.num_processes;
MESOSPHERE_ABORT_UNLESS(current <= end); for (size_t i = 0; i < num_processes; ++i) {
KInitialProcessReader reader; /* Validate that we can read the current KIP. */
MESOSPHERE_ABORT_UNLESS(reader.Attach(current)); MESOSPHERE_ABORT_UNLESS(current <= end - sizeof(KInitialProcessHeader));
/* If the process uses secure memory, account for that. */ /* Attach to the current KIP. */
if (reader.UsesSecureMemory()) { KInitialProcessReader reader;
size += util::AlignUp(reader.GetSize(), PageSize); MESOSPHERE_ABORT_UNLESS(reader.Attach(current) != Null<KVirtualAddress>);
/* If the process uses secure memory, account for that. */
if (reader.UsesSecureMemory()) {
g_initial_process_secure_memory_size += reader.GetSize() + util::AlignUp(reader.GetStackSize(), PageSize);
}
} }
/* Advance the reader. */
current += reader.GetBinarySize();
} }
return size;
} }
void CreateProcesses(InitialProcessInfo *infos, KVirtualAddress binary_address, const InitialProcessBinaryHeader &header) { void CreateProcesses(InitialProcessInfo *infos) {
u8 *current = GetPointer<u8>(binary_address + sizeof(InitialProcessBinaryHeader)); /* Determine process image extents. */
const u8 * const end = GetPointer<u8>(binary_address + header.size - sizeof(KInitialProcessHeader)); KVirtualAddress current = g_initial_process_binary_address + sizeof(InitialProcessBinaryHeader);
KVirtualAddress end = g_initial_process_binary_address + g_initial_process_binary_header.size;
/* Decide on pools to use. */ /* Decide on pools to use. */
const auto unsafe_pool = static_cast<KMemoryManager::Pool>(KSystemControl::GetCreateProcessMemoryPool()); const auto unsafe_pool = static_cast<KMemoryManager::Pool>(KSystemControl::GetCreateProcessMemoryPool());
const auto secure_pool = (GetTargetFirmware() >= TargetFirmware_2_0_0) ? KMemoryManager::Pool_Secure : unsafe_pool; const auto secure_pool = (GetTargetFirmware() >= TargetFirmware_2_0_0) ? KMemoryManager::Pool_Secure : unsafe_pool;
const size_t num_processes = header.num_processes; const size_t num_processes = g_initial_process_binary_header.num_processes;
for (size_t i = 0; i < num_processes; i++) { for (size_t i = 0; i < num_processes; ++i) {
/* Validate that we can read the current KIP. */ /* Validate that we can read the current KIP header. */
MESOSPHERE_ABORT_UNLESS(current <= end); MESOSPHERE_ABORT_UNLESS(current <= end - sizeof(KInitialProcessHeader));
KInitialProcessReader reader;
MESOSPHERE_ABORT_UNLESS(reader.Attach(current));
/* Parse process parameters and reserve memory. */ /* Attach to the current kip. */
KInitialProcessReader reader;
KVirtualAddress data = reader.Attach(current);
MESOSPHERE_ABORT_UNLESS(data != Null<KVirtualAddress>);
/* Ensure that the remainder of our parse is page aligned. */
if (!util::IsAligned(GetInteger(data), PageSize)) {
const KVirtualAddress aligned_data = util::AlignDown(GetInteger(data), PageSize);
std::memmove(GetVoidPointer(aligned_data), GetVoidPointer(data), end - data);
data = aligned_data;
end -= (data - aligned_data);
}
/* If we crossed a page boundary, free the pages we're done using. */
if (KVirtualAddress aligned_current = util::AlignDown(GetInteger(current), PageSize); aligned_current != data) {
const size_t freed_size = data - aligned_current;
Kernel::GetMemoryManager().Close(aligned_current, freed_size / PageSize);
Kernel::GetSystemResourceLimit().Release(ams::svc::LimitableResource_PhysicalMemoryMax, freed_size);
}
/* Parse process parameters. */
ams::svc::CreateProcessParameter params; ams::svc::CreateProcessParameter params;
MESOSPHERE_R_ABORT_UNLESS(reader.MakeCreateProcessParameter(std::addressof(params), true)); MESOSPHERE_R_ABORT_UNLESS(reader.MakeCreateProcessParameter(std::addressof(params), true));
MESOSPHERE_ABORT_UNLESS(Kernel::GetSystemResourceLimit().Reserve(ams::svc::LimitableResource_PhysicalMemoryMax, params.code_num_pages * PageSize));
/* Get the binary size for the kip. */
const size_t binary_size = reader.GetBinarySize();
const size_t binary_pages = binary_size / PageSize;
/* Get the pool for both the current (compressed) image, and the decompressed process. */
const auto src_pool = Kernel::GetMemoryManager().GetPool(data);
const auto dst_pool = reader.UsesSecureMemory() ? secure_pool : unsafe_pool;
/* Determine the process size, and how much memory isn't already reserved. */
const size_t process_size = params.code_num_pages * PageSize;
const size_t unreserved_size = process_size - (src_pool == dst_pool ? util::AlignDown(binary_size, PageSize) : 0);
/* Reserve however much memory we need to reserve. */
MESOSPHERE_ABORT_UNLESS(Kernel::GetSystemResourceLimit().Reserve(ams::svc::LimitableResource_PhysicalMemoryMax, unreserved_size));
/* Create the process. */ /* Create the process. */
KProcess *new_process = nullptr; KProcess *new_process = nullptr;
{ {
/* Declare page group to use for process memory. */ /* Make page groups to represent the data. */
KPageGroup pg(std::addressof(Kernel::GetBlockInfoManager())); KPageGroup pg(std::addressof(Kernel::GetBlockInfoManager()));
KPageGroup workaround_pg(std::addressof(Kernel::GetBlockInfoManager()));
/* Allocate memory for the process. */ /* Populate the page group to represent the data. */
auto &mm = Kernel::GetMemoryManager();
const auto pool = reader.UsesSecureMemory() ? secure_pool : unsafe_pool;
MESOSPHERE_R_ABORT_UNLESS(mm.AllocateAndOpen(std::addressof(pg), params.code_num_pages, KMemoryManager::EncodeOption(pool, KMemoryManager::Direction_FromFront)));
{ {
/* Ensure that we do not leak pages. */ /* Allocate the previously unreserved pages. */
ON_SCOPE_EXIT { pg.Close(); }; KPageGroup unreserve_pg(std::addressof(Kernel::GetBlockInfoManager()));
MESOSPHERE_R_ABORT_UNLESS(Kernel::GetMemoryManager().AllocateAndOpen(std::addressof(unreserve_pg), unreserved_size / PageSize, KMemoryManager::EncodeOption(dst_pool, KMemoryManager::Direction_FromFront)));
/* Get the temporary region. */ /* Add the previously reserved pages. */
const auto &temp_region = KMemoryLayout::GetTempRegion(); if (src_pool == dst_pool && binary_pages != 0) {
MESOSPHERE_ABORT_UNLESS(temp_region.GetEndAddress() != 0); /* NOTE: Nintendo does not check the result of this operation. */
pg.AddBlock(data, binary_pages);
}
/* Map the process's memory into the temporary region. */ /* Add the previously unreserved pages. */
KProcessAddress temp_address = Null<KProcessAddress>; for (const auto &block : unreserve_pg) {
MESOSPHERE_R_ABORT_UNLESS(Kernel::GetKernelPageTable().MapPageGroup(std::addressof(temp_address), pg, temp_region.GetAddress(), temp_region.GetSize() / PageSize, KMemoryState_Kernel, KMemoryPermission_KernelReadWrite)); /* NOTE: Nintendo does not check the result of this operation. */
pg.AddBlock(block.GetAddress(), block.GetNumPages());
/* Load the process. */ }
MESOSPHERE_R_ABORT_UNLESS(reader.Load(temp_address, params));
/* Unmap the temporary mapping. */
MESOSPHERE_R_ABORT_UNLESS(Kernel::GetKernelPageTable().UnmapPageGroup(temp_address, pg, KMemoryState_Kernel));
/* Create a KProcess object. */
new_process = KProcess::Create();
MESOSPHERE_ABORT_UNLESS(new_process != nullptr);
/* Initialize the process. */
MESOSPHERE_R_ABORT_UNLESS(new_process->Initialize(params, pg, reader.GetCapabilities(), reader.GetNumCapabilities(), std::addressof(Kernel::GetSystemResourceLimit()), pool));
} }
MESOSPHERE_ABORT_UNLESS(pg.GetNumPages() == static_cast<size_t>(params.code_num_pages));
/* Ensure that we do not leak pages. */
KPageGroup *process_pg = std::addressof(pg);
ON_SCOPE_EXIT { process_pg->Close(); };
/* Get the temporary region. */
const auto &temp_region = KMemoryLayout::GetTempRegion();
MESOSPHERE_ABORT_UNLESS(temp_region.GetEndAddress() != 0);
/* Map the process's memory into the temporary region. */
KProcessAddress temp_address = Null<KProcessAddress>;
MESOSPHERE_R_ABORT_UNLESS(Kernel::GetKernelPageTable().MapPageGroup(std::addressof(temp_address), pg, temp_region.GetAddress(), temp_region.GetSize() / PageSize, KMemoryState_Kernel, KMemoryPermission_KernelReadWrite));
/* Setup the new page group's memory, so that we can load the process. */
{
/* Copy the unaligned ending of the compressed binary. */
if (const size_t unaligned_size = binary_size - util::AlignDown(binary_size, PageSize); unaligned_size != 0) {
std::memcpy(GetVoidPointer(temp_address + process_size - unaligned_size), GetVoidPointer(data + binary_size - unaligned_size), unaligned_size);
}
/* Copy the aligned part of the compressed binary. */
if (const size_t aligned_size = util::AlignDown(binary_size, PageSize); aligned_size != 0 && src_pool == dst_pool) {
std::memmove(GetVoidPointer(temp_address + process_size - binary_size), GetVoidPointer(temp_address), aligned_size);
} else {
if (src_pool != dst_pool) {
std::memcpy(GetVoidPointer(temp_address + process_size - binary_size), GetVoidPointer(data), aligned_size);
Kernel::GetMemoryManager().Close(data, aligned_size / PageSize);
}
}
/* Clear the first part of the memory. */
std::memset(GetVoidPointer(temp_address), 0, process_size - binary_size);
}
/* Load the process. */
MESOSPHERE_R_ABORT_UNLESS(reader.Load(temp_address, params, temp_address + process_size - binary_size));
/* Unmap the temporary mapping. */
MESOSPHERE_R_ABORT_UNLESS(Kernel::GetKernelPageTable().UnmapPageGroup(temp_address, pg, KMemoryState_Kernel));
/* Create a KProcess object. */
new_process = KProcess::Create();
MESOSPHERE_ABORT_UNLESS(new_process != nullptr);
/* Ensure the page group is usable for the process. */
/* If the pool is the same, we need to use the workaround page group. */
if (src_pool == dst_pool) {
/* Allocate a new, usable group for the process. */
MESOSPHERE_R_ABORT_UNLESS(Kernel::GetMemoryManager().AllocateAndOpen(std::addressof(workaround_pg), static_cast<size_t>(params.code_num_pages), KMemoryManager::EncodeOption(dst_pool, KMemoryManager::Direction_FromFront)));
/* Copy data from the working page group to the usable one. */
auto work_it = pg.begin();
MESOSPHERE_ABORT_UNLESS(work_it != pg.end());
{
auto work_address = work_it->GetAddress();
auto work_remaining = work_it->GetNumPages();
for (const auto &block : workaround_pg) {
auto block_address = block.GetAddress();
auto block_remaining = block.GetNumPages();
while (block_remaining > 0) {
if (work_remaining == 0) {
++work_it;
work_address = work_it->GetAddress();
work_remaining = work_it->GetNumPages();
}
const size_t cur_pages = std::min(block_remaining, work_remaining);
const size_t cur_size = cur_pages * PageSize;
std::memcpy(GetVoidPointer(block_address), GetVoidPointer(work_address), cur_size);
block_address += cur_size;
work_address += cur_size;
block_remaining -= cur_pages;
work_remaining -= cur_pages;
}
}
++work_it;
}
MESOSPHERE_ABORT_UNLESS(work_it == pg.end());
/* We want to use the new page group. */
process_pg = std::addressof(workaround_pg);
pg.Close();
}
/* Initialize the process. */
MESOSPHERE_R_ABORT_UNLESS(new_process->Initialize(params, *process_pg, reader.GetCapabilities(), reader.GetNumCapabilities(), std::addressof(Kernel::GetSystemResourceLimit()), dst_pool, reader.IsImmortal()));
}
/* Release the memory that was previously reserved. */
if (const size_t aligned_bin_size = util::AlignDown(binary_size, PageSize); aligned_bin_size != 0 && src_pool != dst_pool) {
Kernel::GetSystemResourceLimit().Release(ams::svc::LimitableResource_PhysicalMemoryMax, aligned_bin_size);
} }
/* Set the process's memory permissions. */ /* Set the process's memory permissions. */
@@ -137,15 +255,18 @@ namespace ams::kern {
infos[i].priority = reader.GetPriority(); infos[i].priority = reader.GetPriority();
/* Advance the reader. */ /* Advance the reader. */
current += reader.GetBinarySize(); current = data + binary_size;
}
/* Release remaining memory used by the image. */
{
const size_t remaining_size = util::AlignUp(GetInteger(g_initial_process_binary_address) + g_initial_process_binary_header.size, PageSize) - util::AlignDown(GetInteger(current), PageSize);
const size_t remaining_pages = remaining_size / PageSize;
Kernel::GetMemoryManager().Close(util::AlignDown(GetInteger(current), PageSize), remaining_pages);
Kernel::GetSystemResourceLimit().Release(ams::svc::LimitableResource_PhysicalMemoryMax, remaining_size);
} }
} }
constinit KVirtualAddress g_initial_process_binary_address = Null<KVirtualAddress>;
constinit InitialProcessBinaryHeader g_initial_process_binary_header = {};
constinit u64 g_initial_process_id_min = std::numeric_limits<u64>::max();
constinit u64 g_initial_process_id_max = std::numeric_limits<u64>::min();
} }
u64 GetInitialProcessIdMin() { u64 GetInitialProcessIdMin() {
@@ -156,32 +277,37 @@ namespace ams::kern {
return g_initial_process_id_max; return g_initial_process_id_max;
} }
size_t GetInitialProcessesSecureMemorySize() { KVirtualAddress GetInitialProcessBinaryAddress() {
LoadInitialProcessBinaryHeader(&g_initial_process_binary_header); /* Get, validate the pool region. */
const auto *pool_region = KMemoryLayout::GetVirtualMemoryRegionTree().FindLastDerived(KMemoryRegionType_VirtualDramUserPool);
return GetProcessesSecureMemorySize(g_initial_process_binary_address != Null<KVirtualAddress> ? g_initial_process_binary_address : GetInitialProcessBinaryAddress(), g_initial_process_binary_header); MESOSPHERE_INIT_ABORT_UNLESS(pool_region != nullptr);
MESOSPHERE_INIT_ABORT_UNLESS(pool_region->GetEndAddress() != 0);
MESOSPHERE_ABORT_UNLESS(pool_region->GetSize() >= InitialProcessBinarySizeMax);
return pool_region->GetEndAddress() - InitialProcessBinarySizeMax;
} }
void CopyInitialProcessBinaryToKernelMemory() { size_t GetInitialProcessesSecureMemorySize() {
LoadInitialProcessBinaryHeader(&g_initial_process_binary_header); LoadInitialProcessBinaryHeader();
return g_initial_process_secure_memory_size;
}
size_t CopyInitialProcessBinaryToKernelMemory() {
LoadInitialProcessBinaryHeader();
if (g_initial_process_binary_header.num_processes > 0) { if (g_initial_process_binary_header.num_processes > 0) {
/* Reserve pages for the initial process binary from the system resource limit. */ /* Reserve pages for the initial process binary from the system resource limit. */
auto &mm = Kernel::GetMemoryManager();
const size_t total_size = util::AlignUp(g_initial_process_binary_header.size, PageSize); const size_t total_size = util::AlignUp(g_initial_process_binary_header.size, PageSize);
const size_t num_pages = total_size / PageSize;
MESOSPHERE_ABORT_UNLESS(Kernel::GetSystemResourceLimit().Reserve(ams::svc::LimitableResource_PhysicalMemoryMax, total_size)); MESOSPHERE_ABORT_UNLESS(Kernel::GetSystemResourceLimit().Reserve(ams::svc::LimitableResource_PhysicalMemoryMax, total_size));
/* Allocate memory for the image. */ /* The initial process binary is potentially over-allocated, so free any extra pages. */
const KMemoryManager::Pool pool = static_cast<KMemoryManager::Pool>(KSystemControl::GetCreateProcessMemoryPool()); if (total_size < InitialProcessBinarySizeMax) {
const auto allocate_option = KMemoryManager::EncodeOption(pool, KMemoryManager::Direction_FromFront); Kernel::GetMemoryManager().Close(g_initial_process_binary_address + total_size, (InitialProcessBinarySizeMax - total_size) / PageSize);
KVirtualAddress allocated_memory = mm.AllocateAndOpenContinuous(num_pages, 1, allocate_option); }
MESOSPHERE_ABORT_UNLESS(allocated_memory != Null<KVirtualAddress>);
/* Relocate the image. */ return total_size;
std::memmove(GetVoidPointer(allocated_memory), GetVoidPointer(GetInitialProcessBinaryAddress()), g_initial_process_binary_header.size); } else {
std::memset(GetVoidPointer(GetInitialProcessBinaryAddress()), 0, g_initial_process_binary_header.size); return 0;
g_initial_process_binary_address = allocated_memory;
} }
} }
@@ -190,15 +316,7 @@ namespace ams::kern {
InitialProcessInfo *infos = static_cast<InitialProcessInfo *>(__builtin_alloca(sizeof(InitialProcessInfo) * g_initial_process_binary_header.num_processes)); InitialProcessInfo *infos = static_cast<InitialProcessInfo *>(__builtin_alloca(sizeof(InitialProcessInfo) * g_initial_process_binary_header.num_processes));
/* Create the processes. */ /* Create the processes. */
CreateProcesses(infos, g_initial_process_binary_address, g_initial_process_binary_header); CreateProcesses(infos);
/* Release the memory used by the image. */
{
const size_t total_size = util::AlignUp(g_initial_process_binary_header.size, PageSize);
const size_t num_pages = total_size / PageSize;
Kernel::GetMemoryManager().Close(g_initial_process_binary_address, num_pages);
Kernel::GetSystemResourceLimit().Release(ams::svc::LimitableResource_PhysicalMemoryMax, total_size);
}
/* Determine the initial process id range. */ /* Determine the initial process id range. */
for (size_t i = 0; i < g_initial_process_binary_header.num_processes; i++) { for (size_t i = 0; i < g_initial_process_binary_header.num_processes; i++) {
@@ -210,6 +328,7 @@ namespace ams::kern {
/* Run the processes. */ /* Run the processes. */
for (size_t i = 0; i < g_initial_process_binary_header.num_processes; i++) { for (size_t i = 0; i < g_initial_process_binary_header.num_processes; i++) {
MESOSPHERE_R_ABORT_UNLESS(infos[i].process->Run(infos[i].priority, infos[i].stack_size)); MESOSPHERE_R_ABORT_UNLESS(infos[i].process->Run(infos[i].priority, infos[i].stack_size));
infos[i].process->Close();
} }
} }

View File

@@ -42,6 +42,10 @@ namespace ams::kern {
return this->GetParent()->IsLight(); return this->GetParent()->IsLight();
} }
bool KClientPort::IsServerClosed() const {
return this->GetParent()->IsServerClosed();
}
void KClientPort::Destroy() { void KClientPort::Destroy() {
/* Note with our parent that we're closed. */ /* Note with our parent that we're closed. */
m_parent->OnClientClosed(); m_parent->OnClientClosed();

View File

@@ -23,12 +23,11 @@ namespace ams::kern {
/* Set members. */ /* Set members. */
m_owner = GetCurrentProcessPointer(); m_owner = GetCurrentProcessPointer();
/* Initialize the page group. */ /* Get the owner page table. */
auto &page_table = m_owner->GetPageTable(); auto &page_table = m_owner->GetPageTable();
new (GetPointer(m_page_group)) KPageGroup(page_table.GetBlockInfoManager());
/* Ensure that our page group's state is valid on exit. */ /* Construct the page group, guarding to make sure our state is valid on exit. */
auto pg_guard = SCOPE_GUARD { GetReference(m_page_group).~KPageGroup(); }; auto pg_guard = util::ConstructAtGuarded(m_page_group, page_table.GetBlockInfoManager());
/* Lock the memory. */ /* Lock the memory. */
R_TRY(page_table.LockForCodeMemory(GetPointer(m_page_group), addr, size)); R_TRY(page_table.LockForCodeMemory(GetPointer(m_page_group), addr, size));

View File

@@ -102,14 +102,13 @@ namespace ams::kern {
} }
} }
MESOSPHERE_ASSERT(owner_thread.IsNotNull()); MESOSPHERE_ASSERT(owner_thread.IsNotNull());
}
/* Remove the thread as a waiter from the lock owner. */ /* Remove the thread as a waiter from the lock owner. */
{ {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
KThread *owner_thread = cur_thread->GetLockOwner(); if (KThread *mutex_owner = cur_thread->GetLockOwner(); mutex_owner != nullptr) {
if (owner_thread != nullptr) { mutex_owner->RemoveWaiter(cur_thread);
owner_thread->RemoveWaiter(cur_thread); }
} }
} }
@@ -118,7 +117,7 @@ namespace ams::kern {
return cur_thread->GetWaitResult(std::addressof(dummy)); return cur_thread->GetWaitResult(std::addressof(dummy));
} }
KThread *KConditionVariable::SignalImpl(KThread *thread) { void KConditionVariable::SignalImpl(KThread *thread) {
/* Check pre-conditions. */ /* Check pre-conditions. */
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread()); MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
@@ -137,7 +136,6 @@ namespace ams::kern {
} }
} }
KThread *thread_to_close = nullptr;
if (AMS_LIKELY(can_access)) { if (AMS_LIKELY(can_access)) {
if (prev_tag == ams::svc::InvalidHandle) { if (prev_tag == ams::svc::InvalidHandle) {
/* If nobody held the lock previously, we're all good. */ /* If nobody held the lock previously, we're all good. */
@@ -150,7 +148,7 @@ namespace ams::kern {
if (AMS_LIKELY(owner_thread != nullptr)) { if (AMS_LIKELY(owner_thread != nullptr)) {
/* Add the thread as a waiter on the owner. */ /* Add the thread as a waiter on the owner. */
owner_thread->AddWaiter(thread); owner_thread->AddWaiter(thread);
thread_to_close = owner_thread; owner_thread->Close();
} else { } else {
/* The lock was tagged with a thread that doesn't exist. */ /* The lock was tagged with a thread that doesn't exist. */
thread->SetSyncedObject(nullptr, svc::ResultInvalidState()); thread->SetSyncedObject(nullptr, svc::ResultInvalidState());
@@ -162,17 +160,9 @@ namespace ams::kern {
thread->SetSyncedObject(nullptr, svc::ResultInvalidCurrentMemory()); thread->SetSyncedObject(nullptr, svc::ResultInvalidCurrentMemory());
thread->Wakeup(); thread->Wakeup();
} }
return thread_to_close;
} }
void KConditionVariable::Signal(uintptr_t cv_key, s32 count) { void KConditionVariable::Signal(uintptr_t cv_key, s32 count) {
/* Prepare for signaling. */
constexpr int MaxThreads = 16;
KLinkedList<KThread> thread_list;
KThread *thread_array[MaxThreads];
int num_to_close = 0;
/* Perform signaling. */ /* Perform signaling. */
int num_waiters = 0; int num_waiters = 0;
{ {
@@ -182,14 +172,7 @@ namespace ams::kern {
while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) && (it->GetConditionVariableKey() == cv_key)) { while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) && (it->GetConditionVariableKey() == cv_key)) {
KThread *target_thread = std::addressof(*it); KThread *target_thread = std::addressof(*it);
if (KThread *thread = this->SignalImpl(target_thread); thread != nullptr) { this->SignalImpl(target_thread);
if (num_to_close < MaxThreads) {
thread_array[num_to_close++] = thread;
} else {
thread_list.push_back(*thread);
}
}
it = m_tree.erase(it); it = m_tree.erase(it);
target_thread->ClearConditionVariable(); target_thread->ClearConditionVariable();
++num_waiters; ++num_waiters;
@@ -201,16 +184,6 @@ namespace ams::kern {
WriteToUser(cv_key, std::addressof(has_waiter_flag)); WriteToUser(cv_key, std::addressof(has_waiter_flag));
} }
} }
/* Close threads in the array. */
for (auto i = 0; i < num_to_close; ++i) {
thread_array[i]->Close();
}
/* Close threads in the list. */
for (auto it = thread_list.begin(); it != thread_list.end(); it = thread_list.erase(it)) {
(*it).Close();
}
} }
Result KConditionVariable::Wait(KProcessAddress addr, uintptr_t key, u32 value, s64 timeout) { Result KConditionVariable::Wait(KProcessAddress addr, uintptr_t key, u32 value, s64 timeout) {
@@ -276,11 +249,6 @@ namespace ams::kern {
} }
} }
/* Cancel the timer wait. */
if (timer != nullptr) {
timer->CancelTask(cur_thread);
}
/* Remove from the condition variable. */ /* Remove from the condition variable. */
{ {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
@@ -295,6 +263,11 @@ namespace ams::kern {
} }
} }
/* Cancel the timer wait. */
if (timer != nullptr) {
timer->CancelTask(cur_thread);
}
/* Get the result. */ /* Get the result. */
KSynchronizationObject *dummy; KSynchronizationObject *dummy;
return cur_thread->GetWaitResult(std::addressof(dummy)); return cur_thread->GetWaitResult(std::addressof(dummy));

View File

@@ -92,51 +92,7 @@ namespace ams::kern {
R_TRY(target_pt.ReadDebugMemory(GetVoidPointer(buffer), cur_address, cur_size)); R_TRY(target_pt.ReadDebugMemory(GetVoidPointer(buffer), cur_address, cur_size));
} else { } else {
/* The memory is IO memory. */ /* The memory is IO memory. */
R_TRY(target_pt.ReadDebugIoMemory(GetVoidPointer(buffer), cur_address, cur_size));
/* Verify that the memory is readable. */
R_UNLESS((info.GetPermission() & KMemoryPermission_UserRead) == KMemoryPermission_UserRead, svc::ResultInvalidAddress());
/* Get the physical address of the memory. */
/* NOTE: Nintendo does not verify the result of this call. */
KPhysicalAddress phys_addr;
target_pt.GetPhysicalAddress(std::addressof(phys_addr), cur_address);
/* Map the address as IO in the current process. */
R_TRY(debugger_pt.MapIo(util::AlignDown(GetInteger(phys_addr), PageSize), PageSize, KMemoryPermission_UserRead));
/* Get the address of the newly mapped IO. */
KProcessAddress io_address;
Result query_result = debugger_pt.QueryIoMapping(std::addressof(io_address), util::AlignDown(GetInteger(phys_addr), PageSize), PageSize);
MESOSPHERE_R_ASSERT(query_result);
R_TRY(query_result);
/* Ensure we clean up the new mapping on scope exit. */
ON_SCOPE_EXIT { MESOSPHERE_R_ABORT_UNLESS(debugger_pt.UnmapPages(util::AlignDown(GetInteger(io_address), PageSize), 1, KMemoryState_Io)); };
/* Adjust the io address for alignment. */
io_address += (GetInteger(cur_address) & (PageSize - 1));
/* Get the readable size. */
const size_t readable_size = std::min(cur_size, util::AlignDown(GetInteger(cur_address) + PageSize, PageSize) - GetInteger(cur_address));
/* Read the memory. */
switch ((GetInteger(cur_address) | readable_size) & 3) {
case 0:
{
R_UNLESS(UserspaceAccess::ReadIoMemory32Bit(GetVoidPointer(buffer), GetVoidPointer(io_address), readable_size), svc::ResultInvalidPointer());
}
break;
case 2:
{
R_UNLESS(UserspaceAccess::ReadIoMemory16Bit(GetVoidPointer(buffer), GetVoidPointer(io_address), readable_size), svc::ResultInvalidPointer());
}
break;
default:
{
R_UNLESS(UserspaceAccess::ReadIoMemory8Bit(GetVoidPointer(buffer), GetVoidPointer(io_address), readable_size), svc::ResultInvalidPointer());
}
break;
}
} }
/* Advance. */ /* Advance. */
@@ -185,51 +141,7 @@ namespace ams::kern {
R_TRY(target_pt.WriteDebugMemory(cur_address, GetVoidPointer(buffer), cur_size)); R_TRY(target_pt.WriteDebugMemory(cur_address, GetVoidPointer(buffer), cur_size));
} else { } else {
/* The memory is IO memory. */ /* The memory is IO memory. */
R_TRY(target_pt.WriteDebugIoMemory(cur_address, GetVoidPointer(buffer), cur_size));
/* Verify that the memory is writable. */
R_UNLESS((info.GetPermission() & KMemoryPermission_UserReadWrite) == KMemoryPermission_UserReadWrite, svc::ResultInvalidAddress());
/* Get the physical address of the memory. */
/* NOTE: Nintendo does not verify the result of this call. */
KPhysicalAddress phys_addr;
target_pt.GetPhysicalAddress(std::addressof(phys_addr), cur_address);
/* Map the address as IO in the current process. */
R_TRY(debugger_pt.MapIo(util::AlignDown(GetInteger(phys_addr), PageSize), PageSize, KMemoryPermission_UserReadWrite));
/* Get the address of the newly mapped IO. */
KProcessAddress io_address;
Result query_result = debugger_pt.QueryIoMapping(std::addressof(io_address), util::AlignDown(GetInteger(phys_addr), PageSize), PageSize);
MESOSPHERE_R_ASSERT(query_result);
R_TRY(query_result);
/* Ensure we clean up the new mapping on scope exit. */
ON_SCOPE_EXIT { MESOSPHERE_R_ABORT_UNLESS(debugger_pt.UnmapPages(util::AlignDown(GetInteger(io_address), PageSize), 1, KMemoryState_Io)); };
/* Adjust the io address for alignment. */
io_address += (GetInteger(cur_address) & (PageSize - 1));
/* Get the readable size. */
const size_t readable_size = std::min(cur_size, util::AlignDown(GetInteger(cur_address) + PageSize, PageSize) - GetInteger(cur_address));
/* Read the memory. */
switch ((GetInteger(cur_address) | readable_size) & 3) {
case 0:
{
R_UNLESS(UserspaceAccess::WriteIoMemory32Bit(GetVoidPointer(io_address), GetVoidPointer(buffer), readable_size), svc::ResultInvalidPointer());
}
break;
case 2:
{
R_UNLESS(UserspaceAccess::WriteIoMemory16Bit(GetVoidPointer(io_address), GetVoidPointer(buffer), readable_size), svc::ResultInvalidPointer());
}
break;
default:
{
R_UNLESS(UserspaceAccess::WriteIoMemory8Bit(GetVoidPointer(io_address), GetVoidPointer(buffer), readable_size), svc::ResultInvalidPointer());
}
break;
}
} }
/* Advance. */ /* Advance. */
@@ -443,6 +355,15 @@ namespace ams::kern {
} else if (state == KProcess::State_DebugBreak) { } else if (state == KProcess::State_DebugBreak) {
/* If the process is debug breaked, transition it accordingly. */ /* If the process is debug breaked, transition it accordingly. */
new_state = KProcess::State_Crashed; new_state = KProcess::State_Crashed;
/* Suspend all the threads in the process. */
{
auto end = target->GetThreadList().end();
for (auto it = target->GetThreadList().begin(); it != end; ++it) {
/* Request that we suspend the thread. */
it->RequestSuspend(KThread::SuspendType_Debug);
}
}
} else { } else {
/* Otherwise, don't transition. */ /* Otherwise, don't transition. */
new_state = state; new_state = state;
@@ -557,8 +478,12 @@ namespace ams::kern {
/* Verify that the thread's svc state is valid. */ /* Verify that the thread's svc state is valid. */
if (thread->IsCallingSvc()) { if (thread->IsCallingSvc()) {
R_UNLESS(thread->GetSvcId() != svc::SvcId_Break, svc::ResultInvalidState()); const u8 svc_id = thread->GetSvcId();
R_UNLESS(thread->GetSvcId() != svc::SvcId_ReturnFromException, svc::ResultInvalidState());
const bool is_valid_svc = svc_id == svc::SvcId_Break ||
svc_id == svc::SvcId_ReturnFromException;
R_UNLESS(is_valid_svc, svc::ResultInvalidState());
} }
/* Set the thread context. */ /* Set the thread context. */
@@ -924,9 +849,6 @@ namespace ams::kern {
/* If the process isn't null, detach. */ /* If the process isn't null, detach. */
if (process.IsNotNull()) { if (process.IsNotNull()) {
/* When we're done detaching, clear the reference we opened when we attached. */
ON_SCOPE_EXIT { process->Close(); };
/* Detach. */ /* Detach. */
{ {
/* Lock both ourselves and the target process. */ /* Lock both ourselves and the target process. */
@@ -961,6 +883,9 @@ namespace ams::kern {
/* Clear our process. */ /* Clear our process. */
m_process = nullptr; m_process = nullptr;
} }
/* We're done detaching, so clear the reference we opened when we attached. */
process->Close();
} }
} }
} }

View File

@@ -71,12 +71,11 @@ namespace ams::kern {
/* Lock the address space. */ /* Lock the address space. */
KScopedLightLock lk(m_lock); KScopedLightLock lk(m_lock);
/* Lock the pages. */ /* Lock the page table to prevent concurrent device mapping operations. */
KPageGroup pg(page_table->GetBlockInfoManager()); KScopedLightLock pt_lk = page_table->AcquireDeviceMapLock();
R_TRY(page_table->LockForDeviceAddressSpace(std::addressof(pg), process_address, size, ConvertToKMemoryPermission(device_perm), is_aligned));
/* Close the pages we opened when we're done with them. */ /* Lock the pages. */
ON_SCOPE_EXIT { pg.Close(); }; R_TRY(page_table->LockForMapDeviceAddressSpace(process_address, size, ConvertToKMemoryPermission(device_perm), is_aligned));
/* Ensure that if we fail, we don't keep unmapped pages locked. */ /* Ensure that if we fail, we don't keep unmapped pages locked. */
auto unlock_guard = SCOPE_GUARD { MESOSPHERE_R_ABORT_UNLESS(page_table->UnlockForDeviceAddressSpace(process_address, size)); }; auto unlock_guard = SCOPE_GUARD { MESOSPHERE_R_ABORT_UNLESS(page_table->UnlockForDeviceAddressSpace(process_address, size)); };
@@ -87,7 +86,7 @@ namespace ams::kern {
auto mapped_size_guard = SCOPE_GUARD { *out_mapped_size = 0; }; auto mapped_size_guard = SCOPE_GUARD { *out_mapped_size = 0; };
/* Perform the mapping. */ /* Perform the mapping. */
R_TRY(m_table.Map(out_mapped_size, pg, device_address, device_perm, refresh_mappings)); R_TRY(m_table.Map(out_mapped_size, page_table, process_address, size, device_address, device_perm, refresh_mappings));
/* Ensure that we unmap the pages if we fail to update the protections. */ /* Ensure that we unmap the pages if we fail to update the protections. */
/* NOTE: Nintendo does not check the result of this unmap call. */ /* NOTE: Nintendo does not check the result of this unmap call. */
@@ -113,19 +112,18 @@ namespace ams::kern {
/* Lock the address space. */ /* Lock the address space. */
KScopedLightLock lk(m_lock); KScopedLightLock lk(m_lock);
/* Make and open a page group for the unmapped region. */ /* Lock the page table to prevent concurrent device mapping operations. */
KPageGroup pg(page_table->GetBlockInfoManager()); KScopedLightLock pt_lk = page_table->AcquireDeviceMapLock();
R_TRY(page_table->MakePageGroupForUnmapDeviceAddressSpace(std::addressof(pg), process_address, size));
/* Ensure the page group is closed on scope exit. */ /* Lock the pages. */
ON_SCOPE_EXIT { pg.Close(); }; R_TRY(page_table->LockForUnmapDeviceAddressSpace(process_address, size));
/* If we fail to unmap, we want to do a partial unlock. */ /* If we fail to unmap, we want to do a partial unlock. */
{ {
auto unlock_guard = SCOPE_GUARD { page_table->UnlockForDeviceAddressSpacePartialMap(process_address, size, size); }; auto unlock_guard = SCOPE_GUARD { page_table->UnlockForDeviceAddressSpacePartialMap(process_address, size, size); };
/* Unmap. */ /* Unmap. */
R_TRY(m_table.Unmap(pg, device_address)); R_TRY(m_table.Unmap(page_table, process_address, size, device_address));
unlock_guard.Cancel(); unlock_guard.Cancel();
} }

View File

@@ -174,13 +174,20 @@ namespace ams::kern {
MESOSPHERE_ASSERT(!KInterruptManager::AreInterruptsEnabled()); MESOSPHERE_ASSERT(!KInterruptManager::AreInterruptsEnabled());
MESOSPHERE_ASSERT(!KScheduler::IsSchedulerLockedByCurrentThread()); MESOSPHERE_ASSERT(!KScheduler::IsSchedulerLockedByCurrentThread());
/* The only deferred procedure supported by Horizon is thread termination. */ /* Get reference to the current thread. */
/* Check if we need to terminate the current thread. */ KThread &cur_thread = GetCurrentThread();
KThread *cur_thread = GetCurrentThreadPointer();
if (cur_thread->IsTerminationRequested()) { /* Enable interrupts, temporarily. */
KScopedInterruptEnable ei; KScopedInterruptEnable ei;
cur_thread->Exit();
/* If the thread is scheduled for termination, exit the thread. */
if (cur_thread.IsTerminationRequested()) {
cur_thread.Exit();
__builtin_unreachable();
} }
/* We may also need to destroy any closed objects. */
cur_thread.DestroyClosedObjects();
} }
void KDpcManager::Sync() { void KDpcManager::Sync() {

View File

@@ -345,7 +345,6 @@ namespace ams::kern::KDumpObject {
DUMP_KSLABOBJ(KDebug); DUMP_KSLABOBJ(KDebug);
DUMP_KSLABOBJ(KSession); DUMP_KSLABOBJ(KSession);
DUMP_KSLABOBJ(KLightSession); DUMP_KSLABOBJ(KLightSession);
DUMP_KSLABOBJ(KLinkedListNode);
DUMP_KSLABOBJ(KThreadLocalPage); DUMP_KSLABOBJ(KThreadLocalPage);
DUMP_KSLABOBJ(KObjectName); DUMP_KSLABOBJ(KObjectName);
DUMP_KSLABOBJ(KEventInfo); DUMP_KSLABOBJ(KEventInfo);

View File

@@ -21,23 +21,18 @@ namespace ams::kern {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
/* Get the table and clear our record of it. */ /* Get the table and clear our record of it. */
Entry *saved_table = nullptr;
u16 saved_table_size = 0; u16 saved_table_size = 0;
{ {
KScopedDisableDispatch dd; KScopedDisableDispatch dd;
KScopedSpinLock lk(m_lock); KScopedSpinLock lk(m_lock);
std::swap(m_table, saved_table);
std::swap(m_table_size, saved_table_size); std::swap(m_table_size, saved_table_size);
} }
/* Close and free all entries. */ /* Close and free all entries. */
for (size_t i = 0; i < saved_table_size; i++) { for (size_t i = 0; i < saved_table_size; i++) {
Entry *entry = std::addressof(saved_table[i]); if (KAutoObject *obj = m_objects[i]; obj != nullptr) {
if (KAutoObject *obj = entry->GetObject(); obj != nullptr) {
obj->Close(); obj->Close();
this->FreeEntry(entry);
} }
} }
@@ -48,12 +43,13 @@ namespace ams::kern {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
/* Don't allow removal of a pseudo-handle. */ /* Don't allow removal of a pseudo-handle. */
if (ams::svc::IsPseudoHandle(handle)) { if (AMS_UNLIKELY(ams::svc::IsPseudoHandle(handle))) {
return false; return false;
} }
/* Handles must not have reserved bits set. */ /* Handles must not have reserved bits set. */
if (GetHandleBitPack(handle).Get<HandleReserved>() != 0) { const auto handle_pack = GetHandleBitPack(handle);
if (AMS_UNLIKELY(handle_pack.Get<HandleReserved>() != 0)) {
return false; return false;
} }
@@ -63,9 +59,11 @@ namespace ams::kern {
KScopedDisableDispatch dd; KScopedDisableDispatch dd;
KScopedSpinLock lk(m_lock); KScopedSpinLock lk(m_lock);
if (Entry *entry = this->FindEntry(handle); entry != nullptr) { if (AMS_LIKELY(this->IsValidHandle(handle))) {
obj = entry->GetObject(); const auto index = handle_pack.Get<HandleIndex>();
this->FreeEntry(entry);
obj = m_objects[index];
this->FreeEntry(index);
} else { } else {
return false; return false;
} }
@@ -87,10 +85,14 @@ namespace ams::kern {
/* Allocate entry, set output handle. */ /* Allocate entry, set output handle. */
{ {
const auto linear_id = this->AllocateLinearId(); const auto linear_id = this->AllocateLinearId();
Entry *entry = this->AllocateEntry(); const auto index = this->AllocateEntry();
entry->SetUsed(obj, linear_id, type);
m_entry_infos[index].info = { .linear_id = linear_id, .type = type };
m_objects[index] = obj;
obj->Open(); obj->Open();
*out_handle = EncodeHandle(this->GetEntryIndex(entry), linear_id);
*out_handle = EncodeHandle(index, linear_id);
} }
return ResultSuccess(); return ResultSuccess();
@@ -104,7 +106,7 @@ namespace ams::kern {
/* Never exceed our capacity. */ /* Never exceed our capacity. */
R_UNLESS(m_count < m_table_size, svc::ResultOutOfHandles()); R_UNLESS(m_count < m_table_size, svc::ResultOutOfHandles());
*out_handle = EncodeHandle(this->GetEntryIndex(this->AllocateEntry()), this->AllocateLinearId()); *out_handle = EncodeHandle(this->AllocateEntry(), this->AllocateLinearId());
return ResultSuccess(); return ResultSuccess();
} }
@@ -120,15 +122,13 @@ namespace ams::kern {
const auto reserved = handle_pack.Get<HandleReserved>(); const auto reserved = handle_pack.Get<HandleReserved>();
MESOSPHERE_ASSERT(reserved == 0); MESOSPHERE_ASSERT(reserved == 0);
MESOSPHERE_ASSERT(linear_id != 0); MESOSPHERE_ASSERT(linear_id != 0);
MESOSPHERE_ASSERT(index < m_table_size);
MESOSPHERE_UNUSED(linear_id, reserved); MESOSPHERE_UNUSED(linear_id, reserved);
/* Free the entry. */ if (AMS_LIKELY(index < m_table_size)) {
/* NOTE: This code does not check the linear id. */ /* NOTE: This code does not check the linear id. */
Entry *entry = std::addressof(m_table[index]); MESOSPHERE_ASSERT(m_objects[index] == nullptr);
MESOSPHERE_ASSERT(entry->GetObject() == nullptr); this->FreeEntry(index);
}
this->FreeEntry(entry);
} }
void KHandleTable::Register(ams::svc::Handle handle, KAutoObject *obj, u16 type) { void KHandleTable::Register(ams::svc::Handle handle, KAutoObject *obj, u16 type) {
@@ -143,15 +143,17 @@ namespace ams::kern {
const auto reserved = handle_pack.Get<HandleReserved>(); const auto reserved = handle_pack.Get<HandleReserved>();
MESOSPHERE_ASSERT(reserved == 0); MESOSPHERE_ASSERT(reserved == 0);
MESOSPHERE_ASSERT(linear_id != 0); MESOSPHERE_ASSERT(linear_id != 0);
MESOSPHERE_ASSERT(index < m_table_size);
MESOSPHERE_UNUSED(reserved); MESOSPHERE_UNUSED(reserved);
/* Set the entry. */ if (AMS_LIKELY(index < m_table_size)) {
Entry *entry = std::addressof(m_table[index]); /* Set the entry. */
MESOSPHERE_ASSERT(entry->GetObject() == nullptr); MESOSPHERE_ASSERT(m_objects[index] == nullptr);
entry->SetUsed(obj, linear_id, type); m_entry_infos[index].info = { .linear_id = linear_id, .type = type };
obj->Open(); m_objects[index] = obj;
obj->Open();
}
} }
} }

View File

@@ -77,14 +77,14 @@ namespace ams::kern {
Result KInitialProcessReader::MakeCreateProcessParameter(ams::svc::CreateProcessParameter *out, bool enable_aslr) const { Result KInitialProcessReader::MakeCreateProcessParameter(ams::svc::CreateProcessParameter *out, bool enable_aslr) const {
/* Get and validate addresses/sizes. */ /* Get and validate addresses/sizes. */
const uintptr_t rx_address = m_kip_header->GetRxAddress(); const uintptr_t rx_address = m_kip_header.GetRxAddress();
const size_t rx_size = m_kip_header->GetRxSize(); const size_t rx_size = m_kip_header.GetRxSize();
const uintptr_t ro_address = m_kip_header->GetRoAddress(); const uintptr_t ro_address = m_kip_header.GetRoAddress();
const size_t ro_size = m_kip_header->GetRoSize(); const size_t ro_size = m_kip_header.GetRoSize();
const uintptr_t rw_address = m_kip_header->GetRwAddress(); const uintptr_t rw_address = m_kip_header.GetRwAddress();
const size_t rw_size = m_kip_header->GetRwSize(); const size_t rw_size = m_kip_header.GetRwSize();
const uintptr_t bss_address = m_kip_header->GetBssAddress(); const uintptr_t bss_address = m_kip_header.GetBssAddress();
const size_t bss_size = m_kip_header->GetBssSize(); const size_t bss_size = m_kip_header.GetBssSize();
R_UNLESS(util::IsAligned(rx_address, PageSize), svc::ResultInvalidAddress()); R_UNLESS(util::IsAligned(rx_address, PageSize), svc::ResultInvalidAddress());
R_UNLESS(util::IsAligned(ro_address, PageSize), svc::ResultInvalidAddress()); R_UNLESS(util::IsAligned(ro_address, PageSize), svc::ResultInvalidAddress());
R_UNLESS(util::IsAligned(rw_address, PageSize), svc::ResultInvalidAddress()); R_UNLESS(util::IsAligned(rw_address, PageSize), svc::ResultInvalidAddress());
@@ -115,13 +115,13 @@ namespace ams::kern {
/* Set fields in parameter. */ /* Set fields in parameter. */
out->code_address = map_start + start_address; out->code_address = map_start + start_address;
out->code_num_pages = util::AlignUp(end_address - start_address, PageSize) / PageSize; out->code_num_pages = util::AlignUp(end_address - start_address, PageSize) / PageSize;
out->program_id = m_kip_header->GetProgramId(); out->program_id = m_kip_header.GetProgramId();
out->version = m_kip_header->GetVersion(); out->version = m_kip_header.GetVersion();
out->flags = 0; out->flags = 0;
MESOSPHERE_ABORT_UNLESS((out->code_address / PageSize) + out->code_num_pages <= (map_end / PageSize)); MESOSPHERE_ABORT_UNLESS((out->code_address / PageSize) + out->code_num_pages <= (map_end / PageSize));
/* Copy name field. */ /* Copy name field. */
m_kip_header->GetName(out->name, sizeof(out->name)); m_kip_header.GetName(out->name, sizeof(out->name));
/* Apply ASLR, if needed. */ /* Apply ASLR, if needed. */
if (enable_aslr) { if (enable_aslr) {
@@ -146,39 +146,36 @@ namespace ams::kern {
return ResultSuccess(); return ResultSuccess();
} }
Result KInitialProcessReader::Load(KProcessAddress address, const ams::svc::CreateProcessParameter &params) const { Result KInitialProcessReader::Load(KProcessAddress address, const ams::svc::CreateProcessParameter &params, KProcessAddress src) const {
/* Clear memory at the address. */
std::memset(GetVoidPointer(address), 0, params.code_num_pages * PageSize);
/* Prepare to layout the data. */ /* Prepare to layout the data. */
const KProcessAddress rx_address = address + m_kip_header->GetRxAddress(); const KProcessAddress rx_address = address + m_kip_header.GetRxAddress();
const KProcessAddress ro_address = address + m_kip_header->GetRoAddress(); const KProcessAddress ro_address = address + m_kip_header.GetRoAddress();
const KProcessAddress rw_address = address + m_kip_header->GetRwAddress(); const KProcessAddress rw_address = address + m_kip_header.GetRwAddress();
const u8 *rx_binary = reinterpret_cast<const u8 *>(m_kip_header + 1); const u8 *rx_binary = GetPointer<const u8>(src);
const u8 *ro_binary = rx_binary + m_kip_header->GetRxCompressedSize(); const u8 *ro_binary = rx_binary + m_kip_header.GetRxCompressedSize();
const u8 *rw_binary = ro_binary + m_kip_header->GetRoCompressedSize(); const u8 *rw_binary = ro_binary + m_kip_header.GetRoCompressedSize();
/* Copy text. */ /* Copy text. */
if (util::AlignUp(m_kip_header->GetRxSize(), PageSize)) { if (util::AlignUp(m_kip_header.GetRxSize(), PageSize)) {
std::memcpy(GetVoidPointer(rx_address), rx_binary, m_kip_header->GetRxCompressedSize()); std::memmove(GetVoidPointer(rx_address), rx_binary, m_kip_header.GetRxCompressedSize());
if (m_kip_header->IsRxCompressed()) { if (m_kip_header.IsRxCompressed()) {
BlzUncompress(GetVoidPointer(rx_address + m_kip_header->GetRxCompressedSize())); BlzUncompress(GetVoidPointer(rx_address + m_kip_header.GetRxCompressedSize()));
} }
} }
/* Copy rodata. */ /* Copy rodata. */
if (util::AlignUp(m_kip_header->GetRoSize(), PageSize)) { if (util::AlignUp(m_kip_header.GetRoSize(), PageSize)) {
std::memcpy(GetVoidPointer(ro_address), ro_binary, m_kip_header->GetRoCompressedSize()); std::memmove(GetVoidPointer(ro_address), ro_binary, m_kip_header.GetRoCompressedSize());
if (m_kip_header->IsRoCompressed()) { if (m_kip_header.IsRoCompressed()) {
BlzUncompress(GetVoidPointer(ro_address + m_kip_header->GetRoCompressedSize())); BlzUncompress(GetVoidPointer(ro_address + m_kip_header.GetRoCompressedSize()));
} }
} }
/* Copy rwdata. */ /* Copy rwdata. */
if (util::AlignUp(m_kip_header->GetRwSize(), PageSize)) { if (util::AlignUp(m_kip_header.GetRwSize(), PageSize)) {
std::memcpy(GetVoidPointer(rw_address), rw_binary, m_kip_header->GetRwCompressedSize()); std::memmove(GetVoidPointer(rw_address), rw_binary, m_kip_header.GetRwCompressedSize());
if (m_kip_header->IsRwCompressed()) { if (m_kip_header.IsRwCompressed()) {
BlzUncompress(GetVoidPointer(rw_address + m_kip_header->GetRwCompressedSize())); BlzUncompress(GetVoidPointer(rw_address + m_kip_header.GetRwCompressedSize()));
} }
} }
@@ -192,27 +189,27 @@ namespace ams::kern {
} }
Result KInitialProcessReader::SetMemoryPermissions(KProcessPageTable &page_table, const ams::svc::CreateProcessParameter &params) const { Result KInitialProcessReader::SetMemoryPermissions(KProcessPageTable &page_table, const ams::svc::CreateProcessParameter &params) const {
const size_t rx_size = m_kip_header->GetRxSize(); const size_t rx_size = m_kip_header.GetRxSize();
const size_t ro_size = m_kip_header->GetRoSize(); const size_t ro_size = m_kip_header.GetRoSize();
const size_t rw_size = m_kip_header->GetRwSize(); const size_t rw_size = m_kip_header.GetRwSize();
const size_t bss_size = m_kip_header->GetBssSize(); const size_t bss_size = m_kip_header.GetBssSize();
/* Set R-X pages. */ /* Set R-X pages. */
if (rx_size) { if (rx_size) {
const uintptr_t start = m_kip_header->GetRxAddress() + params.code_address; const uintptr_t start = m_kip_header.GetRxAddress() + params.code_address;
R_TRY(page_table.SetProcessMemoryPermission(start, util::AlignUp(rx_size, PageSize), ams::svc::MemoryPermission_ReadExecute)); R_TRY(page_table.SetProcessMemoryPermission(start, util::AlignUp(rx_size, PageSize), ams::svc::MemoryPermission_ReadExecute));
} }
/* Set R-- pages. */ /* Set R-- pages. */
if (ro_size) { if (ro_size) {
const uintptr_t start = m_kip_header->GetRoAddress() + params.code_address; const uintptr_t start = m_kip_header.GetRoAddress() + params.code_address;
R_TRY(page_table.SetProcessMemoryPermission(start, util::AlignUp(ro_size, PageSize), ams::svc::MemoryPermission_Read)); R_TRY(page_table.SetProcessMemoryPermission(start, util::AlignUp(ro_size, PageSize), ams::svc::MemoryPermission_Read));
} }
/* Set RW- pages. */ /* Set RW- pages. */
if (rw_size || bss_size) { if (rw_size || bss_size) {
const uintptr_t start = (rw_size ? m_kip_header->GetRwAddress() : m_kip_header->GetBssAddress()) + params.code_address; const uintptr_t start = (rw_size ? m_kip_header.GetRwAddress() : m_kip_header.GetBssAddress()) + params.code_address;
const uintptr_t end = (bss_size ? m_kip_header->GetBssAddress() + bss_size : m_kip_header->GetRwAddress() + rw_size) + params.code_address; const uintptr_t end = (bss_size ? m_kip_header.GetBssAddress() + bss_size : m_kip_header.GetRwAddress() + rw_size) + params.code_address;
R_TRY(page_table.SetProcessMemoryPermission(start, util::AlignUp(end - start, PageSize), ams::svc::MemoryPermission_ReadWrite)); R_TRY(page_table.SetProcessMemoryPermission(start, util::AlignUp(end - start, PageSize), ams::svc::MemoryPermission_ReadWrite));
} }

View File

@@ -27,14 +27,21 @@ namespace ams::kern {
Result KInterruptEvent::Initialize(int32_t interrupt_name, ams::svc::InterruptType type) { Result KInterruptEvent::Initialize(int32_t interrupt_name, ams::svc::InterruptType type) {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
/* Verify the interrupt is defined and global. */
R_UNLESS(Kernel::GetInterruptManager().IsInterruptDefined(interrupt_name), svc::ResultOutOfRange());
R_UNLESS(Kernel::GetInterruptManager().IsGlobal(interrupt_name), svc::ResultOutOfRange());
/* Set interrupt id. */ /* Set interrupt id. */
m_interrupt_id = interrupt_name; m_interrupt_id = interrupt_name;
/* Set core id. */
m_core_id = GetCurrentCoreId();
/* Initialize readable event base. */ /* Initialize readable event base. */
KReadableEvent::Initialize(nullptr); KReadableEvent::Initialize(nullptr);
/* Try to register the task. */ /* Try to register the task. */
R_TRY(KInterruptEventTask::Register(m_interrupt_id, type == ams::svc::InterruptType_Level, this)); R_TRY(KInterruptEventTask::Register(m_interrupt_id, m_core_id, type == ams::svc::InterruptType_Level, this));
/* Mark initialized. */ /* Mark initialized. */
m_is_initialized = true; m_is_initialized = true;
@@ -44,7 +51,7 @@ namespace ams::kern {
void KInterruptEvent::Finalize() { void KInterruptEvent::Finalize() {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
g_interrupt_event_task_table[m_interrupt_id]->Unregister(m_interrupt_id); g_interrupt_event_task_table[m_interrupt_id]->Unregister(m_interrupt_id, m_core_id);
/* Perform inherited finalization. */ /* Perform inherited finalization. */
KAutoObjectWithSlabHeapAndContainer<KInterruptEvent, KReadableEvent>::Finalize(); KAutoObjectWithSlabHeapAndContainer<KInterruptEvent, KReadableEvent>::Finalize();
@@ -60,16 +67,12 @@ namespace ams::kern {
R_TRY(KReadableEvent::Reset()); R_TRY(KReadableEvent::Reset());
/* Clear the interrupt. */ /* Clear the interrupt. */
Kernel::GetInterruptManager().ClearInterrupt(m_interrupt_id); Kernel::GetInterruptManager().ClearInterrupt(m_interrupt_id, m_core_id);
return ResultSuccess(); return ResultSuccess();
} }
Result KInterruptEventTask::Register(s32 interrupt_id, bool level, KInterruptEvent *event) { Result KInterruptEventTask::Register(s32 interrupt_id, s32 core_id, bool level, KInterruptEvent *event) {
/* Verify the interrupt id is defined and global. */
R_UNLESS(Kernel::GetInterruptManager().IsInterruptDefined(interrupt_id), svc::ResultOutOfRange());
R_UNLESS(Kernel::GetInterruptManager().IsGlobal(interrupt_id), svc::ResultOutOfRange());
/* Lock the task table. */ /* Lock the task table. */
KScopedLightLock lk(g_interrupt_event_lock); KScopedLightLock lk(g_interrupt_event_lock);
@@ -96,7 +99,7 @@ namespace ams::kern {
KScopedLightLock tlk(task->m_lock); KScopedLightLock tlk(task->m_lock);
/* Bind the interrupt handler. */ /* Bind the interrupt handler. */
R_TRY(Kernel::GetInterruptManager().BindHandler(task, interrupt_id, GetCurrentCoreId(), KInterruptController::PriorityLevel_High, true, level)); R_TRY(Kernel::GetInterruptManager().BindHandler(task, interrupt_id, core_id, KInterruptController::PriorityLevel_High, true, level));
/* Set the event. */ /* Set the event. */
task->m_event = event; task->m_event = event;
@@ -112,7 +115,7 @@ namespace ams::kern {
return ResultSuccess(); return ResultSuccess();
} }
void KInterruptEventTask::Unregister(s32 interrupt_id) { void KInterruptEventTask::Unregister(s32 interrupt_id, s32 core_id) {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
/* Lock the task table. */ /* Lock the task table. */
@@ -127,7 +130,7 @@ namespace ams::kern {
/* Unbind the interrupt. */ /* Unbind the interrupt. */
m_event = nullptr; m_event = nullptr;
Kernel::GetInterruptManager().UnbindHandler(interrupt_id, GetCurrentCoreId()); Kernel::GetInterruptManager().UnbindHandler(interrupt_id, core_id);
} }
KInterruptTask *KInterruptEventTask::OnInterrupt(s32 interrupt_id) { KInterruptTask *KInterruptEventTask::OnInterrupt(s32 interrupt_id) {

View File

@@ -83,6 +83,9 @@ namespace ams::kern {
/* Do the task. */ /* Do the task. */
task->DoTask(); task->DoTask();
/* Destroy any objects we may need to close. */
m_thread->DestroyClosedObjects();
} }
} }

View File

@@ -35,11 +35,7 @@ namespace ams::kern {
owner_thread->AddWaiter(cur_thread); owner_thread->AddWaiter(cur_thread);
/* Set thread states. */ /* Set thread states. */
if (AMS_LIKELY(cur_thread->GetState() == KThread::ThreadState_Runnable)) { cur_thread->SetState(KThread::ThreadState_Waiting);
cur_thread->SetState(KThread::ThreadState_Waiting);
} else {
KScheduler::SetSchedulerUpdateNeeded();
}
if (owner_thread->IsSuspended()) { if (owner_thread->IsSuspended()) {
owner_thread->ContinueIfHasKernelWaiters(); owner_thread->ContinueIfHasKernelWaiters();
@@ -49,10 +45,9 @@ namespace ams::kern {
/* We're no longer waiting on the lock owner. */ /* We're no longer waiting on the lock owner. */
{ {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
KThread *owner_thread = cur_thread->GetLockOwner();
if (AMS_UNLIKELY(owner_thread)) { if (KThread *owner_thread = cur_thread->GetLockOwner(); AMS_UNLIKELY(owner_thread != nullptr)) {
owner_thread->RemoveWaiter(cur_thread); owner_thread->RemoveWaiter(cur_thread);
KScheduler::SetSchedulerUpdateNeeded();
} }
} }
} }
@@ -70,17 +65,13 @@ namespace ams::kern {
/* Pass the lock to the next owner. */ /* Pass the lock to the next owner. */
uintptr_t next_tag = 0; uintptr_t next_tag = 0;
if (next_owner) { if (next_owner != nullptr) {
next_tag = reinterpret_cast<uintptr_t>(next_owner); next_tag = reinterpret_cast<uintptr_t>(next_owner);
if (num_waiters > 1) { if (num_waiters > 1) {
next_tag |= 0x1; next_tag |= 0x1;
} }
if (AMS_LIKELY(next_owner->GetState() == KThread::ThreadState_Waiting)) { next_owner->SetState(KThread::ThreadState_Runnable);
next_owner->SetState(KThread::ThreadState_Runnable);
} else {
KScheduler::SetSchedulerUpdateNeeded();
}
if (next_owner->IsSuspended()) { if (next_owner->IsSuspended()) {
next_owner->ContinueIfHasKernelWaiters(); next_owner->ContinueIfHasKernelWaiters();

View File

@@ -37,7 +37,7 @@ namespace ams::kern {
/* Create the new region. */ /* Create the new region. */
KMemoryRegion *region = std::addressof(this->region_heap[this->num_regions++]); KMemoryRegion *region = std::addressof(this->region_heap[this->num_regions++]);
new (region) KMemoryRegion(std::forward<Args>(args)...); std::construct_at(region, std::forward<Args>(args)...);
return region; return region;
} }
@@ -170,14 +170,8 @@ namespace ams::kern {
size_t KMemoryLayout::GetResourceRegionSizeForInit() { size_t KMemoryLayout::GetResourceRegionSizeForInit() {
/* Calculate resource region size based on whether we allow extra threads. */ /* Calculate resource region size based on whether we allow extra threads. */
const bool use_extra_resources = KSystemControl::Init::ShouldIncreaseThreadResourceLimit(); const bool use_extra_resources = KSystemControl::Init::ShouldIncreaseThreadResourceLimit();
size_t resource_region_size = KernelResourceSize + (use_extra_resources ? KernelSlabHeapAdditionalSize : 0);
/* 10.0.0 reduced the slab heap gaps by 64K. */ return KernelResourceSize + (use_extra_resources ? KernelSlabHeapAdditionalSize : 0);
if (kern::GetTargetFirmware() < ams::TargetFirmware_10_0_0) {
resource_region_size += (KernelSlabHeapGapsSizeDeprecated - KernelSlabHeapGapsSize);
}
return resource_region_size;
} }
} }

View File

@@ -100,19 +100,48 @@ namespace ams::kern {
} }
/* Free each region to its corresponding heap. */ /* Free each region to its corresponding heap. */
size_t reserved_sizes[MaxManagerCount] = {};
const uintptr_t ini_start = GetInteger(GetInitialProcessBinaryAddress());
const uintptr_t ini_end = ini_start + InitialProcessBinarySizeMax;
const uintptr_t ini_last = ini_end - 1;
for (const auto &it : KMemoryLayout::GetVirtualMemoryRegionTree()) { for (const auto &it : KMemoryLayout::GetVirtualMemoryRegionTree()) {
if (it.IsDerivedFrom(KMemoryRegionType_VirtualDramUserPool)) { if (it.IsDerivedFrom(KMemoryRegionType_VirtualDramUserPool)) {
/* Check the region. */ /* Get the manager for the region. */
MESOSPHERE_ABORT_UNLESS(it.GetEndAddress() != 0); auto &manager = m_managers[it.GetAttributes()];
/* Free the memory to the heap. */ if (it.GetAddress() <= ini_start && ini_last <= it.GetLastAddress()) {
m_managers[it.GetAttributes()].Free(it.GetAddress(), it.GetSize() / PageSize); /* Free memory before the ini to the heap. */
if (it.GetAddress() != ini_start) {
manager.Free(it.GetAddress(), (ini_start - it.GetAddress()) / PageSize);
}
/* Open/reserve the ini memory. */
manager.OpenFirst(ini_start, InitialProcessBinarySizeMax / PageSize);
reserved_sizes[it.GetAttributes()] += InitialProcessBinarySizeMax;
/* Free memory after the ini to the heap. */
if (ini_last != it.GetLastAddress()) {
MESOSPHERE_ABORT_UNLESS(it.GetEndAddress() != 0);
manager.Free(ini_end, it.GetEndAddress() - ini_end);
}
} else {
/* Ensure there's no partial overlap with the ini image. */
if (it.GetAddress() <= ini_last) {
MESOSPHERE_ABORT_UNLESS(it.GetLastAddress() < ini_start);
} else {
/* Otherwise, check the region for general validity. */
MESOSPHERE_ABORT_UNLESS(it.GetEndAddress() != 0);
}
/* Free the memory to the heap. */
manager.Free(it.GetAddress(), it.GetSize() / PageSize);
}
} }
} }
/* Update the used size for all managers. */ /* Update the used size for all managers. */
for (size_t i = 0; i < m_num_managers; ++i) { for (size_t i = 0; i < m_num_managers; ++i) {
m_managers[i].UpdateUsedHeapSize(); m_managers[i].SetInitialUsedHeapSize(reserved_sizes[i]);
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -28,12 +28,19 @@ namespace ams::kern {
std::atomic<u64> g_initial_process_id = InitialProcessIdMin; std::atomic<u64> g_initial_process_id = InitialProcessIdMin;
std::atomic<u64> g_process_id = ProcessIdMin; std::atomic<u64> g_process_id = ProcessIdMin;
void TerminateChildren(KProcess *process, const KThread *thread_to_not_terminate) { Result TerminateChildren(KProcess *process, const KThread *thread_to_not_terminate) {
/* Request that all children threads terminate. */ /* Request that all children threads terminate. */
{ {
KScopedLightLock proc_lk(process->GetListLock()); KScopedLightLock proc_lk(process->GetListLock());
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
if (thread_to_not_terminate != nullptr && process->GetPinnedThread(GetCurrentCoreId()) == thread_to_not_terminate) {
/* NOTE: Here Nintendo unpins the current thread instead of the thread_to_not_terminate. */
/* This is valid because the only caller which uses non-nullptr as argument uses GetCurrentThreadPointer(), */
/* but it's still notable because it seems incorrect at first glance. */
process->UnpinCurrentThread();
}
auto &thread_list = process->GetThreadList(); auto &thread_list = process->GetThreadList();
for (auto it = thread_list.begin(); it != thread_list.end(); ++it) { for (auto it = thread_list.begin(); it != thread_list.end(); ++it) {
if (KThread *thread = std::addressof(*it); thread != thread_to_not_terminate) { if (KThread *thread = std::addressof(*it); thread != thread_to_not_terminate) {
@@ -70,9 +77,14 @@ namespace ams::kern {
} }
/* Terminate and close the thread. */ /* Terminate and close the thread. */
cur_child->Terminate(); ON_SCOPE_EXIT { cur_child->Close(); };
cur_child->Close();
if (Result terminate_result = cur_child->Terminate(); svc::ResultTerminationRequested::Includes(terminate_result)) {
return terminate_result;
}
} }
return ResultSuccess();
} }
} }
@@ -206,21 +218,20 @@ namespace ams::kern {
KSystemControl::GenerateRandomBytes(m_entropy, sizeof(m_entropy)); KSystemControl::GenerateRandomBytes(m_entropy, sizeof(m_entropy));
/* Clear remaining fields. */ /* Clear remaining fields. */
m_num_threads = 0; m_num_running_threads = 0;
m_peak_num_threads = 0; m_num_process_switches = 0;
m_num_created_threads = 0; m_num_thread_switches = 0;
m_num_process_switches = 0; m_num_fpu_switches = 0;
m_num_thread_switches = 0; m_num_supervisor_calls = 0;
m_num_fpu_switches = 0; m_num_ipc_messages = 0;
m_num_supervisor_calls = 0;
m_num_ipc_messages = 0;
m_is_signaled = false; m_is_signaled = false;
m_attached_object = nullptr; m_attached_object = nullptr;
m_exception_thread = nullptr; m_exception_thread = nullptr;
m_is_suspended = false; m_is_suspended = false;
m_memory_release_hint = 0; m_memory_release_hint = 0;
m_schedule_count = 0; m_schedule_count = 0;
m_is_handle_table_initialized = false;
/* We're initialized! */ /* We're initialized! */
m_is_initialized = true; m_is_initialized = true;
@@ -228,7 +239,7 @@ namespace ams::kern {
return ResultSuccess(); return ResultSuccess();
} }
Result KProcess::Initialize(const ams::svc::CreateProcessParameter &params, const KPageGroup &pg, const u32 *caps, s32 num_caps, KResourceLimit *res_limit, KMemoryManager::Pool pool) { Result KProcess::Initialize(const ams::svc::CreateProcessParameter &params, const KPageGroup &pg, const u32 *caps, s32 num_caps, KResourceLimit *res_limit, KMemoryManager::Pool pool, bool immortal) {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(res_limit != nullptr); MESOSPHERE_ASSERT(res_limit != nullptr);
MESOSPHERE_ABORT_UNLESS((params.code_num_pages * PageSize) / PageSize == static_cast<size_t>(params.code_num_pages)); MESOSPHERE_ABORT_UNLESS((params.code_num_pages * PageSize) / PageSize == static_cast<size_t>(params.code_num_pages));
@@ -238,6 +249,7 @@ namespace ams::kern {
m_resource_limit = res_limit; m_resource_limit = res_limit;
m_system_resource_address = Null<KVirtualAddress>; m_system_resource_address = Null<KVirtualAddress>;
m_system_resource_num_pages = 0; m_system_resource_num_pages = 0;
m_is_immortal = immortal;
/* Setup page table. */ /* Setup page table. */
/* NOTE: Nintendo passes process ID despite not having set it yet. */ /* NOTE: Nintendo passes process ID despite not having set it yet. */
@@ -250,7 +262,7 @@ namespace ams::kern {
auto *mem_block_manager = std::addressof(is_app ? Kernel::GetApplicationMemoryBlockManager() : Kernel::GetSystemMemoryBlockManager()); auto *mem_block_manager = std::addressof(is_app ? Kernel::GetApplicationMemoryBlockManager() : Kernel::GetSystemMemoryBlockManager());
auto *block_info_manager = std::addressof(Kernel::GetBlockInfoManager()); auto *block_info_manager = std::addressof(Kernel::GetBlockInfoManager());
auto *pt_manager = std::addressof(Kernel::GetPageTableManager()); auto *pt_manager = std::addressof(Kernel::GetPageTableManager());
R_TRY(m_page_table.Initialize(m_process_id, as_type, enable_aslr, enable_das_merge, !enable_aslr, pool, params.code_address, params.code_num_pages * PageSize, mem_block_manager, block_info_manager, pt_manager)); R_TRY(m_page_table.Initialize(m_process_id, as_type, enable_aslr, enable_das_merge, !enable_aslr, pool, params.code_address, params.code_num_pages * PageSize, mem_block_manager, block_info_manager, pt_manager, res_limit));
} }
auto pt_guard = SCOPE_GUARD { m_page_table.Finalize(); }; auto pt_guard = SCOPE_GUARD { m_page_table.Finalize(); };
@@ -286,6 +298,7 @@ namespace ams::kern {
/* Set pool and resource limit. */ /* Set pool and resource limit. */
m_memory_pool = pool; m_memory_pool = pool;
m_resource_limit = res_limit; m_resource_limit = res_limit;
m_is_immortal = false;
/* Get the memory sizes. */ /* Get the memory sizes. */
const size_t code_num_pages = params.code_num_pages; const size_t code_num_pages = params.code_num_pages;
@@ -354,7 +367,7 @@ namespace ams::kern {
const auto as_type = static_cast<ams::svc::CreateProcessFlag>(params.flags & ams::svc::CreateProcessFlag_AddressSpaceMask); const auto as_type = static_cast<ams::svc::CreateProcessFlag>(params.flags & ams::svc::CreateProcessFlag_AddressSpaceMask);
const bool enable_aslr = (params.flags & ams::svc::CreateProcessFlag_EnableAslr) != 0; const bool enable_aslr = (params.flags & ams::svc::CreateProcessFlag_EnableAslr) != 0;
const bool enable_das_merge = (params.flags & ams::svc::CreateProcessFlag_DisableDeviceAddressSpaceMerge) == 0; const bool enable_das_merge = (params.flags & ams::svc::CreateProcessFlag_DisableDeviceAddressSpaceMerge) == 0;
R_TRY(m_page_table.Initialize(m_process_id, as_type, enable_aslr, enable_das_merge, !enable_aslr, pool, params.code_address, code_size, mem_block_manager, block_info_manager, pt_manager)); R_TRY(m_page_table.Initialize(m_process_id, as_type, enable_aslr, enable_das_merge, !enable_aslr, pool, params.code_address, code_size, mem_block_manager, block_info_manager, pt_manager, res_limit));
} }
auto pt_guard = SCOPE_GUARD { m_page_table.Finalize(); }; auto pt_guard = SCOPE_GUARD { m_page_table.Finalize(); };
@@ -395,6 +408,11 @@ namespace ams::kern {
/* Terminate child threads. */ /* Terminate child threads. */
TerminateChildren(this, nullptr); TerminateChildren(this, nullptr);
/* Finalize the handle table, if we're not immortal. */
if (!m_is_immortal && m_is_handle_table_initialized) {
this->FinalizeHandleTable();
}
/* Call the debug callback. */ /* Call the debug callback. */
KDebug::OnExitProcess(this); KDebug::OnExitProcess(this);
@@ -402,29 +420,36 @@ namespace ams::kern {
this->FinishTermination(); this->FinishTermination();
} }
void KProcess::StartTermination() { Result KProcess::StartTermination() {
/* Terminate child threads other than the current one. */ /* Finalize the handle table when we're done, if the process isn't immortal. */
TerminateChildren(this, GetCurrentThreadPointer()); ON_SCOPE_EXIT {
if (!m_is_immortal) {
this->FinalizeHandleTable();
}
};
/* Finalize the handle tahble. */ /* Terminate child threads other than the current one. */
m_handle_table.Finalize(); return TerminateChildren(this, GetCurrentThreadPointer());
} }
void KProcess::FinishTermination() { void KProcess::FinishTermination() {
/* Release resource limit hint. */ /* Only allow termination to occur if the process isn't immortal. */
if (m_resource_limit != nullptr) { if (!m_is_immortal) {
m_memory_release_hint = this->GetUsedUserPhysicalMemorySize(); /* Release resource limit hint. */
m_resource_limit->Release(ams::svc::LimitableResource_PhysicalMemoryMax, 0, m_memory_release_hint); if (m_resource_limit != nullptr) {
} m_memory_release_hint = this->GetUsedUserPhysicalMemorySize();
m_resource_limit->Release(ams::svc::LimitableResource_PhysicalMemoryMax, 0, m_memory_release_hint);
}
/* Change state. */ /* Change state. */
{ {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
this->ChangeState(State_Terminated); this->ChangeState(State_Terminated);
} }
/* Close. */ /* Close. */
this->Close(); this->Close();
}
} }
void KProcess::Exit() { void KProcess::Exit() {
@@ -485,16 +510,22 @@ namespace ams::kern {
/* If we need to terminate, do so. */ /* If we need to terminate, do so. */
if (needs_terminate) { if (needs_terminate) {
/* Start termination. */ /* Start termination. */
this->StartTermination(); if (R_SUCCEEDED(this->StartTermination())) {
/* Note for debug that we're terminating the process. */
MESOSPHERE_LOG("KProcess::Terminate() OK pid=%ld name=%-12s\n", m_process_id, m_name);
/* Note for debug that we're terminating the process. */ /* Call the debug callback. */
MESOSPHERE_LOG("KProcess::Terminate() pid=%ld name=%-12s\n", m_process_id, m_name); KDebug::OnTerminateProcess(this);
/* Call the debug callback. */ /* Finish termination. */
KDebug::OnTerminateProcess(this); this->FinishTermination();
} else {
/* Note for debug that we're terminating the process. */
MESOSPHERE_LOG("KProcess::Terminate() FAIL pid=%ld name=%-12s\n", m_process_id, m_name);
/* Finish termination. */ /* Register the process as a work task. */
this->FinishTermination(); KWorkerTaskManager::AddTask(KWorkerTaskManager::WorkerType_Exit, this);
}
} }
return ResultSuccess(); return ResultSuccess();
@@ -703,19 +734,16 @@ namespace ams::kern {
} }
} }
void KProcess::IncrementThreadCount() { void KProcess::IncrementRunningThreadCount() {
MESOSPHERE_ASSERT(m_num_threads >= 0); MESOSPHERE_ASSERT(m_num_running_threads.load() >= 0);
++m_num_created_threads;
if (const auto count = ++m_num_threads; count > m_peak_num_threads) { m_num_running_threads.fetch_add(1);
m_peak_num_threads = count;
}
} }
void KProcess::DecrementThreadCount() { void KProcess::DecrementRunningThreadCount() {
MESOSPHERE_ASSERT(m_num_threads > 0); MESOSPHERE_ASSERT(m_num_running_threads.load() > 0);
if (const auto count = --m_num_threads; count == 0) { if (m_num_running_threads.fetch_sub(1) == 1) {
this->Terminate(); this->Terminate();
} }
} }
@@ -740,25 +768,22 @@ namespace ams::kern {
/* If we have no exception thread, we succeeded. */ /* If we have no exception thread, we succeeded. */
if (m_exception_thread == nullptr) { if (m_exception_thread == nullptr) {
m_exception_thread = cur_thread; m_exception_thread = cur_thread;
KScheduler::SetSchedulerUpdateNeeded();
return true; return true;
} }
/* Otherwise, wait for us to not have an exception thread. */ /* Otherwise, wait for us to not have an exception thread. */
cur_thread->SetAddressKey(address_key); cur_thread->SetAddressKey(address_key | 1);
m_exception_thread->AddWaiter(cur_thread); m_exception_thread->AddWaiter(cur_thread);
if (cur_thread->GetState() == KThread::ThreadState_Runnable) { cur_thread->SetState(KThread::ThreadState_Waiting);
cur_thread->SetState(KThread::ThreadState_Waiting);
} else {
KScheduler::SetSchedulerUpdateNeeded();
}
} }
/* Remove the thread as a waiter from the lock owner. */ /* Remove the thread as a waiter from the lock owner. */
{ {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
KThread *owner_thread = cur_thread->GetLockOwner();
if (owner_thread != nullptr) { if (KThread *owner_thread = cur_thread->GetLockOwner(); owner_thread != nullptr) {
owner_thread->RemoveWaiter(cur_thread); owner_thread->RemoveWaiter(cur_thread);
KScheduler::SetSchedulerUpdateNeeded();
} }
} }
} }
@@ -779,15 +804,12 @@ namespace ams::kern {
/* Remove waiter thread. */ /* Remove waiter thread. */
s32 num_waiters; s32 num_waiters;
KThread *next = thread->RemoveWaiterByKey(std::addressof(num_waiters), reinterpret_cast<uintptr_t>(std::addressof(m_exception_thread))); if (KThread *next = thread->RemoveWaiterByKey(std::addressof(num_waiters), reinterpret_cast<uintptr_t>(std::addressof(m_exception_thread))); next != nullptr) {
if (next != nullptr) { next->SetState(KThread::ThreadState_Runnable);
if (next->GetState() == KThread::ThreadState_Waiting) {
next->SetState(KThread::ThreadState_Runnable);
} else {
KScheduler::SetSchedulerUpdateNeeded();
}
} }
KScheduler::SetSchedulerUpdateNeeded();
return true; return true;
} else { } else {
return false; return false;
@@ -896,13 +918,13 @@ namespace ams::kern {
R_TRY(m_page_table.SetMaxHeapSize(m_max_process_memory - (m_main_thread_stack_size + m_code_size))); R_TRY(m_page_table.SetMaxHeapSize(m_max_process_memory - (m_main_thread_stack_size + m_code_size)));
/* Initialize our handle table. */ /* Initialize our handle table. */
R_TRY(m_handle_table.Initialize(m_capabilities.GetHandleTableSize())); R_TRY(this->InitializeHandleTable(m_capabilities.GetHandleTableSize()));
auto ht_guard = SCOPE_GUARD { m_handle_table.Finalize(); }; auto ht_guard = SCOPE_GUARD { this->FinalizeHandleTable(); };
/* Create a new thread for the process. */ /* Create a new thread for the process. */
KThread *main_thread = KThread::Create(); KThread *main_thread = KThread::Create();
R_UNLESS(main_thread != nullptr, svc::ResultOutOfResource()); R_UNLESS(main_thread != nullptr, svc::ResultOutOfResource());
auto thread_guard = SCOPE_GUARD { main_thread->Close(); }; ON_SCOPE_EXIT { main_thread->Close(); };
/* Initialize the thread. */ /* Initialize the thread. */
R_TRY(KThread::InitializeUserThread(main_thread, reinterpret_cast<KThreadFunction>(GetVoidPointer(this->GetEntryPoint())), 0, stack_top, priority, m_ideal_core_id, this)); R_TRY(KThread::InitializeUserThread(main_thread, reinterpret_cast<KThreadFunction>(GetVoidPointer(this->GetEntryPoint())), 0, stack_top, priority, m_ideal_core_id, this));
@@ -925,9 +947,11 @@ namespace ams::kern {
/* Run our thread. */ /* Run our thread. */
R_TRY(main_thread->Run()); R_TRY(main_thread->Run());
/* Open a reference to represent that we're running. */
this->Open();
/* We succeeded! Cancel our guards. */ /* We succeeded! Cancel our guards. */
state_guard.Cancel(); state_guard.Cancel();
thread_guard.Cancel();
ht_guard.Cancel(); ht_guard.Cancel();
stack_guard.Cancel(); stack_guard.Cancel();
mem_reservation.Commit(); mem_reservation.Commit();
@@ -1003,12 +1027,15 @@ namespace ams::kern {
const s32 core_id = GetCurrentCoreId(); const s32 core_id = GetCurrentCoreId();
KThread *cur_thread = GetCurrentThreadPointer(); KThread *cur_thread = GetCurrentThreadPointer();
/* Pin it. */ /* If the thread isn't terminated, pin it. */
this->PinThread(core_id, cur_thread); if (!cur_thread->IsTerminationRequested()) {
cur_thread->Pin(); /* Pin it. */
this->PinThread(core_id, cur_thread);
cur_thread->Pin();
/* An update is needed. */ /* An update is needed. */
KScheduler::SetSchedulerUpdateNeeded(); KScheduler::SetSchedulerUpdateNeeded();
}
} }
void KProcess::UnpinCurrentThread() { void KProcess::UnpinCurrentThread() {
@@ -1026,6 +1053,20 @@ namespace ams::kern {
KScheduler::SetSchedulerUpdateNeeded(); KScheduler::SetSchedulerUpdateNeeded();
} }
void KProcess::UnpinThread(KThread *thread) {
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
/* Get the thread's core id. */
const auto core_id = thread->GetActiveCore();
/* Unpin it. */
this->UnpinThread(core_id, thread);
thread->Unpin();
/* An update is needed. */
KScheduler::SetSchedulerUpdateNeeded();
}
Result KProcess::GetThreadList(s32 *out_num_threads, ams::kern::svc::KUserPointer<u64 *> out_thread_ids, s32 max_out_count) { Result KProcess::GetThreadList(s32 *out_num_threads, ams::kern::svc::KUserPointer<u64 *> out_thread_ids, s32 max_out_count) {
/* Lock the list. */ /* Lock the list. */
KScopedLightLock lk(m_list_lock); KScopedLightLock lk(m_list_lock);

View File

@@ -146,8 +146,12 @@ namespace ams::kern {
if (m_current_hints[which] + value <= m_limit_values[which] && (timeout < 0 || KHardwareTimer::GetTick() < timeout)) { if (m_current_hints[which] + value <= m_limit_values[which] && (timeout < 0 || KHardwareTimer::GetTick() < timeout)) {
m_waiter_count++; m_waiter_count++;
m_cond_var.Wait(&m_lock, timeout); m_cond_var.Wait(&m_lock, timeout, false);
m_waiter_count--; m_waiter_count--;
if (GetCurrentThread().IsTerminationRequested()) {
return false;
}
} else { } else {
break; break;
} }

View File

@@ -218,9 +218,7 @@ namespace ams::kern {
KThread *task_thread = Kernel::GetInterruptTaskManager().GetThread(); KThread *task_thread = Kernel::GetInterruptTaskManager().GetThread();
{ {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
if (AMS_LIKELY(task_thread->GetState() == KThread::ThreadState_Waiting)) { task_thread->SetState(KThread::ThreadState_Runnable);
task_thread->SetState(KThread::ThreadState_Runnable);
}
} }
} }

View File

@@ -40,7 +40,7 @@ namespace ams::kern {
KServerSession *session = nullptr; KServerSession *session = nullptr;
{ {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
while (!m_session_list.empty()) { if (!m_session_list.empty()) {
session = std::addressof(m_session_list.front()); session = std::addressof(m_session_list.front());
m_session_list.pop_front(); m_session_list.pop_front();
} }
@@ -60,7 +60,7 @@ namespace ams::kern {
KLightServerSession *session = nullptr; KLightServerSession *session = nullptr;
{ {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
while (!m_light_session_list.empty()) { if (!m_light_session_list.empty()) {
session = std::addressof(m_light_session_list.front()); session = std::addressof(m_light_session_list.front());
m_light_session_list.pop_front(); m_light_session_list.pop_front();
} }

View File

@@ -398,17 +398,17 @@ namespace ams::kern {
/* Cleanup Send mappings. */ /* Cleanup Send mappings. */
for (size_t i = 0; i < request->GetSendCount(); ++i) { for (size_t i = 0; i < request->GetSendCount(); ++i) {
R_TRY(server_page_table.CleanupForIpcServer(request->GetSendServerAddress(i), request->GetSendSize(i), request->GetSendMemoryState(i), server_process)); R_TRY(server_page_table.CleanupForIpcServer(request->GetSendServerAddress(i), request->GetSendSize(i), request->GetSendMemoryState(i)));
} }
/* Cleanup Receive mappings. */ /* Cleanup Receive mappings. */
for (size_t i = 0; i < request->GetReceiveCount(); ++i) { for (size_t i = 0; i < request->GetReceiveCount(); ++i) {
R_TRY(server_page_table.CleanupForIpcServer(request->GetReceiveServerAddress(i), request->GetReceiveSize(i), request->GetReceiveMemoryState(i), server_process)); R_TRY(server_page_table.CleanupForIpcServer(request->GetReceiveServerAddress(i), request->GetReceiveSize(i), request->GetReceiveMemoryState(i)));
} }
/* Cleanup Exchange mappings. */ /* Cleanup Exchange mappings. */
for (size_t i = 0; i < request->GetExchangeCount(); ++i) { for (size_t i = 0; i < request->GetExchangeCount(); ++i) {
R_TRY(server_page_table.CleanupForIpcServer(request->GetExchangeServerAddress(i), request->GetExchangeSize(i), request->GetExchangeMemoryState(i), server_process)); R_TRY(server_page_table.CleanupForIpcServer(request->GetExchangeServerAddress(i), request->GetExchangeSize(i), request->GetExchangeMemoryState(i)));
} }
return ResultSuccess(); return ResultSuccess();
@@ -470,7 +470,7 @@ namespace ams::kern {
/* Ensure that we clean up on failure. */ /* Ensure that we clean up on failure. */
auto setup_guard = SCOPE_GUARD { auto setup_guard = SCOPE_GUARD {
dst_page_table.CleanupForIpcServer(dst_address, size, dst_state, request->GetServerProcess()); dst_page_table.CleanupForIpcServer(dst_address, size, dst_state);
src_page_table.CleanupForIpcClient(src_address, size, dst_state); src_page_table.CleanupForIpcClient(src_address, size, dst_state);
}; };
@@ -771,6 +771,9 @@ namespace ams::kern {
/* NOTE: Session is used only for debugging, and so may go unused. */ /* NOTE: Session is used only for debugging, and so may go unused. */
MESOSPHERE_UNUSED(session); MESOSPHERE_UNUSED(session);
/* NOTE: Source page table is not used, and so may go unused. */
MESOSPHERE_UNUSED(src_page_table);
/* Determine the message buffers. */ /* Determine the message buffers. */
u32 *dst_msg_ptr, *src_msg_ptr; u32 *dst_msg_ptr, *src_msg_ptr;
bool dst_user, src_user; bool dst_user, src_user;
@@ -907,7 +910,7 @@ namespace ams::kern {
/* If the fast part of the copy didn't get everything, perform the slow part of the copy. */ /* If the fast part of the copy didn't get everything, perform the slow part of the copy. */
if (fast_size < raw_size) { if (fast_size < raw_size) {
R_TRY(src_page_table.CopyMemoryFromHeapToHeap(dst_page_table, dst_message_buffer + max_fast_size, raw_size - fast_size, R_TRY(dst_page_table.CopyMemoryFromHeapToHeap(dst_page_table, dst_message_buffer + max_fast_size, raw_size - fast_size,
KMemoryState_FlagReferenceCounted, KMemoryState_FlagReferenceCounted, KMemoryState_FlagReferenceCounted, KMemoryState_FlagReferenceCounted,
dst_perm, dst_perm,
KMemoryAttribute_Uncached, KMemoryAttribute_None, KMemoryAttribute_Uncached, KMemoryAttribute_None,
@@ -921,7 +924,7 @@ namespace ams::kern {
constexpr KMemoryPermission DestinationPermission = static_cast<KMemoryPermission>(KMemoryPermission_NotMapped | KMemoryPermission_KernelReadWrite); constexpr KMemoryPermission DestinationPermission = static_cast<KMemoryPermission>(KMemoryPermission_NotMapped | KMemoryPermission_KernelReadWrite);
/* Copy the memory. */ /* Copy the memory. */
R_TRY(src_page_table.CopyMemoryFromUserToLinear(dst_message_buffer + offset_words, raw_size, R_TRY(dst_page_table.CopyMemoryFromUserToLinear(dst_message_buffer + offset_words, raw_size,
KMemoryState_FlagReferenceCounted, KMemoryState_FlagReferenceCounted, KMemoryState_FlagReferenceCounted, KMemoryState_FlagReferenceCounted,
DestinationPermission, DestinationPermission,
KMemoryAttribute_Uncached, KMemoryAttribute_None, KMemoryAttribute_Uncached, KMemoryAttribute_None,

View File

@@ -35,7 +35,7 @@ namespace ams::kern {
m_client.Initialize(this); m_client.Initialize(this);
/* Set state and name. */ /* Set state and name. */
m_state = State::Normal; this->SetState(State::Normal);
m_name = name; m_name = name;
/* Set our owner process. */ /* Set our owner process. */
@@ -62,8 +62,8 @@ namespace ams::kern {
void KSession::OnServerClosed() { void KSession::OnServerClosed() {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
if (m_state == State::Normal) { if (this->GetState() == State::Normal) {
m_state = State::ServerClosed; this->SetState(State::ServerClosed);
m_client.OnServerClosed(); m_client.OnServerClosed();
} }
} }
@@ -71,8 +71,8 @@ namespace ams::kern {
void KSession::OnClientClosed() { void KSession::OnClientClosed() {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
if (m_state == State::Normal) { if (this->GetState() == State::Normal) {
m_state = State::ClientClosed; this->SetState(State::ClientClosed);
m_server.OnClientClosed(); m_server.OnClientClosed();
} }
} }

View File

@@ -19,9 +19,11 @@ namespace ams::kern {
namespace { namespace {
constexpr inline s32 TerminatingThreadPriority = ams::svc::SystemThreadPriorityHighest - 1;
constexpr bool IsKernelAddressKey(KProcessAddress key) { constexpr bool IsKernelAddressKey(KProcessAddress key) {
const uintptr_t key_uptr = GetInteger(key); const uintptr_t key_uptr = GetInteger(key);
return KernelVirtualAddressSpaceBase <= key_uptr && key_uptr <= KernelVirtualAddressSpaceLast; return KernelVirtualAddressSpaceBase <= key_uptr && key_uptr <= KernelVirtualAddressSpaceLast && (key_uptr & 1) == 0;
} }
void InitializeKernelStack(uintptr_t stack_top) { void InitializeKernelStack(uintptr_t stack_top) {
@@ -153,8 +155,9 @@ namespace ams::kern {
m_lock_owner = nullptr; m_lock_owner = nullptr;
m_num_core_migration_disables = 0; m_num_core_migration_disables = 0;
/* We have no waiters, but we do have an entrypoint. */ /* We have no waiters, and no closed objects. */
m_num_kernel_waiters = 0; m_num_kernel_waiters = 0;
m_closed_object = nullptr;
/* Set our current core id. */ /* Set our current core id. */
m_current_core_id = phys_core; m_current_core_id = phys_core;
@@ -182,7 +185,6 @@ namespace ams::kern {
if (owner != nullptr) { if (owner != nullptr) {
m_parent = owner; m_parent = owner;
m_parent->Open(); m_parent->Open();
m_parent->IncrementThreadCount();
} }
/* Initialize thread context. */ /* Initialize thread context. */
@@ -310,11 +312,6 @@ namespace ams::kern {
CleanupKernelStack(reinterpret_cast<uintptr_t>(m_kernel_stack_top)); CleanupKernelStack(reinterpret_cast<uintptr_t>(m_kernel_stack_top));
} }
/* Decrement the parent process's thread count. */
if (m_parent != nullptr) {
m_parent->DecrementThreadCount();
}
/* Perform inherited finalization. */ /* Perform inherited finalization. */
KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>::Finalize(); KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>::Finalize();
} }
@@ -426,6 +423,14 @@ namespace ams::kern {
if (active_core != current_core || m_physical_affinity_mask.GetAffinityMask() != m_original_physical_affinity_mask.GetAffinityMask()) { if (active_core != current_core || m_physical_affinity_mask.GetAffinityMask() != m_original_physical_affinity_mask.GetAffinityMask()) {
KScheduler::OnThreadAffinityMaskChanged(this, m_original_physical_affinity_mask, active_core); KScheduler::OnThreadAffinityMaskChanged(this, m_original_physical_affinity_mask, active_core);
} }
/* Set base priority-on-unpin. */
const s32 old_base_priority = m_base_priority;
m_base_priority_on_unpin = old_base_priority;
/* Set base priority to higher than any possible process priority. */
m_base_priority = std::min<s32>(old_base_priority, __builtin_ctzll(this->GetOwnerProcess()->GetPriorityMask()));
RestorePriority(this);
} }
/* Disallow performing thread suspension. */ /* Disallow performing thread suspension. */
@@ -434,11 +439,7 @@ namespace ams::kern {
m_suspend_allowed_flags &= ~(1 << (SuspendType_Thread + ThreadState_SuspendShift)); m_suspend_allowed_flags &= ~(1 << (SuspendType_Thread + ThreadState_SuspendShift));
/* Update our state. */ /* Update our state. */
const ThreadState old_state = m_thread_state; this->UpdateState();
m_thread_state = static_cast<ThreadState>(this->GetSuspendFlags() | (old_state & ThreadState_Mask));
if (m_thread_state != old_state) {
KScheduler::OnThreadStateChanged(this, old_state);
}
} }
/* Update our SVC access permissions. */ /* Update our SVC access permissions. */
@@ -476,26 +477,23 @@ namespace ams::kern {
} }
KScheduler::OnThreadAffinityMaskChanged(this, old_mask, active_core); KScheduler::OnThreadAffinityMaskChanged(this, old_mask, active_core);
} }
m_base_priority = m_base_priority_on_unpin;
RestorePriority(this);
} }
/* Allow performing thread suspension (if termination hasn't been requested). */ /* Allow performing thread suspension (if termination hasn't been requested). */
{ if (!this->IsTerminationRequested()) {
/* Update our allow flags. */ /* Update our allow flags. */
if (!this->IsTerminationRequested()) { m_suspend_allowed_flags |= (1 << (SuspendType_Thread + ThreadState_SuspendShift));
m_suspend_allowed_flags |= (1 << (SuspendType_Thread + ThreadState_SuspendShift));
}
/* Update our state. */ /* Update our state. */
const ThreadState old_state = m_thread_state; this->UpdateState();
m_thread_state = static_cast<ThreadState>(this->GetSuspendFlags() | (old_state & ThreadState_Mask));
if (m_thread_state != old_state) {
KScheduler::OnThreadStateChanged(this, old_state);
}
}
/* Update our SVC access permissions. */ /* Update our SVC access permissions. */
MESOSPHERE_ASSERT(m_parent != nullptr); MESOSPHERE_ASSERT(m_parent != nullptr);
m_parent->CopyUnpinnedSvcPermissionsTo(this->GetStackParameters()); m_parent->CopyUnpinnedSvcPermissionsTo(this->GetStackParameters());
}
/* Resume any threads that began waiting on us while we were pinned. */ /* Resume any threads that began waiting on us while we were pinned. */
for (auto it = m_pinned_waiter_list.begin(); it != m_pinned_waiter_list.end(); ++it) { for (auto it = m_pinned_waiter_list.begin(); it != m_pinned_waiter_list.end(); ++it) {
@@ -599,14 +597,16 @@ namespace ams::kern {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
MESOSPHERE_ASSERT(m_num_core_migration_disables >= 0); MESOSPHERE_ASSERT(m_num_core_migration_disables >= 0);
/* If the core id is no-update magic, preserve the ideal core id. */ /* If we're updating, set our ideal virtual core. */
if (core_id == ams::svc::IdealCoreNoUpdate) { if (core_id != ams::svc::IdealCoreNoUpdate) {
m_virtual_ideal_core_id = core_id;
} else {
/* Preserve our ideal core id. */
core_id = m_virtual_ideal_core_id; core_id = m_virtual_ideal_core_id;
R_UNLESS(((1ul << core_id) & v_affinity_mask) != 0, svc::ResultInvalidCombination()); R_UNLESS(((1ul << core_id) & v_affinity_mask) != 0, svc::ResultInvalidCombination());
} }
/* Set the virtual core/affinity mask. */ /* Set our affinity mask. */
m_virtual_ideal_core_id = core_id;
m_virtual_affinity_mask = v_affinity_mask; m_virtual_affinity_mask = v_affinity_mask;
/* Translate the virtual core to a physical core. */ /* Translate the virtual core to a physical core. */
@@ -708,13 +708,38 @@ namespace ams::kern {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
/* Determine the priority value to use. */
const s32 target_priority = m_termination_requested.load() && priority >= TerminatingThreadPriority ? TerminatingThreadPriority : priority;
/* Change our base priority. */ /* Change our base priority. */
m_base_priority = priority; if (this->GetStackParameters().is_pinned) {
m_base_priority_on_unpin = target_priority;
} else {
m_base_priority = target_priority;
}
/* Perform a priority restoration. */ /* Perform a priority restoration. */
RestorePriority(this); RestorePriority(this);
} }
void KThread::IncreaseBasePriority(s32 priority) {
MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(ams::svc::HighestThreadPriority <= priority && priority <= ams::svc::LowestThreadPriority);
/* Set our unpin base priority, if we're pinned. */
if (this->GetStackParameters().is_pinned && m_base_priority_on_unpin > priority) {
m_base_priority_on_unpin = priority;
}
/* Set our base priority. */
if (m_base_priority > priority) {
m_base_priority = priority;
/* Perform a priority restoration. */
RestorePriority(this);
}
}
Result KThread::SetPriorityToIdle() { Result KThread::SetPriorityToIdle() {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
@@ -750,11 +775,7 @@ namespace ams::kern {
m_suspend_request_flags &= ~(1u << (ThreadState_SuspendShift + type)); m_suspend_request_flags &= ~(1u << (ThreadState_SuspendShift + type));
/* Update our state. */ /* Update our state. */
const ThreadState old_state = m_thread_state; this->UpdateState();
m_thread_state = static_cast<ThreadState>(this->GetSuspendFlags() | (old_state & ThreadState_Mask));
if (m_thread_state != old_state) {
KScheduler::OnThreadStateChanged(this, old_state);
}
} }
void KThread::WaitCancel() { void KThread::WaitCancel() {
@@ -790,20 +811,22 @@ namespace ams::kern {
MESOSPHERE_ABORT_UNLESS(this->GetNumKernelWaiters() == 0); MESOSPHERE_ABORT_UNLESS(this->GetNumKernelWaiters() == 0);
/* Perform the suspend. */ /* Perform the suspend. */
this->Suspend(); this->UpdateState();
} }
void KThread::Suspend() { void KThread::UpdateState() {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread()); MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
MESOSPHERE_ASSERT(this->IsSuspendRequested());
/* Set our suspend flags in state. */ /* Set our suspend flags in state. */
const auto old_state = m_thread_state; const auto old_state = m_thread_state;
m_thread_state = static_cast<ThreadState>(this->GetSuspendFlags() | (old_state & ThreadState_Mask)); const auto new_state = static_cast<ThreadState>(this->GetSuspendFlags() | (old_state & ThreadState_Mask));
m_thread_state = new_state;
/* Note the state change in scheduler. */ /* Note the state change in scheduler. */
KScheduler::OnThreadStateChanged(this, old_state); if (new_state != old_state) {
KScheduler::OnThreadStateChanged(this, old_state);
}
} }
void KThread::Continue() { void KThread::Continue() {
@@ -956,6 +979,7 @@ namespace ams::kern {
/* Keep track of how many kernel waiters we have. */ /* Keep track of how many kernel waiters we have. */
if (IsKernelAddressKey(thread->GetAddressKey())) { if (IsKernelAddressKey(thread->GetAddressKey())) {
MESOSPHERE_ABORT_UNLESS((m_num_kernel_waiters++) >= 0); MESOSPHERE_ABORT_UNLESS((m_num_kernel_waiters++) >= 0);
KScheduler::SetSchedulerUpdateNeeded();
} }
/* Insert the waiter. */ /* Insert the waiter. */
@@ -970,6 +994,7 @@ namespace ams::kern {
/* Keep track of how many kernel waiters we have. */ /* Keep track of how many kernel waiters we have. */
if (IsKernelAddressKey(thread->GetAddressKey())) { if (IsKernelAddressKey(thread->GetAddressKey())) {
MESOSPHERE_ABORT_UNLESS((m_num_kernel_waiters--) > 0); MESOSPHERE_ABORT_UNLESS((m_num_kernel_waiters--) > 0);
KScheduler::SetSchedulerUpdateNeeded();
} }
/* Remove the waiter. */ /* Remove the waiter. */
@@ -1048,6 +1073,7 @@ namespace ams::kern {
/* Keep track of how many kernel waiters we have. */ /* Keep track of how many kernel waiters we have. */
if (IsKernelAddressKey(thread->GetAddressKey())) { if (IsKernelAddressKey(thread->GetAddressKey())) {
MESOSPHERE_ABORT_UNLESS((m_num_kernel_waiters--) > 0); MESOSPHERE_ABORT_UNLESS((m_num_kernel_waiters--) > 0);
KScheduler::SetSchedulerUpdateNeeded();
} }
it = m_waiter_list.erase(it); it = m_waiter_list.erase(it);
@@ -1094,15 +1120,21 @@ namespace ams::kern {
/* If the current thread has been asked to suspend, suspend it and retry. */ /* If the current thread has been asked to suspend, suspend it and retry. */
if (GetCurrentThread().IsSuspended()) { if (GetCurrentThread().IsSuspended()) {
GetCurrentThread().Suspend(); GetCurrentThread().UpdateState();
continue; continue;
} }
/* If we're not a kernel thread and we've been asked to suspend, suspend ourselves. */ /* If we're not a kernel thread and we've been asked to suspend, suspend ourselves. */
if (this->IsUserThread() && this->IsSuspended()) { if (KProcess *parent = this->GetOwnerProcess(); parent != nullptr) {
this->Suspend(); if (this->IsSuspended()) {
this->UpdateState();
}
parent->IncrementRunningThreadCount();
} }
/* Open a reference, now that we're running. */
this->Open();
/* Set our state and finish. */ /* Set our state and finish. */
this->SetState(KThread::ThreadState_Runnable); this->SetState(KThread::ThreadState_Runnable);
return ResultSuccess(); return ResultSuccess();
@@ -1117,18 +1149,23 @@ namespace ams::kern {
/* Call the debug callback. */ /* Call the debug callback. */
KDebug::OnExitThread(this); KDebug::OnExitThread(this);
/* Release the thread resource hint from parent. */ /* Release the thread resource hint, running thread count from parent. */
if (m_parent != nullptr) { if (m_parent != nullptr) {
m_parent->ReleaseResource(ams::svc::LimitableResource_ThreadCountMax, 0, 1); m_parent->ReleaseResource(ams::svc::LimitableResource_ThreadCountMax, 0, 1);
m_resource_limit_release_hint = true; m_resource_limit_release_hint = true;
m_parent->DecrementRunningThreadCount();
} }
/* Destroy any dependent objects. */
this->DestroyClosedObjects();
/* Perform termination. */ /* Perform termination. */
{ {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
/* Disallow all suspension. */ /* Disallow all suspension. */
m_suspend_allowed_flags = 0; m_suspend_allowed_flags = 0;
this->UpdateState();
/* Start termination. */ /* Start termination. */
this->StartTermination(); this->StartTermination();
@@ -1140,7 +1177,7 @@ namespace ams::kern {
MESOSPHERE_PANIC("KThread::Exit() would return"); MESOSPHERE_PANIC("KThread::Exit() would return");
} }
void KThread::Terminate() { Result KThread::Terminate() {
MESOSPHERE_ASSERT_THIS(); MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(this != GetCurrentThreadPointer()); MESOSPHERE_ASSERT(this != GetCurrentThreadPointer());
@@ -1149,7 +1186,9 @@ namespace ams::kern {
/* If the thread isn't terminated, wait for it to terminate. */ /* If the thread isn't terminated, wait for it to terminate. */
s32 index; s32 index;
KSynchronizationObject *objects[] = { this }; KSynchronizationObject *objects[] = { this };
KSynchronizationObject::Wait(std::addressof(index), objects, 1, ams::svc::WaitInfinite); return KSynchronizationObject::Wait(std::addressof(index), objects, 1, ams::svc::WaitInfinite);
} else {
return ResultSuccess();
} }
} }
@@ -1177,15 +1216,20 @@ namespace ams::kern {
/* Register the terminating dpc. */ /* Register the terminating dpc. */
this->RegisterDpc(DpcFlag_Terminating); this->RegisterDpc(DpcFlag_Terminating);
/* If the thread is pinned, unpin it. */
if (this->GetStackParameters().is_pinned) {
this->GetOwnerProcess()->UnpinThread(this);
}
/* If the thread is suspended, continue it. */ /* If the thread is suspended, continue it. */
if (this->IsSuspended()) { if (this->IsSuspended()) {
m_suspend_allowed_flags = 0; m_suspend_allowed_flags = 0;
this->Continue(); this->UpdateState();
} }
/* Change the thread's priority to be higher than any system thread's. */ /* Change the thread's priority to be higher than any system thread's. */
if (this->GetBasePriority() >= ams::svc::SystemThreadPriorityHighest) { if (this->GetBasePriority() >= ams::svc::SystemThreadPriorityHighest) {
this->SetBasePriority(ams::svc::SystemThreadPriorityHighest - 1); this->SetBasePriority(TerminatingThreadPriority);
} }
/* If the thread is runnable, send a termination interrupt to other cores. */ /* If the thread is runnable, send a termination interrupt to other cores. */

View File

@@ -23,12 +23,11 @@ namespace ams::kern {
/* Set members. */ /* Set members. */
m_owner = GetCurrentProcessPointer(); m_owner = GetCurrentProcessPointer();
/* Initialize the page group. */ /* Get the owner page table. */
auto &page_table = m_owner->GetPageTable(); auto &page_table = m_owner->GetPageTable();
new (GetPointer(m_page_group)) KPageGroup(page_table.GetBlockInfoManager());
/* Ensure that our page group's state is valid on exit. */ /* Construct the page group, guarding to make sure our state is valid on exit. */
auto pg_guard = SCOPE_GUARD { GetReference(m_page_group).~KPageGroup(); }; auto pg_guard = util::ConstructAtGuarded(m_page_group, page_table.GetBlockInfoManager());
/* Lock the memory. */ /* Lock the memory. */
R_TRY(page_table.LockForTransferMemory(GetPointer(m_page_group), addr, size, ConvertToKMemoryPermission(own_perm))); R_TRY(page_table.LockForTransferMemory(GetPointer(m_page_group), addr, size, ConvertToKMemoryPermission(own_perm)));

View File

@@ -68,6 +68,9 @@ namespace ams::kern {
/* Do the task. */ /* Do the task. */
task->DoWorkerTask(); task->DoWorkerTask();
/* Destroy any objects we may need to close. */
m_thread->DestroyClosedObjects();
} }
} }

View File

@@ -49,6 +49,9 @@ namespace ams::kern {
/* Initialize the carveout and the system resource limit. */ /* Initialize the carveout and the system resource limit. */
KSystemControl::InitializePhase1(); KSystemControl::InitializePhase1();
/* Synchronize all cores before proceeding, to ensure access to the global rng is consistent. */
cpu::SynchronizeAllCores();
/* Initialize the memory manager and the KPageBuffer slabheap. */ /* Initialize the memory manager and the KPageBuffer slabheap. */
{ {
const auto &management_region = KMemoryLayout::GetPoolManagementRegion(); const auto &management_region = KMemoryLayout::GetPoolManagementRegion();
@@ -74,6 +77,9 @@ namespace ams::kern {
Kernel::InitializeResourceManagers(pt_heap_region.GetAddress(), pt_heap_region.GetSize()); Kernel::InitializeResourceManagers(pt_heap_region.GetAddress(), pt_heap_region.GetSize());
} }
} else {
/* Synchronize all cores before proceeding, to ensure access to the global rng is consistent. */
cpu::SynchronizeAllCores();
} }
/* Initialize the supervisor page table for each core. */ /* Initialize the supervisor page table for each core. */

View File

@@ -30,32 +30,24 @@ namespace ams::kern::svc {
/* Determine aligned extents. */ /* Determine aligned extents. */
const uintptr_t aligned_start = util::AlignDown(address, PageSize); const uintptr_t aligned_start = util::AlignDown(address, PageSize);
const uintptr_t aligned_end = util::AlignUp(address + size, PageSize); const uintptr_t aligned_end = util::AlignUp(address + size, PageSize);
const size_t num_pages = (aligned_end - aligned_start) / PageSize;
/* Create a page group for the process's memory. */ /* Iterate over and operate on contiguous ranges. */
KPageGroup pg(page_table.GetBlockInfoManager());
/* Make and open the page group. */
R_TRY(page_table.MakeAndOpenPageGroup(std::addressof(pg),
aligned_start, num_pages,
KMemoryState_FlagReferenceCounted, KMemoryState_FlagReferenceCounted,
KMemoryPermission_UserRead, KMemoryPermission_UserRead,
KMemoryAttribute_Uncached, KMemoryAttribute_None));
/* Ensure we don't leak references to the pages we're operating on. */
ON_SCOPE_EXIT { pg.Close(); };
/* Operate on all the blocks. */
uintptr_t cur_address = aligned_start; uintptr_t cur_address = aligned_start;
size_t remaining = size; size_t remaining = size;
for (const auto &block : pg) { while (remaining > 0) {
/* Get the block extents. */ /* Get a contiguous range to operate on. */
KVirtualAddress operate_address = block.GetAddress(); KPageTableBase::MemoryRange contig_range = {};
size_t operate_size = block.GetSize(); R_TRY(page_table.OpenMemoryRangeForProcessCacheOperation(std::addressof(contig_range), cur_address, aligned_end - cur_address));
/* Close the range when we're done operating on it. */
ON_SCOPE_EXIT { contig_range.Close(); };
/* Adjust to remain within range. */ /* Adjust to remain within range. */
KVirtualAddress operate_address = contig_range.address;
size_t operate_size = contig_range.size;
if (cur_address < address) { if (cur_address < address) {
operate_address += (address - cur_address); operate_address += (address - cur_address);
operate_size -= (address - cur_address);
} }
if (operate_size > remaining) { if (operate_size > remaining) {
operate_size = remaining; operate_size = remaining;
@@ -65,7 +57,7 @@ namespace ams::kern::svc {
operation.Operate(GetVoidPointer(operate_address), operate_size); operation.Operate(GetVoidPointer(operate_address), operate_size);
/* Advance. */ /* Advance. */
cur_address += block.GetSize(); cur_address += contig_range.size;
remaining -= operate_size; remaining -= operate_size;
} }
MESOSPHERE_ASSERT(remaining == 0); MESOSPHERE_ASSERT(remaining == 0);

View File

@@ -276,6 +276,25 @@ namespace ams::kern::svc {
} }
} }
break; break;
case ams::svc::InfoType_MesosphereCurrentProcess:
{
/* Verify the input handle is invalid. */
R_UNLESS(handle == ams::svc::InvalidHandle, svc::ResultInvalidHandle());
/* Verify the sub-type is valid. */
R_UNLESS(info_subtype == 0, svc::ResultInvalidCombination());
/* Get the handle table. */
KHandleTable &handle_table = GetCurrentProcess().GetHandleTable();
/* Get a new handle for the current process. */
ams::svc::Handle tmp;
R_TRY(handle_table.Add(std::addressof(tmp), GetCurrentProcessPointer()));
/* Set the output. */
*out = tmp;
}
break;
default: default:
{ {
/* For debug, log the invalid info call. */ /* For debug, log the invalid info call. */

View File

@@ -74,12 +74,17 @@ namespace ams::kern::svc {
/* Wait for a message. */ /* Wait for a message. */
while (true) { while (true) {
/* Close any pending objects before we wait. */
GetCurrentThread().DestroyClosedObjects();
/* Wait for an object. */
s32 index; s32 index;
Result result = KSynchronizationObject::Wait(std::addressof(index), objs, num_objects, timeout); Result result = KSynchronizationObject::Wait(std::addressof(index), objs, num_objects, timeout);
if (svc::ResultTimedOut::Includes(result)) { if (svc::ResultTimedOut::Includes(result)) {
return result; return result;
} }
/* Receive the request. */
if (R_SUCCEEDED(result)) { if (R_SUCCEEDED(result)) {
KServerSession *session = objs[index]->DynamicCast<KServerSession *>(); KServerSession *session = objs[index]->DynamicCast<KServerSession *>();
if (session != nullptr) { if (session != nullptr) {

Some files were not shown because too many files have changed in this diff Show More