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