Hasty Briefsbeta

Kpython – A MicroPython Sidecar for the Linux Kernel (Experimental)

15 hours ago
  • #Experimental
  • #MicroPython
  • #Linux Kernel
  • kpython is a port of MicroPython designed to run as a loadable Linux kernel module (kpython.ko).
  • It allows executing Python code directly within the kernel address space for debugging, prototyping, or policy handling.
  • ⚠️ DANGER: This project is EXPERIMENTAL / PRE-ALPHA and extremely dangerous; not for production use.
  • ⚠️ WARNING: Runs in Ring 0; bugs can panic the kernel or corrupt memory.
  • Developed and tested on WSL2 with a custom kernel (CONFIG_MODULES enabled).
  • Project structure includes micropython/, embed_cfg/, kernel_mod/, and a top-level Makefile.
  • Requirements: Linux Kernel Headers, build-essential, Python 3.
  • Build steps: Initialize submodule, run make (optionally specify KDIR for cross-compiling).
  • Module installation: sudo insmod kernel_mod/kpython.ko; check dmesg for confirmation.
  • Debugfs interface at /sys/kernel/debug/kpython/exec allows Python script execution.
  • Example commands: print statements, calculations, function definitions.
  • Module removal: sudo rmmod kpython.
  • Embed Port: Uses MicroPython's ports/embed for self-contained C source generation.
  • Shims include libc wrappers, allocation mappings (vmalloc/vfree), and I/O (printk).
  • Safety features: stack protection, exception handling, atomic context handling.
  • Limitations: No float support, no file I/O, currently tested on x86_64.
  • Licensing: Kernel module wrapper is GPL; MicroPython core is MIT.