-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprintk.c
78 lines (63 loc) · 1.18 KB
/
printk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <fbos/printk.h>
#include <fbos/sched.h>
#include <fbos/string.h>
#include <fbos/sbi.h>
__kernel __noreturn void die(const char *const message)
{
if (message) {
printk(message);
}
for (;;)
;
}
__kernel void print_digit(uint64_t digit)
{
char buffer[2];
if (digit > 9) {
die("We cannot print numbers with two or more digits :D\n");
}
buffer[0] = '0' + digit;
buffer[1] = '\0';
write(buffer, 2);
}
__kernel void write(const char *const message, size_t n)
{
struct sbi_ret ret = sbi_ecall2(DBCN_EXT, DBCN_WRITE, n, (unsigned long)message);
if (ret.error != SBI_SUCCESS) {
die(nullptr);
}
}
#ifdef __DEBUG__
__kernel void print_task_prefix(void)
{
register struct task_struct *current asm("tp");
if (!current) {
return;
}
size_t len = strlen(current->name);
if (!len) {
return;
}
write("[=> ", 4);
write(current->name, len);
write("] ", 2);
}
#endif
__kernel void printk(const char *const message)
{
#ifdef __DEBUG__
print_task_prefix();
#endif
size_t len = strlen(message);
if (!len) {
return;
}
write(message, len);
}
__kernel void sys_write(const char *const message, size_t n)
{
#ifdef __DEBUG__
print_task_prefix();
#endif
write(message, n);
}