Skip to content

Commit 726dcdb

Browse files
committed
Add some NORETURN attributes
I have a function where it should be impossible to reach the end, so I put in a safe-mode reset at the bottom: ``` int find_unused_slot(void) { // precondition: you already verified that a slot was available for (int i=0; i<NUM_SLOTS; i++) { if( slot_free(i)) { return i; } } safe_mode_reset(MICROPY_FATAL_ERROR); } ``` However, the compiler still gave a diagnostic, because safe_mode_reset was not declared NORETURN. So I started by teaching the compiler that reset_into_safe_mode never returned. This leads at least one level deeper due to reset_cpu needing to be a NORETURN function. Each port is a little different in this area. I also marked reset_to_bootloader as NORETURN. Additional notes: * stm32's reset_to_bootloader was not implemented, but now does a bare reset. Most stm32s are not fitted with uf2 bootloaders anyway. * ditto cxd56 * esp32s2 did not implement reset_cpu at all. I used esp_restart(). (not tested) * litex did not implement reset_cpu at all. I used reboot_ctrl_write. But notably this is what reset_to_bootloader already did, so one or the other must be incorrect (not tested). reboot_ctrl_write cannot be declared NORETURN, as it returns unless the special value 0xac is written), so a new unreachable forever-loop is added. * cxd56's reset is via a boardctl() call which can't generically be declared NORETURN, so a new unreacahble "for(;;)" forever-loop is added. * In several places, NVIC_SystemReset is redeclared with NORETURN applied. This is accepted just fine by gcc. I chose this as preferable to editing the multiple copies of CMSIS headers where it is normally declared. * the stub safe_mode reset simply aborts. This is used in mpy-cross.
1 parent 6bfcb01 commit 726dcdb

File tree

11 files changed

+35
-10
lines changed

11 files changed

+35
-10
lines changed

ports/atmel-samd/reset.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "reset.h"
3030
#include "supervisor/filesystem.h"
3131

