Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
510 jermar 1
/*
2
 * Copyright (C) 2005 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
518 jermar 29
#include <console/kconsole.h>
510 jermar 30
#include <console/console.h>
31
#include <console/chardev.h>
32
#include <print.h>
517 jermar 33
#include <panic.h>
510 jermar 34
#include <typedefs.h>
35
#include <arch/types.h>
517 jermar 36
#include <list.h>
37
#include <arch.h>
38
#include <func.h>
39
#include <macros.h>
518 jermar 40
#include <debug.h>
582 palkovsky 41
#include <symtab.h>
510 jermar 42
 
517 jermar 43
#define MAX_CMDLINE 256
44
 
45
/** Simple kernel console.
46
 *
47
 * The console is realized by kernel thread kconsole.
518 jermar 48
 * It doesn't understand any useful command on its own,
49
 * but makes it possible for other kernel subsystems to
517 jermar 50
 * register their own commands.
51
 */
52
 
53
/** Locking.
54
 *
55
 * There is a list of cmd_info_t structures. This list
56
 * is protected by cmd_lock spinlock. Note that specially
57
 * the link elements of cmd_info_t are protected by
58
 * this lock.
59
 *
60
 * Each cmd_info_t also has its own lock, which protects
61
 * all elements thereof except the link element.
62
 *
63
 * cmd_lock must be acquired before any cmd_info lock.
64
 * When locking two cmd info structures, structure with
65
 * lower address must be locked first.
66
 */
67
 
68
spinlock_t cmd_lock;    /**< Lock protecting command list. */
69
link_t cmd_head;    /**< Command list. */
70
 
71
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
518 jermar 72
static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end);
517 jermar 73
 
518 jermar 74
/** Data and methods for 'help' command. */
75
static int cmd_help(cmd_arg_t *argv);
582 palkovsky 76
static cmd_info_t help_info = {
77
    .name = "help",
78
    .description = "List of supported commands.",
79
    .func = cmd_help,
80
    .argc = 0
81
};
517 jermar 82
 
518 jermar 83
/** Data and methods for 'description' command. */
84
static int cmd_desc(cmd_arg_t *argv);
85
static void desc_help(void);
86
static char desc_buf[MAX_CMDLINE+1];
87
static cmd_arg_t desc_argv = {
88
    .type = ARG_TYPE_STRING,
89
    .buffer = desc_buf,
90
    .len = sizeof(desc_buf)
91
};
582 palkovsky 92
static cmd_info_t desc_info = {
93
    .name = "describe",
94
    .description = "Describe specified command.",
95
    .help = desc_help,
96
    .func = cmd_desc,
97
    .argc = 1,
98
    .argv = &desc_argv
99
};
518 jermar 100
 
582 palkovsky 101
/** Data and methods for 'symaddr' command. */
102
static int cmd_symaddr(cmd_arg_t *argv);
103
static char symaddr_buf[MAX_CMDLINE+1];
104
static cmd_arg_t symaddr_argv = {
105
    .type = ARG_TYPE_STRING,
106
    .buffer = symaddr_buf,
107
    .len = sizeof(symaddr_buf)
108
};
109
static cmd_info_t symaddr_info = {
110
    .name = "symaddr",
111
    .description = "Return symbol address.",
112
    .func = cmd_symaddr,
113
    .argc = 1,
114
    .argv = &symaddr_argv
115
};
116
 
117
/** Call0 - call function with no parameters */
118
static char call0_buf[MAX_CMDLINE+1];
119
 
120
static int cmd_call0(cmd_arg_t *argv);
121
static cmd_arg_t call0_argv = {
122
    .type = ARG_TYPE_STRING,
123
    .buffer = call0_buf,
124
    .len = sizeof(call0_buf)
125
};
126
static cmd_info_t call0_info = {
127
    .name = "call0",
128
    .description = "call0 <function> -> call function().",
129
    .func = cmd_call0,
130
    .argc = 1,
131
    .argv = &call0_argv
132
};
133
 
