Subversion Repositories HelenOS

Rev

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