How Rust Can be used to implement more secure, reliable and High-performance Operating System

Madushan perera
5 min readJan 13, 2023

--

We can define Rust as a new and more secure programming language. Rust was released in 2010 by Mozilla. It is designed to be a language for extremely secure and concurrent systems. It is lightning fast, like C and C++, since it compiles to native code. This lesson uses a straightforward and useful way to explain the fundamentals of Rust programming.

Rust is a programming language that facilitates the creation of more reliable, quick applications. In programming language design, high-level ergonomics and low-level control frequently clash; Rust resolves this tension. Rust gives you the option to regulate low-level details (like memory use) without all the headaches typically associated with such control by striking a balance between robust technical capability and an excellent development experience.

Rust focuses on three goals,
• Safety
• Speed
• Concurrency

With different levels of experience in systems development, huge teams of engineers are finding Rust to be a useful tool for collaboration. Low-level code is vulnerable to a range of subtle defects that, in most other languages, can only be found through thorough testing and meticulous code review by expert developers. Rust’s compiler serves as a gatekeeper by forbidding the compilation of code containing these elusive defects, such as concurrency flaws. Working with the compiler allows the team to concentrate on the program’s logic rather than searching for bugs. Those Who Value Stability and Speed People who need stability and speed in a language should use rust. By speed, we mean both the speed at which Rust allows you to write programs and the speed at which you can develop programs using it. Through the inclusion of new features and refactoring, the Rust compiler’s tests guarantee stability. Developers are frequently hesitant to make changes to the fragile old code in languages without these checks.

Performance of Rust

Rust is a compiled language that converts code directly into machine code that can be executed, which produces efficient code. For many Rust apps, performance is crucial. Both rust-specific approaches and concepts that can be used with programs written in other languages are covered.
Because it doesn’t have a runtime or garbage collector, Rust can power mission-critical applications, run on embedded hardware, and communicate with other languages without any
issues.

Safe

Programming in Rust is very secure, as it performs numerous security checks at build time. While compiling, as opposed to when we run an application, the objective is to find as many errors as we can. A type-safe programming language is Rust. An error would appear if attempted to add an Int and a String, for example, or if passing the incorrect argument to a function.

Error handling

An error that can be fixed is referred to as a "recoverable error." When software runs into a recoverable error, it can either specify a different course of action or try the failed task again. Recoverable errors do not result in an abrupt program failure. The File Not Found error is an
illustration of a recoverable error. Unrecoverable errors result in sudden program failure. A program cannot return to its initial state after experiencing an unrecoverable error. It cannot correct the problem or attempt the unsuccessful operation again. Accessing a place past the end of an array is an illustration of an unrecoverable error.

Concurrency
Different sections of a program run independently when concurrent programming is used. Contrarily, with parallel programming, many portions of a program run concurrently. With the increasing use of several processors in computers, both models are equally crucial.

A Freestanding Rust Binary

The first thing we need to do is create a Rust executable that doesn’t link to the standard library, which is the basis for our own operating system kernel. Since there is no longer a requirement for an underlying operating system, Rust code may now be performed directly on hardware. To build
an operating system kernel, we need code that doesn’t rely on any features of the operating system. As a result, we are unable to access any features like threads, files, heap memory, the network, random numbers, and standard output that rely on OS abstractions or hardware. This makes
sense considering that we’re trying to create our own operating system and drivers. This suggests that even while we are unable to access the majority of the Rust standard library, we can still utilize a large number of its features. Just a few examples are iterators, closures, pattern matching, option and result, string formatting, and, of course, the ownership system. Because of these features, kernels may be written in a very expressive, high-level fashion without having to worry about memory safety or unpredictable behavior. To design an OS kernel in Rust, we must create an
executable that can operate without the aid of an underlying operating system. The terms “freestanding” and “bare-metal” executables are commonly used to describe these files.

A Minimal Rust Kernel

We used cargo to create the standalone binary, although various compilation settings and entry point names were required depending on the operating system. This is because cargo builds by default for the host system, which is the system you’re using. This is not what we want for our
kernel because it makes little sense for a kernel to run on top of, say, Windows. We want to compile with a certain target system in mind.

VGA Text Mode

Writing text on the screen is easy when using the VGA text mode. By enclosing every unsafety in a separate module, we develop an interface in this section that makes its usage safe and straightforward. We also provide support for the formatting macros in Rust.

VGA text buffer
The VGA text buffer is a two-dimensional array of 25 rows and 80 columns that is directly displayed on the screen. Two bytes, or 16 bits, are used to represent each character on the screen. The character attribute is represented by 8 bits, while the final 8 bits serve as the character set’s
code points. According to Unicode standards, a code point will be assigned to each character. For instance, “A” will appear as “U+0041.” UTF-8, UTF-16, and so on. In a nutshell, the first byte represents the character, and the second byte specifies how a character should be shown. The first four bits of the second byte determine the foreground color, the next three bits the background color, and the final bit the blink attribute. Because the VGA text buffer is reachable through memory-mapped I/O at the address 0xb8000, we may read and write to it using standard memory operations at a specific address. Let’s see how to include this in the kernel.

To implement this, the following steps should be used:

Implementing Steps

--

--