134
static int cmd_call1(cmd_arg_t *argv);
135
static cmd_arg_t call1_argv[] = {
136
    {
137
        .type = ARG_TYPE_STRING,
138
        .buffer = call0_buf,
139
        .len = sizeof(call0_buf)
140
    },
141
    { .type = ARG_TYPE_INT }
142
};
143
static cmd_info_t call1_info = {
144
    .name = "call1",
145
    .description = "call1 <function> <arg1> -> call function(arg1).",
146
    .func = cmd_call1,
147
    .argc = 2,
148
    .argv = call1_argv
149
};
150
 
151
static int cmd_call2(cmd_arg_t *argv);
152
static cmd_arg_t call2_argv[] = {
153
    {
154
        .type = ARG_TYPE_STRING,
155
        .buffer = call0_buf,
156
        .len = sizeof(call0_buf)
157
    },
158
    { .type = ARG_TYPE_INT },
159
    { .type = ARG_TYPE_INT }
160
};
161
static cmd_info_t call2_info = {
162
    .name = "call2",
163
    .description = "call2 <function> <arg1> <arg2> -> call function(arg1,arg2).",
164
    .func = cmd_call2,
165
    .argc = 3,
166
    .argv = call2_argv
167
};
168
 
169
 
544 decky 170
/** Data and methods for 'halt' command. */
171
static int cmd_halt(cmd_arg_t *argv);
582 palkovsky 172
static cmd_info_t halt_info = {
173
    .name = "halt",
174
    .description = "Halt the kernel.",
175
    .func = cmd_halt,
176
    .argc = 0
177
};
544 decky 178
 
517 jermar 179
/** Initialize kconsole data structures. */
180
void kconsole_init(void)
181
{
552 palkovsky 182
    spinlock_initialize(&cmd_lock, "kconsole_cmd");
517 jermar 183
    list_initialize(&cmd_head);
184
 
552 palkovsky 185
    spinlock_initialize(&help_info.lock, "kconsole_help");
517 jermar 186
    link_initialize(&help_info.link);
518 jermar 187
    if (!cmd_register(&help_info))
188
        panic("could not register command %s\n", help_info.name);
189
 
190
 
552 palkovsky 191
    spinlock_initialize(&desc_info.lock, "kconsole_desc");
518 jermar 192
    link_initialize(&desc_info.link);
193
    if (!cmd_register(&desc_info))
194
        panic("could not register command %s\n", desc_info.name);
544 decky 195
 
582 palkovsky 196
    spinlock_initialize(&symaddr_info.lock, "kconsole_symaddr");
197
    link_initialize(&symaddr_info.link);
198
    if (!cmd_register(&symaddr_info))
199
        panic("could not register command %s\n", symaddr_info.name);
200
 
201
    spinlock_initialize(&call0_info.lock, "kconsole_call0");
202
    link_initialize(&call0_info.link);
203
    if (!cmd_register(&call0_info))
204
        panic("could not register command %s\n", call0_info.name);
205
 
206
    spinlock_initialize(&call1_info.lock, "kconsole_call1");
207
    link_initialize(&call1_info.link);
208
    if (!cmd_register(&call1_info))
209
        panic("could not register command %s\n", call1_info.name);
210
 
211
 
212
    spinlock_initialize(&call2_info.lock, "kconsole_call2");
213
    link_initialize(&call2_info.link);
214
    if (!cmd_register(&call2_info))
215
        panic("could not register command %s\n", call2_info.name);
544 decky 216
 
552 palkovsky 217
    spinlock_initialize(&halt_info.lock, "kconsole_halt");
544 decky 218
    link_initialize(&halt_info.link);
219
    if (!cmd_register(&halt_info))
220
        panic("could not register command %s\n", halt_info.name);
517 jermar 221
}
222
 
223
 
224
/** Register kconsole command.
225
 *
226
 * @param cmd Structure describing the command.
227
 *
228
 * @return 0 on failure, 1 on success.
229
 */
