Skip to content

Commit

Permalink
feat(util/printk): new more readable and robust printk implemenation
Browse files Browse the repository at this point in the history
A from-sratch improved implementation of printk that allows printing
strings larger than the allocated print buffer. It also makes this
buffer statically allocated instead of allocating it on the printing cpu
stack.

Finally, it removes the per-uart implementation uart_puts function as this is
essnetially replicated behavior, only the uart_putc is needed. This
logic is now part of the console itself.

The format printf attribute is also added so that errors in the format
string type identifiers are detected at compile time.

Signed-off-by: Jose Martins <[email protected]>
  • Loading branch information
josecm authored and DavidMCerdeira committed Oct 20, 2023
1 parent ef30f5b commit 6805eee
Show file tree
Hide file tree
Showing 19 changed files with 221 additions and 216 deletions.
8 changes: 4 additions & 4 deletions src/arch/armv8/aarch32/aborts.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
void internal_abort_handler(unsigned long gprs[]) {

for(ssize_t i = 14; i >= 0; i--) {
printk("x%d:\t\t0x%0lx\n", i, gprs[14 - i]);
console_printk("x%d:\t\t0x%0lx\n", i, gprs[14 - i]);
}
printk("ESR:\t0x%0lx\n", sysreg_esr_el2_read());
printk("ELR:\t0x%0lx\n", sysreg_elr_el2_read());
printk("FAR:\t0x%0lx\n", sysreg_far_el2_read());
console_printk("ESR:\t0x%0lx\n", sysreg_esr_el2_read());
console_printk("ELR:\t0x%0lx\n", sysreg_elr_el2_read());
console_printk("FAR:\t0x%0lx\n", sysreg_far_el2_read());
ERROR("cpu%d internal hypervisor abort - PANIC\n", cpu()->id);
}
10 changes: 5 additions & 5 deletions src/arch/armv8/aarch64/aborts.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
void internal_abort_handler(unsigned long gprs[]) {

for(size_t i = 0; i < 31; i++) {
printk("x%d:\t\t0x%0lx\n", i, gprs[i]);
console_printk("x%d:\t\t0x%0lx\n", i, gprs[i]);
}
printk("SP:\t\t0x%0lx\n", gprs[31]);
printk("ESR:\t0x%0lx\n", sysreg_esr_el2_read());
printk("ELR:\t0x%0lx\n", sysreg_elr_el2_read());
printk("FAR:\t0x%0lx\n", sysreg_far_el2_read());
console_printk("SP:\t\t0x%0lx\n", gprs[31]);
console_printk("ESR:\t0x%0lx\n", sysreg_esr_el2_read());
console_printk("ELR:\t0x%0lx\n", sysreg_elr_el2_read());
console_printk("FAR:\t0x%0lx\n", sysreg_far_el2_read());
ERROR("cpu%d internal hypervisor abort - PANIC\n", cpu()->id);
}
8 changes: 4 additions & 4 deletions src/arch/riscv/sync_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
void internal_exception_handler(unsigned long gprs[]) {

for(int i = 0; i < 31; i++) {
printk("x%d:\t\t0x%0lx\n", i, gprs[i]);
console_printk("x%d:\t\t0x%0lx\n", i, gprs[i]);
}
printk("sstatus:\t0x%0lx\n", CSRR(sstatus));
printk("stval:\t\t0x%0lx\n", CSRR(stval));
printk("sepc:\t\t0x%0lx\n", CSRR(sepc));
console_printk("sstatus:\t0x%0lx\n", CSRR(sstatus));
console_printk("stval:\t\t0x%0lx\n", CSRR(stval));
console_printk("sepc:\t\t0x%0lx\n", CSRR(sepc));
ERROR("cpu%d internal hypervisor abort - PANIC\n", cpu()->id);
}

Expand Down
44 changes: 35 additions & 9 deletions src/core/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
#include <mem.h>
#include <fences.h>
#include <spinlock.h>
#include <printk.h>
#include <util.h>

volatile bao_uart_t *uart;
bool ready = false;
static spinlock_t print_lock = SPINLOCK_INITVAL;
static volatile bao_uart_t *uart;
static bool console_ready = false;
static spinlock_t console_lock = SPINLOCK_INITVAL;

void console_init()
{
Expand All @@ -31,16 +33,40 @@ void console_init()
uart_init(uart);
uart_enable(uart);

ready = true;
console_ready = true;
}

cpu_sync_and_clear_msgs(&cpu_glb_sync);
}

void console_write(char const* const str)

void console_write(const char* buf, size_t n)
{
if (!ready) return;
spin_lock(&print_lock);
uart_puts(uart, str);
spin_unlock(&print_lock);
while (!console_ready);
for (size_t i = 0; i < n; i++) {
if (buf[i] == '\n') {
uart_putc(uart, '\r');
}
uart_putc(uart, buf[i]);
}
}

#define PRINTF_BUFFER_LEN (256)
static char console_bufffer[PRINTF_BUFFER_LEN];

__attribute__((format(printf, 1, 2))) void console_printk(const char* fmt, ...)
{
va_list args;
size_t chars_writen;
const char* fmt_it = fmt;

va_start(args, fmt);
spin_lock(&console_lock);
while (*fmt_it != '\0') {
chars_writen =
vsnprintk(console_bufffer, PRINTF_BUFFER_LEN, &fmt_it, &args);
console_write(console_bufffer, min(PRINTF_BUFFER_LEN, chars_writen));
}
spin_unlock(&console_lock);
va_end(args);
}
16 changes: 8 additions & 8 deletions src/core/inc/bao.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
#ifndef __ASSEMBLER__

#include <types.h>
#include <printk.h>
#include <console.h>
#include <util.h>

#define INFO(args, ...) \
printk("BAO INFO: " args "\n" __VA_OPT__(, ) __VA_ARGS__);
console_printk("BAO INFO: " args "\n" __VA_OPT__(, ) __VA_ARGS__);

#define WARNING(args, ...) \
printk("BAO WARNING: " args "\n" __VA_OPT__(, ) __VA_ARGS__);
console_printk("BAO WARNING: " args "\n" __VA_OPT__(, ) __VA_ARGS__);

#define ERROR(args, ...) \
{ \
printk("BAO ERROR: " args "\n" __VA_OPT__(, ) __VA_ARGS__); \
while (1) \
; \
#define ERROR(args, ...) \
{ \
console_printk("BAO ERROR: " args "\n" __VA_OPT__(, ) __VA_ARGS__); \
while (1) \
; \
}

#endif /* __ASSEMBLER__ */
Expand Down
3 changes: 2 additions & 1 deletion src/core/inc/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <bao.h>

void console_init();
void console_write(char const* const str);
void console_write(const char* buf, size_t n);
void console_printk(const char* fmt, ...);

#endif /* __CONSOLE_H__ */
2 changes: 1 addition & 1 deletion src/core/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void init(cpuid_t cpu_id, paddr_t load_addr)
console_init();

if (cpu()->id == CPU_MASTER) {
printk("Bao Hypervisor\n\r");
console_printk("Bao Hypervisor\n\r");
}

interrupts_init();
Expand Down
4 changes: 3 additions & 1 deletion src/lib/inc/printk.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#define __PRINTK_H

#include <bao.h>
#include <stdarg.h>

size_t printk(const char *fmt, ...);
size_t vsnprintk(char* buf, size_t buf_size, const char** fmt,
va_list* args);

#endif /* __PRINTK_H */
Loading

0 comments on commit 6805eee

Please sign in to comment.