2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / utils / mono-codeman.c
1 #include "config.h"
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <assert.h>
6
7 #ifdef PLATFORM_WIN32
8 #include <windows.h>
9 #include <io.h>
10 #else
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/mman.h>
14 #include <fcntl.h>
15 #endif
16
17 #include "mono-codeman.h"
18
19 #ifdef PLATFORM_WIN32
20 #define FORCE_MALLOC
21 #endif
22
23 #define MIN_PAGES 16
24 #define MIN_ALIGN 8
25 /* if a chunk has less than this amount of free space it's considered full */
26 #define MAX_WASTAGE 32
27 #define MIN_BSIZE 32
28
29 #ifndef MAP_ANONYMOUS
30 #ifdef MAP_ANON
31 #define MAP_ANONYMOUS MAP_ANON
32 #else
33 #define FORCE_MALLOC
34 #endif
35 #endif
36
37 typedef struct _CodeChunck CodeChunk;
38
39 enum {
40         CODE_FLAG_MMAP,
41         CODE_FLAG_MALLOC
42 };
43
44 struct _CodeChunck {
45         char *data;
46         int pos;
47         int size;
48         CodeChunk *next;
49         unsigned int flags: 8;
50         /* this number of bytes is available to resolve addresses far in memory */
51         unsigned int bsize: 24;
52 };
53
54 struct _MonoCodeManager {
55         int dynamic;
56         CodeChunk *current;
57         CodeChunk *full;
58 };
59
60 MonoCodeManager* 
61 mono_code_manager_new (void)
62 {
63         MonoCodeManager *cman = malloc (sizeof (MonoCodeManager));
64         if (!cman)
65                 return NULL;
66         cman->current = NULL;
67         cman->full = NULL;
68         cman->dynamic = 0;
69         return cman;
70 }
71
72 MonoCodeManager* 
73 mono_code_manager_new_dynamic (void)
74 {
75         MonoCodeManager *cman = mono_code_manager_new ();
76         cman->dynamic = 1;
77         return cman;
78 }
79
80
81 static void
82 free_chunklist (CodeChunk *chunk)
83 {
84         CodeChunk *dead;
85         for (; chunk; ) {
86                 dead = chunk;
87                 chunk = chunk->next;
88                 if (dead->flags == CODE_FLAG_MMAP) {
89 #ifndef FORCE_MALLOC
90                         munmap (dead->data, dead->size);
91 #endif
92                 } else if (dead->flags == CODE_FLAG_MALLOC) {
93                         free (dead->data);
94                 }
95                 free (dead);
96         }
97 }
98
99 void
100 mono_code_manager_destroy (MonoCodeManager *cman)
101 {
102         free_chunklist (cman->full);
103         free_chunklist (cman->current);
104         free (cman);
105 }
106
107 /* fill all the memory with the 0x2a (42) value */
108 void             
109 mono_code_manager_invalidate (MonoCodeManager *cman)
110 {
111         CodeChunk *chunk;
112
113 #if defined(__i386__) || defined(__x86_64__)
114         int fill_value = 0xcc; /* x86 break */
115 #else
116         int fill_value = 0x2a;
117 #endif
118
119         for (chunk = cman->current; chunk; chunk = chunk->next)
120                 memset (chunk->data, fill_value, chunk->size);
121         for (chunk = cman->full; chunk; chunk = chunk->next)
122                 memset (chunk->data, fill_value, chunk->size);
123 }
124
125 void
126 mono_code_manager_foreach (MonoCodeManager *cman, MonoCodeManagerFunc func, void *user_data)
127 {
128         CodeChunk *chunk;
129         for (chunk = cman->current; chunk; chunk = chunk->next) {
130                 if (func (chunk->data, chunk->size, chunk->bsize, user_data))
131                         return;
132         }
133         for (chunk = cman->full; chunk; chunk = chunk->next) {
134                 if (func (chunk->data, chunk->size, chunk->bsize, user_data))
135                         return;
136         }
137 }
138
139 static int
140 query_pagesize (void)
141 {
142 #ifdef PLATFORM_WIN32
143         SYSTEM_INFO info;
144         GetSystemInfo (&info);
145         return info.dwAllocationGranularity;
146 #else
147         return getpagesize ();
148 #endif
149 }
150
151 /* BIND_ROOM is the divisor for the chunck of code size dedicated
152  * to binding branches (branches not reachable with the immediate displacement)
153  * bind_size = size/BIND_ROOM;
154  * we should reduce it and make MIN_PAGES bigger for such systems
155  */
156 #if defined(__ppc__) || defined(__powerpc__)
157 #define BIND_ROOM 4
158 #endif
159
160 static CodeChunk*
161 new_codechunk (int dynamic, int size)
162 {
163         static int pagesize = 0;
164         int minsize, flags = CODE_FLAG_MMAP;
165         int chunk_size, bsize = 0;
166         CodeChunk *chunk;
167         void *ptr;
168
169 #ifdef FORCE_MALLOC
170         flags = CODE_FLAG_MALLOC;
171 #endif
172
173         if (!pagesize)
174                 pagesize = query_pagesize ();
175
176         if (dynamic) {
177                 chunk_size = size;
178                 flags = CODE_FLAG_MALLOC;
179         }
180         else {
181                 minsize = pagesize * MIN_PAGES;
182                 if (size < minsize)
183                         chunk_size = minsize;
184                 else {
185                         chunk_size = size;
186                         chunk_size += pagesize - 1;
187                         chunk_size &= ~ (pagesize - 1);
188                 }
189         }
190 #ifdef BIND_ROOM
191         bsize = chunk_size / BIND_ROOM;
192         if (bsize < MIN_BSIZE)
193                 bsize = MIN_BSIZE;
194         bsize += MIN_ALIGN -1;
195         bsize &= ~ (MIN_ALIGN - 1);
196         if (chunk_size - size < bsize) {
197                 if (dynamic)
198                         chunk_size = size + bsize;
199                 else
200                         chunk_size += pagesize;
201         }
202 #endif
203
204         /* does it make sense to use the mmap-like API? */
205         if (flags == CODE_FLAG_MALLOC) {
206                 ptr = malloc (chunk_size);
207                 if (!ptr)
208                         return NULL;
209
210         }
211         else {
212 #ifndef FORCE_MALLOC
213                 ptr = mmap (0, chunk_size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
214                 if (ptr == (void*)-1) {
215                         int fd = open ("/dev/zero", O_RDONLY);
216                         if (fd != -1) {
217                                 ptr = mmap (0, chunk_size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, fd, 0);
218                                 close (fd);
219                         }
220                         if (ptr == (void*)-1) {
221                                 ptr = malloc (chunk_size);
222                                 if (!ptr)
223                                         return NULL;
224                                 flags = CODE_FLAG_MALLOC;
225                         }
226                 }
227 #else
228                 return NULL;
229 #endif
230         }
231
232         if (flags == CODE_FLAG_MALLOC) {
233                 /*
234                  * AMD64 processors maintain icache coherency only for pages which are 
235                  * marked executable.
236                  */
237 #ifndef PLATFORM_WIN32
238                 {
239                         char *page_start = (char *) (((unsigned long long) (ptr)) & ~ (pagesize - 1));
240                         int pages = ((char*)ptr + chunk_size - page_start + pagesize - 1) / pagesize;
241                         int err = mprotect (page_start, pages * pagesize, PROT_READ | PROT_WRITE | PROT_EXEC);
242                         assert (!err);
243                 }
244 #else
245                 {
246                         DWORD oldp;
247                         int err = VirtualProtect (ptr, chunk_size, PAGE_EXECUTE_READWRITE, &oldp);
248                         assert (err);
249                 }
250 #endif
251
252                         /* Make sure the thunks area is zeroed */
253                         memset (ptr, 0, bsize);
254         }
255
256         chunk = malloc (sizeof (CodeChunk));
257         if (!chunk) {
258                 if (flags == CODE_FLAG_MALLOC)
259                         free (ptr);
260 #ifndef FORCE_MALLOC
261                 else
262                         munmap (ptr, chunk_size);
263 #endif
264                 return NULL;
265         }
266         chunk->next = NULL;
267         chunk->size = chunk_size;
268         chunk->data = ptr;
269         chunk->flags = flags;
270         chunk->pos = bsize;
271         chunk->bsize = bsize;
272
273         /*printf ("code chunk at: %p\n", ptr);*/
274         return chunk;
275 }
276
277 void*
278 mono_code_manager_reserve (MonoCodeManager *cman, int size)
279 {
280         CodeChunk *chunk, *prev;
281         void *ptr;
282         
283         size += MIN_ALIGN;
284         size &= ~ (MIN_ALIGN - 1);
285
286         if (!cman->current) {
287                 cman->current = new_codechunk (cman->dynamic, size);
288                 if (!cman->current)
289                         return NULL;
290         }
291
292         for (chunk = cman->current; chunk; chunk = chunk->next) {
293                 if (chunk->pos + size <= chunk->size) {
294                         ptr = chunk->data + chunk->pos;
295                         chunk->pos += size;
296                         return ptr;
297                 }
298         }
299         /* 
300          * no room found, move one filled chunk to cman->full 
301          * to keep cman->current from growing too much
302          */
303         prev = NULL;
304         for (chunk = cman->current; chunk; prev = chunk, chunk = chunk->next) {
305                 if (chunk->pos + MIN_ALIGN * 4 <= chunk->size)
306                         continue;
307                 if (prev) {
308                         prev->next = chunk->next;
309                 } else {
310                         cman->current = chunk->next;
311                 }
312                 chunk->next = cman->full;
313                 cman->full = chunk;
314                 break;
315         }
316         chunk = new_codechunk (cman->dynamic, size);
317         if (!chunk)
318                 return NULL;
319         chunk->next = cman->current;
320         cman->current = chunk;
321         chunk->pos += size;
322         return chunk->data;
323 }
324
325 /* 
326  * if we reserved too much room for a method and we didn't allocate
327  * already from the code manager, we can get back the excess allocation.
328  */
329 void
330 mono_code_manager_commit (MonoCodeManager *cman, void *data, int size, int newsize)
331 {
332         newsize += MIN_ALIGN;
333         newsize &= ~ (MIN_ALIGN - 1);
334         size += MIN_ALIGN;
335         size &= ~ (MIN_ALIGN - 1);
336
337         if (cman->current && (size != newsize) && (data == cman->current->data + cman->current->pos - size)) {
338                 cman->current->pos -= size - newsize;
339         }
340 }
341