public inbox for astral-devel@lists.astral-os.org
 help / color / mirror / Atom feed
* [PATCH 0/4] kcov: initial implementation
@ 2026-10-06 14:46 Matheus
  2026-10-06 14:46 ` [PATCH 1/4] mm: properly fork shared file mappings Matheus
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Matheus @ 2026-10-06 14:46 UTC (permalink / raw)
  To: astral-devel; +Cc: Matheus

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-10-06 14:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-06 14:46 [PATCH 0/4] kcov: initial implementation Matheus
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox