1// Copyright 2016 The Fuchsia Authors
2// Copyright (c) 2008-2012 Travis Geiselbrecht
3//
4// Use of this source code is governed by a MIT-style
5// license that can be found in the LICENSE file or at
6// https://opensource.org/licenses/MIT
7
8#pragma once
9
10#include <stddef.h>
11#include <stdio.h>
12#include <string.h>
13#include <zircon/compiler.h>
14#include <platform/debug.h>
15
16#if !defined(LK_DEBUGLEVEL)
17#define LK_DEBUGLEVEL 0
18#endif
19
20/* debug levels */
21#define CRITICAL 0
22#define ALWAYS 0
23#define INFO 1
24#define SPEW 2
25
26__BEGIN_CDECLS
27
28typedef int(hexdump_print_fn_t)(const char* fmt, ...);
29
30#if !DISABLE_DEBUG_OUTPUT
31
32/* dump memory */
33void hexdump_very_ex(const void *ptr,
34                     size_t len,
35                     uint64_t disp_addr_start,
36                     hexdump_print_fn_t* pfn);
37void hexdump8_very_ex(const void *ptr,
38                      size_t len,
39                      uint64_t disp_addr_start,
40                      hexdump_print_fn_t* pfn);
41
42#else
43
44/* Obtain the panic file descriptor. */
45static inline FILE *get_panic_fd(void) { return NULL; }
46
47/* dump memory */
48static inline void hexdump_very_ex(const void *ptr,
49                                   size_t len,
50                                   uint64_t disp_addr_start,
51                                   hexdump_print_fn_t* pfn) { }
52static inline void hexdump8_very_ex(const void *ptr,
53                                    size_t len,
54                                    uint64_t disp_addr_start,
55                                    hexdump_print_fn_t* pfn) { }
56
57#endif /* DISABLE_DEBUG_OUTPUT */
58
59static inline void hexdump_ex(const void *ptr, size_t len, uint64_t disp_addr_start)
60{
61    hexdump_very_ex(ptr, len, disp_addr_start, _printf);
62}
63
64static inline void hexdump8_ex(const void *ptr, size_t len, uint64_t disp_addr_start)
65{
66    hexdump8_very_ex(ptr, len, disp_addr_start, _printf);
67}
68
69static inline void hexdump(const void *ptr, size_t len)
70{
71    hexdump_ex(ptr, len, (uint64_t)((addr_t)ptr));
72}
73
74static inline void hexdump8(const void *ptr, size_t len)
75{
76    hexdump8_ex(ptr, len, (uint64_t)((addr_t)ptr));
77}
78
79#define dprintf(level, x...) do { if ((level) <= LK_DEBUGLEVEL) { printf(x); } } while (0)
80
81/* systemwide halts */
82void _panic(void *caller, void *frame, const char *fmt, ...) __PRINTFLIKE(3, 4) __NO_RETURN;
83
84#define panic(x...) _panic(__GET_CALLER(), __GET_FRAME(), x)
85
86void _panic_no_format(const char *msg, size_t len) __NO_RETURN;
87
88__NO_RETURN static inline void panic_no_format(const char* msg) {
89    _panic_no_format(msg, __builtin_strlen(msg));
90}
91
92#define PANIC_UNIMPLEMENTED panic("%s unimplemented\n", __PRETTY_FUNCTION__)
93
94void __stack_chk_fail(void) __NO_RETURN;
95
96uintptr_t choose_stack_guard(void);
97
98/* spin the cpu for a period of (short) time */
99void spin(uint32_t usecs);
100
101/* spin the cpu for a certain number of cpu cycles */
102void spin_cycles(uint32_t usecs);
103
104__END_CDECLS
105