From 4a9440dd626a505f73cef267558e99a6d816ba1c Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Sat, 13 Dec 2014 18:00:35 +0100 Subject: [PATCH] redesign of the IO interface - all messages are stored in a ring buffer - new devices (e.g. UART) are able to print previous messages --- arch/x86/include/asm/uart.h | 6 --- arch/x86/kernel/uart.c | 83 ++++++++++++++++++++------------- include/eduos/config.h.example | 1 + include/eduos/stdio.h | 5 ++ kernel/main.c | 4 +- libkern/stdio.c | 85 ++++++++++++++++++++++++++++------ 6 files changed, 129 insertions(+), 55 deletions(-) diff --git a/arch/x86/include/asm/uart.h b/arch/x86/include/asm/uart.h index b1803d9..f019159 100644 --- a/arch/x86/include/asm/uart.h +++ b/arch/x86/include/asm/uart.h @@ -60,12 +60,6 @@ int uart_puts(const char *text); */ int uart_putchar(unsigned char c); -/** @brief Start thread to handle inputs on the serial device - * - * @return error code of create_kernel_thread - */ -int uart_enable_input(void); - #ifdef __cplusplus } #endif diff --git a/arch/x86/kernel/uart.c b/arch/x86/kernel/uart.c index ad5de22..f35bba0 100644 --- a/arch/x86/kernel/uart.c +++ b/arch/x86/kernel/uart.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -79,23 +80,32 @@ #define UART_LCR_STOP 0x04 /* Stop bits: 0=1 bit, 1=2 bits */ #define UART_LCR_WLEN8 0x03 /* Wordlength: 8 bits */ +static uint8_t mmio = 0; static uint32_t iobase = 0; static tid_t id; static mailbox_uint8_t input_queue; -#define MEMMAPIO -#if 1 -#define READ_FROM_UART(x) inportb(iobase + x) -#define WRITE_TO_UART(x, y) outportb(iobase + x, y) -#else -#define READ_FROM_UART(x) inportb(iobase + x) -#define WRITE_TO_UART(x, y) outportb(iobase + x, y) -#endif +static inline unsigned char read_from_uart(uint32_t off) +{ + if (mmio) + return *((volatile unsigned char*) (iobase + off)); + else + return inportb(iobase + off); +} + +static void write_to_uart(uint32_t off, unsigned char c) +{ + if (mmio) + *((volatile unsigned char*) (iobase + off)) = c; + else + outportb(iobase + off, c); +} + /* Get a single character on a serial device */ static unsigned char uart_getchar(void) { - return READ_FROM_UART(UART_RX); + return read_from_uart(UART_RX); } /* Puts a single character on a serial device */ @@ -104,7 +114,7 @@ int uart_putchar(unsigned char c) if (!iobase) return 0; - WRITE_TO_UART(UART_TX, c); + write_to_uart(UART_TX, c); return (int) c; } @@ -126,7 +136,7 @@ int uart_puts(const char *text) /* Handles all UART's interrupt */ static void uart_handler(struct state *s) { - unsigned char c = READ_FROM_UART(UART_IIR); + unsigned char c = read_from_uart(UART_IIR); while (!(c & UART_IIR_NO_INT)) { if (c & UART_IIR_RDI) { @@ -135,7 +145,7 @@ static void uart_handler(struct state *s) mailbox_uint8_post(&input_queue, c); } - c = READ_FROM_UART(UART_IIR); + c = read_from_uart(UART_IIR); } } @@ -153,18 +163,6 @@ static int uart_thread(void* arg) return 0; } -int uart_enable_input(void) -{ - int err = create_kernel_task(&id, uart_thread, NULL, HIGH_PRIO); - - if (BUILTIN_EXPECT(err, 0)) - kprintf("Failed to create task (uart): %d\n", err); - else - kputs("Create task to handle incoming messages (uart)\n"); - - return err; -} - static void uart_config(void) { mailbox_uint8_init(&input_queue); @@ -174,7 +172,7 @@ static void uart_config(void) * clear RX and TX FIFO * set irq trigger to 8 bytes */ - WRITE_TO_UART(UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | UART_FCR_TRIGGER_1); + write_to_uart(UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | UART_FCR_TRIGGER_1); /* * 8bit word length @@ -183,19 +181,25 @@ static void uart_config(void) * set DLAB=1 */ char lcr = UART_LCR_WLEN8 | UART_LCR_DLAB; - WRITE_TO_UART(UART_LCR, lcr); + write_to_uart(UART_LCR, lcr); /* * set baudrate to 115200 (on qemu) */ - WRITE_TO_UART(UART_DLL, 0x01); - WRITE_TO_UART(UART_DLM, 0x00); + write_to_uart(UART_DLL, 0x01); + write_to_uart(UART_DLM, 0x00); /* set DLAB=0 */ - WRITE_TO_UART(UART_LCR, lcr & (~UART_LCR_DLAB)); + write_to_uart(UART_LCR, lcr & (~UART_LCR_DLAB)); /* enable interrupt */ - WRITE_TO_UART(UART_IER, UART_IER_RDI | UART_IER_RLSI | UART_IER_THRI); + write_to_uart(UART_IER, UART_IER_RDI | UART_IER_RLSI | UART_IER_THRI); + + int err = create_kernel_task(&id, uart_thread, NULL, HIGH_PRIO); + if (BUILTIN_EXPECT(err, 0)) + kprintf("Failed to create task for the uart device: %d\n", err); + + koutput_add_uart(); } int uart_init(void) @@ -213,12 +217,25 @@ int uart_init(void) return -1; Lsuccess: - // we use COM1 - iobase = 0x03f8; - irq_install_handler(32+4, uart_handler); + if (pci_info.type[0]) { + // we use COM1 + mmio = 0; + iobase = 0x3F8; + irq_install_handler(32+4, uart_handler); + } else { + mmio = 1; + iobase = pci_info.base[0]; + irq_install_handler(32+pci_info.irq, uart_handler); + page_map(iobase & PAGE_MASK, iobase & PAGE_MASK, 1, PG_GLOBAL | PG_RW | PG_PCD); + } // configure uart uart_config(); +#else + // we use COM1 + mmio = 0; + iobase = 0x3F8; + irq_install_handler(32+4, uart_handler); #endif return 0; diff --git a/include/eduos/config.h.example b/include/eduos/config.h.example index 33a1429..b8a375f 100644 --- a/include/eduos/config.h.example +++ b/include/eduos/config.h.example @@ -40,6 +40,7 @@ extern "C" { #define CACHE_LINE 64 #define KERNEL_STACK_SIZE (8<<10) /* 8 KiB */ #define BITMAP_SIZE (128<<5) /* for 128 MiB of RAM */ +#define KMSG_SIZE (8*1024) #define INT_SYSCALL 0x80 #define MAILBOX_SIZE 32 diff --git a/include/eduos/stdio.h b/include/eduos/stdio.h index ffb9552..73835a9 100644 --- a/include/eduos/stdio.h +++ b/include/eduos/stdio.h @@ -77,6 +77,11 @@ int ksnprintf(char *str, size_t size, const char *format, ...); */ int kvprintf(char const *fmt, void (*func) (int, void *), void *arg, int radix, va_list ap); +/** + * Add UART device to dump kernel messages + */ +int koutput_add_uart(void); + #ifdef __cplusplus } #endif diff --git a/kernel/main.c b/kernel/main.c index 1d9d039..6415928 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -117,14 +117,14 @@ static int eduos_init(void) // initialize .bss section memset((void*)&bss_start, 0x00, ((size_t) &bss_end - (size_t) &bss_start)); + koutput_init(); system_init(); irq_init(); timer_init(); - koutput_init(); multitasking_init(); memory_init(); #ifdef CONFIG_UART - uart_enable_input(); + uart_init(); #endif return 0; diff --git a/libkern/stdio.c b/libkern/stdio.c index 771cc64..8530ae4 100644 --- a/libkern/stdio.c +++ b/libkern/stdio.c @@ -29,16 +29,30 @@ #include #include #include -#include +#include +#include +#ifdef CONFIG_VGA #include +#endif +#ifdef CONFIG_UART +#include +#endif +#define NO_EARLY_PRINT 0x00 +#define VGA_EARLY_PRINT 0x01 +#define UART_EARLY_PRINT 0x02 + +#ifdef CONFIG_VGA +static uint32_t early_print = VGA_EARLY_PRINT; +#else +static uint32_t early_print = NO_EARLY_PRINT; +#endif static spinlock_irqsave_t olock = SPINLOCK_IRQSAVE_INIT; +static atomic_int32_t kmsg_counter = ATOMIC_INIT(0); +static unsigned char kmessages[KMSG_SIZE] __attribute__ ((section(".kmsg"))) = {[0 ... KMSG_SIZE-1] = 0x00}; int koutput_init(void) { -#ifdef CONFIG_UART - uart_init(); -#endif #ifdef CONFIG_VGA vga_init(); #endif @@ -48,24 +62,67 @@ int koutput_init(void) int kputchar(int c) { - spinlock_irqsave_lock(&olock); -#ifdef CONFIG_UART - uart_putchar(c); -#endif + int pos; + + if (early_print != NO_EARLY_PRINT) + spinlock_irqsave_lock(&olock); + + pos = atomic_int32_inc(&kmsg_counter); + kmessages[pos % KMSG_SIZE] = (unsigned char) c; + #ifdef CONFIG_VGA - vga_putchar(c); + if (early_print & VGA_EARLY_PRINT) + vga_putchar(c); #endif - spinlock_irqsave_unlock(&olock); +#ifdef CONFIG_UART + if (early_print & UART_EARLY_PRINT) + uart_putchar(c); +#endif + + if (early_print != NO_EARLY_PRINT) + spinlock_irqsave_unlock(&olock); return 1; } int kputs(const char *str) { - int i; + int pos, i, len = strlen(str); - for(i=0; str[i] != '\0'; i++) - kputchar((int) str[i]); + if (early_print != NO_EARLY_PRINT) + spinlock_irqsave_lock(&olock); - return i; + for(i=0; i