[PDF] [PDF] Reenix: Implementing a Unix-Like Operating System in Rust

Using the basic design and much of the lowest-level support code from the Weenix operating system written for CS167/9 I was able to create a basic kernel 



Previous PDF Next PDF





[PDF] Safe Kernel Programming with Rust - DiVA

the Rust programming language A Rust Kernel Programming Inter- face is designed and implemented, and a network device driver is then ported to Rust



[PDF] The Case for Writing a Kernel in Rust - Department of Computer

2 sept 2017 · In our kernel, unsafe code falls into two categories: Rust library code, written by language developers, and ker- nel code, written by kernel 



[PDF] A microkernel written in Rust

Rust Redox A microkernel written in Rust Porting the UNIX-like Redox OS to Arm Rust Redox What Name Aims History Stack Schemes Kernel Relibc



[PDF] Practical Safe Linux Kernel Extensibility - University of Washington

We then explore the feasibility of writing kernel extensions in a high-level, type safe language (i e , Rust) while preserving compatibility with Linux and nd this  



[PDF] Reenix: Implementing a Unix-Like Operating System in Rust

Using the basic design and much of the lowest-level support code from the Weenix operating system written for CS167/9 I was able to create a basic kernel 



[PDF] Experiences Building an Embedded OS in Rust - Tock OS

Rust [2], a new, safe language designed for systems programming, including operating system kernels, promises memory safety with no runtime overhead Rust 



[PDF] Qualitative and quantitative evaluation of writing an OS kernel in Rust

Rust can limit the scope of potential damage caused by drivers Page 10 Motivation and Goal • Kernel is responsible for every program in the computer Insecure 



[PDF] High Velocity Kernel File Systems with Bento - Computer Science

23 fév 2021 · written in safe Rust to be installed in the Linux kernel, with errors largely sandboxed to the file system Bento file systems can be replaced with 



[PDF] Exemple de notebook avec Rust

14 fév 2021 · Le kernel Rust n'est pas installé par défaut avec Jupyter Il faut installer evcxr et le kernel evcxr_jupyter 1 2 Exemples 1 2 1 Un exemple écrit 

[PDF] rust kernel module

[PDF] rust linux

[PDF] rust os phill

[PDF] rust programming language book github

[PDF] rv rentals in paris texas

[PDF] rv requirements

[PDF] rv seating capacity

[PDF] rvc form e

[PDF] rwandan francs to dollars in 1994

[PDF] rythme cardiaque au repos

[PDF] rythme cardiaque au repos femme

[PDF] rythme cardiaque au repos selon l'age

[PDF] s corp tax return due date

[PDF] s1 form france

[PDF] s1106 ameli

Reenix: Implementing a Unix-Like Operating System inRust

Alex Light (alexanderlight@brown.edu)

Advisor: Tom Doeppner

Reader: Shriram Krishnamurthi

Brown University, Department of Computer Science

April 2015

Abstract

This paper describes the experience, problems and successes found in implementing aunix-like operating

system kernel inrust. Using the basic design and much of the lowest-level support code from theWeenix

operating system written forCS167/9I was able to create a basic kernel supporting multiple kernel processes

scheduled cooperatively, drivers for the basic devices and the beginnings of a virtual le system. I made

note of where therustprogramming language, and its safety and type systems, helped and hindered my work and made some, tentative, performance comparisons between therustandCimplementations of this kernel. I also include a short introduction to therustprogramming language and theweenixproject.

Contents

1 Introduction 1

1.1 The Weenix OS . . . . . . . . . . . . .

2

1.2 TheRustlanguage . . . . . . . . . . .2

2 Reenix 7

2.1 Organization . . . . . . . . . . . . . .

7

2.2 Booting . . . . . . . . . . . . . . . . .

8

2.3 Memory & Initialization . . . . . . . .

8

2.4 Processes . . . . . . . . . . . . . . . .

9

