RustyGecko - Developing Rust on Bare-Metal An experimental embedded software platform Håvard Wormdal Høiby Sondre Lefsaker Master of Science in Computer Science Submission date: June 2015 Supervisor: Magnus Lie Hetland, IDI Co-supervisor: Antonio Garcia Guirado, IDI Marius Grannæs, Silicon Labs Norwegian University of Science and Technology Department of Computer and Information Science Preface This report is submitted to the Norwegian University of Science and Technology in fulfillment of the requirements for master thesis. This work has been performed at the Department of Computer and Information Science, NTNU, with Prof. Magnus Lie Hetland as the supervisor, Antonio Garcia Guirado (ARM), and Marius Grannæs (Silicon Labs) as co-supervisors. The initial problem description was our own proposal and further developed in co-operation with our supervisors. Acknowledgements Thanks to Magnus Lie Hetland, Antonio Garcia Guirado, and Marius Grannæs for directions and guidance on technical issues and this report. Thanks to Silicon Labs for providing us with development platforms for us to develop and test our implementation on. Thanks to Antonio Garcia Guirado for the implementation of the CircleGame application for us to port to Rust and use in our benchmarks. Thanks to Itera w/Tommy Ryen for office space. A special thanks to Leslie Ho and Siri Aagedal for all the support and for proofreading the thesis. Sondre Lefsaker and H˚avard Wormdal Høiby 2015-06-14 i Project Description The Rust programming language is a new system language developed by Mozilla. With the language being statically compiled and built on the LLVM compiler infras- tructure, and because of features like low-level control and zero-cost abstractions, the language is a candidate for use in bare-metal systems. The EFM32 series of microcontrollers focuses on energy efficiency and for their ability to function in power constrained environments. The tool suite by the vendor, Silicon Labs, includes energy monitoring tools to analyze energy consumption at the source code level. The series is based on the ARM Cortex-M processor family. With ARM's move towards using LLVM in their own toolchain, a back-end for the Cortex-M series is already available. The goal of the project is to explore programming for the EFM32 series of micro- processors with the Rust Programming Language. This should be realized by using as much of the features and tools available in the Rust ecosystem as deemed fit. This whole platform should be evaluated and compared to the existing C platform provided by Silicon Labs. iii Abstract Embedded computer systems are an invisible, ever-growing part of our lives. Through market trends, like the Internet of Things, these computers are brought to new do- mains. These constrained systems set different requirements to the tools used to develop software, compared to the conventional systems found in mobile, desktop, and server computers. In recent decades, the number of programming languages have flourished on con- ventional computer systems. The traditional categorization of high-level languages have shifted from static and hardware platform-agnostic languages like C, to the dynamic and highly managed languages like JavaScript. The safety mechanisms provided by these new high-level languages come at a cost of the low-level control found in low-level languages. Rust is an emerging programming language that has a new take on this trade-off be- tween control and safety. This language takes a static approach for guaranteeing the safety, which a high-level language needs to ensure with dynamic checking. In this thesis, we present our experiments and evaluate the result of bringing Rust to a bare-metal computer system. We describe the design and implementation of our bare-metal platform called RustyGecko, which encompasses libraries for controlling the hardware device. On this platform, we developed and evaluated several programs and abstract libraries. To support our platform, we have developed and presented an extension to the Rust standard package manager, which facilitates building Rust applications for non- standard targets. The extension was ultimately contributed back to the package manager project. We have evaluated the platform based on performance, energy consumption, and code size. These results were compared to the existing C platform for the target chip, the ARM Cortex-M3 based EFM32GG called Giant Gecko. Our evaluation shows that Rust performs equally well when considering performance and energy consumption. However, we find that the code size can increase substantially, espe- cially when building the applications in debugging mode. v Sammendrag Innvevde datasystemer blir gradvis en større del av v˚arhverdag. Disse datamask- inene m˚astadig tilpasse seg nye domener, slik som tingenes internett. Sammen- lignet med andre konvensjonelle datasystemer (mobile-, personlige-, og tjeneste- systemer), er mange begrensninger i disse systemene som setter krav til hvilke utviklingsverktøy som kan brukes. I nyere tid har det kommet mange nye programmeringsspr˚aksom er tilrettelagt for konvensjonelle datasystemer. Kategoriseringen av høyere-niv˚aprogrammer- ingsspr˚ak har endret seg i løpet av de siste ti˚arene,fra ˚ahandle om statiske, maskinvare-agnostiske spr˚aksom C, til ˚adreie seg om dynamiske spr˚akmed kjøre- tidssytemer, slik som JavaScript. Sikkerhetsmekanismene som er tilgjengelige i disse høyere-niv˚aspr˚akene kommer ofte p˚abekostning av lav-niv˚akontroll, som er tilgjengelig i lav-niv˚aprogrammeringsspr˚ak. Rust er et nytt og voksende programmeringsspr˚ak,som gjør et forsøk p˚a˚askape et nytt kompromiss mellom kontroll og sikkerhet. Dette spr˚aket kan garantere sikker- het ved statisk analyse, som i andre høyere-niv˚aspr˚akblir løst dynamisk. I denne avhandlingen presenterer vi v˚armetode for ˚abenytte Rust i et innvevd datasystem, og en evaluering av denne. Vi beskriver designet og implementasjonen av v˚aroperativsystemløse plattform kalt RustyGecko, som omfatter biblioteker for ˚akontrollere maskinvaren. Vi vister i tilleg flere programmer og abstrakte biblioteker som er blitt bygget p˚adenne plattformen. For ˚astøtte plattformen har vi ogs˚aimplementert og presentert en utvidelse til Rust sin standard pakkebehandler. Denne utvidelsen gjør det enklere ˚abygge Rust-applikasjoner for ikke-standard plattformer, og har ogs˚ablitt inkludert i det opprinnelige prosjektet som utvikler pakkebehandleren. Vi har evaluert plattformen basert p˚aytelse, energieffektivitet og kodestørrelse, ved bruk av en ARM Cortex-M3-basert EFM32-brikke kalt Giant Gecko. Disse resultatene har blitt sammenlignet med den allerede eksisterende C plattformen. V˚areevalueringer viser at Rust har tilsvarende ytelse og energieffektivitet som C. Vi har imidlertid oppdaget at kodestørrelsen kan øke betraktelig, særlig for applikasjoner som er bygget for feilsøking. vii Contents Preface i Project Description iii Abstract v Sammendrag vii Contents ix List of Tables xiii List of Figures xv List of Listings xviii List of Abbreviations xix 1 Introduction 1 1.1 Motivation . .1 1.2 Embedded Computer System . .2 1.2.1 Abstraction Level . .3 1.2.2 Programming Model . .3 1.2.3 Programming Language . .4 1.3 Benefits of the Rust language . .4 1.4 The RustyGecko Platform . .5 1.5 Interpretation of Assignment . .6 1.6 Project Outline . .8 1.6.1 Phase 1 - Hello World . .8 1.6.2 Phase 2 - Platform Design . .8 1.6.3 Phase 3 - Development . .9 1.6.4 Phase 4 - Measurement . .9 1.6.5 Phase 5 - Evaluation . .9 1.7 Contributions . .9 1.8 Report Outline . 10 ix x CONTENTS 2 Background 13 2.1 The Rust Programming Language . 13 2.1.1 Hello World . 13 2.1.2 Language Features . 14 2.1.3 Organization . 21 2.1.4 Zero-cost Abstractions . 21 2.1.5 Guaranteed memory safety . 23 2.1.6 Concurrency Model . 27 2.1.7 Unsafe Code . 27 2.2 The Cargo Package Manager . 29 2.2.1 Project Structure . 29 2.2.2 Building and testing . 31 2.3 Hardware Platform . 32 2.3.1 EFM32 . 33 2.3.2 Evaluation boards . 33 2.3.3 Peripherals . 36 2.4 Software Libraries . 37 2.4.1 CMSIS . 38 2.4.2 Emlib . 38 2.4.3 Emdrv . 39 2.4.4 Newlib . 39 2.5 The Zinc Project . 39 2.6 Microcontroller Startup . 41 2.6.1 Prelude . 41 2.6.2 Executable and Linkable File Format . 41 2.6.3 Before main . 41 3 Startup for Rust 43 3.1 Booting Rust on the Gecko . 44 3.1.1 Minimal Rust program to boot . 44 3.1.2 Storage qualifiers . 45 3.1.3 Bootstrapping startup . 46 3.2 Handling Interrupts in Rust . 47 4 Rust Embedded Library 49 4.1 The Core Library . 50 4.2 The Allocation Library . 50 4.3 The Collection Library . 51 4.4 The Rust Embedded Library . 51 5 Binding Libraries 53 5.1 Object-oriented Embedded Programming . 54 5.1.1 Memory Mapped I/O . 54 5.1.2 Memory Layout of Objects . 55 5.1.3 Adding Object Functionality . 56 5.1.4 Instantiating a MMIO object . 57 CONTENTS xi 5.2 Library Bindings . 58 5.2.1 The Libraries . 58 5.2.2 Defining the Bindings . 62 5.2.3 Exposing Static Inline Functions to Rust . 63 5.2.4 Naming Conventions . 65 5.2.5 Testing . 66 5.2.6 Discussion . 68 6 Build System 71 6.1 Manual Makefile . 72 6.2 Transitioning to Cargo . 73 6.3 Conditional linking with Cargo . 75 6.4 Continuous Integration . 76 6.5 Contributing to Cargo . 77 6.6 Final Library Build Artifacts . 78 6.6.1 Discussion . 79 7 Application Layer 81 7.1 Porting GPIO Interrupt Driver . 82 7.1.1 Presenting the Problem . 82 7.1.2 Analysis of Assembly . 83 7.1.3 Proposed solution . 84 7.1.4 Discussion . 85 7.2 Handling interrupts with Closures . 86 7.2.1 Motivation . 86 7.2.2 Implementation . 88 7.2.3 Discussion .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages154 Page
-
File Size-