diff --git a/.github/workflows/codestyle.yml b/.github/workflows/codestyle.yml index fefa263e04a5..62251271221c 100644 --- a/.github/workflows/codestyle.yml +++ b/.github/workflows/codestyle.yml @@ -38,3 +38,14 @@ jobs: run: yamllint -f parsable -d "$yamllint_config" --strict .github/workflows/*.yml *.yml + + kernel-includes: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + filter: 'tree:0' + + - name: no kernel-only headers in user-space code + # see scripts/check_kernel_includes.py + run: python3 scripts/check_kernel_includes.py --sources --strict diff --git a/posix/include/rtos/panic.h b/posix/include/rtos/panic.h index ca0578c51fa9..4ae6a716a05c 100644 --- a/posix/include/rtos/panic.h +++ b/posix/include/rtos/panic.h @@ -11,7 +11,6 @@ #include #include -#include #ifdef __ZEPHYR__ #error "This file should only be included in XTOS builds." @@ -30,9 +29,6 @@ "Add it to CMake's target with sof_append_relative_path_definitions." #endif -void dump_panicinfo(void *addr, struct sof_ipc_panic_info *panic_info); -void panic_dump(uint32_t p, struct sof_ipc_panic_info *panic_info, - uintptr_t *data) SOF_NORETURN; void __panic(uint32_t p, const char *filename, uint32_t linenum) SOF_NORETURN; /** panic dump filename and linenumber of the call diff --git a/scripts/check_kernel_includes.py b/scripts/check_kernel_includes.py index d4e9a639043e..16eb9e22050b 100755 --- a/scripts/check_kernel_includes.py +++ b/scripts/check_kernel_includes.py @@ -24,13 +24,20 @@ checked builds are reported, so that they can be dropped from the list once they are clean in every configuration. +With --sources it checks the source tree instead of build directories: +kernel-only headers in src/kernel/include are included as +, and no user-space source or header may include one. +This covers code that no checked build compiles, and has no baseline. + Usage: check_kernel_includes.py [--strict] [--summary] [...] + check_kernel_includes.py --sources [--strict] """ import argparse import fnmatch import os +import re import subprocess import sys from collections import defaultdict @@ -122,9 +129,52 @@ def check_build(build_dir, manifest, sof_root, zephyr_base): return leaks, built +KERNEL_INCLUDE = re.compile(r'^\s*#\s*include\s*[<"](sof/kernel/[^>"]+)[>"]', re.M) + + +def source_files(sof_root): + """Return the files of the SOF source tree, relative to it.""" + try: + out = subprocess.run(['git', '-C', sof_root, 'ls-files'], + check=True, capture_output=True, text=True).stdout + return out.splitlines() + except (OSError, subprocess.CalledProcessError): + files = [] + for top, dirs, names in os.walk(sof_root): + dirs[:] = [d for d in dirs if d != '.git' and not d.startswith('build')] + files += [os.path.relpath(os.path.join(top, n), sof_root).replace(os.sep, '/') + for n in names] + return files + + +def check_sources(manifest, sof_root): + """Return {user-space file: [kernel header includes]} from the source tree.""" + user_src = manifest['user-sources'] + not_user = manifest['not-user-sources'] + user_hdr = manifest['user-headers'] + kernel_hdr = manifest['kernel-headers'] + + found = {} + for rel in source_files(sof_root): + if rel.endswith(SOURCE_EXTS): + if not matches(rel, user_src) or matches(rel, not_user): + continue + elif not (rel.endswith(('.h', '.hpp')) and matches(rel, user_hdr)) or \ + matches(rel, not_user) or matches(rel, kernel_hdr): + continue + path = os.path.join(sof_root, rel) + if not os.path.isfile(path): + continue + with open(path, encoding='utf-8', errors='replace') as f: + incs = KERNEL_INCLUDE.findall(f.read()) + if incs: + found[rel] = incs + return found + + def main(): parser = argparse.ArgumentParser(description=__doc__.split('\n', 1)[0]) - parser.add_argument('build_dirs', nargs='+', metavar='build-dir') + parser.add_argument('build_dirs', nargs='*', metavar='build-dir') parser.add_argument('--manifest', default=DEFAULT_MANIFEST) parser.add_argument('--sof-root', default=SOF_ROOT, help='SOF source directory the build directories were built from') @@ -135,9 +185,25 @@ def main(): 'includes a kernel-only header') parser.add_argument('--summary', action='store_true', help='only print per-header counts, not every source file') + parser.add_argument('--sources', action='store_true', + help='check the source tree for includes from ' + 'user-space code instead of build directories') args = parser.parse_args() manifest = parse_manifest(args.manifest) + + if args.sources: + if args.build_dirs: + parser.error('--sources cannot be combined with build directories') + found = check_sources(manifest, args.sof_root) + for src in sorted(found): + print(f'{src}:') + for inc in found[src]: + print(f'\t{inc}') + print(f'{len(found)} user-space files include headers from src/kernel/include') + return 1 if args.strict and found else 0 + if not args.build_dirs: + parser.error('build directories or --sources are required') zephyr_base = args.zephyr_base if os.path.isdir(args.zephyr_base) else None leaks = defaultdict(set) diff --git a/scripts/kernel_user_partition.txt b/scripts/kernel_user_partition.txt index e857a20acc21..b8246ad9d479 100644 --- a/scripts/kernel_user_partition.txt +++ b/scripts/kernel_user_partition.txt @@ -19,11 +19,23 @@ [user-sources] src/* -# Kernel sources inside src/: src/kernel/ and the legacy drivers. +# Kernel sources inside src/: src/kernel/, and the legacy drivers and LL +# scheduling domains, which go with the legacy drivers. [not-user-sources] src/kernel/* src/audio/dai-legacy.c src/audio/host-legacy.c +src/schedule/dma_multi_chan_domain.c +src/schedule/dma_single_chan_domain.c +src/schedule/ll_schedule_xtos.c +src/schedule/zephyr_dma_domain.c + +# Headers that user-space code may include, checked with --sources: they +# must not include headers from src/kernel/include. The kernel sources +# above and the [kernel-headers] below are excluded. +[user-headers] +src/* +zephyr/include/* # SOF headers that only kernel code may include. # Not listed on purpose: rtos/init.h, which only provides the diff --git a/src/debug/panic.c b/src/debug/panic.c deleted file mode 100644 index 6ec6ea4eafb6..000000000000 --- a/src/debug/panic.c +++ /dev/null @@ -1,150 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Liam Girdwood - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -void dump_panicinfo(void *addr, struct sof_ipc_panic_info *panic_info) -{ - int ret; - - if (!panic_info) - return; - ret = memcpy_s(addr, sizeof(struct sof_ipc_panic_info), panic_info, - sizeof(struct sof_ipc_panic_info)); - /* TODO are asserts even safe in this context? */ - assert(!ret); - dcache_writeback_region(addr, sizeof(struct sof_ipc_panic_info)); -} - -/** Dumps stack as part of panic. - * - * \return SOF_IPC_PANIC_STACK if offset is off the stack_limit, - * unchanged 'p' panic code input otherwise. - */ -static uint32_t dump_stack(uint32_t p, void *addr, size_t offset, - size_t limit, uintptr_t *stack_ptr) -{ - uintptr_t stack_limit = (uintptr_t)arch_get_stack_entry(); - uintptr_t stack_bottom = stack_limit + arch_get_stack_size() - - sizeof(void *); - uintptr_t stack_top = (uintptr_t)arch_get_stack_ptr() + offset; - size_t size = stack_bottom - stack_top; - int ret; - - *stack_ptr = stack_top; - - /* is stack smashed ? */ - if (stack_top - offset <= stack_limit) { - p = SOF_IPC_PANIC_STACK; - return p; - } - - /* make sure stack size won't overflow dump area */ - if (size > limit) - size = limit; - - /* copy stack contents and writeback */ - ret = memcpy_s(addr, limit, (void *)stack_top, size - sizeof(void *)); - assert(!ret); - dcache_writeback_region(addr, size - sizeof(void *)); - - return p; -} - -/** Copy registers, panic_info and current stack to mailbox exception - * window. Opaque 'data' (e.g.: optional epc1) is passed to - * arch_dump_regs(). - */ -void panic_dump(uint32_t p, struct sof_ipc_panic_info *panic_info, - uintptr_t *data) -{ - char *ext_offset; - size_t avail; - uintptr_t stack_ptr; - - /* disable all IRQs */ - interrupt_global_disable(); - - /* ARCH_OOPS_SIZE is platform-dependent */ - ext_offset = (char *)mailbox_get_exception_base() + ARCH_OOPS_SIZE; - - /* dump panic info, filename and linenum */ - dump_panicinfo(ext_offset, panic_info); - ext_offset += sizeof(struct sof_ipc_panic_info); - - /* Dump stack frames and override panic code 'p' if ext_offset is - * off stack_limit. Find stack_ptr. - */ - avail = MAILBOX_EXCEPTION_SIZE - - (size_t)(ext_offset - (char *)mailbox_get_exception_base()); - p = dump_stack(p, ext_offset, 0, avail, &stack_ptr); - - /* Write oops.arch_hdr and oops.plat_hdr headers and dump DSP core - * registers. After arch_dump_regs() use only inline funcs if - * needed. - */ - arch_dump_regs((void *)mailbox_get_exception_base(), stack_ptr, data); - - /* panic - send IPC oops message to host */ - platform_panic(p); - - /* and loop forever */ - while (1) - ; -} - -void __panic(uint32_t panic_code, const char *filename, uint32_t linenum) -{ - struct sof_ipc_panic_info panicinfo = { .linenum = linenum }; - const unsigned int length_max = sizeof(panicinfo.filename); - int mem_len; - int ret; - - /* including the ending '\0' */ - mem_len = rstrlen(filename) + 1; - - if (mem_len > length_max) { - /* copy those last bytes only */ - ret = memcpy_s(panicinfo.filename, - length_max, - filename + mem_len - length_max, - length_max); - /* TODO are asserts safe in this context? */ - assert(!ret); - - /* prefixing with "..." */ - ret = memcpy_s(panicinfo.filename, length_max, "...", 3); - assert(!ret); - } else { - ret = memcpy_s(panicinfo.filename, length_max, filename, mem_len); - assert(!ret); - } - - /* To distinguish regular panic() calls from exceptions, we will - * set a reserved value for the exception cause (63) so the - * coredumper tool could distinguish between the situations. - */ -#if !__clang_analyzer__ - __asm__ __volatile__("movi a3, 63\n\t" - "wsr.exccause a3\n\t" - "esync" : : : - "a3", "memory"); -#endif - - panic_dump(panic_code, &panicinfo, NULL); -} diff --git a/src/include/sof/debug/backtrace.h b/src/include/sof/debug/backtrace.h deleted file mode 100644 index d0f67161c3fb..000000000000 --- a/src/include/sof/debug/backtrace.h +++ /dev/null @@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * - * Copyright(c) 2020 Intel Corporation. All rights reserved. - */ - -#ifndef __SOF_DEBUG_BACKTRACE_H__ -#define __SOF_DEBUG_BACKTRACE_H__ - -#include - -#endif diff --git a/src/kernel/README.md b/src/kernel/README.md index 8e44f25d8462..f5ad04f9daa6 100644 --- a/src/kernel/README.md +++ b/src/kernel/README.md @@ -30,6 +30,9 @@ Kernel-only headers are in [`include/sof/kernel`](include/sof/kernel) and are in ``. The directory is on the include path of the firmware, the host library and the testbench, but not of LLEXT modules. Kernel-only headers that the host builds replace with their own versions, such as the `rtos/` headers, have not moved yet. +User-space code must not include anything from here: the `codestyle` CI workflow runs +`scripts/check_kernel_includes.py --sources --strict`, which fails when a user-space source or +header includes ``. Code here may use kernel-only headers. User-space code must not call into it directly. The checker in `scripts/check_kernel_includes.py` reports kernel-only includes from user-space diff --git a/src/kernel/arch/host/include/arch/debug/backtrace.h b/src/kernel/arch/host/include/arch/debug/backtrace.h deleted file mode 100644 index e9abe3de0471..000000000000 --- a/src/kernel/arch/host/include/arch/debug/backtrace.h +++ /dev/null @@ -1,45 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * - * Copyright(c) 2020 Intel Corporation. All rights reserved. - */ - -#ifdef __SOF_DEBUG_BACKTRACE_H__ - -#ifndef __ARCH_DEBUG_BACKTRACE_H__ -#define __ARCH_DEBUG_BACKTRACE_H__ - -#include -#include -#include -#include - -/* architecture specific stack frames to dump */ -#define ARCH_STACK_DUMP_FRAMES 32 - -static inline void *arch_get_stack_ptr(void) -{ - void *frames[ARCH_STACK_DUMP_FRAMES]; - size_t frame_count; - size_t i; - char **symbols; - - frame_count = backtrace(frames, ARCH_STACK_DUMP_FRAMES); - symbols = backtrace_symbols(frames, frame_count); - - fprintf(stderr, "Dumping %zd stack frames.\n", frame_count); - - for (i = 0; i < frame_count; i++) - fprintf(stderr, "\t%s\n", symbols[i]); - - free(symbols); - - return NULL; -} - -#endif /* __ARCH_DEBUG_BACKTRACE_H__ */ - -#else - -#error "This file shouldn't be included from outside of sof/debug/backtrace.h" - -#endif /* __SOF_DEBUG_BACKTRACE_H__ */ diff --git a/src/kernel/arch/host/include/arch/debug/panic.h b/src/kernel/arch/host/include/arch/debug/panic.h deleted file mode 100644 index 85f47a5ecbbc..000000000000 --- a/src/kernel/arch/host/include/arch/debug/panic.h +++ /dev/null @@ -1,24 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * - * Copyright(c) 2019 Intel Corporation. All rights reserved. - * - * Author: Tomasz Lauda - */ - -#ifdef __POSIX_RTOS_PANIC_H__ - -#ifndef __ARCH_DEBUG_PANIC_H__ -#define __ARCH_DEBUG_PANIC_H__ - -#include - -static inline void arch_dump_regs(void *dump_buf, uintptr_t stack_ptr, - uintptr_t *epc1) { } - -#endif /* __ARCH_DEBUG_PANIC_H__ */ - -#else - -#error "This file shouldn't be included from outside of XTOS's rtos/panic.h" - -#endif /* __POSIX_RTOS_PANIC_H__ */