2.5 Drivers . . . . . . . . . . . . . . . . . .

11

2.6 KShell . . . . . . . . . . . . . . . . . .

13

2.7 Virtual File System . . . . . . . . . . .

13

2.8 Other Challenges . . . . . . . . . . . .

15

2.9 Future Work . . . . . . . . . . . . . .

17

3 Rust Evaluation 18

3.1 Benets of Rust . . . . . . . . . . . . .

18

3.2 Challenges of Rust . . . . . . . . . . .

20

3.3 Critical Problem: Allocation . . . . . .

21

3.4 Performance . . . . . . . . . . . . . . .

24

4 Conclusions 251 Introduction

Ever since it was rst created in 1971 theUNIXoperat- ing system has been a xture of software engineering. One of its largest contributions to the world of OS engineering, and software engineering in general, was theCprogramming language created to write it. In the 4 decades that have passed since being released,C has changed relatively little but the state-of-the-art in programming language design and checking, has ad- vanced tremendously. Thanks to the success ofunix almost all operating system kernels have been written largely inCor similar languages likeC++. This means that these advances in language design have largely passed by one of the elds that could most benet from the more expressive, more veriable languages that have come afterC. The goal of this project is to try to create aunix- like operating system kernel using therustprogram- ming language. As far as I know, this is a project that has never really been attempted seriously, nor had anyone made much progress on before now 1. By doing so I will be able to explore the feasibility and convience of creating a kernel with a higher-level1 All otherrustoperating systems I was able to nd were little more than toys capable of running basicrustcode. Few had any notion of threads of execution and even fewer had any form of process control beyond starting new threads. None had any form of drivers beyond painting the screen and maybe echoing key presses without processing them. 1 language such asrust, as well as nd where the lan- guage could be improved to better suit this purpose. Furthermore, it will allow us to evaluate how well the basic design ofunixholds up when examined through a language other thanC. Finally, we will see how the much more sophisticated type and safety system of rusthandle the complicated task of verifying a ker- nel.

In order to allow me to begin working on the more

high-level parts of the kernel faster, I based my ef- fort o of theweenixoperating system that is im- plemented inCS169. This allowed me to not worry about implementing many of the lowest level pieces of the kernel, such as the memory allocators, which are not specic to operating system development.

1.1 The Weenix OS

TheWeenixoperating system is a smallx86based

teaching OS created in 1998 for use with Brown's

CS167course on operating systems[12]. Today, stu-

dents in the optional lab courseCS169attached to

CS167implement much of the higher level pieces of

aunix-like OS withweenix. Students doing this project start out with the code necessary to get the OS booted and runningCcode, with memory- management, a debug-printing system, and a basic libcimplementation. Using this as their base,CS169 students then proceed to implement a fairly complete unixOS. The project, and its support code, are writ- ten almost entirely inC, with some of the initial boot code beingx86 assembly, and some python and shell scripts for running and testing the OS. This project is split into multiple parts, commonly referred to as,PROCS,DRIVERS,VFS,S5FS, &VM. For

PROCS, they implement aunix-style process model,

with parent-child relationships among processes and ainitprocess, as well as a simple scheduler and syn- chronization primitives. DuringDRIVERS, they imple- ment large parts of aTTYdriver, allowing user input and interaction, as well as a (very bare-bones) ATA driver allowing use of a hard-disk. ForVFS, they im- plement a virtual le system type abstraction, using a provided ram-backed le system calledRamFSfor testing. InS5FS, a version of thesysv-fsle system, called theS5le system, is implemented to allow real non-volatile storage. Finally forVMa virtual memory and user-space is implemented. There are also many provided user-space utilities that allow one to test the nal OS.1.2 TheRustlanguage

Therust2programming language is a relatively new

