[kernel] Move kassert out of kutil

Continuing moving things out of kutil. The assert as implemented could
only ever work in the kernel, so remaining kutil uses of kassert have
been moved to including standard C assert instead.

Along the way, kassert was broken out into panic::panic and kassert,
and the panic.serial namespace was renamed panicking.
This commit is contained in:
Justin C. Miller
2022-01-02 01:38:04 -08:00
parent a6ec294f63
commit 5f88f5ed02
43 changed files with 148 additions and 117 deletions

View File

@@ -1,5 +1,5 @@
#include "kutil/assert.h"
#include "apic.h"
#include "assert.h"
#include "clock.h"
#include "interrupts.h"
#include "io.h"

13
src/kernel/assert.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "assert.h"
namespace panic {
uint32_t *apic_icr = reinterpret_cast<uint32_t*>(0xffffc000fee00300);
uintptr_t symbol_table = 0;
} // namespace panic
extern "C"
void _PDCLIB_assert(const char *message, const char *function, const char *file, unsigned line) {
panic::panic(message, function, file, line);
}

61
src/kernel/assert.h Normal file
View File

@@ -0,0 +1,61 @@
#pragma once
#include <stdint.h>
namespace panic {
constexpr uint32_t send_nmi_command =
(4 << 8) | // Delivery mode NMI
(1 << 14) | // assert level high
(1 << 18); // destination self
extern uint32_t *apic_icr;
extern uintptr_t symbol_table;
__attribute__ ((always_inline))
inline void panic(
const char *message = nullptr,
const char *function = __builtin_FUNCTION(),
const char *file = __builtin_FILE(),
uint64_t line = __builtin_LINE())
{
register uintptr_t syms asm("rdi");
register const char *m asm("rsi");
register const char *fn asm("rdx");
register const char *fi asm("rcx");
register uint64_t l asm("r8");
asm volatile ("mov %1, %0" : "=r"(syms) : "r"(symbol_table));
asm volatile ("mov %1, %0" : "=r"(m) : "r"(message));
asm volatile ("mov %1, %0" : "=r"(fn) : "r"(function));
asm volatile ("mov %1, %0" : "=r"(fi) : "r"(file));
asm volatile ("mov %1, %0" : "=r"(l) : "r"(line));
*apic_icr = send_nmi_command;
while (1) asm ("hlt");
}
} // namespace panic
#ifndef NDEBUG
__attribute__ ((always_inline))
inline void kassert(
bool check,
const char *message = nullptr,
const char *function = __builtin_FUNCTION(),
const char *file = __builtin_FILE(),
uint64_t line = __builtin_LINE())
{
if (!check)
panic::panic(message, function, file, line);
}
#define assert(x) kassert((x))
#else // NDEBUG
#define kassert(...) ((void)0)
#define assert(x) ((void)0)
#endif // NDEBUG

View File

@@ -1,4 +1,4 @@
#include "kutil/assert.h"
#include "assert.h"
using __exit_func = void (*)(void *);

View File

@@ -1,6 +1,6 @@
#include <stdint.h>
#include "kutil/assert.h"
#include "assert.h"
#include "cpu.h"
#include "cpu/cpu_id.h"
#include "device_manager.h"

View File

@@ -1,8 +1,7 @@
#include <stddef.h>
#include <stdint.h>
#include "kutil/assert.h"
#include "assert.h"
#include "acpi_tables.h"
#include "apic.h"
#include "clock.h"

View File

@@ -1,5 +1,4 @@
#include "kutil/assert.h"
#include "assert.h"
#include "frame_allocator.h"
#include "kernel_args.h"
#include "kernel_memory.h"

View File

@@ -1,9 +1,9 @@
#include <stdint.h>
#include <string.h>
#include "kutil/assert.h"
#include "kutil/no_construct.h"
#include "assert.h"
#include "console.h"
#include "cpu.h"
#include "gdt.h"

View File

@@ -1,8 +1,8 @@
#include <stdint.h>
#include <string.h>
#include "kutil/assert.h"
#include "kutil/util.h"
#include "assert.h"
#include "heap_allocator.h"
#include "memory.h"

View File

@@ -1,5 +1,6 @@
#include "kernel_memory.h"
#include "kutil/assert.h"
#include "assert.h"
#include "device_manager.h"
#include "hpet.h"
#include "io.h"

View File

@@ -3,6 +3,7 @@
#include "kernel_memory.h"
#include "printf/printf.h"
#include "assert.h"
#include "cpu.h"
#include "device_manager.h"
#include "idt.h"

View File

