1#include <console/crash.h>
2#include <console/panic.h>
3#include <console/printf.h>
4#include <console/report.h>
5#include <dbg.h>
6#include <stdarg.h>
7#include <string.h>
8#include <sync/raw_spinlock.h>
9
10static char msg[REPORT_LINE_MAX];
11static struct raw_spinlock panic_msg_lock = RAW_SPINLOCK_INIT;
12
13static cc_noreturn void panic_crash(const struct crash_code *code,
14 const char *file, int line,
15 const char *func) {
16 crash_full(ctx: &(struct crash_context){
17 .payload = CRASH_CODE_TO_PAYLOAD(code),
18 .source = CRASH_SOURCE_PANIC,
19 .formats = CRASH_FMT_DEFAULT,
20 .file = file,
21 .line = line,
22 .func = func,
23 .msg = msg,
24 .regs = NULL,
25 });
26}
27
28static cc_noreturn void panic_v(const struct crash_code *code, const char *file,
29 int line, const char *func, const char *fmt,
30 va_list args) {
31 raw_spin_lock(lock: &panic_msg_lock);
32 vsnprintf(msg, (int) sizeof(msg), fmt, args);
33 panic_crash(code, file, line, func);
34}
35
36cc_noreturn void panic_impl_msg(const char *file, int line, const char *func,
37 const char *fmt, ...) {
38 va_list args;
39 va_start(args, fmt);
40 panic_v(NULL, file, line, func, fmt, args);
41 va_end(args);
42}
43
44cc_noreturn void panic_impl_code(const char *file, int line, const char *func,
45 const struct crash_code *code, const char *fmt,
46 ...) {
47 va_list args;
48 va_start(args, fmt);
49 panic_v(code, file, line, func, fmt, args);
50 va_end(args);
51}
52
53cc_noreturn void panic_impl_code_only(const char *file, int line,
54 const char *func,
55 const struct crash_code *code) {
56 raw_spin_lock(lock: &panic_msg_lock);
57 snprintf(msg, (int) sizeof(msg), "No message supplied");
58 panic_crash(code, file, line, func);
59}
60