smokey: Add traps test for trap routing of primary mode threads
RFC PATCH 1/1 smokey: Add traps test for trap routing of primary mode threads
From: Jan Kiszka <[email protected]>
A thread running in primary mode which takes a synchronous exception must be reported to the Cobalt core by the Dovetail layer exactly once per trap, before the in-band kernel applies its fixup. Cobalt then relaxes the thread, accounts the trap and sends SIGDEBUG_MIGRATE_FAULT when asked for. Only the page fault flavor of this was covered so far, by the sigdebug test.
Add a test which triggers one specific trap per case from a primary mode thread and checks that the expected in-band fixup happened. Most traps for x86, arm, and arm64 are covered here. Left out are those that are practically irrelevant: AC for unaligned accesses on x86 and VFP exceptions on ARM. Not yet covered is RISC-V, but it should be easy to add on top.
See extensive AI generated explanation at the top of traps.c for more details.
This test may become useful for checking future OBB trap reporting by extending it only slightly.
---
I know this is huge. Therefore, it is also something that we likely would not have done without LLM support - way too much effort for testing corner cases.
I went through it, but not in all details. The tests fail when they are expected to on unfixed kernels, they apparently pass on fixed one, at least on our CI hardware. "Should be OK"...
BTW, if we merge this, we implicitly document parts of our so far unwritten AI policy ("yes, we take generated code"). I still need to think about which of the templates out there to use best, then I will propose one.
configure.ac | 1 + testsuite/smokey/Makefile.am | 2 + testsuite/smokey/traps/Makefile.am | 8 + testsuite/smokey/traps/traps.c | 2259 ++++++++++++++++++++++++++++ 4 files changed, 2270 insertions(+) create mode 100644 testsuite/smokey/traps/Makefile.am create mode 100644 testsuite/smokey/traps/traps.c
configure.ac +1 -0
@@ -1086,6 +1086,7 @@ AC_CONFIG_FILES([ \ testsuite/smokey/bufp/Makefile \ testsuite/smokey/sigdebug/Makefile \ testsuite/smokey/timerfd/Makefile \ + testsuite/smokey/traps/Makefile \ testsuite/smokey/tsc/Makefile \ testsuite/smokey/leaks/Makefile \ testsuite/smokey/memcheck/Makefile \
testsuite/smokey/Makefile.am +2 -0
@@ -37,6 +37,7 @@ COBALT_SUBDIRS = \ setsched \ sigdebug \ timerfd \ + traps \ tsc \ xddp \ y2038 \
@@ -82,6 +83,7 @@ DIST_SUBDIRS = \ setsched \ sigdebug \ timerfd \ + traps \ tsc \ xddp \ y2038 \
testsuite/smokey/traps/Makefile.am new file +8 -0
@@ -0,0 +1,8 @@ + +noinst_LIBRARIES = libtraps.a + +libtraps_a_SOURCES = traps.c + +libtraps_a_CPPFLAGS = \ + @XENO_USER_CFLAGS@ \ + -I$(top_srcdir)/include
testsuite/smokey/traps/traps.c new file +2259 -0
2,259 lines not on this page · raw message
Signature · 1 line
2.47.3
Quoted 27 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
A thread running in primary mode which takes a synchronous exception must be reported to the Cobalt core by the Dovetail layer exactly once per trap, before the in-band kernel applies its fixup. Cobalt then relaxes the thread, accounts the trap and sends
SIGDEBUG_MIGRATE_FAULTwhen asked for. Only the page fault flavor of this was covered so far, by the sigdebug test.Add a test which triggers one specific trap per case from a primary mode thread and checks that the expected in-band fixup happened. Most traps for x86, arm, and arm64 are covered here. Left out are those that are practically irrelevant: AC for unaligned accesses on x86 and VFP exceptions on ARM. Not yet covered is RISC-V, but it should be easy to add on top.
See extensive AI generated explanation at the top of
traps.cfor more details.This test may become useful for checking future OBB trap reporting by extending it only slightly.
Assisted-by opencode:Fable-5.1/Opus-5.5 Signed-off-by: Jan Kiszka<[email protected]> ---
I know this is huge. Therefore, it is also something that we likely would not have done without LLM support - way too much effort for testing corner cases.
Quoted 8 lines
I went through it, but not in all details. The tests fail when they are expected to on unfixed kernels, they apparently pass on fixed one, at least on our CI hardware. "Should be OK"...
BTW, if we merge this, we implicitly document parts of our so far unwritten AI policy ("yes, we take generated code"). I still need to think about which of the templates out there to use best, then I will propose one.
Hi Jan,
Following your input, I spent some time last week on RISC-V trap handling in EVL. To verify the proposed patches, I ended up with a similar trap matrix test generated for EVL.
With the number of fixes we can now produce with AI support, I think it is right, and important, to put more time into test infrastructure. I think it makes sense to pin down in more detail how Dovetail and the co-kernels built on top are supposed to behave to make sure this is not changed by AI without us noticing when these tools are trusted more and more.
I am asking myself if we want to keep some kind of human readable source of truth that tells us how this is supposed to work. If we start generating both sides and both sides are hard to understand (at least for me, the kernel side still is) I feel like it will be hard to notice if the tools start to slowly change the design.
In addition, I feel like most of the tests are Dovetail functionality tests and not specific to cobalt/EVL. I think we should at least try to find a way to tackle this with joined forces for cobalt, EVL and what ever other co-kernel using the infrastructure.
I think that RV can maybe help here. It would allow us to decouple some of the checks from the triggering of the test cases. Triggering could be in the co-kernel projects while most of the verification lives in Dovetail.
I created a POC of a more detailed trap monitor paired with a matrix test that solely triggers the traps from the different contexts in cobalt here:
https://github.com/TobiasSchaffner/linux/commits/tobsch/trap-monitor-
rfc-proposal-nofixes/
Asking Opus 5.5 about a comparison to your proposed RFC it tells me:
``` Not covered: - SIGDEBUG with SIGDEBUG_MIGRATE_FAULT, exactly once. That's Cobalt's notification to the application, and we don't check it. - The counters in /proc/xenomai/faults: the right trap +1, all others unchanged. That's Cobalt's accounting. - The exact mode-switch count (Cobalt's msw statistics). On top of the RFC, it covers (checks no user-space test can see): - the handler level of the unwind, through the stack frame; - irqs off on trap exit; - demotion only from task context; - no IRQ in NMI context; - no sched-out during a kprobe; - the time limit. - more fault cases: read-null, write-ro, write-retry, exec-nx, bus-eof, fresh, fork-write, kernel-fault, kernel-fresh, misaligned. ```
The output makes sense cause the POC has no cobalt API specific checks anymore while the monitor adds more kernel internal checks.
The hole approach Monitor + Triggering matrix is smaller than your test while the monitor part could be reused. A rendering of the monitor is attached.
Maybe you want to consider it or spend some tokens for further analysis if this can be valuable.
Best,
Tobias
Quoted 2,319 lines
configure.ac | 1 + testsuite/smokey/Makefile.am | 2 + testsuite/smokey/traps/Makefile.am | 8 + testsuite/smokey/traps/traps.c | 2259 ++++++++++++++++++++++++++++ 4 files changed, 2270 insertions(+)create mode 100644 testsuite/smokey/traps/Makefile.am create mode 100644
testsuite/smokey/traps/traps.cconfigure.ac +1
-0@@ -1086,6 +1086,7 @@ AC_CONFIG_FILES([ \ testsuite/smokey/bufp/Makefile \ testsuite/smokey/sigdebug/Makefile \ testsuite/smokey/timerfd/Makefile \ + testsuite/smokey/traps/Makefile \ testsuite/smokey/tsc/Makefile \ testsuite/smokey/leaks/Makefile \ testsuite/smokey/memcheck/Makefile \testsuite/smokey/Makefile.am +2
-0@@ -37,6 +37,7 @@ COBALT_SUBDIRS = \ setsched \ sigdebug \ timerfd \ + traps \ tsc \ xddp \ y2038 \@@ -82,6 +83,7 @@ DIST_SUBDIRS = \ setsched \ sigdebug \ timerfd \ + traps \ tsc \ xddp \ y2038 \testsuite/smokey/traps/Makefile.am new file +8
-0@@ -0,0 +1,8 @@ + +noinst_LIBRARIES = libtraps.a + +libtraps_a_SOURCES = traps.c + +libtraps_a_CPPFLAGS = \ + @XENO_USER_CFLAGS@ \ + -I$(top_srcdir)/includetestsuite/smokey/traps/traps.c new file +2259
-0@@ -0,0 +1,2259 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Functional testing of trap routing for primary mode threads. +* +* Copyright (c) Siemens AG, 2026 +* +* A thread running in primary mode which takes a synchronous +* exception (page fault, breakpoint, undefined instruction, ...) must +* be reported to the Cobalt core by the Dovetail layer of the kernel +* exactly once per trap, before the in-band kernel applies its fixup +* (fault handling, signal injection, instruction emulation). Cobalt +* then relaxes the thread to secondary mode, accounts the fault in +* /proc/xenomai/faults and sends SIGDEBUG_MIGRATE_FAULT if the thread +* asked for mode switch warnings. +* +* Each test case below triggers one specific trap from primary mode +* and checks: +* +* - the fixup the in-band kernel is expected to apply did happen +* (signal with the right si_code, emulation result), +* - the thread was relaxed by the trap, i.e. it runs in secondary +* mode when it gets back to user space, +* - exactly one SIGDEBUG was received, with reason MIGRATE_FAULT, +* - the fault counter of the expected trap incremented by exactly +* one, all other trap counters stayed put, +* - the thread switched to secondary mode exactly once. +* +* Some cases trigger a nested trap on purpose: the in-band fixup of +* the outer trap accesses a user page which is not resident, so the +* kernel takes a page fault while handling the first trap. Only the +* outer trap must be reported. On the Cobalt side, this is observable +* as "no additional fault accounted, no additional mode switch, and +* the next case is still reported", since Cobalt does not act on trap +* exit notifications. Such fixups are the SWP and alignment emulations +* on arm, the vsyscall and UMIP emulations on x86. An arm64 kernel +* has them for compat tasks only (SWP, alignment), which the arm +* build of the test covers when run on an arm64 kernel. +* +* A trap which is not reported leaves the thread on the oob stage +* while the in-band kernel injects a signal; the thread keeps +* re-executing the faulting instruction until the Cobalt watchdog +* kicks it (the arm exception return path does not run the mayday +* trap). The test therefore requires the watchdog and shortens its +* timeout while running, so that a broken kernel fails instead of +* hanging. Such a failure is reported as "trap not reported to the +* core"; a failure of the trailing pagefault_again case indicates +* that an earlier case left a pending trap notification behind. +* +* The trap numbers the core accounts are those of the kernel +* architecture, which may differ from the test's: an arm (32-bit) +* build of the test on an arm64 kernel sees arm64 traps. The test +* therefore identifies traps by the labels the core shows in +* /proc/xenomai/faults, and knows which arm64 traps the compat +* flavors of its arm cases end up as. An arm64 kernel also offers no +* runtime control over misaligned accesses of compat tasks: they are +* either fixed up or signaled depending on COMPAT_ALIGNMENT_FIXUPS. +* The test probes this, and runs the matching alignment cases only. +* SWP emulation is switched on via abi.swp instead. +* +* Cases whose prerequisites are not met are skipped with a note. Note +* that emulators may not implement all traps the test relies on: +* QEMU/TCG for instance ignores SCTLR.SW and executes SWP natively on +* ARMv7 models, and does not check SP alignment on arm64. +* +* The fault handler runs in the context of the faulting instruction, +* and we leave it by longjmp: whatever that context had set up to +* trap stays in effect afterwards. The handler therefore runs on an +* alternate stack and restores a sane state first thing, before any +* library code is executed again. +* +* Case overview (cases=<name>[,<name>...] selects a subset): +* +* common pagefault, kprobe, pagefault_again, fork +* arm bkpt, undef, swp_valid, swp_fresh_page, swp_badaddr, +* align_fixup, align_fixup_fresh_page, align_signal +* arm64 brk, udf, sp_align +* x86_64 int3, ud2, div0, gp, simd_div0, vsyscall, +* vsyscall_fresh_page, vsyscall_badaddr, umip, +* umip_fresh_page, umip_badaddr +* +* The kprobe case places a kprobe on a current-mode Cobalt syscall +* handler and invokes it from primary mode, which traps from kernel +* mode on the oob stage. It needs root and KPROBE_EVENTS. +*/ + +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdbool.h> +#include <string.h> +#include <signal.h> +#include <ucontext.h> +#include <setjmp.h> +#include <fcntl.h> +#include <errno.h> +#include <sched.h> +#include <stdint.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/wait.h> +#include <sys/syscall.h> +#include <linux/perf_event.h> +#include <pthread.h> +#include <sys/cobalt.h> +#include <smokey/smokey.h> + +smokey_test_plugin (traps, +SMOKEY_ARGLIST ( + SMOKEY_STRING(cases), + ), +"Check trap routing of primary mode threads.\n" +"\tcases=<name>[,<name>...]\trun only the listed cases\n" +); + +#if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__) + +#define NR_TRAPS 32 +#define MAX_REASONS 8 +#define MAX_CASES 32 +#define WATCHDOG_TIMEOUT 2/* seconds, while the test runs */ +#define ALTSTACK_SIZE (64 * 1024)/* room for AVX-512/SVE frames */ + +#define FAULTS_PATH "/proc/xenomai/faults" +#define WATCHDOG_PATH "/sys/module/xenomai/parameters/watchdog_timeout" +#define KPROBES_OPT_PATH "/proc/sys/debug/kprobes-optimization" +#define KPROBE_TARGET "CoBaLt_thread_setname" +#define KPROBE_EVENT "smokey_traps" + +#define TRAP_LABEL_LEN 48 + +struct snapshot { +unsigned int faults[NR_TRAPS ]; +struct cobalt_threadstat stat; +}; + +/* +* The kinds of traps the cases expect. Each architecture maps them to +* the labels the core shows in /proc/xenomai/faults, which tell us the +* trap numbers at runtime. +*/ +enum trap_kind { +TRAP_PAGEFAULT , +TRAP_KPROBE , +TRAP_BREAKPOINT , +TRAP_UNDEF , +TRAP_ALIGNMENT , +TRAP_SP_ALIGNMENT , +TRAP_DIVIDE , +TRAP_GPF , +TRAP_SIMD , +TRAP_VSYSCALL ,/* x86: #PF, or #GP with LASS */ +NR_TRAP_KINDS +}; + +struct trap_case { +const char *name; +int (*prepare)(void );/* in secondary mode, optional */ +void (*trigger)(void );/* in primary mode */ +int (*verify)(void );/* check fixup results, optional */ +void (*cleanup)(void );/* always runs after prepare, optional */ +enum trap_kind trap;/* expected trap */ +int signo;/* expected signal, 0 for none */ +int si_code;/* expected si_code, -1 for any */ +bool nested;/* in-band fixup faults on a fresh page */ +}; + +static char trap_labels[NR_TRAPS ][TRAP_LABEL_LEN ]; +static int trap_nr[NR_TRAP_KINDS ];/* resolved, -1 if unknown */ + +static int rt_cpu; +static int failures; + +static sigjmp_buf trap_env; +static volatile int trap_signo; +static volatile int trap_si_code; +static volatile int trap_signals; +static volatile int trap_in_sigdebug;/* fault taken inside sigdebug_handler */ +static volatile unsigned long trap_ip; +static void *volatile trap_addr; +static volatile int in_sigdebug; +static volatile unsigned int sigdebug_reasons[MAX_REASONS ]; +static volatile int nr_sigdebug; + +static int kprobe_armed; +static char kprobe_opt_saved[8 ]; +static char tracefs[64 ]; +static const char *skip_reason;/* set by prepare handlers returning -ENOSYS */ +static char watchdog_saved[16 ]; + +static unsigned char static_buf[64] __attribute__((aligned(16))); +static unsigned int *fresh_page; +static unsigned char *ro_page; + +/* +* Architecture specific part: trap numbers (see the dovetail.h of the +* respective arch), the fault signals the cases may raise, the +* faulting instructions and the cases built on them. +*/ + +static int read_file (const char *path,char *buf,size_t len); +static int write_file (const char *path,const char *s); +static int map_fresh_page (void ); +static void unmap_fresh_page (void ); +static int trap_by_label (const char *label); + +#if defined(__arm__) + +/* Labels of kernel/cobalt/arch/arm/machine.c */ +static const char *const arm_trap_labels[NR_TRAP_KINDS ] = { + [TRAP_PAGEFAULT ] ="Data or instruction access" , + [TRAP_KPROBE ] ="Undefined instruction" , + [TRAP_BREAKPOINT ] ="Instruction breakpoint" , + [TRAP_UNDEF ] ="Undefined instruction" , + [TRAP_ALIGNMENT ] ="Unaligned access exception" , +}; + +/* +* Running on an arm64 kernel (compat): labels of +* kernel/cobalt/arch/arm64/machine.c. AArch32 breakpoints and SWP are +* undefined instructions to AArch64, kernel kprobes use BRK, and +* misaligned accesses are data aborts. +*/ +static const char *const compat_trap_labels[NR_TRAP_KINDS ] = { + [TRAP_PAGEFAULT ] ="Data or instruction abort" , + [TRAP_KPROBE ] ="Debug trap" , + [TRAP_BREAKPOINT ] ="Undefined instruction" , + [TRAP_UNDEF ] ="Undefined instruction" , + [TRAP_ALIGNMENT ] ="Data or instruction abort" , +}; + +static const char *const *arch_trap_labels (void ) +{ +if (trap_by_label ("Instruction breakpoint" ) <0 && +trap_by_label ("Debug trap" ) >=0 ) +return compat_trap_labels; + +return arm_trap_labels; +} + +static const unsigned int kprobe_offsets[] = {0 }; + +#define ALIGNMENT_PATH "/proc/cpu/alignment" +#define SWP_EMULATION_PATH "/proc/cpu/swp_emulation" +#define ABI_SWP_PATH "/proc/sys/abi/swp"/* arm64 kernel */ + +#define ALIGN_MODE_FIXUP 2 +#define ALIGN_MODE_SIGNAL 4 + +static const int arch_sigs[] = {SIGILL ,SIGBUS ,SIGTRAP }; + +/* +* ARM-state islands for the instructions which trigger the traps. +* The test might be compiled to Thumb-2 code, where some of the +* encodings do not exist (SWP) or behave differently. Keep the +* faulting instructions in leaf functions so that we can longjmp out +* of the signal handler without caring for the state they left. +*/ +asm( +" .text\n" +" .arm\n" +" .align 2\n" +" .global traps_arm_bkpt\n" +" .type traps_arm_bkpt, %function\n" +"traps_arm_bkpt:\n" +" .inst 0xe7f001f0\n" /* gdb/ptrace ARM breakpoint */ +" bx lr\n" +" .size traps_arm_bkpt, . - traps_arm_bkpt\n" +"\n" +" .global traps_arm_undef\n" +" .type traps_arm_undef, %function\n" +"traps_arm_undef:\n" +" .inst 0xe7f000f0\n" /* udf #0 */ +" bx lr\n" +" .size traps_arm_undef, . - traps_arm_undef\n" +"\n" +/* unsigned int traps_arm_swp(unsigned int *addr, unsigned int val) */ +" .global traps_arm_swp\n" +" .type traps_arm_swp, %function\n" +"traps_arm_swp:\n" +" mov r2, r0\n" +" .inst 0xe1020091\n" /* swp r0, r1, [r2] */ +" bx lr\n" +" .size traps_arm_swp, . - traps_arm_swp\n" +"\n" +/* void traps_arm_ldrd(void *addr, unsigned int out[2]) */ +" .global traps_arm_ldrd\n" +" .type traps_arm_ldrd, %function\n" +"traps_arm_ldrd:\n" +" .inst 0xe1c020d0\n" /* ldrd r2, r3, [r0] */ +" stm r1, {r2, r3}\n" +" bx lr\n" +" .size traps_arm_ldrd, . - traps_arm_ldrd\n" +); + +void traps_arm_bkpt (void ); +void traps_arm_undef (void ); +unsigned int traps_arm_swp (unsigned int *addr,unsigned int val); +void traps_arm_ldrd (void *addr,unsigned int out[2 ]); + +static int have_swp_emulation; +static const char *swp_skip_reason; +static char abi_swp_saved[8 ]; +static int align_fd =-1 ; +static int align_mode_saved =-1 ; +static int align_mode_current =-1 ;/* -1: unknown yet */ + +static void trigger_bkpt (void ) +{ +traps_arm_bkpt (); +} + +static void trigger_undef (void ) +{ +traps_arm_undef (); +} + +/* +* SWP is deprecated since ARMv7 and obsolete since ARMv8: with +* CONFIG_SWP_EMULATE, an arm kernel disables it (SCTLR.SW) and +* emulates it from the undefined instruction trap; an arm64 kernel +* does the same for compat tasks if abi.swp is set to emulation. +* Whether the hardware actually traps is another story, e.g. QEMU/TCG +* ignores SCTLR.SW and executes SWP natively on ARMv7 models. Probe +* this from secondary mode, counting emulations: both kernels account +* them as a perf software event, an arm kernel also exports a counter +* in /proc. +*/ +static int open_emulation_counter (void ) +{ +struct perf_event_attr attr; + +memset (&attr,0 ,sizeof (attr)); + attr.size =sizeof (attr); + attr.type =PERF_TYPE_SOFTWARE ; + attr.config =PERF_COUNT_SW_EMULATION_FAULTS ; + attr.exclude_kernel =1 ;/* still counts traps from user mode */ + attr.exclude_hv =1 ; + +return syscall (__NR_perf_event_open, &attr,0 ,-1 ,-1 ,0 ); +} + +static long long read_swp_emulated (int perf_fd) +{ +char buf[256 ], *p; +uint64_t count; + +if (perf_fd >=0 ) +return read (perf_fd, &count,sizeof (count)) ==sizeof (count) ? + (long long )count :-1 ; + +if (read_file (SWP_EMULATION_PATH , buf,sizeof (buf))) +return -1 ; + + p =strstr (buf,"Emulated SWP:" ); +if (p ==NULL ) +return -1 ; + +return strtoll (p +strlen ("Emulated SWP:" ),NULL ,10 ); +} + +static int prepare_swp (void ) +{ +long long before, after; +unsigned int word =0 ; +int perf_fd; + +if (!have_swp_emulation) { + skip_reason = swp_skip_reason; +return -ENOSYS ; + } + +/* The syscalls relax us if we are not in secondary mode yet. */ + perf_fd =open_emulation_counter (); + before =read_swp_emulated (perf_fd); + +if (sigsetjmp (trap_env,1 ) ==0 ) { +traps_arm_swp (&word,1 ); + }else { +if (perf_fd >=0 ) +close (perf_fd); + skip_reason ="SWP raises SIGILL, not emulated" ; +return -ENOSYS ; + } + + after =read_swp_emulated (perf_fd); +if (perf_fd >=0 ) +close (perf_fd); + +/* +* Without a counter, assume it trapped: ARMv8 hardware has +* no SWP in AArch32. +*/ +if (before >=0 && after == before) { + skip_reason ="SWP is executed natively, not trapped" ; +return -ENOSYS ; + } + +return 0 ; +} + +static unsigned int swp_result; + +static void trigger_swp_valid (void ) +{ +unsigned int *word = (unsigned int *)static_buf; + + *word =0x11223344 ; + swp_result =traps_arm_swp (word,0x55667788 ); +} + +static int verify_swp_valid (void ) +{ +unsigned int *word = (unsigned int *)static_buf; + +if (swp_result !=0x11223344 || *word !=0x55667788 ) { +smokey_warning ("swp: bad emulation result %#x/%#x" , + swp_result, *word); +return -1 ; + } + +return 0 ; +} + +static int prepare_swp_fresh_page (void ) +{ +int ret; + + ret =prepare_swp (); +if (ret) +return ret; + +return map_fresh_page (); +} + +static void trigger_swp_fresh_page (void ) +{ + swp_result =traps_arm_swp (fresh_page,0x55667788 ); +} + +static int verify_swp_fresh_page (void ) +{ +if (swp_result !=0 || *fresh_page !=0x55667788 ) { +smokey_warning ("swp: bad emulation result %#x/%#x on fresh page" , + swp_result, *fresh_page); +return -1 ; + } + +return 0 ; +} + +static void trigger_swp_badaddr (void ) +{ +/* Lowest page is never mapped (vm.mmap_min_addr). */ +traps_arm_swp ((unsigned int *)0x10 ,0 ); +} + +static int read_align_mode (void ) +{ +char line[128 ]; +int mode =-1 ; +FILE *fp; + + fp =fopen (ALIGNMENT_PATH ,"r" ); +if (fp ==NULL ) +return -errno; + +while (fgets (line,sizeof (line), fp)) +if (sscanf (line,"User faults: %d" , &mode) ==1 ) +break ; + +fclose (fp); + +return mode; +} + +static int set_align_mode (int mode) +{ +char c ='0' + mode; + +if (align_fd <0 ) +return -ENOSYS ; + +if (write (align_fd, &c,1 ) !=1 ) +return -errno; + +return 0 ; +} + +static unsigned int ldrd_result[2 ]; + +/* +* How does the kernel handle a misaligned LDRD from user mode? Probed +* from secondary mode, for kernels which do not tell: arm64 kernels +* fix up (CONFIG_COMPAT_ALIGNMENT_FIXUPS) or signal, as configured at +* build time. +*/ +static int probe_align_mode (void ) +{ +memcpy (static_buf,"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a" ,10 ); + +if (sigsetjmp (trap_env,1 ) ==0 ) { +traps_arm_ldrd (static_buf +1 , ldrd_result); +return memcmp (ldrd_result, static_buf +1 , +sizeof (ldrd_result)) ?-1 :ALIGN_MODE_FIXUP ; + } + +return trap_signo ==SIGBUS ?ALIGN_MODE_SIGNAL :-1 ; +} + +/* +* Make the kernel handle misaligned user accesses the given way. We +* can switch the mode as root on an arm kernel, otherwise only the +* cases matching the current mode can run. +*/ +static int use_align_mode (int mode) +{ +if (align_fd >=0 ) +return set_align_mode (mode); + +if (align_mode_current <0 ) + align_mode_current =probe_align_mode (); + +if (align_mode_current >=0 && (align_mode_current & mode)) +return 0 ; + +if (align_mode_current <0 ) + skip_reason ="cannot determine the alignment fault mode" ; +else if (align_mode_current &ALIGN_MODE_FIXUP ) + skip_reason ="kernel fixes up misaligned accesses" ; +else if (align_mode_current &ALIGN_MODE_SIGNAL ) + skip_reason ="kernel signals misaligned accesses" ; +else + skip_reason ="kernel ignores misaligned accesses" ; + +return -ENOSYS ; +} + +static int prepare_align_fixup (void ) +{ +return use_align_mode (ALIGN_MODE_FIXUP ); +} + +static void trigger_align_fixup (void ) +{ +memcpy (static_buf,"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a" ,10 ); +traps_arm_ldrd (static_buf +1 , ldrd_result); +} + +static int verify_align_fixup (void ) +{ +unsigned int expected[2 ]; + +memcpy (expected, static_buf +1 ,sizeof (expected)); +if (memcmp (ldrd_result, expected,sizeof (expected))) { +smokey_warning ("ldrd: bad fixup result %#x/%#x, expected %#x/%#x" , + ldrd_result[0 ], ldrd_result[1 ], + expected[0 ], expected[1 ]); +return -1 ; + } + +return 0 ; +} + +static int prepare_align_fixup_fresh_page (void ) +{ +int ret; + + ret =use_align_mode (ALIGN_MODE_FIXUP ); +if (ret) +return ret; + +return map_fresh_page (); +} + +static void trigger_align_fixup_fresh_page (void ) +{ +traps_arm_ldrd ((unsigned char *)fresh_page +1 , ldrd_result); +} + +static int verify_align_fixup_fresh_page (void ) +{ +if (ldrd_result[0 ] !=0 || ldrd_result[1 ] !=0 ) { +smokey_warning ("ldrd: bad fixup result %#x/%#x on fresh page" , + ldrd_result[0 ], ldrd_result[1 ]); +return -1 ; + } + +return 0 ; +} + +static int prepare_align_signal (void ) +{ +return use_align_mode (ALIGN_MODE_SIGNAL ); +} + +static void trigger_align_signal (void ) +{ +traps_arm_ldrd (static_buf +1 , ldrd_result); +} + +static void cleanup_align (void ) +{ +if (align_fd >=0 ) +set_align_mode (align_mode_saved); +} + +static void cleanup_align_fresh_page (void ) +{ +unmap_fresh_page (); +cleanup_align (); +} + +static const struct trap_case arch_cases[] = { + { + .name ="bkpt" , + .trigger = trigger_bkpt, + .trap =TRAP_BREAKPOINT , + .signo =SIGTRAP , + .si_code =TRAP_BRKPT , + }, { + .name ="undef" , + .trigger = trigger_undef, + .trap =TRAP_UNDEF , + .signo =SIGILL , + .si_code =ILL_ILLOPC , + }, { + .name ="swp_valid" , + .prepare = prepare_swp, + .trigger = trigger_swp_valid, + .verify = verify_swp_valid, + .trap =TRAP_UNDEF , + }, { + .name ="swp_fresh_page" , + .prepare = prepare_swp_fresh_page, + .trigger = trigger_swp_fresh_page, + .verify = verify_swp_fresh_page, + .cleanup = unmap_fresh_page, + .trap =TRAP_UNDEF , + .nested = true, + }, { +/* +* set_segfault() in the kernel picks the si_code with +* find_vma(), which yields the lowest mapping above +* the address, hence ACCERR rather than MAPERR. Don't +* depend on this. +*/ + .name ="swp_badaddr" , + .prepare = prepare_swp, + .trigger = trigger_swp_badaddr, + .trap =TRAP_UNDEF , + .signo =SIGSEGV , + .si_code =-1 , + .nested = true, + }, { + .name ="align_fixup" , + .prepare = prepare_align_fixup, + .trigger = trigger_align_fixup, + .verify = verify_align_fixup, + .cleanup = cleanup_align, + .trap =TRAP_ALIGNMENT , + }, { + .name ="align_fixup_fresh_page" , + .prepare = prepare_align_fixup_fresh_page, + .trigger = trigger_align_fixup_fresh_page, + .verify = verify_align_fixup_fresh_page, + .cleanup = cleanup_align_fresh_page, + .trap =TRAP_ALIGNMENT , + .nested = true, + }, { + .name ="align_signal" , + .prepare = prepare_align_signal, + .trigger = trigger_align_signal, + .cleanup = cleanup_align, + .trap =TRAP_ALIGNMENT , + .signo =SIGBUS , + .si_code =BUS_ADRALN , + }, +}; + +static inline void arch_sanitize_handler_context (void ) { } + +static inline unsigned long arch_fault_ip (void *context) +{ +return ((ucontext_t *)context)->uc_mcontext.arm_pc; +} + +static void arch_init (void ) +{ +if (access (SWP_EMULATION_PATH ,F_OK ) ==0 ) { + have_swp_emulation =1 ; + }else if (read_file (ABI_SWP_PATH , abi_swp_saved, +sizeof (abi_swp_saved)) ==0 ) { +/* arm64 kernel: 0=undef, 1=emulate, 2=hw (n/a for SWP) */ +if (atoi (abi_swp_saved) ==1 ) { + have_swp_emulation =1 ; + abi_swp_saved[0 ] ='\0' ;/* nothing to restore */ + }else if (write_file (ABI_SWP_PATH ,"1\n" ) ==0 ) { + have_swp_emulation =1 ; + }else { + abi_swp_saved[0 ] ='\0' ; + swp_skip_reason ="SWP emulation disabled (" ABI_SWP_PATH ")" ; + } + }else + swp_skip_reason ="kernel does not emulate SWP" ; + +/* An arm64 kernel has no such file, we probe it later on. */ + align_mode_current =read_align_mode (); + align_fd =open (ALIGNMENT_PATH ,O_WRONLY ); +if (align_fd >=0 ) { + align_mode_saved = align_mode_current; +if (align_mode_saved <0 ) { +close (align_fd); + align_fd =-1 ; + } + } +} + +static void arch_cleanup (void ) +{ +if (align_fd >=0 ) { +set_align_mode (align_mode_saved); +close (align_fd); + align_fd =-1 ; + } + +if (abi_swp_saved[0 ]) { +write_file (ABI_SWP_PATH , abi_swp_saved); + abi_swp_saved[0 ] ='\0' ; + } +} + +#elif defined(__aarch64__) + +/* Labels of kernel/cobalt/arch/arm64/machine.c, kprobes use BRK */ +static const char *const arm64_trap_labels[NR_TRAP_KINDS ] = { + [TRAP_PAGEFAULT ] ="Data or instruction abort" , + [TRAP_KPROBE ] ="Debug trap" , + [TRAP_BREAKPOINT ] ="Debug trap" , + [TRAP_UNDEF ] ="Undefined instruction" , + [TRAP_SP_ALIGNMENT ] ="SP/PC alignment abort" , +}; + +static const char *const *arch_trap_labels (void ) +{ +return arm64_trap_labels; +} + +static const unsigned int kprobe_offsets[] = {0 }; + +static const int arch_sigs[] = {SIGILL ,SIGBUS ,SIGTRAP }; + +static void trigger_brk (void ) +{ + asmvolatile ("brk #0" :::"memory" ); +} + +static void trigger_udf (void ) +{ + asmvolatile (".inst 0x00000000" :::"memory" );/* udf #0 */ +} + +/* +* SCTLR_EL1.SA0 is set by Linux: a data access through a misaligned +* SP from EL0 raises an SP alignment fault, regardless of the +* alignment of the address itself. Emulators may not implement this +* check (QEMU/TCG does not), probe it from secondary mode first. +*/ +static void trigger_sp_align (void ) +{ + asmvolatile ( +" mov x9, sp\n" +" add x10, %0, #1\n" +" mov sp, x10\n" +" ldr x11, [sp]\n" +" mov sp, x9\n" + : :"r" (static_buf) :"x9" ,"x10" ,"x11" ,"memory" ); +} + +static int prepare_sp_align (void ) +{ +if (sigsetjmp (trap_env,1 ) ==0 ) { +trigger_sp_align (); + skip_reason ="SP alignment is not checked by this CPU/emulator" ; +return -ENOSYS ; + } + +return 0 ; +} + +/* The misaligned SP of sp_align is taken care of by the altstack. */ +static inline void arch_sanitize_handler_context (void ) { } + +static inline unsigned long arch_fault_ip (void *context) +{ +return ((ucontext_t *)context)->uc_mcontext.pc; +} + +static const struct trap_case arch_cases[] = { + { + .name ="brk" , + .trigger = trigger_brk, + .trap =TRAP_BREAKPOINT , + .signo =SIGTRAP , + .si_code =TRAP_BRKPT , + }, { + .name ="udf" , + .trigger = trigger_udf, + .trap =TRAP_UNDEF , + .signo =SIGILL , + .si_code =ILL_ILLOPC , + }, { + .name ="sp_align" , + .prepare = prepare_sp_align, + .trigger = trigger_sp_align, + .trap =TRAP_SP_ALIGNMENT , + .signo =SIGBUS , + .si_code =BUS_ADRALN , + }, +}; + +static void arch_init (void ) { } +static void arch_cleanup (void ) { } + +#elif defined(__x86_64__) + +/* Labels of kernel/cobalt/arch/x86/machine.c, kprobes use INT3 */ +static const char *x86_trap_labels[NR_TRAP_KINDS ] = { + [TRAP_PAGEFAULT ] ="Page fault" , + [TRAP_KPROBE ] ="Int3" , + [TRAP_BREAKPOINT ] ="Int3" , + [TRAP_UNDEF ] ="Invalid opcode" , + [TRAP_ALIGNMENT ] ="Alignment check" , + [TRAP_DIVIDE ] ="Divide error" , + [TRAP_GPF ] ="General protection" , + [TRAP_SIMD ] ="SIMD error" , + [TRAP_VSYSCALL ] ="Page fault" , +}; + +static bool cpu_has_flag (const char *flag) +{ +char *line =NULL , *p, *save; +bool found = false; +size_t len =0 ; +FILE *fp; + + fp =fopen ("/proc/cpuinfo" ,"r" ); +if (fp ==NULL ) +return false; + +while (getline (&line, &len, fp) >0 ) { +if (strncmp (line,"flags" ,5 )) +continue ; + p =strchr (line,':' ); +for (p = p ?strtok_r (p +1 ," \n" , &save) :NULL ; p && !found; + p =strtok_r (NULL ," \n" , &save)) + found =strcmp (p, flag) ==0 ; +break ; + } + +free (line); +fclose (fp); + +return found; +} + +static const char *const *arch_trap_labels (void ) +{ +/* +* With LASS, user mode accesses to the upper half of the +* address space raise #GP, calling a vsyscall included. +*/ +if (cpu_has_flag ("lass" )) + x86_trap_labels[TRAP_VSYSCALL ] ="General protection" ; + +return x86_trap_labels; +} + +/* +* With KPROBES_ON_FTRACE, a probe on the function entry is hooked via +* the __fentry__ call and does not trap at all. Fall back to the +* first instruction after the 5-byte fentry NOP, or after +* ENDBR64+NOP with IBT. The kernel rejects offsets which are not an +* instruction boundary, so trying is safe. +*/ +static const unsigned int kprobe_offsets[] = {0 ,5 ,9 }; + +#define MXCSR_ZE (1 << 2)/* divide-by-zero flag */ +#define MXCSR_ZM (1 << 9)/* divide-by-zero mask */ + +static const int arch_sigs[] = {SIGILL ,SIGBUS ,SIGTRAP ,SIGFPE }; + +static void trigger_int3 (void ) +{ + asmvolatile ("int3" :::"memory" ); +} + +static void trigger_ud2 (void ) +{ + asmvolatile ("ud2" :::"memory" ); +} + +static void trigger_div0 (void ) +{ +unsigned int zero =0 ; + + asmvolatile ( +" xor %%edx, %%edx\n" +" mov $1, %%eax\n" +" divl %0\n" + : :"m" (zero) :"eax" ,"edx" ,"cc" ,"memory" ); +} + +static void trigger_gp (void ) +{ + asmvolatile ("hlt" :::"memory" );/* privileged */ +} + +/* +* Divide 1.0f by 0.0f with the given MXCSR, return MXCSR afterwards. +* The trap is taken when the divide-by-zero exception is unmasked. +*/ +static unsigned int simd_div (unsigned int mxcsr) +{ + static const float one[4] __attribute__((aligned(16))) = { 1, 1, 1, 1 }; + static const float zero[4] __attribute__((aligned(16))) = { 0, 0, 0, 0 }; +unsigned int out; + + asmvolatile ( +" ldmxcsr %1\n" +" movaps %2, %%xmm0\n" +" movaps %3, %%xmm1\n" +" divps %%xmm1, %%xmm0\n" +" stmxcsr %0\n" + :"=m" (out) + :"m" (mxcsr),"m" (one),"m" (zero) + :"xmm0" ,"xmm1" ,"memory" ); + +return out; +} + +static unsigned int mxcsr_saved; + +/* +* Probe from secondary mode whether an unmasked divide-by-zero +* actually traps on this hardware/emulator (SIGFPE lands in +* fault_handler(), which longjmps back to us). +*/ +static int prepare_simd_div0 (void ) +{ + asmvolatile ("stmxcsr %0" :"=m" (mxcsr_saved)); + +if (sigsetjmp (trap_env,1 ) ==0 ) { +simd_div (mxcsr_saved & ~MXCSR_ZM ); + asmvolatile ("ldmxcsr %0" : :"m" (mxcsr_saved)); + skip_reason ="SIMD exceptions do not trap" ; +return -ENOSYS ; + } + + asmvolatile ("ldmxcsr %0" : :"m" (mxcsr_saved)); +return 0 ; +} + +static void trigger_simd_div0 (void ) +{ +simd_div (mxcsr_saved & ~MXCSR_ZM ); +} + +static void cleanup_simd_div0 (void ) +{ +/* Restore the mask bits, clear the sticky ZE flag. */ + asmvolatile ("ldmxcsr %0" : :"m" (mxcsr_saved)); +} + +/* +* Mask all SIMD exceptions again. +*/ +static inline void arch_sanitize_handler_context (void ) +{ +static const unsigned int mxcsr_default =0x1f80 ; + + asmvolatile ("ldmxcsr %0\n" : :"m" (mxcsr_default)); +} + +static inline unsigned long arch_fault_ip (void *context) +{ +return ((ucontext_t *)context)->uc_mcontext.gregs[REG_RIP ]; +} + +/* +* Legacy vsyscalls: calling into the vsyscall page traps, and the +* kernel emulates the call unless booted with vsyscall=none. The +* emulated gettimeofday() writes its result to user memory from the +* in-band fixup. +*/ +#define VSYSCALL_GETTIMEOFDAY 0xffffffffff600000UL + +typedef long (*vgettimeofday_t )(struct timeval *tv,struct timezone *tz); + +static struct timeval vsyscall_tv; +static long vsyscall_ret; + +static int prepare_vsyscall (void ) +{ +bool found = false; +char line[256 ]; +FILE *fp; + + fp =fopen ("/proc/self/maps" ,"r" ); +if (fp ==NULL ) +return -errno; + +while (!found &&fgets (line,sizeof (line), fp)) + found =strstr (line,"[vsyscall]" ) !=NULL ; + +fclose (fp); + +if (!found) { + skip_reason ="no vsyscall page (vsyscall=none)" ; +return -ENOSYS ; + } + + vsyscall_ret =-1 ; +memset (&vsyscall_tv,0 ,sizeof (vsyscall_tv)); + +return 0 ; +} + +static void trigger_vsyscall (void ) +{ + vsyscall_ret = ((vgettimeofday_t )VSYSCALL_GETTIMEOFDAY )(&vsyscall_tv, +NULL ); +} + +static int check_vsyscall_result (const struct timeval *tv) +{ +if (vsyscall_ret !=0 || tv->tv_sec <=0 || + tv->tv_usec <0 || tv->tv_usec >=1000000 ) { +smokey_warning ("vsyscall gettimeofday: returned %ld, " +"tv = %lld.%06ld" , vsyscall_ret, + (long long )tv->tv_sec, (long )tv->tv_usec); +return -1 ; + } + +return 0 ; +} + +static int verify_vsyscall (void ) +{ +return check_vsyscall_result (&vsyscall_tv); +} + +static int prepare_vsyscall_fresh_page (void ) +{ +int ret; + + ret =prepare_vsyscall (); +if (ret) +return ret; + +return map_fresh_page (); +} + +static void trigger_vsyscall_fresh_page (void ) +{ + vsyscall_ret = ((vgettimeofday_t )VSYSCALL_GETTIMEOFDAY )( + (struct timeval *)fresh_page,NULL ); +} + +static int verify_vsyscall_fresh_page (void ) +{ +return check_vsyscall_result ((struct timeval *)fresh_page); +} + +/* +* The emulation checks the pointer against the user address range +* only, the kernel takes a page fault when writing the result. This +* logs "vsyscall fault (exploit attempt?)". +* +* The failed emulation leaves the IP on the vsyscall page, which has +* no unwind info. With relax tracing, libcobalt answers the SIGSHADOW +* request Cobalt sends upon relaxing us with a backtrace(), which +* unwinds into that context, and the unwinder then reads the code at +* the IP to look for a sigreturn trampoline. With vsyscall=xonly, this +* read faults, and our fault handler leaves for good before SIGDEBUG +* and the SIGSEGV of the emulation are delivered. Keep SIGSHADOW +* blocked until the case is over, so that the backtrace is taken from +* the cleanup handler instead. +*/ +static sigset_t vsyscall_saved_sigmask; +static bool vsyscall_sigshadow_blocked; + +static int prepare_vsyscall_badaddr (void ) +{ +sigset_t set; +int ret; + + ret =prepare_vsyscall (); +if (ret) +return ret; + +sigemptyset (&set); +sigaddset (&set,SIGSHADOW ); + ret =pthread_sigmask (SIG_BLOCK , &set, &vsyscall_saved_sigmask); +if (ret) +return -ret; + + vsyscall_sigshadow_blocked = true; + +return 0 ; +} + +static void trigger_vsyscall_badaddr (void ) +{ +/* Lowest page is never mapped (vm.mmap_min_addr). */ + ((vgettimeofday_t )VSYSCALL_GETTIMEOFDAY )((struct timeval *)0x10 ,NULL ); +} + +static void cleanup_vsyscall_badaddr (void ) +{ +if (vsyscall_sigshadow_blocked) { +pthread_sigmask (SIG_SETMASK , &vsyscall_saved_sigmask,NULL ); + vsyscall_sigshadow_blocked = false; + } +} + +/* +* With UMIP enabled (CR4.UMIP), SGDT, SIDT, SLDT, SMSW and STR raise +* #GP from user mode, and the kernel emulates them with dummy results. +* Probe this from secondary mode: SGDT yields the dummy GDT base only +* if emulated. +*/ +#define UMIP_DUMMY_GDT_BASE 0xfffffffffffe0000ULL + +struct dtr { +uint16_t limit; +uint64_t base; +} __attribute__((packed)); + +static void sgdt (void *dst) +{ + asmvolatile ("sgdt (%0)" : :"r" (dst) :"memory" ); +} + +static int prepare_umip (void ) +{ +struct dtr dtr = {0 ,0 }; + +sgdt (&dtr); +if (dtr.base !=UMIP_DUMMY_GDT_BASE ) { + skip_reason ="UMIP is not enabled" ; +return -ENOSYS ; + } + +memset (static_buf,0 ,sizeof (dtr)); + +return 0 ; +} + +static void trigger_umip (void ) +{ +sgdt (static_buf); +} + +static int check_umip_result (const void *p) +{ +struct dtr dtr; + +memcpy (&dtr, p,sizeof (dtr)); +if (dtr.base !=UMIP_DUMMY_GDT_BASE ) { +smokey_warning ("sgdt: base %#llx, expected the emulated %#llx" , + (unsigned long long )dtr.base, +UMIP_DUMMY_GDT_BASE ); +return -1 ; + } + +return 0 ; +} + +static int verify_umip (void ) +{ +return check_umip_result (static_buf); +} + +static int prepare_umip_fresh_page (void ) +{ +int ret; + + ret =prepare_umip (); +if (ret) +return ret; + +return map_fresh_page (); +} + +static void trigger_umip_fresh_page (void ) +{ +sgdt (fresh_page); +} + +static int verify_umip_fresh_page (void ) +{ +return check_umip_result (fresh_page); +} + +/* +* A canonical kernel address: copy_to_user() rejects it right away, +* the emulation sends SIGSEGV without taking a page fault. +*/ +static void trigger_umip_badaddr (void ) +{ +sgdt ((void *)0xffff800000000000UL ); +} + +static const struct trap_case arch_cases[] = { + { +/* do_int3_user() → do_trap() with sicode 0: SI_KERNEL. */ + .name ="int3" , + .trigger = trigger_int3, + .trap =TRAP_BREAKPOINT , + .signo =SIGTRAP , + .si_code =-1 , + }, { + .name ="ud2" , + .trigger = trigger_ud2, + .trap =TRAP_UNDEF , + .signo =SIGILL , + .si_code =ILL_ILLOPN , + }, { + .name ="div0" , + .trigger = trigger_div0, + .trap =TRAP_DIVIDE , + .signo =SIGFPE , + .si_code =FPE_INTDIV , + }, { +/* gp_user_force_sig_segv() uses force_sig(): SI_KERNEL. */ + .name ="gp" , + .trigger = trigger_gp, + .trap =TRAP_GPF , + .signo =SIGSEGV , + .si_code =-1 , + }, { + .name ="simd_div0" , + .prepare = prepare_simd_div0, + .trigger = trigger_simd_div0, + .cleanup = cleanup_simd_div0, + .trap =TRAP_SIMD , + .signo =SIGFPE , + .si_code =FPE_FLTDIV , + }, { + .name ="vsyscall" , + .prepare = prepare_vsyscall, + .trigger = trigger_vsyscall, + .verify = verify_vsyscall, + .trap =TRAP_VSYSCALL , + }, { + .name ="vsyscall_fresh_page" , + .prepare = prepare_vsyscall_fresh_page, + .trigger = trigger_vsyscall_fresh_page, + .verify = verify_vsyscall_fresh_page, + .cleanup = unmap_fresh_page, + .trap =TRAP_VSYSCALL , + .nested = true, + }, { +/* The emulation uses force_sig(): SI_KERNEL. */ + .name ="vsyscall_badaddr" , + .prepare = prepare_vsyscall_badaddr, + .trigger = trigger_vsyscall_badaddr, + .cleanup = cleanup_vsyscall_badaddr, + .trap =TRAP_VSYSCALL , + .signo =SIGSEGV , + .si_code =SI_KERNEL , + .nested = true, + }, { + .name ="umip" , + .prepare = prepare_umip, + .trigger = trigger_umip, + .verify = verify_umip, + .trap =TRAP_GPF , + }, { + .name ="umip_fresh_page" , + .prepare = prepare_umip_fresh_page, + .trigger = trigger_umip_fresh_page, + .verify = verify_umip_fresh_page, + .cleanup = unmap_fresh_page, + .trap =TRAP_GPF , + .nested = true, + }, { + .name ="umip_badaddr" , + .prepare = prepare_umip, + .trigger = trigger_umip_badaddr, + .trap =TRAP_GPF , + .signo =SIGSEGV , + .si_code =SEGV_MAPERR , + }, +}; + +static void arch_init (void ) { } +static void arch_cleanup (void ) { } + +#endif /* arch */ + +/* +* Runtime state collection. +*/ + +static int read_faults (unsigned int faults[NR_TRAPS ]) +{ +char line[2048 ], *p, *q; +unsigned int val; +int trap, n; +FILE *fp; + +memset (faults,0 ,sizeof (unsigned int ) *NR_TRAPS ); + + fp =fopen (FAULTS_PATH ,"r" ); +if (fp ==NULL ) +return -errno; + +/* Header: "TRAP CPU0 CPU1 ..." */ +if (fgets (line,sizeof (line), fp) ==NULL ) { +fclose (fp); +return -EIO ; + } + +/* The test runs on the first realtime CPU, we only parse its column. */ + p =strstr (line,"CPU" ); +if (p ==NULL ) { +fclose (fp); +return -EIO ; + } + rt_cpu =atoi (p +3 ); + +while (fgets (line,sizeof (line), fp)) { +if (sscanf (line," %d:%n" , &trap, &n) !=1 ) +continue ; +if (trap <0 || trap >=NR_TRAPS ) +continue ; + p = line + n; +if (sscanf (p," %u" , &val) ==1 ) + faults[trap] = val; +/* Trailing label: " (Page fault)" */ + p =strrchr (line,'(' ); + q =strrchr (line,')' ); +if (p && q && q > p) { + *q ='\0' ; +snprintf (trap_labels[trap],TRAP_LABEL_LEN ,"%s" , p +1 ); + } + } + +fclose (fp); + +return 0 ; +} + +static int trap_by_label (const char *label) +{ +int trap; + +for (trap =0 ; trap <NR_TRAPS ; trap++) +if (strcmp (trap_labels[trap], label) ==0 ) +return trap; + +return -1 ; +} + +static void resolve_traps (void ) +{ +const char *const *labels =arch_trap_labels (); +int kind; + +for (kind =0 ; kind <NR_TRAP_KINDS ; kind++) + trap_nr[kind] = labels[kind] ?trap_by_label (labels[kind]) :-1 ; +} + +static int take_snapshot (struct snapshot *s) +{ +int ret; + + ret =read_faults (s->faults); +if (ret) +return ret; + +return cobalt_thread_stat (0 , &s->stat); +} + +static int read_file (const char *path,char *buf,size_t len) +{ +ssize_t n; +int fd; + + fd =open (path,O_RDONLY ); +if (fd <0 ) +return -errno; + + n =read (fd, buf, len -1 ); +close (fd); +if (n <0 ) +return -errno; + + buf[n] ='\0' ; + +return 0 ; +} + +static int write_file (const char *path,const char *s) +{ +int fd, ret =0 ; + + fd =open (path,O_WRONLY ); +if (fd <0 ) +return -errno; + +if (write (fd, s,strlen (s)) <0 ) + ret = -errno; + +close (fd); + +return ret; +} + +static const char *sigdebug_reason_name (unsigned int reason) +{ +static const char *const names[] = { + [SIGDEBUG_UNDEFINED ] ="UNDEFINED" , + [SIGDEBUG_MIGRATE_SIGNAL ] ="MIGRATE_SIGNAL" , + [SIGDEBUG_MIGRATE_SYSCALL ] ="MIGRATE_SYSCALL" , + [SIGDEBUG_MIGRATE_FAULT ] ="MIGRATE_FAULT" , + [SIGDEBUG_MIGRATE_PRIOINV ] ="MIGRATE_PRIOINV" , + [SIGDEBUG_NOMLOCK ] ="NOMLOCK" , + [SIGDEBUG_WATCHDOG ] ="WATCHDOG" , + [SIGDEBUG_RESCNT_IMBALANCE ] ="RESCNT_IMBALANCE" , + [SIGDEBUG_LOCK_BREAK ] ="LOCK_BREAK" , + [SIGDEBUG_MUTEX_SLEEP ] ="MUTEX_SLEEP" , + }; + +if (reason <ARRAY_SIZE (names) && names[reason]) +return names[reason]; + +return "?" ; +} + +/* +* The Cobalt watchdog is our safety net against a kernel which does +* not report a trap: the thread then spins on the faulting +* instruction on the oob stage until the watchdog relaxes it. +*/ +static int setup_watchdog (void ) +{ +char buf[16 ]; +int wd, ret; + + ret =cobalt_corectl (_CC_COBALT_GET_WATCHDOG, &wd,sizeof (wd)); +if (ret || wd ==0 ) { +smokey_note ("Cobalt watchdog not available, refusing to run" ); +return -ENOSYS ; + } + + ret =read_file (WATCHDOG_PATH , watchdog_saved,sizeof (watchdog_saved)); +if (ret ==0 ) { +snprintf (buf,sizeof (buf),"%d" ,WATCHDOG_TIMEOUT ); + ret =write_file (WATCHDOG_PATH , buf); + } +if (ret) { + watchdog_saved[0 ] ='\0' ; +smokey_note ("cannot set the watchdog timeout (%s), " +"a broken kernel takes longer to fail" , +strerror (-ret)); + } + +return 0 ; +} + +static void restore_watchdog (void ) +{ +if (watchdog_saved[0 ]) { +write_file (WATCHDOG_PATH , watchdog_saved); + watchdog_saved[0 ] ='\0' ; + } +} + +/* +* Signal handling. +*/ + +/* +* Both signals of a case (SIGDEBUG from the relax, the fault signal +* from the in-band fixup) are pending on the same return to user +* space. The kernel dequeues the synchronous one first and stacks +* the SIGDEBUG frame on top of it, so this handler runs first, with +* the register state of the trapping instruction (minus what the +* kernel clears for signal delivery, x86: DF, RF, TF). Undo what our +* triggers enabled before touching anything else. +*/ +static void sigdebug_handler (int sig,siginfo_t *si,void *context) +{ +arch_sanitize_handler_context (); + + in_sigdebug =1 ; +if (nr_sigdebug <MAX_REASONS ) + sigdebug_reasons[nr_sigdebug] =sigdebug_reason (si); + nr_sigdebug++; + in_sigdebug =0 ; +} + +static void fault_handler (int sig,siginfo_t *si,void *context) +{ +/* See above, and we never return to the interrupted code. */ +arch_sanitize_handler_context (); + + trap_signals++; + trap_signo = sig; + trap_si_code = si->si_code; + trap_addr = si->si_addr; + trap_ip =arch_fault_ip (context); + trap_in_sigdebug = in_sigdebug; + in_sigdebug =0 ; + +if (sig ==SIGSEGV && si->si_addr == ro_page) { +/* Page fault case: fix up and retry the access. */ +mprotect (ro_page,1 ,PROT_READ |PROT_WRITE ); +return ; + } + +siglongjmp (trap_env,1 ); +} + +static void reset_trap_state (void ) +{ + trap_signals =0 ; + trap_signo =0 ; + trap_si_code =0 ; + trap_addr =NULL ; + trap_ip =0 ; + trap_in_sigdebug =0 ; + nr_sigdebug =0 ; +} + +/* +* Helpers for the nested cases: a fresh anonymous page, which is +* guaranteed not to be resident when the in-band fixup of a trap +* accesses it. We run with mlockall(MCL_FUTURE), so the page is +* populated upon mmap() and we have to unlock and drop it explicitly. +* arm64 has no in-band fixups which access user memory for AArch64 +* tasks, the helpers are unused there. +*/ +static __maybe_unused int map_fresh_page (void ) +{ +long psz =sysconf (_SC_PAGESIZE); +void *p; + + p =mmap (NULL , psz,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (p ==MAP_FAILED ) +return -errno; + + fresh_page = p; + +if (munlock (p, psz) ||madvise (p, psz,MADV_DONTNEED )) { +int ret = -errno; +munmap (p, psz); + fresh_page =NULL ; +return ret; + } + +return 0 ; +} + +static void unmap_fresh_page (void ) +{ +if (fresh_page) { +munmap (fresh_page,sysconf (_SC_PAGESIZE)); + fresh_page =NULL ; + } +} + +/* +* Common test cases. +*/ + +static void trigger_pagefault (void ) +{ + *ro_page ^=0xff ; +} + +static int prepare_pagefault (void ) +{ +return mprotect (ro_page,1 ,PROT_READ ) ? -errno :0 ; +} + +static int arm_kprobe (unsigned int offset) +{ +char path[128 ], buf[64 ]; +int ret; + +snprintf (path,sizeof (path),"%s/kprobe_events" , tracefs); +snprintf (buf,sizeof (buf),"p:%s %s+%u\n" ,KPROBE_EVENT ,KPROBE_TARGET , + offset); + ret =write_file (path, buf); +if (ret) +return ret; + + kprobe_armed =1 ; + +snprintf (path,sizeof (path),"%s/events/kprobes/%s/enable" , + tracefs,KPROBE_EVENT ); +return write_file (path,"1\n" ); +} + +static void disarm_kprobe (void ) +{ +char path[128 ], buf[64 ]; + +if (!kprobe_armed) +return ; + +snprintf (path,sizeof (path),"%s/events/kprobes/%s/enable" , + tracefs,KPROBE_EVENT ); +write_file (path,"0\n" ); + +snprintf (path,sizeof (path),"%s/kprobe_events" , tracefs); +snprintf (buf,sizeof (buf),"-:%s\n" ,KPROBE_EVENT ); +write_file (path, buf); + + kprobe_armed =0 ; +} + +/* +* Check how the kernel implemented our probe: only a breakpoint +* instruction traps, a probe hooked on ftrace or optimized into a +* jump does not. Returns 1 if trapping, 0 if not, -1 if unknown. +*/ +static int kprobe_traps (void ) +{ +char buf[4096 ], *line, *next; + +if (read_file ("/sys/kernel/debug/kprobes/list" , buf,sizeof (buf))) +return -1 ; + +for (line = buf; line && *line; line = next) { + next =strchr (line,'\n' ); +if (next) + *next++ ='\0' ; +if (strstr (line,KPROBE_TARGET ) ==NULL ) +continue ; +return strstr (line,"[FTRACE]" ) ==NULL && +strstr (line,"[OPTIMIZED]" ) ==NULL ; + } + +return -1 ; +} + +static int prepare_kprobe (void ) +{ +bool armed = false; +unsigned int i; +int ret; + +if (geteuid () !=0 ) +return -EPERM ; + +if (access ("/sys/kernel/tracing/kprobe_events" ,W_OK ) ==0 ) +strcpy (tracefs,"/sys/kernel/tracing" ); +else if (access ("/sys/kernel/debug/tracing/kprobe_events" ,W_OK ) ==0 ) +strcpy (tracefs,"/sys/kernel/debug/tracing" ); +else { + skip_reason ="kernel lacks kprobe support" ; +return -ENOSYS ; + } + +/* +* An optimized kprobe is a branch, not a trap. Disable the +* optimizer while we are at it (the knob only exists with +* CONFIG_OPTPROBES). +*/ +if (read_file (KPROBES_OPT_PATH , kprobe_opt_saved, +sizeof (kprobe_opt_saved)) ==0 ) { + ret =write_file (KPROBES_OPT_PATH ,"0\n" ); +if (ret) { + kprobe_opt_saved[0 ] ='\0' ; +return ret; + } + }else + kprobe_opt_saved[0 ] ='\0' ; + +for (i =0 ; i <ARRAY_SIZE (kprobe_offsets); i++) { + ret =arm_kprobe (kprobe_offsets[i]); +if (ret) { +disarm_kprobe (); +continue ; + } + armed = true; +if (kprobe_traps () !=0 ) +return 0 ; +disarm_kprobe (); + } + + skip_reason = armed ?"kprobe is hooked via ftrace, does not trap" : +"cannot arm a kprobe on " KPROBE_TARGET ; +return -ENOSYS ; +} + +static void cleanup_kprobe (void ) +{ +disarm_kprobe (); + +if (kprobe_opt_saved[0 ]) { +write_file (KPROBES_OPT_PATH , kprobe_opt_saved); + kprobe_opt_saved[0 ] ='\0' ; + } +} + +static int kprobe_ret; + +static void trigger_kprobe (void ) +{ +/* +* A current-mode Cobalt syscall, i.e. it runs in whatever +* mode the caller is in: in primary here. The kprobe fires +* from kernel mode on the oob stage. +*/ + kprobe_ret =pthread_setname_np (pthread_self (),"traps" ); +} + +static int verify_kprobe (void ) +{ +if (kprobe_ret) { +smokey_warning ("pthread_setname_np: %s" ,strerror (kprobe_ret)); +return -1 ; + } + +return 0 ; +} + +static const struct trap_case pagefault_case = { + .name ="pagefault" , + .prepare = prepare_pagefault, + .trigger = trigger_pagefault, + .trap =TRAP_PAGEFAULT , + .signo =SIGSEGV , + .si_code =SEGV_ACCERR , +}; + +static const struct trap_case kprobe_case = { + .name ="kprobe" , + .prepare = prepare_kprobe, + .trigger = trigger_kprobe, + .verify = verify_kprobe, + .cleanup = cleanup_kprobe, + .trap =TRAP_KPROBE , +}; + +/* +* Run the plain page fault case once more at the end: if any trap +* exit was missed before, the pending trap state suppresses this +* notification. +*/ +static const struct trap_case pagefault_again_case = { + .name ="pagefault_again" , + .prepare = prepare_pagefault, + .trigger = trigger_pagefault, + .trap =TRAP_PAGEFAULT , + .signo =SIGSEGV , + .si_code =SEGV_ACCERR , +}; + +/* +* Test driver. +*/ + +static bool name_selected (const char *name) +{ +const char *list, *p; +size_t len =strlen (name); + +if (!SMOKEY_ARG_ISSET (traps, cases)) +return true; + + list =SMOKEY_ARG_STRING (traps, cases); +for (p = list; p; p =strchr (p,',' )) { +if (*p ==',' ) + p++; +if (strncmp (p, name, len) ==0 && + (p[len] =='\0' || p[len] ==',' )) +return true; + } + +return false; +} + +static bool case_selected (const struct trap_case *c) +{ +/* pagefault_again is implied by "pagefault". */ +if (c == &pagefault_again_case) +return name_selected ("pagefault" ); + +return name_selected (c->name); +} + +static int go_primary (void ) +{ +struct timespec zero = {0 ,0 }; +int ret; + + ret =clock_nanosleep (CLOCK_MONOTONIC ,0 , &zero,NULL ); +if (ret) { +smokey_warning ("clock_nanosleep: %s" ,strerror (ret)); +return -1 ; + } + +if (cobalt_thread_mode () &XNRELAX ) { +smokey_warning ("not in primary mode after clock_nanosleep()" ); +return -1 ; + } + +return 0 ; +} + +static void wait_for_sigdebug (void ) +{ +struct timespec ts = {0 ,1000000 }; +int i; + +/* +* SIGDEBUG is pending at this point at the latest, but we +* might have left the fault handler by longjmp, so the signal +* is only delivered at the next kernel entry. We are in +* secondary mode now, a plain Linux syscall does not switch +* modes. If the trap was not reported, we are still in +* primary mode and this syscall relaxes us, with a +* SIGDEBUG_MIGRATE_SYSCALL our caller knows to ignore. +*/ +for (i =0 ; i <100 && nr_sigdebug ==0 ; i++) +__STD (nanosleep (&ts,NULL )); +} + +/* +* Help diagnosing a missing SIGDEBUG: is it blocked, pending, still +* routed to us, and does the thread still ask for it (XNWARN)? +*/ +static void report_sigdebug_state (const char *name) +{ +struct sigaction sa; +sigset_t mask, pend; + +pthread_sigmask (SIG_BLOCK ,NULL , &mask); +sigpending (&pend); +sigaction (SIGDEBUG ,NULL , &sa); + +smokey_warning ("%s: SIGDEBUG state: blocked=%d pending=%d " +"handler=%s XNWARN=%d mode=%#x" , name, +sigismember (&mask,SIGDEBUG ), +sigismember (&pend,SIGDEBUG ), + sa.sa_sigaction == sigdebug_handler ?"ours" : + sa.sa_handler ==SIG_IGN ?"SIG_IGN" : + sa.sa_handler ==SIG_DFL ?"SIG_DFL" :"other" , + !!(cobalt_thread_mode () &XNWARN ), +cobalt_thread_mode ()); +} + +static int check_case (const struct trap_case *c, +const struct snapshot *before, +const struct snapshot *after, +bool reported) +{ +int expected = trap_nr[c->trap], pagefault = trap_nr[TRAP_PAGEFAULT ]; +unsigned int delta; +int ret =0 , trap; + +if (c->signo) { +if (trap_signals !=1 || trap_signo != c->signo || + (c->si_code >=0 && trap_si_code != c->si_code)) { +smokey_warning ("%s: expected signal %d/%d once, " +"got signal %d/%d %d time(s)" , + c->name, c->signo, c->si_code, + trap_signo, trap_si_code, trap_signals); + ret =-1 ; + } + }else if (trap_signals) { +smokey_warning ("%s: unexpected signal %d/%d" , + c->name, trap_signo, trap_si_code); + ret =-1 ; + } + +if (trap_in_sigdebug) { +smokey_warning ("%s: signal %d/%d taken inside the SIGDEBUG " +"handler, ip=%#lx addr=%p" , c->name, trap_signo, + trap_si_code, trap_ip, trap_addr); + ret =-1 ; + } + +/* +* If the trap was not reported at all, the thread was relaxed +* by the watchdog or by our own syscall afterwards: SIGDEBUG +* and the mode switch count carry no information in this +* case, and the issue was flagged already. +*/ +if (reported) { +if (nr_sigdebug !=1 || + sigdebug_reasons[0 ] !=SIGDEBUG_MIGRATE_FAULT ) { +smokey_warning ("%s: expected one SIGDEBUG_MIGRATE_FAULT, " +"got %d SIGDEBUG(s), first reason %u (%s)" , + c->name, nr_sigdebug, + nr_sigdebug ? sigdebug_reasons[0 ] :0 , + nr_sigdebug ? +sigdebug_reason_name (sigdebug_reasons[0 ]) : +"-" ); +if (nr_sigdebug ==0 ) { +report_sigdebug_state (c->name); +smokey_warning ("%s: last fault: signal %d/%d " +"ip=%#lx addr=%p" , c->name, + trap_signo, trap_si_code, + trap_ip, trap_addr); + } + ret =-1 ; + } + + delta = after->stat.msw - before->stat.msw; +if (delta !=1 ) { +smokey_warning ("%s: %u mode switch(es), expected 1" , + c->name, delta); + ret =-1 ; + } + }else + ret =-1 ; + +for (trap =0 ; trap <NR_TRAPS ; trap++) { + delta = after->faults[trap] - before->faults[trap]; +if (delta != (trap == expected ?1 :0 )) { +smokey_warning ("%s: trap #%d (%s) accounted %u time(s), " +"expected %d%s" , c->name, trap, + trap_labels[trap], delta, + trap == expected ?1 :0 , + c->nested && trap == pagefault ? +" (nested page fault leaked?)" :"" ); + ret =-1 ; + } + } + +/* +* The core accounts any trap with the number of the page fault +* trap in the thread statistics. +*/ + delta = after->stat.pf - before->stat.pf; +if (delta != (expected == pagefault)) { +smokey_warning ("%s: %u page fault(s) accounted, expected %d" , + c->name, delta, expected == pagefault); + ret =-1 ; + } + +return ret; +} + +static int run_case (const struct trap_case *c,const char *prev_name) +{ +struct snapshot before, after; +bool reported; +int ret; + +if (!case_selected (c)) +return 0 ; + +if (trap_nr[c->trap] <0 ) { +smokey_note ("%s: skipped (trap unknown to the core)" , c->name); +return 0 ; + } + +if (c->prepare) { + skip_reason =NULL ; + ret = c->prepare (); +switch (ret) { +case 0 : +break ; +case -EPERM : +smokey_note ("%s: skipped (not root)" , c->name); + ret =0 ; + goto out; +case -ENOSYS : +smokey_note ("%s: skipped (%s)" , c->name, + skip_reason ?:"prerequisites not met" ); + ret =0 ; + goto out; +default : +smokey_warning ("%s: preparation failed (%s)" , + c->name,strerror (-ret)); + ret =-1 ; + goto out; + } + } + +smokey_trace ("%s" , c->name); + + ret =take_snapshot (&before); +if (ret) { +smokey_warning ("%s: snapshot failed (%s)" , c->name, +strerror (-ret)); + ret =-1 ; + goto out; + } + +/* +* No syscall may sneak in between the switch to primary mode +* and the trigger, so set up the jump buffer first (saving the +* signal mask is a syscall, fine at this point). Restoring the +* mask on longjmp matters: the fault handler may be entered +* while the SIGDEBUG handler is running, i.e. with SIGDEBUG +* blocked. longjmp leaves the altstack without further ado. +*/ +if (sigsetjmp (trap_env,1 ) ==0 ) { + ret =go_primary (); +if (ret) + goto out; + +reset_trap_state (); + c->trigger (); + } + +/* +* A reported trap left us in secondary mode. Still running in +* primary mode here means that either no trap was taken at +* all, or the kernel handled it without telling the core. +*/ + reported = !!(cobalt_thread_mode () &XNRELAX ); + +wait_for_sigdebug (); + +if (!reported) { +/* +* The syscall above relaxed us, so a fault signal the +* in-band fixup of an unreported trap queued has been +* delivered by now. None at all means the instruction +* did not trap in the first place. +*/ +if (c->signo && trap_signals ==0 ) { +smokey_note ("%s: skipped (instruction did not trap " +"on this CPU/emulator)" , c->name); + goto out; + } +smokey_warning ("%s: trap not reported to the core, " +"thread still in primary mode%s%s%s" , c->name, + prev_name ?" (notification left pending by " +"the preceding case '" :"" , + prev_name ?:"" , prev_name ?"'?)" :"" ); + } + +if (take_snapshot (&after)) { +smokey_warning ("%s: snapshot failed" , c->name); + ret =-1 ; + goto out; + } + +if (c->verify && c->verify ()) + ret =-1 ; + +if (check_case (c, &before, &after, reported)) + ret =-1 ; +out: +if (c->cleanup) + c->cleanup (); + +return ret; +} + +static int run_fork (void ) +{ +int status; +pid_t pid; + +if (!name_selected ("fork" )) +return 0 ; + +/* +* Fork from the thread which took all the traps above: with a +* debug kernel, an unbalanced trap entry is caught by the +* core at this point (inband_task_init()), check dmesg. +*/ +smokey_trace ("fork" ); + + pid =do_fork (); +if (pid <0 ) { +smokey_warning ("fork: %s" ,strerror (errno)); +return -1 ; + } +if (pid ==0 ) +_exit (0 ); + +if (waitpid (pid, &status,0 ) != pid || !WIFEXITED (status) || +WEXITSTATUS (status) !=0 ) { +smokey_warning ("fork: child did not exit cleanly" ); +return -1 ; + } + +return 0 ; +} + +static void *rt_thread (void *arg) +{ + static unsigned char altstack[ALTSTACK_SIZE] __attribute__((aligned(16))); +stack_t ss = { + .ss_sp = altstack, + .ss_size =sizeof (altstack), + }; +const struct trap_case *cases[MAX_CASES ]; +const char *prev_name =NULL ; +unsigned int i, n =0 ; +int ret; + +/* +* The fault handler must not run on the stack of the trapping +* context, which may be unusable (arm64 sp_align). +*/ +if (sigaltstack (&ss,NULL )) { +smokey_warning ("sigaltstack: %s" ,strerror (errno)); + failures++; +return NULL ; + } + + ret =pthread_setmode_np (0 ,PTHREAD_WARNSW ,NULL ); +if (ret) { +smokey_warning ("pthread_setmode_np: %s" ,strerror (ret)); + failures++; +return NULL ; + } + + cases[n++] = &pagefault_case; +for (i =0 ; i <ARRAY_SIZE (arch_cases) && n <MAX_CASES -2 ; i++) + cases[n++] = &arch_cases[i]; + cases[n++] = &kprobe_case; + cases[n++] = &pagefault_again_case; + +/* +* We are in primary mode now. The first Linux syscall below +* relaxes us with a SIGDEBUG_MIGRATE_SYSCALL, which we don't +* care about: the per-case accounting starts after the +* explicit switch to primary mode. +*/ +for (i =0 ; i < n; i++) { +if (!case_selected (cases[i])) +continue ; +if (run_case (cases[i], prev_name)) + failures++; + prev_name = cases[i]->name; + } + +if (run_fork ()) + failures++; + +return NULL ; +} + +static void setup_sighandlers (bool install) +{ +static struct sigaction old_sa[NSIG ]; +struct sigaction sa; +unsigned int i; + +if (!install) { +sigaction (SIGDEBUG , &old_sa[SIGDEBUG ],NULL ); +sigaction (SIGSEGV , &old_sa[SIGSEGV ],NULL ); +for (i =0 ; i <ARRAY_SIZE (arch_sigs); i++) +sigaction (arch_sigs[i], &old_sa[arch_sigs[i]],NULL ); +return ; + } + +memset (&sa,0 ,sizeof (sa)); +sigemptyset (&sa.sa_mask); + sa.sa_flags =SA_SIGINFO ; + sa.sa_sigaction = sigdebug_handler; +sigaction (SIGDEBUG , &sa, &old_sa[SIGDEBUG ]); + + sa.sa_flags =SA_SIGINFO |SA_NODEFER |SA_ONSTACK ; + sa.sa_sigaction = fault_handler; +sigaction (SIGSEGV , &sa, &old_sa[SIGSEGV ]); +for (i =0 ; i <ARRAY_SIZE (arch_sigs); i++) +sigaction (arch_sigs[i], &sa, &old_sa[arch_sigs[i]]); +} + +static int run_traps (struct smokey_test *t,int argc,char *const argv[]) +{ +struct sched_param param = { .sched_priority =1 }; +unsigned int faults[NR_TRAPS ]; +pthread_attr_t attr; +cpu_set_t cpus; +pthread_t tid; +int ret; + +smokey_parse_args (t, argc, argv); + + ret =read_faults (faults); +if (ret) { +smokey_note ("cannot read " FAULTS_PATH " (%s)" ,strerror (-ret)); +return -ENOSYS ; + } + +resolve_traps (); +if (trap_nr[TRAP_PAGEFAULT ] <0 ) { +smokey_note ("trap labels in " FAULTS_PATH " not recognized" ); +return -ENOSYS ; + } + + ret =setup_watchdog (); +if (ret) +return ret; + +arch_init (); + + ro_page =mmap (NULL ,sysconf (_SC_PAGESIZE),PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (ro_page ==MAP_FAILED ) { + ret = -errno; + goto out_arch; + } + *ro_page =0 ; + +mlockall (MCL_CURRENT |MCL_FUTURE ); + +setup_sighandlers (true); + +CPU_ZERO (&cpus); +CPU_SET (rt_cpu, &cpus); + +pthread_attr_init (&attr); +pthread_attr_setinheritsched (&attr,PTHREAD_EXPLICIT_SCHED ); +pthread_attr_setschedpolicy (&attr,SCHED_FIFO ); +pthread_attr_setschedparam (&attr, ¶m); +pthread_attr_setaffinity_np (&attr,sizeof (cpus), &cpus); + + ret =pthread_create (&tid, &attr, rt_thread,NULL ); +pthread_attr_destroy (&attr); +if (ret) { +smokey_warning ("pthread_create: %s" ,strerror (ret)); + ret = -ret; + goto out_sig; + } + +pthread_join (tid,NULL ); + + ret = failures ? -EFAULT :0 ; + +out_sig: +setup_sighandlers (false); +cleanup_kprobe (); +unmap_fresh_page (); +munmap (ro_page,sysconf (_SC_PAGESIZE)); +out_arch: +arch_cleanup (); +restore_watchdog (); + +return ret; +} + +#else /* unsupported architecture */ + +static int run_traps (struct smokey_test *t,int argc,char *const argv[]) +{ +smokey_note ("no trap cases implemented for this architecture" ); +return -ENOSYS ; +} + +#endif /* unsupported architecture */
Quoted 54 lines · Tobias Schaffner
Quoted 34 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
A thread running in primary mode which takes a synchronous exception must be reported to the Cobalt core by the Dovetail layer exactly once per trap, before the in-band kernel applies its fixup. Cobalt then relaxes the thread, accounts the trap and sends
SIGDEBUG_MIGRATE_FAULTwhen asked for. Only the page fault flavor of this was covered so far, by the sigdebug test.Add a test which triggers one specific trap per case from a primary mode thread and checks that the expected in-band fixup happened. Most traps for x86, arm, and arm64 are covered here. Left out are those that are practically irrelevant: AC for unaligned accesses on x86 and VFP exceptions
on ARM. Not yet covered is RISC-V, but it should be easy to add on top.See extensive AI generated explanation at the top of
traps.cfor more details.This test may become useful for checking future OBB trap reporting by extending it only slightly.
Assisted-by opencode:Fable-5.1/Opus-5.5 Signed-off-by: Jan Kiszka<[email protected]> ---
I know this is huge. Therefore, it is also something that we likely would not have done without LLM support - way too much effort for testing corner cases.
I went through it, but not in all details. The tests fail when they are expected to on unfixed kernels, they apparently pass on fixed one, at least on our CI hardware. "Should be OK"...
Quoted 5 lines
BTW, if we merge this, we implicitly document parts of our so far unwritten
AI policy ("yes, we take generated code"). I still need to think about which of the templates out there to use best, then I will propose one.Hi Jan,
Following your input, I spent some time last week on RISC-V trap handling in EVL. To verify the proposed patches, I ended up with a similar trap matrix test generated for EVL.
With the number of fixes we can now produce with AI support, I think it is right, and important, to put more time into test infrastructure. I think it makes sense to pin down in more detail how Dovetail and the co-kernels built on top are supposed to behave to make sure this is not changed by AI without us noticing when these tools are trusted more and more.
Quoted 6 lines
I am asking myself if we want to keep some kind of human readable source of truth that tells us how this is supposed to work. If we start generating both sides and both sides are hard to understand (at least for me, the kernel side still is) I feel like it will be hard to notice if the tools start to slowly change the design.
Good point. I'm currently fairly impressed how much the models understood about the current dovetail architecture by just reading the kernel patches. Maybe they consumed some documents upfront, but definitely not while answering prompts.
That said, we more guardrails, also for models that might be cheaper, more sovereign (local open-weight...), but also less smart could be useful. Question remains how to encode things best to have both side understand the same.
Quoted 4 lines
In addition, I feel like most of the tests are Dovetail functionality tests and not specific to cobalt/EVL. I think we should at least try to find a way to tackle this with joined forces for cobalt, EVL and what ever other co-kernel using the infrastructure.
Yeah, valid point. Maybe the availability of a POSIX interface for EVL will once allow to write more portable test cases or even share the same code base.
Quoted 35 lines
I think that RV can maybe help here. It would allow us to decouple some of the checks from the triggering of the test cases. Triggering could be in the co-kernel projects while most of the verification lives in Dovetail.
I created a POC of a more detailed trap monitor paired with a matrix test that solely triggers the traps from the different contexts in cobalt here:
https://github.com/TobiasSchaffner/linux/commits/tobsch/trap-monitor-
rfc-proposal-nofixes/Asking Opus 5.5 about a comparison to your proposed RFC it tells me:
``` Not covered: - SIGDEBUG with SIGDEBUG_MIGRATE_FAULT, exactly once. That's Cobalt's notification to the application, and we don't check it. - The counters in /proc/xenomai/faults: the right trap +1, all others unchanged. That's Cobalt's accounting. - The exact mode-switch count (Cobalt's msw statistics). On top of the RFC, it covers (checks no user-space test can see): - the handler level of the unwind, through the stack frame; - irqs off on trap exit; - demotion only from task context; - no IRQ in NMI context; - no sched-out during a kprobe; - the time limit. - more fault cases: read-null, write-ro, write-retry, exec-nx, bus-eof, fresh, fork-write, kernel-fault, kernel-fresh, misaligned. ```The output makes sense cause the POC has no cobalt API specific checks anymore while the monitor adds more kernel internal checks.
Quoted 7 lines
The hole approach Monitor + Triggering matrix is smaller than your test while the monitor part could be reused. A rendering of the monitor is attached.
Maybe you want to consider it or spend some tokens for further analysis if this can be valuable.
"Smaller" triggers me for sure! Would be worth to see the outcome in code. And we may still consider addressing potential gaps on top in the userland. Key is that the userland test orchestrator and stimulus generator and also read back the result of the RV monitor run.
But what might be more challenging: we would have to backport your infrastructure to all kernels we are maintaining. Is that realistic? At least down to 5.10?
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 124 lines · Jan Kiszka
Quoted 54 lines · Tobias Schaffner
Quoted 34 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
A thread running in primary mode which takes a synchronous exception must be reported to the Cobalt core by the Dovetail layer exactly once per trap, before the in-band kernel applies its fixup. Cobalt then relaxes the thread, accounts the trap and sends
SIGDEBUG_MIGRATE_FAULTwhen asked for. Only the page fault flavor of this was covered so far, by the sigdebug test.Add a test which triggers one specific trap per case from a primary mode thread and checks that the expected in-band fixup happened. Most traps for x86, arm, and arm64 are covered here. Left out are those that are practically irrelevant: AC for unaligned accesses on x86 and VFP exceptions
on ARM. Not yet covered is RISC-V, but it should be easy to add on top.See extensive AI generated explanation at the top of
traps.cfor more details.This test may become useful for checking future OBB trap reporting by extending it only slightly.
Assisted-by opencode:Fable-5.1/Opus-5.5 Signed-off-by: Jan Kiszka<[email protected]> ---
I know this is huge. Therefore, it is also something that we likely would not have done without LLM support - way too much effort for testing corner cases.
I went through it, but not in all details. The tests fail when they are expected to on unfixed kernels, they apparently pass on fixed one, at least on our CI hardware. "Should be OK"...
Quoted 5 lines
BTW, if we merge this, we implicitly document parts of our so far unwritten
AI policy ("yes, we take generated code"). I still need to think about which of the templates out there to use best, then I will propose one.Hi Jan,
Following your input, I spent some time last week on RISC-V trap handling in EVL. To verify the proposed patches, I ended up with a similar trap matrix test generated for EVL.
With the number of fixes we can now produce with AI support, I think it is right, and important, to put more time into test infrastructure. I think it makes sense to pin down in more detail how Dovetail and the co-kernels built on top are supposed to behave to make sure this is not changed by AI without us noticing when these tools are trusted more and more.
Quoted 6 lines
I am asking myself if we want to keep some kind of human readable source of truth that tells us how this is supposed to work. If we start generating both sides and both sides are hard to understand (at least for me, the kernel side still is) I feel like it will be hard to notice if the tools start to slowly change the design.
Good point. I'm currently fairly impressed how much the models understood about the current dovetail architecture by just reading the kernel patches. Maybe they consumed some documents upfront, but definitely not while answering prompts.
That said, we more guardrails, also for models that might be cheaper, more sovereign (local open-weight...), but also less smart could be useful. Question remains how to encode things best to have both side understand the same.
Quoted 4 lines
In addition, I feel like most of the tests are Dovetail functionality tests and not specific to cobalt/EVL. I think we should at least try to find a way to tackle this with joined forces for cobalt, EVL and what ever other co-kernel using the infrastructure.
Yeah, valid point. Maybe the availability of a POSIX interface for EVL will once allow to write more portable test cases or even share the same code base.
Quoted 35 lines
I think that RV can maybe help here. It would allow us to decouple some of the checks from the triggering of the test cases. Triggering could be in the co-kernel projects while most of the verification lives in Dovetail.
I created a POC of a more detailed trap monitor paired with a matrix test that solely triggers the traps from the different contexts in cobalt here:
https://github.com/TobiasSchaffner/linux/commits/tobsch/trap-monitor-
rfc-proposal-nofixes/Asking Opus 5.5 about a comparison to your proposed RFC it tells me:
``` Not covered: - SIGDEBUG with SIGDEBUG_MIGRATE_FAULT, exactly once. That's Cobalt's notification to the application, and we don't check it. - The counters in /proc/xenomai/faults: the right trap +1, all others unchanged. That's Cobalt's accounting. - The exact mode-switch count (Cobalt's msw statistics). On top of the RFC, it covers (checks no user-space test can see): - the handler level of the unwind, through the stack frame; - irqs off on trap exit; - demotion only from task context; - no IRQ in NMI context; - no sched-out during a kprobe; - the time limit. - more fault cases: read-null, write-ro, write-retry, exec-nx, bus-eof, fresh, fork-write, kernel-fault, kernel-fresh, misaligned. ```The output makes sense cause the POC has no cobalt API specific checks anymore while the monitor adds more kernel internal checks.
Quoted 7 lines
The hole approach Monitor + Triggering matrix is smaller than your test while the monitor part could be reused. A rendering of the monitor is attached.
Maybe you want to consider it or spend some tokens for further analysis if this can be valuable.
Quoted 8 lines
"Smaller" triggers me for sure! Would be worth to see the outcome in code. And we may still consider addressing potential gaps on top in the userland. Key is that the userland test orchestrator and stimulus generator and also read back the result of the RV monitor run.
But what might be more challenging: we would have to backport your infrastructure to all kernels we are maintaining. Is that realistic? At least down to 5.10?
Hmm... I did not have this on my radar... The RV subsystem was merged in 6.0 and the Hybrid Automata that I build on was introduced in 7.1. I think that back porting the whole subsystem as well as all the needed tracepoints would be a huge lift.
Quoted 2 lines
Jan
Quoted 130 lines · Tobias Schaffner
Quoted 129 lines · Jan Kiszka
Quoted 54 lines · Tobias Schaffner
Quoted 34 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
A thread running in primary mode which takes a synchronous exception must be reported to the Cobalt core by the Dovetail layer exactly once per trap, before the in-band kernel applies its fixup. Cobalt then relaxes the thread, accounts the trap and sends
SIGDEBUG_MIGRATE_FAULTwhen asked for. Only the page fault flavor of this was covered so far, by the sigdebug test.Add a test which triggers one specific trap per case from a primary mode thread and checks that the expected in-band fixup happened. Most traps for x86, arm, and arm64 are covered here. Left out are those that are practically irrelevant: AC for unaligned accesses on x86 and VFP exceptions
on ARM. Not yet covered is RISC-V, but it should be easy to add on top.See extensive AI generated explanation at the top of
traps.cfor more details.This test may become useful for checking future OBB trap reporting by extending it only slightly.
Assisted-by opencode:Fable-5.1/Opus-5.5 Signed-off-by: Jan Kiszka<[email protected]> ---
I know this is huge. Therefore, it is also something that we likely would not have done without LLM support - way too much effort for testing corner cases.
I went through it, but not in all details. The tests fail when they are expected to on unfixed kernels, they apparently pass on fixed one, at least on our CI hardware. "Should be OK"...
Quoted 5 lines
BTW, if we merge this, we implicitly document parts of our so far unwritten
AI policy ("yes, we take generated code"). I still need to think about which of the templates out there to use best, then I will propose one.Hi Jan,
Following your input, I spent some time last week on RISC-V trap handling in EVL. To verify the proposed patches, I ended up with a similar trap matrix test generated for EVL.
With the number of fixes we can now produce with AI support, I think it is right, and important, to put more time into test infrastructure. I think it makes sense to pin down in more detail how Dovetail and the co-kernels built on top are supposed to behave to make sure this is not changed by AI without us noticing when these tools are trusted more and more.
Quoted 6 lines
I am asking myself if we want to keep some kind of human readable source of truth that tells us how this is supposed to work. If we start generating both sides and both sides are hard to understand (at least for me, the kernel side still is) I feel like it will be hard to notice if the tools start to slowly change the design.
Good point. I'm currently fairly impressed how much the models understood about the current dovetail architecture by just reading the kernel patches. Maybe they consumed some documents upfront, but definitely not while answering prompts.
That said, we more guardrails, also for models that might be cheaper, more sovereign (local open-weight...), but also less smart could be useful. Question remains how to encode things best to have both side understand the same.
Quoted 4 lines
In addition, I feel like most of the tests are Dovetail functionality tests and not specific to cobalt/EVL. I think we should at least try to find a way to tackle this with joined forces for cobalt, EVL and what ever other co-kernel using the infrastructure.
Yeah, valid point. Maybe the availability of a POSIX interface for EVL will once allow to write more portable test cases or even share the same code base.
Quoted 35 lines
I think that RV can maybe help here. It would allow us to decouple some of the checks from the triggering of the test cases. Triggering could be in the co-kernel projects while most of the verification lives in Dovetail.
I created a POC of a more detailed trap monitor paired with a matrix test that solely triggers the traps from the different contexts in cobalt here:
https://github.com/TobiasSchaffner/linux/commits/tobsch/trap-monitor-
rfc-proposal-nofixes/Asking Opus 5.5 about a comparison to your proposed RFC it tells me:
``` Not covered: - SIGDEBUG with SIGDEBUG_MIGRATE_FAULT, exactly once. That's Cobalt's notification to the application, and we don't check it. - The counters in /proc/xenomai/faults: the right trap +1, all others unchanged. That's Cobalt's accounting. - The exact mode-switch count (Cobalt's msw statistics). On top of the RFC, it covers (checks no user-space test can see): - the handler level of the unwind, through the stack frame; - irqs off on trap exit; - demotion only from task context; - no IRQ in NMI context; - no sched-out during a kprobe; - the time limit. - more fault cases: read-null, write-ro, write-retry, exec-nx, bus-eof, fresh, fork-write, kernel-fault, kernel-fresh, misaligned. ```The output makes sense cause the POC has no cobalt API specific checks anymore while the monitor adds more kernel internal checks.
Quoted 7 lines
The hole approach Monitor + Triggering matrix is smaller than your test while the monitor part could be reused. A rendering of the monitor is attached.
Maybe you want to consider it or spend some tokens for further analysis if this can be valuable.
"Smaller" triggers me for sure! Would be worth to see the outcome in code. And we may still consider addressing potential gaps on top in the userland. Key is that the userland test orchestrator and stimulus generator and also read back the result of the RV monitor run.
Quoted 10 lines
Quoted 3 lines
But what might be more challenging: we would have to backport your infrastructure to all kernels we are maintaining. Is that realistic? At least down to 5.10?
Hmm... I did not have this on my radar... The RV subsystem was merged in 6.0 and the Hybrid Automata that I build on was introduced in 7.1. I think that back porting the whole subsystem as well as all the needed tracepoints would be a huge lift.
The Linux RV monitor is usable since 7.3, before that it simply had to much overhead when compiled in, even if not being used. With 7.3 you can build enable it without any impact unless you start using it.
That said, I don't think that we should backport that at all. If someone is interested in validation => use a modern kernel.
Quoted 142 lines · Florian Bezdeka
Quoted 130 lines · Tobias Schaffner
Quoted 129 lines · Jan Kiszka
Quoted 54 lines · Tobias Schaffner
Quoted 34 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
A thread running in primary mode which takes a synchronous exception must be reported to the Cobalt core by the Dovetail layer exactly once per trap, before the in-band kernel applies its fixup. Cobalt then relaxes the thread, accounts the trap and sends
SIGDEBUG_MIGRATE_FAULTwhen asked for. Only the page fault flavor of this was covered so far, by the sigdebug test.Add a test which triggers one specific trap per case from a primary mode thread and checks that the expected in-band fixup happened. Most traps for x86, arm, and arm64 are covered here. Left out are those that are practically irrelevant: AC for unaligned accesses on x86 and VFP exceptions
on ARM. Not yet covered is RISC-V, but it should be easy to add on top.See extensive AI generated explanation at the top of
traps.cfor more details.This test may become useful for checking future OBB trap reporting by extending it only slightly.
Assisted-by opencode:Fable-5.1/Opus-5.5 Signed-off-by: Jan Kiszka<[email protected]> ---
I know this is huge. Therefore, it is also something that we likely would not have done without LLM support - way too much effort for testing corner cases.
I went through it, but not in all details. The tests fail when they are expected to on unfixed kernels, they apparently pass on fixed one, at least on our CI hardware. "Should be OK"...
Quoted 5 lines
BTW, if we merge this, we implicitly document parts of our so far unwritten
AI policy ("yes, we take generated code"). I still need to think about which of the templates out there to use best, then I will propose one.Hi Jan,
Following your input, I spent some time last week on RISC-V trap handling in EVL. To verify the proposed patches, I ended up with a similar trap matrix test generated for EVL.
With the number of fixes we can now produce with AI support, I think it is right, and important, to put more time into test infrastructure. I think it makes sense to pin down in more detail how Dovetail and the co-kernels built on top are supposed to behave to make sure this is not changed by AI without us noticing when these tools are trusted more and more.
Quoted 6 lines
I am asking myself if we want to keep some kind of human readable source of truth that tells us how this is supposed to work. If we start generating both sides and both sides are hard to understand (at least for me, the kernel side still is) I feel like it will be hard to notice if the tools start to slowly change the design.
Good point. I'm currently fairly impressed how much the models understood about the current dovetail architecture by just reading the kernel patches. Maybe they consumed some documents upfront, but definitely not while answering prompts.
That said, we more guardrails, also for models that might be cheaper, more sovereign (local open-weight...), but also less smart could be useful. Question remains how to encode things best to have both side understand the same.
Quoted 4 lines
In addition, I feel like most of the tests are Dovetail functionality tests and not specific to cobalt/EVL. I think we should at least try to find a way to tackle this with joined forces for cobalt, EVL and what ever other co-kernel using the infrastructure.
Yeah, valid point. Maybe the availability of a POSIX interface for EVL will once allow to write more portable test cases or even share the same code base.
Quoted 35 lines
I think that RV can maybe help here. It would allow us to decouple some of the checks from the triggering of the test cases. Triggering could be in the co-kernel projects while most of the verification lives in Dovetail.
I created a POC of a more detailed trap monitor paired with a matrix test that solely triggers the traps from the different contexts in cobalt here:
https://github.com/TobiasSchaffner/linux/commits/tobsch/trap-monitor-
rfc-proposal-nofixes/Asking Opus 5.5 about a comparison to your proposed RFC it tells me:
``` Not covered: - SIGDEBUG with SIGDEBUG_MIGRATE_FAULT, exactly once. That's Cobalt's notification to the application, and we don't check it. - The counters in /proc/xenomai/faults: the right trap +1, all others unchanged. That's Cobalt's accounting. - The exact mode-switch count (Cobalt's msw statistics). On top of the RFC, it covers (checks no user-space test can see): - the handler level of the unwind, through the stack frame; - irqs off on trap exit; - demotion only from task context; - no IRQ in NMI context; - no sched-out during a kprobe; - the time limit. - more fault cases: read-null, write-ro, write-retry, exec-nx, bus-eof, fresh, fork-write, kernel-fault, kernel-fresh, misaligned. ```The output makes sense cause the POC has no cobalt API specific checks anymore while the monitor adds more kernel internal checks.
Quoted 7 lines
The hole approach Monitor + Triggering matrix is smaller than your test while the monitor part could be reused. A rendering of the monitor is attached.
Maybe you want to consider it or spend some tokens for further analysis if this can be valuable.
"Smaller" triggers me for sure! Would be worth to see the outcome in code. And we may still consider addressing potential gaps on top in the userland. Key is that the userland test orchestrator and stimulus generator and also read back the result of the RV monitor run.
Quoted 10 lines
Quoted 3 lines
But what might be more challenging: we would have to backport your infrastructure to all kernels we are maintaining. Is that realistic? At least down to 5.10?
Hmm... I did not have this on my radar... The RV subsystem was merged in 6.0 and the Hybrid Automata that I build on was introduced in 7.1. I think that back porting the whole subsystem as well as all the needed tracepoints would be a huge lift.
Quoted 3 lines
The Linux RV monitor is usable since 7.3, before that it simply had to much overhead when compiled in, even if not being used. With 7.3 you can build enable it without any impact unless you start using it.
Do you have any source or measurement results that show this? This
statement does not fit to what I know about RV and contradicts my
measurement results. RV has always been very cheap when running and close to free when it's not.
Quoted 2 lines
That said, I don't think that we should backport that at all. If someone is interested in validation => use a modern kernel.
Quoted 143 lines · Tobias Schaffner
Quoted 142 lines · Florian Bezdeka
Quoted 140 lines · Tobias Schaffner
Quoted 124 lines · Jan Kiszka
Quoted 54 lines · Tobias Schaffner
Quoted 34 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
A thread running in primary mode which takes a synchronous exception must be reported to the Cobalt core by the Dovetail layer exactly once per trap, before the in-band kernel applies its fixup. Cobalt then relaxes the thread, accounts the trap and sends
SIGDEBUG_MIGRATE_FAULTwhen asked for. Only the page fault flavor of this was covered so far, by the sigdebug test.Add a test which triggers one specific trap per case from a primary mode thread and checks that the expected in-band fixup happened. Most traps for x86, arm, and arm64 are covered here. Left out are those that are practically irrelevant: AC for unaligned accesses on x86 and VFP exceptions
on ARM. Not yet covered is RISC-V, but it should be easy to add on top.See extensive AI generated explanation at the top of
traps.cfor more details.This test may become useful for checking future OBB trap reporting by extending it only slightly.
Assisted-by opencode:Fable-5.1/Opus-5.5 Signed-off-by: Jan Kiszka<[email protected]> ---
I know this is huge. Therefore, it is also something that we likely would not have done without LLM support - way too much effort for testing corner cases.
I went through it, but not in all details. The tests fail when they are expected to on unfixed kernels, they apparently pass on fixed one, at least on our CI hardware. "Should be OK"...
Quoted 5 lines
BTW, if we merge this, we implicitly document parts of our so far unwritten
AI policy ("yes, we take generated code"). I still need to think about which of the templates out there to use best, then I will propose one.Hi Jan,
Following your input, I spent some time last week on RISC-V trap handling in EVL. To verify the proposed patches, I ended up with a similar trap matrix test generated for EVL.
With the number of fixes we can now produce with AI support, I think it is right, and important, to put more time into test infrastructure. I think it makes sense to pin down in more detail how Dovetail and the co-kernels built on top are supposed to behave to make sure this is not changed by AI without us noticing when these tools are trusted more and more.
Quoted 6 lines
I am asking myself if we want to keep some kind of human readable source of truth that tells us how this is supposed to work. If we start generating both sides and both sides are hard to understand (at least for me, the kernel side still is) I feel like it will be hard to notice if the tools start to slowly change the design.
Good point. I'm currently fairly impressed how much the models understood about the current dovetail architecture by just reading the kernel patches. Maybe they consumed some documents upfront, but definitely not while answering prompts.
That said, we more guardrails, also for models that might be cheaper, more sovereign (local open-weight...), but also less smart could be useful. Question remains how to encode things best to have both side understand the same.
Quoted 4 lines
In addition, I feel like most of the tests are Dovetail functionality tests and not specific to cobalt/EVL. I think we should at least try to find a way to tackle this with joined forces for cobalt, EVL and what ever other co-kernel using the infrastructure.
Yeah, valid point. Maybe the availability of a POSIX interface for EVL will once allow to write more portable test cases or even share the same code base.
Quoted 35 lines
I think that RV can maybe help here. It would allow us to decouple some of the checks from the triggering of the test cases. Triggering could be in the co-kernel projects while most of the verification lives in Dovetail.
I created a POC of a more detailed trap monitor paired with a matrix test that solely triggers the traps from the different contexts in cobalt here:
https://github.com/TobiasSchaffner/linux/commits/tobsch/trap-monitor-
rfc-proposal-nofixes/Asking Opus 5.5 about a comparison to your proposed RFC it tells me:
``` Not covered: - SIGDEBUG with SIGDEBUG_MIGRATE_FAULT, exactly once. That's Cobalt's notification to the application, and we don't check it. - The counters in /proc/xenomai/faults: the right trap +1, all others unchanged. That's Cobalt's accounting. - The exact mode-switch count (Cobalt's msw statistics). On top of the RFC, it covers (checks no user-space test can see): - the handler level of the unwind, through the stack frame; - irqs off on trap exit; - demotion only from task context; - no IRQ in NMI context; - no sched-out during a kprobe; - the time limit. - more fault cases: read-null, write-ro, write-retry, exec-nx, bus-eof, fresh, fork-write, kernel-fault, kernel-fresh, misaligned. ```The output makes sense cause the POC has no cobalt API specific checks anymore while the monitor adds more kernel internal checks.
Quoted 7 lines
The hole approach Monitor + Triggering matrix is smaller than your test while the monitor part could be reused. A rendering of the monitor is attached.
Maybe you want to consider it or spend some tokens for further analysis if this can be valuable.
Quoted 8 lines
"Smaller" triggers me for sure! Would be worth to see the outcome in code. And we may still consider addressing potential gaps on top in the userland. Key is that the userland test orchestrator and stimulus generator and also read back the result of the RV monitor run.
But what might be more challenging: we would have to backport your infrastructure to all kernels we are maintaining. Is that realistic? At least down to 5.10?
Hmm... I did not have this on my radar... The RV subsystem was merged in 6.0 and the Hybrid Automata that I build on was introduced in 7.1. I think that back porting the whole subsystem as well as all the needed tracepoints would be a huge lift.
Quoted 8 lines
Quoted 3 lines
The Linux RV monitor is usable since 7.3, before that it simply had to much overhead when compiled in, even if not being used. With 7.3 you can build enable it without any impact unless you start using it.
Do you have any source or measurement results that show this? This statement does not fit to what I know about RV and contradicts my measurement results. RV has always been very cheap when running and close to free when it's not.
Close to free is not acceptable for production systems. Depending on the workload you could easily run into problems (pre 7.3).
My own experiments (especially with the RV monitor), the discussions we had at plumbers this year and especially John's talk about the RV at the RT user forum in Prague (see
https://www.youtube.com/watch?v=jO4M5dx52Vw) would be he references you are looking for.
Quoted 153 lines · Florian Bezdeka
Quoted 143 lines · Tobias Schaffner
Quoted 142 lines · Florian Bezdeka
Quoted 140 lines · Tobias Schaffner
Quoted 124 lines · Jan Kiszka
Quoted 54 lines · Tobias Schaffner
Quoted 34 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
A thread running in primary mode which takes a synchronous exception must be reported to the Cobalt core by the Dovetail layer exactly once per trap, before the in-band kernel applies its fixup. Cobalt then relaxes the thread, accounts the trap and sends
SIGDEBUG_MIGRATE_FAULTwhen asked for. Only the page fault flavor of this was covered so far, by the sigdebug test.Add a test which triggers one specific trap per case from a primary mode thread and checks that the expected in-band fixup happened. Most traps for x86, arm, and arm64 are covered here. Left out are those that are practically irrelevant: AC for unaligned accesses on x86 and VFP exceptions
on ARM. Not yet covered is RISC-V, but it should be easy to add on top.See extensive AI generated explanation at the top of
traps.cfor more details.This test may become useful for checking future OBB trap reporting by extending it only slightly.
Assisted-by opencode:Fable-5.1/Opus-5.5 Signed-off-by: Jan Kiszka<[email protected]> ---
I know this is huge. Therefore, it is also something that we likely would not have done without LLM support - way too much effort for testing corner cases.
I went through it, but not in all details. The tests fail when they are expected to on unfixed kernels, they apparently pass on fixed one, at least on our CI hardware. "Should be OK"...
Quoted 5 lines
BTW, if we merge this, we implicitly document parts of our so far unwritten
AI policy ("yes, we take generated code"). I still need to think about which of the templates out there to use best, then I will propose one.Hi Jan,
Following your input, I spent some time last week on RISC-V trap handling in EVL. To verify the proposed patches, I ended up with a similar trap matrix test generated for EVL.
With the number of fixes we can now produce with AI support, I think it is right, and important, to put more time into test infrastructure. I think it makes sense to pin down in more detail how Dovetail and the co-kernels built on top are supposed to behave to make sure this is not changed by AI without us noticing when these tools are trusted more and more.
Quoted 6 lines
I am asking myself if we want to keep some kind of human readable source of truth that tells us how this is supposed to work. If we start generating both sides and both sides are hard to understand (at least for me, the kernel side still is) I feel like it will be hard to notice if the tools start to slowly change the design.
Good point. I'm currently fairly impressed how much the models understood about the current dovetail architecture by just reading the kernel patches. Maybe they consumed some documents upfront, but definitely not while answering prompts.
That said, we more guardrails, also for models that might be cheaper, more sovereign (local open-weight...), but also less smart could be useful. Question remains how to encode things best to have both side understand the same.
Quoted 4 lines
In addition, I feel like most of the tests are Dovetail functionality tests and not specific to cobalt/EVL. I think we should at least try to find a way to tackle this with joined forces for cobalt, EVL and what ever other co-kernel using the infrastructure.
Yeah, valid point. Maybe the availability of a POSIX interface for EVL will once allow to write more portable test cases or even share the same code base.
Quoted 35 lines
I think that RV can maybe help here. It would allow us to decouple some of the checks from the triggering of the test cases. Triggering could be in the co-kernel projects while most of the verification lives in Dovetail.
I created a POC of a more detailed trap monitor paired with a matrix test that solely triggers the traps from the different contexts in cobalt here:
https://github.com/TobiasSchaffner/linux/commits/tobsch/trap-monitor-
rfc-proposal-nofixes/Asking Opus 5.5 about a comparison to your proposed RFC it tells me:
``` Not covered: - SIGDEBUG with SIGDEBUG_MIGRATE_FAULT, exactly once. That's Cobalt's notification to the application, and we don't check it. - The counters in /proc/xenomai/faults: the right trap +1, all others unchanged. That's Cobalt's accounting. - The exact mode-switch count (Cobalt's msw statistics). On top of the RFC, it covers (checks no user-space test can see): - the handler level of the unwind, through the stack frame; - irqs off on trap exit; - demotion only from task context; - no IRQ in NMI context; - no sched-out during a kprobe; - the time limit. - more fault cases: read-null, write-ro, write-retry, exec-nx, bus-eof, fresh, fork-write, kernel-fault, kernel-fresh, misaligned. ```The output makes sense cause the POC has no cobalt API specific checks anymore while the monitor adds more kernel internal checks.
Quoted 7 lines
The hole approach Monitor + Triggering matrix is smaller than your test while the monitor part could be reused. A rendering of the monitor is attached.
Maybe you want to consider it or spend some tokens for further analysis if this can be valuable.
Quoted 8 lines
"Smaller" triggers me for sure! Would be worth to see the outcome in code. And we may still consider addressing potential gaps on top in the userland. Key is that the userland test orchestrator and stimulus generator and also read back the result of the RV monitor run.
But what might be more challenging: we would have to backport your infrastructure to all kernels we are maintaining. Is that realistic? At least down to 5.10?
Hmm... I did not have this on my radar... The RV subsystem was merged in 6.0 and the Hybrid Automata that I build on was introduced in 7.1. I think that back porting the whole subsystem as well as all the needed tracepoints would be a huge lift.
Quoted 8 lines
Quoted 3 lines
The Linux RV monitor is usable since 7.3, before that it simply had to much overhead when compiled in, even if not being used. With 7.3 you can build enable it without any impact unless you start using it.
Do you have any source or measurement results that show this? This statement does not fit to what I know about RV and contradicts my measurement results. RV has always been very cheap when running and close to free when it's not.
Quoted 8 lines
Close to free is not acceptable for production systems. Depending on the workload you could easily run into problems (pre 7.3).
My own experiments (especially with the RV monitor), the discussions we had at plumbers this year and especially John's talk about the RV at the RT user forum in Prague (see
https://www.youtube.com/watch?v=jO4M5dx52Vw) would be he references you are looking for.
1. Nobody is talking about using this in production. RV was proposed as a testing tool in this project.
2. John is talking about the rtapp RV monitor when talking about overhead in his talk not about RV in general. Are you mixing these two up?
3. The improvements that have been merged into RV between 7.2 and 7.3 are not measurable in my setups. Having a more sophisticated HA monitor in 7.2 enabled adds about 1us of overhead in my tests. That is not "unusable" from my point of view.
For reference. These are my numbers of last Friday when evaluating the matrix test and trap monitor on the visionfive2 10 minutes each measured with latmus in us.
┌───────────────────────────────┬─────────┬──────┬──────┐ │ Scenario │ Monitor │ avg │ max │ ├───────────────────────────────┼─────────┼──────┼──────┤ │ baseline │ off │ 0.27 │ 47.8 │ ├───────────────────────────────┼─────────┼──────┼──────┤ │ baseline │ on │ 0.85 │ 46.1 │ ├───────────────────────────────┼─────────┼──────┼──────┤ │ matrix without kprobes, CPU 2 │ off │ 5.83 │ 51.9 │ ├───────────────────────────────┼─────────┼──────┼──────┤ │ matrix without kprobes, CPU 2 │ on │ 6.94 │ 51.8 │ ├───────────────────────────────┼─────────┼──────┼──────┤ │ full matrix, CPU 3 │ off │ 0.34 │ 48.1 │ ├───────────────────────────────┼─────────┼──────┼──────┤ │ full matrix, CPU 3 │ on │ 1.04 │ 49.1 │ └───────────────────────────────┴─────────┴──────┴──────┘
Quoted 11 lines · Tobias Schaffner
Quoted 8 lines
Close to free is not acceptable for production systems. Depending on the workload you could easily run into problems (pre 7.3).
My own experiments (especially with the RV monitor), the discussions we had at plumbers this year and especially John's talk about the RV at the RT user forum in Prague (see
https://www.youtube.com/watch?v=jO4M5dx52Vw) would be he references you are looking for.1. Nobody is talking about using this in production. RV was proposed as a testing tool in this project.
Why not? Something similar to the rtapp RV monitor (as we have it for PREEMPT_RT meanwhile) would be very valuable.
You compile it in, with zero impact, and enable it once you ran into a problem.
Quoted 3 lines
2. John is talking about the rtapp RV monitor when talking about overhead in his talk not about RV in general. Are you mixing these two up?
Maybe to some degree, but the infrastructure behind is the same.
Quoted 4 lines
3. The improvements that have been merged into RV between 7.2 and 7.3 are not measurable in my setups. Having a more sophisticated HA monitor in 7.2 enabled adds about 1us of overhead in my tests. That is not "unusable" from my point of view.
That highly depends on the workload and the scenario. Which non-rt workloads are you running in parallel? Networking? Containers? GPU? That is where the most challenges come in.
The rtapp RV monitor has made significant progress in 7.3 in this regard already. Other parts of the RV infrastructure were discussed but that will take some more cycles to arrive.