2015-04-11 01:44:37 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, 2013-2014 Wind River Systems, Inc.
|
|
|
|
*
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-11 01:44:37 +02:00
|
|
|
*/
|
|
|
|
|
2015-07-16 20:07:57 +02:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Low-level debug output
|
|
|
|
*
|
2015-07-24 22:55:19 +02:00
|
|
|
* Low-level debugging output. Platform installs a character output routine at
|
2015-07-16 20:07:57 +02:00
|
|
|
* init time. If no routine is installed, a nop routine is called.
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2022-05-06 11:23:05 +02:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/sys/printk.h>
|
2015-04-11 01:44:37 +02:00
|
|
|
#include <stdarg.h>
|
2022-05-06 11:23:05 +02:00
|
|
|
#include <zephyr/toolchain.h>
|
|
|
|
#include <zephyr/linker/sections.h>
|
2023-09-27 00:46:01 +02:00
|
|
|
#include <zephyr/internal/syscall_handler.h>
|
2022-05-06 11:23:05 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
#include <zephyr/sys/cbprintf.h>
|
2023-09-27 15:10:10 +02:00
|
|
|
#include <zephyr/llext/symbol.h>
|
2019-06-14 04:44:07 +02:00
|
|
|
#include <sys/types.h>
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2022-01-19 07:31:25 +01:00
|
|
|
/* Option present only when CONFIG_USERSPACE enabled. */
|
|
|
|
#ifndef CONFIG_PRINTK_BUFFER_SIZE
|
|
|
|
#define CONFIG_PRINTK_BUFFER_SIZE 0
|
|
|
|
#endif
|
|
|
|
|
2022-01-19 14:43:00 +01:00
|
|
|
#if defined(CONFIG_PRINTK_SYNC)
|
2020-06-03 00:07:28 +02:00
|
|
|
static struct k_spinlock lock;
|
|
|
|
#endif
|
|
|
|
|
2020-01-02 20:57:43 +01:00
|
|
|
#ifdef CONFIG_PRINTK
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Default character output routine that does nothing
|
2015-07-16 20:07:57 +02:00
|
|
|
* @param c Character to swallow
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2018-08-19 19:58:06 +02:00
|
|
|
* Note this is defined as a weak symbol, allowing architecture code
|
|
|
|
* to override it where possible to enable very early logging.
|
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return 0
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2019-06-29 00:48:54 +02:00
|
|
|
/* LCOV_EXCL_START */
|
2019-11-07 21:43:29 +01:00
|
|
|
__attribute__((weak)) int arch_printk_char_out(int c)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
|
|
|
ARG_UNUSED(c);
|
|
|
|
|
|
|
|
/* do nothing */
|
|
|
|
return 0;
|
|
|
|
}
|
2019-06-29 00:48:54 +02:00
|
|
|
/* LCOV_EXCL_STOP */
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2021-11-14 14:15:46 +01:00
|
|
|
int (*_char_out)(int c) = arch_printk_char_out;
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Install the character output routine for printk
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-07-24 22:55:19 +02:00
|
|
|
* To be called by the platform's console driver at init time. Installs a
|
|
|
|
* routine that outputs one ASCII character at a time.
|
2015-07-16 20:07:57 +02:00
|
|
|
* @param fn putc routine to install
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2021-11-14 14:15:46 +01:00
|
|
|
void __printk_hook_install(int (*fn)(int c))
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
|
|
|
_char_out = fn;
|
|
|
|
}
|
|
|
|
|
2017-01-19 15:37:20 +01:00
|
|
|
/**
|
|
|
|
* @brief Get the current character output routine for printk
|
|
|
|
*
|
|
|
|
* To be called by any console driver that would like to save
|
|
|
|
* current hook - if any - for later re-installation.
|
|
|
|
*
|
|
|
|
* @return a function pointer or NULL if no hook is set
|
|
|
|
*/
|
|
|
|
void *__printk_get_hook(void)
|
|
|
|
{
|
|
|
|
return _char_out;
|
|
|
|
}
|
|
|
|
|
2017-10-13 19:24:30 +02:00
|
|
|
struct buf_out_context {
|
2022-06-24 00:51:06 +02:00
|
|
|
#ifdef CONFIG_PICOLIBC
|
|
|
|
FILE file;
|
|
|
|
#endif
|
2017-10-11 01:01:49 +02:00
|
|
|
unsigned int buf_count;
|
|
|
|
char buf[CONFIG_PRINTK_BUFFER_SIZE];
|
|
|
|
};
|
|
|
|
|
2017-10-13 19:24:30 +02:00
|
|
|
static void buf_flush(struct buf_out_context *ctx)
|
2017-10-11 01:01:49 +02:00
|
|
|
{
|
|
|
|
k_str_out(ctx->buf, ctx->buf_count);
|
2018-11-29 20:15:18 +01:00
|
|
|
ctx->buf_count = 0U;
|
2017-10-11 01:01:49 +02:00
|
|
|
}
|
|
|
|
|
2017-10-13 19:24:30 +02:00
|
|
|
static int buf_char_out(int c, void *ctx_p)
|
2017-10-11 01:01:49 +02:00
|
|
|
{
|
2017-10-13 19:24:30 +02:00
|
|
|
struct buf_out_context *ctx = ctx_p;
|
|
|
|
|
2017-10-11 01:01:49 +02:00
|
|
|
ctx->buf[ctx->buf_count++] = c;
|
|
|
|
if (ctx->buf_count == CONFIG_PRINTK_BUFFER_SIZE) {
|
|
|
|
buf_flush(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
2017-10-13 19:24:30 +02:00
|
|
|
|
|
|
|
static int char_out(int c, void *ctx_p)
|
2016-11-28 08:55:19 +01:00
|
|
|
{
|
2024-03-01 12:33:12 +01:00
|
|
|
ARG_UNUSED(ctx_p);
|
2016-11-28 08:55:19 +01:00
|
|
|
return _char_out(c);
|
|
|
|
}
|
|
|
|
|
2018-09-12 00:57:04 +02:00
|
|
|
void vprintk(const char *fmt, va_list ap)
|
2017-04-20 21:31:37 +02:00
|
|
|
{
|
2022-01-19 14:43:00 +01:00
|
|
|
if (IS_ENABLED(CONFIG_LOG_PRINTK)) {
|
|
|
|
z_log_vprintk(fmt, ap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-27 17:03:18 +01:00
|
|
|
if (k_is_user_context()) {
|
2022-06-24 00:51:06 +02:00
|
|
|
struct buf_out_context ctx = {
|
|
|
|
#ifdef CONFIG_PICOLIBC
|
|
|
|
.file = FDEV_SETUP_STREAM((int(*)(char, FILE *))buf_char_out,
|
|
|
|
NULL, NULL, _FDEV_SETUP_WRITE),
|
|
|
|
#else
|
|
|
|
0
|
|
|
|
#endif
|
|
|
|
};
|
2017-04-20 21:31:37 +02:00
|
|
|
|
2022-06-24 00:51:06 +02:00
|
|
|
#ifdef CONFIG_PICOLIBC
|
|
|
|
(void) vfprintf(&ctx.file, fmt, ap);
|
|
|
|
#else
|
2020-11-09 02:57:52 +01:00
|
|
|
cbvprintf(buf_char_out, &ctx, fmt, ap);
|
2022-06-24 00:51:06 +02:00
|
|
|
#endif
|
2017-10-13 19:24:30 +02:00
|
|
|
if (ctx.buf_count) {
|
|
|
|
buf_flush(&ctx);
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-03 00:07:28 +02:00
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
#endif
|
2017-10-13 19:24:30 +02:00
|
|
|
|
2022-06-24 00:51:06 +02:00
|
|
|
#ifdef CONFIG_PICOLIBC
|
|
|
|
FILE console = FDEV_SETUP_STREAM((int(*)(char, FILE *))char_out,
|
|
|
|
NULL, NULL, _FDEV_SETUP_WRITE);
|
|
|
|
(void) vfprintf(&console, fmt, ap);
|
|
|
|
#else
|
2022-06-24 00:48:46 +02:00
|
|
|
cbvprintf(char_out, NULL, fmt, ap);
|
2022-06-24 00:51:06 +02:00
|
|
|
#endif
|
2020-06-03 00:07:28 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
#endif
|
2017-10-11 01:01:49 +02:00
|
|
|
}
|
2017-10-13 19:24:30 +02:00
|
|
|
}
|
2024-01-09 15:50:31 +01:00
|
|
|
EXPORT_SYMBOL(vprintk);
|
2017-04-20 21:31:37 +02:00
|
|
|
|
2019-03-08 22:19:05 +01:00
|
|
|
void z_impl_k_str_out(char *c, size_t n)
|
2017-10-11 01:01:49 +02:00
|
|
|
{
|
2020-04-03 18:55:04 +02:00
|
|
|
size_t i;
|
2020-06-03 00:07:28 +02:00
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
#endif
|
2017-10-11 01:01:49 +02:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
_char_out(c[i]);
|
|
|
|
}
|
2020-06-03 00:07:28 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_PRINTK_SYNC
|
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
#endif
|
2017-10-11 01:01:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
userspace: Support for split 64 bit arguments
System call arguments, at the arch layer, are single words. So
passing wider values requires splitting them into two registers at
call time. This gets even more complicated for values (e.g
k_timeout_t) that may have different sizes depending on configuration.
This patch adds a feature to gen_syscalls.py to detect functions with
wide arguments and automatically generates code to split/unsplit them.
Unfortunately the current scheme of Z_SYSCALL_DECLARE_* macros won't
work with functions like this, because for N arguments (our current
maximum N is 10) there are 2^N possible configurations of argument
widths. So this generates the complete functions for each handler and
wrapper, effectively doing in python what was originally done in the
preprocessor.
Another complexity is that traditional the z_hdlr_*() function for a
system call has taken the raw list of word arguments, which does not
work when some of those arguments must be 64 bit types. So instead of
using a single Z_SYSCALL_HANDLER macro, this splits the job of
z_hdlr_*() into two steps: An automatically-generated unmarshalling
function, z_mrsh_*(), which then calls a user-supplied verification
function z_vrfy_*(). The verification function is typesafe, and is a
simple C function with exactly the same argument and return signature
as the syscall impl function. It is also not responsible for
validating the pointers to the extra parameter array or a wide return
value, that code gets automatically generated.
This commit includes new vrfy/msrh handling for all syscalls invoked
during CI runs. Future commits will port the less testable code.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-08-06 22:34:31 +02:00
|
|
|
static inline void z_vrfy_k_str_out(char *c, size_t n)
|
2017-10-11 01:01:49 +02:00
|
|
|
{
|
2023-09-27 13:20:28 +02:00
|
|
|
K_OOPS(K_SYSCALL_MEMORY_READ(c, n));
|
2019-03-08 22:19:05 +01:00
|
|
|
z_impl_k_str_out((char *)c, n);
|
2017-10-11 01:01:49 +02:00
|
|
|
}
|
userspace: Support for split 64 bit arguments
System call arguments, at the arch layer, are single words. So
passing wider values requires splitting them into two registers at
call time. This gets even more complicated for values (e.g
k_timeout_t) that may have different sizes depending on configuration.
This patch adds a feature to gen_syscalls.py to detect functions with
wide arguments and automatically generates code to split/unsplit them.
Unfortunately the current scheme of Z_SYSCALL_DECLARE_* macros won't
work with functions like this, because for N arguments (our current
maximum N is 10) there are 2^N possible configurations of argument
widths. So this generates the complete functions for each handler and
wrapper, effectively doing in python what was originally done in the
preprocessor.
Another complexity is that traditional the z_hdlr_*() function for a
system call has taken the raw list of word arguments, which does not
work when some of those arguments must be 64 bit types. So instead of
using a single Z_SYSCALL_HANDLER macro, this splits the job of
z_hdlr_*() into two steps: An automatically-generated unmarshalling
function, z_mrsh_*(), which then calls a user-supplied verification
function z_vrfy_*(). The verification function is typesafe, and is a
simple C function with exactly the same argument and return signature
as the syscall impl function. It is also not responsible for
validating the pointers to the extra parameter array or a wide return
value, that code gets automatically generated.
This commit includes new vrfy/msrh handling for all syscalls invoked
during CI runs. Future commits will port the less testable code.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2019-08-06 22:34:31 +02:00
|
|
|
#include <syscalls/k_str_out_mrsh.c>
|
2020-01-02 20:57:43 +01:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
2017-10-11 01:01:49 +02:00
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Output a string
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-07-24 22:55:19 +02:00
|
|
|
* Output a string on output installed by platform at init time. Some
|
|
|
|
* printf-like formatting is available.
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
|
|
|
* Available formatting:
|
2019-06-14 04:44:07 +02:00
|
|
|
* - %x/%X: outputs a number in hexadecimal format
|
|
|
|
* - %s: outputs a null-terminated string
|
|
|
|
* - %p: pointer, same as %x with a 0x prefix
|
|
|
|
* - %u: outputs a number in unsigned decimal format
|
|
|
|
* - %d/%i: outputs a number in signed decimal format
|
|
|
|
*
|
|
|
|
* Field width (with or without leading zeroes) is supported.
|
|
|
|
* Length attributes h, hh, l, ll and z are supported. However, integral
|
|
|
|
* values with %lld and %lli are only printed if they fit in a long
|
|
|
|
* otherwise 'ERR' is printed. Full 64-bit values may be printed with %llx.
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-07-16 20:07:57 +02:00
|
|
|
* @param fmt formatted string to output
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2020-12-22 19:55:11 +01:00
|
|
|
|
2018-09-12 00:57:04 +02:00
|
|
|
void printk(const char *fmt, ...)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
2015-04-15 00:15:52 +02:00
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
va_start(ap, fmt);
|
2018-05-16 08:50:33 +02:00
|
|
|
|
2022-01-19 14:43:00 +01:00
|
|
|
vprintk(fmt, ap);
|
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
2023-09-27 15:10:10 +02:00
|
|
|
EXPORT_SYMBOL(printk);
|
2022-01-19 14:43:00 +01:00
|
|
|
#endif /* defined(CONFIG_PRINTK) */
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2022-06-24 00:08:17 +02:00
|
|
|
#ifndef CONFIG_PICOLIBC
|
|
|
|
|
2016-11-28 09:31:18 +01:00
|
|
|
struct str_context {
|
|
|
|
char *str;
|
|
|
|
int max;
|
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int str_out(int c, struct str_context *ctx)
|
|
|
|
{
|
2019-03-14 22:38:13 +01:00
|
|
|
if (ctx->str == NULL || ctx->count >= ctx->max) {
|
2016-11-28 09:31:18 +01:00
|
|
|
ctx->count++;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->count == ctx->max - 1) {
|
|
|
|
ctx->str[ctx->count++] = '\0';
|
|
|
|
} else {
|
|
|
|
ctx->str[ctx->count++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
int snprintk(char *str, size_t size, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2019-06-29 00:44:38 +02:00
|
|
|
int ret;
|
2016-11-28 09:31:18 +01:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2019-06-29 00:44:38 +02:00
|
|
|
ret = vsnprintk(str, size, fmt, ap);
|
2016-11-28 09:31:18 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
2019-06-29 00:44:38 +02:00
|
|
|
return ret;
|
2016-11-28 09:31:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int vsnprintk(char *str, size_t size, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
struct str_context ctx = { str, size, 0 };
|
|
|
|
|
2024-08-08 18:11:27 +02:00
|
|
|
cbvprintf((cbprintf_cb)str_out, &ctx, fmt, ap);
|
2016-11-28 09:31:18 +01:00
|
|
|
|
|
|
|
if (ctx.count < ctx.max) {
|
|
|
|
str[ctx.count] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.count;
|
|
|
|
}
|
2022-06-24 00:08:17 +02:00
|
|
|
|
|
|
|
#endif
|