Skip to content

Commit ec759bd

Browse files
authored
Merge pull request #2283 from alfreb/fieldmedic
Revive Fieldmedic plugin, add early kernel diagnostic hooks
2 parents 71a6035 + 955746d commit ec759bd

File tree

16 files changed

+295
-81
lines changed

16 files changed

+295
-81
lines changed

api/info

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@
2121

2222
#define LINEWIDTH 80
2323

24+
#include <kprint>
25+
2426
// Informational prints
2527
#ifndef NO_INFO
26-
#define INFO(FROM, TEXT, ...) printf("%13s ] " TEXT "\n", "[ " FROM, ##__VA_ARGS__)
27-
#define INFO2(TEXT, ...) printf("%16s" TEXT "\n"," ", ##__VA_ARGS__)
28-
#define CENTER(TEXT) printf("%*s%*s\n",LINEWIDTH/2 + (int)strlen(TEXT)/2,TEXT,LINEWIDTH/2-(int)strlen(TEXT)/2,"")
28+
#define INFO(FROM, TEXT, ...) kprintf("%13s ] " TEXT "\n", "[ " FROM, ##__VA_ARGS__)
29+
#define INFO2(TEXT, ...) kprintf("%16s" TEXT "\n"," ", ##__VA_ARGS__)
30+
#define CENTER(TEXT) kprintf("%*s%*s\n",LINEWIDTH/2 + (int)strlen(TEXT)/2,TEXT,LINEWIDTH/2-(int)strlen(TEXT)/2,"")
2931
#include <string>
30-
#define FILLINE(CHAR) printf("%s\n",std::string(LINEWIDTH,CHAR).c_str())
31-
#define CAPTION(TEXT) FILLINE('='); printf("\n"); CENTER(TEXT); printf("\n"); FILLINE('=')
32+
#define FILLINE(CHAR) kprintf("%s\n",std::pmr::string(LINEWIDTH,CHAR).c_str())
33+
#define CAPTION(TEXT) FILLINE('='); kprintf("\n"); CENTER(TEXT); kprintf("\n"); FILLINE('=')
3234

3335
// Checkboxes - for initialization output and testing
34-
#define CHECK(TEST, TEXT, ...) printf("%16s[%s] " TEXT "\n","", TEST ? "x" : " ", ##__VA_ARGS__)
36+
#define CHECK(TEST, TEXT, ...) kprintf("%16s[%s] " TEXT "\n","", TEST ? "x" : " ", ##__VA_ARGS__)
3537
#define CHECKSERT(TEST, TEXT, ...) CHECK(TEST, TEXT, ##__VA_ARGS__), assert(TEST)
36-
#define FAIL(TEXT, ...) printf("FAIL: " TEXT "\n", ##__VA_ARGS__); os::panic("FAIL")
38+
#define FAIL(TEXT, ...) kprintf("FAIL: " TEXT "\n", ##__VA_ARGS__); os::panic("FAIL")
3739

3840
// Undefine
3941
#else

api/kernel/diag.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef KERNEL_DIAG_HOOKS_HPP
2+
#define KERNEL_DIAG_HOOKS_HPP
3+
#define RUN_DIAG_HOOKS true
4+
5+
constexpr bool run_diag_hooks = RUN_DIAG_HOOKS;
6+
7+
namespace kernel::diag {
8+
void post_bss() noexcept;
9+
void post_machine_init() noexcept;
10+
void post_init_libc() noexcept;
11+
void post_service() noexcept;
12+
13+
void default_post_init_libc() noexcept;
14+
15+
template <auto Func>
16+
void hook() {
17+
if constexpr (run_diag_hooks) {
18+
Func();
19+
}
20+
}
21+
}
22+
23+
#endif

src/kernel/kernel.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <os.hpp>
1919
#include <kernel.hpp>
2020
#include <kernel/cpuid.hpp>
21+
#include <kernel/diag.hpp>
2122
#include <kernel/rng.hpp>
2223
#include <service>
2324
#include <cstdio>
@@ -168,6 +169,7 @@ void kernel::post_start()
168169

169170
// service program start
170171
Service::start();
172+
kernel::diag::hook<kernel::diag::post_service>();
171173
}
172174

173175
void os::add_stdout(os::print_func func)
@@ -227,3 +229,5 @@ void os::print_timestamps(const bool enabled)
227229
{
228230
kernel::state().timestamps = enabled;
229231
}
232+
233+
void __attribute__((weak)) kernel::diag::post_service() noexcept {}

