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