Kernel: Add new generic Printer class

kprint is now just a #define to Printer::print<terminal_putc>.

This allows us to use same print formatting for serial output :)
This commit is contained in:
Bananymous 2022-12-10 00:33:03 +02:00
parent ac2ba8bc56
commit 3d3f12bd30
9 changed files with 480 additions and 174 deletions

View File

@ -36,6 +36,7 @@ kernel/Keyboard.o \
kernel/kmalloc.o \
kernel/PIC.o \
kernel/PIT.o \
kernel/Serial.o \
kernel/SSP.o \
OBJS=\

View File

@ -5,10 +5,12 @@
#include "vga.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
namespace TTY
{
static size_t VGA_WIDTH;
static size_t VGA_HEIGHT;
static uint16_t* VGA_MEMORY;
@ -18,20 +20,20 @@ static size_t terminal_col;
static uint8_t terminal_color;
static uint16_t* terminal_buffer;
void terminal_putentryat(unsigned char c, uint8_t color, size_t x, size_t y)
void putentryat(unsigned char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_clear()
void clear()
{
for (size_t y = 0; y < VGA_HEIGHT; y++)
for (size_t x = 0; x < VGA_WIDTH; x++)
terminal_putentryat(' ', terminal_color, x, y);
putentryat(' ', terminal_color, x, y);
}
void terminal_initialize()
void initialize()
{
if (s_multiboot_info->flags & (1 << 12))
{
@ -51,19 +53,19 @@ void terminal_initialize()
terminal_col = 0;
terminal_color = vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
terminal_buffer = VGA_MEMORY;
terminal_clear();
clear();
if (s_multiboot_info->flags & (1 << 12))
if (s_multiboot_info->framebuffer.type != 2)
Kernel::panic("Invalid framebuffer_type in multiboot info");
}
void terminal_setcolor(uint8_t color)
void setcolor(uint8_t color)
{
terminal_color = color;
}
void terminal_scroll_line(size_t line)
void scroll_line(size_t line)
{
for (size_t x = 0; x < VGA_WIDTH; x++)
{
@ -72,13 +74,13 @@ void terminal_scroll_line(size_t line)
}
}
void terminal_clear_line(size_t line)
void clear_line(size_t line)
{
for (size_t x = 0; x < VGA_WIDTH; x++)
terminal_putentryat(' ', terminal_color, x, line);
putentryat(' ', terminal_color, x, line);
}
static void terminal_update_cursor()
static void update_cursor()
{
uint16_t pos = terminal_row * VGA_WIDTH + terminal_col;
IO::outb(0x3D4, 0x0F);
@ -87,14 +89,14 @@ static void terminal_update_cursor()
IO::outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF));
}
void terminal_set_cursor_pos(int x, int y)
void set_cursor_pos(int x, int y)
{
terminal_row = y;
terminal_col = x;
terminal_update_cursor();
update_cursor();
}
void terminal_putchar(char c)
void putchar(char c)
{
if (c == '\t')
c = ' ';
@ -108,11 +110,11 @@ void terminal_putchar(char c)
{
if (terminal_col > 0)
terminal_col--;
terminal_putentryat(' ', terminal_color, terminal_col, terminal_row);
putentryat(' ', terminal_color, terminal_col, terminal_row);
}
else
{
terminal_putentryat(c, terminal_color, terminal_col, terminal_row);
putentryat(c, terminal_color, terminal_col, terminal_row);
terminal_col++;
}
@ -125,23 +127,29 @@ void terminal_putchar(char c)
if (terminal_row == VGA_HEIGHT)
{
for (size_t line = 1; line < VGA_HEIGHT; line++)
terminal_scroll_line(line);
terminal_clear_line(VGA_HEIGHT - 1);
scroll_line(line);
clear_line(VGA_HEIGHT - 1);
terminal_col = 0;
terminal_row = VGA_HEIGHT - 1;
}
terminal_update_cursor();
update_cursor();
}
void terminal_write(const char* data, size_t size)
void write(const char* data, size_t size)
{
for (size_t i = 0; i < size; i++)
terminal_putchar(data[i]);
putchar(data[i]);
}
void terminal_writestring(const char* data)
void writestring(const char* data)
{
terminal_write(data, strlen(data));
while (*data)
{
putchar(*data);
data++;
}
}
}

View File

