don't print SYSTEM stacktrace on exceptions as it's not scheduled any more.
[minix.git] / lib / libsys / alloc_util.c
blobdf4ffd77be1833164dd42b4af4c97b16e1daa71b
2 #include "syslib.h"
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <sys/mman.h>
7 #include <minix/sysutil.h>
9 int sys_umap_data_fb(endpoint_t ep, vir_bytes buf, vir_bytes len, phys_bytes *phys)
11 int r;
13 if((r=sys_umap(ep, VM_D, buf, len, phys)) != OK) {
14 if(r != EINVAL)
15 return r;
16 r = sys_umap(ep, D, buf, len, phys);
20 return r;
24 void *alloc_contig(size_t len, int flags, phys_bytes *phys)
26 vir_bytes buf;
27 int mmapflags = MAP_PREALLOC|MAP_CONTIG|MAP_ANON;
29 if(flags & AC_LOWER16M)
30 mmapflags |= MAP_LOWER16M;
31 if(flags & AC_LOWER1M)
32 mmapflags |= MAP_LOWER1M;
33 if(flags & AC_ALIGN64K)
34 mmapflags |= MAP_ALIGN64K;
36 /* First try to get memory with mmap. This is gauranteed
37 * to be page-aligned, and we can tell VM it has to be
38 * pre-allocated and contiguous.
40 errno = 0;
41 buf = (vir_bytes) mmap(0, len, PROT_READ|PROT_WRITE, mmapflags, -1, 0);
43 /* If that failed, maybe we're not running in paged mode.
44 * If that's the case, ENXIO will be returned.
45 * Memory returned with malloc() will be preallocated and
46 * contiguous, so fallback on that, and ask for a little extra
47 * so we can page align it ourselves.
49 if(buf == (vir_bytes) MAP_FAILED) {
50 u32_t align = 0;
51 if(errno != (_SIGN ENXIO)) {
52 return NULL;
54 if(flags & AC_ALIGN4K)
55 align = 4*1024;
56 if(flags & AC_ALIGN64K)
57 align = 64*1024;
58 if(len + align < len)
59 return NULL;
60 len += align;
61 if(!(buf = (vir_bytes) malloc(len))) {
62 return NULL;
64 if(align)
65 buf += align - (buf % align);
68 /* Get physical address, if requested. */
69 if(phys != NULL && sys_umap_data_fb(SELF, buf, len, phys) != OK)
70 panic("sys_umap_data_fb failed");
72 return (void *) buf;
75 int free_contig(void *addr, size_t len)
77 return munmap(addr, len);