/kernel/trunk/generic/include/func.h |
---|
30,11 → 30,13 |
#define __FUNC_H__ |
#include <arch/types.h> |
#include <typedefs.h> |
extern __u32 haltstate; |
extern void halt(void); |
extern int strcmp(const char *src, const char *dst); |
extern size_t strlen(const char *str); |
extern int strcmp(const char *src, const char *dst, size_t len); |
#endif |
/kernel/trunk/generic/include/main/kconsole.h |
---|
29,6 → 29,40 |
#ifndef __KCONSOLE_H__ |
#define __KCONSOLE_H__ |
#include <typedefs.h> |
#include <list.h> |
#include <synch/spinlock.h> |
enum cmd_arg_type { |
ARG_TYPE_INVALID = 0, |
ARG_TYPE_INT, |
ARG_TYPE_STRING |
}; |
/** Structure representing one argument of kconsole command line. */ |
struct cmd_arg { |
cmd_arg_type_t type; /**< Type descriptor. */ |
void *buffer; /**< Buffer where to store data. */ |
size_t buflen; /**< Size of the buffer. */ |
}; |
/** Structure representing one kconsole command. */ |
struct cmd_info { |
link_t link; /**< Command list link. */ |
spinlock_t lock; /**< This lock protects everything below. */ |
const char *name; /**< Command name. */ |
const char *description; /**< Textual description. */ |
int (* func)(cmd_arg_t *cmd); /**< Function implementing the command. */ |
count_t argc; /**< Number of arguments. */ |
cmd_arg_t *argv; /**< Argument vector. */ |
}; |
extern spinlock_t cmd_lock; |
extern link_t cmd_head; |
extern void kconsole_init(void); |
extern void kconsole(void *arg); |
extern int cmd_register(cmd_info_t *cmd); |
#endif |
/kernel/trunk/generic/include/console/console.h |
---|
35,6 → 35,6 |
extern chardev_t *stdin; |
extern __u8 getc(chardev_t *chardev); |
extern void gets(chardev_t *chardev, __u8 *buf, size_t buflen); |
extern count_t gets(chardev_t *chardev, char *buf, size_t buflen); |
#endif /* __CHARDEV_H__ */ |
/kernel/trunk/generic/include/typedefs.h |
---|
81,4 → 81,8 |
typedef struct chardev chardev_t; |
typedef enum cmd_arg_type cmd_arg_type_t; |
typedef struct cmd_arg cmd_arg_t; |
typedef struct cmd_info cmd_info_t; |
#endif |
/kernel/trunk/generic/include/macros.h |
---|
0,0 → 1,39 |
/* |
* Copyright (C) 2005 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
#ifndef __MACROS_H__ |
#define __MACROS_H__ |
#define is_digit(d) (((d) >= '0') && ((d)<='9')) |
#define is_lower(c) (((c) >= 'a') && ((c) <= 'z')) |
#define is_upper(c) (((c) >= 'A') && ((c) <= 'Z')) |
#define is_alpha(c) (is_lower(c) || is_upper(c)) |
#define is_alphanum(c) (is_alpha(c) || is_digit(c)) |
#define is_white(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r')) |
#endif |
/kernel/trunk/generic/src/console/console.c |
---|
44,10 → 44,12 |
* of newline character. |
* |
* @param chardev Character device. |
* @param string Buffer where to store string terminated by '\0'. |
* @param buf Buffer where to store string terminated by '\0'. |
* @param len Size of the buffer. |
* |
* @return Number of characters read. |
*/ |
void gets(chardev_t *chardev, __u8 *string, size_t buflen) |
count_t gets(chardev_t *chardev, char *buf, size_t buflen) |
{ |
index_t index = 0; |
char ch; |
55,12 → 57,12 |
while (index < buflen) { |
ch = getc(chardev); |
if (ch == '\n') { /* end of string => write 0, return */ |
string[index] = '\0'; |
return; |
buf[index] = '\0'; |
return (count_t) index; |
} |
string[index++] = ch; |
buf[index++] = ch; |
} |
return; |
return (count_t) index; |
} |
/** Get character from character device. |
/kernel/trunk/generic/src/main/kinit.c |
---|
41,8 → 41,10 |
#include <mm/page.h> |
#include <arch/mm/page.h> |
#include <mm/vm.h> |
#include <mm/frame.h> |
#include <print.h> |
#include <memstr.h> |
#include <console/console.h> |
#ifdef CONFIG_SMP |
#include <arch/smp/mps.h> |
55,8 → 57,8 |
#include <test.h> |
#endif /* CONFIG_TEST */ |
#include <mm/frame.h> |
void kinit(void *arg) |
{ |
vm_t *m; |
168,9 → 170,11 |
test(); |
#endif /* CONFIG_TEST */ |
while (1) { |
thread_sleep(1); |
printf("kinit... "); |
if (!stdin) { |
while (1) { |
thread_sleep(1); |
printf("kinit... "); |
} |
} |
} |
/kernel/trunk/generic/src/main/main.c |
---|
36,6 → 36,7 |
#include <proc/thread.h> |
#include <proc/task.h> |
#include <main/kinit.h> |
#include <main/kconsole.h> |
#include <cpu.h> |
#include <align.h> |
157,6 → 158,13 |
the_initialize(THE); |
/* |
* kconsole data structures must be initialized very early |
* because other subsystems will register their respective |
* commands. |
*/ |
kconsole_init(); |
arch_pre_mm_init(); |
early_heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, heap_size + heap_delta); |
frame_init(); |
/kernel/trunk/generic/src/main/kconsole.c |
---|
30,9 → 30,128 |
#include <console/console.h> |
#include <console/chardev.h> |
#include <print.h> |
#include <panic.h> |
#include <typedefs.h> |
#include <arch/types.h> |
#include <list.h> |
#include <arch.h> |
#include <func.h> |
#include <macros.h> |
#define MAX_CMDLINE 256 |
/** Simple kernel console. |
* |
* The console is realized by kernel thread kconsole. |
* It doesn't understand any commands on its own, but |
* makes it possible for other kernel subsystems to |
* register their own commands. |
*/ |
/** Locking. |
* |
* There is a list of cmd_info_t structures. This list |
* is protected by cmd_lock spinlock. Note that specially |
* the link elements of cmd_info_t are protected by |
* this lock. |
* |
* Each cmd_info_t also has its own lock, which protects |
* all elements thereof except the link element. |
* |
* cmd_lock must be acquired before any cmd_info lock. |
* When locking two cmd info structures, structure with |
* lower address must be locked first. |
*/ |
spinlock_t cmd_lock; /**< Lock protecting command list. */ |
link_t cmd_head; /**< Command list. */ |
static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
static int cmd_help(cmd_arg_t *cmd); |
static cmd_info_t help_info; |
/** Initialize kconsole data structures. */ |
void kconsole_init(void) |
{ |
spinlock_initialize(&cmd_lock); |
list_initialize(&cmd_head); |
help_info.name = "help"; |
help_info.description = "List supported commands."; |
help_info.func = cmd_help; |
help_info.argc = 0; |
help_info.argv = NULL; |
spinlock_initialize(&help_info.lock); |
link_initialize(&help_info.link); |
if (!cmd_register(&help_info)) |
panic("could not register command\n"); |
} |
/** Register kconsole command. |
* |
* @param cmd Structure describing the command. |
* |
* @return 0 on failure, 1 on success. |
*/ |
int cmd_register(cmd_info_t *cmd) |
{ |
ipl_t ipl; |
link_t *cur; |
ipl = interrupts_disable(); |
spinlock_lock(&cmd_lock); |
/* |
* Make sure the command is not already listed. |
*/ |
for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
cmd_info_t *hlp; |
hlp = list_get_instance(cur, cmd_info_t, link); |
if (hlp == cmd) { |
/* The command is already there. */ |
spinlock_unlock(&cmd_lock); |
interrupts_restore(ipl); |
return 0; |
} |
/* Avoid deadlock. */ |
if (hlp < cmd) { |
spinlock_lock(&hlp->lock); |
spinlock_lock(&cmd->lock); |
} else { |
spinlock_lock(&cmd->lock); |
spinlock_lock(&hlp->lock); |
} |
if ((strcmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) { |
/* The command is already there. */ |
spinlock_unlock(&hlp->lock); |
spinlock_unlock(&cmd->lock); |
spinlock_unlock(&cmd_lock); |
interrupts_restore(ipl); |
return 0; |
} |
spinlock_unlock(&hlp->lock); |
spinlock_unlock(&cmd->lock); |
} |
/* |
* Now the command can be added. |
*/ |
list_append(&cmd->link, &cmd_head); |
spinlock_unlock(&cmd_lock); |
interrupts_restore(ipl); |
return 1; |
} |
/** Kernel console managing thread. |
* |
* @param arg Not used. |
39,7 → 158,9 |
*/ |
void kconsole(void *arg) |
{ |
__u8 buf[CHARDEV_BUFLEN]; |
char cmdline[MAX_CMDLINE+1]; |
cmd_info_t *cmd_info; |
count_t len; |
if (!stdin) { |
printf("%s: no stdin\n", __FUNCTION__); |
48,7 → 169,118 |
while (true) { |
printf("%s> ", __FUNCTION__); |
gets(stdin, buf, sizeof(buf)); |
printf("?\n"); |
len = gets(stdin, cmdline, sizeof(cmdline)); |
cmdline[len] = '\0'; |
cmd_info = parse_cmdline(cmdline, len); |
if (!cmd_info) { |
printf("?\n"); |
continue; |
} |
(void) cmd_info->func(cmd_info->argv); |
} |
} |
/** Parse command line. |
* |
* @param cmdline Command line as read from input device. |
* @param len Command line length. |
* |
* @return Structure describing the command. |
*/ |
cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
{ |
index_t start = 0, end = 0; |
bool found_start = false; |
cmd_info_t *cmd = NULL; |
link_t *cur; |
ipl_t ipl; |
int i; |
for (i = 0; i < len; i++) { |
if (!found_start) { |
if (is_white(cmdline[i])) |
start++; |
else |
found_start = true; |
} else { |
if (is_white(cmdline[i])) |
break; |
} |
} |
end = i - 1; |
if (!found_start) { |
/* Command line did not contain alphanumeric word. */ |
return NULL; |
} |
ipl = interrupts_disable(); |
spinlock_lock(&cmd_lock); |
for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
cmd_info_t *hlp; |
hlp = list_get_instance(cur, cmd_info_t, link); |
spinlock_lock(&hlp->lock); |
if (strcmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) { |
cmd = hlp; |
break; |
} |
spinlock_unlock(&hlp->lock); |
} |
spinlock_unlock(&cmd_lock); |
if (!cmd) { |
/* Unknown command. */ |
interrupts_restore(ipl); |
return NULL; |
} |
/* cmd == hlp is locked */ |
/* |
* TODO: |
* The command line must be further analyzed and |
* the parameters therefrom must be matched and |
* converted to those specified in the cmd info |
* structure. |
*/ |
spinlock_unlock(&cmd->lock); |
interrupts_restore(ipl); |
return cmd; |
} |
/** List supported commands. |
* |
* @param cmd Argument vector. |
* |
* @return 0 on failure, 1 on success. |
*/ |
int cmd_help(cmd_arg_t *cmd) |
{ |
link_t *cur; |
ipl_t ipl; |
ipl = interrupts_disable(); |
spinlock_lock(&cmd_lock); |
for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
cmd_info_t *hlp; |
hlp = list_get_instance(cur, cmd_info_t, link); |
spinlock_lock(&hlp->lock); |
printf("%s\t%s\n", hlp->name, hlp->description); |
spinlock_unlock(&hlp->lock); |
} |
spinlock_unlock(&cmd_lock); |
interrupts_restore(ipl); |
return 1; |
} |
/kernel/trunk/generic/src/lib/func.c |
---|
31,6 → 31,7 |
#include <cpu.h> |
#include <arch/asm.h> |
#include <arch.h> |
#include <typedefs.h> |
__u32 haltstate = 0; /**< Halt flag */ |
51,26 → 52,43 |
cpu_halt(); |
} |
/** Return number of characters in a string. |
* |
* @param str NULL terminated string. |
* |
* @return Number of characters in str. |
*/ |
size_t strlen(const char *str) |
{ |
int i; |
for (i = 0; str[i]; i++) |
; |
return i; |
} |
/** Compare two NULL terminated strings |
* |
* Do a char-by-char comparment of two NULL terminated strings. |
* The strings are considered equal iff they have the same |
* length and consist of the same characters. |
* Do a char-by-char comparison of two NULL terminated strings. |
* The strings are considered equal iff they consist of the same |
* characters on the minimum of their lengths and specified maximal |
* length. |
* |
* @param src First string to compare. |
* @param dst Second string to compare. |
* @param len Maximal length for comparison. |
* |
* @return 0 if the strings are equal, 1 otherwise. |
* |
*/ |
int strcmp(const char *src, const char *dst) |
int strcmp(const char *src, const char *dst, size_t len) |
{ |
int i; |
i = 0; |
while (src[i] == dst[i]) { |
if (src[i] == '\0') |
while ((i < len) && (src[i] == dst[i])) { |
if ((i == len - 1) || (src[i] == '\0')) |
return 0; |
i++; |
} |
/kernel/trunk/arch/ia32/src/drivers/i8042.c |
---|
37,6 → 37,7 |
#include <typedefs.h> |
#include <console/chardev.h> |
#include <console/console.h> |
#include <macros.h> |
/** |
* i8042 processor driver. |
307,7 → 308,7 |
keyflags |= PRESSED_CAPSLOCK; |
break; |
default: |
letter = (ascii >= 'a') && (ascii <= 'z'); |
letter = is_lower(ascii); |
capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK); |
shift = keyflags & PRESSED_SHIFT; |
if (letter && capslock) |