systems programming language being made by the Mozilla foundation. It is designed to be usable as a replacement forCin the low-level and embedded pro- gramming environments that are common for small and high-performance software. The Mozilla Founda- tion is currently usingrustin a few ocial projects, including therustcompiler (rustc), and an exper- imental web-browser calledServo. It also plans to begin usingrustcode in its popularFirefoxweb- browser in the near future[4].Rustis currently being developed and hosted on Github

3. The project is

very popular and open, with thousands of contribu- tors, most of whom are not associated with Mozilla.

Rustitself is a procedural programming language

withC-like syntax. It uses its very comprehensive type system, a data `lifetime' system, and an ex- tremely small runtime to ensure memory and thread safety during compile time. Specically,rustuses its ownership and lifetime tracking system to ensure that data is not unexpectedly modied when it is still being used by another object. Furthermore, it uses the lifetime tracking system to ensure that there are no dangling pointers possible in the language. The runtime of rust is made up of several pieces, many of which are separable. Its only required (and most ba- sic) function is to simply recover from out-of-memory or other fatal errors. In most cases, including most ofreenix, it also provides an interface for allocation of heap memory. All other functions of the runtime are essentially just to provide a consistent interface to the underlying operating system it is running on, al- lowing disk-io, inter-process communications and the creation of threads, among other things.

1.2.1 Syntax & Semantics

The syntax ofrustis similar to, but slightly dier- ent from most otherC-like programming languages. Figure 1 contains a basic quicksort implementation in rustthat I will use to illustrate some of the languages features. A full description of therustsyntax and se- mantics can be found online at doc.rust-lang.org 4.

The most notable dierence is thatrusthas a

somewhat dierent divide between expressions and statements. Inrustan expression is any piece of code that yields a value. A statement, on the other2 http://www.rust-lang.org(April 2015)

3https://github.com/rust-lang/rust(April 2015)

4http://doc.rust-lang.org/reference.html(April 2015)

2

1//!A b asicq uick-sorti mplementation2

3///A t ypeg enericq uick-sort.' T'i st het ypew ea res orting, i tm usth avea t otalo rdering4///( implementt he' Ord't rait).I tt akesa l istb yv aluea ndr eturnsa s ortedl istc ontainingt he5///s amee lementss orted.W es ayt hatt hisp assedi nl isti sm utables ow ec anm odifyi t.6pubf nq uicksort< T:O rd>(mutl st:V ec)- >V ec{ 7//G ett hef irste lementa so urp ivot,P opw illr eturnN one( andg ot ot hee lseb ranch)i ft his8//l isti se mpty,o therwisei tw illr emovet hef irste lementf romt hel ista ndr eturni t.9ifl etS ome(pivot)= l st.pop(){ 1?//S plitl ista roundt hep ivot.W ei teratet hrought hel ist( into_iterf unction)a nd11//p artitioni ti ntot wol ists.T hep artitionf unctiont urnsa ni teratori ntoa p airo f12//l istsw heret hef irsti sa l isto fa lle lementsw heret hec onditiong iveni st ruea nd13//t heo theri sf alse.14let( less,m ore):( Vec<_>,V ec<_>)= l st.into_iter().partition(|x|x < & pivot);15//R ecursivelys ortt heh alfo ft hel istl esst hent hep ivot.T hisw illb et hes tarto fo ur16//r eturnedl ist.17letm utr es= q uicksort(less);18//P usht hep ivote lemento ntot hee ndo ft hes ortedl owerh alfo ft hel ist.T hisa ppends19//t hep ivoto ntot he' res'l ist.2?res.push(pivot);21//S ortt hel argerh alfo ft hel ista nda ppendi tt ot hes ortedl owerh alfa ndp ivot.22//e xtendw illa ppendt hee ntireg ivenl isto ntot he' res'l ist.23res.extend(quicksort(more));24//R eturnt hen ows ortedl ist.N otet her eturns tatementi sn otr equiredh ere.S imply25//m akingt hisl ine" res"( notet hel acko fa " ;")w ouldb ee quivalents incet hef unction26//w illr eturnt hev alueo ft hel aste xpression( thisi f-else)w hicht akest hev alueo ft he27//l aste xpressioni ni tsb ranches( Vec).28returnr es;29}e lse{ 3?//S incel st.pop()r eturnedN onet hel istp assedi nm ustb ee mptys ow ew illr eturna n31//e mptyl isth ere.N otet hatr eturni sn otn eededb ecauset hisi st hel aste xpressioni na 32//b locka ndt hisb locki st hel aste xpressioni nt hef unction.v ec!i sa s tandardm acro33//t hatc reatesa V ec.34vec![]35}36}37

38fnm ain(){ 39//C reatea l istt os ort.v ec!i sa m acroa ndw illc reatea v ecc ontainingt hee lementsl isted.4?letl st= v ec![3,1,5,9,2,8,4,2,?,3,12,4,9,?,11];41println!("unsorted:{ :?}",l st);42//C allq uicksort.T hisr elinquisheso wnershipo fl st.43println!("sorted:{ :?}",q uicksort(lst));44}

Figure 1:Arustquicksort

3

1///A t rait.S tructsa nde numsc ani mplementt his.2pubt raitI d{ 3///A r equiredf unction.A lli mplementersm ustp rovidea d efinitionf ort hisf unctiono re lse4///t ype-checkingw illf ail.T he" staticm eanst her eturneds tringm ustb es tatically5///a llocated.6fnu sername(&self)- >& "statics tr;7///A f unctionw itha d efaulti mplementation.T her eturneds tringm ustb eu sablea tl easta s8///l onga st heI de xists.T he" am eanst hatt her eturneds trm ustb eu sablea tl easta sl ong9///a s" self"i s.T het ypec heckerw ille nsuret hisi st rue.1?fns creenname< "a>(&"as elf,_ board:& str)- >& "as tr{ s elf.username()} 11}12

13///A s tructure.T hed erivep rovidesd efaulti mplementationsf ort heg ivent raits.O nlyc ertain14///t raitsm ayb ei mplementedi nt hisw ay.15#[derive(Debug, Eq, PartialEq)]16pubs tructA ccount{ n ame:& "statics tr,m sgs:V ec,} 17

18//I mplementingt heI dt rait.N otew ed on otn eedt op rovidea " screenname" i mplementations ince19//t herei sa d efaultv ersion.2?implI df orA ccount{ 21fnu sername(&self)- >& "statics tr{ s elf.name} 22}23

24//F unctionsa ssociatedw ithA ccountd irectly.25implA ccount{ 26pubf ng et_messages(&self)- >& [u64]{ & self.msgs[..]} 27}28

29#[derive(Debug, Eq, PartialEq)]3?pube numC ommenter{ 31///A ne numv ariantw ithd ata32User(Account),33///A ne numv ariantw ithoutd ata34Anon,35}36

37///I mplementt heI dt rait.38implI df orC ommenter{ 39fnu sername(&self)- >& "statics tr{ 4?//W et aked ifferenta ctionsd ependingo nt hev ariant.41match* self{ 42Commenter::User(refa )= >a .username(),43Commenter::Anon =>" Anon",44}45}46}

Figure 2:Rusttraits and types

4 hand does not create a value. Within functions ev- erything is generally an expression except for (1)let variable bindings, such as on lines 14, 17, and 40 of Figure 1, (2) looping constructs, and (3) any expres- sion or statement with a semicolon (;) placed after it. Note that blocks, delimited by curly-braces (fg) are also expressions, using the value of the last expres- sion they contain as their value. In the same vein bothif-elseandmatchblocks are also expressions. In Figure 1 theif-elseblock beginning on line 9 is an expression of typeVec, for example.Rusttakes this idea of the nal expression of a block being its value even further, placing an implicitreturnbefore the nal top-level expression in a function (in Fig- ure 1 this is theif-elsestarting on line 9); one may still use `return ;' to return earlier, however. This can be seen on lines 41-44 of Figure 2, where the result of thematchis what is returned by the func- tion. Furthermore, this means that we could change line 28 of Figure 1 to simply be `res' and the meaning of the program would remain the same.

