2019-06-24 11:05:51 +02:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-06-24 11:05:51 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-05-12 00:02:10 +02:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-24 11:05:51 +02:00
|
|
|
#include "ro_nro_utils.hpp"
|
|
|
|
|
2019-10-24 11:30:10 +02:00
|
|
|
namespace ams::ro::impl {
|
2019-06-24 11:05:51 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr size_t MaxMapRetries = 0x200;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-04 21:33:09 +02:00
|
|
|
Result MapNro(u64 *out_base_address, os::NativeHandle process_handle, u64 nro_heap_address, u64 nro_heap_size, u64 bss_heap_address, u64 bss_heap_size) {
|
2019-10-24 10:40:44 +02:00
|
|
|
map::MappedCodeMemory nro_mcm(ResultInternalError{});
|
|
|
|
map::MappedCodeMemory bss_mcm(ResultInternalError{});
|
2019-06-24 11:05:51 +02:00
|
|
|
u64 base_address;
|
|
|
|
|
|
|
|
/* Map the NRO, and map the BSS immediately after it. */
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < MaxMapRetries; i++) {
|
2019-10-24 10:40:44 +02:00
|
|
|
map::MappedCodeMemory tmp_nro_mcm(ResultInternalError{});
|
2019-06-24 11:05:51 +02:00
|
|
|
R_TRY(map::MapCodeMemoryInProcess(tmp_nro_mcm, process_handle, nro_heap_address, nro_heap_size));
|
|
|
|
base_address = tmp_nro_mcm.GetDstAddress();
|
|
|
|
|
|
|
|
if (bss_heap_size > 0) {
|
|
|
|
map::MappedCodeMemory tmp_bss_mcm(process_handle, base_address + nro_heap_size, bss_heap_address, bss_heap_size);
|
|
|
|
R_TRY_CATCH(tmp_bss_mcm.GetResult()) {
|
2020-02-14 07:05:20 +01:00
|
|
|
R_CATCH(svc::ResultInvalidCurrentMemory) {
|
2019-06-24 11:05:51 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
|
|
|
|
if (!map::CanAddGuardRegionsInProcess(process_handle, base_address, nro_heap_size + bss_heap_size)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bss_mcm = std::move(tmp_bss_mcm);
|
|
|
|
} else {
|
|
|
|
if (!map::CanAddGuardRegionsInProcess(process_handle, base_address, nro_heap_size)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nro_mcm = std::move(tmp_nro_mcm);
|
|
|
|
break;
|
|
|
|
}
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(i < MaxMapRetries, ResultOutOfAddressSpace());
|
2019-06-24 11:05:51 +02:00
|
|
|
|
|
|
|
/* Invalidation here actually prevents them from unmapping at scope exit. */
|
|
|
|
nro_mcm.Invalidate();
|
|
|
|
bss_mcm.Invalidate();
|
|
|
|
|
|
|
|
*out_base_address = base_address;
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:33:09 +02:00
|
|
|
Result SetNroPerms(os::NativeHandle process_handle, u64 base_address, u64 rx_size, u64 ro_size, u64 rw_size) {
|
2019-06-24 11:05:51 +02:00
|
|
|
const u64 rx_offset = 0;
|
|
|
|
const u64 ro_offset = rx_offset + rx_size;
|
|
|
|
const u64 rw_offset = ro_offset + ro_size;
|
|
|
|
|
|
|
|
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + rx_offset, rx_size, Perm_Rx));
|
|
|
|
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + ro_offset, ro_size, Perm_R ));
|
|
|
|
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + rw_offset, rw_size, Perm_Rw));
|
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:33:09 +02:00
|
|
|
Result UnmapNro(os::NativeHandle process_handle, u64 base_address, u64 nro_heap_address, u64 bss_heap_address, u64 bss_heap_size, u64 code_size, u64 rw_size) {
|
2019-06-24 11:05:51 +02:00
|
|
|
/* First, unmap bss. */
|
|
|
|
if (bss_heap_size > 0) {
|
|
|
|
R_TRY(svcUnmapProcessCodeMemory(process_handle, base_address + code_size + rw_size, bss_heap_address, bss_heap_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next, unmap .rwdata */
|
|
|
|
if (rw_size > 0) {
|
|
|
|
R_TRY(svcUnmapProcessCodeMemory(process_handle, base_address + code_size, nro_heap_address + code_size, rw_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, unmap .text + .rodata. */
|
|
|
|
R_TRY(svcUnmapProcessCodeMemory(process_handle, base_address, nro_heap_address, code_size));
|
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|