Skip to content

Commit

Permalink
riscv: support early_printk
Browse files Browse the repository at this point in the history
Because printk() relies on a serial driver (like the ns16550 driver)
and drivers require working virtual memory (ioremap()) there is not
print functionality early in Xen boot.

This commit adds early printk implementation built on the putc SBI call.

Signed-off-by: Bobby Eshleman <bobby.eshleman@gmail.com>
  • Loading branch information
beshleman authored and xiexun162534 committed May 26, 2022
1 parent 6cd6752 commit a3c9916
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions xen/arch/riscv/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ obj-y += domctl.o
obj-y += domain.o
obj-y += domain_build.o
obj-y += delay.o
obj-y += early_printk.o
obj-y += entry.o
obj-y += guestcopy.o
obj-y += irq.o
Expand Down
48 changes: 48 additions & 0 deletions xen/arch/riscv/early_printk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* RISC-V early printk using SBI
*
* Copyright (C) 2021 Bobby Eshleman <bobbyeshleman@gmail.com>
*/
#include <asm/sbi.h>
#include <asm/early_printk.h>
#include <xen/stdarg.h>
#include <xen/lib.h>

void _early_puts(const char *s, size_t nr)
{
while ( nr-- > 0 )
{
if (*s == '\n')
sbi_console_putchar('\r');
sbi_console_putchar(*s);
s++;
}
}

static void vprintk_early(const char *prefix, const char *fmt, va_list args)
{
char buf[128];
int sz;

early_puts(prefix);

sz = vscnprintf(buf, sizeof(buf), fmt, args);

if ( sz < 0 ) {
early_puts("(XEN) vprintk_early error\n");
return;
}

if ( sz == 0 )
return;

_early_puts(buf, sz);
}

void early_printk(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintk_early("(XEN) ", fmt, args);
va_end(args);
}
10 changes: 10 additions & 0 deletions xen/arch/riscv/include/asm/early_printk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __EARLY_PRINTK_H__
#define __EARLY_PRINTK_H__

#include <xen/string.h>

#define early_puts(s) _early_puts((s), strlen((s)))
void _early_puts(const char *s, size_t nr);
void early_printk(const char *fmt, ...);

#endif /* __EARLY_PRINTK_H__ */

0 comments on commit a3c9916

Please sign in to comment.