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>
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
2021extern 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+
3446extern " C"
3547int 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 (" \t Elf size: %zu \n " , size);
99+ KDEBUG (" \t Elf size: %zu \n " , size);
92100 for (int i = 0 ; i < ehdr->e_phnum ; i++)
93101 {
94- PRATTLE (" \t Phdr %i @ %p, va_addr: 0x%lx \n " , i, &phdr[i], phdr[i].p_vaddr );
102+ KDEBUG (" \t Phdr %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 }
0 commit comments