static void *physmap_common(const char *descr, uintptr_t phys_addr, size_t len, bool readonly, bool autocleanup, bool round) { void *virt_addr; uintptr_t offset = 0; if (len == 0) { msg_pspew("Not mapping %s, zero size at 0x%0*" PRIxPTR ".\n", descr, PRIxPTR_WIDTH, phys_addr); return ERROR_PTR; } if (round) offset = round_to_page_boundaries(&phys_addr, &len); if (readonly) virt_addr = sys_physmap_ro_cached(phys_addr, len); else virt_addr = sys_physmap_rw_uncached(phys_addr, len); if (ERROR_PTR == virt_addr) { if (NULL == descr) descr = "memory"; msg_perr("Error accessing %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n", descr, len, PRIxPTR_WIDTH, phys_addr); msg_perr(MEM_DEV " mmap failed: %s\n", strerror(errno)); #ifdef __linux__ if (EINVAL == errno) { msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n"); msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n"); } #elif defined (__OpenBSD__) msg_perr("Please set securelevel=-1 in /etc/rc.securelevel " "and reboot, or reboot into\n" "single user mode.\n"); #endif return ERROR_PTR; } if (autocleanup) { struct undo_physmap_data *d = malloc(sizeof(struct undo_physmap_data)); if (d == NULL) { msg_perr("%s: Out of memory!\n", __func__); physunmap(virt_addr, len); return ERROR_PTR; } d->virt_addr = virt_addr; d->len = len; if (register_shutdown(undo_physmap, d) != 0) { msg_perr("%s: Could not register shutdown function!\n", __func__); physunmap(virt_addr, len); return ERROR_PTR; } } return virt_addr + offset; }
void physunmap(void *virt_addr, size_t len) { uintptr_t tmp; /* No need to check for zero size, such mappings would have yielded ERROR_PTR. */ if (virt_addr == ERROR_PTR) { msg_perr("Trying to unmap a nonexisting mapping!\n" "Please report a bug at [email protected]\n"); return; } tmp = (uintptr_t)virt_addr; /* We assume that the virtual address of a page-aligned physical address is page-aligned as well. By * extension, rounding a virtual unaligned address as returned by physmap should yield the same offset * between rounded and original virtual address as between rounded and original physical address. */ round_to_page_boundaries(&tmp, &len); virt_addr = (void *)tmp; physunmap_unaligned(virt_addr, len); }