memory: exclude AGC driver reservation from Demon's Souls guest budget #1399

Open
github-bot wants to merge 12 commits from github-bot/gh-1399 into main AGit
Owner

@⁠kewalker on GitHub · 2026-10-11 03:00 UTC · original
kewalker:codex/demons-souls-agc-startup → main · +376 −340 · 5 files · 12 commits · open

Demon's Souls can claim all advertised direct memory before calling sce::Agc::init(). The lazy 2 MiB AGC driver allocation introduced in 165cd884 then fails with 0x80020023 (KERNEL_ERROR_EAGAIN), and the game hits its fatal int 0x41 assertion during startup.

Build on upstream 9a5dd9fa, which reserves and initializes AGC driver memory before guest execution. Allocate the tracked block at the end of direct memory and exclude it from the guest's advertised memory budget. Preserve upstream function-table initialization and release the budget reservation during graphics shutdown.

The regression test reserves the driver block, lets the guest exhaust the remaining direct-memory pool, and verifies that AGC initialization and the existing system-table checks still succeed. A focused --agc-system-table-only option runs this test independently.

Fixes #1392.

Validation:

  • Windows Release emulator and compute-test builds passed locally.
  • shader_recompiler_compute_tests.exe --agc-system-table-only passed on the local fixed build.
  • The same focused test passed against the isolated PR sources, recompiling all production translation units affected by local experiments and reusing unchanged build objects and dependency libraries.
  • @⁠kewalker manually confirmed that Demon’s Souls now passes the startup failure.
  • Repository clang-format pre-commit hook and git diff --check passed. Formatting changes in the four production files come from the required hook.

AI assistance and approval: Codex assisted with diagnosis, implementation, regression testing, and this PR description. I confirm that I reviewed the changes and tested them myself.

This PR contains the startup fix and its regression coverage.

Merged current main (c0ebf224), preserving upstream AGC initialization and its Windows test-variable fix.

The allocation explicitly aligns the physical pool end down; the budget excludes the driver block plus any tail alignment padding. The unaligned-pool regression verifies this contiguous guest budget. Restored AGC system-table initialization after repairing a misapplied GitHub suggestion.

**@⁠kewalker** on GitHub · 2026-10-11 03:00 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399) `kewalker:codex/demons-souls-agc-startup` → `main` · +376 −340 · 5 files · 12 commits · open Demon's Souls can claim all advertised direct memory before calling `sce::Agc::init()`. The lazy 2 MiB AGC driver allocation introduced in 165cd884 then fails with `0x80020023` (`KERNEL_ERROR_EAGAIN`), and the game hits its fatal `int 0x41` assertion during startup. Build on upstream 9a5dd9fa, which reserves and initializes AGC driver memory before guest execution. Allocate the tracked block at the end of direct memory and exclude it from the guest's advertised memory budget. Preserve upstream function-table initialization and release the budget reservation during graphics shutdown. The regression test reserves the driver block, lets the guest exhaust the remaining direct-memory pool, and verifies that AGC initialization and the existing system-table checks still succeed. A focused `--agc-system-table-only` option runs this test independently. Fixes #1392. Validation: - Windows Release emulator and compute-test builds passed locally. - `shader_recompiler_compute_tests.exe --agc-system-table-only` passed on the local fixed build. - The same focused test passed against the isolated PR sources, recompiling all production translation units affected by local experiments and reusing unchanged build objects and dependency libraries. - @⁠kewalker manually confirmed that Demon’s Souls now passes the startup failure. - Repository clang-format pre-commit hook and `git diff --check` passed. Formatting changes in the four production files come from the required hook. AI assistance and approval: Codex assisted with diagnosis, implementation, regression testing, and this PR description. I confirm that I reviewed the changes and tested them myself. This PR contains the startup fix and its regression coverage. Merged current main (c0ebf224), preserving upstream AGC initialization and its Windows test-variable fix. The allocation explicitly aligns the physical pool end down; the budget excludes the driver block plus any tail alignment padding. The unaligned-pool regression verifies this contiguous guest budget. Restored AGC system-table initialization after repairing a misapplied GitHub suggestion.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:55:41 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-11 03:06 UTC · original · edited 2026-10-21 15:37 UTC

Review in Change Stack →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

The kernel now tracks a system direct-memory reserve and subtracts it from the guest direct-memory size. The AGC driver allocates its DMEM region from the aligned end of direct memory and clears the reserve when it releases the allocation. Compute tests check the reduced guest budget, allocation exhaustion, the EAGAIN result, and sparse-image buffer retention. Command-line selectors run the AGC test with standard and unaligned memory pools.

Suggested reviewers: brandostrong

Priority: ➖ Normal

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: 🔵 Low · up to 5d81c

The change fixes the AGC startup allocation failure. In a narrow unaligned-pool case, a guest can still allocate slightly beyond its reported memory budget. This is a small gap to address before or shortly after merge.

Pre-merge checks: Passed 4 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Docstring Coverage Inconclusive Docstring coverage is 4.23% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 71 functions across 4 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Issue #1392 reports the Demon’s Souls startup failure but provides no explicit coding acceptance criteria. The PR addresses the reported failure mechanism by reserving the AGC driver block before gues…
Out of Scope Changes check Passed The changes remain connected to issue #1392. The kernel reserve API, AGC allocation and release logic, regression coverage, test selectors, and required formatting support the startup fix or its valid…
Title check Passed The title clearly and concisely identifies the main change: excluding the AGC driver reservation from the guest memory budget.
Description check Passed The description directly explains the startup failure, the memory-reservation fix, preserved initialization, regression coverage, validation, and linked issue.
Full details: Linked Issues check

Explanation

Issue #1392 reports the Demon’s Souls startup failure but provides no explicit coding acceptance criteria. The PR addresses the reported failure mechanism by reserving the AGC driver block before guest execution and excluding the block and tail padding from the guest direct-memory budget. The regression test exhausts the guest budget before AGC initialization and checks the existing system-table behavior. The focused test selector supports independent validation.

Full details: Out of Scope Changes check

Explanation

The changes remain connected to issue #1392. The kernel reserve API, AGC allocation and release logic, regression coverage, test selectors, and required formatting support the startup fix or its validation. The image-upload test adjustment does not demonstrate unrelated production behavior. No concrete out-of-scope change is established.

Full details: Docstring Coverage

Explanation

