From: Matheus <matheus@astral-os.org>
To: astral-devel@lists.astral-os.org
Cc: Matheus <matheusuky@gmail.com>
Subject: [PATCH 0/4] kcov: initial implementation
Date: Tue, 6 Oct 2026 11:46:31 -0300 [thread overview]
Message-ID: <20261006144635.141071-1-matheus@astral-os.org> (raw)
From: Matheus <matheusuky@gmail.com>
Implement a kernel coverage system and necessary fixes.
The coverage device works roughly like this:
1. A thread opens "/dev/kcov", allocating a new kcov object and attaching
it to the thread. The initial state of this kcov is no buffer and not
running. This also creates a vnode that represents this kcov object,
and can be used to map it to memory/control it from userspace.
2. The thread creates a buffer for it using the SET_SIZE ioctl.
3. The thread maps the buffer using mmap()
4. Before executing the code that needs to profiled, the thread will set
the first 64-bit word of the buffer to 0 and start it using an ioctl
with either the PC or CMP mode.
5. After execution, the program can use the data in the buffer to
determine which paths were taken in the kernel
A kcov buffer is an array of 64-bit words with the following format:
[(1) entry count][(X) data entries]
The format of the data entries varies by mode:
PC mode: Array of 1-word entries, those being the return PC after the
callbacks added by the GCC coverage sanitizer along the code
CMP mode: Array of 4-word entries. With format
[FLAGS][V1][V2][PC]
- FLAGS:
- Bit 0: If the comparison contains a compile time constant.
- Bit 1,2: Whether it is a 1 byte (0), 2 bytes (1),
4 bytes (2) or 8 bytes (3) comparison
- V1: First operand
- V2: Second operand
- PC: return PC from the GCC coverage sanitizer callback
The code expects a thread will act on its own kcov object and thus
accessing another thread's kcov object through their vnode is not
supported outside of faulting-in/unmapping buffers.
Support for this also needed a few fixes around the kernel:
- This is the second character device which mmaps memory, and it has a
much more complex buffer lifetime than /dev/fb*, which meant shared
device file mappings had to properly be handled.
- IRQ depth accounting needed to be added to prevent IRQs from
appearing in the coverage.
- TLB shootdown was changed to be more like an IRQ, as it would
otherwise affect coverage.
Matheus (4):
mm: properly fork shared file mappings
cpu: add irq depth counting
mmu: move tlb shootdown to IPL_MAX
kcov: initial implementation
kernel-src/Makefile | 4 +
kernel-src/arch/x86-64/context.c | 2 +-
kernel-src/arch/x86-64/cpu.c | 6 +-
kernel-src/arch/x86-64/mmu.c | 2 +-
kernel-src/arch/x86-64/smp.c | 5 +-
kernel-src/include/kernel/kcov.h | 43 +++++
kernel-src/include/kernel/thread.h | 4 +
kernel-src/include/x86-64/arch/cpu.h | 22 ++-
kernel-src/include/x86-64/arch/msr.h | 5 +-
kernel-src/main.c | 2 +-
kernel-src/mm/mm.c | 26 ++-
kernel-src/sys/interrupt.c | 38 ++--
kernel-src/sys/kcov.c | 129 ++++++++++++++
kernel-src/sys/kcov_dev.c | 255 +++++++++++++++++++++++++++
kernel-src/sys/kcov_gcc.c | 101 +++++++++++
kernel-src/sys/thread.c | 9 +
16 files changed, 617 insertions(+), 36 deletions(-)
create mode 100644 kernel-src/include/kernel/kcov.h
create mode 100644 kernel-src/sys/kcov.c
create mode 100644 kernel-src/sys/kcov_dev.c
create mode 100644 kernel-src/sys/kcov_gcc.c
--
2.43.0
next reply other threads:[~2026-10-06 14:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-06 14:46 Matheus [this message]
2026-10-06 14:46 ` [PATCH 1/4] mm: properly fork shared file mappings Matheus
2026-10-06 14:46 ` [PATCH 2/4] cpu: add irq depth counting Matheus
2026-10-06 14:46 ` [PATCH 3/4] mmu: move tlb shootdown to IPL_MAX Matheus
2026-10-06 14:46 ` [PATCH 4/4] kcov: initial implementation Matheus
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261006144635.141071-1-matheus@astral-os.org \
--to=matheus@astral-os.org \
--cc=astral-devel@lists.astral-os.org \
--cc=matheusuky@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox