Signed-off-by: Xie Xun <xiexun162534@gmail.com>
---
xen/arch/riscv/Makefile | 1 +
xen/arch/riscv/early_printk.c | 48 +++++++++++++++++++++++
xen/arch/riscv/include/asm/early_printk.h | 10 +++++
3 files changed, 59 insertions(+)
create mode 100644 xen/arch/riscv/early_printk.c
create mode 100644 xen/arch/riscv/include/asm/early_printk.h
diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile
index c61349818f..f9abc8401b 100644
--- a/xen/arch/riscv/Makefile
+++ b/xen/arch/riscv/Makefile
@@ -3,6 +3,7 @@ obj-y += lib/
obj-y += domctl.o
obj-y += domain.o
obj-y += delay.o
+obj-y += early_printk.o
obj-y += guestcopy.o
obj-y += irq.o
obj-y += p2m.o
diff --git a/xen/arch/riscv/early_printk.c b/xen/arch/riscv/early_printk.c
new file mode 100644
index 0000000000..81d69add01
--- /dev/null
+++ b/xen/arch/riscv/early_printk.c
@@ -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);
+}
diff --git a/xen/arch/riscv/include/asm/early_printk.h b/xen/arch/riscv/include/asm/early_printk.h
new file mode 100644
index 0000000000..0d9928b333
--- /dev/null
+++ b/xen/arch/riscv/include/asm/early_printk.h
@@ -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__ */
--
2.30.2
On Tue, May 31, 2022 at 5:09 PM Xie Xun <xiexun162534@gmail.com> wrote:
>
> Signed-off-by: Xie Xun <xiexun162534@gmail.com>
> ---
> xen/arch/riscv/Makefile | 1 +
> xen/arch/riscv/early_printk.c | 48 +++++++++++++++++++++++
> xen/arch/riscv/include/asm/early_printk.h | 10 +++++
> 3 files changed, 59 insertions(+)
> create mode 100644 xen/arch/riscv/early_printk.c
> create mode 100644 xen/arch/riscv/include/asm/early_printk.h
>
> diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile
> index c61349818f..f9abc8401b 100644
> --- a/xen/arch/riscv/Makefile
> +++ b/xen/arch/riscv/Makefile
> @@ -3,6 +3,7 @@ obj-y += lib/
> obj-y += domctl.o
> obj-y += domain.o
> obj-y += delay.o
> +obj-y += early_printk.o
> obj-y += guestcopy.o
> obj-y += irq.o
> obj-y += p2m.o
> diff --git a/xen/arch/riscv/early_printk.c b/xen/arch/riscv/early_printk.c
> new file mode 100644
> index 0000000000..81d69add01
> --- /dev/null
> +++ b/xen/arch/riscv/early_printk.c
This should be named differently. This file should be called
`sbi_console_early_printk.c` to better indicate that it's using the
sbi_console
The SBI print functions are useful, but they have been marked as
deprecated with no future replacement (see
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/659950dc57f9840cf8242c1cff138c2ee67634bb/riscv-sbi.adoc#5-legacy-extensions-eids-0x00---0x0f)
For the initial port I think it's ok to use these, but this isn't a
long term solution, we should aim to migrate to using the standard
hardware UART.
I'm sure Xen already has a driver for the ns16550 UART so it might be
worth just using that directly and not worrying with the sbi_console.
Alistair
> @@ -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);
> +}
> diff --git a/xen/arch/riscv/include/asm/early_printk.h b/xen/arch/riscv/include/asm/early_printk.h
> new file mode 100644
> index 0000000000..0d9928b333
> --- /dev/null
> +++ b/xen/arch/riscv/include/asm/early_printk.h
> @@ -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__ */
> --
> 2.30.2
>
>
On 01.06.2022 05:59, Alistair Francis wrote: > On Tue, May 31, 2022 at 5:09 PM Xie Xun <xiexun162534@gmail.com> wrote: >> --- /dev/null >> +++ b/xen/arch/riscv/early_printk.c > > This should be named differently. This file should be called > `sbi_console_early_printk.c` to better indicate that it's using the > sbi_console To not go overboard with file name length, perhaps sbi-earlyprintk.c or sbi-early-printk.c would suffice? Or, if other variants are to appear, *-sbi.c? > The SBI print functions are useful, but they have been marked as > deprecated with no future replacement (see > https://github.com/riscv-non-isa/riscv-sbi-doc/blob/659950dc57f9840cf8242c1cff138c2ee67634bb/riscv-sbi.adoc#5-legacy-extensions-eids-0x00---0x0f) > > For the initial port I think it's ok to use these, but this isn't a > long term solution, we should aim to migrate to using the standard > hardware UART. > > I'm sure Xen already has a driver for the ns16550 UART so it might be > worth just using that directly and not worrying with the sbi_console. Of course we have a driver for that, but depending on the device flavors in use on RISC-V it may require touching. Jan
© 2016 - 2025 Red Hat, Inc.