/branches/dd/kernel/generic/src/event/event.c |
---|
File deleted |
/branches/dd/kernel/generic/src/synch/spinlock.c |
---|
32,9 → 32,9 |
/** |
* @file |
* @brief Spinlocks. |
* @brief Spinlocks. |
*/ |
#include <synch/spinlock.h> |
#include <atomic.h> |
#include <arch/barrier.h> |
106,9 → 106,9 |
#endif |
if (i++ > DEADLOCK_THRESHOLD) { |
printf("cpu%u: looping on spinlock %" PRIp ":%s, " |
"caller=%" PRIp "(%s)", CPU->id, sl, sl->name, |
"caller=%" PRIp "(%s)\n", CPU->id, sl, sl->name, |
CALLER, symtab_fmt_name_lookup(CALLER)); |
i = 0; |
deadlock_reported = true; |
} |
/branches/dd/kernel/generic/src/main/kinit.c |
---|
186,14 → 186,14 |
char *name; |
name = init.tasks[i].name; |
if (name[0] == '\0') |
if (name[0] == 0) |
name = "<unknown>"; |
ASSERT(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN); |
strncpy(namebuf, INIT_PREFIX, TASK_NAME_BUFLEN); |
strncpy(namebuf + INIT_PREFIX_LEN, name, |
TASK_NAME_BUFLEN - INIT_PREFIX_LEN); |
str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX); |
str_cpy(namebuf + INIT_PREFIX_LEN, |
TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name); |
int rc = program_create_from_image((void *) init.tasks[i].addr, |
namebuf, &programs[i]); |
/branches/dd/kernel/generic/src/main/main.c |
---|
32,7 → 32,7 |
/** |
* @file |
* @brief Main initialization kernel function for all processors. |
* @brief Main initialization kernel function for all processors. |
* |
* During kernel boot, all processors, after architecture dependent |
* initialization, start executing code found in this file. After |
82,7 → 82,7 |
#include <smp/smp.h> |
#include <ddi/ddi.h> |
#include <main/main.h> |
#include <event/event.h> |
#include <ipc/event.h> |
/** Global configuration structure. */ |
config_t config; |
/branches/dd/kernel/generic/src/main/shutdown.c |
---|
32,10 → 32,11 |
/** |
* @file |
* @brief Shutdown procedures. |
* @brief Shutdown procedures. |
*/ |
#include <arch.h> |
#include <func.h> |
#include <print.h> |
void reboot(void) |
47,6 → 48,7 |
#endif |
arch_reboot(); |
halt(); |
} |
/** @} |
/branches/dd/kernel/generic/src/debug/symtab.c |
---|
32,7 → 32,7 |
/** |
* @file |
* @brief Kernel symbol resolver. |
* @brief Kernel symbol resolver. |
*/ |
#include <symtab.h> |
43,30 → 43,33 |
#include <typedefs.h> |
#include <errno.h> |
/** Get name of symbol that seems most likely to correspond to address. |
/** Get name of a symbol that seems most likely to correspond to address. |
* |
* @param addr Address. |
* @param name Place to store pointer to the symbol name. |
* @param addr Address. |
* @param name Place to store pointer to the symbol name. |
* |
* @return Zero on success or negative error code, ENOENT if not found, |
* ENOTSUP if symbol table not available. |
* @return Zero on success or negative error code, ENOENT if not found, |
* ENOTSUP if symbol table not available. |
* |
*/ |
int symtab_name_lookup(unative_t addr, char **name) |
{ |
#ifdef CONFIG_SYMTAB |
count_t i; |
for (i = 1; symbol_table[i].address_le; ++i) { |
for (i = 1; symbol_table[i].address_le; i++) { |
if (addr < uint64_t_le2host(symbol_table[i].address_le)) |
break; |
} |
if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) { |
*name = symbol_table[i - 1].symbol_name; |
return EOK; |
} |
*name = NULL; |
return ENOENT; |
#else |
*name = NULL; |
return ENOTSUP; |
78,21 → 81,24 |
* Returns name of closest corresponding symbol, "Not found" if none exists |
* or "N/A" if no symbol information is available. |
* |
* @param addr Address. |
* @param name Place to store pointer to the symbol name. |
* @param addr Address. |
* @param name Place to store pointer to the symbol name. |
* |
* @return Pointer to a human-readable string. |
* @return Pointer to a human-readable string. |
* |
*/ |
char *symtab_fmt_name_lookup(unative_t addr) |
{ |
int rc; |
char *name; |
rc = symtab_name_lookup(addr, &name); |
int rc = symtab_name_lookup(addr, &name); |
switch (rc) { |
case EOK: return name; |
case ENOENT: return "Not found"; |
default: return "N/A"; |
case EOK: |
return name; |
case ENOENT: |
return "Not found"; |
default: |
return "N/A"; |
} |
} |
100,95 → 106,90 |
/** Find symbols that match the parameter forward and print them. |
* |
* @param name - search string |
* @param startpos - starting position, changes to found position |
* @return Pointer to the part of string that should be completed or NULL |
* @param name Search string |
* @param startpos Starting position, changes to found position |
* |
* @return Pointer to the part of string that should be completed or NULL. |
* |
*/ |
static char * symtab_search_one(const char *name, int *startpos) |
static const char *symtab_search_one(const char *name, count_t *startpos) |
{ |
unsigned int namelen = strlen(name); |
char *curname; |
int i, j; |
int colonoffset = -1; |
for (i = 0; name[i]; i++) |
if (name[i] == ':') { |
colonoffset = i; |
break; |
} |
for (i = *startpos; symbol_table[i].address_le; ++i) { |
/* Find a ':' in name */ |
curname = symbol_table[i].symbol_name; |
for (j = 0; curname[j] && curname[j] != ':'; j++) |
; |
if (!curname[j]) |
count_t namelen = str_length(name); |
count_t pos; |
for (pos = *startpos; symbol_table[pos].address_le; pos++) { |
const char *curname = symbol_table[pos].symbol_name; |
/* Find a ':' in curname */ |
const char *colon = str_chr(curname, ':'); |
if (colon == NULL) |
continue; |
j -= colonoffset; |
curname += j; |
if (strlen(curname) < namelen) |
if (str_length(curname) < namelen) |
continue; |
if (strncmp(curname, name, namelen) == 0) { |
*startpos = i; |
return curname + namelen; |
if (str_lcmp(name, curname, namelen) == 0) { |
*startpos = pos; |
return (curname + str_lsize(curname, namelen)); |
} |
} |
return NULL; |
} |
#endif |
/** Return address that corresponds to the entry |
/** Return address that corresponds to the entry. |
* |
* Search symbol table, and if there is one match, return it |
* |
* @param name Name of the symbol |
* @param addr Place to store symbol address |
* @param name Name of the symbol |
* @param addr Place to store symbol address |
* |
* @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate |
* symbol, ENOTSUP - no symbol information available. |
* @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate |
* symbol, ENOTSUP - no symbol information available. |
* |
*/ |
int symtab_addr_lookup(const char *name, uintptr_t *addr) |
{ |
#ifdef CONFIG_SYMTAB |
count_t found = 0; |
char *hint; |
int i; |
i = 0; |
while ((hint = symtab_search_one(name, &i))) { |
if (!strlen(hint)) { |
*addr = uint64_t_le2host(symbol_table[i].address_le); |
count_t pos = 0; |
const char *hint; |
while ((hint = symtab_search_one(name, &pos))) { |
if (str_length(hint) == 0) { |
*addr = uint64_t_le2host(symbol_table[pos].address_le); |
found++; |
} |
i++; |
pos++; |
} |
if (found > 1) |
return EOVERFLOW; |
if (found < 1) |
return ENOENT; |
return EOK; |
#else |
return ENOTSUP; |
#endif |
} |
/** Find symbols that match parameter and prints them */ |
/** Find symbols that match parameter and print them */ |
void symtab_print_search(const char *name) |
{ |
#ifdef CONFIG_SYMTAB |
int i; |
uintptr_t addr; |
char *realname; |
i = 0; |
while (symtab_search_one(name, &i)) { |
addr = uint64_t_le2host(symbol_table[i].address_le); |
realname = symbol_table[i].symbol_name; |
count_t pos = 0; |
while (symtab_search_one(name, &pos)) { |
uintptr_t addr = uint64_t_le2host(symbol_table[pos].address_le); |
char *realname = symbol_table[pos].symbol_name; |
printf("%p: %s\n", addr, realname); |
i++; |
pos++; |
} |
#else |
printf("No symbol information available.\n"); |
#endif |
196,55 → 197,54 |
/** Symtab completion |
* |
* @param input - Search string, completes to symbol name |
* @returns - 0 - nothing found, 1 - success, >1 print duplicates |
* @param input Search string, completes to symbol name |
* @param size Input buffer size |
* |
* @return 0 - nothing found, 1 - success, >1 print duplicates |
* |
*/ |
int symtab_compl(char *input) |
int symtab_compl(char *input, count_t size) |
{ |
#ifdef CONFIG_SYMTAB |
char output[MAX_SYMBOL_NAME + 1]; |
int startpos = 0; |
char *foundtxt; |
int found = 0; |
int i; |
char *name = input; |
/* Allow completion of pointers */ |
if (name[0] == '*' || name[0] == '&') |
const char *name = input; |
/* Allow completion of pointers */ |
if ((name[0] == '*') || (name[0] == '&')) |
name++; |
/* Do not print everything */ |
if (!strlen(name)) |
/* Do not print all symbols */ |
if (str_length(name) == 0) |
return 0; |
output[0] = '\0'; |
while ((foundtxt = symtab_search_one(name, &startpos))) { |
startpos++; |
if (!found) |
strncpy(output, foundtxt, strlen(foundtxt) + 1); |
else { |
for (i = 0; output[i] && foundtxt[i] && |
output[i] == foundtxt[i]; i++) |
; |
output[i] = '\0'; |
} |
count_t found = 0; |
count_t pos = 0; |
const char *hint; |
char output[MAX_SYMBOL_NAME]; |
output[0] = 0; |
while ((hint = symtab_search_one(name, &pos))) { |
if ((found == 0) || (str_length(output) > str_length(hint))) |
str_cpy(output, MAX_SYMBOL_NAME, hint); |
pos++; |
found++; |
} |
if (!found) |
return 0; |
if (found > 1 && !strlen(output)) { |
if ((found > 1) && (str_length(output) != 0)) { |
printf("\n"); |
startpos = 0; |
while ((foundtxt = symtab_search_one(name, &startpos))) { |
printf("%s\n", symbol_table[startpos].symbol_name); |
startpos++; |
pos = 0; |
while ((hint = symtab_search_one(name, &pos))) { |
printf("%s\n", symbol_table[pos].symbol_name); |
pos++; |
} |
} |
strncpy(input, output, MAX_SYMBOL_NAME); |
if (found > 0) |
str_cpy(input, size, output); |
return found; |
#else |
return 0; |
#endif |
/branches/dd/kernel/generic/src/ddi/irq.c |
---|
101,11 → 101,12 |
*/ |
static index_t irq_ht_hash(unative_t *key); |
static bool irq_ht_compare(unative_t *key, count_t keys, link_t *item); |
static void irq_ht_remove(link_t *item); |
static hash_table_operations_t irq_ht_ops = { |
.hash = irq_ht_hash, |
.compare = irq_ht_compare, |
.remove_callback = NULL /* not used */ |
.remove_callback = irq_ht_remove, |
}; |
/** |
116,11 → 117,12 |
*/ |
static index_t irq_lin_hash(unative_t *key); |
static bool irq_lin_compare(unative_t *key, count_t keys, link_t *item); |
static void irq_lin_remove(link_t *item); |
static hash_table_operations_t irq_lin_ops = { |
.hash = irq_lin_hash, |
.compare = irq_lin_compare, |
.remove_callback = NULL /* not used */ |
.remove_callback = irq_lin_remove, |
}; |
/** Number of buckets in either of the hash tables. */ |
347,6 → 349,17 |
return rv; |
} |
/** Unlock IRQ structure after hash_table_remove(). |
* |
* @param lnk Link in the removed and locked IRQ structure. |
*/ |
void irq_ht_remove(link_t *lnk) |
{ |
irq_t *irq __attribute__((unused)) |
= hash_table_get_instance(lnk, irq_t, link); |
spinlock_unlock(&irq->lock); |
} |
/** Compute hash index for the key. |
* |
* This function computes hash index into |
406,5 → 419,16 |
return rv; |
} |
/** Unlock IRQ structure after hash_table_remove(). |
* |
* @param lnk Link in the removed and locked IRQ structure. |
*/ |
void irq_lin_remove(link_t *lnk) |
{ |
irq_t *irq __attribute__((unused)) |
= hash_table_get_instance(lnk, irq_t, link); |
spinlock_unlock(&irq->lock); |
} |
/** @} |
*/ |
/branches/dd/kernel/generic/src/console/console.c |
---|
41,7 → 41,7 |
#include <arch/types.h> |
#include <ddi/irq.h> |
#include <ddi/ddi.h> |
#include <event/event.h> |
#include <ipc/event.h> |
#include <ipc/irq.h> |
#include <arch.h> |
#include <func.h> |
50,12 → 50,14 |
#include <atomic.h> |
#include <syscall/copy.h> |
#include <errno.h> |
#include <string.h> |
#define KLOG_SIZE PAGE_SIZE |
#define KLOG_PAGES 4 |
#define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t)) |
#define KLOG_LATENCY 8 |
/** Kernel log cyclic buffer */ |
static wchar_t klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE))); |
static wchar_t klog[KLOG_LENGTH] __attribute__ ((aligned (PAGE_SIZE))); |
/** Kernel log initialized */ |
static bool klog_inited = false; |
93,7 → 95,6 |
void *faddr = (void *) KA2PA(klog); |
ASSERT((uintptr_t) faddr % FRAME_SIZE == 0); |
ASSERT(KLOG_SIZE % FRAME_SIZE == 0); |
klog_parea.pbase = (uintptr_t) faddr; |
klog_parea.frames = SIZE2FRAMES(sizeof(klog)); |
100,7 → 101,7 |
ddi_parea_register(&klog_parea); |
sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr); |
sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(sizeof(klog))); |
sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES); |
spinlock_lock(&klog_lock); |
klog_inited = true; |
154,7 → 155,7 |
* @return Character read. |
* |
*/ |
uint8_t _getc(indev_t *indev) |
wchar_t _getc(indev_t *indev) |
{ |
if (atomic_get(&haltstate)) { |
/* If we are here, we are hopefully on the processor that |
178,7 → 179,7 |
waitq_sleep(&indev->wq); |
ipl_t ipl = interrupts_disable(); |
spinlock_lock(&indev->lock); |
uint8_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN]; |
wchar_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN]; |
indev->counter--; |
spinlock_unlock(&indev->lock); |
interrupts_restore(ipl); |
192,7 → 193,7 |
* of newline character. |
* |
* @param indev Input character device. |
* @param buf Buffer where to store string terminated by '\0'. |
* @param buf Buffer where to store string terminated by NULL. |
* @param buflen Size of the buffer. |
* |
* @return Number of characters read. |
200,36 → 201,38 |
*/ |
count_t gets(indev_t *indev, char *buf, size_t buflen) |
{ |
index_t index = 0; |
size_t offset = 0; |
count_t count = 0; |
buf[offset] = 0; |
while (index < buflen) { |
char ch = _getc(indev); |
wchar_t ch; |
while ((ch = _getc(indev)) != '\n') { |
if (ch == '\b') { |
if (index > 0) { |
index--; |
/* Space backspace, space */ |
if (count > 0) { |
/* Space, backspace, space */ |
putchar('\b'); |
putchar(' '); |
putchar('\b'); |
count--; |
offset = str_lsize(buf, count); |
buf[offset] = 0; |
} |
continue; |
} |
putchar(ch); |
if (ch == '\n') { /* end of string => write 0, return */ |
buf[index] = '\0'; |
return (count_t) index; |
} |
buf[index++] = ch; |
if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) { |
putchar(ch); |
count++; |
buf[offset] = 0; |
} |
} |
return (count_t) index; |
return count; |
} |
/** Get character from input device & echo it to screen */ |
uint8_t getc(indev_t *indev) |
wchar_t getc(indev_t *indev) |
{ |
uint8_t ch = _getc(indev); |
wchar_t ch = _getc(indev); |
putchar(ch); |
return ch; |
} |
254,16 → 257,16 |
/* Print charaters stored in kernel log */ |
index_t i; |
for (i = klog_len - klog_stored; i < klog_len; i++) |
stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE], silent); |
stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent); |
klog_stored = 0; |
} |
/* Store character in the cyclic kernel log */ |
klog[(klog_start + klog_len) % KLOG_SIZE] = ch; |
if (klog_len < KLOG_SIZE) |
klog[(klog_start + klog_len) % KLOG_LENGTH] = ch; |
if (klog_len < KLOG_LENGTH) |
klog_len++; |
else |
klog_start = (klog_start + 1) % KLOG_SIZE; |
klog_start = (klog_start + 1) % KLOG_LENGTH; |
if ((stdout) && (stdout->op->write)) |
stdout->op->write(stdout, ch, silent); |
295,25 → 298,25 |
* Print to kernel log. |
* |
*/ |
unative_t sys_klog(int fd, const void * buf, size_t count) |
unative_t sys_klog(int fd, const void *buf, size_t size) |
{ |
char *data; |
int rc; |
if (count > PAGE_SIZE) |
if (size > PAGE_SIZE) |
return ELIMIT; |
if (count > 0) { |
data = (char *) malloc(count + 1, 0); |
if (size > 0) { |
data = (char *) malloc(size + 1, 0); |
if (!data) |
return ENOMEM; |
rc = copy_from_uspace(data, buf, count); |
rc = copy_from_uspace(data, buf, size); |
if (rc) { |
free(data); |
return rc; |
} |
data[count] = 0; |
data[size] = 0; |
printf("%s", data); |
free(data); |
320,7 → 323,7 |
} else |
klog_update(); |
return count; |
return size; |
} |
/** @} |
/branches/dd/kernel/generic/src/console/cmd.c |
---|
31,8 → 31,8 |
*/ |
/** |
* @file cmd.c |
* @brief Kernel console command wrappers. |
* @file cmd.c |
* @brief Kernel console command wrappers. |
* |
* This file is meant to contain all wrapper functions for |
* all kconsole commands. The point is in separating |
64,7 → 64,7 |
#include <proc/task.h> |
#include <ipc/ipc.h> |
#include <ipc/irq.h> |
#include <event/event.h> |
#include <ipc/event.h> |
#include <symtab.h> |
#include <errno.h> |
513,14 → 513,14 |
spinlock_lock(&cmd_lock); |
link_t *cur; |
size_t len = 0; |
count_t len = 0; |
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 (strlen(hlp->name) > len) |
len = strlen(hlp->name); |
if (str_length(hlp->name) > len) |
len = str_length(hlp->name); |
spinlock_unlock(&hlp->lock); |
} |
582,7 → 582,7 |
int cmd_desc(cmd_arg_t *argv) |
{ |
link_t *cur; |
spinlock_lock(&cmd_lock); |
for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
590,8 → 590,8 |
hlp = list_get_instance(cur, cmd_info_t, link); |
spinlock_lock(&hlp->lock); |
if (strncmp(hlp->name, (const char *) argv->buffer, strlen(hlp->name)) == 0) { |
if (str_lcmp(hlp->name, (const char *) argv->buffer, str_length(hlp->name)) == 0) { |
printf("%s - %s\n", hlp->name, hlp->description); |
if (hlp->help) |
hlp->help(); |
598,12 → 598,12 |
spinlock_unlock(&hlp->lock); |
break; |
} |
spinlock_unlock(&hlp->lock); |
} |
spinlock_unlock(&cmd_lock); |
return 1; |
} |
969,11 → 969,11 |
*/ |
int cmd_tests(cmd_arg_t *argv) |
{ |
size_t len = 0; |
count_t len = 0; |
test_t *test; |
for (test = tests; test->name != NULL; test++) { |
if (strlen(test->name) > len) |
len = strlen(test->name); |
if (str_length(test->name) > len) |
len = str_length(test->name); |
} |
for (test = tests; test->name != NULL; test++) |
996,7 → 996,8 |
interrupts_restore(ipl); |
/* Execute the test */ |
char * ret = test->entry(false); |
test_quiet = false; |
char *ret = test->entry(); |
/* Update and read thread accounting */ |
ipl = interrupts_disable(); |
1048,7 → 1049,8 |
interrupts_restore(ipl); |
/* Execute the test */ |
char * ret = test->entry(true); |
test_quiet = true; |
char * ret = test->entry(); |
/* Update and read thread accounting */ |
ipl = interrupts_disable(); |
1096,7 → 1098,7 |
{ |
test_t *test; |
if (strcmp((char *) argv->buffer, "*") == 0) { |
if (str_cmp((char *) argv->buffer, "*") == 0) { |
for (test = tests; test->name != NULL; test++) { |
if (test->safe) { |
printf("\n"); |
1108,7 → 1110,7 |
bool fnd = false; |
for (test = tests; test->name != NULL; test++) { |
if (strcmp(test->name, (char *) argv->buffer) == 0) { |
if (str_cmp(test->name, (char *) argv->buffer) == 0) { |
fnd = true; |
run_test(test); |
break; |
1133,24 → 1135,33 |
test_t *test; |
uint32_t cnt = argv[1].intval; |
bool fnd = false; |
for (test = tests; test->name != NULL; test++) { |
if (strcmp(test->name, (char *) argv->buffer) == 0) { |
fnd = true; |
if (test->safe) |
run_bench(test, cnt); |
else |
printf("Unsafe test\n"); |
break; |
if (str_cmp((char *) argv->buffer, "*") == 0) { |
for (test = tests; test->name != NULL; test++) { |
if (test->safe) { |
if (!run_bench(test, cnt)) |
break; |
} |
} |
} else { |
bool fnd = false; |
for (test = tests; test->name != NULL; test++) { |
if (str_cmp(test->name, (char *) argv->buffer) == 0) { |
fnd = true; |
if (test->safe) |
run_bench(test, cnt); |
else |
printf("Unsafe test\n"); |
break; |
} |
} |
if (!fnd) |
printf("Unknown test\n"); |
} |
if (!fnd) |
printf("Unknown test\n"); |
return 1; |
} |
/branches/dd/kernel/generic/src/console/chardev.c |
---|
59,7 → 59,7 |
* @param ch Character being pushed. |
* |
*/ |
void indev_push_character(indev_t *indev, uint8_t ch) |
void indev_push_character(indev_t *indev, wchar_t ch) |
{ |
ASSERT(indev); |
/branches/dd/kernel/generic/src/console/kconsole.c |
---|
31,10 → 31,11 |
*/ |
/** |
* @file kconsole.c |
* @brief Kernel console. |
* @file kconsole.c |
* @brief Kernel console. |
* |
* This file contains kernel thread managing the kernel console. |
* |
*/ |
#include <console/kconsole.h> |
56,6 → 57,7 |
#include <symtab.h> |
#include <errno.h> |
#include <putchar.h> |
#include <string.h> |
/** Simple kernel console. |
* |
64,7 → 66,7 |
* 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 |
79,15 → 81,13 |
* When locking two cmd info structures, structure with |
* lower address must be locked first. |
*/ |
SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
LIST_INITIALIZE(cmd_head); /**< Command list. */ |
static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
static bool parse_argument(char *cmdline, size_t len, index_t *start, |
index_t *end); |
static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
LIST_INITIALIZE(cmd_head); /**< Command list. */ |
static wchar_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
static count_t history_pos = 0; |
/** Initialize kconsole data structures |
* |
* This is the most basic initialization, almost no |
97,10 → 97,10 |
void kconsole_init(void) |
{ |
unsigned int i; |
cmd_init(); |
for (i = 0; i < KCONSOLE_HISTORY; i++) |
history[i][0] = '\0'; |
history[i][0] = 0; |
} |
/** Register kconsole command. |
107,9 → 107,10 |
* |
* @param cmd Structure describing the command. |
* |
* @return 0 on failure, 1 on success. |
* @return False on failure, true on success. |
* |
*/ |
int cmd_register(cmd_info_t *cmd) |
bool cmd_register(cmd_info_t *cmd) |
{ |
link_t *cur; |
119,16 → 120,14 |
* Make sure the command is not already listed. |
*/ |
for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
cmd_info_t *hlp; |
cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link); |
hlp = list_get_instance(cur, cmd_info_t, link); |
if (hlp == cmd) { |
/* The command is already there. */ |
spinlock_unlock(&cmd_lock); |
return 0; |
return false; |
} |
/* Avoid deadlock. */ |
if (hlp < cmd) { |
spinlock_lock(&hlp->lock); |
137,13 → 136,13 |
spinlock_lock(&cmd->lock); |
spinlock_lock(&hlp->lock); |
} |
if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name), |
strlen(hlp->name))) == 0)) { |
if (str_cmp(hlp->name, cmd->name) == 0) { |
/* The command is already there. */ |
spinlock_unlock(&hlp->lock); |
spinlock_unlock(&cmd->lock); |
spinlock_unlock(&cmd_lock); |
return 0; |
return false; |
} |
spinlock_unlock(&hlp->lock); |
156,11 → 155,11 |
list_append(&cmd->link, &cmd_head); |
spinlock_unlock(&cmd_lock); |
return 1; |
return true; |
} |
/** Print count times a character */ |
static void rdln_print_c(wchar_t ch, count_t count) |
static void print_cc(wchar_t ch, count_t count) |
{ |
count_t i; |
for (i = 0; i < count; i++) |
167,313 → 166,265 |
putchar(ch); |
} |
/** Insert character to string */ |
static void insert_char(char *str, char ch, int pos) |
/** Try to find a command beginning with prefix */ |
static const char *cmdtab_search_one(const char *name, link_t **startpos) |
{ |
int i; |
count_t namelen = str_length(name); |
for (i = strlen(str); i > pos; i--) |
str[i] = str[i - 1]; |
str[pos] = ch; |
} |
/** Try to find a command beginning with prefix */ |
static const char *cmdtab_search_one(const char *name,link_t **startpos) |
{ |
size_t namelen = strlen(name); |
const char *curname; |
spinlock_lock(&cmd_lock); |
if (!*startpos) |
if (*startpos == NULL) |
*startpos = cmd_head.next; |
for (; *startpos != &cmd_head; *startpos = (*startpos)->next) { |
cmd_info_t *hlp; |
hlp = list_get_instance(*startpos, cmd_info_t, link); |
curname = hlp->name; |
if (strlen(curname) < namelen) |
cmd_info_t *hlp = list_get_instance(*startpos, cmd_info_t, link); |
const char *curname = hlp->name; |
if (str_length(curname) < namelen) |
continue; |
if (strncmp(curname, name, namelen) == 0) { |
spinlock_unlock(&cmd_lock); |
return curname+namelen; |
if (str_lcmp(curname, name, namelen) == 0) { |
spinlock_unlock(&cmd_lock); |
return (curname + str_lsize(curname, namelen)); |
} |
} |
spinlock_unlock(&cmd_lock); |
spinlock_unlock(&cmd_lock); |
return NULL; |
} |
/** Command completion of the commands |
/** Command completion of the commands |
* |
* @param name - string to match, changed to hint on exit |
* @return number of found matches |
* @param name String to match, changed to hint on exit |
* @param size Input buffer size |
* |
* @return Number of found matches |
* |
*/ |
static int cmdtab_compl(char *name) |
static int cmdtab_compl(char *input, size_t size) |
{ |
static char output[/*MAX_SYMBOL_NAME*/128 + 1]; |
link_t *startpos = NULL; |
const char *foundtxt; |
int found = 0; |
int i; |
output[0] = '\0'; |
while ((foundtxt = cmdtab_search_one(name, &startpos))) { |
startpos = startpos->next; |
if (!found) |
strncpy(output, foundtxt, strlen(foundtxt) + 1); |
else { |
for (i = 0; output[i] && foundtxt[i] && |
output[i] == foundtxt[i]; i++) |
; |
output[i] = '\0'; |
} |
const char *name = input; |
count_t found = 0; |
link_t *pos = NULL; |
const char *hint; |
char output[MAX_CMDLINE]; |
output[0] = 0; |
while ((hint = cmdtab_search_one(name, &pos))) { |
if ((found == 0) || (str_length(output) > str_length(hint))) |
str_cpy(output, MAX_CMDLINE, hint); |
pos = pos->next; |
found++; |
} |
if (!found) |
return 0; |
if (found > 1 && !strlen(output)) { |
if ((found > 1) && (str_length(output) != 0)) { |
printf("\n"); |
startpos = NULL; |
while ((foundtxt = cmdtab_search_one(name, &startpos))) { |
cmd_info_t *hlp; |
hlp = list_get_instance(startpos, cmd_info_t, link); |
printf("%s - %s\n", hlp->name, hlp->description); |
startpos = startpos->next; |
pos = NULL; |
while ((hint = cmdtab_search_one(name, &pos))) { |
cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link); |
printf("%s (%s)\n", hlp->name, hlp->description); |
pos = pos->next; |
} |
} |
strncpy(name, output, 128/*MAX_SYMBOL_NAME*/); |
if (found > 0) |
str_cpy(input, size, output); |
return found; |
} |
static char *clever_readline(const char *prompt, indev_t *input) |
static wchar_t *clever_readline(const char *prompt, indev_t *indev) |
{ |
static int histposition = 0; |
static char tmp[MAX_CMDLINE + 1]; |
int curlen = 0, position = 0; |
char *current = history[histposition]; |
int i; |
char mod; /* Command Modifier */ |
char c; |
printf("%s> ", prompt); |
while (1) { |
c = _getc(input); |
if (c == '\n') { |
putchar(c); |
count_t position = 0; |
wchar_t *current = history[history_pos]; |
current[0] = 0; |
while (true) { |
wchar_t ch = _getc(indev); |
if (ch == '\n') { |
/* Enter */ |
putchar(ch); |
break; |
} |
if (c == '\b') { /* Backspace */ |
if (ch == '\b') { |
/* Backspace */ |
if (position == 0) |
continue; |
for (i = position; i < curlen; i++) |
current[i - 1] = current[i]; |
curlen--; |
position--; |
putchar('\b'); |
for (i = position; i < curlen; i++) |
putchar(current[i]); |
putchar(' '); |
rdln_print_c('\b', curlen - position + 1); |
continue; |
if (wstr_remove(current, position - 1)) { |
position--; |
putchar('\b'); |
printf("%ls ", current + position); |
print_cc('\b', wstr_length(current) - position + 1); |
continue; |
} |
} |
if (c == '\t') { /* Tabulator */ |
int found; |
if (ch == '\t') { |
/* Tab completion */ |
/* Move to the end of the word */ |
for (; position < curlen && current[position] != ' '; |
for (; (current[position] != 0) && (!isspace(current[position])); |
position++) |
putchar(current[position]); |
/* Copy to tmp last word */ |
for (i = position - 1; i >= 0 && current[i] != ' '; i--) |
; |
/* If word begins with * or &, skip it */ |
if (tmp[0] == '*' || tmp[0] == '&') |
for (i = 1; tmp[i]; i++) |
tmp[i - 1] = tmp[i]; |
i++; /* I is at the start of the word */ |
strncpy(tmp, current + i, position - i + 1); |
if (i == 0) { /* Command completion */ |
found = cmdtab_compl(tmp); |
} else { /* Symtab completion */ |
found = symtab_compl(tmp); |
if (position == 0) |
continue; |
/* Find the beginning of the word |
and copy it to tmp */ |
count_t beg; |
for (beg = position - 1; (beg > 0) && (!isspace(current[beg])); |
beg--); |
if (isspace(current[beg])) |
beg++; |
char tmp[STR_BOUNDS(MAX_CMDLINE)]; |
wstr_nstr(tmp, current + beg, position - beg + 1); |
int found; |
if (beg == 0) { |
/* Command completion */ |
found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE)); |
} else { |
/* Symbol completion */ |
found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE)); |
} |
if (found == 0) |
if (found == 0) |
continue; |
for (i = 0; tmp[i] && curlen < MAX_CMDLINE; |
i++, curlen++) |
insert_char(current, tmp[i], i + position); |
if (strlen(tmp) || found == 1) { /* If we have a hint */ |
for (i = position; i < curlen; i++) |
putchar(current[i]); |
position += strlen(tmp); |
/* Add space to end */ |
if (found == 1 && position == curlen && |
curlen < MAX_CMDLINE) { |
current[position] = ' '; |
curlen++; |
if (found > 1) { |
/* No unique hint, list was printed */ |
printf("%s> ", prompt); |
printf("%ls", current); |
print_cc('\b', wstr_length(current) - position); |
continue; |
} |
/* We have a hint */ |
size_t off = 0; |
count_t i = 0; |
while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) { |
if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE)) |
break; |
i++; |
} |
printf("%ls", current + position); |
position += str_length(tmp); |
print_cc('\b', wstr_length(current) - position); |
if (position == wstr_length(current)) { |
/* Insert a space after the last completed argument */ |
if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) { |
printf("%ls", current + position); |
position++; |
putchar(' '); |
} |
} else { /* No hint, table was printed */ |
printf("%s> ", prompt); |
for (i = 0; i < curlen; i++) |
putchar(current[i]); |
position += strlen(tmp); |
} |
rdln_print_c('\b', curlen - position); |
continue; |
} |
if (c == 0x1b) { /* Special command */ |
mod = _getc(input); |
c = _getc(input); |
if (mod != 0x5b && mod != 0x4f) |
continue; |
if (c == 0x33 && _getc(input) == 0x7e) { |
/* Delete */ |
if (position == curlen) |
continue; |
for (i = position + 1; i < curlen; i++) { |
putchar(current[i]); |
current[i - 1] = current[i]; |
} |
putchar(' '); |
rdln_print_c('\b', curlen - position); |
curlen--; |
} else if (c == 0x48) { /* Home */ |
rdln_print_c('\b', position); |
position = 0; |
} else if (c == 0x46) { /* End */ |
for (i = position; i < curlen; i++) |
putchar(current[i]); |
position = curlen; |
} else if (c == 0x44) { /* Left */ |
if (position > 0) { |
putchar('\b'); |
position--; |
} |
continue; |
} else if (c == 0x43) { /* Right */ |
if (position < curlen) { |
putchar(current[position]); |
position++; |
} |
continue; |
} else if (c == 0x41 || c == 0x42) { |
/* Up, down */ |
rdln_print_c('\b', position); |
rdln_print_c(' ', curlen); |
rdln_print_c('\b', curlen); |
if (c == 0x41) /* Up */ |
histposition--; |
else |
histposition++; |
if (histposition < 0) { |
histposition = KCONSOLE_HISTORY - 1; |
} else { |
histposition = |
histposition % KCONSOLE_HISTORY; |
} |
current = history[histposition]; |
printf("%s", current); |
curlen = strlen(current); |
position = curlen; |
continue; |
if (ch == U_LEFT_ARROW) { |
/* Left */ |
if (position > 0) { |
putchar('\b'); |
position--; |
} |
continue; |
} |
if (curlen >= MAX_CMDLINE) |
if (ch == U_RIGHT_ARROW) { |
/* Right */ |
if (position < wstr_length(current)) { |
putchar(current[position]); |
position++; |
} |
continue; |
insert_char(current, c, position); |
curlen++; |
for (i = position; i < curlen; i++) |
putchar(current[i]); |
position++; |
rdln_print_c('\b',curlen - position); |
} |
if (curlen) { |
histposition++; |
histposition = histposition % KCONSOLE_HISTORY; |
} |
current[curlen] = '\0'; |
return current; |
} |
bool kconsole_check_poll(void) |
{ |
return check_poll(stdin); |
} |
/** Kernel console prompt. |
* |
* @param prompt Kernel console prompt (e.g kconsole/panic). |
* @param msg Message to display in the beginning. |
* @param kcon Wait for keypress to show the prompt |
* and never exit. |
* |
*/ |
void kconsole(char *prompt, char *msg, bool kcon) |
{ |
cmd_info_t *cmd_info; |
count_t len; |
char *cmdline; |
if (!stdin) { |
LOG("No stdin for kernel console"); |
return; |
} |
if (msg) |
printf("%s", msg); |
if (kcon) |
_getc(stdin); |
else |
printf("Type \"exit\" to leave the console.\n"); |
while (true) { |
cmdline = clever_readline((char *) prompt, stdin); |
len = strlen(cmdline); |
if (!len) |
} |
if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) { |
/* Up, down */ |
print_cc('\b', position); |
print_cc(' ', wstr_length(current)); |
print_cc('\b', wstr_length(current)); |
if (ch == U_UP_ARROW) { |
/* Up */ |
if (history_pos == 0) |
history_pos = KCONSOLE_HISTORY - 1; |
else |
history_pos--; |
} else { |
/* Down */ |
history_pos++; |
history_pos = history_pos % KCONSOLE_HISTORY; |
} |
current = history[history_pos]; |
printf("%ls", current); |
position = wstr_length(current); |
continue; |
} |
if ((!kcon) && (len == 4) && (strncmp(cmdline, "exit", 4) == 0)) |
break; |
if (ch == U_HOME_ARROW) { |
/* Home */ |
print_cc('\b', position); |
position = 0; |
continue; |
} |
cmd_info = parse_cmdline(cmdline, len); |
if (!cmd_info) |
if (ch == U_END_ARROW) { |
/* End */ |
printf("%ls", current + position); |
position = wstr_length(current); |
continue; |
} |
(void) cmd_info->func(cmd_info->argv); |
if (ch == U_DELETE) { |
/* Delete */ |
if (position == wstr_length(current)) |
continue; |
if (wstr_remove(current, position)) { |
printf("%ls ", current + position); |
print_cc('\b', wstr_length(current) - position + 1); |
} |
continue; |
} |
if (wstr_linsert(current, ch, position, MAX_CMDLINE)) { |
printf("%ls", current + position); |
position++; |
print_cc('\b', wstr_length(current) - position); |
} |
} |
if (wstr_length(current) > 0) { |
history_pos++; |
history_pos = history_pos % KCONSOLE_HISTORY; |
} |
return current; |
} |
/** Kernel console managing thread. |
* |
*/ |
void kconsole_thread(void *data) |
bool kconsole_check_poll(void) |
{ |
kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true); |
return check_poll(stdin); |
} |
static int parse_int_arg(char *text, size_t len, unative_t *result) |
static bool parse_int_arg(const char *text, size_t len, unative_t *result) |
{ |
uintptr_t symaddr; |
bool isaddr = false; |
bool isptr = false; |
int rc; |
static char symname[MAX_SYMBOL_NAME]; |
/* If we get a name, try to find it in symbol table */ |
if (text[0] == '&') { |
485,67 → 436,113 |
text++; |
len--; |
} |
if (text[0] < '0' || text[0] > '9') { |
strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME)); |
rc = symtab_addr_lookup(symname, &symaddr); |
if ((text[0] < '0') || (text[0] > '9')) { |
char symname[MAX_SYMBOL_NAME]; |
str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1); |
uintptr_t symaddr; |
int rc = symtab_addr_lookup(symname, &symaddr); |
switch (rc) { |
case ENOENT: |
printf("Symbol %s not found.\n", symname); |
return -1; |
return false; |
case EOVERFLOW: |
printf("Duplicate symbol %s.\n", symname); |
symtab_print_search(symname); |
return -1; |
default: |
return false; |
case ENOTSUP: |
printf("No symbol information available.\n"); |
return -1; |
return false; |
} |
if (isaddr) |
*result = (unative_t)symaddr; |
*result = (unative_t) symaddr; |
else if (isptr) |
*result = **((unative_t **)symaddr); |
*result = **((unative_t **) symaddr); |
else |
*result = *((unative_t *)symaddr); |
} else { /* It's a number - convert it */ |
*result = *((unative_t *) symaddr); |
} else { |
/* It's a number - convert it */ |
*result = atoi(text); |
if (isptr) |
*result = *((unative_t *)*result); |
*result = *((unative_t *) *result); |
} |
return true; |
} |
return 0; |
/** Parse argument. |
* |
* Find start and end positions of command line argument. |
* |
* @param cmdline Command line as read from the input device. |
* @param size Size (in bytes) of the string. |
* @param start On entry, 'start' contains pointer to the offset |
* of the first unprocessed character of cmdline. |
* On successful exit, it marks beginning of the next argument. |
* @param end Undefined on entry. On exit, 'end' is the offset of the first |
* character behind the next argument. |
* |
* @return False on failure, true on success. |
* |
*/ |
static bool parse_argument(const char *cmdline, size_t size, size_t *start, size_t *end) |
{ |
ASSERT(start != NULL); |
ASSERT(end != NULL); |
bool found_start = false; |
size_t offset = *start; |
size_t prev = *start; |
wchar_t ch; |
while ((ch = str_decode(cmdline, &offset, size)) != 0) { |
if (!found_start) { |
if (!isspace(ch)) { |
*start = prev; |
found_start = true; |
} |
} else { |
if (isspace(ch)) |
break; |
} |
prev = offset; |
} |
*end = prev; |
return found_start; |
} |
/** Parse command line. |
* |
* @param cmdline Command line as read from input device. |
* @param len Command line length. |
* @param cmdline Command line as read from input device. |
* @param size Size (in bytes) of the string. |
* |
* @return Structure describing the command. |
* |
*/ |
cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
static cmd_info_t *parse_cmdline(const char *cmdline, size_t size) |
{ |
index_t start = 0, end = 0; |
cmd_info_t *cmd = NULL; |
link_t *cur; |
count_t i; |
int error = 0; |
if (!parse_argument(cmdline, len, &start, &end)) { |
size_t start = 0; |
size_t end = 0; |
if (!parse_argument(cmdline, size, &start, &end)) { |
/* Command line did not contain alphanumeric word. */ |
return NULL; |
} |
spinlock_lock(&cmd_lock); |
cmd_info_t *cmd = NULL; |
link_t *cur; |
for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
cmd_info_t *hlp; |
hlp = list_get_instance(cur, cmd_info_t, link); |
cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link); |
spinlock_lock(&hlp->lock); |
if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name), |
end - start + 1)) == 0) { |
if (str_lcmp(hlp->name, cmdline + start, |
max(str_length(hlp->name), |
str_nlength(cmdline + start, (count_t) (end - start) - 1))) == 0) { |
cmd = hlp; |
break; |
} |
553,7 → 550,7 |
spinlock_unlock(&hlp->lock); |
} |
spinlock_unlock(&cmd_lock); |
spinlock_unlock(&cmd_lock); |
if (!cmd) { |
/* Unknown command. */ |
560,7 → 557,7 |
printf("Unknown command.\n"); |
return NULL; |
} |
/* cmd == hlp is locked */ |
/* |
569,52 → 566,54 |
* converted to those specified in the cmd info |
* structure. |
*/ |
bool error = false; |
count_t i; |
for (i = 0; i < cmd->argc; i++) { |
char *buf; |
start = end + 1; |
if (!parse_argument(cmdline, len, &start, &end)) { |
start = end; |
if (!parse_argument(cmdline, size, &start, &end)) { |
printf("Too few arguments.\n"); |
spinlock_unlock(&cmd->lock); |
return NULL; |
} |
error = 0; |
char *buf; |
switch (cmd->argv[i].type) { |
case ARG_TYPE_STRING: |
buf = (char *) cmd->argv[i].buffer; |
strncpy(buf, (const char *) &cmdline[start], |
min((end - start) + 2, cmd->argv[i].len)); |
buf[min((end - start) + 1, cmd->argv[i].len - 1)] = |
'\0'; |
str_ncpy(buf, cmd->argv[i].len, cmdline + start, |
(end - start) + 1); |
break; |
case ARG_TYPE_INT: |
if (parse_int_arg(cmdline + start, end - start + 1, |
case ARG_TYPE_INT: |
if (!parse_int_arg(cmdline + start, end - start, |
&cmd->argv[i].intval)) |
error = 1; |
error = true; |
break; |
case ARG_TYPE_VAR: |
if (start != end && cmdline[start] == '"' && |
cmdline[end] == '"') { |
buf = (char *) cmd->argv[i].buffer; |
strncpy(buf, (const char *) &cmdline[start + 1], |
min((end-start), cmd->argv[i].len)); |
buf[min((end - start), cmd->argv[i].len - 1)] = |
'\0'; |
cmd->argv[i].intval = (unative_t) buf; |
cmd->argv[i].vartype = ARG_TYPE_STRING; |
} else if (!parse_int_arg(cmdline + start, |
end - start + 1, &cmd->argv[i].intval)) { |
if ((start < end - 1) && (cmdline[start] == '"')) { |
if (cmdline[end - 1] == '"') { |
buf = (char *) cmd->argv[i].buffer; |
str_ncpy(buf, cmd->argv[i].len, |
cmdline + start + 1, |
(end - start) - 1); |
cmd->argv[i].intval = (unative_t) buf; |
cmd->argv[i].vartype = ARG_TYPE_STRING; |
} else { |
printf("Wrong synxtax.\n"); |
error = true; |
} |
} else if (parse_int_arg(cmdline + start, |
end - start, &cmd->argv[i].intval)) { |
cmd->argv[i].vartype = ARG_TYPE_INT; |
} else { |
printf("Unrecognized variable argument.\n"); |
error = 1; |
error = true; |
} |
break; |
case ARG_TYPE_INVALID: |
default: |
printf("invalid argument type\n"); |
error = 1; |
printf("Invalid argument type\n"); |
error = true; |
break; |
} |
} |
624,8 → 623,8 |
return NULL; |
} |
start = end + 1; |
if (parse_argument(cmdline, len, &start, &end)) { |
start = end; |
if (parse_argument(cmdline, size, &start, &end)) { |
printf("Too many arguments.\n"); |
spinlock_unlock(&cmd->lock); |
return NULL; |
635,42 → 634,55 |
return cmd; |
} |
/** Parse argument. |
/** Kernel console prompt. |
* |
* Find start and end positions of command line argument. |
* @param prompt Kernel console prompt (e.g kconsole/panic). |
* @param msg Message to display in the beginning. |
* @param kcon Wait for keypress to show the prompt |
* and never exit. |
* |
* @param cmdline Command line as read from the input device. |
* @param len Number of characters in cmdline. |
* @param start On entry, 'start' contains pointer to the index |
* of first unprocessed character of cmdline. |
* On successful exit, it marks beginning of the next argument. |
* @param end Undefined on entry. On exit, 'end' points to the last character |
* of the next argument. |
* |
* @return false on failure, true on success. |
*/ |
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end) |
void kconsole(char *prompt, char *msg, bool kcon) |
{ |
index_t i; |
bool found_start = false; |
if (!stdin) { |
LOG("No stdin for kernel console"); |
return; |
} |
ASSERT(start != NULL); |
ASSERT(end != NULL); |
if (msg) |
printf("%s", msg); |
for (i = *start; i < len; i++) { |
if (!found_start) { |
if (isspace(cmdline[i])) |
(*start)++; |
else |
found_start = true; |
} else { |
if (isspace(cmdline[i])) |
break; |
} |
if (kcon) |
_getc(stdin); |
else |
printf("Type \"exit\" to leave the console.\n"); |
while (true) { |
wchar_t *tmp = clever_readline((char *) prompt, stdin); |
count_t len = wstr_length(tmp); |
if (!len) |
continue; |
char cmdline[STR_BOUNDS(MAX_CMDLINE)]; |
wstr_nstr(cmdline, tmp, STR_BOUNDS(MAX_CMDLINE)); |
if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0)) |
break; |
cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE)); |
if (!cmd_info) |
continue; |
(void) cmd_info->func(cmd_info->argv); |
} |
*end = i - 1; |
} |
return found_start; |
/** Kernel console managing thread. |
* |
*/ |
void kconsole_thread(void *data) |
{ |
kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true); |
} |
/** @} |
/branches/dd/kernel/generic/src/printf/vprintf.c |
---|
43,13 → 43,13 |
SPINLOCK_INITIALIZE(printf_lock); /**< vprintf spinlock */ |
static int vprintf_write_utf8(const char *str, size_t size, void *data) |
static int vprintf_str_write(const char *str, size_t size, void *data) |
{ |
index_t index = 0; |
index_t chars = 0; |
size_t offset = 0; |
count_t chars = 0; |
while (index < size) { |
putchar(chr_decode(str, &index, size)); |
while (offset < size) { |
putchar(str_decode(str, &offset, size)); |
chars++; |
} |
56,25 → 56,27 |
return chars; |
} |
static int vprintf_write_utf32(const wchar_t *str, size_t size, void *data) |
static int vprintf_wstr_write(const wchar_t *str, size_t size, void *data) |
{ |
index_t index = 0; |
size_t offset = 0; |
count_t chars = 0; |
while (index < (size / sizeof(wchar_t))) { |
putchar(str[index]); |
index++; |
while (offset < size) { |
putchar(str[chars]); |
chars++; |
offset += sizeof(wchar_t); |
} |
return index; |
return chars; |
} |
int puts(const char *str) |
{ |
index_t index = 0; |
index_t chars = 0; |
size_t offset = 0; |
count_t chars = 0; |
wchar_t uc; |
while ((uc = chr_decode(str, &index, UTF8_NO_LIMIT)) != 0) { |
while ((uc = str_decode(str, &offset, STR_NO_LIMIT)) != 0) { |
putchar(uc); |
chars++; |
} |
85,8 → 87,8 |
int vprintf(const char *fmt, va_list ap) |
{ |
printf_spec_t ps = { |
vprintf_write_utf8, |
vprintf_write_utf32, |
vprintf_str_write, |
vprintf_wstr_write, |
NULL |
}; |
/branches/dd/kernel/generic/src/printf/vsnprintf.c |
---|
36,6 → 36,7 |
#include <printf/printf_core.h> |
#include <string.h> |
#include <memstr.h> |
#include <errno.h> |
typedef struct { |
size_t size; /* Total size of the buffer (in bytes) */ |
43,7 → 44,7 |
char *dst; /* Destination */ |
} vsnprintf_data_t; |
/** Write UTF-8 string to given buffer. |
/** Write string to given buffer. |
* |
* Write at most data->size plain characters including trailing zero. |
* According to C99, snprintf() has to return number of characters that |
51,16 → 52,16 |
* the return value is not the number of actually printed characters |
* but size of the input string. |
* |
* @param str Source UTF-8 string to print. |
* @param str Source string to print. |
* @param size Number of plain characters in str. |
* @param data Structure describing destination string, counter |
* of used space and total string size. |
* |
* @return Number of UTF-8 characters to print (not characters actually |
* @return Number of characters to print (not characters actually |
* printed). |
* |
*/ |
static int vsnprintf_write_utf8(const char *str, size_t size, vsnprintf_data_t *data) |
static int vsnprintf_str_write(const char *str, size_t size, vsnprintf_data_t *data) |
{ |
size_t left = data->size - data->len; |
77,7 → 78,7 |
} |
if (left <= size) { |
/* We have not enought space for whole string |
/* We do not have enough space for the whole string |
* with the trailing zero => print only a part |
* of string |
*/ |
84,9 → 85,9 |
index_t index = 0; |
while (index < size) { |
wchar_t uc = chr_decode(str, &index, size); |
if (!chr_encode(uc, data->dst, &data->len, data->size - 1)) |
wchar_t uc = str_decode(str, &index, size); |
if (chr_encode(uc, data->dst, &data->len, data->size - 1) != EOK) |
break; |
} |
110,7 → 111,7 |
return ((int) size); |
} |
/** Write UTF-32 string to given buffer. |
/** Write wide string to given buffer. |
* |
* Write at most data->size plain characters including trailing zero. |
* According to C99, snprintf() has to return number of characters that |
118,16 → 119,16 |
* the return value is not the number of actually printed characters |
* but size of the input string. |
* |
* @param str Source UTF-32 string to print. |
* @param str Source wide string to print. |
* @param size Number of bytes in str. |
* @param data Structure describing destination string, counter |
* of used space and total string size. |
* |
* @return Number of UTF-8 characters to print (not characters actually |
* @return Number of wide characters to print (not characters actually |
* printed). |
* |
*/ |
static int vsnprintf_write_utf32(const wchar_t *str, size_t size, vsnprintf_data_t *data) |
static int vsnprintf_wstr_write(const wchar_t *str, size_t size, vsnprintf_data_t *data) |
{ |
index_t index = 0; |
146,7 → 147,7 |
return ((int) size); |
} |
if (!chr_encode(str[index], data->dst, &data->len, data->size - 1)) |
if (chr_encode(str[index], data->dst, &data->len, data->size - 1) != EOK) |
break; |
index++; |
168,8 → 169,8 |
str |
}; |
printf_spec_t ps = { |
(int(*) (const char *, size_t, void *)) vsnprintf_write_utf8, |
(int(*) (const wchar_t *, size_t, void *)) vsnprintf_write_utf32, |
(int(*) (const char *, size_t, void *)) vsnprintf_str_write, |
(int(*) (const wchar_t *, size_t, void *)) vsnprintf_wstr_write, |
&data |
}; |
/branches/dd/kernel/generic/src/printf/printf_core.c |
---|
81,53 → 81,54 |
static char nullstr[] = "(NULL)"; |
static char digits_small[] = "0123456789abcdef"; |
static char digits_big[] = "0123456789ABCDEF"; |
static char invalch = U_SPECIAL; |
/** Print one or more UTF-8 characters without adding newline. |
/** Print one or more characters without adding newline. |
* |
* @param buf Buffer holding UTF-8 characters with size of |
* @param buf Buffer holding characters with size of |
* at least size bytes. NULL is not allowed! |
* @param size Size of the buffer in bytes. |
* @param ps Output method and its data. |
* |
* @return Number of UTF-8 characters printed. |
* @return Number of characters printed. |
* |
*/ |
static int printf_putnchars_utf8(const char *buf, size_t size, |
static int printf_putnchars(const char *buf, size_t size, |
printf_spec_t *ps) |
{ |
return ps->write_utf8((void *) buf, size, ps->data); |
return ps->str_write((void *) buf, size, ps->data); |
} |
/** Print one or more UTF-32 characters without adding newline. |
/** Print one or more wide characters without adding newline. |
* |
* @param buf Buffer holding UTF-32 characters with size of |
* @param buf Buffer holding wide characters with size of |
* at least size bytes. NULL is not allowed! |
* @param size Size of the buffer in bytes. |
* @param ps Output method and its data. |
* |
* @return Number of UTF-32 characters printed. |
* @return Number of wide characters printed. |
* |
*/ |
static int printf_putnchars_utf32(const wchar_t *buf, size_t size, |
static int printf_wputnchars(const wchar_t *buf, size_t size, |
printf_spec_t *ps) |
{ |
return ps->write_utf32((void *) buf, size, ps->data); |
return ps->wstr_write((void *) buf, size, ps->data); |
} |
/** Print UTF-8 string without adding a newline. |
/** Print string without adding a newline. |
* |
* @param str UTF-8 string to print. |
* @param str String to print. |
* @param ps Write function specification and support data. |
* |
* @return Number of UTF-8 characters printed. |
* @return Number of characters printed. |
* |
*/ |
static int printf_putstr(const char *str, printf_spec_t *ps) |
{ |
if (str == NULL) |
return printf_putnchars_utf8(nullstr, strlen(nullstr), ps); |
return printf_putnchars(nullstr, str_size(nullstr), ps); |
return ps->write_utf8((void *) str, strlen(str), ps->data); |
return ps->str_write((void *) str, str_size(str), ps->data); |
} |
/** Print one ASCII character. |
141,14 → 142,14 |
static int printf_putchar(const char ch, printf_spec_t *ps) |
{ |
if (!ascii_check(ch)) |
return ps->write_utf8((void *) &invalch, 1, ps->data); |
return ps->str_write((void *) &invalch, 1, ps->data); |
return ps->write_utf8(&ch, 1, ps->data); |
return ps->str_write(&ch, 1, ps->data); |
} |
/** Print one UTF-32 character. |
/** Print one wide character. |
* |
* @param c UTF-32 character to be printed. |
* @param c Wide character to be printed. |
* @param ps Output method. |
* |
* @return Number of characters printed. |
156,10 → 157,10 |
*/ |
static int printf_putwchar(const wchar_t ch, printf_spec_t *ps) |
{ |
if (!unicode_check(ch)) |
return ps->write_utf8((void *) &invalch, 1, ps->data); |
if (!chr_check(ch)) |
return ps->str_write((void *) &invalch, 1, ps->data); |
return ps->write_utf32(&ch, sizeof(wchar_t), ps->data); |
return ps->wstr_write(&ch, sizeof(wchar_t), ps->data); |
} |
/** Print one formatted ASCII character. |
200,7 → 201,7 |
return (int) (counter + 1); |
} |
/** Print one formatted UTF-32 character. |
/** Print one formatted wide character. |
* |
* @param ch Character to print. |
* @param width Width modifier. |
238,26 → 239,27 |
return (int) (counter + 1); |
} |
/** Print UTF-8 string. |
/** Print string. |
* |
* @param str UTF-8 string to be printed. |
* @param str String to be printed. |
* @param width Width modifier. |
* @param precision Precision modifier. |
* @param flags Flags that modify the way the string is printed. |
* |
* @return Number of UTF-8 characters printed, negative value on failure. |
* @return Number of characters printed, negative value on failure. |
*/ |
static int print_utf8(char *str, int width, unsigned int precision, |
uint32_t flags, printf_spec_t *ps) |
static int print_str(char *str, int width, unsigned int precision, |
uint32_t flags, printf_spec_t *ps) |
{ |
if (str == NULL) |
return printf_putstr(nullstr, ps); |
/* Print leading spaces */ |
size_t size = str_length(str); |
/* Print leading spaces. */ |
count_t strw = str_length(str); |
if (precision == 0) |
precision = size; |
precision = strw; |
/* Left padding */ |
count_t counter = 0; |
width -= precision; |
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
267,13 → 269,15 |
} |
} |
/* Part of @a str fitting into the alloted space. */ |
int retval; |
size_t bytes = str_lsize(str, min(size, precision)); |
if ((retval = printf_putnchars_utf8(str, bytes, ps)) < 0) |
size_t size = str_lsize(str, precision); |
if ((retval = printf_putnchars(str, size, ps)) < 0) |
return -counter; |
counter += retval; |
/* Right padding */ |
while (width-- > 0) { |
if (printf_putchar(' ', ps) == 1) |
counter++; |
280,28 → 284,33 |
} |
return ((int) counter); |
} |
/** Print UTF-32 string. |
/** Print wide string. |
* |
* @param str UTF-32 string to be printed. |
* @param str Wide string to be printed. |
* @param width Width modifier. |
* @param precision Precision modifier. |
* @param flags Flags that modify the way the string is printed. |
* |
* @return Number of UTF-32 characters printed, negative value on failure. |
* @return Number of wide characters printed, negative value on failure. |
*/ |
static int print_utf32(wchar_t *str, int width, unsigned int precision, |
uint32_t flags, printf_spec_t *ps) |
static int print_wstr(wchar_t *str, int width, unsigned int precision, |
uint32_t flags, printf_spec_t *ps) |
{ |
if (str == NULL) |
return printf_putstr(nullstr, ps); |
/* Print leading spaces */ |
size_t size = wstr_length(str); |
if (*str == U_BOM) |
str++; |
/* Print leading spaces. */ |
size_t strw = wstr_length(str); |
if (precision == 0) |
precision = size; |
precision = strw; |
/* Left padding */ |
count_t counter = 0; |
width -= precision; |
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
311,18 → 320,20 |
} |
} |
/* Part of @a wstr fitting into the alloted space. */ |
int retval; |
size_t bytes = min(size, precision) * sizeof(wchar_t); |
if ((retval = printf_putnchars_utf32(str, bytes, ps)) < 0) |
size_t size = wstr_lsize(str, precision); |
if ((retval = printf_wputnchars(str, size, ps)) < 0) |
return -counter; |
counter += retval; |
/* Right padding */ |
while (width-- > 0) { |
if (printf_putchar(' ', ps) == 1) |
counter++; |
} |
return ((int) counter); |
} |
537,8 → 548,8 |
* - "h" Signed or unsigned short.@n |
* - "" Signed or unsigned int (default value).@n |
* - "l" Signed or unsigned long int.@n |
* If conversion is "c", the character is wchar_t (UTF-32).@n |
* If conversion is "s", the string is wchar_t * (UTF-32).@n |
* If conversion is "c", the character is wchar_t (wide character).@n |
* If conversion is "s", the string is wchar_t * (wide string).@n |
* - "ll" Signed or unsigned long long int.@n |
* |
* CONVERSION:@n |
546,15 → 557,12 |
* |
* - c Print single character. The character is expected to be plain |
* ASCII (e.g. only values 0 .. 127 are valid).@n |
* If type is "l", then the character is expected to be UTF-32 |
* If type is "l", then the character is expected to be wide character |
* (e.g. values 0 .. 0x10ffff are valid). |
* |
* - s Print zero terminated string. If a NULL value is passed as |
* value, "(NULL)" is printed instead.@n |
* The string is expected to be correctly encoded UTF-8 (or plain |
* ASCII, which is a subset of UTF-8).@n |
* If type is "l", then the string is expected to be correctly |
* encoded UTF-32. |
* If type is "l", then the string is expected to be wide string. |
* |
* - P, p Print value of a pointer. Void * value is expected and it is |
* printed in hexadecimal notation with prefix (as with \%#X / \%#x |
574,35 → 582,35 |
* - X, x Print hexadecimal number with upper- or lower-case. Prefix is |
* not printed by default. |
* |
* All other characters from fmt except the formatting directives are printed in |
* All other characters from fmt except the formatting directives are printed |
* verbatim. |
* |
* @param fmt Formatting NULL terminated string (UTF-8 or plain ASCII). |
* @param fmt Format NULL-terminated string. |
* |
* @return Number of UTF-8 characters printed, negative value on failure. |
* @return Number of characters printed, negative value on failure. |
* |
*/ |
int printf_core(const char *fmt, printf_spec_t *ps, va_list ap) |
{ |
index_t i = 0; /* Index of the currently processed character from fmt */ |
index_t nxt = 0; |
index_t j = 0; /* Index to the first not printed nonformating character */ |
size_t i; /* Index of the currently processed character from fmt */ |
size_t nxt = 0; /* Index of the next character from fmt */ |
size_t j = 0; /* Index to the first not printed nonformating character */ |
wchar_t uc; /* Current UTF-32 character decoded from fmt */ |
count_t counter = 0; /* Number of UTF-8 characters printed */ |
count_t counter = 0; /* Number of characters printed */ |
int retval; /* Return values from nested functions */ |
while (true) { |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
if (uc == '\0') break; |
wchar_t uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
if (uc == 0) |
break; |
/* Control character */ |
if (uc == '%') { |
/* Print common characters if any processed */ |
if (i > j) { |
if ((retval = printf_putnchars_utf8(&fmt[j], i - j, ps)) < 0) { |
if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
/* Error */ |
counter = -counter; |
goto out; |
618,7 → 626,7 |
do { |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
switch (uc) { |
case '#': |
flags |= __PRINTF_FLAG_PREFIX; |
646,10 → 654,10 |
while (true) { |
width *= 10; |
width += uc - '0'; |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
if (uc == '\0') |
uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
if (uc == 0) |
break; |
if (!isdigit(uc)) |
break; |
657,7 → 665,7 |
} else if (uc == '*') { |
/* Get width value from argument list */ |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
width = (int) va_arg(ap, int); |
if (width < 0) { |
/* Negative width sets '-' flag */ |
670,15 → 678,15 |
int precision = 0; |
if (uc == '.') { |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
if (isdigit(uc)) { |
while (true) { |
precision *= 10; |
precision += uc - '0'; |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
if (uc == '\0') |
uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
if (uc == 0) |
break; |
if (!isdigit(uc)) |
break; |
686,7 → 694,7 |
} else if (uc == '*') { |
/* Get precision value from the argument list */ |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
precision = (int) va_arg(ap, int); |
if (precision < 0) { |
/* Ignore negative precision */ |
705,10 → 713,10 |
/* Char or short */ |
qualifier = PrintfQualifierShort; |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
if (uc == 'h') { |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
qualifier = PrintfQualifierByte; |
} |
break; |
716,10 → 724,10 |
/* Long or long long */ |
qualifier = PrintfQualifierLong; |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
if (uc == 'l') { |
i = nxt; |
uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); |
uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
qualifier = PrintfQualifierLongLong; |
} |
break; |
736,9 → 744,9 |
*/ |
case 's': |
if (qualifier == PrintfQualifierLong) |
retval = print_utf32(va_arg(ap, wchar_t *), width, precision, flags, ps); |
retval = print_wstr(va_arg(ap, wchar_t *), width, precision, flags, ps); |
else |
retval = print_utf8(va_arg(ap, char *), width, precision, flags, ps); |
retval = print_str(va_arg(ap, char *), width, precision, flags, ps); |
if (retval < 0) { |
counter = -counter; |
871,7 → 879,7 |
} |
if (i > j) { |
if ((retval = printf_putnchars_utf8(&fmt[j], i - j, ps)) < 0) { |
if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
/* Error */ |
counter = -counter; |
goto out; |
/branches/dd/kernel/generic/src/proc/task.c |
---|
151,7 → 151,7 |
ta->as = as; |
memcpy(ta->name, name, TASK_NAME_BUFLEN); |
ta->name[TASK_NAME_BUFLEN - 1] = '\0'; |
ta->name[TASK_NAME_BUFLEN - 1] = 0; |
atomic_set(&ta->refcount, 0); |
atomic_set(&ta->lifecount, 0); |
274,7 → 274,7 |
return (unative_t) rc; |
namebuf[name_len] = '\0'; |
strncpy(TASK->name, namebuf, TASK_NAME_BUFLEN); |
str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf); |
return EOK; |
} |
/branches/dd/kernel/generic/src/proc/program.c |
---|
213,7 → 213,7 |
if (rc != 0) |
return (unative_t) rc; |
namebuf[name_len] = '\0'; |
namebuf[name_len] = 0; |
/* Spawn the new task. */ |
/branches/dd/kernel/generic/src/proc/thread.c |
---|
316,7 → 316,7 |
interrupts_restore(ipl); |
memcpy(t->name, name, THREAD_NAME_BUFLEN); |
t->name[THREAD_NAME_BUFLEN - 1] = '\0'; |
t->name[THREAD_NAME_BUFLEN - 1] = 0; |
t->thread_code = func; |
t->thread_arg = arg; |
723,7 → 723,7 |
if (rc != 0) |
return (unative_t) rc; |
namebuf[name_len] = '\0'; |
namebuf[name_len] = 0; |
/* |
* In case of failure, kernel_uarg will be deallocated in this function. |
/branches/dd/kernel/generic/src/lib/string.c |
---|
32,7 → 32,73 |
/** |
* @file |
* @brief Miscellaneous functions. |
* @brief String functions. |
* |
* Strings and characters use the Universal Character Set (UCS). The standard |
* strings, called just strings are encoded in UTF-8. Wide strings (encoded |
* in UTF-32) are supported to a limited degree. A single character is |
* represented as wchar_t.@n |
* |
* Overview of the terminology:@n |
* |
* Term Meaning |
* -------------------- ---------------------------------------------------- |
* byte 8 bits stored in uint8_t (unsigned 8 bit integer) |
* |
* character UTF-32 encoded Unicode character, stored in wchar_t |
* (signed 32 bit integer), code points 0 .. 1114111 |
* are valid |
* |
* ASCII character 7 bit encoded ASCII character, stored in char |
* (usually signed 8 bit integer), code points 0 .. 127 |
* are valid |
* |
* string UTF-8 encoded NULL-terminated Unicode string, char * |
* |
* wide string UTF-32 encoded NULL-terminated Unicode string, |
* wchar_t * |
* |
* [wide] string size number of BYTES in a [wide] string (excluding |
* the NULL-terminator), size_t |
* |
* [wide] string length number of CHARACTERS in a [wide] string (excluding |
* the NULL-terminator), count_t |
* |
* [wide] string width number of display cells on a monospace display taken |
* by a [wide] string, count_t |
* |
* |
* Overview of string metrics:@n |
* |
* Metric Abbrev. Type Meaning |
* ------ ------ ------ ------------------------------------------------- |
* size n size_t number of BYTES in a string (excluding the |
* NULL-terminator) |
* |
* length l count_t number of CHARACTERS in a string (excluding the |
* null terminator) |
* |
* width w count_t number of display cells on a monospace display |
* taken by a string |
* |
* |
* Function naming prefixes:@n |
* |
* chr_ operate on characters |
* ascii_ operate on ASCII characters |
* str_ operate on strings |
* wstr_ operate on wide strings |
* |
* [w]str_[n|l|w] operate on a prefix limited by size, length |
* or width |
* |
* |
* A specific character inside a [wide] string can be referred to by:@n |
* |
* pointer (char *, wchar_t *) |
* byte offset (size_t) |
* character index (count_t) |
* |
*/ |
#include <string.h> |
40,52 → 106,50 |
#include <cpu.h> |
#include <arch/asm.h> |
#include <arch.h> |
#include <console/kconsole.h> |
#include <errno.h> |
#include <align.h> |
#include <debug.h> |
char invalch = '?'; |
/** Byte mask consisting of lowest @n bits (out of 8) */ |
#define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1)) |
/** Byte mask consisting of lowest @n bits (out of eight). */ |
#define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1)) |
/** Byte mask consisting of lowest @n bits (out of 32) */ |
#define LO_MASK_32(n) ((uint32_t) ((1 << (n)) - 1)) |
/** Byte mask consisting of lowest @n bits (out of 32). */ |
#define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1)) |
/** Byte mask consisting of highest @n bits (out of 8) */ |
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n))) |
/** Byte mask consisting of highest @n bits (out of eight). */ |
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n))) |
/** Number of data bits in a UTF-8 continuation byte */ |
#define CONT_BITS 6 |
/** Number of data bits in a UTF-8 continuation byte. */ |
#define CONT_BITS 6 |
/** Decode a single character from a substring. |
/** Decode a single character from a string. |
* |
* Decode a single character from a substring of size @a sz. Decoding starts |
* Decode a single character from a string of size @a size. Decoding starts |
* at @a offset and this offset is moved to the beginning of the next |
* character. In case of decoding error, offset generally advances at least |
* by one. However, offset is never moved beyond (str + sz). |
* by one. However, offset is never moved beyond size. |
* |
* @param str String (not necessarily NULL-terminated). |
* @param index Index (counted in plain characters) where to start |
* the decoding. |
* @param limit Size of the substring. |
* @param str String (not necessarily NULL-terminated). |
* @param offset Byte offset in string where to start decoding. |
* @param size Size of the string (in bytes). |
* |
* @return Value of decoded character or '?' on decoding error. |
* @return Value of decoded character, U_SPECIAL on decoding error or |
* NULL if attempt to decode beyond @a size. |
* |
*/ |
wchar_t chr_decode(const char *str, size_t *offset, size_t sz) |
wchar_t str_decode(const char *str, size_t *offset, size_t size) |
{ |
uint8_t b0, b; /* Bytes read from str. */ |
wchar_t ch; |
int b0_bits; /* Data bits in first byte. */ |
int cbytes; /* Number of continuation bytes. */ |
if (*offset + 1 > sz) |
return invalch; |
b0 = (uint8_t) str[(*offset)++]; |
/* Determine code length. */ |
if (*offset + 1 > size) |
return 0; |
/* First byte read from string */ |
uint8_t b0 = (uint8_t) str[(*offset)++]; |
/* Determine code length */ |
unsigned int b0_bits; /* Data bits in first byte */ |
unsigned int cbytes; /* Number of continuation bytes */ |
if ((b0 & 0x80) == 0) { |
/* 0xxxxxxx (Plain ASCII) */ |
b0_bits = 7; |
103,30 → 167,28 |
b0_bits = 3; |
cbytes = 3; |
} else { |
/* 10xxxxxx -- unexpected continuation byte. */ |
return invalch; |
/* 10xxxxxx -- unexpected continuation byte */ |
return U_SPECIAL; |
} |
if (*offset + cbytes > sz) { |
return invalch; |
} |
ch = b0 & LO_MASK_8(b0_bits); |
/* Decode continuation bytes. */ |
if (*offset + cbytes > size) |
return U_SPECIAL; |
wchar_t ch = b0 & LO_MASK_8(b0_bits); |
/* Decode continuation bytes */ |
while (cbytes > 0) { |
b = (uint8_t) str[(*offset)++]; |
/* Must be 10xxxxxx. */ |
if ((b & 0xc0) != 0x80) { |
return invalch; |
} |
/* Shift data bits to ch. */ |
uint8_t b = (uint8_t) str[(*offset)++]; |
/* Must be 10xxxxxx */ |
if ((b & 0xc0) != 0x80) |
return U_SPECIAL; |
/* Shift data bits to ch */ |
ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS)); |
--cbytes; |
cbytes--; |
} |
return ch; |
} |
136,33 → 198,32 |
* it into a buffer at @a offset. Encoding starts at @a offset and this offset |
* is moved to the position where the next character can be written to. |
* |
* @param ch Input character. |
* @param str Output buffer. |
* @param offset Offset (in bytes) where to start writing. |
* @param sz Size of the output buffer. |
* @param ch Input character. |
* @param str Output buffer. |
* @param offset Byte offset where to start writing. |
* @param size Size of the output buffer (in bytes). |
* |
* @return True if the character was encoded successfully or false if there |
* was not enough space in the output buffer or the character code |
* was invalid. |
* @return EOK if the character was encoded successfully, EOVERFLOW if there |
* was not enough space in the output buffer or EINVAL if the character |
* code was invalid. |
*/ |
bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz) |
int chr_encode(wchar_t ch, char *str, size_t *offset, size_t size) |
{ |
uint32_t cc; /* Unsigned version of ch. */ |
int cbytes; /* Number of continuation bytes. */ |
int b0_bits; /* Number of data bits in first byte. */ |
int i; |
if (*offset >= sz) |
return false; |
if (ch < 0) |
return false; |
/* Bit operations should only be done on unsigned numbers. */ |
cc = (uint32_t) ch; |
/* Determine how many continuation bytes are needed. */ |
if (*offset >= size) |
return EOVERFLOW; |
if (!chr_check(ch)) |
return EINVAL; |
/* Unsigned version of ch (bit operations should only be done |
on unsigned types). */ |
uint32_t cc = (uint32_t) ch; |
/* Determine how many continuation bytes are needed */ |
unsigned int b0_bits; /* Data bits in first byte */ |
unsigned int cbytes; /* Number of continuation bytes */ |
if ((cc & ~LO_MASK_32(7)) == 0) { |
b0_bits = 7; |
cbytes = 0; |
176,241 → 237,474 |
b0_bits = 3; |
cbytes = 3; |
} else { |
/* Codes longer than 21 bits are not supported. */ |
return false; |
/* Codes longer than 21 bits are not supported */ |
return EINVAL; |
} |
/* Check for available space in buffer. */ |
if (*offset + cbytes >= sz) |
return false; |
/* Encode continuation bytes. */ |
for (i = cbytes; i > 0; --i) { |
/* Check for available space in buffer */ |
if (*offset + cbytes >= size) |
return EOVERFLOW; |
/* Encode continuation bytes */ |
unsigned int i; |
for (i = cbytes; i > 0; i--) { |
str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS)); |
cc = cc >> CONT_BITS; |
} |
/* Encode first byte. */ |
/* Encode first byte */ |
str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); |
/* Advance offset */ |
*offset += cbytes + 1; |
return EOK; |
} |
/* Advance offset. */ |
*offset += (1 + cbytes); |
/** Get size of string. |
* |
* Get the number of bytes which are used by the string @a str (excluding the |
* NULL-terminator). |
* |
* @param str String to consider. |
* |
* @return Number of bytes used by the string |
* |
*/ |
size_t str_size(const char *str) |
{ |
size_t size = 0; |
return true; |
while (*str++ != 0) |
size++; |
return size; |
} |
/** Get size of string, with length limit. |
/** Get size of wide string. |
* |
* Get the number of bytes which are used by the wide string @a str (excluding the |
* NULL-terminator). |
* |
* @param str Wide string to consider. |
* |
* @return Number of bytes used by the wide string |
* |
*/ |
size_t wstr_size(const wchar_t *str) |
{ |
return (wstr_length(str) * sizeof(wchar_t)); |
} |
/** Get size of string with length limit. |
* |
* Get the number of bytes which are used by up to @a max_len first |
* characters in the string @a str. If @a max_len is greater than |
* the length of @a str, the entire string is measured. |
* the length of @a str, the entire string is measured (excluding the |
* NULL-terminator). |
* |
* @param str String to consider. |
* @param count Maximum number of characters to measure. |
* @param str String to consider. |
* @param max_len Maximum number of characters to measure. |
* |
* @return Number of bytes used by the characters. |
* |
*/ |
size_t str_lsize(const char *str, count_t max_len) |
{ |
count_t len = 0; |
size_t cur = 0; |
size_t prev; |
wchar_t ch; |
while (true) { |
prev = cur; |
if (len >= max_len) |
size_t offset = 0; |
while (len < max_len) { |
if (str_decode(str, &offset, STR_NO_LIMIT) == 0) |
break; |
ch = chr_decode(str, &cur, UTF8_NO_LIMIT); |
if (ch == '\0') break; |
len++; |
} |
return prev; |
return offset; |
} |
/** Check whether character is plain ASCII. |
/** Get size of wide string with length limit. |
* |
* @return True if character is plain ASCII. |
* Get the number of bytes which are used by up to @a max_len first |
* wide characters in the wide string @a str. If @a max_len is greater than |
* the length of @a str, the entire wide string is measured (excluding the |
* NULL-terminator). |
* |
* @param str Wide string to consider. |
* @param max_len Maximum number of wide characters to measure. |
* |
* @return Number of bytes used by the wide characters. |
* |
*/ |
bool ascii_check(const wchar_t ch) |
size_t wstr_lsize(const wchar_t *str, count_t max_len) |
{ |
if ((ch >= 0) && (ch <= 127)) |
return true; |
return false; |
return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t)); |
} |
/** Check whether character is Unicode. |
/** Get number of characters in a string. |
* |
* @return True if character is valid Unicode code point. |
* @param str NULL-terminated string. |
* |
* @return Number of characters in string. |
* |
*/ |
bool unicode_check(const wchar_t ch) |
count_t str_length(const char *str) |
{ |
if ((ch >= 0) && (ch <= 1114111)) |
return true; |
count_t len = 0; |
size_t offset = 0; |
return false; |
while (str_decode(str, &offset, STR_NO_LIMIT) != 0) |
len++; |
return len; |
} |
/** Return number of plain characters in a string. |
/** Get number of characters in a wide string. |
* |
* @param str NULL-terminated string. |
* @param str NULL-terminated wide string. |
* |
* @return Number of characters in @a str. |
* |
*/ |
size_t strlen(const char *str) |
count_t wstr_length(const wchar_t *wstr) |
{ |
size_t size; |
for (size = 0; str[size]; size++); |
count_t len = 0; |
return size; |
while (*wstr++ != 0) |
len++; |
return len; |
} |
/** Return number of characters in a string. |
/** Get number of characters in a string with size limit. |
* |
* @param str NULL-terminated string. |
* @param str NULL-terminated string. |
* @param size Maximum number of bytes to consider. |
* |
* @return Number of characters in string. |
* |
*/ |
count_t str_length(const char *str) |
count_t str_nlength(const char *str, size_t size) |
{ |
count_t len = 0; |
size_t offset = 0; |
while (str_decode(str, &offset, size) != 0) |
len++; |
return len; |
} |
while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) { |
/** Get number of characters in a string with size limit. |
* |
* @param str NULL-terminated string. |
* @param size Maximum number of bytes to consider. |
* |
* @return Number of characters in string. |
* |
*/ |
count_t wstr_nlength(const wchar_t *str, size_t size) |
{ |
count_t len = 0; |
count_t limit = ALIGN_DOWN(size, sizeof(wchar_t)); |
count_t offset = 0; |
while ((offset < limit) && (*str++ != 0)) { |
len++; |
offset += sizeof(wchar_t); |
} |
return len; |
} |
/** Return number of characters in a wide string. |
/** Check whether character is plain ASCII. |
* |
* @param str NULL-terminated wide string. |
* @return Number of characters in @a str. |
* @return True if character is plain ASCII. |
* |
*/ |
count_t wstr_length(const wchar_t *wstr) |
bool ascii_check(wchar_t ch) |
{ |
count_t len; |
if ((ch >= 0) && (ch <= 127)) |
return true; |
return false; |
} |
len = 0; |
while (*wstr++ != '\0') |
++len; |
return len; |
/** Check whether character is valid |
* |
* @return True if character is a valid Unicode code point. |
* |
*/ |
bool chr_check(wchar_t ch) |
{ |
if ((ch >= 0) && (ch <= 1114111)) |
return true; |
return false; |
} |
/** Compare two NULL terminated strings |
/** Compare two NULL terminated strings. |
* |
* Do a char-by-char comparison of two NULL terminated strings. |
* 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. |
* |
* @param src First string to compare. |
* @param dst Second string to compare. |
* @param s1 First string to compare. |
* @param s2 Second string to compare. |
* |
* @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
* @return 0 if the strings are equal, -1 if first is smaller, |
* 1 if second smaller. |
* |
*/ |
int strcmp(const char *src, const char *dst) |
int str_cmp(const char *s1, const char *s2) |
{ |
for (; *src && *dst; src++, dst++) { |
if (*src < *dst) |
wchar_t c1 = 0; |
wchar_t c2 = 0; |
size_t off1 = 0; |
size_t off2 = 0; |
while (true) { |
c1 = str_decode(s1, &off1, STR_NO_LIMIT); |
c2 = str_decode(s2, &off2, STR_NO_LIMIT); |
if (c1 < c2) |
return -1; |
if (*src > *dst) |
if (c1 > c2) |
return 1; |
if (c1 == 0 || c2 == 0) |
break; |
} |
if (*src == *dst) |
return 0; |
if (!*src) |
return -1; |
return 1; |
return 0; |
} |
/** Compare two NULL terminated strings |
/** Compare two NULL terminated strings with length limit. |
* |
* Do a char-by-char comparison of two NULL terminated strings. |
* 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. |
* characters on the minimum of their lengths and the length limit. |
* |
* @param src First string to compare. |
* @param dst Second string to compare. |
* @param len Maximal length for comparison. |
* @param s1 First string to compare. |
* @param s2 Second string to compare. |
* @param max_len Maximum number of characters to consider. |
* |
* @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
* @return 0 if the strings are equal, -1 if first is smaller, |
* 1 if second smaller. |
* |
*/ |
int strncmp(const char *src, const char *dst, size_t len) |
int str_lcmp(const char *s1, const char *s2, count_t max_len) |
{ |
unsigned int i; |
wchar_t c1 = 0; |
wchar_t c2 = 0; |
for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) { |
if (*src < *dst) |
size_t off1 = 0; |
size_t off2 = 0; |
count_t len = 0; |
while (true) { |
if (len >= max_len) |
break; |
c1 = str_decode(s1, &off1, STR_NO_LIMIT); |
c2 = str_decode(s2, &off2, STR_NO_LIMIT); |
if (c1 < c2) |
return -1; |
if (*src > *dst) |
if (c1 > c2) |
return 1; |
if (c1 == 0 || c2 == 0) |
break; |
++len; |
} |
return 0; |
} |
/** Copy string. |
* |
* Copy source string @a src to destination buffer @a dest. |
* No more than @a size bytes are written. If the size of the output buffer |
* is at least one byte, the output string will always be well-formed, i.e. |
* null-terminated and containing only complete characters. |
* |
* @param dst Destination buffer. |
* @param count Size of the destination buffer (must be > 0). |
* @param src Source string. |
*/ |
void str_cpy(char *dest, size_t size, const char *src) |
{ |
wchar_t ch; |
size_t src_off; |
size_t dest_off; |
/* There must be space for a null terminator in the buffer. */ |
ASSERT(size > 0); |
if (i == len || *src == *dst) |
return 0; |
if (!*src) |
return -1; |
return 1; |
src_off = 0; |
dest_off = 0; |
while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) { |
if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) |
break; |
} |
dest[dest_off] = '\0'; |
} |
/** Copy size-limited substring. |
* |
* Copy prefix of string @a src of max. size @a size to destination buffer |
* @a dest. No more than @a size bytes are written. The output string will |
* always be well-formed, i.e. null-terminated and containing only complete |
* characters. |
* |
* No more than @a n bytes are read from the input string, so it does not |
* have to be null-terminated. |
* |
* @param dst Destination buffer. |
* @param count Size of the destination buffer (must be > 0). |
* @param src Source string. |
* @param n Maximum number of bytes to read from @a src. |
*/ |
void str_ncpy(char *dest, size_t size, const char *src, size_t n) |
{ |
wchar_t ch; |
size_t src_off; |
size_t dest_off; |
/* There must be space for a null terminator in the buffer. */ |
ASSERT(size > 0); |
src_off = 0; |
dest_off = 0; |
/** Copy NULL terminated string. |
while ((ch = str_decode(src, &src_off, n)) != 0) { |
if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) |
break; |
} |
dest[dest_off] = '\0'; |
} |
/** Copy NULL-terminated wide string to string |
* |
* Copy at most 'len' characters from string 'src' to 'dest'. |
* If 'src' is shorter than 'len', '\0' is inserted behind the |
* last copied character. |
* Copy source wide string @a src to destination buffer @a dst. |
* No more than @a size bytes are written. NULL-terminator is always |
* written after the last succesfully copied character (i.e. if the |
* destination buffer is has at least 1 byte, it will be always |
* NULL-terminated). |
* |
* @param src Source string. |
* @param dest Destination buffer. |
* @param len Size of destination buffer. |
* @param src Source wide string. |
* @param dst Destination buffer. |
* @param count Size of the destination buffer. |
* |
*/ |
void strncpy(char *dest, const char *src, size_t len) |
void wstr_nstr(char *dst, const wchar_t *src, size_t size) |
{ |
unsigned int i; |
/* No space for the NULL-terminator in the buffer */ |
if (size == 0) |
return; |
for (i = 0; i < len; i++) { |
if (!(dest[i] = src[i])) |
return; |
wchar_t ch; |
count_t src_idx = 0; |
size_t dst_off = 0; |
while ((ch = src[src_idx++]) != 0) { |
if (chr_encode(ch, dst, &dst_off, size) != EOK) |
break; |
} |
dest[i - 1] = '\0'; |
if (dst_off >= size) |
dst[size - 1] = 0; |
else |
dst[dst_off] = 0; |
} |
/** Find first occurence of character in string. |
* |
* @param s String to search. |
* @param i Character to look for. |
* @param str String to search. |
* @param ch Character to look for. |
* |
* @return Pointer to character in @a s or NULL if not found. |
* @return Pointer to character in @a str or NULL if not found. |
* |
*/ |
extern char *strchr(const char *s, int i) |
const char *str_chr(const char *str, wchar_t ch) |
{ |
while (*s != '\0') { |
if (*s == i) |
return (char *) s; |
++s; |
wchar_t acc; |
size_t off = 0; |
while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) { |
if (acc == ch) |
return (str + off); |
} |
return NULL; |
} |
/** Insert a wide character into a wide string. |
* |
* Insert a wide character into a wide string at position |
* @a pos. The characters after the position are shifted. |
* |
* @param str String to insert to. |
* @param ch Character to insert to. |
* @param pos Character index where to insert. |
@ @param max_pos Characters in the buffer. |
* |
* @return True if the insertion was sucessful, false if the position |
* is out of bounds. |
* |
*/ |
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos) |
{ |
count_t len = wstr_length(str); |
if ((pos > len) || (pos + 1 > max_pos)) |
return false; |
count_t i; |
for (i = len; i + 1 > pos; i--) |
str[i + 1] = str[i]; |
str[pos] = ch; |
return true; |
} |
/** Remove a wide character from a wide string. |
* |
* Remove a wide character from a wide string at position |
* @a pos. The characters after the position are shifted. |
* |
* @param str String to remove from. |
* @param pos Character index to remove. |
* |
* @return True if the removal was sucessful, false if the position |
* is out of bounds. |
* |
*/ |
bool wstr_remove(wchar_t *str, count_t pos) |
{ |
count_t len = wstr_length(str); |
if (pos >= len) |
return false; |
count_t i; |
for (i = pos + 1; i <= len; i++) |
str[i - 1] = str[i]; |
return true; |
} |
/** @} |
*/ |
/branches/dd/kernel/generic/src/adt/hash_table.c |
---|
32,7 → 32,7 |
/** |
* @file |
* @brief Implementation of generic chained hash table. |
* @brief Implementation of generic chained hash table. |
* |
* This file contains implementation of generic chained hash table. |
*/ |
56,13 → 56,15 |
index_t i; |
ASSERT(h); |
ASSERT(op && op->hash && op->compare); |
ASSERT(op); |
ASSERT(op->hash); |
ASSERT(op->compare); |
ASSERT(max_keys > 0); |
h->entry = (link_t *) malloc(m * sizeof(link_t), 0); |
if (!h->entry) { |
if (!h->entry) |
panic("Cannot allocate memory for hash table."); |
} |
memsetb(h->entry, m * sizeof(link_t), 0); |
for (i = 0; i < m; i++) |
82,10 → 84,13 |
void hash_table_insert(hash_table_t *h, unative_t key[], link_t *item) |
{ |
index_t chain; |
ASSERT(item); |
ASSERT(h && h->op && h->op->hash && h->op->compare); |
ASSERT(h); |
ASSERT(h->op); |
ASSERT(h->op->hash); |
ASSERT(h->op->compare); |
chain = h->op->hash(key); |
ASSERT(chain < h->entries); |
103,9 → 108,12 |
{ |
link_t *cur; |
index_t chain; |
ASSERT(h && h->op && h->op->hash && h->op->compare); |
ASSERT(h); |
ASSERT(h->op); |
ASSERT(h->op->hash); |
ASSERT(h->op->compare); |
chain = h->op->hash(key); |
ASSERT(chain < h->entries); |
123,7 → 131,7 |
/** Remove all matching items from hash table. |
* |
* For each removed item, h->remove_callback() is called. |
* For each removed item, h->remove_callback() is called (if not NULL). |
* |
* @param h Hash table. |
* @param key Array of keys that will be compared against items of the hash table. |
133,12 → 141,15 |
{ |
index_t chain; |
link_t *cur; |
ASSERT(h && h->op && h->op->hash && h->op->compare && h->op->remove_callback); |
ASSERT(h); |
ASSERT(h->op); |
ASSERT(h->op->hash); |
ASSERT(h->op->compare); |
ASSERT(keys <= h->max_keys); |
if (keys == h->max_keys) { |
/* |
* All keys are known, hash_table_find() can be used to find the entry. |
*/ |
146,7 → 157,8 |
cur = hash_table_find(h, key); |
if (cur) { |
list_remove(cur); |
h->op->remove_callback(cur); |
if (h->op->remove_callback) |
h->op->remove_callback(cur); |
} |
return; |
} |
164,7 → 176,8 |
cur = cur->prev; |
list_remove(hlp); |
h->op->remove_callback(hlp); |
if (h->op->remove_callback) |
h->op->remove_callback(hlp); |
continue; |
} |
/branches/dd/kernel/generic/src/mm/frame.c |
---|
992,14 → 992,27 |
/* If no memory, reclaim some slab memory, |
if it does not help, reclaim all */ |
if ((znum == (count_t) -1) && (!(flags & FRAME_NO_RECLAIM))) { |
spinlock_unlock(&zones.lock); |
interrupts_restore(ipl); |
count_t freed = slab_reclaim(0); |
ipl = interrupts_disable(); |
spinlock_lock(&zones.lock); |
if (freed > 0) |
znum = find_free_zone(order, |
FRAME_TO_ZONE_FLAGS(flags), hint); |
if (znum == (count_t) -1) { |
spinlock_unlock(&zones.lock); |
interrupts_restore(ipl); |
freed = slab_reclaim(SLAB_RECLAIM_ALL); |
ipl = interrupts_disable(); |
spinlock_lock(&zones.lock); |
if (freed > 0) |
znum = find_free_zone(order, |
FRAME_TO_ZONE_FLAGS(flags), hint); |
/branches/dd/kernel/generic/src/syscall/syscall.c |
---|
48,7 → 48,7 |
#include <synch/futex.h> |
#include <synch/smc.h> |
#include <ddi/ddi.h> |
#include <event/event.h> |
#include <ipc/event.h> |
#include <security/cap.h> |
#include <sysinfo/sysinfo.h> |
#include <console/console.h> |
/branches/dd/kernel/generic/src/ipc/event.c |
---|
0,0 → 1,155 |
/* |
* Copyright (c) 2009 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. |
*/ |
/** @addtogroup generic |
* @{ |
*/ |
/** |
* @file |
* @brief Kernel event notifications. |
*/ |
#include <ipc/event.h> |
#include <ipc/event_types.h> |
#include <mm/slab.h> |
#include <arch/types.h> |
#include <synch/spinlock.h> |
#include <console/console.h> |
#include <memstr.h> |
#include <errno.h> |
#include <arch.h> |
/** |
* The events array. |
* Arranging the events in this two-dimensional array should decrease the |
* likelyhood of cacheline ping-pong. |
*/ |
static event_t events[EVENT_END]; |
/** Initialize kernel events. */ |
void event_init(void) |
{ |
unsigned int i; |
for (i = 0; i < EVENT_END; i++) { |
spinlock_initialize(&events[i].lock, "event.lock"); |
events[i].answerbox = NULL; |
events[i].counter = 0; |
events[i].method = 0; |
} |
} |
static int |
event_subscribe(event_type_t evno, unative_t method, answerbox_t *answerbox) |
{ |
if (evno >= EVENT_END) |
return ELIMIT; |
spinlock_lock(&events[evno].lock); |
int res; |
if (events[evno].answerbox == NULL) { |
events[evno].answerbox = answerbox; |
events[evno].method = method; |
events[evno].counter = 0; |
res = EOK; |
} else |
res = EEXISTS; |
spinlock_unlock(&events[evno].lock); |
return res; |
} |
unative_t sys_event_subscribe(unative_t evno, unative_t method) |
{ |
return (unative_t) event_subscribe((event_type_t) evno, (unative_t) |
method, &TASK->answerbox); |
} |
bool event_is_subscribed(event_type_t evno) |
{ |
bool res; |
ASSERT(evno < EVENT_END); |
spinlock_lock(&events[evno].lock); |
res = events[evno].answerbox != NULL; |
spinlock_unlock(&events[evno].lock); |
return res; |
} |
void event_cleanup_answerbox(answerbox_t *answerbox) |
{ |
unsigned int i; |
for (i = 0; i < EVENT_END; i++) { |
spinlock_lock(&events[i].lock); |
if (events[i].answerbox == answerbox) { |
events[i].answerbox = NULL; |
events[i].counter = 0; |
events[i].method = 0; |
} |
spinlock_unlock(&events[i].lock); |
} |
} |
void |
event_notify(event_type_t evno, unative_t a1, unative_t a2, unative_t a3, |
unative_t a4, unative_t a5) |
{ |
ASSERT(evno < EVENT_END); |
spinlock_lock(&events[evno].lock); |
if (events[evno].answerbox != NULL) { |
call_t *call = ipc_call_alloc(FRAME_ATOMIC); |
if (call) { |
call->flags |= IPC_CALL_NOTIF; |
call->priv = ++events[evno].counter; |
IPC_SET_METHOD(call->data, events[evno].method); |
IPC_SET_ARG1(call->data, a1); |
IPC_SET_ARG2(call->data, a2); |
IPC_SET_ARG3(call->data, a3); |
IPC_SET_ARG4(call->data, a4); |
IPC_SET_ARG5(call->data, a5); |
spinlock_lock(&events[evno].answerbox->irq_lock); |
list_append(&call->link, &events[evno].answerbox->irq_notifs); |
spinlock_unlock(&events[evno].answerbox->irq_lock); |
waitq_wakeup(&events[evno].answerbox->wq, WAKEUP_FIRST); |
} |
} |
spinlock_unlock(&events[evno].lock); |
} |
/** @} |
*/ |
Property changes: |
Added: svn:mergeinfo |
/branches/dd/kernel/generic/src/ipc/ipc.c |
---|
44,7 → 44,7 |
#include <synch/synch.h> |
#include <ipc/ipc.h> |
#include <ipc/kbox.h> |
#include <event/event.h> |
#include <ipc/event.h> |
#include <errno.h> |
#include <mm/slab.h> |
#include <arch.h> |
51,8 → 51,6 |
#include <proc/task.h> |
#include <memstr.h> |
#include <debug.h> |
#include <print.h> |
#include <console/console.h> |
#include <proc/thread.h> |
/branches/dd/kernel/generic/src/ipc/irq.c |
---|
128,13 → 128,14 |
/** Register an answerbox as a receiving end for IRQ notifications. |
* |
* @param box Receiving answerbox. |
* @param inr IRQ number. |
* @param devno Device number. |
* @param method Method to be associated with the notification. |
* @param ucode Uspace pointer to top-half pseudocode. |
* @param box Receiving answerbox. |
* @param inr IRQ number. |
* @param devno Device number. |
* @param method Method to be associated with the notification. |
* @param ucode Uspace pointer to top-half pseudocode. |
* |
* @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success. |
* @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success. |
* |
*/ |
int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno, |
unative_t method, irq_code_t *ucode) |
142,11 → 143,12 |
ipl_t ipl; |
irq_code_t *code; |
irq_t *irq; |
link_t *hlp; |
unative_t key[] = { |
(unative_t) inr, |
(unative_t) devno |
}; |
if (ucode) { |
code = code_from_uspace(ucode); |
if (!code) |
154,7 → 156,7 |
} else { |
code = NULL; |
} |
/* |
* Allocate and populate the IRQ structure. |
*/ |
169,7 → 171,7 |
irq->notif_cfg.method = method; |
irq->notif_cfg.code = code; |
irq->notif_cfg.counter = 0; |
/* |
* Enlist the IRQ structure in the uspace IRQ hash table and the |
* answerbox's list. |
176,23 → 178,28 |
*/ |
ipl = interrupts_disable(); |
spinlock_lock(&irq_uspace_hash_table_lock); |
spinlock_lock(&irq->lock); |
spinlock_lock(&box->irq_lock); |
if (hash_table_find(&irq_uspace_hash_table, key)) { |
hlp = hash_table_find(&irq_uspace_hash_table, key); |
if (hlp) { |
irq_t *hirq __attribute__((unused)) |
= hash_table_get_instance(hlp, irq_t, link); |
/* hirq is locked */ |
spinlock_unlock(&hirq->lock); |
code_free(code); |
spinlock_unlock(&box->irq_lock); |
spinlock_unlock(&irq->lock); |
spinlock_unlock(&irq_uspace_hash_table_lock); |
free(irq); |
interrupts_restore(ipl); |
return EEXISTS; |
} |
spinlock_lock(&irq->lock); /* Not really necessary, but paranoid */ |
spinlock_lock(&box->irq_lock); |
hash_table_insert(&irq_uspace_hash_table, key, &irq->link); |
list_append(&irq->notif_cfg.link, &box->irq_head); |
spinlock_unlock(&box->irq_lock); |
spinlock_unlock(&irq->lock); |
spinlock_unlock(&irq_uspace_hash_table_lock); |
interrupts_restore(ipl); |
return EOK; |
} |
222,7 → 229,7 |
return ENOENT; |
} |
irq = hash_table_get_instance(lnk, irq_t, link); |
spinlock_lock(&irq->lock); |
/* irq is locked */ |
spinlock_lock(&box->irq_lock); |
ASSERT(irq->notif_cfg.answerbox == box); |
233,11 → 240,19 |
/* Remove the IRQ from the answerbox's list. */ |
list_remove(&irq->notif_cfg.link); |
/* |
* We need to drop the IRQ lock now because hash_table_remove() will try |
* to reacquire it. That basically violates the natural locking order, |
* but a deadlock in hash_table_remove() is prevented by the fact that |
* we already held the IRQ lock and didn't drop the hash table lock in |
* the meantime. |
*/ |
spinlock_unlock(&irq->lock); |
/* Remove the IRQ from the uspace IRQ hash table. */ |
hash_table_remove(&irq_uspace_hash_table, key, 2); |
spinlock_unlock(&irq_uspace_hash_table_lock); |
spinlock_unlock(&irq->lock); |
spinlock_unlock(&box->irq_lock); |
/* Free up the IRQ structure. */ |
291,13 → 306,21 |
/* Unlist from the answerbox. */ |
list_remove(&irq->notif_cfg.link); |
/* Remove from the hash table. */ |
hash_table_remove(&irq_uspace_hash_table, key, 2); |
/* Free up the pseudo code and associated structures. */ |
code_free(irq->notif_cfg.code); |
/* |
* We need to drop the IRQ lock now because hash_table_remove() |
* will try to reacquire it. That basically violates the natural |
* locking order, but a deadlock in hash_table_remove() is |
* prevented by the fact that we already held the IRQ lock and |
* didn't drop the hash table lock in the meantime. |
*/ |
spinlock_unlock(&irq->lock); |
/* Remove from the hash table. */ |
hash_table_remove(&irq_uspace_hash_table, key, 2); |
free(irq); |
} |