Skip to content

Commit 595f04d

Browse files
author
zhiayang
committed
kernel: switch to zpr for all printing and logging
1 parent 005dfec commit 595f04d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2830
-614
lines changed

efx/source/efx_main.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ void efx::init()
4848
auto kernelPath = options::get_option("kernel");
4949
if(kernelPath.empty()) efi::abort("did not specify 'kernel' option!");
5050

51+
52+
efi::println("reading kernel from disk...");
53+
5154
size_t len = 0;
5255
auto buf = fs::readFile(kernelPath, &len);
5356

@@ -90,8 +93,13 @@ void efx::init()
9093
memset((uint8_t*) out + len, 0, pages * 0x1000 - len);
9194

9295
auto crc32 = [](const uint8_t* ptr, size_t buf_len) -> uint32_t {
93-
static const uint32_t s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
94-
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
96+
static const uint32_t s_crc32[16] = {
97+
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
98+
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
99+
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
100+
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
101+
};
102+
95103
uint32_t crcu32 = 0;
96104
if(!ptr) return 0;
97105

efx/source/loader.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ namespace efx
192192

193193
switch(efiEnt->Type)
194194
{
195-
196195
case EfiBootServicesCode: // fallthrough
197196
case EfiBootServicesData: entries[k].memoryType = nx::MemoryType::Reserved; break;
198197

@@ -242,6 +241,7 @@ namespace efx
242241
entries[k].memoryType = (nx::MemoryType) customExtents[i].type;
243242

244243
bi->mmEntryCount += 1;
244+
k += 1;
245245
}
246246
}
247247
}
@@ -251,10 +251,14 @@ namespace efx
251251

252252
} break;
253253

254-
default: skip = true; break; // do not include
254+
// do not include
255+
default:
256+
skip = true;
257+
break;
255258
}
256259

257-
if(skip) continue;
260+
if(skip)
261+
continue;
258262

259263
entries[k].address = efiEnt->PhysicalStart;
260264
entries[k].numPages = efiEnt->NumberOfPages;
@@ -273,6 +277,7 @@ namespace efx
273277

274278
entries[k].address = customExtents[i].base;
275279
entries[k].numPages = customExtents[i].num;
280+
entries[k].memoryType = (nx::MemoryType) customExtents[i].type;
276281
entries[k].efiAttributes = 0;
277282

278283
bi->mmEntryCount += 1;

efx/source/support/memory.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ namespace memory
113113
{
114114
auto entry = (efi_memory_descriptor*) (buffer + (i * descSz));
115115

116-
if(krt::match(entry->Type, EfiLoaderCode, EfiBootServicesCode, EfiBootServicesData, efi::MemoryType_BootInfo,
117-
efi::MemoryType_MemoryMap, efi::MemoryType_Initrd, efi::MemoryType_KernelElf))
116+
if(krt::match(entry->Type, EfiLoaderCode, EfiBootServicesCode, EfiBootServicesData))
118117
{
118+
// efi::println("map %p - %p", entry->PhysicalStart, entry->PhysicalStart + entry->NumberOfPages * 0x1000);
119119
mapVirtual(entry->PhysicalStart, entry->PhysicalStart, entry->NumberOfPages);
120120
}
121121

@@ -295,7 +295,12 @@ namespace efi
295295
efi::abort_if_error(stat, "failed to allocate memory for %s!", user);
296296

297297
// we need to pass the originally intended type to the accounting part.
298-
efx::memory::accountMemory(out, numPages, memType);
298+
if(efiType != memType)
299+
efx::memory::accountMemory(out, numPages, memType);
300+
301+
// if this isn't for the vmm mapping, then map it in the kernel's (future) cr3
302+
if(memType != efi::MemoryType_VMFrame)
303+
efx::memory::mapVirtual(out, out, numPages);
299304

300305
return (void*) out;
301306
}

kernel/include/defs.h

+91-24
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,75 @@
1010

1111
#include "krt.h"
1212

13+
#define ZPR_FREESTANDING 1
14+
#include <zst/zpr.h>
15+
1316
using addr_t = uintptr_t;
1417

