Fix GICD initialization failure in VirtCCA/Realm by preventing post-index addressing#22
Merged
ZR233 merged 4 commits intorcore-os:masterfrom Mar 26, 2026
Merged
Fix GICD initialization failure in VirtCCA/Realm by preventing post-index addressing#22ZR233 merged 4 commits intorcore-os:masterfrom
ZR233 merged 4 commits intorcore-os:masterfrom
Conversation
…ters Signed-off-by: luozhixian <luozhixian@kylinos.cn>
ZR233
requested changes
Mar 25, 2026
| let num_regs = num_regs.min(self.ICENABLER.len()); | ||
|
|
||
| for i in 0..num_regs { | ||
| self.ICENABLER[i].set(u32::MAX); |
Member
There was a problem hiding this comment.
感谢这个 PR,这里有一个潜在问题:
black_box 本质是 best-effort 的优化屏障
Rust / LLVM 没有保证它一定会阻止某种特定的 codegen(比如 post-index addressing)
不同编译器版本 / 优化级别 / backend 组合下,行为可能变化
从长期来看不太稳。
我比较推荐的方式是:不要让编译器从“数组索引”推导寻址模式,而是显式表达 MMIO 写操作
具体来说,可以考虑:
建议改为统一的寄存器访问 helper,例如:
impl Gicd {
#[inline(always)]
unsafe fn write32(&self, offset: usize, val: u32) {
core::ptr::write_volatile(
self.base.add(offset).cast::<u32>(),
val,
);
}
#[inline(always)]
unsafe fn write_icenabler(&self, n: usize, val: u32) {
self.write32(0x180 + n * 4, val);
}
}然后初始化代码变成:
for i in 0..num {
self.write_icenabler(i, u32::MAX);
}
Contributor
Author
There was a problem hiding this comment.
感谢建议,已经按照建议进行修改,virtcca环境测试可以正常通过初始化。不过有一点不同的是,write32函数使用了inline never。如果使用inline always,在我的编译环境下测试仍然会使用post-index addressing方式寻址。
added 3 commits
March 26, 2026 09:44
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a system hang/crash during the GIC Distributor (GICD) initialization phase in VirtCCA (Realm) confidential computing environments. By introducing
core::hint::black_boxto disrupt compiler optimization, we force the generation of addressing modes without Writeback, ensuring the Hardware Exception Syndrome (ISV bit) remains valid for MMIO emulation.Root Cause Analysis
In confidential computing environments like VirtCCA (based on ARM Confidential Compute Architecture), guest access to GICD MMIO registers triggers a Data Abort exception, which is trapped and handled by the Hypervisor (KVM) or Realm Management Monitor (RMM).
Hardware Limitation (ISV = 0):
According to the ARMv8 Architecture Reference Manual, when a Data Abort is triggered by a Load/Store instruction with Writeback semantics (e.g., Post-indexed:
str w8, [x10], #4or Pre-indexed:str w8, [x10, #4]!), the hardware sets the ISV (Instruction Specific Syndrome Valid) bit in theESR_EL2register to 0.Confidential Computing Impact:
ISV=0, KVM attempts to "fetch and decode" the guest instruction by reading guest memory to determine the target register and value.ISV=1) and without the ability to disassemble guest code, the Hypervisor cannot emulate the MMIO write, leading to a hang or an unrecoverable exception during early boot.Compiler Optimization:
In a standard
forloop, the Rust compiler (LLVM) optimizes address increments by using Post-indexed addressing:str w8, [x10], #4(Result:ISV=0, Emulation Fails)Solution
We introduce
core::hint::black_box(i)within the loop. This breaks the compiler's static analysis of the loop indexi, forcing it to abandon the post-index increment optimization and fall back to Register Offset Addressing:str w12, [x11, x9, lsl #2]Addressing Mode Comparison:
str w8, [x10], #4-> Has Writeback -> ISV=0 -> Realm emulation fails.str w12, [x11, x9, lsl #2]-> No Writeback -> ISV=1 -> Hypervisor reads the syndrome directly fromESR_EL2and emulates the write successfully.Changes
core::hint::black_boxto the loop index inGICDregister clearing/initialization logic.Verification Results