Skip to content

Commit

Permalink
Create log subsystem
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjian85 committed Jun 13, 2024
1 parent ef4c2ca commit 062a03f
Show file tree
Hide file tree
Showing 23 changed files with 244 additions and 71 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ endif()

add_executable(karg
src/dev.c
src/dev_table.c
src/drivers/fb.c
src/drivers/fbcon.c
src/drivers/font.c
Expand All @@ -19,6 +18,7 @@ add_executable(karg
src/init.c
src/intr.c
src/kmain.c
src/log.c
src/mm/kalloc.c
src/mm/page_alloc.c
src/proc.c
Expand Down
1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define FB_DEV_PROBE_CAPACITY 8
#define INTR_ISR_CAPACITY 256
#define KHEAP_SIZE 0x400000
#define LOG_BUF_SIZE 4096
#define PROC_FD_CAPACITY 64
#define PROC_TABLE_SIZE 256
#define SCHED_TIMESLICE 10000000
Expand Down
21 changes: 21 additions & 0 deletions include/dev.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <compiler.h>
#include <driver.h>
#include <drivers/plic.h>
#include <stdarg.h>
#include <types.h>

Expand All @@ -15,3 +17,22 @@ isize dev_read(dev_t dev, u8 *buf, usize size);
isize dev_write(dev_t dev, const u8 *buf, usize size);
i32 dev_ioctl(dev_t dev, u32 req, ...);
i32 dev_vioctl(dev_t dev, u32 req, va_list args);

typedef struct {
const char *name;
usize addr;
irq_t irq;
u8 reg_size;
} dev_node_t;

extern const dev_node_t DEV_TABLE[];

typedef struct {
const char *name;
const char *compat;
i32 (*init)(const dev_node_t *node);
} dev_init_t;

#define dev_init(compat, func) \
__section(.init.dev) \
__used static dev_init_t _DEV_INIT = {_MODULE_NAME, compat, func}
22 changes: 0 additions & 22 deletions include/dev_table.h

This file was deleted.

14 changes: 11 additions & 3 deletions include/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
#include <compiler.h>
#include <types.h>

typedef struct {
const char *name;
i32 (*init)(void);
} init_t;

#define module_pre_init(func) \
__section(.init.pre_init) __used static i32 (*_pre_init)(void) = func
__section(.init.pre_init) \
__used static const init_t _PRE_INIT = {_MODULE_NAME, func}
#define module_init(func) \
__section(.init.init) __used static i32 (*_init)(void) = func
__section(.init.init) \
__used static const init_t _INIT = {_MODULE_NAME, func}
#define module_post_init(func) \
__section(.init.post_init) __used static i32 (*_post_init)(void) = func
__section(.init.post_init) \
__used static const init_t _POST_INIT = {_MODULE_NAME, func}

i32 init_modules(void);
11 changes: 11 additions & 0 deletions include/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

typedef enum {
LOG_SEVERITY_INFO,
} log_severity_t;

void klogf(log_severity_t severity, const char *module_name, const char *fmt,
...);

#define log_info(fmt, ...) \
klogf(LOG_SEVERITY_INFO, _MODULE_NAME, fmt, ##__VA_ARGS__)
3 changes: 3 additions & 0 deletions include/module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#define MODULE_NAME(name) static const char *const _MODULE_NAME = name
14 changes: 12 additions & 2 deletions kernel.ld
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ SECTIONS {
}

. = ALIGN(4K);
PROVIDE(_pre_init_start = .);
.init.pre_init : {
*(.init.pre_init)
}
PROVIDE(_pre_init_end = .);

PROVIDE(_init_start = .);
.init.init : {
*(.init.pre_init)
*(.init.init)
*(.init.post_init)
}
PROVIDE(_init_end = .);

PROVIDE(_post_init_start = .);
.init.post_init : {
*(.init.post_init)
}
PROVIDE(_post_init_end = .);

PROVIDE(_dev_init_start = .);
.init.dev : {
*(.init.dev)
Expand Down
29 changes: 29 additions & 0 deletions src/dev.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#include <dev.h>

#include <init.h>
#include <log.h>
#include <module.h>
#include <string.h>

MODULE_NAME("dev");

extern dev_init_t _dev_init_start, _dev_init_end;

isize dev_read(dev_t dev, u8 *buf, usize size) {
return dev.driver->read(dev.num, buf, size);
}
Expand All @@ -19,3 +28,23 @@ i32 dev_ioctl(dev_t dev, u32 req, ...) {
i32 dev_vioctl(dev_t dev, u32 req, va_list args) {
return dev.driver->ioctl(dev.num, req, args);
}

static i32 init(void) {
for (usize i = 0; DEV_TABLE[i].name; i++) {
const dev_node_t *node = &DEV_TABLE[i];
for (dev_init_t *init_ptr = &_dev_init_start; init_ptr < &_dev_init_end;
init_ptr++) {
if (strcmp(init_ptr->compat, node->name) == 0) {
i32 res = init_ptr->init(node);
log_info("loaded driver \"%s\" for device \"%s\"\n",
init_ptr->name, init_ptr->compat);
if (res < 0)
return res;
break;
}
}
}
return 0;
}

module_init(init);
24 changes: 0 additions & 24 deletions src/dev_table.c

This file was deleted.

4 changes: 0 additions & 4 deletions src/drivers/fb.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ static driver_t driver = {
.ioctl = ioctl,
};

static i32 init(void) { return 0; }

module_init(init);

i32 fb_register_dev(dev_t dev) {
if (nr_devs == DRIVER_DEV_CAPACITY)
return -EAGAIN;
Expand Down
19 changes: 18 additions & 1 deletion src/drivers/fbcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <drivers/tty.h>
#include <errno.h>
#include <init.h>
#include <module.h>

MODULE_NAME("fbcon");

#define DEFAULT_FG 0xFFF2EFDE
#define DEFAULT_BG 0xFF202020
Expand Down Expand Up @@ -98,6 +101,9 @@ void set_color(u32 num) {
case 90:
ctrl_blk->fg = 0xFF808080;
break;
case 92:
ctrl_blk->fg = 0xFF85FF85;
break;
}
}

Expand Down Expand Up @@ -174,6 +180,17 @@ static void init_dev(dev_t dev) {
ctrl_blk->fg = DEFAULT_FG;
ctrl_blk->bg = DEFAULT_BG;

u32 width, height;
dev_ioctl(dev, FB_GET_SCREEN_INFO, &width, &height);

for (u32 y = 0; y < height; y++) {
for (u32 x = 0; x < width; x++) {
dev_ioctl(dev, FB_WRITE_PIX, x, y, DEFAULT_BG);
}
}

dev_ioctl(dev, FB_FLUSH, 0, height);

tty_register_sink(dev.num, make_dev(driver, dev.num));
}

Expand All @@ -182,4 +199,4 @@ static i32 init(void) {
return 0;
}

module_post_init(init);
module_init(init);
5 changes: 4 additions & 1 deletion src/drivers/plic.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <drivers/plic.h>

#include <csr.h>
#include <dev_table.h>
#include <dev.h>
#include <errno.h>
#include <module.h>

MODULE_NAME("plic");

static usize base_addr = 0;

Expand Down
4 changes: 0 additions & 4 deletions src/drivers/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ driver_t tty_driver = {
.ioctl = nullptr,
};

static i32 init(void) { return 0; }

module_init(init);

static void lazy_init_ctrl_blks(u32 num) {
if (num < nr_devs)
return;
Expand Down
6 changes: 4 additions & 2 deletions src/drivers/uart.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include <config.h>
#include <dev.h>
#include <dev_table.h>
#include <drivers/plic.h>
#include <drivers/tty.h>
#include <errno.h>
#include <init.h>
#include <intr.h>
#include <module.h>
#include <sem.h>
#include <trap.h>

MODULE_NAME("uart");

#define REG(num, reg) \
*((volatile u8 *) (ctrl_blks[num].addr + ctrl_blks[num].reg_size * (reg)))
#define THR 0
Expand Down Expand Up @@ -130,4 +132,4 @@ static i32 init_dev(const dev_node_t *node) {
return 0;
}

dev_init("uart", init_dev);
dev_init("uart8250", init_dev);
5 changes: 4 additions & 1 deletion src/drivers/virtio-gpu.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <dev.h>
#include <drivers/fb.h>
#include <init.h>
#include <module.h>
#include <types.h>

MODULE_NAME("virtio_gpu");

enum virtio_gpu_ctrl_type {
/* 2d commands */
VIRTIO_GPU_CMD_GET_DISPLAY_INFO = 0x0100,
Expand Down Expand Up @@ -383,4 +386,4 @@ static i32 init(void) {
return 0;
}

module_post_init(init);
module_init(init);
33 changes: 30 additions & 3 deletions src/init.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
#include <init.h>

extern i32 (*_init_start)(void), (*_init_end)(void);
#include <log.h>
#include <module.h>

extern init_t _pre_init_start, _pre_init_end;
extern init_t _init_start, _init_end;
extern init_t _post_init_start, _post_init_end;

MODULE_NAME("init");

i32 init_modules(void) {
for (i32 (**init_ptr)(void) = &_init_start; init_ptr < &_init_end;
log_info("entering the pre-initialization phase\n");
for (init_t *init_ptr = &_pre_init_start; init_ptr < &_pre_init_end;
init_ptr++) {
i32 res = init_ptr->init();
log_info("initialized %s\n", init_ptr->name);
if (res < 0)
return res;
}

log_info("entering the initialization phase\n");
for (init_t *init_ptr = &_init_start; init_ptr < &_init_end; init_ptr++) {
i32 res = init_ptr->init();
log_info("initialized %s\n", init_ptr->name);
if (res < 0)
return res;
}

log_info("entering the post-initialization phase\n");
for (init_t *init_ptr = &_post_init_start; init_ptr < &_post_init_end;
init_ptr++) {
i32 res = (*init_ptr)();
i32 res = init_ptr->init();
log_info("initialized %s\n", init_ptr->name);
if (res < 0)
return res;
}

return 0;
}
Loading

0 comments on commit 062a03f

Please sign in to comment.