1518
namespace nx
1619
{
1720
constexpr size_t PAGE_SIZE = 0x1000;
1821

22+
[[noreturn]] void __internal_abort();
23+
void console_print(const char* s, size_t len);
24+
25+
namespace interrupts
26+
{
27+
void enable();
28+
void disable();
29+
}
30+
31+
namespace serial
32+
{
33+
void debugprintchar(char c);
34+
void debugprint(const char* c, size_t l);
35+
36+
template <typename... Args>
37+
void debugprintf(const char* fmt, Args&&... args)
38+
{
39+
zpr::cprint(&serial::debugprint, fmt, static_cast<Args&&>(args)...);
40+
}
41+
}
42+
43+
44+
template <typename... Args>
45+
void print(const char* fmt, Args&&... args)
46+
{
47+
zpr::cprint(&console_print, fmt, static_cast<Args&&>(args)...);
48+
}
49+
50+
template <typename... Args>
51+
void println(const char* fmt, Args&&... args)
52+
{
53+
zpr::cprintln(&console_print, fmt, static_cast<Args&&>(args)...);
54+
}
55+
56+
template <typename... Args>
57+
[[noreturn]] void abort(const char* fmt, Args&&... args)
58+
{
59+
serial::debugprintf("\n\nkernel abort! error: ");
60+
serial::debugprintf(fmt, static_cast<Args&&>(args)...);
61+
62+
__internal_abort();
63+
}
64+
65+
template <typename... Args>
66+
[[noreturn]] void assert_fail(const char* file, size_t line, const char* thing, const char* fmt, Args&&... args)
67+
{
68+
interrupts::disable();
69+
70+
println("failed assertion");
71+
println("invariant: {}", thing);
72+
print("reason: ");
73+
print(fmt, static_cast<Args&&>(args)...);
74+
75+
print("\nlocation: {}:{}\n\n", file, line);
76+
77+
__internal_abort();
78+
}
79+
80+
81+
1982
struct _allocator
2083
{
2184
static void* allocate(size_t sz, size_t align);
@@ -34,10 +97,18 @@ namespace nx
3497

3598
struct _aborter
3699
{
37-
static void abort(const char* fmt, ...);
38-
static void debuglog(const char* fmt, ...);
39-
};
100+
template <typename... Args>
101+
static void abort(const char* fmt, Args&&... args)
102+
{
103+
abort(fmt, static_cast<Args&&>(args)...);
104+
}
40105

106+
template <typename... Args>
107+
static void debuglog(const char* fmt, Args&&... args)
108+
{
109+
abort(fmt, static_cast<Args&&>(args)...);
110+
}
111+
};
41112

42113
// re-export the krt types with our own stuff.
43114
using string = krt::string<_allocator, _aborter>;
@@ -76,19 +147,8 @@ namespace nx
76147
constexpr auto none = krt::nullopt;
77148
}
78149

79-
80150
[[noreturn]] void halt();
81151

82-
[[noreturn]] void vabort(const char* fmt, va_list args);
83-
[[noreturn]] void abort(const char* fmt, ...);
84-
85-
void vabort_nohalt(const char* fmt, va_list args);
86-
void abort_nohalt(const char* fmt, ...);
87-
88-
[[noreturn]] void assert_fail(const char* file, size_t line, const char* thing);
89-
[[noreturn]] void assert_fail(const char* file, size_t line, const char* thing, const char* fmt, ...);
90-
91-
92152
namespace scheduler
93153
{
94154
struct Thread;
@@ -114,17 +174,28 @@ namespace nx
114174
constexpr uint64_t ALIGN_BITS = 12;
115175
}
116176

117-
void dbg(const char* sys, const char* fmt, ...);
118-
void log(const char* sys, const char* fmt, ...);
119-
void warn(const char* sys, const char* fmt, ...);
120-
void error(const char* sys, const char* fmt, ...);
177+
178+
179+
180+
template <typename... Args>
181+
nx::string sprint(const char* fmt, Args&&... args)
182+
{
183+
nx::string ret;
184+
zpr::cprint([&ret](const char* s, size_t len) {
185+
186+
ret += nx::string_view(s, len);
187+
188+
}, fmt, static_cast<Args&&>(args)...);
189+
190+
return ret;
191+
}
121192
}
122193

123194
// wow fucking c++ is so poorly designed
124195
namespace std
125196
{
126197
enum class align_val_t : size_t { };
127-
using max_align_t = long double;
198+
// using max_align_t = long double;
128199
}
129200

130201
[[nodiscard]] void* operator new (size_t count);
@@ -145,11 +216,7 @@ void operator delete[] (void* ptr, size_t al);
145216
#undef assert
146217
#endif
147218

