Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/debug/debug_stream/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# SPDX-License-Identifier: BSD-3-Clause

add_local_sources_ifdef(CONFIG_SOF_DEBUG_STREAM_SLOT sof debug_stream_slot.c)

add_local_sources_ifdef(CONFIG_SOF_DEBUG_STREAM_THREAD_INFO sof debug_stream_thread_info.c)

add_local_sources_ifdef(CONFIG_SOF_DEBUG_STREAM_TEXT_MSG sof debug_stream_text_msg.c)
8 changes: 8 additions & 0 deletions src/debug/debug_stream/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# SOF Debug Stream

> **Runs in:** Boundary / bridge — the debug stream clients here (`ds_msg()` and `ds_vamsg()`
> in `debug_stream_text_msg.c`) can run in user threads and send their records through the
> `debug_stream_slot_send_record()` syscall. The kernel parts are in
> `src/kernel/debug/debug_stream`: the debug window slot with both halves of the syscall
> (`debug_stream_slot.c`), the thread info client (`debug_stream_thread_info.c`) and the
> exception dump hook with `assert_print()` (`debug_stream_text_msg-kernel.c`). See
> [User/kernel split](../../include/sof/userspace/README.md).

The `debug_stream` framework is an abstract logging and live-data streaming mechanism allowing the DSP to asynchronously push structured or freeform diagnostic records immediately out to the host.

## Feature Overview
Expand Down
144 changes: 5 additions & 139 deletions src/debug/debug_stream/debug_stream_text_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
#include <soc.h>
#include <adsp_debug_window.h>
#include <sof/common.h>
#include <rtos/symbol.h>
#include <zephyr/logging/log.h>
#include <zephyr/arch/exception.h>

#include <user/debug_stream_text_msg.h>

LOG_MODULE_REGISTER(debug_stream_text_msg);

/* The exception dump hook and assert_print(), which run in kernel mode, are in
* src/kernel/debug/debug_stream/debug_stream_text_msg-kernel.c
*/