32+
void NVIC_SystemReset(void) NORETURN;
33+
3234
void reset(void) {
3335
filesystem_flush();
3436
NVIC_SystemReset();

ports/atmel-samd/reset.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@
2929
#include <stdbool.h>
3030
#include <stdint.h>
3131

32+
#include "py/mpconfig.h"
33+
3234
// Copied from inc/uf2.h in https://github.com/Microsoft/uf2-samd21
3335
#define DBL_TAP_MAGIC 0xf01669ef // Randomly selected, adjusted to have first and last bit set
3436
#define DBL_TAP_MAGIC_QUICK_BOOT 0xf02669ef
3537

3638
extern uint32_t _bootloader_dbl_tap;
3739

38-
void reset_to_bootloader(void);
39-
void reset(void);
40+
void reset_to_bootloader(void) NORETURN;
41+
void reset(void) NORETURN;
4042
bool bootloader_available(void);
4143

4244
#endif // MICROPY_INCLUDED_ATMEL_SAMD_RESET_H

ports/cxd56/supervisor/port.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ safe_mode_t port_init(void) {
7171

7272
void reset_cpu(void) {
7373
boardctl(BOARDIOC_RESET, 0);
74+
for (;;) {
75+
}
7476
}
7577

7678
void reset_port(void) {
@@ -91,6 +93,9 @@ void reset_port(void) {
9193
}
9294

9395
void reset_to_bootloader(void) {
96+
boardctl(BOARDIOC_RESET, 0);
97+
for (;;) {
98+
}
9499
}
95100

96101
supervisor_allocation* port_fixed_stack(void) {

ports/esp32s2/supervisor/port.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ void reset_to_bootloader(void) {
121121
}
122122

123123
void reset_cpu(void) {
124+
esp_restart();
124125
}
125126

126127
uint32_t *port_heap_get_bottom(void) {

ports/litex/supervisor/port.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,17 @@ void reset_port(void) {
8585

8686
void reset_to_bootloader(void) {
8787
reboot_ctrl_write(0xac);
88+
for(;;) {}
8889
}
8990

9091
void reset_cpu(void) {
92+
// "You can reset Fomu by writing a special value to the CSR_REBOOT_CTRL
93+
// register at 0xe0006000L. All writes to this register must start with
94+
// 0xac, to ensure random values aren’t written. We can reboot Fomu by
95+
// simply writing this value" --
96+
// https://workshop.fomu.im/en/latest/riscv.html
97+
reboot_ctrl_write(0xac);
98+
for(;;) {}
9199
}
92100

93101
supervisor_allocation* port_fixed_stack(void) {

ports/mimxrt10xx/reset.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
#define DBL_TAP_MAGIC 0xf01669ef // Randomly selected, adjusted to have first and last bit set
3535
#define DBL_TAP_MAGIC_QUICK_BOOT 0xf02669ef
3636

37-
void reset_to_bootloader(void);
38-
void reset(void);
37+
void reset_to_bootloader(void) NORETURN;
38+
void reset(void) NORETURN;
3939
bool bootloader_available(void);
4040

4141
#endif // MICROPY_INCLUDED_MIMXRT10XX_RESET_H

ports/nrf/supervisor/port.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ void reset_cpu(void) {
234234
uint32_t ticks = nrfx_rtc_counter_get(&rtc_instance);
235235
overflow_tracker.overflowed_ticks += ticks / 32;
236236
NVIC_SystemReset();
237+
for (;;) {
238+
}
237239
}
238240

239241
// The uninitialized data section is placed directly after BSS, under the theory

ports/stm/supervisor/port.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656

5757
#include STM32_HAL_H
5858

59+
void NVIC_SystemReset(void) NORETURN;
60+
5961
#if (CPY_STM32H7) || (CPY_STM32F7)
6062

6163
// Device memories must be accessed in order.
@@ -247,7 +249,7 @@ void reset_port(void) {
247249
}
248250

249251
void reset_to_bootloader(void) {
250-
252+
NVIC_SystemReset();
251253
}
252254

253255
void reset_cpu(void) {

supervisor/port.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extern uint32_t _ebss;
4444
safe_mode_t port_init(void);
4545

4646
// Reset the microcontroller completely.
47-
void reset_cpu(void);
47+
void reset_cpu(void) NORETURN;
4848

4949
// Reset the microcontroller state.
5050
void reset_port(void);
@@ -53,7 +53,7 @@ void reset_port(void);
5353
void reset_board(void);
5454

5555
// Reset to the bootloader
56-
void reset_to_bootloader(void);
56+
void reset_to_bootloader(void) NORETURN;
5757

5858
// Get stack limit address
5959
uint32_t *port_stack_get_limit(void);

supervisor/shared/safe_mode.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#ifndef MICROPY_INCLUDED_SUPERVISOR_SAFE_MODE_H
2828
#define MICROPY_INCLUDED_SUPERVISOR_SAFE_MODE_H
2929

30+
#include "py/mpconfig.h"
31+
3032
typedef enum {
3133
NO_SAFE_MODE = 0,
3234
BROWNOUT,
@@ -48,7 +50,7 @@ typedef enum {
4850
safe_mode_t wait_for_safe_mode_reset(void);
4951

5052
void safe_mode_on_next_reset(safe_mode_t reason);
51-
void reset_into_safe_mode(safe_mode_t reason);
53+
void reset_into_safe_mode(safe_mode_t reason) NORETURN;
5254

5355
void print_safe_mode_message(safe_mode_t reason);
5456

supervisor/stub/safe_mode.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626

2727
#include "supervisor/shared/safe_mode.h"
2828

29+
#include <stdlib.h>
30+
2931
safe_mode_t wait_for_safe_mode_reset(void) {
3032
return NO_SAFE_MODE;
3133
}
3234

3335
void reset_into_safe_mode(safe_mode_t reason) {
3436
(void) reason;
35-
for (;;) {
36-
}
37+
abort();
3738
}
3839

3940
void print_safe_mode_message(safe_mode_t reason) {

0 commit comments

Comments
 (0)