C Sharp Bare Bones: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
No edit summary
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(3 intermediate revisions by 3 users not shown)
Line 3:
 
This tutorial is intended to help you write a simple 'Hello World' OS in [[C Sharp | C#]] which you can then compile to machine code for the IA32 architecture and boot via [[GRUB]]. There are a number of tools for compiling C# to [[CIL]], including Microsoft's csc (distributed with [http://www.microsoft.com/net Microsoft.NET]) and mcs/gmcs/dmcs (distributed with [http://www.mono-project.com Mono]). In addition there are a number of tools for compiling [[CIL]] to native machine code in an ahead-of-time manner, including Microsoft's ngen, mono (with the --aot option), [http://gocosmos.org Cosmos'] IL2CPU, mosacl from the [http://www.mosa-project.org/projects/mosa MOSA project] and tysila from the [http://www.tysos.org/redmine/projects/tysos/ tysos] project. Given the author's familiarity with tysos, that is what we will focus on here.
 
[[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]] [[File:Eiriley.jpg]]
 
==Prerequisites==
Line 15 ⟶ 13:
 
This is only required if you have not downloaded the pre-compiled binaries above. Use subversion to get the latest sources 'svn co http://www.tysos.org/svn/trunk tysos' (not in Wayback Machine), or download the latest tar ball from http://www.tysos.org/files/src/tysos-latest.tar.bz2 (http://web.archive.org/web/*/http://www.tysos.org/files/src/tysos-latest.tar.bz2). Tysos is a project developing a full OS kernel and drivers in C#, however we only want the compiler from it therefore we only want to compile part of the build tree. Enter the tysos directory and run
<sourcesyntaxhighlight lang="bash">
cd tybuild && make && cd ..
cd mono/corlib && make mscorlib.dll && cd ../..
cd tysila2 && make && cd ..
</syntaxhighlight>
</source>
 
You will need to put the mono/corlib/mscorlib.dll, tysila2/bin/Release/tysila2.exe, tysila2/bin/Release/libsupcs.dll, tysila2/bin/Release/libtysila.dll, tysila2/bin/Release/tydbfile.dll, tysila2/bin/Release/tydisasm.dll and tybuild/bin/Release/tybuild.exe files somewhere in your path.
Line 33 ⟶ 31:
This is the assembly stub which will contain a Multiboot header.
 
<sourcesyntaxhighlight lang="asm">
global sthrow
 
Line 54 ⟶ 52:
hlt
jmp sthrow
</syntaxhighlight>
</source>
 
===kernel.cs===
Line 60 ⟶ 58:
This is the actual simple kernel - it just prints a message to the screen.
 
<sourcesyntaxhighlight lang="csharp">
namespace BareBones
{
Line 91 ⟶ 89:
}
}
</syntaxhighlight>
</source>
 
===linker.ld===
Line 97 ⟶ 95:
The linker script
 
<sourcesyntaxhighlight lang="c">
ENTRY (_start)
 
Line 125 ⟶ 123:
}
}
</syntaxhighlight>
</source>
 
===iso/boot/grub/grub.cfg===
This is a short file to tell grub where to find our kernel
<sourcesyntaxhighlight lang="c">
multiboot /kernel.bin
boot
</syntaxhighlight>
</source>
 
==Building it all==
The following commands should build your new C# kernel. First, assemble the multiboot stub:
<sourcesyntaxhighlight lang="bash">nasm -felf -o loader.o loader.asm</sourcesyntaxhighlight>
To compile the .cs file to a .exe you have a choice of three options (depending on your architecture):
<sourcesyntaxhighlight lang="bash">
gmcs /target:exe /out:kernel.exe /unsafe kernel.cs
csc /target:exe /out:kernel.exe /unsafe kernel.cs
tybuild.exe /unsafe kernel.cs
</syntaxhighlight>
</source>
To compile kernel.exe to machine code we use tysila:
<sourcesyntaxhighlight lang="bash">tysila2.exe --arch i586-elf-tysos -fno-rtti -o kernel.o kernel.exe</sourcesyntaxhighlight>
Here, the -fno-rtti switch disables run-time type information, support for which would greatly enlarge the size of your kernel and require you to provide a great number of run time functions to support this.
 
To link:
<sourcesyntaxhighlight lang="bash">ld -m elf_i386 -T linker.ld -o iso/kernel.bin loader.o kernel.o</sourcesyntaxhighlight>
 
Then we make a bootable iso image with:
<sourcesyntaxhighlight lang="bash">grub-mkrescue -o barebones.iso iso</sourcesyntaxhighlight>
 
And run it on qemu with:
<sourcesyntaxhighlight lang="bash">qemu-system-i386 -cdrom barebones.iso</sourcesyntaxhighlight>
 
[[Category:Bare bones tutorials]]