1*82dada91Spooka /* $NetBSD: rumpuser_mem.c,v 1.2 2014/08/24 14:37:31 pooka Exp $ */
2*82dada91Spooka
34ce85d0bSjustin /*
44ce85d0bSjustin * Copyright (c) 2007-2010 Antti Kantee. All Rights Reserved.
54ce85d0bSjustin *
64ce85d0bSjustin * Redistribution and use in source and binary forms, with or without
74ce85d0bSjustin * modification, are permitted provided that the following conditions
84ce85d0bSjustin * are met:
94ce85d0bSjustin * 1. Redistributions of source code must retain the above copyright
104ce85d0bSjustin * notice, this list of conditions and the following disclaimer.
114ce85d0bSjustin * 2. Redistributions in binary form must reproduce the above copyright
124ce85d0bSjustin * notice, this list of conditions and the following disclaimer in the
134ce85d0bSjustin * documentation and/or other materials provided with the distribution.
144ce85d0bSjustin *
154ce85d0bSjustin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
164ce85d0bSjustin * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
174ce85d0bSjustin * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
184ce85d0bSjustin * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
194ce85d0bSjustin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
204ce85d0bSjustin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
214ce85d0bSjustin * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224ce85d0bSjustin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234ce85d0bSjustin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
244ce85d0bSjustin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
254ce85d0bSjustin * SUCH DAMAGE.
264ce85d0bSjustin */
274ce85d0bSjustin
284ce85d0bSjustin #include "rumpuser_port.h"
294ce85d0bSjustin
304ce85d0bSjustin #if !defined(lint)
31*82dada91Spooka __RCSID("$NetBSD: rumpuser_mem.c,v 1.2 2014/08/24 14:37:31 pooka Exp $");
324ce85d0bSjustin #endif /* !lint */
334ce85d0bSjustin
344ce85d0bSjustin #include <sys/mman.h>
354ce85d0bSjustin
364ce85d0bSjustin #include <assert.h>
374ce85d0bSjustin #include <errno.h>
384ce85d0bSjustin #include <stdint.h>
394ce85d0bSjustin #include <stdio.h>
404ce85d0bSjustin #include <stdlib.h>
414ce85d0bSjustin
424ce85d0bSjustin #include <rump/rumpuser.h>
434ce85d0bSjustin
444ce85d0bSjustin #include "rumpuser_int.h"
454ce85d0bSjustin
464ce85d0bSjustin int
rumpuser_malloc(size_t howmuch,int alignment,void ** memp)474ce85d0bSjustin rumpuser_malloc(size_t howmuch, int alignment, void **memp)
484ce85d0bSjustin {
494ce85d0bSjustin void *mem = NULL;
504ce85d0bSjustin int rv;
514ce85d0bSjustin
524ce85d0bSjustin if (alignment == 0)
534ce85d0bSjustin alignment = sizeof(void *);
544ce85d0bSjustin
554ce85d0bSjustin rv = posix_memalign(&mem, (size_t)alignment, howmuch);
564ce85d0bSjustin if (__predict_false(rv != 0)) {
574ce85d0bSjustin if (rv == EINVAL) {
584ce85d0bSjustin printf("rumpuser_malloc: invalid alignment %d\n",
594ce85d0bSjustin alignment);
604ce85d0bSjustin abort();
614ce85d0bSjustin }
624ce85d0bSjustin }
634ce85d0bSjustin
644ce85d0bSjustin *memp = mem;
654ce85d0bSjustin ET(rv);
664ce85d0bSjustin }
674ce85d0bSjustin
684ce85d0bSjustin /*ARGSUSED1*/
694ce85d0bSjustin void
rumpuser_free(void * ptr,size_t size)704ce85d0bSjustin rumpuser_free(void *ptr, size_t size)
714ce85d0bSjustin {
724ce85d0bSjustin
734ce85d0bSjustin free(ptr);
744ce85d0bSjustin }
754ce85d0bSjustin
764ce85d0bSjustin int
rumpuser_anonmmap(void * prefaddr,size_t size,int alignbit,int exec,void ** memp)774ce85d0bSjustin rumpuser_anonmmap(void *prefaddr, size_t size, int alignbit,
784ce85d0bSjustin int exec, void **memp)
794ce85d0bSjustin {
804ce85d0bSjustin void *mem;
814ce85d0bSjustin int prot, rv;
824ce85d0bSjustin
834ce85d0bSjustin #ifndef MAP_ALIGNED
844ce85d0bSjustin #define MAP_ALIGNED(a) 0
854ce85d0bSjustin if (alignbit)
864ce85d0bSjustin fprintf(stderr, "rumpuser_anonmmap: warning, requested "
874ce85d0bSjustin "alignment not supported by hypervisor\n");
884ce85d0bSjustin #endif
894ce85d0bSjustin
904ce85d0bSjustin prot = PROT_READ|PROT_WRITE;
914ce85d0bSjustin if (exec)
924ce85d0bSjustin prot |= PROT_EXEC;
934ce85d0bSjustin mem = mmap(prefaddr, size, prot,
944ce85d0bSjustin MAP_PRIVATE | MAP_ANON | MAP_ALIGNED(alignbit), -1, 0);
954ce85d0bSjustin if (mem == MAP_FAILED) {
964ce85d0bSjustin rv = errno;
974ce85d0bSjustin } else {
984ce85d0bSjustin *memp = mem;
994ce85d0bSjustin rv = 0;
1004ce85d0bSjustin }
1014ce85d0bSjustin
1024ce85d0bSjustin ET(rv);
1034ce85d0bSjustin }
1044ce85d0bSjustin
1054ce85d0bSjustin void
rumpuser_unmap(void * addr,size_t len)1064ce85d0bSjustin rumpuser_unmap(void *addr, size_t len)
1074ce85d0bSjustin {
1084ce85d0bSjustin
1094ce85d0bSjustin munmap(addr, len);
1104ce85d0bSjustin }
1114ce85d0bSjustin
112