Cosmos (operating system)


C# Open Source Managed Operating System is a toolkit for building operating systems, written mostly in the programming language C# and small amounts of a high level assembly language named X#. Cosmos is a backronym, in that the acronym was chosen before the meaning. It is open-source software released under a BSD license. It is referred to as OS Legos
Cosmos encompasses an AOT compiler named IL2CPU to translate Common Intermediate Language into native instructions. Cosmos compiles user-made programs and associated libraries using IL2CPU to create a bootable native executable that can be run with no support. The resulting output can be booted from a USB flash drive, CD-ROM, over a network via Preboot Execution Environment, or inside a virtual machine. Recent releases also allow deploying to certain x86 embedded devices over Universal Serial Bus. While C# is the primary language used by developers, many CLI languages can be used, provided they compile to pure CIL without the use of Platform Invocation Services. Cosmos is mainly intended for use with.NET Framework, with Mono support in the works.
, Cosmos does not aim to become a full operating system, but rather a toolkit to allow other developers to simply and easily build their own operating systems using the.net framework. It also functions as an abstraction layer, hiding much of the inner workings of the hardware from the eventual developer.
Older versions of Cosmos were released in Milestones, with the last being Milestone 5. More recently, the project switched to simply naming new releases after the latest commit number. Releases of Cosmos are divided into two types: the Userkit, and the Devkit. The Userkit"is a pre-packaged release that is updated irregularly, as new and improved features are added. Userkits are generally considered stable, but do not include recent changes and may lack features. The Devkit refers to the source code of Cosmos and must be built manually. The devkits are usually unstable as they may contain unfinished or untested code. The Devkit can be acquired on GitHub and uses Git as the source control management. Most work on Cosmos is currently aimed at improving debugger functionality and Visual Studio integration. Kernel work is focused on implementing file systems, memory management, and developing a reliable network interface. Syslinux serves as the project's bootloader.

Developing with Cosmos

Cosmos has many facilities to improve the experience of developing operating systems with it, designed to make the process as fast and painless as possible, knowledge of the assembly language is not required to use Cosmos.

Visual Studio integration

A key feature of Cosmos, which separates it from other operating systems of its type, is its tight integration with Microsoft Visual Studio. Code can be written, compiled, debugged, and run entirely through Visual Studio, with only a few key presses. Cosmos no longer supports Visual Studio 2015 or Visual Studio 2017, now it only supports Visual Studio 2019.

Debugging

Cosmos can be seamlessly debugged through Visual Studio when running over PXE or in a virtual machine. Many standard debugging features are present, such as breakpoints, tracing, and logging. Additionally, debugging can be done via serial cables, if running on physical hardware. When running in VMWare, Cosmos supports stepping and breakpoints, even while an operating system is running.

Running

Cosmos uses virtualization to help speed development by allowing developers to test their operating systems without having to restart their computers as often. By default, VMWare Player is used, due to its ease of use in terms of integration with the project. Other virtualization environments are supported as well, such as Bochs and VirtualPC. An ISO disk image may also be generated that can be burned to a USB flash drive, CD-ROM, or similar media.
PXE booting is also supported, allowing for remote machines to run Cosmos over a network connection.

Compile process

IL2CPU

To compile.NET CIL into assembly language, Cosmos developers created an ahead-of-time compiler named IL2CPU, designed to parse CIL and output x86 opcodes.
is an AOT compiler that is written using a Common Intermediate Language compliant language. It translates Common Intermediate Language to machine code.

X#

X# is a low-level programming language developed for the x86 processor architecture as a part of Cosmos operating system to make operating system development easier. X# is designed to bring some of C-like language syntax to assembly language. In the beginning, X# was an aid for debugging services of Cosmos. The X# compiler is an open source console interface program with an atypical architecture. It parses lines of code into tokens and compares them with patterns. Finally, matched X# code patterns are translated to intel syntax x86 assembly, usually for the NASM compiler. In first versions, X# operation was mostly 1:1 with assembly code but hasn't been the reason why the X# compiler was written.

Syntax

The syntax of X# is simple. Despite being similar to C, X# syntax differs and is stricter.

