kernel: Update to use atmosphere macros and correct Result (#7242)

* kernel: Switch to atmosphere style macros

* code: Rename ResultCode to Result

* code: Result constants are lower case

* Address review comments

* core: Remove CASCADE_CODE

* R_TRY replaces completely

* core: Run clang format
This commit is contained in:
GPUCode 2023-12-31 19:01:40 +02:00 committed by GitHub
parent 811303ea54
commit 5a7f615da1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
132 changed files with 2807 additions and 2995 deletions

View file

@ -192,9 +192,12 @@ void Process::Run(s32 main_thread_priority, u32 stack_size) {
return;
}
VAddr out_addr{};
auto MapSegment = [&](CodeSet::Segment& segment, VMAPermission permissions,
MemoryState memory_state) {
HeapAllocate(segment.addr, segment.size, permissions, memory_state, true);
HeapAllocate(std::addressof(out_addr), segment.addr, segment.size, permissions,
memory_state, true);
kernel.memory.WriteBlock(*this, segment.addr, codeset->memory.data() + segment.offset,
segment.size);
};
@ -205,8 +208,8 @@ void Process::Run(s32 main_thread_priority, u32 stack_size) {
MapSegment(codeset->DataSegment(), VMAPermission::ReadWrite, MemoryState::Private);
// Allocate and map stack
HeapAllocate(Memory::HEAP_VADDR_END - stack_size, stack_size, VMAPermission::ReadWrite,
MemoryState::Locked, true);
HeapAllocate(std::addressof(out_addr), Memory::HEAP_VADDR_END - stack_size, stack_size,
VMAPermission::ReadWrite, MemoryState::Locked, true);
// Map special address mappings
kernel.MapSharedPages(vm_manager);
@ -246,14 +249,14 @@ VAddr Process::GetLinearHeapLimit() const {
return GetLinearHeapBase() + memory_region->size;
}
ResultVal<VAddr> Process::HeapAllocate(VAddr target, u32 size, VMAPermission perms,
MemoryState memory_state, bool skip_range_check) {
Result Process::HeapAllocate(VAddr* out_addr, VAddr target, u32 size, VMAPermission perms,
MemoryState memory_state, bool skip_range_check) {
LOG_DEBUG(Kernel, "Allocate heap target={:08X}, size={:08X}", target, size);
if (target < Memory::HEAP_VADDR || target + size > Memory::HEAP_VADDR_END ||
target + size < target) {
if (!skip_range_check) {
LOG_ERROR(Kernel, "Invalid heap address");
return ERR_INVALID_ADDRESS;
return ResultInvalidAddress;
}
}
{
@ -261,13 +264,13 @@ ResultVal<VAddr> Process::HeapAllocate(VAddr target, u32 size, VMAPermission per
if (vma->second.type != VMAType::Free ||
vma->second.base + vma->second.size < target + size) {
LOG_ERROR(Kernel, "Trying to allocate already allocated memory");
return ERR_INVALID_ADDRESS_STATE;
return ResultInvalidAddressState;
}
}
auto allocated_fcram = memory_region->HeapAllocate(size);
if (allocated_fcram.empty()) {
LOG_ERROR(Kernel, "Not enough space");
return ERR_OUT_OF_HEAP_MEMORY;
return ResultOutOfHeapMemory;
}
// Maps heap block by block
@ -290,20 +293,19 @@ ResultVal<VAddr> Process::HeapAllocate(VAddr target, u32 size, VMAPermission per
memory_used += size;
resource_limit->Reserve(ResourceLimitType::Commit, size);
return target;
*out_addr = target;
return ResultSuccess;
}
ResultCode Process::HeapFree(VAddr target, u32 size) {
Result Process::HeapFree(VAddr target, u32 size) {
LOG_DEBUG(Kernel, "Free heap target={:08X}, size={:08X}", target, size);
if (target < Memory::HEAP_VADDR || target + size > Memory::HEAP_VADDR_END ||
target + size < target) {
LOG_ERROR(Kernel, "Invalid heap address");
return ERR_INVALID_ADDRESS;
return ResultInvalidAddress;
}
if (size == 0) {
return RESULT_SUCCESS;
}
R_SUCCEED_IF(size == 0);
// Free heaps block by block
CASCADE_RESULT(auto backing_blocks, vm_manager.GetBackingBlocksForRange(target, size));
@ -313,23 +315,23 @@ ResultCode Process::HeapFree(VAddr target, u32 size) {
holding_memory -= MemoryRegionInfo::Interval(backing_offset, backing_offset + block_size);
}
ResultCode result = vm_manager.UnmapRange(target, size);
Result result = vm_manager.UnmapRange(target, size);
ASSERT(result.IsSuccess());
memory_used -= size;
resource_limit->Release(ResourceLimitType::Commit, size);
return RESULT_SUCCESS;
return ResultSuccess;
}
ResultVal<VAddr> Process::LinearAllocate(VAddr target, u32 size, VMAPermission perms) {
Result Process::LinearAllocate(VAddr* out_addr, VAddr target, u32 size, VMAPermission perms) {
LOG_DEBUG(Kernel, "Allocate linear heap target={:08X}, size={:08X}", target, size);
u32 physical_offset;
if (target == 0) {
auto offset = memory_region->LinearAllocate(size);
if (!offset) {
LOG_ERROR(Kernel, "Not enough space");
return ERR_OUT_OF_HEAP_MEMORY;
return ResultOutOfHeapMemory;
}
physical_offset = *offset;
target = physical_offset + GetLinearHeapAreaAddress();
@ -337,7 +339,7 @@ ResultVal<VAddr> Process::LinearAllocate(VAddr target, u32 size, VMAPermission p
if (target < GetLinearHeapBase() || target + size > GetLinearHeapLimit() ||
target + size < target) {
LOG_ERROR(Kernel, "Invalid linear heap address");
return ERR_INVALID_ADDRESS;
return ResultInvalidAddress;
}
// Kernel would crash/return error when target doesn't meet some requirement.
@ -350,7 +352,7 @@ ResultVal<VAddr> Process::LinearAllocate(VAddr target, u32 size, VMAPermission p
physical_offset = target - GetLinearHeapAreaAddress(); // relative to FCRAM
if (!memory_region->LinearAllocate(physical_offset, size)) {
LOG_ERROR(Kernel, "Trying to allocate already allocated memory");
return ERR_INVALID_ADDRESS_STATE;
return ResultInvalidAddressState;
}
}
@ -366,26 +368,20 @@ ResultVal<VAddr> Process::LinearAllocate(VAddr target, u32 size, VMAPermission p
resource_limit->Reserve(ResourceLimitType::Commit, size);
LOG_DEBUG(Kernel, "Allocated at target={:08X}", target);
return target;
*out_addr = target;
return ResultSuccess;
}
ResultCode Process::LinearFree(VAddr target, u32 size) {
Result Process::LinearFree(VAddr target, u32 size) {
LOG_DEBUG(Kernel, "Free linear heap target={:08X}, size={:08X}", target, size);
if (target < GetLinearHeapBase() || target + size > GetLinearHeapLimit() ||
target + size < target) {
LOG_ERROR(Kernel, "Invalid linear heap address");
return ERR_INVALID_ADDRESS;
return ResultInvalidAddress;
}
if (size == 0) {
return RESULT_SUCCESS;
}
ResultCode result = vm_manager.UnmapRange(target, size);
if (result.IsError()) {
LOG_ERROR(Kernel, "Trying to free already freed memory");
return result;
}
R_SUCCEED_IF(size == 0);
R_TRY(vm_manager.UnmapRange(target, size));
u32 physical_offset = target - GetLinearHeapAreaAddress(); // relative to FCRAM
memory_region->Free(physical_offset, size);
@ -394,7 +390,7 @@ ResultCode Process::LinearFree(VAddr target, u32 size) {
memory_used -= size;
resource_limit->Release(ResourceLimitType::Commit, size);
return RESULT_SUCCESS;
return ResultSuccess;
}
ResultVal<VAddr> Process::AllocateThreadLocalStorage() {
@ -435,7 +431,7 @@ ResultVal<VAddr> Process::AllocateThreadLocalStorage() {
if (!offset) {
LOG_ERROR(Kernel_SVC,
"Not enough space in BASE linear region to allocate a new TLS page");
return ERR_OUT_OF_MEMORY;
return ResultOutOfMemory;
}
holding_tls_memory +=
@ -467,14 +463,13 @@ ResultVal<VAddr> Process::AllocateThreadLocalStorage() {
return tls_address;
}
ResultCode Process::Map(VAddr target, VAddr source, u32 size, VMAPermission perms,
bool privileged) {
Result Process::Map(VAddr target, VAddr source, u32 size, VMAPermission perms, bool privileged) {
LOG_DEBUG(Kernel, "Map memory target={:08X}, source={:08X}, size={:08X}, perms={:08X}", target,
source, size, perms);
if (!privileged && (source < Memory::HEAP_VADDR || source + size > Memory::HEAP_VADDR_END ||
source + size < source)) {
LOG_ERROR(Kernel, "Invalid source address");
return ERR_INVALID_ADDRESS;
return ResultInvalidAddress;
}
// TODO(wwylele): check target address range. Is it also restricted to heap region?
@ -489,17 +484,17 @@ ResultCode Process::Map(VAddr target, VAddr source, u32 size, VMAPermission perm
VMAPermission::ReadWrite,
MemoryState::AliasCode, perms);
} else {
return ERR_INVALID_ADDRESS;
return ResultInvalidAddress;
}
} else {
return ERR_INVALID_ADDRESS_STATE;
return ResultInvalidAddressState;
}
}
auto vma = vm_manager.FindVMA(target);
if (vma->second.type != VMAType::Free || vma->second.base + vma->second.size < target + size) {
LOG_ERROR(Kernel, "Trying to map to already allocated memory");
return ERR_INVALID_ADDRESS_STATE;
return ResultInvalidAddressState;
}
MemoryState source_state = privileged ? MemoryState::Locked : MemoryState::Aliased;
@ -507,8 +502,8 @@ ResultCode Process::Map(VAddr target, VAddr source, u32 size, VMAPermission perm
VMAPermission source_perm = privileged ? VMAPermission::None : VMAPermission::ReadWrite;
// Mark source region as Aliased
CASCADE_CODE(vm_manager.ChangeMemoryState(source, size, MemoryState::Private,
VMAPermission::ReadWrite, source_state, source_perm));
R_TRY(vm_manager.ChangeMemoryState(source, size, MemoryState::Private, VMAPermission::ReadWrite,
source_state, source_perm));
CASCADE_RESULT(auto backing_blocks, vm_manager.GetBackingBlocksForRange(source, size));
VAddr interval_target = target;
@ -520,16 +515,15 @@ ResultCode Process::Map(VAddr target, VAddr source, u32 size, VMAPermission perm
interval_target += block_size;
}
return RESULT_SUCCESS;
return ResultSuccess;
}
ResultCode Process::Unmap(VAddr target, VAddr source, u32 size, VMAPermission perms,
bool privileged) {
Result Process::Unmap(VAddr target, VAddr source, u32 size, VMAPermission perms, bool privileged) {
LOG_DEBUG(Kernel, "Unmap memory target={:08X}, source={:08X}, size={:08X}, perms={:08X}",
target, source, size, perms);
if (!privileged && (source < Memory::HEAP_VADDR || source + size > Memory::HEAP_VADDR_END ||
source + size < source)) {
LOG_ERROR(Kernel, "Invalid source address");
return ERR_INVALID_ADDRESS;
return ResultInvalidAddress;
}
// TODO(wwylele): check target address range. Is it also restricted to heap region?
@ -543,10 +537,10 @@ ResultCode Process::Unmap(VAddr target, VAddr source, u32 size, VMAPermission pe
VMAPermission::None, MemoryState::Private,
perms);
} else {
return ERR_INVALID_ADDRESS;
return ResultInvalidAddress;
}
} else {
return ERR_INVALID_ADDRESS_STATE;
return ResultInvalidAddressState;
}
}
@ -555,13 +549,13 @@ ResultCode Process::Unmap(VAddr target, VAddr source, u32 size, VMAPermission pe
MemoryState source_state = privileged ? MemoryState::Locked : MemoryState::Aliased;
CASCADE_CODE(vm_manager.UnmapRange(target, size));
R_TRY(vm_manager.UnmapRange(target, size));
// Change back source region state. Note that the permission is reprotected according to param
CASCADE_CODE(vm_manager.ChangeMemoryState(source, size, source_state, VMAPermission::None,
MemoryState::Private, perms));
R_TRY(vm_manager.ChangeMemoryState(source, size, source_state, VMAPermission::None,
MemoryState::Private, perms));
return RESULT_SUCCESS;
return ResultSuccess;
}
void Process::FreeAllMemory() {