1/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2014 Fabian Vogt
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <stdio.h>
28#include <stdint.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/mman.h>
32
33#include "py/mpstate.h"
34#include "py/gc.h"
35
36#if MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC)
37
38#if defined(__OpenBSD__) || defined(__MACH__)
39#define MAP_ANONYMOUS MAP_ANON
40#endif
41
42// The memory allocated here is not on the GC heap (and it may contain pointers
43// that need to be GC'd) so we must somehow trace this memory. We do it by
44// keeping a linked list of all mmap'd regions, and tracing them explicitly.
45
46typedef struct _mmap_region_t {
47 void *ptr;
48 size_t len;
49 struct _mmap_region_t *next;
50} mmap_region_t;
51
52void mp_unix_alloc_exec(size_t min_size, void **ptr, size_t *size) {
53 // size needs to be a multiple of the page size
54 *size = (min_size + 0xfff) & (~0xfff);
55 *ptr = mmap(NULL, *size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
56 if (*ptr == MAP_FAILED) {
57 *ptr = NULL;
58 }
59
60 // add new link to the list of mmap'd regions
61 mmap_region_t *rg = m_new_obj(mmap_region_t);
62 rg->ptr = *ptr;
63 rg->len = min_size;
64 rg->next = MP_STATE_VM(mmap_region_head);
65 MP_STATE_VM(mmap_region_head) = rg;
66}
67
68void mp_unix_free_exec(void *ptr, size_t size) {
69 munmap(ptr, size);
70
71 // unlink the mmap'd region from the list
72 for (mmap_region_t **rg = (mmap_region_t **)&MP_STATE_VM(mmap_region_head); *rg != NULL; *rg = (*rg)->next) {
73 if ((*rg)->ptr == ptr) {
74 mmap_region_t *next = (*rg)->next;
75 m_del_obj(mmap_region_t, *rg);
76 *rg = next;
77 return;
78 }
79 }
80}
81
82void mp_unix_mark_exec(void) {
83 for (mmap_region_t *rg = MP_STATE_VM(mmap_region_head); rg != NULL; rg = rg->next) {
84 gc_collect_root(rg->ptr, rg->len / sizeof(mp_uint_t));
85 }
86}
87
88#if MICROPY_FORCE_PLAT_ALLOC_EXEC
89// Provide implementation of libffi ffi_closure_* functions in terms
90// of the functions above. On a normal Linux system, this save a lot
91// of code size.
92void *ffi_closure_alloc(size_t size, void **code);
93void ffi_closure_free(void *ptr);
94
95void *ffi_closure_alloc(size_t size, void **code) {
96 size_t dummy;
97 mp_unix_alloc_exec(size, code, &dummy);
98 return *code;
99}
100
101void ffi_closure_free(void *ptr) {
102 (void)ptr;
103 // TODO
104}
105#endif
106
107#endif // MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC)
108