Subversion Repositories HelenOS

Rev

Rev 3386 | Rev 4263 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
596 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2005 Jakub Jermar
596 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
 
1888 jermar 29
/** @addtogroup genericconsole
1702 cejka 30
 * @{
31
 */
32
 
596 jermar 33
/**
1264 jermar 34
 * @file    cmd.c
35
 * @brief   Kernel console command wrappers.
36
 *
596 jermar 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>
1474 palkovsky 44
#include <console/console.h>
596 jermar 45
#include <console/kconsole.h>
46
#include <print.h>
47
#include <panic.h>
48
#include <arch/types.h>
788 jermar 49
#include <adt/list.h>
596 jermar 50
#include <arch.h>
2227 decky 51
#include <config.h>
596 jermar 52
#include <func.h>
4153 mejdrech 53
#include <string.h>
596 jermar 54
#include <macros.h>
55
#include <debug.h>
673 jermar 56
#include <cpu.h>
596 jermar 57
#include <mm/tlb.h>
58
#include <arch/mm/tlb.h>
668 bondari 59
#include <mm/frame.h>
673 jermar 60
#include <main/version.h>
759 palkovsky 61
#include <mm/slab.h>
775 palkovsky 62
#include <proc/scheduler.h>
777 palkovsky 63
#include <proc/thread.h>
1060 palkovsky 64
#include <proc/task.h>
1573 palkovsky 65
#include <ipc/ipc.h>
1712 palkovsky 66
#include <ipc/irq.h>
4153 mejdrech 67
#include <symtab.h>
68
#include <errno.h>
596 jermar 69
 
2019 decky 70
#ifdef CONFIG_TEST
71
#include <test.h>
72
#endif
73
 
1702 cejka 74
/* Data and methods for 'help' command. */
596 jermar 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
 
2227 decky 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
 
2275 decky 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
 
1474 palkovsky 99
static int cmd_continue(cmd_arg_t *argv);
100
static cmd_info_t continue_info = {
101
    .name = "continue",
2019 decky 102
    .description = "Return console back to userspace.",
1474 palkovsky 103
    .func = cmd_continue,
104
    .argc = 0
105
};
106
 
2019 decky 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
};
2050 decky 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
};
2019 decky 151
#endif
152
 
1702 cejka 153
/* Data and methods for 'description' command. */
596 jermar 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
 
1702 cejka 171
/* Data and methods for 'symaddr' command. */
596 jermar 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
 
603 palkovsky 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
 
1702 cejka 207
/* Data and methods for 'call0' command. */
2223 decky 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];
596 jermar 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
 
2223 decky 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
 
1702 cejka 242
/* Data and methods for 'call1' command. */
596 jermar 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
 
1702 cejka 264
/* Data and methods for 'call2' command. */
596 jermar 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
 
1702 cejka 291
/* Data and methods for 'call3' command. */
596 jermar 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
 
1702 cejka 324
/* Data and methods for 'halt' command. */
596 jermar 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
 
2721 decky 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
 
1702 cejka 344
/* Data and methods for 'tlb' command. */
673 jermar 345
static int cmd_tlb(cmd_arg_t *argv);
346
cmd_info_t tlb_info = {
347
    .name = "tlb",
596 jermar 348
    .description = "Print TLB of current processor.",
349
    .help = NULL,
673 jermar 350
    .func = cmd_tlb,
596 jermar 351
    .argc = 0,
352
    .argv = NULL
353
};
354
 
777 palkovsky 355
static int cmd_threads(cmd_arg_t *argv);
356
static cmd_info_t threads_info = {
357
    .name = "threads",
1695 jermar 358
    .description = "List all threads.",
777 palkovsky 359
    .func = cmd_threads,
360
    .argc = 0
361
};
668 bondari 362
 
1060 palkovsky 363
static int cmd_tasks(cmd_arg_t *argv);
364
static cmd_info_t tasks_info = {
365
    .name = "tasks",
1695 jermar 366
    .description = "List all tasks.",
1060 palkovsky 367
    .func = cmd_tasks,
368
    .argc = 0
369
};
777 palkovsky 370
 
1060 palkovsky 371
 
775 palkovsky 372
static int cmd_sched(cmd_arg_t *argv);
373
static cmd_info_t sched_info = {
374
    .name = "scheduler",
1695 jermar 375
    .description = "List all scheduler information.",
775 palkovsky 376
    .func = cmd_sched,
377
    .argc = 0
378
};
379
 
