Writing Your First Kernel Module

dancehall flamenco, new orleans carnatic, harpischord doo-wop, psychedelic dream pop

Listen on 93

Lyrics

[Verse 1]
Download the kernel headers, prepare your coding space
Time to bridge the sacred boundary between two worlds we face
Userspace runs protected, sandboxed and contained
But kernel space holds power where the hardware is ordained

[Chorus]
Module init, module exit, that's the lifecycle you need
Include linux kernel dot h, plant the fundamental seed
Register your driver, claim your place inside the core
Kernel space versus userspace, now you know what each is for

[Verse 2]
Write your init function, mark it double underscore
Static int init module, this is how you open doors
Print kern info messages, let the system know you're here
Return zero for success, any other means you fear

[Chorus]
Module init, module exit, that's the lifecycle you need
Include linux kernel dot h, plant the fundamental seed
Register your driver, claim your place inside the core
Kernel space versus userspace, now you know what each is for

[Bridge]
Ring zero privileges, no standard lib to call
One mistake can crash it all, bring the system to a crawl
GFP kernel allocations, spinlocks guard your code
Module license GPL, or the kernel won't load

[Verse 3]
Cleanup function mandatory, free what you have claimed
Double underscore exit, properly maintained
Make file declarations, object files compile
Insmod loads your module, lsmod shows your style

[Chorus]
Module init, module exit, that's the lifecycle you need
Include linux kernel dot h, plant the fundamental seed
Register your driver, claim your place inside the core
Kernel space versus userspace, now you know what each is for

[Outro]
From userland to kernel land, you've crossed the sacred line
Your first module running, kernel hacker by design

← Device Trees: Hardware Description Language | Character and Block Device Drivers →