11 #include <sys/types.h>
17 #include "mono-codeman.h"
25 /* if a chunk has less than this amount of free space it's considered full */
26 #define MAX_WASTAGE 32
31 #define MAP_ANONYMOUS MAP_ANON
37 typedef struct _CodeChunck CodeChunk;
49 unsigned int flags: 8;
50 /* this number of bytes is available to resolve addresses far in memory */
51 unsigned int bsize: 24;
54 struct _MonoCodeManager {
61 mono_code_manager_new (void)
63 MonoCodeManager *cman = malloc (sizeof (MonoCodeManager));
73 mono_code_manager_new_dynamic (void)
75 MonoCodeManager *cman = mono_code_manager_new ();
82 free_chunklist (CodeChunk *chunk)
88 if (dead->flags == CODE_FLAG_MMAP) {
90 munmap (dead->data, dead->size);
92 } else if (dead->flags == CODE_FLAG_MALLOC) {
100 mono_code_manager_destroy (MonoCodeManager *cman)
102 free_chunklist (cman->full);
103 free_chunklist (cman->current);
107 /* fill all the memory with the 0x2a (42) value */
109 mono_code_manager_invalidate (MonoCodeManager *cman)
113 #if defined(__i386__) || defined(__x86_64__)
114 int fill_value = 0xcc; /* x86 break */
116 int fill_value = 0x2a;
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);
126 mono_code_manager_foreach (MonoCodeManager *cman, MonoCodeManagerFunc func, void *user_data)
129 for (chunk = cman->current; chunk; chunk = chunk->next) {
130 if (func (chunk->data, chunk->size, chunk->bsize, user_data))
133 for (chunk = cman->full; chunk; chunk = chunk->next) {
134 if (func (chunk->data, chunk->size, chunk->bsize, user_data))
140 query_pagesize (void)
142 #ifdef PLATFORM_WIN32
144 GetSystemInfo (&info);
145 return info.dwAllocationGranularity;
147 return getpagesize ();
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
156 #if defined(__ppc__) || defined(__powerpc__)
161 new_codechunk (int dynamic, int size)
163 static int pagesize = 0;
164 int minsize, flags = CODE_FLAG_MMAP;
165 int chunk_size, bsize = 0;
170 flags = CODE_FLAG_MALLOC;
174 pagesize = query_pagesize ();
178 flags = CODE_FLAG_MALLOC;
181 minsize = pagesize * MIN_PAGES;
183 chunk_size = minsize;
186 chunk_size += pagesize - 1;
187 chunk_size &= ~ (pagesize - 1);
191 bsize = chunk_size / BIND_ROOM;
192 if (bsize < MIN_BSIZE)
194 bsize += MIN_ALIGN -1;
195 bsize &= ~ (MIN_ALIGN - 1);
196 if (chunk_size - size < bsize) {
198 chunk_size = size + bsize;
200 chunk_size += pagesize;
204 /* does it make sense to use the mmap-like API? */
205 if (flags == CODE_FLAG_MALLOC) {
206 ptr = malloc (chunk_size);
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);
217 ptr = mmap (0, chunk_size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, fd, 0);
220 if (ptr == (void*)-1) {
221 ptr = malloc (chunk_size);
224 flags = CODE_FLAG_MALLOC;
232 if (flags == CODE_FLAG_MALLOC) {
234 * AMD64 processors maintain icache coherency only for pages which are
237 #ifndef PLATFORM_WIN32
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);
247 int err = VirtualProtect (ptr, chunk_size, PAGE_EXECUTE_READWRITE, &oldp);
252 /* Make sure the thunks area is zeroed */
253 memset (ptr, 0, bsize);
256 chunk = malloc (sizeof (CodeChunk));
258 if (flags == CODE_FLAG_MALLOC)
262 munmap (ptr, chunk_size);
267 chunk->size = chunk_size;
269 chunk->flags = flags;
271 chunk->bsize = bsize;
273 /*printf ("code chunk at: %p\n", ptr);*/
278 mono_code_manager_reserve (MonoCodeManager *cman, int size)
280 CodeChunk *chunk, *prev;
284 size &= ~ (MIN_ALIGN - 1);
286 if (!cman->current) {
287 cman->current = new_codechunk (cman->dynamic, size);
292 for (chunk = cman->current; chunk; chunk = chunk->next) {
293 if (chunk->pos + size <= chunk->size) {
294 ptr = chunk->data + chunk->pos;
300 * no room found, move one filled chunk to cman->full
301 * to keep cman->current from growing too much
304 for (chunk = cman->current; chunk; prev = chunk, chunk = chunk->next) {
305 if (chunk->pos + MIN_ALIGN * 4 <= chunk->size)
308 prev->next = chunk->next;
310 cman->current = chunk->next;
312 chunk->next = cman->full;
316 chunk = new_codechunk (cman->dynamic, size);
319 chunk->next = cman->current;
320 cman->current = chunk;
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.
330 mono_code_manager_commit (MonoCodeManager *cman, void *data, int size, int newsize)
332 newsize += MIN_ALIGN;
333 newsize &= ~ (MIN_ALIGN - 1);
335 size &= ~ (MIN_ALIGN - 1);
337 if (cman->current && (size != newsize) && (data == cman->current->data + cman->current->pos - size)) {
338 cman->current->pos -= size - newsize;