Documented GVF_SAVE_VAR alongside other flags, and removed a query/doubt
[AROS.git] / arch / all-unix / bootstrap / memory.c
blob026c2fb8739acd18458997aecbca68b12a557f88
1 /*
2 Copyright © 1995-2015, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/mman.h>
10 #include <aros/cpu.h> // for __WORDSIZE
12 #include "bootstrap.h"
14 #ifndef MAP_32BIT
15 #define MAP_32BIT 0
16 #endif
18 static void *data = NULL;
19 static void *code = NULL;
20 static size_t code_len = 0;
22 static void *RAM32 = NULL;
23 static size_t RAM32_len = 0;
25 #if (__WORDSIZE == 64)
26 static void *RAM = NULL;
27 static size_t RAM_len = 0;
28 #endif
31 * Allocate memory for kickstart's .code and .rodata. We allocate it as writable
32 * because we will load the kickstart into it. We will enable execution later in SetRO().
33 * We have to use mmap() and not posix_memalign() here because posix_memalign()
34 * does not pad the allocated memory up to next page boundary. As a result, setting
35 * it read-only will affect the whole page, but the page will still have some unallocated
36 * space which can be reused by malloc().
37 * This causes DisplayError() function to crash on iOS. This also may cause similar effects
38 * on other systems.
41 void *doMMap(void **addr_store, size_t *size_store, size_t len, int prot, int flags)
43 /* There's no sense to set MAP_SHARED for ROM */
44 void *ret = mmap(NULL, len, prot, flags, -1, 0);
46 if (ret == MAP_FAILED)
47 ret = NULL;
48 else
50 if (addr_store)
51 *addr_store = ret;
52 if (size_store)
53 *size_store = len;
55 return ret;
58 void *AllocateRO(size_t len)
60 /* There's no sense to set MAP_SHARED for ROM */
61 return doMMap(&code, &code_len, len, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|MAP_32BIT);
65 * Disable write and enable execution.
66 * We actually have to do this in two steps because some systems
67 * (Apple iOS) do not allow read-write-execute permissions.
69 int SetRO(void *addr, size_t len)
71 return mprotect(addr, len, PROT_READ|PROT_EXEC);
75 * Allocate kickstart's .data and .bss. Nothing is executed from there, so we
76 * don't have to take some special care about it. Simple malloc() is enough here.
78 void *AllocateRW(size_t len)
80 data = malloc(len);
81 return data;
85 * These routines allocate memory usable as AROS RAM. This means they
86 * need to have full permissions.
87 * Yes, iOS will silently mask out PROT_EXEC here. This is bad.
88 * Well, iOS will be a little bit special story in InternalLoadSeg()...
90 void *AllocateRAM32(size_t len)
92 return doMMap(&RAM32, &RAM32_len, len, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_SHARED|MAP_32BIT);
95 #if (__WORDSIZE == 64)
96 void *AllocateRAM(size_t len)
98 return doMMap(&RAM, &RAM_len, len, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_SHARED);
100 #endif
102 void Host_FreeMem(void)
104 munmap(code, code_len);
105 free(data);
106 #if (__WORDSIZE == 64)
107 if (RAM)
108 munmap(RAM, RAM_len);
109 #endif
110 munmap(RAM32, RAM32_len);
112 free(SystemVersion);
113 if (KernelArgs)
114 free(KernelArgs);