Subversion Repositories HelenOS-historic

Rev

Rev 594 | Rev 601 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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