diff options
Diffstat (limited to 'supervisor')
| -rw-r--r-- | supervisor/port.h | 18 | ||||
| -rw-r--r-- | supervisor/shared/rgb_led_status.h | 2 | ||||
| -rw-r--r-- | supervisor/shared/safe_mode.c | 126 | ||||
| -rw-r--r-- | supervisor/shared/safe_mode.h | 47 | ||||
| -rwxr-xr-x | supervisor/shared/stack.c | 14 | ||||
| -rwxr-xr-x | supervisor/shared/stack.h | 7 | ||||
| -rw-r--r-- | supervisor/shared/translate.c | 8 | ||||
| -rw-r--r-- | supervisor/shared/translate.h | 2 | ||||
| -rw-r--r-- | supervisor/stub/serial.c | 1 | ||||
| -rw-r--r-- | supervisor/stub/stack.c | 48 | ||||
| -rw-r--r-- | supervisor/supervisor.mk | 1 |
11 files changed, 263 insertions, 11 deletions
diff --git a/supervisor/port.h b/supervisor/port.h index dd4a82209..59898c8e4 100644 --- a/supervisor/port.h +++ b/supervisor/port.h @@ -33,12 +33,7 @@ #error "Please define PORT_HEAP_SIZE to specify heap size in bytes." #endif -typedef enum { - NO_SAFE_MODE = 0, - BROWNOUT, - HARD_CRASH, - USER_SAFE_MODE, -} safe_mode_t; +#include "supervisor/shared/safe_mode.h" // Provided by the linker; extern uint32_t _ezero; @@ -51,7 +46,10 @@ extern uint32_t _ebss; safe_mode_t port_init(void); -// Reset the microcontroller. +// Reset the microcontroller completely. +void reset_cpu(void); + +// Reset the microcontroller state. void reset_port(void); // Reset the rest of the board. @@ -60,8 +58,8 @@ void reset_board(void); // Reset to the bootloader void reset_to_bootloader(void); -#ifdef NRF52_SERIES -void HardFault_Handler(void); -#endif +// Save and retrieve a word from memory that is preserved over reset. Used for safe mode. +void port_set_saved_word(uint32_t); +uint32_t port_get_saved_word(void); #endif // MICROPY_INCLUDED_SUPERVISOR_PORT_H diff --git a/supervisor/shared/rgb_led_status.h b/supervisor/shared/rgb_led_status.h index 9542b3197..83f4822d7 100644 --- a/supervisor/shared/rgb_led_status.h +++ b/supervisor/shared/rgb_led_status.h @@ -36,6 +36,8 @@ #include "py/mpconfig.h" #include "rgb_led_colors.h" +#include "supervisor/shared/safe_mode.h" + // Overall, the time module must be implemented. // To work with a DotStar, one must have MICROPY_HW_APA102_SCK and // MICROPY_HW_APA102_MOSI defined and bitbangio.SPI or busio.SPI implemented. diff --git a/supervisor/shared/safe_mode.c b/supervisor/shared/safe_mode.c new file mode 100644 index 000000000..c1ce1fc9c --- /dev/null +++ b/supervisor/shared/safe_mode.c @@ -0,0 +1,126 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * 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 "supervisor/shared/safe_mode.h" + +#include "mphalport.h" +// #include "py/mpconfig.h" + +#include "shared-bindings/digitalio/DigitalInOut.h" + +#include "supervisor/serial.h" +#include "supervisor/shared/rgb_led_colors.h" +#include "supervisor/shared/rgb_led_status.h" +#include "supervisor/shared/translate.h" + +#define SAFE_MODE_DATA_GUARD 0xad0000af +#define SAFE_MODE_DATA_GUARD_MASK 0xff0000ff + +static safe_mode_t current_safe_mode; + +safe_mode_t wait_for_safe_mode_reset(void) { + uint32_t reset_state = port_get_saved_word(); + safe_mode_t safe_mode = NO_SAFE_MODE; + if ((reset_state & SAFE_MODE_DATA_GUARD_MASK) == SAFE_MODE_DATA_GUARD) { + safe_mode = (reset_state & ~SAFE_MODE_DATA_GUARD_MASK) >> 8; + } + if (safe_mode != NO_SAFE_MODE) { + port_set_saved_word(SAFE_MODE_DATA_GUARD); + current_safe_mode = safe_mode; + return safe_mode; + } + port_set_saved_word(SAFE_MODE_DATA_GUARD | (MANUAL_SAFE_MODE << 8)); + // Wait for a while to allow for reset. + temp_status_color(SAFE_MODE); + #ifdef MICROPY_HW_LED_STATUS + digitalio_digitalinout_obj_t status_led; + common_hal_digitalio_digitalinout_construct(&status_led, MICROPY_HW_LED_STATUS); + common_hal_digitalio_digitalinout_switch_to_output(&status_led, true, DRIVE_MODE_PUSH_PULL); + #endif + uint64_t start_ticks = ticks_ms; + uint64_t diff = 0; + while (diff < 700) { + #ifdef MICROPY_HW_LED_STATUS + // Blink on for 100, off for 100, on for 100, off for 100 and on for 200 + common_hal_digitalio_digitalinout_set_value(&status_led, diff > 100 && diff / 100 != 2 && diff / 100 != 4); + #endif + diff = ticks_ms - start_ticks; + } + #ifdef MICROPY_HW_LED_STATUS + common_hal_digitalio_digitalinout_deinit(&status_led); + #endif + clear_temp_status(); + port_set_saved_word(SAFE_MODE_DATA_GUARD); + return NO_SAFE_MODE; +} + +void reset_into_safe_mode(safe_mode_t reason) { + if (current_safe_mode > BROWNOUT && reason > BROWNOUT) { + while (true) { + // This very bad because it means running in safe mode didn't save us. Only ignore brownout + // because it may be due to a switch bouncing. + } + } + + port_set_saved_word(SAFE_MODE_DATA_GUARD | (reason << 8)); + reset_cpu(); +} + +void print_safe_mode_message(safe_mode_t reason) { + // Output a user safe mode string if its set. + #ifdef BOARD_USER_SAFE_MODE + if (reason == USER_SAFE_MODE) { + serial_write("\r\n"); + serial_write_compressed(translate("You requested starting safe mode by ")); + serial_write(BOARD_USER_SAFE_MODE_ACTION); + serial_write("\r\n"); + serial_write_compressed(translate("To exit, please reset the board without ")); + serial_write(BOARD_USER_SAFE_MODE_ACTION); + serial_write("\r\n"); + } else + #endif + if (reason != NO_SAFE_MODE) { + serial_write("\r\n"); + serial_write_compressed(translate("You are running in safe mode which means something unanticipated happened.\n")); + if (reason == HARD_CRASH || reason == MICROPY_NLR_JUMP_FAIL || reason == MICROPY_FATAL_ERROR) { + serial_write_compressed(translate("Looks like our core CircuitPython code crashed hard. Whoops!\nPlease file an issue at https://github.com/adafruit/circuitpython/issues\n with the contents of your CIRCUITPY drive and this message:\n")); + if (reason == HARD_CRASH) { + serial_write_compressed(translate("Crash into the HardFault_Handler.\n")); + } else if (reason == MICROPY_NLR_JUMP_FAIL) { + serial_write_compressed(translate("MicroPython NLR jump failed. Likely memory corruption.\n")); + } else if (reason == MICROPY_FATAL_ERROR) { + serial_write_compressed(translate("MicroPython fatal error.\n")); + } + } else if (reason == BROWNOUT) { + serial_write_compressed(translate("The microcontroller's power dipped. Please make sure your power supply provides\nenough power for the whole circuit and press reset (after ejecting CIRCUITPY).\n")); + } else if (reason == HEAP_OVERWRITTEN) { + serial_write_compressed(translate("The CircuitPython heap was corrupted because the stack was too small.\nPlease increase stack size limits and press reset (after ejecting CIRCUITPY).\nIf you didn't change the stack, then file an issue here with the contents of your CIRCUITPY drive:\n")); + serial_write("https://github.com/adafruit/circuitpython/issues\r\n"); + } else if (reason == MANUAL_SAFE_MODE) { + serial_write_compressed(translate("The reset button was pressed while booting CircuitPython. Press again to exit safe mode.\n")); + } + } +} diff --git a/supervisor/shared/safe_mode.h b/supervisor/shared/safe_mode.h new file mode 100644 index 000000000..eba2668ac --- /dev/null +++ b/supervisor/shared/safe_mode.h @@ -0,0 +1,47 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_SUPERVISOR_SAFE_MODE_H +#define MICROPY_INCLUDED_SUPERVISOR_SAFE_MODE_H + +typedef enum { + NO_SAFE_MODE = 0, + BROWNOUT, + HARD_CRASH, + USER_SAFE_MODE, + HEAP_OVERWRITTEN, + MANUAL_SAFE_MODE, + MICROPY_NLR_JUMP_FAIL, + MICROPY_FATAL_ERROR +} safe_mode_t; + +safe_mode_t wait_for_safe_mode_reset(void); + +void reset_into_safe_mode(safe_mode_t reason); + +void print_safe_mode_message(safe_mode_t reason); + +#endif // MICROPY_INCLUDED_SUPERVISOR_SAFE_MODE_H diff --git a/supervisor/shared/stack.c b/supervisor/shared/stack.c index 6b74742eb..71e239c0a 100755 --- a/supervisor/shared/stack.c +++ b/supervisor/shared/stack.c @@ -27,7 +27,9 @@ #include "stack.h" #include "py/mpconfig.h" +#include "py/runtime.h" #include "supervisor/cpu.h" +#include "supervisor/shared/safe_mode.h" extern uint32_t _estack; @@ -50,6 +52,17 @@ void allocate_stack(void) { } else { current_stack_size = next_stack_size; } + *stack_alloc->ptr = STACK_CANARY_VALUE; +} + +inline bool stack_ok(void) { + return *stack_alloc->ptr == STACK_CANARY_VALUE; +} + +inline void assert_heap_ok(void) { + if (!stack_ok()) { + reset_into_safe_mode(HEAP_OVERWRITTEN); + } } void stack_init(void) { @@ -58,6 +71,7 @@ void stack_init(void) { void stack_resize(void) { if (next_stack_size == current_stack_size) { + *stack_alloc->ptr = STACK_CANARY_VALUE; return; } free_memory(stack_alloc); diff --git a/supervisor/shared/stack.h b/supervisor/shared/stack.h index 3a5c18e7d..1fb43be57 100755 --- a/supervisor/shared/stack.h +++ b/supervisor/shared/stack.h @@ -37,5 +37,12 @@ void stack_init(void); void stack_resize(void); void set_next_stack_size(uint32_t size); uint32_t get_current_stack_size(void); +bool stack_ok(void); + +// Use this after any calls into a library which may use a lot of stack. This will raise a Python +// exception when the stack has likely overwritten a portion of the heap. +void assert_heap_ok(void); + +#define STACK_CANARY_VALUE 0x017829ef #endif // MICROPY_INCLUDED_SUPERVISOR_STACK_H diff --git a/supervisor/shared/translate.c b/supervisor/shared/translate.c index 1fa361c15..aa5e4517c 100644 --- a/supervisor/shared/translate.c +++ b/supervisor/shared/translate.c @@ -34,6 +34,14 @@ #include "genhdr/compression.generated.h" #endif +#include "supervisor/serial.h" + +void serial_write_compressed(const compressed_string_t* compressed) { + char decompressed[compressed->length]; + decompress(compressed, decompressed); + serial_write(decompressed); +} + char* decompress(const compressed_string_t* compressed, char* decompressed) { uint8_t this_byte = 0; uint8_t this_bit = 7; diff --git a/supervisor/shared/translate.h b/supervisor/shared/translate.h index 3a1ba4076..5e8acbb6a 100644 --- a/supervisor/shared/translate.h +++ b/supervisor/shared/translate.h @@ -35,7 +35,7 @@ typedef struct { } compressed_string_t; const compressed_string_t* translate(const char* c); - +void serial_write_compressed(const compressed_string_t* compressed); char* decompress(const compressed_string_t* compressed, char* decompressed); #endif // MICROPY_INCLUDED_SUPERVISOR_TRANSLATE_H diff --git a/supervisor/stub/serial.c b/supervisor/stub/serial.c index 4f0e11873..956551914 100644 --- a/supervisor/stub/serial.c +++ b/supervisor/stub/serial.c @@ -43,4 +43,5 @@ bool serial_bytes_available(void) { } void serial_write(const char* text) { + (void) text; } diff --git a/supervisor/stub/stack.c b/supervisor/stub/stack.c new file mode 100644 index 000000000..9a9ecd32f --- /dev/null +++ b/supervisor/stub/stack.c @@ -0,0 +1,48 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * 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 "supervisor/shared/stack.h" + +bool stack_ok(void) { + return true; +} + +void assert_heap_ok(void) { +} + +void stack_init(void) { +} + +void stack_resize(void) { +} + +void set_next_stack_size(uint32_t size) { + (void) size; +} + +uint32_t get_current_stack_size(void) { + return 0; +} diff --git a/supervisor/supervisor.mk b/supervisor/supervisor.mk index 73c76ebb7..56ad8a3ba 100644 --- a/supervisor/supervisor.mk +++ b/supervisor/supervisor.mk @@ -6,6 +6,7 @@ SRC_SUPERVISOR = \ supervisor/shared/flash.c \ supervisor/shared/micropython.c \ supervisor/shared/rgb_led_status.c \ + supervisor/shared/safe_mode.c \ supervisor/shared/stack.c \ supervisor/shared/status_leds.c \ supervisor/shared/translate.c |