Docstring coverage is 4.23% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 71 functions across 4 files. (1 skipped: 1 too large.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-11 03:06 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#issuecomment-6104871459) · edited 2026-10-21 15:37 UTC <!-- coderabbit-ai-generated:v1 --> <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1399?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- This is an auto-generated comment: review paused by coderabbit.ai --> > [!NOTE] > ## Reviews paused > > It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the `reviews.auto_review.auto_pause_after_reviewed_commits` setting. > > - Last reviewed commit: 5d81c42ff08df9808dabf87f6273cee06f30c8a8 > - Latest commit, not reviewed: 519186792c28f824d9395a0ce9411c6ae650b094 > > Use the following commands to manage reviews: > - `@coderabbitai resume` to resume automatic reviews. > - `@coderabbitai review` to trigger a single review. > > Use the checkboxes below for quick actions: > - [ ] <!-- {"checkboxId":"7f6cc2e2-2e4e-497a-8c31-c9e4573e93d1"} --> ▶️ Resume reviews > - [ ] <!-- {"checkboxId":"e9bb8d72-00e8-4f67-9cb2-caf3b22574fe"} --> 🔍 Trigger review <!-- end of auto-generated comment: review paused by coderabbit.ai --> <!-- walkthrough_start --> <details> <summary><strong>📝 Walkthrough</strong></summary> <dl> <dd> The kernel now tracks a system direct-memory reserve and subtracts it from the guest direct-memory size. The AGC driver allocates its DMEM region from the aligned end of direct memory and clears the reserve when it releases the allocation. Compute tests check the reduced guest budget, allocation exhaustion, the `EAGAIN` result, and sparse-image buffer retention. Command-line selectors run the AGC test with standard and unaligned memory pools. **Suggested reviewers:** `brandostrong` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"5d81c42ff08df9808dabf87f6273cee06f30c8a8" --> **Change:** Bug fix · **Severity of issue fixed:** Medium <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong>🔵 Low</strong> · up to <code>5d81c</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **🔵 Low** · up to `5d81c` --> <!-- final_review_risk_coverage:{"sourceCommitId":"5d81c42ff08df9808dabf87f6273cee06f30c8a8","coveredCommitId":"5d81c42ff08df9808dabf87f6273cee06f30c8a8","kind":"reviewed"} --> The change fixes the AGC startup allocation failure. In a narrow unaligned-pool case, a guest can still allocate slightly beyond its reported memory budget. This is a small gap to address before or shortly after merge. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-inconclusive-14-v2.svg" width="14" height="14" align="middle" alt="Inconclusive" title="Inconclusive"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 inconclusive</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :--------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Inconclusive](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-inconclusive.svg) | Docstring coverage is 4.23% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 71 functions across 4 files. (1 skipped: 1… | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> Passed checks: 4 passed</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Issue `#1392` reports the Demon’s Souls startup failure but provides no explicit coding acceptance criteria. The PR addresses the reported failure mechanism by reserving the AGC driver block before gues… | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The changes remain connected to issue `#1392`. The kernel reserve API, AGC allocation and release logic, regression coverage, test selectors, and required formatting support the startup fix or its valid… | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly and concisely identifies the main change: excluding the AGC driver reservation from the guest memory budget. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description directly explains the startup failure, the memory-reservation fix, preserved initialization, regression coverage, validation, and linked issue. | <details> <summary><strong>Full details: Linked Issues check</strong></summary> <dl> <dd> **Explanation** Issue `#1392` reports the Demon’s Souls startup failure but provides no explicit coding acceptance criteria. The PR addresses the reported failure mechanism by reserving the AGC driver block before guest execution and excluding the block and tail padding from the guest direct-memory budget. The regression test exhausts the guest budget before AGC initialization and checks the existing system-table behavior. The focused test selector supports independent validation. </dd> </dl> </details> <details> <summary><strong>Full details: Out of Scope Changes check</strong></summary> <dl> <dd> **Explanation** The changes remain connected to issue `#1392`. The kernel reserve API, AGC allocation and release logic, regression coverage, test selectors, and required formatting support the startup fix or its validation. The image-upload test adjustment does not demonstrate unrelated production behavior. No concrete out-of-scope change is established. </dd> </dl> </details> </dd> </dl> </details> <details> <summary><strong>Full details: Docstring Coverage</strong></summary> <dl> <dd> **Explanation** Docstring coverage is 4.23% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 71 functions across 4 files. (1 skipped: 1 too large.) **Resolution** Write docstrings for the functions missing them to satisfy the coverage threshold. </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autofix</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1399)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>❤️ Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
@ -4092,2 +4092,4 @@
g_driver_dmem.physical_offset = physical_offset;
// Hide the driver block and any unaligned tail so the guest budget ends before the mapping.
LibKernel::Memory::SetSystemDirectMemoryReserve(reported_end - start);
return OK;
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-11 03:06 UTC · original · edited 2026-10-11 03:29 UTC

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

end - start can over-reserve and conflicts with the AGC mapping on a retry.

start is AlignDown(end - DriverDmemSize, DriverDmemSize). If end is not 2 MiB aligned, the reserve (end - start) is larger than 2 MiB. The allocation covers [start, start + 2 MiB). Memory between start + 2 MiB and end is excluded from the guest budget but not allocated.

The effect is small and bounded. Record the exact allocated size, or align end down first, so the reserve matches the block.

Proposed fix
-	const auto end             = LibKernel::Memory::KernelGetDirectMemorySize();
-	const auto start           = Common::AlignDown(end - DriverDmemSize, DriverDmemSize);
+	const auto reported_end    = LibKernel::Memory::KernelGetDirectMemorySize();
+	const auto end             = Common::AlignDown(reported_end, DriverDmemSize);
+	const auto start           = end - DriverDmemSize;

The reserve must still cover reported_end - start if the tail should be hidden. Choose one behavior deliberately.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

	int64_t    physical_offset = -1;
	const auto reported_end    = LibKernel::Memory::KernelGetDirectMemorySize();
	const auto end             = Common::AlignDown(reported_end, DriverDmemSize);
	const auto start           = end - DriverDmemSize;
	auto       result          = LibKernel::Memory::KernelAllocateDirectMemory(
	    start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset);
	if (result != OK) {
		return result;
	}
	void* address = reinterpret_cast<void*>(DriverDmemBase);
	result        = LibKernel::Memory::KernelMapNamedDirectMemory(
	    &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver");
	if (result != OK) {
		EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset,
		                                                            DriverDmemSize) != OK);
		return result;
	}
	EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase);
	std::memset(address, 0, DriverDmemSize);
	g_driver_dmem.physical_offset = physical_offset;
	LibKernel::Memory::SetSystemDirectMemoryReserve(end - start);
	return OK;

✅ Addressed in commit 9a6ed5c

