123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- #include <kernel.h>
- #include <sys/printk.h>
- #include <stdarg.h>
- #include <toolchain.h>
- #include <linker/sections.h>
- #include <syscall_handler.h>
- #include <logging/log.h>
- #include <sys/cbprintf.h>
- #include <sys/types.h>
- #include <soc.h>
- #if defined(CONFIG_PRINTK_SYNC) && \
- !(defined(CONFIG_LOG_PRINTK) && defined(CONFIG_LOG2))
- static struct k_spinlock lock;
- #endif
- #ifdef CONFIG_PRINTK
- __attribute__((weak)) int arch_printk_char_out(int c)
- {
- ARG_UNUSED(c);
-
- return 0;
- }
- int (*_char_out)(int) = arch_printk_char_out;
- void __printk_hook_install(int (*fn)(int))
- {
- _char_out = fn;
- }
- void *__printk_get_hook(void)
- {
- return _char_out;
- }
- #endif
- #if defined(CONFIG_PRINTK) && \
- !(defined(CONFIG_LOG_PRINTK) && defined(CONFIG_LOG2))
- #ifdef CONFIG_USERSPACE
- struct buf_out_context {
- int count;
- unsigned int buf_count;
- char buf[CONFIG_PRINTK_BUFFER_SIZE];
- };
- static void buf_flush(struct buf_out_context *ctx)
- {
- k_str_out(ctx->buf, ctx->buf_count);
- ctx->buf_count = 0U;
- }
- static int buf_char_out(int c, void *ctx_p)
- {
- struct buf_out_context *ctx = ctx_p;
- ctx->count++;
- ctx->buf[ctx->buf_count++] = c;
- if (ctx->buf_count == CONFIG_PRINTK_BUFFER_SIZE) {
- buf_flush(ctx);
- }
- return c;
- }
- #endif
- struct out_context {
- int count;
- };
- static int char_out(int c, void *ctx_p)
- {
- struct out_context *ctx = ctx_p;
- ctx->count++;
- return _char_out(c);
- }
- #ifdef CONFIG_USERSPACE
- void vprintk(const char *fmt, va_list ap)
- {
- if (k_is_user_context()) {
- struct buf_out_context ctx = { 0 };
- cbvprintf(buf_char_out, &ctx, fmt, ap);
- if (ctx.buf_count) {
- buf_flush(&ctx);
- }
- } else {
- struct out_context ctx = { 0 };
- #ifdef CONFIG_PRINTK_SYNC
- k_spinlock_key_t key = k_spin_lock(&lock);
- #endif
- cbvprintf(char_out, &ctx, fmt, ap);
- #ifdef CONFIG_PRINTK_SYNC
- k_spin_unlock(&lock, key);
- #endif
- }
- }
- #else
- #ifdef CONFIG_ACTIONS_PRINTK_DMA
- void __vprintk(const char *fmt, va_list ap)
- #else
- void vprintk(const char *fmt, va_list ap)
- #endif
- {
- struct out_context ctx = { 0 };
- #ifdef CONFIG_PRINTK_SYNC
- k_spinlock_key_t key = k_spin_lock(&lock);
- #endif
- if (soc_in_sleep_mode()) {
- sl_vprintk(fmt, ap);
- } else {
- cbvprintf(char_out, &ctx, fmt, ap);
- }
- #ifdef CONFIG_PRINTK_SYNC
- k_spin_unlock(&lock, key);
- #endif
- }
- #endif
- void z_impl_k_str_out(char *c, size_t n)
- {
- size_t i;
- #ifdef CONFIG_PRINTK_SYNC
- k_spinlock_key_t key = k_spin_lock(&lock);
- #endif
- for (i = 0; i < n; i++) {
- _char_out(c[i]);
- }
- #ifdef CONFIG_PRINTK_SYNC
- k_spin_unlock(&lock, key);
- #endif
- }
- #ifdef CONFIG_USERSPACE
- static inline void z_vrfy_k_str_out(char *c, size_t n)
- {
- Z_OOPS(Z_SYSCALL_MEMORY_READ(c, n));
- z_impl_k_str_out((char *)c, n);
- }
- #include <syscalls/k_str_out_mrsh.c>
- #endif
- void printk(const char *fmt, ...)
- {
- va_list ap;
- va_start(ap, fmt);
- if (IS_ENABLED(CONFIG_LOG_PRINTK)) {
- log_printk(fmt, ap);
- } else {
- vprintk(fmt, ap);
- }
- va_end(ap);
- }
- #endif
- struct str_context {
- char *str;
- int max;
- int count;
- };
- static int str_out(int c, struct str_context *ctx)
- {
- if (ctx->str == NULL || ctx->count >= ctx->max) {
- 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;
- int ret;
- va_start(ap, fmt);
- ret = vsnprintk(str, size, fmt, ap);
- va_end(ap);
- return ret;
- }
- int vsnprintk(char *str, size_t size, const char *fmt, va_list ap)
- {
- struct str_context ctx = { str, size, 0 };
- cbvprintf(str_out, &ctx, fmt, ap);
- if (ctx.count < ctx.max) {
- str[ctx.count] = '\0';
- }
- return ctx.count;
- }
|