Page 1 of 2
C# compiler for Kernel dev?
Posted: Wed Dec 16, 2009 6:31 am
by LordMage
Okay, I'm looking to make my kernel using C# as you probably gathered from the subject. I have been recently reading some of the benefits and some of the issues still needing to be sovled and I think it will be fun! What I have been having trouble figuring out is what compiler to use. I downloaded the latest Singularity release and tried to find the "Bartok" compiler but only found a visual studio solution that I couldn't open for some reason in VS2008. I have also downloaded Cosmos and SharpOS but haven't had a chance to really dive into the stuff I downloaded. My main question is:
Is there an out of the box C# compiler I could use for kernel development?
I would really prefer to use Visual Studio but from the Singularity project it seems like this won't work. I think SharpOS is using the mono project to compile stuff for their OS but I am not sure if it is an unmodified version. I basically understand what JIT is and AOT. but I don't understand the implementation as I have not delved into the inner workings of the C# language and IL. If I do need to write my own compiler for C# so that I can make a kernel that would be a cool project to undertake but I don't know where to start. I have been looking for compiler source and instruction but havn't found much. Any help would be greatly appreciated.
Re: C# compiler for Kernel dev?
Posted: Wed Dec 16, 2009 7:11 am
by redoktober
LordMage wrote:
Is there an out of the box C# compiler I could use for kernel development?
uh..."out of the box"?? what, in your dictionary, refers to an out of the box solution? is it a compiler which compiles C# code to native assembly code? then you should check out Cosmos and their IL2CPU system.
LordMage wrote:
I basically understand what JIT is and AOT. but I don't understand the implementation as I have not delved into the inner workings of the C# language and IL.
why do you wish to use C#?
is it because the language is good?
well, trust me, mate.
using C# is *only* effective when it has a runtime associated with it.
and with no base operating system, where *your* kernel is operating, how do you implement a runtime?
you should first have a good command of the C# language, as well as IL.
take a couple of months, pick up a good book.
and here's a tip: when you write some C# code, and run it, disassemble the managed executable using ildasm. that will help you understand how the 'behind-the-scenes' IL works.
also, obtain a copy of the ECMA specifications for the CLR and C#. they are downloadable and also can be ordered.
and..they're invaluable.
check out my blueprint for my project.
http://sites.google.com/site/thehackerl ... ects=0&d=1
hopefully, it will answer some of your questions, and maybe even have you jump onboard!
good luck, kid. you're gonna need it.
Re: C# compiler for Kernel dev?
Posted: Wed Dec 16, 2009 7:16 am
by redoktober
when you read the blueprint, you'll see how my solution could actually *work*.
AOT isn't worth a thing, since it still doesn't bring out the true power of the CLR.
only a JIT implementation can do that.
and that's the reason where i feel that Singularity, Cosmos and SharpOS/MOSA lack.
*all* of them use AOT to compile C# to native assembly, usually x86 assembly.
and that really isn't what i'd intend using C# for...i mean, it wasn't intended for AOT!
and to the people working on MOSA and Cosmos:
NO OFFENSE INTENDED.
i applaud the fact that you guys have working kernels ready, against all odds.
Re: C# compiler for Kernel dev?
Posted: Wed Dec 16, 2009 12:15 pm
by LordMage
Okay, I will go read your blueprint. I have downloaded Cosmos and read about the IL2CPU, but I wasn't sure if that is what I wanted. One question though.
I agree that JIT is probably better for security than AOT. but how do you have a JITed kernel. wouldn't that be impossible. If this is explained in your blueprint don't yell at me. I just wanted to get some questions out during lunch break. Also I was asking the question I asked to aquire links to the resources you are talking about with the C# CLR and IL explained. If I built the parts of the kernel I wanted using say the standard C# .NET compiler and then used IL2CPU would that even work for kernel dev? is there part of the runtime in windows that gets builtin that I would have to strip out first? that is why I was asking for an "out of the box" and yes to me that means that C# gets compiled to single file ASM code. anyway, I'm going to read your blueprint and see what I think, I will also search for the keywords you gave and hope I come up with the documentation I want.
Re: C# compiler for Kernel dev?
Posted: Wed Dec 16, 2009 8:14 pm
by AndrewAPrice
I'm working on a .Net JIT'er embedded into a kernel, and I have to say writing a C#->native or AOT compiler would be much harder than a JIT'er.
It is entirely possible to write a C# or IL front end for GCC. You'd need to link against a rather complex runtime (that is different to the library) that'll handle garbage collection, metadata (attributes, ToString(), boxing/unboxing, reflection), and events.
Re: C# compiler for Kernel dev?
Posted: Wed Dec 16, 2009 8:46 pm
by rootnode
@LordMage: At Mosa (
http://mosa-project.org and
http://github.com/Kintaro/Mosa) we're working on a compiler that can work as a JIT and AOT compiler for .NET. We have a pretty basic booting example. And small demonstration can be found here
http://www.youtube.com/watch?v=ob8Zg5qcpu4
If you need more information, just visit us on #mosa on freenode
Re: C# compiler for Kernel dev?
Posted: Thu Dec 17, 2009 7:11 am
by LordMage
@Redoktober: okay, I will read that blueprint today after work. I had to download OpenOffice because I don't have MS Office on my pc because its a relatively new install.
@MessiahAndrw: I would really like to see some of your stuff for putting the JITer in the kernel, your site doesn't seem to have been updated in a bit and only has info from an old version of your OS.
@MOSA: I couldn't read the text on that you tube video, but you compiled twice was that linking and then compiling or was that first compile to IL and then compile to AOT executable? forgive me if I don't use the proper terminology, I hope you get the jest of my question.
Thank everyone for the replies
Re: C# compiler for Kernel dev?
Posted: Thu Dec 17, 2009 7:51 am
by rootnode
@LordMage: There are 3 steps in the video. First: Compiling everything from C# -> IL, 2nd step is to compile the "kernel" from IL -> x86 and 3rd step is creating the disk images.
Re: C# compiler for Kernel dev?
Posted: Thu Dec 17, 2009 4:51 pm
by LordMage
@redoktober: Okay, finally skimmed over your blueprint. I am sure I missed something but I get the basic idea. I really like the idea of the micro .NET runtime but I am really wanting to dev a hobby OS I can run on x86_64. So I don't think your project would help me much until The micro .NET is compatible with common PC hardware or visa versa.
@rootnode: Okay, thats what I was kinda hopeing it was. given that is is possible to "strip" a C# project in visual studio of all namespace references and maybe change some of the project settings, then compile to IL, then compile to x86 and viola have a kernel? Or do I need to use a specific compiler to convert the C# Kernel code into IL?
Okay, I have my own bootloader that I have been working on for a while following Neons tutorials. They are great for creating a traditional Unmanaged OS using hardware protection and so on. So I would like to keep what I have learned from that but I want to use the C# managed Kernel so I will be creating my own disk image. I use Windows primarily although I dabble in linux from time to time. What I really want is a Bartok similar development enviroment where I code using visual Studio as my IDE. since the IDE and compiler are separate I should be able to do this. My main question is - Is the compiler being used for the mosa project, that is the compiler that takes C# and makes it IL, homegrown, of the shelf, or proprietary?
If it is homegrown can I get the source to study? If it is of the shelf where do I get it? If it is proprietary I guess I am sol.
Secondly, are you using IL2CPU like Cosmos? or do you have something different to convert the IL to x86 code?
Forgive me for asking questions that I am sure are probably on the MOSA site. I looked at it a few days ago and didn't have much luck finding things. I am looking again tonight and will postback if I find the answers before you post them. Thanks for being patient and the help.
Re: C# compiler for Kernel dev?
Posted: Thu Dec 17, 2009 6:22 pm
by rootnode
@LordMage: You can use the standard MS compiler or mono to get from C# to IL. The rest is done by our compiler.
To get from C# to IL I use mostly mono, or MS compiler if it happens that I work on windows.
But if you combine one of these two with our compiler, then it is similar to bartok. With the only difference that
our compiler is not only for AOT but for JIT also.
Re: C# compiler for Kernel dev?
Posted: Thu Dec 17, 2009 7:12 pm
by LordMage
Okay, you just made my day! WOOT! So if I have this right I can use VS2008 and make a C# program just with out the windows system namespaces, I'd have to make my own OS specific ones I'm assuming and compile that as a ?Class Library?Console App?, not sure what project type to use, then use your compiler with a certain command to AOT the kernel then add it to a disk image and poof I'm ready? I know it's not that easy but is that basically what you are saying?
Re: C# compiler for Kernel dev?
Posted: Thu Dec 17, 2009 7:17 pm
by rootnode
Yes, basically that's the idea and plan. The working sample (let's call it) "kernel" is written in pure C# in VS as an application.
We still lack some CIL operations, but we're near to finish them.
Re: C# compiler for Kernel dev?
Posted: Fri Dec 18, 2009 9:38 am
by redoktober
hmm.
i daresay, then it's MOSA and Cosmos you should be concentrating at.
i like the .NET micro framework for two reasons:
1) bootable runtime;
2) it supports ARM.
apart from the reasons i want a bootable runtime, i like the ARM architecture because it's pretty simple to write assembly code for, it's pretty inexpensive, has a low power requirement. perfect for robotics.
good luck!
Re: C# compiler for Kernel dev?
Posted: Sat Dec 19, 2009 12:03 am
by AndrewAPrice
LordMage wrote:@MessiahAndrw: I would really like to see some of your stuff for putting the JITer in the kernel, your site doesn't seem to have been updated in a bit and only has info from an old version of your OS.
Sorry about that, I don't see a point in updating my site just yet when I could be working on the kernel. My first version will only have a very minimal runtime library, yet I'm aiming for a complete compliant CLI runtime. You'll be able to run any .Net <= 2.5 assembly (even those depending on libraries if you also load them), however you won't be able to link to unmanaged code (which includes the Microsoft .Net and Mono libraries), but I'll allow unsafe code (so you can write to 0xb800 for text).
So basically, my kernel will be able to load a C# assembly and run it on raw hardware the same as GRUB can load an C++ ELF executable and run it on raw hardware. You can be sure I'll make an announcement when it's ready. Of course, interrupts and IO won't be in just yet.
My road-map for my proof-of-concept kernel is basically as follows:
Release 1: JIT'ing unsafe code and executing it on bare metal x86.
Release 2: Restrict to safe code, multithreading, and IO via the kernel.
Release 3: Multiple application domains (multitasking) and IPC.
Release 3 will mark the end of the proof-of-concept stage, and I'll design a much more useful kernel architecture and start using it in a practical way.
Re: C# compiler for Kernel dev?
Posted: Sat Dec 19, 2009 1:23 am
by rootnode
MessiahAndrw wrote:Sorry about that, I don't see a point in updating my site just yet when I could be working on the kernel. My first version will only have a very minimal runtime library, yet I'm aiming for a complete compliant CLI runtime. You'll be able to run any .Net <= 2.5 assembly (even those depending on libraries if you also load them), however you won't be able to link to unmanaged code (which includes the Microsoft .Net and Mono libraries), but I'll allow unsafe code (so you can write to 0xb800 for text).
We'll have a solution for that later. At the moment we're patching the mono libraries to make them fully managed and remove all P/Invokes from their mscorlib.
We already spoke to Miguel about that. If the patches are as fast as the unmanaged version they'll include them, otherwise there will be a configure flag to choose between unmanaged code and our managed patches. Maybe you could use them.