230
int cmd_register(cmd_info_t *cmd)
231
{
232
    ipl_t ipl;
233
    link_t *cur;
234
 
235
    spinlock_lock(&cmd_lock);
236
 
237
    /*
238
     * Make sure the command is not already listed.
239
     */
240
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
241
        cmd_info_t *hlp;
242
 
243
        hlp = list_get_instance(cur, cmd_info_t, link);
244
 
245
        if (hlp == cmd) {
246
            /* The command is already there. */
247
            spinlock_unlock(&cmd_lock);
248
            return 0;
249
        }
250
 
251
        /* Avoid deadlock. */
252
        if (hlp < cmd) {
253
            spinlock_lock(&hlp->lock);
254
            spinlock_lock(&cmd->lock);
255
        } else {
256
            spinlock_lock(&cmd->lock);
257
            spinlock_lock(&hlp->lock);
258
        }
259
 
518 jermar 260
        if ((strncmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) {
517 jermar 261
            /* The command is already there. */
262
            spinlock_unlock(&hlp->lock);
263
            spinlock_unlock(&cmd->lock);
264
            spinlock_unlock(&cmd_lock);
265
            return 0;
266
        }
267
 
268
        spinlock_unlock(&hlp->lock);
269
        spinlock_unlock(&cmd->lock);
270
    }
271
 
272
    /*
273
     * Now the command can be added.
274
     */
275
    list_append(&cmd->link, &cmd_head);
276
 
277
    spinlock_unlock(&cmd_lock);
278
    return 1;
279
}
280
 
510 jermar 281
/** Kernel console managing thread.
282
 *
283
 * @param arg Not used.
284
 */
285
void kconsole(void *arg)
286
{
517 jermar 287
    char cmdline[MAX_CMDLINE+1];
288
    cmd_info_t *cmd_info;
289
    count_t len;
510 jermar 290
 
291
    if (!stdin) {
292
        printf("%s: no stdin\n", __FUNCTION__);
293
        return;
294
    }
295
 
296
    while (true) {
297
        printf("%s> ", __FUNCTION__);
518 jermar 298
        if (!(len = gets(stdin, cmdline, sizeof(cmdline))))
299
            continue;
517 jermar 300
        cmdline[len] = '\0';
301
        cmd_info = parse_cmdline(cmdline, len);
518 jermar 302
        if (!cmd_info)
517 jermar 303
            continue;
304
        (void) cmd_info->func(cmd_info->argv);
510 jermar 305
    }
306
}
517 jermar 307
 
308
/** Parse command line.
309
 *
310
 * @param cmdline Command line as read from input device.
311
 * @param len Command line length.
312
 *
313
 * @return Structure describing the command.
314
 */
315
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
316
{
317
    index_t start = 0, end = 0;
318
    cmd_info_t *cmd = NULL;
319
    link_t *cur;
320
    ipl_t ipl;
321
    int i;
322
 
518 jermar 323
    if (!parse_argument(cmdline, len, &start, &end)) {
517 jermar 324
        /* Command line did not contain alphanumeric word. */
325
        return NULL;
326
    }
327
 
328
    spinlock_lock(&cmd_lock);
329
 
330
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
331
        cmd_info_t *hlp;
332
 
333
        hlp = list_get_instance(cur, cmd_info_t, link);
334
        spinlock_lock(&hlp->lock);
335
 
518 jermar 336
        if (strncmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) {
517 jermar 337
            cmd = hlp;
338
            break;
339
        }
340
 
341
        spinlock_unlock(&hlp->lock);
342
    }
343
 
344
    spinlock_unlock(&cmd_lock);
345
 
346
    if (!cmd) {
347
        /* Unknown command. */
518 jermar 348
        printf("Unknown command.\n");
517 jermar 349
        return NULL;
350
    }
351
 
352
    /* cmd == hlp is locked */
353
 
354
    /*
355
     * The command line must be further analyzed and
356
     * the parameters therefrom must be matched and
357
     * converted to those specified in the cmd info
358
     * structure.
359
     */
518 jermar 360
 
