source: mainline/kernel/generic/src/console/cmd.c@ ada559c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ada559c was da1bafb, checked in by Martin Decky <martin@…>, 15 years ago

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
  • Property mode set to 100644
File size: 25.8 KB
Line 
1/*
2 * Copyright (c) 2005 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericconsole
30 * @{
31 */
32
33/**
34 * @file cmd.c
35 * @brief Kernel console command wrappers.
36 *
37 * This file is meant to contain all wrapper functions for
38 * all kconsole commands. The point is in separating
39 * kconsole specific wrappers from kconsole-unaware functions
40 * from other subsystems.
41 */
42
43#include <console/cmd.h>
44#include <console/console.h>
45#include <console/kconsole.h>
46#include <print.h>
47#include <panic.h>
48#include <typedefs.h>
49#include <adt/list.h>
50#include <arch.h>
51#include <config.h>
52#include <func.h>
53#include <str.h>
54#include <macros.h>
55#include <debug.h>
56#include <cpu.h>
57#include <mm/tlb.h>
58#include <arch/mm/tlb.h>
59#include <mm/frame.h>
60#include <main/version.h>
61#include <mm/slab.h>
62#include <proc/scheduler.h>
63#include <proc/thread.h>
64#include <proc/task.h>
65#include <ipc/ipc.h>
66#include <ipc/irq.h>
67#include <ipc/event.h>
68#include <sysinfo/sysinfo.h>
69#include <symtab.h>
70#include <errno.h>
71
72#ifdef CONFIG_TEST
73#include <test.h>
74#endif
75
76/* Data and methods for 'help' command. */
77static int cmd_help(cmd_arg_t *argv);
78static cmd_info_t help_info = {
79 .name = "help",
80 .description = "List of supported commands.",
81 .func = cmd_help,
82 .argc = 0
83};
84
85static int cmd_reboot(cmd_arg_t *argv);
86static cmd_info_t reboot_info = {
87 .name = "reboot",
88 .description = "Reboot.",
89 .func = cmd_reboot,
90 .argc = 0
91};
92
93static int cmd_uptime(cmd_arg_t *argv);
94static cmd_info_t uptime_info = {
95 .name = "uptime",
96 .description = "Print uptime information.",
97 .func = cmd_uptime,
98 .argc = 0
99};
100
101static int cmd_continue(cmd_arg_t *argv);
102static cmd_info_t continue_info = {
103 .name = "continue",
104 .description = "Return console back to userspace.",
105 .func = cmd_continue,
106 .argc = 0
107};
108
109#ifdef CONFIG_TEST
110static int cmd_tests(cmd_arg_t *argv);
111static cmd_info_t tests_info = {
112 .name = "tests",
113 .description = "Print available kernel tests.",
114 .func = cmd_tests,
115 .argc = 0
116};
117
118static char test_buf[MAX_CMDLINE + 1];
119static int cmd_test(cmd_arg_t *argv);
120static cmd_arg_t test_argv[] = {
121 {
122 .type = ARG_TYPE_STRING,
123 .buffer = test_buf,
124 .len = sizeof(test_buf)
125 }
126};
127static cmd_info_t test_info = {
128 .name = "test",
129 .description = "Run kernel test.",
130 .func = cmd_test,
131 .argc = 1,
132 .argv = test_argv
133};
134
135static int cmd_bench(cmd_arg_t *argv);
136static cmd_arg_t bench_argv[] = {
137 {
138 .type = ARG_TYPE_STRING,
139 .buffer = test_buf,
140 .len = sizeof(test_buf)
141 },
142 {
143 .type = ARG_TYPE_INT,
144 }
145};
146static cmd_info_t bench_info = {
147 .name = "bench",
148 .description = "Run kernel test as benchmark.",
149 .func = cmd_bench,
150 .argc = 2,
151 .argv = bench_argv
152};
153#endif
154
155/* Data and methods for 'description' command. */
156static int cmd_desc(cmd_arg_t *argv);
157static void desc_help(void);
158static char desc_buf[MAX_CMDLINE+1];
159static cmd_arg_t desc_argv = {
160 .type = ARG_TYPE_STRING,
161 .buffer = desc_buf,
162 .len = sizeof(desc_buf)
163};
164static cmd_info_t desc_info = {
165 .name = "describe",
166 .description = "Describe specified command.",
167 .help = desc_help,
168 .func = cmd_desc,
169 .argc = 1,
170 .argv = &desc_argv
171};
172
173/* Data and methods for 'symaddr' command. */
174static int cmd_symaddr(cmd_arg_t *argv);
175static char symaddr_buf[MAX_CMDLINE+1];
176static cmd_arg_t symaddr_argv = {
177 .type = ARG_TYPE_STRING,
178 .buffer = symaddr_buf,
179 .len = sizeof(symaddr_buf)
180};
181static cmd_info_t symaddr_info = {
182 .name = "symaddr",
183 .description = "Return symbol address.",
184 .func = cmd_symaddr,
185 .argc = 1,
186 .argv = &symaddr_argv
187};
188
189static char set_buf[MAX_CMDLINE+1];
190static int cmd_set4(cmd_arg_t *argv);
191static cmd_arg_t set4_argv[] = {
192 {
193 .type = ARG_TYPE_STRING,
194 .buffer = set_buf,
195 .len = sizeof(set_buf)
196 },
197 {
198 .type = ARG_TYPE_INT
199 }
200};
201static cmd_info_t set4_info = {
202 .name = "set4",
203 .description = "set <dest_addr> <value> - 4byte version",
204 .func = cmd_set4,
205 .argc = 2,
206 .argv = set4_argv
207};
208
209/* Data and methods for 'call0' command. */
210static char call0_buf[MAX_CMDLINE + 1];
211static char carg1_buf[MAX_CMDLINE + 1];
212static char carg2_buf[MAX_CMDLINE + 1];
213static char carg3_buf[MAX_CMDLINE + 1];
214
215static int cmd_call0(cmd_arg_t *argv);
216static cmd_arg_t call0_argv = {
217 .type = ARG_TYPE_STRING,
218 .buffer = call0_buf,
219 .len = sizeof(call0_buf)
220};
221static cmd_info_t call0_info = {
222 .name = "call0",
223 .description = "call0 <function> -> call function().",
224 .func = cmd_call0,
225 .argc = 1,
226 .argv = &call0_argv
227};
228
229/* Data and methods for 'mcall0' command. */
230static int cmd_mcall0(cmd_arg_t *argv);
231static cmd_arg_t mcall0_argv = {
232 .type = ARG_TYPE_STRING,
233 .buffer = call0_buf,
234 .len = sizeof(call0_buf)
235};
236static cmd_info_t mcall0_info = {
237 .name = "mcall0",
238 .description = "mcall0 <function> -> call function() on each CPU.",
239 .func = cmd_mcall0,
240 .argc = 1,
241 .argv = &mcall0_argv
242};
243
244/* Data and methods for 'call1' command. */
245static int cmd_call1(cmd_arg_t *argv);
246static cmd_arg_t call1_argv[] = {
247 {
248 .type = ARG_TYPE_STRING,
249 .buffer = call0_buf,
250 .len = sizeof(call0_buf)
251 },
252 {
253 .type = ARG_TYPE_VAR,
254 .buffer = carg1_buf,
255 .len = sizeof(carg1_buf)
256 }
257};
258static cmd_info_t call1_info = {
259 .name = "call1",
260 .description = "call1 <function> <arg1> -> call function(arg1).",
261 .func = cmd_call1,
262 .argc = 2,
263 .argv = call1_argv
264};
265
266/* Data and methods for 'call2' command. */
267static int cmd_call2(cmd_arg_t *argv);
268static cmd_arg_t call2_argv[] = {
269 {
270 .type = ARG_TYPE_STRING,
271 .buffer = call0_buf,
272 .len = sizeof(call0_buf)
273 },
274 {
275 .type = ARG_TYPE_VAR,
276 .buffer = carg1_buf,
277 .len = sizeof(carg1_buf)
278 },
279 {
280 .type = ARG_TYPE_VAR,
281 .buffer = carg2_buf,
282 .len = sizeof(carg2_buf)
283 }
284};
285static cmd_info_t call2_info = {
286 .name = "call2",
287 .description = "call2 <function> <arg1> <arg2> -> call function(arg1,arg2).",
288 .func = cmd_call2,
289 .argc = 3,
290 .argv = call2_argv
291};
292
293/* Data and methods for 'call3' command. */
294static int cmd_call3(cmd_arg_t *argv);
295static cmd_arg_t call3_argv[] = {
296 {
297 .type = ARG_TYPE_STRING,
298 .buffer = call0_buf,
299 .len = sizeof(call0_buf)
300 },
301 {
302 .type = ARG_TYPE_VAR,
303 .buffer = carg1_buf,
304 .len = sizeof(carg1_buf)
305 },
306 {
307 .type = ARG_TYPE_VAR,
308 .buffer = carg2_buf,
309 .len = sizeof(carg2_buf)
310 },
311 {
312 .type = ARG_TYPE_VAR,
313 .buffer = carg3_buf,
314 .len = sizeof(carg3_buf)
315 }
316
317};
318static cmd_info_t call3_info = {
319 .name = "call3",
320 .description = "call3 <function> <arg1> <arg2> <arg3> -> call function(arg1,arg2,arg3).",
321 .func = cmd_call3,
322 .argc = 4,
323 .argv = call3_argv
324};
325
326/* Data and methods for 'halt' command. */
327static int cmd_halt(cmd_arg_t *argv);
328static cmd_info_t halt_info = {
329 .name = "halt",
330 .description = "Halt the kernel.",
331 .func = cmd_halt,
332 .argc = 0
333};
334
335/* Data and methods for 'physmem' command. */
336static int cmd_physmem(cmd_arg_t *argv);
337cmd_info_t physmem_info = {
338 .name = "physmem",
339 .description = "Print physical memory configuration.",
340 .help = NULL,
341 .func = cmd_physmem,
342 .argc = 0,
343 .argv = NULL
344};
345
346/* Data and methods for 'tlb' command. */
347static int cmd_tlb(cmd_arg_t *argv);
348cmd_info_t tlb_info = {
349 .name = "tlb",
350 .description = "Print TLB of current processor.",
351 .help = NULL,
352 .func = cmd_tlb,
353 .argc = 0,
354 .argv = NULL
355};
356
357static int cmd_threads(cmd_arg_t *argv);
358static cmd_info_t threads_info = {
359 .name = "threads",
360 .description = "List all threads.",
361 .func = cmd_threads,
362 .argc = 0
363};
364
365static int cmd_tasks(cmd_arg_t *argv);
366static cmd_info_t tasks_info = {
367 .name = "tasks",
368 .description = "List all tasks.",
369 .func = cmd_tasks,
370 .argc = 0
371};
372
373
374static int cmd_sched(cmd_arg_t *argv);
375static cmd_info_t sched_info = {
376 .name = "scheduler",
377 .description = "List all scheduler information.",
378 .func = cmd_sched,
379 .argc = 0
380};
381
382static int cmd_slabs(cmd_arg_t *argv);
383static cmd_info_t slabs_info = {
384 .name = "slabs",
385 .description = "List slab caches.",
386 .func = cmd_slabs,
387 .argc = 0
388};
389
390static int cmd_sysinfo(cmd_arg_t *argv);
391static cmd_info_t sysinfo_info = {
392 .name = "sysinfo",
393 .description = "Dump sysinfo.",
394 .func = cmd_sysinfo,
395 .argc = 0
396};
397
398/* Data and methods for 'zones' command */
399static int cmd_zones(cmd_arg_t *argv);
400static cmd_info_t zones_info = {
401 .name = "zones",
402 .description = "List of memory zones.",
403 .func = cmd_zones,
404 .argc = 0
405};
406
407/* Data and methods for 'ipc' command */
408static int cmd_ipc(cmd_arg_t *argv);
409static cmd_arg_t ipc_argv = {
410 .type = ARG_TYPE_INT,
411};
412static cmd_info_t ipc_info = {
413 .name = "ipc",
414 .description = "ipc <taskid> Show IPC information of given task.",
415 .func = cmd_ipc,
416 .argc = 1,
417 .argv = &ipc_argv
418};
419
420/* Data and methods for 'kill' command */
421static int cmd_kill(cmd_arg_t *argv);
422static cmd_arg_t kill_argv = {
423 .type = ARG_TYPE_INT,
424};
425static cmd_info_t kill_info = {
426 .name = "kill",
427 .description = "kill <taskid> Kill a task.",
428 .func = cmd_kill,
429 .argc = 1,
430 .argv = &kill_argv
431};
432
433/* Data and methods for 'zone' command */
434static int cmd_zone(cmd_arg_t *argv);
435static cmd_arg_t zone_argv = {
436 .type = ARG_TYPE_INT,
437};
438
439static cmd_info_t zone_info = {
440 .name = "zone",
441 .description = "Show memory zone structure.",
442 .func = cmd_zone,
443 .argc = 1,
444 .argv = &zone_argv
445};
446
447/* Data and methods for 'cpus' command. */
448static int cmd_cpus(cmd_arg_t *argv);
449cmd_info_t cpus_info = {
450 .name = "cpus",
451 .description = "List all processors.",
452 .help = NULL,
453 .func = cmd_cpus,
454 .argc = 0,
455 .argv = NULL
456};
457
458/* Data and methods for 'version' command. */
459static int cmd_version(cmd_arg_t *argv);
460cmd_info_t version_info = {
461 .name = "version",
462 .description = "Print version information.",
463 .help = NULL,
464 .func = cmd_version,
465 .argc = 0,
466 .argv = NULL
467};
468
469static cmd_info_t *basic_commands[] = {
470 &call0_info,
471 &mcall0_info,
472 &call1_info,
473 &call2_info,
474 &call3_info,
475 &continue_info,
476 &cpus_info,
477 &desc_info,
478 &reboot_info,
479 &uptime_info,
480 &halt_info,
481 &help_info,
482 &ipc_info,
483 &kill_info,
484 &set4_info,
485 &slabs_info,
486 &sysinfo_info,
487 &symaddr_info,
488 &sched_info,
489 &threads_info,
490 &tasks_info,
491 &physmem_info,
492 &tlb_info,
493 &version_info,
494 &zones_info,
495 &zone_info,
496#ifdef CONFIG_TEST
497 &tests_info,
498 &test_info,
499 &bench_info,
500#endif
501 NULL
502};
503
504
505/** Initialize command info structure.
506 *
507 * @param cmd Command info structure.
508 *
509 */
510void cmd_initialize(cmd_info_t *cmd)
511{
512 spinlock_initialize(&cmd->lock, "cmd.lock");
513 link_initialize(&cmd->link);
514}
515
516/** Initialize and register commands. */
517void cmd_init(void)
518{
519 unsigned int i;
520
521 for (i = 0; basic_commands[i]; i++) {
522 cmd_initialize(basic_commands[i]);
523 if (!cmd_register(basic_commands[i]))
524 printf("Cannot register command %s\n", basic_commands[i]->name);
525 }
526}
527
528
529/** List supported commands.
530 *
531 * @param argv Argument vector.
532 *
533 * @return 0 on failure, 1 on success.
534 */
535int cmd_help(cmd_arg_t *argv)
536{
537 spinlock_lock(&cmd_lock);
538
539 link_t *cur;
540 size_t len = 0;
541 for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
542 cmd_info_t *hlp;
543 hlp = list_get_instance(cur, cmd_info_t, link);
544
545 spinlock_lock(&hlp->lock);
546 if (str_length(hlp->name) > len)
547 len = str_length(hlp->name);
548 spinlock_unlock(&hlp->lock);
549 }
550
551 for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
552 cmd_info_t *hlp;
553 hlp = list_get_instance(cur, cmd_info_t, link);
554
555 spinlock_lock(&hlp->lock);
556 printf("%-*s %s\n", len, hlp->name, hlp->description);
557 spinlock_unlock(&hlp->lock);
558 }
559
560 spinlock_unlock(&cmd_lock);
561
562 return 1;
563}
564
565
566/** Reboot the system.
567 *
568 * @param argv Argument vector.
569 *
570 * @return 0 on failure, 1 on success.
571 */
572int cmd_reboot(cmd_arg_t *argv)
573{
574 reboot();
575
576 /* Not reached */
577 return 1;
578}
579
580
581/** Print system uptime information.
582 *
583 * @param argv Argument vector.
584 *
585 * @return 0 on failure, 1 on success.
586 */
587int cmd_uptime(cmd_arg_t *argv)
588{
589 ASSERT(uptime);
590
591 /* This doesn't have to be very accurate */
592 unative_t sec = uptime->seconds1;
593
594 printf("Up %" PRIun " days, %" PRIun " hours, %" PRIun " minutes, %" PRIun " seconds\n",
595 sec / 86400, (sec % 86400) / 3600, (sec % 3600) / 60, sec % 60);
596
597 return 1;
598}
599
600/** Describe specified command.
601 *
602 * @param argv Argument vector.
603 *
604 * @return 0 on failure, 1 on success.
605 */
606int cmd_desc(cmd_arg_t *argv)
607{
608 link_t *cur;
609
610 spinlock_lock(&cmd_lock);
611
612 for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
613 cmd_info_t *hlp;
614
615 hlp = list_get_instance(cur, cmd_info_t, link);
616 spinlock_lock(&hlp->lock);
617
618 if (str_lcmp(hlp->name, (const char *) argv->buffer, str_length(hlp->name)) == 0) {
619 printf("%s - %s\n", hlp->name, hlp->description);
620 if (hlp->help)
621 hlp->help();
622 spinlock_unlock(&hlp->lock);
623 break;
624 }
625
626 spinlock_unlock(&hlp->lock);
627 }
628
629 spinlock_unlock(&cmd_lock);
630
631 return 1;
632}
633
634/** Search symbol table */
635int cmd_symaddr(cmd_arg_t *argv)
636{
637 symtab_print_search((char *) argv->buffer);
638
639 return 1;
640}
641
642/** Call function with zero parameters */
643int cmd_call0(cmd_arg_t *argv)
644{
645 uintptr_t symaddr;
646 char *symbol;
647 unative_t (*fnc)(void);
648 fncptr_t fptr;
649 int rc;
650
651 symbol = (char *) argv->buffer;
652 rc = symtab_addr_lookup(symbol, &symaddr);
653
654 if (rc == ENOENT)
655 printf("Symbol %s not found.\n", symbol);
656 else if (rc == EOVERFLOW) {
657 symtab_print_search(symbol);
658 printf("Duplicate symbol, be more specific.\n");
659 } else if (rc == EOK) {
660 fnc = (unative_t (*)(void)) arch_construct_function(&fptr,
661 (void *) symaddr, (void *) cmd_call0);
662 printf("Calling %s() (%p)\n", symbol, symaddr);
663 printf("Result: %#" PRIxn "\n", fnc());
664 } else {
665 printf("No symbol information available.\n");
666 }
667 return 1;
668}
669
670/** Call function with zero parameters on each CPU */
671int cmd_mcall0(cmd_arg_t *argv)
672{
673 /*
674 * For each CPU, create a thread which will
675 * call the function.
676 */
677
678 size_t i;
679 for (i = 0; i < config.cpu_count; i++) {
680 if (!cpus[i].active)
681 continue;
682
683 thread_t *thread;
684 if ((thread = thread_create((void (*)(void *)) cmd_call0,
685 (void *) argv, TASK, THREAD_FLAG_WIRED, "call0", false))) {
686 irq_spinlock_lock(&thread->lock, true);
687 thread->cpu = &cpus[i];
688 irq_spinlock_unlock(&thread->lock, true);
689
690 printf("cpu%" PRIs ": ", i);
691
692 thread_ready(thread);
693 thread_join(thread);
694 thread_detach(thread);
695 } else
696 printf("Unable to create thread for cpu%" PRIs "\n", i);
697 }
698
699 return 1;
700}
701
702/** Call function with one parameter */
703int cmd_call1(cmd_arg_t *argv)
704{
705 uintptr_t symaddr;
706 char *symbol;
707 unative_t (*fnc)(unative_t, ...);
708 unative_t arg1 = argv[1].intval;
709 fncptr_t fptr;
710 int rc;
711
712 symbol = (char *) argv->buffer;
713 rc = symtab_addr_lookup(symbol, &symaddr);
714
715 if (rc == ENOENT) {
716 printf("Symbol %s not found.\n", symbol);
717 } else if (rc == EOVERFLOW) {
718 symtab_print_search(symbol);
719 printf("Duplicate symbol, be more specific.\n");
720 } else if (rc == EOK) {
721 fnc = (unative_t (*)(unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call1);
722 printf("Calling f(%#" PRIxn "): %p: %s\n", arg1, symaddr, symbol);
723 printf("Result: %#" PRIxn "\n", fnc(arg1));
724 } else {
725 printf("No symbol information available.\n");
726 }
727
728 return 1;
729}
730
731/** Call function with two parameters */
732int cmd_call2(cmd_arg_t *argv)
733{
734 uintptr_t symaddr;
735 char *symbol;
736 unative_t (*fnc)(unative_t, unative_t, ...);
737 unative_t arg1 = argv[1].intval;
738 unative_t arg2 = argv[2].intval;
739 fncptr_t fptr;
740 int rc;
741
742 symbol = (char *) argv->buffer;
743 rc = symtab_addr_lookup(symbol, &symaddr);
744
745 if (rc == ENOENT) {
746 printf("Symbol %s not found.\n", symbol);
747 } else if (rc == EOVERFLOW) {
748 symtab_print_search(symbol);
749 printf("Duplicate symbol, be more specific.\n");
750 } else if (rc == EOK) {
751 fnc = (unative_t (*)(unative_t, unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call2);
752 printf("Calling f(%#" PRIxn ", %#" PRIxn "): %p: %s\n",
753 arg1, arg2, symaddr, symbol);
754 printf("Result: %#" PRIxn "\n", fnc(arg1, arg2));
755 } else {
756 printf("No symbol information available.\n");
757 }
758 return 1;
759}
760
761/** Call function with three parameters */
762int cmd_call3(cmd_arg_t *argv)
763{
764 uintptr_t symaddr;
765 char *symbol;
766 unative_t (*fnc)(unative_t, unative_t, unative_t, ...);
767 unative_t arg1 = argv[1].intval;
768 unative_t arg2 = argv[2].intval;
769 unative_t arg3 = argv[3].intval;
770 fncptr_t fptr;
771 int rc;
772
773 symbol = (char *) argv->buffer;
774 rc = symtab_addr_lookup(symbol, &symaddr);
775
776 if (rc == ENOENT) {
777 printf("Symbol %s not found.\n", symbol);
778 } else if (rc == EOVERFLOW) {
779 symtab_print_search(symbol);
780 printf("Duplicate symbol, be more specific.\n");
781 } else if (rc == EOK) {
782 fnc = (unative_t (*)(unative_t, unative_t, unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call3);
783 printf("Calling f(%#" PRIxn ",%#" PRIxn ", %#" PRIxn "): %p: %s\n",
784 arg1, arg2, arg3, symaddr, symbol);
785 printf("Result: %#" PRIxn "\n", fnc(arg1, arg2, arg3));
786 } else {
787 printf("No symbol information available.\n");
788 }
789 return 1;
790}
791
792
793/** Print detailed description of 'describe' command. */
794void desc_help(void)
795{
796 printf("Syntax: describe command_name\n");
797}
798
799/** Halt the kernel.
800 *
801 * @param argv Argument vector (ignored).
802 *
803 * @return 0 on failure, 1 on success (never returns).
804 */
805int cmd_halt(cmd_arg_t *argv)
806{
807 halt();
808 return 1;
809}
810
811/** Command for printing TLB contents.
812 *
813 * @param argv Not used.
814 *
815 * @return Always returns 1.
816 */
817int cmd_tlb(cmd_arg_t *argv)
818{
819 tlb_print();
820 return 1;
821}
822
823/** Command for printing physical memory configuration.
824 *
825 * @param argv Not used.
826 *
827 * @return Always returns 1.
828 */
829int cmd_physmem(cmd_arg_t *argv)
830{
831 physmem_print();
832 return 1;
833}
834
835/** Write 4 byte value to address */
836int cmd_set4(cmd_arg_t *argv)
837{
838 uintptr_t addr;
839 uint32_t arg1 = argv[1].intval;
840 bool pointer = false;
841 int rc;
842
843 if (((char *) argv->buffer)[0] == '*') {
844 rc = symtab_addr_lookup((char *) argv->buffer + 1, &addr);
845 pointer = true;
846 } else if (((char *) argv->buffer)[0] >= '0' &&
847 ((char *) argv->buffer)[0] <= '9') {
848 uint64_t value;
849 rc = str_uint64((char *) argv->buffer, NULL, 0, true, &value);
850 if (rc == EOK)
851 addr = (uintptr_t) value;
852 } else
853 rc = symtab_addr_lookup((char *) argv->buffer, &addr);
854
855 if (rc == ENOENT)
856 printf("Symbol %s not found.\n", argv->buffer);
857 else if (rc == EINVAL)
858 printf("Invalid address.\n");
859 else if (rc == EOVERFLOW) {
860 symtab_print_search((char *) argv->buffer);
861 printf("Duplicate symbol (be more specific) or address overflow.\n");
862 } else if (rc == EOK) {
863 if (pointer)
864 addr = *(uintptr_t *) addr;
865 printf("Writing %#" PRIx64 " -> %p\n", arg1, addr);
866 *(uint32_t *) addr = arg1;
867 } else
868 printf("No symbol information available.\n");
869
870 return 1;
871}
872
873/** Command for listings SLAB caches
874 *
875 * @param argv Ignores
876 *
877 * @return Always 1
878 */
879int cmd_slabs(cmd_arg_t * argv)
880{
881 slab_print_list();
882 return 1;
883}
884
885/** Command for dumping sysinfo
886 *
887 * @param argv Ignores
888 *
889 * @return Always 1
890 */
891int cmd_sysinfo(cmd_arg_t * argv)
892{
893 sysinfo_dump(NULL);
894 return 1;
895}
896
897
898/** Command for listings Thread information
899 *
900 * @param argv Ignores
901 *
902 * @return Always 1
903 */
904int cmd_threads(cmd_arg_t * argv)
905{
906 thread_print_list();
907 return 1;
908}
909
910/** Command for listings Task information
911 *
912 * @param argv Ignores
913 *
914 * @return Always 1
915 */
916int cmd_tasks(cmd_arg_t * argv)
917{
918 task_print_list();
919 return 1;
920}
921
922/** Command for listings Thread information
923 *
924 * @param argv Ignores
925 *
926 * @return Always 1
927 */
928int cmd_sched(cmd_arg_t * argv)
929{
930 sched_print_list();
931 return 1;
932}
933
934/** Command for listing memory zones
935 *
936 * @param argv Ignored
937 *
938 * return Always 1
939 */
940int cmd_zones(cmd_arg_t * argv)
941{
942 zones_print_list();
943 return 1;
944}
945
946/** Command for memory zone details
947 *
948 * @param argv Integer argument from cmdline expected
949 *
950 * return Always 1
951 */
952int cmd_zone(cmd_arg_t * argv)
953{
954 zone_print_one(argv[0].intval);
955 return 1;
956}
957
958/** Command for printing task ipc details
959 *
960 * @param argv Integer argument from cmdline expected
961 *
962 * return Always 1
963 */
964int cmd_ipc(cmd_arg_t * argv)
965{
966 ipc_print_task(argv[0].intval);
967 return 1;
968}
969
970/** Command for killing a task
971 *
972 * @param argv Integer argument from cmdline expected
973 *
974 * return 0 on failure, 1 on success.
975 */
976int cmd_kill(cmd_arg_t * argv)
977{
978 if (task_kill(argv[0].intval) != EOK)
979 return 0;
980
981 return 1;
982}
983
984/** Command for listing processors.
985 *
986 * @param argv Ignored.
987 *
988 * return Always 1.
989 */
990int cmd_cpus(cmd_arg_t *argv)
991{
992 cpu_list();
993 return 1;
994}
995
996/** Command for printing kernel version.
997 *
998 * @param argv Ignored.
999 *
1000 * return Always 1.
1001 */
1002int cmd_version(cmd_arg_t *argv)
1003{
1004 version_print();
1005 return 1;
1006}
1007
1008/** Command for returning console back to userspace.
1009 *
1010 * @param argv Ignored.
1011 *
1012 * return Always 1.
1013 */
1014int cmd_continue(cmd_arg_t *argv)
1015{
1016 printf("The kernel will now relinquish the console.\n");
1017 release_console();
1018
1019 event_notify_0(EVENT_KCONSOLE);
1020 indev_pop_character(stdin);
1021
1022 return 1;
1023}
1024
1025#ifdef CONFIG_TEST
1026/** Command for printing kernel tests list.
1027 *
1028 * @param argv Ignored.
1029 *
1030 * return Always 1.
1031 */
1032int cmd_tests(cmd_arg_t *argv)
1033{
1034 size_t len = 0;
1035 test_t *test;
1036 for (test = tests; test->name != NULL; test++) {
1037 if (str_length(test->name) > len)
1038 len = str_length(test->name);
1039 }
1040
1041 for (test = tests; test->name != NULL; test++)
1042 printf("%-*s %s%s\n", len, test->name, test->desc, (test->safe ? "" : " (unsafe)"));
1043
1044 printf("%-*s Run all safe tests\n", len, "*");
1045 return 1;
1046}
1047
1048static bool run_test(const test_t *test)
1049{
1050 printf("%s (%s)\n", test->name, test->desc);
1051
1052 /* Update and read thread accounting
1053 for benchmarking */
1054 irq_spinlock_lock(&TASK->lock, true);
1055 uint64_t ucycles0, kcycles0;
1056 task_get_accounting(TASK, &ucycles0, &kcycles0);
1057 irq_spinlock_unlock(&TASK->lock, true);
1058
1059 /* Execute the test */
1060 test_quiet = false;
1061 const char *ret = test->entry();
1062
1063 /* Update and read thread accounting */
1064 uint64_t ucycles1, kcycles1;
1065 irq_spinlock_lock(&TASK->lock, true);
1066 task_get_accounting(TASK, &ucycles1, &kcycles1);
1067 irq_spinlock_unlock(&TASK->lock, true);
1068
1069 uint64_t ucycles, kcycles;
1070 char usuffix, ksuffix;
1071 order_suffix(ucycles1 - ucycles0, &ucycles, &usuffix);
1072 order_suffix(kcycles1 - kcycles0, &kcycles, &ksuffix);
1073
1074 printf("Time: %" PRIu64 "%c user cycles, %" PRIu64 "%c kernel cycles\n",
1075 ucycles, usuffix, kcycles, ksuffix);
1076
1077 if (ret == NULL) {
1078 printf("Test passed\n");
1079 return true;
1080 }
1081
1082 printf("%s\n", ret);
1083 return false;
1084}
1085
1086static bool run_bench(const test_t *test, const uint32_t cnt)
1087{
1088 uint32_t i;
1089 bool ret = true;
1090 uint64_t ucycles, kcycles;
1091 char usuffix, ksuffix;
1092
1093 if (cnt < 1)
1094 return true;
1095
1096 uint64_t *data = (uint64_t *) malloc(sizeof(uint64_t) * cnt, 0);
1097 if (data == NULL) {
1098 printf("Error allocating memory for statistics\n");
1099 return false;
1100 }
1101
1102 for (i = 0; i < cnt; i++) {
1103 printf("%s (%u/%u) ... ", test->name, i + 1, cnt);
1104
1105 /* Update and read thread accounting
1106 for benchmarking */
1107 irq_spinlock_lock(&TASK->lock, true);
1108 uint64_t ucycles0, kcycles0;
1109 task_get_accounting(TASK, &ucycles0, &kcycles0);
1110 irq_spinlock_unlock(&TASK->lock, true);
1111
1112 /* Execute the test */
1113 test_quiet = true;
1114 const char *ret = test->entry();
1115
1116 /* Update and read thread accounting */
1117 irq_spinlock_lock(&TASK->lock, true);
1118 uint64_t ucycles1, kcycles1;
1119 task_get_accounting(TASK, &ucycles1, &kcycles1);
1120 irq_spinlock_unlock(&TASK->lock, true);
1121
1122 if (ret != NULL) {
1123 printf("%s\n", ret);
1124 ret = false;
1125 break;
1126 }
1127
1128 data[i] = ucycles1 - ucycles0 + kcycles1 - kcycles0;
1129 order_suffix(ucycles1 - ucycles0, &ucycles, &usuffix);
1130 order_suffix(kcycles1 - kcycles0, &kcycles, &ksuffix);
1131 printf("OK (%" PRIu64 "%c user cycles, %" PRIu64 "%c kernel cycles)\n",
1132 ucycles, usuffix, kcycles, ksuffix);
1133 }
1134
1135 if (ret) {
1136 printf("\n");
1137
1138 uint64_t sum = 0;
1139
1140 for (i = 0; i < cnt; i++) {
1141 sum += data[i];
1142 }
1143
1144 order_suffix(sum / (uint64_t) cnt, &ucycles, &usuffix);
1145 printf("Average\t\t%" PRIu64 "%c\n", ucycles, usuffix);
1146 }
1147
1148 free(data);
1149
1150 return ret;
1151}
1152
1153/** Command for returning kernel tests
1154 *
1155 * @param argv Argument vector.
1156 *
1157 * return Always 1.
1158 */
1159int cmd_test(cmd_arg_t *argv)
1160{
1161 test_t *test;
1162
1163 if (str_cmp((char *) argv->buffer, "*") == 0) {
1164 for (test = tests; test->name != NULL; test++) {
1165 if (test->safe) {
1166 printf("\n");
1167 if (!run_test(test))
1168 break;
1169 }
1170 }
1171 } else {
1172 bool fnd = false;
1173
1174 for (test = tests; test->name != NULL; test++) {
1175 if (str_cmp(test->name, (char *) argv->buffer) == 0) {
1176 fnd = true;
1177 run_test(test);
1178 break;
1179 }
1180 }
1181
1182 if (!fnd)
1183 printf("Unknown test\n");
1184 }
1185
1186 return 1;
1187}
1188
1189/** Command for returning kernel tests as benchmarks
1190 *
1191 * @param argv Argument vector.
1192 *
1193 * return Always 1.
1194 */
1195int cmd_bench(cmd_arg_t *argv)
1196{
1197 test_t *test;
1198 uint32_t cnt = argv[1].intval;
1199
1200 if (str_cmp((char *) argv->buffer, "*") == 0) {
1201 for (test = tests; test->name != NULL; test++) {
1202 if (test->safe) {
1203 if (!run_bench(test, cnt))
1204 break;
1205 }
1206 }
1207 } else {
1208 bool fnd = false;
1209
1210 for (test = tests; test->name != NULL; test++) {
1211 if (str_cmp(test->name, (char *) argv->buffer) == 0) {
1212 fnd = true;
1213
1214 if (test->safe)
1215 run_bench(test, cnt);
1216 else
1217 printf("Unsafe test\n");
1218
1219 break;
1220 }
1221 }
1222
1223 if (!fnd)
1224 printf("Unknown test\n");
1225 }
1226
1227 return 1;
1228}
1229
1230#endif
1231
1232/** @}
1233 */
Note: See TracBrowser for help on using the repository browser.