Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4347 → Rev 4348

/branches/dynload/kernel/generic/src/console/console.c
41,21 → 41,22
#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>
#include <print.h>
#include <putchar.h>
#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;
68,9 → 69,6
/** Number of stored kernel log characters for uspace */
static size_t klog_uspace = 0;
 
/** Silence output */
bool silent = false;
 
/** Kernel log spinlock */
SPINLOCK_INITIALIZE(klog_lock);
 
77,10 → 75,28
/** Physical memory area used for klog buffer */
static parea_t klog_parea;
 
static indev_operations_t stdin_ops = {
.poll = NULL
};
 
/** Silence output */
bool silent = false;
 
/** Standard input and output character devices */
indev_t *stdin = NULL;
outdev_t *stdout = NULL;
 
indev_t *stdin_wire(void)
{
if (stdin == NULL) {
stdin = malloc(sizeof(indev_t), FRAME_ATOMIC);
if (stdin != NULL)
indev_initialize("stdin", stdin, &stdin_ops);
}
return stdin;
}
 
/** Initialize kernel logging facility
*
* The shared area contains kernel cyclic buffer. Userspace application may
93,7 → 109,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 → 115,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;
109,8 → 124,14
 
void grab_console(void)
{
bool prev = silent;
silent = false;
arch_grab_console();
/* Force the console to print the prompt */
if ((stdin) && (prev))
indev_push_character(stdin, '\n');
}
 
void release_console(void)
137,55 → 158,6
return true;
}
 
bool check_poll(indev_t *indev)
{
if (indev == NULL)
return false;
if (indev->op == NULL)
return false;
return (indev->op->poll != NULL);
}
 
/** Get character from input character device. Do not echo character.
*
* @param indev Input character device.
* @return Character read.
*
*/
uint8_t _getc(indev_t *indev)
{
if (atomic_get(&haltstate)) {
/* If we are here, we are hopefully on the processor that
* issued the 'halt' command, so proceed to read the character
* directly from input
*/
if (check_poll(indev))
return indev->op->poll(indev);
/* No other way of interacting with user */
interrupts_disable();
if (CPU)
printf("cpu%u: ", CPU->id);
else
printf("cpu: ");
printf("halted (no polling input)\n");
cpu_halt();
}
waitq_sleep(&indev->wq);
ipl_t ipl = interrupts_disable();
spinlock_lock(&indev->lock);
uint8_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN];
indev->counter--;
spinlock_unlock(&indev->lock);
interrupts_restore(ipl);
return ch;
}
 
/** Get string from input character device.
*
* Read characters from input character device until first occurrence
192,7 → 164,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 → 172,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 = indev_pop_character(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 = indev_pop_character(indev);
putchar(ch);
return ch;
}
254,16 → 228,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 → 269,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 → 294,7
} else
klog_update();
return count;
return size;
}
 
/** @}