**@⁠coderabbitai[bot]** on GitHub · 2026-10-11 03:06 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#discussion_r4240063027) · edited 2026-10-11 03:29 UTC **🎯 Functional Correctness** | **🟡 Minor** | **⚡ Quick win** **`end - start` can over-reserve and conflicts with the AGC mapping on a retry.** `start` is `AlignDown(end - DriverDmemSize, DriverDmemSize)`. If `end` is not 2 MiB aligned, the reserve (`end - start`) is larger than 2 MiB. The allocation covers `[start, start + 2 MiB)`. Memory between `start + 2 MiB` and `end` is excluded from the guest budget but not allocated. The effect is small and bounded. Record the exact allocated size, or align `end` down first, so the reserve matches the block. <details> <summary>Proposed fix</summary> ```diff - const auto end = LibKernel::Memory::KernelGetDirectMemorySize(); - const auto start = Common::AlignDown(end - DriverDmemSize, DriverDmemSize); + const auto reported_end = LibKernel::Memory::KernelGetDirectMemorySize(); + const auto end = Common::AlignDown(reported_end, DriverDmemSize); + const auto start = end - DriverDmemSize; ``` The reserve must still cover `reported_end - start` if the tail should be hidden. Choose one behavior deliberately. </details> <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. ```suggestion int64_t physical_offset = -1; const auto reported_end = LibKernel::Memory::KernelGetDirectMemorySize(); const auto end = Common::AlignDown(reported_end, DriverDmemSize); const auto start = end - DriverDmemSize; auto result = LibKernel::Memory::KernelAllocateDirectMemory( start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset); if (result != OK) { return result; } void* address = reinterpret_cast<void*>(DriverDmemBase); result = LibKernel::Memory::KernelMapNamedDirectMemory( &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver"); if (result != OK) { EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset, DriverDmemSize) != OK); return result; } EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase); std::memset(address, 0, DriverDmemSize); g_driver_dmem.physical_offset = physical_offset; LibKernel::Memory::SetSystemDirectMemoryReserve(end - start); return OK; ``` </details> <!-- suggestion_end --> <!-- fingerprinting:phantom:poseidon:beignet55 --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:b40e5a3505393567c3e51667 --> <!-- This is an auto-generated comment by CodeRabbit --> ✅ Addressed in commit 9a6ed5c
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-11 03:06 UTC · original · reviewed

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 89903cc0-068b-47f3-ba9d-e218b21bf25a
📥 Commits

Reviewing files that changed from the base of the PR and between 730e1a3cf3 and a35ae61e84.

📒 Files selected for processing (5)
  • src/kernel/memory.cpp
  • src/kernel/memory.h
  • src/libs/agc.cpp
  • src/libs/agc.h
  • tests/ShaderRecompilerComputeTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-11 03:06 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#pullrequestreview-5481688592) · **reviewed** **Actionable comments posted: 1** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `89903cc0-068b-47f3-ba9d-e218b21bf25a` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 730e1a3cf39d50c787d477e813e0bef92006a063 and a35ae61e84c9737048a852076e381026dad56400. </details> <details> <summary>📒 Files selected for processing (5)</summary> * `src/kernel/memory.cpp` * `src/kernel/memory.h` * `src/libs/agc.cpp` * `src/libs/agc.h` * `tests/ShaderRecompilerComputeTests.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=eccea515-f426-42ae-a178-f436c59bf1fc attempt=d8084762-5f3c-4e99-8432-61d1967f9a89 batch=1/1 -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-11 03:34 UTC · original · edited 2026-10-11 07:53 UTC
On src/libs/agc.cpp line 4199 (outdated):

@@ -4180,36 +4175,37 @@ int Initialize() {
 		uint32_t user_data[2];
 	};
 	static_assert(sizeof(FunctionShaderBindings) == 16);
-	// Ordered-count queue validator, including its return to the caller.
-	static constexpr std::array<uint32_t, 16> validator {
-	    0xbeeb03ff, 0x00000021, 0xb96a1818, 0xbf06106a,
-	    0xbf800000, 0x8590807e, 0xb96a0a18, 0xbf066a80,
-	    0xbf800000, 0x85ea807e, 0x88ea106a, 0xbf870003,
-	    0xbefc03ff, 0x8a6ca000, 0xbf920009, 0xbefd210e,
-	};
-	const auto base = DriverDmemBase + AgcDmemOffset;
-	const auto function_table = base + 0x60;
-	const auto code_address = base + 0x100; // Shader code requires 256-byte alignment.
-	std::array<FunctionShaderBindings, 8> functions {};
-	functions[2].code = code_address;
-	const std::array<uint32_t, 4> descriptor {
-	    static_cast<uint32_t>(function_table),
-	    static_cast<uint32_t>(function_table >> 32u) | 0x00100000u,
-	    static_cast<uint32_t>(functions.size()), 0x5204u,
-	};
-	std::memcpy(reinterpret_cast<void*>(function_table), functions.data(), sizeof(functions));
-	std::memcpy(reinterpret_cast<void*>(code_address), validator.data(), sizeof(validator));
-	std::memcpy(reinterpret_cast<void*>(base), descriptor.data(), sizeof(descriptor));
+	int64_t    physical_offset = -1;
+	const auto reported_end    = LibKernel::Memory::KernelGetDirectMemorySize();
+	const auto end             = Common::AlignDown(reported_end, DriverDmemSize);
+	const auto start           = end - DriverDmemSize;
+	auto       result          = LibKernel::Memory::KernelAllocateDirectMemory(
+	    start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset);
+	if (result != OK) {
+		return result;
+	}
+	void* address = reinterpret_cast<void*>(DriverDmemBase);
+	result        = LibKernel::Memory::KernelMapNamedDirectMemory(
+	    &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver");
+	if (result != OK) {
+		EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset,
+		                                                            DriverDmemSize) != OK);
+		return result;
+	}
+	EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase);
+	std::memset(address, 0, DriverDmemSize);
 	g_driver_dmem.physical_offset = physical_offset;
+	LibKernel::Memory::SetSystemDirectMemoryReserve(end - start);
 	return OK;

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '4135,4215p' src/libs/agc.cpp

Repository: KytyPS5/KytyPS5

Length of output: 3297


🏁 Script executed:

set -e
printf '%s\n' '--- head function ---'
nl -ba src/libs/agc.cpp | sed -n '4145,4210p'
printf '%s\n' '--- base-to-head diff for Initialize ---'
git diff --no-ext-diff --unified=80 730e1a3cf39d50c787d477e813e0bef92006a063 9a6ed5c84b7126c91c9ebaf2b3dca39aa842ba03 -- src/libs/agc.cpp | sed -n '/int Initialize()/,/static void ReleaseDmem()/p'

Repository: KytyPS5/KytyPS5

Length of output: 7221


Remove the first allocation block.

Gen5Driver::Initialize declares physical_offset, end, start, result, and address at lines 4155–4172. Lines 4178–4196 redeclare the same identifiers in the same scope. The C++ compiler rejects these redeclarations, so builds that compile src/libs/agc.cpp fail.

