feat(libs): implement the AMM remap, multimap and protection changes

libSceAmpr gains sceAmprAmmCommandBufferRemap, MultiMap, ModifyProtect
and ModifyMtypeProtect with their GPU mask forms and the eight measure
functions. Signatures and the protection and mask checks follow ampr_emu.

The executor keeps a page table of the AMM ranges: each page's physical
offset, protection, memory type and whether it came from the pool. A
remap moves the pages of a fully mapped range and leaves the old range
reserved; a multimap maps the same pages again; a pool page returns to
the pool only when its last mapping goes. A protection change applies
the masked bits page by page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chad
2026-10-05 15:36:24 -05:00
parent c16e59bfd5
commit 166ea44993
5 changed files with 372 additions and 15 deletions
+101
View File
@@ -141,6 +141,34 @@ static int RecordAmmMap(Apr::CommandBufferObject* buffer, Apr::Opcode opcode, st
return AppendAmm(buffer, &command, sizeof(command));
}
static bool ValidAmmProtection(std::int32_t protection) {
return (static_cast<std::uint32_t>(protection) & 0xFFFFFC0Cu) == 0u;
}
static bool ValidRemap(std::uint64_t address, std::uint64_t source, std::uint64_t size, std::int32_t protection) {
return ValidMapRange(address, size) && ValidMapRange(source, size) && ValidAmmProtection(protection);
}
static bool ValidProtect(std::uint64_t address, std::uint64_t size, std::int32_t protection, std::int32_t mask) {
return ValidMapRange(address, size) && ValidAmmProtection(protection) && ValidAmmProtection(mask);
}
static int RecordAmmRemap(Apr::CommandBufferObject* buffer, Apr::Opcode opcode, std::uint64_t address, std::uint64_t source, std::uint64_t size, std::int32_t protection) {
if (!buffer) return SCE_KERNEL_ERROR_EINVAL;
if (!buffer->base) return SCE_KERNEL_ERROR_EPERM;
if (!ValidRemap(address, source, size, protection)) return SCE_KERNEL_ERROR_EINVAL;
const Apr::AmmRemapCommand command{{opcode, sizeof(command)}, address, source, size, protection, 0};
return AppendAmm(buffer, &command, sizeof(command));
}
static int RecordAmmProtect(Apr::CommandBufferObject* buffer, Apr::Opcode opcode, std::uint64_t address, std::uint64_t size, std::int32_t type, std::int32_t protection, std::int32_t mask) {
if (!buffer) return SCE_KERNEL_ERROR_EINVAL;
if (!buffer->base) return SCE_KERNEL_ERROR_EPERM;
if (!ValidProtect(address, size, protection, mask)) return SCE_KERNEL_ERROR_EINVAL;
const Apr::AmmProtectCommand command{{opcode, sizeof(command)}, address, size, type, protection, mask, 0};
return AppendAmm(buffer, &command, sizeof(command));
}
static int RecordMapBegin(Apr::CommandBufferObject* buffer, Apr::Opcode opcode, std::uint64_t address, std::uint64_t directOffset, std::uint64_t size, std::int32_t type, std::int32_t protection) {
if (!ValidMap(address, directOffset, size, protection)) return SCE_KERNEL_ERROR_EINVAL;
if (InMap(buffer)) return SCE_KERNEL_ERROR_EPERM;
@@ -613,6 +641,79 @@ std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeUnmap(std::uint64_t addres
return ValidMapRange(address, size) ? std::int64_t{sizeof(Apr::AmmUnmapCommand)} : AmmMeasureInvalid;
}
int APS5_VABI sceAmprAmmCommandBufferRemap(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t source, std::uint64_t size, std::int32_t protection) {
return RecordAmmRemap(buffer, Apr::Opcode::AmmRemap, address, source, size, protection);
}
int APS5_VABI sceAmprAmmCommandBufferRemapWithGpuMaskId(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t source, std::uint64_t size, std::int32_t protection, std::uint8_t gpuMaskId) {
(void)gpuMaskId;
return RecordAmmRemap(buffer, Apr::Opcode::AmmRemap, address, source, size, protection);
}
int APS5_VABI sceAmprAmmCommandBufferMultiMap(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t alias, std::uint64_t size, std::int32_t protection) {
return RecordAmmRemap(buffer, Apr::Opcode::AmmMultiMap, address, alias, size, protection);
}
int APS5_VABI sceAmprAmmCommandBufferMultiMapWithGpuMaskId(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t alias, std::uint64_t size, std::int32_t protection, std::uint8_t gpuMaskId) {
(void)gpuMaskId;
return RecordAmmRemap(buffer, Apr::Opcode::AmmMultiMap, address, alias, size, protection);
}
int APS5_VABI sceAmprAmmCommandBufferModifyProtect(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t size, std::int32_t protection, std::int32_t mask) {
return RecordAmmProtect(buffer, Apr::Opcode::AmmModifyProtect, address, size, 0, protection, mask);
}
int APS5_VABI sceAmprAmmCommandBufferModifyProtectWithGpuMaskId(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t size, std::int32_t protection, std::int32_t mask, std::uint8_t gpuMaskId) {
(void)gpuMaskId;
return RecordAmmProtect(buffer, Apr::Opcode::AmmModifyProtect, address, size, 0, protection, mask);
}
int APS5_VABI sceAmprAmmCommandBufferModifyMtypeProtect(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t size, std::int32_t type, std::int32_t protection, std::int32_t mask) {
return RecordAmmProtect(buffer, Apr::Opcode::AmmModifyMtypeProtect, address, size, type, protection, mask);
}
int APS5_VABI sceAmprAmmCommandBufferModifyMtypeProtectWithGpuMaskId(Apr::CommandBufferObject* buffer, std::uint64_t address, std::uint64_t size, std::int32_t type, std::int32_t protection, std::int32_t mask, std::uint8_t gpuMaskId) {
(void)gpuMaskId;
return RecordAmmProtect(buffer, Apr::Opcode::AmmModifyMtypeProtect, address, size, type, protection, mask);
}
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeRemap(std::uint64_t address, std::uint64_t source, std::uint64_t size, std::int32_t protection) {
return ValidRemap(address, source, size, protection) ? std::int64_t{sizeof(Apr::AmmRemapCommand)} : AmmMeasureInvalid;
}
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeRemapWithGpuMaskId(std::uint64_t address, std::uint64_t source, std::uint64_t size, std::int32_t protection, std::uint8_t gpuMaskId) {
(void)gpuMaskId;
return sceAmprAmmMeasureAmmCommandSizeRemap(address, source, size, protection);
}
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMultiMap(std::uint64_t address, std::uint64_t alias, std::uint64_t size, std::int32_t protection) {
return ValidRemap(address, alias, size, protection) ? std::int64_t{sizeof(Apr::AmmRemapCommand)} : AmmMeasureInvalid;
}
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMultiMapWithGpuMaskId(std::uint64_t address, std::uint64_t alias, std::uint64_t size, std::int32_t protection, std::uint8_t gpuMaskId) {
(void)gpuMaskId;
return sceAmprAmmMeasureAmmCommandSizeMultiMap(address, alias, size, protection);
}
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeModifyProtect(std::uint64_t address, std::uint64_t size, std::int32_t protection, std::int32_t mask) {
return ValidProtect(address, size, protection, mask) ? std::int64_t{sizeof(Apr::AmmProtectCommand)} : AmmMeasureInvalid;
}
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeModifyProtectWithGpuMaskId(std::uint64_t address, std::uint64_t size, std::int32_t protection, std::int32_t mask, std::uint8_t gpuMaskId) {
(void)gpuMaskId;
return sceAmprAmmMeasureAmmCommandSizeModifyProtect(address, size, protection, mask);
}
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeModifyMtypeProtect(std::uint64_t address, std::uint64_t size, std::int32_t type, std::int32_t protection, std::int32_t mask) {
(void)type;
return ValidProtect(address, size, protection, mask) ? std::int64_t{sizeof(Apr::AmmProtectCommand)} : AmmMeasureInvalid;
}
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeModifyMtypeProtectWithGpuMaskId(std::uint64_t address, std::uint64_t size, std::int32_t type, std::int32_t protection, std::int32_t mask, std::uint8_t gpuMaskId) {
(void)gpuMaskId;
return sceAmprAmmMeasureAmmCommandSizeModifyMtypeProtect(address, size, type, protection, mask);
}
int APS5_VABI sceAmprAmmGiveDirectMemory(std::int64_t searchStart, std::int64_t searchEnd, std::size_t size, std::size_t alignment, int usage, std::int64_t* offset) {
return AmmGiveDirectMemory_nid_no_patch(searchStart, searchEnd, size, alignment, usage, offset);
}
@@ -48,6 +48,10 @@ enum class Opcode : std::uint32_t {
AmmMapDirect = 18,
AmmUnmap = 19,
MapEnd = 20,
AmmRemap = 21,
AmmMultiMap = 22,
AmmModifyProtect = 23,
AmmModifyMtypeProtect = 24,
};
struct CommandHeader {
@@ -87,6 +91,25 @@ struct MapEndCommand {
CommandHeader header;
};
struct AmmRemapCommand {
CommandHeader header;
std::uint64_t address;
std::uint64_t source;
std::uint64_t size;
std::int32_t protection;
std::int32_t reserved;
};
struct AmmProtectCommand {
CommandHeader header;
std::uint64_t address;
std::uint64_t size;
std::int32_t type;
std::int32_t protection;
std::int32_t mask;
std::int32_t reserved;
};
struct WriteAddressCommand {
CommandHeader header;
std::uint64_t address;
+106 -14
View File
@@ -244,11 +244,19 @@ TCommand _read(const Apr::CommandBufferObject& buffer, std::uint32_t cursor) {
constexpr std::uint64_t AmmRangeBytes = 32ull << 30;
constexpr int GuestMapFixed = 0x10;
struct AmmPage {
std::uint64_t physical;
std::int32_t type;
std::int32_t protection;
bool pooled;
};
struct AmmState {
std::mutex lock;
std::uintptr_t base = 0;
std::map<std::uint64_t, std::uint64_t> pool;
std::map<std::uintptr_t, std::uint64_t> autoPages;
std::map<std::uintptr_t, AmmPage> pages;
std::map<std::uint64_t, std::uint32_t> pooledUses;
std::atomic<std::uint32_t> lastSubmit{0};
};
@@ -307,35 +315,111 @@ std::uint64_t _takePoolPages(AmmState& state, std::uint64_t bytes) {
throw std::runtime_error("AMM: no free run of " + std::to_string(bytes) + " bytes in the direct memory given to the mapper");
}
void _releaseAutoPages(AmmState& state, std::uint64_t address, std::uint64_t size) {
for (auto it = state.autoPages.lower_bound(address); it != state.autoPages.end() && it->first < address + size;) {
_returnPoolPages(state, it->second, PS5_PAGE_SIZE);
it = state.autoPages.erase(it);
void _addPage(AmmState& state, std::uint64_t address, const AmmPage& page) {
state.pages[address] = page;
if (page.pooled) ++state.pooledUses[page.physical];
}
void _forgetPages(AmmState& state, std::uint64_t address, std::uint64_t size, bool release) {
for (auto it = state.pages.lower_bound(address); it != state.pages.end() && it->first < address + size;) {
if (it->second.pooled) {
const auto uses = state.pooledUses.find(it->second.physical);
if (--uses->second == 0 && release) {
state.pooledUses.erase(uses);
_returnPoolPages(state, it->second.physical, PS5_PAGE_SIZE);
}
}
it = state.pages.erase(it);
}
}
std::vector<AmmPage> _mappedPages(AmmState& state, std::uint64_t address, std::uint64_t size) {
std::vector<AmmPage> pages;
for (std::uint64_t offset = 0; offset < size; offset += PS5_PAGE_SIZE) {
const auto found = state.pages.find(address + offset);
if (found == state.pages.end()) throw std::runtime_error("AMM: range is not fully mapped");
pages.push_back(found->second);
}
return pages;
}
void _reserve(std::uint64_t address, std::uint64_t size) {
void* target = reinterpret_cast<void*>(address);
if (DoReserveVirtual(&target, size, GuestMapFixed, 0) != 0) throw std::runtime_error("AMM: cannot return the range to the reservation");
}
void _mapRun(std::uint64_t address, std::uint64_t physical, std::uint64_t size, std::int32_t protection) {
void* target = reinterpret_cast<void*>(address);
if (DoMapDirect(&target, size, _ammHostProtection(protection), GuestMapFixed, static_cast<std::int64_t>(physical), 0) != 0) throw std::runtime_error("AMM: map failed");
}
void _mapPages(AmmState& state, std::uint64_t address, const std::vector<AmmPage>& pages) {
for (std::size_t first = 0; first < pages.size();) {
std::size_t last = first + 1;
while (last < pages.size() && pages[last].physical == pages[last - 1].physical + PS5_PAGE_SIZE && pages[last].protection == pages[first].protection) ++last;
_mapRun(address + first * PS5_PAGE_SIZE, pages[first].physical, (last - first) * PS5_PAGE_SIZE, pages[first].protection);
first = last;
}
for (std::size_t index = 0; index < pages.size(); ++index) _addPage(state, address + index * PS5_PAGE_SIZE, pages[index]);
}
void _ammUnmap(const Apr::AmmUnmapCommand& command) {
auto& state = _amm();
const std::lock_guard lock(state.lock);
_ammCheckRange(state, command.address, command.size);
_releaseAutoPages(state, command.address, command.size);
void* address = reinterpret_cast<void*>(command.address);
if (DoReserveVirtual(&address, command.size, GuestMapFixed, 0) != 0) throw std::runtime_error("AMM: unmap failed");
_forgetPages(state, command.address, command.size, true);
_reserve(command.address, command.size);
}
void _ammMap(const Apr::AmmMapCommand& command, bool direct) {
auto& state = _amm();
const std::lock_guard lock(state.lock);
_ammCheckRange(state, command.address, command.size);
_releaseAutoPages(state, command.address, command.size);
_forgetPages(state, command.address, command.size, true);
const auto physical = direct ? command.directOffset : _takePoolPages(state, command.size);
void* address = reinterpret_cast<void*>(command.address);
if (DoMapDirect(&address, command.size, _ammHostProtection(command.protection), GuestMapFixed, static_cast<std::int64_t>(physical), 0) != 0) {
try {
_mapRun(command.address, physical, command.size, command.protection);
} catch (...) {
if (!direct) _returnPoolPages(state, physical, command.size);
throw std::runtime_error("AMM: map failed");
throw;
}
for (std::uint64_t offset = 0; offset < command.size; offset += PS5_PAGE_SIZE) _addPage(state, command.address + offset, {physical + offset, command.type, command.protection, !direct});
}
void _ammRemap(const Apr::AmmRemapCommand& command, bool alias) {
auto& state = _amm();
const std::lock_guard lock(state.lock);
_ammCheckRange(state, command.address, command.size);
_ammCheckRange(state, command.source, command.size);
if (alias && command.address < command.source + command.size && command.source < command.address + command.size) throw std::runtime_error("AMM: multimap onto its own source range");
auto pages = _mappedPages(state, command.source, command.size);
for (auto& page : pages) page.protection = command.protection;
if (!alias) {
_forgetPages(state, command.source, command.size, false);
_reserve(command.source, command.size);
}
_forgetPages(state, command.address, command.size, true);
_mapPages(state, command.address, pages);
}
void _ammProtect(const Apr::AmmProtectCommand& command, bool type) {
auto& state = _amm();
const std::lock_guard lock(state.lock);
_ammCheckRange(state, command.address, command.size);
auto pages = _mappedPages(state, command.address, command.size);
for (std::size_t index = 0; index < pages.size(); ++index) {
auto& page = pages[index];
page.protection = (page.protection & ~command.mask) | (command.protection & command.mask);
if (type) page.type = command.type;
state.pages[command.address + index * PS5_PAGE_SIZE] = page;
}
for (std::size_t first = 0; first < pages.size();) {
const int host = _ammHostProtection(pages[first].protection);
std::size_t last = first + 1;
while (last < pages.size() && _ammHostProtection(pages[last].protection) == host) ++last;
if (DoMprotect(reinterpret_cast<void*>(command.address + first * PS5_PAGE_SIZE), (last - first) * PS5_PAGE_SIZE, host) != 0) throw std::runtime_error("AMM: protection change failed");
first = last;
}
if (direct) return;
for (std::uint64_t offset = 0; offset < command.size; offset += PS5_PAGE_SIZE) state.autoPages.emplace(command.address + offset, physical + offset);
}
struct ReadCursor {
@@ -386,6 +470,14 @@ void _execute(const Apr::CommandBufferObject& buffer) {
case Apr::Opcode::AmmUnmap:
_ammUnmap(_read<Apr::AmmUnmapCommand>(buffer, cursor));
break;
case Apr::Opcode::AmmRemap:
case Apr::Opcode::AmmMultiMap:
_ammRemap(_read<Apr::AmmRemapCommand>(buffer, cursor), header.opcode == Apr::Opcode::AmmMultiMap);
break;
case Apr::Opcode::AmmModifyProtect:
case Apr::Opcode::AmmModifyMtypeProtect:
_ammProtect(_read<Apr::AmmProtectCommand>(buffer, cursor), header.opcode == Apr::Opcode::AmmModifyMtypeProtect);
break;
case Apr::Opcode::MapEnd:
break;
case Apr::Opcode::WriteAddress: {
+141 -1
View File
@@ -89,7 +89,23 @@ int APS5_VABI sceAmprAprCommandBufferMapEnd(Apr::CommandBufferObject*);
std::uint64_t APS5_VABI sceAmprMeasureCommandSizeMapBegin(std::uint64_t, std::uint64_t, std::uint32_t, std::uint32_t);
std::uint64_t APS5_VABI sceAmprMeasureCommandSizeMapDirectBegin(std::uint64_t, std::uint64_t, std::uint64_t, std::uint32_t, std::uint32_t);
std::uint64_t APS5_VABI sceAmprMeasureCommandSizeMapEnd();
int APS5_VABI sceAmprCommandBufferWriteCounter_04_00(Apr::CommandBufferObject*, std::uint8_t, std::uint8_t, std::uint64_t, std::uint8_t, std::uint8_t);
int APS5_VABI sceKernelQueryMemoryProtection(void*, void**, void**, int*);
int APS5_VABI sceAmprAmmCommandBufferRemap(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t);
int APS5_VABI sceAmprAmmCommandBufferRemapWithGpuMaskId(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t, std::uint8_t);
int APS5_VABI sceAmprAmmCommandBufferMultiMap(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t);
int APS5_VABI sceAmprAmmCommandBufferMultiMapWithGpuMaskId(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t, std::uint8_t);
int APS5_VABI sceAmprAmmCommandBufferModifyProtect(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t);
int APS5_VABI sceAmprAmmCommandBufferModifyProtectWithGpuMaskId(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t, std::uint8_t);
int APS5_VABI sceAmprAmmCommandBufferModifyMtypeProtect(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t, std::int32_t);
int APS5_VABI sceAmprAmmCommandBufferModifyMtypeProtectWithGpuMaskId(Apr::CommandBufferObject*, std::uint64_t, std::uint64_t, std::int32_t, std::int32_t, std::int32_t, std::uint8_t);
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeRemap(std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t);
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeRemapWithGpuMaskId(std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t, std::uint8_t);
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMultiMap(std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t);
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeMultiMapWithGpuMaskId(std::uint64_t, std::uint64_t, std::uint64_t, std::int32_t, std::uint8_t);
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeModifyProtect(std::uint64_t, std::uint64_t, std::int32_t, std::int32_t);
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeModifyProtectWithGpuMaskId(std::uint64_t, std::uint64_t, std::int32_t, std::int32_t, std::uint8_t);
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeModifyMtypeProtect(std::uint64_t, std::uint64_t, std::int32_t, std::int32_t, std::int32_t);
std::int64_t APS5_VABI sceAmprAmmMeasureAmmCommandSizeModifyMtypeProtectWithGpuMaskId(std::uint64_t, std::uint64_t, std::int32_t, std::int32_t, std::int32_t, std::uint8_t);
}
static void Require(bool value) { if (!value) std::abort(); }
@@ -659,6 +675,129 @@ void TestAmm() {
Require(sceAmprMeasureCommandSizeMapDirectBegin(start, direct, page, 0, cpuReadWrite) == sizeof(Apr::AmmMapCommand));
}
int Protection(std::uint64_t address) {
int protection = -1;
Require(sceKernelQueryMemoryProtection(reinterpret_cast<void*>(address), nullptr, nullptr, &protection) == 0);
return protection;
}
void SubmitAmm(Recorder& recorder) {
std::uint32_t id = 0;
Require(sceAmprAmmSubmitCommandBuffer3(recorder.memory.data(), recorder.Offset(), 0, &id) == 0);
Require(sceAmprAmmWaitCommandBufferCompletion(id) == 0);
}
void TestAmmRemapAndProtect() {
std::uint64_t start = 0;
std::uint64_t end = 0;
std::uint64_t multimapStart = 0;
std::uint64_t multimapEnd = 0;
Require(sceAmprAmmGetVirtualAddressRanges(&start, &end, &multimapStart, &multimapEnd) == 0);
const auto directMemory = static_cast<std::int64_t>(sceKernelGetDirectMemorySize());
std::int64_t pool = -1;
std::int64_t direct = -1;
Require(sceAmprAmmGiveDirectMemory(0, directMemory, 4 * page, page, 1, &pool) == 0);
Require(sceAmprAmmGiveDirectMemory(0, directMemory, page, page, 0, &direct) == 0);
const std::uint64_t source = start + 0x100000;
const std::uint64_t directSource = start + 0x140000;
const std::uint64_t moved = start + 0x180000;
const std::uint64_t alias = multimapStart + 0x100000;
const std::uint64_t directAlias = multimapStart + 0x140000;
const std::uint64_t fresh = start + 0x1C0000;
const std::uint64_t returned = start + 0x200000;
Recorder maps;
Require(sceAmprAmmCommandBufferMap(&maps.buffer, source, 2 * page, 0, cpuReadWrite) == 0);
Require(sceAmprAmmCommandBufferMapDirect(&maps.buffer, directSource, direct, page, 0, cpuReadWrite) == 0);
SubmitAmm(maps);
At(source) = 0xA1;
At(source + page) = 0xA2;
At(directSource) = 0xD1;
Recorder moves;
auto offset = moves.Offset();
auto commands = moves.Commands();
Require(sceAmprAmmCommandBufferRemap(&moves.buffer, moved, source, 2 * page, cpuReadWrite) == 0);
RequireAppended(moves, offset, commands, Apr::Opcode::AmmRemap, sceAmprAmmMeasureAmmCommandSizeRemap(moved, source, 2 * page, cpuReadWrite));
offset = moves.Offset();
commands = moves.Commands();
Require(sceAmprAmmCommandBufferMultiMap(&moves.buffer, alias, moved, 2 * page, cpuReadWrite) == 0);
RequireAppended(moves, offset, commands, Apr::Opcode::AmmMultiMap, sceAmprAmmMeasureAmmCommandSizeMultiMap(alias, moved, 2 * page, cpuReadWrite));
Require(sceAmprAmmCommandBufferMultiMapWithGpuMaskId(&moves.buffer, directAlias, directSource, page, amprReadWrite, 2) == 0);
SubmitAmm(moves);
Require(At(moved) == 0xA1 && At(moved + page) == 0xA2 && At(alias + page) == 0xA2);
At(alias) = 0xB1;
Require(At(moved) == 0xB1);
Require(At(directAlias) == 0xD1);
Recorder shared;
Require(sceAmprAmmCommandBufferUnmap(&shared.buffer, moved, 2 * page) == 0);
Require(sceAmprAmmCommandBufferMap(&shared.buffer, fresh, 2 * page, 0, cpuReadWrite) == 0);
SubmitAmm(shared);
At(fresh) = 0xC1;
At(fresh + page) = 0xC2;
Require(At(alias) == 0xB1 && At(alias + page) == 0xA2);
Recorder released;
Require(sceAmprAmmCommandBufferUnmap(&released.buffer, alias, 2 * page) == 0);
Require(sceAmprAmmCommandBufferRemapWithGpuMaskId(&released.buffer, returned, fresh, 2 * page, cpuReadWrite, 1) == 0);
Require(sceAmprAmmCommandBufferMap(&released.buffer, fresh, 2 * page, 0, cpuReadWrite) == 0);
SubmitAmm(released);
Require(At(returned) == 0xC1 && At(returned + page) == 0xC2);
At(fresh) = 0xE1;
Require(At(returned) == 0xC1);
Recorder protects;
offset = protects.Offset();
commands = protects.Commands();
Require(sceAmprAmmCommandBufferModifyProtect(&protects.buffer, returned, 2 * page, 0x01, 0x02) == 0);
RequireAppended(protects, offset, commands, Apr::Opcode::AmmModifyProtect, sceAmprAmmMeasureAmmCommandSizeModifyProtect(returned, 2 * page, 0x01, 0x02));
offset = protects.Offset();
commands = protects.Commands();
Require(sceAmprAmmCommandBufferModifyMtypeProtect(&protects.buffer, returned, page, 3, 0x22, 0x22) == 0);
RequireAppended(protects, offset, commands, Apr::Opcode::AmmModifyMtypeProtect, sceAmprAmmMeasureAmmCommandSizeModifyMtypeProtect(returned, page, 3, 0x22, 0x22));
Require(sceAmprAmmCommandBufferModifyProtectWithGpuMaskId(&protects.buffer, directAlias, page, 0x00, 0x80, 0) == 0);
Require(sceAmprAmmCommandBufferModifyMtypeProtectWithGpuMaskId(&protects.buffer, fresh, page, 1, 0x10, 0x13, 0) == 0);
SubmitAmm(protects);
Require(Protection(returned) == 0x23 && Protection(returned + page) == 0x01);
Require(Protection(directAlias) == 0x01);
Require(Protection(fresh) == 0x10 && Protection(fresh + page) == 0x03);
At(returned) = 0xF1;
Require(At(returned) == 0xF1 && At(returned + page) == 0xC2);
const std::int64_t ammRejected = invalidArgument;
Recorder empty;
Require(sceAmprAmmCommandBufferRemap(&empty.buffer, moved, source + 8, page, cpuReadWrite) == invalidArgument);
Require(sceAmprAmmCommandBufferRemap(&empty.buffer, moved + 8, source, page, cpuReadWrite) == invalidArgument);
Require(sceAmprAmmCommandBufferRemap(&empty.buffer, moved, source, 0, cpuReadWrite) == invalidArgument);
Require(sceAmprAmmCommandBufferMultiMap(&empty.buffer, alias, moved, page, 0x04) == invalidArgument);
Require(sceAmprAmmCommandBufferModifyProtect(&empty.buffer, returned, page, 0x04, 0x03) == invalidArgument);
Require(sceAmprAmmCommandBufferModifyProtect(&empty.buffer, returned, page, 0x03, 0x04) == invalidArgument);
Require(sceAmprAmmCommandBufferModifyMtypeProtect(&empty.buffer, returned + 8, page, 0, 0x03, 0x03) == invalidArgument);
Require(sceAmprAmmCommandBufferRemap(nullptr, moved, source, page, cpuReadWrite) == invalidArgument);
Require(empty.Offset() == 0 && empty.Commands() == 0);
Apr::CommandBufferObject unbound{};
Require(sceAmprCommandBufferConstructor(&unbound) == 0);
Require(sceAmprAmmCommandBufferMultiMap(&unbound, alias, moved, page, cpuReadWrite) == permissionDenied);
Require(sceAmprAmmCommandBufferModifyProtect(&unbound, returned, page, 0x03, 0x03) == permissionDenied);
const auto measured = static_cast<std::uint32_t>(sceAmprAmmMeasureAmmCommandSizeRemap(moved, source, page, cpuReadWrite));
Recorder exact(measured);
Require(sceAmprAmmCommandBufferRemap(&exact.buffer, moved, source, page, cpuReadWrite) == 0);
Require(sceAmprAmmCommandBufferModifyProtect(&exact.buffer, returned, page, 0x03, 0x03) == busy);
Require(sceAmprAmmMeasureAmmCommandSizeRemapWithGpuMaskId(moved, source + 8, page, cpuReadWrite, 0) == ammRejected);
Require(sceAmprAmmMeasureAmmCommandSizeRemapWithGpuMaskId(moved, source, page, cpuReadWrite, 0) == std::int64_t{sizeof(Apr::AmmRemapCommand)});
Require(sceAmprAmmMeasureAmmCommandSizeMultiMapWithGpuMaskId(alias, moved, page, 0x400, 0) == ammRejected);
Require(sceAmprAmmMeasureAmmCommandSizeMultiMapWithGpuMaskId(alias, moved, page, cpuReadWrite, 0) == std::int64_t{sizeof(Apr::AmmRemapCommand)});
Require(sceAmprAmmMeasureAmmCommandSizeModifyProtectWithGpuMaskId(returned, page, 0x03, 0x04, 0) == ammRejected);
Require(sceAmprAmmMeasureAmmCommandSizeModifyProtectWithGpuMaskId(returned, page, 0x03, 0x03, 0) == std::int64_t{sizeof(Apr::AmmProtectCommand)});
Require(sceAmprAmmMeasureAmmCommandSizeModifyMtypeProtectWithGpuMaskId(returned, 0, 0, 0x03, 0x03, 0) == ammRejected);
Require(sceAmprAmmMeasureAmmCommandSizeModifyMtypeProtectWithGpuMaskId(returned, page, 0, 0x03, 0x03, 0) == std::int64_t{sizeof(Apr::AmmProtectCommand)});
Require(sceAmprAmmMeasureAmmCommandSizeMultiMap(alias, moved, 0, cpuReadWrite) == ammRejected);
Require(sceAmprAmmMeasureAmmCommandSizeModifyMtypeProtect(returned, page, 0, 0x08, 0x03) == ammRejected);
}
}
int main() {
@@ -679,5 +818,6 @@ int main() {
TestConstructed();
TestGatherScatter();
TestAmm();
TestAmmRemapAndProtect();
return 0;
}
+1
View File
@@ -36,6 +36,7 @@ Throughout the project, every function at every stage either **does exactly what
- [AMPR counter access](../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libSceAmpr) - signatures and checks of `WaitOnCounter_04_00`, `WriteCounter_04_00`, `ConstructNop` and `ConstructMarker` taken from the [ampr_emu](https://github.com/drakmor/ampr_emu) reimplementation, which hands counter commands to the console and does not show their effect. The counters are taken as 32 bits each: the 8-byte access of counter n spans n (low) and n + 1 (high), the 2- and 1-byte accesses select a half or a byte of the counter by its offset, waits compare at the access width (signed and wrapped compares included), the AND mask applies to both sides and the atomic writes wrap within the access. The `ConstructNop` type is not recorded
- [APR gather and scatter reads](../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libSceAmpr) - signatures, argument checks and the read cursor taken from the [ampr_emu](https://github.com/drakmor/ampr_emu) reimplementation, not confirmed on a title: each read leaves the file, the next file offset and the next destination; a gather reads that file at a new offset into the next destination, a scatter continues in the file into a new destination, a gather-scatter takes both. Recording one needs a read file since the last `ResetGatherScatterState`; like ampr_emu, `sceAmprCommandBufferReset` keeps that state, and a gather or scatter with no read before it in the submitted buffer throws instead of returning `SCE_AMPR_ERROR_APR_INVALIDGATHERSCATTERSTATE`
- [AMM map, map direct and unmap](../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libSceAmpr) - signatures and the protection mask (CPU, GPU, AMPR and ACP read and write) taken from the [ampr_emu](https://github.com/drakmor/ampr_emu) reimplementation, which hands the remaining checks and the mapping to the console kernel; the mapper model taken from [PS5PCEM](https://github.com/iStark/PS5PCEM): `sceAmprAmmGiveDirectMemory` usage 1 gives the allocated direct memory to the pool plain maps take pages from, usage 0 only allocates it. Not confirmed on a title. The AMM virtual address ranges are a 32 GiB range and a 32 GiB multimap range reserved in the guest arena on first use, not the console's; maps outside them throw. Addresses, sizes and direct offsets must be 16 KiB aligned. The memory type and the GPU mask are ignored, AMPR and ACP access maps as CPU access, unmapped pages go back to the pool and stay reserved. Submission runs the buffer before returning, the submit result is always 0 and a failing command throws; the AMM measures return errors sign-extended, as ampr_emu does. `sceAmprAmmGetVirtualAddressRanges` throws for a null output
- [AMM remap, multimap and protection changes](../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libSceAmpr) - signatures and the protection and mask checks taken from ampr_emu; the effects are inferred from the names, not confirmed on a title. A remap moves every page of a fully mapped range to the new address with the given protection and leaves the old range reserved; a multimap maps the same pages at a second address, anywhere in the AMM ranges, and a pool page goes back to the pool once its last mapping is unmapped; a multimap onto its own source range throws. A protection change sets the masked bits on every page of a fully mapped range; the memory type is recorded and ignored. A command on a range that is not fully mapped throws
- [APR map begin and end](../../core/libs/prx/libSceAmpr/Export.cpp) (libSceAmpr) - as in ampr_emu, `MapBegin`/`MapDirectBegin` map when they run and `MapEnd` only closes the region; while a region is open a second begin and every completion write return `EPERM`, the at-start `_04_00` writes are accepted
- [AMPR markers](../../core/libs/prx/libSceAmpr/Export.cpp) (libSceAmpr) - signatures taken from the [ampr_emu](https://github.com/drakmor/ampr_emu) reimplementation, not confirmed on a title: `sceAmprCommandBufferSetMarkerWithColor` takes the color by pointer, the push and measure variants by value. Colors are not recorded
- [sceFontGetRenderScaledKerning](../../core/libs/prx/libSceFont/src/Render.cpp) (libSceFont) - arguments, the cleared output and the bound renderer check taken from `cellFontGetRenderScaledKerning` in [RPCS3](https://github.com/RPCS3/rpcs3/blob/master/rpcs3/Emu/Cell/Modules/cellFont.cpp) (PS3 predecessor of the library), not confirmed on a title. The kerning is the `sceFontGetKerning` one at the render scale