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