759 palkovsky 380
static int cmd_slabs(cmd_arg_t *argv);
381
static cmd_info_t slabs_info = {
382
    .name = "slabs",
1695 jermar 383
    .description = "List slab caches.",
759 palkovsky 384
    .func = cmd_slabs,
385
    .argc = 0
386
};
387
 
1702 cejka 388
/* Data and methods for 'zones' command */
668 bondari 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
 
4153 mejdrech 397
/* Data and methods for 'ipc' command */
398
static int cmd_ipc(cmd_arg_t *argv);
399
static cmd_arg_t ipc_argv = {
1573 palkovsky 400
    .type = ARG_TYPE_INT,
401
};
4153 mejdrech 402
static cmd_info_t ipc_info = {
403
    .name = "ipc",
404
    .description = "ipc <taskid> Show IPC information of given task.",
405
    .func = cmd_ipc,
1573 palkovsky 406
    .argc = 1,
4153 mejdrech 407
    .argv = &ipc_argv
1573 palkovsky 408
};
409
 
1702 cejka 410
/* Data and methods for 'zone' command */
668 bondari 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
 
1702 cejka 424
/* Data and methods for 'cpus' command. */
673 jermar 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
};
668 bondari 434
 
1702 cejka 435
/* Data and methods for 'version' command. */
673 jermar 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
};
668 bondari 445
 
775 palkovsky 446
static cmd_info_t *basic_commands[] = {
447
    &call0_info,
2223 decky 448
    &mcall0_info,
775 palkovsky 449
    &call1_info,
450
    &call2_info,
451
    &call3_info,
1474 palkovsky 452
    &continue_info,
775 palkovsky 453
    &cpus_info,
454
    &desc_info,
2227 decky 455
    &reboot_info,
2275 decky 456
    &uptime_info,
775 palkovsky 457
    &halt_info,
458
    &help_info,
4153 mejdrech 459
    &ipc_info,
775 palkovsky 460
    &set4_info,
461
    &slabs_info,
462
    &symaddr_info,
463
    &sched_info,
777 palkovsky 464
    &threads_info,
1060 palkovsky 465
    &tasks_info,
2721 decky 466
    &physmem_info,
775 palkovsky 467
    &tlb_info,
468
    &version_info,
469
    &zones_info,
470
    &zone_info,
2019 decky 471
#ifdef CONFIG_TEST
472
    &tests_info,
473
    &test_info,
2050 decky 474
    &bench_info,
2019 decky 475
#endif
775 palkovsky 476
    NULL
477
};
673 jermar 478
 
479
 
596 jermar 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
{
2721 decky 494
    unsigned int i;
596 jermar 495
 
2721 decky 496
    for (i = 0; basic_commands[i]; i++) {
775 palkovsky 497
        cmd_initialize(basic_commands[i]);
498
        if (!cmd_register(basic_commands[i]))
4153 mejdrech 499
            printf("Cannot register command %s\n", basic_commands[i]->name);
775 palkovsky 500
    }
596 jermar 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
 
4153 mejdrech 514
    link_t *cur;
515
    size_t len = 0;
596 jermar 516
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
517
        cmd_info_t *hlp;
4153 mejdrech 518
        hlp = list_get_instance(cur, cmd_info_t, link);
596 jermar 519
 
4153 mejdrech 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;
596 jermar 528
        hlp = list_get_instance(cur, cmd_info_t, link);
4153 mejdrech 529
 
596 jermar 530
        spinlock_lock(&hlp->lock);
4153 mejdrech 531
        printf("%-*s %s\n", len, hlp->name, hlp->description);
596 jermar 532
        spinlock_unlock(&hlp->lock);
533
    }
534
 
4153 mejdrech 535
    spinlock_unlock(&cmd_lock);
536
 
596 jermar 537
    return 1;
538
}
539
 
2227 decky 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
 
2275 decky 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
 
3065 decky 569
    printf("Up %" PRIun " days, %" PRIun " hours, %" PRIun " minutes, %" PRIun " seconds\n",
2275 decky 570
        sec / 86400, (sec % 86400) / 3600, (sec % 3600) / 60, sec % 60);
571
 
572
    return 1;
573
}
574
 
596 jermar 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
{
2114 decky 612
    symtab_print_search((char *) argv->buffer);
596 jermar 613
 
614
    return 1;
615
}
616
 