Comments

X# supports only one kind of comment, the C++-style single line comment, started with a double forward slash - //.

Constants

X# supports the definition of named constants which are declared outside of functions. Defining a numeric constant is similar to C++ - for instance, const i = 0. Referencing the constant elsewhere requires a # before the name - "#i", for instance.
Labels in X# are mostly equivalent to labels in other assembly languages. The instruction to jump to a label uses the goto mnemonic, as opposed to the conventional jump or jmp mnemonic.

CodeLabel1:
goto CodeLabel2:

Namespaces

X# program files must begin with a namespace directive. X# lacks a namespace hierarchy, so any directive will change the current namespace until it's changed again or the file ends. Variables or constants in different namespaces may have the same name as the namespace is prefixed to the member's name on assembly output. Namespaces cannot reference each other except through "cheats" using native-assembly-level operations.

namespace FIRST
// Everything variable or constant name will be prefixed with FIRST and an underscore. Hence the true full name of the below variable
// is FIRST_aVar.
var aVar
namespace SECOND
// It's not a problem to name another variable aVar. Its true name is SECOND_aVar.
var aVar
namespace FIRST
// This code is now back to the FIRST namespace until the file ends.

Functions

All X# executive code should be placed in functions defined by the 'function' keyword. Unlike C, X# does not support any formal parameter declaration in the header of the functions, so the conventional parentheses after the function name are omitted. Because line-fixed patterns are specified in syntax implemented in code parser, the opening curly bracket can't be placed on the next line, unlike in many other C-style languages.

function xSharpFunction

Because X# is a low-level language, there are no stack frames inserted, so by default, there should be the return EIP address on the top of the stack.
X# function calls do contain arguments enclosed in parentheses, unlike in function headers. Arguments passed to functions can be registers, addresses, or constants. These arguments are pushed onto the stack in reverse order. Note that the stack on x86 platforms cannot push or pop one-byte registers.

function xSharpFunction
function anotherFunction

The return keyword returns execution to the return EIP address saved in the stack.

Arithmetic and bitwise operations

X# can work with three low-level data structures: the registers, the stack and the memory, on different ports. The registers are the base of all normal operations for X#. A register can be copied to another by writing DST = SRC as opposed to mov or load/store instructions. Registers can be incremented or decremented just as easily. Arithmetic operations are written as dest op src where src is a constant, variable, or register, and dest is both an operand and the location where the result is stored.
Examples of assignment and arithmetic operations are shown below.

ESI = 12345 // assign 12345 to ESI
EDX = #constantForEDX // assign #ConstantForEDX to EDX
EAX = EBX // move EBX to EAX => mov eax, ebx
EAX-- // decrement EAX => dec eax
EAX++ // increment EAX => inc eax
EAX + 2 // add 2 to eax => add eax, 2
EAX - $80 // subtract 0x80 from eax => sub eax, 0x80
BX * CX // multiply BX by CX => mul cx -- division, multiplication and modulo should preserve registers
CX / BX // divide CX by BX => div bx
CX mod BX // remainder of CX/BX to BX => div bx

Register shifting and rolling is similar to C.

DX << 10 // shift left by 10 bits
CX >> 8 // shift right by 8 bits
EAX <~ 6 // rotate left by 6 bits
EAX ~> 4 // rotate right by 4 bits

Other bitwise operations are similar to arithmetic operations.

DL & $08 // perform bitwise AND on DL with 0x08 and store the result in DL
CX | 1 // set the lowest bit of CX to 1
EAX = ~ECX // perform bitwise NOT on ECX and store the result in EAX
EAX ^ EAX // erase EAX by XORing it with itself

Stack

Stack manipulation in X# is performed using + and - prefixes, where + pushes a register, value, constant or all registers onto the stack and - pops a value to some register. All constants are pushed on stack as double words, unless stated otherwise.

+ESI // push esi
-EDI // pop into edi
+All // save all registers => pushad
-All // load all registers => popad
+$1badboo2 // push 0x1badboo2 on the stack
+$cafe as word // \/
+$babe as word // push 0xcafebabe
+#VideoMemory // push value of constant VideoMemory

