Subversion Repositories HelenOS

Rev

Rev 4345 | Rev 4347 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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 <arch/types.h>
  49. #include <adt/list.h>
  50. #include <arch.h>
  51. #include <config.h>
  52. #include <func.h>
  53. #include <string.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 <symtab.h>
  68. #include <errno.h>
  69.  
  70. #ifdef CONFIG_TEST
  71. #include <test.h>
  72. #endif
  73.  
  74. /* Data and methods for 'help' command. */
  75. static int cmd_help(cmd_arg_t *argv);
  76. static cmd_info_t help_info = {
  77.     .name = "help",
  78.     .description = "List of supported commands.",
  79.     .func = cmd_help,
  80.     .argc = 0
  81. };
  82.  
  83. static int cmd_reboot(cmd_arg_t *argv);
  84. static cmd_info_t reboot_info = {
  85.     .name = "reboot",
  86.     .description = "Reboot.",
  87.     .func = cmd_reboot,
  88.     .argc = 0
  89. };
  90.  
  91. static int cmd_uptime(cmd_arg_t *argv);
  92. static cmd_info_t uptime_info = {
  93.     .name = "uptime",
  94.     .description = "Print uptime information.",
  95.     .func = cmd_uptime,
  96.     .argc = 0
  97. };
  98.  
  99. static int cmd_continue(cmd_arg_t *argv);
  100. static cmd_info_t continue_info = {
  101.     .name = "continue",
  102.     .description = "Return console back to userspace.",
  103.     .func = cmd_continue,
  104.     .argc = 0
  105. };
  106.  
  107. #ifdef CONFIG_TEST
  108. static int cmd_tests(cmd_arg_t *argv);
  109. static cmd_info_t tests_info = {
  110.     .name = "tests",
  111.     .description = "Print available kernel tests.",
  112.     .func = cmd_tests,
  113.     .argc = 0
  114. };
  115.  
  116. static char test_buf[MAX_CMDLINE + 1];
  117. static int cmd_test(cmd_arg_t *argv);
  118. static cmd_arg_t test_argv[] = {
  119.     {
  120.         .type = ARG_TYPE_STRING,
  121.         .buffer = test_buf,
  122.         .len = sizeof(test_buf)
  123.     }
  124. };
  125. static cmd_info_t test_info = {
  126.     .name = "test",
  127.     .description = "Run kernel test.",
  128.     .func = cmd_test,
  129.     .argc = 1,
  130.     .argv = test_argv
  131. };
  132.  
  133. static int cmd_bench(cmd_arg_t *argv);
  134. static cmd_arg_t bench_argv[] = {
  135.     {
  136.         .type = ARG_TYPE_STRING,
  137.         .buffer = test_buf,
  138.         .len = sizeof(test_buf)
  139.     },
  140.     {
  141.         .type = ARG_TYPE_INT,
  142.     }
  143. };
  144. static cmd_info_t bench_info = {
  145.     .name = "bench",
  146.     .description = "Run kernel test as benchmark.",
  147.     .func = cmd_bench,
  148.     .argc = 2,
  149.     .argv = bench_argv
  150. };
  151. #endif
  152.  
  153. /* Data and methods for 'description' command. */
  154. static int cmd_desc(cmd_arg_t *argv);
  155. static void desc_help(void);
  156. static char desc_buf[MAX_CMDLINE+1];
  157. static cmd_arg_t desc_argv = {
  158.     .type = ARG_TYPE_STRING,
  159.     .buffer = desc_buf,
  160.     .len = sizeof(desc_buf)
  161. };
  162. static cmd_info_t desc_info = {
  163.     .name = "describe",
  164.     .description = "Describe specified command.",
  165.     .help = desc_help,
  166.     .func = cmd_desc,
  167.     .argc = 1,
  168.     .argv = &desc_argv
  169. };
  170.  
  171. /* Data and methods for 'symaddr' command. */
  172. static int cmd_symaddr(cmd_arg_t *argv);
  173. static char symaddr_buf[MAX_CMDLINE+1];
  174. static cmd_arg_t symaddr_argv = {
  175.     .type = ARG_TYPE_STRING,
  176.     .buffer = symaddr_buf,
  177.     .len = sizeof(symaddr_buf)
  178. };
  179. static cmd_info_t symaddr_info = {
  180.     .name = "symaddr",
  181.     .description = "Return symbol address.",
  182.     .func = cmd_symaddr,
  183.     .argc = 1,
  184.     .argv = &symaddr_argv
  185. };
  186.  
  187. static char set_buf[MAX_CMDLINE+1];
  188. static int cmd_set4(cmd_arg_t *argv);
  189. static cmd_arg_t set4_argv[] = {
  190.     {
  191.         .type = ARG_TYPE_STRING,
  192.         .buffer = set_buf,
  193.         .len = sizeof(set_buf)
  194.     },
  195.     {
  196.         .type = ARG_TYPE_INT
  197.     }
  198. };
  199. static cmd_info_t set4_info = {
  200.     .name = "set4",
  201.     .description = "set <dest_addr> <value> - 4byte version",
  202.     .func = cmd_set4,
  203.     .argc = 2,
  204.     .argv = set4_argv
  205. };
  206.  
  207. /* Data and methods for 'call0' command. */
  208. static char call0_buf[MAX_CMDLINE + 1];
  209. static char carg1_buf[MAX_CMDLINE + 1];
  210. static char carg2_buf[MAX_CMDLINE + 1];
  211. static char carg3_buf[MAX_CMDLINE + 1];
  212.  
  213. static int cmd_call0(cmd_arg_t *argv);
  214. static cmd_arg_t call0_argv = {
  215.     .type = ARG_TYPE_STRING,
  216.     .buffer = call0_buf,
  217.     .len = sizeof(call0_buf)
  218. };
  219. static cmd_info_t call0_info = {
  220.     .name = "call0",
  221.     .description = "call0 <function> -> call function().",
  222.     .func = cmd_call0,
  223.     .argc = 1,
  224.     .argv = &call0_argv
  225. };
  226.  
  227. /* Data and methods for 'mcall0' command. */
  228. static int cmd_mcall0(cmd_arg_t *argv);
  229. static cmd_arg_t mcall0_argv = {
  230.     .type = ARG_TYPE_STRING,
  231.     .buffer = call0_buf,
  232.     .len = sizeof(call0_buf)
  233. };
  234. static cmd_info_t mcall0_info = {
  235.     .name = "mcall0",
  236.     .description = "mcall0 <function> -> call function() on each CPU.",
  237.     .func = cmd_mcall0,
  238.     .argc = 1,
  239.     .argv = &mcall0_argv
  240. };
  241.  
  242. /* Data and methods for 'call1' command. */
  243. static int cmd_call1(cmd_arg_t *argv);
  244. static cmd_arg_t call1_argv[] = {
  245.     {
  246.         .type = ARG_TYPE_STRING,
  247.         .buffer = call0_buf,
  248.         .len = sizeof(call0_buf)
  249.     },
  250.     {
  251.         .type = ARG_TYPE_VAR,
  252.         .buffer = carg1_buf,
  253.         .len = sizeof(carg1_buf)
  254.     }
  255. };
  256. static cmd_info_t call1_info = {
  257.     .name = "call1",
  258.     .description = "call1 <function> <arg1> -> call function(arg1).",
  259.     .func = cmd_call1,
  260.     .argc = 2,
  261.     .argv = call1_argv
  262. };
  263.  
  264. /* Data and methods for 'call2' command. */
  265. static int cmd_call2(cmd_arg_t *argv);
  266. static cmd_arg_t call2_argv[] = {
  267.     {
  268.         .type = ARG_TYPE_STRING,
  269.         .buffer = call0_buf,
  270.         .len = sizeof(call0_buf)
  271.     },
  272.     {
  273.         .type = ARG_TYPE_VAR,
  274.         .buffer = carg1_buf,
  275.         .len = sizeof(carg1_buf)
  276.     },
  277.     {
  278.         .type = ARG_TYPE_VAR,
  279.         .buffer = carg2_buf,
  280.         .len = sizeof(carg2_buf)
  281.     }
  282. };
  283. static cmd_info_t call2_info = {
  284.     .name = "call2",
  285.     .description = "call2 <function> <arg1> <arg2> -> call function(arg1,arg2).",
  286.     .func = cmd_call2,
  287.     .argc = 3,
  288.     .argv = call2_argv
  289. };
  290.  
  291. /* Data and methods for 'call3' command. */
  292. static int cmd_call3(cmd_arg_t *argv);
  293. static cmd_arg_t call3_argv[] = {
  294.     {
  295.         .type = ARG_TYPE_STRING,
  296.         .buffer = call0_buf,
  297.         .len = sizeof(call0_buf)
  298.     },
  299.     {
  300.         .type = ARG_TYPE_VAR,
  301.         .buffer = carg1_buf,
  302.         .len = sizeof(carg1_buf)
  303.     },
  304.     {
  305.         .type = ARG_TYPE_VAR,
  306.         .buffer = carg2_buf,
  307.         .len = sizeof(carg2_buf)
  308.     },
  309.     {
  310.         .type = ARG_TYPE_VAR,
  311.         .buffer = carg3_buf,
  312.         .len = sizeof(carg3_buf)
  313.     }
  314.  
  315. };
  316. static cmd_info_t call3_info = {
  317.     .name = "call3",
  318.     .description = "call3 <function> <arg1> <arg2> <arg3> -> call function(arg1,arg2,arg3).",
  319.     .func = cmd_call3,
  320.     .argc = 4,
  321.     .argv = call3_argv
  322. };
  323.  
  324. /* Data and methods for 'halt' command. */
  325. static int cmd_halt(cmd_arg_t *argv);
  326. static cmd_info_t halt_info = {
  327.     .name = "halt",
  328.     .description = "Halt the kernel.",
  329.     .func = cmd_halt,
  330.     .argc = 0
  331. };
  332.  
  333. /* Data and methods for 'physmem' command. */
  334. static int cmd_physmem(cmd_arg_t *argv);
  335. cmd_info_t physmem_info = {
  336.     .name = "physmem",
  337.     .description = "Print physical memory configuration.",
  338.     .help = NULL,
  339.     .func = cmd_physmem,
  340.     .argc = 0,
  341.     .argv = NULL
  342. };
  343.  
  344. /* Data and methods for 'tlb' command. */
  345. static int cmd_tlb(cmd_arg_t *argv);
  346. cmd_info_t tlb_info = {
  347.     .name = "tlb",
  348.     .description = "Print TLB of current processor.",
  349.     .help = NULL,
  350.     .func = cmd_tlb,
  351.     .argc = 0,
  352.     .argv = NULL
  353. };
  354.  
  355. static int cmd_threads(cmd_arg_t *argv);
  356. static cmd_info_t threads_info = {
  357.     .name = "threads",
  358.     .description = "List all threads.",
  359.     .func = cmd_threads,
  360.     .argc = 0
  361. };
  362.  
  363. static int cmd_tasks(cmd_arg_t *argv);
  364. static cmd_info_t tasks_info = {
  365.     .name = "tasks",
  366.     .description = "List all tasks.",
  367.     .func = cmd_tasks,
  368.     .argc = 0
  369. };
  370.  
  371.  
  372. static int cmd_sched(cmd_arg_t *argv);
  373. static cmd_info_t sched_info = {
  374.     .name = "scheduler",
  375.     .description = "List all scheduler information.",
  376.     .func = cmd_sched,
  377.     .argc = 0
  378. };
  379.  
  380. static int cmd_slabs(cmd_arg_t *argv);
  381. static cmd_info_t slabs_info = {
  382.     .name = "slabs",
  383.     .description = "List slab caches.",
  384.     .func = cmd_slabs,
  385.     .argc = 0
  386. };
  387.  
  388. /* Data and methods for 'zones' command */
  389. static int cmd_zones(cmd_arg_t *argv);
  390. static cmd_info_t zones_info = {
  391.     .name = "zones",
  392.     .description = "List of memory zones.",
  393.     .func = cmd_zones,
  394.     .argc = 0
  395. };
  396.  
  397. /* Data and methods for 'ipc' command */
  398. static int cmd_ipc(cmd_arg_t *argv);
  399. static cmd_arg_t ipc_argv = {
  400.     .type = ARG_TYPE_INT,
  401. };
  402. static cmd_info_t ipc_info = {
  403.     .name = "ipc",
  404.     .description = "ipc <taskid> Show IPC information of given task.",
  405.     .func = cmd_ipc,
  406.     .argc = 1,
  407.     .argv = &ipc_argv
  408. };
  409.  
  410. /* Data and methods for 'zone' command */
  411. static int cmd_zone(cmd_arg_t *argv);
  412. static cmd_arg_t zone_argv = {
  413.     .type = ARG_TYPE_INT,
  414. };
  415.  
  416. static cmd_info_t zone_info = {
  417.     .name = "zone",
  418.     .description = "Show memory zone structure.",
  419.     .func = cmd_zone,
  420.     .argc = 1,
  421.     .argv = &zone_argv
  422. };
  423.  
  424. /* Data and methods for 'cpus' command. */
  425. static int cmd_cpus(cmd_arg_t *argv);
  426. cmd_info_t cpus_info = {
  427.     .name = "cpus",
  428.     .description = "List all processors.",
  429.     .help = NULL,
  430.     .func = cmd_cpus,
  431.     .argc = 0,
  432.     .argv = NULL
  433. };
  434.  
  435. /* Data and methods for 'version' command. */
  436. static int cmd_version(cmd_arg_t *argv);
  437. cmd_info_t version_info = {
  438.     .name = "version",
  439.     .description = "Print version information.",
  440.     .help = NULL,
  441.     .func = cmd_version,
  442.     .argc = 0,
  443.     .argv = NULL
  444. };
  445.  
  446. static cmd_info_t *basic_commands[] = {
  447.     &call0_info,
  448.     &mcall0_info,
  449.     &call1_info,
  450.     &call2_info,
  451.     &call3_info,
  452.     &continue_info,
  453.     &cpus_info,
  454.     &desc_info,
  455.     &reboot_info,
  456.     &uptime_info,
  457.     &halt_info,
  458.     &help_info,
  459.     &ipc_info,
  460.     &set4_info,
  461.     &slabs_info,
  462.     &symaddr_info,
  463.     &sched_info,
  464.     &threads_info,
  465.     &tasks_info,
  466.     &physmem_info,
  467.     &tlb_info,
  468.     &version_info,
  469.     &zones_info,
  470.     &zone_info,
  471. #ifdef CONFIG_TEST
  472.     &tests_info,
  473.     &test_info,
  474.     &bench_info,
  475. #endif
  476.     NULL
  477. };
  478.  
  479.  
  480. /** Initialize command info structure.
  481.  *
  482.  * @param cmd Command info structure.
  483.  *
  484.  */
  485. void cmd_initialize(cmd_info_t *cmd)
  486. {
  487.     spinlock_initialize(&cmd->lock, "cmd");
  488.     link_initialize(&cmd->link);
  489. }
  490.  
  491. /** Initialize and register commands. */
  492. void cmd_init(void)
  493. {
  494.     unsigned int i;
  495.  
  496.     for (i = 0; basic_commands[i]; i++) {
  497.         cmd_initialize(basic_commands[i]);
  498.         if (!cmd_register(basic_commands[i]))
  499.             printf("Cannot register command %s\n", basic_commands[i]->name);
  500.     }
  501. }
  502.  
  503.  
  504. /** List supported commands.
  505.  *
  506.  * @param argv Argument vector.
  507.  *
  508.  * @return 0 on failure, 1 on success.
  509.  */
  510. int cmd_help(cmd_arg_t *argv)
  511. {
  512.     spinlock_lock(&cmd_lock);
  513.    
  514.     link_t *cur;
  515.     size_t len = 0;
  516.     for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
  517.         cmd_info_t *hlp;
  518.         hlp = list_get_instance(cur, cmd_info_t, link);
  519.        
  520.         spinlock_lock(&hlp->lock);
  521.         if (strlen(hlp->name) > len)
  522.             len = strlen(hlp->name);
  523.         spinlock_unlock(&hlp->lock);
  524.     }
  525.    
  526.     for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
  527.         cmd_info_t *hlp;
  528.         hlp = list_get_instance(cur, cmd_info_t, link);
  529.        
  530.         spinlock_lock(&hlp->lock);
  531.         printf("%-*s %s\n", len, hlp->name, hlp->description);
  532.         spinlock_unlock(&hlp->lock);
  533.     }
  534.    
  535.     spinlock_unlock(&cmd_lock);
  536.    
  537.     return 1;
  538. }
  539.  
  540.  
  541. /** Reboot the system.
  542.  *
  543.  * @param argv Argument vector.
  544.  *
  545.  * @return 0 on failure, 1 on success.
  546.  */
  547. int cmd_reboot(cmd_arg_t *argv)
  548. {
  549.     reboot();
  550.    
  551.     /* Not reached */
  552.     return 1;
  553. }
  554.  
  555.  
  556. /** Print system uptime information.
  557.  *
  558.  * @param argv Argument vector.
  559.  *
  560.  * @return 0 on failure, 1 on success.
  561.  */
  562. int cmd_uptime(cmd_arg_t *argv)
  563. {
  564.     ASSERT(uptime);
  565.    
  566.     /* This doesn't have to be very accurate */
  567.     unative_t sec = uptime->seconds1;
  568.    
  569.     printf("Up %" PRIun " days, %" PRIun " hours, %" PRIun " minutes, %" PRIun " seconds\n",
  570.         sec / 86400, (sec % 86400) / 3600, (sec % 3600) / 60, sec % 60);
  571.    
  572.     return 1;
  573. }
  574.  
  575. /** Describe specified command.
  576.  *
  577.  * @param argv Argument vector.
  578.  *
  579.  * @return 0 on failure, 1 on success.
  580.  */
  581. int cmd_desc(cmd_arg_t *argv)
  582. {
  583.     link_t *cur;
  584.  
  585.     spinlock_lock(&cmd_lock);
  586.    
  587.     for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
  588.         cmd_info_t *hlp;
  589.        
  590.         hlp = list_get_instance(cur, cmd_info_t, link);
  591.         spinlock_lock(&hlp->lock);
  592.  
  593.         if (strncmp(hlp->name, (const char *) argv->buffer, strlen(hlp->name)) == 0) {
  594.             printf("%s - %s\n", hlp->name, hlp->description);
  595.             if (hlp->help)
  596.                 hlp->help();
  597.             spinlock_unlock(&hlp->lock);
  598.             break;
  599.         }
  600.  
  601.         spinlock_unlock(&hlp->lock);
  602.     }
  603.    
  604.     spinlock_unlock(&cmd_lock);
  605.  
  606.     return 1;
  607. }
  608.  
  609. /** Search symbol table */
  610. int cmd_symaddr(cmd_arg_t *argv)
  611. {
  612.     symtab_print_search((char *) argv->buffer);
  613.    
  614.     return 1;
  615. }
  616.  
  617. /** Call function with zero parameters */
  618. int cmd_call0(cmd_arg_t *argv)
  619. {
  620.     uintptr_t symaddr;
  621.     char *symbol;
  622.     unative_t (*fnc)(void);
  623.     fncptr_t fptr;
  624.     int rc;
  625.  
  626.     symbol = (char *) argv->buffer;
  627.     rc = symtab_addr_lookup(symbol, &symaddr);
  628.  
  629.     if (rc == ENOENT)
  630.         printf("Symbol %s not found.\n", symbol);
  631.     else if (rc == EOVERFLOW) {
  632.         symtab_print_search(symbol);
  633.         printf("Duplicate symbol, be more specific.\n");
  634.     } else if (rc == EOK) {
  635.         fnc = (unative_t (*)(void)) arch_construct_function(&fptr,
  636.             (void *) symaddr, (void *) cmd_call0);
  637.         printf("Calling %s() (%p)\n", symbol, symaddr);
  638.         printf("Result: %#" PRIxn "\n", fnc());
  639.     } else {
  640.         printf("No symbol information available.\n");
  641.     }
  642.     return 1;
  643. }
  644.  
  645. /** Call function with zero parameters on each CPU */
  646. int cmd_mcall0(cmd_arg_t *argv)
  647. {
  648.     /*
  649.      * For each CPU, create a thread which will
  650.      * call the function.
  651.      */
  652.    
  653.     count_t i;
  654.     for (i = 0; i < config.cpu_count; i++) {
  655.         if (!cpus[i].active)
  656.             continue;
  657.        
  658.         thread_t *t;
  659.         if ((t = thread_create((void (*)(void *)) cmd_call0, (void *) argv, TASK, THREAD_FLAG_WIRED, "call0", false))) {
  660.             spinlock_lock(&t->lock);
  661.             t->cpu = &cpus[i];
  662.             spinlock_unlock(&t->lock);
  663.             printf("cpu%u: ", i);
  664.             thread_ready(t);
  665.             thread_join(t);
  666.             thread_detach(t);
  667.         } else
  668.             printf("Unable to create thread for cpu%u\n", i);
  669.     }
  670.    
  671.     return 1;
  672. }
  673.  
  674. /** Call function with one parameter */
  675. int cmd_call1(cmd_arg_t *argv)
  676. {
  677.     uintptr_t symaddr;
  678.     char *symbol;
  679.     unative_t (*fnc)(unative_t, ...);
  680.     unative_t arg1 = argv[1].intval;
  681.     fncptr_t fptr;
  682.     int rc;
  683.  
  684.     symbol = (char *) argv->buffer;
  685.     rc = symtab_addr_lookup(symbol, &symaddr);
  686.  
  687.     if (rc == ENOENT) {
  688.         printf("Symbol %s not found.\n", symbol);
  689.     } else if (rc == EOVERFLOW) {
  690.         symtab_print_search(symbol);
  691.         printf("Duplicate symbol, be more specific.\n");
  692.     } else if (rc == EOK) {
  693.         fnc = (unative_t (*)(unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call1);
  694.         printf("Calling f(%#" PRIxn "): %p: %s\n", arg1, symaddr, symbol);
  695.         printf("Result: %#" PRIxn "\n", fnc(arg1));
  696.     } else {
  697.         printf("No symbol information available.\n");
  698.     }
  699.  
  700.     return 1;
  701. }
  702.  
  703. /** Call function with two parameters */
  704. int cmd_call2(cmd_arg_t *argv)
  705. {
  706.     uintptr_t symaddr;
  707.     char *symbol;
  708.     unative_t (*fnc)(unative_t, unative_t, ...);
  709.     unative_t arg1 = argv[1].intval;
  710.     unative_t arg2 = argv[2].intval;
  711.     fncptr_t fptr;
  712.     int rc;
  713.  
  714.     symbol = (char *) argv->buffer;
  715.     rc = symtab_addr_lookup(symbol, &symaddr);
  716.  
  717.     if (rc == ENOENT) {
  718.         printf("Symbol %s not found.\n", symbol);
  719.     } else if (rc == EOVERFLOW) {
  720.         symtab_print_search(symbol);
  721.         printf("Duplicate symbol, be more specific.\n");
  722.     } else if (rc == EOK) {
  723.         fnc = (unative_t (*)(unative_t, unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call2);
  724.         printf("Calling f(%#" PRIxn ", %#" PRIxn "): %p: %s\n",
  725.                arg1, arg2, symaddr, symbol);
  726.         printf("Result: %#" PRIxn "\n", fnc(arg1, arg2));
  727.     } else {
  728.         printf("No symbol information available.\n");
  729.     }
  730.     return 1;
  731. }
  732.  
  733. /** Call function with three parameters */
  734. int cmd_call3(cmd_arg_t *argv)
  735. {
  736.     uintptr_t symaddr;
  737.     char *symbol;
  738.     unative_t (*fnc)(unative_t, unative_t, unative_t, ...);
  739.     unative_t arg1 = argv[1].intval;
  740.     unative_t arg2 = argv[2].intval;
  741.     unative_t arg3 = argv[3].intval;
  742.     fncptr_t fptr;
  743.     int rc;
  744.    
  745.     symbol = (char *) argv->buffer;
  746.     rc = symtab_addr_lookup(symbol, &symaddr);
  747.  
  748.     if (rc == ENOENT) {
  749.         printf("Symbol %s not found.\n", symbol);
  750.     } else if (rc == EOVERFLOW) {
  751.         symtab_print_search(symbol);
  752.         printf("Duplicate symbol, be more specific.\n");
  753.     } else if (rc == EOK) {
  754.         fnc = (unative_t (*)(unative_t, unative_t, unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call3);
  755.         printf("Calling f(%#" PRIxn ",%#" PRIxn ", %#" PRIxn "): %p: %s\n",
  756.                arg1, arg2, arg3, symaddr, symbol);
  757.         printf("Result: %#" PRIxn "\n", fnc(arg1, arg2, arg3));
  758.     } else {
  759.         printf("No symbol information available.\n");
  760.     }
  761.     return 1;
  762. }
  763.  
  764.  
  765. /** Print detailed description of 'describe' command. */
  766. void desc_help(void)
  767. {
  768.     printf("Syntax: describe command_name\n");
  769. }
  770.  
  771. /** Halt the kernel.
  772.  *
  773.  * @param argv Argument vector (ignored).
  774.  *
  775.  * @return 0 on failure, 1 on success (never returns).
  776.  */
  777. int cmd_halt(cmd_arg_t *argv)
  778. {
  779.     halt();
  780.     return 1;
  781. }
  782.  
  783. /** Command for printing TLB contents.
  784.  *
  785.  * @param argv Not used.
  786.  *
  787.  * @return Always returns 1.
  788.  */
  789. int cmd_tlb(cmd_arg_t *argv)
  790. {
  791.     tlb_print();
  792.     return 1;
  793. }
  794.  
  795. /** Command for printing physical memory configuration.
  796.  *
  797.  * @param argv Not used.
  798.  *
  799.  * @return Always returns 1.
  800.  */
  801. int cmd_physmem(cmd_arg_t *argv)
  802. {
  803.     physmem_print();
  804.     return 1;
  805. }
  806.  
  807. /** Write 4 byte value to address */
  808. int cmd_set4(cmd_arg_t *argv)
  809. {
  810.     uintptr_t addr;
  811.     uint32_t arg1 = argv[1].intval;
  812.     bool pointer = false;
  813.     int rc;
  814.  
  815.     if (((char *)argv->buffer)[0] == '*') {
  816.         rc = symtab_addr_lookup((char *) argv->buffer + 1, &addr);
  817.         pointer = true;
  818.     } else if (((char *) argv->buffer)[0] >= '0' &&
  819.            ((char *)argv->buffer)[0] <= '9') {
  820.         rc = EOK;
  821.         addr = atoi((char *)argv->buffer);
  822.     } else {
  823.         rc = symtab_addr_lookup((char *) argv->buffer, &addr);
  824.     }
  825.  
  826.     if (rc == ENOENT)
  827.         printf("Symbol %s not found.\n", argv->buffer);
  828.     else if (rc == EOVERFLOW) {
  829.         symtab_print_search((char *) argv->buffer);
  830.         printf("Duplicate symbol, be more specific.\n");
  831.     } else if (rc == EOK) {
  832.         if (pointer)
  833.             addr = *(uintptr_t *) addr;
  834.         printf("Writing %#" PRIx64 " -> %p\n", arg1, addr);
  835.         *(uint32_t *) addr = arg1;
  836.     } else {
  837.         printf("No symbol information available.\n");
  838.     }
  839.    
  840.     return 1;
  841. }
  842.  
  843. /** Command for listings SLAB caches
  844.  *
  845.  * @param argv Ignores
  846.  *
  847.  * @return Always 1
  848.  */
  849. int cmd_slabs(cmd_arg_t * argv) {
  850.     slab_print_list();
  851.     return 1;
  852. }
  853.  
  854.  
  855. /** Command for listings Thread information
  856.  *
  857.  * @param argv Ignores
  858.  *
  859.  * @return Always 1
  860.  */
  861. int cmd_threads(cmd_arg_t * argv) {
  862.     thread_print_list();
  863.     return 1;
  864. }
  865.  
  866. /** Command for listings Task information
  867.  *
  868.  * @param argv Ignores
  869.  *
  870.  * @return Always 1
  871.  */
  872. int cmd_tasks(cmd_arg_t * argv) {
  873.     task_print_list();
  874.     return 1;
  875. }
  876.  
  877. /** Command for listings Thread information
  878.  *
  879.  * @param argv Ignores
  880.  *
  881.  * @return Always 1
  882.  */
  883. int cmd_sched(cmd_arg_t * argv) {
  884.     sched_print_list();
  885.     return 1;
  886. }
  887.  
  888. /** Command for listing memory zones
  889.  *
  890.  * @param argv Ignored
  891.  *
  892.  * return Always 1
  893.  */
  894. int cmd_zones(cmd_arg_t * argv) {
  895.     zone_print_list();
  896.     return 1;
  897. }
  898.  
  899. /** Command for memory zone details
  900.  *
  901.  * @param argv Integer argument from cmdline expected
  902.  *
  903.  * return Always 1
  904.  */
  905. int cmd_zone(cmd_arg_t * argv) {
  906.     zone_print_one(argv[0].intval);
  907.     return 1;
  908. }
  909.  
  910. /** Command for printing task ipc details
  911.  *
  912.  * @param argv Integer argument from cmdline expected
  913.  *
  914.  * return Always 1
  915.  */
  916. int cmd_ipc(cmd_arg_t * argv) {
  917.     ipc_print_task(argv[0].intval);
  918.     return 1;
  919. }
  920.  
  921.  
  922. /** Command for listing processors.
  923.  *
  924.  * @param argv Ignored.
  925.  *
  926.  * return Always 1.
  927.  */
  928. int cmd_cpus(cmd_arg_t *argv)
  929. {
  930.     cpu_list();
  931.     return 1;
  932. }
  933.  
  934. /** Command for printing kernel version.
  935.  *
  936.  * @param argv Ignored.
  937.  *
  938.  * return Always 1.
  939.  */
  940. int cmd_version(cmd_arg_t *argv)
  941. {
  942.     version_print();
  943.     return 1;
  944. }
  945.  
  946. /** Command for returning console back to userspace.
  947.  *
  948.  * @param argv Ignored.
  949.  *
  950.  * return Always 1.
  951.  */
  952. int cmd_continue(cmd_arg_t *argv)
  953. {
  954.     printf("The kernel will now relinquish the console.\n");
  955.     release_console();
  956.    
  957.     if ((kconsole_notify) && (kconsole_irq.notif_cfg.notify))
  958.         ipc_irq_send_msg_0(&kconsole_irq);
  959.    
  960.     return 1;
  961. }
  962.  
  963. #ifdef CONFIG_TEST
  964. /** Command for printing kernel tests list.
  965.  *
  966.  * @param argv Ignored.
  967.  *
  968.  * return Always 1.
  969.  */
  970. int cmd_tests(cmd_arg_t *argv)
  971. {
  972.     size_t len = 0;
  973.     test_t *test;
  974.     for (test = tests; test->name != NULL; test++) {
  975.         if (strlen(test->name) > len)
  976.             len = strlen(test->name);
  977.     }
  978.    
  979.     for (test = tests; test->name != NULL; test++)
  980.         printf("%-*s %s%s\n", len, test->name, test->desc, (test->safe ? "" : " (unsafe)"));
  981.    
  982.     printf("%-*s Run all safe tests\n", len, "*");
  983.     return 1;
  984. }
  985.  
  986. static bool run_test(const test_t *test)
  987. {
  988.     printf("%s (%s)\n", test->name, test->desc);
  989.    
  990.     /* Update and read thread accounting
  991.        for benchmarking */
  992.     ipl_t ipl = interrupts_disable();
  993.     spinlock_lock(&TASK->lock);
  994.     uint64_t t0 = task_get_accounting(TASK);
  995.     spinlock_unlock(&TASK->lock);
  996.     interrupts_restore(ipl);
  997.    
  998.     /* Execute the test */
  999.     char * ret = test->entry(false);
  1000.    
  1001.     /* Update and read thread accounting */
  1002.     ipl = interrupts_disable();
  1003.     spinlock_lock(&TASK->lock);
  1004.     uint64_t dt = task_get_accounting(TASK) - t0;
  1005.     spinlock_unlock(&TASK->lock);
  1006.     interrupts_restore(ipl);
  1007.    
  1008.     uint64_t cycles;
  1009.     char suffix;
  1010.     order(dt, &cycles, &suffix);
  1011.        
  1012.     printf("Time: %" PRIu64 "%c cycles\n", cycles, suffix);
  1013.    
  1014.     if (ret == NULL) {
  1015.         printf("Test passed\n");
  1016.         return true;
  1017.     }
  1018.  
  1019.     printf("%s\n", ret);
  1020.     return false;
  1021. }
  1022.  
  1023. static bool run_bench(const test_t *test, const uint32_t cnt)
  1024. {
  1025.     uint32_t i;
  1026.     bool ret = true;
  1027.     uint64_t cycles;
  1028.     char suffix;
  1029.    
  1030.     if (cnt < 1)
  1031.         return true;
  1032.    
  1033.     uint64_t *data = (uint64_t *) malloc(sizeof(uint64_t) * cnt, 0);
  1034.     if (data == NULL) {
  1035.         printf("Error allocating memory for statistics\n");
  1036.         return false;
  1037.     }
  1038.    
  1039.     for (i = 0; i < cnt; i++) {
  1040.         printf("%s (%u/%u) ... ", test->name, i + 1, cnt);
  1041.        
  1042.         /* Update and read thread accounting
  1043.            for benchmarking */
  1044.         ipl_t ipl = interrupts_disable();
  1045.         spinlock_lock(&TASK->lock);
  1046.         uint64_t t0 = task_get_accounting(TASK);
  1047.         spinlock_unlock(&TASK->lock);
  1048.         interrupts_restore(ipl);
  1049.        
  1050.         /* Execute the test */
  1051.         char * ret = test->entry(true);
  1052.        
  1053.         /* Update and read thread accounting */
  1054.         ipl = interrupts_disable();
  1055.         spinlock_lock(&TASK->lock);
  1056.         uint64_t dt = task_get_accounting(TASK) - t0;
  1057.         spinlock_unlock(&TASK->lock);
  1058.         interrupts_restore(ipl);
  1059.        
  1060.         if (ret != NULL) {
  1061.             printf("%s\n", ret);
  1062.             ret = false;
  1063.             break;
  1064.         }
  1065.        
  1066.         data[i] = dt;
  1067.         order(dt, &cycles, &suffix);
  1068.         printf("OK (%" PRIu64 "%c cycles)\n", cycles, suffix);
  1069.     }
  1070.    
  1071.     if (ret) {
  1072.         printf("\n");
  1073.        
  1074.         uint64_t sum = 0;
  1075.        
  1076.         for (i = 0; i < cnt; i++) {
  1077.             sum += data[i];
  1078.         }
  1079.        
  1080.         order(sum / (uint64_t) cnt, &cycles, &suffix);
  1081.         printf("Average\t\t%" PRIu64 "%c\n", cycles, suffix);
  1082.     }
  1083.    
  1084.     free(data);
  1085.    
  1086.     return ret;
  1087. }
  1088.  
  1089. /** Command for returning kernel tests
  1090.  *
  1091.  * @param argv Argument vector.
  1092.  *
  1093.  * return Always 1.
  1094.  */
  1095. int cmd_test(cmd_arg_t *argv)
  1096. {
  1097.     test_t *test;
  1098.    
  1099.     if (strcmp((char *) argv->buffer, "*") == 0) {
  1100.         for (test = tests; test->name != NULL; test++) {
  1101.             if (test->safe) {
  1102.                 printf("\n");
  1103.                 if (!run_test(test))
  1104.                     break;
  1105.             }
  1106.         }
  1107.     } else {
  1108.         bool fnd = false;
  1109.        
  1110.         for (test = tests; test->name != NULL; test++) {
  1111.             if (strcmp(test->name, (char *) argv->buffer) == 0) {
  1112.                 fnd = true;
  1113.                 run_test(test);
  1114.                 break;
  1115.             }
  1116.         }
  1117.        
  1118.         if (!fnd)
  1119.             printf("Unknown test\n");
  1120.     }
  1121.    
  1122.     return 1;
  1123. }
  1124.  
  1125. /** Command for returning kernel tests as benchmarks
  1126.  *
  1127.  * @param argv Argument vector.
  1128.  *
  1129.  * return Always 1.
  1130.  */
  1131. int cmd_bench(cmd_arg_t *argv)
  1132. {
  1133.     test_t *test;
  1134.     uint32_t cnt = argv[1].intval;
  1135.    
  1136.     bool fnd = false;
  1137.    
  1138.     for (test = tests; test->name != NULL; test++) {
  1139.         if (strcmp(test->name, (char *) argv->buffer) == 0) {
  1140.             fnd = true;
  1141.            
  1142.             if (test->safe)
  1143.                 run_bench(test, cnt);
  1144.             else
  1145.                 printf("Unsafe test\n");
  1146.            
  1147.             break;
  1148.         }
  1149.     }
  1150.        
  1151.     if (!fnd)
  1152.         printf("Unknown test\n");
  1153.  
  1154.     return 1;
  1155. }
  1156.  
  1157. #endif
  1158.  
  1159. /** @}
  1160.  */
  1161.