Best way to get into Kernel programming?

7,522

Solution 1

Firstly:

For the baby stages, writing various variations on "hello world" modules, and virtual hardware drivers, are the best way to start (real hardware introduces real world problems best faced when you have more of an idea what you are doing).

"Linux Device Drivers" is an excellent book and well worth starting with: http://lwn.net/Kernel/LDD3/

LDD (used to, at least) have exercises where you wrote virtual drivers, e.g. RAM disks, and virtual network devices.

Secondly:

subscribe to https://lkml.org/ or to the mailing list of a sub-system you will be hacking in. Lurk for a bit, scanning over threads, reading code review (replies to patches) to see what kind of things people stumble on or pick up on.

See if you can obtain (cheap) hardware for a device that is not yet supported, or not yet supported well. Good candidates are cheap-ish USB NICs or similar, low-cost USB peripherals. Something with an out-of-date, or out-of-tree driver, perhaps vendor written, perhaps for 2.4.x, is ideal, since you can start with something that works (sort-of), and gradually adapt it/rewrite it, testing as you go. My first driver attempt was for a Davicom DM9601 USB NIC. There was a 2.4-series vendor-written kernel driver that I slowly adapted to 2.6. (Note: the driver in mainline is not my driver, in the end someone else wrote one from scratch).

Another good way in is to look at the Kernel Newbies site, specifically the "kernel janitors" todo: http://kernelnewbies.org/KernelJanitors/Todo This is a list of tasks that a beginner should be able to tackle.

Solution 2

There are many kernels in existence.

Linux is not the only game out there, but it's the most widely used. Some people recommend the minix kernel to understand operating system concepts. I would go with Linux 2.6.x series as it's the most common and it won't be wasted on something (almost) nobody uses. Solaris, XNU (Mac), FreeBSD and Dragonfly are notable.1

Find something super simple (serial port, usb, etc.) and make it do something else. For an extra challenge, try writing a kernel module for something that doesn't yet have one. (Be sure to google the suspect by pci sudo lspci -nn and/or usb id sudo lsusb first.)

Update: Don't do this directly on your primary system, use virtual machines (VM) (computer in a computer) as mentioned. VirtualBox can provide this and it's free. Also, I don't want it to seem overwhelming, but try VM snapshots so you can go back in case something breaks the VM itself.

Update 2: The note about Git (DVCSes inc. mercurial, fossil, bazaar, darcs) is a good idea, mostly for code and small, important files. Try not to use Git to backup music, videos and such unless you know the implications. ;)

Solution 3

Find a module or driver you want to modify or start with a null / hello world type of module and build from there. Specifically work with a module because it will allow you to load, unload, recompile and reload the code into a running kernel. Later you can get into the core bits, but basically modules have all kinds of and you can do pretty much whatever you like.

Second, use a VM. Setup a guest machine under VirtualBox or some over VM and do your kernel testing in there for a while. That way you won't kill your main system when you break something, and restarting, backtracking to a working snapshot, etc is really easy.

Third, learn git.

Solution 4

Someone already mentions the book "Linux Device Drivers" - this is an excellent resource, and I highly recommend it as well.

Although the question mentions the originator isn't asking specifically for books, the newest edition of the book "Understanding the Linux Kernel" ( http://oreilly.com/catalog/9780596005658/ ) is invaluable in learning about the Linux kernel in general, and also contains a section on Linux device drivers.

Share:
7,522

Related videos on Youtube

Jeff Schaller
Author by

Jeff Schaller

Unix Systems administrator http://www.catb.org/esr/faqs/smart-questions.html http://unix.stackexchange.com/help/how-to-ask http://sscce.org/ http://stackoverflow.com/help/mcve

Updated on September 18, 2022

Comments

  • Jeff Schaller
    Jeff Schaller over 1 year

    Not so much asking what books (although if you know of any guides/tutorials that'd be helpful) but what is the best way to start doing kernel programming and is there a particular distribution that would be best to learn on?

    I'm mostly interested in the Device Drivers portion, but I want to learn how the Kernel is set up as well (Modules and such) I have around 4-5 years Experience with C/C++ but it's mostly knowledge from College (so it's not like 4-5 years work experience, if you know what I mean)

    • tcoolspy
      tcoolspy almost 13 years
      To start with, it's spelled kernel with an e.
    • Admin
      Admin almost 13 years
      Ya I noticed that when I was putting on the tags lol.
    • tcoolspy
      tcoolspy almost 13 years
      Next time take the time to fix it. It's a good way to respect the community here that is taking the time to help you. Also if you want to code for the kernel, your going to have to be rigorous about details like that!
    • Admin
      Admin almost 13 years
      @Caleb, will do!
    • Mike Pennington
      Mike Pennington almost 13 years
      Thank you for asking this question, I have always wondered myself
    • Ciro Santilli Путлер Капут 六四事
      Ciro Santilli Путлер Капут 六四事 almost 7 years
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 13 years
    Good answer for Linux, missing just one resource: LXR for browsing the kernel source.