361
    for (i = 0; i < cmd->argc; i++) {
362
        char *buf;
363
        start = end + 1;
364
        if (!parse_argument(cmdline, len, &start, &end)) {
365
            printf("Too few arguments.\n");
366
            spinlock_unlock(&cmd->lock);
367
            return NULL;
368
        }
369
 
370
        switch (cmd->argv[i].type) {
582 palkovsky 371
        case ARG_TYPE_STRING:
518 jermar 372
                buf = cmd->argv[i].buffer;
373
                strncpy(buf, (const char *) &cmdline[start], min((end - start) + 1, cmd->argv[i].len - 1));
374
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0';
375
            break;
582 palkovsky 376
        case ARG_TYPE_INT: {
377
            char symname[MAX_SYMBOL_NAME];
378
            __address symaddr;
379
 
380
            /* If we get a name, try to find it in symbol table */
381
            if (cmdline[start] < '0' | cmdline[start] > '9') {
382
                strncpy(symname, cmdline+start, min((end-start) + 1, MAX_SYMBOL_NAME -1 ));
383
                symaddr = get_symbol_addr(symname);
384
                if (!symaddr) {
385
                    printf("Symbol %s not found.\n",symname);
386
                    return NULL;
387
                }
388
                if (symaddr == (__address) -1) {
389
                    printf("Duplicate symbol %s.\n",symname);
390
                    symtab_print_search(symname);
391
                    return NULL;
392
                }
393
                cmd->argv[i].intval = *((__native *)symaddr);
394
            } else /* It's a number - convert it */
395
                cmd->argv[i].intval = atoi(cmdline+start);
518 jermar 396
            break;
582 palkovsky 397
            }
398
        case ARG_TYPE_INVALID:
399
        default:
400
            printf("invalid argument type\n");
401
            return NULL;
402
            break;
518 jermar 403
        }
404
    }
517 jermar 405
 
518 jermar 406
    start = end + 1;
407
    if (parse_argument(cmdline, len, &start, &end)) {
408
        printf("Too many arguments.\n");
409
        spinlock_unlock(&cmd->lock);
410
        return NULL;
411
    }
412
 
517 jermar 413
    spinlock_unlock(&cmd->lock);
414
    return cmd;
415
}
416
 
518 jermar 417
/** Parse argument.
418
 *
419
 * Find start and end positions of command line argument.
420
 *
421
 * @param cmdline Command line as read from the input device.
422
 * @param len Number of characters in cmdline.
423
 * @param start On entry, 'start' contains pointer to the index
424
 *        of first unprocessed character of cmdline.
425
 *        On successful exit, it marks beginning of the next argument.
426
 * @param end Undefined on entry. On exit, 'end' points to the last character
427
 *        of the next argument.
428
 *
429
 * @return false on failure, true on success.
430
 */
431
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
432
{
433
    int i;
434
    bool found_start = false;
435
 
436
    ASSERT(start != NULL);
437
    ASSERT(end != NULL);
438
 
439
    for (i = *start; i < len; i++) {
440
        if (!found_start) {
441
            if (is_white(cmdline[i]))
442
                (*start)++;
443
            else
444
                found_start = true;
445
        } else {
446
            if (is_white(cmdline[i]))
447
                break;
448
        }
449
    }
450
    *end = i - 1;
451
 
452
    return found_start;
453
}
454
 
455
 
517 jermar 456
/** List supported commands.
457
 *
518 jermar 458
 * @param argv Argument vector.
517 jermar 459
 *
460
 * @return 0 on failure, 1 on success.
461
 */
518 jermar 462
int cmd_help(cmd_arg_t *argv)
517 jermar 463
{
464
    link_t *cur;
465
    ipl_t ipl;
466
 
467
    spinlock_lock(&cmd_lock);
468
 
469
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
470
        cmd_info_t *hlp;
471
 
472
        hlp = list_get_instance(cur, cmd_info_t, link);
473
        spinlock_lock(&hlp->lock);
474
 
518 jermar 475
        printf("%s - %s\n", hlp->name, hlp->description);
517 jermar 476
 
477
        spinlock_unlock(&hlp->lock);
478
    }
479
 
480
    spinlock_unlock(&cmd_lock);
481
 
482
    return 1;
483
}
518 jermar 484
 
485
/** Describe specified command.
486
 *
487
 * @param argv Argument vector.
488
 *
489
 * @return 0 on failure, 1 on success.
490
 */