Suggested fix
-	int64_t    physical_offset = -1;
-	const auto end             = LibKernel::Memory::KernelGetDirectMemorySize();
-	const auto start           = Common::AlignDown(end - DriverDmemSize, DriverDmemSize);
-	auto       result          = LibKernel::Memory::KernelAllocateDirectMemory(
-	    start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset);
-	if (result != OK) {
-		return result;
-	}
-	void* address = reinterpret_cast<void*>(DriverDmemBase);
-	result        = LibKernel::Memory::KernelMapNamedDirectMemory(
-	    &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver");
-	if (result != OK) {
-		EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset,
-		                                                            DriverDmemSize) != OK);
-		return result;
-	}
-	EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase);
-	std::memset(address, 0, DriverDmemSize);
 	struct FunctionShaderBindings {

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Restore AGC system-table initialization after mapping.

When the mapped driver block is available, Gen5Driver::Initialize currently clears it and returns without writing the descriptor, eight function records, or validator shader. Gen5::AgcInit does not populate these entries. CheckAgcSystemTable requires all of them, so AGC initialization leaves an invalid system table and the system-table test can fail.

Suggested fix
 	std::memset(address, 0, DriverDmemSize);
+	struct FunctionShaderBindings {
+		uint64_t code;
+		uint32_t user_data[2];
+	};
+	static_assert(sizeof(FunctionShaderBindings) == 16);
+	static constexpr std::array<uint32_t, 16> validator{
+		0xbeeb03ff, 0x00000021, 0xb96a1818, 0xbf06106a,
+		0xbf800000, 0x8590807e, 0xb96a0a18, 0xbf066a80,
+		0xbf800000, 0x85ea807e, 0x88ea106a, 0xbf870003,
+		0xbefc03ff, 0x8a6ca000, 0xbf920009, 0xbefd210e,
+	};
+	const auto base = DriverDmemBase + AgcDmemOffset;
+	const auto function_table = base + 0x60;
+	const auto code_address = base + 0x100;
+	std::array<FunctionShaderBindings, 8> functions{};
+	functions[2].code = code_address;
+	const std::array<uint32_t, 4> descriptor{
+		static_cast<uint32_t>(function_table),
+		static_cast<uint32_t>(function_table >> 32u) | 0x00100000u,
+		static_cast<uint32_t>(functions.size()), 0x5204u,
+	};
+	std::memcpy(reinterpret_cast<void*>(function_table), functions.data(),
+	            sizeof(functions));
+	std::memcpy(reinterpret_cast<void*>(code_address), validator.data(),
+	            sizeof(validator));
+	std::memcpy(reinterpret_cast<void*>(base), descriptor.data(),
+	            sizeof(descriptor));
 	g_driver_dmem.physical_offset = physical_offset;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

	int64_t    physical_offset = -1;
	const auto reported_end    = LibKernel::Memory::KernelGetDirectMemorySize();
	const auto end             = Common::AlignDown(reported_end, DriverDmemSize);
	const auto start           = end - DriverDmemSize;
	auto       result          = LibKernel::Memory::KernelAllocateDirectMemory(
	    start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset);
	if (result != OK) {
		return result;
	}
	void* address = reinterpret_cast<void*>(DriverDmemBase);
	result        = LibKernel::Memory::KernelMapNamedDirectMemory(
	    &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver");
	if (result != OK) {
		EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset,
		                                                            DriverDmemSize) != OK);
		return result;
	}
	EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase);
	std::memset(address, 0, DriverDmemSize);
	struct FunctionShaderBindings {
		uint64_t code;
		uint32_t user_data[2];
	};
	static_assert(sizeof(FunctionShaderBindings) == 16);
	static constexpr std::array<uint32_t, 16> validator{
		0xbeeb03ff, 0x00000021, 0xb96a1818, 0xbf06106a,
		0xbf800000, 0x8590807e, 0xb96a0a18, 0xbf066a80,
		0xbf800000, 0x85ea807e, 0x88ea106a, 0xbf870003,
		0xbefc03ff, 0x8a6ca000, 0xbf920009, 0xbefd210e,
	};
	const auto base = DriverDmemBase + AgcDmemOffset;
	const auto function_table = base + 0x60;
	const auto code_address = base + 0x100;
	std::array<FunctionShaderBindings, 8> functions{};
	functions[2].code = code_address;
	const std::array<uint32_t, 4> descriptor{
		static_cast<uint32_t>(function_table),
		static_cast<uint32_t>(function_table >> 32u) | 0x00100000u,
		static_cast<uint32_t>(functions.size()), 0x5204u,
	};
	std::memcpy(reinterpret_cast<void*>(function_table), functions.data(),
	            sizeof(functions));
	std::memcpy(reinterpret_cast<void*>(code_address), validator.data(),
	            sizeof(validator));
	std::memcpy(reinterpret_cast<void*>(base), descriptor.data(),
	            sizeof(descriptor));
	g_driver_dmem.physical_offset = physical_offset;
	LibKernel::Memory::SetSystemDirectMemoryReserve(end - start);
	return OK;

✅ Confirmed as addressed by @⁠kewalker

