fix bug: 'cargo qemu --ch 2 --release' is hung#17
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug that causes cargo qemu --ch 2 --release to hang in release mode. The issue stems from aggressive compiler optimizations that break assumptions in inline assembly and stack initialization.
Key changes:
- Fixed register corruption in
LocalContext::execute()by using separate variables for context pointer and old sscratch value - Added
#[inline(never)]to prevent inlining issues with inline assembly - Replaced zero-initialized user stack with
MaybeUninitto avoid optimization issues
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| kernel-context/src/lib.rs | Fixed register corruption bug in execute() function by separating context pointer from sscratch register and preventing function inlining |
| ch2/src/main.rs | Changed user stack from zero-initialized array to MaybeUninit and added black_box to prevent compiler optimizations |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| sstatus = inlateout(reg) sstatus, | ||
| execute_naked = sym execute_naked, | ||
| ); | ||
| let _ = old_sscratch; // suppress unused warning |
There was a problem hiding this comment.
The variable old_sscratch is already used in the inline assembly as an output operand (old_ss), so this line is redundant. The compiler won't produce an "unused variable" warning for variables that are used in inline assembly operands.
Suggested change
| let _ = old_sscratch; // suppress unused warning |
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.
在 release 模式下,
cargo qemu --ch 2 --release卡住的原因有两个:1.
kernel-context/src/lib.rs中的execute函数原代码:
在
csrrw指令后,存放self的寄存器被交换成了旧的sscratch值。但编译器在 asm 块后仍然用该寄存器去写self.sepc,导致写入到错误的内存地址。修复:使用独立的寄存器保存上下文指针和旧 sscratch 值:
2.
ch2/src/main.rs中的用户栈初始化原代码
let mut user_stack = [0usize; 256];在 release 模式下,2048 字节的零初始化数组会触发某些优化问题。修复:使用
MaybeUninit跳过零初始化: