xref: /DragonOS/kernel/src/ipc/shm.rs (revision 86ee1395de7c614865236ee15071c3603b794e44)
1 use crate::{
2     arch::mm::LockedFrameAllocator,
3     filesystem::vfs::syscall::ModeType,
4     libs::{
5         align::page_align_up,
6         spinlock::{SpinLock, SpinLockGuard},
7     },
8     mm::{
9         allocator::page_frame::{FrameAllocator, PageFrameCount, PhysPageFrame},
10         page::{page_manager_lock_irqsave, Page},
11         PhysAddr,
12     },
13     process::{Pid, ProcessManager},
14     syscall::user_access::{UserBufferReader, UserBufferWriter},
15     time::PosixTimeSpec,
16 };
17 use alloc::vec::Vec;
18 use core::sync::atomic::{compiler_fence, Ordering};
19 use hashbrown::{HashMap, HashSet};
20 use ida::IdAllocator;
21 use log::info;
22 use num::ToPrimitive;
23 use system_error::SystemError;
24 
25 pub static mut SHM_MANAGER: Option<SpinLock<ShmManager>> = None;
26 
27 /// 用于创建新的私有IPC对象
28 pub const IPC_PRIVATE: ShmKey = ShmKey::new(0);
29 
30 /// 初始化SHM_MANAGER
31 pub fn shm_manager_init() {
32     info!("shm_manager_init");
33     let shm_manager = SpinLock::new(ShmManager::new());
34 
35     compiler_fence(Ordering::SeqCst);
36     unsafe { SHM_MANAGER = Some(shm_manager) };
37     compiler_fence(Ordering::SeqCst);
38 
39     info!("shm_manager_init done");
40 }
41 
42 pub fn shm_manager_lock() -> SpinLockGuard<'static, ShmManager> {
43     unsafe { SHM_MANAGER.as_ref().unwrap().lock() }
44 }
45 
46 int_like!(ShmId, usize);
47 int_like!(ShmKey, usize);
48 
49 bitflags! {
50     pub struct ShmFlags:u32{
51         const SHM_RDONLY = 0o10000;
52         const SHM_RND = 0o20000;
53         const SHM_REMAP = 0o40000;
54         const SHM_EXEC = 0o100000;
55         const SHM_HUGETLB = 0o4000;
56 
57         const IPC_CREAT = 0o1000;
58         const IPC_EXCL = 0o2000;
59 
60         const SHM_DEST = 0o1000;
61         const SHM_LOCKED = 0o2000;
62     }
63 }
64 
65 /// 管理共享内存段信息的操作码
66 #[derive(Eq, Clone, Copy)]
67 pub enum ShmCtlCmd {
68     /// 删除共享内存段
69     IpcRmid = 0,
70     /// 设置KernIpcPerm选项
71     IpcSet = 1,
72     /// 获取ShmIdDs
73     IpcStat = 2,
74     /// 查看ShmMetaData
75     IpcInfo = 3,
76 
77     /// 不允许共享内存段被置换出物理内存
78     ShmLock = 11,
79     /// 允许共享内存段被置换出物理内存
80     ShmUnlock = 12,
81     /// 查看ShmMetaData
82     ShmStat = 13,
83     /// 查看ShmInfo
84     ShmInfo = 14,
85     /// 查看ShmMetaData
86     ShmtStatAny = 15,
87 
88     Default,
89 }
90 
91 impl From<usize> for ShmCtlCmd {
92     fn from(cmd: usize) -> ShmCtlCmd {
93         match cmd {
94             0 => Self::IpcRmid,
95             1 => Self::IpcSet,
96             2 => Self::IpcStat,
97             3 => Self::IpcInfo,
98             11 => Self::ShmLock,
99             12 => Self::ShmUnlock,
100             13 => Self::ShmStat,
101             14 => Self::ShmInfo,
102             15 => Self::ShmtStatAny,
103             _ => Self::Default,
104         }
105     }
106 }
107 
108 impl PartialEq for ShmCtlCmd {
109     fn eq(&self, other: &ShmCtlCmd) -> bool {
110         *self as usize == *other as usize
111     }
112 }
113 
114 /// 共享内存管理器
115 #[derive(Debug)]
116 pub struct ShmManager {
117     /// ShmId分配器
118     id_allocator: IdAllocator,
119     /// ShmId映射共享内存信息表
120     id2shm: HashMap<ShmId, KernelShm>,
121     /// ShmKey映射ShmId表
122     key2id: HashMap<ShmKey, ShmId>,
123 }
124 
125 impl ShmManager {
126     pub fn new() -> Self {
127         ShmManager {
128             id_allocator: IdAllocator::new(0, usize::MAX - 1),
129             id2shm: HashMap::new(),
130             key2id: HashMap::new(),
131         }
132     }
133 
134     /// # 添加共享内存段
135     ///
136     /// ## 参数
137     ///
138     /// - `key`: 共享内存键值
139     /// - `size`: 共享内存大小
140     /// - `shmflg`: 共享内存标志
141     ///
142     /// ## 返回值
143     ///
144     /// 成功:共享内存id
145     /// 失败:对应错误码
146     pub fn add(
147         &mut self,
148         key: ShmKey,
149         size: usize,
150         shmflg: ShmFlags,
151     ) -> Result<usize, SystemError> {
152         // 判断共享内存大小是否过小或溢出
153         if !(PosixShmMetaInfo::SHMMIN..=PosixShmMetaInfo::SHMMAX).contains(&size) {
154             return Err(SystemError::EINVAL);
155         }
156 
157         let id = self.id_allocator.alloc().expect("No more id to allocate.");
158         let shm_id = ShmId::new(id);
159 
160         // 分配共享内存页面
161         let page_count = PageFrameCount::from_bytes(page_align_up(size)).unwrap();
162         let phys_page =
163             unsafe { LockedFrameAllocator.allocate(page_count) }.ok_or(SystemError::EINVAL)?;
164         // 创建共享内存page,并添加到PAGE_MANAGER中
165         let mut page_manager_guard = page_manager_lock_irqsave();
166         let mut cur_phys = PhysPageFrame::new(phys_page.0);
167         for _ in 0..page_count.data() {
168             let mut page = Page::new(true);
169             page.set_shm_id(shm_id);
170             let paddr = cur_phys.phys_address();
171             page_manager_guard.insert(paddr, page);
172             cur_phys = cur_phys.next();
173         }
174 
175         // 创建共享内存信息结构体
176         let paddr = phys_page.0;
177         let kern_ipc_perm = KernIpcPerm {
178             id: shm_id,
179             key,
180             uid: 0,
181             gid: 0,
182             _cuid: 0,
183             _cgid: 0,
184             mode: shmflg & ShmFlags::from_bits_truncate(ModeType::S_IRWXUGO.bits()),
185             _seq: 0,
186         };
187         let shm_kernel = KernelShm::new(kern_ipc_perm, paddr, size);
188 
189         // 将key、id及其对应KernelShm添加到表中
190         self.id2shm.insert(shm_id, shm_kernel);
191         self.key2id.insert(key, shm_id);
192 
193         return Ok(shm_id.data());
194     }
195 
196     pub fn contains_key(&self, key: &ShmKey) -> Option<&ShmId> {
197         self.key2id.get(key)
198     }
199 
200     pub fn get_mut(&mut self, id: &ShmId) -> Option<&mut KernelShm> {
201         self.id2shm.get_mut(id)
202     }
203 
204     pub fn free_key(&mut self, key: &ShmKey) {
205         self.key2id.remove(key);
206     }
207 
208     pub fn free_id(&mut self, id: &ShmId) {
209         self.id2shm.remove(id);
210         self.id_allocator.free(id.0);
211     }
212 
213     pub fn ipc_info(&self, user_buf: *const u8, from_user: bool) -> Result<usize, SystemError> {
214         let mut user_buffer_writer = UserBufferWriter::new(
215             user_buf as *mut u8,
216             core::mem::size_of::<PosixShmMetaInfo>(),
217             from_user,
218         )?;
219 
220         let shm_meta_info = PosixShmMetaInfo::new();
221         user_buffer_writer.copy_one_to_user(&shm_meta_info, 0)?;
222 
223         return Ok(0);
224     }
225 
226     pub fn shm_info(&self, user_buf: *const u8, from_user: bool) -> Result<usize, SystemError> {
227         // 已使用id数量
228         let used_ids = self.id2shm.len().to_i32().unwrap();
229         // 共享内存总和
230         let shm_tot = self.id2shm.iter().fold(0, |acc, (_, kernel_shm)| {
231             acc + PageFrameCount::from_bytes(page_align_up(kernel_shm.shm_size))
232                 .unwrap()
233                 .data()
234         });
235         let shm_info = PosixShmInfo::new(used_ids, shm_tot, 0, 0, 0, 0);
236 
237         let mut user_buffer_writer = UserBufferWriter::new(
238             user_buf as *mut u8,
239             core::mem::size_of::<PosixShmInfo>(),
240             from_user,
241         )?;
242         user_buffer_writer.copy_one_to_user(&shm_info, 0)?;
243 
244         return Ok(0);
245     }
246 
247     pub fn shm_stat(
248         &self,
249         id: ShmId,
250         cmd: ShmCtlCmd,
251         user_buf: *const u8,
252         from_user: bool,
253     ) -> Result<usize, SystemError> {
254         let kernel_shm = self.id2shm.get(&id).ok_or(SystemError::EINVAL)?;
255         let key = kernel_shm.kern_ipc_perm.key.data().to_i32().unwrap();
256         let mode = kernel_shm.kern_ipc_perm.mode.bits();
257 
258         let shm_perm = PosixIpcPerm::new(key, 0, 0, 0, 0, mode);
259         let shm_segsz = kernel_shm.shm_size;
260         let shm_atime = kernel_shm.shm_atim.total_nanos();
261         let shm_dtime = kernel_shm.shm_dtim.total_nanos();
262         let shm_ctime = kernel_shm.shm_ctim.total_nanos();
263         let shm_cpid = kernel_shm.shm_cprid.data().to_u32().unwrap();
264         let shm_lpid = kernel_shm.shm_lprid.data().to_u32().unwrap();
265         let shm_map_count = kernel_shm.map_count();
266         let shm_id_ds = PosixShmIdDs {
267             shm_perm,
268             shm_segsz,
269             shm_atime,
270             shm_dtime,
271             shm_ctime,
272             shm_cpid,
273             shm_lpid,
274             shm_map_count,
275             _unused1: 0,
276             _unused2: 0,
277         };
278 
279         let mut user_buffer_writer = UserBufferWriter::new(
280             user_buf as *mut u8,
281             core::mem::size_of::<PosixShmIdDs>(),
282             from_user,
283         )?;
284         user_buffer_writer.copy_one_to_user(&shm_id_ds, 0)?;
285 
286         let r: usize = if cmd == ShmCtlCmd::IpcStat {
287             0
288         } else {
289             id.data()
290         };
291 
292         return Ok(r);
293     }
294 
295     pub fn ipc_set(
296         &mut self,
297         id: ShmId,
298         user_buf: *const u8,
299         from_user: bool,
300     ) -> Result<usize, SystemError> {
301         let kernel_shm = self.id2shm.get_mut(&id).ok_or(SystemError::EINVAL)?;
302 
303         let user_buffer_reader =
304             UserBufferReader::new(user_buf, core::mem::size_of::<PosixShmIdDs>(), from_user)?;
305         let mut shm_id_ds = PosixShmIdDs::default();
306         user_buffer_reader.copy_one_from_user(&mut shm_id_ds, 0)?;
307 
308         kernel_shm.copy_from(shm_id_ds);
309 
310         return Ok(0);
311     }
312 
313     pub fn ipc_rmid(&mut self, id: ShmId) -> Result<usize, SystemError> {
314         let kernel_shm = self.id2shm.get_mut(&id).ok_or(SystemError::EINVAL)?;
315         kernel_shm.set_mode(ShmFlags::SHM_DEST, true);
316 
317         let mut cur_phys = PhysPageFrame::new(kernel_shm.shm_start_paddr);
318         let count = PageFrameCount::from_bytes(page_align_up(kernel_shm.shm_size)).unwrap();
319         let key = kernel_shm.kern_ipc_perm.key;
320         let id = kernel_shm.kern_ipc_perm.id;
321         let map_count = kernel_shm.map_count();
322 
323         let mut page_manager_guard = page_manager_lock_irqsave();
324         if map_count > 0 {
325             // 设置共享内存物理页当映射计数等于0时可被回收
326             for _ in 0..count.data() {
327                 let page = page_manager_guard.get_mut(&cur_phys.phys_address());
328                 page.set_dealloc_when_zero(true);
329 
330                 cur_phys = cur_phys.next();
331             }
332 
333             // 释放key,不让后续进程连接
334             self.free_key(&key);
335         } else {
336             // 释放共享内存物理页
337             for _ in 0..count.data() {
338                 let paddr = cur_phys.phys_address();
339                 unsafe {
340                     LockedFrameAllocator.free(paddr, PageFrameCount::new(1));
341                 }
342                 // 将已回收的物理页面对应的Page从PAGE_MANAGER中删去
343                 page_manager_guard.remove_page(&paddr);
344                 cur_phys = cur_phys.next();
345             }
346 
347             // 释放key和id
348             self.free_id(&id);
349             self.free_key(&key)
350         }
351 
352         return Ok(0);
353     }
354 
355     pub fn shm_lock(&mut self, id: ShmId) -> Result<usize, SystemError> {
356         let kernel_shm = self.id2shm.get_mut(&id).ok_or(SystemError::EINVAL)?;
357         kernel_shm.set_mode(ShmFlags::SHM_LOCKED, true);
358 
359         return Ok(0);
360     }
361 
362     pub fn shm_unlock(&mut self, id: ShmId) -> Result<usize, SystemError> {
363         let kernel_shm = self.id2shm.get_mut(&id).ok_or(SystemError::EINVAL)?;
364         kernel_shm.set_mode(ShmFlags::SHM_LOCKED, false);
365 
366         return Ok(0);
367     }
368 }
369 /// 共享内存信息
370 #[derive(Debug)]
371 pub struct KernelShm {
372     /// 权限信息
373     kern_ipc_perm: KernIpcPerm,
374     /// 共享内存起始物理地址
375     shm_start_paddr: PhysAddr,
376     /// 共享内存大小(bytes),注意是用户指定的大小(未经过页面对齐)
377     shm_size: usize,
378     /// 最后一次连接的时间
379     shm_atim: PosixTimeSpec,
380     /// 最后一次断开连接的时间
381     shm_dtim: PosixTimeSpec,
382     /// 最后一次更改信息的时间
383     shm_ctim: PosixTimeSpec,
384     /// 创建者进程id
385     shm_cprid: Pid,
386     /// 最后操作者进程id
387     shm_lprid: Pid,
388 }
389 
390 impl KernelShm {
391     pub fn new(kern_ipc_perm: KernIpcPerm, shm_start_paddr: PhysAddr, shm_size: usize) -> Self {
392         let shm_cprid = ProcessManager::current_pid();
393         KernelShm {
394             kern_ipc_perm,
395             shm_start_paddr,
396             shm_size,
397             shm_atim: PosixTimeSpec::new(0, 0),
398             shm_dtim: PosixTimeSpec::new(0, 0),
399             shm_ctim: PosixTimeSpec::now(),
400             shm_cprid,
401             shm_lprid: shm_cprid,
402         }
403     }
404 
405     pub fn start_paddr(&self) -> PhysAddr {
406         self.shm_start_paddr
407     }
408 
409     pub fn size(&self) -> usize {
410         self.shm_size
411     }
412 
413     /// 更新最后连接时间
414     pub fn update_atim(&mut self) {
415         // 更新最后一次连接时间
416         self.shm_atim = PosixTimeSpec::now();
417 
418         // 更新最后操作当前共享内存的进程ID
419         self.shm_lprid = ProcessManager::current_pid();
420     }
421 
422     /// 更新最后断开连接时间
423     pub fn update_dtim(&mut self) {
424         // 更新最后一次断开连接时间
425         self.shm_dtim = PosixTimeSpec::now();
426 
427         // 更新最后操作当前共享内存的进程ID
428         self.shm_lprid = ProcessManager::current_pid();
429     }
430 
431     /// 更新最后一次修改信息的时间
432     pub fn update_ctim(&mut self) {
433         // 更新最后一次修改信息的时间
434         self.shm_ctim = PosixTimeSpec::now();
435     }
436 
437     /// 共享内存段的映射计数(有多少个不同的VMA映射)
438     pub fn map_count(&self) -> usize {
439         let page_manager_guard = page_manager_lock_irqsave();
440         let mut id_set: HashSet<usize> = HashSet::new();
441         let mut cur_phys = PhysPageFrame::new(self.shm_start_paddr);
442         let page_count = PageFrameCount::from_bytes(page_align_up(self.shm_size)).unwrap();
443 
444         for _ in 0..page_count.data() {
445             let page = page_manager_guard.get(&cur_phys.phys_address()).unwrap();
446             id_set.extend(
447                 page.anon_vma()
448                     .iter()
449                     .map(|vma| vma.id())
450                     .collect::<Vec<_>>(),
451             );
452 
453             cur_phys = cur_phys.next();
454         }
455 
456         // 由于LockedVMA的id是独一无二的,因此有多少个不同的id,就代表着有多少个不同的VMA映射到共享内存段
457         return id_set.len();
458     }
459 
460     pub fn copy_from(&mut self, shm_id_ds: PosixShmIdDs) {
461         self.kern_ipc_perm.uid = shm_id_ds.uid() as usize;
462         self.kern_ipc_perm.gid = shm_id_ds.gid() as usize;
463         self.kern_ipc_perm.mode = ShmFlags::from_bits_truncate(shm_id_ds.mode());
464         self.update_ctim();
465     }
466 
467     pub fn set_mode(&mut self, shmflg: ShmFlags, set: bool) {
468         if set {
469             self.kern_ipc_perm.mode.insert(shmflg);
470         } else {
471             self.kern_ipc_perm.mode.remove(shmflg);
472         }
473 
474         self.update_ctim();
475     }
476 }
477 
478 /// 共享内存权限信息
479 #[derive(Debug)]
480 pub struct KernIpcPerm {
481     /// 共享内存id
482     id: ShmId,
483     /// 共享内存键值,由创建共享内存用户指定
484     key: ShmKey,
485     /// 共享内存拥有者用户id
486     uid: usize,
487     /// 共享内存拥有者所在组id
488     gid: usize,
489     /// 共享内存创建者用户id
490     _cuid: usize,
491     /// 共享内存创建者所在组id
492     _cgid: usize,
493     /// 共享内存区权限模式
494     mode: ShmFlags,
495     _seq: usize,
496 }
497 
498 /// 共享内存元信息,符合POSIX标准
499 #[repr(C)]
500 #[derive(Debug, Clone, Copy)]
501 pub struct PosixShmMetaInfo {
502     /// 最大共享内存段的大小(bytes)
503     shmmax: usize,
504     /// 最小共享内存段的大小(bytes)
505     shmmin: usize,
506     /// 最大共享内存标识符数量
507     shmmni: usize,
508     /// 单个进程可以拥有的最大共享内存段的数量,和最大共享内存标识符数量相同
509     shmseg: usize,
510     /// 所有共享内存段总共可以使用的最大内存量(pages)
511     shmall: usize,
512     _unused1: usize,
513     _unused2: usize,
514     _unused3: usize,
515     _unused4: usize,
516 }
517 
518 impl PosixShmMetaInfo {
519     /// 最小共享内存段的大小(bytes)
520     pub const SHMMIN: usize = 1;
521     /// 最大共享内存标识符数量
522     pub const SHMMNI: usize = 4096;
523     /// 最大共享内存段的大小(bytes)
524     pub const SHMMAX: usize = usize::MAX - (1 << 24);
525     /// 所有共享内存段总共可以使用的最大内存量(pages)
526     pub const SHMALL: usize = usize::MAX - (1 << 24);
527     /// 单个进程可以拥有的最大共享内存段的数量,和最大共享内存标识符数量相同
528     pub const SHMSEG: usize = 4096;
529 
530     pub fn new() -> Self {
531         PosixShmMetaInfo {
532             shmmax: Self::SHMMAX,
533             shmmin: Self::SHMMIN,
534             shmmni: Self::SHMMNI,
535             shmseg: Self::SHMSEG,
536             shmall: Self::SHMALL,
537             _unused1: 0,
538             _unused2: 0,
539             _unused3: 0,
540             _unused4: 0,
541         }
542     }
543 }
544 
545 /// 共享内存信息,符合POSIX标准
546 #[repr(C)]
547 #[derive(Clone, Copy)]
548 pub struct PosixShmInfo {
549     /// 已使用id数
550     used_ids: i32,
551     /// 共享内存总量(pages)
552     shm_tot: usize,
553     /// 保留在内存中的共享内存大小
554     shm_rss: usize,
555     /// 被置换出的共享内存大小
556     shm_swp: usize,
557     /// 尝试置换次数
558     swap_attempts: usize,
559     /// 成功置换次数
560     swap_successes: usize,
561 }
562 
563 impl PosixShmInfo {
564     pub fn new(
565         used_ids: i32,
566         shm_tot: usize,
567         shm_rss: usize,
568         shm_swp: usize,
569         swap_attempts: usize,
570         swap_successes: usize,
571     ) -> Self {
572         PosixShmInfo {
573             used_ids,
574             shm_tot,
575             shm_rss,
576             shm_swp,
577             swap_attempts,
578             swap_successes,
579         }
580     }
581 }
582 
583 /// 共享内存段属性信息,符合POSIX标准
584 #[repr(C)]
585 #[derive(Debug, Clone, Copy, Default)]
586 pub struct PosixShmIdDs {
587     /// 共享内存段权限
588     shm_perm: PosixIpcPerm,
589     /// 共享内存大小(bytes)
590     shm_segsz: usize,
591     /// 最后一次连接的时间
592     shm_atime: i64,
593     /// 最后一次断开连接的时间
594     shm_dtime: i64,
595     /// 最后一次更改信息的时间
596     shm_ctime: i64,
597     /// 创建者进程id
598     shm_cpid: u32,
599     /// 最后操作者进程id
600     shm_lpid: u32,
601     /// 链接数
602     shm_map_count: usize,
603     _unused1: usize,
604     _unused2: usize,
605 }
606 
607 impl PosixShmIdDs {
608     pub fn uid(&self) -> u32 {
609         self.shm_perm.uid
610     }
611 
612     pub fn gid(&self) -> u32 {
613         self.shm_perm.gid
614     }
615 
616     pub fn mode(&self) -> u32 {
617         self.shm_perm.mode
618     }
619 }
620 
621 /// 共享内存段权限,符合POSIX标准
622 #[repr(C)]
623 #[derive(Debug, Clone, Copy, Default)]
624 pub struct PosixIpcPerm {
625     /// IPC对象键值
626     key: i32,
627     /// 当前用户id
628     uid: u32,
629     /// 当前用户组id
630     gid: u32,
631     /// 创建者用户id
632     cuid: u32,
633     /// 创建者组id
634     cgid: u32,
635     /// 权限
636     mode: u32,
637     /// 序列号
638     seq: i32,
639     _pad1: i32,
640     _unused1: usize,
641     _unused2: usize,
642 }
643 
644 impl PosixIpcPerm {
645     pub fn new(key: i32, uid: u32, gid: u32, cuid: u32, cgid: u32, mode: u32) -> Self {
646         PosixIpcPerm {
647             key,
648             uid,
649             gid,
650             cuid,
651             cgid,
652             mode,
653             seq: 0,
654             _pad1: 0,
655             _unused1: 0,
656             _unused2: 0,
657         }
658     }
659 }
660