**@⁠coderabbitai[bot]** on GitHub · 2026-10-11 03:34 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#discussion_r4240121731) · edited 2026-10-11 07:53 UTC On `src/libs/agc.cpp` line 4199 (outdated): ```diff @@ -4180,36 +4175,37 @@ int Initialize() { uint32_t user_data[2]; }; static_assert(sizeof(FunctionShaderBindings) == 16); - // Ordered-count queue validator, including its return to the caller. - static constexpr std::array<uint32_t, 16> validator { - 0xbeeb03ff, 0x00000021, 0xb96a1818, 0xbf06106a, - 0xbf800000, 0x8590807e, 0xb96a0a18, 0xbf066a80, - 0xbf800000, 0x85ea807e, 0x88ea106a, 0xbf870003, - 0xbefc03ff, 0x8a6ca000, 0xbf920009, 0xbefd210e, - }; - const auto base = DriverDmemBase + AgcDmemOffset; - const auto function_table = base + 0x60; - const auto code_address = base + 0x100; // Shader code requires 256-byte alignment. - std::array<FunctionShaderBindings, 8> functions {}; - functions[2].code = code_address; - const std::array<uint32_t, 4> descriptor { - static_cast<uint32_t>(function_table), - static_cast<uint32_t>(function_table >> 32u) | 0x00100000u, - static_cast<uint32_t>(functions.size()), 0x5204u, - }; - std::memcpy(reinterpret_cast<void*>(function_table), functions.data(), sizeof(functions)); - std::memcpy(reinterpret_cast<void*>(code_address), validator.data(), sizeof(validator)); - std::memcpy(reinterpret_cast<void*>(base), descriptor.data(), sizeof(descriptor)); + int64_t physical_offset = -1; + const auto reported_end = LibKernel::Memory::KernelGetDirectMemorySize(); + const auto end = Common::AlignDown(reported_end, DriverDmemSize); + const auto start = end - DriverDmemSize; + auto result = LibKernel::Memory::KernelAllocateDirectMemory( + start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset); + if (result != OK) { + return result; + } + void* address = reinterpret_cast<void*>(DriverDmemBase); + result = LibKernel::Memory::KernelMapNamedDirectMemory( + &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver"); + if (result != OK) { + EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset, + DriverDmemSize) != OK); + return result; + } + EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase); + std::memset(address, 0, DriverDmemSize); g_driver_dmem.physical_offset = physical_offset; + LibKernel::Memory::SetSystemDirectMemoryReserve(end - start); return OK; ``` **🎯 Functional Correctness** | **🟠 Major** | **⚡ Quick win** <details> <summary>🔎 Supported by static analysis</summary> 🏁 Script executed: ```bash sed -n '4135,4215p' src/libs/agc.cpp ``` Repository: KytyPS5/KytyPS5 Length of output: 3297 --- 🏁 Script executed: ```bash set -e printf '%s\n' '--- head function ---' nl -ba src/libs/agc.cpp | sed -n '4145,4210p' printf '%s\n' '--- base-to-head diff for Initialize ---' git diff --no-ext-diff --unified=80 730e1a3cf39d50c787d477e813e0bef92006a063 9a6ed5c84b7126c91c9ebaf2b3dca39aa842ba03 -- src/libs/agc.cpp | sed -n '/int Initialize()/,/static void ReleaseDmem()/p' ``` Repository: KytyPS5/KytyPS5 Length of output: 7221 --- </details> **Remove the first allocation block.** `Gen5Driver::Initialize` declares `physical_offset`, `end`, `start`, `result`, and `address` at lines 4155–4172. Lines 4178–4196 redeclare the same identifiers in the same scope. The C++ compiler rejects these redeclarations, so builds that compile `src/libs/agc.cpp` fail. <details> <summary>Suggested fix</summary> ```diff - int64_t physical_offset = -1; - const auto end = LibKernel::Memory::KernelGetDirectMemorySize(); - const auto start = Common::AlignDown(end - DriverDmemSize, DriverDmemSize); - auto result = LibKernel::Memory::KernelAllocateDirectMemory( - start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset); - if (result != OK) { - return result; - } - void* address = reinterpret_cast<void*>(DriverDmemBase); - result = LibKernel::Memory::KernelMapNamedDirectMemory( - &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver"); - if (result != OK) { - EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset, - DriverDmemSize) != OK); - return result; - } - EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase); - std::memset(address, 0, DriverDmemSize); struct FunctionShaderBindings { ``` </details> <!-- fingerprinting:phantom:medusa:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:22a6e35576afed5ac7d669bb --> --- **🎯 Functional Correctness** | **🟠 Major** | **⚡ Quick win** **Restore AGC system-table initialization after mapping.** When the mapped driver block is available, `Gen5Driver::Initialize` currently clears it and returns without writing the descriptor, eight function records, or validator shader. `Gen5::AgcInit` does not populate these entries. `CheckAgcSystemTable` requires all of them, so AGC initialization leaves an invalid system table and the system-table test can fail. <details> <summary>Suggested fix</summary> ```diff std::memset(address, 0, DriverDmemSize); + struct FunctionShaderBindings { + uint64_t code; + uint32_t user_data[2]; + }; + static_assert(sizeof(FunctionShaderBindings) == 16); + static constexpr std::array<uint32_t, 16> validator{ + 0xbeeb03ff, 0x00000021, 0xb96a1818, 0xbf06106a, + 0xbf800000, 0x8590807e, 0xb96a0a18, 0xbf066a80, + 0xbf800000, 0x85ea807e, 0x88ea106a, 0xbf870003, + 0xbefc03ff, 0x8a6ca000, 0xbf920009, 0xbefd210e, + }; + const auto base = DriverDmemBase + AgcDmemOffset; + const auto function_table = base + 0x60; + const auto code_address = base + 0x100; + std::array<FunctionShaderBindings, 8> functions{}; + functions[2].code = code_address; + const std::array<uint32_t, 4> descriptor{ + static_cast<uint32_t>(function_table), + static_cast<uint32_t>(function_table >> 32u) | 0x00100000u, + static_cast<uint32_t>(functions.size()), 0x5204u, + }; + std::memcpy(reinterpret_cast<void*>(function_table), functions.data(), + sizeof(functions)); + std::memcpy(reinterpret_cast<void*>(code_address), validator.data(), + sizeof(validator)); + std::memcpy(reinterpret_cast<void*>(base), descriptor.data(), + sizeof(descriptor)); g_driver_dmem.physical_offset = physical_offset; ``` </details> <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. ```suggestion int64_t physical_offset = -1; const auto reported_end = LibKernel::Memory::KernelGetDirectMemorySize(); const auto end = Common::AlignDown(reported_end, DriverDmemSize); const auto start = end - DriverDmemSize; auto result = LibKernel::Memory::KernelAllocateDirectMemory( start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset); if (result != OK) { return result; } void* address = reinterpret_cast<void*>(DriverDmemBase); result = LibKernel::Memory::KernelMapNamedDirectMemory( &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver"); if (result != OK) { EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset, DriverDmemSize) != OK); return result; } EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase); std::memset(address, 0, DriverDmemSize); struct FunctionShaderBindings { uint64_t code; uint32_t user_data[2]; }; static_assert(sizeof(FunctionShaderBindings) == 16); static constexpr std::array<uint32_t, 16> validator{ 0xbeeb03ff, 0x00000021, 0xb96a1818, 0xbf06106a, 0xbf800000, 0x8590807e, 0xb96a0a18, 0xbf066a80, 0xbf800000, 0x85ea807e, 0x88ea106a, 0xbf870003, 0xbefc03ff, 0x8a6ca000, 0xbf920009, 0xbefd210e, }; const auto base = DriverDmemBase + AgcDmemOffset; const auto function_table = base + 0x60; const auto code_address = base + 0x100; std::array<FunctionShaderBindings, 8> functions{}; functions[2].code = code_address; const std::array<uint32_t, 4> descriptor{ static_cast<uint32_t>(function_table), static_cast<uint32_t>(function_table >> 32u) | 0x00100000u, static_cast<uint32_t>(functions.size()), 0x5204u, }; std::memcpy(reinterpret_cast<void*>(function_table), functions.data(), sizeof(functions)); std::memcpy(reinterpret_cast<void*>(code_address), validator.data(), sizeof(validator)); std::memcpy(reinterpret_cast<void*>(base), descriptor.data(), sizeof(descriptor)); g_driver_dmem.physical_offset = physical_offset; LibKernel::Memory::SetSystemDirectMemoryReserve(end - start); return OK; ``` </details> <!-- suggestion_end --> <!-- fingerprinting:phantom:medusa:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:c24f2a2a4b164a4290d55812 --> <!-- This is an auto-generated reply by CodeRabbit --> ✅ Confirmed as addressed by @⁠kewalker <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-11 03:34 UTC · original · reviewed

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 51acd021-6ed4-4214-8ccd-26626a20dfa8
📥 Commits

Reviewing files that changed from the base of the PR and between 9a4f004fca and 9a6ed5c84b.

📒 Files selected for processing (1)
  • src/libs/agc.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-11 03:34 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#pullrequestreview-5481746143) · **reviewed** **Actionable comments posted: 2** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `51acd021-6ed4-4214-8ccd-26626a20dfa8` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 9a4f004fcaac0d543a4e58a51a8b4214c773e446 and 9a6ed5c84b7126c91c9ebaf2b3dca39aa842ba03. </details> <details> <summary>📒 Files selected for processing (1)</summary> * `src/libs/agc.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=95a363b7-400b-4e7b-ad21-d0fbb5ad4249 attempt=1000d6d0-b18b-4c6d-bc3c-3165788d14b9 batch=1/1 -->
Author
Owner

@⁠kewalker on GitHub · 2026-10-11 07:52 UTC · original
On src/libs/agc.cpp line 4199 (outdated):

@@ -4180,36 +4175,37 @@ int Initialize() {
 		uint32_t user_data[2];
 	};
 	static_assert(sizeof(FunctionShaderBindings) == 16);
