Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console functionality enhancement #248

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions common/cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ bool_cmd("hpet", opt_hpet);
bool opt_fpu = false;
bool_cmd("fpu", opt_fpu);

bool opt_qemu_console = false;
bool_cmd("qemu_console", opt_qemu_console);

const char *kernel_cmdline;

void __text_init cmdline_parse(const char *cmdline) {
Expand Down
33 changes: 20 additions & 13 deletions common/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@

#include <smp/smp.h>

#define QEMU_CONSOLE 0x0e9
#define SERIAL_CONSOLE (com_ports[0])

#define VPRINTK_BUF_SIZE 1024

static console_callback_t console_callbacks[2];
static console_callback_entry_t console_callbacks[2];
static unsigned int num_console_callbacks;

static void vprintk(const char *fmt, va_list args) {
Expand All @@ -56,8 +53,11 @@ static void vprintk(const char *fmt, va_list args) {
if (rc > (int) sizeof(buf))
panic("vprintk() buffer overflow\n");

for (i = 0; i < num_console_callbacks; i++)
console_callbacks[i](buf, rc);
for (i = 0; i < num_console_callbacks; i++) {
void *arg = console_callbacks[i].arg;

console_callbacks[i].cb(arg, buf, rc);
}

spin_unlock(&lock);
}
Expand All @@ -82,18 +82,25 @@ void AcpiOsPrintf(const char *Format, ...) {
}
#endif

void putchar(int c) { putc(SERIAL_CONSOLE, c); }
void serial_console_write(void *arg, const char *buf, size_t len) {
io_port_t port = (io_port_t) _ul(arg);

void serial_console_write(const char *buf, size_t len) {
serial_write(SERIAL_CONSOLE, buf, len);
serial_write(port, buf, len);
}

void qemu_console_write(const char *buf, size_t len) { puts(QEMU_CONSOLE, buf, len); }
void qemu_console_write(void *arg, const char *buf, size_t len) {
io_port_t port = (io_port_t) _ul(arg);

void vga_console_write(const char *buf, size_t len) { vga_write(buf, len, VGA_WHITE); }
puts(port, buf, len);
}

void vga_console_write(void *vga_memory, const char *buf, size_t len) {
vga_write(vga_memory, buf, len, VGA_WHITE);
}

void register_console_callback(console_callback_t cb) {
console_callbacks[num_console_callbacks++] = cb;
void register_console_callback(console_callback_t cb, void *arg) {
console_callbacks[num_console_callbacks].cb = cb;
console_callbacks[num_console_callbacks++].arg = arg;
}

void __noreturn panic(const char *fmt, ...) {
Expand Down
10 changes: 8 additions & 2 deletions common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ char cpu_identifier[49];
unsigned get_bsp_cpu_id(void) { return bsp_cpu_id; }
void set_bsp_cpu_id(unsigned cpu_id) { bsp_cpu_id = cpu_id; }

#define QEMU_CONSOLE_PORT 0x0e9
static void __text_init init_console(void) {
get_com_ports();

uart_init(com_ports[0], DEFAULT_BAUD_SPEED);
register_console_callback(serial_console_write);
register_console_callback(serial_console_write, _ptr(com_ports[0]));

printk("COM1: %x, COM2: %x\n", com_ports[0], com_ports[1]);

if (opt_qemu_console) {
register_console_callback(qemu_console_write, _ptr(QEMU_CONSOLE_PORT));
printk("Initialized QEMU console at port 0x%x", QEMU_CONSOLE_PORT);
}
}

static __always_inline void zero_bss(void) {
Expand Down Expand Up @@ -118,7 +124,7 @@ static void __text_init init_vga_console(void) {

printk("Enabling VGA support\n");
map_vga_area();
register_console_callback(vga_console_write);
register_console_callback(vga_console_write, paddr_to_virt_kern(VGA_START_ADDR));
}

void __noreturn __text_init kernel_start(uint32_t multiboot_magic,
Expand Down
4 changes: 2 additions & 2 deletions drivers/vga.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void vga_scroll_up(void) {
write_vga_buffer(--scroll_screen);
}

void vga_write(const char *buf, size_t len, vga_color_t color) {
void vga_write(void *vga_memory, const char *buf, size_t len, vga_color_t color) {
static int screen = 0, row = 0, col = 0;

for (unsigned int i = 0; i < len; i++) {
Expand All @@ -77,7 +77,7 @@ void vga_write(const char *buf, size_t len, vga_color_t color) {
}

scroll_screen = screen;
write_vga_buffer(screen);
memcpy(vga_memory, vga_buffer[screen], sizeof(vga_buffer[screen]));
}

void map_vga_area(void) {
Expand Down
1 change: 1 addition & 0 deletions include/cmdline.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ extern bool opt_pit;
extern bool opt_apic_timer;
extern bool opt_hpet;
extern bool opt_fpu;
extern bool opt_qemu_console;
extern const char *kernel_cmdline;

extern void cmdline_parse(const char *cmdline);
Expand Down
18 changes: 11 additions & 7 deletions include/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@

#include <ktf.h>

typedef void (*console_callback_t)(const char *buf, size_t len);
typedef void (*console_callback_t)(void *arg, const char *buf, size_t len);

struct console_callback_entry {
console_callback_t cb;
void *arg;
};
typedef struct console_callback_entry console_callback_entry_t;

extern void printk(const char *fmt, ...);

Expand All @@ -37,13 +43,11 @@ extern void printk(const char *fmt, ...);
printk("%s (%s.%d): " fmt, __FILE__, __func__, __LINE__, ##__VA_ARGS__); \
} while (0)

extern void putchar(int c);

extern void serial_console_write(const char *buf, size_t len);
extern void qemu_console_write(const char *buf, size_t len);
extern void vga_console_write(const char *buf, size_t len);
extern void serial_console_write(void *arg, const char *buf, size_t len);
extern void qemu_console_write(void *arg, const char *buf, size_t len);
extern void vga_console_write(void *arg, const char *buf, size_t len);

extern void register_console_callback(console_callback_t func);
extern void register_console_callback(console_callback_t func, void *arg);

extern void panic(const char *fmt, ...);

Expand Down
2 changes: 1 addition & 1 deletion include/drivers/vga.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef enum vga_color vga_color_t;
extern void vga_scroll_up(void);
extern void vga_scroll_down(void);

extern void vga_write(const char *buf, size_t len, vga_color_t color);
extern void vga_write(void *vga_memory, const char *buf, size_t len, vga_color_t color);

extern void map_vga_area(void);
#endif /* KTF_DRV_VGA_H */