Kernel development

From j4flmao/agent-skills · View interactive version

Kernel development is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about Linux kernel modules, device drivers, system calls, interrupt handlers, spinlocks, RCU, kernel oops or panic, Rust for Linux, bootloaders, page tables or writing a hobby OS. Produces kernel code and debugging steps. Not for eBPF programs (use ebpf-engineering) or RTOS fi

How to use this skill

Skill file

Kernel Development and OS Internals

Purpose

Kernel code runs with full privilege, cannot sleep in many contexts, and a single bug crashes the machine. This skill explains the execution contexts and rules that matter (process vs interrupt context, what may sleep, which lock to use), shows a minimal correct module, and lists the debugging tools that make kernel work tractable.

Agent Protocol

Trigger

"kernel module", "Linux driver", "device driver", "ring 0", "system call", "interrupt handler", "spinlock", "RCU", "kernel panic", "oops", "write an OS", "bootloader", "page tables", "Rust for Linux".

Input Context

Output Artifact

Kernel C (or Rust) code, Kbuild/Makefile, debugging plan. No file unless requested.

Response Format

Kernel: {version, arch}
Context: {process | softirq/tasklet | hardirq} — may sleep: {yes/no}
Locking: {primitive and why}
Code: {snippet}
Debug: {config options / tools to verify}

1. Privilege and Address Spaces

2. Kernel Architectures

| Design | Examples | Trade-off |

|--------|----------|-----------|

| Monolithic (with loadable modules) | Linux, FreeBSD | fast in-kernel calls; a driver bug can take down the system |

| Microkernel | seL4, QNX, MINIX 3 | drivers/filesystems as user processes; isolation and verifiability, IPC cost |

| Hybrid | Windows NT, XNU | monolithic in practice with microkernel structure |

3. System Calls

x86-64 Linux: arguments in rdi, rsi, rdx, r10, r8, r9, number in rax, syscall instruction; return in rax (negative errno on failure, translated by libc). ARM64: x0–x5, number in x8, svc #0. Kernel entry saves user registers, switches to the kernel stack, and dispatches through the syscall table. vDSO provides some calls (e.g. clock_gettime) without a mode switch. See references/syscall-mechanism.md.

4. Interrupts and Deferred Work

| Mechanism | Context | May sleep |

|-----------|---------|-----------|

| Threaded IRQ (request_threaded_irq) | kernel thread | yes — preferred for most drivers |

| Softirq / tasklet | softirq | no (tasklets are being phased out in favour of threaded IRQs / BH workqueues) |

| Workqueue (queue_work) | kernel thread | yes |

| Timers (timer_list, hrtimer) | softirq/hardirq | no |

See references/interrupts-exceptions.md.

5. Memory

6. Synchronisation

| Primitive | Use | Rules |

|-----------|-----|-------|

| spinlock_t (spin_lock_irqsave when shared with IRQ handlers) | short critical sections, atomic context | never sleep while holding |

| mutex | process context, may sleep inside | not from IRQ context |

| rw_semaphore, seqlock | read-mostly data | seqlock readers retry on concurrent write |

| Atomics (atomic_t, refcount_t) | counters, refcounts | refcount_t saturates instead of overflowing |

| RCU | read-mostly linked data, lock-free readers | readers rcu_read_lock(); writers publish with rcu_assign_pointer() and free after synchronize_rcu() / kfree_rcu() |

Lock ordering must be consistent; enable lockdep (CONFIG_PROVE_LOCKING) during development. See references/kernel-synchronization.md.

7. Minimal Module

// hello.c
#include <linux/module.h>
#include <linux/init.h>

static int __init hello_init(void)
{
    pr_info("hello: loaded\n");
    return 0;                       /* non-zero aborts the load */
}

static void __exit hello_exit(void)
{
    pr_info("hello: unloaded\n");
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("minimal example");
# Makefile (out-of-tree)
obj-m += hello.o
all:
	$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
	$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

insmod hello.ko, dmesg, rmmod hello. Secure Boot systems require signed modules. There is no stable in-kernel API: out-of-tree modules must be rebuilt (and often adapted) per kernel version — upstream drivers when possible.

Drivers use the device model: register a platform_driver/pci_driver/i2c_driver with an of_match_table/ID table, set up in probe(), tear down in remove(), prefer devm_ helpers. Rust drivers are supported for a growing set of subsystems (Rust for Linux, mainline since 6.1).

8. Debugging

9. Hobby OS Bring-up (x86-64)

Boot via UEFI or a Multiboot2/Limine bootloader → set up GDT/IDT and paging (4-level, optionally 5-level) → enable interrupts (APIC) → physical frame allocator → kernel heap → scheduler with context switch in assembly → syscalls → user mode. Use QEMU, a serial console for logs, and the OSDev wiki as the reference.

10. Anti-Patterns

11. Checklist

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.