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_fgetc(void * ctx)58 static int __panic_stdio_fgetc(void *ctx) {
59     char c;
60     int err;
61 
62     err = platform_pgetc(&c, false);
63     if (err < 0)
64         return err;
65     return (unsigned char)c;
66 }
67 
__panic_stdio_read(io_handle_t * io,char * s,size_t len)68 static ssize_t __panic_stdio_read(io_handle_t *io, char *s, size_t len) {
69     if (len == 0)
70         return 0;
71 
72     int err = platform_pgetc(s, false);
73     if (err < 0)
74         return err;
75 
76     return 1;
77 }
78 
__panic_stdio_write(io_handle_t * io,const char * s,size_t len)79 static ssize_t __panic_stdio_write(io_handle_t *io, const char *s, size_t len) {
80     for (size_t i = 0; i < len; i++) {
81         platform_pputc(s[i]);
82     }
83     return len;
84 }
85 
get_panic_fd(void)86 FILE *get_panic_fd(void) {
87     static const io_handle_hooks_t panic_hooks = {
88         .write = __panic_stdio_write,
89         .read = __panic_stdio_read,
90     };
91     static io_handle_t panic_io = {
92         .magic = IO_HANDLE_MAGIC,
93         .hooks = &panic_hooks
94     };
95     static FILE panic_fd = {
96         .io = &panic_io
97     };
98 
99     return &panic_fd;
100 }
101 
hexdump(const void * ptr,size_t len)102 void hexdump(const void *ptr, size_t len) {
103     addr_t address = (addr_t)ptr;
104     size_t count;
105 
106     for (count = 0 ; count < len; count += 16) {
107         union {
108             uint32_t buf[4];
109             uint8_t  cbuf[16];
110         } u;
111         size_t s = ROUNDUP(MIN(len - count, 16), 4);
112         size_t i;
113 
114         printf("0x%08lx: ", address);
115         for (i = 0; i < s / 4; i++) {
116             u.buf[i] = ((const uint32_t *)address)[i];
117             printf("%08x ", u.buf[i]);
118         }
119         for (; i < 4; i++) {
120             printf("         ");
121         }
122         printf("|");
123 
124         for (i=0; i < 16; i++) {
125             char c = u.cbuf[i];
126             if (i < s && isprint(c)) {
127                 printf("%c", c);
128             } else {
129                 printf(".");
130             }
131         }
132         printf("|\n");
133         address += 16;
134     }
135 }
136 
hexdump8_ex(const void * ptr,size_t len,uint64_t disp_addr)137 void hexdump8_ex(const void *ptr, size_t len, uint64_t disp_addr) {
138     addr_t address = (addr_t)ptr;
139     size_t count;
140     size_t i;
141     const char *addr_fmt = ((disp_addr + len) > 0xFFFFFFFF)
142                            ? "0x%016llx: "
143                            : "0x%08llx: ";
144 
145     for (count = 0 ; count < len; count += 16) {
146         printf(addr_fmt, disp_addr + count);
147 
148         for (i=0; i < MIN(len - count, 16); i++) {
149             printf("%02hhx ", *(const uint8_t *)(address + i));
150         }
151 
152         for (; i < 16; i++) {
153             printf("   ");
154         }
155 
156         printf("|");
157 
158         for (i=0; i < MIN(len - count, 16); i++) {
159             char c = ((const char *)address)[i];
160             printf("%c", isprint(c) ? c : '.');
161         }
162 
163         printf("\n");
164         address += 16;
165     }
166 }
167 
168 #endif // !DISABLE_DEBUG_OUTPUT
169