src/kernel/syscalls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ void kernel::default_exit() {
252252
}
253253

254254
extern "C"
255-
void _init_syscalls()
255+
void __init_crash_contexts()
256256
{
257257
// make sure each buffer is zero length so it won't always show up in crashes
258258
for (auto& ctx : contexts)

src/platform/aarch64_vm/kernel_start.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ extern "C" {/*
129129
uintptr_t _move_symbols(uintptr_t loc);
130130
// void _init_bss();
131131
void _init_heap(uintptr_t);
132-
void _init_syscalls();
132+
void __init_crash_contexts();
133133
*/
134134
void __init_sanity_checks();
135135
void kernel_sanity_checks();
136136
void _init_bss();
137137
uintptr_t _move_symbols(uintptr_t loc);
138138
void _init_elf_parser();
139-
void _init_syscalls();
139+
void __init_crash_contexts();
140140
void __elf_validate_section(const void*);
141141
}
142142

@@ -241,7 +241,7 @@ void kernel_start(uintptr_t magic, uintptr_t addrin)
241241
__machine->init();
242242

243243
// Initialize system calls
244-
_init_syscalls();
244+
__init_crash_contexts();
245245

246246
//probably not very sane!
247247
cpu_debug_enable();

src/platform/x86_nano/kernel_start.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern "C" {
2727
uintptr_t _move_symbols(uintptr_t loc);
2828
void _init_heap(uintptr_t);
2929
void _init_elf_parser();
30-
void _init_syscalls();
30+
void __init_crash_contexts();
3131
}
3232

3333
uintptr_t _multiboot_free_begin(uintptr_t boot_addr);
@@ -61,7 +61,7 @@ void kernel_start(uintptr_t magic, uintptr_t addr)
6161
_init_elf_parser();
6262

6363
// Initialize system calls
64-
_init_syscalls();
64+
__init_crash_contexts();
6565

6666
// Initialize stdout handlers
6767
if (os_default_stdout)

src/platform/x86_pc/init_libc.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <kernel.hpp>
55
#include <kernel/auxvec.h>
66
#include <kernel/cpuid.hpp>
7+
#include <kernel/diag.hpp>
78
#include <kernel/rng.hpp>
89
#include <kernel/service.hpp>
910
#include <util/elf_binary.hpp>
@@ -12,9 +13,9 @@
1213

1314
//#define KERN_DEBUG 1
1415
#ifdef KERN_DEBUG
15-
#define PRATTLE(fmt, ...) kprintf(fmt, ##__VA_ARGS__)
16+
#define KDEBUG(fmt, ...) kprintf(fmt, ##__VA_ARGS__)
1617
#else
17-
#define PRATTLE(fmt, ...) /* fmt */
18+
#define KDEBUG(fmt, ...) /* fmt */
1819
#endif
1920

2021
extern char _ELF_START_;
@@ -31,17 +32,24 @@ static void global_ctor_test(){
3132
global_ctors_ok = 42;
3233
}
3334

35+
namespace kernel::diag {
36+
void default_post_init_libc() noexcept {
37+
Expects(global_ctors_ok == 42 && "Global constructors functional");
38+
Elf_binary<Elf64> elf{{(char*)&_ELF_START_, static_cast<size_t>(&_ELF_END_ - &_ELF_START_)}};
39+
Expects(elf.is_ELF() && "ELF header intact");
40+
}
41+
void __attribute__((weak)) post_init_libc() noexcept {
42+
default_post_init_libc();
43+
}
44+
}
45+
3446
extern "C"
3547
int kernel_main(int, char * *, char * *)
3648
{
37-
PRATTLE("<kernel_main> libc initialization complete \n");
38-
LL_ASSERT(global_ctors_ok == 42);
49+
KDEBUG("<kernel_main> libc initialization complete \n");
3950
kernel::state().libc_initialized = true;
40-
41-
Elf_binary<Elf64> elf{{(char*)&_ELF_START_, static_cast<size_t>(&_ELF_END_ - &_ELF_START_)}};
42-
LL_ASSERT(elf.is_ELF() && "ELF header intact");
43-
44-
PRATTLE("<kernel_main> OS start \n");
51+
kernel::diag::hook<kernel::diag::post_init_libc>();
52+
KDEBUG("<kernel_main> OS start \n");
4553

4654
// Initialize early OS, platform and devices
4755
#if defined(PLATFORM_x86_pc)
@@ -57,7 +65,7 @@ int kernel_main(int, char * *, char * *)
5765
// NOTE: because of page protection we can choose to stop checking here
5866
kernel_sanity_checks();
5967

60-
PRATTLE("<kernel_main> post start \n");
68+
KDEBUG("<kernel_main> post start \n");
6169
// Initialize common subsystems and call Service::start
6270
kernel::post_start();
6371

@@ -77,7 +85,7 @@ namespace x86
7785
grub_magic = magic;
7886
grub_addr = addr;
7987

80-
PRATTLE("* Elf start: %p\n", &_ELF_START_);
88+
KDEBUG("* Elf start: %p\n", &_ELF_START_);
8189
auto* ehdr = (Elf64_Ehdr*)&_ELF_START_;
8290
auto* phdr = (Elf64_Phdr*)((char*)ehdr + ehdr->e_phoff);
8391
LL_ASSERT(phdr);
@@ -86,12 +94,12 @@ namespace x86
8694
LL_ASSERT(phdr[0].p_type == PT_LOAD);
8795

8896
#ifdef KERN_DEBUG
89-
PRATTLE("* Elf ident: %s, program headers: %p\n", ehdr->e_ident, ehdr);
97+
KDEBUG("* Elf ident: %s, program headers: %p\n", ehdr->e_ident, ehdr);
9098
size_t size = &_ELF_END_ - &_ELF_START_;
91-
PRATTLE("\tElf size: %zu \n", size);
99+
KDEBUG("\tElf size: %zu \n", size);
92100
for (int i = 0; i < ehdr->e_phnum; i++)
93101
{
94-
PRATTLE("\tPhdr %i @ %p, va_addr: 0x%lx \n", i, &phdr[i], phdr[i].p_vaddr);
102+
KDEBUG("\tPhdr %i @ %p, va_addr: 0x%lx \n", i, &phdr[i], phdr[i].p_vaddr);
95103
}
96104
#endif
97105

@@ -110,7 +118,7 @@ namespace x86
110118

111119
// auxiliary vector
112120
auxv_t* aux = (auxv_t*) &argv[6];
113-
PRATTLE("* Initializing aux-vector @ %p\n", aux);
121+
KDEBUG("* Initializing aux-vector @ %p\n", aux);
114122

115123
int i = 0;
116124
aux[i++].set_long(AT_PAGESZ, 4096);
@@ -147,20 +155,20 @@ namespace x86
147155
#ifdef PLATFORM_x86_pc
148156
// SYSCALL instruction
149157
#if defined(__x86_64__)
150-
PRATTLE("* Initialize syscall MSR (64-bit)\n");
158+
KDEBUG("* Initialize syscall MSR (64-bit)\n");
151159
uint64_t star_kernel_cs = 8ull << 32;
152160
uint64_t star_user_cs = 8ull << 48;
153161
uint64_t star = star_kernel_cs | star_user_cs;
154162
x86::CPU::write_msr(IA32_STAR, star);
155163
x86::CPU::write_msr(IA32_LSTAR, (uintptr_t)&__syscall_entry);
156164
#elif defined(__i386__)
157-
PRATTLE("Initialize syscall intr (32-bit)\n");
165+
KDEBUG("Initialize syscall intr (32-bit)\n");
158166
#warning Classical syscall interface missing for 32-bit
159167
#endif
160168
#endif
161169

162170
// GDB_ENTRY;
163-
PRATTLE("* Starting libc initialization\n");
171+
KDEBUG("* Starting libc initialization\n");
164172
kernel::state().allow_syscalls = true;
165173
__libc_start_main(kernel_main, argc, argv.data());
166174
}

src/platform/x86_pc/kernel_start.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <kernel.hpp>
1818
#include <kernel/rng.hpp>
19+
#include <kernel/diag.hpp>
1920
#include <os.hpp>
2021
#include <boot/multiboot.h>
2122
#include <kprint>
@@ -26,9 +27,9 @@
2627

2728
//#define KERN_DEBUG 1
2829
#ifdef KERN_DEBUG
29-
#define PRATTLE(fmt, ...) kprintf(fmt, ##__VA_ARGS__)
30+
#define KDEBUG(fmt, ...) kprintf(fmt, ##__VA_ARGS__)
3031
#else
31-
#define PRATTLE(fmt, ...) /* fmt */
32+
#define KDEBUG(fmt, ...) /* fmt */
3233
#endif
3334

3435
extern "C" {
@@ -37,10 +38,15 @@ extern "C" {
3738
void _init_bss();
3839
uintptr_t _move_symbols(uintptr_t loc);
3940
void _init_elf_parser();
40-
void _init_syscalls();
41+
void __init_crash_contexts();
4142
void __elf_validate_section(const void*);
4243
}
4344

45+
namespace kernel::diag {
46+
void __attribute__((weak)) post_bss() noexcept {}
47+
void __attribute__((weak)) post_machine_init() noexcept {}
48+
}
49+
4450
uintptr_t _multiboot_free_begin(uintptr_t bootinfo);
4551
uintptr_t _multiboot_memory_end(uintptr_t bootinfo);
4652

@@ -57,18 +63,22 @@ os::Machine& os::machine() noexcept {
5763
return *__machine;
5864
}
5965

66+
const char* os::Machine::name() noexcept {
67+
return "x86 PC";
68+
}
69+
6070
// x86 kernel start
6171
extern "C"
6272
__attribute__((no_sanitize("all")))
6373
void kernel_start(uint32_t magic, uint32_t addr)
6474
{
65-
PRATTLE("\n////////////////// IncludeOS kernel start ////////////////// \n");
66-
PRATTLE("* Booted with magic 0x%x, grub @ 0x%x \n",
75+
KDEBUG("\n////////////////// IncludeOS kernel start ////////////////// \n");
76+
KDEBUG("* Booted with magic 0x%x, grub @ 0x%x \n",
6777
magic, addr);
6878
// generate checksums of read-only areas etc.
6979
__init_sanity_checks();
7080

71-
PRATTLE("* Grub magic: 0x%x, grub info @ 0x%x\n", magic, addr);
81+
KDEBUG("* Grub magic: 0x%x, grub info @ 0x%x\n", magic, addr);
7282

7383
// Determine where free memory starts
7484
extern char _end;
@@ -83,34 +93,36 @@ void kernel_start(uint32_t magic, uint32_t addr)
8393
{
8494
memory_end = kernel::softreset_memory_end(addr);
8595
}
86-
PRATTLE("* Free mem begin: 0x%zx, memory end: 0x%zx \n",
96+
KDEBUG("* Free mem begin: 0x%zx, memory end: 0x%zx \n",
8797
free_mem_begin, memory_end);
8898

89-
PRATTLE("* Moving symbols. \n");
99+
KDEBUG("* Moving symbols. \n");
90100
// Preserve symbols from the ELF binary
91101
free_mem_begin += _move_symbols(free_mem_begin);
92-
PRATTLE("* Free mem moved to: %p \n", (void*) free_mem_begin);
102+
KDEBUG("* Free mem moved to: %p \n", (void*) free_mem_begin);
93103

94-
PRATTLE("* Init .bss\n");
104+
KDEBUG("* Init .bss\n");
95105
_init_bss();
106+
kernel::diag::hook<kernel::diag::post_bss>();
96107

97108
// Instantiate machine
98109
size_t memsize = memory_end - free_mem_begin;
99110
__machine = os::Machine::create((void*)free_mem_begin, memsize);
100111

101-
PRATTLE("* Init ELF parser\n");
112+
KDEBUG("* Init ELF parser\n");
102113
_init_elf_parser();
103114

104115
// Begin portable HAL initialization
105116
__machine->init();
117+
kernel::diag::hook<kernel::diag::post_machine_init>();
106118

107119
// TODO: Move more stuff into Machine::init
108120
RNG::init();
109121

110-
PRATTLE("* Init syscalls\n");
111-
_init_syscalls();
122+
KDEBUG("* Init per CPU crash contexts\n");
123+
__init_crash_contexts();
112124

113-
PRATTLE("* Init CPU exceptions\n");
125+
KDEBUG("* Init CPU exceptions\n");
114126
x86::idt_initialize_for_cpu(0);
115127

116128
x86::init_libc(magic, addr);

src/platform/x86_solo5/kernel_start.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern "C" {
1010
extern "C" {
1111
void __init_sanity_checks();
1212
uintptr_t _move_symbols(uintptr_t loc);
13-
void _init_syscalls();
13+
void __init_crash_contexts();
1414
void _init_elf_parser();
1515
}
1616

@@ -59,7 +59,7 @@ void kernel_start()
5959
__machine->init();
6060

6161
// Initialize system calls
62-
_init_syscalls();
62+
__init_crash_contexts();
6363

6464
x86::init_libc((uint32_t) (uintptr_t) temp_cmdline, 0);
6565
}

0 commit comments

Comments
 (0)