Browse Source

Add cache flush in py/asmarm.c and add new MP_PLAT_ALLOC_EXEC and MP_PLAT_FREE_EXEC macros

Fixes issue #840
pull/845/head
Fabian Vogt 10 years ago
parent
commit
b7235b8412
  1. 25
      py/asmarm.c
  2. 9
      py/asmthumb.c
  3. 28
      py/asmx64.c
  4. 8
      py/mpconfig.h
  5. 1
      unix/Makefile
  6. 46
      unix/alloc.c
  7. 5
      unix/mpconfigport.h

25
py/asmarm.c

@ -40,8 +40,8 @@
struct _asm_arm_t {
uint pass;
uint code_offset;
uint code_size;
mp_uint_t code_offset;
mp_uint_t code_size;
byte *code_base;
byte dummy_data[4];
@ -64,7 +64,7 @@ asm_arm_t *asm_arm_new(uint max_num_labels) {
void asm_arm_free(asm_arm_t *as, bool free_code) {
if (free_code) {
m_del(byte, as->code_base, as->code_size);
MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
}
m_del_obj(asm_arm_t, as);
@ -80,9 +80,22 @@ void asm_arm_start_pass(asm_arm_t *as, uint pass) {
void asm_arm_end_pass(asm_arm_t *as) {
if (as->pass == ASM_ARM_PASS_COMPUTE) {
// calculate size of code in bytes
as->code_size = as->code_offset;
as->code_base = m_new(byte, as->code_size);
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
if(as->code_base == NULL) {
assert(0);
}
}
else if(as->pass == ASM_ARM_PASS_EMIT) {
#ifdef __arm__
// flush I- and D-cache
asm volatile(
"0:"
"mrc p15, 0, r15, c7, c10, 3\n"
"bne 0b\n"
"mov r0, #0\n"
"mcr p15, 0, r0, c7, c7, 0\n"
: : : "r0", "cc");
#endif
}
}

9
py/asmthumb.c

@ -67,7 +67,7 @@ asm_thumb_t *asm_thumb_new(uint max_num_labels) {
void asm_thumb_free(asm_thumb_t *as, bool free_code) {
if (free_code) {
m_del(byte, as->code_base, as->code_size);
MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
}
/*
if (as->label != NULL) {
@ -94,9 +94,10 @@ void asm_thumb_start_pass(asm_thumb_t *as, uint pass) {
void asm_thumb_end_pass(asm_thumb_t *as) {
if (as->pass == ASM_THUMB_PASS_COMPUTE) {
// calculate size of code in bytes
as->code_size = as->code_offset;
as->code_base = m_new(byte, as->code_size);
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
if(as->code_base == NULL) {
assert(0);
}
//printf("code_size: %u\n", as->code_size);
}

28
py/asmx64.c

@ -113,8 +113,8 @@
struct _asm_x64_t {
uint pass;
uint code_offset;
uint code_size;
mp_uint_t code_offset;
mp_uint_t code_size;
byte *code_base;
byte dummy_data[8];
@ -123,18 +123,6 @@ struct _asm_x64_t {
int num_locals;
};
// for allocating memory, see src/v8/src/platform-linux.cc
void *alloc_mem(uint req_size, uint *alloc_size, bool is_exec) {
req_size = (req_size + 0xfff) & (~0xfff);
int prot = PROT_READ | PROT_WRITE | (is_exec ? PROT_EXEC : 0);
void *ptr = mmap(NULL, req_size, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED) {
assert(0);
}
*alloc_size = req_size;
return ptr;
}
asm_x64_t *asm_x64_new(uint max_num_labels) {
asm_x64_t *as;
@ -147,8 +135,7 @@ asm_x64_t *asm_x64_new(uint max_num_labels) {
void asm_x64_free(asm_x64_t *as, bool free_code) {
if (free_code) {
// need to un-mmap
//m_free(as->code_base);
MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
}
/*
if (as->label != NULL) {
@ -176,11 +163,10 @@ void asm_x64_start_pass(asm_x64_t *as, uint pass) {
void asm_x64_end_pass(asm_x64_t *as) {
if (as->pass == ASM_X64_PASS_COMPUTE) {
// calculate size of code in bytes
as->code_size = as->code_offset;
//as->code_base = m_new(byte, as->code_size); need to allocale executable memory
uint actual_alloc;
as->code_base = alloc_mem(as->code_size, &actual_alloc, true);
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
if(as->code_base == NULL) {
assert(0);
}
//printf("code_size: %u\n", as->code_size);
}

8
py/mpconfig.h

@ -440,6 +440,14 @@ typedef double mp_float_t;
#define MICROPY_MAKE_POINTER_CALLABLE(p) (p)
#endif
#ifndef MP_PLAT_ALLOC_EXEC
#define MP_PLAT_ALLOC_EXEC(min_size, ptr, size) do { *ptr = m_new(byte, min_size); *size = min_size; } while(0)
#endif
#ifndef MP_PLAT_FREE_EXEC
#define MP_PLAT_FREE_EXEC(ptr, size) m_del(byte, ptr, size)
#endif
// printf format spec to use for mp_int_t and friends
#ifndef INT_FMT
#ifdef __LP64__

1
unix/Makefile

@ -75,6 +75,7 @@ SRC_C = \
file.c \
modsocket.c \
modos.c \
alloc.c \
$(SRC_MOD)
ifeq ($(UNAME_S),Darwin)

46
unix/alloc.c

@ -0,0 +1,46 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2014 Fabian Vogt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <sys/mman.h>
#include "mpconfigport.h"
void mp_unix_alloc_exec(mp_uint_t min_size, void** ptr, mp_uint_t *size)
{
// size needs to be a multiple of the page size
*size = (min_size + 0xfff) & (~0xfff);
*ptr = mmap(NULL, *size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (*ptr == MAP_FAILED) {
*ptr = NULL;
}
}
void mp_unix_free_exec(void *ptr, mp_uint_t size)
{
munmap(ptr, size);
}

5
unix/mpconfigport.h

@ -114,6 +114,11 @@ typedef unsigned int mp_uint_t; // must be pointer size
typedef void *machine_ptr_t; // must be of pointer size
typedef const void *machine_const_ptr_t; // must be of pointer size
void mp_unix_alloc_exec(mp_uint_t min_size, void** ptr, mp_uint_t *size);
void mp_unix_free_exec(void *ptr, mp_uint_t size);
#define MP_PLAT_ALLOC_EXEC(min_size, ptr, size) mp_unix_alloc_exec(min_size, ptr, size)
#define MP_PLAT_FREE_EXEC(ptr, size) mp_unix_free_exec(ptr, size)
extern const struct _mp_obj_fun_builtin_t mp_builtin_input_obj;
extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
#define MICROPY_PORT_BUILTINS \

Loading…
Cancel
Save