diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2018-12-06 14:24:20 -0800 |
|---|---|---|
| committer | Scott Shawcroft <scott@tannewt.org> | 2018-12-06 14:24:20 -0800 |
| commit | 6ef863997186e1893cdf19c0e04d0a8a3f33ad9d (patch) | |
| tree | 461360ed76e39ca0a534a0dee040fc1cf6848d70 /supervisor | |
| parent | 7ad2e6ace34a6d417ec1b84472fa409d5451137b (diff) | |
Rework safe mode and have heap overwrite trigger it.
This creates a common safe mode mechanic that ports can share.
As a result, the nRF52 now has safe mode support as well.
The common safe mode adds a 700ms delay at startup where a reset
during that window will cause a reset into safe mode. This window
is designated by a yellow status pixel and flashing the single led
three times.
A couple NeoPixel fixes are included for the nRF52 as well.
Fixes #1034. Fixes #990. Fixes #615.
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 | 7 | ||||
| -rwxr-xr-x | supervisor/shared/stack.h | 4 | ||||
| -rw-r--r-- | supervisor/shared/translate.c | 8 | ||||
| -rw-r--r-- | supervisor/shared/translate.h | 2 | ||||
| -rw-r--r-- | supervisor/supervisor.mk | 1 |
9 files changed, 198 insertions, 17 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 8f4da4ce6..71e239c0a 100755 --- a/supervisor/shared/stack.c +++ b/supervisor/shared/stack.c @@ -29,6 +29,7 @@ #include "py/mpconfig.h" #include "py/runtime.h" #include "supervisor/cpu.h" +#include "supervisor/shared/safe_mode.h" extern uint32_t _estack; @@ -38,8 +39,6 @@ supervisor_allocation* stack_alloc = NULL; #define EXCEPTION_STACK_SIZE 1024 -#define STACK_CANARY_VALUE 0x017829ef - void allocate_stack(void) { mp_uint_t regs[10]; mp_uint_t sp = cpu_get_regs_and_sp(regs); @@ -62,9 +61,7 @@ inline bool stack_ok(void) { inline void assert_heap_ok(void) { if (!stack_ok()) { - asm("nop"); - while(true) {} - mp_raise_RuntimeError(translate("Stack clobbered heap.")); + reset_into_safe_mode(HEAP_OVERWRITTEN); } } diff --git a/supervisor/shared/stack.h b/supervisor/shared/stack.h index 8f1e881f2..1fb43be57 100755 --- a/supervisor/shared/stack.h +++ b/supervisor/shared/stack.h @@ -40,7 +40,9 @@ 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 portio of the heap. +// 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/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 |