148-
#define assert(x) ((x) ? ((void) 0) : ::nx::assert_fail(__FILE__, __LINE__, #x))
149-
150-
#define VA_ARGS(...) , ##__VA_ARGS__
151-
#define kassert(x, fmt, ...) ((x) ? ((void) 0) : ::nx::assert_fail(__FILE__, __LINE__, #x, fmt VA_ARGS(__VA_ARGS__)))
152-
219+
#define assert(x) ((x) ? ((void) 0) : ::nx::assert_fail(__FILE__, __LINE__, #x, ""))
153220

154221
#define __unlikely(x) __builtin_expect((x), 0)
155222
#define __likely(x) __builtin_expect((x), 1)

kernel/include/devices/serial.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ namespace nx
1414
void init();
1515
void initReceive(void (*callback)(uint8_t));
1616

17-
void debugprint(char c);
18-
void debugprint(char* c, size_t l);
17+
void debugprintchar(char c);
18+
void debugprint(const char* c, size_t l);
1919
void debugprint(const char* s);
20-
21-
void debugprintf(const char* fmt, ...);
2220
}
2321
}

kernel/include/extmm.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#pragma once
66

7-
7+
#include "log.h"
88
#include "synchro.h"
99
#include "devices/serial.h"
1010

@@ -118,7 +118,7 @@ namespace extmm
118118
template <typename Predicate>
119119
addr_t allocate(size_t num, Predicate&& satisfies)
120120
{
121-
if(num == 0) abort("extmm/%s::allocate(): cannot allocate 0 pages!", this->owner);
121+
if(num == 0) abort("extmm/{}::allocate(): cannot allocate 0 pages!", this->owner);
122122

123123
autolock lk(&this->lock);
124124

@@ -155,7 +155,7 @@ namespace extmm
155155
ext = ext->next;
156156
}
157157

158-
error("extmm", "(%s): allocate() out of pages!", this->owner);
158+
error("extmm", "({}): allocate() out of pages!", this->owner);
159159
return 0;
160160
}
161161

@@ -198,7 +198,7 @@ namespace extmm
198198
ext = ext->next;
199199
}
200200

201-
serial::debugprintf("extmm/%s::allocateSpecific(): could not fulfil request!\n", this->owner);
201+
serial::debugprintf("extmm/{}::allocateSpecific(): could not fulfil request!\n", this->owner);
202202
return 0;
203203
}
204204

@@ -228,7 +228,7 @@ namespace extmm
228228
else if(ext->addr == addr)
229229
{
230230
// hmm...
231-
abort("extmm: potential double free! addr: %p, size: %zu", addr, num);
231+
abort("extmm: potential double free! addr: {p}, size: {}", addr, num);
232232
}
233233

234234
ext = ext->next;
@@ -241,11 +241,11 @@ namespace extmm
241241

242242
void dump()
243243
{
244-
serial::debugprintf("dumping extmm state %s (%p):\n", this->owner, this);
244+
serial::debugprintf("dumping extmm state {} ({p}):\n", this->owner, this);
245245
auto ext = this->head;
246246
while(ext)
247247
{
248-
serial::debugprintf(" %p - %p (%zu)\n", ext->addr, end(ext), ext->size);
248+
serial::debugprintf(" {p} - {p} ({})\n", ext->addr, end(ext), ext->size);
249249
ext = ext->next;
250250
}
251251
serial::debugprintf("\n");
@@ -263,7 +263,7 @@ namespace extmm
263263
// check if they intersect.
264264
if(intersects(ext1, ext2))
265265
{
266-
error("extmm", "inconsistent extent state! (%p, %p, %zu) intersects (%p, %p, %zu)",
266+
error("extmm", "inconsistent extent state! ({p}, {p}, {}) intersects ({p}, {p}, {})",
267267
ext1->addr, end(ext1), ext1->size, ext2->addr, end(ext2), ext2->size);
268268

269269
return false;
@@ -274,7 +274,7 @@ namespace extmm
274274

275275
if(ext1->size == 0)
276276
{
277-
error("extmm", "inconsistent extent state! extent at %p has 0 size", ext1->addr);
277+
error("extmm", "inconsistent extent state! extent at {p} has 0 size", ext1->addr);
278278
return false;
279279
}
280280

0 commit comments

Comments
 (0)