Skip to content

Commit f4fe741

Browse files
Youjie ZhengYoujie Zheng
Youjie Zheng
authored and
Youjie Zheng
committed
bugfix: Putting fence.i after loading apps
1 parent 564d885 commit f4fe741

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

os/src/loader.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ pub fn load_apps() {
7070
let num_app_ptr = _num_app as usize as *const usize;
7171
let num_app = get_num_app();
7272
let app_start = unsafe { core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) };
73-
// clear i-cache first
74-
unsafe {
75-
asm!("fence.i");
76-
}
7773
// load apps
7874
for i in 0..num_app {
7975
let base_i = get_base_i(i);
@@ -87,6 +83,15 @@ pub fn load_apps() {
8783
let dst = unsafe { core::slice::from_raw_parts_mut(base_i as *mut u8, src.len()) };
8884
dst.copy_from_slice(src);
8985
}
86+
// Memory fence about fetching the instruction memory
87+
// It is guaranteed that a subsequent instruction fetch must
88+
// observes all previous writes to the instruction memory.
89+
// Therefore, fence.i must be executed after we have loaded
90+
// the code of the next app into the instruction memory.
91+
// See also: riscv non-priv spec chapter 3, 'Zifencei' extension.
92+
unsafe {
93+
asm!("fence.i");
94+
}
9095
}
9196

9297
/// get app info with entry and sp and save `TrapContext` in kernel stack

0 commit comments

Comments
 (0)