Another notable dierence fromCis thatrustis

fully immutable by default. In order to use an object mutably one must declare itmut, as is done in line

17 of Figure 1. One must do this even for function

arguments, which is why there is amutbefore the argumentlston line 6 of Figure 1. This immutable default extends to pointers, which must be declared &mutto be used mutably. Rusthas a syntax for declaring structures and enu- merations that is very similar toC. One of the main dierences is that enumerations can have data asso- ciated with them. In Figure 2 on line 30 we see the denition of an enumeration where one of its vari- ants (User) has data of the typeAccountassociated with it. This data can be used inmatchexpressions, such as on line 42 of Figure 2. Inrustthere are alsotraits, which are similar toJavainterfaces and may have default function denitions associated with them. Usingtraits it is much easier to create generic functions than it is inC. For example, the quicksort implementation in Figure 1 only requires that the objects being sorted implement theOrdtrait, mean- ing they have a total ordering. We can see theId trait be dened on lines 2-11 of Figure 2, it is imple- mented for theAccounttype on line 20 and for the

Commentertype on line 38. Both enumerations and

structures can have methods implemented on them directly or throughtraits. TheCommentertrait has agetmessagesfunction implemented on it on line 26 of Figure 2.Rustwill transparently redirect functioncalls through virtual method tables (vtables) 5when appropriate to allow handling objects astraitpoint- ers. This also makes it much easier to write generic code, as well as to hide implementation details in a much more straightforward way than is possible inC.

