[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"

View File

@@ -1,10 +0,0 @@
#include "kutil/assert.h"
namespace kutil {
namespace assert {
uint32_t *apic_icr = reinterpret_cast<uint32_t*>(0xffffc000fee00300);
uintptr_t symbol_table = 0;
} // namespace assert
} // namespace kutil

View File

@@ -1,4 +1,9 @@
#include "kutil/assert.h"
#if __has_include(<assert.h>)
#include <assert.h>
#else
#define assert(x) ((void)0)
#endif
#include "kutil/bip_buffer.h"
namespace kutil {
@@ -61,14 +66,14 @@ size_t bip_buffer::reserve(size_t size, void **area)
void bip_buffer::commit(size_t size)
{
kassert(size <= m_size_r, "Tried to commit more than reserved");
assert(size <= m_size_r && "Tried to commit more than reserved");
if (m_start_r == m_start_a + m_size_a) {
// We were adding to A
m_size_a += size;
} else {
// We were adding to B
kassert(m_start_r == m_start_b + m_size_b, "Bad m_start_r!");
assert(m_start_r == m_start_b + m_size_b && "Bad m_start_r!");
m_size_b += size;
}
@@ -83,7 +88,7 @@ size_t bip_buffer::get_block(void **area) const
void bip_buffer::consume(size_t size)
{
kassert(size <= m_size_a, "Consumed more bytes than exist in A");
assert(size <= m_size_a && "Consumed more bytes than exist in A");
if (size >= m_size_a) {
m_size_a = m_size_b;
m_start_a = m_start_b;

View File

@@ -1,44 +0,0 @@
#pragma once
#include <stdint.h>
namespace kutil {
namespace assert {
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;
} // namespace assert
} // namespace kutil
__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) {
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"(kutil::assert::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));
*kutil::assert::apic_icr = kutil::assert::send_nmi_command;
while (1) asm ("hlt");
}
}

View File

@@ -11,6 +11,7 @@
/// http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/
#include <new>
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "kutil/hash.h"
@@ -145,7 +146,7 @@ protected:
}
void set_capacity(size_t capacity) {
kassert((capacity & (capacity - 1)) == 0,
assert((capacity & (capacity - 1)) == 0 &&
"Map capacity must be a power of two");
m_capacity = capacity;

View File

@@ -2,9 +2,9 @@
/// \file vector.h
/// Definition of a simple dynamic vector collection for use in kernel space
#include <assert.h>
#include <string.h>
#include <utility>
#include "kutil/assert.h"
#include "kutil/util.h"
namespace kutil {
@@ -158,7 +158,7 @@ public:
/// Remove an item from the end of the array.
void remove()
{
kassert(m_size, "Called remove() on an empty array");
assert(m_size && "Called remove() on an empty array");
m_size -= 1;
m_elements[m_size].~T();
@@ -167,14 +167,14 @@ public:
/// Remove an item from the front of the array, preserving order.
void remove_front()
{
kassert(m_size, "Called remove_front() on an empty array");
assert(m_size && "Called remove_front() on an empty array");
remove_at(0);
}
/// Remove an item from the array.
void remove(const T &item)
{
kassert(m_size, "Called remove() on an empty array");
assert(m_size && "Called remove() on an empty array");
for (count_t i = 0; i < m_size; ++i) {
if (m_elements[i] == item) {
remove_at(i);
@@ -224,7 +224,7 @@ public:
/// Remove an item from the end of the array and return it.
T pop()
{
kassert(m_size, "Called pop() on an empty array");
assert(m_size && "Called pop() on an empty array");
T temp = m_elements[m_size - 1];
remove();
@@ -234,7 +234,7 @@ public:
/// Remove an item from the beginning of the array and return it.
T pop_front()
{
kassert(m_size, "Called pop_front() on an empty array");
assert(m_size && "Called pop_front() on an empty array");
T temp = m_elements[0];
remove_front();

View File

@@ -4,7 +4,6 @@ module("kutil",
kind = "lib",
includes = [ "include" ],
sources = [
"assert.cpp",
"bip_buffer.cpp",
"spinlock.cpp",
])

View File

@@ -10,7 +10,7 @@
CPP_CHECK_BEGIN
void _PDCLIB_assert( const char * const, const char * const, const char * const );
void _PDCLIB_assert( const char * const, const char * const, const char * const, unsigned );
/* If NDEBUG is set, assert() is a null operation. */
#undef assert
@@ -19,11 +19,7 @@ void _PDCLIB_assert( const char * const, const char * const, const char * const
#define assert( ignore ) ( (void) 0 )
#else
#define assert( expression ) ( ( expression ) ? (void) 0 \
: _PDCLIB_assert( "Assertion failed: " #expression \
", function ", __func__, \
", file " __FILE__ \
", line " _PDCLIB_symbol2string( __LINE__ ) \
"." _PDCLIB_endl ) )
: _PDCLIB_assert( #expression, __func__, __FILE__, __LINE__ ) )
#endif
CPP_CHECK_END

View File

@@ -10,10 +10,9 @@
#include "j6libc/aux.h"
void _PDCLIB_assert( const char * const message1, const char * const function, const char * const message2 )
void _PDCLIB_assert( const char * const message, const char * const function, const char * const file, unsigned line )
{
fputs( message1, stderr );
fputs( function, stderr );
fputs( message2, stderr );
fprintf( stderr, "Assertion failed: %s, function %s, file %s, line %d.%s",
message, function, file, line, _PDCLIB_endl );
abort();
}