2012-10-11 15:15:49 +02:00
|
|
|
|
|
|
|
/* This file implements the methods of anonymous memory.
|
|
|
|
*
|
|
|
|
* Anonymous memory is memory that is for private use to a process
|
|
|
|
* and can not be related to a file (hence anonymous).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "proto.h"
|
|
|
|
#include "vm.h"
|
|
|
|
#include "region.h"
|
|
|
|
#include "glo.h"
|
|
|
|
|
|
|
|
/* These functions are static so as to not pollute the
|
|
|
|
* global namespace, and are accessed through their function
|
|
|
|
* pointers.
|
|
|
|
*/
|
|
|
|
|
2013-03-20 20:18:52 +01:00
|
|
|
static void anon_split(struct vmproc *vmp, struct vir_region *vr,
|
|
|
|
struct vir_region *r1, struct vir_region *r2);
|
|
|
|
static int anon_lowshrink(struct vir_region *vr, vir_bytes len);
|
2012-10-11 15:15:49 +02:00
|
|
|
static int anon_unreference(struct phys_region *pr);
|
|
|
|
static int anon_pagefault(struct vmproc *vmp, struct vir_region *region,
|
2013-06-25 14:41:01 +02:00
|
|
|
struct phys_region *ph, int write, vfs_callback_t cb, void *state,
|
|
|
|
int len, int *io);
|
make vfs & filesystems use failable copying
Change the kernel to add features to vircopy and safecopies so that
transparent copy fixing won't happen to avoid deadlocks, and such copies
fail with EFAULT.
Transparently making copying work from filesystems (as normally done by
the kernel & VM when copying fails because of missing/readonly memory)
is problematic as it can happen that, for file-mapped ranges, that that
same filesystem that is blocked on the copy request is needed to satisfy
the memory range, leading to deadlock. Dito for VFS itself, if done with
a blocking call.
This change makes the copying done from a filesystem fail in such cases
with EFAULT by VFS adding the CPF_TRY flag to the grants. If a FS call
fails with EFAULT, VFS will then request the range to be made available
to VM after the FS is unblocked, allowing it to be used to satisfy the
range if need be in another VFS thread.
Similarly, for datacopies that VFS itself does, it uses the failable
vircopy variant and callers use a wrapper that talk to VM if necessary
to get the copy to work.
. kernel: add CPF_TRY flag to safecopies
. kernel: only request writable ranges to VM for the
target buffer when copying fails
. do copying in VFS TRY-first
. some fixes in VM to build SANITYCHECK mode
. add regression test for the cases where
- a FS system call needs memory mapped in a process that the
FS itself must map.
- such a range covers more than one file-mapped region.
. add 'try' mode to vircopy, physcopy
. add flags field to copy kernel call messages
. if CP_FLAG_TRY is set, do not transparently try
to fix memory ranges
. for use by VFS when accessing user buffers to avoid
deadlock
. remove some obsolete backwards compatability assignments
. VFS: let thread scheduling work for VM requests too
Allows VFS to make calls to VM while suspending and resuming
the currently running thread. Does currently not work for the
main thread.
. VM: add fix memory range call for use by VFS
Change-Id: I295794269cea51a3163519a9cfe5901301d90b32
2014-01-16 14:22:13 +01:00
|
|
|
static int anon_sanitycheck(struct phys_region *pr, const char *file, int line);
|
2012-10-11 15:15:49 +02:00
|
|
|
static int anon_writable(struct phys_region *pr);
|
|
|
|
static int anon_resize(struct vmproc *vmp, struct vir_region *vr, vir_bytes l);
|
|
|
|
static u32_t anon_regionid(struct vir_region *region);
|
|
|
|
static int anon_refcount(struct vir_region *vr);
|
2013-09-25 10:41:26 +02:00
|
|
|
static int anon_pt_flags(struct vir_region *vr);
|
2012-10-11 15:15:49 +02:00
|
|
|
|
|
|
|
struct mem_type mem_type_anon = {
|
|
|
|
.name = "anonymous memory",
|
|
|
|
.ev_unreference = anon_unreference,
|
|
|
|
.ev_pagefault = anon_pagefault,
|
|
|
|
.ev_resize = anon_resize,
|
|
|
|
.ev_sanitycheck = anon_sanitycheck,
|
2013-03-20 20:18:52 +01:00
|
|
|
.ev_lowshrink = anon_lowshrink,
|
|
|
|
.ev_split = anon_split,
|
2012-10-11 15:15:49 +02:00
|
|
|
.regionid = anon_regionid,
|
|
|
|
.writable = anon_writable,
|
2013-09-25 10:41:26 +02:00
|
|
|
.refcount = anon_refcount,
|
|
|
|
.pt_flags = anon_pt_flags,
|
2012-10-11 15:15:49 +02:00
|
|
|
};
|
|
|
|
|
2013-09-25 10:41:26 +02:00
|
|
|
static int anon_pt_flags(struct vir_region *vr){
|
|
|
|
#if defined(__arm__)
|
|
|
|
return ARM_VM_PTE_CACHED;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
static int anon_unreference(struct phys_region *pr)
|
|
|
|
{
|
|
|
|
assert(pr->ph->refcount == 0);
|
|
|
|
if(pr->ph->phys != MAP_NONE)
|
|
|
|
free_mem(ABS2CLICK(pr->ph->phys), 1);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int anon_pagefault(struct vmproc *vmp, struct vir_region *region,
|
2013-06-25 14:41:01 +02:00
|
|
|
struct phys_region *ph, int write, vfs_callback_t cb, void *state,
|
|
|
|
int len, int *io)
|
2012-10-11 15:15:49 +02:00
|
|
|
{
|
|
|
|
phys_bytes new_page, new_page_cl;
|
|
|
|
u32_t allocflags;
|
|
|
|
|
|
|
|
allocflags = vrallocflags(region->flags);
|
|
|
|
|
|
|
|
assert(ph->ph->refcount > 0);
|
|
|
|
|
2013-05-29 13:48:54 +02:00
|
|
|
if((new_page_cl = alloc_mem(1, allocflags)) == NO_MEM) {
|
|
|
|
printf("anon_pagefault: out of memory\n");
|
2012-10-11 15:15:49 +02:00
|
|
|
return ENOMEM;
|
2013-05-29 13:48:54 +02:00
|
|
|
}
|
2012-10-11 15:15:49 +02:00
|
|
|
new_page = CLICK2ABS(new_page_cl);
|
|
|
|
|
|
|
|
/* Totally new block? Create it. */
|
|
|
|
if(ph->ph->phys == MAP_NONE) {
|
|
|
|
ph->ph->phys = new_page;
|
|
|
|
assert(ph->ph->phys != MAP_NONE);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ph->ph->refcount < 2 || !write) {
|
make vfs & filesystems use failable copying
Change the kernel to add features to vircopy and safecopies so that
transparent copy fixing won't happen to avoid deadlocks, and such copies
fail with EFAULT.
Transparently making copying work from filesystems (as normally done by
the kernel & VM when copying fails because of missing/readonly memory)
is problematic as it can happen that, for file-mapped ranges, that that
same filesystem that is blocked on the copy request is needed to satisfy
the memory range, leading to deadlock. Dito for VFS itself, if done with
a blocking call.
This change makes the copying done from a filesystem fail in such cases
with EFAULT by VFS adding the CPF_TRY flag to the grants. If a FS call
fails with EFAULT, VFS will then request the range to be made available
to VM after the FS is unblocked, allowing it to be used to satisfy the
range if need be in another VFS thread.
Similarly, for datacopies that VFS itself does, it uses the failable
vircopy variant and callers use a wrapper that talk to VM if necessary
to get the copy to work.
. kernel: add CPF_TRY flag to safecopies
. kernel: only request writable ranges to VM for the
target buffer when copying fails
. do copying in VFS TRY-first
. some fixes in VM to build SANITYCHECK mode
. add regression test for the cases where
- a FS system call needs memory mapped in a process that the
FS itself must map.
- such a range covers more than one file-mapped region.
. add 'try' mode to vircopy, physcopy
. add flags field to copy kernel call messages
. if CP_FLAG_TRY is set, do not transparently try
to fix memory ranges
. for use by VFS when accessing user buffers to avoid
deadlock
. remove some obsolete backwards compatability assignments
. VFS: let thread scheduling work for VM requests too
Allows VFS to make calls to VM while suspending and resuming
the currently running thread. Does currently not work for the
main thread.
. VM: add fix memory range call for use by VFS
Change-Id: I295794269cea51a3163519a9cfe5901301d90b32
2014-01-16 14:22:13 +01:00
|
|
|
/* memory is ready already */
|
2012-10-11 15:15:49 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(region->flags & VR_WRITABLE);
|
|
|
|
|
2013-03-20 20:18:52 +01:00
|
|
|
return mem_cow(region, ph, new_page_cl, new_page);
|
2012-10-11 15:15:49 +02:00
|
|
|
}
|
|
|
|
|
make vfs & filesystems use failable copying
Change the kernel to add features to vircopy and safecopies so that
transparent copy fixing won't happen to avoid deadlocks, and such copies
fail with EFAULT.
Transparently making copying work from filesystems (as normally done by
the kernel & VM when copying fails because of missing/readonly memory)
is problematic as it can happen that, for file-mapped ranges, that that
same filesystem that is blocked on the copy request is needed to satisfy
the memory range, leading to deadlock. Dito for VFS itself, if done with
a blocking call.
This change makes the copying done from a filesystem fail in such cases
with EFAULT by VFS adding the CPF_TRY flag to the grants. If a FS call
fails with EFAULT, VFS will then request the range to be made available
to VM after the FS is unblocked, allowing it to be used to satisfy the
range if need be in another VFS thread.
Similarly, for datacopies that VFS itself does, it uses the failable
vircopy variant and callers use a wrapper that talk to VM if necessary
to get the copy to work.
. kernel: add CPF_TRY flag to safecopies
. kernel: only request writable ranges to VM for the
target buffer when copying fails
. do copying in VFS TRY-first
. some fixes in VM to build SANITYCHECK mode
. add regression test for the cases where
- a FS system call needs memory mapped in a process that the
FS itself must map.
- such a range covers more than one file-mapped region.
. add 'try' mode to vircopy, physcopy
. add flags field to copy kernel call messages
. if CP_FLAG_TRY is set, do not transparently try
to fix memory ranges
. for use by VFS when accessing user buffers to avoid
deadlock
. remove some obsolete backwards compatability assignments
. VFS: let thread scheduling work for VM requests too
Allows VFS to make calls to VM while suspending and resuming
the currently running thread. Does currently not work for the
main thread.
. VM: add fix memory range call for use by VFS
Change-Id: I295794269cea51a3163519a9cfe5901301d90b32
2014-01-16 14:22:13 +01:00
|
|
|
static int anon_sanitycheck(struct phys_region *pr, const char *file, int line)
|
2012-10-11 15:15:49 +02:00
|
|
|
{
|
|
|
|
MYASSERT(usedpages_add(pr->ph->phys, VM_PAGE_SIZE) == OK);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int anon_writable(struct phys_region *pr)
|
|
|
|
{
|
|
|
|
assert(pr->ph->refcount > 0);
|
2013-03-05 17:17:21 +01:00
|
|
|
if(pr->ph->phys == MAP_NONE)
|
|
|
|
return 0;
|
2012-10-11 15:15:49 +02:00
|
|
|
if(pr->parent->remaps > 0)
|
|
|
|
return 1;
|
2013-03-05 17:17:21 +01:00
|
|
|
return pr->ph->refcount == 1;
|
2012-10-11 15:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int anon_resize(struct vmproc *vmp, struct vir_region *vr, vir_bytes l)
|
|
|
|
{
|
|
|
|
/* Shrinking not implemented; silently ignored.
|
|
|
|
* (Which is ok for brk().)
|
|
|
|
*/
|
|
|
|
if(l <= vr->length)
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
assert(vr);
|
|
|
|
assert(vr->flags & VR_ANON);
|
|
|
|
assert(!(l % VM_PAGE_SIZE));
|
|
|
|
|
|
|
|
USE(vr, vr->length = l;);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32_t anon_regionid(struct vir_region *region)
|
|
|
|
{
|
|
|
|
return region->id;
|
|
|
|
}
|
|
|
|
|
2013-03-20 20:18:52 +01:00
|
|
|
static int anon_lowshrink(struct vir_region *vr, vir_bytes len)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
static int anon_refcount(struct vir_region *vr)
|
|
|
|
{
|
|
|
|
return 1 + vr->remaps;
|
|
|
|
}
|
|
|
|
|
2013-03-20 20:18:52 +01:00
|
|
|
static void anon_split(struct vmproc *vmp, struct vir_region *vr,
|
|
|
|
struct vir_region *r1, struct vir_region *r2)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|