- // XXX: Add kernel reserved memory page check or simply ownership check?
- uint32_t pg = (uintptr_t)page >> 12;
- if (pg && pg < max_pg)
- {
- pmm_mark_page_free(pg);
- return 1;
+ struct pp_struct* pm = &pm_table[page >> 12];
+
+ // Is this a MMIO mapping or double free?
+ if ((page >> 12) >= max_pg || !(pm->ref_counts)) {
+ return 0;
+ }
+
+ // 如果是锁定页,则不作处理
+ if ((pm->attr & PP_FGLOCKED)) {
+ return 0;
+ }
+
+ // TODO: 检查权限,保证:1) 只有正在使用该页(包括被分享者)的进程可以释放;
+ // 2) 内核可释放所有页。
+ pm->ref_counts--;
+ return 1;
+}
+
+int
+pmm_ref_page(pid_t owner, ptr_t page)
+{
+ (void)owner; // TODO: do smth with owner
+
+ u32_t ppn = page >> 12;
+
+ if (ppn >= PM_BMP_MAX_SIZE) {
+ return 0;
+ }
+
+ struct pp_struct* pm = &pm_table[ppn];
+ if (ppn >= max_pg || !pm->ref_counts) {
+ return 0;
+ }
+
+ pm->ref_counts++;
+ return 1;
+}
+
+struct pp_struct*
+pmm_query(ptr_t pa)
+{
+ u32_t ppn = pa >> 12;
+
+ if (ppn >= PM_BMP_MAX_SIZE) {
+ return NULL;