From mboxrd@z Thu Jan 1 00:00:00 1970 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lists.astral-os.org; s=s2026; t=1791298014; bh=E1KGZTRCv4TWbxwguAe/t2r7xHREuTC6+nxJe1KVuew=; h=From:To:Subject:Date:CC:List-Id:List-Help:List-Owner:List-Post: List-Subscribe:List-Unsubscribe:From; b=tDQddKebze8uXhDtIZYFIVZN7cKeKtu3PL8Qn28j/fzPeOcAvl0zwpC5KwJDeqIdw nRHmTTYOPWYs9f1g5JBy1PJ1p1iFmC8mO7JQI/ldngX3uLBH8JDG+WxmWOTm2nJLBs k0CO0oLH4BnhZ5UPi0FKp/RkLMUrqz1ukehtIuWff84R/JbHWsQeMq35ppaIjonAa0 THfmJSR7Pe4PFBLuLnz/zHnE7V2kaPXalt73PatulUMG3eEK/HtgoHjXFrgc88oi/J umz2EJ/m1Sw/c1SQODo67EviS/FTUNt5RtCNfFbuEr8clpiA4hssAHTHM+JDKiDTv+ eKafgdxqqRkMg== Received: from vps-ace4d786.vps.ovh.us (localhost [127.0.0.1]) by mail.astral-os.org (Postfix) with ESMTP id 9A3173ECFD; Tue, 06 Oct 2026 14:46:54 +0000 (UTC) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=lists.astral-os.org; s=arc2026; t=1791298014; b=MAam5zYTgk3I13/Qijdhj66KOAOvtef5VW3yaBXNKSvpHbGNXtSBa/+k3mDgMOAjDa6D/ XNXl8cehNFn7LkBrp3K17tQKistV2z3WoLIp7xvhTeM9GxKXq96gcqWqkJPJpHWrHS6BRpC xSk5kCOuSamM5M96kmxM80kQr2SvoZxEi9uaHCiYY5QMKfgWNu5sXusvCjl0xSnaE0DgYFY VzHhcoAxxpziUoKnUJSLaFC84kErgTdsMKZ8NP94ZlJqEqqBbzKQLYu26OizXxMWddr3JU5 jg1RqmPvuqSOtf1X/51QnjbwfiGgkQYZR98veTKEoOfIG0I56eiim9ViKkqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=lists.astral-os.org; s=arc2026; t=1791298014; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=6IeP70tEP58JVqUYFz11VmyAheCIURlA3LuaIaDhSbc=; b=M2QDwgk74ue6YsRID6S04uVNSQawKW/sZR9G5NVueGsba9Vnn1ZvLzHwHevaJYxwQnRZ/ McT3z5H/n0VCa3b/y43RXTPCd4CGVU2hbl7mC9Ne73S+dBdVsknasRxhhQOe2Jbdxt6nPuz j+iPHdcELy3A1rMWGnSjpWyyjyYezBw23gLno9TwxoN1Q12PiZ8ZvzQ4Az3UsL+Cuxf6zzW PLcp+yqn01OTLufA/7vThl6+LgZSsRFlWHfS/F1jwYEGny6SiPumpKq5MQ2C+KE8ytT8Utu xTY8IM4t2bZ+SzPCGN3nYkD+whfIuFXPgLkHKdIh9oxqTXP7oNljf9cedI8w== ARC-Authentication-Results: i=1; lists.astral-os.org; dkim=pass header.d=astral-os.org; arc=none; dmarc=none Authentication-Results: lists.astral-os.org; dkim=pass header.d=astral-os.org; arc=none (Message is not ARC signed); dmarc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=astral-os.org; s=mail2026; t=1791298011; bh=E1KGZTRCv4TWbxwguAe/t2r7xHREuTC6+nxJe1KVuew=; h=From:To:Cc:Subject:Date:From; b=amPWOLaz8RFbYlwWwvv/xpmK9mL2pZzhmAzEaWOa48SFmYvha0C86cPC9t/WtzcpJ 4FfcKcDD8itvXzzSWYGODJf6FWBCVHn8Bq76v5aJuuGM3PstTeeI0+Xagi7WPWWb3U 7K07zLVvQRJ07yHZXC+XX6c22aBLqIqC/9lKEa+QRJdnkwTjcP7lnH5ia2In6RrnL6 mk4Iv9ME7Jooubwe9U24VQdcYkiItn5/t8Dh+rbbl8Eqpa1ksCd36hHF0DeD53f8a7 gunANCIjq3cxJMBZ1lqK/6K8as+/wA0LwlP+s1QCvjCc4cxrWI4FTKkhMSn8w63fSt HkPh+tV3b106w== Received: from LAPTOP-R01LEAQM.localdomain (unknown [189.112.151.75]) by mail.astral-os.org (Postfix) with ESMTPSA id D77663EA48; Tue, 06 Oct 2026 14:46:50 +0000 (UTC) From: Matheus To: astral-devel@lists.astral-os.org Subject: [PATCH 0/4] kcov: initial implementation Date: Tue, 6 Oct 2026 11:46:31 -0300 Message-ID: <20261006144635.141071-1-matheus@astral-os.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: A4Q7MV4NTH4YPXYWLXLLHKEQMHLTCHMH X-Message-ID-Hash: A4Q7MV4NTH4YPXYWLXLLHKEQMHLTCHMH X-MailFrom: matheus@astral-os.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Matheus X-Mailman-Version: 3.3.10 Precedence: list List-Id: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Matheus 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