From c3cd50085e400541e595a7852f84df680bb1dd14 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Thu, 11 Jan 2024 16:36:50 +0100 Subject: Initial commit --- .gitignore | 3 + Fosdem2022/TALK.txt | 201 +++++++ Fosdem2022/contents.md | 244 ++++++++ Fosdem2022/img/better_documentation.png | Bin 0 -> 102745 bytes Fosdem2022/img/drake.jpg | Bin 0 -> 93013 bytes Fosdem2022/template.tex | 236 ++++++++ Fosdem2023/Makefile | 25 + Fosdem2023/contents.md | 488 ++++++++++++++++ Fosdem2023/img/BASE.svg | 118 ++++ Fosdem2023/img/Bootstrapping.svg | 378 +++++++++++++ Fosdem2023/img/Sneaky-Compiler.svg | 267 +++++++++ Fosdem2023/img/Source-Binary-Compiler.svg | 172 ++++++ Fosdem2023/img/Source-Binary.svg | 154 +++++ Fosdem2023/img/meme.jpg | Bin 0 -> 41379 bytes Fosdem2023/template.tex | 236 ++++++++ Fosdem2024/Makefile | 25 + Fosdem2024/contents.md | 271 +++++++++ Fosdem2024/img/Bootstrapping_now.svg | 397 +++++++++++++ Fosdem2024/img/Bootstrapping_simple.svg | 371 ++++++++++++ Fosdem2024/img/Bootstrapping_with_backport.svg | 371 ++++++++++++ Fosdem2024/img/Both.svg | 177 ++++++ Fosdem2024/img/NGIAssure.svg | 1 + Fosdem2024/img/bootstrapping.svg | 432 ++++++++++++++ Fosdem2024/img/nlnet.svg | 34 ++ Fosdem2024/img/tcc-boot.svg | 749 +++++++++++++++++++++++++ Fosdem2024/template.tex | 236 ++++++++ 26 files changed, 5586 insertions(+) create mode 100644 .gitignore create mode 100644 Fosdem2022/TALK.txt create mode 100644 Fosdem2022/contents.md create mode 100644 Fosdem2022/img/better_documentation.png create mode 100644 Fosdem2022/img/drake.jpg create mode 100644 Fosdem2022/template.tex create mode 100644 Fosdem2023/Makefile create mode 100644 Fosdem2023/contents.md create mode 100644 Fosdem2023/img/BASE.svg create mode 100644 Fosdem2023/img/Bootstrapping.svg create mode 100644 Fosdem2023/img/Sneaky-Compiler.svg create mode 100644 Fosdem2023/img/Source-Binary-Compiler.svg create mode 100644 Fosdem2023/img/Source-Binary.svg create mode 100644 Fosdem2023/img/meme.jpg create mode 100644 Fosdem2023/template.tex create mode 100644 Fosdem2024/Makefile create mode 100644 Fosdem2024/contents.md create mode 100644 Fosdem2024/img/Bootstrapping_now.svg create mode 100644 Fosdem2024/img/Bootstrapping_simple.svg create mode 100644 Fosdem2024/img/Bootstrapping_with_backport.svg create mode 100644 Fosdem2024/img/Both.svg create mode 100644 Fosdem2024/img/NGIAssure.svg create mode 100644 Fosdem2024/img/bootstrapping.svg create mode 100644 Fosdem2024/img/nlnet.svg create mode 100644 Fosdem2024/img/tcc-boot.svg create mode 100644 Fosdem2024/template.tex diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0fa730 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.pdf +*.mkv +*.mp4 diff --git a/Fosdem2022/TALK.txt b/Fosdem2022/TALK.txt new file mode 100644 index 0000000..719c001 --- /dev/null +++ b/Fosdem2022/TALK.txt @@ -0,0 +1,201 @@ +Who I am (it's a little bit relevant to understand my previous knowledge) +- telecommunication engineer (EEE equivalent) +- Freelance engineer/programmer at ElenQ.Tech +- Guix user and contributor + +Recently: +- Interested in small computing +- Lisp, specially scheme, specially small scheme implementations + +The early beginnings: +- Started a very small scheme compiler for small machines, and thought + about RISC-V. I decided to target RISC-V assembly (following: + https://compilers.iecc.com/crenshaw/) +- I studied RISC-V via [riscvbook] +- I studied several scheme implementations +- Nothing happened out of this, I gave up when I read about continuations but I + learned some stuff. + +The random events +- One day a friend tells me guix mailing list is looking for help to make a + RISC-V port. I decide to raise my hand. +- Now I'm involved in RISC-V porting effort somehow. +- One day Andy Wingo, the Guile maintainer, mentions it would be interesting + to port Lightening, Guile's JIT code generation library to RISC-V. + +Now I'm working on Lightening: things learned +- Code is data -> I'm a lisp guy, I already knew that! +- Some cool GDB debugging I already forgot (disassemble) +- Now I'm able to hand-assemble instructions too +- Machine code generation is not that complex +- Relocations and large immediates are painful + +Lightening RISC-V is passing all the tests, next step is to give it a serious +try and see if it works! Cross fingers! + +Stage0 +- There was a chance to work on the bootstrap system of Guix via NlNet and the + guys involved in the porting effort helped me prepare a proposal. +- I had to learn about all the steps around so I started with Stage0. + +Stage0's steps: +hex0 -> hex1 -> hex2 -> M0 (macro system) -> M2-Planet [ -> Gnu Mes ] + +Hex0 +- Hex0 are hex files that contain what the ELF executable would, but encoded in + Hexadecimal text. +- I started with hex0, porting it to RISC-V (RV64). Thankfully, I was + developing a RISC-V assembler (I never finished) that helped me generate the + instructions by hand. +- Took the ELF headers from the wikipedia. +- It worked!! + +Effects of this effort +- Other people at Bootstrappable took this work and finished the whole chain + of Stage0! + +A random afternoon: +- I was bored and the Hex0 for RV32 was missing. Looked the headers in the + wikipedia and 20 minutes later it was added to the project. Another starting + point solved! + +Now: +- Took the NlNet grant and now I'm going to contribute to the full source + bootstrap ecosystem. So now this is kind of my job. + +Conclusions: +- This is just random work +- Embrace the randomness of life, stay curious and you may reach interesting + places. + + + +--- + + +My blogposts about this process: +https://ekaitz.elenq.tech/lightening.html +https://ekaitz.elenq.tech/hex0.html +https://ekaitz.elenq.tech/machine-code-generation.html + +RISC-V: +http://riscvbook.com/ + +Bootstrap related: +http://bootstrappable.org +https://github.com/oriansj/stage0 +https://github.com/oriansj/bootstrap-seeds + +GUILE +https://www.gnu.org/software/guile/ +https://gitlab.com/wingo/lightening/-/merge_requests/14/commits + +--- + +We are excited to announce a devroom on Declarative and Minimalistic +Computing at FOSDEM on 5th and 6th of February, 2022, online! + +FOSDEM is one of the most important free software conferences and is +hosted annually at Université libre de Bruxelles in Brussels, +Belgium. Unfortunately again this year FOSDEM will not run a physical +conference but will be online only. Talks will be pre-recorded with +some live content including Q&A sessions and discussion panels. + +We accept talks from languages that attempt to minimize use of hardware +and software while trying to make systems simpler, more robust and more +secure. If you are working on improving today's systems taking +declarative/minimalistic approaches feel free to submit a talk +proposal. Examples include the Scheme/Lisp family of programmings +languages. In past editions, this devroom has received presentations +from a varied number of language communities, including Forth, Guile, +Lua, Nim, Racket, Raku and Tcl as well as several experimental projects +that push minimalism in new directions. + +Minimalism and declarative programming are two important topics for +this devroom. Minimalism matters. Minimalism allows for smaller +systems that take less resources and consume less energy. More +importantly, free and open source minimalism allows for secure systems +that are easy to understand. Declarative programming is a programming +paradigm that expresses the logic of a computation without describing +its control flow. Many languages that apply this style attempt to +minimize or eliminate side effects by describing what the program must +accomplish in terms of the problem domain, rather than describe how to +accomplish it as a sequence of the programming language primitives. + +Finally, in this year's virtual conference we will honor the late Professor +[https://en.wikipedia.org/wiki/John_McCarthy_(computer_scientist) John +McCarthy] as the founder of AI and the inventor of LISP. McCarthy with +his work pioneered artificial intelligence, developed the Lisp +programming language family and kickstarted our modern computing world. + +We want to invite you to submit a talk on declarative and minimalistic +computing that fits that description. We are especially happy to +receive talk submissions from members of groups underrepresented in +free software. + +If you have something you’d like to share with your fellow developers, +please E-mail us! Talks considered for the devroom will have to +be entered in + + - https://penta.fosdem.org/submission/FOSDEM22 + +The deadline for submission is December 20th. If you have a FOSDEM +pentabarf account from a previous year, please use that +account. Otherwise add one on +https://penta.fosdem.org/user/new_account. Reach out to +pjotr.public456@thebird.nl or manolis837@gmail.com if you run into any +trouble. + +When submitting your talk make doubly sure to select "Declarative and +Minimalistic Computing devroom" as track (if you don't we won't find +it), and include the following information: + + * The title and subtitle of your talk + * A short abstract of one paragraph + * A longer description if you wish to do so + * Links to related websites/blogs etc + * Presentations has to be pre-recorded and streamed before the event. + * Start recording early! + +To see what a final talk looks like see + + https://archive.fosdem.org/2021/schedule/event/gnumes/ + +For accepted talks + + * Once your talk was accepted, we will assign you an organizer to +help you to produce the pre-recorded content. + * The organizer will review the content and ensure it has the +required quality. He is also responsible for ensuring the content is +into the system and ready to broadcast. + * During the stream of your talk, you must be available online for +the Q/A session + +Let's make this a fun day! + += Organizers = + +Pjotr Prins, Manolis Ragkousis, Ludovic Courtès, Amirouche Boubekki, +Hisham Muhammad, Jan Nieuwenhuizen, Ricardo Wurmus, Alex +Sassmannshausen, William Byrd, Oliver Propst, Efraim Flashner, Julien +Lepiller + += Code of conduct = + + - https://fosdem.org/2022/practical/conduct/ + += Original proposal = + + - https://libreplanet.org/wiki/FOSDEM2022-devroom-proposal + += Important dates: = + + - Dec 20th 2021: submission deadline for talk proposals + - Dec 31th 2021: announcement of the final schedule + - Jan 14th 2021: submission deadline for recordings + - Feb 5th 2022: FOSDEM! + += Links: = + + - +https://libreplanet.org/wiki/FOSDEM2022-devroom-declarative-and-minimalistic-computing-cfp diff --git a/Fosdem2022/contents.md b/Fosdem2022/contents.md new file mode 100644 index 0000000..e8533b8 --- /dev/null +++ b/Fosdem2022/contents.md @@ -0,0 +1,244 @@ +--- +title: "A year of RISC-V adventures: embracing chaos in your software journey" + +subtitle: How I started from zero and ended up porting a JIT compilation library and assembling files by hand + +license: CC-BY-SA +author: Ekaitz Zárraga +links-as-notes: true +lang: spanish +polyglossia-lang: + name: english +how-to: pandoc -f markdown+smart -t beamer contents.md -o beamer.pdf --pdf-engine=xelatex --template=template.tex +... + +# Who I am +- Telecommunication engineer (EEE equivalent) +- Freelance engineer/programmer at [ElenQ.Tech](https://elenq.tech) +- Guix user and contributor + +## Recently +- Interested in small computing +- Lisp, specially Scheme, more specifically small Scheme implementations. + + + +# The beginnings +- Started a very small scheme compiler for small machines, and thought about + RISC-V. I decided to target RISC-V assembly following ["Let's Build a + Compiler" by Jack Crenshaw](https://compilers.iecc.com/crenshaw/) +- I studied RISC-V with: ["The RISC-V Reader: An Open Architecture + Atlas"](http://riscvbook.com/) +- I studied several scheme implementations and read some papers +- Nothing happened out of this, I gave up when I read about continuations but I + learned some stuff. + + + +# Chaos starts +- One day a friend (the same guy who introduced me to Guix) tells me the Guix + mailing list is looking for help to make a RISC-V port. I decide to raise my + hand. +- Now I'm involved in RISC-V porting effort somehow. +- One day Andy Wingo, the Guile maintainer, mentions it would be interesting to + port Lightening, Guile's JIT code generation library to RISC-V. + + + +# Now I'm working on the Lightening port somehow + +Lightening is a machine code generation library. It's a fork of GNU Lightning, +made for Guile, that aims to be simple. + +## The experience +- My C programming skills are rusty +- 0 documentation: only the one inherited from GNU Lightning +- Dead code +- Andy Wingo helped me take the good direction and made everything easier + +# Lightening: Things learned +- How to assemble instructions by hand * +- Code is data. I'm a lisp guy, I already knew that! * +- Some cool GDB debugging tricks I already forgot +- Machine code generation is not that complex +- Relocations and immediates are painful[^more] + +[^more]: Read more: + +# Lightening: Assemble by hand + +``` +addi a0, zero, 56 +``` + +- Opcode `addi`: `0010011` +- Destination register `a0`: `01010` +- `funct3`: `000` +- Source register `zero`: `00000` +- The immediate `56`: `000000111000` + +`000000111000 | 00000 | 000 | 01010 | 0010011` + +All together: `00000011100000000000010100010011` +(In hex: `0x3800513`) + + +# Lightening: Code is data + +``` c +#include +#include + +typedef int f0(void); + +int main(int argc, char* argv[]){ + uint32_t instructions[2]; + + instructions[0] = 0x03800513; // addi a0, zero, 56 + instructions[1] = 0x00008067; // jalr zero, ra, 0 + + // Reinterpret the array address as a function + f0 *load_56 = (f0*) instructions; + + int a = load_56(); + printf("%d\n", a); +} +``` + + +# Moving to the bootstrap system + +There was a chance to work on Guix's RISC-V support via NlNet and the +guys involved in the porting effort told me I should send a proposal. + +I had to learn about the full-source bootstrap project for this so I ended up +taking part on it. + +The proposal was rejected, but the learning was already done. *And what now?* + +# Stage0 + +Stage0 is a full source bootstrap system. + +Stage0's steps: + +1. Hex0: Hex encoded raw ELF file with comments +2. Hex1: Hex0 + one character labels and some extras +3. Hex2: Hex1 + proper labels and reasonable basics +4. M0 (macro system): A simple macro system +5. M2-Planet: A C subset that uses M0 as output +6. C compilers (i.e. GNU Mes's `mescc`) + + +# Stage0 + +## The experience + +![](img/better_documentation.png) + +# Hex0 + +Hex0 is a simple assembler written in Hex0. It just converts a Hexadecimal +encoded ELF file to binary. + +It's heavily commented so anyone can decode the instructions and make sure that +it works as expected. It looks like this: + +``` +# :_start ; (0x0600078) + +; Open input file and store FD in s2 +93 08 80 03 # RD_A7 !56 ADDI ; sys_openat +13 05 C0 F9 # RD_A0 !-100 ADDI ; AT_FDCWD +93 05 06 00 # RD_A1 RS1_A2 MV ; input file +13 06 00 00 # RD_A2 MV ; read only +73 00 00 00 # ECALL +13 09 05 00 # RD_S2 RS1_A0 MV ; Save fd + +``` + +# Hex0 + +I wanted to make the first version of the POSIX based Hex0 for RV64. I just +needed to: + +1. Write it in assembly first +2. Assemble everything by hand... + +![](img/drake.jpg) + + + +# Hex0: Abandoned projects that add entropy: pysc-v + +While I was working on Lightening I started a RISC-V assembler in Python. + +*[I abandoned the project](http://git.elenq.tech/pysc-v/)* + +But the backend was reusable so I could generate all the instructions in the +Hex0 with the format I wanted! + + +# Hex0: disassembly tricks + +I still need to calculate addresses by hand though. + +*But wait!* I learned how to disassemble when I worked on Lightening so I could +obtain each instructions address easily, without all the counting. + +# Hex0 + +## The experience + +- It was rewarding to see that I could write assembly. +- I reused all the GDB tricks from Lightening to check that everything was + working correctly. +- The documentation is weird and there are too many subprojects that make the + project hard to understand for newcomers. It's hard to understand the reasons + behind some decisions and everything looks fragile. +- But `#bootstrappable` at `libera.chat` is a good place to ask questions and + learn from others. + + +# Hex0 extra: embrace boredom + +Sunday morning. I'm bored. + +I reviewed the status of the project and saw RV32 is not ready yet, even if it +is similar to RV64. + +I start a discussion on IRC and in less than half an hour and with the help of +the Wikipedia we replace the ELF headers and make the Hex0 support for RV32. + + +# Status of the projects +- **Lightening**: is passing all the test but needs testing. [The + code][lightening] is available for anyone to read and improve. +- **Stage0**: included [my small RISC-V contribution to Hex0][stage0] and other + contributors expanded my work to every stage. + +[lightening]: https://gitlab.com/wingo/lightening/-/merge_requests/14/commits +[stage0]: https://github.com/oriansj/bootstrap-seeds/pull/2 + + + +# My status + +I tried again with NlNet, with a proposal for some RISC-V porting efforts on +GNU Mes and other bootstrap related projects. + +They look interested on a full-source-bootstrap for RISC-V! + +*I might end up working on a compiler* + + +# Conclusions +- All this is just random work, done without a plan or purpose +- Embrace the chaos of life, stay curious and you may reach interesting places +- If you can, learn from people: it's faster and better +- You don't need to be a genius: most of the things are easy once you know the + context. + +--- + +## Just try, and let it happen diff --git a/Fosdem2022/img/better_documentation.png b/Fosdem2022/img/better_documentation.png new file mode 100644 index 0000000..b8348dc Binary files /dev/null and b/Fosdem2022/img/better_documentation.png differ diff --git a/Fosdem2022/img/drake.jpg b/Fosdem2022/img/drake.jpg new file mode 100644 index 0000000..10a1e74 Binary files /dev/null and b/Fosdem2022/img/drake.jpg differ diff --git a/Fosdem2022/template.tex b/Fosdem2022/template.tex new file mode 100644 index 0000000..31b2bf7 --- /dev/null +++ b/Fosdem2022/template.tex @@ -0,0 +1,236 @@ +\documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$babel-lang$,$endif$$if(handout)$handout,$endif$$if(beamer)$ignorenonframetext,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$} +\setbeamertemplate{caption}[numbered] +\setbeamertemplate{caption label separator}{: } +\selectcolormodel{gray} % Color B&W +\beamertemplatenavigationsymbols$if(navigation)$$navigation$$else$empty$endif$ + +\usepackage{amssymb,amsmath} +\usepackage{ifxetex,ifluatex} +\usepackage{fixltx2e} % provides \textsubscript + + +\usefonttheme{professionalfonts} % using non standard fonts for beamer +\usefonttheme{serif} % default family is serif + +% Font +\usepackage{fontspec} +\setmainfont[ + BoldFont = Lato-Bold.ttf, + ItalicFont = Lato-Italic.ttf, + BoldItalicFont = Lato-BoldItalic.ttf +]{Lato-Regular.ttf} +\newcommand{\euro}{€} + +% Images with no captions +\usepackage{caption} +\captionsetup[figure]{labelformat=empty} + +% Bullet style +\useinnertheme{circles} +\newlength{\wideitemsep} +\setlength{\wideitemsep}{\itemsep} +\addtolength{\wideitemsep}{5pt} +\let\olditem\item +\renewcommand{\item}{\setlength{\itemsep}{\wideitemsep}\olditem} + +$if(euro)$ + \newcommand{\euro}{€} +$endif$ + +% Language +\usepackage{polyglossia} +\setmainlanguage[$polyglossia-lang.options$]{$polyglossia-lang.name$} + +\newif\ifbibliography +$if(natbib)$ +\usepackage{natbib} +\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$} +$endif$ +$if(biblatex)$ +\usepackage[$if(biblio-style)$style=$biblio-style$,$endif$$for(biblatexoptions)$$biblatexoptions$$sep$,$endfor$]{biblatex} +$for(bibliography)$ +\addbibresource{$bibliography$} +$endfor$ +$endif$ + +$if(verbatim-in-note)$ +\usepackage{fancyvrb} +$endif$ + +\hypersetup{ +$if(title-meta)$ + pdftitle={$title-meta$}, +$endif$ +$if(author-meta)$ + pdfauthor={$author-meta$}, +$endif$ +$if(keywords)$ + pdfkeywords={$for(keywords)$$keywords$$sep$, $endfor$}, +$endif$ +$if(colorlinks)$ + colorlinks=true, + linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$, + citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$, + urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$, +$else$ + pdfborder={0 0 0}, +$endif$ + breaklinks=true} +\urlstyle{same} % don't use monospace font for urls +$if(verbatim-in-note)$ +\VerbatimFootnotes % allows verbatim text in footnotes +$endif$ +$if(listings)$ +\usepackage{listings} +$endif$ +$if(lhs)$ +\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{} +$endif$ +$if(highlighting-macros)$ +$highlighting-macros$ +$endif$ +$if(tables)$ +\usepackage{longtable,booktabs} +\usepackage{caption} +% These lines are needed to make table captions work with longtable: +\makeatletter +\def\fnum@table{\tablename~\thetable} +\makeatother +$endif$ +$if(graphics)$ +\usepackage{graphicx,grffile} +\makeatletter +\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi} +\def\maxheight{\ifdim\Gin@nat@height>\textheight0.8\textheight\else\Gin@nat@height\fi} +\makeatother +% Scale images if necessary, so that they will not overflow the page +% margins by default, and it is still possible to overwrite the defaults +% using explicit options in \includegraphics[width, height, ...]{} +\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio} +$endif$ + +% Prevent slide breaks in the middle of a paragraph: +\widowpenalties 1 10000 +\raggedbottom + +$if(section-titles)$ +\AtBeginPart{ + \let\insertpartnumber\relax + \let\partname\relax + \frame{\partpage} +} +\AtBeginSection{ + \ifbibliography + \else + \let\insertsectionnumber\relax + \let\sectionname\relax + \frame{\sectionpage} + \fi +} +\AtBeginSubsection{ + \let\insertsubsectionnumber\relax + \let\subsectionname\relax + \frame{\subsectionpage} +} +$endif$ + +$if(links-as-notes)$ +% Make links footnotes instead of hotlinks: +\renewcommand{\href}[2]{#2\footnote{\url{#1}}} +$endif$ + +$if(strikeout)$ +\usepackage[normalem]{ulem} +% avoid problems with \sout in headers with hyperref: +\pdfstringdefDisableCommands{\renewcommand{\sout}{}} +$endif$ +\setlength{\parindent}{0pt} +\setlength{\parskip}{6pt plus 2pt minus 1pt} +\setlength{\emergencystretch}{3em} % prevent overfull lines +\providecommand{\tightlist}{% + \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} +$if(numbersections)$ +\setcounter{secnumdepth}{$if(secnumdepth)$$secnumdepth$$else$5$endif$} +$else$ +\setcounter{secnumdepth}{0} +$endif$ +$if(dir)$ +\ifxetex + % load bidi as late as possible as it modifies e.g. graphicx + $if(latex-dir-rtl)$ + \usepackage[RTLdocument]{bidi} + $else$ + \usepackage{bidi} + $endif$ +\fi +\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex + \TeXXeTstate=1 + \newcommand{\RL}[1]{\beginR #1\endR} + \newcommand{\LR}[1]{\beginL #1\endL} + \newenvironment{RTL}{\beginR}{\endR} + \newenvironment{LTR}{\beginL}{\endL} +\fi +$endif$ +$for(header-includes)$ +$header-includes$ +$endfor$ + +$if(title)$ +\title{$title$} +$endif$ +$if(subtitle)$ +\subtitle{$subtitle$} +$endif$ +$if(author)$ +\author{$for(author)$$author$$sep$ \and $endfor$} +$endif$ +$if(institute)$ +\institute{$for(institute)$$institute$$sep$ \and $endfor$} +$endif$ +\date{$date$} + +\begin{document} +$if(title)$ +\frame{\titlepage} +$endif$ + +$for(include-before)$ +$include-before$ + +$endfor$ +$if(toc)$ +\begin{frame} +\tableofcontents[hideallsubsections] +\end{frame} + +$endif$ +$body$ + +$if(natbib)$ +$if(bibliography)$ +$if(biblio-title)$ +$if(book-class)$ +\renewcommand\bibname{$biblio-title$} +$else$ +\renewcommand\refname{$biblio-title$} +$endif$ +$endif$ +\begin{frame}[allowframebreaks]{$biblio-title$} +\bibliographytrue +\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$} +\end{frame} + +$endif$ +$endif$ +$if(biblatex)$ +\begin{frame}[allowframebreaks]{$biblio-title$} +\bibliographytrue +\printbibliography[heading=none] +\end{frame} + +$endif$ +$for(include-after)$ +$include-after$ + +$endfor$ +\end{document} diff --git a/Fosdem2023/Makefile b/Fosdem2023/Makefile new file mode 100644 index 0000000..1f2daf1 --- /dev/null +++ b/Fosdem2023/Makefile @@ -0,0 +1,25 @@ +TARGET = presentation.pdf + +TEMPLATE = template.tex +CONTENT = contents.md + +# IMG_DIR = img +# IMG_SRCS = $(wildcard $(IMG_DIR)/*.svg) +# IMG_BASENAME = $(notdir $(basename $(IMG_SRCS))) +# TMP_IMG_DIR = tmp +# IMGS = $(addsuffix .png, $(addprefix $(TMP_IMG_DIR)/, $(IMG_BASENAME))) + +all: $(TARGET) + +$(TARGET): $(CONTENT) $(TEMPLATE) # $(IMGS) + pandoc -f markdown+smart -t beamer $(CONTENT) -o $(TARGET) --pdf-engine=xelatex --template=$(TEMPLATE) + +# $(IMGS): $(TMP_IMG_DIR)/%.png: $(IMG_DIR)/%.svg +# mkdir -p $(TMP_IMG_DIR) +# inkscape $< --export-filename $@ --export-type=png --export-background-opacity=0.0 + +clean: + rm -r $(TARGET) # $(TMP_IMG_DIR) + +.PHONY: all clean $(CONTENT) $(TEMPLATE) + diff --git a/Fosdem2023/contents.md b/Fosdem2023/contents.md new file mode 100644 index 0000000..924371c --- /dev/null +++ b/Fosdem2023/contents.md @@ -0,0 +1,488 @@ +--- +title: "Bringing RISC-V to Guix's bootstrap" + +subtitle: What's done and what we need to do + +license: CC-BY-SA +author: Ekaitz Zárraga +links-as-notes: true +lang: spanish +polyglossia-lang: + name: english +how-to: pandoc -f markdown+smart -t beamer contents.md -o beamer.pdf --pdf-engine=xelatex --template=template.tex +header-includes: + - \usepackage{multicol} + - \newcommand{\hideFromPandoc}[1]{#1} + - \hideFromPandoc{ + \let\Begin\begin + \let\End\end + } +... + +## Who I am +- Telecommunication engineer (EEE equivalent) +- Freelance engineer/programmer at [ElenQ.Tech](https://elenq.tech) +- Guix user and contributor +- You might remember me from my talk last year: + *"A year of RISC-V adventures: embracing chaos in your software journey"* + + +## Intro + +- Last year I asked [NlNet for a grant][nlnet-grant]. +- I wanted to push the bootstrapping effort for RISC-V, and they funded me to + do so. +- In this talk I'm going to introduce what I did[^1], what I think it's more or + less done and what's missing. + +[nlnet-grant]: https://nlnet.nl/project/GNUMes-RISCV/index.html + +[^1]: Read the longer version here: + +# Intro to bootstrapping + +## Free software is not enough + +We love Free Software because it helps us audit our programs. + +![ ](img/Source-Binary.svg){width=200px} + +But do we know if the source code we read actually maps to the binary we are +executing? **Not really** + +## Reproducibility + +![The relation is one-way: the compiler is in the middle](img/Source-Binary-Compiler.svg){width=200px} + +In Guix we have **reproducibility**, so we can make sure some inputs (the +source, the compiler and the environment where it runs) always produce the same +outputs. + +**We can challenge the binaries**, so nobody will give us a malicious binary. + +## Trusting trust + +![But what if the bad actor is not a person but a program?](img/Sneaky-Compiler.svg){width=200px} + +## Trusting trust + +![But what if the bad actor is not a person but a program?](img/Sneaky-Compiler.svg){width=160px} + +Reproducibility here will only make sure we generate the same **corrupt** +binary. + +> This kind of attack [can be done in real life][ken-trust]. + +[ken-trust]: https://niconiconi.neocities.org/posts/ken-thompson-really-did-launch-his-trusting-trust-trojan-attack-in-real-life/ + +## Recursive problem, recursive solution + +The compiler is a program too. **This issue is recursive**: a corrupt compiler +could corrupt it's output compiler! + +![What's the exit point?](img/Bootstrapping.svg){width=300px} + +## Recursive problem, recursive solution + +![What's the exit point?](img/Bootstrapping.svg){width=200px} + +If we could have a compiler that we can make sure its output is not corrupt +(**but how?**), we could make sure all the chain is correct. + +## In practice + +GNU+Linux distributions often rely in many prebuilt binaries: Bash, GCC, +Coreutils, Python... + +Some distributions like Guix are interested on reducing the amount of binaries +they have to trust. + +We can compile most of **The World** from source using a powerful compiler (GCC +FTW). But we can't use a pre-built compiler (remember the previous slides?) + +**The key**: Who is compiling the compiler? + +## In practice - II + +Let's try with GCC: + +0. The World (requires a modern GCC) +1. Modern GCC (requires ISO C++11 compiler) + +## In practice - II + +Let's try with GCC: + +0. The World (requires a modern GCC) +1. Modern GCC (requires ISO C++11 compiler) +2. GCC 11 (requires ISO C++98 compiler) + +## In practice - II + +Let's try with GCC: + +0. The World (requires a modern GCC) +1. Modern GCC (requires ISO C++11 compiler) +2. GCC 11 (requires ISO C++98 compiler) +3. GCC 4.8 (requires ISO C89 compiler) + +## In practice - II + +Let's try with GCC: + +0. The World (requires a modern GCC) +1. Modern GCC (requires ISO C++11 compiler) +2. GCC 11 (requires ISO C++98 compiler) +3. GCC 4.8 (requires ISO C89 compiler) +4. GCC 3.4 (requires K&R compiler) + +## In practice - II + +Let's try with GCC: + +0. The World (requires a modern GCC) +1. Modern GCC (requires ISO C++11 compiler) +2. GCC 11 (requires ISO C++98 compiler) +3. GCC 4.8 (requires ISO C89 compiler) +4. GCC 3.4 (requires K&R compiler) +5. ... + +## In practice - II + +Let's try with GCC: + +0. The World (requires a modern GCC) +1. Modern GCC (requires ISO C++11 compiler) +2. GCC 11 (requires ISO C++98 compiler) +3. GCC 4.8 (requires ISO C89 compiler) +4. GCC 3.4 (requires K&R compiler) +5. ... + +*I didn't mention libraries here, that's also a lot of fun* + +## Guix's bootstrapping + +0. The World +1. Modern GCC +2. GCC 7.5 +3. GCC 4.6.4 +4. GCC 2.95 +5. TinyCC + - Bootstrappable TinyCC +6. GNU Mes +7. Stage0-POSIX => **SOURCE CODE** + +## GNU Mes + +> GNU Mes is a Scheme interpreter and C compiler for bootstrapping the GNU +> System. Since version 0.22 it has again helped to halve the size of opaque, +> uninspectable binary seeds that are currently being used in the Further +> Reduced Binary Seed bootstrap of GNU Guix. **The final goal is to help create +> a full-source bootstrap as part of the bootstrappable builds effort for +> UNIX-like operating systems**. + +> The Scheme interpreter is written in ~5,000 LOC of simple C, and the C +> compiler written in Scheme and these are mutual self-hosting. Mes can now be +> bootstrapped from M2-Planet and Mescc-Tools. + + + + +## Stage0-POSIX + +> It bootstraps all these from a single 256 byte seed (which you will find in +> the folder bootstrap-seeds). **The ultimate goal is for this to bootstrap all +> the way up to GCC**. + +> There is only one "missing" part that is not bootstrappable from the hex0 +> seed: a kernel. This issue is not yet solved and at the moment the kernel is +> trusted. + + + + +## Boostrapping - wrapping up + +![ ](img/meme.jpg) + + +# RISC-V support + +## Guix's bootstrapping - RISC-V support + +\Begin{multicols}{2} + +0. The World +1. Modern GCC +2. GCC 7.5 +3. GCC 4.6.4 +4. GCC 2.95 +5. TinyCC + - Bootstrappable TinyCC +6. GNU Mes +7. Stage0-POSIX + +\columnbreak + +- N/A +- YES +- YES +- NO +- NO +- YES + - NO +- PARTIAL +- YES + +\End{multicols} + + +## Guix's bootstrapping - RISC-V support *SPOILER* + +\Begin{multicols}{2} + +0. The World +1. Modern GCC +2. GCC 7.5 +3. GCC 4.6.4 +4. GCC 2.95 +5. TinyCC + - Bootstrappable TinyCC +6. GNU Mes +7. Stage0-POSIX + +\columnbreak + +- N/A +- YES +- YES +- ~~NO~~ *I backported this* +- ~~NO~~ *We will remove it* +- YES + - ~~NO~~ *I backported this* +- PARTIAL +- YES *I made some of this* + +\End{multicols} + +# What I did + +## GCC + +GCC uses a Davidson-Fraser model. Meaning that it uses an intermediate language +that is machine dependant: RTL (Register Transfer Language). + +``` +HLL -> GIMPLE -> RTL -> OPTIMIZATIONS -> RTL -> ASSEMBLY +``` + +GCC is only a coordinator: it calls `as` and `ld` from binutils as the +assembler and linker. + +- `GIMPLE -> RTL`: is done using identifiers. The GIMPLE nodes match insn + identifiers. + +- `RTL -> OPTIMIZATIONS`: RTL matches the RTL templates we write in the + backend part of GCC. Those can be expanded to other RTL expressions. + +- `RTL -> ASSEMBLY`: The expanded RTL expressions are matched against RTL + templates that also describe their equivalent in assembly and assembly is + generated from them. + +## GCC + +RTL templates are written in LISP in machine descriptor files (`*.md`), they +look like this: + +\small +``` lisp +(define_insn + "adddi3" ;; Identifier + + ;; The behavior of the instruction + [(set (match_operand:DI 0 "register_operand" "=r,r") + (plus:DI (match_operand:DI 1 "register_operand" "r,r") + (match_operand:DI 2 "arith_operand" "r,I"))) + ] + + "TARGET_64BIT" ;; Predicate to test + "add\t%0,%1,%2" ;; Assembly output template + + ;; Attributes + [(set_attr "type" "arith") + (set_attr "mode" "DI")]) +``` +\normalsize + +## GCC + +Apart from that GCC needs tons of other definitions in order to get another +target: + +- Target description macros and functions +- Libraries like libgcc and many others + +## GCC - What I did + +Cherry picked the RISC-V support from GCC 7.5 to GCC 4.6.4 + +1. There were missing insns => Used older ones that were equivalent. +2. Some RTL constructs (`int_iterator`) didn't exist in 4.6.4 => Expanded the + iterator by hand. +3. There were missing predicates => Copied them. +4. The internal GCC API moved from C to C++ in the meantime => I had to convert + the code from using a class to the older interface. +5. Memory barriers didn't exist back then => Always introduce a `fence` to make + sure code is correct. +6. `libgcc` is a mess => Play around until it works + +**TL;DR**: Touch everything until it works. + +## GCC - What I did + +Finally I managed to make a GCC 4.6.4 that is able to generate RISC-V binary. + +See the blog for a more detailed description of the changes: + +- +- + + +## Bootstrappable TinyCC + +TinyCC has RISC-V support but it's not boostrappable using GNU Mes. + +The bootstrappable fork of TinyCC GNU Mes uses is old => Backport again. + +## Bootstrappable TinyCC - What I did + +Copy the relevant files from the upstream TinyCC and: + +0. Prepare a reproducible way to build the Bootstrappable TinyCC. +1. Just read the code and make it match. + **SURPRISE**: The code is really hard to read... But I eventually managed to + make it work. +2. Some core code was needed for the backend to work => Remove it! It was only + some optimization code! + +More detailed description of the changes: + +- + +## Bootstrappable TinyCC - What I did + +\tiny +\Begin{multicols}{2} + +**OPTIMIZED VERSION** +``` +0000000000000000
: + 0: fd010113 addi sp,sp,-48 + 4: 02113423 sd ra,40(sp) + 8: 02813023 sd s0,32(sp) + c: 03010413 addi s0,sp,48 + 10: 00000013 nop + 14: fea43423 sd a0,-24(s0) + 18: feb43023 sd a1,-32(s0) + 1c: 0130051b addiw a0,zero,19 + 20: fca42e23 sw a0,-36(s0) + 24: 05a0051b addiw a0,zero,90 + 28: fca42c23 sw a0,-40(s0) + 2c: fdc42503 lw a0,-36(s0) + 30: 00051463 bnez a0,38 + 34: 0180006f j 4c + 38: fd842503 lw a0,-40(s0) + 3c: 00051463 bnez a0,44 + 40: 00c0006f j 4c + 44: 0010051b addiw a0,zero,1 + 48: 0100006f j 58 + 4c: 00008537 lui a0,0x8 + 50: 7005051b addiw a0,a0,1792 + 54: 00000033 add zero,zero,zero + 58: 02813083 ld ra,40(sp) + 5c: 02013403 ld s0,32(sp) + 60: 03010113 addi sp,sp,48 + 64: 00008067 ret +``` +\columnbreak +**UNOPTIMIZED VERSION** +``` +0000000000000000
: + 0: fd010113 addi sp,sp,-48 + 4: 02113423 sd ra,40(sp) + 8: 02813023 sd s0,32(sp) + c: 03010413 addi s0,sp,48 + 10: 00000013 nop + 14: fea43423 sd a0,-24(s0) + 18: feb43023 sd a1,-32(s0) + 1c: 0130051b addiw a0,zero,19 + 20: fca42e23 sw a0,-36(s0) + 24: 05a0051b addiw a0,zero,90 + 28: fca42c23 sw a0,-40(s0) + 2c: fdc42503 lw a0,-36(s0) + 30: 00051463 bnez a0,38 + 34: 01c0006f j 50 + 38: fd842503 lw a0,-40(s0) + 3c: 00051463 bnez a0,44 + 40: 0100006f j 50 + 44: 0010051b addiw a0,zero,1 + 48: 0140006f j 5c + 4c: 0100006f j 5c + 50: 00008537 lui a0,0x8 + 54: 7005051b addiw a0,a0,1792 + 58: 00000033 add zero,zero,zero + 5c: 02813083 ld ra,40(sp) + 60: 02013403 ld s0,32(sp) + 64: 03010113 addi sp,sp,48 + 68: 00008067 ret +``` +\End{multicols} + +\normalsize + +# What needs to be done + +## GCC - What needs to be done + +- Properly package the GCC-4.6.4 to include C++ support, and fix all the + libraries that might be missing. +- Build the backported GCC-4.6.4 using TinyCC +- Build GCC-7.5 using the backported GCC-4.6.4 + +## TinyCC - What needs to be done + +- Build the bootstrappable TinyCC using GNU Mes +- Decide if we need the upstream TinyCC to build GCC or not + - If we do: build the upstream TinyCC with the bootstrappable TinyCC + +## GNU Mes - What needs to be done + +- Review the current RISC-V support and prepare it to be merged + +## Guix - What needs to be done + +- Describe the whole compiler compiler chain in the bootstrapping packages so + everyone can benefit from it + +## Extra + +Do it all in real hardware + +# Last words + +## Last words + +There's still a lot of work to be done, most of it being the integration of the +work I already did in the past thanks to NlNet. + +The future is bright though. We are probably going to get more funds from NlNet +to involve more people on this. + +Wanna join? + +## Contact and take part + +- Email me: +- Relevant IRC channels: `#bootstrappable`, `#guix`, `#guix-risc-v` + +# Thank you diff --git a/Fosdem2023/img/BASE.svg b/Fosdem2023/img/BASE.svg new file mode 100644 index 0000000..facfa8e --- /dev/null +++ b/Fosdem2023/img/BASE.svg @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + Source + Binary + + + diff --git a/Fosdem2023/img/Bootstrapping.svg b/Fosdem2023/img/Bootstrapping.svg new file mode 100644 index 0000000..6f61055 --- /dev/null +++ b/Fosdem2023/img/Bootstrapping.svg @@ -0,0 +1,378 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Source + + Compiler + Binary + + + + Source + + Compiler + + + -1 + + + + + diff --git a/Fosdem2023/img/Sneaky-Compiler.svg b/Fosdem2023/img/Sneaky-Compiler.svg new file mode 100644 index 0000000..76790cc --- /dev/null +++ b/Fosdem2023/img/Sneaky-Compiler.svg @@ -0,0 +1,267 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + Source + + Ć̡̹͇̼̲̰̱͐ͧ́̎ͫo̵̗̎ͭ̈̈̚͢ḿ̴̴͎̮̫̣͔̰̐̌̆ͨ̇͐ͯ̉̉̌̊͢pil̞ͤę̝͍̭̱͆̃ͦͫr̰̗̥̬̣̔̄͑̾̔͆̕̕ + Compiler + + B̧͔̹͔̜̗͙͉͎̻̹͚͕̫͛̈̀̊̔ͦ͂̈̍͘͡͠i̶̢̘̞͕̓̓̿̂̅ͮ͜͜͞n̯̲̫̥͇̞̞̬̥̜͍͕̺̱ͩͧ̆́̽ͧ̾̋̆ͤ̚̚̚͞ar̢̼̲͈̝̱̄y + Binary + + + (▀̿Ĺ̯▀̿ ̿) + + diff --git a/Fosdem2023/img/Source-Binary-Compiler.svg b/Fosdem2023/img/Source-Binary-Compiler.svg new file mode 100644 index 0000000..8afafdd --- /dev/null +++ b/Fosdem2023/img/Source-Binary-Compiler.svg @@ -0,0 +1,172 @@ + + + + + + + + + + + + + + + + + + + + Source + + Compiler + Binary + + + + diff --git a/Fosdem2023/img/Source-Binary.svg b/Fosdem2023/img/Source-Binary.svg new file mode 100644 index 0000000..26dc5ba --- /dev/null +++ b/Fosdem2023/img/Source-Binary.svg @@ -0,0 +1,154 @@ + + + + + + + + + + + + + + + + + + + + Source + Binary + + ? + + diff --git a/Fosdem2023/img/meme.jpg b/Fosdem2023/img/meme.jpg new file mode 100644 index 0000000..e015a1c Binary files /dev/null and b/Fosdem2023/img/meme.jpg differ diff --git a/Fosdem2023/template.tex b/Fosdem2023/template.tex new file mode 100644 index 0000000..0ae0832 --- /dev/null +++ b/Fosdem2023/template.tex @@ -0,0 +1,236 @@ +\documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$babel-lang$,$endif$$if(handout)$handout,$endif$$if(beamer)$ignorenonframetext,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$} +\setbeamertemplate{caption}[numbered] +\setbeamertemplate{caption label separator}{: } +\selectcolormodel{gray} % Color B&W +\beamertemplatenavigationsymbols$if(navigation)$$navigation$$else$empty$endif$ + +\usepackage{amssymb,amsmath} +\usepackage{ifxetex,ifluatex} +\usepackage{fixltx2e} % provides \textsubscript + + +\usefonttheme{professionalfonts} % using non standard fonts for beamer +\usefonttheme{serif} % default family is serif + +% Font +\usepackage{fontspec} +\setmainfont[ + BoldFont = {*-Bold}, + ItalicFont = {*-Italic}, + BoldItalicFont = {*-BoldItalic}, +]{Lato} +\newcommand{\euro}{€} + +% Images with no captions +\usepackage{caption} +\captionsetup[figure]{labelformat=empty} + +% Bullet style +\useinnertheme{circles} +\newlength{\wideitemsep} +\setlength{\wideitemsep}{\itemsep} +\addtolength{\wideitemsep}{5pt} +\let\olditem\item +\renewcommand{\item}{\setlength{\itemsep}{\wideitemsep}\olditem} + +$if(euro)$ + \newcommand{\euro}{€} +$endif$ + +% Language +\usepackage{polyglossia} +\setmainlanguage[$polyglossia-lang.options$]{$polyglossia-lang.name$} + +\newif\ifbibliography +$if(natbib)$ +\usepackage{natbib} +\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$} +$endif$ +$if(biblatex)$ +\usepackage[$if(biblio-style)$style=$biblio-style$,$endif$$for(biblatexoptions)$$biblatexoptions$$sep$,$endfor$]{biblatex} +$for(bibliography)$ +\addbibresource{$bibliography$} +$endfor$ +$endif$ + +$if(verbatim-in-note)$ +\usepackage{fancyvrb} +$endif$ + +\hypersetup{ +$if(title-meta)$ + pdftitle={$title-meta$}, +$endif$ +$if(author-meta)$ + pdfauthor={$author-meta$}, +$endif$ +$if(keywords)$ + pdfkeywords={$for(keywords)$$keywords$$sep$, $endfor$}, +$endif$ +$if(colorlinks)$ + colorlinks=true, + linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$, + citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$, + urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$, +$else$ + pdfborder={0 0 0}, +$endif$ + breaklinks=true} +\urlstyle{same} % don't use monospace font for urls +$if(verbatim-in-note)$ +\VerbatimFootnotes % allows verbatim text in footnotes +$endif$ +$if(listings)$ +\usepackage{listings} +$endif$ +$if(lhs)$ +\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{} +$endif$ +$if(highlighting-macros)$ +$highlighting-macros$ +$endif$ +$if(tables)$ +\usepackage{longtable,booktabs} +\usepackage{caption} +% These lines are needed to make table captions work with longtable: +\makeatletter +\def\fnum@table{\tablename~\thetable} +\makeatother +$endif$ +$if(graphics)$ +\usepackage{graphicx,grffile} +\makeatletter +\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi} +\def\maxheight{\ifdim\Gin@nat@height>\textheight0.8\textheight\else\Gin@nat@height\fi} +\makeatother +% Scale images if necessary, so that they will not overflow the page +% margins by default, and it is still possible to overwrite the defaults +% using explicit options in \includegraphics[width, height, ...]{} +\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio} +$endif$ + +% Prevent slide breaks in the middle of a paragraph: +\widowpenalties 1 10000 +\raggedbottom + +$if(section-titles)$ +\AtBeginPart{ + \let\insertpartnumber\relax + \let\partname\relax + \frame{\partpage} +} +\AtBeginSection{ + \ifbibliography + \else + \let\insertsectionnumber\relax + \let\sectionname\relax + \frame{\sectionpage} + \fi +} +\AtBeginSubsection{ + \let\insertsubsectionnumber\relax + \let\subsectionname\relax + \frame{\subsectionpage} +} +$endif$ + +$if(links-as-notes)$ +% Make links footnotes instead of hotlinks: +\renewcommand{\href}[2]{#2\footnote{\url{#1}}} +$endif$ + +$if(strikeout)$ +\usepackage[normalem]{ulem} +% avoid problems with \sout in headers with hyperref: +\pdfstringdefDisableCommands{\renewcommand{\sout}{}} +$endif$ +\setlength{\parindent}{0pt} +\setlength{\parskip}{6pt plus 2pt minus 1pt} +\setlength{\emergencystretch}{3em} % prevent overfull lines +\providecommand{\tightlist}{% + \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} +$if(numbersections)$ +\setcounter{secnumdepth}{$if(secnumdepth)$$secnumdepth$$else$5$endif$} +$else$ +\setcounter{secnumdepth}{0} +$endif$ +$if(dir)$ +\ifxetex + % load bidi as late as possible as it modifies e.g. graphicx + $if(latex-dir-rtl)$ + \usepackage[RTLdocument]{bidi} + $else$ + \usepackage{bidi} + $endif$ +\fi +\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex + \TeXXeTstate=1 + \newcommand{\RL}[1]{\beginR #1\endR} + \newcommand{\LR}[1]{\beginL #1\endL} + \newenvironment{RTL}{\beginR}{\endR} + \newenvironment{LTR}{\beginL}{\endL} +\fi +$endif$ +$for(header-includes)$ +$header-includes$ +$endfor$ + +$if(title)$ +\title{$title$} +$endif$ +$if(subtitle)$ +\subtitle{$subtitle$} +$endif$ +$if(author)$ +\author{$for(author)$$author$$sep$ \and $endfor$} +$endif$ +$if(institute)$ +\institute{$for(institute)$$institute$$sep$ \and $endfor$} +$endif$ +\date{$date$} + +\begin{document} +$if(title)$ +\frame{\titlepage} +$endif$ + +$for(include-before)$ +$include-before$ + +$endfor$ +$if(toc)$ +\begin{frame} +\tableofcontents[hideallsubsections] +\end{frame} + +$endif$ +$body$ + +$if(natbib)$ +$if(bibliography)$ +$if(biblio-title)$ +$if(book-class)$ +\renewcommand\bibname{$biblio-title$} +$else$ +\renewcommand\refname{$biblio-title$} +$endif$ +$endif$ +\begin{frame}[allowframebreaks]{$biblio-title$} +\bibliographytrue +\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$} +\end{frame} + +$endif$ +$endif$ +$if(biblatex)$ +\begin{frame}[allowframebreaks]{$biblio-title$} +\bibliographytrue +\printbibliography[heading=none] +\end{frame} + +$endif$ +$for(include-after)$ +$include-after$ + +$endfor$ +\end{document} diff --git a/Fosdem2024/Makefile b/Fosdem2024/Makefile new file mode 100644 index 0000000..1f2daf1 --- /dev/null +++ b/Fosdem2024/Makefile @@ -0,0 +1,25 @@ +TARGET = presentation.pdf + +TEMPLATE = template.tex +CONTENT = contents.md + +# IMG_DIR = img +# IMG_SRCS = $(wildcard $(IMG_DIR)/*.svg) +# IMG_BASENAME = $(notdir $(basename $(IMG_SRCS))) +# TMP_IMG_DIR = tmp +# IMGS = $(addsuffix .png, $(addprefix $(TMP_IMG_DIR)/, $(IMG_BASENAME))) + +all: $(TARGET) + +$(TARGET): $(CONTENT) $(TEMPLATE) # $(IMGS) + pandoc -f markdown+smart -t beamer $(CONTENT) -o $(TARGET) --pdf-engine=xelatex --template=$(TEMPLATE) + +# $(IMGS): $(TMP_IMG_DIR)/%.png: $(IMG_DIR)/%.svg +# mkdir -p $(TMP_IMG_DIR) +# inkscape $< --export-filename $@ --export-type=png --export-background-opacity=0.0 + +clean: + rm -r $(TARGET) # $(TMP_IMG_DIR) + +.PHONY: all clean $(CONTENT) $(TEMPLATE) + diff --git a/Fosdem2024/contents.md b/Fosdem2024/contents.md new file mode 100644 index 0000000..4c8b74e --- /dev/null +++ b/Fosdem2024/contents.md @@ -0,0 +1,271 @@ +--- +title: RISC-V Bootstrapping in Guix and Live-Bootstrap +subtitle: Status report for 2023, and what's coming up next + +license: CC-BY-SA +author: Ekaitz Zárraga +links-as-notes: true +lang: spanish +polyglossia-lang: + name: english +how-to: pandoc -f markdown+smart -t beamer contents.md -o beamer.pdf --pdf-engine=xelatex --template=template.tex +convert-img: inkscape IN.svg --export-filename OUT.png --export-type=png --export-background-opacity=0.0 # NOT NEEDED! +header-includes: + - \usepackage{multicol} + - \newcommand{\hideFromPandoc}[1]{#1} + - \hideFromPandoc{ + \let\Begin\begin + \let\End\end + } +... + +## Before we start + +If you want details and don't mind reading large text walls full of +nitty-gritty details, you can leave this talk and read [all the blogposts I did +on the process](https://ekaitz.elenq.tech/tag/bootstrapping-gcc-in-risc-v.html) + +**You can still stay here and smile, too.** + +## Who I am + +- Telecommunication engineer (EEE equivalent) +- Freelance engineer/programmer at [ElenQ.Tech](https://elenq.tech) +- Guix user and contributor +- You might remember me from my talk last year: + *"Bringing RISC-V to Guix's bootstrap"* + + + +## Context + +- In 2021 [NlNet][nlnet-grant-i] funded me to do some bootstrapping work: I + backported RISC-V support to GCC 4.6.4 from 7.5, and to our TinyCC-Boot from + upstream TinyCC. These were fundamental points in our bootstrap chain. +- I explained that in [FOSDEM 2023][fosdem-2023]. + +[nlnet-grant-i]: https://nlnet.nl/project/GNUMes-RISCV/index.html +[fosdem-2023]: https://archive.fosdem.org/2023/schedule/event/guixriscv/ + +\hfill ![\ ](img/nlnet.svg){height=25px}\ +\hfill ![\ ](img/NGIAssure.svg){height=15px} + + +## Intro + +- The work wasn't finished so I asked for a [second grant to + NlNet][nlnet-grant-ii]. +- I was tired of the previous effort and I needed help. I decided to bring more + people to the project: + - **Andrius Štikonas**: Involved in `live-bootstrap`, `stage-0` and the + related projects. Has the context awareness I lack. + - **Janneke**: `Mes` maintainer and author, `TinyCC-boot` maintainer and + `guix` contributor. All these projects are structural to the + bootstrapping process. + - More people +- My goal was to pay people for their good work, using the success of my + previous effort as a lever to get funded. + +[nlnet-grant-ii]: https://nlnet.nl/project/GNUMes-RISCV-bootstrap/ + +\hfill ![\ ](img/nlnet.svg){height=25px}\ +\hfill ![\ ](img/NGIAssure.svg){height=15px} + + +# In pictures + +## Bootstrapping process - Before + +\Begin{multicols}{2} + +- Colors represent RISC-V support: + - **\textcolor{red}{RED}**: no RISC-V support + - **\textcolor{orange}{ORANGE}**: some RISC-V support + - **\textcolor{green}{GREEN}**: full RISC-V support +- **TinyCC-boot** and **GCC (old)** are **\textcolor{red}{RED}**. +\vfill + +\columnbreak + +![\ ](img/Bootstrapping_simple.svg) + +\End{multicols} + +## Bootstrapping process - After the Backport (2022) + +\Begin{multicols}{2} + +- **Arrows** are still **\textcolor{red}{RED}** +- At that time I didn't know **TinyCC** was **\textcolor{orange}{ORANGE}**, + that was discovered later +- **GCC (old)** was actually **\textcolor{green}{GREEN}ER** than I thought +\vfill + +\columnbreak + +![\ ](img/Bootstrapping_with_backport.svg) + +\End{multicols} + +## Bootstrapping process - Now + +\Begin{multicols}{2} +- **TinyCC-Boot** is **\textcolor{green}{GREEN}** now +- The **\textcolor{green}{GREEN}** rectangle is included in `Guix` and + `live-bootstrap` +- **GCC (old)** is **\textcolor{green}{GREEN}** now: Tested in Debian in real + RISC-V hardware. +\vfill + +\columnbreak + +![\ ](img/Bootstrapping_now.svg) + +\End{multicols} + +## Bootstrapping process - Future + +\Begin{multicols}{2} +- **\textcolor{red}{->} TinyCC** requires changes in MesLibC +- **\textcolor{red}{->} GCC (old)**: + - requires `make` + - TinyCC claims to be able to build GCC 4, but we didn't test that (i386 + bootstrap uses GCC 2.95 in between). +- **\textcolor{red}{->} GCC** should *just work* +- A powerful `libc` is needed, built with TinyCC, which doesn't support + Extended Assembly. +\vfill + +\columnbreak + +![\ ](img/Bootstrapping_now.svg) + +\End{multicols} + + +# Questions? + +## + + + +# Limitations of the backport + +## TinyCC + +- I only tested the backend, in an emulated environment, using a using TinyCC + as a cross-compiler +- I tested on **GLibC**, but in the bootstrapping process it uses **MesLibC**, + a minimal **LibC**, part of Mes. +- **TinyCC-Boot** is in fact compiled several times (up to 6) with different + features (using conditional compilation via macro definitions like `-D...`). + - In a cross compiled environment this is not possible to test +- I didn't test the TinyCC **RISC-V assembler**, which happened to be + unimplemented (yeah... LOL), and it's needed for the **LibC**. + +## TinyCC + +![TinyCC-Boot bootstrapping process](img/tcc-boot.svg) + +## GCC + +- I tested it as a Cross-Compiler, so only the backend was tested. Again, no + bootstrap[^gcc-bootstrap]. +- It never compiled itself +- I didn't work on the C++ support + +[^gcc-bootstrap]: GCC has a bootstrap process that checks if the compiler works + correctly. First, it's compiled with your compiler of choice. Then, the + resulting binary is used to compile the GCC codebase again. The result of + that compiles GCC again. Then the binaries are compared, to make sure they + are identical. This can't be done in a cross-compiled environment, for + obvious reasons. + +# The work + +## TinyCC-Boot + +We focused on this, and let the other projects be carried by this effort. + +- Andrius and I spent many nights debugging crazy things in here. +- TinyCC's codebase is hard to read +- Many errors we got were not reproducible using a TinyCC compiled with GCC, so + we needed to build it from MesCC, which is very slow +- MesCC doesn't provide very helpful debug symbols +- We learned many things as we went + +> I wouldn't have the energy to make this without Andrius. + +## TinyCC-Boot - Crazy things + +1. Body is never executed: + + ``` c + if ( x < 8 ) { + // body + } + ``` +2. `A << 20 >> 20` +3. `__global_pointer$ is not a valid symbol` +4. Assembler uses a weird syntax and doesn't support Extended Assembly +5. `cannot cast from/to void` +6. And segfaults, segfaults everywhere! + +[Read more about them here](https://ekaitz.elenq.tech/bootstrapGcc8.html) + +## TinyCC-Boot - Results + +We finally managed to bootstrap TinyCC-Boot. + +- Andrius included the [build recipe in `live-bootstrap`](https://github.com/fosslinux/live-bootstrap/blob/master/steps/tcc-0.9.26/pass1.kaem) +- I did the [`guix` recipe](https://issues.guix.gnu.org/68222) + +## GNU Mes + +The problems we found in **TinyCC-Boot** made us improve Mes in several ways: + +- **MesCC**, the C compiler, had some limitations that were unnoticed in the i386 + bootstrap that appeared in RISC-V: faulty `switch-case`s, wrong `struct` + initializations, some integer weirdness... +- **MesLibC**, had to be split for our TinyCC as it doesn't support Extended + Assembly, and it also needed some extra tweaks: `char` signedness, `va_args` + support, build script support for the changes... + +> Having Janneke with us made the process very smooth. + + +## GCC + +- We compiled GCC with itself in a Debian machine in real RISC-V hardware +- Including C++ support +- [Only needed a few commits!](https://github.com/ekaitz-zarraga/gcc/compare/working-compiler...riscv) +- Guix recipe is pending, as we need to fix other steps to be able to add this: + `make`, a proper `libc`, TinyCC... + + +# Last words + +## Last words + +- People is important: I felt alone, and I didn't know how to continue. If I + had to spend another year working alone I would've just rejected the project. + **Bringing people gave me energy, knowledge and emotional support** + +- Money is important: thanks to NlNet I had the chance to pay people for their + work and buy some hardware. Covering people's basic needs is fundamental. + **Getting paid makes people independent, so they can focus on what they are + good at instead of struggling to survive** + +## So + +- Thanks, Andrius and Janneke +- Thanks, NlNet +- And... + +# Thank you + +## Contact and take part + +- Email me: +- Relevant IRC channels: `#bootstrappable`, `#guix`, `#guix-risc-v` + diff --git a/Fosdem2024/img/Bootstrapping_now.svg b/Fosdem2024/img/Bootstrapping_now.svg new file mode 100644 index 0000000..314dd32 --- /dev/null +++ b/Fosdem2024/img/Bootstrapping_now.svg @@ -0,0 +1,397 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Stage0-Posix + Guix and Live-Bootstrap + Mes + TinyCC-Boot + TinyCC + GCC (old) + GCC + The World™ + + diff --git a/Fosdem2024/img/Bootstrapping_simple.svg b/Fosdem2024/img/Bootstrapping_simple.svg new file mode 100644 index 0000000..1fbc6fc --- /dev/null +++ b/Fosdem2024/img/Bootstrapping_simple.svg @@ -0,0 +1,371 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Stage0-Posix + Mes + TinyCC-Boot + TinyCC + GCC (old) + GCC + The World™ + + diff --git a/Fosdem2024/img/Bootstrapping_with_backport.svg b/Fosdem2024/img/Bootstrapping_with_backport.svg new file mode 100644 index 0000000..44d1803 --- /dev/null +++ b/Fosdem2024/img/Bootstrapping_with_backport.svg @@ -0,0 +1,371 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Stage0-Posix + Mes + TinyCC-Boot + TinyCC + GCC (old) + GCC + The World™ + + diff --git a/Fosdem2024/img/Both.svg b/Fosdem2024/img/Both.svg new file mode 100644 index 0000000..144b76b --- /dev/null +++ b/Fosdem2024/img/Both.svg @@ -0,0 +1,177 @@ + +Logo-NGIAssure-tagLogo-NGIAssure-tag diff --git a/Fosdem2024/img/NGIAssure.svg b/Fosdem2024/img/NGIAssure.svg new file mode 100644 index 0000000..62b7c12 --- /dev/null +++ b/Fosdem2024/img/NGIAssure.svg @@ -0,0 +1 @@ +Logo-NGIAssure-tag diff --git a/Fosdem2024/img/bootstrapping.svg b/Fosdem2024/img/bootstrapping.svg new file mode 100644 index 0000000..2845e5a --- /dev/null +++ b/Fosdem2024/img/bootstrapping.svg @@ -0,0 +1,432 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Stage0-Posix + Mes + MesLibC + TinyCC-Boot + TinyCC + GCC (old) + GCC + The World™ + + + + diff --git a/Fosdem2024/img/nlnet.svg b/Fosdem2024/img/nlnet.svg new file mode 100644 index 0000000..373c8d8 --- /dev/null +++ b/Fosdem2024/img/nlnet.svg @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Fosdem2024/img/tcc-boot.svg b/Fosdem2024/img/tcc-boot.svg new file mode 100644 index 0000000..b53c61f --- /dev/null +++ b/Fosdem2024/img/tcc-boot.svg @@ -0,0 +1,749 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tcc-Boot5 + Tcc-Boot6 + + + + + + + + + + + + Tcc-Boot5 + Tcc-Boot6 + =? + + + + TinyCC-Boot + TinyCC-Boot source code + ... + MesCC + Tcc-Mes + Tcc-Boot0 + + Tcc-Boot1 + -D HAVE_BITFIELD=1 + + + diff --git a/Fosdem2024/template.tex b/Fosdem2024/template.tex new file mode 100644 index 0000000..15b1f41 --- /dev/null +++ b/Fosdem2024/template.tex @@ -0,0 +1,236 @@ +\documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$babel-lang$,$endif$$if(handout)$handout,$endif$$if(beamer)$ignorenonframetext,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$} +\setbeamertemplate{caption}[numbered] +\setbeamertemplate{caption label separator}{: } +% \selectcolormodel{gray} % Color B&W +\beamertemplatenavigationsymbols$if(navigation)$$navigation$$else$empty$endif$ + +\usepackage{amssymb,amsmath} +\usepackage{ifxetex,ifluatex} +\usepackage{fixltx2e} % provides \textsubscript + + +\usefonttheme{professionalfonts} % using non standard fonts for beamer +\usefonttheme{serif} % default family is serif + +% Font +\usepackage{fontspec} +\setmainfont[ + BoldFont = {*-Bold}, + ItalicFont = {*-Italic}, + BoldItalicFont = {*-BoldItalic}, +]{Lato} +\newcommand{\euro}{€} + +% Images with no captions +\usepackage{caption} +\captionsetup[figure]{labelformat=empty} + +% Bullet style +\useinnertheme{circles} +\newlength{\wideitemsep} +\setlength{\wideitemsep}{\itemsep} +\addtolength{\wideitemsep}{5pt} +\let\olditem\item +\renewcommand{\item}{\setlength{\itemsep}{\wideitemsep}\olditem} + +$if(euro)$ + \newcommand{\euro}{€} +$endif$ + +% Language +\usepackage{polyglossia} +\setmainlanguage[$polyglossia-lang.options$]{$polyglossia-lang.name$} + +\newif\ifbibliography +$if(natbib)$ +\usepackage{natbib} +\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$} +$endif$ +$if(biblatex)$ +\usepackage[$if(biblio-style)$style=$biblio-style$,$endif$$for(biblatexoptions)$$biblatexoptions$$sep$,$endfor$]{biblatex} +$for(bibliography)$ +\addbibresource{$bibliography$} +$endfor$ +$endif$ + +$if(verbatim-in-note)$ +\usepackage{fancyvrb} +$endif$ + +\hypersetup{ +$if(title-meta)$ + pdftitle={$title-meta$}, +$endif$ +$if(author-meta)$ + pdfauthor={$author-meta$}, +$endif$ +$if(keywords)$ + pdfkeywords={$for(keywords)$$keywords$$sep$, $endfor$}, +$endif$ +$if(colorlinks)$ + colorlinks=true, + linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$, + citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$, + urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$, +$else$ + pdfborder={0 0 0}, +$endif$ + breaklinks=true} +\urlstyle{same} % don't use monospace font for urls +$if(verbatim-in-note)$ +\VerbatimFootnotes % allows verbatim text in footnotes +$endif$ +$if(listings)$ +\usepackage{listings} +$endif$ +$if(lhs)$ +\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{} +$endif$ +$if(highlighting-macros)$ +$highlighting-macros$ +$endif$ +$if(tables)$ +\usepackage{longtable,booktabs} +\usepackage{caption} +% These lines are needed to make table captions work with longtable: +\makeatletter +\def\fnum@table{\tablename~\thetable} +\makeatother +$endif$ +$if(graphics)$ +\usepackage{graphicx,grffile} +\makeatletter +\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi} +\def\maxheight{\ifdim\Gin@nat@height>\textheight0.8\textheight\else\Gin@nat@height\fi} +\makeatother +% Scale images if necessary, so that they will not overflow the page +% margins by default, and it is still possible to overwrite the defaults +% using explicit options in \includegraphics[width, height, ...]{} +\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio} +$endif$ + +% Prevent slide breaks in the middle of a paragraph: +\widowpenalties 1 10000 +\raggedbottom + +$if(section-titles)$ +\AtBeginPart{ + \let\insertpartnumber\relax + \let\partname\relax + \frame{\partpage} +} +\AtBeginSection{ + \ifbibliography + \else + \let\insertsectionnumber\relax + \let\sectionname\relax + \frame{\sectionpage} + \fi +} +\AtBeginSubsection{ + \let\insertsubsectionnumber\relax + \let\subsectionname\relax + \frame{\subsectionpage} +} +$endif$ + +$if(links-as-notes)$ +% Make links footnotes instead of hotlinks: +\renewcommand{\href}[2]{#2\footnote{\url{#1}}} +$endif$ + +$if(strikeout)$ +\usepackage[normalem]{ulem} +% avoid problems with \sout in headers with hyperref: +\pdfstringdefDisableCommands{\renewcommand{\sout}{}} +$endif$ +\setlength{\parindent}{0pt} +\setlength{\parskip}{6pt plus 2pt minus 1pt} +\setlength{\emergencystretch}{3em} % prevent overfull lines +\providecommand{\tightlist}{% + \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} +$if(numbersections)$ +\setcounter{secnumdepth}{$if(secnumdepth)$$secnumdepth$$else$5$endif$} +$else$ +\setcounter{secnumdepth}{0} +$endif$ +$if(dir)$ +\ifxetex + % load bidi as late as possible as it modifies e.g. graphicx + $if(latex-dir-rtl)$ + \usepackage[RTLdocument]{bidi} + $else$ + \usepackage{bidi} + $endif$ +\fi +\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex + \TeXXeTstate=1 + \newcommand{\RL}[1]{\beginR #1\endR} + \newcommand{\LR}[1]{\beginL #1\endL} + \newenvironment{RTL}{\beginR}{\endR} + \newenvironment{LTR}{\beginL}{\endL} +\fi +$endif$ +$for(header-includes)$ +$header-includes$ +$endfor$ + +$if(title)$ +\title{$title$} +$endif$ +$if(subtitle)$ +\subtitle{$subtitle$} +$endif$ +$if(author)$ +\author{$for(author)$$author$$sep$ \and $endfor$} +$endif$ +$if(institute)$ +\institute{$for(institute)$$institute$$sep$ \and $endfor$} +$endif$ +\date{$date$} + +\begin{document} +$if(title)$ +\frame{\titlepage} +$endif$ + +$for(include-before)$ +$include-before$ + +$endfor$ +$if(toc)$ +\begin{frame} +\tableofcontents[hideallsubsections] +\end{frame} + +$endif$ +$body$ + +$if(natbib)$ +$if(bibliography)$ +$if(biblio-title)$ +$if(book-class)$ +\renewcommand\bibname{$biblio-title$} +$else$ +\renewcommand\refname{$biblio-title$} +$endif$ +$endif$ +\begin{frame}[allowframebreaks]{$biblio-title$} +\bibliographytrue +\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$} +\end{frame} + +$endif$ +$endif$ +$if(biblatex)$ +\begin{frame}[allowframebreaks]{$biblio-title$} +\bibliographytrue +\printbibliography[heading=none] +\end{frame} + +$endif$ +$for(include-after)$ +$include-after$ + +$endfor$ +\end{document} -- cgit v1.2.3