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 #include <arch.h>
9 #include <arch/ops.h>
10 #include <ctype.h>
11 #include <debug.h>
12 #include <kernel/cmdline.h>
13 #include <kernel/thread.h>
14 #include <list.h>
15 #include <platform.h>
16 #include <platform/debug.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <vm/pmm.h>
21 #include <zircon/time.h>
22 #include <zircon/types.h>
23
24 #include <lib/console.h>
25
26
27 static int cmd_display_mem(int argc, const cmd_args *argv, uint32_t flags);
28 static int cmd_modify_mem(int argc, const cmd_args *argv, uint32_t flags);
29 static int cmd_fill_mem(int argc, const cmd_args *argv, uint32_t flags);
30 static int cmd_reset(int argc, const cmd_args *argv, uint32_t flags);
31 static int cmd_memtest(int argc, const cmd_args *argv, uint32_t flags);
32 static int cmd_copy_mem(int argc, const cmd_args *argv, uint32_t flags);
33 static int cmd_sleep(int argc, const cmd_args *argv, uint32_t flags);
34 static int cmd_crash(int argc, const cmd_args *argv, uint32_t flags);
35 static int cmd_stackstomp(int argc, const cmd_args *argv, uint32_t flags);
36 static int cmd_cmdline(int argc, const cmd_args *argv, uint32_t flags);
37
38 STATIC_COMMAND_START
39 #if LK_DEBUGLEVEL > 0
40 STATIC_COMMAND_MASKED("dw", "display memory in words", &cmd_display_mem, CMD_AVAIL_ALWAYS)
41 STATIC_COMMAND_MASKED("dh", "display memory in halfwords", &cmd_display_mem, CMD_AVAIL_ALWAYS)
42 STATIC_COMMAND_MASKED("db", "display memory in bytes", &cmd_display_mem, CMD_AVAIL_ALWAYS)
43 STATIC_COMMAND_MASKED("mw", "modify word of memory", &cmd_modify_mem, CMD_AVAIL_ALWAYS)
44 STATIC_COMMAND_MASKED("mh", "modify halfword of memory", &cmd_modify_mem, CMD_AVAIL_ALWAYS)
45 STATIC_COMMAND_MASKED("mb", "modify byte of memory", &cmd_modify_mem, CMD_AVAIL_ALWAYS)
46 STATIC_COMMAND_MASKED("fw", "fill range of memory by word", &cmd_fill_mem, CMD_AVAIL_ALWAYS)
47 STATIC_COMMAND_MASKED("fh", "fill range of memory by halfword", &cmd_fill_mem, CMD_AVAIL_ALWAYS)
48 STATIC_COMMAND_MASKED("fb", "fill range of memory by byte", &cmd_fill_mem, CMD_AVAIL_ALWAYS)
49 STATIC_COMMAND_MASKED("mc", "copy a range of memory", &cmd_copy_mem, CMD_AVAIL_ALWAYS)
50 STATIC_COMMAND("crash", "intentionally crash", &cmd_crash)
51 STATIC_COMMAND("stackstomp", "intentionally overrun the stack", &cmd_stackstomp)
52 #endif
53 #if LK_DEBUGLEVEL > 1
54 STATIC_COMMAND("mtest", "simple memory test", &cmd_memtest)
55 #endif
56 STATIC_COMMAND("cmdline", "display kernel commandline", &cmd_cmdline)
57 STATIC_COMMAND("sleep", "sleep number of seconds", &cmd_sleep)
58 STATIC_COMMAND("sleepm", "sleep number of milliseconds", &cmd_sleep)
59 STATIC_COMMAND_END(mem);
60
cmd_display_mem(int argc,const cmd_args * argv,uint32_t flags)61 static int cmd_display_mem(int argc, const cmd_args *argv, uint32_t flags)
62 {
63 /* save the last address and len so we can continue where we left off */
64 static unsigned long address;
65 static size_t len;
66
67 if (argc < 3 && len == 0) {
68 printf("not enough arguments\n");
69 printf("%s [-l] [-b] [address] [length]\n", argv[0].str);
70 return -1;
71 }
72
73 int size;
74 if (strcmp(argv[0].str, "dw") == 0) {
75 size = 4;
76 } else if (strcmp(argv[0].str, "dh") == 0) {
77 size = 2;
78 } else {
79 size = 1;
80 }
81
82 uint byte_order = BYTE_ORDER;
83 int argindex = 1;
84 bool read_address = false;
85 while (argc > argindex) {
86 if (!strcmp(argv[argindex].str, "-l")) {
87 byte_order = LITTLE_ENDIAN;
88 } else if (!strcmp(argv[argindex].str, "-b")) {
89 byte_order = BIG_ENDIAN;
90 } else if (!read_address) {
91 address = argv[argindex].u;
92 read_address = true;
93 } else {
94 len = argv[argindex].u;
95 }
96
97 argindex++;
98 }
99
100 unsigned long stop = address + len;
101 int count = 0;
102
103 if ((address & (size - 1)) != 0) {
104 printf("unaligned address, cannot display\n");
105 return -1;
106 }
107
108 /* preflight the start address to see if it's mapped */
109 if (vaddr_to_paddr((void *)address) == 0) {
110 printf("ERROR: address 0x%lx is unmapped\n", address);
111 return -1;
112 }
113
114 for ( ; address < stop; address += size) {
115 if (count == 0)
116 printf("0x%08lx: ", address);
117 switch (size) {
118 case 4: {
119 uint32_t val = (byte_order != BYTE_ORDER) ?
120 SWAP_32(*(uint32_t *)address) :
121 *(uint32_t *)address;
122 printf("%08x ", val);
123 break;
124 }
125 case 2: {
126 uint16_t val = (byte_order != BYTE_ORDER) ?
127 SWAP_16(*(uint16_t *)address) :
128 *(uint16_t *)address;
129 printf("%04hx ", val);
130 break;
131 }
132 case 1:
133 printf("%02hhx ", *(uint8_t *)address);
134 break;
135 }
136 count += size;
137 if (count == 16) {
138 printf("\n");
139 count = 0;
140 }
141 }
142
143 if (count != 0)
144 printf("\n");
145
146 return 0;
147 }
148
cmd_modify_mem(int argc,const cmd_args * argv,uint32_t flags)149 static int cmd_modify_mem(int argc, const cmd_args *argv, uint32_t flags)
150 {
151 int size;
152
153 if (argc < 3) {
154 printf("not enough arguments\n");
155 printf("%s <address> <val>\n", argv[0].str);
156 return -1;
157 }
158
159 if (strcmp(argv[0].str, "mw") == 0) {
160 size = 4;
161 } else if (strcmp(argv[0].str, "mh") == 0) {
162 size = 2;
163 } else {
164 size = 1;
165 }
166
167 unsigned long address = argv[1].u;
168 unsigned long val = argv[2].u;
169
170 if ((address & (size - 1)) != 0) {
171 printf("unaligned address, cannot modify\n");
172 return -1;
173 }
174
175 switch (size) {
176 case 4:
177 *(uint32_t *)address = (uint32_t)val;
178 break;
179 case 2:
180 *(uint16_t *)address = (uint16_t)val;
181 break;
182 case 1:
183 *(uint8_t *)address = (uint8_t)val;
184 break;
185 }
186
187 return 0;
188 }
189
cmd_fill_mem(int argc,const cmd_args * argv,uint32_t flags)190 static int cmd_fill_mem(int argc, const cmd_args *argv, uint32_t flags)
191 {
192 int size;
193
194 if (argc < 4) {
195 printf("not enough arguments\n");
196 printf("%s <address> <len> <val>\n", argv[0].str);
197 return -1;
198 }
199
200 if (strcmp(argv[0].str, "fw") == 0) {
201 size = 4;
202 } else if (strcmp(argv[0].str, "fh") == 0) {
203 size = 2;
204 } else {
205 size = 1;
206 }
207
208 unsigned long address = argv[1].u;
209 unsigned long len = argv[2].u;
210 unsigned long stop = address + len;
211 unsigned long val = argv[3].u;
212
213 if ((address & (size - 1)) != 0) {
214 printf("unaligned address, cannot modify\n");
215 return -1;
216 }
217
218 for ( ; address < stop; address += size) {
219 switch (size) {
220 case 4:
221 *(uint32_t *)address = (uint32_t)val;
222 break;
223 case 2:
224 *(uint16_t *)address = (uint16_t)val;
225 break;
226 case 1:
227 *(uint8_t *)address = (uint8_t)val;
228 break;
229 }
230 }
231
232 return 0;
233 }
234
cmd_copy_mem(int argc,const cmd_args * argv,uint32_t flags)235 static int cmd_copy_mem(int argc, const cmd_args *argv, uint32_t flags)
236 {
237 if (argc < 4) {
238 printf("not enough arguments\n");
239 printf("%s <source address> <target address> <len>\n", argv[0].str);
240 return -1;
241 }
242
243 addr_t source = argv[1].u;
244 addr_t target = argv[2].u;
245 size_t len = argv[3].u;
246
247 memcpy((void *)target, (const void *)source, len);
248
249 return 0;
250 }
251
cmd_memtest(int argc,const cmd_args * argv,uint32_t flags)252 static int cmd_memtest(int argc, const cmd_args *argv, uint32_t flags)
253 {
254 if (argc < 3) {
255 printf("not enough arguments\n");
256 printf("%s <base> <len>\n", argv[0].str);
257 return -1;
258 }
259
260 uint32_t *ptr;
261 size_t len;
262
263 ptr = (uint32_t *)argv[1].u;
264 len = (size_t)argv[2].u;
265
266 size_t i;
267 // write out
268 printf("writing first pass...");
269 for (i = 0; i < len / 4; i++) {
270 ptr[i] = static_cast<uint32_t>(i);
271 }
272 printf("done\n");
273
274 // verify
275 printf("verifying...");
276 for (i = 0; i < len / 4; i++) {
277 if (ptr[i] != i)
278 printf("error at %p\n", &ptr[i]);
279 }
280 printf("done\n");
281
282 return 0;
283 }
284
cmd_sleep(int argc,const cmd_args * argv,uint32_t flags)285 static int cmd_sleep(int argc, const cmd_args *argv, uint32_t flags)
286 {
287 zx_duration_t t = ZX_SEC(1); /* default to 1 second */
288
289 if (argc >= 2) {
290 t = ZX_MSEC(argv[1].u);
291 if (!strcmp(argv[0].str, "sleep"))
292 t = zx_duration_mul_int64(t, 1000);
293 }
294
295 thread_sleep_relative(t);
296
297 return 0;
298 }
299
crash_thread(void *)300 static int crash_thread(void *)
301 {
302 /* should crash */
303 volatile uint32_t *ptr = (volatile uint32_t *)1u;
304 *ptr = 1;
305
306 return 0;
307 }
308
cmd_crash(int argc,const cmd_args * argv,uint32_t flags)309 static int cmd_crash(int argc, const cmd_args *argv, uint32_t flags)
310 {
311 if (argc > 1) {
312 if (!strcmp(argv[1].str, "thread")) {
313 thread_t *t = thread_create("crasher", &crash_thread, NULL, DEFAULT_PRIORITY);
314 thread_resume(t);
315
316 thread_join(t, NULL, ZX_TIME_INFINITE);
317 return 0;
318 }
319 }
320
321 crash_thread(nullptr);
322
323 /* if it didn't, panic the system */
324 panic("crash");
325
326 return 0;
327 }
328
stomp_stack(size_t size)329 __attribute__((noinline)) static void stomp_stack(size_t size) {
330 // -Wvla prevents VLAs but not explicit alloca.
331 // Neither is allowed anywhere in the kernel outside this test code.
332 void* death = __builtin_alloca(size); // OK in test-only code.
333 memset(death, 0xaa, size);
334 thread_sleep_relative(ZX_USEC(1));
335 }
336
cmd_stackstomp(int argc,const cmd_args * argv,uint32_t flags)337 static int cmd_stackstomp(int argc, const cmd_args *argv, uint32_t flags)
338 {
339 for (size_t i = 0; i < DEFAULT_STACK_SIZE * 2; i++)
340 stomp_stack(i);
341
342 printf("survived.\n");
343
344 return 0;
345 }
346
347 #define DEBUG_CMDLINE_MAX 1024
cmd_cmdline(int argc,const cmd_args * argv,uint32_t flags)348 static int cmd_cmdline(int argc, const cmd_args *argv, uint32_t flags)
349 {
350 if (argc == 1) {
351 char cmdline_buf[DEBUG_CMDLINE_MAX];
352 memset(cmdline_buf, 0, DEBUG_CMDLINE_MAX);
353 const char* cmdline = cmdline_get(NULL);
354 for (size_t i = 0; i < DEBUG_CMDLINE_MAX; i++) {
355 if (cmdline[i] == '\0') {
356 if (cmdline[i+1] == '\0') {
357 break;
358 }
359 cmdline_buf[i] = ' ';
360 } else {
361 cmdline_buf[i] = cmdline[i];
362 }
363 }
364 printf("cmdline: %s\n", cmdline_buf);
365 } else {
366 const char* key = argv[1].str;
367 const char* val = cmdline_get(key);
368 if (!val) {
369 printf("cmdline: %s not found\n", key);
370 } else {
371 printf("cmdline: %s=%s\n", key, val);
372 }
373 }
374
375 return 0;
376 }
377