Rustalso supports declaring anonymous functions.

Anonymous functions are declared by having a list of arguments surrounded by pipes (|) followed by a sin- gle expression. Type annotations similar to those on normal functions are allowed, but optional. The re- turn and argument types will be inferred if they are absent. An anonymous function is used on line 14 of Figure 1. On this line it is used to distinguish be- tween items less then the pivot so that thepartition function can split the items in the list into two lists.

Figure 1 also makes use of therustmacro system.

Inrustmacros are pieces of code that transform the abstract syntax tree (AST)

6at compile time, rather

than just the raw text of the source code asCmacros do. Macros may be implemented using a special macro Domain Specic Language (DSL)

7or by writ-

ing a compiler-plugin forrustc[5]. Both systems allow the creation of hygienic macros, where there can be no name collision and the meaning is (mostly) independent of the context it is used in. The macro

DSL does not allow any compile-time computation

beyond pattern matching and has no explicit quasi- quote operator

8, however compiler plugins may do

both these things. Macros are identied by the exclamation-mark (!) that ends their name. They may expand to be either statements or expressions and may (generally) be nested. In Figure 1 I make use of thevec![...]macro, which creates aVec lled with the arguments given to the macro, on lines

34 and 40.

Rustalso has fairly robust support for pattern5

VTables are structures containing function-pointers used to allow types to specify dierent implementations of standard functions for their use. They are similar to interfaces inJava.

6An AST is a representation of a program as a tree, with

the nodes and edges representing the syntactic elements of the language. The tree as a whole represents the parse of the pro- gram being examined. It is used as the representation of a pro- gram during compilation, optimization, and macro-expansion.

7A DSL is a programming language created for some spe-

cic purpose. It is usually quite well suited for use in this domain but is less powerful or more dicult to use than more general languages. Some commonly used DSLs are the regular- expression syntax used by perl, the Hyper-text Markup Lan- guage (HTML) commonly used for creating web-pages, and the typesetting language L ATEX.

8A quasi-quote is an operator that turns a given piece of

text into an AST for the text. Using a related operation calledquotesdbs_dbs17.pdfusesText_23