1 /*
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 <lk/compiler.h>
11 #include <platform/debug.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 
15 #if !defined(LK_DEBUGLEVEL)
16 #define LK_DEBUGLEVEL 0
17 #endif
18 
19 /* debug levels */
20 #define CRITICAL 0
21 #define ALWAYS 0
22 #define INFO 1
23 #define SPEW 2
24 
25 __BEGIN_CDECLS
26 
27 #if !DISABLE_DEBUG_OUTPUT
28 
29 /* Obtain the panic file descriptor. */
30 FILE *get_panic_fd(void);
31 
32 /* dump memory */
33 void hexdump(const void *ptr, size_t len);
34 void hexdump8_ex(const void *ptr, size_t len, uint64_t disp_addr_start);
35 
36 #else
37 
38 /* Obtain the panic file descriptor. */
39 static inline FILE *get_panic_fd(void) { return NULL; }
40 
41 /* dump memory */
42 static inline void hexdump(const void *ptr, size_t len) { }
43 static inline void hexdump8_ex(const void *ptr, size_t len, uint64_t disp_addr_start) { }
44 
45 #endif /* DISABLE_DEBUG_OUTPUT */
46 
hexdump8(const void * ptr,size_t len)47 static inline void hexdump8(const void *ptr, size_t len) {
48     hexdump8_ex(ptr, len, (uint64_t)((addr_t)ptr));
49 }
50 
51 #define dprintf(level, x...) do { if ((level) <= LK_DEBUGLEVEL) { printf(x); } } while (0)
52 
53 /* systemwide halts */
54 void panic(const char *fmt, ...) __PRINTFLIKE(1, 2) __NO_RETURN;
55 
56 #define PANIC_UNIMPLEMENTED panic("%s:%d unimplemented\n", __PRETTY_FUNCTION__, __LINE__)
57 #define PANIC_UNIMPLEMENTED_MSG(x...) panic("%s:%d unimplemented: %s\n", __PRETTY_FUNCTION__, __LINE__, x)
58 
59 /* spin the cpu for a period of (short) time */
60 void spin(uint32_t usecs);
61 
62 /* spin the cpu for a certain number of cpu cycles */
63 void spin_cycles(uint32_t usecs);
64 
65 __END_CDECLS
66