@ -0,0 +1,233 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
namespace Printer
{
struct ValueFormat;
template<void(*PUTC_LIKE)(char)>
static void print(const char* format);
template<void(*PUTC_LIKE)(char), typename Arg, typename... Args>
static void print(const char* format, Arg arg, Args... args);
template<void(*PUTC_LIKE)(char), typename... Args>
static void println(const char* format, Args... args);
template<void(*PUTC_LIKE)(char), typename T>
static size_t print_argument(const char* format, T arg);
template<void(*PUTC_LIKE)(char), typename T>
static void print_value(T value, const ValueFormat& format);
/*
IMPLEMENTATION
*/
struct ValueFormat
{
int base = 10;
int percision = 3;
bool upper = false;
};
template<void(*PUTC_LIKE)(char)>
void print(const char* format)
{
while (*format)
{
PUTC_LIKE(*format);
format++;
}
}
template<void(*PUTC_LIKE)(char), typename Arg, typename... Args>
void print(const char* format, Arg arg, Args... args)
{
while (*format && *format != '{')
{
PUTC_LIKE(*format);
format++;
}
if (*format == '{')
{
size_t arg_len = print_argument<PUTC_LIKE>(format, arg);
if (arg_len == size_t(-1))
return print<PUTC_LIKE>(format);
print<PUTC_LIKE>(format + arg_len, args...);
}
}
template<void(*PUTC_LIKE)(char), typename... Args>
void println(const char* format, Args... args)
{
print<PUTC_LIKE>(format, args...);
PUTC_LIKE('\n');
}
template<void(*PUTC_LIKE)(char), typename Arg>
size_t print_argument(const char* format, Arg argument)
{
ValueFormat value_format;
if (format[0] != '{')
return size_t(-1);
size_t i = 1;
do
{
if (!format[i] || format[i] == '}')
break;
switch (format[i])
{
case 'b': value_format.base = 2; value_format.upper = false; i++; break;
case 'B': value_format.base = 2; value_format.upper = true; i++; break;
case 'o': value_format.base = 8; value_format.upper = false; i++; break;
case 'O': value_format.base = 8; value_format.upper = true; i++; break;
case 'd': value_format.base = 10; value_format.upper = false; i++; break;
case 'D': value_format.base = 10; value_format.upper = true; i++; break;
case 'h': value_format.base = 16; value_format.upper = false; i++; break;
case 'H': value_format.base = 16; value_format.upper = true; i++; break;
default: break;
}
if (!format[i] || format[i] == '}')
break;
if (format[i] == '.')
{
i++;
int percision = 0;
while ('0' <= format[i] && format[i] <= '9')
{
percision = (percision * 10) + (format[i] - '0');
i++;
}
value_format.percision = percision;
}
} while(false);
if (format[i] != '}')
return size_t(-1);
print_value<PUTC_LIKE>(argument, value_format);
return i + 1;
}
static char value_to_base_char(uint8_t value, int base, bool upper)
{
if (base <= 10)
return value + '0';
if (base <= 36)
{
if (value < 10)
return value + '0';
return value + (upper ? 'A' : 'a') - 10;
}
return '?';
}
template<void(*PUTC_LIKE)(char), typename T>
void print_integer(T value, const ValueFormat& format)
{
if (value == 0)
return PUTC_LIKE('0');
bool sign = false;
// Fits signed 64-bit binary number and null
char buffer[66];
char* ptr = buffer + sizeof(buffer);
*(--ptr) = '\0';
if (value < 0)
{
sign = true;
T digit = (format.base - (value % format.base)) % format.base;
*(--ptr) = value_to_base_char(digit, format.base, format.upper);
value = -(value / format.base);
}
while (value)
{
*(--ptr) = value_to_base_char(value % format.base, format.base, format.upper);
value /= format.base;
}
if (sign)
*(--ptr) = '-';
print<PUTC_LIKE>(ptr);
}
template<void(*PUTC_LIKE)(char), typename T>
void print_floating(T value, const ValueFormat& format)
{
int64_t int_part = (int64_t)value;
T frac_part = value - (T)int_part;
if (frac_part < 0)
frac_part = -frac_part;
print_integer<PUTC_LIKE>(int_part, format);
if (format.percision > 0)
PUTC_LIKE('.');
for (int i = 0; i < format.percision; i++)
{
frac_part *= format.base;
if (i == format.percision - 1)
frac_part += 0.5;
PUTC_LIKE(value_to_base_char((uint8_t)frac_part % format.base, format.base, format.upper));
}
}
template<void(*PUTC_LIKE)(char)>
void print_pointer(void* ptr, const ValueFormat& format)
{
uintptr_t value = (uintptr_t)ptr;
print<PUTC_LIKE>("0x");
for (int i = sizeof(void*) * 8 - 4; i >= 0; i -= 4)
PUTC_LIKE(value_to_base_char((value >> i) & 0xF, 16, format.upper));
}
/*
TEMPLATE SPECIALIZATIONS
*/
template<void(*PUTC_LIKE)(char)> void print_value(short value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(int value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(long long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(unsigned short value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(unsigned int value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(unsigned long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(unsigned long long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(float value, const ValueFormat& format) { print_floating<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(double value, const ValueFormat& format) { print_floating<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(long double value, const ValueFormat& format) { print_floating<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(char value, const ValueFormat&) { PUTC_LIKE(value); }
template<void(*PUTC_LIKE)(char)> void print_value(signed char value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(unsigned char value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
template<void(*PUTC_LIKE)(char), typename T> void print_value(T* value, const ValueFormat& format) { print_pointer<PUTC_LIKE>((void*)value, format); }
template<void(*PUTC_LIKE)(char)> void print_value(const char* value, const ValueFormat&) { print<PUTC_LIKE>(value);}
}

View File

@ -0,0 +1,15 @@
#pragma once
#include <kernel/Printer.h>
#define dprint Printer::print<Serial::serial_putc>
#define dprintln Printer::println<Serial::serial_putc>
namespace Serial
{
void initialize();
void serial_putc(char);
}

View File

@ -1,7 +1,13 @@
#pragma once
#include <kernel/Printer.h>
#include <kernel/tty.h>
#define kprint Printer::print<TTY::putchar>
#define kprintln Printer::println<TTY::putchar>
#if 0
#include <stdint.h>
#include <string.h>
@ -67,6 +73,8 @@ static void kprint_floating(T value, int percision)
terminal_write(".", 1);
while (percision > 0)
{
frac_part *= 10;
@ -79,10 +87,7 @@ static void kprint_floating(T value, int percision)
}
template<typename T>
static void kprint_val(T)
{
terminal_writestring("<unknown type>");
}
static void kprint_val(T);
static void kprint(const char* format)
{
@ -95,7 +100,7 @@ static void kprint(const char* format, Arg arg, Args... args)
const char* next = strstr(format, "{}");
if (next == NULL)
{
terminal_writestring(format);
terminal_write(format, strlen(format));
return;
}
terminal_write(format, next - format);
@ -124,8 +129,6 @@ template<> void kprint_val(unsigned char value) { kprint_unsigned(value); }
template<> void kprint_val(const char* value) { terminal_writestring(value); }
template<> void kprint_val(char* value) { terminal_writestring(value); }
static char bits_to_hex(uint8_t val)
{
val = val & 0xF;
@ -137,37 +140,9 @@ static char bits_to_hex(uint8_t val)
template<> void kprint_val(void* value)
{
terminal_write("0x", 2);
if constexpr(sizeof(void*) == sizeof(uint32_t))
{
uint32_t addr = (uint32_t)value;
terminal_putchar(bits_to_hex(addr >> 28));
terminal_putchar(bits_to_hex(addr >> 24));
terminal_putchar(bits_to_hex(addr >> 20));
terminal_putchar(bits_to_hex(addr >> 16));
terminal_putchar(bits_to_hex(addr >> 12));
terminal_putchar(bits_to_hex(addr >> 8));
terminal_putchar(bits_to_hex(addr >> 4));
terminal_putchar(bits_to_hex(addr >> 0));
}
else
{
uint64_t addr = (uint64_t)value;
terminal_putchar(bits_to_hex(addr >> 60));
terminal_putchar(bits_to_hex(addr >> 56));
terminal_putchar(bits_to_hex(addr >> 52));
terminal_putchar(bits_to_hex(addr >> 48));
terminal_putchar(bits_to_hex(addr >> 44));
terminal_putchar(bits_to_hex(addr >> 40));
terminal_putchar(bits_to_hex(addr >> 36));
terminal_putchar(bits_to_hex(addr >> 32));
terminal_putchar(bits_to_hex(addr >> 28));
terminal_putchar(bits_to_hex(addr >> 24));
terminal_putchar(bits_to_hex(addr >> 20));
terminal_putchar(bits_to_hex(addr >> 16));
terminal_putchar(bits_to_hex(addr >> 12));
terminal_putchar(bits_to_hex(addr >> 8));
terminal_putchar(bits_to_hex(addr >> 4));
terminal_putchar(bits_to_hex(addr >> 0));
}
for (int i = sizeof(void*) * 8 - 4; i >= 0; i -= 4)
terminal_putchar(bits_to_hex(addr >> i));
}
#endif

View File

@ -1,10 +1,14 @@
#pragma once
#include <stddef.h>
#include <sys/cdefs.h>
void terminal_initialize();
void terminal_putchar(char c);
void terminal_write(const char* data, size_t size);
void terminal_writestring(const char* data);
void terminal_set_cursor_pos(int x, int y);
namespace TTY
{
void initialize();
void putchar(char c);
void write(const char* data, size_t size);
void writestring(const char* data);
void set_cursor_pos(int x, int y);
}

48
kernel/kernel/Serial.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <kernel/IO.h>
#include <kernel/kprint.h>
#include <kernel/Serial.h>
#define COM1_PORT 0x3f8
namespace Serial
{
static bool s_initialized = false;
void initialize()
{
IO::outb(COM1_PORT + 1, 0x00); // Disable all interrupts
IO::outb(COM1_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
IO::outb(COM1_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
IO::outb(COM1_PORT + 1, 0x00); // (hi byte)
IO::outb(COM1_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
IO::outb(COM1_PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
IO::outb(COM1_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
IO::outb(COM1_PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
IO::outb(COM1_PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
// Check if serial is faulty (i.e: not same byte as sent)
if(IO::inb(COM1_PORT + 0) != 0xAE) {
kprint("Could not initialize COM1 serial port\n");
return;
}
// If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
IO::outb(COM1_PORT + 4, 0x0F);
s_initialized = true;
}
int is_transmit_empty() {
return IO::inb(COM1_PORT + 5) & 0x20;
}
void serial_putc(char c)
{
if (!s_initialized)
return;
while (is_transmit_empty() == 0);
IO::outb(COM1_PORT, c);
}
}

View File

@ -1,14 +1,15 @@
#include <kernel/GDT.h>
#include <kernel/IDT.h>
#include <kernel/IO.h>
#include <kernel/Keyboard.h>
#include <kernel/kmalloc.h>
#include <kernel/kprint.h>
#include <kernel/multiboot.h>
#include <kernel/panic.h>
#include <kernel/PIC.h>
#include <kernel/PIT.h>
#include <kernel/Serial.h>
#include <kernel/tty.h>
#include <kernel/kprint.h>
#include <kernel/IO.h>
#include <string.h>
#include <stdlib.h>
@ -22,6 +23,12 @@ void on_key_press(Keyboard::Key key, uint8_t modifiers, bool pressed)
{
if (pressed)
{
if (key == Keyboard::Key::Escape)
{
kprint("time since boot: {} ms\n", PIT::ms_since_boot());
return;
}
char ascii = Keyboard::key_to_ascii(key, modifiers);
if (ascii)
kprint("{}", ascii);
@ -36,9 +43,13 @@ void kernel_main(multiboot_info_t* mbi, uint32_t magic)
s_multiboot_info = mbi;
if (magic != 0x2BADB002)
goto halt;
return;
terminal_initialize();
if (mbi->framebuffer.type != 2)
return;
TTY::initialize();
Serial::initialize();
kmalloc_initialize();
@ -49,11 +60,18 @@ void kernel_main(multiboot_info_t* mbi, uint32_t magic)
PIT::initialize();
Keyboard::initialize(on_key_press);
kprint("Hello from the kernel!\n");
ENABLE_INTERRUPTS();
kprintln("Hello from the kernel!");
dprintln("Hello emulator from kernel!");
int** lol = new int*[10];
for (int i = 0; i < 10; i++)
lol[i] = new int;
kprint("{.2}\n", -12.123f);
kprint("0x{.H}", 0xcafebabe);
halt:
for (;;)
{
asm("hlt");

View File

@ -2,4 +2,8 @@
set -e
. ./iso.sh
qemu-system-$(./target-triplet-to-arch.sh $HOST) -cdrom banan-os.iso
qemu-system-$(./target-triplet-to-arch.sh $HOST) \
-m 128 \
-drive format=raw,media=cdrom,file=banan-os.iso \
-serial stdio \