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 
28 typedef int(hexdump_print_fn_t)(const char* fmt, ...);
29 
30 #if !DISABLE_DEBUG_OUTPUT
31 
32 /* dump memory */
33 void hexdump_very_ex(const void *ptr,
34                      size_t len,
35                      uint64_t disp_addr_start,
36                      hexdump_print_fn_t* pfn);
37 void 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. */
get_panic_fd(void)45 static inline FILE *get_panic_fd(void) { return NULL; }
46 
47 /* dump memory */
hexdump_very_ex(const void * ptr,size_t len,uint64_t disp_addr_start,hexdump_print_fn_t * pfn)48 static inline void hexdump_very_ex(const void *ptr,
49                                    size_t len,
50                                    uint64_t disp_addr_start,
51                                    hexdump_print_fn_t* pfn) { }
hexdump8_very_ex(const void * ptr,size_t len,uint64_t disp_addr_start,hexdump_print_fn_t * pfn)52 static 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 
hexdump_ex(const void * ptr,size_t len,uint64_t disp_addr_start)59 static 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 
hexdump8_ex(const void * ptr,size_t len,uint64_t disp_addr_start)64 static 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 
hexdump(const void * ptr,size_t len)69 static inline void hexdump(const void *ptr, size_t len)
70 {
71     hexdump_ex(ptr, len, (uint64_t)((addr_t)ptr));
72 }
73 
hexdump8(const void * ptr,size_t len)74 static 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 */
82 void _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 
86 void _panic_no_format(const char *msg, size_t len) __NO_RETURN;
87 
panic_no_format(const char * msg)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 
94 void __stack_chk_fail(void) __NO_RETURN;
95 
96 uintptr_t choose_stack_guard(void);
97 
98 /* spin the cpu for a period of (short) time */
99 void spin(uint32_t usecs);
100 
101 /* spin the cpu for a certain number of cpu cycles */
102 void spin_cycles(uint32_t usecs);
103 
104 __END_CDECLS
105