uint64_t kalloc_wired(uint64_t size) { kern_return_t err; mach_vm_address_t addr = 0; mach_vm_size_t ksize = round_page_kernel(size); printf("vm_kernel_page_size: %lx\n", vm_kernel_page_size); err = mach_vm_allocate(tfpzero, &addr, ksize+0x4000, VM_FLAGS_ANYWHERE); if (err != KERN_SUCCESS) { printf("unable to allocate kernel memory via tfp0: %s %x\n", mach_error_string(err), err); sleep(3); return 0; } printf("allocated address: %llx\n", addr); addr += 0x3fff; addr &= ~0x3fffull; printf("address to wire: %llx\n", addr); err = mach_vm_wire(fake_host_priv(), tfpzero, addr, ksize, VM_PROT_READ|VM_PROT_WRITE); if (err != KERN_SUCCESS) { printf("unable to wire kernel memory via tfp0: %s %x\n", mach_error_string(err), err); sleep(3); return 0; } return addr; }
int munlock(__unused proc_t p, struct munlock_args *uap, __unused register_t *retval) { mach_vm_offset_t addr; mach_vm_size_t size; vm_map_t user_map; kern_return_t result; AUDIT_ARG(addr, uap->addr); AUDIT_ARG(addr, uap->len); addr = (mach_vm_offset_t) uap->addr; size = (mach_vm_size_t)uap->len; user_map = current_map(); /* JMM - need to remove all wirings by spec - this just removes one */ result = mach_vm_wire(host_priv_self(), user_map, addr, size, VM_PROT_NONE); return (result == KERN_SUCCESS ? 0 : ENOMEM); }
/* * If we try to wire superpages * - the call should succeed * - the memory should remain readable and writable */ boolean_t test_wire() { int kr; int ret; mach_vm_address_t addr = 0; mach_vm_size_t size = SUPERPAGE_SIZE; kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB); if (!(ret = check_kr(kr, "mach_vm_allocate"))) return ret; kr = mach_vm_wire(mach_host_self(), mach_task_self(), addr, size, VM_PROT_WRITE | VM_PROT_READ); if (!geteuid()) /* may fail as user */ if (!(ret = check_kr(kr, "mach_vm_wire"))) return ret; if (!(ret = check_rw(addr, size))) return ret; kr = mach_vm_deallocate(mach_task_self(), addr, size); if (!(ret = check_kr(kr, "mach_vm_deallocate"))) return ret; return TRUE; }
/* * If we try to wire superpages * - the call should fail * - the memory should remain readable and writable * Currently, superpages are always wired. */ boolean_t test_unwire() { int kr; int ret; mach_vm_address_t addr = 0; mach_vm_size_t size = SUPERPAGE_SIZE; kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE | VM_FLAGS_SUPERPAGE_SIZE_2MB); if (!(ret = check_kr(kr, "mach_vm_allocate"))) return ret; kr = mach_vm_wire(mach_host_self(), mach_task_self(), addr, size, VM_PROT_NONE); if ((ret = check_kr(kr, "mach_vm_wire"))) { sprintf(error, "could unwire"); return FALSE; } if (!(ret = check_rw(addr, size))) return ret; kr = mach_vm_deallocate(mach_task_self(), addr, size); if (!(ret = check_kr(kr, "mach_vm_deallocate"))) return ret; return TRUE; }