617
/** Call function with zero parameters */
618
int cmd_call0(cmd_arg_t *argv)
619
{
1780 jermar 620
    uintptr_t symaddr;
596 jermar 621
    char *symbol;
4153 mejdrech 622
    unative_t (*fnc)(void);
623
    fncptr_t fptr;
624
    int rc;
596 jermar 625
 
4153 mejdrech 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);
596 jermar 633
        printf("Duplicate symbol, be more specific.\n");
4153 mejdrech 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());
596 jermar 639
    } else {
4153 mejdrech 640
        printf("No symbol information available.\n");
596 jermar 641
    }
642
    return 1;
643
}
644
 
2223 decky 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++) {
2319 decky 655
        if (!cpus[i].active)
656
            continue;
657
 
2223 decky 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);
2224 decky 666
            thread_detach(t);
2223 decky 667
        } else
668
            printf("Unable to create thread for cpu%u\n", i);
669
    }
670
 
671
    return 1;
672
}
673
 
596 jermar 674
/** Call function with one parameter */
675
int cmd_call1(cmd_arg_t *argv)
676
{
1780 jermar 677
    uintptr_t symaddr;
596 jermar 678
    char *symbol;
4153 mejdrech 679
    unative_t (*fnc)(unative_t, ...);
1780 jermar 680
    unative_t arg1 = argv[1].intval;
4153 mejdrech 681
    fncptr_t fptr;
682
    int rc;
596 jermar 683
 
4153 mejdrech 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);
596 jermar 691
        printf("Duplicate symbol, be more specific.\n");
4153 mejdrech 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));
596 jermar 696
    } else {
4153 mejdrech 697
        printf("No symbol information available.\n");
698
    }
1666 palkovsky 699
 
596 jermar 700
    return 1;
701
}
702
 
703
/** Call function with two parameters */
704
int cmd_call2(cmd_arg_t *argv)
705
{
1780 jermar 706
    uintptr_t symaddr;
596 jermar 707
    char *symbol;
4153 mejdrech 708
    unative_t (*fnc)(unative_t, unative_t, ...);
1780 jermar 709
    unative_t arg1 = argv[1].intval;
710
    unative_t arg2 = argv[2].intval;
4153 mejdrech 711
    fncptr_t fptr;
712
    int rc;
596 jermar 713
 
4153 mejdrech 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);
596 jermar 721
        printf("Duplicate symbol, be more specific.\n");
4153 mejdrech 722
    } else if (rc == EOK) {
723
        fnc = (unative_t (*)(unative_t, unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call2);
3065 decky 724
        printf("Calling f(%#" PRIxn ", %#" PRIxn "): %p: %s\n",
725
               arg1, arg2, symaddr, symbol);
4153 mejdrech 726
        printf("Result: %#" PRIxn "\n", fnc(arg1, arg2));
727
    } else {
728
        printf("No symbol information available.\n");
596 jermar 729
    }
730
    return 1;
731
}
732
 
