|
|
@ -713,50 +713,61 @@ ResultCode KPageTable::UnmapPages(VAddr addr, KPageLinkedList& page_linked_list,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ResultCode KPageTable::SetProcessMemoryPermission(VAddr addr, std::size_t size,
|
|
|
|
ResultCode KPageTable::SetProcessMemoryPermission(VAddr addr, std::size_t size,
|
|
|
|
KMemoryPermission perm) {
|
|
|
|
Svc::MemoryPermission svc_perm) {
|
|
|
|
|
|
|
|
const size_t num_pages = size / PageSize;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Lock the table.
|
|
|
|
std::lock_guard lock{page_table_lock};
|
|
|
|
std::lock_guard lock{page_table_lock};
|
|
|
|
|
|
|
|
|
|
|
|
KMemoryState prev_state{};
|
|
|
|
// Verify we can change the memory permission.
|
|
|
|
KMemoryPermission prev_perm{};
|
|
|
|
KMemoryState old_state;
|
|
|
|
|
|
|
|
KMemoryPermission old_perm;
|
|
|
|
|
|
|
|
size_t num_allocator_blocks;
|
|
|
|
|
|
|
|
R_TRY(this->CheckMemoryState(std::addressof(old_state), std::addressof(old_perm), nullptr,
|
|
|
|
|
|
|
|
std::addressof(num_allocator_blocks), addr, size,
|
|
|
|
|
|
|
|
KMemoryState::FlagCode, KMemoryState::FlagCode,
|
|
|
|
|
|
|
|
KMemoryPermission::None, KMemoryPermission::None,
|
|
|
|
|
|
|
|
KMemoryAttribute::All, KMemoryAttribute::None));
|
|
|
|
|
|
|
|
|
|
|
|
CASCADE_CODE(CheckMemoryState(
|
|
|
|
// Determine new perm/state.
|
|
|
|
&prev_state, &prev_perm, nullptr, nullptr, addr, size, KMemoryState::FlagCode,
|
|
|
|
const KMemoryPermission new_perm = ConvertToKMemoryPermission(svc_perm);
|
|
|
|
KMemoryState::FlagCode, KMemoryPermission::None, KMemoryPermission::None,
|
|
|
|
KMemoryState new_state = old_state;
|
|
|
|
KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped));
|
|
|
|
const bool is_w = (new_perm & KMemoryPermission::UserWrite) == KMemoryPermission::UserWrite;
|
|
|
|
|
|
|
|
const bool is_x = (new_perm & KMemoryPermission::UserExecute) == KMemoryPermission::UserExecute;
|
|
|
|
|
|
|
|
const bool was_x =
|
|
|
|
|
|
|
|
(old_perm & KMemoryPermission::UserExecute) == KMemoryPermission::UserExecute;
|
|
|
|
|
|
|
|
ASSERT(!(is_w && is_x));
|
|
|
|
|
|
|
|
|
|
|
|
KMemoryState state{prev_state};
|
|
|
|
if (is_w) {
|
|
|
|
|
|
|
|
switch (old_state) {
|
|
|
|
// Ensure state is mutable if permission allows write
|
|
|
|
case KMemoryState::Code:
|
|
|
|
if ((perm & KMemoryPermission::Write) != KMemoryPermission::None) {
|
|
|
|
new_state = KMemoryState::CodeData;
|
|
|
|
if (prev_state == KMemoryState::Code) {
|
|
|
|
break;
|
|
|
|
state = KMemoryState::CodeData;
|
|
|
|
case KMemoryState::AliasCode:
|
|
|
|
} else if (prev_state == KMemoryState::AliasCode) {
|
|
|
|
new_state = KMemoryState::AliasCodeData;
|
|
|
|
state = KMemoryState::AliasCodeData;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return early if there is nothing to change
|
|
|
|
// Succeed if there's nothing to do.
|
|
|
|
if (state == prev_state && perm == prev_perm) {
|
|
|
|
R_SUCCEED_IF(old_perm == new_perm && old_state == new_state);
|
|
|
|
return ResultSuccess;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((prev_perm & KMemoryPermission::Execute) != (perm & KMemoryPermission::Execute)) {
|
|
|
|
// Perform mapping operation.
|
|
|
|
|
|
|
|
const auto operation =
|
|
|
|
|
|
|
|
was_x ? OperationType::ChangePermissionsAndRefresh : OperationType::ChangePermissions;
|
|
|
|
|
|
|
|
R_TRY(Operate(addr, num_pages, new_perm, operation));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update the blocks.
|
|
|
|
|
|
|
|
block_manager->Update(addr, num_pages, new_state, new_perm, KMemoryAttribute::None);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure cache coherency, if we're setting pages as executable.
|
|
|
|
|
|
|
|
if (is_x) {
|
|
|
|
// Memory execution state is changing, invalidate CPU cache range
|
|
|
|
// Memory execution state is changing, invalidate CPU cache range
|
|
|
|
system.InvalidateCpuInstructionCacheRange(addr, size);
|
|
|
|
system.InvalidateCpuInstructionCacheRange(addr, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::size_t num_pages{size / PageSize};
|
|
|
|
|
|
|
|
const OperationType operation{(perm & KMemoryPermission::Execute) != KMemoryPermission::None
|
|
|
|
|
|
|
|
? OperationType::ChangePermissionsAndRefresh
|
|
|
|
|
|
|
|
: OperationType::ChangePermissions};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CASCADE_CODE(Operate(addr, num_pages, perm, operation));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
block_manager->Update(addr, num_pages, state, perm);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ResultSuccess;
|
|
|
|
return ResultSuccess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|