491
int cmd_desc(cmd_arg_t *argv)
492
{
493
    link_t *cur;
494
    ipl_t ipl;
495
 
496
    spinlock_lock(&cmd_lock);
497
 
498
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
499
        cmd_info_t *hlp;
500
 
501
        hlp = list_get_instance(cur, cmd_info_t, link);
502
        spinlock_lock(&hlp->lock);
503
 
504
        if (strncmp(hlp->name, (const char *) argv->buffer, strlen(hlp->name)) == 0) {
505
            printf("%s - %s\n", hlp->name, hlp->description);
506
            if (hlp->help)
507
                hlp->help();
508
            spinlock_unlock(&hlp->lock);
509
            break;
510
        }
511
 
512
        spinlock_unlock(&hlp->lock);
513
    }
514
 
515
    spinlock_unlock(&cmd_lock);
516
 
517
    return 1;
518
}
519
 
582 palkovsky 520
/** Search symbol table */
521
int cmd_symaddr(cmd_arg_t *argv)
522
{
523
    __address symaddr;
524
    char *symbol;
525
 
526
    symtab_print_search(argv->buffer);
527
 
528
    return 1;
529
}
530
 
531
/** Call function with zero parameters */
532
int cmd_call0(cmd_arg_t *argv)
533
{
534
    __address symaddr;
535
    char *symbol;
536
    __native (*f)(void);
537
 
538
    symaddr = get_symbol_addr(argv->buffer);
539
    if (!symaddr)
540
        printf("Symbol not found.\n");
541
    else if (symaddr == (__address) -1) {
542
        symtab_print_search(argv->buffer);
543
        printf("Duplicate symbol, be more specific.\n");
544
    } else {
545
        symbol = get_symtab_entry(symaddr);
546
        printf("Calling f(): 0x%p: %s\n", symaddr, symbol);
547
        f =  (__native (*)(void)) symaddr;
548
        printf("Result: 0x%X\n", f());
549
    }
550
 
551
    return 1;
552
}
553
 
554
/** Call function with one parameter */
555
int cmd_call1(cmd_arg_t *argv)
556
{
557
    __address symaddr;
558
    char *symbol;
559
    __native (*f)(__native);
560
    __native arg1 = argv[1].intval;
561
 
562
    symaddr = get_symbol_addr(argv->buffer);
563
    if (!symaddr)
564
        printf("Symbol not found.\n");
565
    else if (symaddr == (__address) -1) {
566
        symtab_print_search(argv->buffer);
567
        printf("Duplicate symbol, be more specific.\n");
568
    } else {
569
        symbol = get_symtab_entry(symaddr);
570
        printf("Calling f(0x%x): 0x%p: %s\n", arg1, symaddr, symbol);
571
        f =  (__native (*)(__native)) symaddr;
572
        printf("Result: 0x%x\n", f(arg1));
573
    }
574
 
575
    return 1;
576
}
577
 
578
/** Call function with two parameters */
579
int cmd_call2(cmd_arg_t *argv)
580
{
581
    __address symaddr;
582
    char *symbol;
583
    __native (*f)(__native);
584
    __native arg1 = argv[1].intval;
585
    __native arg2 = argv[2].intval;
586
 
587
    symaddr = get_symbol_addr(argv->buffer);
588
    if (!symaddr)
589
        printf("Symbol not found.\n");
590
    else if (symaddr == (__address) -1) {
591
        symtab_print_search(argv->buffer);
592
        printf("Duplicate symbol, be more specific.\n");
593
    } else {
594
        symbol = get_symtab_entry(symaddr);
595
        printf("Calling f(0x%x,0x%x): 0x%p: %s\n",
596
               arg1, arg2, symaddr, symbol);
597
        f =  (__native (*)(__native)) symaddr;
598
        printf("Result: 0x%x\n", f(arg1));
599
    }
600
 
601
    return 1;
602
}
603
 
604
 
548 jermar 605
/** Print detailed description of 'describe' command. */
606
void desc_help(void)
607
{
608
    printf("Syntax: describe command_name\n");
609
}
610
 
544 decky 611
/** Halt the kernel.
612
 *
548 jermar 613
 * @param argv Argument vector (ignored).
544 decky 614
 *
548 jermar 615
 * @return 0 on failure, 1 on success (never returns).
544 decky 616
 */
617
int cmd_halt(cmd_arg_t *argv)
618
{
619
    halt();
620
    return 1;
621
}