733
/** Call function with three parameters */
734
int cmd_call3(cmd_arg_t *argv)
735
{
1780 jermar 736
    uintptr_t symaddr;
596 jermar 737
    char *symbol;
4153 mejdrech 738
    unative_t (*fnc)(unative_t, unative_t, unative_t, ...);
1780 jermar 739
    unative_t arg1 = argv[1].intval;
740
    unative_t arg2 = argv[2].intval;
741
    unative_t arg3 = argv[3].intval;
4153 mejdrech 742
    fncptr_t fptr;
743
    int rc;
744
 
745
    symbol = (char *) argv->buffer;
746
    rc = symtab_addr_lookup(symbol, &symaddr);
596 jermar 747
 
4153 mejdrech 748
    if (rc == ENOENT) {
749
        printf("Symbol %s not found.\n", symbol);
750
    } else if (rc == EOVERFLOW) {
751
        symtab_print_search(symbol);
596 jermar 752
        printf("Duplicate symbol, be more specific.\n");
4153 mejdrech 753
    } else if (rc == EOK) {
754
        fnc = (unative_t (*)(unative_t, unative_t, unative_t, ...)) arch_construct_function(&fptr, (void *) symaddr, (void *) cmd_call3);
3065 decky 755
        printf("Calling f(%#" PRIxn ",%#" PRIxn ", %#" PRIxn "): %p: %s\n",
756
               arg1, arg2, arg3, symaddr, symbol);
4153 mejdrech 757
        printf("Result: %#" PRIxn "\n", fnc(arg1, arg2, arg3));
758
    } else {
759
        printf("No symbol information available.\n");
596 jermar 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
 */
673 jermar 789
int cmd_tlb(cmd_arg_t *argv)
596 jermar 790
{
791
    tlb_print();
792
    return 1;
793
}
603 palkovsky 794
 
2721 decky 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
 
603 palkovsky 807
/** Write 4 byte value to address */
808
int cmd_set4(cmd_arg_t *argv)
809
{
4153 mejdrech 810
    uintptr_t addr;
1780 jermar 811
    uint32_t arg1 = argv[1].intval;
603 palkovsky 812
    bool pointer = false;
4153 mejdrech 813
    int rc;
603 palkovsky 814
 
815
    if (((char *)argv->buffer)[0] == '*') {
4153 mejdrech 816
        rc = symtab_addr_lookup((char *) argv->buffer + 1, &addr);
603 palkovsky 817
        pointer = true;
2114 decky 818
    } else if (((char *) argv->buffer)[0] >= '0' &&
4153 mejdrech 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
    }
603 palkovsky 825
 
4153 mejdrech 826
    if (rc == ENOENT)
603 palkovsky 827
        printf("Symbol %s not found.\n", argv->buffer);
4153 mejdrech 828
    else if (rc == EOVERFLOW) {
2114 decky 829
        symtab_print_search((char *) argv->buffer);
603 palkovsky 830
        printf("Duplicate symbol, be more specific.\n");
4153 mejdrech 831
    } else if (rc == EOK) {
603 palkovsky 832
        if (pointer)
4153 mejdrech 833
            addr = *(uintptr_t *) addr;
3065 decky 834
        printf("Writing %#" PRIx64 " -> %p\n", arg1, addr);
4153 mejdrech 835
        *(uint32_t *) addr = arg1;
836
    } else {
837
        printf("No symbol information available.\n");
603 palkovsky 838
    }
839
 
840
    return 1;
841
}
668 bondari 842
 
759 palkovsky 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
 
777 palkovsky 854
 
775 palkovsky 855
/** Command for listings Thread information
856
 *
857
 * @param argv Ignores
858
 *
859
 * @return Always 1
860
 */
777 palkovsky 861
int cmd_threads(cmd_arg_t * argv) {
862
    thread_print_list();
863
    return 1;
864
}
865
 
1060 palkovsky 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
 
777 palkovsky 877
/** Command for listings Thread information
878
 *
879
 * @param argv Ignores
880
 *
881
 * @return Always 1
882
 */
775 palkovsky 883
int cmd_sched(cmd_arg_t * argv) {
884
    sched_print_list();
885
    return 1;
886
}
887
 
677 bondari 888
/** Command for listing memory zones
889
 *
890
 * @param argv Ignored
891
 *
892
 * return Always 1
893
 */
668 bondari 894
int cmd_zones(cmd_arg_t * argv) {
676 bondari 895
    zone_print_list();
668 bondari 896
    return 1;
897
}
673 jermar 898
 
677 bondari 899
/** Command for memory zone details
900
 *
901
 * @param argv Integer argument from cmdline expected
902
 *
903
 * return Always 1
904
 */
668 bondari 905
int cmd_zone(cmd_arg_t * argv) {
676 bondari 906
    zone_print_one(argv[0].intval);
668 bondari 907
    return 1;
908
}
909
 
1573 palkovsky 910
/** Command for printing task ipc details
911
 *
912
 * @param argv Integer argument from cmdline expected
913
 *
914
 * return Always 1
915
 */
4153 mejdrech 916
int cmd_ipc(cmd_arg_t * argv) {
1573 palkovsky 917
    ipc_print_task(argv[0].intval);
918
    return 1;
919
}
920
 
921
 
673 jermar 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
}
1474 palkovsky 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
{
1695 jermar 954
    printf("The kernel will now relinquish the console.\n");
4153 mejdrech 955
    release_console();
956
 
957
    if ((kconsole_notify) && (kconsole_irq.notif_cfg.notify))
958
        ipc_irq_send_msg_0(&kconsole_irq);
959
 
1474 palkovsky 960
    return 1;
961
}
1702 cejka 962
 
2020 decky 963
#ifdef CONFIG_TEST
2019 decky 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
{
4153 mejdrech 972
    size_t len = 0;
2019 decky 973
    test_t *test;
4153 mejdrech 974
    for (test = tests; test->name != NULL; test++) {
975
        if (strlen(test->name) > len)
976
            len = strlen(test->name);
977
    }
2019 decky 978
 
979
    for (test = tests; test->name != NULL; test++)
4153 mejdrech 980
        printf("%-*s %s%s\n", len, test->name, test->desc, (test->safe ? "" : " (unsafe)"));
2019 decky 981
 
4153 mejdrech 982
    printf("%-*s Run all safe tests\n", len, "*");
2019 decky 983
    return 1;
984
}
985
 
2042 decky 986
static bool run_test(const test_t *test)
2027 decky 987
{
4153 mejdrech 988
    printf("%s (%s)\n", test->name, test->desc);
2030 decky 989
 
990
    /* Update and read thread accounting
991
       for benchmarking */
992
    ipl_t ipl = interrupts_disable();
2039 decky 993
    spinlock_lock(&TASK->lock);
994
    uint64_t t0 = task_get_accounting(TASK);
995
    spinlock_unlock(&TASK->lock);
2030 decky 996
    interrupts_restore(ipl);
997
 
998
    /* Execute the test */
2050 decky 999
    char * ret = test->entry(false);
2030 decky 1000
 
1001
    /* Update and read thread accounting */
1002
    ipl = interrupts_disable();
2039 decky 1003
    spinlock_lock(&TASK->lock);
1004
    uint64_t dt = task_get_accounting(TASK) - t0;
1005
    spinlock_unlock(&TASK->lock);
2030 decky 1006
    interrupts_restore(ipl);
1007
 
2050 decky 1008
    uint64_t cycles;
1009
    char suffix;
1010
    order(dt, &cycles, &suffix);
1011
 
3065 decky 1012
    printf("Time: %" PRIu64 "%c cycles\n", cycles, suffix);
2027 decky 1013
 
1014
    if (ret == NULL) {
1015
        printf("Test passed\n");
2042 decky 1016
        return true;
2027 decky 1017
    }
1018
 
1019
    printf("%s\n", ret);
2042 decky 1020
    return false;
2027 decky 1021
}
1022
 
2050 decky 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
 
2114 decky 1033
    uint64_t *data = (uint64_t *) malloc(sizeof(uint64_t) * cnt, 0);
2050 decky 1034
    if (data == NULL) {
1035
        printf("Error allocating memory for statistics\n");
1036
        return false;
1037
    }
1038
 
1039
    for (i = 0; i < cnt; i++) {
3065 decky 1040
        printf("%s (%u/%u) ... ", test->name, i + 1, cnt);
2050 decky 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);
3065 decky 1068
        printf("OK (%" PRIu64 "%c cycles)\n", cycles, suffix);
2050 decky 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);
3065 decky 1081
        printf("Average\t\t%" PRIu64 "%c\n", cycles, suffix);
