xref: /DragonOS/kernel/src/mm/syscall.rs (revision d2b28acb4d1f160779b25d76afca49ed60ad5d48)
1 use core::intrinsics::unlikely;
2 
3 use alloc::sync::Arc;
4 use system_error::SystemError;
5 
6 use crate::{
7     arch::MMArch,
8     kerror,
9     libs::align::{check_aligned, page_align_up},
10     mm::MemoryManagementArch,
11     syscall::Syscall,
12 };
13 
14 use super::{
15     allocator::page_frame::{PageFrameCount, VirtPageFrame},
16     ucontext::{AddressSpace, DEFAULT_MMAP_MIN_ADDR},
17     verify_area, VirtAddr, VmFlags,
18 };
19 
20 bitflags! {
21     /// Memory protection flags
22     pub struct ProtFlags: u64 {
23         const PROT_NONE = 0x0;
24         const PROT_READ = 0x1;
25         const PROT_WRITE = 0x2;
26         const PROT_EXEC = 0x4;
27     }
28 
29     /// Memory mapping flags
30     pub struct MapFlags: u64 {
31         const MAP_NONE = 0x0;
32         /// share changes
33         const MAP_SHARED = 0x1;
34         /// changes are private
35         const MAP_PRIVATE = 0x2;
36         /// Interpret addr exactly
37         const MAP_FIXED = 0x10;
38         /// don't use a file
39         const MAP_ANONYMOUS = 0x20;
40         // linux-6.1-rc5/include/uapi/asm-generic/mman.h#7
41         /// stack-like segment
42         const MAP_GROWSDOWN = 0x100;
43         /// ETXTBSY
44         const MAP_DENYWRITE = 0x800;
45         /// Mark it as an executable
46         const MAP_EXECUTABLE = 0x1000;
47         /// Pages are locked
48         const MAP_LOCKED = 0x2000;
49         /// don't check for reservations
50         const MAP_NORESERVE = 0x4000;
51         /// populate (prefault) pagetables
52         const MAP_POPULATE = 0x8000;
53         /// do not block on IO
54         const MAP_NONBLOCK = 0x10000;
55         /// give out an address that is best suited for process/thread stacks
56         const MAP_STACK = 0x20000;
57         /// create a huge page mapping
58         const MAP_HUGETLB = 0x40000;
59         /// perform synchronous page faults for the mapping
60         const MAP_SYNC = 0x80000;
61         /// MAP_FIXED which doesn't unmap underlying mapping
62         const MAP_FIXED_NOREPLACE = 0x100000;
63 
64         /// For anonymous mmap, memory could be uninitialized
65         const MAP_UNINITIALIZED = 0x4000000;
66     }
67 
68     /// Memory mremapping flags
69     pub struct MremapFlags: u8 {
70         const MREMAP_MAYMOVE = 1;
71         const MREMAP_FIXED = 2;
72         const MREMAP_DONTUNMAP = 4;
73     }
74 }
75 
76 impl From<MapFlags> for VmFlags {
77     fn from(map_flags: MapFlags) -> Self {
78         let mut vm_flags = VmFlags::VM_NONE;
79 
80         if map_flags.contains(MapFlags::MAP_GROWSDOWN) {
81             vm_flags |= VmFlags::VM_GROWSDOWN;
82         }
83 
84         if map_flags.contains(MapFlags::MAP_LOCKED) {
85             vm_flags |= VmFlags::VM_LOCKED;
86         }
87 
88         if map_flags.contains(MapFlags::MAP_SYNC) {
89             vm_flags |= VmFlags::VM_SYNC;
90         }
91 
92         vm_flags
93     }
94 }
95 
96 impl From<ProtFlags> for VmFlags {
97     fn from(prot_flags: ProtFlags) -> Self {
98         let mut vm_flags = VmFlags::VM_NONE;
99 
100         if prot_flags.contains(ProtFlags::PROT_READ) {
101             vm_flags |= VmFlags::VM_READ;
102         }
103 
104         if prot_flags.contains(ProtFlags::PROT_WRITE) {
105             vm_flags |= VmFlags::VM_WRITE;
106         }
107 
108         if prot_flags.contains(ProtFlags::PROT_EXEC) {
109             vm_flags |= VmFlags::VM_EXEC;
110         }
111 
112         vm_flags
113     }
114 }
115 
116 impl Into<MapFlags> for VmFlags {
117     fn into(self) -> MapFlags {
118         let mut map_flags = MapFlags::MAP_NONE;
119 
120         if self.contains(VmFlags::VM_GROWSDOWN) {
121             map_flags |= MapFlags::MAP_GROWSDOWN;
122         }
123 
124         if self.contains(VmFlags::VM_LOCKED) {
125             map_flags |= MapFlags::MAP_LOCKED;
126         }
127 
128         if self.contains(VmFlags::VM_SYNC) {
129             map_flags |= MapFlags::MAP_SYNC;
130         }
131 
132         map_flags
133     }
134 }
135 
136 impl Into<ProtFlags> for VmFlags {
137     fn into(self) -> ProtFlags {
138         let mut prot_flags = ProtFlags::PROT_NONE;
139 
140         if self.contains(VmFlags::VM_READ) {
141             prot_flags |= ProtFlags::PROT_READ;
142         }
143 
144         if self.contains(VmFlags::VM_WRITE) {
145             prot_flags |= ProtFlags::PROT_WRITE;
146         }
147 
148         if self.contains(VmFlags::VM_EXEC) {
149             prot_flags |= ProtFlags::PROT_EXEC;
150         }
151 
152         prot_flags
153     }
154 }
155 
156 impl Syscall {
157     pub fn brk(new_addr: VirtAddr) -> Result<VirtAddr, SystemError> {
158         // kdebug!("brk: new_addr={:?}", new_addr);
159         let address_space = AddressSpace::current()?;
160         let mut address_space = address_space.write();
161 
162         if new_addr < address_space.brk_start || new_addr >= MMArch::USER_END_VADDR {
163             return Ok(address_space.brk);
164         }
165         if new_addr == address_space.brk {
166             return Ok(address_space.brk);
167         }
168 
169         unsafe {
170             address_space
171                 .set_brk(VirtAddr::new(page_align_up(new_addr.data())))
172                 .ok();
173 
174             return Ok(address_space.sbrk(0).unwrap());
175         }
176     }
177 
178     pub fn sbrk(incr: isize) -> Result<VirtAddr, SystemError> {
179         let address_space = AddressSpace::current()?;
180         assert!(address_space.read().user_mapper.utable.is_current());
181         let mut address_space = address_space.write();
182         let r = unsafe { address_space.sbrk(incr) };
183 
184         return r;
185     }
186 
187     /// ## mmap系统调用
188     ///
189     /// 该函数的实现参考了Linux内核的实现,但是并不完全相同。因为有些功能咱们还没实现
190     ///
191     /// ## 参数
192     ///
193     /// - `start_vaddr`:映射的起始地址
194     /// - `len`:映射的长度
195     /// - `prot`:保护标志
196     /// - `flags`:映射标志
197     /// - `fd`:文件描述符(暂时不支持)
198     /// - `offset`:文件偏移量 (暂时不支持)
199     ///
200     /// ## 返回值
201     ///
202     /// 成功时返回映射的起始地址,失败时返回错误码
203     pub fn mmap(
204         start_vaddr: VirtAddr,
205         len: usize,
206         prot_flags: usize,
207         map_flags: usize,
208         _fd: i32,
209         _offset: usize,
210     ) -> Result<usize, SystemError> {
211         let map_flags = MapFlags::from_bits_truncate(map_flags as u64);
212         let prot_flags = ProtFlags::from_bits_truncate(prot_flags as u64);
213 
214         if start_vaddr < VirtAddr::new(DEFAULT_MMAP_MIN_ADDR)
215             && map_flags.contains(MapFlags::MAP_FIXED)
216         {
217             kerror!(
218                 "mmap: MAP_FIXED is not supported for address below {}",
219                 DEFAULT_MMAP_MIN_ADDR
220             );
221             return Err(SystemError::EINVAL);
222         }
223         // 暂时不支持除匿名页以外的映射
224         if !map_flags.contains(MapFlags::MAP_ANONYMOUS) {
225             kerror!("mmap: not support file mapping");
226             return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
227         }
228 
229         // 暂时不支持巨页映射
230         if map_flags.contains(MapFlags::MAP_HUGETLB) {
231             kerror!("mmap: not support huge page mapping");
232             return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
233         }
234         let current_address_space = AddressSpace::current()?;
235         let start_page = current_address_space.write().map_anonymous(
236             start_vaddr,
237             len,
238             prot_flags,
239             map_flags,
240             true,
241         )?;
242         return Ok(start_page.virt_address().data());
243     }
244 
245     /// ## mremap系统调用
246     ///
247     ///
248     /// ## 参数
249     ///
250     /// - `old_vaddr`:原映射的起始地址
251     /// - `old_len`:原映射的长度
252     /// - `new_len`:重新映射的长度
253     /// - `mremap_flags`:重映射标志
254     /// - `new_vaddr`:重新映射的起始地址
255     ///
256     /// ## 返回值
257     ///
258     /// 成功时返回重映射的起始地址,失败时返回错误码
259     pub fn mremap(
260         old_vaddr: VirtAddr,
261         old_len: usize,
262         new_len: usize,
263         mremap_flags: MremapFlags,
264         new_vaddr: VirtAddr,
265     ) -> Result<usize, SystemError> {
266         // 需要重映射到新内存区域的情况下,必须包含MREMAP_MAYMOVE并且指定新地址
267         if mremap_flags.contains(MremapFlags::MREMAP_FIXED)
268             && (!mremap_flags.contains(MremapFlags::MREMAP_MAYMOVE)
269                 || new_vaddr == VirtAddr::new(0))
270         {
271             return Err(SystemError::EINVAL);
272         }
273 
274         // 不取消旧映射的情况下,必须包含MREMAP_MAYMOVE并且新内存大小等于旧内存大小
275         if mremap_flags.contains(MremapFlags::MREMAP_DONTUNMAP)
276             && (!mremap_flags.contains(MremapFlags::MREMAP_MAYMOVE) || old_len != new_len)
277         {
278             return Err(SystemError::EINVAL);
279         }
280 
281         // 旧内存地址必须对齐
282         if !old_vaddr.check_aligned(MMArch::PAGE_SIZE) {
283             return Err(SystemError::EINVAL);
284         }
285 
286         // 将old_len、new_len 对齐页面大小
287         let old_len = page_align_up(old_len);
288         let new_len = page_align_up(new_len);
289 
290         // 不允许重映射内存区域大小为0
291         if new_len == 0 {
292             return Err(SystemError::EINVAL);
293         }
294 
295         let current_address_space = AddressSpace::current()?;
296         let vma = current_address_space.read().mappings.contains(old_vaddr);
297         if vma.is_none() {
298             return Err(SystemError::EINVAL);
299         }
300         let vma = vma.unwrap();
301         let vm_flags = vma.lock().vm_flags().clone();
302 
303         // 暂时不支持巨页映射
304         if vm_flags.contains(VmFlags::VM_HUGETLB) {
305             kerror!("mmap: not support huge page mapping");
306             return Err(SystemError::ENOSYS);
307         }
308 
309         // 缩小旧内存映射区域
310         if old_len > new_len {
311             Self::munmap(old_vaddr + new_len, old_len - new_len)?;
312             return Ok(old_vaddr.data());
313         }
314 
315         // 重映射到新内存区域
316         let r = current_address_space.write().mremap(
317             old_vaddr,
318             old_len,
319             new_len,
320             mremap_flags,
321             new_vaddr,
322             vm_flags,
323         )?;
324 
325         if !mremap_flags.contains(MremapFlags::MREMAP_DONTUNMAP) {
326             Self::munmap(old_vaddr, old_len)?;
327         }
328 
329         return Ok(r.data());
330     }
331 
332     /// ## munmap系统调用
333     ///
334     /// ## 参数
335     ///
336     /// - `start_vaddr`:取消映射的起始地址(已经对齐到页)
337     /// - `len`:取消映射的字节数(已经对齐到页)
338     ///
339     /// ## 返回值
340     ///
341     /// 成功时返回0,失败时返回错误码
342     pub fn munmap(start_vaddr: VirtAddr, len: usize) -> Result<usize, SystemError> {
343         assert!(start_vaddr.check_aligned(MMArch::PAGE_SIZE));
344         assert!(check_aligned(len, MMArch::PAGE_SIZE));
345 
346         if unlikely(verify_area(start_vaddr, len).is_err()) {
347             return Err(SystemError::EINVAL);
348         }
349         if unlikely(len == 0) {
350             return Err(SystemError::EINVAL);
351         }
352 
353         let current_address_space: Arc<AddressSpace> = AddressSpace::current()?;
354         let start_frame = VirtPageFrame::new(start_vaddr);
355         let page_count = PageFrameCount::new(len / MMArch::PAGE_SIZE);
356 
357         current_address_space
358             .write()
359             .munmap(start_frame, page_count)
360             .map_err(|_| SystemError::EINVAL)?;
361 
362         return Ok(0);
363     }
364 
365     /// ## mprotect系统调用
366     ///
367     /// ## 参数
368     ///
369     /// - `start_vaddr`:起始地址(已经对齐到页)
370     /// - `len`:长度(已经对齐到页)
371     /// - `prot_flags`:保护标志
372     pub fn mprotect(
373         start_vaddr: VirtAddr,
374         len: usize,
375         prot_flags: usize,
376     ) -> Result<usize, SystemError> {
377         assert!(start_vaddr.check_aligned(MMArch::PAGE_SIZE));
378         assert!(check_aligned(len, MMArch::PAGE_SIZE));
379 
380         if unlikely(verify_area(start_vaddr, len).is_err()) {
381             return Err(SystemError::EINVAL);
382         }
383         if unlikely(len == 0) {
384             return Err(SystemError::EINVAL);
385         }
386 
387         let prot_flags = ProtFlags::from_bits(prot_flags as u64).ok_or(SystemError::EINVAL)?;
388 
389         let current_address_space: Arc<AddressSpace> = AddressSpace::current()?;
390         let start_frame = VirtPageFrame::new(start_vaddr);
391         let page_count = PageFrameCount::new(len / MMArch::PAGE_SIZE);
392 
393         current_address_space
394             .write()
395             .mprotect(start_frame, page_count, prot_flags)
396             .map_err(|_| SystemError::EINVAL)?;
397         return Ok(0);
398     }
399 }
400