Another look at kernel in Objective-C

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
technix
Member
Member
Posts: 28
Joined: Sun Jun 16, 2013 10:13 am

Another look at kernel in Objective-C

Post by technix »

I have seen some debates on kernels with Objective-C, and I know that using full runtime and library is (almost) impossible in kernels. However, what about doing this:

The kernel itself is written in part C part Objective-C, running on a stripped down version of libobjc2 (runtime library) and Foundation (standard library) that provides these features of Objective-C: (with required subset of C library)
  • Property lists (Objective-C basic data handling, including strings, numbers, dates, binary blobs, object arrays and dictionaries)
  • Bundles (loading executable from a specially structured folder with all its resources)
And here is the features that I think should present in the kernel: (Philosophy: everything is object.)
  • Virtual root filesystem (essentially a tmpfs)
  • tar unarchiving (tar as initramfs)
  • Universal hardware abstraction as objects
  • Mount point management
  • Kernel work queue management (GCD)
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Another look at kernel in Objective-C

Post by iansjack »

Why would you want to use Objective-C to write a kernel? You have a huge runtime to implement, handling message passing, polymorphism, and the like all for something that can be done (IMO) far more simply in C.

The language that you use to implement a kernel doesn't seem to be particularly important to me, so why not use one that works out of the box and was designed specifically for this purpose? What do you see as being the advantage of using Objective-C?
technix
Member
Member
Posts: 28
Joined: Sun Jun 16, 2013 10:13 am

Re: Another look at kernel in Objective-C

Post by technix »

iansjack wrote:Why would you want to use Objective-C to write a kernel? You have a huge runtime to implement, handling message passing, polymorphism, and the like all for something that can be done (IMO) far more simply in C.

The language that you use to implement a kernel doesn't seem to be particularly important to me, so why not use one that works out of the box and was designed specifically for this purpose? What do you see as being the advantage of using Objective-C?
I designed the system to be a microkernel (or picokernel) that can exposes an object representation of abstracted hardware by using some Objective-C tricks:
  • Distributed Objects: one process can talk to another (in this case, a device driver to a kernel) via captured and forwarded method calls on objects in an proxy class.
  • Class substitution: In Objective-C, an object can change its class on the fly (!)
  • Class categories: In Objective-C, an class can be "patched" by adding mew methods on-the-fy in the runtime. (!)
This is how I load drivers and how drivers work: the kernel exposes all detected hardware as objects of an abstract hardware class that does nothing more than wrapping those instructions that is only available in ring 0, and a device driver load itself by substituting all suitable exposed objects' classes and/or categorizing on them. Memory-mapped hardware can also be drived by using a special kernel-only category method on NSMutableData (binary blob) class. and injecting a class with that NSMutableData as its ivar into the hierachy. Here is the boot procedure, suuming at least 2 processor cores: (Kernel itself preempts in the same way as other threads. A trap to kernel merely put the calling thread into wait and get the kernel into running.)
  1. System up, POST, GRUB to kernel.
  2. Kernel set up virtual root filesystem, unarchive initramfs (an tar archive) into it.
  3. Kernel set up GCD (a queue-based scheduler with IPC support)
  4. Kernel spawns init in userspace.
  5. Kernel start to enumerate and monitor hardware changes.
  6. Meanwhile, init scans all daemons in the vrfs and prepare them for load (load into memory, but not calling their init methods yet).
  7. Hardware enumeration is now done. Kernel pings init.
  8. After init is being pinged, it enumerates the established kernel device object tree and set up corresponding userspace counterparts, OSDevice objects.
  9. init load up initramfs drivers in parallel. They substitute the class OSDevice of the objects init set up with their own. Memory maps are set up by calling -[NSMutableData(OSKernelExtension) mappedMemoryAtAddress:(const void *)address length:(NSUInteger)length movable:(BOOL)movable] method with movable=NO. (essentially this is what you usually known as mmap(), but it is object oriented so mapped memory appears as an object too, but this movable argument is kernel specific and a non-movable mapping will never be paged out.)
  10. After all drivers are loaded, the rootfs driver is called to load root filesystem object tree. Then the real root filesystem is mounted over the vrfs. vrfs memory is reclaimed as original vrfs objects are collected. (I use Objective-C ARC for this.)
  11. Now with rootfs available, init will look for fstab to mount all remaining filesystems, and load up all remaining drivers, and start the daemons.
After the kernel start up, there will be a object tree matching your hardware and some unmovable mappings of NSMutableData used by drivers. Kernel and init will swap themselves out unless used.

Also, on my system malloc() in user mode is backed by NSMutableData objects in kernel, so kernel can optionally perform heap pointer range check at byte percision that hardens system at a speed penalty.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Another look at kernel in Objective-C

Post by iansjack »

Fair enough. All you need to do now is to construct the run-time to support all these object-oriented features.
OSwhatever
Member
Member
Posts: 595
Joined: Mon Jul 05, 2010 4:15 pm

Re: Another look at kernel in Objective-C

Post by OSwhatever »

So in practice you would need run-time library, probably written in C or other compiled language and that would be like the "kernel".
technix
Member
Member
Posts: 28
Joined: Sun Jun 16, 2013 10:13 am

Re: Another look at kernel in Objective-C

Post by technix »

iansjack wrote:Fair enough. All you need to do now is to construct the run-time to support all these object-oriented features.
Actually there is an existing Objective-C runtime library by GNU and I can port that (and strip away functions unused by the kernel) as well as the standard library (Apple's Foundation)
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Another look at kernel in Objective-C

Post by iansjack »

Be sure to come back and tell us how you get on with that.
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Another look at kernel in Objective-C

Post by Mikemk »

technix wrote:
iansjack wrote:Fair enough. All you need to do now is to construct the run-time to support all these object-oriented features.
Actually there is an existing Objective-C runtime library by GNU and I can port that (and strip away functions unused by the kernel)
That would require your project to be GPL'd.
as well as the standard library (Apple's Foundation)
I'm not sure what that's licensed as.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Another look at kernel in Objective-C

Post by Owen »

The GNUStep project has an X11 licensed Objective-C runtime (which is significantly better than that included with GCC)

GNUStep foundation is LGPL v2
Post Reply