-	// Ordered-count queue validator, including its return to the caller.
-	static constexpr std::array<uint32_t, 16> validator {
-	    0xbeeb03ff, 0x00000021, 0xb96a1818, 0xbf06106a,
-	    0xbf800000, 0x8590807e, 0xb96a0a18, 0xbf066a80,
-	    0xbf800000, 0x85ea807e, 0x88ea106a, 0xbf870003,
-	    0xbefc03ff, 0x8a6ca000, 0xbf920009, 0xbefd210e,
-	};
-	const auto base = DriverDmemBase + AgcDmemOffset;
-	const auto function_table = base + 0x60;
-	const auto code_address = base + 0x100; // Shader code requires 256-byte alignment.
-	std::array<FunctionShaderBindings, 8> functions {};
-	functions[2].code = code_address;
-	const std::array<uint32_t, 4> descriptor {
-	    static_cast<uint32_t>(function_table),
-	    static_cast<uint32_t>(function_table >> 32u) | 0x00100000u,
-	    static_cast<uint32_t>(functions.size()), 0x5204u,
-	};
-	std::memcpy(reinterpret_cast<void*>(function_table), functions.data(), sizeof(functions));
-	std::memcpy(reinterpret_cast<void*>(code_address), validator.data(), sizeof(validator));
-	std::memcpy(reinterpret_cast<void*>(base), descriptor.data(), sizeof(descriptor));
+	int64_t    physical_offset = -1;
+	const auto reported_end    = LibKernel::Memory::KernelGetDirectMemorySize();
+	const auto end             = Common::AlignDown(reported_end, DriverDmemSize);
+	const auto start           = end - DriverDmemSize;
+	auto       result          = LibKernel::Memory::KernelAllocateDirectMemory(
+	    start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset);
+	if (result != OK) {
+		return result;
+	}
+	void* address = reinterpret_cast<void*>(DriverDmemBase);
+	result        = LibKernel::Memory::KernelMapNamedDirectMemory(
+	    &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver");
+	if (result != OK) {
+		EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset,
+		                                                            DriverDmemSize) != OK);
+		return result;
+	}
+	EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase);
+	std::memset(address, 0, DriverDmemSize);
 	g_driver_dmem.physical_offset = physical_offset;
+	LibKernel::Memory::SetSystemDirectMemoryReserve(end - start);
 	return OK;

Resolved the new test-variable conflict using upstream's small_array name. Repaired the misapplied AGC suggestion by removing the duplicate allocation block and restoring the descriptor, eight function records, and validator shader.

The allocation now explicitly aligns the physical end down before choosing the 2 MiB block. The advertised guest budget ends at the block's start: any remaining alignment padding above the block is deliberately hidden as well. Reporting only a 2 MiB deduction for an unaligned pool would expose part of the driver mapping inside the guest budget. Added --agc-unaligned-pool-only coverage alongside the original exhaustion/system-table test.

The docstring coverage threshold remains unchanged as requested.

Validation passed: --agc-system-table-only and --agc-unaligned-pool-only, the formatting hook, and git diff --check.

**@⁠kewalker** on GitHub · 2026-10-11 07:52 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#discussion_r4240770050) On `src/libs/agc.cpp` line 4199 (outdated): ```diff @@ -4180,36 +4175,37 @@ int Initialize() { uint32_t user_data[2]; }; static_assert(sizeof(FunctionShaderBindings) == 16); - // Ordered-count queue validator, including its return to the caller. - static constexpr std::array<uint32_t, 16> validator { - 0xbeeb03ff, 0x00000021, 0xb96a1818, 0xbf06106a, - 0xbf800000, 0x8590807e, 0xb96a0a18, 0xbf066a80, - 0xbf800000, 0x85ea807e, 0x88ea106a, 0xbf870003, - 0xbefc03ff, 0x8a6ca000, 0xbf920009, 0xbefd210e, - }; - const auto base = DriverDmemBase + AgcDmemOffset; - const auto function_table = base + 0x60; - const auto code_address = base + 0x100; // Shader code requires 256-byte alignment. - std::array<FunctionShaderBindings, 8> functions {}; - functions[2].code = code_address; - const std::array<uint32_t, 4> descriptor { - static_cast<uint32_t>(function_table), - static_cast<uint32_t>(function_table >> 32u) | 0x00100000u, - static_cast<uint32_t>(functions.size()), 0x5204u, - }; - std::memcpy(reinterpret_cast<void*>(function_table), functions.data(), sizeof(functions)); - std::memcpy(reinterpret_cast<void*>(code_address), validator.data(), sizeof(validator)); - std::memcpy(reinterpret_cast<void*>(base), descriptor.data(), sizeof(descriptor)); + int64_t physical_offset = -1; + const auto reported_end = LibKernel::Memory::KernelGetDirectMemorySize(); + const auto end = Common::AlignDown(reported_end, DriverDmemSize); + const auto start = end - DriverDmemSize; + auto result = LibKernel::Memory::KernelAllocateDirectMemory( + start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset); + if (result != OK) { + return result; + } + void* address = reinterpret_cast<void*>(DriverDmemBase); + result = LibKernel::Memory::KernelMapNamedDirectMemory( + &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver"); + if (result != OK) { + EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset, + DriverDmemSize) != OK); + return result; + } + EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase); + std::memset(address, 0, DriverDmemSize); g_driver_dmem.physical_offset = physical_offset; + LibKernel::Memory::SetSystemDirectMemoryReserve(end - start); return OK; ``` Resolved the new test-variable conflict using upstream's `small_array` name. Repaired the misapplied AGC suggestion by removing the duplicate allocation block and restoring the descriptor, eight function records, and validator shader. The allocation now explicitly aligns the physical end down before choosing the 2 MiB block. The advertised guest budget ends at the block's start: any remaining alignment padding above the block is deliberately hidden as well. Reporting only a 2 MiB deduction for an unaligned pool would expose part of the driver mapping inside the guest budget. Added `--agc-unaligned-pool-only` coverage alongside the original exhaustion/system-table test. The docstring coverage threshold remains unchanged as requested. Validation passed: --agc-system-table-only and --agc-unaligned-pool-only, the formatting hook, and git diff --check.
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-11 07:53 UTC · original
On src/libs/agc.cpp line 4199 (outdated):

@@ -4180,36 +4175,37 @@ int Initialize() {
 		uint32_t user_data[2];
 	};
 	static_assert(sizeof(FunctionShaderBindings) == 16);
