140fe15e0SLoGin use core::intrinsics::unlikely; 240fe15e0SLoGin 340fe15e0SLoGin use alloc::sync::Arc; 4*91e9d4abSLoGin use system_error::SystemError; 540fe15e0SLoGin 6ab5c8ca4Slogin use crate::{ 740fe15e0SLoGin arch::MMArch, 840fe15e0SLoGin kerror, 940fe15e0SLoGin libs::align::{check_aligned, page_align_up}, 1040fe15e0SLoGin mm::MemoryManagementArch, 11*91e9d4abSLoGin syscall::Syscall, 12ab5c8ca4Slogin }; 13ab5c8ca4Slogin 1440fe15e0SLoGin use super::{ 1540fe15e0SLoGin allocator::page_frame::{PageFrameCount, VirtPageFrame}, 1640fe15e0SLoGin ucontext::{AddressSpace, DEFAULT_MMAP_MIN_ADDR}, 1740fe15e0SLoGin verify_area, VirtAddr, 1840fe15e0SLoGin }; 1940fe15e0SLoGin 2040fe15e0SLoGin bitflags! { 2140fe15e0SLoGin /// Memory protection flags 2240fe15e0SLoGin pub struct ProtFlags: u64 { 2340fe15e0SLoGin const PROT_NONE = 0x0; 2440fe15e0SLoGin const PROT_READ = 0x1; 2540fe15e0SLoGin const PROT_WRITE = 0x2; 2640fe15e0SLoGin const PROT_EXEC = 0x4; 27ab5c8ca4Slogin } 2840fe15e0SLoGin 2940fe15e0SLoGin /// Memory mapping flags 3040fe15e0SLoGin pub struct MapFlags: u64 { 3140fe15e0SLoGin const MAP_NONE = 0x0; 3240fe15e0SLoGin /// share changes 3340fe15e0SLoGin const MAP_SHARED = 0x1; 3440fe15e0SLoGin /// changes are private 3540fe15e0SLoGin const MAP_PRIVATE = 0x2; 3640fe15e0SLoGin /// Interpret addr exactly 3740fe15e0SLoGin const MAP_FIXED = 0x10; 3840fe15e0SLoGin /// don't use a file 3940fe15e0SLoGin const MAP_ANONYMOUS = 0x20; 4040fe15e0SLoGin // linux-6.1-rc5/include/uapi/asm-generic/mman.h#7 4140fe15e0SLoGin /// stack-like segment 4240fe15e0SLoGin const MAP_GROWSDOWN = 0x100; 4340fe15e0SLoGin /// ETXTBSY 4440fe15e0SLoGin const MAP_DENYWRITE = 0x800; 4540fe15e0SLoGin /// Mark it as an executable 4640fe15e0SLoGin const MAP_EXECUTABLE = 0x1000; 4740fe15e0SLoGin /// Pages are locked 4840fe15e0SLoGin const MAP_LOCKED = 0x2000; 4940fe15e0SLoGin /// don't check for reservations 5040fe15e0SLoGin const MAP_NORESERVE = 0x4000; 5140fe15e0SLoGin /// populate (prefault) pagetables 5240fe15e0SLoGin const MAP_POPULATE = 0x8000; 5340fe15e0SLoGin /// do not block on IO 5440fe15e0SLoGin const MAP_NONBLOCK = 0x10000; 5540fe15e0SLoGin /// give out an address that is best suited for process/thread stacks 5640fe15e0SLoGin const MAP_STACK = 0x20000; 5740fe15e0SLoGin /// create a huge page mapping 5840fe15e0SLoGin const MAP_HUGETLB = 0x40000; 5940fe15e0SLoGin /// perform synchronous page faults for the mapping 6040fe15e0SLoGin const MAP_SYNC = 0x80000; 6140fe15e0SLoGin /// MAP_FIXED which doesn't unmap underlying mapping 6240fe15e0SLoGin const MAP_FIXED_NOREPLACE = 0x100000; 6340fe15e0SLoGin 6440fe15e0SLoGin /// For anonymous mmap, memory could be uninitialized 6540fe15e0SLoGin const MAP_UNINITIALIZED = 0x4000000; 6640fe15e0SLoGin 6740fe15e0SLoGin } 6840fe15e0SLoGin } 6940fe15e0SLoGin 70ab5c8ca4Slogin impl Syscall { 7140fe15e0SLoGin pub fn brk(new_addr: VirtAddr) -> Result<VirtAddr, SystemError> { 7240fe15e0SLoGin // kdebug!("brk: new_addr={:?}", new_addr); 7340fe15e0SLoGin let address_space = AddressSpace::current()?; 7440fe15e0SLoGin let mut address_space = address_space.write(); 7540fe15e0SLoGin 766d81180bSLoGin if new_addr < address_space.brk_start || new_addr >= MMArch::USER_END_VADDR { 776d81180bSLoGin return Ok(address_space.brk); 786d81180bSLoGin } 796d81180bSLoGin if new_addr == address_space.brk { 806d81180bSLoGin return Ok(address_space.brk); 816d81180bSLoGin } 826d81180bSLoGin 8340fe15e0SLoGin unsafe { 8440fe15e0SLoGin address_space 8540fe15e0SLoGin .set_brk(VirtAddr::new(page_align_up(new_addr.data()))) 8640fe15e0SLoGin .ok(); 8740fe15e0SLoGin 8840fe15e0SLoGin return Ok(address_space.sbrk(0).unwrap()); 89ab5c8ca4Slogin } 90d8ad0a5eSLoGin } 91d8ad0a5eSLoGin 9240fe15e0SLoGin pub fn sbrk(incr: isize) -> Result<VirtAddr, SystemError> { 9340fe15e0SLoGin let address_space = AddressSpace::current()?; 941496ba7bSLoGin assert!(address_space.read().user_mapper.utable.is_current()); 9540fe15e0SLoGin let mut address_space = address_space.write(); 9640fe15e0SLoGin let r = unsafe { address_space.sbrk(incr) }; 9740fe15e0SLoGin 9840fe15e0SLoGin return r; 99d8ad0a5eSLoGin } 100d8ad0a5eSLoGin 10140fe15e0SLoGin /// ## mmap系统调用 102d8ad0a5eSLoGin /// 10340fe15e0SLoGin /// 该函数的实现参考了Linux内核的实现,但是并不完全相同。因为有些功能咱们还没实现 10440fe15e0SLoGin /// 10540fe15e0SLoGin /// ## 参数 10640fe15e0SLoGin /// 10740fe15e0SLoGin /// - `start_vaddr`:映射的起始地址 10840fe15e0SLoGin /// - `len`:映射的长度 10940fe15e0SLoGin /// - `prot`:保护标志 11040fe15e0SLoGin /// - `flags`:映射标志 11140fe15e0SLoGin /// - `fd`:文件描述符(暂时不支持) 11240fe15e0SLoGin /// - `offset`:文件偏移量 (暂时不支持) 11340fe15e0SLoGin /// 11440fe15e0SLoGin /// ## 返回值 11540fe15e0SLoGin /// 11640fe15e0SLoGin /// 成功时返回映射的起始地址,失败时返回错误码 11740fe15e0SLoGin pub fn mmap( 11840fe15e0SLoGin start_vaddr: VirtAddr, 11940fe15e0SLoGin len: usize, 12040fe15e0SLoGin prot_flags: usize, 12140fe15e0SLoGin map_flags: usize, 12240fe15e0SLoGin _fd: i32, 12340fe15e0SLoGin _offset: usize, 12440fe15e0SLoGin ) -> Result<usize, SystemError> { 12540fe15e0SLoGin let map_flags = MapFlags::from_bits_truncate(map_flags as u64); 12640fe15e0SLoGin let prot_flags = ProtFlags::from_bits_truncate(prot_flags as u64); 12740fe15e0SLoGin 12840fe15e0SLoGin if start_vaddr < VirtAddr::new(DEFAULT_MMAP_MIN_ADDR) 12940fe15e0SLoGin && map_flags.contains(MapFlags::MAP_FIXED) 13040fe15e0SLoGin { 13140fe15e0SLoGin kerror!( 13240fe15e0SLoGin "mmap: MAP_FIXED is not supported for address below {}", 13340fe15e0SLoGin DEFAULT_MMAP_MIN_ADDR 13440fe15e0SLoGin ); 13540fe15e0SLoGin return Err(SystemError::EINVAL); 136d8ad0a5eSLoGin } 13740fe15e0SLoGin // 暂时不支持除匿名页以外的映射 13840fe15e0SLoGin if !map_flags.contains(MapFlags::MAP_ANONYMOUS) { 13940fe15e0SLoGin kerror!("mmap: not support file mapping"); 14040fe15e0SLoGin return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 14140fe15e0SLoGin } 14240fe15e0SLoGin 14340fe15e0SLoGin // 暂时不支持巨页映射 14440fe15e0SLoGin if map_flags.contains(MapFlags::MAP_HUGETLB) { 14540fe15e0SLoGin kerror!("mmap: not support huge page mapping"); 14640fe15e0SLoGin return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 14740fe15e0SLoGin } 14840fe15e0SLoGin let current_address_space = AddressSpace::current()?; 14940fe15e0SLoGin let start_page = current_address_space.write().map_anonymous( 15040fe15e0SLoGin start_vaddr, 15140fe15e0SLoGin len, 15240fe15e0SLoGin prot_flags, 15340fe15e0SLoGin map_flags, 15440fe15e0SLoGin true, 15540fe15e0SLoGin )?; 15640fe15e0SLoGin return Ok(start_page.virt_address().data()); 15740fe15e0SLoGin } 15840fe15e0SLoGin 15940fe15e0SLoGin /// ## munmap系统调用 16040fe15e0SLoGin /// 16140fe15e0SLoGin /// ## 参数 16240fe15e0SLoGin /// 16340fe15e0SLoGin /// - `start_vaddr`:取消映射的起始地址(已经对齐到页) 16440fe15e0SLoGin /// - `len`:取消映射的字节数(已经对齐到页) 16540fe15e0SLoGin /// 16640fe15e0SLoGin /// ## 返回值 16740fe15e0SLoGin /// 16840fe15e0SLoGin /// 成功时返回0,失败时返回错误码 16940fe15e0SLoGin pub fn munmap(start_vaddr: VirtAddr, len: usize) -> Result<usize, SystemError> { 17040fe15e0SLoGin assert!(start_vaddr.check_aligned(MMArch::PAGE_SIZE)); 17140fe15e0SLoGin assert!(check_aligned(len, MMArch::PAGE_SIZE)); 17240fe15e0SLoGin 17340fe15e0SLoGin if unlikely(verify_area(start_vaddr, len).is_err()) { 17440fe15e0SLoGin return Err(SystemError::EINVAL); 17540fe15e0SLoGin } 17640fe15e0SLoGin if unlikely(len == 0) { 17740fe15e0SLoGin return Err(SystemError::EINVAL); 17840fe15e0SLoGin } 17940fe15e0SLoGin 18040fe15e0SLoGin let current_address_space: Arc<AddressSpace> = AddressSpace::current()?; 18140fe15e0SLoGin let start_frame = VirtPageFrame::new(start_vaddr); 18240fe15e0SLoGin let page_count = PageFrameCount::new(len / MMArch::PAGE_SIZE); 18340fe15e0SLoGin 18440fe15e0SLoGin current_address_space 18540fe15e0SLoGin .write() 18640fe15e0SLoGin .munmap(start_frame, page_count) 18740fe15e0SLoGin .map_err(|_| SystemError::EINVAL)?; 18840fe15e0SLoGin return Ok(0); 18940fe15e0SLoGin } 19040fe15e0SLoGin 19140fe15e0SLoGin /// ## mprotect系统调用 19240fe15e0SLoGin /// 19340fe15e0SLoGin /// ## 参数 19440fe15e0SLoGin /// 19540fe15e0SLoGin /// - `start_vaddr`:起始地址(已经对齐到页) 19640fe15e0SLoGin /// - `len`:长度(已经对齐到页) 19740fe15e0SLoGin /// - `prot_flags`:保护标志 19840fe15e0SLoGin pub fn mprotect( 19940fe15e0SLoGin start_vaddr: VirtAddr, 20040fe15e0SLoGin len: usize, 20140fe15e0SLoGin prot_flags: usize, 20240fe15e0SLoGin ) -> Result<usize, SystemError> { 20340fe15e0SLoGin assert!(start_vaddr.check_aligned(MMArch::PAGE_SIZE)); 20440fe15e0SLoGin assert!(check_aligned(len, MMArch::PAGE_SIZE)); 20540fe15e0SLoGin 20640fe15e0SLoGin if unlikely(verify_area(start_vaddr, len).is_err()) { 20740fe15e0SLoGin return Err(SystemError::EINVAL); 20840fe15e0SLoGin } 20940fe15e0SLoGin if unlikely(len == 0) { 21040fe15e0SLoGin return Err(SystemError::EINVAL); 21140fe15e0SLoGin } 21240fe15e0SLoGin 21340fe15e0SLoGin let prot_flags = ProtFlags::from_bits(prot_flags as u64).ok_or(SystemError::EINVAL)?; 21440fe15e0SLoGin 21540fe15e0SLoGin let current_address_space: Arc<AddressSpace> = AddressSpace::current()?; 21640fe15e0SLoGin let start_frame = VirtPageFrame::new(start_vaddr); 21740fe15e0SLoGin let page_count = PageFrameCount::new(len / MMArch::PAGE_SIZE); 21840fe15e0SLoGin 21940fe15e0SLoGin current_address_space 22040fe15e0SLoGin .write() 22140fe15e0SLoGin .mprotect(start_frame, page_count, prot_flags) 22240fe15e0SLoGin .map_err(|_| SystemError::EINVAL)?; 22340fe15e0SLoGin return Ok(0); 224ab5c8ca4Slogin } 225ab5c8ca4Slogin } 226