2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
19 // this is some dupe code to avoid libpicofe dep
21 //#include "../libpicofe/plat.h"
23 /* XXX: maybe unhardcode pagesize? */
24 #define HUGETLB_PAGESIZE (2 * 1024 * 1024)
25 #define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2)
27 #define MAP_HUGETLB 0x40000 /* arch specific */
30 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
32 static int hugetlb_disabled;
33 int prot = PROT_READ | PROT_WRITE;
34 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
42 if (size >= HUGETLB_THRESHOLD && !hugetlb_disabled)
45 ret = mmap(req, size, prot, flags, -1, 0);
46 if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
48 "warning: failed to do hugetlb mmap (%p, %zu): %d\n",
51 flags &= ~MAP_HUGETLB;
52 ret = mmap(req, size, prot, flags, -1, 0);
54 if (ret == MAP_FAILED)
57 if (req != NULL && ret != req)
59 "warning: mmaped to %p, requested %p\n", ret, req);
64 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
68 ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
69 if (ret == MAP_FAILED)
72 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
77 void plat_munmap(void *ptr, size_t size)
81 ret = munmap(ptr, size);
82 if (ret != 0 && (size & (HUGETLB_PAGESIZE - 1))) {
83 // prehaps an autorounded hugetlb mapping?
84 size = (size + HUGETLB_PAGESIZE - 1) & ~(HUGETLB_PAGESIZE - 1);
85 ret = munmap(ptr, size);
89 "munmap(%p, %zu) failed: %d\n", ptr, size, errno);