Subversion Repositories HelenOS-historic

Rev

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

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