-	// Ordered-count queue validator, including its return to the caller.
-	static constexpr std::array<uint32_t, 16> validator {
-	    0xbeeb03ff, 0x00000021, 0xb96a1818, 0xbf06106a,
-	    0xbf800000, 0x8590807e, 0xb96a0a18, 0xbf066a80,
-	    0xbf800000, 0x85ea807e, 0x88ea106a, 0xbf870003,
-	    0xbefc03ff, 0x8a6ca000, 0xbf920009, 0xbefd210e,
-	};
-	const auto base = DriverDmemBase + AgcDmemOffset;
-	const auto function_table = base + 0x60;
-	const auto code_address = base + 0x100; // Shader code requires 256-byte alignment.
-	std::array<FunctionShaderBindings, 8> functions {};
-	functions[2].code = code_address;
-	const std::array<uint32_t, 4> descriptor {
-	    static_cast<uint32_t>(function_table),
-	    static_cast<uint32_t>(function_table >> 32u) | 0x00100000u,
-	    static_cast<uint32_t>(functions.size()), 0x5204u,
-	};
-	std::memcpy(reinterpret_cast<void*>(function_table), functions.data(), sizeof(functions));
-	std::memcpy(reinterpret_cast<void*>(code_address), validator.data(), sizeof(validator));
-	std::memcpy(reinterpret_cast<void*>(base), descriptor.data(), sizeof(descriptor));
+	int64_t    physical_offset = -1;
+	const auto reported_end    = LibKernel::Memory::KernelGetDirectMemorySize();
+	const auto end             = Common::AlignDown(reported_end, DriverDmemSize);
+	const auto start           = end - DriverDmemSize;
+	auto       result          = LibKernel::Memory::KernelAllocateDirectMemory(
+	    start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset);
+	if (result != OK) {
+		return result;
+	}
+	void* address = reinterpret_cast<void*>(DriverDmemBase);
+	result        = LibKernel::Memory::KernelMapNamedDirectMemory(
+	    &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver");
+	if (result != OK) {
+		EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset,
+		                                                            DriverDmemSize) != OK);
+		return result;
+	}
+	EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase);
+	std::memset(address, 0, DriverDmemSize);
 	g_driver_dmem.physical_offset = physical_offset;
+	LibKernel::Memory::SetSystemDirectMemoryReserve(end - start);
 	return OK;

@kewalker Thanks for the fixes and the clarification.

The supplied Gen5Driver::Initialize now has one allocation block and writes the descriptor, eight function records, and validator shader after mapping. This addresses both findings.

The reserve calculation, reported_end - start, also hides the alignment padding so the advertised guest budget ends before the driver block. Thanks for adding the unaligned-pool test and reporting that both test selectors passed.


✏️ Learnings added
Learnt from: kewalker
Repo: KytyPS5/KytyPS5 PR: 1399
File: src/libs/agc.cpp:0-0
Timestamp: 2026-10-11T07:52:59.893Z
Learning: In the C++ AGC driver in src/libs/agc.cpp, Libs::Graphics::Gen5Driver::Initialize aligns the reported physical-memory end down to DriverDmemSize before allocating the driver block. The system direct-memory reserve must include both the driver block and any alignment padding above it: reported_end - start. This keeps the advertised guest budget ending at the driver block's start. Deducting only DriverDmemSize for an unaligned pool would expose part of the driver mapping within the advertised guest budget.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

✅ Review thread resolved.

You are interacting with an AI system.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-11 07:53 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#discussion_r4240771319) On `src/libs/agc.cpp` line 4199 (outdated): ```diff @@ -4180,36 +4175,37 @@ int Initialize() { uint32_t user_data[2]; }; static_assert(sizeof(FunctionShaderBindings) == 16); - // Ordered-count queue validator, including its return to the caller. - static constexpr std::array<uint32_t, 16> validator { - 0xbeeb03ff, 0x00000021, 0xb96a1818, 0xbf06106a, - 0xbf800000, 0x8590807e, 0xb96a0a18, 0xbf066a80, - 0xbf800000, 0x85ea807e, 0x88ea106a, 0xbf870003, - 0xbefc03ff, 0x8a6ca000, 0xbf920009, 0xbefd210e, - }; - const auto base = DriverDmemBase + AgcDmemOffset; - const auto function_table = base + 0x60; - const auto code_address = base + 0x100; // Shader code requires 256-byte alignment. - std::array<FunctionShaderBindings, 8> functions {}; - functions[2].code = code_address; - const std::array<uint32_t, 4> descriptor { - static_cast<uint32_t>(function_table), - static_cast<uint32_t>(function_table >> 32u) | 0x00100000u, - static_cast<uint32_t>(functions.size()), 0x5204u, - }; - std::memcpy(reinterpret_cast<void*>(function_table), functions.data(), sizeof(functions)); - std::memcpy(reinterpret_cast<void*>(code_address), validator.data(), sizeof(validator)); - std::memcpy(reinterpret_cast<void*>(base), descriptor.data(), sizeof(descriptor)); + int64_t physical_offset = -1; + const auto reported_end = LibKernel::Memory::KernelGetDirectMemorySize(); + const auto end = Common::AlignDown(reported_end, DriverDmemSize); + const auto start = end - DriverDmemSize; + auto result = LibKernel::Memory::KernelAllocateDirectMemory( + start, end, DriverDmemSize, DriverDmemSize, 12, &physical_offset); + if (result != OK) { + return result; + } + void* address = reinterpret_cast<void*>(DriverDmemBase); + result = LibKernel::Memory::KernelMapNamedDirectMemory( + &address, DriverDmemSize, 0x33, 0, physical_offset, DriverDmemSize, "SceAgcDriver"); + if (result != OK) { + EXIT_IF(LibKernel::Memory::KernelCheckedReleaseDirectMemory(physical_offset, + DriverDmemSize) != OK); + return result; + } + EXIT_IF(reinterpret_cast<uint64_t>(address) != DriverDmemBase); + std::memset(address, 0, DriverDmemSize); g_driver_dmem.physical_offset = physical_offset; + LibKernel::Memory::SetSystemDirectMemoryReserve(end - start); return OK; ``` `@kewalker` Thanks for the fixes and the clarification. The supplied `Gen5Driver::Initialize` now has one allocation block and writes the descriptor, eight function records, and validator shader after mapping. This addresses both findings. The reserve calculation, `reported_end - start`, also hides the alignment padding so the advertised guest budget ends before the driver block. Thanks for adding the unaligned-pool test and reporting that both test selectors passed. --- <details> <summary>✏️ Learnings added</summary> ``` Learnt from: kewalker Repo: KytyPS5/KytyPS5 PR: 1399 File: src/libs/agc.cpp:0-0 Timestamp: 2026-10-11T07:52:59.893Z Learning: In the C++ AGC driver in src/libs/agc.cpp, Libs::Graphics::Gen5Driver::Initialize aligns the reported physical-memory end down to DriverDmemSize before allocating the driver block. The system direct-memory reserve must include both the driver block and any alignment padding above it: reported_end - start. This keeps the advertised guest budget ending at the driver block's start. Deducting only DriverDmemSize for an unaligned pool would expose part of the driver mapping within the advertised guest budget. ``` > Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure [review instructions](https://docs.coderabbit.ai/guides/review-instructions). You can manage existing learnings in the [app](https://app.coderabbit.ai/learnings). </details> --- <!-- This is an auto-generated comment: tweet message by coderabbit.ai --> If you found this review helpful, would you consider giving us a shout-out on [X](https://twitter.com/intent/tweet?related=CodeRabbitAI&text=Aligning%20the%20AGC%20pool%20and%20restoring%20its%20descriptor%2C%20function%20records%2C%20and%20validator%20shader%20prevents%20guest%20memory%20overlap%20and%20keeps%20the%20system%20table%20valid.%20%40coderabbitai%20catches%20the%20details%20that%20matter%E2%80%94give%20it%20a%20try!%20%F0%9F%9B%A0%EF%B8%8F%E2%9C%85&url=https%3A%2F%2Fgithub.com%2FKytyPS5%2FKytyPS5%2Fpull%2F1399%23discussion_r4240121731)? Thank you for using CodeRabbit! <!-- end of auto-generated comment: tweet message by coderabbit.ai --> ✅ Review thread resolved. _You are interacting with an AI system._ <!-- This is an auto-generated reply by CodeRabbit -->
Author
Owner