Variables

Variables are defined within namespaces using the var keyword. Arrays can be defined by adding the array's type and size on the end of the declaration. Variables and arrays are zeroed by default. To reference a variable's value, it must be prefixed with a dot. Prefixing that with an @ will reference the variable's address.

namespace XSharpVariables
var zeroVar // variable will be assigned zero
var myVar1 = $f000beef // variable will be assigned 0xf000beef
var someString = 'Hello XSharp!' // variable will be assigned 'Hello XSharp!\0',
var buffer byte // variable of size 1024 bytes will be assigned 1024 zero bytes
...
EAX =.myVar1 // moves value of myVar1 to EAX
ESI = @.someString // moves address of someString to ESI
CL =.someString // moves first character of someString to CL
.zeroVar = EAX // assigns zeroVar to value of EAX

X# can access an address with a specified offset using square brackets:

var someString = 'Hello XSharp!' //variable will be assigned to 'Hello XSharp!\0'
...
ESI = @.someString // load address of someString to ESI
CL = 'B' // set CL to 'B'
CH = ESI // move second character from string to CH
ESI = $00 // end string
//Value of someString will be 'Bell'

Comparison

There are two ways of comparing values: pure comparison and if-comparison.
Here are two ways of writing a X# string length function:

// Method 1: using pure comparison
function strlen

Writing Cosmos code

An operating system built with Cosmos is developed in a similar fashion to any.NET C# console program. Additional references are made in the start of the program which give access to the Cosmos libraries. These libraries override the system libraries normally used in C# console programs since the resulting binary won't be running on an installation of Microsoft Windows.

User Kit and Visual Studio

The Cosmos User Kit is a part of Cosmos designed to make Cosmos easier to use for developers using Microsoft Visual Studio. When installed, the user kit adds a new project type to Visual Studio, called a Cosmos Project. This is a modified version of a console application, with the Cosmos compiler and bootup stub code already added.

Compiling a project

Once the code is complete, it may be compiled using.NET compiler. This converts the application from the original source code into Common Intermediate Language, the native language of.NET Framework. Next the application is run, showing the Cosmos Builder Window, which presents the developer with options which determine exactly how the project is compiled. These options include how to boot the project - via emulators such as Quick Emulator, Virtual PC, and VMWare, writing to a disk image file that can later be written to a CD-ROM, or via Preboot Execution Environment network booting - as well as debug options using Cosmos' built-in debugger, and other options.
When the user has chosen their desired options, they press the Build button. This invokes the IL2CPU compiler which systematically scans through all of the applications CIL code, converting it into assembly language for the selected processor architecture., only x86 is supported. Next, Cosmos invokes the selected assembler to convert this assembly language code into native central processing unit opcode. Finally, the desired output option is activated, be this starting an emulator, starting a PXE engine, or producing an ISO disk image file from the binary code opcode.

Debug options

Cosmos offers several options as to how to deploy the resulting OS and how to debug the output.

Virtualization

Cosmos allows users to boot the operating system in an emulated environment using a virtual machine. This lets developers test the system on their own computer without having to reboot, giving the advantages of not requiring extra hardware or that developers exit their development environment. Currently, only VMWare is supported. Bochs support is underway. QEMU and VirtualBox are not officially supported.

Disk images

This option writes the operating system to a disk image file, which can be loaded into some emulators or written to a CD-ROM and booted on real hardware. This option also allows deploying to a USB mass storage device, such as a USB flash drive, to boot on devices that may not have an optical disc drive. Because networking is not in place yet, debugging is unsupported with this deploy option.

PXE network boot

This option allows the operating system to boot on real hardware. The data is sent via a local area network to the client machine. This requires two computers: one as the client machine and one as the server. It also requires a network connecting the two computers, a client machine with a network card, and a Basic Input/Output System that can PXE boot., debugging over a network is unsupported.

Cosmos assembler

The Cosmos Project team have also created an assembler that is designed to eventually become the main assembler for the Cosmos system. However, the assembler is still inefficient and slow, and so the Netwide Assembler is used instead.

News coverage