1 /* $NetBSD: rumpuser_mem.c,v 1.2 2014/08/24 14:37:31 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2007-2010 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include "rumpuser_port.h"
29
30 #if !defined(lint)
31 __RCSID("$NetBSD: rumpuser_mem.c,v 1.2 2014/08/24 14:37:31 pooka Exp $");
32 #endif /* !lint */
33
34 #include <sys/mman.h>
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 #include <rump/rumpuser.h>
43
44 #include "rumpuser_int.h"
45
46 int
rumpuser_malloc(size_t howmuch,int alignment,void ** memp)47 rumpuser_malloc(size_t howmuch, int alignment, void **memp)
48 {
49 void *mem = NULL;
50 int rv;
51
52 if (alignment == 0)
53 alignment = sizeof(void *);
54
55 rv = posix_memalign(&mem, (size_t)alignment, howmuch);
56 if (__predict_false(rv != 0)) {
57 if (rv == EINVAL) {
58 printf("rumpuser_malloc: invalid alignment %d\n",
59 alignment);
60 abort();
61 }
62 }
63
64 *memp = mem;
65 ET(rv);
66 }
67
68 /*ARGSUSED1*/
69 void
rumpuser_free(void * ptr,size_t size)70 rumpuser_free(void *ptr, size_t size)
71 {
72
73 free(ptr);
74 }
75
76 int
rumpuser_anonmmap(void * prefaddr,size_t size,int alignbit,int exec,void ** memp)77 rumpuser_anonmmap(void *prefaddr, size_t size, int alignbit,
78 int exec, void **memp)
79 {
80 void *mem;
81 int prot, rv;
82
83 #ifndef MAP_ALIGNED
84 #define MAP_ALIGNED(a) 0
85 if (alignbit)
86 fprintf(stderr, "rumpuser_anonmmap: warning, requested "
87 "alignment not supported by hypervisor\n");
88 #endif
89
90 prot = PROT_READ|PROT_WRITE;
91 if (exec)
92 prot |= PROT_EXEC;
93 mem = mmap(prefaddr, size, prot,
94 MAP_PRIVATE | MAP_ANON | MAP_ALIGNED(alignbit), -1, 0);
95 if (mem == MAP_FAILED) {
96 rv = errno;
97 } else {
98 *memp = mem;
99 rv = 0;
100 }
101
102 ET(rv);
103 }
104
105 void
rumpuser_unmap(void * addr,size_t len)106 rumpuser_unmap(void *addr, size_t len)
107 {
108
109 munmap(addr, len);
110 }
111
112