@⁠kewalker on GitHub · 2026-10-11 14:06 UTC · original

@⁠itsmemac CodeRabbit suggested you as a reviewer for this AGC startup fix. Would you be able to review it when you have time? The normal and unaligned-pool AGC regression tests passed locally, and CodeRabbit's actionable findings have been addressed. Thank you.

**@⁠kewalker** on GitHub · 2026-10-11 14:06 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#issuecomment-6109855183) @⁠itsmemac CodeRabbit suggested you as a reviewer for this AGC startup fix. Would you be able to review it when you have time? The normal and unaligned-pool AGC regression tests passed locally, and CodeRabbit's actionable findings have been addressed. Thank you.
Author
Owner

@⁠theantipopau on GitHub · 2026-10-12 11:14 UTC · original

Verified the mechanism against main: g_system_direct_memory_reserve is set by the AGC driver's reserve path and subtracted inside the reported PhysicalMemory::Size(), so guest flexible-memory sizing (DEFAULT_FLEXIBLE_MEMORY_SIZE accounting in MapFlexibleMemory/antisize checks) no longer counts the driver's own backing that was allocated after the size was published. The Initialize() reset-once at line ~1013 plus atomic store/load makes ordering safe: the reserve store happens after PhysicalMemory exists and before grow callbacks that read it. The two test exits ("driver memory was not reserved before guest allocation", "guest memory size still includes the reserved driver block") lock the failure mode in, good.

Holding off a formal approve purely because CI is behind the action_required maintainer gate on this fork (unverified-green locally visible only to maintainers); no code objection. If the Demon's Souls title area changes again after this, the interleaving between ReplaceFixedRangeWithReserved and this reserve is worth one more look before merge — the reserve lands in system direct memory whose ranges ReplaceSpan can touch with VirtualRangeType::Reserved pending before replacement.

**@⁠theantipopau** on GitHub · 2026-10-12 11:14 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#issuecomment-6123138138) Verified the mechanism against main: `g_system_direct_memory_reserve` is set by the AGC driver's reserve path and subtracted inside the reported `PhysicalMemory::Size()`, so guest flexible-memory sizing (`DEFAULT_FLEXIBLE_MEMORY_SIZE` accounting in `MapFlexibleMemory`/antisize checks) no longer counts the driver's own backing that was allocated **after** the size was published. The `Initialize()` reset-once at line ~1013 plus atomic store/load makes ordering safe: the reserve store happens after `PhysicalMemory` exists and before grow callbacks that read it. The two test exits ("driver memory was not reserved before guest allocation", "guest memory size still includes the reserved driver block") lock the failure mode in, good. Holding off a formal approve purely because CI is behind the `action_required` maintainer gate on this fork (unverified-green locally visible only to maintainers); no code objection. If the Demon's Souls title area changes again after this, the interleaving between `ReplaceFixedRangeWithReserved` and this reserve is worth one more look before merge — the reserve lands in system direct memory whose ranges `ReplaceSpan` can touch with `VirtualRangeType::Reserved` pending before replacement.
@ -2636,3 +2642,3 @@
PRINT_NAME();
return PhysicalMemory::Size();
return PhysicalMemory::Size() - g_system_direct_memory_reserve.load(std::memory_order_relaxed);
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-14 00:53 UTC · original · edited 2026-10-14 00:58 UTC

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Apply the guest budget to main direct-memory allocations.

If PhysicalMemory::Size() has an unaligned tail, AGC excludes that tail from KernelGetDirectMemorySize() but does not allocate it. KernelAllocateMainDirectMemory still searches through PhysicalMemory::Size(). A guest can therefore allocate memory beyond its reported budget. Use the reported guest limit for main allocations, while keeping AGC’s driver allocation able to use the physical limit.

✅ Addressed in commit f3fd301

**@⁠coderabbitai[bot]** on GitHub · 2026-10-14 00:53 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#discussion_r4265653570) · edited 2026-10-14 00:58 UTC **🎯 Functional Correctness** | **🟡 Minor** | **⚡ Quick win** **Apply the guest budget to main direct-memory allocations.** If `PhysicalMemory::Size()` has an unaligned tail, AGC excludes that tail from `KernelGetDirectMemorySize()` but does not allocate it. `KernelAllocateMainDirectMemory` still searches through `PhysicalMemory::Size()`. A guest can therefore allocate memory beyond its reported budget. Use the reported guest limit for main allocations, while keeping AGC’s driver allocation able to use the physical limit. <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:3eee557ce8d6d58ff6ff46e0 --> <!-- This is an auto-generated comment by CodeRabbit --> ✅ Addressed in commit f3fd301
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-14 00:54 UTC · original · reviewed

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 612b512e-adc9-422d-a655-fc8f9a030316
📥 Commits

Reviewing files that changed from the base of the PR and between b8939cc5ba and 5d81c42ff0.

📒 Files selected for processing: 3
  • src/kernel/memory.cpp
  • src/kernel/memory.h
  • tests/ShaderRecompilerComputeTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-14 00:54 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1399#pullrequestreview-5511816290) · **reviewed** **Actionable comments posted: 1** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `612b512e-adc9-422d-a655-fc8f9a030316` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between b8939cc5ba8478443394a4f712bb7c3611eb052f and 5d81c42ff08df9808dabf87f6273cee06f30c8a8. </details> <details> <summary>📒 Files selected for processing: 3</summary> * `src/kernel/memory.cpp` * `src/kernel/memory.h` * `tests/ShaderRecompilerComputeTests.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=ea3a1bd5-92ca-4b4b-9bd5-5b4d86f2be8d attempt=0f4fc7a5-3155-4b30-9c94-7def8b9b86d0 batch=1/1 -->
This pull request has changes conflicting with the target branch.
  • src/kernel/memory.cpp
  • src/kernel/memory.h
  • src/libs/agc.cpp
  • tests/ShaderRecompilerComputeTests.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1399/head:github-bot/gh-1399'
git switch 'github-bot/gh-1399'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1399'
git switch 'github-bot/gh-1399'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1399'
git switch 'github-bot/gh-1399'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1399'
git switch 'main'
git merge --squash 'github-bot/gh-1399'
git switch 'main'
git merge --ff-only 'github-bot/gh-1399'
git switch 'main'
git merge 'github-bot/gh-1399'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.