From 486afba4fe4e973df0c57635ba3969dd11e8699f Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Thu, 3 Apr 2025 15:56:31 +0800 Subject: [PATCH] lkl: Implement lkl_printf and lkl_bug internally Currently, lkl.o needs the lkl_printf and lkl_bug symbols to work but does not implement the corresponding functions. These helper functions are implemented as part of liblkl in tools/lkl/lib/util.c. This works, but it requires users of lkl.o to implement lkl_printf manually. However, this does not make much sense, because lkl_printf, by definition, is just a wrapper to host_ops->print. In fact, lkl_printf can be implemented correctly using the utilities from linux/stdarg.h and linux/sprintf.h, as well as the host operations host_ops->mem_alloc and host_ops->print. See changes in this commit for more details. Implementing it internally in arch/lkl makes lkl.o more self-contained and easier to use in environments without libc support (since in such environments tool/lkl/lib/util.c may fail to compile due to missing headers like stdio.h and stdarg.h). Signed-off-by: Ruihan Li --- arch/lkl/include/uapi/asm/host_ops.h | 15 ++++++-- arch/lkl/lib/Makefile | 2 +- arch/lkl/lib/host_ops.c | 53 ++++++++++++++++++++++++++++ tools/lkl/include/lkl_host.h | 7 ---- tools/lkl/lib/utils.c | 49 ------------------------- 5 files changed, 67 insertions(+), 59 deletions(-) create mode 100644 arch/lkl/lib/host_ops.c diff --git a/arch/lkl/include/uapi/asm/host_ops.h b/arch/lkl/include/uapi/asm/host_ops.h index ac4c0f1c7075c3..09570ea2f548ba 100644 --- a/arch/lkl/include/uapi/asm/host_ops.h +++ b/arch/lkl/include/uapi/asm/host_ops.h @@ -226,7 +226,18 @@ void lkl_cleanup(void); */ int lkl_is_running(void); -int lkl_printf(const char *, ...); -void lkl_bug(const char *, ...); +/** + * lkl_printf - print a message via the host print operation + * + * @fmt: printf like format string + */ +int lkl_printf(const char *fmt, ...); + +/** + * lkl_bug - print a message and panic via the host operations + * + * @fmt: printf like format string + */ +void lkl_bug(const char *fmt, ...); #endif diff --git a/arch/lkl/lib/Makefile b/arch/lkl/lib/Makefile index 305ad781ea6581..03539d6514d2a8 100644 --- a/arch/lkl/lib/Makefile +++ b/arch/lkl/lib/Makefile @@ -1,3 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y += string.o +obj-y += host_ops.o string.o diff --git a/arch/lkl/lib/host_ops.c b/arch/lkl/lib/host_ops.c new file mode 100644 index 00000000000000..eadef31021a20b --- /dev/null +++ b/arch/lkl/lib/host_ops.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +static int lkl_vprintf(const char *fmt, va_list args) +{ + int n; + char *buffer; + va_list copy; + + if (!lkl_ops->print) + return 0; + + va_copy(copy, args); + n = vsnprintf(NULL, 0, fmt, copy); + va_end(copy); + + buffer = lkl_ops->mem_alloc(n + 1); + if (!buffer) + return -1; + + vsnprintf(buffer, n + 1, fmt, args); + + lkl_ops->print(buffer, n); + lkl_ops->mem_free(buffer); + + return n; +} + +int lkl_printf(const char *fmt, ...) +{ + int n; + va_list args; + + va_start(args, fmt); + n = lkl_vprintf(fmt, args); + va_end(args); + + return n; +} + +void lkl_bug(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + lkl_vprintf(fmt, args); + va_end(args); + + lkl_ops->panic(); +} diff --git a/tools/lkl/include/lkl_host.h b/tools/lkl/include/lkl_host.h index 05cd459a841fe3..a02add72a89f3b 100644 --- a/tools/lkl/include/lkl_host.h +++ b/tools/lkl/include/lkl_host.h @@ -11,13 +11,6 @@ extern "C" { extern struct lkl_host_operations lkl_host_ops; extern void lkl_change_tls_mode(void); -/** - * lkl_printf - print a message via the host print operation - * - * @fmt: printf like format string - */ -int lkl_printf(const char *fmt, ...); - extern char lkl_virtio_devs[4096]; #if defined(LKL_HOST_CONFIG_POSIX) || defined(__MSYS__) diff --git a/tools/lkl/lib/utils.c b/tools/lkl/lib/utils.c index af69b1511356d1..b8d99029969378 100644 --- a/tools/lkl/lib/utils.c +++ b/tools/lkl/lib/utils.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -159,54 +158,6 @@ void lkl_perror(char *msg, int err) lkl_printf("%s: %s\n", msg, err_msg); } -static int lkl_vprintf(const char *fmt, va_list args) -{ - int n; - char *buffer; - va_list copy; - - if (!lkl_host_ops.print) - return 0; - - va_copy(copy, args); - n = vsnprintf(NULL, 0, fmt, copy); - va_end(copy); - - buffer = lkl_host_ops.mem_alloc(n + 1); - if (!buffer) - return -1; - - vsnprintf(buffer, n + 1, fmt, args); - - lkl_host_ops.print(buffer, n); - lkl_host_ops.mem_free(buffer); - - return n; -} - -int lkl_printf(const char *fmt, ...) -{ - int n; - va_list args; - - va_start(args, fmt); - n = lkl_vprintf(fmt, args); - va_end(args); - - return n; -} - -void lkl_bug(const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - lkl_vprintf(fmt, args); - va_end(args); - - lkl_host_ops.panic(); -} - int lkl_sysctl(const char *path, const char *value) { int ret;