-
-
Notifications
You must be signed in to change notification settings - Fork 161
feat(syscall/mincore):实现了mincore系统调用 #1258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fslongjin
merged 8 commits into
DragonOS-Community:master
from
kaleidoscope416:sys_mincore
Sep 24, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
252b107
feat(syscall/mincore):实现了mincore系统调用
kaleidoscope416 5c0b490
往usebuffer直接写入,在get_page处改为拿一次锁
kaleidoscope416 00add4d
Merge branch 'master' into sys_mincore
kaleidoscope416 310c293
更改原先测试
kaleidoscope416 9c09ad2
完成can_do_mincore
kaleidoscope416 a652716
fmt
kaleidoscope416 e9af717
Merge branch 'master' into jiangruizhi-sys_mincore
fslongjin b1cf6bf
fix(mm): 完善mincore系统调用实现
fslongjin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use crate::arch::MMArch; | ||
use crate::{ | ||
arch::mm::PageMapper, | ||
mm::{ucontext::LockedVMA, MemoryManagementArch, VirtAddr}, | ||
}; | ||
use system_error::SystemError; | ||
|
||
impl LockedVMA { | ||
pub fn do_mincore( | ||
&self, | ||
mapper: &PageMapper, | ||
vec: &mut [u8], | ||
start_addr: VirtAddr, | ||
end_addr: VirtAddr, | ||
offset: usize, | ||
) -> Result<(), SystemError> { | ||
let total_pages = (end_addr - start_addr) >> MMArch::PAGE_SHIFT; | ||
if vec.len() < total_pages + offset { | ||
return Err(SystemError::EINVAL); | ||
} | ||
|
||
if !self.can_do_mincore() { | ||
let pages = (end_addr - start_addr) >> MMArch::PAGE_SHIFT; | ||
vec[offset..offset + pages].fill(0); | ||
return Ok(()); | ||
} | ||
// 支持多级页表遍历;在遇到大页时按4K粒度填充 | ||
self.mincore_walk_page_range(mapper, start_addr, end_addr, 3, vec, offset); | ||
Ok(()) | ||
} | ||
|
||
fn mincore_walk_page_range( | ||
&self, | ||
mapper: &PageMapper, | ||
start_addr: VirtAddr, | ||
end_addr: VirtAddr, | ||
level: usize, | ||
vec: &mut [u8], | ||
vec_offset: usize, | ||
) -> usize { | ||
let mut page_count = 0; | ||
let mut start = start_addr; | ||
while start < end_addr { | ||
let entry_size = MMArch::PAGE_SIZE << (level * MMArch::PAGE_ENTRY_SHIFT); | ||
let next = core::cmp::min(end_addr, start + entry_size); | ||
if let Some(entry) = mapper.get_entry(start, level) { | ||
// 大页处理:当上层条目标记为大页时,按子页数量批量填充 | ||
if level > 0 && entry.flags().has_flag(MMArch::ENTRY_FLAG_HUGE_PAGE) { | ||
let sub_pages = (next - start) >> MMArch::PAGE_SHIFT; | ||
let val = if entry.present() { 1 } else { 0 }; | ||
vec[vec_offset + page_count..vec_offset + page_count + sub_pages].fill(val); | ||
page_count += sub_pages; | ||
} else if level > 0 { | ||
let sub_pages = self.mincore_walk_page_range( | ||
mapper, | ||
start, | ||
next, | ||
level - 1, | ||
vec, | ||
vec_offset + page_count, | ||
); | ||
page_count += sub_pages; | ||
} else { | ||
vec[vec_offset + page_count] = if entry.present() { 1 } else { 0 }; | ||
page_count += 1; | ||
} | ||
} else { | ||
let unmapped_pages = | ||
self.mincore_unmapped_range(start, next, vec, vec_offset + page_count); | ||
page_count += unmapped_pages; | ||
} | ||
start = next; | ||
} | ||
page_count | ||
} | ||
|
||
fn mincore_unmapped_range( | ||
&self, | ||
start_addr: VirtAddr, | ||
end_addr: VirtAddr, | ||
vec: &mut [u8], | ||
vec_offset: usize, | ||
) -> usize { | ||
let nr = (end_addr - start_addr) >> MMArch::PAGE_SHIFT; | ||
if self.is_anonymous() { | ||
vec[vec_offset..vec_offset + nr].fill(0); | ||
} else { | ||
let guard = self.lock_irqsave(); | ||
let pgoff = ((start_addr - guard.region().start()) >> MMArch::PAGE_SHIFT) | ||
+ guard.file_page_offset().unwrap(); | ||
if guard.vm_file().is_none() { | ||
vec[vec_offset..vec_offset + nr].fill(0); | ||
return nr; | ||
} | ||
let page_cache = guard.vm_file().unwrap().inode().page_cache(); | ||
match page_cache { | ||
Some(page_cache) => { | ||
let cache_guard = page_cache.lock_irqsave(); | ||
for i in 0..nr { | ||
if cache_guard.get_page(pgoff + i).is_some() { | ||
vec[vec_offset + i] = 1; | ||
} else { | ||
vec[vec_offset + i] = 0; | ||
} | ||
} | ||
} | ||
None => { | ||
vec[vec_offset..vec_offset + nr].fill(0); | ||
} | ||
} | ||
} | ||
nr | ||
} | ||
|
||
pub fn can_do_mincore(&self) -> bool { | ||
//todo: 没有实现vm_ops,这里只能找到匿名映射和文件映射。对于设备映射和其他特殊映射(对应linux中vm_ops有值,但不是文件映射的vma),返回false | ||
if self.is_accessible() { | ||
return true; | ||
} else { | ||
//todo: 若文件不是当前用户所有,需要当前用户对文件有写权限,否则返回false | ||
return true; | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里直接unwrap可能会panic. 比如inode刚好删掉了,然后page cache因为是延迟清除的所以导致vm_file为空。