2050 decky 1082
    }
1083
 
1084
    free(data);
1085
 
1086
    return ret;
1087
}
1088
 
2019 decky 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
 
2114 decky 1099
    if (strcmp((char *) argv->buffer, "*") == 0) {
2020 decky 1100
        for (test = tests; test->name != NULL; test++) {
1101
            if (test->safe) {
2027 decky 1102
                printf("\n");
1103
                if (!run_test(test))
1104
                    break;
2020 decky 1105
            }
2019 decky 1106
        }
2020 decky 1107
    } else {
1108
        bool fnd = false;
1109
 
1110
        for (test = tests; test->name != NULL; test++) {
2114 decky 1111
            if (strcmp(test->name, (char *) argv->buffer) == 0) {
2020 decky 1112
                fnd = true;
2027 decky 1113
                run_test(test);
2020 decky 1114
                break;
1115
            }
1116
        }
1117
 
1118
        if (!fnd)
2027 decky 1119
            printf("Unknown test\n");
2019 decky 1120
    }
1121
 
1122
    return 1;
1123
}
2050 decky 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++) {
2114 decky 1139
        if (strcmp(test->name, (char *) argv->buffer) == 0) {
2050 decky 1140
            fnd = true;
2051 decky 1141
 
1142
            if (test->safe)
1143
                run_bench(test, cnt);
1144
            else
1145
                printf("Unsafe test\n");
1146
 
2050 decky 1147
            break;
1148
        }
1149
    }
1150
 
1151
    if (!fnd)
1152
        printf("Unknown test\n");
1153
 
1154
    return 1;
1155
}
1156
 
2020 decky 1157
#endif
2019 decky 1158
 
1888 jermar 1159
/** @}
1702 cejka 1160
 */