struct kmem_cache *vmr_kcache;
-static int __vmr_free_pgs(struct proc *p, pte_t *pte, void *va, void *arg);
+static int __vmr_free_pgs(struct proc *p, pte_t pte, void *va, void *arg);
/* minor helper, will ease the file->chan transition */
static struct page_map *file2pm(struct file *file)
{
vm_i = TAILQ_FIRST(&p->vm_regions);
/* This works for now, but if all we have is BRK_END ones, we'll start
* growing backwards (TODO) */
- if (!vm_i || (va + len < vm_i->vm_base)) {
+ if (!vm_i || (va + len <= vm_i->vm_base)) {
vmr = kmem_cache_alloc(vmr_kcache, 0);
if (!vmr)
panic("EOM!");
/* this only gets called from __proc_free, so there should be no sync
* concerns. still, better safe than sorry. */
spin_lock(&p->vmr_lock);
+ p->vmr_history++;
spin_lock(&p->pte_lock);
TAILQ_FOREACH(vmr_i, &p->vm_regions, vm_link) {
/* note this CB sets the PTE = 0, regardless of if it was P or not */
/* Helper: copies the contents of pages from p to new p. For pages that aren't
* present, once we support swapping or CoW, we can do something more
- * intelligent. 0 on success, -ERROR on failure. */
+ * intelligent. 0 on success, -ERROR on failure. Can't handle jumbos. */
static int copy_pages(struct proc *p, struct proc *new_p, uintptr_t va_start,
uintptr_t va_end)
{
va_end);
return -EINVAL;
}
- int copy_page(struct proc *p, pte_t *pte, void *va, void *arg) {
+ int copy_page(struct proc *p, pte_t pte, void *va, void *arg) {
struct proc *new_p = (struct proc*)arg;
struct page *pp;
- if (PAGE_UNMAPPED(*pte))
+ if (pte_is_unmapped(pte))
return 0;
/* pages could be !P, but right now that's only for file backed VMRs
* undergoing page removal, which isn't the caller of copy_pages. */
- if (PAGE_PRESENT(*pte)) {
+ if (pte_is_mapped(pte)) {
/* TODO: check for jumbos */
if (upage_alloc(new_p, &pp, 0))
return -ENOMEM;
- if (page_insert(new_p->env_pgdir, pp, va, *pte & PTE_PERM)) {
+ if (page_insert(new_p->env_pgdir, pp, va, pte_get_settings(pte))) {
page_decref(pp);
return -ENOMEM;
}
- memcpy(page2kva(pp), ppn2kva(PTE2PPN(*pte)), PGSIZE);
+ memcpy(page2kva(pp), KADDR(pte_get_paddr(pte)), PGSIZE);
page_decref(pp);
- } else if (PAGE_PAGED_OUT(*pte)) {
+ } else if (pte_is_paged_out(pte)) {
/* TODO: (SWAP) will need to either make a copy or CoW/refcnt the
* backend store. For now, this PTE will be the same as the
* original PTE */
panic("Swapping not supported!");
} else {
- panic("Weird PTE %p in %s!", *pte, __FUNCTION__);
+ panic("Weird PTE %p in %s!", pte_print(pte), __FUNCTION__);
}
return 0;
}
vmr->vm_file, vmr->vm_foff);
}
-/* Helper: returns the number of pages required to hold nr_bytes */
-unsigned long nr_pages(unsigned long nr_bytes)
+void enumerate_vmrs(struct proc *p,
+ void (*func)(struct vm_region *vmr, void *opaque),
+ void *opaque)
{
- return (nr_bytes >> PGSHIFT) + (PGOFF(nr_bytes) ? 1 : 0);
+ struct vm_region *vmr;
+
+ spin_lock(&p->vmr_lock);
+ TAILQ_FOREACH(vmr, &p->vm_regions, vm_link)
+ func(vmr, opaque);
+ spin_unlock(&p->vmr_lock);
}
/* Error values aren't quite comprehensive - check man mmap() once we do better
return result;
}
-void *do_mmap(struct proc *p, uintptr_t addr, size_t len, int prot, int flags,
- struct file *file, size_t offset)
-{
- /* read/write vmr lock (will change the tree) */
- spin_lock(&p->vmr_lock);
- void *ret = __do_mmap(p, addr, len, prot, flags, file, offset);
- spin_unlock(&p->vmr_lock);
- return ret;
-}
-
/* Helper: returns TRUE if the VMR is allowed to access the file with prot.
* This is a bit ghetto still: messes with the file mode and assumes it can walk
* the dentry/inode paths without locking. It also ignores the CoW stuff we'll
return FALSE;
}
-void *__do_mmap(struct proc *p, uintptr_t addr, size_t len, int prot, int flags,
- struct file *file, size_t offset)
+/* Helper, maps in page at addr, but only if nothing is mapped there. Returns
+ * 0 on success. If this is called by non-PM code, we'll store your ref in the
+ * PTE. */
+static int map_page_at_addr(struct proc *p, struct page *page, uintptr_t addr,
+ int prot)
{
- len = ROUNDUP(len, PGSIZE);
- int num_pages = len / PGSIZE;
- int retval;
+ pte_t pte;
+ spin_lock(&p->pte_lock); /* walking and changing PTEs */
+ /* find offending PTE (prob don't read this in). This might alloc an
+ * intermediate page table page. */
+ pte = pgdir_walk(p->env_pgdir, (void*)addr, TRUE);
+ if (!pte_walk_okay(pte)) {
+ spin_unlock(&p->pte_lock);
+ return -ENOMEM;
+ }
+ /* a spurious, valid PF is possible due to a legit race: the page might have
+ * been faulted in by another core already (and raced on the memory lock),
+ * in which case we should just return. */
+ if (pte_is_present(pte)) {
+ spin_unlock(&p->pte_lock);
+ /* callers expect us to eat the ref if we succeed. */
+ page_decref(page);
+ return 0;
+ }
+ if (pte_is_mapped(pte)) {
+ /* we're clobbering an old entry. if we're just updating the prot, then
+ * it's no big deal. o/w, there might be an issue. */
+ if (page2pa(page) != pte_get_paddr(pte)) {
+ warn_once("Clobbered a PTE mapping (%p -> %p)\n", pte_print(pte),
+ page2pa(page) | prot);
+ }
+ page_decref(pa2page(pte_get_paddr(pte)));
+ }
+ /* preserve the dirty bit - pm removal could be looking concurrently */
+ prot |= (pte_is_dirty(pte) ? PTE_D : 0);
+ /* We have a ref to page, which we are storing in the PTE */
+ pte_write(pte, page2pa(page), prot);
+ spin_unlock(&p->pte_lock);
+ return 0;
+}
+
+/* Helper: copies *pp's contents to a new page, replacing your page pointer. If
+ * this succeeds, you'll have a non-PM page, which matters for how you put it.*/
+static int __copy_and_swap_pmpg(struct proc *p, struct page **pp)
+{
+ struct page *new_page, *old_page = *pp;
+ if (upage_alloc(p, &new_page, FALSE))
+ return -ENOMEM;
+ memcpy(page2kva(new_page), page2kva(old_page), PGSIZE);
+ pm_put_page(old_page);
+ *pp = new_page;
+ return 0;
+}
+
+/* Hold the VMR lock when you call this - it'll assume the entire VA range is
+ * mappable, which isn't true if there are concurrent changes to the VMRs. */
+static int populate_anon_va(struct proc *p, uintptr_t va, unsigned long nr_pgs,
+ int pte_prot)
+{
+ struct page *page;
+ int ret;
+ for (long i = 0; i < nr_pgs; i++) {
+ if (upage_alloc(p, &page, TRUE))
+ return -ENOMEM;
+ /* could imagine doing a memwalk instead of a for loop */
+ ret = map_page_at_addr(p, page, va + i * PGSIZE, pte_prot);
+ if (ret) {
+ page_decref(page);
+ return ret;
+ }
+ }
+ return 0;
+}
+/* This will periodically unlock the vmr lock. */
+static int populate_pm_va(struct proc *p, uintptr_t va, unsigned long nr_pgs,
+ int pte_prot, struct page_map *pm, size_t offset,
+ int flags, bool exec)
+{
+ int ret = 0;
+ unsigned long pm_idx0 = offset >> PGSHIFT;
+ int vmr_history = ACCESS_ONCE(p->vmr_history);
+ struct page *page;
+
+ /* locking rules: start the loop holding the vmr lock, enter and exit the
+ * entire func holding the lock. */
+ for (long i = 0; i < nr_pgs; i++) {
+ ret = pm_load_page_nowait(pm, pm_idx0 + i, &page);
+ if (ret) {
+ if (ret != -EAGAIN)
+ break;
+ spin_unlock(&p->vmr_lock);
+ /* might block here, can't hold the spinlock */
+ ret = pm_load_page(pm, pm_idx0 + i, &page);
+ spin_lock(&p->vmr_lock);
+ if (ret)
+ break;
+ /* while we were sleeping, the VMRs could have changed on us. */
+ if (vmr_history != ACCESS_ONCE(p->vmr_history)) {
+ pm_put_page(page);
+ printk("[kernel] FYI: VMR changed during populate\n");
+ break;
+ }
+ }
+ if (flags & MAP_PRIVATE) {
+ ret = __copy_and_swap_pmpg(p, &page);
+ if (ret) {
+ pm_put_page(page);
+ break;
+ }
+ }
+ /* if this is an executable page, we might have to flush the
+ * instruction cache if our HW requires it.
+ * TODO: is this still needed? andrew put this in a while ago*/
+ if (exec)
+ icache_flush_page(0, page2kva(page));
+ ret = map_page_at_addr(p, page, va + i * PGSIZE, pte_prot);
+ if (atomic_read(&page->pg_flags) & PG_PAGEMAP)
+ pm_put_page(page);
+ if (ret)
+ break;
+ }
+ return ret;
+}
+
+void *do_mmap(struct proc *p, uintptr_t addr, size_t len, int prot, int flags,
+ struct file *file, size_t offset)
+{
+ len = ROUNDUP(len, PGSIZE);
struct vm_region *vmr, *vmr_temp;
+ /* read/write vmr lock (will change the tree) */
+ spin_lock(&p->vmr_lock);
+ p->vmr_history++;
/* Sanity check, for callers that bypass mmap(). We want addr for anon
* memory to start above the break limit (BRK_END), but not 0. Keep this in
* sync with BRK_END in mmap(). */
addr = BRK_END;
assert(!PGOFF(offset));
-#ifndef CONFIG_DEMAND_PAGING
- flags |= MAP_POPULATE;
-#endif
+ /* MCPs will need their code and data pinned. This check will start to fail
+ * after uthread_slim_init(), at which point userspace should have enough
+ * control over its mmaps (i.e. no longer done by LD or load_elf) that it
+ * can ask for pinned and populated pages. Except for dl_opens(). */
+ struct preempt_data *vcpd = &p->procdata->vcore_preempt_data[0];
+ if (file && (atomic_read(&vcpd->flags) & VC_SCP_NOVCCTX))
+ flags |= MAP_POPULATE | MAP_LOCKED;
/* Need to make sure nothing is in our way when we want a FIXED location.
* We just need to split on the end points (if they exist), and then remove
* everything in between. __do_munmap() will do this. Careful, this means
if (!vmr) {
printk("[kernel] do_mmap() aborted for %p + %d!\n", addr, len);
set_errno(ENOMEM);
- return MAP_FAILED; /* TODO: error propagation for mmap() */
+ spin_unlock(&p->vmr_lock);
+ return MAP_FAILED;
}
+ addr = vmr->vm_base;
vmr->vm_prot = prot;
vmr->vm_flags = flags;
if (file) {
assert(!vmr->vm_file);
destroy_vmr(vmr);
set_errno(EACCES);
+ spin_unlock(&p->vmr_lock);
return MAP_FAILED;
}
/* TODO: consider locking the file while checking (not as manadatory as
assert(!vmr->vm_file);
destroy_vmr(vmr);
set_errno(EACCES); /* not quite */
+ spin_unlock(&p->vmr_lock);
return MAP_FAILED;
}
kref_get(&file->f_kref, 1);
}
vmr->vm_file = file;
vmr->vm_foff = offset;
- addr = vmr->vm_base; /* so we know which pages to populate later */
vmr = merge_me(vmr); /* attempts to merge with neighbors */
- /* Fault in pages now if MAP_POPULATE. We want to populate the region
- * requested, but we need to be careful and only populate the requested
- * length and not any merged regions, which is why we set addr above and use
- * it here.
- *
- * If HPF errors out, we'll warn and fail for now. This could be due to
- * some userspace error, but also occurs when we run out of memory. If we
- * are out of memory, the kernel can't really handle it. */
- if (flags & MAP_POPULATE && vmr->vm_prot != PROT_NONE)
- for (int i = 0; i < num_pages; i++) {
- retval = __handle_page_fault(p, addr + i * PGSIZE, vmr->vm_prot);
- if (retval) {
- warn("do_mmap() failing (%d) on addr %p with prot 0x%x",
- retval, addr + i * PGSIZE, vmr->vm_prot);
- destroy_vmr(vmr);
- set_errno(-retval);
- if (retval == -ENOMEM) {
- printk("[kernel] ENOMEM, killing %d\n", p->pid);
- proc_destroy(p);
- }
- return MAP_FAILED;
- }
+
+ if (flags & MAP_POPULATE && prot != PROT_NONE) {
+ int pte_prot = (prot & PROT_WRITE) ? PTE_USER_RW :
+ (prot & (PROT_READ|PROT_EXEC)) ? PTE_USER_RO : 0;
+ unsigned long nr_pgs = len >> PGSHIFT;
+ int ret = 0;
+ if (!file) {
+ ret = populate_anon_va(p, addr, nr_pgs, pte_prot);
+ } else {
+ /* Note: this will unlock if it blocks. our refcnt on the file
+ * keeps the pm alive when we unlock */
+ ret = populate_pm_va(p, addr, nr_pgs, pte_prot, file->f_mapping,
+ offset, flags, prot & PROT_EXEC);
+ }
+ if (ret == -ENOMEM) {
+ spin_unlock(&p->vmr_lock);
+ printk("[kernel] ENOMEM, killing %d\n", p->pid);
+ proc_destroy(p);
+ return MAP_FAILED; /* will never make it back to userspace */
}
- return (void*SAFE)TC(addr);
+ }
+ spin_unlock(&p->vmr_lock);
+ return (void*)addr;
}
int mprotect(struct proc *p, uintptr_t addr, size_t len, int prot)
}
/* read/write lock, will probably change the tree and settings */
spin_lock(&p->vmr_lock);
+ p->vmr_history++;
int ret = __do_mprotect(p, addr, len, prot);
spin_unlock(&p->vmr_lock);
return ret;
int __do_mprotect(struct proc *p, uintptr_t addr, size_t len, int prot)
{
struct vm_region *vmr, *next_vmr;
- pte_t *pte;
+ pte_t pte;
bool shootdown_needed = FALSE;
int pte_prot = (prot & PROT_WRITE) ? PTE_USER_RW :
- (prot & (PROT_READ|PROT_EXEC)) ? PTE_USER_RO : 0;
+ (prot & (PROT_READ|PROT_EXEC)) ? PTE_USER_RO : PTE_NONE;
/* TODO: this is aggressively splitting, when we might not need to if the
* prots are the same as the previous. Plus, there are three excessive
* scans. Finally, we might be able to merge when we are done. */
}
vmr->vm_prot = prot;
spin_lock(&p->pte_lock); /* walking and changing PTEs */
- /* TODO: use a memwalk */
- for (uintptr_t va = vmr->vm_base; va < vmr->vm_end; va += PGSIZE) {
+ /* TODO: use a memwalk. At a minimum, we need to change every existing
+ * PTE that won't trigger a PF (meaning, present PTEs) to have the new
+ * prot. The others will fault on access, and we'll change the PTE
+ * then. In the off chance we have a mapped but not present PTE, we
+ * might as well change it too, since we're already here. */
+ for (uintptr_t va = vmr->vm_base; va < vmr->vm_end; va += PGSIZE) {
pte = pgdir_walk(p->env_pgdir, (void*)va, 0);
- if (pte && PAGE_PRESENT(*pte)) {
- *pte = (*pte & ~PTE_PERM) | pte_prot;
+ if (pte_walk_okay(pte) && pte_is_mapped(pte)) {
+ pte_replace_perm(pte, pte_prot);
shootdown_needed = TRUE;
}
}
printd("munmap(addr %x, len %x)\n", addr, len);
if (!len)
return 0;
+ len = ROUNDUP(len, PGSIZE);
+
if ((addr % PGSIZE) || (addr < MMAP_LOWEST_VA)) {
set_errno(EINVAL);
return -1;
}
/* read/write: changing the vmrs (trees, properties, and whatnot) */
spin_lock(&p->vmr_lock);
+ p->vmr_history++;
int ret = __do_munmap(p, addr, len);
spin_unlock(&p->vmr_lock);
return ret;
}
-static int __munmap_mark_not_present(struct proc *p, pte_t *pte, void *va,
+static int __munmap_mark_not_present(struct proc *p, pte_t pte, void *va,
void *arg)
{
bool *shootdown_needed = (bool*)arg;
- struct page *page;
/* could put in some checks here for !P and also !0 */
- if (!PAGE_PRESENT(*pte)) /* unmapped (== 0) *ptes are also not PTE_P */
+ if (!pte_is_present(pte)) /* unmapped (== 0) *ptes are also not PTE_P */
return 0;
- page = ppn2page(PTE2PPN(*pte));
- *pte &= ~PTE_P;
+ pte_clear_present(pte);
*shootdown_needed = TRUE;
return 0;
}
* 0 or not. If it isn't, then we're still okay to look at the page. Consider
* the PTE a weak ref on the page. So long as you hold the mm lock, you can
* look at the PTE and know the page isn't being freed. */
-static int __vmr_free_pgs(struct proc *p, pte_t *pte, void *va, void *arg)
+static int __vmr_free_pgs(struct proc *p, pte_t pte, void *va, void *arg)
{
struct page *page;
- if (!*pte)
+ if (pte_is_unmapped(pte))
return 0;
- page = ppn2page(PTE2PPN(*pte));
- *pte = 0;
+ page = pa2page(pte_get_paddr(pte));
+ pte_clear(pte);
if (!(atomic_read(&page->pg_flags) & PG_PAGEMAP))
page_decref(page);
return 0;
int __do_munmap(struct proc *p, uintptr_t addr, size_t len)
{
struct vm_region *vmr, *next_vmr, *first_vmr;
- pte_t *pte;
bool shootdown_needed = FALSE;
/* TODO: this will be a bit slow, since we end up doing three linear
return 0;
}
-int handle_page_fault(struct proc* p, uintptr_t va, int prot)
+/* Helper - drop the page differently based on where it is from */
+static void __put_page(struct page *page)
{
- va = ROUNDDOWN(va,PGSIZE);
+ if (atomic_read(&page->pg_flags) & PG_PAGEMAP)
+ pm_put_page(page);
+ else
+ page_decref(page);
+}
- if (prot != PROT_READ && prot != PROT_WRITE && prot != PROT_EXEC)
- panic("bad prot!");
- /* read access to the VMRs TODO: RCU */
- spin_lock(&p->vmr_lock);
- int ret = __handle_page_fault(p, va, prot);
- spin_unlock(&p->vmr_lock);
- return ret;
+static int __hpf_load_page(struct proc *p, struct page_map *pm,
+ unsigned long idx, struct page **page, bool first)
+{
+ int ret = 0;
+ int coreid = core_id();
+ struct per_cpu_info *pcpui = &per_cpu_info[coreid];
+ bool wake_scp = FALSE;
+ spin_lock(&p->proc_lock);
+ switch (p->state) {
+ case (PROC_RUNNING_S):
+ wake_scp = TRUE;
+ __proc_set_state(p, PROC_WAITING);
+ /* it's possible for HPF to loop a few times; we can only save the
+ * first time, o/w we could clobber. */
+ if (first) {
+ __proc_save_context_s(p, pcpui->cur_ctx);
+ __proc_save_fpu_s(p);
+ /* We clear the owner, since userspace doesn't run here
+ * anymore, but we won't abandon since the fault handler
+ * still runs in our process. */
+ clear_owning_proc(coreid);
+ }
+ /* other notes: we don't currently need to tell the ksched
+ * we switched from running to waiting, though we probably
+ * will later for more generic scheds. */
+ break;
+ case (PROC_RUNNABLE_M):
+ case (PROC_RUNNING_M):
+ spin_unlock(&p->proc_lock);
+ return -EAGAIN; /* will get reflected back to userspace */
+ case (PROC_DYING):
+ spin_unlock(&p->proc_lock);
+ return -EINVAL;
+ default:
+ /* shouldn't have any waitings, under the current yield style. if
+ * this becomes an issue, we can branch on is_mcp(). */
+ printk("HPF unexpectecd state(%s)", procstate2str(p->state));
+ spin_unlock(&p->proc_lock);
+ return -EINVAL;
+ }
+ spin_unlock(&p->proc_lock);
+ ret = pm_load_page(pm, idx, page);
+ if (wake_scp)
+ proc_wakeup(p);
+ if (ret) {
+ printk("load failed with ret %d\n", ret);
+ return ret;
+ }
+ /* need to put our old ref, next time around HPF will get another. */
+ pm_put_page(*page);
+ return 0;
}
-/* Returns 0 on success, or an appropriate -error code. Assumes you hold the
- * vmr_lock.
+/* Returns 0 on success, or an appropriate -error code.
*
* Notes: if your TLB caches negative results, you'll need to flush the
* appropriate tlb entry. Also, you could have a weird race where a present PTE
* shootdown is on its way. Userspace should have waited for the mprotect to
* return before trying to write (or whatever), so we don't care and will fault
* them. */
-int __handle_page_fault(struct proc *p, uintptr_t va, int prot)
+int handle_page_fault(struct proc *p, uintptr_t va, int prot)
{
struct vm_region *vmr;
struct page *a_page;
unsigned int f_idx; /* index of the missing page in the file */
- int retval;
- pte_t *pte;
+ int ret = 0;
+ bool first = TRUE;
+ va = ROUNDDOWN(va,PGSIZE);
+refault:
+ /* read access to the VMRs TODO: RCU */
+ spin_lock(&p->vmr_lock);
/* Check the vmr's protection */
vmr = find_vmr(p, va);
- if (!vmr) /* not mapped at all */
- return -EFAULT;
- if (!(vmr->vm_prot & prot)) /* wrong prots for this vmr */
- return -EPERM;
+ if (!vmr) { /* not mapped at all */
+ printd("fault: %p not mapped\n", va);
+ ret = -EFAULT;
+ goto out;
+ }
+ if (!(vmr->vm_prot & prot)) { /* wrong prots for this vmr */
+ ret = -EPERM;
+ goto out;
+ }
if (!vmr->vm_file) {
/* No file - just want anonymous memory */
- if (upage_alloc(p, &a_page, TRUE))
- return -ENOMEM;
+ if (upage_alloc(p, &a_page, TRUE)) {
+ ret = -ENOMEM;
+ goto out;
+ }
} else {
/* If this fails, either something got screwed up with the VMR, or the
* permissions changed after mmap/mprotect. Either way, I want to know
if (f_idx + 1 > nr_pages(vmr->vm_file->f_dentry->d_inode->i_size)) {
/* We're asking for pages that don't exist in the file */
/* TODO: unlock the file */
- return -ESPIPE; /* linux sends a SIGBUS at access time */
+ ret = -ESPIPE; /* linux sends a SIGBUS at access time */
+ goto out;
+ }
+ ret = pm_load_page_nowait(vmr->vm_file->f_mapping, f_idx, &a_page);
+ if (ret) {
+ if (ret != -EAGAIN)
+ goto out;
+ /* keep the file alive after we unlock */
+ kref_get(&vmr->vm_file->f_kref, 1);
+ spin_unlock(&p->vmr_lock);
+ ret = __hpf_load_page(p, vmr->vm_file->f_mapping, f_idx, &a_page,
+ first);
+ first = FALSE;
+ kref_put(&vmr->vm_file->f_kref);
+ if (ret)
+ return ret;
+ goto refault;
}
- retval = pm_load_page(vmr->vm_file->f_mapping, f_idx, &a_page);
- /* TODO: should be able to let go of that file shrink-lock now. We have
- * a page refcnt, which might be enough (depending on how it works) */
- if (retval)
- return retval;
/* If we want a private map, we'll preemptively give you a new page. We
* used to just care if it was private and writable, but were running
* into issues with libc changing its mapping (map private, then
* mprotect to writable...) In the future, we want to CoW this anyway,
* so it's not a big deal. */
if ((vmr->vm_flags & MAP_PRIVATE)) {
- struct page *cache_page = a_page;
- if (upage_alloc(p, &a_page, FALSE)) {
- page_decref(cache_page); /* was the original a_page */
- return -ENOMEM;
- }
- memcpy(page2kva(a_page), page2kva(cache_page), PGSIZE);
- page_decref(cache_page); /* was the original a_page */
- /* Debugging */
- if (!(vmr->vm_prot & PROT_WRITE))
- printd("[kernel] private, but unwritable file mapping of %s "
- "at va %p\n", file_name(vmr->vm_file), va);
+ ret = __copy_and_swap_pmpg(p, &a_page);
+ if (ret)
+ goto out_put_pg;
}
/* if this is an executable page, we might have to flush the instruction
* cache if our HW requires it. */
* separately (file, no file) */
int pte_prot = (vmr->vm_prot & PROT_WRITE) ? PTE_USER_RW :
(vmr->vm_prot & (PROT_READ|PROT_EXEC)) ? PTE_USER_RO : 0;
- spin_lock(&p->pte_lock); /* walking and changing PTEs */
- /* find offending PTE (prob don't read this in). This might alloc an
- * intermediate page table page. */
- pte = pgdir_walk(p->env_pgdir, (void*)va, 1);
- if (!pte) {
- spin_unlock(&p->pte_lock);
- pm_put_page(a_page);
- return -ENOMEM;
+ ret = map_page_at_addr(p, a_page, va, pte_prot);
+ if (ret) {
+ printd("map_page_at for %p fails with %d\n", va, ret);
}
- /* a spurious, valid PF is possible due to a legit race: the page might have
- * been faulted in by another core already (and raced on the memory lock),
- * in which case we should just return. */
- if (PAGE_PRESENT(*pte)) {
- spin_unlock(&p->pte_lock);
+ /* fall through, even for errors */
+out_put_pg:
+ /* the VMR's existence in the PM (via the mmap) allows us to have PTE point
+ * to a_page without it magically being reallocated. For non-PM memory
+ * (anon memory or private pages) we transferred the ref to the PTE. */
+ if (atomic_read(&a_page->pg_flags) & PG_PAGEMAP)
pm_put_page(a_page);
- return 0;
- } else if (PAGE_PAGED_OUT(*pte)) {
- /* TODO: (SWAP) bring in the paged out frame. (BLK) */
- panic("Swapping not supported!");
- spin_unlock(&p->pte_lock);
- pm_put_page(a_page);
- return -1;
+out:
+ spin_unlock(&p->vmr_lock);
+ return ret;
+}
+
+/* Attempts to populate the pages, as if there was a page faults. Bails on
+ * errors, and returns the number of pages populated. */
+unsigned long populate_va(struct proc *p, uintptr_t va, unsigned long nr_pgs)
+{
+ struct vm_region *vmr, vmr_copy;
+ unsigned long nr_pgs_this_vmr;
+ unsigned long nr_filled = 0;
+ struct page *page;
+ int pte_prot;
+
+ /* we can screw around with ways to limit the find_vmr calls (can do the
+ * next in line if we didn't unlock, etc., but i don't expect us to do this
+ * for more than a single VMR in most cases. */
+ spin_lock(&p->vmr_lock);
+ while (nr_pgs) {
+ vmr = find_vmr(p, va);
+ if (!vmr)
+ break;
+ if (vmr->vm_prot == PROT_NONE)
+ break;
+ pte_prot = (vmr->vm_prot & PROT_WRITE) ? PTE_USER_RW :
+ (vmr->vm_prot & (PROT_READ|PROT_EXEC)) ? PTE_USER_RO : 0;
+ nr_pgs_this_vmr = MIN(nr_pgs, (vmr->vm_end - va) >> PGSHIFT);
+ if (!vmr->vm_file) {
+ if (populate_anon_va(p, va, nr_pgs_this_vmr, pte_prot)) {
+ /* on any error, we can just bail. we might be underestimating
+ * nr_filled. */
+ break;
+ }
+ } else {
+ /* need to keep the file alive in case we unlock/block */
+ kref_get(&vmr->vm_file->f_kref, 1);
+ if (populate_pm_va(p, va, nr_pgs_this_vmr, pte_prot,
+ vmr->vm_file->f_mapping,
+ vmr->vm_foff - (va - vmr->vm_base),
+ vmr->vm_flags, vmr->vm_prot & PROT_EXEC)) {
+ /* we might have failed if the underlying file doesn't cover the
+ * mmap window, depending on how we'll deal with truncation. */
+ break;
+ }
+ kref_put(&vmr->vm_file->f_kref);
+ }
+ nr_filled += nr_pgs_this_vmr;
+ va += nr_pgs_this_vmr << PGSHIFT;
+ nr_pgs -= nr_pgs_this_vmr;
}
- /* We have a ref to a_page, which we are storing in the PTE */
- *pte = PTE(page2ppn(a_page), PTE_P | pte_prot);
- spin_unlock(&p->pte_lock);
- return 0;
+ spin_unlock(&p->vmr_lock);
+ return nr_filled;
}
/* Kernel Dynamic Memory Mappings */
* isn't enough, since there might be a race on outer levels of page tables.
* For now, we'll just use the dyn_vmap_lock (which technically works). */
spin_lock(&dyn_vmap_lock);
- pte_t *pte;
+ pte_t pte;
#ifdef CONFIG_X86
perm |= PTE_G;
#endif
for (int i = 0; i < num_pages; i++) {
pte = pgdir_walk(boot_pgdir, (void*)(vaddr + i * PGSIZE), 1);
- if (!pte) {
+ if (!pte_walk_okay(pte)) {
spin_unlock(&dyn_vmap_lock);
return -ENOMEM;
}
/* You probably should have unmapped first */
- if (*pte)
- warn("Existing PTE value %p\n", *pte);
- *pte = PTE(pa2ppn(paddr + i * PGSIZE), perm);
+ if (pte_is_mapped(pte))
+ warn("Existing PTE value %p\n", pte_print(pte));
+ pte_write(pte, paddr + i * PGSIZE, perm);
}
spin_unlock(&dyn_vmap_lock);
return 0;
warn("Incomplete, don't call this yet.");
spin_lock(&dyn_vmap_lock);
/* TODO: For all pgdirs */
- pte_t *pte;
+ pte_t pte;
for (int i = 0; i < num_pages; i++) {
pte = pgdir_walk(boot_pgdir, (void*)(vaddr + i * PGSIZE), 1);
- *pte = 0;
+ if (pte_walk_okay(pte))
+ pte_clear(pte);
}
/* TODO: TLB shootdown. Also note that the global flag is set on the PTE
* (for x86 for now), which requires a global shootdown. bigger issue is
return 0;
}
-uintptr_t vmap_pmem(uintptr_t paddr, size_t nr_bytes)
+/* This can handle unaligned paddrs */
+static uintptr_t vmap_pmem_flags(uintptr_t paddr, size_t nr_bytes, int flags)
{
uintptr_t vaddr;
- unsigned long nr_pages = ROUNDUP(nr_bytes, PGSIZE) >> PGSHIFT;
+ unsigned long nr_pages;
assert(nr_bytes && paddr);
+ nr_bytes += PGOFF(paddr);
+ nr_pages = ROUNDUP(nr_bytes, PGSIZE) >> PGSHIFT;
vaddr = get_vmap_segment(nr_pages);
if (!vaddr) {
warn("Unable to get a vmap segment"); /* probably a bug */
return 0;
}
- if (map_vmap_segment(vaddr, paddr, nr_pages, PTE_P | PTE_KERN_RW)) {
+ /* it's not strictly necessary to drop paddr's pgoff, but it might save some
+ * vmap heartache in the future. */
+ if (map_vmap_segment(vaddr, PG_ADDR(paddr), nr_pages,
+ PTE_KERN_RW | flags)) {
warn("Unable to map a vmap segment"); /* probably a bug */
return 0;
}
- return vaddr;
+ return vaddr + PGOFF(paddr);
+}
+
+uintptr_t vmap_pmem(uintptr_t paddr, size_t nr_bytes)
+{
+ return vmap_pmem_flags(paddr, nr_bytes, 0);
+}
+
+uintptr_t vmap_pmem_nocache(uintptr_t paddr, size_t nr_bytes)
+{
+ return vmap_pmem_flags(paddr, nr_bytes, PTE_NOCACHE);
}
int vunmap_vmem(uintptr_t vaddr, size_t nr_bytes)