8000 Merge pull request #845 from Vogtinator/master · tmbinc/micropython@91fbea2 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 91fbea2

Browse files
committed
Merge pull request micropython#845 from Vogtinator/master
Add allocation macros (per platform) and ARM cache flush
2 parents fc54250 + b7235b8 commit 91fbea2

File tree

7 files changed

+91
-31
lines changed

7 files changed

+91
-31
lines changed

py/asmarm.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040

4141
struct _asm_arm_t {
4242
uint pass;
43-
uint code_offset;
44-
uint code_size;
43+
mp_uint_t code_offset;
44+
mp_uint_t code_size;
4545
byte *code_base;
4646
byte dummy_data[4];
4747

@@ -64,7 +64,7 @@ asm_arm_t *asm_arm_new(uint max_num_labels) {
6464

6565
void asm_arm_free(asm_arm_t *as, bool free_code) {
6666
if (free_code) {
67-
m_del(byte, as->code_base, as->code_size);
67+
MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
6868
}
6969

7070
m_del_obj(asm_arm_t, as);
@@ -80,9 +80,22 @@ void asm_arm_start_pass(asm_arm_t *as, uint pass) {
8080

8181
void asm_arm_end_pass(asm_arm_t *as) {
8282
if (as->pass == ASM_ARM_PASS_COMPUTE) {
83-
// calculate size of code in bytes
84-
as->code_size = as->code_offset;
85-
as->code_base = m_new(byte, as->code_size);
83+
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
84+
if(as->code_base == NULL) {
85+
assert(0);
86+
}
87+
}
88+
else if(as->pass == ASM_ARM_PASS_EMIT) {
89+
#ifdef __arm__
90+
// flush I- and D-cache
91+
asm volatile(
92+
"0:"
93+
"mrc p15, 0, r15, c7, c10, 3\n"
94+
"bne 0b\n"
95+
"mov r0, #0\n"
96+
"mcr p15, 0, r0, c7, c7, 0\n"
97+
: : : "r0", "cc");
98+
#endif
8699
}
87100
}
88101

py/asmthumb.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ asm_thumb_t *asm_thumb_new(uint max_num_labels) {
6767

6868
void asm_thumb_free(asm_thumb_t *as, bool free_code) {
6969
if (free_code) {
70-
m_del(byte, as->code_base, as->code_size);
70+
MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
7171
}
7272
/*
7373
if (as->label != NULL) {
@@ -94,9 +94,10 @@ void asm_thumb_start_pass(asm_thumb_t *as, uint pass) {
9494

9595
void asm_thumb_end_pass(asm_thumb_t *as) {
9696
if (as->pass == ASM_THUMB_PASS_COMPUTE) {
97-
// calculate size of code in bytes
98-
as->code_size = as->code_offset;
99-
as->code_base = m_new(byte, as->code_size);
97+
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
98+
if(as->code_base == NULL) {
99+
assert(0);
100+
}
100101
//printf("code_size: %u\n", as->code_size);
101102
}
102103

py/asmx64.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@
113113

114114
struct _asm_x64_t {
115115
uint pass;
116-
uint code_offset;
117-
uint code_size;
116+
mp_uint_t code_offset;
117+
mp_uint_t code_size;
118118
byte *code_base;
119119
byte dummy_data[8];
120120

@@ -123,18 +123,6 @@ struct _asm_x64_t {
123123
int num_locals;
124124
};
125125

126-
// for allocating memory, see src/v8/src/platform-linux.cc
127-
void *alloc_mem(uint req_size, uint *alloc_size, bool is_exec) {
128-
req_size = (req_size + 0xfff) & (~0xfff);
129-
int prot = PROT_READ | PROT_WRITE | (is_exec ? PROT_EXEC : 0);
130-
void *ptr = mmap(NULL, req_size, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
131-
if (ptr == MAP_FAILED) {
132-
assert(0);
133-
}
134-
*alloc_size = req_size;
135-
return ptr;
136-
}
137-
138126
asm_x64_t *asm_x64_new(uint max_num_labels) {
139127
asm_x64_t *as;
140128

@@ -147,8 +135,7 @@ asm_x64_t *asm_x64_new(uint max_num_labels) {
147135

148136
void asm_x64_free(asm_x64_t *as, bool free_code) {
149137
if (free_code) {
150-
// need to un-mmap
151-
//m_free(as->code_base);
138+
MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
152139
}
153140
/*
154141
if (as->label != NULL) {
@@ -176,11 +163,10 @@ void asm_x64_start_pass(asm_x64_t *as, uint pass) {
176163

177164
void asm_x64_end_pass(asm_x64_t *as) {
178165
if (as->pass == ASM_X64_PASS_COMPUTE) {
179-
// calculate size of code in bytes
180-
as->code_size = as->code_offset;
181-
//as->code_base = m_new(byte, as->code_size); need to allocale executable memory
182-
uint actual_alloc;
183-
as->code_base = alloc_mem(as->code_size, &actual_alloc, true);
166+
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
167+
if(as->code_base == NULL) {
168+
assert(0);
169+
}
184170
//printf("code_size: %u\n", as->code_size);
185171
}
186172

py/mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,14 @@ typedef double mp_float_t;
440440
#define MICROPY_MAKE_POINTER_CALLABLE(p) (p)
441441
#endif
442442

443+
#ifndef MP_PLAT_ALLOC_EXEC
444+
#define MP_PLAT_ALLOC_EXEC(min_size, ptr, size) do { *ptr = m_new(byte, min_size); *size = min_size; } while(0)
445+
#endif
446+
447+
#ifndef MP_PLAT_FREE_EXEC
448+
#define MP_PLAT_FREE_EXEC(ptr, size) m_del(byte, ptr, size)
449+
#endif
450+
443451
// printf format spec to use for mp_int_t and friends
444452
#ifndef INT_FMT
445453
#ifdef __LP64__

unix/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ SRC_C = \
7575
file.c \
7676
modsocket.c \
7777
modos.c \
78+
alloc.c \
7879
$(SRC_MOD)
7980

8081
ifeq ($(UNAME_S),Darwin)

unix/alloc.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of the Micro Python 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 <stdint.h>
28+
#include <stdlib.h>
29+
#include <sys/mman.h>
30+
31+
#include "mpconfigport.h"
32+
33+
void mp_unix_alloc_exec(mp_uint_t min_size, void** ptr, mp_uint_t *size)
34+
{
35+
// size needs to be a multiple of the page size
36+
*size = (min_size + 0xfff) & (~0xfff);
37+
*ptr = mmap(NULL, *size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
38+
if (*ptr == MAP_FAILED) {
39+
*ptr = NULL;
40+
}
41+
}
42+
43+
void mp_unix_free_exec(void *ptr, mp_uint_t size)
44+
{
45+
munmap(ptr, size);
46+
}

unix/mpconfigport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ typedef unsigned int mp_uint_t; // must be pointer size
114114
typedef void *machine_ptr_t; // must be of pointer size
115115
typedef const void *machine_const_ptr_t; // must be of pointer size
116116

117+
void mp_unix_alloc_exec(mp_uint_t min_size, void** ptr, mp_uint_t *size);
118+
void mp_unix_free_exec(void *ptr, mp_uint_t size);
119+
#define MP_PLAT_ALLOC_EXEC(min_size, ptr, size) mp_unix_alloc_exec(min_size, ptr, size)
120+
#define MP_PLAT_FREE_EXEC(ptr, size) mp_unix_free_exec(ptr, size)
121+
117122
extern const struct _mp_obj_fun_builtin_t mp_builtin_input_obj;
118123
extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
119124
#define MICROPY_PORT_BUILTINS \

0 commit comments

Comments
 (0)
0