|
| 1 | +#include <stdint.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | +#include <compartment.h> |
| 5 | + |
| 6 | +#include "py/builtin.h" |
| 7 | +#include "py/compile.h" |
| 8 | +#include "py/runtime.h" |
| 9 | +#include "py/repl.h" |
| 10 | +#include "py/gc.h" |
| 11 | +#include "py/mperrno.h" |
| 12 | +#include "shared/runtime/pyexec.h" |
| 13 | + |
| 14 | +#if MICROPY_ENABLE_COMPILER |
| 15 | +void do_str(const char *src, mp_parse_input_kind_t input_kind) { |
| 16 | + nlr_buf_t nlr; |
| 17 | + if (nlr_push(&nlr) == 0) { |
| 18 | + mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); |
| 19 | + qstr source_name = lex->source_name; |
| 20 | + mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); |
| 21 | + mp_obj_t module_fun = mp_compile(&parse_tree, source_name, true); |
| 22 | + mp_call_function_0(module_fun); |
| 23 | + nlr_pop(); |
| 24 | + } else { |
| 25 | + // uncaught exception |
| 26 | + mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); |
| 27 | + } |
| 28 | +} |
| 29 | +#endif |
| 30 | + |
| 31 | +static char *stack_top; |
| 32 | +#if MICROPY_ENABLE_GC |
| 33 | +static char heap[16384]; //[MICROPY_HEAP_SIZE]; |
| 34 | +#endif |
| 35 | + |
| 36 | +#include <stdio.h> |
| 37 | + |
| 38 | +void __cheri_compartment("mp_main") mp_main(void) { |
| 39 | + MP_STATE_THREAD_HACK_INIT |
| 40 | + *MMIO_CAPABILITY(uint32_t, gpio) = 0xaa; |
| 41 | + printf("Test\n"); |
| 42 | + int stack_dummy; |
| 43 | + stack_top = (char *)&stack_dummy; |
| 44 | + #if MICROPY_ENABLE_GC |
| 45 | + gc_init(heap, heap + sizeof(heap)); |
| 46 | + printf("GC initialised\n"); |
| 47 | + #endif |
| 48 | + mp_init(); |
| 49 | + printf("Micropython initialised\n"); |
| 50 | + #if MICROPY_ENABLE_COMPILER |
| 51 | + #if MICROPY_REPL_EVENT_DRIVEN |
| 52 | + pyexec_event_repl_init(); |
| 53 | + for (;;) { |
| 54 | + int c = mp_hal_stdin_rx_chr(); |
| 55 | + if (pyexec_event_repl_process_char(c)) { |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + #else |
| 60 | + pyexec_friendly_repl(); |
| 61 | + #endif |
| 62 | + // do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT); |
| 63 | + // do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT); |
| 64 | + #else |
| 65 | +#error "We don't want to be using frozen modules" |
| 66 | + pyexec_frozen_module("frozentest.py", false); |
| 67 | + #endif |
| 68 | + mp_deinit(); |
| 69 | +} |
| 70 | + |
| 71 | +enum ErrorRecoveryBehaviour compartment_error_handler(struct ErrorState *frame, size_t mcause, size_t mtval) { |
| 72 | + static const char * const cheri_exc_code[32] = { "NONE", "BOUNDS", "TAG", "SEAL" , "04" , "05" , "06", "07", |
| 73 | + "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", |
| 74 | + "10", "EXEC", "LOAD", "STORE", "14", "STCAP", "STLOC", "17", |
| 75 | + "SYSREG", "19", "1a", "1b", "1c", "1d", "1e", "1f" }; |
| 76 | + static const char * const reg_names[16] = { "zr", "ra", "sp", "gp", "tp", "t0", "t1", "t2", |
| 77 | + "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5" }; |
| 78 | + |
| 79 | + if(mcause == 0x1c) { |
| 80 | + printf("CHERI fault (type %s):\n", cheri_exc_code[mtval & 0x1f]); |
| 81 | + printf("\tat %p\n", frame->pcc); |
| 82 | + if(mtval & 0x400) { // S=1, special register |
| 83 | + switch(mtval >> 5) { |
| 84 | + case 0x20: printf("\ton PCC\n"); break; |
| 85 | + case 0x3c: printf("\ton MTCC\n"); break; |
| 86 | + case 0x3d: printf("\ton MTDC\n"); break; |
| 87 | + case 0x3e: printf("\ton MScratchC\n"); break; |
| 88 | + case 0x3f: printf("\ton MEPCC\n"); break; |
| 89 | + default: printf("on unknown special register %ld\n", (mtval >> 5) & 0x1f); |
| 90 | + } |
| 91 | + } else { |
| 92 | + int errreg = mtval >> 5; |
| 93 | + void * errcap = frame->registers[errreg - 1]; |
| 94 | + printf("\ton capability %p [base %lx, len %lx, perms %lx] in register c%s\n", errcap, __builtin_cheri_base_get(errcap), __builtin_cheri_length_get(errcap), __builtin_cheri_perms_get(errcap), reg_names[errreg]); |
| 95 | + } |
| 96 | + } |
| 97 | + else { |
| 98 | + printf("Error (mcause 0x%lx; mtval 0x%lx):\n", mcause, mtval); |
| 99 | + printf("\tat %p\n", frame->pcc); |
| 100 | + } |
| 101 | + printf("\tRegisters:\n"); |
| 102 | + for(int i = 0; i < 15; i++) { |
| 103 | + printf("\t\tc%s : %p [base %lx, len %lx, perms %lx, tag %d]\n", reg_names[i+1], frame->registers[i], __builtin_cheri_base_get(frame->registers[i]), __builtin_cheri_length_get(frame->registers[i]), __builtin_cheri_perms_get(frame->registers[i]), __builtin_cheri_tag_get(frame->registers[i])); |
| 104 | + } |
| 105 | + printf("\tStack view:\n", frame->registers[1]); |
| 106 | + void ** stack = (void**) frame->registers[1]; |
| 107 | + if(__builtin_cheri_tag_get(stack)) for(int i = 0; i < 32; i++) { |
| 108 | + printf("\t\t+%02x : %p [base %lx, len %lx, perms %lx, tag %d]\n", sizeof(void*)*i, stack[i], __builtin_cheri_base_get(stack[i]), __builtin_cheri_length_get(stack[i]), __builtin_cheri_perms_get(stack[i]), __builtin_cheri_tag_get(stack[i])); |
| 109 | + } |
| 110 | + while(1);//mp_raise_OSError(mcause); |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +#if MICROPY_ENABLE_GC |
| 115 | +void gc_collect(void) { |
| 116 | + // WARNING: This gc_collect implementation doesn't try to get root |
| 117 | + // pointers from CPU registers, and thus may function incorrectly. |
| 118 | + void *dummy; |
| 119 | + gc_collect_start(); |
| 120 | + gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); |
| 121 | + gc_collect_end(); |
| 122 | + gc_dump_info(&mp_plat_print); |
| 123 | +} |
| 124 | +#endif |
| 125 | + |
| 126 | +mp_lexer_t *mp_lexer_new_from_file(qstr filename) { |
| 127 | + mp_raise_OSError(MP_ENOENT); |
| 128 | +} |
| 129 | + |
| 130 | +mp_import_stat_t mp_import_stat(const char *path) { |
| 131 | + return MP_IMPORT_STAT_NO_EXIST; |
| 132 | +} |
| 133 | + |
| 134 | +void nlr_jump_fail(void *val) { |
| 135 | + while (1) { |
| 136 | + ; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +void NORETURN __fatal_error(const char *msg) { |
| 141 | + while (1) { |
| 142 | + ; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#ifndef NDEBUG |
| 147 | +void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) { |
| 148 | + printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); |
| 149 | + __fatal_error("Assertion failed"); |
| 150 | +} |
| 151 | +#endif |
| 152 | + |
0 commit comments