Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/console/cmd.c

    r0b0f4bb rcb3d641a  
    4646#include <print.h>
    4747#include <panic.h>
    48 #include <typedefs.h>
     48#include <arch/types.h>
    4949#include <adt/list.h>
    5050#include <arch.h>
    5151#include <config.h>
    5252#include <func.h>
    53 #include <str.h>
     53#include <string.h>
    5454#include <macros.h>
    5555#include <debug.h>
     
    6666#include <ipc/irq.h>
    6767#include <ipc/event.h>
    68 #include <sysinfo/sysinfo.h>
    6968#include <symtab.h>
    7069#include <errno.h>
     
    108107
    109108#ifdef CONFIG_TEST
     109static int cmd_tests(cmd_arg_t *argv);
     110static cmd_info_t tests_info = {
     111        .name = "tests",
     112        .description = "Print available kernel tests.",
     113        .func = cmd_tests,
     114        .argc = 0
     115};
     116
    110117static char test_buf[MAX_CMDLINE + 1];
    111118static int cmd_test(cmd_arg_t *argv);
    112119static cmd_arg_t test_argv[] = {
    113120        {
    114                 .type = ARG_TYPE_STRING_OPTIONAL,
     121                .type = ARG_TYPE_STRING,
    115122                .buffer = test_buf,
    116123                .len = sizeof(test_buf)
     
    119126static cmd_info_t test_info = {
    120127        .name = "test",
    121         .description = "Print list of kernel tests or run a test.",
     128        .description = "Run kernel test.",
    122129        .func = cmd_test,
    123130        .argc = 1,
     
    199206};
    200207
    201 /* Data and methods for 'call0' and 'mcall0' command. */
     208/* Data and methods for 'call0' command. */
    202209static char call0_buf[MAX_CMDLINE + 1];
    203210static char carg1_buf[MAX_CMDLINE + 1];
     
    347354};
    348355
    349 static char flag_buf[MAX_CMDLINE + 1];
    350 
    351356static int cmd_threads(cmd_arg_t *argv);
    352 static cmd_arg_t threads_argv = {
    353         .type = ARG_TYPE_STRING_OPTIONAL,
    354         .buffer = flag_buf,
    355         .len = sizeof(flag_buf)
    356 };
    357357static cmd_info_t threads_info = {
    358358        .name = "threads",
    359         .description = "List all threads (use -a for additional information).",
     359        .description = "List all threads.",
    360360        .func = cmd_threads,
    361         .argc = 1,
    362         .argv = &threads_argv
     361        .argc = 0
    363362};
    364363
    365364static int cmd_tasks(cmd_arg_t *argv);
    366 static cmd_arg_t tasks_argv = {
    367         .type = ARG_TYPE_STRING_OPTIONAL,
    368         .buffer = flag_buf,
    369         .len = sizeof(flag_buf)
    370 };
    371365static cmd_info_t tasks_info = {
    372366        .name = "tasks",
    373         .description = "List all tasks (use -a for additional information).",
     367        .description = "List all tasks.",
    374368        .func = cmd_tasks,
    375         .argc = 1,
    376         .argv = &tasks_argv
     369        .argc = 0
    377370};
    378371
     
    391384        .description = "List slab caches.",
    392385        .func = cmd_slabs,
    393         .argc = 0
    394 };
    395 
    396 static int cmd_sysinfo(cmd_arg_t *argv);
    397 static cmd_info_t sysinfo_info = {
    398         .name = "sysinfo",
    399         .description = "Dump sysinfo.",
    400         .func = cmd_sysinfo,
    401386        .argc = 0
    402387};
     
    490475        &set4_info,
    491476        &slabs_info,
    492         &sysinfo_info,
    493477        &symaddr_info,
    494478        &sched_info,
     
    501485        &zone_info,
    502486#ifdef CONFIG_TEST
     487        &tests_info,
    503488        &test_info,
    504489        &bench_info,
     
    515500void cmd_initialize(cmd_info_t *cmd)
    516501{
    517         spinlock_initialize(&cmd->lock, "cmd.lock");
     502        spinlock_initialize(&cmd->lock, "cmd");
    518503        link_initialize(&cmd->link);
    519504}
     
    554539        }
    555540       
    556         unsigned int _len = (unsigned int) len;
    557         if ((_len != len) || (((int) _len) < 0)) {
    558                 printf("Command length overflow\n");
    559                 return 1;
    560         }
    561        
    562541        for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
    563542                cmd_info_t *hlp;
     
    565544               
    566545                spinlock_lock(&hlp->lock);
    567                 printf("%-*s %s\n", _len, hlp->name, hlp->description);
     546                printf("%-*s %s\n", len, hlp->name, hlp->description);
    568547                spinlock_unlock(&hlp->lock);
    569548        }
     
    669648                printf("Duplicate symbol, be more specific.\n");
    670649        } else if (rc == EOK) {
    671                 ipl_t ipl;
    672 
    673                 ipl = interrupts_disable();
    674650                fnc = (unative_t (*)(void)) arch_construct_function(&fptr,
    675651                    (void *) symaddr, (void *) cmd_call0);
    676                 printf("Calling %s() (%p)\n", symbol, (void *) symaddr);
     652                printf("Calling %s() (%p)\n", symbol, symaddr);
    677653                printf("Result: %#" PRIxn "\n", fnc());
    678                 interrupts_restore(ipl);
    679654        } else {
    680655                printf("No symbol information available.\n");
     
    691666         */
    692667       
    693         unsigned int i;
     668        size_t i;
    694669        for (i = 0; i < config.cpu_count; i++) {
    695670                if (!cpus[i].active)
    696671                        continue;
    697672               
    698                 thread_t *thread;
    699                 if ((thread = thread_create((void (*)(void *)) cmd_call0,
    700                     (void *) argv, TASK, THREAD_FLAG_WIRED, "call0", false))) {
    701                         irq_spinlock_lock(&thread->lock, true);
    702                         thread->cpu = &cpus[i];
    703                         irq_spinlock_unlock(&thread->lock, true);
    704                        
     673                thread_t *t;
     674                if ((t = thread_create((void (*)(void *)) cmd_call0, (void *) argv, TASK, THREAD_FLAG_WIRED, "call0", false))) {
     675                        spinlock_lock(&t->lock);
     676                        t->cpu = &cpus[i];
     677                        spinlock_unlock(&t->lock);
    705678                        printf("cpu%u: ", i);
    706                        
    707                         thread_ready(thread);
    708                         thread_join(thread);
    709                         thread_detach(thread);
     679                        thread_ready(t);
     680                        thread_join(t);
     681                        thread_detach(t);
    710682                } else
    711683                        printf("Unable to create thread for cpu%u\n", i);
     
    734706                printf("Duplicate symbol, be more specific.\n");
    735707        } else if (rc == EOK) {
    736                 ipl_t ipl;
    737 
    738                 ipl = interrupts_disable();
    739                 fnc = (unative_t (*)(unative_t, ...))
    740                     arch_construct_function(&fptr, (void *) symaddr,
    741                     (void *) cmd_call1);
    742                 printf("Calling f(%#" PRIxn "): %p: %s\n", arg1,
    743                     (void *) symaddr, symbol);
     708                fnc = (unative_t (*)(unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call1);
     709                printf("Calling f(%#" PRIxn "): %p: %s\n", arg1, symaddr, symbol);
    744710                printf("Result: %#" PRIxn "\n", fnc(arg1));
    745                 interrupts_restore(ipl);
    746711        } else {
    747712                printf("No symbol information available.\n");
     
    771736                printf("Duplicate symbol, be more specific.\n");
    772737        } else if (rc == EOK) {
    773                 ipl_t ipl;
    774 
    775                 ipl = interrupts_disable();
    776                 fnc = (unative_t (*)(unative_t, unative_t, ...))
    777                     arch_construct_function(&fptr, (void *) symaddr,
    778                     (void *) cmd_call2);
     738                fnc = (unative_t (*)(unative_t, unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call2);
    779739                printf("Calling f(%#" PRIxn ", %#" PRIxn "): %p: %s\n",
    780                        arg1, arg2, (void *) symaddr, symbol);
     740                       arg1, arg2, symaddr, symbol);
    781741                printf("Result: %#" PRIxn "\n", fnc(arg1, arg2));
    782                 interrupts_restore(ipl);
    783742        } else {
    784743                printf("No symbol information available.\n");
     
    808767                printf("Duplicate symbol, be more specific.\n");
    809768        } else if (rc == EOK) {
    810                 ipl_t ipl;
    811 
    812                 ipl = interrupts_disable();
    813                 fnc = (unative_t (*)(unative_t, unative_t, unative_t, ...))
    814                     arch_construct_function(&fptr, (void *) symaddr,
    815                     (void *) cmd_call3);
    816                 printf("Calling f(%#" PRIxn ",%#" PRIxn ", %#" PRIxn "): %p: %s\n",
    817                        arg1, arg2, arg3, (void *) symaddr, symbol);
     769                fnc = (unative_t (*)(unative_t, unative_t, unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call3);
     770                printf("Calling f(%#" PRIxn ",%#" PRIxn ", %#" PRIxn "): %p: %s\n",
     771                       arg1, arg2, arg3, symaddr, symbol);
    818772                printf("Result: %#" PRIxn "\n", fnc(arg1, arg2, arg3));
    819                 interrupts_restore(ipl);
    820773        } else {
    821774                printf("No symbol information available.\n");
     
    874827        bool pointer = false;
    875828        int rc;
    876        
    877         if (((char *) argv->buffer)[0] == '*') {
     829
     830        if (((char *)argv->buffer)[0] == '*') {
    878831                rc = symtab_addr_lookup((char *) argv->buffer + 1, &addr);
    879832                pointer = true;
    880         } else if (((char *) argv->buffer)[0] >= '0' &&
    881                    ((char *) argv->buffer)[0] <= '9') {
    882                 uint64_t value;
    883                 rc = str_uint64((char *) argv->buffer, NULL, 0, true, &value);
    884                 if (rc == EOK)
    885                         addr = (uintptr_t) value;
    886         } else
     833        } else if (((char *) argv->buffer)[0] >= '0' &&
     834                   ((char *)argv->buffer)[0] <= '9') {
     835                rc = EOK;
     836                addr = atoi((char *)argv->buffer);
     837        } else {
    887838                rc = symtab_addr_lookup((char *) argv->buffer, &addr);
    888        
     839        }
     840
    889841        if (rc == ENOENT)
    890                 printf("Symbol %s not found.\n", (char *) argv->buffer);
    891         else if (rc == EINVAL)
    892                 printf("Invalid address.\n");
     842                printf("Symbol %s not found.\n", argv->buffer);
    893843        else if (rc == EOVERFLOW) {
    894844                symtab_print_search((char *) argv->buffer);
    895                 printf("Duplicate symbol (be more specific) or address overflow.\n");
     845                printf("Duplicate symbol, be more specific.\n");
    896846        } else if (rc == EOK) {
    897847                if (pointer)
    898848                        addr = *(uintptr_t *) addr;
    899                 printf("Writing %#" PRIx32" -> %p\n", arg1, (void *) addr);
     849                printf("Writing %#" PRIx64 " -> %p\n", arg1, addr);
    900850                *(uint32_t *) addr = arg1;
    901         } else
     851        } else {
    902852                printf("No symbol information available.\n");
     853        }
    903854       
    904855        return 1;
     
    917868}
    918869
    919 /** Command for dumping sysinfo
     870
     871/** Command for listings Thread information
    920872 *
    921873 * @param argv Ignores
     
    923875 * @return Always 1
    924876 */
    925 int cmd_sysinfo(cmd_arg_t * argv)
    926 {
    927         sysinfo_dump(NULL);
    928         return 1;
    929 }
    930 
     877int cmd_threads(cmd_arg_t * argv)
     878{
     879        thread_print_list();
     880        return 1;
     881}
     882
     883/** Command for listings Task information
     884 *
     885 * @param argv Ignores
     886 *
     887 * @return Always 1
     888 */
     889int cmd_tasks(cmd_arg_t * argv)
     890{
     891        task_print_list();
     892        return 1;
     893}
    931894
    932895/** Command for listings Thread information
    933896 *
     897 * @param argv Ignores
     898 *
     899 * @return Always 1
     900 */
     901int cmd_sched(cmd_arg_t * argv)
     902{
     903        sched_print_list();
     904        return 1;
     905}
     906
     907/** Command for listing memory zones
     908 *
    934909 * @param argv Ignored
    935910 *
    936  * @return Always 1
    937  */
    938 int cmd_threads(cmd_arg_t *argv)
    939 {
    940         if (str_cmp(flag_buf, "-a") == 0)
    941                 thread_print_list(true);
    942         else if (str_cmp(flag_buf, "") == 0)
    943                 thread_print_list(false);
    944         else
    945                 printf("Unknown argument \"%s\".\n", flag_buf);
    946        
    947         return 1;
    948 }
    949 
    950 /** Command for listings Task information
    951  *
    952  * @param argv Ignored
    953  *
    954  * @return Always 1
    955  */
    956 int cmd_tasks(cmd_arg_t *argv)
    957 {
    958         if (str_cmp(flag_buf, "-a") == 0)
    959                 task_print_list(true);
    960         else if (str_cmp(flag_buf, "") == 0)
    961                 task_print_list(false);
    962         else
    963                 printf("Unknown argument \"%s\".\n", flag_buf);
    964        
    965         return 1;
    966 }
    967 
    968 /** Command for listings Thread information
    969  *
    970  * @param argv Ignores
    971  *
    972  * @return Always 1
    973  */
    974 int cmd_sched(cmd_arg_t * argv)
    975 {
    976         sched_print_list();
    977         return 1;
    978 }
    979 
    980 /** Command for listing memory zones
    981  *
    982  * @param argv Ignored
    983  *
    984911 * return Always 1
    985912 */
    986913int cmd_zones(cmd_arg_t * argv)
    987914{
    988         zones_print_list();
     915        zone_print_list();
    989916        return 1;
    990917}
     
    1070997
    1071998#ifdef CONFIG_TEST
     999/** Command for printing kernel tests list.
     1000 *
     1001 * @param argv Ignored.
     1002 *
     1003 * return Always 1.
     1004 */
     1005int cmd_tests(cmd_arg_t *argv)
     1006{
     1007        size_t len = 0;
     1008        test_t *test;
     1009        for (test = tests; test->name != NULL; test++) {
     1010                if (str_length(test->name) > len)
     1011                        len = str_length(test->name);
     1012        }
     1013       
     1014        for (test = tests; test->name != NULL; test++)
     1015                printf("%-*s %s%s\n", len, test->name, test->desc, (test->safe ? "" : " (unsafe)"));
     1016       
     1017        printf("%-*s Run all safe tests\n", len, "*");
     1018        return 1;
     1019}
     1020
    10721021static bool run_test(const test_t *test)
    10731022{
     
    10761025        /* Update and read thread accounting
    10771026           for benchmarking */
    1078         irq_spinlock_lock(&TASK->lock, true);
    1079         uint64_t ucycles0, kcycles0;
    1080         task_get_accounting(TASK, &ucycles0, &kcycles0);
    1081         irq_spinlock_unlock(&TASK->lock, true);
     1027        ipl_t ipl = interrupts_disable();
     1028        spinlock_lock(&TASK->lock);
     1029        uint64_t t0 = task_get_accounting(TASK);
     1030        spinlock_unlock(&TASK->lock);
     1031        interrupts_restore(ipl);
    10821032       
    10831033        /* Execute the test */
    10841034        test_quiet = false;
    1085         const char *ret = test->entry();
     1035        char *ret = test->entry();
    10861036       
    10871037        /* Update and read thread accounting */
    1088         uint64_t ucycles1, kcycles1;
    1089         irq_spinlock_lock(&TASK->lock, true);
    1090         task_get_accounting(TASK, &ucycles1, &kcycles1);
    1091         irq_spinlock_unlock(&TASK->lock, true);
    1092        
    1093         uint64_t ucycles, kcycles;
    1094         char usuffix, ksuffix;
    1095         order_suffix(ucycles1 - ucycles0, &ucycles, &usuffix);
    1096         order_suffix(kcycles1 - kcycles0, &kcycles, &ksuffix);
    1097        
    1098         printf("Time: %" PRIu64 "%c user cycles, %" PRIu64 "%c kernel cycles\n",
    1099             ucycles, usuffix, kcycles, ksuffix);
     1038        ipl = interrupts_disable();
     1039        spinlock_lock(&TASK->lock);
     1040        uint64_t dt = task_get_accounting(TASK) - t0;
     1041        spinlock_unlock(&TASK->lock);
     1042        interrupts_restore(ipl);
     1043       
     1044        uint64_t cycles;
     1045        char suffix;
     1046        order(dt, &cycles, &suffix);
     1047               
     1048        printf("Time: %" PRIu64 "%c cycles\n", cycles, suffix);
    11001049       
    11011050        if (ret == NULL) {
     
    11031052                return true;
    11041053        }
    1105        
     1054
    11061055        printf("%s\n", ret);
    11071056        return false;
     
    11121061        uint32_t i;
    11131062        bool ret = true;
    1114         uint64_t ucycles, kcycles;
    1115         char usuffix, ksuffix;
     1063        uint64_t cycles;
     1064        char suffix;
    11161065       
    11171066        if (cnt < 1)
     
    11291078                /* Update and read thread accounting
    11301079                   for benchmarking */
    1131                 irq_spinlock_lock(&TASK->lock, true);
    1132                 uint64_t ucycles0, kcycles0;
    1133                 task_get_accounting(TASK, &ucycles0, &kcycles0);
    1134                 irq_spinlock_unlock(&TASK->lock, true);
     1080                ipl_t ipl = interrupts_disable();
     1081                spinlock_lock(&TASK->lock);
     1082                uint64_t t0 = task_get_accounting(TASK);
     1083                spinlock_unlock(&TASK->lock);
     1084                interrupts_restore(ipl);
    11351085               
    11361086                /* Execute the test */
    11371087                test_quiet = true;
    1138                 const char *ret = test->entry();
     1088                char * ret = test->entry();
    11391089               
    11401090                /* Update and read thread accounting */
    1141                 irq_spinlock_lock(&TASK->lock, true);
    1142                 uint64_t ucycles1, kcycles1;
    1143                 task_get_accounting(TASK, &ucycles1, &kcycles1);
    1144                 irq_spinlock_unlock(&TASK->lock, true);
     1091                ipl = interrupts_disable();
     1092                spinlock_lock(&TASK->lock);
     1093                uint64_t dt = task_get_accounting(TASK) - t0;
     1094                spinlock_unlock(&TASK->lock);
     1095                interrupts_restore(ipl);
    11451096               
    11461097                if (ret != NULL) {
     
    11501101                }
    11511102               
    1152                 data[i] = ucycles1 - ucycles0 + kcycles1 - kcycles0;
    1153                 order_suffix(ucycles1 - ucycles0, &ucycles, &usuffix);
    1154                 order_suffix(kcycles1 - kcycles0, &kcycles, &ksuffix);
    1155                 printf("OK (%" PRIu64 "%c user cycles, %" PRIu64 "%c kernel cycles)\n",
    1156                     ucycles, usuffix, kcycles, ksuffix);
     1103                data[i] = dt;
     1104                order(dt, &cycles, &suffix);
     1105                printf("OK (%" PRIu64 "%c cycles)\n", cycles, suffix);
    11571106        }
    11581107       
     
    11661115                }
    11671116               
    1168                 order_suffix(sum / (uint64_t) cnt, &ucycles, &usuffix);
    1169                 printf("Average\t\t%" PRIu64 "%c\n", ucycles, usuffix);
     1117                order(sum / (uint64_t) cnt, &cycles, &suffix);
     1118                printf("Average\t\t%" PRIu64 "%c\n", cycles, suffix);
    11701119        }
    11711120       
     
    11751124}
    11761125
    1177 static void list_tests(void)
    1178 {
    1179         size_t len = 0;
    1180         test_t *test;
    1181        
    1182         for (test = tests; test->name != NULL; test++) {
    1183                 if (str_length(test->name) > len)
    1184                         len = str_length(test->name);
    1185         }
    1186        
    1187         unsigned int _len = (unsigned int) len;
    1188         if ((_len != len) || (((int) _len) < 0)) {
    1189                 printf("Command length overflow\n");
    1190                 return;
    1191         }
    1192        
    1193         for (test = tests; test->name != NULL; test++)
    1194                 printf("%-*s %s%s\n", _len, test->name, test->desc,
    1195                     (test->safe ? "" : " (unsafe)"));
    1196        
    1197         printf("%-*s Run all safe tests\n", _len, "*");
    1198 }
    1199 
    1200 /** Command for listing and running kernel tests
     1126/** Command for returning kernel tests
    12011127 *
    12021128 * @param argv Argument vector.
    12031129 *
    12041130 * return Always 1.
    1205  *
    12061131 */
    12071132int cmd_test(cmd_arg_t *argv)
     
    12171142                        }
    12181143                }
    1219         } else if (str_cmp((char *) argv->buffer, "") != 0) {
     1144        } else {
    12201145                bool fnd = false;
    12211146               
     
    12301155                if (!fnd)
    12311156                        printf("Unknown test\n");
    1232         } else
    1233                 list_tests();
     1157        }
    12341158       
    12351159        return 1;
Note: See TracChangeset for help on using the changeset viewer.