OpenBSD Journal

[Ask OBSDJ] How to write kernel extensions?

Contributed by Dengue on from the kernel-puffy dept.

george writes : "Is there a guide on how to start writing your own extentions to the OBSD kernel? Not something very fancy like SMP stuff. Simpler (but not simple) like having something like the tux web server on the OBSD kernel.

Please I am not asking on the feasibility of such a project, if it is a good or a bad thing. I am simply asking where to start to do it as a means to learn how to tinker with the OBSD kernel."

One good place to start is man lkm and look at the examples in /usr/share/lkm . Any other resources out there?

(Comments are closed)


Comments
  1. By Anonymous Coward () on

    no OpenBSD kernel is only for hacker. If your are not an hacker, you should not know how it work. This is for security.

    Comments
    1. By Alistair McMillan () on www.mcmillan.cx

      This is a stupid attitude. Everyone has to start somewhere. What kind of situation would we be in if all the current kernel hackers had been met with this attitude when they were starting out...

      Comments
      1. By Anonymous Coward () on

        this is stupid ... but this is the reality, you should be a member of the kernel team if you wan't to hack the kernel. This kernel team does not produce documentation (source code is not a documentation ...). This is a kind of chicken before egg question ...

        Comments
        1. By tedu () on

          there are many man pages in section 9. many major subsystems are documented.

          man pages don't tell you what to do, of course. if the man pages could say "you add this feature by doing this..." don't you think the author would have just written the code?

          Comments
          1. By Anonymous Coward () on

            you are right, man page is the primary raison I use OpenBSD, but the guy ask for a guide about how to write kernel extension. Man pages are definitly not a good guide to learn how to program ...

    2. By m-- () on

      yes, security by obscurity, sounds /exactly/
      like something the OpenBSD project would
      endorse.

    3. By tedu () on

      whatever...

      there's no reason kernel modules or patches can't be maintained by somebody without incorporation into the official kernel.

      but the best answer to "how do i learn how to program in the kernel?" is "you learn by doing." The kernel is large, and has many different subsystems. And telling you how one thing works doesn't always help you edit something else, or add a new feature.

      One good starting point is the man pages for section 9, kernel functions. They are not all complete, however. If you are not happy with how well something is described, try to figure it out and submit patches. If you can write a manpage for something, you're on your way to understanding it.

      One final note: All new features require documentation. So don't submit anything without it. :)

    4. By Josh () selerius at codefusion dot org on mailto:selerius at codefusion dot org

      i thought we started including the IP's of the posters, to discourage the trolls....obviously this post is a pointless waste of time and bandwidth, and the author has the mental maturity of a 3 year-old.

  2. By Jason () on

    This link to an article about writing lkm's came up on tech@ a couple days ago:

    http://deadly.org/article.php3?sid=20010812210650

    I think it's pretty useful. I don't recall who posted it though (Dries?). Anyway, enjoy and happy hacking!

    Comments
    1. By Anonymous Coward () on

      LKMs are broken.

  3. By Anonymous Coward () on

    There isn't any one source for learning how to tinker with the openbsd kernel, but i found this book very useful:

    "The Design and Implementation of the 4.4BSD Operating System" by McKusick, Bostic,Karels, Quaterman, isbn: 0201549794

    While this book is not openbsd specific, it is gives a pretty good description of many kernel structures found in openbsd. With this book you should be able to start reading through some kernel code to figure out how the source is organized, etc. I think that cscope (http://cscope.sourceforge.net/) is a really good tool for this, allowing you to search through all of the source files easily.

    After you read through kernel code for a while, it shouldn't be that hard for you to start coding yourself!

    Comments
    1. By Anonymous Coward () on

      i agree, just get in there and poke around, if you do something cool, send it to tech@. another good book is "The Basic Kernel Source Code Secrets" by Jolitz & Jolitz (the 386BSD guy). its painfully old and out of date, but the description of locore.s is well worth it (if you hate reading asm, as i do :)

    2. By linny () linny1106@yahoo.com on mailto:linny1106@yahoo.com

      hi:
      i am linny from china.
      who can do me a favor to send me a copy of the book (The Design and Implementation of the 4.4BSD Operating System" by McKusick, Bostic,Karels, Quaterman, isbn: 0201549794
      ) :)
      i am now 20 .in a aust.edu.cn.fot the study of chemistry.(my godness).but i am interested in
      linux kernel .hurd/mach,bsd.xinu,enjoying their differences and their ..
      now i am not a hacker ,and i am not sure i wont .
      hoping to get the book (
      1.The Design and Implementation of the 4.4BSD Operating System" by McKusick, Bostic,Karels, Quaterman, isbn: 0201549794
      2.internetworking with tcpip vol11:design,implementtatio,and internals
      )
      because in my china i only can get the chinese version.i would prefer the english version.
      thanks from linny who hope to do something to the
      ones he love --linux.hurd,xinu.bsd.
      linny1106@yahoo.com.cn
      linny1106@hotmail.com
      .may be you will give some advises on how to do
      the OS-kenel designing .protocol implementing

  4. By Dave Parfitt () diparfitt@yahoo.com on mailto:diparfitt@yahoo.com

    Although I don't have any experience with OpenBSD directly, I have done alot of FreeBSD hacking... I would suggest starting out by writing a simple system call (maybe it calls a halt or a uprintf or something). get it to compile and run... maybe add something to the proc (or thread?) structure and modify the values via a system call. etc etc. learning the ufs/ffs code is fun too - add ACL's or something to the filesystem.

    reading through the code is the best way to go...

    Hope this helps -
    dave

  5. By Anonymous Coward () on

    Anybody know why LKM's don't work in ELF i386, or have any hints as to what can be done to fix them?

  6. By GPS () GeorgePS@XMission.com on mailto:GeorgePS@XMission.com

    I've been working with Bochs to play around with OpenBSD's kernel. I've written simple modifications to the kernel that change the colors and output my own message on startup. The cool thing about Bochs is that you don't have to worry about messing up an existing setup. When I play with a new kernel I just copy my old harddisk image for a backup. I usually compile kernels in my real OpenBSD setup, and then use a vnode image to transfer to the emulated system, because compiling within Bochs is too slow. I suggest you get really friendly with grep, and start with sys/kern/init_main.c

Credits

Copyright © - Daniel Hartmeier. All rights reserved. Articles and comments are copyright their respective authors, submission implies license to publish on this web site. Contents of the archive prior to as well as images and HTML templates were copied from the fabulous original deadly.org with Jose's and Jim's kind permission. This journal runs as CGI with httpd(8) on OpenBSD, the source code is BSD licensed. undeadly \Un*dead"ly\, a. Not subject to death; immortal. [Obs.]