1 /*
2  * Copyright (c) 2008-2015 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 
9 #include <ctype.h>
10 #include <lk/debug.h>
11 #include <stdlib.h>
12 #include <printf.h>
13 #include <stdio.h>
14 #include <assert.h>
15 #include <lk/list.h>
16 #include <arch/ops.h>
17 #include <platform.h>
18 #include <platform/debug.h>
19 #include <kernel/spinlock.h>
20 
spin(uint32_t usecs)21 void spin(uint32_t usecs) {
22     lk_bigtime_t start = current_time_hires();
23 
24     while ((current_time_hires() - start) < usecs)
25         ;
26 }
27 
panic(const char * fmt,...)28 void panic(const char *fmt, ...) {
29     printf("panic (caller %p): ", __GET_CALLER());
30 
31     va_list ap;
32     va_start(ap, fmt);
33     vprintf(fmt, ap);
34     va_end(ap);
35 
36     platform_halt(HALT_ACTION_HALT, HALT_REASON_SW_PANIC);
37 }
38 
assert_fail_msg(const char * file,int line,const char * expression,const char * fmt,...)39 void assert_fail_msg(const char* file, int line, const char* expression, const char* fmt, ...) {
40 
41     // Print the user message.
42     printf("ASSERT FAILED at (%s:%d): %s\n", file, line, expression);
43     va_list ap;
44     va_start(ap, fmt);
45     vprintf(fmt, ap);
46     va_end(ap);
47 
48     platform_halt(HALT_ACTION_HALT, HALT_REASON_SW_PANIC);
49 }
50 
assert_fail(const char * file,int line,const char * expression)51 void assert_fail(const char* file, int line, const char* expression) {
52     printf("ASSERT FAILED at (%s:%d): %s\n", file, line, expression);
53     platform_halt(HALT_ACTION_HALT, HALT_REASON_SW_PANIC);
54 }
55 
56 #if !DISABLE_DEBUG_OUTPUT
57 
__panic_stdio_read(io_handle_t * io,char * s,size_t len)58 static ssize_t __panic_stdio_read(io_handle_t *io, char *s, size_t len) {
59     if (len == 0)
60         return 0;
61 
62     int err = platform_pgetc(s, false);
63     if (err < 0)
64         return err;
65 
66     return 1;
67 }
68 
__panic_stdio_write(io_handle_t * io,const char * s,size_t len)69 static ssize_t __panic_stdio_write(io_handle_t *io, const char *s, size_t len) {
70     for (size_t i = 0; i < len; i++) {
71         platform_pputc(s[i]);
72     }
73     return len;
74 }
75 
get_panic_fd(void)76 FILE *get_panic_fd(void) {
77     static const io_handle_hooks_t panic_hooks = {
78         .write = __panic_stdio_write,
79         .read = __panic_stdio_read,
80     };
81     static io_handle_t panic_io = {
82         .magic = IO_HANDLE_MAGIC,
83         .hooks = &panic_hooks
84     };
85     static FILE panic_fd = {
86         .io = &panic_io
87     };
88 
89     return &panic_fd;
90 }
91 
hexdump(const void * ptr,size_t len)92 void hexdump(const void *ptr, size_t len) {
93     addr_t address = (addr_t)ptr;
94     size_t count;
95 
96     for (count = 0 ; count < len; count += 16) {
97         union {
98             uint32_t buf[4];
99             uint8_t  cbuf[16];
100         } u;
101         size_t s = ROUNDUP(MIN(len - count, 16), 4);
102         size_t i;
103 
104         printf("0x%08lx: ", address);
105         for (i = 0; i < s / 4; i++) {
106             u.buf[i] = ((const uint32_t *)address)[i];
107             printf("%08x ", u.buf[i]);
108         }
109         for (; i < 4; i++) {
110             printf("         ");
111         }
112         printf("|");
113 
114         for (i=0; i < 16; i++) {
115             char c = u.cbuf[i];
116             if (i < s && isprint(c)) {
117                 printf("%c", c);
118             } else {
119                 printf(".");
120             }
121         }
122         printf("|\n");
123         address += 16;
124     }
125 }
126 
hexdump8_ex(const void * ptr,size_t len,uint64_t disp_addr)127 void hexdump8_ex(const void *ptr, size_t len, uint64_t disp_addr) {
128     addr_t address = (addr_t)ptr;
129     size_t count;
130     size_t i;
131     const char *addr_fmt = ((disp_addr + len) > 0xFFFFFFFF)
132                            ? "0x%016llx: "
133                            : "0x%08llx: ";
134 
135     for (count = 0 ; count < len; count += 16) {
136         printf(addr_fmt, disp_addr + count);
137 
138         for (i=0; i < MIN(len - count, 16); i++) {
139             printf("%02hhx ", *(const uint8_t *)(address + i));
140         }
141 
142         for (; i < 16; i++) {
143             printf("   ");
144         }
145 
146         printf("|");
147 
148         for (i=0; i < MIN(len - count, 16); i++) {
149             char c = ((const char *)address)[i];
150             printf("%c", isprint(c) ? c : '.');
151         }
152 
153         printf("\n");
154         address += 16;
155     }
156 }
157 
158 #endif // !DISABLE_DEBUG_OUTPUT
159