Patch configure so that we dont need a seperate spec file for x86_64. Pass info about...
[AROS.git] / arch / all-unix / bootstrap / memory.c
blob6c19ec691c9f12a4240271342798a5711090e0b2
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <sys/mman.h>
5 #include "bootstrap.h"
7 #ifndef MAP_32BIT
8 #define MAP_32BIT 0
9 #endif
11 static void *code = NULL;
12 static void *data = NULL;
13 static void *RAM = NULL;
15 static size_t code_len = 0;
16 static size_t RAM_len = 0;
19 * Allocate memory for kickstart's .code and .rodata. We allocate is as writable
20 * because we will load the kickstart into it. We will enable execution later in SetRO().
21 * We have to use mmap() and not posix_memalign() here because posix_memalign()
22 * does not pad the allocated memory up to next page boundary. As a result, setting
23 * it read-only will affect the whole page, but the page will still have some unallocated
24 * space which can be reused by malloc().
25 * This causes DisplayError() function to crash on iOS. This also may cause similar effects
26 * on other systems.
28 void *AllocateRO(size_t len)
30 /* There's no sense to set MAP_SHARED for ROM */
31 void *ret = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|MAP_32BIT, -1, 0);
33 if (ret == MAP_FAILED)
34 return NULL;
35 else
37 code = ret;
38 code_len = len;
39 return ret;
44 * Disable write and enable execution.
45 * We actually have to do this in two steps because some systems
46 * (Apple iOS) do not allow read-write-execute permissions.
48 int SetRO(void *addr, size_t len)
50 return mprotect(addr, len, PROT_READ|PROT_EXEC);
54 * Allocate kickstart's .data and .bss. Nothing is executed from there, so we
55 * don't have to take some special care about it. Simple malloc() is enough here.
57 void *AllocateRW(size_t len)
59 data = malloc(len);
60 return data;
64 * This routine allocates memory usable as AROS ram. This means it
65 * needs to have full permissions.
66 * Yes, iOS will silently mask out PROT_EXEC here. This is bad.
67 * Well, iOS will be a little bit special story in InternalLoadSeg()...
69 void *AllocateRAM(size_t len)
71 void *ret = mmap(NULL, len, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_SHARED|MAP_32BIT, -1, 0);
73 if (ret == MAP_FAILED)
74 return NULL;
75 else
77 RAM = ret;
78 RAM_len = len;
79 return ret;
83 void Host_FreeMem(void)
85 munmap(code, code_len);
86 free(data);
87 munmap(RAM, RAM_len);
89 free(SystemVersion);
90 if (KernelArgs)
91 free(KernelArgs);