Add/move code into kutil library
This commit is contained in:
4
Makefile
4
Makefile
@@ -11,7 +11,7 @@ GITSHA ?= $(shell git rev-parse --short HEAD)
|
||||
KERNEL_FILENAME:= popcorn.bin
|
||||
KERNEL_FONT := assets/fonts/tamsyn8x16r.psf
|
||||
|
||||
MODULES :=
|
||||
MODULES := kutil
|
||||
|
||||
|
||||
EFI_DIR := external/gnu-efi
|
||||
@@ -184,7 +184,7 @@ $(BUILD_D)/boot/%.c.o: src/boot/%.c $(INIT_DEP)
|
||||
$(CC) $(BOOT_CFLAGS) -c -o $@ $<
|
||||
|
||||
$(BUILD_D)/kernel.elf: $(KERN_OBJS) $(MOD_TARGETS) $(ARCH_D)/kernel.ld
|
||||
$(LD) $(LDFLAGS) -u _header -T $(ARCH_D)/kernel.ld -o $@ $(patsubst %,-l%,$(MODULES)) $(KERN_OBJS)
|
||||
$(LD) $(LDFLAGS) -u _header -T $(ARCH_D)/kernel.ld -o $@ $(KERN_OBJS) $(patsubst %,-l%,$(MODULES))
|
||||
$(OBJC) --only-keep-debug $@ $@.sym
|
||||
|
||||
$(BUILD_D)/kernel.dump: $(BUILD_D)/kernel.elf
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "kutil/coord.h"
|
||||
#include "kutil/misc.h"
|
||||
|
||||
struct acpi_table_header
|
||||
{
|
||||
@@ -21,7 +22,7 @@ struct acpi_table_header
|
||||
} __attribute__ ((packed));
|
||||
|
||||
#define TABLE_HEADER(signature) \
|
||||
static const uint32_t type_id = byteswap(signature); \
|
||||
static const uint32_t type_id = kutil::byteswap(signature); \
|
||||
acpi_table_header header;
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "assert.h"
|
||||
#include "console.h"
|
||||
#include "util.h"
|
||||
|
||||
[[noreturn]] void
|
||||
__kernel_assert(const char *file, unsigned line, const char *message)
|
||||
5
src/kernel/assert.h
Normal file
5
src/kernel/assert.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
[[noreturn]] void __kernel_assert(const char *file, unsigned line, const char *message);
|
||||
|
||||
#define kassert(stmt, message) if(!(stmt)) { __kernel_assert(__FILE__, __LINE__, (message)); } else {}
|
||||
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "kutil/coord.h"
|
||||
#include "font.h"
|
||||
#include "screen.h"
|
||||
#include "util.h"
|
||||
|
||||
struct console_data;
|
||||
|
||||
@@ -34,8 +34,8 @@ private:
|
||||
font m_font;
|
||||
screen m_screen;
|
||||
|
||||
coord<unsigned> m_size;
|
||||
coord<unsigned> m_pos;
|
||||
kutil::coord<unsigned> m_size;
|
||||
kutil::coord<unsigned> m_pos;
|
||||
screen::pixel_t m_fg, m_bg;
|
||||
uint16_t m_attr;
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "kutil/memory.h"
|
||||
#include "acpi_tables.h"
|
||||
#include "assert.h"
|
||||
#include "device_manager.h"
|
||||
#include "console.h"
|
||||
#include "util.h"
|
||||
|
||||
static const char expected_signature[] = "RSD PTR ";
|
||||
|
||||
@@ -32,7 +33,8 @@ struct acpi2_rsdp
|
||||
} __attribute__ ((packed));
|
||||
|
||||
|
||||
uint8_t acpi_checksum(const void *p, size_t len, size_t off = 0)
|
||||
uint8_t
|
||||
acpi_checksum(const void *p, size_t len, size_t off = 0)
|
||||
{
|
||||
uint8_t sum = 0;
|
||||
const uint8_t *c = reinterpret_cast<const uint8_t *>(p);
|
||||
@@ -115,9 +117,6 @@ device_manager::load_xsdt(const acpi_xsdt *xsdt)
|
||||
cons->puts("\n");
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T read_from(const uint8_t *p) { return *reinterpret_cast<const T *>(p); }
|
||||
|
||||
void
|
||||
device_manager::load_apic(const acpi_apic *apic)
|
||||
{
|
||||
@@ -143,8 +142,8 @@ device_manager::load_apic(const acpi_apic *apic)
|
||||
break;
|
||||
|
||||
case 1: // I/O APIC
|
||||
m_io_apic = reinterpret_cast<uint32_t *>(read_from<uint32_t>(p+4));
|
||||
m_global_interrupt_base = read_from<uint32_t>(p+8);
|
||||
m_io_apic = reinterpret_cast<uint32_t *>(kutil::read_from<uint32_t>(p+4));
|
||||
m_global_interrupt_base = kutil::read_from<uint32_t>(p+8);
|
||||
cons->puts(" ");
|
||||
cons->put_hex((uint64_t)m_io_apic);
|
||||
cons->puts(" ");
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#include "kutil/coord.h"
|
||||
#include "screen.h"
|
||||
#include "util.h"
|
||||
|
||||
class font
|
||||
{
|
||||
@@ -28,7 +28,7 @@ private:
|
||||
font();
|
||||
font(unsigned height, unsigned width, unsigned count, uint8_t const *data);
|
||||
|
||||
coord<unsigned> m_size;
|
||||
kutil::coord<unsigned> m_size;
|
||||
unsigned m_count;
|
||||
uint8_t const *m_data;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "kutil/memory.h"
|
||||
#include "console.h"
|
||||
#include "interrupts.h"
|
||||
|
||||
@@ -112,10 +113,8 @@ set_idt_entry(uint8_t i, uint64_t addr, uint16_t selector, uint8_t flags)
|
||||
void
|
||||
interrupts_init()
|
||||
{
|
||||
uint8_t *p;
|
||||
|
||||
p = reinterpret_cast<uint8_t *>(&g_gdt_table);
|
||||
for (int i = 0; i < sizeof(g_gdt_table); ++i) p[i] = 0;
|
||||
kutil::memset(&g_gdt_table, 0, sizeof(g_gdt_table));
|
||||
kutil::memset(&g_idt_table, 0, sizeof(g_idt_table));
|
||||
|
||||
g_gdtr.limit = sizeof(g_gdt_table) - 1;
|
||||
g_gdtr.base = reinterpret_cast<uint64_t>(&g_gdt_table);
|
||||
@@ -129,8 +128,6 @@ interrupts_init()
|
||||
set_gdt_entry(7, 0, 0xfffff, true, gdt_flags::rw | gdt_flags::ex);
|
||||
|
||||
gdt_write();
|
||||
p = reinterpret_cast<uint8_t *>(&g_idt_table);
|
||||
for (int i = 0; i < sizeof(g_idt_table); ++i) p[i] = 0;
|
||||
|
||||
g_idtr.limit = sizeof(g_idt_table) - 1;
|
||||
g_idtr.base = reinterpret_cast<uint64_t>(&g_idt_table);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "kutil/coord.h"
|
||||
|
||||
class screen
|
||||
{
|
||||
@@ -36,5 +36,5 @@ private:
|
||||
|
||||
pixel_t *m_framebuffer;
|
||||
color_masks m_masks;
|
||||
coord<unsigned> m_resolution;
|
||||
kutil::coord<unsigned> m_resolution;
|
||||
};
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
template <typename T>
|
||||
struct coord {
|
||||
T x, y;
|
||||
coord() : x(T{}), y(T{}) {}
|
||||
coord(T x, T y) : x(x), y(y) {}
|
||||
T size() const { return x * y; }
|
||||
};
|
||||
|
||||
[[noreturn]] void __kernel_assert(const char *file, unsigned line, const char *message);
|
||||
|
||||
#define kassert(stmt, message) if(!(stmt)) { __kernel_assert(__FILE__, __LINE__, (message)); } else {}
|
||||
|
||||
constexpr uint32_t byteswap(uint32_t x)
|
||||
{
|
||||
return ((x >> 24) & 0x000000ff) | ((x >> 8) & 0x0000ff00)
|
||||
| ((x << 8) & 0x00ff0000) | ((x << 24) & 0xff000000);
|
||||
}
|
||||
14
src/modules/kutil/coord.h
Normal file
14
src/modules/kutil/coord.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
namespace kutil {
|
||||
|
||||
template <typename T>
|
||||
struct coord
|
||||
{
|
||||
T x, y;
|
||||
coord() : x(T{}), y(T{}) {}
|
||||
coord(T x, T y) : x(x), y(y) {}
|
||||
T size() const { return x * y; }
|
||||
};
|
||||
|
||||
} // namespace kutil
|
||||
13
src/modules/kutil/memory.cpp
Normal file
13
src/modules/kutil/memory.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "memory.h"
|
||||
|
||||
namespace kutil {
|
||||
|
||||
void *
|
||||
memset(void *s, uint8_t v, size_t n)
|
||||
{
|
||||
uint8_t *p = reinterpret_cast<uint8_t *>(s);
|
||||
for (int i = 0; i < n; ++i) p[i] = 0;
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace kutil
|
||||
13
src/modules/kutil/memory.h
Normal file
13
src/modules/kutil/memory.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace kutil {
|
||||
|
||||
void * memset(void *p, uint8_t v, size_t n);
|
||||
|
||||
template <typename T>
|
||||
T read_from(const void *p) { return *reinterpret_cast<const T *>(p); }
|
||||
|
||||
} // namespace kutil
|
||||
12
src/modules/kutil/misc.h
Normal file
12
src/modules/kutil/misc.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
namespace kutil {
|
||||
|
||||
constexpr uint32_t
|
||||
byteswap(uint32_t x)
|
||||
{
|
||||
return ((x >> 24) & 0x000000ff) | ((x >> 8) & 0x0000ff00)
|
||||
| ((x << 8) & 0x00ff0000) | ((x << 24) & 0xff000000);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
MOD_NAME := main
|
||||
MOD_NAME := kutil
|
||||
include modules.mk
|
||||
Reference in New Issue
Block a user