copyuvum

 avatar
unknown
plain_text
5 months ago
986 B
12
Indexable
pde_t *
copyuvm(pde_t *pgdir, uint sz)
{
    pde_t *d;
    pte_t *pte;
    uint pa, i, flags;
    char *mem;

    // Allocate a new page directory
    if ((d = setupkvm()) == 0)
        return 0;

    // Copy each page
    for (i = 0; i < sz; i += PGSIZE) {
        if ((pte = walkpgdir(pgdir, (void *)i, 0)) == 0)
            continue; // Skip unmapped pages
        if (!(*pte & PTE_P))
            continue; // Skip pages that are not present

        // Get the physical address and flags of the page
        pa = PTE_ADDR(*pte);
        flags = PTE_FLAGS(*pte);

        // Allocate a new physical page
        if ((mem = kalloc()) == 0)
            goto bad;

        // Copy the contents of the page
        memmove(mem, (char *)P2V(pa), PGSIZE);

        // Map the new page into the new page table
        if (mappages(d, (void *)i, PGSIZE, V2P(mem), flags) < 0) {
            kfree(mem);
            goto bad;
        }
    }
    return d;

bad:
    freevm(d);
    return 0;
}
Editor is loading...
Leave a Comment