|
|
Log in / Subscribe / Register

Writing kernel modules in Haskell

Writing kernel modules in Haskell

Posted Sep 14, 2009 8:31 UTC (Mon) by simlo (guest, #10866)
Parent article: Writing kernel modules in Haskell

Yes!

At my work I write "drivers" in Java (in user space, through simple native calls to get access to memory mapped hardware registers in the hardware). There is nothing wrong about writing hardware near stuff in higher level languages, where you have no direct access to low-level bit manipulation. In fact, to make C portable you must avoid doing the very low-level bit-manipulations directly but put them away in libraries. Then you can just as well access those libraries from another language.

In fact, if I were to write an OS kernel from scratch, I would start out by making a small runtime system for a language like OCAML and then write drivers and filesystem etc. in that language. Some of the scheduler and the garbage collector would have to be written in C. The major technological hurdle would be to make a scaleable, realtime garbage collector, but if you can construct it along with the compiler and the scheduler, it might turn out to be easier than doing it in userspace. And you can also use that in "almost functional" languages, you make far fewer memory modifications than in Java. I.e. you can optimize the system for "many readers", "few writers". Old memory can not have pointers to new memory.

This also tells us one of the benifits: Better scaleability since the garbage collector superseeds the RCU. A functional language makes it natural to write good RCU client code, since you never change objects. You (almost) only need locks to protect the hardware from simultanious access, since (almost) all ordinary memory structures are read-only. And then, strongly typed functional language makes must safer code. Writing the kernel in such a system, would be a more efficient way to get a stable system than using microkernels.


to post comments

Writing kernel modules in Haskell

Posted Sep 14, 2009 14:56 UTC (Mon) by PaulMcKenney (✭ supporter ✭, #9624) [Link]

Still working on getting the equivalent of rcu_dereference() and rcu_assign_pointer() into the Java standard. Thus far, the powers that be remain unamused by this suggestion. That said, there seemed to be a bit less resistance this time around than last time.

In the meantime, make sure you use the "volatile" keyword appropriately...

Writing kernel modules in Haskell

Posted Sep 15, 2009 18:10 UTC (Tue) by dambacher (subscriber, #1710) [Link]

don't reinvent!
Look at smalltalk80 .-)=====

Writing kernel modules in Haskell

Posted Sep 17, 2009 10:56 UTC (Thu) by bboissin (subscriber, #29506) [Link] (3 responses)

Some students in my school did a small ocaml kernel som years ago, it was
booting in qemu and on real hardware.

http://gna.org/projects/funk/

Writing kernel modules in Haskell

Posted Sep 18, 2009 14:19 UTC (Fri) by simlo (guest, #10866) [Link] (2 responses)

Last time I looked at OCAML, the implementation was bad: I used a "grand interpreter lock" (just as Python). You have to get rid of that for it to make sense.

I also disagree with the main page statement

"It could then be used as an educational tool for teaching OS design, for instance, or may be just replace Linux before Hurd goes out. "

Sorry, it is the other way around: It would be faster and more stable than any microkernel, it is just a question of optimizing the compiler. Even in very optimized assambly a microkernel will be slower - and each "service" will be more buggy since it typically will be written in C.

Writing kernel modules in Haskell

Posted Sep 18, 2009 14:30 UTC (Fri) by bboissin (subscriber, #29506) [Link] (1 responses)

Here Ocaml isn't run in interpreted mode, and maybe the GC has a global
lock but I don't think there's any other lock when Ocaml is run natively.

Secondly, I think you took that comment too seriously, the reference to
hurd shows it was meant as a joke :)
(It was just students having fun building an OS from scratch)

Writing kernel modules in Haskell

Posted Sep 19, 2009 22:41 UTC (Sat) by simlo (guest, #10866) [Link]

Well, I haven't checked again, but years ago I looked into it and that version of OCAML had something similar to the "grand interpreter lock" even though it was copiled to native code :-(

But with help from the compiler and the scheduler, it ought to be possible to make a system with no central lock - making it scale - although it will be very hard. But using an (almost) pure functional language will help in that: You for instance know that an old piece of memory can never have references to later allocated memory since it wasn't change. And in the few places where you do update a reference the compiler can put in checks to help the GC as well as the right read/write barriers for the readers on other CPUs.

Well, I can always keep on dreaming..

assignment-free garbage-collected data processing considered no substitute for cache locality

Posted Sep 27, 2009 13:46 UTC (Sun) by xoddam (subscriber, #2322) [Link]

Unfortunately one consequence of avoiding assignment in your language is that sane-looking code requires new locations for new data. The only way to say "I want this data updated in place" is to declare a monad for that data, and a multitude of monads is probably as problematic to debug as C code.

Otherwise you will end up doing *everything* as garbage-collected RCU. Great, in theory, for scalability on the grand scale but dreadful for performance on typical machines with only a few CPUs(!) and a few gigabytes (!) of RAM.

Perhaps best to implement the kernel of your kernel in well-audited C but begin to adopt the secure languages for peripheral components such as device drivers.

Writing kernel modules in Haskell, you might say.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds