kfsw-platform/src/lastwords.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <string.h> | ||
| 3 | |||
| 4 | #include <zephyr/kernel.h> | ||
| 5 | #include <zephyr/sys/atomic.h> | ||
| 6 | #include <zephyr/sys/barrier.h> | ||
| 7 | #include <zephyr/sys/crc.h> | ||
| 8 | |||
| 9 | #include <kfsw/platform/lastwords.h> | ||
| 10 | |||
| 11 | #define KFSW_LASTWORDS_MAGIC 0x4B464C57UL /* "KFLW" */ | ||
| 12 | #define KFSW_LASTWORDS_VERSION 1U | ||
| 13 | |||
| 14 | struct lastwords_record { | ||
| 15 | volatile uint32_t magic; | ||
| 16 | uint8_t version; | ||
| 17 | uint8_t reason; | ||
| 18 | uint16_t reserved; | ||
| 19 | uint32_t detail; | ||
| 20 | uint32_t uptime_ms; | ||
| 21 | uint32_t boot_count; | ||
| 22 | uint32_t crc; | ||
| 23 | }; | ||
| 24 | |||
| 25 | /* Not in .bss, so start-up doesn't clear it. */ | ||
| 26 | static __noinit struct lastwords_record record; | ||
| 27 | /* Never wait here: a fault may interrupt the current owner. */ | ||
| 28 | static atomic_t record_busy; | ||
| 29 | |||
| 30 | 44 | static uint32_t record_crc(const struct lastwords_record *value) | |
| 31 | { | ||
| 32 | /* CRC over everything except the CRC field. */ | ||
| 33 | 44 | return crc32_ieee((const uint8_t *)value, offsetof(struct lastwords_record, crc)); | |
| 34 | } | ||
| 35 | |||
| 36 | 24 | void kfsw_lastwords_write(enum kfsw_lastwords_reason reason, uint32_t detail, uint32_t uptime_ms, | |
| 37 | uint32_t boot_count) | ||
| 38 | { | ||
| 39 | 24 | struct lastwords_record next = { | |
| 40 | .magic = KFSW_LASTWORDS_MAGIC, | ||
| 41 | .version = KFSW_LASTWORDS_VERSION, | ||
| 42 | 24 | .reason = (uint8_t)reason, | |
| 43 | .detail = detail, | ||
| 44 | .uptime_ms = uptime_ms, | ||
| 45 | .boot_count = boot_count, | ||
| 46 | }; | ||
| 47 | |||
| 48 | 24 | if (!atomic_cas(&record_busy, 0, 1)) { | |
| 49 | 2 | return; | |
| 50 | } | ||
| 51 | 22 | next.crc = record_crc(&next); | |
| 52 | /* Invalidate before copying; publish the magic after all other fields. */ | ||
| 53 | 22 | record.magic = 0U; | |
| 54 | 22 | barrier_dmem_fence_full(); | |
| 55 | 22 | next.magic = 0U; | |
| 56 | 22 | memcpy(&record, &next, sizeof(record)); | |
| 57 | 22 | barrier_dmem_fence_full(); | |
| 58 | 22 | record.magic = KFSW_LASTWORDS_MAGIC; | |
| 59 | 22 | atomic_clear(&record_busy); | |
| 60 | } | ||
| 61 | |||
| 62 | 62 | bool kfsw_lastwords_take(struct kfsw_lastwords *value) | |
| 63 | { | ||
| 64 | bool valid; | ||
| 65 | |||
| 66 | 62 | if (value == NULL) { | |
| 67 | ✗ | return false; | |
| 68 | } | ||
| 69 | 62 | memset(value, 0, sizeof(*value)); | |
| 70 | 62 | if (!atomic_cas(&record_busy, 0, 1)) { | |
| 71 | 2 | return false; | |
| 72 | } | ||
| 73 | |||
| 74 | 138 | valid = (record.magic == KFSW_LASTWORDS_MAGIC) && | |
| 75 | 18 | (record.version == KFSW_LASTWORDS_VERSION) && (record.crc == record_crc(&record)) && | |
| 76 | 94 | (record.reason > (uint8_t)KFSW_LASTWORDS_NONE) && | |
| 77 | 16 | (record.reason <= (uint8_t)KFSW_LASTWORDS_UNKNOWN); | |
| 78 | |||
| 79 | 60 | if (valid) { | |
| 80 | 16 | value->reason = (enum kfsw_lastwords_reason)record.reason; | |
| 81 | 16 | value->detail = record.detail; | |
| 82 | 16 | value->uptime_ms = record.uptime_ms; | |
| 83 | 16 | value->boot_count = record.boot_count; | |
| 84 | } | ||
| 85 | |||
| 86 | /* Cleared even when invalid, so it is only reported once. */ | ||
| 87 | 60 | record.magic = 0U; | |
| 88 | 60 | record.crc = 0U; | |
| 89 | 60 | atomic_clear(&record_busy); | |
| 90 | 60 | return valid; | |
| 91 | } | ||
| 92 | |||
| 93 | 12 | bool kfsw_lastwords_withdraw(enum kfsw_lastwords_reason reason) | |
| 94 | { | ||
| 95 | 12 | if (!atomic_cas(&record_busy, 0, 1)) { | |
| 96 | 2 | return false; | |
| 97 | } | ||
| 98 | 14 | if ((record.magic != KFSW_LASTWORDS_MAGIC) || (record.version != KFSW_LASTWORDS_VERSION) || | |
| 99 | 8 | (record.crc != record_crc(&record)) || (record.reason != (uint8_t)reason)) { | |
| 100 | 8 | atomic_clear(&record_busy); | |
| 101 | 8 | return false; | |
| 102 | } | ||
| 103 | /* Clearing the magic is enough to invalidate the note. */ | ||
| 104 | 2 | record.magic = 0U; | |
| 105 | 2 | atomic_clear(&record_busy); | |
| 106 | 2 | return true; | |
| 107 | } | ||
| 108 | |||
| 109 | 12 | const char *kfsw_lastwords_reason_name(enum kfsw_lastwords_reason reason) | |
| 110 | { | ||
| 111 | 12 | switch (reason) { | |
| 112 | 2 | case KFSW_LASTWORDS_COMMANDED: | |
| 113 | 2 | return "commanded"; | |
| 114 | 2 | case KFSW_LASTWORDS_BROWNOUT: | |
| 115 | 2 | return "brownout"; | |
| 116 | 2 | case KFSW_LASTWORDS_FATAL: | |
| 117 | 2 | return "fatal"; | |
| 118 | 2 | case KFSW_LASTWORDS_STARVED: | |
| 119 | 2 | return "starved"; | |
| 120 | 2 | case KFSW_LASTWORDS_UNKNOWN: | |
| 121 | 2 | return "unknown"; | |
| 122 | 2 | case KFSW_LASTWORDS_NONE: | |
| 123 | default: | ||
| 124 | 2 | return "none"; | |
| 125 | } | ||
| 126 | } | ||
| 127 |