@@ -10,6 +10,7 @@ kernel = module("kernel",
sources = [
"apic.cpp",
"ap_startup.s",
"assert.cpp",
"boot.s",
"clock.cpp",
"console.cpp",

View File

@@ -1,6 +1,7 @@
#include "j6/signals.h"
#include "kutil/no_construct.h"
#include "assert.h"
#include "console.h"
#include "log.h"
#include "memory.h"

View File

@@ -1,9 +1,9 @@
#include <string.h>
#include "kutil/assert.h"
#include "kutil/constexpr_hash.h"
#include "printf/printf.h"
#include "assert.h"
#include "logger.h"
namespace logs {

View File

@@ -5,9 +5,9 @@
#include "j6/signals.h"
#include "kernel_args.h"
#include "kernel_memory.h"
#include "kutil/assert.h"
#include "apic.h"
#include "assert.h"
#include "block_device.h"
#include "clock.h"
#include "console.h"
@@ -83,7 +83,7 @@ kernel_main(init::args *args)
{
if (args->panic) {
IDT::set_nmi_handler(args->panic->entrypoint);
kutil::assert::symbol_table = args->symbol_table | memory::page_offset;
panic::symbol_table = args->symbol_table | memory::page_offset;
}
init_console();

View File

@@ -4,9 +4,9 @@
#include "j6/init.h"
#include "enum_bitfields.h"
#include "kutil/assert.h"
#include "kutil/no_construct.h"
#include "assert.h"
#include "device_manager.h"
#include "frame_allocator.h"
#include "gdt.h"

View File

@@ -1,6 +1,6 @@
#include <string.h>
#include "kutil/assert.h"
#include "assert.h"
#include "kernel_memory.h"
#include "objects/channel.h"
#include "objects/vm_area.h"

View File

@@ -1,6 +1,8 @@
#include "j6/errors.h"
#include "j6/signals.h"
#include "j6/types.h"
#include "assert.h"
#include "objects/kobject.h"
#include "objects/thread.h"

View File

@@ -1,5 +1,6 @@
#include "kutil/assert.h"
#include "kutil/no_construct.h"
#include "assert.h"
#include "cpu.h"
#include "objects/process.h"
#include "objects/thread.h"

View File

@@ -1,5 +1,7 @@
#include "frame_allocator.h"
#include "kernel_memory.h"
#include "assert.h"
#include "frame_allocator.h"
#include "objects/vm_area.h"
#include "page_tree.h"
#include "vm_space.h"

View File

@@ -1,7 +1,6 @@
#include <string.h>
#include "kutil/assert.h"
#include "assert.h"
#include "console.h"
#include "frame_allocator.h"
#include "kernel_memory.h"

View File

@@ -1,7 +1,6 @@
#include <string.h>
#include "kutil/assert.h"
#include "assert.h"
#include "frame_allocator.h"
#include "kernel_memory.h"
#include "page_tree.h"

View File

@@ -5,7 +5,7 @@
#include "serial.h"
#include "symbol_table.h"
namespace panic {
namespace panicking {
void
print_header(
@@ -102,7 +102,7 @@ print_cpu_state(serial_port &out, const cpu_state &regs)
out.write("\e[0m\n");
}
} // namespace panic
} // namespace panicking
// For printf.c
extern "C" void putchar_(char c) {}

View File

@@ -6,7 +6,7 @@
struct cpu_state;
namespace panic {
namespace panicking {
class serial_port;
class symbol_table;
@@ -27,4 +27,4 @@ void print_header(
void print_callstack(serial_port &out, symbol_table &syms, frame const *fp);
void print_cpu_state(serial_port &out, const cpu_state &regs);
}
} // namespace panicking

View File

@@ -1,3 +1,6 @@
#include "kutil/spinlock.h"
#include "assert.h"
#include "display.h"
#include "serial.h"
#include "symbol_table.h"
@@ -13,10 +16,10 @@ void panic_handler(
uint64_t line,
const cpu_state *regs)
{
panic::serial_port com1(panic::COM1);
panic::symbol_table syms(symbol_data);
panicking::serial_port com1(panicking::COM1);
panicking::symbol_table syms(symbol_data);
panic::frame const *fp = nullptr;
panicking::frame const *fp = nullptr;
asm ( "mov %%rbp, %0" : "=r" (fp) );
print_header(com1, message, function, file, line);

View File

@@ -1,6 +1,6 @@
#include "serial.h"
namespace panic {
namespace panicking {
// register offsets
constexpr uint16_t THR = 0; // Write
@@ -52,4 +52,4 @@ serial_port::write(const char *s)
}
}
} // namespace panic
} // namespace panicking

View File

@@ -3,7 +3,7 @@
/// Non-interrupt-driven serial 'driver' for panic handling
#include <stdint.h>
namespace panic {
namespace panicking {
class serial_port
{
@@ -20,4 +20,4 @@ private:
constexpr uint16_t COM1 = 0x03f8;
} // namespace panic
} // namespace panicking

View File

@@ -1,7 +1,7 @@
#include "pointer_manipulation.h"
#include "symbol_table.h"
namespace panic {
namespace panicking {
symbol_table::symbol_table(const void *data) :
m_data(data)
@@ -25,4 +25,4 @@ symbol_table::find_symbol(uintptr_t addr) const
return nullptr;
}
} // namespace panic
} // namespace panicking

View File

@@ -3,7 +3,7 @@
#include <stdint.h>
#include "counted.h"
namespace panic {
namespace panicking {
class symbol_table
{
@@ -29,4 +29,4 @@ private:
counted<entry const> m_entries;
};
} // namespace panic
} // namespace panicking

View File

@@ -1,4 +1,4 @@
#include "kutil/assert.h"
#include "assert.h"
#include "console.h"
#include "log.h"
#include "interrupts.h"

View File

@@ -1,9 +1,9 @@
#include <stddef.h>
#include <j6/init.h>
#include "kutil/assert.h"
#include "apic.h"
#include "assert.h"
#include "clock.h"
#include "console.h"
#include "cpu.h"

View File

@@ -1,7 +1,7 @@
#include <string.h>
#include "kutil/assert.h"
#include "kutil/no_construct.h"
#include "assert.h"
#include "interrupts.h"
#include "io.h"
#include "serial.h"

View File

@@ -2,6 +2,7 @@
#include "j6/signals.h"
#include "j6/types.h"
#include "assert.h"
#include "log.h"
#include "objects/thread.h"
#include "syscalls/helpers.h"

View File

@@ -1,7 +1,7 @@
#include <string.h>
#include "kutil/assert.h"
#include "kutil/no_construct.h"
#include "assert.h"
#include "cpu.h"
#include "kernel_memory.h"
#include "log.h"

View File

@@ -1,7 +1,9 @@
#include <string.h>
#include "frame_allocator.h"
#include "kernel_memory.h"
#include "assert.h"
#include "frame_allocator.h"
#include "log.h"
#include "objects/process.h"
#include "objects/thread.h"