2015-07-09 07:20:15 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2015 Nuxi, https://nuxi.nl/
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2015-07-17 09:00:38 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mman.h>
|
2017-02-06 20:57:12 +00:00
|
|
|
#include <sys/proc.h>
|
2017-02-13 09:04:38 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
2015-07-17 09:00:38 +00:00
|
|
|
|
2016-03-24 21:47:15 +00:00
|
|
|
#include <contrib/cloudabi/cloudabi_types_common.h>
|
|
|
|
|
2015-07-09 07:20:15 +00:00
|
|
|
#include <compat/cloudabi/cloudabi_proto.h>
|
2015-07-17 09:00:38 +00:00
|
|
|
|
|
|
|
/* Converts CloudABI's memory protection flags to FreeBSD's. */
|
|
|
|
static int
|
2016-02-23 09:22:00 +00:00
|
|
|
convert_mprot(cloudabi_mprot_t in, int *out)
|
2015-07-17 09:00:38 +00:00
|
|
|
{
|
|
|
|
|
2016-02-23 09:22:00 +00:00
|
|
|
/* Unknown protection flags. */
|
|
|
|
if ((in & ~(CLOUDABI_PROT_EXEC | CLOUDABI_PROT_WRITE |
|
|
|
|
CLOUDABI_PROT_READ)) != 0)
|
|
|
|
return (ENOTSUP);
|
|
|
|
/* W^X: Write and exec cannot be enabled at the same time. */
|
|
|
|
if ((in & (CLOUDABI_PROT_EXEC | CLOUDABI_PROT_WRITE)) ==
|
|
|
|
(CLOUDABI_PROT_EXEC | CLOUDABI_PROT_WRITE))
|
|
|
|
return (ENOTSUP);
|
|
|
|
|
|
|
|
*out = 0;
|
2015-07-17 09:00:38 +00:00
|
|
|
if (in & CLOUDABI_PROT_EXEC)
|
2016-02-23 09:22:00 +00:00
|
|
|
*out |= PROT_EXEC;
|
2015-07-17 09:00:38 +00:00
|
|
|
if (in & CLOUDABI_PROT_WRITE)
|
2016-02-23 09:22:00 +00:00
|
|
|
*out |= PROT_WRITE;
|
2015-07-17 09:00:38 +00:00
|
|
|
if (in & CLOUDABI_PROT_READ)
|
2016-02-23 09:22:00 +00:00
|
|
|
*out |= PROT_READ;
|
|
|
|
return (0);
|
2015-07-17 09:00:38 +00:00
|
|
|
}
|
2015-07-09 07:20:15 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
cloudabi_sys_mem_advise(struct thread *td,
|
|
|
|
struct cloudabi_sys_mem_advise_args *uap)
|
|
|
|
{
|
2017-02-06 20:57:12 +00:00
|
|
|
int behav;
|
2015-07-17 09:00:38 +00:00
|
|
|
|
|
|
|
switch (uap->advice) {
|
|
|
|
case CLOUDABI_ADVICE_DONTNEED:
|
2017-02-06 20:57:12 +00:00
|
|
|
behav = MADV_DONTNEED;
|
2015-07-17 09:00:38 +00:00
|
|
|
break;
|
|
|
|
case CLOUDABI_ADVICE_NORMAL:
|
2017-02-06 20:57:12 +00:00
|
|
|
behav = MADV_NORMAL;
|
2015-07-17 09:00:38 +00:00
|
|
|
break;
|
|
|
|
case CLOUDABI_ADVICE_RANDOM:
|
2017-02-06 20:57:12 +00:00
|
|
|
behav = MADV_RANDOM;
|
2015-07-17 09:00:38 +00:00
|
|
|
break;
|
|
|
|
case CLOUDABI_ADVICE_SEQUENTIAL:
|
2017-02-06 20:57:12 +00:00
|
|
|
behav = MADV_SEQUENTIAL;
|
2015-07-17 09:00:38 +00:00
|
|
|
break;
|
|
|
|
case CLOUDABI_ADVICE_WILLNEED:
|
2017-02-06 20:57:12 +00:00
|
|
|
behav = MADV_WILLNEED;
|
2015-07-17 09:00:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-02-13 09:04:38 +00:00
|
|
|
return (kern_madvise(td, (uintptr_t)uap->mapping, uap->mapping_len,
|
|
|
|
behav));
|
2015-07-09 07:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cloudabi_sys_mem_map(struct thread *td, struct cloudabi_sys_mem_map_args *uap)
|
|
|
|
{
|
2017-02-06 20:57:12 +00:00
|
|
|
int error, flags, prot;
|
2015-07-17 09:00:38 +00:00
|
|
|
|
|
|
|
/* Translate flags. */
|
2017-02-06 20:57:12 +00:00
|
|
|
flags = 0;
|
2015-07-17 09:00:38 +00:00
|
|
|
if (uap->flags & CLOUDABI_MAP_ANON)
|
2017-02-06 20:57:12 +00:00
|
|
|
flags |= MAP_ANON;
|
2015-07-17 09:00:38 +00:00
|
|
|
if (uap->flags & CLOUDABI_MAP_FIXED)
|
2017-02-06 20:57:12 +00:00
|
|
|
flags |= MAP_FIXED;
|
2015-07-17 09:00:38 +00:00
|
|
|
if (uap->flags & CLOUDABI_MAP_PRIVATE)
|
2017-02-06 20:57:12 +00:00
|
|
|
flags |= MAP_PRIVATE;
|
2015-07-17 09:00:38 +00:00
|
|
|
if (uap->flags & CLOUDABI_MAP_SHARED)
|
2017-02-06 20:57:12 +00:00
|
|
|
flags |= MAP_SHARED;
|
2015-07-17 09:00:38 +00:00
|
|
|
|
2016-02-23 09:22:00 +00:00
|
|
|
/* Translate protection. */
|
2017-02-06 20:57:12 +00:00
|
|
|
error = convert_mprot(uap->prot, &prot);
|
2016-02-23 09:22:00 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
2017-02-13 09:04:38 +00:00
|
|
|
return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot, flags,
|
|
|
|
uap->fd, uap->off));
|
2015-07-09 07:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cloudabi_sys_mem_protect(struct thread *td,
|
|
|
|
struct cloudabi_sys_mem_protect_args *uap)
|
|
|
|
{
|
2017-02-06 20:57:12 +00:00
|
|
|
int error, prot;
|
2016-02-23 09:22:00 +00:00
|
|
|
|
|
|
|
/* Translate protection. */
|
2017-02-06 20:57:12 +00:00
|
|
|
error = convert_mprot(uap->prot, &prot);
|
2016-02-23 09:22:00 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
2015-07-09 07:20:15 +00:00
|
|
|
|
2017-02-13 09:04:38 +00:00
|
|
|
return (kern_mprotect(td, (uintptr_t)uap->mapping, uap->mapping_len,
|
|
|
|
prot));
|
2015-07-09 07:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cloudabi_sys_mem_sync(struct thread *td, struct cloudabi_sys_mem_sync_args *uap)
|
|
|
|
{
|
2017-02-06 20:57:12 +00:00
|
|
|
int flags;
|
2015-07-17 09:00:38 +00:00
|
|
|
|
|
|
|
/* Convert flags. */
|
|
|
|
switch (uap->flags & (CLOUDABI_MS_ASYNC | CLOUDABI_MS_SYNC)) {
|
|
|
|
case CLOUDABI_MS_ASYNC:
|
2017-02-06 20:57:12 +00:00
|
|
|
flags = MS_ASYNC;
|
2015-07-17 09:00:38 +00:00
|
|
|
break;
|
|
|
|
case CLOUDABI_MS_SYNC:
|
2017-02-06 20:57:12 +00:00
|
|
|
flags = MS_SYNC;
|
2015-07-17 09:00:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
if ((uap->flags & CLOUDABI_MS_INVALIDATE) != 0)
|
2017-02-06 20:57:12 +00:00
|
|
|
flags |= MS_INVALIDATE;
|
2015-07-17 09:00:38 +00:00
|
|
|
|
2017-02-13 09:04:38 +00:00
|
|
|
return (kern_msync(td, (uintptr_t)uap->mapping, uap->mapping_len,
|
|
|
|
flags));
|
2015-07-09 07:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cloudabi_sys_mem_unmap(struct thread *td,
|
|
|
|
struct cloudabi_sys_mem_unmap_args *uap)
|
|
|
|
{
|
|
|
|
|
2017-02-13 09:04:38 +00:00
|
|
|
return (kern_munmap(td, (uintptr_t)uap->mapping, uap->mapping_len));
|
2015-07-09 07:20:15 +00:00
|
|
|
}
|