rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2008-2015 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <ctype.h> |
| 25 | #include <debug.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <printf.h> |
| 28 | #include <stdio.h> |
| 29 | #include <list.h> |
| 30 | #include <string.h> |
| 31 | #include <arch/ops.h> |
| 32 | #include <platform.h> |
| 33 | #include <platform/debug.h> |
| 34 | #include <kernel/thread.h> |
| 35 | |
| 36 | #if !DISABLE_DEBUG_OUTPUT |
| 37 | static int _dvprintf(const char *fmt, va_list ap); |
| 38 | #else |
| 39 | static inline int _dvprintf(const char *fmt, va_list ap) { return 0; } |
| 40 | #endif |
| 41 | |
| 42 | #if WITH_LIB_SM |
| 43 | #define PRINT_LOCK_FLAGS SPIN_LOCK_FLAG_IRQ_FIQ |
| 44 | #else |
| 45 | #define PRINT_LOCK_FLAGS SPIN_LOCK_FLAG_INTERRUPTS |
| 46 | #endif |
| 47 | |
| 48 | static spin_lock_t print_spin_lock = 0; |
| 49 | static struct list_node print_callbacks = LIST_INITIAL_VALUE(print_callbacks); |
| 50 | |
| 51 | /* print lock must be held when invoking out, outs, outc */ |
| 52 | static void out_count(const char *str, size_t len) |
| 53 | { |
| 54 | print_callback_t *cb; |
| 55 | size_t i; |
| 56 | |
| 57 | /* print to any registered loggers */ |
| 58 | if (!list_is_empty(&print_callbacks)) { |
| 59 | spin_lock_saved_state_t state; |
| 60 | spin_lock_save(&print_spin_lock, &state, PRINT_LOCK_FLAGS); |
| 61 | |
| 62 | list_for_every_entry(&print_callbacks, cb, print_callback_t, entry) { |
| 63 | if (cb->print) |
| 64 | cb->print(cb, str, len); |
| 65 | } |
| 66 | |
| 67 | spin_unlock_restore(&print_spin_lock, state, PRINT_LOCK_FLAGS); |
| 68 | } |
| 69 | |
| 70 | /* write out the serial port */ |
| 71 | for (i = 0; i < len; i++) { |
| 72 | platform_dputc(str[i]); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void register_print_callback(print_callback_t *cb) |
| 77 | { |
| 78 | spin_lock_saved_state_t state; |
| 79 | spin_lock_save(&print_spin_lock, &state, PRINT_LOCK_FLAGS); |
| 80 | |
| 81 | list_add_head(&print_callbacks, &cb->entry); |
| 82 | |
| 83 | spin_unlock_restore(&print_spin_lock, state, PRINT_LOCK_FLAGS); |
| 84 | } |
| 85 | |
| 86 | void unregister_print_callback(print_callback_t *cb) |
| 87 | { |
| 88 | spin_lock_saved_state_t state; |
| 89 | spin_lock_save(&print_spin_lock, &state, PRINT_LOCK_FLAGS); |
| 90 | |
| 91 | list_delete(&cb->entry); |
| 92 | |
| 93 | spin_unlock_restore(&print_spin_lock, state, PRINT_LOCK_FLAGS); |
| 94 | } |
| 95 | |
| 96 | void spin(uint32_t usecs) |
| 97 | { |
| 98 | lk_bigtime_t start = current_time_hires(); |
| 99 | |
| 100 | while ((current_time_hires() - start) < usecs) |
| 101 | ; |
| 102 | } |
| 103 | |
| 104 | void _panic(void *caller, const char *fmt, ...) |
| 105 | { |
| 106 | dprintf(ALWAYS, "panic (caller %p): ", caller); |
| 107 | |
| 108 | va_list ap; |
| 109 | va_start(ap, fmt); |
| 110 | _dvprintf(fmt, ap); |
| 111 | va_end(ap); |
| 112 | |
| 113 | platform_halt(HALT_ACTION_HALT, HALT_REASON_SW_PANIC); |
| 114 | } |
| 115 | |
| 116 | static int __debug_stdio_fputc(void *ctx, int c) |
| 117 | { |
| 118 | char x = c; |
| 119 | out_count(&x, 1); |
| 120 | return c; |
| 121 | } |
| 122 | |
| 123 | static int __debug_stdio_fputs(void *ctx, const char *s) |
| 124 | { |
| 125 | out_count(s, strlen(s)); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int __debug_stdio_fgetc(void *ctx) |
| 130 | { |
| 131 | char c; |
| 132 | int err; |
| 133 | |
| 134 | err = platform_dgetc(&c, true); |
| 135 | if (err < 0) |
| 136 | return err; |
| 137 | return (unsigned char)c; |
| 138 | } |
| 139 | |
| 140 | static int __panic_stdio_fgetc(void *ctx) |
| 141 | { |
| 142 | char c; |
| 143 | int err; |
| 144 | |
| 145 | err = platform_pgetc(&c, false); |
| 146 | if (err < 0) |
| 147 | return err; |
| 148 | return (unsigned char)c; |
| 149 | } |
| 150 | |
| 151 | static int __debug_stdio_vfprintf(void *ctx, const char *fmt, va_list ap) |
| 152 | { |
| 153 | return _dvprintf(fmt, ap); |
| 154 | } |
| 155 | |
| 156 | #define DEFINE_STDIO_DESC(id) \ |
| 157 | [(id)] = { \ |
| 158 | .ctx = &__stdio_FILEs[(id)], \ |
| 159 | .fputc = __debug_stdio_fputc, \ |
| 160 | .fputs = __debug_stdio_fputs, \ |
| 161 | .fgetc = __debug_stdio_fgetc, \ |
| 162 | .vfprintf = __debug_stdio_vfprintf, \ |
| 163 | } |
| 164 | |
| 165 | FILE __stdio_FILEs[3] = { |
| 166 | DEFINE_STDIO_DESC(0), /* stdin */ |
| 167 | DEFINE_STDIO_DESC(1), /* stdout */ |
| 168 | DEFINE_STDIO_DESC(2), /* stderr */ |
| 169 | }; |
| 170 | #undef DEFINE_STDIO_DESC |
| 171 | |
| 172 | FILE get_panic_fd(void) |
| 173 | { |
| 174 | FILE panic_fd; |
| 175 | panic_fd.fgetc = __panic_stdio_fgetc; |
| 176 | |
| 177 | panic_fd.fputc = __debug_stdio_fputc; |
| 178 | panic_fd.fputs = __debug_stdio_fputs; |
| 179 | panic_fd.vfprintf = __debug_stdio_vfprintf; |
| 180 | return panic_fd; |
| 181 | } |
| 182 | |
| 183 | #if !DISABLE_DEBUG_OUTPUT |
| 184 | |
| 185 | static int _dprintf_output_func(const char *str, size_t len, void *state) |
| 186 | { |
| 187 | out_count(str, len); |
| 188 | return len; |
| 189 | } |
| 190 | |
| 191 | int _dvprintf(const char *fmt, va_list ap) |
| 192 | { |
| 193 | return _printf_engine(&_dprintf_output_func, NULL, fmt, ap); |
| 194 | } |
| 195 | |
| 196 | int _dprintf(const char *fmt, ...) |
| 197 | { |
| 198 | int err; |
| 199 | va_list ap; |
| 200 | |
| 201 | va_start(ap, fmt); |
| 202 | err = _printf_engine(&_dprintf_output_func, NULL, fmt, ap); |
| 203 | va_end(ap); |
| 204 | |
| 205 | return err; |
| 206 | } |
| 207 | |
| 208 | void hexdump(const void *ptr, size_t len) |
| 209 | { |
| 210 | addr_t address = (addr_t)ptr; |
| 211 | size_t count; |
| 212 | |
| 213 | for (count = 0 ; count < len; count += 16) { |
| 214 | union { |
| 215 | uint32_t buf[4]; |
| 216 | uint8_t cbuf[16]; |
| 217 | } u; |
| 218 | size_t s = ROUNDUP(MIN(len - count, 16), 4); |
| 219 | size_t i; |
| 220 | |
| 221 | printf("0x%08lx: ", address); |
| 222 | for (i = 0; i < s / 4; i++) { |
| 223 | u.buf[i] = ((const uint32_t *)address)[i]; |
| 224 | printf("%08x ", u.buf[i]); |
| 225 | } |
| 226 | for (; i < 4; i++) { |
| 227 | printf(" "); |
| 228 | } |
| 229 | printf("|"); |
| 230 | |
| 231 | for (i=0; i < 16; i++) { |
| 232 | char c = u.cbuf[i]; |
| 233 | if (i < s && isprint(c)) { |
| 234 | printf("%c", c); |
| 235 | } else { |
| 236 | printf("."); |
| 237 | } |
| 238 | } |
| 239 | printf("|\n"); |
| 240 | address += 16; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void hexdump8_ex(const void *ptr, size_t len, uint64_t disp_addr) |
| 245 | { |
| 246 | addr_t address = (addr_t)ptr; |
| 247 | size_t count; |
| 248 | size_t i; |
| 249 | const char* addr_fmt = ((disp_addr + len) > 0xFFFFFFFF) |
| 250 | ? "0x%016llx: " |
| 251 | : "0x%08llx: "; |
| 252 | |
| 253 | for (count = 0 ; count < len; count += 16) { |
| 254 | printf(addr_fmt, disp_addr + count); |
| 255 | |
| 256 | for (i=0; i < MIN(len - count, 16); i++) { |
| 257 | printf("%02hhx ", *(const uint8_t *)(address + i)); |
| 258 | } |
| 259 | |
| 260 | for (; i < 16; i++) { |
| 261 | printf(" "); |
| 262 | } |
| 263 | |
| 264 | printf("|"); |
| 265 | |
| 266 | for (i=0; i < MIN(len - count, 16); i++) { |
| 267 | char c = ((const char *)address)[i]; |
| 268 | printf("%c", isprint(c) ? c : '.'); |
| 269 | } |
| 270 | |
| 271 | printf("\n"); |
| 272 | address += 16; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | #endif // !DISABLE_DEBUG_OUTPUT |
| 277 | |
| 278 | // vim: set noexpandtab: |