1 /*
2  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3  *               Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10 #define _GNU_SOURCE 1  // needed for mremap
11 #include <sys/mman.h>
12 #include <bits/l4-malloc.h>
13 #include <errno.h>
14 
15 
16 #include <l4/re/mem_alloc>
17 #include <l4/re/dataspace>
18 #include <l4/re/error_helper>
19 #include <l4/cxx/iostream>
20 
21 #include "globals.h"
22 
23 enum {
24   Heap_max = L4_PAGESIZE * 64,
25 };
26 
27 extern char __executable_start[];
28 static void *current_morecore_end;
29 
mc_err_msg(long bytes,char const * msg)30 static void *mc_err_msg(long bytes, char const *msg)
31 {
32   L4::cout << "uclibc_morecore(" << L4::hex << bytes << "): " << msg << ".\n";
33   errno = ENOMEM;
34   return (void *)-1;
35 }
36 
uclibc_morecore(long bytes)37 void *uclibc_morecore(long bytes)
38 {
39   using L4Re::Mem_alloc;
40   using L4Re::Dataspace;
41 
42   // Calling morecore with 0 size is done by the malloc/free implementation
43   // to check for the amount of memory it got from the last call to
44   // morecore.
45   // With a negative value, 'free' wants to return memory, we do not support
46   // that here.
47   if (bytes <= 0)
48     return current_morecore_end;
49 
50   if (!current_morecore_end)
51     {
52       // first call allocates a dataspace
53       L4::Cap<L4Re::Dataspace> heap;
54       heap = Global::cap_alloc->alloc<L4Re::Dataspace>();
55       if (Global::allocator->alloc(Heap_max, heap) < 0)
56         return mc_err_msg(bytes, "Failed to allocate memory");
57 
58       void *hp = __executable_start + 0x100000;
59       if (L4Re::Env::env()->rm()->attach(&hp, Heap_max, L4Re::Rm::F::RW,
60                                          L4::Ipc::make_cap_rw(heap), 0) < 0)
61         return mc_err_msg(bytes, "Failed to attach memory");
62 
63       current_morecore_end = static_cast<char *>(hp) + Heap_max;
64       return hp;
65     }
66 
67   return mc_err_msg(bytes, "Cannot provide more memory");
68 }
69 
mmap(void * start,size_t length,int prot,int flags,int fd,off_t offset)70 void * mmap(void *start, size_t length, int prot,
71             int flags, int fd, off_t offset)
72 noexcept(noexcept(mmap(start, length, prot, flags, fd, offset)))
73 {
74   L4::cout << "mmap() called: unimplemented! size=" << length << "\n";
75   errno = ENOMEM;
76   return MAP_FAILED;
77 }
78 
munmap(void * start,size_t length)79 int munmap(void *start, size_t length) noexcept(noexcept(munmap(start, length)))
80 {
81   L4::cout << "munmap() called: unimplemented!\n";
82   errno = EINVAL;
83   return -1;
84 }
85 
mremap(void * old_address,size_t old_size,size_t new_size,int may_move,...)86 void *mremap(void *old_address, size_t old_size, size_t new_size,
87              int may_move, ...)
88 noexcept(noexcept(mremap(old_address, old_size, new_size, may_move)))
89 {
90   L4::cout << "mremap() called: unimplemented!\n";
91   errno = EINVAL;
92   return MAP_FAILED;
93 }
94