2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#define _SYSTEM 1
|
|
|
|
|
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include <minix/com.h>
|
|
|
|
#include <minix/config.h>
|
|
|
|
#include <minix/const.h>
|
|
|
|
#include <minix/ds.h>
|
|
|
|
#include <minix/endpoint.h>
|
|
|
|
#include <minix/minlib.h>
|
|
|
|
#include <minix/type.h>
|
|
|
|
#include <minix/ipc.h>
|
|
|
|
#include <minix/sysutil.h>
|
|
|
|
#include <minix/syslib.h>
|
|
|
|
#include <minix/safecopies.h>
|
2009-09-21 16:49:49 +02:00
|
|
|
#include <minix/bitmap.h>
|
2010-04-12 13:25:24 +02:00
|
|
|
#include <minix/debug.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
#include <machine/vmparam.h>
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
#include <sys/mman.h>
|
2012-09-18 13:17:52 +02:00
|
|
|
#include <sys/param.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
2010-04-12 13:25:24 +02:00
|
|
|
#include <assert.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <env.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "glo.h"
|
|
|
|
#include "proto.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "region.h"
|
|
|
|
|
2013-03-20 19:45:10 +01:00
|
|
|
|
|
|
|
static struct vir_region *mmap_region(struct vmproc *vmp, vir_bytes addr,
|
|
|
|
u32_t vmm_flags, size_t len, u32_t vrflags,
|
|
|
|
mem_type_t *mt, int execpriv)
|
|
|
|
{
|
|
|
|
u32_t mfflags = 0;
|
|
|
|
struct vir_region *vr = NULL;
|
|
|
|
|
|
|
|
if(vmm_flags & MAP_LOWER16M) vrflags |= VR_LOWER16MB;
|
|
|
|
if(vmm_flags & MAP_LOWER1M) vrflags |= VR_LOWER1MB;
|
2013-11-22 16:38:29 +01:00
|
|
|
if(vmm_flags & MAP_ALIGNMENT_64KB) vrflags |= VR_PHYS64K;
|
2013-03-20 19:45:10 +01:00
|
|
|
if(vmm_flags & MAP_PREALLOC) mfflags |= MF_PREALLOC;
|
|
|
|
if(vmm_flags & MAP_UNINITIALIZED) {
|
|
|
|
if(!execpriv) return NULL;
|
|
|
|
vrflags |= VR_UNINITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len <= 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len % VM_PAGE_SIZE)
|
|
|
|
len += VM_PAGE_SIZE - (len % VM_PAGE_SIZE);
|
|
|
|
|
|
|
|
if (addr && (vmm_flags & MAP_FIXED)) {
|
|
|
|
int r = map_unmap_range(vmp, addr, len);
|
|
|
|
if(r != OK) {
|
|
|
|
printf("mmap_region: map_unmap_range failed (%d)\n", r);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr || (vmm_flags & MAP_FIXED)) {
|
|
|
|
/* An address is given, first try at that address. */
|
|
|
|
vr = map_page_region(vmp, addr, 0, len,
|
|
|
|
vrflags, mfflags, mt);
|
|
|
|
if(!vr && (vmm_flags & MAP_FIXED))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vr) {
|
|
|
|
/* No address given or address already in use. */
|
|
|
|
vr = map_page_region(vmp, VM_PAGE_SIZE, VM_DATATOP, len,
|
|
|
|
vrflags, mfflags, mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return vr;
|
|
|
|
}
|
|
|
|
|
2013-05-07 14:36:09 +02:00
|
|
|
static int mmap_file(struct vmproc *vmp,
|
2013-11-28 17:51:21 +01:00
|
|
|
int vmfd, off_t file_offset, int flags,
|
2013-05-07 14:36:09 +02:00
|
|
|
ino_t ino, dev_t dev, u64_t filesize, vir_bytes addr, vir_bytes len,
|
|
|
|
vir_bytes *retaddr, u16_t clearend, int writable, int mayclosefd)
|
|
|
|
{
|
|
|
|
/* VFS has replied to a VMVFSREQ_FDLOOKUP request. */
|
|
|
|
struct vir_region *vr;
|
2013-11-28 17:51:21 +01:00
|
|
|
u64_t page_offset;
|
2013-05-07 14:36:09 +02:00
|
|
|
int result = OK;
|
|
|
|
u32_t vrflags = 0;
|
|
|
|
|
|
|
|
if(writable) vrflags |= VR_WRITABLE;
|
|
|
|
|
|
|
|
/* Do some page alignments. */
|
|
|
|
if((page_offset = (file_offset % VM_PAGE_SIZE))) {
|
|
|
|
file_offset -= page_offset;
|
|
|
|
len += page_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = roundup(len, VM_PAGE_SIZE);
|
|
|
|
|
|
|
|
/* All numbers should be page-aligned now. */
|
|
|
|
assert(!(len % VM_PAGE_SIZE));
|
|
|
|
assert(!(filesize % VM_PAGE_SIZE));
|
|
|
|
assert(!(file_offset % VM_PAGE_SIZE));
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* XXX ld.so relies on longer-than-file mapping */
|
|
|
|
if((u64_t) len + file_offset > filesize) {
|
|
|
|
printf("VM: truncating mmap dev 0x%x ino %d beyond file size in %d; offset %llu, len %lu, size %llu; ",
|
|
|
|
dev, ino, vmp->vm_endpoint,
|
|
|
|
file_offset, len, filesize);
|
|
|
|
len = filesize - file_offset;
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(!(vr = mmap_region(vmp, addr, flags, len,
|
|
|
|
vrflags, &mem_type_mappedfile, 0))) {
|
|
|
|
result = ENOMEM;
|
|
|
|
} else {
|
|
|
|
*retaddr = vr->vaddr + page_offset;
|
|
|
|
result = OK;
|
|
|
|
|
|
|
|
mappedfile_setfile(vmp, vr, vmfd,
|
|
|
|
file_offset, dev, ino, clearend, 1, mayclosefd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_vfs_mmap(message *m)
|
|
|
|
{
|
|
|
|
vir_bytes v;
|
|
|
|
struct vmproc *vmp;
|
|
|
|
int r, n;
|
|
|
|
u16_t clearend, flags = 0;
|
|
|
|
|
|
|
|
/* It might be disabled */
|
|
|
|
if(!enable_filemap) return ENXIO;
|
|
|
|
|
2013-11-28 18:59:46 +01:00
|
|
|
clearend = m->m_vm_vfs_mmap.clearend;
|
|
|
|
flags = m->m_vm_vfs_mmap.flags;
|
2013-05-07 14:36:09 +02:00
|
|
|
|
2013-11-28 18:59:46 +01:00
|
|
|
if((r=vm_isokendpt(m->m_vm_vfs_mmap.who, &n)) != OK)
|
|
|
|
panic("bad ep %d from vfs", m->m_vm_vfs_mmap.who);
|
2013-05-07 14:36:09 +02:00
|
|
|
vmp = &vmproc[n];
|
|
|
|
|
2013-11-28 18:59:46 +01:00
|
|
|
return mmap_file(vmp, m->m_vm_vfs_mmap.fd, m->m_vm_vfs_mmap.offset,
|
2013-05-07 14:36:09 +02:00
|
|
|
MAP_PRIVATE | MAP_FIXED,
|
2013-11-28 18:59:46 +01:00
|
|
|
m->m_vm_vfs_mmap.ino, m->m_vm_vfs_mmap.dev,
|
2013-05-07 14:36:09 +02:00
|
|
|
(u64_t) LONG_MAX * VM_PAGE_SIZE,
|
2013-11-28 18:59:46 +01:00
|
|
|
m->m_vm_vfs_mmap.vaddr, m->m_vm_vfs_mmap.len, &v,
|
2013-05-07 14:36:09 +02:00
|
|
|
clearend, flags, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mmap_file_cont(struct vmproc *vmp, message *replymsg, void *cbarg,
|
|
|
|
void *origmsg_v)
|
|
|
|
{
|
|
|
|
message *origmsg = (message *) origmsg_v;
|
|
|
|
message mmap_reply;
|
|
|
|
int result;
|
|
|
|
int writable = 0;
|
|
|
|
vir_bytes v = (vir_bytes) MAP_FAILED;
|
|
|
|
|
|
|
|
if(origmsg->VMM_PROT & PROT_WRITE)
|
|
|
|
writable = 1;
|
|
|
|
|
|
|
|
if(replymsg->VMV_RESULT != OK) {
|
libminixfs: allow non-pagesize-multiple FSes
The memory-mapped files implementation (mmap() etc.) is implemented with
the help of the filesystems using the in-VM FS cache. Filesystems tell it
about all cached blocks and their metadata. Metadata is: device offset and,
if any (and known), inode number and in-inode offset. VM can then map in
requested memory-mapped file blocks, and request them if necessary.
A limitation of this system is that filesystem block sizes that are not
a multiple of the VM system (and VM hardware) page size are not possible;
we can't map blocks in partially. (We can copy, but then the benefits of
mapping and sharing the physical pages is gone.) So until before this
commit various pieces of caching code assumed page size multiple
blocksizes. This isn't strictly necessary as long as mmap() needn't be
supported on that FS.
This change allows the in-FS cache code (libminixfs) to allocate any-sized
blocks, and will not interact with the VM cache for non-pagesize-multiple
blocks. In that case it will also signal requestors, by failing 'peek'
requests, that mmap() should not be supported on this FS. VM and VFS
will then gracefully fail all file-mapping mmap() calls, and exec() will
fall back to copying executable blocks instead of mmap()ping executables.
As a result, 3 diagnostics that signal file-mapped mmap()s failing
(hitherto an unusual occurence) are disabled, as ld.so does file-mapped
mmap()s to map in objects it needs. On FSes not supporting it this situation
is legitimate and shouldn't cause so much noise. ld.so will revert to its own
minix-specific allocate+copy style of starting executables if mmap()s fail.
Change-Id: Iecb1c8090f5e0be28da8f5181bb35084eb18f67b
2013-11-19 16:59:52 +01:00
|
|
|
#if 0 /* Noisy diagnostic for mmap() by ld.so */
|
2013-05-07 14:36:09 +02:00
|
|
|
printf("VM: VFS reply failed (%d)\n", replymsg->VMV_RESULT);
|
2013-09-21 00:58:46 +02:00
|
|
|
sys_diagctl_stacktrace(vmp->vm_endpoint);
|
libminixfs: allow non-pagesize-multiple FSes
The memory-mapped files implementation (mmap() etc.) is implemented with
the help of the filesystems using the in-VM FS cache. Filesystems tell it
about all cached blocks and their metadata. Metadata is: device offset and,
if any (and known), inode number and in-inode offset. VM can then map in
requested memory-mapped file blocks, and request them if necessary.
A limitation of this system is that filesystem block sizes that are not
a multiple of the VM system (and VM hardware) page size are not possible;
we can't map blocks in partially. (We can copy, but then the benefits of
mapping and sharing the physical pages is gone.) So until before this
commit various pieces of caching code assumed page size multiple
blocksizes. This isn't strictly necessary as long as mmap() needn't be
supported on that FS.
This change allows the in-FS cache code (libminixfs) to allocate any-sized
blocks, and will not interact with the VM cache for non-pagesize-multiple
blocks. In that case it will also signal requestors, by failing 'peek'
requests, that mmap() should not be supported on this FS. VM and VFS
will then gracefully fail all file-mapping mmap() calls, and exec() will
fall back to copying executable blocks instead of mmap()ping executables.
As a result, 3 diagnostics that signal file-mapped mmap()s failing
(hitherto an unusual occurence) are disabled, as ld.so does file-mapped
mmap()s to map in objects it needs. On FSes not supporting it this situation
is legitimate and shouldn't cause so much noise. ld.so will revert to its own
minix-specific allocate+copy style of starting executables if mmap()s fail.
Change-Id: Iecb1c8090f5e0be28da8f5181bb35084eb18f67b
2013-11-19 16:59:52 +01:00
|
|
|
#endif
|
2013-05-07 14:36:09 +02:00
|
|
|
result = origmsg->VMV_RESULT;
|
|
|
|
} else {
|
|
|
|
/* Finish mmap */
|
2013-11-28 17:51:21 +01:00
|
|
|
result = mmap_file(vmp, replymsg->VMV_FD, origmsg->VMM_OFFSET,
|
|
|
|
origmsg->VMM_FLAGS,
|
2013-05-07 14:36:09 +02:00
|
|
|
replymsg->VMV_INO, replymsg->VMV_DEV,
|
|
|
|
(u64_t) replymsg->VMV_SIZE_PAGES*PAGE_SIZE,
|
2013-11-28 17:51:21 +01:00
|
|
|
(vir_bytes) origmsg->VMM_ADDR,
|
2013-05-07 14:36:09 +02:00
|
|
|
origmsg->VMM_LEN, &v, 0, writable, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unblock requesting process. */
|
|
|
|
memset(&mmap_reply, 0, sizeof(mmap_reply));
|
|
|
|
mmap_reply.m_type = result;
|
2013-11-28 17:51:21 +01:00
|
|
|
mmap_reply.VMM_RETADDR = (void *) v;
|
2013-05-07 14:36:09 +02:00
|
|
|
|
2013-11-01 13:34:14 +01:00
|
|
|
if(ipc_send(vmp->vm_endpoint, &mmap_reply) != OK)
|
|
|
|
panic("VM: mmap_file_cont: ipc_send() failed");
|
2013-05-07 14:36:09 +02:00
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* do_mmap *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_mmap(message *m)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
int r, n;
|
|
|
|
struct vmproc *vmp;
|
2013-11-28 17:51:21 +01:00
|
|
|
vir_bytes addr = (vir_bytes) m->VMM_ADDR;
|
2008-11-19 13:26:10 +01:00
|
|
|
struct vir_region *vr = NULL;
|
2012-06-06 19:05:28 +02:00
|
|
|
int execpriv = 0;
|
2013-03-20 19:45:10 +01:00
|
|
|
size_t len = (vir_bytes) m->VMM_LEN;
|
2012-06-06 19:05:28 +02:00
|
|
|
|
|
|
|
/* RS and VFS can do slightly more special mmap() things */
|
|
|
|
if(m->m_source == VFS_PROC_NR || m->m_source == RS_PROC_NR)
|
|
|
|
execpriv = 1;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-06-06 00:50:13 +02:00
|
|
|
if(m->VMM_FLAGS & MAP_THIRDPARTY) {
|
2012-06-06 19:05:28 +02:00
|
|
|
if(!execpriv) return EPERM;
|
2012-06-06 00:50:13 +02:00
|
|
|
if((r=vm_isokendpt(m->VMM_FORWHOM, &n)) != OK)
|
|
|
|
return ESRCH;
|
|
|
|
} else {
|
|
|
|
/* regular mmap, i.e. for caller */
|
|
|
|
if((r=vm_isokendpt(m->m_source, &n)) != OK) {
|
|
|
|
panic("do_mmap: message from strange source: %d",
|
|
|
|
m->m_source);
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vmp = &vmproc[n];
|
|
|
|
|
2013-05-07 14:36:09 +02:00
|
|
|
/* "SUSv3 specifies that mmap() should fail if length is 0" */
|
|
|
|
if(len <= 0) {
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
if(m->VMM_FD == -1 || (m->VMM_FLAGS & MAP_ANON)) {
|
2013-03-20 19:45:10 +01:00
|
|
|
/* actual memory in some form */
|
|
|
|
mem_type_t *mt = NULL;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2013-05-07 14:36:09 +02:00
|
|
|
if(m->VMM_FD != -1) {
|
2013-03-20 19:45:10 +01:00
|
|
|
printf("VM: mmap: fd %d, len 0x%x\n", m->VMM_FD, len);
|
2008-11-19 13:26:10 +01:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-07-20 20:57:25 +02:00
|
|
|
/* Contiguous phys memory has to be preallocated. */
|
|
|
|
if((m->VMM_FLAGS & (MAP_CONTIG|MAP_PREALLOC)) == MAP_CONTIG) {
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(m->VMM_FLAGS & MAP_CONTIG) {
|
|
|
|
mt = &mem_type_anon_contig;
|
|
|
|
} else mt = &mem_type_anon;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2013-03-20 19:45:10 +01:00
|
|
|
if(!(vr = mmap_region(vmp, addr, m->VMM_FLAGS, len,
|
|
|
|
VR_WRITABLE | VR_ANON, mt, execpriv))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-07 14:36:09 +02:00
|
|
|
/* File mapping might be disabled */
|
|
|
|
if(!enable_filemap) return ENXIO;
|
|
|
|
|
2014-03-17 15:53:28 +01:00
|
|
|
/* For files, we only can't accept writable MAP_SHARED
|
|
|
|
* mappings.
|
|
|
|
*/
|
|
|
|
if((m->VMM_FLAGS & MAP_SHARED) && (m->VMM_PROT & PROT_WRITE)) {
|
2013-05-07 14:36:09 +02:00
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(vfs_request(VMVFSREQ_FDLOOKUP, m->VMM_FD, vmp, 0, 0,
|
|
|
|
mmap_file_cont, NULL, m, sizeof(*m)) != OK) {
|
|
|
|
printf("VM: vfs_request for mmap failed\n");
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* request queued; don't reply. */
|
|
|
|
return SUSPEND;
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return mapping, as seen from process. */
|
2013-11-28 17:51:21 +01:00
|
|
|
m->VMM_RETADDR = (void *) vr->vaddr;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2009-11-03 12:12:23 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* map_perm_check *
|
|
|
|
*===========================================================================*/
|
2013-08-20 14:02:33 +02:00
|
|
|
static int map_perm_check(endpoint_t caller, endpoint_t target,
|
2009-11-03 12:12:23 +01:00
|
|
|
phys_bytes physaddr, phys_bytes len)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/* TTY and memory are allowed to do anything.
|
|
|
|
* They have to be special cases as they have to be able to do
|
|
|
|
* anything; TTY even on behalf of anyone for the TIOCMAPMEM
|
|
|
|
* ioctl. MEM just for itself.
|
|
|
|
*/
|
|
|
|
if(caller == TTY_PROC_NR)
|
|
|
|
return OK;
|
|
|
|
if(caller != target)
|
|
|
|
return EPERM;
|
|
|
|
if(caller == MEM_PROC_NR)
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* Anyone else needs explicit permission from the kernel (ultimately
|
|
|
|
* set by PCI).
|
|
|
|
*/
|
|
|
|
r = sys_privquery_mem(caller, physaddr, len);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* do_map_phys *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_map_phys(message *m)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
int r, n;
|
|
|
|
struct vmproc *vmp;
|
|
|
|
endpoint_t target;
|
|
|
|
struct vir_region *vr;
|
2008-12-11 18:36:33 +01:00
|
|
|
vir_bytes len;
|
2009-11-03 12:12:23 +01:00
|
|
|
phys_bytes startaddr;
|
2010-10-21 19:07:05 +02:00
|
|
|
size_t offset;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
target = m->VMMP_EP;
|
2009-11-03 12:12:23 +01:00
|
|
|
len = m->VMMP_LEN;
|
|
|
|
|
2011-07-19 08:58:01 +02:00
|
|
|
if (len <= 0) return EINVAL;
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
if(target == SELF)
|
|
|
|
target = m->m_source;
|
|
|
|
|
2008-12-08 17:43:20 +01:00
|
|
|
if((r=vm_isokendpt(target, &n)) != OK)
|
2008-11-19 13:26:10 +01:00
|
|
|
return EINVAL;
|
|
|
|
|
2009-11-03 12:12:23 +01:00
|
|
|
startaddr = (vir_bytes)m->VMMP_PHADDR;
|
|
|
|
|
|
|
|
/* First check permission, then round range down/up. Caller can't
|
|
|
|
* help it if we can't map in lower than page granularity.
|
|
|
|
*/
|
|
|
|
if(map_perm_check(m->m_source, target, startaddr, len) != OK) {
|
|
|
|
printf("VM: unauthorized mapping of 0x%lx by %d\n",
|
|
|
|
startaddr, m->m_source);
|
|
|
|
return EPERM;
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
vmp = &vmproc[n];
|
|
|
|
|
2010-10-21 19:07:05 +02:00
|
|
|
offset = startaddr % VM_PAGE_SIZE;
|
|
|
|
len += offset;
|
|
|
|
startaddr -= offset;
|
|
|
|
|
2008-12-11 18:36:33 +01:00
|
|
|
if(len % VM_PAGE_SIZE)
|
|
|
|
len += VM_PAGE_SIZE - (len % VM_PAGE_SIZE);
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(!(vr = map_page_region(vmp, 0, VM_DATATOP, len,
|
|
|
|
VR_DIRECT | VR_WRITABLE, 0, &mem_type_directphys))) {
|
2008-11-19 13:26:10 +01:00
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
phys_setphys(vr, startaddr);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
m->VMMP_VADDR_REPLY = (void *) (vr->vaddr + offset);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_remap *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_remap(message *m)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
2010-06-09 11:14:53 +02:00
|
|
|
int dn, sn;
|
2012-10-11 15:15:49 +02:00
|
|
|
vir_bytes da, sa;
|
2009-09-21 16:49:49 +02:00
|
|
|
size_t size;
|
2012-10-11 15:15:49 +02:00
|
|
|
u32_t flags;
|
|
|
|
struct vir_region *src_region, *vr;
|
2009-09-21 16:49:49 +02:00
|
|
|
struct vmproc *dvmp, *svmp;
|
|
|
|
int r;
|
2011-11-28 19:05:50 +01:00
|
|
|
int readonly;
|
|
|
|
|
|
|
|
if(m->m_type == VM_REMAP)
|
|
|
|
readonly = 0;
|
|
|
|
else if(m->m_type == VM_REMAP_RO)
|
|
|
|
readonly = 1;
|
|
|
|
else panic("do_remap: can't be");
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
da = (vir_bytes) m->VMRE_DA;
|
|
|
|
sa = (vir_bytes) m->VMRE_SA;
|
|
|
|
size = m->VMRE_SIZE;
|
|
|
|
|
2011-07-19 08:58:01 +02:00
|
|
|
if (size <= 0) return EINVAL;
|
|
|
|
|
2010-06-09 11:14:53 +02:00
|
|
|
if ((r = vm_isokendpt((endpoint_t) m->VMRE_D, &dn)) != OK)
|
2009-09-21 16:49:49 +02:00
|
|
|
return EINVAL;
|
2010-06-09 11:14:53 +02:00
|
|
|
if ((r = vm_isokendpt((endpoint_t) m->VMRE_S, &sn)) != OK)
|
2009-09-21 16:49:49 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
dvmp = &vmproc[dn];
|
|
|
|
svmp = &vmproc[sn];
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if (!(src_region = map_lookup(svmp, sa, NULL)))
|
2009-09-21 16:49:49 +02:00
|
|
|
return EINVAL;
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(src_region->vaddr != sa) {
|
2010-04-12 13:25:24 +02:00
|
|
|
printf("VM: do_remap: not start of region.\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size % VM_PAGE_SIZE)
|
|
|
|
size += VM_PAGE_SIZE - size % VM_PAGE_SIZE;
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(size != src_region->length) {
|
2010-04-12 13:25:24 +02:00
|
|
|
printf("VM: do_remap: not size of region.\n");
|
|
|
|
return EFAULT;
|
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
flags = VR_SHARED;
|
|
|
|
if(!readonly)
|
|
|
|
flags |= VR_WRITABLE;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(da)
|
|
|
|
vr = map_page_region(dvmp, da, 0, size, flags, 0,
|
|
|
|
&mem_type_shared);
|
|
|
|
else
|
|
|
|
vr = map_page_region(dvmp, 0, VM_DATATOP, size, flags, 0,
|
|
|
|
&mem_type_shared);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if(!vr) {
|
|
|
|
printf("VM: re-map of shared area failed\n");
|
|
|
|
return ENOMEM;
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
shared_setsource(vr, svmp->vm_endpoint, src_region);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
m->VMRE_RETA = (char *) vr->vaddr;
|
2009-09-21 16:49:49 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_get_phys *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_get_phys(message *m)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
|
|
|
int r, n;
|
|
|
|
struct vmproc *vmp;
|
|
|
|
endpoint_t target;
|
|
|
|
phys_bytes ret;
|
|
|
|
vir_bytes addr;
|
|
|
|
|
|
|
|
target = m->VMPHYS_ENDPT;
|
|
|
|
addr = m->VMPHYS_ADDR;
|
|
|
|
|
|
|
|
if ((r = vm_isokendpt(target, &n)) != OK)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
vmp = &vmproc[n];
|
|
|
|
|
|
|
|
r = map_get_phys(vmp, addr, &ret);
|
|
|
|
|
|
|
|
m->VMPHYS_RETA = ret;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_get_refcount *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_get_refcount(message *m)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
|
|
|
int r, n;
|
|
|
|
struct vmproc *vmp;
|
|
|
|
endpoint_t target;
|
|
|
|
u8_t cnt;
|
|
|
|
vir_bytes addr;
|
|
|
|
|
|
|
|
target = m->VMREFCNT_ENDPT;
|
|
|
|
addr = m->VMREFCNT_ADDR;
|
|
|
|
|
|
|
|
if ((r = vm_isokendpt(target, &n)) != OK)
|
2008-11-19 13:26:10 +01:00
|
|
|
return EINVAL;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
vmp = &vmproc[n];
|
|
|
|
|
|
|
|
r = map_get_ref(vmp, addr, &cnt);
|
|
|
|
|
|
|
|
m->VMREFCNT_RETC = cnt;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_munmap *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_munmap(message *m)
|
2009-09-21 16:49:49 +02:00
|
|
|
{
|
|
|
|
int r, n;
|
|
|
|
struct vmproc *vmp;
|
2013-03-20 20:18:52 +01:00
|
|
|
vir_bytes addr, len;
|
2012-10-11 15:15:49 +02:00
|
|
|
endpoint_t target = SELF;
|
|
|
|
|
|
|
|
if(m->m_type == VM_UNMAP_PHYS) {
|
|
|
|
target = m->VMUP_EP;
|
|
|
|
} else if(m->m_type == VM_SHM_UNMAP) {
|
|
|
|
target = m->VMUN_ENDPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(target == SELF)
|
|
|
|
target = m->m_source;
|
2012-09-18 13:17:52 +02:00
|
|
|
|
2012-10-11 15:15:49 +02:00
|
|
|
if((r=vm_isokendpt(target, &n)) != OK) {
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("do_mmap: message from strange source: %d", m->m_source);
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vmp = &vmproc[n];
|
|
|
|
|
2012-10-13 19:07:47 +02:00
|
|
|
if(m->m_type == VM_UNMAP_PHYS) {
|
|
|
|
addr = (vir_bytes) m->VMUP_VADDR;
|
|
|
|
} else if(m->m_type == VM_SHM_UNMAP) {
|
2012-10-11 15:15:49 +02:00
|
|
|
addr = (vir_bytes) m->VMUN_ADDR;
|
|
|
|
} else addr = (vir_bytes) m->VMUM_ADDR;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2012-09-18 13:17:52 +02:00
|
|
|
if(addr % VM_PAGE_SIZE)
|
|
|
|
return EFAULT;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2012-10-13 19:07:47 +02:00
|
|
|
if(m->m_type == VM_UNMAP_PHYS || m->m_type == VM_SHM_UNMAP) {
|
2013-03-20 20:18:52 +01:00
|
|
|
struct vir_region *vr;
|
|
|
|
if(!(vr = map_lookup(vmp, addr, NULL))) {
|
|
|
|
printf("VM: unmap: address 0x%lx not found in %d\n",
|
|
|
|
addr, target);
|
2013-09-21 00:58:46 +02:00
|
|
|
sys_diagctl_stacktrace(target);
|
2013-03-20 20:18:52 +01:00
|
|
|
return EFAULT;
|
|
|
|
}
|
2012-10-11 15:15:49 +02:00
|
|
|
len = vr->length;
|
|
|
|
} else len = roundup(m->VMUM_LEN, VM_PAGE_SIZE);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2013-03-20 20:18:52 +01:00
|
|
|
return map_unmap_range(vmp, addr, len);
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
|