- for (size_t j = 0; j < pg_num; j++) {
- vmm_set_mapping(KERNEL_PID,
- (pa + (j << PG_SIZE_BITS)),
- (pa + (j << PG_SIZE_BITS)),
- PG_PREM_R);
+ size_t j = 0;
+ if (!unlock) {
+ for (; j < pg_num; j++) {
+ uintptr_t _pa = pa + (j << PG_SIZE_BITS);
+ if (_pa >= KERNEL_MM_BASE) {
+ // Don't fuck up our kernel space!
+ break;
+ }
+ vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
+ pmm_mark_page_occupied(
+ KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
+ }
+ // Save the progress for later unmapping.
+ mmaps[i].len_low = j * PG_SIZE;
+ } else {
+ for (; j < pg_num; j++) {
+ uintptr_t _pa = pa + (j << PG_SIZE_BITS);
+ vmm_del_mapping(PD_REFERENCED, _pa);
+ if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
+ pmm_mark_page_free(_pa >> PG_SIZE_BITS);
+ }
+ }