void ds_vamsg(const char *format, va_list args)
{
struct {
Expand Down Expand Up @@ -44,141 +48,3 @@ void ds_msg(const char *format, ...)
va_end(args);
}
EXPORT_SYMBOL(ds_msg);

#if defined(CONFIG_EXCEPTION_DUMP_HOOK)
/* The debug stream debug window slot is 4k, and when it is split
* between the cores and the header/other overhead is removed, with 5
* cores the size is 768 bytes. The dump record must be smaller than
* that to get through to the host side.
*
* Also, because of the limited circular buffer size, we should only
* send one exception record. On some situations the exceptions happen
* in bursts, and sending more than one record in short time makes the
* host-side decoder lose track of things.
*/

/* Per-CPU state for exception dump and assert_print(). Static data is
* currently placed in .bss and its ATM uncached so the ds_cpu table
* elements do not need to be cache aligned, but if this changes we
* need __aligned(CONFIG_DCACHE_LINE_SIZE) here.
*/
static struct ds_cpu_state {
struct {
struct debug_stream_text_msg msg;
char text[640];
} __packed buf;
int reports_sent;
size_t pos;
} ds_cpu[CONFIG_SOF_DEBUG_STREAM_SLOT_FORCE_MAX_CPUS];

static void ds_exception_drain(bool flush)
{
unsigned int cpu = arch_proc_id();
struct ds_cpu_state *cs = &ds_cpu[cpu];

if (flush) {
cs->pos = 0;
return;
}

if (cs->pos == 0)
return;

if (cs->reports_sent > 0)
return;

cs->buf.msg.hdr.id = DEBUG_STREAM_RECORD_ID_TEXT_MSG;
cs->buf.msg.hdr.size_words =
SOF_DIV_ROUND_UP(sizeof(cs->buf.msg) + cs->pos,
sizeof(cs->buf.msg.hdr.data[0]));

/* Make sure the possible up to 3 extra bytes at end of msg are '\0' */
memset(cs->buf.text + cs->pos, 0,
cs->buf.msg.hdr.size_words * sizeof(cs->buf.msg.hdr.data[0]) - cs->pos);
debug_stream_slot_send_record(&cs->buf.msg.hdr);
cs->reports_sent = 1;
cs->pos = 0;
}

static void ds_exception_dump(const char *format, va_list args)
{
ssize_t len;
size_t avail;
size_t written;
unsigned int cpu = arch_proc_id();
struct ds_cpu_state *cs = &ds_cpu[cpu];

if (cs->reports_sent > 0)
return;

avail = sizeof(cs->buf.text) - cs->pos;
if (avail == 0) {
ds_exception_drain(false);
return;
}

if (format[0] == '\0')
return;

/* Skip useless " ** " prefix to save bytes */
if (strlen(format) >= 4 &&
format[0] == ' ' && format[1] == '*' && format[2] == '*' && format[3] == ' ')
format += 4;

len = vsnprintf(cs->buf.text + cs->pos, avail, format, args);
if (len < 0) {
cs->pos = 0;
return;
}

if (len == 0)
return;

if ((size_t)len >= avail)
written = avail - 1;
else
written = (size_t)len;

cs->pos += written;

if (cs->pos >= sizeof(cs->buf.text))
ds_exception_drain(false);
}

static int init_exception_dump_hook(void)
{
arch_exception_set_dump_hook(ds_exception_dump, ds_exception_drain);
LOG_DBG("exception_dump_hook set");
return 0;
}

SYS_INIT(init_exception_dump_hook, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);

#if defined(CONFIG_SOF_DEBUG_STREAM_TEXT_MSG_ASSERT_PRINT)
void assert_print(const char *fmt, ...)
{
va_list ap;

/* Do not print assert after exception has been dumped */
if (ds_cpu[arch_proc_id()].reports_sent > 0)
return;

va_start(ap, fmt);
#if !defined(CONFIG_EXCEPTION_DUMP_HOOK_ONLY)
{
va_list ap2;

va_copy(ap2, ap);
#endif
ds_vamsg(fmt, ap);
#if !defined(CONFIG_EXCEPTION_DUMP_HOOK_ONLY)
vprintk(fmt, ap2);
va_end(ap2);
}
#endif
ds_vamsg(fmt, ap);
va_end(ap);
}
EXPORT_SYMBOL(assert_print);
#endif
#endif
6 changes: 2 additions & 4 deletions src/include/sof/userspace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@ its subdirectories span all tiers.
| Tier | Meaning | Directories |
|------|---------|-------------|
| **Kernel-only** | OS integration, HAL, boot, inter-core. Never executes in a user thread. Must not be reached directly from user code — only via a syscall. | `kernel/` (see [its README](../../../kernel/README.md)) |
| **Boundary / bridge** | Owns the syscalls and the thread / memory-domain setup. Split internally: some translation units run kernel-side, some user-side (e.g. `handler-kernel.c` vs `handler-user.c`). | `schedule/`, `ipc/`, `debug/debug_stream/` *, and (in the Zephyr integration tree) `zephyr/syscall/` |
| **Boundary / bridge** | Owns the syscalls and the thread / memory-domain setup. Split internally: some translation units run kernel-side, some user-side (e.g. `handler-kernel.c` vs `handler-user.c`). | `schedule/`, `ipc/`, `debug/debug_stream/`, and (in the Zephyr integration tree) `zephyr/syscall/` |
| **Application** | Audio pipeline / component logic that runs in user threads under userspace configs. | `audio/` |
| **Shared library** | Context-agnostic code linked into whichever context calls it. Must stay "user-safe": no privileged operations (no direct MMIO, no spinlocks held across syscalls, no raw kernel object access) so it is safe when pulled into a user build. | `math/`, `module/` |
| **Mixed** | A directory whose files span more than one tier; see its own README for per-file classification. | `lib/` |

Directories marked * do not yet have a "Runs in:" README banner.

Kernel-only code lives under `src/kernel/`, which Zephyr builds into a library of its own,
`sof_kernel`, instead of `modules_sof`. Source files that mix kernel and user code are split,
and their kernel halves moved there, separately.
Expand Down Expand Up @@ -125,7 +123,7 @@ syscall by subsystem:
| Module management | `mod_balloc_align`, `mod_alloc_ext`, `mod_free`, `mod_free_all`, `mod_data_blob_handler_new`, `mod_data_blob_set`, `mod_fast_get` | `sof/audio/module_adapter/module/generic.h` | scattered: `audio/module_adapter/module/generic.c` |
| Library manager | `lib_manager_free_module` | `sof/lib_manager.h` | scattered: `kernel/library_manager/lib_manager.c` |
| Scheduling | `zephyr_ll_task_sem_alloc`, `zephyr_ll_task_sem_free`, `scheduler_dp_internal_free`, `scheduler_dp_ll_tick` | `sof/schedule/ll_schedule_domain.h`, `sof/schedule/dp_schedule.h` | scattered: `kernel/schedule/zephyr_ll-kernel.c`, `kernel/schedule/zephyr_dp_schedule*-kernel.c` |
| Debug | `debug_stream_slot_send_record` | `user/debug_stream_slot.h` | scattered: `debug/debug_stream/debug_stream_slot.c` |
| Debug | `debug_stream_slot_send_record` | `user/debug_stream_slot.h` | `kernel/debug/debug_stream/debug_stream_slot.c` |
| KPB | `kpb_notifier_init` | `sof/audio/kpb.h` | scattered: `audio/kpb.c` |

### Allowed callers
Expand Down
1 change: 1 addition & 0 deletions src/kernel/debug/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
is_zephyr(zephyr)
if(zephyr) ### Zephyr ###

add_subdirectory(debug_stream)
add_subdirectory(telemetry)

zephyr_library_sources_ifdef(CONFIG_COLD_STORE_EXECUTE_DEBUG dram.c)
Expand Down
8 changes: 8 additions & 0 deletions src/kernel/debug/debug_stream/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: BSD-3-Clause

add_local_sources_ifdef(CONFIG_SOF_DEBUG_STREAM_SLOT sof debug_stream_slot.c)

add_local_sources_ifdef(CONFIG_SOF_DEBUG_STREAM_THREAD_INFO sof debug_stream_thread_info.c)

# The kernel part of src/debug/debug_stream/debug_stream_text_msg.c, built with it
add_local_sources_ifdef(CONFIG_SOF_DEBUG_STREAM_TEXT_MSG sof debug_stream_text_msg-kernel.c)
160 changes: 160 additions & 0 deletions src/kernel/debug/debug_stream/debug_stream_text_msg-kernel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2024 Intel Corporation.

#include <sys/types.h>
#include <stdarg.h>
#include <stdio.h>
#include <soc.h>
#include <adsp_debug_window.h>
#include <sof/common.h>
#include <rtos/symbol.h>
#include <zephyr/logging/log.h>
#include <zephyr/arch/exception.h>

#include <user/debug_stream_text_msg.h>

LOG_MODULE_DECLARE(debug_stream_text_msg);

/* Kernel part of the debug stream text messages: the exception dump hook, which
* runs in the fatal error handler, and assert_print(), which shares its per-CPU
* state. ds_msg() and ds_vamsg() are in src/debug/debug_stream/debug_stream_text_msg.c
*/

#if defined(CONFIG_EXCEPTION_DUMP_HOOK)
/* The debug stream debug window slot is 4k, and when it is split
* between the cores and the header/other overhead is removed, with 5
* cores the size is 768 bytes. The dump record must be smaller than
* that to get through to the host side.
*
* Also, because of the limited circular buffer size, we should only
* send one exception record. On some situations the exceptions happen
* in bursts, and sending more than one record in short time makes the
* host-side decoder lose track of things.
*/

/* Per-CPU state for exception dump and assert_print(). Static data is
* currently placed in .bss and its ATM uncached so the ds_cpu table
* elements do not need to be cache aligned, but if this changes we
* need __aligned(CONFIG_DCACHE_LINE_SIZE) here.
*/
static struct ds_cpu_state {
struct {
struct debug_stream_text_msg msg;
char text[640];
} __packed buf;
int reports_sent;
size_t pos;
} ds_cpu[CONFIG_SOF_DEBUG_STREAM_SLOT_FORCE_MAX_CPUS];

static void ds_exception_drain(bool flush)
{
unsigned int cpu = arch_proc_id();
struct ds_cpu_state *cs = &ds_cpu[cpu];

if (flush) {
cs->pos = 0;
return;
}

if (cs->pos == 0)
return;

if (cs->reports_sent > 0)
return;

cs->buf.msg.hdr.id = DEBUG_STREAM_RECORD_ID_TEXT_MSG;
cs->buf.msg.hdr.size_words =
SOF_DIV_ROUND_UP(sizeof(cs->buf.msg) + cs->pos,
sizeof(cs->buf.msg.hdr.data[0]));

/* Make sure the possible up to 3 extra bytes at end of msg are '\0' */
memset(cs->buf.text + cs->pos, 0,
cs->buf.msg.hdr.size_words * sizeof(cs->buf.msg.hdr.data[0]) - cs->pos);
debug_stream_slot_send_record(&cs->buf.msg.hdr);
cs->reports_sent = 1;
cs->pos = 0;
}

static void ds_exception_dump(const char *format, va_list args)
{
ssize_t len;
size_t avail;
size_t written;
unsigned int cpu = arch_proc_id();
struct ds_cpu_state *cs = &ds_cpu[cpu];

if (cs->reports_sent > 0)
return;

avail = sizeof(cs->buf.text) - cs->pos;
if (avail == 0) {
ds_exception_drain(false);
return;
}

if (format[0] == '\0')
return;

/* Skip useless " ** " prefix to save bytes */
if (strlen(format) >= 4 &&
format[0] == ' ' && format[1] == '*' && format[2] == '*' && format[3] == ' ')
format += 4;

len = vsnprintf(cs->buf.text + cs->pos, avail, format, args);
if (len < 0) {
cs->pos = 0;
return;
}

if (len == 0)
return;

if ((size_t)len >= avail)
written = avail - 1;
else
written = (size_t)len;

cs->pos += written;

if (cs->pos >= sizeof(cs->buf.text))
ds_exception_drain(false);
}

static int init_exception_dump_hook(void)
{
arch_exception_set_dump_hook(ds_exception_dump, ds_exception_drain);
LOG_DBG("exception_dump_hook set");
return 0;
}

SYS_INIT(init_exception_dump_hook, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);

#if defined(CONFIG_SOF_DEBUG_STREAM_TEXT_MSG_ASSERT_PRINT)
void assert_print(const char *fmt, ...)
{
va_list ap;

/* Do not print assert after exception has been dumped */
if (ds_cpu[arch_proc_id()].reports_sent > 0)
return;

va_start(ap, fmt);
#if !defined(CONFIG_EXCEPTION_DUMP_HOOK_ONLY)
{
va_list ap2;

va_copy(ap2, ap);
#endif
ds_vamsg(fmt, ap);
#if !defined(CONFIG_EXCEPTION_DUMP_HOOK_ONLY)
vprintk(fmt, ap2);
va_end(ap2);
}
#endif
ds_vamsg(fmt, ap);
va_end(ap);
}
EXPORT_SYMBOL(assert_print);
#endif
#endif
Loading