xref: /DragonOS/kernel/src/mm/madvise.rs (revision 52dc4c3ee87b31fee4f567f776fd8ad58fbfd9a5)
1 use system_error::SystemError;
2 
3 use crate::arch::{mm::PageMapper, MMArch};
4 
5 use super::{page::Flusher, syscall::MadvFlags, ucontext::LockedVMA, VmFlags};
6 
7 impl LockedVMA {
8     pub fn do_madvise(
9         &self,
10         behavior: MadvFlags,
11         _mapper: &mut PageMapper,
12         _flusher: impl Flusher<MMArch>,
13     ) -> Result<(), SystemError> {
14         //TODO https://code.dragonos.org.cn/xref/linux-6.6.21/mm/madvise.c?fi=madvise#do_madvise
15         let mut vma = self.lock_irqsave();
16         let mut new_flags = *vma.vm_flags();
17         match behavior {
18             MadvFlags::MADV_REMOVE => {
19                 // TODO
20             }
21 
22             MadvFlags::MADV_WILLNEED => {
23                 // TODO
24             }
25 
26             MadvFlags::MADV_COLD => {
27                 // TODO
28             }
29 
30             MadvFlags::MADV_PAGEOUT => {
31                 // TODO
32             }
33 
34             MadvFlags::MADV_FREE => {
35                 // TODO
36             }
37 
38             MadvFlags::MADV_POPULATE_READ | MadvFlags::MADV_POPULATE_WRITE => {
39                 // TODO
40             }
41 
42             MadvFlags::MADV_NORMAL => {
43                 new_flags = new_flags & !VmFlags::VM_RAND_READ & !VmFlags::VM_SEQ_READ
44             }
45 
46             MadvFlags::MADV_SEQUENTIAL => {
47                 new_flags = (new_flags & !VmFlags::VM_RAND_READ) | VmFlags::VM_SEQ_READ
48             }
49             MadvFlags::MADV_RANDOM => {
50                 new_flags = (new_flags & !VmFlags::VM_SEQ_READ) | VmFlags::VM_RAND_READ
51             }
52 
53             MadvFlags::MADV_DONTFORK => new_flags |= VmFlags::VM_DONTCOPY,
54 
55             MadvFlags::MADV_DOFORK => {
56                 if vma.vm_flags().contains(VmFlags::VM_IO) {
57                     return Err(SystemError::EINVAL);
58                 }
59                 new_flags &= !VmFlags::VM_DONTCOPY;
60             }
61 
62             MadvFlags::MADV_WIPEONFORK => {
63                 //MADV_WIPEONFORK仅支持匿名映射,后续实现其他映射方式后要在此处添加判断条件
64                 new_flags |= VmFlags::VM_WIPEONFORK;
65             }
66 
67             MadvFlags::MADV_KEEPONFORK => new_flags &= !VmFlags::VM_WIPEONFORK,
68 
69             MadvFlags::MADV_DONTDUMP => new_flags |= VmFlags::VM_DONTDUMP,
70 
71             //MADV_DODUMP不支持巨页映射,后续需要添加判断条件
72             MadvFlags::MADV_DODUMP => new_flags &= !VmFlags::VM_DONTDUMP,
73 
74             MadvFlags::MADV_MERGEABLE | MadvFlags::MADV_UNMERGEABLE => {}
75 
76             MadvFlags::MADV_HUGEPAGE | MadvFlags::MADV_NOHUGEPAGE => {}
77 
78             MadvFlags::MADV_COLLAPSE => {}
79             _ => {}
80         }
81         vma.set_vm_flags(new_flags);
82         Ok(())
83     }
84 }
85