Files
linux-kernel-module-cheat/index.html
Ciro Santilli 六四事件 法轮功 5131afcd07 cb6ea17771
2019-10-27 00:00:00 +00:00

32634 lines
1.3 MiB
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.10">
<meta name="description" content="The perfect emulation setup to study and develop the &lt;&lt;linux-kernel&gt;&gt; v5.2.1, kernel modules, &lt;&lt;qemu-buildroot-setup,QEMU&gt;&gt;, &lt;&lt;gem5-buildroot-setup,gem5&gt;&gt; and x86_64, ARMv7 and ARMv8 &lt;&lt;userland-assembly,userland&gt;&gt; and &lt;&lt;baremetal-setup,baremetal&gt;&gt; assembly, &lt;&lt;c,ANSI C&gt;&gt;, &lt;&lt;cpp,C++&gt;&gt; and &lt;&lt;posix,POSIX&gt;&gt;. &lt;&lt;gdb&gt;&gt; and &lt;&lt;kgdb&gt;&gt; just work. Powered by &lt;&lt;about-the-qemu-buildroot-setup,Buildroot&gt;&gt; and &lt;&lt;about-the-baremetal-setup,crosstool-NG&gt;&gt;. Highly automated. Thoroughly documented. Automated &lt;&lt;test-this-repo,tests&gt;&gt;. "Tested" in an Ubuntu 18.04 host.">
<title>Linux Kernel Module Cheat</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<style>
/* Asciidoctor default stylesheet | MIT License | https://asciidoctor.org */
/* Uncomment @import statement to use as custom stylesheet */
/*@import "https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700";*/
article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section{display:block}
audio,video{display:inline-block}
audio:not([controls]){display:none;height:0}
html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}
a{background:none}
a:focus{outline:thin dotted}
a:active,a:hover{outline:0}
h1{font-size:2em;margin:.67em 0}
abbr[title]{border-bottom:1px dotted}
b,strong{font-weight:bold}
dfn{font-style:italic}
hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0}
mark{background:#ff0;color:#000}
code,kbd,pre,samp{font-family:monospace;font-size:1em}
pre{white-space:pre-wrap}
q{quotes:"\201C" "\201D" "\2018" "\2019"}
small{font-size:80%}
sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}
sup{top:-.5em}
sub{bottom:-.25em}
img{border:0}
svg:not(:root){overflow:hidden}
figure{margin:0}
fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}
legend{border:0;padding:0}
button,input,select,textarea{font-family:inherit;font-size:100%;margin:0}
button,input{line-height:normal}
button,select{text-transform:none}
button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer}
button[disabled],html input[disabled]{cursor:default}
input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0}
button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}
textarea{overflow:auto;vertical-align:top}
table{border-collapse:collapse;border-spacing:0}
*,*::before,*::after{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}
html,body{font-size:100%}
body{background:#fff;color:rgba(0,0,0,.8);padding:0;margin:0;font-family:"Noto Serif","DejaVu Serif",serif;font-weight:400;font-style:normal;line-height:1;position:relative;cursor:auto;tab-size:4;-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased}
a:hover{cursor:pointer}
img,object,embed{max-width:100%;height:auto}
object,embed{height:100%}
img{-ms-interpolation-mode:bicubic}
.left{float:left!important}
.right{float:right!important}
.text-left{text-align:left!important}
.text-right{text-align:right!important}
.text-center{text-align:center!important}
.text-justify{text-align:justify!important}
.hide{display:none}
img,object,svg{display:inline-block;vertical-align:middle}
textarea{height:auto;min-height:50px}
select{width:100%}
.center{margin-left:auto;margin-right:auto}
.stretch{width:100%}
.subheader,.admonitionblock td.content>.title,.audioblock>.title,.exampleblock>.title,.imageblock>.title,.listingblock>.title,.literalblock>.title,.stemblock>.title,.openblock>.title,.paragraph>.title,.quoteblock>.title,table.tableblock>.title,.verseblock>.title,.videoblock>.title,.dlist>.title,.olist>.title,.ulist>.title,.qlist>.title,.hdlist>.title{line-height:1.45;color:#7a2518;font-weight:400;margin-top:0;margin-bottom:.25em}
div,dl,dt,dd,ul,ol,li,h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6,pre,form,p,blockquote,th,td{margin:0;padding:0;direction:ltr}
a{color:#2156a5;text-decoration:underline;line-height:inherit}
a:hover,a:focus{color:#1d4b8f}
a img{border:0}
p{font-family:inherit;font-weight:400;font-size:1em;line-height:1.6;margin-bottom:1.25em;text-rendering:optimizeLegibility}
p aside{font-size:.875em;line-height:1.35;font-style:italic}
h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{font-family:"Open Sans","DejaVu Sans",sans-serif;font-weight:300;font-style:normal;color:#ba3925;text-rendering:optimizeLegibility;margin-top:1em;margin-bottom:.5em;line-height:1.0125em}
h1 small,h2 small,h3 small,#toctitle small,.sidebarblock>.content>.title small,h4 small,h5 small,h6 small{font-size:60%;color:#e99b8f;line-height:0}
h1{font-size:2.125em}
h2{font-size:1.6875em}
h3,#toctitle,.sidebarblock>.content>.title{font-size:1.375em}
h4,h5{font-size:1.125em}
h6{font-size:1em}
hr{border:solid #dddddf;border-width:1px 0 0;clear:both;margin:1.25em 0 1.1875em;height:0}
em,i{font-style:italic;line-height:inherit}
strong,b{font-weight:bold;line-height:inherit}
small{font-size:60%;line-height:inherit}
code{font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;font-weight:400;color:rgba(0,0,0,.9)}
ul,ol,dl{font-size:1em;line-height:1.6;margin-bottom:1.25em;list-style-position:outside;font-family:inherit}
ul,ol{margin-left:1.5em}
ul li ul,ul li ol{margin-left:1.25em;margin-bottom:0;font-size:1em}
ul.square li ul,ul.circle li ul,ul.disc li ul{list-style:inherit}
ul.square{list-style-type:square}
ul.circle{list-style-type:circle}
ul.disc{list-style-type:disc}
ol li ul,ol li ol{margin-left:1.25em;margin-bottom:0}
dl dt{margin-bottom:.3125em;font-weight:bold}
dl dd{margin-bottom:1.25em}
abbr,acronym{text-transform:uppercase;font-size:90%;color:rgba(0,0,0,.8);border-bottom:1px dotted #ddd;cursor:help}
abbr{text-transform:none}
blockquote{margin:0 0 1.25em;padding:.5625em 1.25em 0 1.1875em;border-left:1px solid #ddd}
blockquote cite{display:block;font-size:.9375em;color:rgba(0,0,0,.6)}
blockquote cite::before{content:"\2014 \0020"}
blockquote cite a,blockquote cite a:visited{color:rgba(0,0,0,.6)}
blockquote,blockquote p{line-height:1.6;color:rgba(0,0,0,.85)}
@media screen and (min-width:768px){h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{line-height:1.2}
h1{font-size:2.75em}
h2{font-size:2.3125em}
h3,#toctitle,.sidebarblock>.content>.title{font-size:1.6875em}
h4{font-size:1.4375em}}
table{background:#fff;margin-bottom:1.25em;border:solid 1px #dedede}
table thead,table tfoot{background:#f7f8f7}
table thead tr th,table thead tr td,table tfoot tr th,table tfoot tr td{padding:.5em .625em .625em;font-size:inherit;color:rgba(0,0,0,.8);text-align:left}
table tr th,table tr td{padding:.5625em .625em;font-size:inherit;color:rgba(0,0,0,.8)}
table tr.even,table tr.alt{background:#f8f8f7}
table thead tr th,table tfoot tr th,table tbody tr td,table tr td,table tfoot tr td{display:table-cell;line-height:1.6}
h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{line-height:1.2;word-spacing:-.05em}
h1 strong,h2 strong,h3 strong,#toctitle strong,.sidebarblock>.content>.title strong,h4 strong,h5 strong,h6 strong{font-weight:400}
.clearfix::before,.clearfix::after,.float-group::before,.float-group::after{content:" ";display:table}
.clearfix::after,.float-group::after{clear:both}
:not(pre):not([class^=L])>code{font-size:.9375em;font-style:normal!important;letter-spacing:0;padding:.1em .5ex;word-spacing:-.15em;background:#f7f7f8;-webkit-border-radius:4px;border-radius:4px;line-height:1.45;text-rendering:optimizeSpeed;word-wrap:break-word}
:not(pre)>code.nobreak{word-wrap:normal}
:not(pre)>code.nowrap{white-space:nowrap}
pre{color:rgba(0,0,0,.9);font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;line-height:1.45;text-rendering:optimizeSpeed}
pre code,pre pre{color:inherit;font-size:inherit;line-height:inherit}
pre>code{display:block}
pre.nowrap,pre.nowrap pre{white-space:pre;word-wrap:normal}
em em{font-style:normal}
strong strong{font-weight:400}
.keyseq{color:rgba(51,51,51,.8)}
kbd{font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;display:inline-block;color:rgba(0,0,0,.8);font-size:.65em;line-height:1.45;background:#f7f7f7;border:1px solid #ccc;-webkit-border-radius:3px;border-radius:3px;-webkit-box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em white inset;box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em #fff inset;margin:0 .15em;padding:.2em .5em;vertical-align:middle;position:relative;top:-.1em;white-space:nowrap}
.keyseq kbd:first-child{margin-left:0}
.keyseq kbd:last-child{margin-right:0}
.menuseq,.menuref{color:#000}
.menuseq b:not(.caret),.menuref{font-weight:inherit}
.menuseq{word-spacing:-.02em}
.menuseq b.caret{font-size:1.25em;line-height:.8}
.menuseq i.caret{font-weight:bold;text-align:center;width:.45em}
b.button::before,b.button::after{position:relative;top:-1px;font-weight:400}
b.button::before{content:"[";padding:0 3px 0 2px}
b.button::after{content:"]";padding:0 2px 0 3px}
p a>code:hover{color:rgba(0,0,0,.9)}
#header,#content,#footnotes,#footer{width:100%;margin-left:auto;margin-right:auto;margin-top:0;margin-bottom:0;max-width:62.5em;*zoom:1;position:relative;padding-left:.9375em;padding-right:.9375em}
#header::before,#header::after,#content::before,#content::after,#footnotes::before,#footnotes::after,#footer::before,#footer::after{content:" ";display:table}
#header::after,#content::after,#footnotes::after,#footer::after{clear:both}
#content{margin-top:1.25em}
#content::before{content:none}
#header>h1:first-child{color:rgba(0,0,0,.85);margin-top:2.25rem;margin-bottom:0}
#header>h1:first-child+#toc{margin-top:8px;border-top:1px solid #dddddf}
#header>h1:only-child,body.toc2 #header>h1:nth-last-child(2){border-bottom:1px solid #dddddf;padding-bottom:8px}
#header .details{border-bottom:1px solid #dddddf;line-height:1.45;padding-top:.25em;padding-bottom:.25em;padding-left:.25em;color:rgba(0,0,0,.6);display:-ms-flexbox;display:-webkit-flex;display:flex;-ms-flex-flow:row wrap;-webkit-flex-flow:row wrap;flex-flow:row wrap}
#header .details span:first-child{margin-left:-.125em}
#header .details span.email a{color:rgba(0,0,0,.85)}
#header .details br{display:none}
#header .details br+span::before{content:"\00a0\2013\00a0"}
#header .details br+span.author::before{content:"\00a0\22c5\00a0";color:rgba(0,0,0,.85)}
#header .details br+span#revremark::before{content:"\00a0|\00a0"}
#header #revnumber{text-transform:capitalize}
#header #revnumber::after{content:"\00a0"}
#content>h1:first-child:not([class]){color:rgba(0,0,0,.85);border-bottom:1px solid #dddddf;padding-bottom:8px;margin-top:0;padding-top:1rem;margin-bottom:1.25rem}
#toc{border-bottom:1px solid #e7e7e9;padding-bottom:.5em}
#toc>ul{margin-left:.125em}
#toc ul.sectlevel0>li>a{font-style:italic}
#toc ul.sectlevel0 ul.sectlevel1{margin:.5em 0}
#toc ul{font-family:"Open Sans","DejaVu Sans",sans-serif;list-style-type:none}
#toc li{line-height:1.3334;margin-top:.3334em}
#toc a{text-decoration:none}
#toc a:active{text-decoration:underline}
#toctitle{color:#7a2518;font-size:1.2em}
@media screen and (min-width:768px){#toctitle{font-size:1.375em}
body.toc2{padding-left:15em;padding-right:0}
#toc.toc2{margin-top:0!important;background:#f8f8f7;position:fixed;width:15em;left:0;top:0;border-right:1px solid #e7e7e9;border-top-width:0!important;border-bottom-width:0!important;z-index:1000;padding:1.25em 1em;height:100%;overflow:auto}
#toc.toc2 #toctitle{margin-top:0;margin-bottom:.8rem;font-size:1.2em}
#toc.toc2>ul{font-size:.9em;margin-bottom:0}
#toc.toc2 ul ul{margin-left:0;padding-left:1em}
#toc.toc2 ul.sectlevel0 ul.sectlevel1{padding-left:0;margin-top:.5em;margin-bottom:.5em}
body.toc2.toc-right{padding-left:0;padding-right:15em}
body.toc2.toc-right #toc.toc2{border-right-width:0;border-left:1px solid #e7e7e9;left:auto;right:0}}
@media screen and (min-width:1280px){body.toc2{padding-left:20em;padding-right:0}
#toc.toc2{width:20em}
#toc.toc2 #toctitle{font-size:1.375em}
#toc.toc2>ul{font-size:.95em}
#toc.toc2 ul ul{padding-left:1.25em}
body.toc2.toc-right{padding-left:0;padding-right:20em}}
#content #toc{border-style:solid;border-width:1px;border-color:#e0e0dc;margin-bottom:1.25em;padding:1.25em;background:#f8f8f7;-webkit-border-radius:4px;border-radius:4px}
#content #toc>:first-child{margin-top:0}
#content #toc>:last-child{margin-bottom:0}
#footer{max-width:100%;background:rgba(0,0,0,.8);padding:1.25em}
#footer-text{color:rgba(255,255,255,.8);line-height:1.44}
#content{margin-bottom:.625em}
.sect1{padding-bottom:.625em}
@media screen and (min-width:768px){#content{margin-bottom:1.25em}
.sect1{padding-bottom:1.25em}}
.sect1:last-child{padding-bottom:0}
.sect1+.sect1{border-top:1px solid #e7e7e9}
#content h1>a.anchor,h2>a.anchor,h3>a.anchor,#toctitle>a.anchor,.sidebarblock>.content>.title>a.anchor,h4>a.anchor,h5>a.anchor,h6>a.anchor{position:absolute;z-index:1001;width:1.5ex;margin-left:-1.5ex;display:block;text-decoration:none!important;visibility:hidden;text-align:center;font-weight:400}
#content h1>a.anchor::before,h2>a.anchor::before,h3>a.anchor::before,#toctitle>a.anchor::before,.sidebarblock>.content>.title>a.anchor::before,h4>a.anchor::before,h5>a.anchor::before,h6>a.anchor::before{content:"\00A7";font-size:.85em;display:block;padding-top:.1em}
#content h1:hover>a.anchor,#content h1>a.anchor:hover,h2:hover>a.anchor,h2>a.anchor:hover,h3:hover>a.anchor,#toctitle:hover>a.anchor,.sidebarblock>.content>.title:hover>a.anchor,h3>a.anchor:hover,#toctitle>a.anchor:hover,.sidebarblock>.content>.title>a.anchor:hover,h4:hover>a.anchor,h4>a.anchor:hover,h5:hover>a.anchor,h5>a.anchor:hover,h6:hover>a.anchor,h6>a.anchor:hover{visibility:visible}
#content h1>a.link,h2>a.link,h3>a.link,#toctitle>a.link,.sidebarblock>.content>.title>a.link,h4>a.link,h5>a.link,h6>a.link{color:#ba3925;text-decoration:none}
#content h1>a.link:hover,h2>a.link:hover,h3>a.link:hover,#toctitle>a.link:hover,.sidebarblock>.content>.title>a.link:hover,h4>a.link:hover,h5>a.link:hover,h6>a.link:hover{color:#a53221}
details,.audioblock,.imageblock,.literalblock,.listingblock,.stemblock,.videoblock{margin-bottom:1.25em}
details>summary:first-of-type{cursor:pointer;display:list-item;outline:none;margin-bottom:.75em}
.admonitionblock td.content>.title,.audioblock>.title,.exampleblock>.title,.imageblock>.title,.listingblock>.title,.literalblock>.title,.stemblock>.title,.openblock>.title,.paragraph>.title,.quoteblock>.title,table.tableblock>.title,.verseblock>.title,.videoblock>.title,.dlist>.title,.olist>.title,.ulist>.title,.qlist>.title,.hdlist>.title{text-rendering:optimizeLegibility;text-align:left;font-family:"Noto Serif","DejaVu Serif",serif;font-size:1rem;font-style:italic}
table.tableblock.fit-content>caption.title{white-space:nowrap;width:0}
.paragraph.lead>p,#preamble>.sectionbody>[class="paragraph"]:first-of-type p{font-size:1.21875em;line-height:1.6;color:rgba(0,0,0,.85)}
table.tableblock #preamble>.sectionbody>[class="paragraph"]:first-of-type p{font-size:inherit}
.admonitionblock>table{border-collapse:separate;border:0;background:none;width:100%}
.admonitionblock>table td.icon{text-align:center;width:80px}
.admonitionblock>table td.icon img{max-width:none}
.admonitionblock>table td.icon .title{font-weight:bold;font-family:"Open Sans","DejaVu Sans",sans-serif;text-transform:uppercase}
.admonitionblock>table td.content{padding-left:1.125em;padding-right:1.25em;border-left:1px solid #dddddf;color:rgba(0,0,0,.6)}
.admonitionblock>table td.content>:last-child>:last-child{margin-bottom:0}
.exampleblock>.content{border-style:solid;border-width:1px;border-color:#e6e6e6;margin-bottom:1.25em;padding:1.25em;background:#fff;-webkit-border-radius:4px;border-radius:4px}
.exampleblock>.content>:first-child{margin-top:0}
.exampleblock>.content>:last-child{margin-bottom:0}
.sidebarblock{border-style:solid;border-width:1px;border-color:#dbdbd6;margin-bottom:1.25em;padding:1.25em;background:#f3f3f2;-webkit-border-radius:4px;border-radius:4px}
.sidebarblock>:first-child{margin-top:0}
.sidebarblock>:last-child{margin-bottom:0}
.sidebarblock>.content>.title{color:#7a2518;margin-top:0;text-align:center}
.exampleblock>.content>:last-child>:last-child,.exampleblock>.content .olist>ol>li:last-child>:last-child,.exampleblock>.content .ulist>ul>li:last-child>:last-child,.exampleblock>.content .qlist>ol>li:last-child>:last-child,.sidebarblock>.content>:last-child>:last-child,.sidebarblock>.content .olist>ol>li:last-child>:last-child,.sidebarblock>.content .ulist>ul>li:last-child>:last-child,.sidebarblock>.content .qlist>ol>li:last-child>:last-child{margin-bottom:0}
.literalblock pre,.listingblock>.content>pre{-webkit-border-radius:4px;border-radius:4px;word-wrap:break-word;overflow-x:auto;padding:1em;font-size:.8125em}
@media screen and (min-width:768px){.literalblock pre,.listingblock>.content>pre{font-size:.90625em}}
@media screen and (min-width:1280px){.literalblock pre,.listingblock>.content>pre{font-size:1em}}
.literalblock pre,.listingblock>.content>pre:not(.highlight),.listingblock>.content>pre[class="highlight"],.listingblock>.content>pre[class^="highlight "]{background:#f7f7f8}
.literalblock.output pre{color:#f7f7f8;background:rgba(0,0,0,.9)}
.listingblock>.content{position:relative}
.listingblock code[data-lang]::before{display:none;content:attr(data-lang);position:absolute;font-size:.75em;top:.425rem;right:.5rem;line-height:1;text-transform:uppercase;color:inherit;opacity:.5}
.listingblock:hover code[data-lang]::before{display:block}
.listingblock.terminal pre .command::before{content:attr(data-prompt);padding-right:.5em;color:inherit;opacity:.5}
.listingblock.terminal pre .command:not([data-prompt])::before{content:"$"}
.listingblock pre.highlightjs{padding:0}
.listingblock pre.highlightjs>code{padding:1em;-webkit-border-radius:4px;border-radius:4px}
.listingblock pre.prettyprint{border-width:0}
.prettyprint{background:#f7f7f8}
pre.prettyprint .linenums{line-height:1.45;margin-left:2em}
pre.prettyprint li{background:none;list-style-type:inherit;padding-left:0}
pre.prettyprint li code[data-lang]::before{opacity:1}
pre.prettyprint li:not(:first-child) code[data-lang]::before{display:none}
table.linenotable{border-collapse:separate;border:0;margin-bottom:0;background:none}
table.linenotable td[class]{color:inherit;vertical-align:top;padding:0;line-height:inherit;white-space:normal}
table.linenotable td.code{padding-left:.75em}
table.linenotable td.linenos{border-right:1px solid currentColor;opacity:.35;padding-right:.5em}
pre.pygments .lineno{border-right:1px solid currentColor;opacity:.35;display:inline-block;margin-right:.75em}
pre.pygments .lineno::before{content:"";margin-right:-.125em}
.quoteblock{margin:0 1em 1.25em 1.5em;display:table}
.quoteblock:not(.excerpt)>.title{margin-left:-1.5em;margin-bottom:.75em}
.quoteblock blockquote,.quoteblock p{color:rgba(0,0,0,.85);font-size:1.15rem;line-height:1.75;word-spacing:.1em;letter-spacing:0;font-style:italic;text-align:justify}
.quoteblock blockquote{margin:0;padding:0;border:0}
.quoteblock blockquote::before{content:"\201c";float:left;font-size:2.75em;font-weight:bold;line-height:.6em;margin-left:-.6em;color:#7a2518;text-shadow:0 1px 2px rgba(0,0,0,.1)}
.quoteblock blockquote>.paragraph:last-child p{margin-bottom:0}
.quoteblock .attribution{margin-top:.75em;margin-right:.5ex;text-align:right}
.verseblock{margin:0 1em 1.25em}
.verseblock pre{font-family:"Open Sans","DejaVu Sans",sans;font-size:1.15rem;color:rgba(0,0,0,.85);font-weight:300;text-rendering:optimizeLegibility}
.verseblock pre strong{font-weight:400}
.verseblock .attribution{margin-top:1.25rem;margin-left:.5ex}
.quoteblock .attribution,.verseblock .attribution{font-size:.9375em;line-height:1.45;font-style:italic}
.quoteblock .attribution br,.verseblock .attribution br{display:none}
.quoteblock .attribution cite,.verseblock .attribution cite{display:block;letter-spacing:-.025em;color:rgba(0,0,0,.6)}
.quoteblock.abstract blockquote::before,.quoteblock.excerpt blockquote::before,.quoteblock .quoteblock blockquote::before{display:none}
.quoteblock.abstract blockquote,.quoteblock.abstract p,.quoteblock.excerpt blockquote,.quoteblock.excerpt p,.quoteblock .quoteblock blockquote,.quoteblock .quoteblock p{line-height:1.6;word-spacing:0}
.quoteblock.abstract{margin:0 1em 1.25em;display:block}
.quoteblock.abstract>.title{margin:0 0 .375em;font-size:1.15em;text-align:center}
.quoteblock.excerpt>blockquote,.quoteblock .quoteblock{padding:0 0 .25em 1em;border-left:.25em solid #dddddf}
.quoteblock.excerpt,.quoteblock .quoteblock{margin-left:0}
.quoteblock.excerpt blockquote,.quoteblock.excerpt p,.quoteblock .quoteblock blockquote,.quoteblock .quoteblock p{color:inherit;font-size:1.0625rem}
.quoteblock.excerpt .attribution,.quoteblock .quoteblock .attribution{color:inherit;text-align:left;margin-right:0}
table.tableblock{max-width:100%;border-collapse:separate}
p.tableblock:last-child{margin-bottom:0}
td.tableblock>.content>:last-child{margin-bottom:-1.25em}
td.tableblock>.content>:last-child.sidebarblock{margin-bottom:0}
table.tableblock,th.tableblock,td.tableblock{border:0 solid #dedede}
table.grid-all>thead>tr>.tableblock,table.grid-all>tbody>tr>.tableblock{border-width:0 1px 1px 0}
table.grid-all>tfoot>tr>.tableblock{border-width:1px 1px 0 0}
table.grid-cols>*>tr>.tableblock{border-width:0 1px 0 0}
table.grid-rows>thead>tr>.tableblock,table.grid-rows>tbody>tr>.tableblock{border-width:0 0 1px}
table.grid-rows>tfoot>tr>.tableblock{border-width:1px 0 0}
table.grid-all>*>tr>.tableblock:last-child,table.grid-cols>*>tr>.tableblock:last-child{border-right-width:0}
table.grid-all>tbody>tr:last-child>.tableblock,table.grid-all>thead:last-child>tr>.tableblock,table.grid-rows>tbody>tr:last-child>.tableblock,table.grid-rows>thead:last-child>tr>.tableblock{border-bottom-width:0}
table.frame-all{border-width:1px}
table.frame-sides{border-width:0 1px}
table.frame-topbot,table.frame-ends{border-width:1px 0}
table.stripes-all tr,table.stripes-odd tr:nth-of-type(odd),table.stripes-even tr:nth-of-type(even),table.stripes-hover tr:hover{background:#f8f8f7}
th.halign-left,td.halign-left{text-align:left}
th.halign-right,td.halign-right{text-align:right}
th.halign-center,td.halign-center{text-align:center}
th.valign-top,td.valign-top{vertical-align:top}
th.valign-bottom,td.valign-bottom{vertical-align:bottom}
th.valign-middle,td.valign-middle{vertical-align:middle}
table thead th,table tfoot th{font-weight:bold}
tbody tr th{display:table-cell;line-height:1.6;background:#f7f8f7}
tbody tr th,tbody tr th p,tfoot tr th,tfoot tr th p{color:rgba(0,0,0,.8);font-weight:bold}
p.tableblock>code:only-child{background:none;padding:0}
p.tableblock{font-size:1em}
ol{margin-left:1.75em}
ul li ol{margin-left:1.5em}
dl dd{margin-left:1.125em}
dl dd:last-child,dl dd:last-child>:last-child{margin-bottom:0}
ol>li p,ul>li p,ul dd,ol dd,.olist .olist,.ulist .ulist,.ulist .olist,.olist .ulist{margin-bottom:.625em}
ul.checklist,ul.none,ol.none,ul.no-bullet,ol.no-bullet,ol.unnumbered,ul.unstyled,ol.unstyled{list-style-type:none}
ul.no-bullet,ol.no-bullet,ol.unnumbered{margin-left:.625em}
ul.unstyled,ol.unstyled{margin-left:0}
ul.checklist{margin-left:.625em}
ul.checklist li>p:first-child>.fa-square-o:first-child,ul.checklist li>p:first-child>.fa-check-square-o:first-child{width:1.25em;font-size:.8em;position:relative;bottom:.125em}
ul.checklist li>p:first-child>input[type="checkbox"]:first-child{margin-right:.25em}
ul.inline{display:-ms-flexbox;display:-webkit-box;display:flex;-ms-flex-flow:row wrap;-webkit-flex-flow:row wrap;flex-flow:row wrap;list-style:none;margin:0 0 .625em -1.25em}
ul.inline>li{margin-left:1.25em}
.unstyled dl dt{font-weight:400;font-style:normal}
ol.arabic{list-style-type:decimal}
ol.decimal{list-style-type:decimal-leading-zero}
ol.loweralpha{list-style-type:lower-alpha}
ol.upperalpha{list-style-type:upper-alpha}
ol.lowerroman{list-style-type:lower-roman}
ol.upperroman{list-style-type:upper-roman}
ol.lowergreek{list-style-type:lower-greek}
.hdlist>table,.colist>table{border:0;background:none}
.hdlist>table>tbody>tr,.colist>table>tbody>tr{background:none}
td.hdlist1,td.hdlist2{vertical-align:top;padding:0 .625em}
td.hdlist1{font-weight:bold;padding-bottom:1.25em}
.literalblock+.colist,.listingblock+.colist{margin-top:-.5em}
.colist td:not([class]):first-child{padding:.4em .75em 0;line-height:1;vertical-align:top}
.colist td:not([class]):first-child img{max-width:none}
.colist td:not([class]):last-child{padding:.25em 0}
.thumb,.th{line-height:0;display:inline-block;border:solid 4px #fff;-webkit-box-shadow:0 0 0 1px #ddd;box-shadow:0 0 0 1px #ddd}
.imageblock.left{margin:.25em .625em 1.25em 0}
.imageblock.right{margin:.25em 0 1.25em .625em}
.imageblock>.title{margin-bottom:0}
.imageblock.thumb,.imageblock.th{border-width:6px}
.imageblock.thumb>.title,.imageblock.th>.title{padding:0 .125em}
.image.left,.image.right{margin-top:.25em;margin-bottom:.25em;display:inline-block;line-height:0}
.image.left{margin-right:.625em}
.image.right{margin-left:.625em}
a.image{text-decoration:none;display:inline-block}
a.image object{pointer-events:none}
sup.footnote,sup.footnoteref{font-size:.875em;position:static;vertical-align:super}
sup.footnote a,sup.footnoteref a{text-decoration:none}
sup.footnote a:active,sup.footnoteref a:active{text-decoration:underline}
#footnotes{padding-top:.75em;padding-bottom:.75em;margin-bottom:.625em}
#footnotes hr{width:20%;min-width:6.25em;margin:-.25em 0 .75em;border-width:1px 0 0}
#footnotes .footnote{padding:0 .375em 0 .225em;line-height:1.3334;font-size:.875em;margin-left:1.2em;margin-bottom:.2em}
#footnotes .footnote a:first-of-type{font-weight:bold;text-decoration:none;margin-left:-1.05em}
#footnotes .footnote:last-of-type{margin-bottom:0}
#content #footnotes{margin-top:-.625em;margin-bottom:0;padding:.75em 0}
.gist .file-data>table{border:0;background:#fff;width:100%;margin-bottom:0}
.gist .file-data>table td.line-data{width:99%}
div.unbreakable{page-break-inside:avoid}
.big{font-size:larger}
.small{font-size:smaller}
.underline{text-decoration:underline}
.overline{text-decoration:overline}
.line-through{text-decoration:line-through}
.aqua{color:#00bfbf}
.aqua-background{background:#00fafa}
.black{color:#000}
.black-background{background:#000}
.blue{color:#0000bf}
.blue-background{background:#0000fa}
.fuchsia{color:#bf00bf}
.fuchsia-background{background:#fa00fa}
.gray{color:#606060}
.gray-background{background:#7d7d7d}
.green{color:#006000}
.green-background{background:#007d00}
.lime{color:#00bf00}
.lime-background{background:#00fa00}
.maroon{color:#600000}
.maroon-background{background:#7d0000}
.navy{color:#000060}
.navy-background{background:#00007d}
.olive{color:#606000}
.olive-background{background:#7d7d00}
.purple{color:#600060}
.purple-background{background:#7d007d}
.red{color:#bf0000}
.red-background{background:#fa0000}
.silver{color:#909090}
.silver-background{background:#bcbcbc}
.teal{color:#006060}
.teal-background{background:#007d7d}
.white{color:#bfbfbf}
.white-background{background:#fafafa}
.yellow{color:#bfbf00}
.yellow-background{background:#fafa00}
span.icon>.fa{cursor:default}
a span.icon>.fa{cursor:inherit}
.admonitionblock td.icon [class^="fa icon-"]{font-size:2.5em;text-shadow:1px 1px 2px rgba(0,0,0,.5);cursor:default}
.admonitionblock td.icon .icon-note::before{content:"\f05a";color:#19407c}
.admonitionblock td.icon .icon-tip::before{content:"\f0eb";text-shadow:1px 1px 2px rgba(155,155,0,.8);color:#111}
.admonitionblock td.icon .icon-warning::before{content:"\f071";color:#bf6900}
.admonitionblock td.icon .icon-caution::before{content:"\f06d";color:#bf3400}
.admonitionblock td.icon .icon-important::before{content:"\f06a";color:#bf0000}
.conum[data-value]{display:inline-block;color:#fff!important;background:rgba(0,0,0,.8);-webkit-border-radius:100px;border-radius:100px;text-align:center;font-size:.75em;width:1.67em;height:1.67em;line-height:1.67em;font-family:"Open Sans","DejaVu Sans",sans-serif;font-style:normal;font-weight:bold}
.conum[data-value] *{color:#fff!important}
.conum[data-value]+b{display:none}
.conum[data-value]::after{content:attr(data-value)}
pre .conum[data-value]{position:relative;top:-.125em}
b.conum *{color:inherit!important}
.conum:not([data-value]):empty{display:none}
dt,th.tableblock,td.content,div.footnote{text-rendering:optimizeLegibility}
h1,h2,p,td.content,span.alt{letter-spacing:-.01em}
p strong,td.content strong,div.footnote strong{letter-spacing:-.005em}
p,blockquote,dt,td.content,span.alt{font-size:1.0625rem}
p{margin-bottom:1.25rem}
.sidebarblock p,.sidebarblock dt,.sidebarblock td.content,p.tableblock{font-size:1em}
.exampleblock>.content{background:#fffef7;border-color:#e0e0dc;-webkit-box-shadow:0 1px 4px #e0e0dc;box-shadow:0 1px 4px #e0e0dc}
.print-only{display:none!important}
@page{margin:1.25cm .75cm}
@media print{*{-webkit-box-shadow:none!important;box-shadow:none!important;text-shadow:none!important}
html{font-size:80%}
a{color:inherit!important;text-decoration:underline!important}
a.bare,a[href^="#"],a[href^="mailto:"]{text-decoration:none!important}
a[href^="http:"]:not(.bare)::after,a[href^="https:"]:not(.bare)::after{content:"(" attr(href) ")";display:inline-block;font-size:.875em;padding-left:.25em}
abbr[title]::after{content:" (" attr(title) ")"}
pre,blockquote,tr,img,object,svg{page-break-inside:avoid}
thead{display:table-header-group}
svg{max-width:100%}
p,blockquote,dt,td.content{font-size:1em;orphans:3;widows:3}
h2,h3,#toctitle,.sidebarblock>.content>.title{page-break-after:avoid}
#toc,.sidebarblock,.exampleblock>.content{background:none!important}
#toc{border-bottom:1px solid #dddddf!important;padding-bottom:0!important}
body.book #header{text-align:center}
body.book #header>h1:first-child{border:0!important;margin:2.5em 0 1em}
body.book #header .details{border:0!important;display:block;padding:0!important}
body.book #header .details span:first-child{margin-left:0!important}
body.book #header .details br{display:block}
body.book #header .details br+span::before{content:none!important}
body.book #toc{border:0!important;text-align:left!important;padding:0!important;margin:0!important}
body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-break-before:always}
.listingblock code[data-lang]::before{display:block}
#footer{padding:0 .9375em}
.hide-on-print{display:none!important}
.print-only{display:block!important}
.hide-for-print{display:none!important}
.show-for-print{display:inherit!important}}
@media print,amzn-kf8{#header>h1:first-child{margin-top:1.25rem}
.sect1{padding:0!important}
.sect1+.sect1{border:0}
#footer{background:none}
#footer-text{color:rgba(0,0,0,.6);font-size:.9em}}
@media amzn-kf8{#header,#content,#footnotes,#footer{padding:0}}
</style>
</head>
<body class="article">
<div id="header">
<h1>Linux Kernel Module Cheat</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p><a href="https://zenodo.org/badge/latestdoi/64534859"><span class="image"><img src="https://zenodo.org/badge/64534859.svg" alt="64534859"></span></a></p>
</div>
<div class="paragraph">
<p>The perfect emulation setup to study and develop the <a href="#linux-kernel">Linux kernel</a> v5.2.1, kernel modules, <a href="#qemu-buildroot-setup">QEMU</a>, <a href="#gem5-buildroot-setup">gem5</a> and x86_64, ARMv7 and ARMv8 <a href="#userland-assembly">userland</a> and <a href="#baremetal-setup">baremetal</a> assembly, <a href="#c">ANSI C</a>, <a href="#cpp">C++</a> and <a href="#posix">POSIX</a>. <a href="#gdb">GDB step debug</a> and <a href="#kgdb">KGDB</a> just work. Powered by <a href="#about-the-qemu-buildroot-setup">Buildroot</a> and <a href="#about-the-baremetal-setup">crosstool-NG</a>. Highly automated. Thoroughly documented. Automated <a href="#test-this-repo">tests</a>. "Tested" in an Ubuntu 18.04 host.</p>
</div>
<div class="paragraph">
<p>TL;DR: <a href="#qemu-buildroot-setup-getting-started">Section 1.1.1, &#8220;QEMU Buildroot setup getting started&#8221;</a></p>
</div>
<div class="paragraph">
<p>The source code for this page is located at: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat</a>. Due to <a href="https://github.com/isaacs/github/issues/1610">a GitHub limitation</a>, this README is too long and not fully rendered on github.com. Either use: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/README.adoc">README.adoc</a>, <a href="https://cirosantilli.com/linux-kernel-module-cheat" class="bare">https://cirosantilli.com/linux-kernel-module-cheat</a> or <a href="#build-the-documentation">build the docs yourself</a>.</p>
</div>
<div id="toc" class="toc">
<div id="toctitle" class="title"></div>
<ul class="sectlevel1">
<li><a href="#getting-started">1. Getting started</a>
<ul class="sectlevel2">
<li><a href="#qemu-buildroot-setup">1.1. QEMU Buildroot setup</a>
<ul class="sectlevel3">
<li><a href="#qemu-buildroot-setup-getting-started">1.1.1. QEMU Buildroot setup getting started</a></li>
<li><a href="#how-to-hack-stuff">1.1.2. How to hack stuff</a>
<ul class="sectlevel4">
<li><a href="#your-first-linux-kernel-hack">1.1.2.1. Your first Linux kernel hack</a></li>
<li><a href="#your-first-kernel-module-hack">1.1.2.2. Your first kernel module hack</a></li>
<li><a href="#your-first-qemu-hack">1.1.2.3. Your first QEMU hack</a></li>
<li><a href="#your-first-glibc-hack">1.1.2.4. Your first glibc hack</a></li>
<li><a href="#your-first-binutils-hack">1.1.2.5. Your first Binutils hack</a></li>
<li><a href="#your-first-gcc-hack">1.1.2.6. Your first GCC hack</a></li>
</ul>
</li>
<li><a href="#about-the-qemu-buildroot-setup">1.1.3. About the QEMU Buildroot setup</a></li>
</ul>
</li>
<li><a href="#gem5-buildroot-setup">1.2. gem5 Buildroot setup</a>
<ul class="sectlevel3">
<li><a href="#about-the-gem5-buildroot-setup">1.2.1. About the gem5 Buildroot setup</a></li>
<li><a href="#gem5-buildroot-setup-getting-started">1.2.2. gem5 Buildroot setup getting started</a></li>
</ul>
</li>
<li><a href="#docker">1.3. Docker host setup</a></li>
<li><a href="#prebuilt">1.4. Prebuilt setup</a>
<ul class="sectlevel3">
<li><a href="#about-the-prebuilt-setup">1.4.1. About the prebuilt setup</a></li>
<li><a href="#prebuilt-setup-getting-started">1.4.2. Prebuilt setup getting started</a></li>
</ul>
</li>
<li><a href="#host">1.5. Host kernel module setup</a>
<ul class="sectlevel3">
<li><a href="#hello-host">1.5.1. Hello host</a></li>
</ul>
</li>
<li><a href="#userland-setup">1.6. Userland setup</a>
<ul class="sectlevel3">
<li><a href="#about-the-userland-setup">1.6.1. About the userland setup</a></li>
<li><a href="#userland-setup-getting-started">1.6.2. Userland setup getting started</a>
<ul class="sectlevel4">
<li><a href="#userland-setup-getting-started-natively">1.6.2.1. Userland setup getting started natively</a></li>
<li><a href="#userland-setup-getting-started-with-prebuilt-toolchain-and-qemu-user-mode">1.6.2.2. Userland setup getting started with prebuilt toolchain and QEMU user mode</a></li>
<li><a href="#userland-setup-getting-started-full-system">1.6.2.3. Userland setup getting started full system</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#baremetal-setup">1.7. Baremetal setup</a>
<ul class="sectlevel3">
<li><a href="#about-the-baremetal-setup">1.7.1. About the baremetal setup</a></li>
<li><a href="#baremetal-setup-getting-started">1.7.2. Baremetal setup getting started</a></li>
</ul>
</li>
<li><a href="#build-the-documentation">1.8. Build the documentation</a></li>
</ul>
</li>
<li><a href="#gdb">2. GDB step debug</a>
<ul class="sectlevel2">
<li><a href="#gdb-step-debug-kernel-boot">2.1. GDB step debug kernel boot</a>
<ul class="sectlevel3">
<li><a href="#gdb-step-debug-kernel-boot-other-archs">2.1.1. GDB step debug kernel boot other archs</a></li>
<li><a href="#kernel-o0">2.1.2. Disable kernel compiler optimizations</a></li>
</ul>
</li>
<li><a href="#gdb-step-debug-kernel-post-boot">2.2. GDB step debug kernel post-boot</a></li>
<li><a href="#tmux">2.3. tmux</a>
<ul class="sectlevel3">
<li><a href="#tmux-gem5">2.3.1. tmux gem5</a></li>
</ul>
</li>
<li><a href="#gdb-step-debug-kernel-module">2.4. GDB step debug kernel module</a>
<ul class="sectlevel3">
<li><a href="#gdb-step-debug-kernel-module-arm">2.4.1. GDB step debug kernel module insmodded by init on ARM</a></li>
<li><a href="#gdb-module_init">2.4.2. GDB module_init</a>
<ul class="sectlevel4">
<li><a href="#gdb-module_init-step-into-it">2.4.2.1. GDB module_init step into it</a></li>
<li><a href="#gdb-module_init-calculate-entry-address">2.4.2.2. GDB module_init calculate entry address</a></li>
<li><a href="#gdb-module_init-break-at-the-end-of-sys_init_module">2.4.2.3. GDB module_init break at the end of sys_init_module</a></li>
<li><a href="#gdb-module_init-add-trap-instruction">2.4.2.4. GDB module_init add trap instruction</a></li>
</ul>
</li>
<li><a href="#bypass-lx-symbols">2.4.3. Bypass lx-symbols</a></li>
</ul>
</li>
<li><a href="#gdb-step-debug-early-boot">2.5. GDB step debug early boot</a>
<ul class="sectlevel3">
<li><a href="#gdb-step-debug-early-boot-by-address">2.5.1. GDB step debug early boot by address</a></li>
</ul>
</li>
<li><a href="#gdb-step-debug-userland-processes">2.6. GDB step debug userland processes</a>
<ul class="sectlevel3">
<li><a href="#gdb-step-debug-userland-custom-init">2.6.1. GDB step debug userland custom init</a></li>
<li><a href="#gdb-step-debug-userland-busybox-init">2.6.2. GDB step debug userland BusyBox init</a></li>
<li><a href="#gdb-step-debug-userland-non-init">2.6.3. GDB step debug userland non-init</a>
<ul class="sectlevel4">
<li><a href="#gdb-step-debug-userland-non-init-without-gdb-wait">2.6.3.1. GDB step debug userland non-init without --gdb-wait</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#gdb-call">2.7. GDB call</a></li>
<li><a href="#gdb-view-arm-system-registers">2.8. GDB view ARM system registers</a></li>
<li><a href="#gdb-step-debug-multicore-userland">2.9. GDB step debug multicore userland</a></li>
<li><a href="#linux-kernel-gdb-scripts">2.10. Linux kernel GDB scripts</a>
<ul class="sectlevel3">
<li><a href="#lx-ps">2.10.1. lx-ps</a></li>
</ul>
</li>
<li><a href="#debug-the-gdb-remote-protocol">2.11. Debug the GDB remote protocol</a>
<ul class="sectlevel3">
<li><a href="#remote-g-packet">2.11.1. Remote 'g' packet reply is too long</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#kgdb">3. KGDB</a>
<ul class="sectlevel2">
<li><a href="#kgdb-arm">3.1. KGDB ARM</a></li>
<li><a href="#kgdb-kernel-modules">3.2. KGDB kernel modules</a></li>
<li><a href="#kdb">3.3. KDB</a>
<ul class="sectlevel3">
<li><a href="#kdb-graphic">3.3.1. KDB graphic</a></li>
<li><a href="#kdb-arm">3.3.2. KDB ARM</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#gdbserver">4. gdbserver</a>
<ul class="sectlevel2">
<li><a href="#gdbserver-busybox">4.1. gdbserver BusyBox</a></li>
<li><a href="#gdbserver-libc">4.2. gdbserver libc</a></li>
<li><a href="#gdbserver-dynamic-loader">4.3. gdbserver dynamic loader</a></li>
</ul>
</li>
<li><a href="#cpu-architecture">5. CPU architecture</a>
<ul class="sectlevel2">
<li><a href="#x86_64">5.1. x86_64</a>
<ul class="sectlevel3">
<li><a href="#ring0">5.1.1. ring0</a></li>
</ul>
</li>
<li><a href="#arm">5.2. arm</a>
<ul class="sectlevel3">
<li><a href="#run-arm-executable-in-aarch64">5.2.1. Run arm executable in aarch64</a></li>
</ul>
</li>
<li><a href="#mips">5.3. MIPS</a></li>
<li><a href="#other-architectures">5.4. Other architectures</a></li>
</ul>
</li>
<li><a href="#init">6. init</a>
<ul class="sectlevel2">
<li><a href="#replace-init">6.1. Replace init</a>
<ul class="sectlevel3">
<li><a href="#poweroff-out">6.1.1. poweroff.out</a></li>
<li><a href="#sleep_forever-out">6.1.2. sleep_forever.out</a></li>
<li><a href="#time_boot-out">6.1.3. time_boot.out</a></li>
</ul>
</li>
<li><a href="#init-busybox">6.2. Run command at the end of BusyBox init</a></li>
<li><a href="#path-to-init">6.3. Path to init</a></li>
<li><a href="#init-environment">6.4. Init environment</a>
<ul class="sectlevel3">
<li><a href="#init-arguments">6.4.1. init arguments</a></li>
<li><a href="#init-environment-env">6.4.2. init environment env</a></li>
<li><a href="#busybox-shell-init-environment">6.4.3. BusyBox shell init environment</a>
<ul class="sectlevel4">
<li><a href="#busybox-shell-initrc-files">6.4.3.1. BusyBox shell initrc files</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#initrd">7. initrd</a>
<ul class="sectlevel2">
<li><a href="#initrd-in-desktop-distros">7.1. initrd in desktop distros</a></li>
<li><a href="#initramfs">7.2. initramfs</a></li>
<li><a href="#rootfs">7.3. rootfs</a>
<ul class="sectlevel3">
<li><a href="#devroot">7.3.1. /dev/root</a></li>
</ul>
</li>
<li><a href="#gem5-initrd">7.4. gem5 initrd</a></li>
<li><a href="#gem5-initramfs">7.5. gem5 initramfs</a></li>
</ul>
</li>
<li><a href="#device-tree">8. Device tree</a>
<ul class="sectlevel2">
<li><a href="#dtb-files">8.1. DTB files</a></li>
<li><a href="#device-tree-syntax">8.2. Device tree syntax</a></li>
<li><a href="#get-device-tree-from-a-running-kernel">8.3. Get device tree from a running kernel</a></li>
<li><a href="#device-tree-emulator-generation">8.4. Device tree emulator generation</a></li>
</ul>
</li>
<li><a href="#kvm">9. KVM</a>
<ul class="sectlevel2">
<li><a href="#kvm-arm">9.1. KVM arm</a></li>
<li><a href="#gem5-kvm">9.2. gem5 KVM</a></li>
</ul>
</li>
<li><a href="#user-mode-simulation">10. User mode simulation</a>
<ul class="sectlevel2">
<li><a href="#qemu-user-mode-getting-started">10.1. QEMU user mode getting started</a>
<ul class="sectlevel3">
<li><a href="#user-mode-gdb">10.1.1. User mode GDB</a></li>
</ul>
</li>
<li><a href="#user-mode-tests">10.2. User mode tests</a></li>
<li><a href="#user-mode-buildroot-executables">10.3. User mode Buildroot executables</a></li>
<li><a href="#user-mode-simulation-with-glibc">10.4. User mode simulation with glibc</a>
<ul class="sectlevel3">
<li><a href="#fatal-kernel-too-old">10.4.1. FATAL: kernel too old</a></li>
<li><a href="#stack-smashing-detected">10.4.2. stack smashing detected</a></li>
</ul>
</li>
<li><a href="#user-mode-static-executables">10.5. User mode static executables</a>
<ul class="sectlevel3">
<li><a href="#user-mode-static-executables-with-dynamic-libraries">10.5.1. User mode static executables with dynamic libraries</a></li>
</ul>
</li>
<li><a href="#gem5-syscall-emulation-mode">10.6. gem5 syscall emulation mode</a>
<ul class="sectlevel3">
<li><a href="#gem5-syscall-emulation-exit-status">10.6.1. gem5 syscall emulation exit status</a></li>
<li><a href="#gem5-syscall-emulation-mode-program-stdin">10.6.2. gem5 syscall emulation mode program stdin</a></li>
<li><a href="#user-mode-vs-full-system-benchmark">10.6.3. User mode vs full system benchmark</a></li>
<li><a href="#gem5-syscall-emulation-mode-syscall-tracing">10.6.4. gem5 syscall emulation mode syscall tracing</a></li>
</ul>
</li>
<li><a href="#qemu-user-mode-quirks">10.7. QEMU user mode quirks</a>
<ul class="sectlevel3">
<li><a href="#qemu-user-mode-does-not-show-stdout-immediately">10.7.1. QEMU user mode does not show stdout immediately</a>
<ul class="sectlevel4">
<li><a href="#qemu-user-mode-does-not-show-errors">10.7.1.1. QEMU user mode does not show errors</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#kernel-module-utilities">11. Kernel module utilities</a>
<ul class="sectlevel2">
<li><a href="#insmod">11.1. insmod</a></li>
<li><a href="#myinsmod">11.2. myinsmod</a></li>
<li><a href="#modprobe">11.3. modprobe</a></li>
<li><a href="#kmod">11.4. kmod</a>
<ul class="sectlevel3">
<li><a href="#module-init-tools">11.4.1. module-init-tools</a></li>
<li><a href="#kmod-modprobe">11.4.2. kmod modprobe</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#filesystems">12. Filesystems</a>
<ul class="sectlevel2">
<li><a href="#overlayfs">12.1. OverlayFS</a></li>
<li><a href="#secondary-disk">12.2. Secondary disk</a></li>
</ul>
</li>
<li><a href="#graphics">13. Graphics</a>
<ul class="sectlevel2">
<li><a href="#qemu-text-mode">13.1. QEMU text mode</a>
<ul class="sectlevel3">
<li><a href="#quit-qemu-from-text-mode">13.1.1. Quit QEMU from text mode</a></li>
</ul>
</li>
<li><a href="#qemu-graphic-mode">13.2. QEMU graphic mode</a>
<ul class="sectlevel3">
<li><a href="#scroll-up-in-graphic-mode">13.2.1. Scroll up in graphic mode</a></li>
<li><a href="#qemu-graphic-mode-arm">13.2.2. QEMU Graphic mode arm</a>
<ul class="sectlevel4">
<li><a href="#qemu-graphic-mode-arm-terminal">13.2.2.1. QEMU graphic mode arm terminal</a></li>
<li><a href="#qemu-graphic-mode-arm-terminal-implementation">13.2.2.2. QEMU graphic mode arm terminal implementation</a></li>
<li><a href="#qemu-graphic-mode-arm-vga">13.2.2.3. QEMU graphic mode arm VGA</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#gem5-graphic-mode">13.3. gem5 graphic mode</a>
<ul class="sectlevel3">
<li><a href="#graphic-mode-gem5-aarch64">13.3.1. Graphic mode gem5 aarch64</a></li>
<li><a href="#gem5-graphic-mode-dp650">13.3.2. gem5 graphic mode DP650</a></li>
<li><a href="#gem5-graphic-mode-internals">13.3.3. gem5 graphic mode internals</a></li>
</ul>
</li>
<li><a href="#x11">13.4. X11 Buildroot</a>
<ul class="sectlevel3">
<li><a href="#x11-buildroot-mouse-not-moving">13.4.1. X11 Buildroot mouse not moving</a></li>
<li><a href="#x11-buildroot-arm">13.4.2. X11 Buildroot ARM</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#networking">14. Networking</a>
<ul class="sectlevel2">
<li><a href="#enable-networking">14.1. Enable networking</a></li>
<li><a href="#ping">14.2. ping</a></li>
<li><a href="#guest-host-networking">14.3. Guest host networking</a>
<ul class="sectlevel3">
<li><a href="#host-to-guest-networking">14.3.1. Host to guest networking</a>
<ul class="sectlevel4">
<li><a href="#nc-host-to-guest">14.3.1.1. nc host to guest</a></li>
<li><a href="#ssh-into-guest">14.3.1.2. ssh into guest</a></li>
<li><a href="#gem5-host-to-guest-networking">14.3.1.3. gem5 host to guest networking</a></li>
</ul>
</li>
<li><a href="#guest-to-host-networking">14.3.2. Guest to host networking</a></li>
</ul>
</li>
<li><a href="#9p">14.4. 9P</a>
<ul class="sectlevel3">
<li><a href="#9p-vs-nfs">14.4.1. 9P vs NFS</a></li>
<li><a href="#9p-getting-started">14.4.2. 9P getting started</a></li>
<li><a href="#9p-gem5">14.4.3. 9P gem5</a></li>
<li><a href="#nfs">14.4.4. NFS</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#linux-kernel">15. Linux kernel</a>
<ul class="sectlevel2">
<li><a href="#linux-kernel-configuration">15.1. Linux kernel configuration</a>
<ul class="sectlevel3">
<li><a href="#modify-kernel-config">15.1.1. Modify kernel config</a></li>
<li><a href="#find-the-kernel-config">15.1.2. Find the kernel config</a></li>
<li><a href="#kernel-configs-about">15.1.3. About our Linux kernel configs</a>
<ul class="sectlevel4">
<li><a href="#buildroot-kernel-config">15.1.3.1. About Buildroot&#8217;s kernel configs</a>
<ul class="sectlevel5">
<li><a href="#linux-kernel-defconfig">15.1.3.1.1. Linux kernel defconfig</a></li>
<li><a href="#linux-kernel-min-config">15.1.3.1.2. Linux kernel min config</a></li>
</ul>
</li>
<li><a href="#notable-alternate-gem5-kernel-configs">15.1.3.2. Notable alternate gem5 kernel configs</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#kernel-version">15.2. Kernel version</a>
<ul class="sectlevel3">
<li><a href="#find-the-kernel-version">15.2.1. Find the kernel version</a></li>
<li><a href="#update-the-linux-kernel">15.2.2. Update the Linux kernel</a>
<ul class="sectlevel4">
<li><a href="#update-the-linux-kernel-lkmc-procedure">15.2.2.1. Update the Linux kernel LKMC procedure</a></li>
</ul>
</li>
<li><a href="#downgrade-the-linux-kernel">15.2.3. Downgrade the Linux kernel</a></li>
</ul>
</li>
<li><a href="#kernel-command-line-parameters">15.3. Kernel command line parameters</a>
<ul class="sectlevel3">
<li><a href="#kernel-command-line-parameters-escaping">15.3.1. Kernel command line parameters escaping</a></li>
<li><a href="#kernel-command-line-parameters-definition-points">15.3.2. Kernel command line parameters definition points</a></li>
<li><a href="#rw">15.3.3. rw</a></li>
<li><a href="#norandmaps">15.3.4. norandmaps</a></li>
</ul>
</li>
<li><a href="#printk">15.4. printk</a>
<ul class="sectlevel3">
<li><a href="#procsyskernelprintk">15.4.1. /proc/sys/kernel/printk</a></li>
<li><a href="#ignore_loglevel">15.4.2. ignore_loglevel</a></li>
<li><a href="#pr_debug">15.4.3. pr_debug</a>
<ul class="sectlevel4">
<li><a href="#pr_debug-printkkern_debug">15.4.3.1. pr_debug != printk(KERN_DEBUG</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#linux-kernel-entry-point">15.5. Linux kernel entry point</a></li>
<li><a href="#kernel-module-apis">15.6. Kernel module APIs</a>
<ul class="sectlevel3">
<li><a href="#kernel-module-parameters">15.6.1. Kernel module parameters</a>
<ul class="sectlevel4">
<li><a href="#modprobe-conf">15.6.1.1. modprobe.conf</a></li>
</ul>
</li>
<li><a href="#kernel-module-dependencies">15.6.2. Kernel module dependencies</a>
<ul class="sectlevel4">
<li><a href="#kernel-module-dependencies-with-modprobe">15.6.2.1. Kernel module dependencies with modprobe</a></li>
</ul>
</li>
<li><a href="#module_info">15.6.3. MODULE_INFO</a></li>
<li><a href="#vermagic">15.6.4. vermagic</a></li>
<li><a href="#init_module">15.6.5. init_module</a></li>
<li><a href="#floating-point-in-kernel-modules">15.6.6. Floating point in kernel modules</a></li>
</ul>
</li>
<li><a href="#kernel-panic-and-oops">15.7. Kernel panic and oops</a>
<ul class="sectlevel3">
<li><a href="#kernel-panic">15.7.1. Kernel panic</a>
<ul class="sectlevel4">
<li><a href="#kernel-module-stack-trace-to-source-line">15.7.1.1. Kernel module stack trace to source line</a></li>
<li><a href="#bug_on">15.7.1.2. BUG_ON</a></li>
<li><a href="#exit-emulator-on-panic">15.7.1.3. Exit emulator on panic</a>
<ul class="sectlevel5">
<li><a href="#exit-qemu-on-panic">15.7.1.3.1. Exit QEMU on panic</a></li>
<li><a href="#exit-gem5-on-panic">15.7.1.3.2. Exit gem5 on panic</a></li>
</ul>
</li>
<li><a href="#reboot-on-panic">15.7.1.4. Reboot on panic</a></li>
<li><a href="#panic-trace-show-addresses-instead-of-symbols">15.7.1.5. Panic trace show addresses instead of symbols</a></li>
</ul>
</li>
<li><a href="#oops">15.7.2. Kernel oops</a></li>
<li><a href="#dump_stack">15.7.3. dump_stack</a></li>
<li><a href="#warn_on">15.7.4. WARN_ON</a></li>
</ul>
</li>
<li><a href="#pseudo-filesystems">15.8. Pseudo filesystems</a>
<ul class="sectlevel3">
<li><a href="#debugfs">15.8.1. debugfs</a></li>
<li><a href="#procfs">15.8.2. procfs</a>
<ul class="sectlevel4">
<li><a href="#proc-version">15.8.2.1. /proc/version</a></li>
</ul>
</li>
<li><a href="#sysfs">15.8.3. sysfs</a></li>
<li><a href="#character-devices">15.8.4. Character devices</a>
<ul class="sectlevel4">
<li><a href="#automatically-create-character-device-file-on-insmod">15.8.4.1. Automatically create character device file on insmod</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#pseudo-files">15.9. Pseudo files</a>
<ul class="sectlevel3">
<li><a href="#file-operations">15.9.1. File operations</a></li>
<li><a href="#seq_file">15.9.2. seq_file</a>
<ul class="sectlevel4">
<li><a href="#seq_file-single_open">15.9.2.1. seq_file single_open</a></li>
</ul>
</li>
<li><a href="#poll">15.9.3. poll</a></li>
<li><a href="#ioctl">15.9.4. ioctl</a></li>
<li><a href="#mmap">15.9.5. mmap</a></li>
<li><a href="#anonymous-inode">15.9.6. Anonymous inode</a></li>
<li><a href="#netlink-sockets">15.9.7. netlink sockets</a></li>
</ul>
</li>
<li><a href="#kthread">15.10. kthread</a>
<ul class="sectlevel3">
<li><a href="#kthreads">15.10.1. kthreads</a></li>
<li><a href="#sleep">15.10.2. sleep</a></li>
<li><a href="#workqueues">15.10.3. Workqueues</a>
<ul class="sectlevel4">
<li><a href="#workqueue-from-workqueue">15.10.3.1. Workqueue from workqueue</a></li>
</ul>
</li>
<li><a href="#schedule">15.10.4. schedule</a></li>
<li><a href="#wait-queues">15.10.5. Wait queues</a></li>
</ul>
</li>
<li><a href="#timers">15.11. Timers</a></li>
<li><a href="#irq">15.12. IRQ</a>
<ul class="sectlevel3">
<li><a href="#irq-ko">15.12.1. irq.ko</a></li>
<li><a href="#dummy-irq">15.12.2. dummy-irq</a></li>
<li><a href="#procinterrupts">15.12.3. /proc/interrupts</a></li>
</ul>
</li>
<li><a href="#kernel-utility-functions">15.13. Kernel utility functions</a>
<ul class="sectlevel3">
<li><a href="#kstrto">15.13.1. kstrto</a></li>
<li><a href="#virt_to_phys">15.13.2. virt_to_phys</a>
<ul class="sectlevel4">
<li><a href="#userland-physical-address-experiments">15.13.2.1. Userland physical address experiments</a>
<ul class="sectlevel5">
<li><a href="#qemu-xp">15.13.2.1.1. QEMU xp</a></li>
<li><a href="#dev-mem">15.13.2.1.2. /dev/mem</a></li>
<li><a href="#pagemap_dump-out">15.13.2.1.3. pagemap_dump.out</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#linux-kernel-tracing">15.14. Linux kernel tracing</a>
<ul class="sectlevel3">
<li><a href="#config_proc_events">15.14.1. CONFIG_PROC_EVENTS</a>
<ul class="sectlevel4">
<li><a href="#config_proc_events-aarch64">15.14.1.1. CONFIG_PROC_EVENTS aarch64</a></li>
</ul>
</li>
<li><a href="#ftrace">15.14.2. ftrace</a>
<ul class="sectlevel4">
<li><a href="#ftrace-system-calls">15.14.2.1. ftrace system calls</a></li>
<li><a href="#trace-cmd">15.14.2.2. trace-cmd</a></li>
</ul>
</li>
<li><a href="#kprobes">15.14.3. Kprobes</a></li>
<li><a href="#count-boot-instructions">15.14.4. Count boot instructions</a></li>
</ul>
</li>
<li><a href="#linux-kernel-hardening">15.15. Linux kernel hardening</a>
<ul class="sectlevel3">
<li><a href="#config_fortify_source">15.15.1. CONFIG_FORTIFY_SOURCE</a></li>
<li><a href="#linux-security-modules">15.15.2. Linux security modules</a>
<ul class="sectlevel4">
<li><a href="#selinux">15.15.2.1. SELinux</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#user-mode-linux">15.16. User mode Linux</a></li>
<li><a href="#uio">15.17. UIO</a></li>
<li><a href="#linux-kernel-interactive-stuff">15.18. Linux kernel interactive stuff</a>
<ul class="sectlevel3">
<li><a href="#fbcon">15.18.1. Linux kernel console fun</a></li>
<li><a href="#linux-kernel-magic-keys">15.18.2. Linux kernel magic keys</a>
<ul class="sectlevel4">
<li><a href="#ctrl-alt-del">15.18.2.1. Ctrl Alt Del</a></li>
<li><a href="#sysrq">15.18.2.2. SysRq</a></li>
</ul>
</li>
<li><a href="#tty">15.18.3. TTY</a>
<ul class="sectlevel4">
<li><a href="#start-a-getty-from-outside-of-init">15.18.3.1. Start a getty from outside of init</a></li>
<li><a href="#console-kernel-boot-parameter">15.18.3.2. console kernel boot parameter</a></li>
</ul>
</li>
<li><a href="#config_logo">15.18.4. CONFIG_LOGO</a></li>
</ul>
</li>
<li><a href="#drm">15.19. DRM</a>
<ul class="sectlevel3">
<li><a href="#kmscube">15.19.1. kmscube</a></li>
<li><a href="#kmscon">15.19.2. kmscon</a></li>
<li><a href="#libdri2">15.19.3. libdri2</a></li>
</ul>
</li>
<li><a href="#linux-kernel-testing">15.20. Linux kernel testing</a>
<ul class="sectlevel3">
<li><a href="#linux-test-project">15.20.1. Linux Test Project</a></li>
<li><a href="#stress">15.20.2. stress</a></li>
</ul>
</li>
<li><a href="#linux-kernel-build-system">15.21. Linux kernel build system</a>
<ul class="sectlevel3">
<li><a href="#vmlinux-vs-bzimage-vs-zimage-vs-image">15.21.1. vmlinux vs bzImage vs zImage vs Image</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#xen">16. Xen</a></li>
<li><a href="#u-boot">17. U-Boot</a></li>
<li><a href="#qemu">18. QEMU</a>
<ul class="sectlevel2">
<li><a href="#introduction-to-qemu">18.1. Introduction to QEMU</a></li>
<li><a href="#disk-persistency">18.2. Disk persistency</a>
<ul class="sectlevel3">
<li><a href="#gem5-disk-persistency">18.2.1. gem5 disk persistency</a></li>
</ul>
</li>
<li><a href="#gem5-qcow2">18.3. gem5 qcow2</a></li>
<li><a href="#snapshot">18.4. Snapshot</a>
<ul class="sectlevel3">
<li><a href="#snapshot-internals">18.4.1. Snapshot internals</a></li>
</ul>
</li>
<li><a href="#device-models">18.5. Device models</a>
<ul class="sectlevel3">
<li><a href="#pci">18.5.1. PCI</a>
<ul class="sectlevel4">
<li><a href="#pci_min">18.5.1.1. pci_min</a></li>
<li><a href="#qemu-edu">18.5.1.2. QEMU edu PCI device</a></li>
<li><a href="#manipulate-pci-registers-directly">18.5.1.3. Manipulate PCI registers directly</a></li>
<li><a href="#pciutils">18.5.1.4. pciutils</a></li>
<li><a href="#introduction-to-pci">18.5.1.5. Introduction to PCI</a></li>
<li><a href="#pci-bfd">18.5.1.6. PCI BFD</a></li>
<li><a href="#pci-bar">18.5.1.7. PCI BAR</a></li>
</ul>
</li>
<li><a href="#gpio">18.5.2. GPIO</a></li>
<li><a href="#leds">18.5.3. LEDs</a></li>
<li><a href="#platform_device">18.5.4. platform_device</a></li>
<li><a href="#gem5-educational-hardware-models">18.5.5. gem5 educational hardware models</a></li>
</ul>
</li>
<li><a href="#qemu-monitor">18.6. QEMU monitor</a>
<ul class="sectlevel3">
<li><a href="#qemu-monitor-from-guest">18.6.1. QEMU monitor from guest</a></li>
<li><a href="#qemu-monitor-from-gdb">18.6.2. QEMU monitor from GDB</a></li>
</ul>
</li>
<li><a href="#debug-the-emulator">18.7. Debug the emulator</a>
<ul class="sectlevel3">
<li><a href="#reverse-debug-the-emulator">18.7.1. Reverse debug the emulator</a></li>
<li><a href="#debug-gem5-python-scripts">18.7.2. Debug gem5 Python scripts</a></li>
</ul>
</li>
<li><a href="#tracing">18.8. Tracing</a>
<ul class="sectlevel3">
<li><a href="#qemu-d-tracing">18.8.1. QEMU -d tracing</a></li>
<li><a href="#qemu-trace-register-values">18.8.2. QEMU trace register values</a></li>
<li><a href="#qemu-trace-memory-accesses">18.8.3. QEMU trace memory accesses</a></li>
<li><a href="#trace-source-lines">18.8.4. Trace source lines</a></li>
<li><a href="#qemu-record-and-replay">18.8.5. QEMU record and replay</a>
<ul class="sectlevel4">
<li><a href="#qemu-reverse-debugging">18.8.5.1. QEMU reverse debugging</a></li>
</ul>
</li>
<li><a href="#qemu-trace-multicore">18.8.6. QEMU trace multicore</a></li>
<li><a href="#gem5-tracing">18.8.7. gem5 tracing</a>
<ul class="sectlevel4">
<li><a href="#gem5-trace-internals">18.8.7.1. gem5 trace internals</a></li>
<li><a href="#gem5-execall-trace-format">18.8.7.2. gem5 ExecAll trace format</a></li>
<li><a href="#gem5-registers-trace-format">18.8.7.3. gem5 Registers trace format</a></li>
<li><a href="#gem5-tarmac-traces">18.8.7.4. gem5 TARMAC traces</a></li>
<li><a href="#gem5-tracing-internals">18.8.7.5. gem5 tracing internals</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#qemu-gui-is-unresponsive">18.9. QEMU GUI is unresponsive</a></li>
</ul>
</li>
<li><a href="#gem5">19. gem5</a>
<ul class="sectlevel2">
<li><a href="#gem5-vs-qemu">19.1. gem5 vs QEMU</a></li>
<li><a href="#gem5-run-benchmark">19.2. gem5 run benchmark</a>
<ul class="sectlevel3">
<li><a href="#skip-extra-benchmark-instructions">19.2.1. Skip extra benchmark instructions</a></li>
<li><a href="#gem5-system-parameters">19.2.2. gem5 system parameters</a>
<ul class="sectlevel4">
<li><a href="#number-of-cores">19.2.2.1. Number of cores</a>
<ul class="sectlevel5">
<li><a href="#qemu-user-mode-multithreading">19.2.2.1.1. QEMU user mode multithreading</a></li>
<li><a href="#gem5-syscall-emulation-multithreading">19.2.2.1.2. gem5 syscall emulation multithreading</a></li>
<li><a href="#gem5-se-py-user-mode-with-2-or-more-pthreads-fails-with-because-simulate-limit-reached">19.2.2.1.3. gem5 se.py user mode with 2 or more pthreads fails with because simulate() limit reached</a></li>
<li><a href="#gem5-arm-full-system-with-more-than-8-cores">19.2.2.1.4. gem5 ARM full system with more than 8 cores</a></li>
</ul>
</li>
<li><a href="#gem5-cache-size">19.2.2.2. gem5 cache size</a></li>
<li><a href="#gem5-memory-latency">19.2.2.3. gem5 memory latency</a></li>
<li><a href="#memory-size">19.2.2.4. Memory size</a></li>
<li><a href="#gem5-disk-and-network-latency">19.2.2.5. gem5 disk and network latency</a></li>
<li><a href="#gem5-clock-frequency">19.2.2.6. gem5 clock frequency</a></li>
</ul>
</li>
<li><a href="#interesting-benchmarks">19.2.3. Interesting benchmarks</a>
<ul class="sectlevel4">
<li><a href="#dhrystone">19.2.3.1. Dhrystone</a></li>
<li><a href="#bst-vs-heap-vs-hashmap">19.2.3.2. BST vs heap vs hashmap</a></li>
<li><a href="#blas">19.2.3.3. BLAS</a></li>
<li><a href="#eigen">19.2.3.4. Eigen</a></li>
<li><a href="#parsec-benchmark">19.2.3.5. PARSEC benchmark</a>
<ul class="sectlevel5">
<li><a href="#parsec-benchmark-without-parsecmgmt">19.2.3.5.1. PARSEC benchmark without parsecmgmt</a></li>
<li><a href="#parsec-change-the-input-size">19.2.3.5.2. PARSEC change the input size</a></li>
<li><a href="#parsec-benchmark-with-parsecmgmt">19.2.3.5.3. PARSEC benchmark with parsecmgmt</a></li>
<li><a href="#parsec-uninstall">19.2.3.5.4. PARSEC uninstall</a></li>
<li><a href="#parsec-benchmark-hacking">19.2.3.5.5. PARSEC benchmark hacking</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#gem5-kernel-command-line-parameters">19.3. gem5 kernel command line parameters</a></li>
<li><a href="#gem5-gdb">19.4. gem5 GDB step debug</a>
<ul class="sectlevel3">
<li><a href="#gem5-gdb-step-debug-kernel">19.4.1. gem5 GDB step debug kernel</a></li>
<li><a href="#gem5-gdb-step-debug-userland-process">19.4.2. gem5 GDB step debug userland process</a></li>
</ul>
</li>
<li><a href="#gem5-checkpoint">19.5. gem5 checkpoint</a>
<ul class="sectlevel3">
<li><a href="#gem5-checkpoint-internals">19.5.1. gem5 checkpoint internals</a></li>
<li><a href="#gem5-restore-new-script">19.5.2. gem5 checkpoint restore and run a different script</a></li>
<li><a href="#gem5-restore-checkpoint-with-a-different-cpu">19.5.3. gem5 restore checkpoint with a different CPU</a></li>
</ul>
</li>
<li><a href="#pass-extra-options-to-gem5">19.6. Pass extra options to gem5</a></li>
<li><a href="#m5ops">19.7. m5ops</a>
<ul class="sectlevel3">
<li><a href="#m5">19.7.1. m5</a>
<ul class="sectlevel4">
<li><a href="#m5-exit">19.7.1.1. m5 exit</a></li>
<li><a href="#m5-fail">19.7.1.2. m5 fail</a></li>
<li><a href="#m5-writefile">19.7.1.3. m5 writefile</a></li>
<li><a href="#m5-readfile">19.7.1.4. m5 readfile</a></li>
<li><a href="#m5-initparam">19.7.1.5. m5 initparam</a></li>
<li><a href="#m5-execfile">19.7.1.6. m5 execfile</a></li>
</ul>
</li>
<li><a href="#m5ops-instructions">19.7.2. m5ops instructions</a>
<ul class="sectlevel4">
<li><a href="#m5ops-instructions-interface">19.7.2.1. m5ops instructions interface</a></li>
<li><a href="#m5op-annotations">19.7.2.2. m5op annotations</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#gem5-arm-linux-kernel-patches">19.8. gem5 arm Linux kernel patches</a>
<ul class="sectlevel3">
<li><a href="#gem5-arm-linux-kernel-patches-boot-speedup">19.8.1. gem5 arm Linux kernel patches boot speedup</a></li>
</ul>
</li>
<li><a href="#m5out-directory">19.9. m5out directory</a>
<ul class="sectlevel3">
<li><a href="#gem5-m5out-system-terminal-file">19.9.1. gem5 m5out/system.terminal file</a></li>
<li><a href="#gem5-m5out-stats-txt-file">19.9.2. gem5 m5out/stats.txt file</a>
<ul class="sectlevel4">
<li><a href="#gem5-only-dump-selected-stats">19.9.2.1. gem5 only dump selected stats</a></li>
</ul>
</li>
<li><a href="#gem5-config-ini">19.9.3. gem5 config.ini</a></li>
</ul>
</li>
<li><a href="#m5term">19.10. m5term</a></li>
<li><a href="#gem5-python-scripts-without-rebuild">19.11. gem5 Python scripts without rebuild</a></li>
<li><a href="#gem5-fs_biglittle">19.12. gem5 fs_bigLITTLE</a></li>
<li><a href="#gem5-unit-tests">19.13. gem5 unit tests</a></li>
<li><a href="#gem5-regression-tests">19.14. gem5 regression tests</a></li>
<li><a href="#gem5-simulate-limit-reached">19.15. gem5 simulate() limit reached</a></li>
<li><a href="#gem5-build-options">19.16. gem5 build options</a>
<ul class="sectlevel3">
<li><a href="#gem5-debug-build">19.16.1. gem5 debug build</a></li>
<li><a href="#gem5-clang-build">19.16.2. gem5 clang build</a></li>
<li><a href="#gem5-sanitation-build">19.16.3. gem5 sanitation build</a></li>
<li><a href="#gem5-ruby-build">19.16.4. gem5 Ruby build</a></li>
<li><a href="#gem5-python-3-build">19.16.5. gem5 Python 3 build</a></li>
</ul>
</li>
<li><a href="#gem5-cpu-types">19.17. gem5 CPU types</a>
<ul class="sectlevel3">
<li><a href="#gem5-basesimplecpu">19.17.1. gem5 BaseSimpleCPU</a></li>
<li><a href="#gem5-minorcpu">19.17.2. gem5 MinorCPU</a></li>
<li><a href="#gem5-deriveo3cpu">19.17.3. gem5 DeriveO3CPU</a></li>
<li><a href="#gem5-arm-rsk">19.17.4. gem5 ARM RSK</a></li>
</ul>
</li>
<li><a href="#gem5-arm-platforms">19.18. gem5 ARM platforms</a></li>
<li><a href="#gem5-internals">19.19. gem5 internals</a>
<ul class="sectlevel3">
<li><a href="#gem5-eclipse-configuration">19.19.1. gem5 Eclipse configuration</a></li>
<li><a href="#gem5-python-c-interaction">19.19.2. gem5 Python C++ interaction</a></li>
<li><a href="#gem5-entry-point">19.19.3. gem5 entry point</a></li>
<li><a href="#gem5-event-queue">19.19.4. gem5 event queue</a>
<ul class="sectlevel4">
<li><a href="#gem5-event-queue-atomicsimplecpu-syscall-emulation-freestanding-example-analysis">19.19.4.1. gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis</a></li>
<li><a href="#gem5-event-queue-minorcpu-syscall-emulation-freestanding-example-analysis">19.19.4.2. gem5 event queue MinorCPU syscall emulation freestanding example analysis</a></li>
</ul>
</li>
<li><a href="#gem5-stats-internals">19.19.5. gem5 stats internals</a></li>
<li><a href="#gem5-code-generation">19.19.6. gem5 code generation</a>
<ul class="sectlevel4">
<li><a href="#gem5-the_isa">19.19.6.1. gem5 THE_ISA</a></li>
</ul>
</li>
<li><a href="#gem5-build-system">19.19.7. gem5 build system</a>
<ul class="sectlevel4">
<li><a href="#gem5-polymorphic-isa-includes">19.19.7.1. gem5 polymorphic ISA includes</a></li>
<li><a href="#why-are-all-c-symlinked-into-the-gem5-build-dir">19.19.7.2. Why are all C++ symlinked into the gem5 build dir?</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#buildroot">20. Buildroot</a>
<ul class="sectlevel2">
<li><a href="#introduction-to-buildroot">20.1. Introduction to Buildroot</a></li>
<li><a href="#custom-buildroot-configs">20.2. Custom Buildroot configs</a>
<ul class="sectlevel3">
<li><a href="#enable-buildroot-compiler-optimizations">20.2.1. Enable Buildroot compiler optimizations</a></li>
</ul>
</li>
<li><a href="#find-buildroot-options-with-make-menuconfig">20.3. Find Buildroot options with make menuconfig</a></li>
<li><a href="#change-user">20.4. Change user</a>
<ul class="sectlevel3">
<li><a href="#login-as-a-non-root-user-without-password">20.4.1. Login as a non-root user without password</a></li>
</ul>
</li>
<li><a href="#add-new-buildroot-packages">20.5. Add new Buildroot packages</a></li>
<li><a href="#remove-buildroot-packages">20.6. Remove Buildroot packages</a></li>
<li><a href="#br2_target_rootfs_ext2_size">20.7. BR2_TARGET_ROOTFS_EXT2_SIZE</a>
<ul class="sectlevel3">
<li><a href="#squashfs">20.7.1. SquashFS</a></li>
</ul>
</li>
<li><a href="#rpath">20.8. Buildroot rebuild is slow when the root filesystem is large</a></li>
<li><a href="#report-upstream-bugs">20.9. Report upstream bugs</a></li>
<li><a href="#libc-choice">20.10. libc choice</a></li>
<li><a href="#buildroot-hello-world">20.11. Buildroot hello world</a></li>
</ul>
</li>
<li><a href="#userland-content">21. Userland content</a>
<ul class="sectlevel2">
<li><a href="#c">21.1. C</a>
<ul class="sectlevel3">
<li><a href="#malloc">21.1.1. malloc</a>
<ul class="sectlevel4">
<li><a href="#malloc-implementation">21.1.1.1. malloc implementation</a></li>
<li><a href="#malloc-maximum-size">21.1.1.2. malloc maximum size</a>
<ul class="sectlevel5">
<li><a href="#linux-out-of-memory-killer">21.1.1.2.1. Linux out-of-memory killer</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#c-multithreading">21.1.2. C multithreading</a></li>
<li><a href="#gcc-c-extensions">21.1.3. GCC C extensions</a>
<ul class="sectlevel4">
<li><a href="#c-empty-struct">21.1.3.1. C empty struct</a></li>
<li><a href="#openmp">21.1.3.2. OpenMP</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#cpp">21.2. C++</a>
<ul class="sectlevel3">
<li><a href="#cpp-multithreading">21.2.1. C++ multithreading</a></li>
<li><a href="#cpp-standards">21.2.2. C++ standards</a>
<ul class="sectlevel4">
<li><a href="#cpp17">21.2.2.1. C++17 N4659 standards draft</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#posix">21.3. POSIX</a>
<ul class="sectlevel3">
<li><a href="#unistd-h">21.3.1. unistd.h</a></li>
<li><a href="#pthreads">21.3.2. pthreads</a></li>
<li><a href="#sysconf">21.3.3. sysconf</a></li>
<li><a href="#mmap-2">21.3.4. mmap</a>
<ul class="sectlevel4">
<li><a href="#mmap-map-anonymous">21.3.4.1. mmap MAP_ANONYMOUS</a></li>
<li><a href="#mmap-file">21.3.4.2. mmap file</a></li>
<li><a href="#brk">21.3.4.3. brk</a></li>
</ul>
</li>
<li><a href="#socket">21.3.5. socket</a></li>
</ul>
</li>
<li><a href="#userland-multithreading">21.4. Userland multithreading</a></li>
<li><a href="#c-debugging">21.5. C debugging</a>
<ul class="sectlevel3">
<li><a href="#stack-smashing">21.5.1. Stack smashing</a></li>
<li><a href="#memory-leaks">21.5.2. Memory leaks</a></li>
</ul>
</li>
<li><a href="#interpreted-languages">21.6. Interpreted languages</a></li>
<li><a href="#node-js">21.7. Node.js</a></li>
<li><a href="#userland-content-bibliography">21.8. Userland content bibliography</a></li>
</ul>
</li>
<li><a href="#userland-assembly">22. Userland assembly</a>
<ul class="sectlevel2">
<li><a href="#assembly-registers">22.1. Assembly registers</a>
<ul class="sectlevel3">
<li><a href="#armv8-aarch64-x31-register">22.1.1. ARMv8 aarch64 x31 register</a></li>
</ul>
</li>
<li><a href="#floating-point-assembly">22.2. Floating point assembly</a></li>
<li><a href="#simd-assembly">22.3. SIMD assembly</a>
<ul class="sectlevel3">
<li><a href="#fma-instruction">22.3.1. FMA instruction</a></li>
</ul>
</li>
<li><a href="#user-vs-system-assembly">22.4. User vs system assembly</a></li>
<li><a href="#userland-assembly-c-standard-library">22.5. Userland assembly C standard library</a>
<ul class="sectlevel3">
<li><a href="#freestanding-programs">22.5.1. Freestanding programs</a>
<ul class="sectlevel4">
<li><a href="#nostartfiles-programs">22.5.1.1. nostartfiles programs</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#gcc-inline-assembly">22.6. GCC inline assembly</a>
<ul class="sectlevel3">
<li><a href="#gcc-inline-assembly-register-variables">22.6.1. GCC inline assembly register variables</a></li>
<li><a href="#gcc-inline-assembly-scratch-registers">22.6.2. GCC inline assembly scratch registers</a></li>
<li><a href="#gcc-inline-assembly-early-clobbers">22.6.3. GCC inline assembly early-clobbers</a></li>
<li><a href="#gcc-inline-assembly-floating-point-arm">22.6.4. GCC inline assembly floating point ARM</a></li>
<li><a href="#gcc-intrinsics">22.6.5. GCC intrinsics</a>
<ul class="sectlevel4">
<li><a href="#gcc-x86-intrinsics">22.6.5.1. GCC x86 intrinsics</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#linux-system-calls">22.7. Linux system calls</a></li>
<li><a href="#linux-calling-conventions">22.8. Linux calling conventions</a>
<ul class="sectlevel3">
<li><a href="#x86_64-calling-convention">22.8.1. x86_64 calling convention</a></li>
<li><a href="#arm-calling-convention">22.8.2. ARM calling convention</a></li>
</ul>
</li>
<li><a href="#gnu-gas-assembler">22.9. GNU GAS assembler</a>
<ul class="sectlevel3">
<li><a href="#gnu-gas-assembler-comments">22.9.1. GNU GAS assembler comments</a></li>
<li><a href="#gnu-gas-assembler-immediates">22.9.2. GNU GAS assembler immediates</a></li>
<li><a href="#gnu-gas-assembler-data-sizes">22.9.3. GNU GAS assembler data sizes</a>
<ul class="sectlevel4">
<li><a href="#gnu-gas-assembler-arm-specifics">22.9.3.1. GNU GAS assembler ARM specifics</a>
<ul class="sectlevel5">
<li><a href="#gnu-gas-assembler-arm-unified-syntax">22.9.3.1.1. GNU GAS assembler ARM unified syntax</a></li>
</ul>
</li>
<li><a href="#gnu-gas-assembler-arm-n-and-w-suffixes">22.9.3.2. GNU GAS assembler ARM .n and .w suffixes</a></li>
</ul>
</li>
<li><a href="#gnu-gas-assembler-char-literals">22.9.4. GNU GAS assembler char literals</a></li>
</ul>
</li>
<li><a href="#nop-instructions">22.10. NOP instructions</a></li>
</ul>
</li>
<li><a href="#x86-userland-assembly">23. x86 userland assembly</a>
<ul class="sectlevel2">
<li><a href="#x86-registers">23.1. x86 registers</a>
<ul class="sectlevel3">
<li><a href="#x86-flags-registers">23.1.1. x86 FLAGS registers</a></li>
</ul>
</li>
<li><a href="#x86-addressing-modes">23.2. x86 addressing modes</a></li>
<li><a href="#x86-data-transfer-instructions">23.3. x86 data transfer instructions</a>
<ul class="sectlevel3">
<li><a href="#x86-exchange-instructions">23.3.1. x86 exchange instructions</a>
<ul class="sectlevel4">
<li><a href="#x86-cmpxchg-instruction">23.3.1.1. x86 CMPXCHG instruction</a></li>
</ul>
</li>
<li><a href="#x86-push-and-pop-instructions">23.3.2. x86 PUSH and POP instructions</a></li>
<li><a href="#x86-cqto-and-cltq-instructions">23.3.3. x86 CQTO and CLTQ instructions</a></li>
<li><a href="#x86-cmovcc-instructions">23.3.4. x86 CMOVcc instructions</a></li>
</ul>
</li>
<li><a href="#x86-binary-arithmetic-instructions">23.4. x86 binary arithmetic instructions</a></li>
<li><a href="#x86-logical-instructions">23.5. x86 logical instructions</a></li>
<li><a href="#x86-shift-and-rotate-instructions">23.6. x86 shift and rotate instructions</a></li>
<li><a href="#x86-bit-and-byte-instructions">23.7. x86 bit and byte instructions</a></li>
<li><a href="#x86-control-transfer-instructions">23.8. x86 control transfer instructions</a>
<ul class="sectlevel3">
<li><a href="#x86-jcc-instructions">23.8.1. x86 Jcc instructions</a></li>
<li><a href="#x86-loop-instruction">23.8.2. x86 LOOP instruction</a></li>
<li><a href="#x86-string-instructions">23.8.3. x86 string instructions</a>
<ul class="sectlevel4">
<li><a href="#x86-rep-prefix">23.8.3.1. x86 REP prefix</a></li>
</ul>
</li>
<li><a href="#x86-enter-and-leave-instructions">23.8.4. x86 ENTER and LEAVE instructions</a></li>
</ul>
</li>
<li><a href="#x86-miscellaneous-instructions">23.9. x86 miscellaneous instructions</a></li>
<li><a href="#x86-random-number-generator-instructions">23.10. x86 random number generator instructions</a>
<ul class="sectlevel3">
<li><a href="#x86-cpuid-instruction">23.10.1. x86 CPUID instruction</a></li>
</ul>
</li>
<li><a href="#x86-x87-fpu-instructions">23.11. x86 x87 FPU instructions</a>
<ul class="sectlevel3">
<li><a href="#x86-x87-fpu-vs-simd">23.11.1. x86 x87 FPU vs SIMD</a></li>
</ul>
</li>
<li><a href="#x86-simd">23.12. x86 SIMD</a>
<ul class="sectlevel3">
<li><a href="#x86-sse-instructions">23.12.1. x86 SSE instructions</a>
<ul class="sectlevel4">
<li><a href="#x86-sse-data-transfer-instructions">23.12.1.1. x86 SSE data transfer instructions</a></li>
<li><a href="#x86-sse-packed-arithmetic-instructions">23.12.1.2. x86 SSE packed arithmetic instructions</a></li>
<li><a href="#x86-sse-conversion-instructions">23.12.1.3. x86 SSE conversion instructions</a></li>
</ul>
</li>
<li><a href="#x86-sse2-instructions">23.12.2. x86 SSE2 instructions</a>
<ul class="sectlevel4">
<li><a href="#x86-paddq-instruction">23.12.2.1. x86 PADDQ instruction</a></li>
</ul>
</li>
<li><a href="#x86-fma">23.12.3. x86 fused multiply add (FMA)</a></li>
</ul>
</li>
<li><a href="#x86-system-instructions">23.13. x86 system instructions</a>
<ul class="sectlevel3">
<li><a href="#x86-rdtsc-instruction">23.13.1. x86 RDTSC instruction</a>
<ul class="sectlevel4">
<li><a href="#x86-rdtscp-instruction">23.13.1.1. x86 RDTSCP instruction</a></li>
<li><a href="#arm-pmccntr-register">23.13.1.2. ARM PMCCNTR register</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#x86-thread-synchronization-primitives">23.14. x86 thread synchronization primitives</a>
<ul class="sectlevel3">
<li><a href="#x86-lock-prefix">23.14.1. x86 LOCK prefix</a></li>
</ul>
</li>
<li><a href="#x86-assembly-bibliography">23.15. x86 assembly bibliography</a>
<ul class="sectlevel3">
<li><a href="#x86-official-bibliography">23.15.1. x86 official bibliography</a>
<ul class="sectlevel4">
<li><a href="#intel-manual">23.15.1.1. Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals</a>
<ul class="sectlevel5">
<li><a href="#intel-manual-1">23.15.1.1.1. Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a></li>
<li><a href="#intel-manual-2">23.15.1.1.2. Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 2</a></li>
<li><a href="#intel-manual-3">23.15.1.1.3. Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 3</a></li>
<li><a href="#intel-manual-4">23.15.1.1.4. Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 4</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#arm-userland-assembly">24. ARM userland assembly</a>
<ul class="sectlevel2">
<li><a href="#introduction-to-the-arm-architecture">24.1. Introduction to the ARM architecture</a>
<ul class="sectlevel3">
<li><a href="#armv8-vs-armv7-vs-aarch64-vs-aarch32">24.1.1. ARMv8 vs ARMv7 vs AArch64 vs AArch32</a>
<ul class="sectlevel4">
<li><a href="#aarch32">24.1.1.1. AArch32</a></li>
<li><a href="#aarch32-vs-aarch64">24.1.1.2. AArch32 vs AArch64</a></li>
</ul>
</li>
<li><a href="#free-arm-implementations">24.1.2. Free ARM implementations</a></li>
<li><a href="#arm-instruction-encodings">24.1.3. ARM instruction encodings</a>
<ul class="sectlevel4">
<li><a href="#arm-thumb-encoding">24.1.3.1. ARM Thumb encoding</a></li>
<li><a href="#arm-big-endian-mode">24.1.3.2. ARM big endian mode</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#arm-branch-instructions">24.2. ARM branch instructions</a>
<ul class="sectlevel3">
<li><a href="#arm-b-instruction">24.2.1. ARM B instruction</a></li>
<li><a href="#arm-beq-instruction">24.2.2. ARM BEQ instruction</a></li>
<li><a href="#arm-bl-instruction">24.2.3. ARM BL instruction</a>
<ul class="sectlevel4">
<li><a href="#arm-bx-instruction">24.2.3.1. ARM BX instruction</a></li>
<li><a href="#armv8-aarch64-ret-instruction">24.2.3.2. ARMv8 aarch64 ret instruction</a></li>
</ul>
</li>
<li><a href="#arm-cbz-instruction">24.2.4. ARM CBZ instruction</a></li>
<li><a href="#arm-conditional-execution">24.2.5. ARM conditional execution</a></li>
</ul>
</li>
<li><a href="#arm-load-and-store-instructions">24.3. ARM load and store instructions</a>
<ul class="sectlevel3">
<li><a href="#arm-ldr-instruction">24.3.1. ARM LDR instruction</a>
<ul class="sectlevel4">
<li><a href="#arm-ldr-pseudo-instruction">24.3.1.1. ARM LDR pseudo-instruction</a></li>
<li><a href="#arm-addressing-modes">24.3.1.2. ARM addressing modes</a>
<ul class="sectlevel5">
<li><a href="#arm-loop-over-array">24.3.1.2.1. ARM loop over array</a></li>
</ul>
</li>
<li><a href="#arm-ldrh-and-ldrb-instructions">24.3.1.3. ARM LDRH and LDRB instructions</a></li>
</ul>
</li>
<li><a href="#arm-str-instruction">24.3.2. ARM STR instruction</a>
<ul class="sectlevel4">
<li><a href="#armv8-aarch64-str-instruction">24.3.2.1. ARMv8 aarch64 STR instruction</a></li>
<li><a href="#armv8-aarch64-ldp-and-stp-instructions">24.3.2.2. ARMv8 aarch64 LDP and STP instructions</a>
<ul class="sectlevel5">
<li><a href="#armv8-aarch64-stack-alignment">24.3.2.2.1. ARMV8 aarch64 stack alignment</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#arm-ldmia-instruction">24.3.3. ARM LDMIA instruction</a></li>
</ul>
</li>
<li><a href="#arm-data-processing-instructions">24.4. ARM data processing instructions</a>
<ul class="sectlevel3">
<li><a href="#arm-cset-instruction">24.4.1. ARM CSET instruction</a></li>
<li><a href="#arm-bitwise-instructions">24.4.2. ARM bitwise instructions</a>
<ul class="sectlevel4">
<li><a href="#arm-bic-instruction">24.4.2.1. ARM BIC instruction</a></li>
<li><a href="#arm-ubfm-instruction">24.4.2.2. ARM UBFM instruction</a>
<ul class="sectlevel5">
<li><a href="#arm-ubfx-instruction">24.4.2.2.1. ARM UBFX instruction</a></li>
</ul>
</li>
<li><a href="#arm-bfm-instruction">24.4.2.3. ARM BFM instruction</a>
<ul class="sectlevel5">
<li><a href="#arm-bfi-instruction">24.4.2.3.1. ARM BFI instruction</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#arm-mov-instruction">24.4.3. ARM MOV instruction</a>
<ul class="sectlevel4">
<li><a href="#arm-movw-and-movt-instructions">24.4.3.1. ARM movw and movt instructions</a></li>
<li><a href="#armv8-aarch64-movk-instruction">24.4.3.2. ARMv8 aarch64 movk instruction</a></li>
<li><a href="#armv8-aarch64-movn-instruction">24.4.3.3. ARMv8 aarch64 movn instruction</a></li>
</ul>
</li>
<li><a href="#arm-data-processing-instruction-suffixes">24.4.4. ARM data processing instruction suffixes</a>
<ul class="sectlevel4">
<li><a href="#arm-shift-suffixes">24.4.4.1. ARM shift suffixes</a></li>
<li><a href="#arm-s-suffix">24.4.4.2. ARM S suffix</a></li>
</ul>
</li>
<li><a href="#arm-adr-instruction">24.4.5. ARM ADR instruction</a>
<ul class="sectlevel4">
<li><a href="#arm-adrl-instruction">24.4.5.1. ARM ADRL instruction</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#arm-miscellaneous-instructions">24.5. ARM miscellaneous instructions</a>
<ul class="sectlevel3">
<li><a href="#arm-nop-instruction">24.5.1. ARM NOP instruction</a></li>
<li><a href="#arm-udf-instruction">24.5.2. ARM UDF instruction</a></li>
</ul>
</li>
<li><a href="#arm-simd">24.6. ARM SIMD</a>
<ul class="sectlevel3">
<li><a href="#arm-vfp">24.6.1. ARM VFP</a>
<ul class="sectlevel4">
<li><a href="#arm-vfp-registers">24.6.1.1. ARM VFP registers</a></li>
<li><a href="#arm-vadd-instruction">24.6.1.2. ARM VADD instruction</a></li>
<li><a href="#arm-vcvt-instruction">24.6.1.3. ARM VCVT instruction</a>
<ul class="sectlevel5">
<li><a href="#arm-vcvtr-instruction">24.6.1.3.1. ARM VCVTR instruction</a></li>
<li><a href="#armv8-aarch32-vcvta-instruction">24.6.1.3.2. ARMv8 AArch32 VCVTA instruction</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#armv8-advanced-simd-and-floating-point-support">24.6.2. ARMv8 Advanced SIMD and floating-point support</a>
<ul class="sectlevel4">
<li><a href="#armv8-floating-point-availability">24.6.2.1. ARMv8 floating point availability</a></li>
<li><a href="#arm-neon">24.6.2.2. ARM NEON</a></li>
</ul>
</li>
<li><a href="#armv8-aarch64-floating-point-registers">24.6.3. ARMv8 AArch64 floating point registers</a>
<ul class="sectlevel4">
<li><a href="#armv8-aarch64-add-vector-instruction">24.6.3.1. ARMv8 aarch64 add vector instruction</a></li>
<li><a href="#armv8-aarch64-fadd-instruction">24.6.3.2. ARMv8 aarch64 FADD instruction</a>
<ul class="sectlevel5">
<li><a href="#arm-fadd-vs-vadd">24.6.3.2.1. ARM FADD vs VADD</a></li>
</ul>
</li>
<li><a href="#armv8-aarch64-ld2-instruction">24.6.3.3. ARMv8 aarch64 ld2 instruction</a></li>
</ul>
</li>
<li><a href="#arm-simd-bibliography">24.6.4. ARM SIMD bibliography</a></li>
<li><a href="#arm-sve">24.6.5. ARM SVE</a>
<ul class="sectlevel4">
<li><a href="#sve-bibliography">24.6.5.1. SVE bibliography</a>
<ul class="sectlevel5">
<li><a href="#sve-spec">24.6.5.1.1. SVE spec</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#armv8-architecture-extensions">24.7. ARMv8 architecture extensions</a>
<ul class="sectlevel3">
<li><a href="#armv8-1-architecture-extension">24.7.1. ARMv8.1 architecture extension</a>
<ul class="sectlevel4">
<li><a href="#arm-lse">24.7.1.1. ARM Large System Extensions (LSE)</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#arm-assembly-bibliography">24.8. ARM assembly bibliography</a>
<ul class="sectlevel3">
<li><a href="#arm-non-official-bibliography">24.8.1. ARM non-official bibliography</a></li>
<li><a href="#arm-official-bibliography">24.8.2. ARM official bibliography</a>
<ul class="sectlevel4">
<li><a href="#armarm7">24.8.2.1. ARMv7 architecture reference manual</a></li>
<li><a href="#armarm8">24.8.2.2. ARMv8 architecture reference manual</a></li>
<li><a href="#armarm8-db">24.8.2.3. ARMv8 architecture reference manual db</a></li>
<li><a href="#armv8-programmers-guide">24.8.2.4. Programmer&#8217;s Guide for ARMv8-A</a></li>
<li><a href="#arm-a64-instruction-set-architecture-future-architecture-technologies-in-the-a-architecture-profile-documentation">24.8.2.5. Arm A64 Instruction Set Architecture: Future Architecture Technologies in the A architecture profile Documentation</a></li>
<li><a href="#arm-processor-documentation">24.8.2.6. ARM processor documentation</a>
<ul class="sectlevel5">
<li><a href="#arm-cortex15-trm">24.8.2.6.1. ARM Cortex-A15 MPCore Processor Technical Reference Manual r4p0</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#elf">25. ELF</a></li>
<li><a href="#ieee-754">26. IEEE 754</a></li>
<li><a href="#baremetal">27. Baremetal</a>
<ul class="sectlevel2">
<li><a href="#baremetal-gdb-step-debug">27.1. Baremetal GDB step debug</a></li>
<li><a href="#baremetal-bootloaders">27.2. Baremetal bootloaders</a></li>
<li><a href="#semihosting">27.3. Semihosting</a>
<ul class="sectlevel3">
<li><a href="#gem5-semihosting">27.3.1. gem5 semihosting</a></li>
</ul>
</li>
<li><a href="#gem5-baremetal-carriage-return">27.4. gem5 baremetal carriage return</a></li>
<li><a href="#baremetal-host-packaged-toolchain">27.5. Baremetal host packaged toolchain</a></li>
<li><a href="#baremetal-cpp">27.6. Baremetal C++</a></li>
<li><a href="#gdb-builtin-cpu-simulator">27.7. GDB builtin CPU simulator</a>
<ul class="sectlevel3">
<li><a href="#gdb-builtin-cpu-simulator-userland">27.7.1. GDB builtin CPU simulator userland</a></li>
</ul>
</li>
<li><a href="#arm-baremetal">27.8. ARM baremetal</a>
<ul class="sectlevel3">
<li><a href="#arm-exception-levels">27.8.1. ARM exception levels</a>
<ul class="sectlevel4">
<li><a href="#arm-change-exception-level">27.8.1.1. ARM change exception level</a></li>
<li><a href="#arm-sp0-vs-spx">27.8.1.2. ARM SP0 vs SPx</a></li>
</ul>
</li>
<li><a href="#arm-svc-instruction">27.8.2. ARM SVC instruction</a>
<ul class="sectlevel4">
<li><a href="#armv8-exception-vector-table-format">27.8.2.1. ARMv8 exception vector table format</a></li>
<li><a href="#arm-esr-register">27.8.2.2. ARM ESR register</a></li>
<li><a href="#arm-elr-register">27.8.2.3. ARM ELR register</a></li>
</ul>
</li>
<li><a href="#arm-multicore">27.8.3. ARM multicore</a>
<ul class="sectlevel4">
<li><a href="#arm-wfe-and-sev-instructions">27.8.3.1. ARM WFE and SEV instructions</a></li>
<li><a href="#arm-psci">27.8.3.2. ARM PSCI</a></li>
<li><a href="#arm-dmb-instruction">27.8.3.3. ARM DMB instruction</a></li>
</ul>
</li>
<li><a href="#arm-timer">27.8.4. ARM timer</a></li>
<li><a href="#arm-gic">27.8.5. ARM GIC</a></li>
<li><a href="#arm-paging">27.8.6. ARM paging</a></li>
<li><a href="#arm-baremetal-bibliography">27.8.7. ARM baremetal bibliography</a>
<ul class="sectlevel4">
<li><a href="#nienfengyaoarmv8-bare-metal">27.8.7.1. NienfengYao/armv8-bare-metal</a></li>
<li><a href="#tukl-msdgem5-bare-metal">27.8.7.2. tukl-msd/gem5.bare-metal</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#how-we-got-some-baremetal-stuff-to-work">27.9. How we got some baremetal stuff to work</a>
<ul class="sectlevel3">
<li><a href="#find-the-uart-address">27.9.1. Find the UART address</a></li>
<li><a href="#aarch64-baremetal-neon-setup">27.9.2. aarch64 baremetal NEON setup</a></li>
</ul>
</li>
<li><a href="#baremetal-tests">27.10. Baremetal tests</a></li>
</ul>
</li>
<li><a href="#android">28. Android</a>
<ul class="sectlevel2">
<li><a href="#android-image-structure">28.1. Android image structure</a>
<ul class="sectlevel3">
<li><a href="#android-images-read-only">28.1.1. Android images read-only</a></li>
<li><a href="#android-data-partition">28.1.2. Android /data partition</a></li>
</ul>
</li>
<li><a href="#install-android-apps">28.2. Install Android apps</a></li>
<li><a href="#android-init">28.3. Android init</a></li>
</ul>
</li>
<li><a href="#benchmark-this-repo">29. Benchmark this repo</a>
<ul class="sectlevel2">
<li><a href="#continuous-integraion">29.1. Continuous integraion</a>
<ul class="sectlevel3">
<li><a href="#travis">29.1.1. Travis</a></li>
<li><a href="#circleci">29.1.2. CircleCI</a></li>
</ul>
</li>
<li><a href="#benchmark-this-repo-benchmarks">29.2. Benchmark this repo benchmarks</a>
<ul class="sectlevel3">
<li><a href="#benchmark-linux-kernel-boot">29.2.1. Benchmark Linux kernel boot</a>
<ul class="sectlevel4">
<li><a href="#gem5-arm-hpi-boot-takes-much-longer-than-aarch64">29.2.1.1. gem5 arm HPI boot takes much longer than aarch64</a></li>
<li><a href="#gem5-x86_64-derivo3cpu-boot-panics">29.2.1.2. gem5 x86_64 DerivO3CPU boot panics</a></li>
</ul>
</li>
<li><a href="#benchmark-builds">29.2.2. Benchmark builds</a>
<ul class="sectlevel4">
<li><a href="#find-which-buildroot-packages-are-making-the-build-slow-and-big">29.2.2.1. Find which Buildroot packages are making the build slow and big</a>
<ul class="sectlevel5">
<li><a href="#prebuilt-toolchain">29.2.2.1.1. Buildroot use prebuilt host toolchain</a></li>
</ul>
</li>
<li><a href="#benchmark-buildroot-build-baseline">29.2.2.2. Benchmark Buildroot build baseline</a></li>
<li><a href="#benchmark-gem5-build">29.2.2.3. Benchmark gem5 build</a>
<ul class="sectlevel5">
<li><a href="#benchmark-gem5-single-file-change-rebuild-time">29.2.2.3.1. Benchmark gem5 single file change rebuild time</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#benchmark-machines">29.3. Benchmark machines</a>
<ul class="sectlevel3">
<li><a href="#p51">29.3.1. P51</a></li>
</ul>
</li>
<li><a href="#benchmark-internets">29.4. Benchmark Internets</a>
<ul class="sectlevel3">
<li><a href="#38mbps-internet">29.4.1. 38Mbps internet</a></li>
</ul>
</li>
<li><a href="#benchmark-this-repo-bibliography">29.5. Benchmark this repo bibliography</a></li>
</ul>
</li>
<li><a href="#xephyr">30. Xephyr</a></li>
<li><a href="#about-this-repo">31. About this repo</a>
<ul class="sectlevel2">
<li><a href="#supported-hosts">31.1. Supported hosts</a></li>
<li><a href="#common-build-issues">31.2. Common build issues</a>
<ul class="sectlevel3">
<li><a href="#put-source-uris-in-sources">31.2.1. You must put some 'source' URIs in your sources.list</a></li>
<li><a href="#build-from-downloaded-source-zip-files">31.2.2. Build from downloaded source zip files</a></li>
</ul>
</li>
<li><a href="#run-command-after-boot">31.3. Run command after boot</a></li>
<li><a href="#default-command-line-arguments">31.4. Default command line arguments</a></li>
<li><a href="#documentation">31.5. Documentation</a>
<ul class="sectlevel3">
<li><a href="#documentation-verification">31.5.1. Documentation verification</a>
<ul class="sectlevel4">
<li><a href="#asciidoctor-extract-link-targets">31.5.1.1. asciidoctor/extract-link-targets</a></li>
<li><a href="#asciidoctor-extract-header-ids">31.5.1.2. asciidoctor/extract-header-ids</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#asciidoctor-link-target-up-rb">31.6. asciidoctor/link-target-up.rb</a>
<ul class="sectlevel3">
<li><a href="#github-pages">31.6.1. GitHub pages</a></li>
</ul>
</li>
<li><a href="#clean-the-build">31.7. Clean the build</a></li>
<li><a href="#ccache">31.8. ccache</a></li>
<li><a href="#rebuild-buildroot-while-running">31.9. Rebuild Buildroot while running</a></li>
<li><a href="#simultaneous-runs">31.10. Simultaneous runs</a></li>
<li><a href="#build-variants">31.11. Build variants</a>
<ul class="sectlevel3">
<li><a href="#linux-kernel-build-variants">31.11.1. Linux kernel build variants</a></li>
<li><a href="#qemu-build-variants">31.11.2. QEMU build variants</a></li>
<li><a href="#gem5-build-variants">31.11.3. gem5 build variants</a>
<ul class="sectlevel4">
<li><a href="#gem5-worktree">31.11.3.1. gem5 worktree</a></li>
<li><a href="#gem5-private-source-trees">31.11.3.2. gem5 private source trees</a></li>
</ul>
</li>
<li><a href="#buildroot-build-variants">31.11.4. Buildroot build variants</a></li>
</ul>
</li>
<li><a href="#directory-structure">31.12. Directory structure</a>
<ul class="sectlevel3">
<li><a href="#lkmc-directory">31.12.1. lkmc directory</a>
<ul class="sectlevel4">
<li><a href="#userland-objects-vs-header-only">31.12.1.1. Userland objects vs header-only</a></li>
</ul>
</li>
<li><a href="#buildroot_packages-directory">31.12.2. buildroot_packages directory</a>
<ul class="sectlevel4">
<li><a href="#kernel_modules-buildroot-package">31.12.2.1. kernel_modules buildroot package</a></li>
</ul>
</li>
<li><a href="#patches-directory">31.12.3. patches directory</a>
<ul class="sectlevel4">
<li><a href="#patches-global-directory">31.12.3.1. patches/global directory</a></li>
<li><a href="#patches-manual-directory">31.12.3.2. patches/manual directory</a></li>
</ul>
</li>
<li><a href="#rootfs_overlay">31.12.4. rootfs_overlay</a></li>
<li><a href="#lkmc-c">31.12.5. lkmc.c</a></li>
<li><a href="#rand_check-out">31.12.6. rand_check.out</a></li>
<li><a href="#lkmc_home">31.12.7. lkmc_home</a></li>
</ul>
</li>
<li><a href="#test-this-repo">31.13. Test this repo</a>
<ul class="sectlevel3">
<li><a href="#automated-tests">31.13.1. Automated tests</a>
<ul class="sectlevel4">
<li><a href="#test-arch-and-emulator-selection">31.13.1.1. Test arch and emulator selection</a></li>
<li><a href="#quit-on-fail">31.13.1.2. Quit on fail</a></li>
<li><a href="#test-userland-in-full-system">31.13.1.3. Test userland in full system</a></li>
<li><a href="#gdb-tests">31.13.1.4. GDB tests</a></li>
<li><a href="#magic-failure-string">31.13.1.5. Magic failure string</a></li>
</ul>
</li>
<li><a href="#non-automated-tests">31.13.2. Non-automated tests</a>
<ul class="sectlevel4">
<li><a href="#test-gdb-linux-kernel">31.13.2.1. Test GDB Linux kernel</a></li>
<li><a href="#test-the-internet">31.13.2.2. Test the Internet</a></li>
<li><a href="#cli-script-tests">31.13.2.3. CLI script tests</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#bisection">31.14. Bisection</a></li>
<li><a href="#path-properties">31.15. path_properties</a></li>
<li><a href="#update-a-forked-submodule">31.16. Update a forked submodule</a></li>
<li><a href="#release">31.17. Release</a>
<ul class="sectlevel3">
<li><a href="#release-procedure">31.17.1. Release procedure</a></li>
<li><a href="#release-zip">31.17.2. release-zip</a></li>
<li><a href="#release-upload">31.17.3. release-upload</a></li>
</ul>
</li>
<li><a href="#design-rationale">31.18. Design rationale</a>
<ul class="sectlevel3">
<li><a href="#design-goals">31.18.1. Design goals</a></li>
<li><a href="#setup-trade-offs">31.18.2. Setup trade-offs</a></li>
<li><a href="#resource-tradeoff-guidelines">31.18.3. Resource tradeoff guidelines</a></li>
<li><a href="#linux-distro-choice">31.18.4. Linux distro choice</a></li>
</ul>
</li>
<li><a href="#soft-topics">31.19. Soft topics</a>
<ul class="sectlevel3">
<li><a href="#fairy-tale">31.19.1. Fairy tale</a></li>
<li><a href="#should-you-waste-your-life-with-systems-programming">31.19.2. Should you waste your life with systems programming?</a></li>
</ul>
</li>
<li><a href="#bibliography">31.20. Bibliography</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="getting-started"><a class="anchor" href="#getting-started"></a><a class="link" href="#getting-started">1. Getting started</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Each child section describes a possible different setup for this repo.</p>
</div>
<div class="paragraph">
<p>If you don&#8217;t know which one to go for, start with <a href="#qemu-buildroot-setup-getting-started">QEMU Buildroot setup getting started</a>.</p>
</div>
<div class="paragraph">
<p>Design goals of this project are documented at: <a href="#design-goals">Section 31.18.1, &#8220;Design goals&#8221;</a>.</p>
</div>
<div class="sect2">
<h3 id="qemu-buildroot-setup"><a class="anchor" href="#qemu-buildroot-setup"></a><a class="link" href="#qemu-buildroot-setup">1.1. QEMU Buildroot setup</a></h3>
<div class="sect3">
<h4 id="qemu-buildroot-setup-getting-started"><a class="anchor" href="#qemu-buildroot-setup-getting-started"></a><a class="link" href="#qemu-buildroot-setup-getting-started">1.1.1. QEMU Buildroot setup getting started</a></h4>
<div class="paragraph">
<p>This setup has been mostly tested on Ubuntu. For other host operating systems see: <a href="#supported-hosts">Section 31.1, &#8220;Supported hosts&#8221;</a>. For greater stability, consider using the <a href="#release-procedure">latest release</a> instead of master: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/releases" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/releases</a></p>
</div>
<div class="paragraph">
<p>Reserve 12Gb of disk and run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git clone https://github.com/cirosantilli/linux-kernel-module-cheat
cd linux-kernel-module-cheat
./build --download-dependencies qemu-buildroot
./run</pre>
</div>
</div>
<div class="paragraph">
<p>You don&#8217;t need to clone recursively even though we have <code>.git</code> submodules: <code>download-dependencies</code> fetches just the submodules that you need for this build to save time.</p>
</div>
<div class="paragraph">
<p>If something goes wrong, see: <a href="#common-build-issues">Section 31.2, &#8220;Common build issues&#8221;</a> and use our issue tracker: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/issues" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/issues</a></p>
</div>
<div class="paragraph">
<p>The initial build will take a while (30 minutes to 2 hours) to clone and build, see <a href="#benchmark-builds">Benchmark builds</a> for more details.</p>
</div>
<div class="paragraph">
<p>If you don&#8217;t want to wait, you could also try the following faster but much more limited methods:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#prebuilt">Prebuilt setup</a></p>
</li>
<li>
<p><a href="#host">Host kernel module setup</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>but you will soon find that they are simply not enough if you anywhere near serious about systems programming.</p>
</div>
<div class="paragraph">
<p>After <code>./run</code>, QEMU opens up leaving you in the <a href="#lkmc_home"><code>/lkmc/</code> directory</a>, and you can start playing with the kernel modules inside the simulated system:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod hello.ko
insmod hello2.ko
rmmod hello
rmmod hello2</pre>
</div>
</div>
<div class="paragraph">
<p>This should print to the screen:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>hello init
hello2 init
hello cleanup
hello2 cleanup</pre>
</div>
</div>
<div class="paragraph">
<p>which are <code>printk</code> messages from <code>init</code> and <code>cleanup</code> methods of those modules.</p>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/hello.c">kernel_modules/hello.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/hello2.c">kernel_modules/hello2.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Quit QEMU with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Ctrl-A X</pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#quit-qemu-from-text-mode">Section 13.1.1, &#8220;Quit QEMU from text mode&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>All available modules can be found in the <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules">kernel_modules</a> directory.</p>
</div>
<div class="paragraph">
<p>It is super easy to build for different <a href="#cpu-architecture">CPU architectures</a>, just use the <code>--arch</code> option:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --arch aarch64 --download-dependencies qemu-buildroot
./run --arch aarch64</pre>
</div>
</div>
<div class="paragraph">
<p>To avoid typing <code>--arch aarch64</code> many times, you can set the default arch as explained at: <a href="#default-command-line-arguments">Section 31.4, &#8220;Default command line arguments&#8221;</a></p>
</div>
<div class="paragraph">
<p>I now urge you to read the following sections which contain widely applicable information:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#run-command-after-boot">Run command after boot</a></p>
</li>
<li>
<p><a href="#clean-the-build">Clean the build</a></p>
</li>
<li>
<p><a href="#build-the-documentation">Build the documentation</a></p>
</li>
<li>
<p>Linux kernel</p>
<div class="ulist">
<ul>
<li>
<p><a href="#printk">printk</a></p>
</li>
<li>
<p><a href="#kernel-command-line-parameters">Kernel command line parameters</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Once you use <a href="#gdb">GDB step debug</a> and <a href="#tmux">tmux</a>, your terminal will look a bit like this:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[ 1.451857] input: AT Translated Set 2 keyboard as /devices/platform/i8042/s1│loading @0xffffffffc0000000: ../kernel_modules-1.0//timer.ko
[ 1.454310] ledtrig-cpu: registered to indicate activity on CPUs │(gdb) b lkmc_timer_callback
[ 1.455621] usbcore: registered new interface driver usbhid │Breakpoint 1 at 0xffffffffc0000000: file /home/ciro/bak/git/linux-kernel-module
[ 1.455811] usbhid: USB HID core driver │-cheat/out/x86_64/buildroot/build/kernel_modules-1.0/./timer.c, line 28.
[ 1.462044] NET: Registered protocol family 10 │(gdb) c
[ 1.467911] Segment Routing with IPv6 │Continuing.
[ 1.468407] sit: IPv6, IPv4 and MPLS over IPv4 tunneling driver │
[ 1.470859] NET: Registered protocol family 17 │Breakpoint 1, lkmc_timer_callback (data=0xffffffffc0002000 &lt;mytimer&gt;)
[ 1.472017] 9pnet: Installing 9P2000 support │ at /linux-kernel-module-cheat//out/x86_64/buildroot/build/
[ 1.475461] sched_clock: Marking stable (1473574872, 0)-&gt;(1554017593, -80442)│kernel_modules-1.0/./timer.c:28
[ 1.479419] ALSA device list: │28 {
[ 1.479567] No soundcards found. │(gdb) c
[ 1.619187] ata2.00: ATAPI: QEMU DVD-ROM, 2.5+, max UDMA/100 │Continuing.
[ 1.622954] ata2.00: configured for MWDMA2 │
[ 1.644048] scsi 1:0:0:0: CD-ROM QEMU QEMU DVD-ROM 2.5+ P5│Breakpoint 1, lkmc_timer_callback (data=0xffffffffc0002000 &lt;mytimer&gt;)
[ 1.741966] tsc: Refined TSC clocksource calibration: 2904.010 MHz │ at /linux-kernel-module-cheat//out/x86_64/buildroot/build/
[ 1.742796] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x29dc0f4s│kernel_modules-1.0/./timer.c:28
[ 1.743648] clocksource: Switched to clocksource tsc │28 {
[ 2.072945] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8043│(gdb) bt
[ 2.078641] EXT4-fs (vda): couldn't mount as ext3 due to feature incompatibis│#0 lkmc_timer_callback (data=0xffffffffc0002000 &lt;mytimer&gt;)
[ 2.080350] EXT4-fs (vda): mounting ext2 file system using the ext4 subsystem│ at /linux-kernel-module-cheat//out/x86_64/buildroot/build/
[ 2.088978] EXT4-fs (vda): mounted filesystem without journal. Opts: (null) │kernel_modules-1.0/./timer.c:28
[ 2.089872] VFS: Mounted root (ext2 filesystem) readonly on device 254:0. │#1 0xffffffff810ab494 in call_timer_fn (timer=0xffffffffc0002000 &lt;mytimer&gt;,
[ 2.097168] devtmpfs: mounted │ fn=0xffffffffc0000000 &lt;lkmc_timer_callback&gt;) at kernel/time/timer.c:1326
[ 2.126472] Freeing unused kernel memory: 1264K │#2 0xffffffff810ab71f in expire_timers (head=&lt;optimized out&gt;,
[ 2.126706] Write protecting the kernel read-only data: 16384k │ base=&lt;optimized out&gt;) at kernel/time/timer.c:1363
[ 2.129388] Freeing unused kernel memory: 2024K │#3 __run_timers (base=&lt;optimized out&gt;) at kernel/time/timer.c:1666
[ 2.139370] Freeing unused kernel memory: 1284K │#4 run_timer_softirq (h=&lt;optimized out&gt;) at kernel/time/timer.c:1692
[ 2.246231] EXT4-fs (vda): warning: mounting unchecked fs, running e2fsck isd│#5 0xffffffff81a000cc in __do_softirq () at kernel/softirq.c:285
[ 2.259574] EXT4-fs (vda): re-mounted. Opts: block_validity,barrier,user_xatr│#6 0xffffffff810577cc in invoke_softirq () at kernel/softirq.c:365
hello S98 │#7 irq_exit () at kernel/softirq.c:405
│#8 0xffffffff818021ba in exiting_irq () at ./arch/x86/include/asm/apic.h:541
Apr 15 23:59:23 login[49]: root login on 'console' │#9 smp_apic_timer_interrupt (regs=&lt;optimized out&gt;)
hello /root/.profile │ at arch/x86/kernel/apic/apic.c:1052
# insmod /timer.ko │#10 0xffffffff8180190f in apic_timer_interrupt ()
[ 6.791945] timer: loading out-of-tree module taints kernel. │ at arch/x86/entry/entry_64.S:857
# [ 7.821621] 4294894248 │#11 0xffffffff82003df8 in init_thread_union ()
[ 8.851385] 4294894504 │#12 0x0000000000000000 in ?? ()
│(gdb)</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="how-to-hack-stuff"><a class="anchor" href="#how-to-hack-stuff"></a><a class="link" href="#how-to-hack-stuff">1.1.2. How to hack stuff</a></h4>
<div class="paragraph">
<p>Besides a seamless <a href="#qemu-buildroot-setup-getting-started">initial build</a>, this project also aims to make it effortless to modify and rebuild several major components of the system, to serve as an awesome development setup.</p>
</div>
<div class="sect4">
<h5 id="your-first-linux-kernel-hack"><a class="anchor" href="#your-first-linux-kernel-hack"></a><a class="link" href="#your-first-linux-kernel-hack">1.1.2.1. Your first Linux kernel hack</a></h5>
<div class="paragraph">
<p>Let&#8217;s hack up the <a href="#linux-kernel-entry-point">Linux kernel entry point</a>, which is an easy place to start.</p>
</div>
<div class="paragraph">
<p>Open the file:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vim submodules/linux/init/main.c</pre>
</div>
</div>
<div class="paragraph">
<p>and find the <code>start_kernel</code> function, then add there a:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>pr_info("I'VE HACKED THE LINUX KERNEL!!!");</pre>
</div>
</div>
<div class="paragraph">
<p>Then rebuild the Linux kernel, quit QEMU and reboot the modified kernel:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux
./run</pre>
</div>
</div>
<div class="paragraph">
<p>and, surely enough, your message has appeared at the beginning of the boot:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;6&gt;[ 0.000000] I'VE HACKED THE LINUX KERNEL!!!</pre>
</div>
</div>
<div class="paragraph">
<p>So you are now officially a Linux kernel hacker, way to go!</p>
</div>
<div class="paragraph">
<p>We could have used just <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build">build</a> to rebuild the kernel as in the <a href="#qemu-buildroot-setup-getting-started">initial build</a> instead of <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-linux">build-linux</a>, but building just the required individual components is preferred during development:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>saves a few seconds from parsing Make scripts and reading timestamps</p>
</li>
<li>
<p>makes it easier to understand what is being done in more detail</p>
</li>
<li>
<p>allows passing more specific options to customize the build</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build">build</a> script is just a lightweight wrapper that calls the smaller build scripts, and you can see what <code>./build</code> does with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --dry-run</pre>
</div>
</div>
<div class="paragraph">
<p>When you reach difficulties, QEMU makes it possible to easily GDB step debug the Linux kernel source code, see: <a href="#gdb">Section 2, &#8220;GDB step debug&#8221;</a>.</p>
</div>
</div>
<div class="sect4">
<h5 id="your-first-kernel-module-hack"><a class="anchor" href="#your-first-kernel-module-hack"></a><a class="link" href="#your-first-kernel-module-hack">1.1.2.2. Your first kernel module hack</a></h5>
<div class="paragraph">
<p>Edit <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/hello.c">kernel_modules/hello.c</a> to contain:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>pr_info("hello init hacked\n");</pre>
</div>
</div>
<div class="paragraph">
<p>and rebuild with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-modules</pre>
</div>
</div>
<div class="paragraph">
<p>Now there are two ways to test it out: the fast way, and the safe way.</p>
</div>
<div class="paragraph">
<p>The fast way is, without quitting or rebooting QEMU, just directly re-insert the module with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod /mnt/9p/out_rootfs_overlay/lkmc/hello.ko</pre>
</div>
</div>
<div class="paragraph">
<p>and the new <code>pr_info</code> message should now show on the terminal at the end of the boot.</p>
</div>
<div class="paragraph">
<p>This works because we have a <a href="#9p">9P</a> mount there setup by default, which mounts the host directory that contains the build outputs on the guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ls "$(./getvar out_rootfs_overlay_dir)"</pre>
</div>
</div>
<div class="paragraph">
<p>The fast method is slightly risky because your previously insmodded buggy kernel module attempt might have corrupted the kernel memory, which could affect future runs.</p>
</div>
<div class="paragraph">
<p>Such failures are however unlikely, and you should be fine if you don&#8217;t see anything weird happening.</p>
</div>
<div class="paragraph">
<p>The safe way, is to fist <a href="#rebuild-buildroot-while-running">quit QEMU</a>, rebuild the modules, put them in the root filesystem, and then reboot:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-modules
./build-buildroot
./run --eval-after 'insmod hello.ko'</pre>
</div>
</div>
<div class="paragraph">
<p><code>./build-buildroot</code> is required after <code>./build-modules</code> because it re-generates the root filesystem with the modules that we compiled at <code>./build-modules</code>.</p>
</div>
<div class="paragraph">
<p>You can see that <code>./build</code> does that as well, by running:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --dry-run</pre>
</div>
</div>
<div class="paragraph">
<p><code>--eval-after</code> is optional: you could just type <code>insmod hello.ko</code> in the terminal, but this makes it run automatically at the end of boot, and then drops you into a shell.</p>
</div>
<div class="paragraph">
<p>If the guest and host are the same arch, typically x86_64, you can speed up boot further with <a href="#kvm">KVM</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kvm</pre>
</div>
</div>
<div class="paragraph">
<p>All of this put together makes the safe procedure acceptably fast for regular development as well.</p>
</div>
<div class="paragraph">
<p>It is also easy to GDB step debug kernel modules with our setup, see: <a href="#gdb-step-debug-kernel-module">Section 2.4, &#8220;GDB step debug kernel module&#8221;</a>.</p>
</div>
</div>
<div class="sect4">
<h5 id="your-first-qemu-hack"><a class="anchor" href="#your-first-qemu-hack"></a><a class="link" href="#your-first-qemu-hack">1.1.2.3. Your first QEMU hack</a></h5>
<div class="paragraph">
<p>Not satisfied with mere software? OK then, let&#8217;s hack up the QEMU x86 CPU identification:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vim submodules/qemu/target/i386/cpu.c</pre>
</div>
</div>
<div class="paragraph">
<p>and modify:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>.model_id = "QEMU Virtual CPU version " QEMU_HW_VERSION,</pre>
</div>
</div>
<div class="paragraph">
<p>to contain:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>.model_id = "QEMU Virtual CPU version HACKED " QEMU_HW_VERSION,</pre>
</div>
</div>
<div class="paragraph">
<p>then as usual rebuild and re-run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-qemu
./run --eval-after 'grep "model name" /proc/cpuinfo'</pre>
</div>
</div>
<div class="paragraph">
<p>and once again, there is your message: QEMU communicated it to the Linux kernel, which printed it out.</p>
</div>
<div class="paragraph">
<p>You have now gone from newb to hardware hacker in a mere 15 minutes, your rate of progress is truly astounding!!!</p>
</div>
<div class="paragraph">
<p>Seriously though, if you want to be a real hardware hacker, it just can&#8217;t be done with open source tools as of 2018. The root obstacle is that:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://en.wikipedia.org/wiki/Semiconductor_fabrication_plant">Silicon fabs</a> don&#8217;t publish reveal their <a href="https://en.wikipedia.org/wiki/Design_rule_checking">design rules</a></p>
</li>
<li>
<p>which implies that there are no decent <a href="https://en.wikipedia.org/wiki/Standard_cell">standard cell libraries</a>. See also: <a href="https://www.quora.com/Are-there-good-open-source-standard-cell-libraries-to-learn-IC-synthesis-with-EDA-tools/answer/Ciro-Santilli" class="bare">https://www.quora.com/Are-there-good-open-source-standard-cell-libraries-to-learn-IC-synthesis-with-EDA-tools/answer/Ciro-Santilli</a></p>
</li>
<li>
<p>which implies that people can&#8217;t develop open source <a href="https://en.wikipedia.org/wiki/Electronic_design_automation">EDA tools</a></p>
</li>
<li>
<p>which implies that you can&#8217;t get decent <a href="https://community.cadence.com/cadence_blogs_8/b/di/posts/hls-ppa-is-it-all-you-need-to-know">power, performance and area</a> estimates</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The only thing you can do with open source is purely functional designs with <a href="https://en.wikipedia.org/wiki/Verilator">Verilator</a>, but you will never know if it can be actually produced and how efficient it can be.</p>
</div>
<div class="paragraph">
<p>If you really want to develop semiconductors, your only choice is to join an university or a semiconductor company that has the EDA licenses.</p>
</div>
<div class="paragraph">
<p>See also: <a href="#should-you-waste-your-life-with-systems-programming">Section 31.19.2, &#8220;Should you waste your life with systems programming?&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>While hacking QEMU, you will likely want to GDB step its source. That is trivial since QEMU is just another userland program like any other, but our setup has a shortcut to make it even more convenient, see: <a href="#debug-the-emulator">Section 18.7, &#8220;Debug the emulator&#8221;</a>.</p>
</div>
</div>
<div class="sect4">
<h5 id="your-first-glibc-hack"><a class="anchor" href="#your-first-glibc-hack"></a><a class="link" href="#your-first-glibc-hack">1.1.2.4. Your first glibc hack</a></h5>
<div class="paragraph">
<p>We use <a href="#libc-choice">glibc as our default libc now</a>, and it is tracked as an unmodified submodule at <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/submodules/glibc">submodules/glibc</a>, at the exact same version that Buildroot has it, which can be found at: <a href="https://github.com/buildroot/buildroot/blob/2018.05/package/glibc/glibc.mk#L13">package/glibc/glibc.mk</a>. Buildroot 2018.05 applies no patches.</p>
</div>
<div class="paragraph">
<p>Let&#8217;s hack up the <code>puts</code> function:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot -- glibc-reconfigure</pre>
</div>
</div>
<div class="paragraph">
<p>with the patch:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>diff --git a/libio/ioputs.c b/libio/ioputs.c
index 706b20b492..23185948f3 100644
--- a/libio/ioputs.c
+++ b/libio/ioputs.c
@@ -38,8 +38,9 @@ _IO_puts (const char *str)
if ((_IO_vtable_offset (_IO_stdout) != 0
|| _IO_fwide (_IO_stdout, -1) == -1)
&amp;&amp; _IO_sputn (_IO_stdout, str, len) == len
+ &amp;&amp; _IO_sputn (_IO_stdout, " hacked", 7) == 7
&amp;&amp; _IO_putc_unlocked ('\n', _IO_stdout) != EOF)
- result = MIN (INT_MAX, len + 1);
+ result = MIN (INT_MAX, len + 1 + 7);
_IO_release_lock (_IO_stdout);
return result;</pre>
</div>
</div>
<div class="paragraph">
<p>And then:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after './c/hello.out'</pre>
</div>
</div>
<div class="paragraph">
<p>outputs:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>hello hacked</pre>
</div>
</div>
<div class="paragraph">
<p>Lol!</p>
</div>
<div class="paragraph">
<p>We can also test our hacked glibc on <a href="#user-mode-simulation">User mode simulation</a> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --userland userland/c/hello.c</pre>
</div>
</div>
<div class="paragraph">
<p>I just noticed that this is actually a good way to develop glibc for other archs.</p>
</div>
<div class="paragraph">
<p>In this example, we got away without recompiling the userland program because we made a change that did not affect the glibc ABI, see this answer for an introduction to ABI stability: <a href="https://stackoverflow.com/questions/2171177/what-is-an-application-binary-interface-abi/54967743#54967743" class="bare">https://stackoverflow.com/questions/2171177/what-is-an-application-binary-interface-abi/54967743#54967743</a></p>
</div>
<div class="paragraph">
<p>Note that for arch agnostic features that don&#8217;t rely on bleeding kernel changes that you host doesn&#8217;t yet have, you can develop glibc natively as explained at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/10412684/how-to-compile-my-own-glibc-c-standard-library-from-source-and-use-it/52454710#52454710" class="bare">https://stackoverflow.com/questions/10412684/how-to-compile-my-own-glibc-c-standard-library-from-source-and-use-it/52454710#52454710</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/847179/multiple-glibc-libraries-on-a-single-host/52454603#52454603" class="bare">https://stackoverflow.com/questions/847179/multiple-glibc-libraries-on-a-single-host/52454603#52454603</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/2856438/how-can-i-link-to-a-specific-glibc-version/52550158#52550158" class="bare">https://stackoverflow.com/questions/2856438/how-can-i-link-to-a-specific-glibc-version/52550158#52550158</a> more focus on symbol versioning, but no one knows how to do it, so I answered</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Tested on a30ed0f047523ff2368d421ee2cce0800682c44e + 1.</p>
</div>
</div>
<div class="sect4">
<h5 id="your-first-binutils-hack"><a class="anchor" href="#your-first-binutils-hack"></a><a class="link" href="#your-first-binutils-hack">1.1.2.5. Your first Binutils hack</a></h5>
<div class="paragraph">
<p>Have you ever felt that a single <code>inc</code> instruction was not enough? Really? Me too!</p>
</div>
<div class="paragraph">
<p>So let&#8217;s hack the <a href="#gnu-gas-assembler">GNU GAS assembler</a>, which is part of <a href="https://en.wikipedia.org/wiki/GNU_Binutils">GNU Binutils</a>, to add a new shiny version of <code>inc</code> called&#8230;&#8203; <code>myinc</code>!</p>
</div>
<div class="paragraph">
<p>GCC uses GNU GAS as its backend, so we will test out new mnemonic with an <a href="#gcc-inline-assembly">GCC inline assembly</a> test program: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/binutils_hack.c">userland/arch/x86_64/binutils_hack.c</a>, which is just a copy of <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/binutils_nohack.c">userland/arch/x86_64/binutils_nohack.c</a> but with <code>myinc</code> instead of <code>inc</code>.</p>
</div>
<div class="paragraph">
<p>The inline assembly is disabled with an <code>#ifdef</code>, so first modify the source to enable that.</p>
</div>
<div class="paragraph">
<p>Then, try to build userland:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland</pre>
</div>
</div>
<div class="paragraph">
<p>and watch it fail with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>binutils_hack.c:8: Error: no such instruction: `myinc %rax'</pre>
</div>
</div>
<div class="paragraph">
<p>Now, edit the file</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vim submodules/binutils-gdb/opcodes/i386-tbl.h</pre>
</div>
</div>
<div class="paragraph">
<p>and add a copy of the <code>"inc"</code> instruction just next to it, but with the new name <code>"myinc"</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>diff --git a/opcodes/i386-tbl.h b/opcodes/i386-tbl.h
index af583ce578..3cc341f303 100644
--- a/opcodes/i386-tbl.h
+++ b/opcodes/i386-tbl.h
@@ -1502,6 +1502,19 @@ const insn_template i386_optab[] =
{ { { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 } } } },
+ { "myinc", 1, 0xfe, 0x0, 1,
+ { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
+ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0 },
+ { { { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
+ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 } } } },
{ "sub", 2, 0x28, None, 1,
{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,</pre>
</div>
</div>
<div class="paragraph">
<p>Finally, rebuild Binutils, userland and test our program with <a href="#user-mode-simulation">User mode simulation</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot -- host-binutils-rebuild
./build-userland --static
./run --static --userland userland/arch/x86_64/binutils_hack.c</pre>
</div>
</div>
<div class="paragraph">
<p>and we se that <code>myinc</code> worked since the assert did not fail!</p>
</div>
<div class="paragraph">
<p>Tested on b60784d59bee993bf0de5cde6c6380dd69420dda + 1.</p>
</div>
</div>
<div class="sect4">
<h5 id="your-first-gcc-hack"><a class="anchor" href="#your-first-gcc-hack"></a><a class="link" href="#your-first-gcc-hack">1.1.2.6. Your first GCC hack</a></h5>
<div class="paragraph">
<p>OK, now time to hack GCC.</p>
</div>
<div class="paragraph">
<p>For convenience, let&#8217;s use the <a href="#user-mode-simulation">User mode simulation</a>.</p>
</div>
<div class="paragraph">
<p>If we run the program <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/gcc_hack.c">userland/c/gcc_hack.c</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland --static
./run --static --userland userland/c/gcc_hack.c</pre>
</div>
</div>
<div class="paragraph">
<p>it produces the normal boring output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>i = 2
j = 0</pre>
</div>
</div>
<div class="paragraph">
<p>So how about we swap <code>++</code> and <code>--</code> to make things more fun?</p>
</div>
<div class="paragraph">
<p>Open the file:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vim submodules/gcc/gcc/c/c-parser.c</pre>
</div>
</div>
<div class="paragraph">
<p>and find the function <code>c_parser_postfix_expression_after_primary</code>.</p>
</div>
<div class="paragraph">
<p>In that function, swap <code>case CPP_PLUS_PLUS</code> and <code>case CPP_MINUS_MINUS</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index 101afb8e35f..89535d1759a 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -8529,7 +8529,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser,
expr.original_type = DECL_BIT_FIELD_TYPE (field);
}
break;
- case CPP_PLUS_PLUS:
+ case CPP_MINUS_MINUS:
/* Postincrement. */
start = expr.get_start ();
finish = c_parser_peek_token (parser)-&gt;get_finish ();
@@ -8548,7 +8548,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser,
expr.original_code = ERROR_MARK;
expr.original_type = NULL;
break;
- case CPP_MINUS_MINUS:
+ case CPP_PLUS_PLUS:
/* Postdecrement. */
start = expr.get_start ();
finish = c_parser_peek_token (parser)-&gt;get_finish ();</pre>
</div>
</div>
<div class="paragraph">
<p>Now rebuild GCC, the program and re-run it:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot -- host-gcc-final-rebuild
./build-userland --static
./run --static --userland userland/c/gcc_hack.c</pre>
</div>
</div>
<div class="paragraph">
<p>and the new ouptut is now:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>i = 2
j = 0</pre>
</div>
</div>
<div class="paragraph">
<p>We need to use the ugly <code>-final</code> thing because GCC has to packages in Buildroot, <code>-initial</code> and <code>-final</code>: <a href="https://stackoverflow.com/questions/54992977/how-to-select-an-override-srcdir-source-for-gcc-when-building-buildroot" class="bare">https://stackoverflow.com/questions/54992977/how-to-select-an-override-srcdir-source-for-gcc-when-building-buildroot</a> No one is able to example precisely with a minimal example why this is required:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/39883865/why-multiple-passes-for-building-linux-from-scratch-lfs" class="bare">https://stackoverflow.com/questions/39883865/why-multiple-passes-for-building-linux-from-scratch-lfs</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/27457835/why-do-cross-compilers-have-a-two-stage-compilation" class="bare">https://stackoverflow.com/questions/27457835/why-do-cross-compilers-have-a-two-stage-compilation</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect3">
<h4 id="about-the-qemu-buildroot-setup"><a class="anchor" href="#about-the-qemu-buildroot-setup"></a><a class="link" href="#about-the-qemu-buildroot-setup">1.1.3. About the QEMU Buildroot setup</a></h4>
<div class="paragraph">
<p>This is our reference setup, and the best supported one, use it unless you have good reason not to.</p>
</div>
<div class="paragraph">
<p>It was historically the first one we did, and all sections have been tested with this setup unless explicitly noted.</p>
</div>
<div class="paragraph">
<p>Read the following sections for further introductory material:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#introduction-to-qemu">Introduction to QEMU</a></p>
</li>
<li>
<p><a href="#introduction-to-buildroot">Introduction to Buildroot</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-buildroot-setup"><a class="anchor" href="#gem5-buildroot-setup"></a><a class="link" href="#gem5-buildroot-setup">1.2. gem5 Buildroot setup</a></h3>
<div class="sect3">
<h4 id="about-the-gem5-buildroot-setup"><a class="anchor" href="#about-the-gem5-buildroot-setup"></a><a class="link" href="#about-the-gem5-buildroot-setup">1.2.1. About the gem5 Buildroot setup</a></h4>
<div class="paragraph">
<p>This setup is like the <a href="#qemu-buildroot-setup">QEMU Buildroot setup</a>, but it uses <a href="http://gem5.org/">gem5</a> instead of QEMU as a system simulator.</p>
</div>
<div class="paragraph">
<p>QEMU tries to run as fast as possible and give correct results at the end, but it does not tell us how many CPU cycles it takes to do something, just the number of instructions it ran. This kind of simulation is known as functional simulation.</p>
</div>
<div class="paragraph">
<p>The number of instructions executed is a very poor estimator of performance because in modern computers, a lot of time is spent waiting for memory requests rather than the instructions themselves.</p>
</div>
<div class="paragraph">
<p>gem5 on the other hand, can simulate the system in more detail than QEMU, including:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>simplified CPU pipeline</p>
</li>
<li>
<p>caches</p>
</li>
<li>
<p>DRAM timing</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>and can therefore be used to estimate system performance, see: <a href="#gem5-run-benchmark">Section 19.2, &#8220;gem5 run benchmark&#8221;</a> for an example.</p>
</div>
<div class="paragraph">
<p>The downside of gem5 much slower than QEMU because of the greater simulation detail.</p>
</div>
<div class="paragraph">
<p>See <a href="#gem5-vs-qemu">gem5 vs QEMU</a> for a more thorough comparison.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-buildroot-setup-getting-started"><a class="anchor" href="#gem5-buildroot-setup-getting-started"></a><a class="link" href="#gem5-buildroot-setup-getting-started">1.2.2. gem5 Buildroot setup getting started</a></h4>
<div class="paragraph">
<p>For the most part, if you just add the <code>--emulator gem5</code> option or <code>*-gem5</code> suffix to all commands and everything should magically work.</p>
</div>
<div class="paragraph">
<p>If you haven&#8217;t built Buildroot yet for <a href="#qemu-buildroot-setup">QEMU Buildroot setup</a>, you can build from the beginning with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --download-dependencies gem5-buildroot
./run --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>If you have already built previously, don&#8217;t be afraid: gem5 and QEMU use almost the same root filesystem and kernel, so <code>./build</code> will be fast.</p>
</div>
<div class="paragraph">
<p>Remember that the gem5 boot is <a href="#benchmark-linux-kernel-boot">considerably slower</a> than QEMU since the simulation is more detailed.</p>
</div>
<div class="paragraph">
<p>To get a terminal, either open a new shell and run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gem5-shell</pre>
</div>
</div>
<div class="paragraph">
<p>You can quit the shell without killing gem5 by typing tilde followed by a period:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>~.</pre>
</div>
</div>
<div class="paragraph">
<p>If you are inside <a href="#tmux">tmux</a>, which I highly recommend, you can both run gem5 stdout and open the guest terminal on a split window with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --emulator gem5 --tmux</pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#tmux-gem5">Section 2.3.1, &#8220;tmux gem5&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>At the end of boot, it might not be very clear that you have the shell since some <a href="#printk">printk</a> messages may appear in front of the prompt like this:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># &lt;6&gt;[ 1.215329] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x1cd486fa865, max_idle_ns: 440795259574 ns
&lt;6&gt;[ 1.215351] clocksource: Switched to clocksource tsc</pre>
</div>
</div>
<div class="paragraph">
<p>but if you look closely, the <code>PS1</code> prompt marker <code>#</code> is there already, just hit enter and a clear prompt line will appear.</p>
</div>
<div class="paragraph">
<p>If you forgot to open the shell and gem5 exit, you can inspect the terminal output post-mortem at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>less "$(./getvar --emulator gem5 m5out_dir)/system.pc.com_1.device"</pre>
</div>
</div>
<div class="paragraph">
<p>More gem5 information is present at: <a href="#gem5">Section 19, &#8220;gem5&#8221;</a></p>
</div>
<div class="paragraph">
<p>Good next steps are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#gem5-run-benchmark">gem5 run benchmark</a></p>
</li>
<li>
<p><a href="#m5out-directory">m5out directory</a></p>
</li>
<li>
<p><a href="#m5ops">m5ops</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="docker"><a class="anchor" href="#docker"></a><a class="link" href="#docker">1.3. Docker host setup</a></h3>
<div class="paragraph">
<p>This repository has been tested inside clean <a href="https://en.wikipedia.org/wiki/Docker_(software)">Docker</a> containers.</p>
</div>
<div class="paragraph">
<p>This is a good option if you are on a Linux host, but the native setup failed due to your weird host distribution, and you have better things to do with your life than to debug it. See also: <a href="#supported-hosts">Section 31.1, &#8220;Supported hosts&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>For example, to do a <a href="#qemu-buildroot-setup">QEMU Buildroot setup</a> inside Docker, run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get install docker
./run-docker create &amp;&amp; \
./run-docker sh -- ./build --download-dependencies qemu-buildroot
./run-docker sh</pre>
</div>
</div>
<div class="paragraph">
<p>You are now left inside a shell in the Docker! From there, just run as usual:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run</pre>
</div>
</div>
<div class="paragraph">
<p>The host git top level directory is mounted inside the guest with a <a href="https://stackoverflow.com/questions/23439126/how-to-mount-a-host-directory-in-a-docker-container">Docker volume</a>, which means for example that you can use your host&#8217;s GUI text editor directly on the files. Just don&#8217;t forget that if you nuke that directory on the guest, then it gets nuked on the host as well!</p>
</div>
<div class="paragraph">
<p>Command breakdown:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>./run-docker create</code>: create the image and container.</p>
<div class="paragraph">
<p>Needed only the very first time you use Docker, or if you run <code>./run-docker DESTROY</code> to restart for scratch, or save some disk space.</p>
</div>
<div class="paragraph">
<p>The image and container name is <code>lkmc</code>. The container shows under:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>docker ps -a</pre>
</div>
</div>
<div class="paragraph">
<p>and the image shows under:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>docker images</pre>
</div>
</div>
</li>
<li>
<p><code>./run-docker sh</code>: open a shell on the container.</p>
<div class="paragraph">
<p>If it has not been started previously, start it. This can also be done explicitly with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-docker start</pre>
</div>
</div>
<div class="paragraph">
<p>Quit the shell as usual with <code>Ctrl-D</code></p>
</div>
<div class="paragraph">
<p>This can be called multiple times from different host terminals to open multiple shells.</p>
</div>
</li>
<li>
<p><code>./run-docker stop</code>: stop the container.</p>
<div class="paragraph">
<p>This might save a bit of CPU and RAM once you stop working on this project, but it should not be a lot.</p>
</div>
</li>
<li>
<p><code>./run-docker DESTROY</code>: delete the container and image.</p>
<div class="paragraph">
<p>This doesn&#8217;t really clean the build, since we mount the guest&#8217;s working directory on the host git top-level, so you basically just got rid of the <code>apt-get</code> installs.</p>
</div>
<div class="paragraph">
<p>To actually delete the Docker build, run on host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># sudo rm -rf out.docker</pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>To use <a href="#gdb">GDB step debug</a> from inside Docker, you need a second shell inside the container. You can either do that from another shell with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-docker sh</pre>
</div>
</div>
<div class="paragraph">
<p>or even better, by starting a <a href="#tmux">tmux</a> session inside the container. We install <code>tmux</code> by default in the container.</p>
</div>
<div class="paragraph">
<p>You can also start a second shell and run a command in it at the same time with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-docker sh -- ./run-gdb start_kernel</pre>
</div>
</div>
<div class="paragraph">
<p>To use <a href="#qemu-graphic-mode">QEMU graphic mode</a> from Docker, run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --graphic --vnc</pre>
</div>
</div>
<div class="paragraph">
<p>and then on host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get install vinagre
./vnc</pre>
</div>
</div>
<div class="paragraph">
<p>TODO make files created inside Docker be owned by the current user in host instead of <code>root</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/33681396/how-do-i-write-to-a-volume-container-as-non-root-in-docker" class="bare">https://stackoverflow.com/questions/33681396/how-do-i-write-to-a-volume-container-as-non-root-in-docker</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/23544282/what-is-the-best-way-to-manage-permissions-for-docker-shared-volumes" class="bare">https://stackoverflow.com/questions/23544282/what-is-the-best-way-to-manage-permissions-for-docker-shared-volumes</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/31779802/shared-volume-file-permissions-ownership-docker" class="bare">https://stackoverflow.com/questions/31779802/shared-volume-file-permissions-ownership-docker</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="prebuilt"><a class="anchor" href="#prebuilt"></a><a class="link" href="#prebuilt">1.4. Prebuilt setup</a></h3>
<div class="sect3">
<h4 id="about-the-prebuilt-setup"><a class="anchor" href="#about-the-prebuilt-setup"></a><a class="link" href="#about-the-prebuilt-setup">1.4.1. About the prebuilt setup</a></h4>
<div class="paragraph">
<p>This setup uses prebuilt binaries that we upload to GitHub from time to time.</p>
</div>
<div class="paragraph">
<p>We don&#8217;t currently provide a full prebuilt because it would be too big to host freely, notably because of the cross toolchain.</p>
</div>
<div class="paragraph">
<p>Our prebuilts currently include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#qemu-buildroot-setup">QEMU Buildroot setup</a> binaries</p>
<div class="ulist">
<ul>
<li>
<p>Linux kernel</p>
</li>
<li>
<p>root filesystem</p>
</li>
</ul>
</div>
</li>
<li>
<p><a href="#baremetal-setup">Baremetal setup</a> binaries for QEMU</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>For more details, see our our <a href="#release">release procedure</a>.</p>
</div>
<div class="paragraph">
<p>Advantage of this setup: saves time and disk space on the initial install, which is expensive in largely due to building the toolchain.</p>
</div>
<div class="paragraph">
<p>The limitations are severe however:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>can&#8217;t <a href="#gdb">GDB step debug the kernel</a>, since the source and cross toolchain with GDB are not available. Buildroot cannot easily use a host toolchain: <a href="#prebuilt-toolchain">Section 29.2.2.1.1, &#8220;Buildroot use prebuilt host toolchain&#8221;</a>.</p>
<div class="paragraph">
<p>Maybe we could work around this by just downloading the kernel source somehow, and using a host prebuilt GDB, but we felt that it would be too messy and unreliable.</p>
</div>
</li>
<li>
<p>you won&#8217;t get the latest version of this repository. Our <a href="#travis">Travis</a> attempt to automate builds failed, and storing a release for every commit would likely make GitHub mad at us anyways.</p>
</li>
<li>
<p><a href="#gem5">gem5</a> is not currently supported. The major blocking point is how to avoid distributing the kernel images twice: once for gem5 which uses <code>vmlinux</code>, and once for QEMU which uses <code>arch/*</code> images, see also:</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/issues/79" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/issues/79</a></p>
</li>
<li>
<p><a href="#vmlinux-vs-bzimage-vs-zimage-vs-image">vmlinux vs bzImage vs zImage vs Image</a>.</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>This setup might be good enough for those developing simulators, as that requires less image modification. But once again, if you are serious about this, why not just let your computer build the <a href="#qemu-buildroot-setup">full featured setup</a> while you take a coffee or a nap? :-)</p>
</div>
</div>
<div class="sect3">
<h4 id="prebuilt-setup-getting-started"><a class="anchor" href="#prebuilt-setup-getting-started"></a><a class="link" href="#prebuilt-setup-getting-started">1.4.2. Prebuilt setup getting started</a></h4>
<div class="paragraph">
<p>Checkout to the latest tag and use the Ubuntu packaged QEMU to boot Linux:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get install qemu-system-x86
git clone https://github.com/cirosantilli/linux-kernel-module-cheat
cd linux-kernel-module-cheat
git checkout "$(git rev-list --tags --max-count=1)"
./release-download-latest
unzip lkmc-*.zip
./run --qemu-which host</pre>
</div>
</div>
<div class="paragraph">
<p>You have to checkout to the latest tag to ensure that the scripts match the release format: <a href="https://stackoverflow.com/questions/1404796/how-to-get-the-latest-tag-name-in-current-branch-in-git" class="bare">https://stackoverflow.com/questions/1404796/how-to-get-the-latest-tag-name-in-current-branch-in-git</a></p>
</div>
<div class="paragraph">
<p>This is known not to work for aarch64 on an Ubuntu 16.04 host with QEMU 2.5.0, presumably because QEMU is too old, the terminal does not show any output. I haven&#8217;t investigated why.</p>
</div>
<div class="paragraph">
<p>Or to run a baremetal example instead:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--baremetal userland/c/hello.c \
--qemu-which host \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Be saner and use our custom built QEMU instead:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --download-dependencies qemu
./run</pre>
</div>
</div>
<div class="paragraph">
<p>This also allows you to <a href="#your-first-qemu-hack">modify QEMU</a> if you&#8217;re into that sort of thing.</p>
</div>
<div class="paragraph">
<p>To build the kernel modules as in <a href="#your-first-kernel-module-hack">Your first kernel module hack</a> do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git submodule update --depth 1 --init --recursive "$(./getvar linux_source_dir)"
./build-linux --no-modules-install -- modules_prepare
./build-modules --gcc-which host
./run</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: for now the only way to test those modules out without <a href="#qemu-buildroot-setup-getting-started">building Buildroot</a> is with 9p, since we currently rely on Buildroot to manipulate the root filesystem.</p>
</div>
<div class="paragraph">
<p>Command explanation:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>modules_prepare</code> does the minimal build procedure required on the kernel for us to be able to compile the kernel modules, and is way faster than doing a full kernel build. A full kernel build would also work however.</p>
</li>
<li>
<p><code>--gcc-which host</code> selects your host Ubuntu packaged GCC, since you don&#8217;t have the Buildroot toolchain</p>
</li>
<li>
<p><code>--no-modules-install</code> is required otherwise the <code>make modules_install</code> target we run by default fails, since the kernel wasn&#8217;t built</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>To modify the Linux kernel, build and use it as usual:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git submodule update --depth 1 --init --recursive "$(./getvar linux_source_dir)"
./build-linux
./run</pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="host"><a class="anchor" href="#host"></a><a class="link" href="#host">1.5. Host kernel module setup</a></h3>
<div class="paragraph">
<p><strong>THIS IS DANGEROUS (AND FUN), YOU HAVE BEEN WARNED</strong></p>
</div>
<div class="paragraph">
<p>This method runs the kernel modules directly on your host computer without a VM, and saves you the compilation time and disk usage of the virtual machine method.</p>
</div>
<div class="paragraph">
<p>It has however severe limitations:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>can&#8217;t control which kernel version and build options to use. So some of the modules will likely not compile because of kernel API changes, since <a href="https://stackoverflow.com/questions/37098482/how-to-build-a-linux-kernel-module-so-that-it-is-compatible-with-all-kernel-rele/45429681#45429681">the Linux kernel does not have a stable kernel module API</a>.</p>
</li>
<li>
<p>bugs can easily break you system. E.g.:</p>
<div class="ulist">
<ul>
<li>
<p>segfaults can trivially lead to a kernel crash, and require a reboot</p>
</li>
<li>
<p>your disk could get erased. Yes, this can also happen with <code>sudo</code> from userland. But you should not use <code>sudo</code> when developing newbie programs. And for the kernel you don&#8217;t have the choice not to use <code>sudo</code>.</p>
</li>
<li>
<p>even more subtle system corruption such as <a href="https://unix.stackexchange.com/questions/78858/cannot-remove-or-reinsert-kernel-module-after-error-while-inserting-it-without-r">not being able to rmmod</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>can&#8217;t control which hardware is used, notably the CPU architecture</p>
</li>
<li>
<p>can&#8217;t step debug it with <a href="#gdb">GDB</a> easily. The alternatives are <a href="https://en.wikipedia.org/wiki/JTAG">JTAG</a> or <a href="#kgdb">KGDB</a>, but those are less reliable, and require extra hardware.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Still interested?</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-modules --gcc-which host --host</pre>
</div>
</div>
<div class="paragraph">
<p>Compilation will likely fail for some modules because of kernel or toolchain differences that we can&#8217;t control on the host.</p>
</div>
<div class="paragraph">
<p>The best workaround is to compile just your modules with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-modules --gcc-which host --host -- hello hello2</pre>
</div>
</div>
<div class="paragraph">
<p>which is equivalent to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-modules \
--gcc-which host \
--host \
-- \
kernel_modules/hello.c \
kernel_modules/hello2.c \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Or just remove the <code>.c</code> extension from the failing files and try again:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd "$(./getvar kernel_modules_source_dir)"
mv broken.c broken.c~</pre>
</div>
</div>
<div class="paragraph">
<p>Once you manage to compile, and have come to terms with the fact that this may blow up your host, try it out with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd "$(./getvar kernel_modules_build_host_subdir)"
sudo insmod hello.ko
# Our module is there.
sudo lsmod | grep hello
# Last message should be: hello init
dmesg -T
sudo rmmod hello
# Last message should be: hello exit
dmesg -T
# Not present anymore
sudo lsmod | grep hello</pre>
</div>
</div>
<div class="sect3">
<h4 id="hello-host"><a class="anchor" href="#hello-host"></a><a class="link" href="#hello-host">1.5.1. Hello host</a></h4>
<div class="paragraph">
<p>Minimal host build system example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd hello_host_kernel_module
make
sudo insmod hello.ko
dmesg
sudo rmmod hello.ko
dmesg</pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="userland-setup"><a class="anchor" href="#userland-setup"></a><a class="link" href="#userland-setup">1.6. Userland setup</a></h3>
<div class="sect3">
<h4 id="about-the-userland-setup"><a class="anchor" href="#about-the-userland-setup"></a><a class="link" href="#about-the-userland-setup">1.6.1. About the userland setup</a></h4>
<div class="paragraph">
<p>In order to test the kernel and emulators, userland content in the form of executables and scripts is of course required, and we store it mostly under:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/">userland/</a></p>
</li>
<li>
<p><a href="#rootfs_overlay">rootfs_overlay</a></p>
</li>
<li>
<p><a href="#add-new-buildroot-packages">Add new Buildroot packages</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>When we started this repository, it only contained content that interacted very closely with the kernel, or that had required performance analysis.</p>
</div>
<div class="paragraph">
<p>However, we soon started to notice that this had an increasing overlap with other userland test repositories: we were duplicating build and test infrastructure and even some examples.</p>
</div>
<div class="paragraph">
<p>Therefore, we decided to consolidate other userland tutorials that we had scattered around into this repository.</p>
</div>
<div class="paragraph">
<p>Notable userland content included / moving into this repository includes:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#userland-assembly">Userland assembly</a></p>
</li>
<li>
<p><a href="#c">C</a></p>
</li>
<li>
<p><a href="#cpp">C++</a></p>
</li>
<li>
<p><a href="#posix">POSIX</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/algorithm-cheat" class="bare">https://github.com/cirosantilli/algorithm-cheat</a> TODO will be good to move here for performance analysis <a href="#gem5-run-benchmark">with gem5</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="userland-setup-getting-started"><a class="anchor" href="#userland-setup-getting-started"></a><a class="link" href="#userland-setup-getting-started">1.6.2. Userland setup getting started</a></h4>
<div class="paragraph">
<p>There are several ways to run our <a href="#userland-content">Userland content</a>, notably:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>natively on the host as shown at: <a href="#userland-setup-getting-started-natively">Section 1.6.2.1, &#8220;Userland setup getting started natively&#8221;</a></p>
<div class="paragraph">
<p>Can only run examples compatible with your host CPU architecture and OS, but has the fastest setup and runtimes.</p>
</div>
</li>
<li>
<p>from user mode simulation with:</p>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>the host prebuilt toolchain: <a href="#userland-setup-getting-started-with-prebuilt-toolchain-and-qemu-user-mode">Section 1.6.2.2, &#8220;Userland setup getting started with prebuilt toolchain and QEMU user mode&#8221;</a></p>
</li>
<li>
<p>the Buildroot toolchain you built yourself: <a href="#qemu-user-mode-getting-started">Section 10.1, &#8220;QEMU user mode getting started&#8221;</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>This setup:</p>
</div>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>can run most examples, including those for other CPU architectures, with the notable exception of examples that rely on kernel modules</p>
</li>
<li>
<p>can run reproducible approximate performance experiments with gem5, see e.g. <a href="#bst-vs-heap-vs-hashmap">BST vs heap vs hashmap</a></p>
</li>
</ul>
</div>
</div>
</div>
</li>
<li>
<p>from full system simulation as shown at: <a href="#qemu-buildroot-setup-getting-started">Section 1.1.1, &#8220;QEMU Buildroot setup getting started&#8221;</a>.</p>
<div class="paragraph">
<p>This is the most reproducible and controlled environment, and all examples work there. But also the slower one to setup.</p>
</div>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="userland-setup-getting-started-natively"><a class="anchor" href="#userland-setup-getting-started-natively"></a><a class="link" href="#userland-setup-getting-started-natively">1.6.2.1. Userland setup getting started natively</a></h5>
<div class="paragraph">
<p>With this setup, we will use the host toolchain and execute executables directly on the host.</p>
</div>
<div class="paragraph">
<p>No toolchain build is required, so you can just download your distro toolchain and jump straight into it.</p>
</div>
<div class="paragraph">
<p>Build, run and example, and clean it in-tree with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get install gcc
cd userland
./build c/hello
./c/hello.out
./build --clean</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/hello.c">userland/c/hello.c</a>.</p>
</div>
<div class="paragraph">
<p>Build an entire directory and test it:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd userland
./build c
./test c</pre>
</div>
</div>
<div class="paragraph">
<p>Build the current directory and test it:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd userland/c
./build
./test</pre>
</div>
</div>
<div class="paragraph">
<p>As mentioned at <a href="#user-mode-tests">User mode tests</a>, tests under <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/libs">userland/libs</a> require certain optional libraries to be installed, and are not built or tested by default.</p>
</div>
<div class="paragraph">
<p>You can install those libraries with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd linux-kernel-module-cheat
./build --download-dependencies userland-host</pre>
</div>
</div>
<div class="paragraph">
<p>and then build the examples and test with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --package-all
./test --package-all</pre>
</div>
</div>
<div class="paragraph">
<p>Pass custom compiler options:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --ccflags='-foptimize-sibling-calls -foptimize-strlen' --force-rebuild</pre>
</div>
</div>
<div class="paragraph">
<p>Here we used <code>--force-rebuild</code> to force rebuild since the sources weren&#8217;t modified since the last build.</p>
</div>
<div class="paragraph">
<p>Some CLI options have more specialized flags, e.g. <code>-O</code> optimization level:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --optimization-level 3 --force-rebuild</pre>
</div>
</div>
<div class="paragraph">
<p>See also <a href="#user-mode-static-executables">User mode static executables</a> for <code>--static</code>.</p>
</div>
<div class="paragraph">
<p>The <code>build</code> scripts inside <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/">userland/</a> are just symlinks to <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-userland-in-tree">build-userland-in-tree</a> which you can also use from toplevel as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland-in-tree
./build-userland-in-tree userland/c
./build-userland-in-tree userland/c/hello.c</pre>
</div>
</div>
<div class="paragraph">
<p><code>build-userland-in-tre</code> is in turn just a thin wrapper around <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-userland">build-userland</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland --gcc-which host --in-tree userland/c</pre>
</div>
</div>
<div class="paragraph">
<p>So you can use any option supported by <code>build-userland</code> script freely with <code>build-userland-in-tree</code> and <code>build</code>.</p>
</div>
<div class="paragraph">
<p>The situation is analogous for <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/test">userland/test</a>, <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/test-executables-in-tree">test-executables-in-tree</a> and <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/test-executables">test-executables</a>, which are further documented at: <a href="#user-mode-tests">Section 10.2, &#8220;User mode tests&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Do a more clean out-of-tree build instead and run the program:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland --gcc-which host --userland-build-id host
./run --emulator native --userland userland/c/hello.c --userland-build-id host</pre>
</div>
</div>
<div class="paragraph">
<p>Here we:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>put the host executables in a separate <a href="#build-variants">build-variant</a> to avoid conflict with Buildroot builds.</p>
</li>
<li>
<p>ran with the <code>--emulator native</code> option to run the program natively</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In this case you can debub the program with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --debug-vm --emulator native --userland userland/c/hello.c --userland-build-id host</pre>
</div>
</div>
<div class="paragraph">
<p>as shown at: <a href="#debug-the-emulator">Section 18.7, &#8220;Debug the emulator&#8221;</a>, although direct GDB host usage works as well of course.</p>
</div>
</div>
<div class="sect4">
<h5 id="userland-setup-getting-started-with-prebuilt-toolchain-and-qemu-user-mode"><a class="anchor" href="#userland-setup-getting-started-with-prebuilt-toolchain-and-qemu-user-mode"></a><a class="link" href="#userland-setup-getting-started-with-prebuilt-toolchain-and-qemu-user-mode">1.6.2.2. Userland setup getting started with prebuilt toolchain and QEMU user mode</a></h5>
<div class="paragraph">
<p>If you are lazy to built the Buildroot toolchain and QEMU, but want to run e.g. ARM <a href="#userland-assembly">Userland assembly</a> in <a href="#user-mode-simulation">User mode simulation</a>, you can get away on Ubuntu 18.04 with just:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get install gcc-aarch64-linux-gnu qemu-system-aarch64
./build-userland \
--arch aarch64 \
--gcc-which host \
--userland-build-id host \
;
./run \
--arch aarch64 \
--qemu-which host \
--userland-build-id host \
--userland userland/c/command_line_arguments.c \
--userland-args 'asdf "qw er"' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>where:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>--gcc-which host</code>: use the host toolchain.</p>
<div class="paragraph">
<p>We must pass this to <code>./run</code> as well because QEMU must know which dynamic libraries to use. See also: <a href="#user-mode-static-executables">Section 10.5, &#8220;User mode static executables&#8221;</a>.</p>
</div>
</li>
<li>
<p><code>--userland-build-id host</code>: put the host built into a <a href="#build-variants">Build variants</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This present the usual trade-offs of using prebuilts as mentioned at: <a href="#prebuilt">Section 1.4, &#8220;Prebuilt setup&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Other functionality are analogous, e.g. testing:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./test-executables \
--arch aarch64 \
--gcc-which host \
--qemu-which host \
--userland-build-id host \
;</pre>
</div>
</div>
<div class="paragraph">
<p>and <a href="#user-mode-gdb">User mode GDB</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--gdb \
--gcc-which host \
--qemu-which host \
--userland-build-id host \
--userland userland/c/command_line_arguments.c \
--userland-args 'asdf "qw er"' \
;</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="userland-setup-getting-started-full-system"><a class="anchor" href="#userland-setup-getting-started-full-system"></a><a class="link" href="#userland-setup-getting-started-full-system">1.6.2.3. Userland setup getting started full system</a></h5>
<div class="paragraph">
<p>First ensure that <a href="#qemu-buildroot-setup">QEMU Buildroot setup</a> is working.</p>
</div>
<div class="paragraph">
<p>After doing that setup, you can already execute your userland programs from inside QEMU: the only missing step is how to rebuild executables and run them.</p>
</div>
<div class="paragraph">
<p>And the answer is exactly analogous to what is shown at: <a href="#your-first-kernel-module-hack">Section 1.1.2.2, &#8220;Your first kernel module hack&#8221;</a></p>
</div>
<div class="paragraph">
<p>For example, if we modify <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/hello.c">userland/c/hello.c</a> to print out something different, we can just rebuild it with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-userland">build-userland</a>. <code>./build</code> calls that script automatically for us when doing the initial full build.</p>
</div>
<div class="paragraph">
<p>Now, run the program either without rebooting use the <a href="#9p">9P</a> mount:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/mnt/9p/out_rootfs_overlay/c/hello.out</pre>
</div>
</div>
<div class="paragraph">
<p>or shutdown QEMU, add the executable to the root filesystem:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot</pre>
</div>
</div>
<div class="paragraph">
<p>reboot and use the root filesystem as usual:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./hello.out</pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="baremetal-setup"><a class="anchor" href="#baremetal-setup"></a><a class="link" href="#baremetal-setup">1.7. Baremetal setup</a></h3>
<div class="sect3">
<h4 id="about-the-baremetal-setup"><a class="anchor" href="#about-the-baremetal-setup"></a><a class="link" href="#about-the-baremetal-setup">1.7.1. About the baremetal setup</a></h4>
<div class="paragraph">
<p>This setup does not use the Linux kernel nor Buildroot at all: it just runs your very own minimal OS.</p>
</div>
<div class="paragraph">
<p><code>x86_64</code> is not currently supported, only <code>arm</code> and <code>aarch64</code>: I had made some x86 bare metal examples at: <a href="https://github.com/cirosantilli/x86-bare-metal-examples" class="bare">https://github.com/cirosantilli/x86-bare-metal-examples</a> but I&#8217;m lazy to port them here now. Pull requests are welcome.</p>
</div>
<div class="paragraph">
<p>The main reason this setup is included in this project, despite the word "Linux" being on the project name, is that a lot of the emulator boilerplate can be reused for both use cases.</p>
</div>
<div class="paragraph">
<p>This setup allows you to make a tiny OS and that runs just a few instructions, use it to fully control the CPU to better understand the simulators for example, or develop your own OS if you are into that.</p>
</div>
<div class="paragraph">
<p>You can also use C and a subset of the C standard library because we enable <a href="https://en.wikipedia.org/wiki/Newlib">Newlib</a> by default. See also: <a href="https://electronics.stackexchange.com/questions/223929/c-standard-libraries-on-bare-metal/400077#400077" class="bare">https://electronics.stackexchange.com/questions/223929/c-standard-libraries-on-bare-metal/400077#400077</a></p>
</div>
<div class="paragraph">
<p>Our C bare-metal compiler is built with <a href="https://github.com/crosstool-ng/crosstool-ng">crosstool-NG</a>. If you have already built <a href="#qemu-buildroot-setup">Buildroot</a> previously, you will end up with two GCCs installed. Unfortunately I don&#8217;t see a solution for this, since we need separate toolchains for Newlib on baremetal and glibc on Linux: <a href="https://stackoverflow.com/questions/38956680/difference-between-arm-none-eabi-and-arm-linux-gnueabi/38989869#38989869" class="bare">https://stackoverflow.com/questions/38956680/difference-between-arm-none-eabi-and-arm-linux-gnueabi/38989869#38989869</a></p>
</div>
</div>
<div class="sect3">
<h4 id="baremetal-setup-getting-started"><a class="anchor" href="#baremetal-setup-getting-started"></a><a class="link" href="#baremetal-setup-getting-started">1.7.2. Baremetal setup getting started</a></h4>
<div class="paragraph">
<p>Every <code>.c</code> file inside <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/">baremetal/</a> and <code>.S</code> file inside <code>baremetal/arch/&lt;arch&gt;/</code> generates a separate baremetal image.</p>
</div>
<div class="paragraph">
<p>For example, to run <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/dump_regs.c">baremetal/arch/aarch64/dump_regs.c</a> in QEMU do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --arch aarch64 --download-dependencies qemu-baremetal
./run --arch aarch64 --baremetal baremetal/arch/aarch64/dump_regs.c</pre>
</div>
</div>
<div class="paragraph">
<p>And the terminal prints the values of certain system registers. This example prints registers that are only accessible from <a href="#arm-exception-levels">EL1</a> or higher, and thus could not be run in userland.</p>
</div>
<div class="paragraph">
<p>In addition to the examples under <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/">baremetal/</a>, several of the <a href="#userland-content">userland examples</a> can also be run in baremetal! This is largely due to the <a href="#about-the-baremetal-setup">awesomeness of Newlib</a>.</p>
</div>
<div class="paragraph">
<p>The examples that work include most <a href="#c">C examples</a> that don&#8217;t rely on complicated syscalls such as threads, and almost all the <a href="#userland-assembly">Userland assembly</a> examples.</p>
</div>
<div class="paragraph">
<p>The exact list of userland programs that work in baremetal is specified in <a href="#path-properties">path_properties</a> with the <code>baremetal</code> property, but you can also easily find it out with a <a href="#baremetal-tests">baremetal test dry run</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./test-executables --arch aarch64 --dry-run --mode baremetal</pre>
</div>
</div>
<div class="paragraph">
<p>For example, we can run the C hello world <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/hello.c">userland/c/hello.c</a> simply as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --baremetal userland/c/hello.c</pre>
</div>
</div>
<div class="paragraph">
<p>and that outputs to the serial port the string:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>hello</pre>
</div>
</div>
<div class="paragraph">
<p>which QEMU shows on the host terminal.</p>
</div>
<div class="paragraph">
<p>To modify a baremetal program, simply edit the file, e.g.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vim userland/c/hello.c</pre>
</div>
</div>
<div class="paragraph">
<p>and rebuild:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-baremetal --arch aarch64
./run --arch aarch64 --baremetal userland/c/hello.c</pre>
</div>
</div>
<div class="paragraph">
<p><code>./build qemu-baremetal</code> that we run previously is only needed for the initial build. That script calls <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-baremetal">build-baremetal</a> for us, in addition to building prerequisites such as QEMU and crosstool-NG.</p>
</div>
<div class="paragraph">
<p><code>./build-baremetal</code> uses crosstool-NG, and so it must be preceded by <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-crosstool-ng">build-crosstool-ng</a>, which <code>./build qemu-baremetal</code> also calls.</p>
</div>
<div class="paragraph">
<p>Now let&#8217;s run <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/add.S">userland/arch/aarch64/add.S</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --baremetal userland/arch/aarch64/add.S</pre>
</div>
</div>
<div class="paragraph">
<p>This time, the terminal does not print anything, which indicates success: if you look into the source, you will see that we just have an assertion there.</p>
</div>
<div class="paragraph">
<p>You can see a sample assertion fail in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/assert_fail.c">userland/c/assert_fail.c</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --baremetal userland/c/assert_fail.c</pre>
</div>
</div>
<div class="paragraph">
<p>and the terminal contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lkmc_exit_status_134
error: simulation error detected by parsing logs</pre>
</div>
</div>
<div class="paragraph">
<p>and the exit status of our script is 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>You can run all the baremetal examples in one go and check that all assertions passed with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./test-executables --arch aarch64 --mode baremetal</pre>
</div>
</div>
<div class="paragraph">
<p>To use gem5 instead of QEMU do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --download-dependencies gem5-baremetal
./run --arch aarch64 --baremetal userland/c/hello.c --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>and then <a href="#qemu-buildroot-setup">as usual</a> open a shell with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gem5-shell</pre>
</div>
</div>
<div class="paragraph">
<p>Or as usual, <a href="#tmux">tmux</a> users can do both in one go with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --baremetal userland/c/hello.c --emulator gem5 --tmux</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: the carriage returns are a bit different than in QEMU, see: <a href="#gem5-baremetal-carriage-return">Section 27.4, &#8220;gem5 baremetal carriage return&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Note that <code>./build-baremetal</code> requires the <code>--emulator gem5</code> option, and generates separate executable images for both, as can be seen from:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo "$(./getvar --arch aarch64 --baremetal userland/c/hello.c --emulator qemu image)"
echo "$(./getvar --arch aarch64 --baremetal userland/c/hello.c --emulator gem5 image)"</pre>
</div>
</div>
<div class="paragraph">
<p>This is unlike the Linux kernel that has a single image for both QEMU and gem5:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo "$(./getvar --arch aarch64 --emulator qemu image)"
echo "$(./getvar --arch aarch64 --emulator gem5 image)"</pre>
</div>
</div>
<div class="paragraph">
<p>The reason for that is that on baremetal we don&#8217;t parse the <a href="#device-tree">device tress</a> from memory like the Linux kernel does, which tells the kernel for example the UART address, and many other system parameters.</p>
</div>
<div class="paragraph">
<p><code>gem5</code> also supports the <code>RealViewPBX</code> machine, which represents an older hardware compared to the default <code>VExpress_GEM5_V1</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-baremetal --arch aarch64 --emulator gem5 --machine RealViewPBX
./run --arch aarch64 --baremetal userland/c/hello.c --emulator gem5 --machine RealViewPBX</pre>
</div>
</div>
<div class="paragraph">
<p>see also: <a href="#gem5-arm-platforms">Section 19.18, &#8220;gem5 ARM platforms&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>This generates yet new separate images with new magic constants:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo "$(./getvar --arch aarch64 --baremetal userland/c/hello.c --emulator gem5 --machine VExpress_GEM5_V1 image)"
echo "$(./getvar --arch aarch64 --baremetal userland/c/hello.c --emulator gem5 --machine RealViewPBX image)"</pre>
</div>
</div>
<div class="paragraph">
<p>But just stick to newer and better <code>VExpress_GEM5_V1</code> unless you have a good reason to use <code>RealViewPBX</code>.</p>
</div>
<div class="paragraph">
<p>When doing baremetal programming, it is likely that you will want to learn userland assembly first, see: <a href="#userland-assembly">Section 22, &#8220;Userland assembly&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>For more information on baremetal, see the section: <a href="#baremetal">Section 27, &#8220;Baremetal&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>The following subjects are particularly important:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#tracing">Tracing</a></p>
</li>
<li>
<p><a href="#baremetal-gdb-step-debug">Baremetal GDB step debug</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="build-the-documentation"><a class="anchor" href="#build-the-documentation"></a><a class="link" href="#build-the-documentation">1.8. Build the documentation</a></h3>
<div class="paragraph">
<p>You don&#8217;t need to depend on GitHub.</p>
</div>
<div class="paragraph">
<p>For a quick and dirty build, install <a href="https://asciidoctor.org/">Asciidoctor</a> however you like and build:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>asciidoctor README.adoc
xdg-open README.html</pre>
</div>
</div>
<div class="paragraph">
<p>For development, you will want to do a more controlled build with extra error checking as follows.</p>
</div>
<div class="paragraph">
<p>For the initial build do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --download-dependencies docs</pre>
</div>
</div>
<div class="paragraph">
<p>which also downloads build dependencies.</p>
</div>
<div class="paragraph">
<p>Then the following times just to the faster:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-doc</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-doc">build-doc</a></p>
</div>
<div class="paragraph">
<p>The HTML output is located at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>xdg-open out/README.html</pre>
</div>
</div>
<div class="paragraph">
<p>More information about our documentation internals can be found at: <a href="#documentation">Section 31.5, &#8220;Documentation&#8221;</a></p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="gdb"><a class="anchor" href="#gdb"></a><a class="link" href="#gdb">2. GDB step debug</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="gdb-step-debug-kernel-boot"><a class="anchor" href="#gdb-step-debug-kernel-boot"></a><a class="link" href="#gdb-step-debug-kernel-boot">2.1. GDB step debug kernel boot</a></h3>
<div class="paragraph">
<p><code>--gdb-wait</code> makes QEMU and gem5 wait for a GDB connection, otherwise we could accidentally go past the point we want to break at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --gdb-wait</pre>
</div>
</div>
<div class="paragraph">
<p>Say you want to break at <code>start_kernel</code>. So on another shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb start_kernel</pre>
</div>
</div>
<div class="paragraph">
<p>or at a given line:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb init/main.c:1088</pre>
</div>
</div>
<div class="paragraph">
<p>Now QEMU will stop there, and you can use the normal GDB commands:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>list
next
continue</pre>
</div>
</div>
<div class="paragraph">
<p>See also:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/11408041/how-to-debug-the-linux-kernel-with-gdb-and-qemu/33203642#33203642" class="bare">https://stackoverflow.com/questions/11408041/how-to-debug-the-linux-kernel-with-gdb-and-qemu/33203642#33203642</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/4943857/linux-kernel-live-debugging-how-its-done-and-what-tools-are-used/42316607#42316607" class="bare">https://stackoverflow.com/questions/4943857/linux-kernel-live-debugging-how-its-done-and-what-tools-are-used/42316607#42316607</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="gdb-step-debug-kernel-boot-other-archs"><a class="anchor" href="#gdb-step-debug-kernel-boot-other-archs"></a><a class="link" href="#gdb-step-debug-kernel-boot-other-archs">2.1.1. GDB step debug kernel boot other archs</a></h4>
<div class="paragraph">
<p>Just don&#8217;t forget to pass <code>--arch</code> to <code>./run-gdb</code>, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --gdb-wait</pre>
</div>
</div>
<div class="paragraph">
<p>and:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --arch aarch64 start_kernel</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="kernel-o0"><a class="anchor" href="#kernel-o0"></a><a class="link" href="#kernel-o0">2.1.2. Disable kernel compiler optimizations</a></h4>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/29151235/how-to-de-optimize-the-linux-kernel-to-and-compile-it-with-o0" class="bare">https://stackoverflow.com/questions/29151235/how-to-de-optimize-the-linux-kernel-to-and-compile-it-with-o0</a></p>
</div>
<div class="paragraph">
<p><code>O=0</code> is an impossible dream, <code>O=2</code> being the default.</p>
</div>
<div class="paragraph">
<p>So get ready for some weird jumps, and <code>&lt;value optimized out&gt;</code> fun. Why, Linux, why.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gdb-step-debug-kernel-post-boot"><a class="anchor" href="#gdb-step-debug-kernel-post-boot"></a><a class="link" href="#gdb-step-debug-kernel-post-boot">2.2. GDB step debug kernel post-boot</a></h3>
<div class="paragraph">
<p>Let&#8217;s observe the kernel <code>write</code> system call as it reacts to some userland actions.</p>
</div>
<div class="paragraph">
<p>Start QEMU with just:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run</pre>
</div>
</div>
<div class="paragraph">
<p>and after boot inside a shell run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./count.sh</pre>
</div>
</div>
<div class="paragraph">
<p>which counts to infinity to stdout. Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/count.sh">rootfs_overlay/lkmc/count.sh</a>.</p>
</div>
<div class="paragraph">
<p>Then in another shell, run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb</pre>
</div>
</div>
<div class="paragraph">
<p>and then hit:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Ctrl-C
break __x64_sys_write
continue
continue
continue</pre>
</div>
</div>
<div class="paragraph">
<p>And you now control the counting on the first shell from GDB!</p>
</div>
<div class="paragraph">
<p>Before v4.17, the symbol name was just <code>sys_write</code>, the change happened at <a href="https://github.com/torvalds/linux/commit/d5a00528b58cdb2c71206e18bd021e34c4eab878">d5a00528b58cdb2c71206e18bd021e34c4eab878</a>. As of Linux v 4.19, the function is called <code>sys_write</code> in <code>arm</code>, and <code>__arm64_sys_write</code> in <code>aarch64</code>. One good way to find it if the name changes again is to try:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rbreak .*sys_write</pre>
</div>
</div>
<div class="paragraph">
<p>or just have a quick look at the sources!</p>
</div>
<div class="paragraph">
<p>When you hit <code>Ctrl-C</code>, if we happen to be inside kernel code at that point, which is very likely if there are no heavy background tasks waiting, and we are just waiting on a <code>sleep</code> type system call of the command prompt, we can already see the source for the random place inside the kernel where we stopped.</p>
</div>
</div>
<div class="sect2">
<h3 id="tmux"><a class="anchor" href="#tmux"></a><a class="link" href="#tmux">2.3. tmux</a></h3>
<div class="paragraph">
<p>tmux just makes things even more fun by allowing us to see both the terminal for:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>emulator stdout</p>
</li>
<li>
<p><a href="#gdb">GDB step debug</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>at once without dragging windows around!</p>
</div>
<div class="paragraph">
<p>First start <code>tmux</code> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>tmux</pre>
</div>
</div>
<div class="paragraph">
<p>Now that you are inside a shell inside tmux, you can start GDB simply with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --gdb</pre>
</div>
</div>
<div class="paragraph">
<p>which is just a convenient shortcut for:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --gdb-wait --tmux --tmux-args start_kernel</pre>
</div>
</div>
<div class="paragraph">
<p>This splits the terminal into two panes:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>left: usual QEMU with terminal</p>
</li>
<li>
<p>right: GDB</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>and focuses on the GDB pane.</p>
</div>
<div class="paragraph">
<p>Now you can navigate with the usual tmux shortcuts:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>switch between the two panes with: <code>Ctrl-B O</code></p>
</li>
<li>
<p>close either pane by killing its terminal with <code>Ctrl-D</code> as usual</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>See the tmux manual for further details:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>man tmux</pre>
</div>
</div>
<div class="paragraph">
<p>To start again, switch back to the QEMU pane with <code>Ctrl-O</code>, kill the emulator, and re-run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --gdb</pre>
</div>
</div>
<div class="paragraph">
<p>This automatically clears the GDB pane, and starts a new one.</p>
</div>
<div class="paragraph">
<p>The option <code>--tmux-args</code> determines which options will be passed to the program running on the second tmux pane, and is equivalent to:</p>
</div>
<div class="paragraph">
<p>This is equivalent to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --gdb-wait
./run-gdb start_kernel</pre>
</div>
</div>
<div class="paragraph">
<p>Due to Python&#8217;s CLI parsing quicks, if the <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/run-gdb">run-gdb</a> arguments start with a dash <code>-</code>, you have to use the <code>=</code> sign, e.g. to <a href="#gdb-step-debug-early-boot">GDB step debug early boot</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --gdb --tmux-args=--no-continue</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://unix.stackexchange.com/questions/152738/how-to-split-a-new-window-and-run-a-command-in-this-new-window-using-tmux/432111#432111" class="bare">https://unix.stackexchange.com/questions/152738/how-to-split-a-new-window-and-run-a-command-in-this-new-window-using-tmux/432111#432111</a></p>
</div>
<div class="sect3">
<h4 id="tmux-gem5"><a class="anchor" href="#tmux-gem5"></a><a class="link" href="#tmux-gem5">2.3.1. tmux gem5</a></h4>
<div class="paragraph">
<p>If you are using gem5 instead of QEMU, <code>--tmux</code> has a different effect by default: it opens the gem5 terminal instead of the debugger:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --emulator gem5 --tmux</pre>
</div>
</div>
<div class="paragraph">
<p>To open a new pane with GDB instead of the terminal, use:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --gdb</pre>
</div>
</div>
<div class="paragraph">
<p>which is equivalent to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --emulator gem5 --gdb-wait --tmux --tmux-args start_kernel --tmux-program gdb</pre>
</div>
</div>
<div class="paragraph">
<p><code>--tmux-program</code> implies <code>--tmux</code>, so we can just write:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --emulator gem5 --gdb-wait --tmux-program gdb</pre>
</div>
</div>
<div class="paragraph">
<p>If you also want to see both GDB and the terminal with gem5, then you will need to open a separate shell manually as usual with <code>./gem5-shell</code>.</p>
</div>
<div class="paragraph">
<p>From inside tmux, you can create new terminals on a new window with <code>Ctrl-B C</code> split a pane yet again vertically with <code>Ctrl-B %</code> or horizontally with <code>Ctrl-B "</code>.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gdb-step-debug-kernel-module"><a class="anchor" href="#gdb-step-debug-kernel-module"></a><a class="link" href="#gdb-step-debug-kernel-module">2.4. GDB step debug kernel module</a></h3>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/28607538/how-to-debug-linux-kernel-modules-with-qemu/44095831#44095831" class="bare">https://stackoverflow.com/questions/28607538/how-to-debug-linux-kernel-modules-with-qemu/44095831#44095831</a></p>
</div>
<div class="paragraph">
<p>Loadable kernel modules are a bit trickier since the kernel can place them at different memory locations depending on load order.</p>
</div>
<div class="paragraph">
<p>So we cannot set the breakpoints before <code>insmod</code>.</p>
</div>
<div class="paragraph">
<p>However, the Linux kernel GDB scripts offer the <code>lx-symbols</code> command, which takes care of that beautifully for us.</p>
</div>
<div class="paragraph">
<p>Shell 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run</pre>
</div>
</div>
<div class="paragraph">
<p>Wait for the boot to end and run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod timer.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/timer.c">kernel_modules/timer.c</a>.</p>
</div>
<div class="paragraph">
<p>This prints a message to dmesg every second.</p>
</div>
<div class="paragraph">
<p>Shell 2:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb</pre>
</div>
</div>
<div class="paragraph">
<p>In GDB, hit <code>Ctrl-C</code>, and note how it says:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>scanning for modules in /root/linux-kernel-module-cheat/out/kernel_modules/x86_64/kernel_modules
loading @0xffffffffc0000000: /root/linux-kernel-module-cheat/out/kernel_modules/x86_64/kernel_modules/timer.ko</pre>
</div>
</div>
<div class="paragraph">
<p>That&#8217;s <code>lx-symbols</code> working! Now simply:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>break lkmc_timer_callback
continue
continue
continue</pre>
</div>
</div>
<div class="paragraph">
<p>and we now control the callback from GDB!</p>
</div>
<div class="paragraph">
<p>Just don&#8217;t forget to remove your breakpoints after <code>rmmod</code>, or they will point to stale memory locations.</p>
</div>
<div class="paragraph">
<p>TODO: why does <code>break work_func</code> for <code>insmod kthread.ko</code> not very well? Sometimes it breaks but not others.</p>
</div>
<div class="sect3">
<h4 id="gdb-step-debug-kernel-module-arm"><a class="anchor" href="#gdb-step-debug-kernel-module-arm"></a><a class="link" href="#gdb-step-debug-kernel-module-arm">2.4.1. GDB step debug kernel module insmodded by init on ARM</a></h4>
<div class="paragraph">
<p>TODO on <code>arm</code> 51e31cdc2933a774c2a0dc62664ad8acec1d2dbe it does not always work, and <code>lx-symbols</code> fails with the message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>loading vmlinux
Traceback (most recent call last):
File "/linux-kernel-module-cheat//out/arm/buildroot/build/linux-custom/scripts/gdb/linux/symbols.py", line 163, in invoke
self.load_all_symbols()
File "/linux-kernel-module-cheat//out/arm/buildroot/build/linux-custom/scripts/gdb/linux/symbols.py", line 150, in load_all_symbols
[self.load_module_symbols(module) for module in module_list]
File "/linux-kernel-module-cheat//out/arm/buildroot/build/linux-custom/scripts/gdb/linux/symbols.py", line 110, in load_module_symbols
module_name = module['name'].string()
gdb.MemoryError: Cannot access memory at address 0xbf0000cc
Error occurred in Python command: Cannot access memory at address 0xbf0000cc</pre>
</div>
</div>
<div class="paragraph">
<p>Can&#8217;t reproduce on <code>x86_64</code> and <code>aarch64</code> are fine.</p>
</div>
<div class="paragraph">
<p>It is kind of random: if you just <code>insmod</code> manually and then immediately <code>./run-gdb --arch arm</code>, then it usually works.</p>
</div>
<div class="paragraph">
<p>But this fails most of the time: shell 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --eval-after 'insmod hello.ko'</pre>
</div>
</div>
<div class="paragraph">
<p>shell 2:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --arch arm</pre>
</div>
</div>
<div class="paragraph">
<p>then hit <code>Ctrl-C</code> on shell 2, and voila.</p>
</div>
<div class="paragraph">
<p>Then:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /proc/modules</pre>
</div>
</div>
<div class="paragraph">
<p>says that the load address is:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0xbf000000</pre>
</div>
</div>
<div class="paragraph">
<p>so it is close to the failing <code>0xbf0000cc</code>.</p>
</div>
<div class="paragraph">
<p><code>readelf</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain readelf -- -s "$(./getvar kernel_modules_build_subdir)/hello.ko"</pre>
</div>
</div>
<div class="paragraph">
<p>does not give any interesting hits at <code>cc</code>, no symbol was placed that far.</p>
</div>
</div>
<div class="sect3">
<h4 id="gdb-module_init"><a class="anchor" href="#gdb-module_init"></a><a class="link" href="#gdb-module_init">2.4.2. GDB module_init</a></h4>
<div class="paragraph">
<p>TODO find a more convenient method. We have working methods, but they are not ideal.</p>
</div>
<div class="paragraph">
<p>This is not very easy, since by the time the module finishes loading, and <code>lx-symbols</code> can work properly, <code>module_init</code> has already finished running!</p>
</div>
<div class="paragraph">
<p>Possibly asked at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/37059320/debug-a-kernel-module-being-loaded" class="bare">https://stackoverflow.com/questions/37059320/debug-a-kernel-module-being-loaded</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/11888412/debug-the-init-module-call-of-a-linux-kernel-module" class="bare">https://stackoverflow.com/questions/11888412/debug-the-init-module-call-of-a-linux-kernel-module</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="gdb-module_init-step-into-it"><a class="anchor" href="#gdb-module_init-step-into-it"></a><a class="link" href="#gdb-module_init-step-into-it">2.4.2.1. GDB module_init step into it</a></h5>
<div class="paragraph">
<p>This is the best method we&#8217;ve found so far.</p>
</div>
<div class="paragraph">
<p>The kernel calls <code>module_init</code> synchronously, therefore it is not hard to step into that call.</p>
</div>
<div class="paragraph">
<p>As of 4.16, the call happens in <code>do_one_initcall</code>, so we can do in shell 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run</pre>
</div>
</div>
<div class="paragraph">
<p>shell 2 after boot finishes (because there are other calls to <code>do_init_module</code> at boot, presumably for the built-in modules):</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb do_one_initcall</pre>
</div>
</div>
<div class="paragraph">
<p>then step until the line:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>833 ret = fn();</pre>
</div>
</div>
<div class="paragraph">
<p>which does the actual call, and then step into it.</p>
</div>
<div class="paragraph">
<p>For the next time, you can also put a breakpoint there directly:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb init/main.c:833</pre>
</div>
</div>
<div class="paragraph">
<p>How we found this out: first we got <a href="#gdb-module_init-calculate-entry-address">GDB module_init calculate entry address</a> working, and then we did a <code>bt</code>. AKA cheating :-)</p>
</div>
</div>
<div class="sect4">
<h5 id="gdb-module_init-calculate-entry-address"><a class="anchor" href="#gdb-module_init-calculate-entry-address"></a><a class="link" href="#gdb-module_init-calculate-entry-address">2.4.2.2. GDB module_init calculate entry address</a></h5>
<div class="paragraph">
<p>This works, but is a bit annoying.</p>
</div>
<div class="paragraph">
<p>The key observation is that the load address of kernel modules is deterministic: there is a pre allocated memory region <a href="https://www.kernel.org/doc/Documentation/x86/x86_64/mm.txt" class="bare">https://www.kernel.org/doc/Documentation/x86/x86_64/mm.txt</a> "module mapping space" filled from bottom up.</p>
</div>
<div class="paragraph">
<p>So once we find the address the first time, we can just reuse it afterwards, as long as we don&#8217;t modify the module.</p>
</div>
<div class="paragraph">
<p>Do a fresh boot and get the module:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after './pr_debug.sh;insmod fops.ko;./linux/poweroff.out'</pre>
</div>
</div>
<div class="paragraph">
<p>The boot must be fresh, because the load address changes every time we insert, even after removing previous modules.</p>
</div>
<div class="paragraph">
<p>The base address shows on terminal:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0xffffffffc0000000 .text</pre>
</div>
</div>
<div class="paragraph">
<p>Now let&#8217;s find the offset of <code>myinit</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain readelf -- \
-s "$(./getvar kernel_modules_build_subdir)/fops.ko" | \
grep myinit</pre>
</div>
</div>
<div class="paragraph">
<p>which gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> 30: 0000000000000240 43 FUNC LOCAL DEFAULT 2 myinit</pre>
</div>
</div>
<div class="paragraph">
<p>so the offset address is <code>0x240</code> and we deduce that the function will be placed at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0xffffffffc0000000 + 0x240 = 0xffffffffc0000240</pre>
</div>
</div>
<div class="paragraph">
<p>Now we can just do a fresh boot on shell 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval 'insmod fops.ko;./linux/poweroff.out' --gdb-wait</pre>
</div>
</div>
<div class="paragraph">
<p>and on shell 2:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb '*0xffffffffc0000240'</pre>
</div>
</div>
<div class="paragraph">
<p>GDB then breaks, and <code>lx-symbols</code> works.</p>
</div>
</div>
<div class="sect4">
<h5 id="gdb-module_init-break-at-the-end-of-sys_init_module"><a class="anchor" href="#gdb-module_init-break-at-the-end-of-sys_init_module"></a><a class="link" href="#gdb-module_init-break-at-the-end-of-sys_init_module">2.4.2.3. GDB module_init break at the end of sys_init_module</a></h5>
<div class="paragraph">
<p>TODO not working. This could be potentially very convenient.</p>
</div>
<div class="paragraph">
<p>The idea here is to break at a point late enough inside <code>sys_init_module</code>, at which point <code>lx-symbols</code> can be called and do its magic.</p>
</div>
<div class="paragraph">
<p>Beware that there are both <code>sys_init_module</code> and <code>sys_finit_module</code> syscalls, and <code>insmod</code> uses <code>fmodule_init</code> by default.</p>
</div>
<div class="paragraph">
<p>Both call <code>do_module_init</code> however, which is what <code>lx-symbols</code> hooks to.</p>
</div>
<div class="paragraph">
<p>If we try:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>b sys_finit_module</pre>
</div>
</div>
<div class="paragraph">
<p>then hitting:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>n</pre>
</div>
</div>
<div class="paragraph">
<p>does not break, and insertion happens, likely because of optimizations? <a href="#kernel-o0">Disable kernel compiler optimizations</a></p>
</div>
<div class="paragraph">
<p>Then we try:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>b do_init_module</pre>
</div>
</div>
<div class="paragraph">
<p>A naive:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>fin</pre>
</div>
</div>
<div class="paragraph">
<p>also fails to break!</p>
</div>
<div class="paragraph">
<p>Finally, in despair we notice that <a href="#pr_debug">pr_debug</a> prints the kernel load address as explained at <a href="#bypass-lx-symbols">Bypass lx-symbols</a>.</p>
</div>
<div class="paragraph">
<p>So, if we set a breakpoint just after that message is printed by searching where that happens on the Linux source code, we must be able to get the correct load address before <code>init_module</code> happens.</p>
</div>
</div>
<div class="sect4">
<h5 id="gdb-module_init-add-trap-instruction"><a class="anchor" href="#gdb-module_init-add-trap-instruction"></a><a class="link" href="#gdb-module_init-add-trap-instruction">2.4.2.4. GDB module_init add trap instruction</a></h5>
<div class="paragraph">
<p>This is another possibility: we could modify the module source by adding a trap instruction of some kind.</p>
</div>
<div class="paragraph">
<p>This appears to be described at: <a href="https://www.linuxjournal.com/article/4525" class="bare">https://www.linuxjournal.com/article/4525</a></p>
</div>
<div class="paragraph">
<p>But it refers to a <code>gdbstart</code> script which is not in the tree anymore and beyond my <code>git log</code> capabilities.</p>
</div>
<div class="paragraph">
<p>And just adding:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>asm( " int $3");</pre>
</div>
</div>
<div class="paragraph">
<p>directly gives an <a href="#oops">oops</a> as I&#8217;d expect.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="bypass-lx-symbols"><a class="anchor" href="#bypass-lx-symbols"></a><a class="link" href="#bypass-lx-symbols">2.4.3. Bypass lx-symbols</a></h4>
<div class="paragraph">
<p>Useless, but a good way to show how hardcore you are. Disable <code>lx-symbols</code> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --no-lxsymbols</pre>
</div>
</div>
<div class="paragraph">
<p>From inside guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod timer.ko
cat /proc/modules</pre>
</div>
</div>
<div class="paragraph">
<p>as mentioned at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/6384605/how-to-get-address-of-a-kernel-module-loaded-using-insmod/6385818" class="bare">https://stackoverflow.com/questions/6384605/how-to-get-address-of-a-kernel-module-loaded-using-insmod/6385818</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/194405/get-base-address-and-size-of-a-loaded-kernel-module" class="bare">https://unix.stackexchange.com/questions/194405/get-base-address-and-size-of-a-loaded-kernel-module</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This will give a line of form:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>fops 2327 0 - Live 0xfffffffa00000000</pre>
</div>
</div>
<div class="paragraph">
<p>And then tell GDB where the module was loaded with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Ctrl-C
add-symbol-file ../../../rootfs_overlay/x86_64/timer.ko 0xffffffffc0000000
0xffffffffc0000000</pre>
</div>
</div>
<div class="paragraph">
<p>Alternatively, if the module panics before you can read <code>/proc/modules</code>, there is a <a href="#pr_debug">pr_debug</a> which shows the load address:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 8 &gt; /proc/sys/kernel/printk
echo 'file kernel/module.c +p' &gt; /sys/kernel/debug/dynamic_debug/control
./linux/myinsmod.out hello.ko</pre>
</div>
</div>
<div class="paragraph">
<p>And then search for a line of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[ 84.877482] 0xfffffffa00000000 .text</pre>
</div>
</div>
<div class="paragraph">
<p>Tested on 4f4749148273c282e80b58c59db1b47049e190bf + 1.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gdb-step-debug-early-boot"><a class="anchor" href="#gdb-step-debug-early-boot"></a><a class="link" href="#gdb-step-debug-early-boot">2.5. GDB step debug early boot</a></h3>
<div class="paragraph">
<p>TODO successfully debug the very first instruction that the Linux kernel runs, before <code>start_kernel</code>!</p>
</div>
<div class="paragraph">
<p>Break at the very first instruction executed by QEMU:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --no-continue</pre>
</div>
</div>
<div class="paragraph">
<p>TODO why can&#8217;t we break at early startup stuff such as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb extract_kernel
./run-gdb main</pre>
</div>
</div>
<div class="paragraph">
<p>Maybe it is because they are being copied around at specific locations instead of being run directly from inside the main image, which is where the debug information points to?</p>
</div>
<div class="paragraph">
<p>See also: <a href="https://stackoverflow.com/questions/2589845/what-are-the-first-operations-that-the-linux-kernel-executes-on-boot" class="bare">https://stackoverflow.com/questions/2589845/what-are-the-first-operations-that-the-linux-kernel-executes-on-boot</a></p>
</div>
<div class="paragraph">
<p><a href="#gem5-tracing">gem5 tracing</a> with <code>--debug-flags=Exec</code> does show the right symbols however! So in the worst case, we can just read their source. Amazing.</p>
</div>
<div class="paragraph">
<p>v4.19 also added a <code>CONFIG_HAVE_KERNEL_UNCOMPRESSED=y</code> option for having the kernel uncompressed which could make following the startup easier, but it is only available on s390. <code>aarch64</code> however is already uncompressed by default, so might be the easiest one. See also: <a href="#vmlinux-vs-bzimage-vs-zimage-vs-image">Section 15.21.1, &#8220;vmlinux vs bzImage vs zImage vs Image&#8221;</a>.</p>
</div>
<div class="sect3">
<h4 id="gdb-step-debug-early-boot-by-address"><a class="anchor" href="#gdb-step-debug-early-boot-by-address"></a><a class="link" href="#gdb-step-debug-early-boot-by-address">2.5.1. GDB step debug early boot by address</a></h4>
<div class="paragraph">
<p>One possibility is to run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./trace-boot --arch arm</pre>
</div>
</div>
<div class="paragraph">
<p>and then find the second address (the first one does not work, already too late maybe):</p>
</div>
<div class="literalblock">
<div class="content">
<pre>less "$(./getvar --arch arm trace_txt_file)"</pre>
</div>
</div>
<div class="paragraph">
<p>and break there:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --gdb-wait
./run-gdb --arch arm '*0x1000'</pre>
</div>
</div>
<div class="paragraph">
<p>but TODO: it does not show the source assembly under <code>arch/arm</code>: <a href="https://stackoverflow.com/questions/11423784/qemu-arm-linux-kernel-boot-debug-no-source-code" class="bare">https://stackoverflow.com/questions/11423784/qemu-arm-linux-kernel-boot-debug-no-source-code</a></p>
</div>
<div class="paragraph">
<p>I also tried to hack <code>run-gdb</code> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>@@ -81,7 +81,7 @@ else
${gdb} \
-q \\
-ex 'add-auto-load-safe-path $(pwd)' \\
--ex 'file vmlinux' \\
+-ex 'file arch/arm/boot/compressed/vmlinux' \\
-ex 'target remote localhost:${port}' \\
${brk} \
-ex 'continue' \\</pre>
</div>
</div>
<div class="paragraph">
<p>and no I do have the symbols from <code>arch/arm/boot/compressed/vmlinux'</code>, but the breaks still don&#8217;t work.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gdb-step-debug-userland-processes"><a class="anchor" href="#gdb-step-debug-userland-processes"></a><a class="link" href="#gdb-step-debug-userland-processes">2.6. GDB step debug userland processes</a></h3>
<div class="paragraph">
<p>QEMU&#8217;s <code>-gdb</code> GDB breakpoints are set on virtual addresses, so you can in theory debug userland processes as well.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/26271901/is-it-possible-to-use-gdb-and-qemu-to-debug-linux-user-space-programs-and-kernel" class="bare">https://stackoverflow.com/questions/26271901/is-it-possible-to-use-gdb-and-qemu-to-debug-linux-user-space-programs-and-kernel</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/16273614/debug-init-on-qemu-using-gdb" class="bare">https://stackoverflow.com/questions/16273614/debug-init-on-qemu-using-gdb</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>You will generally want to use <a href="#gdbserver">gdbserver</a> for this as it is more reliable, but this method can overcome the following limitations of <code>gdbserver</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the emulator does not support host to guest networking. This seems to be the case for gem5 as explained at: <a href="#gem5-host-to-guest-networking">Section 14.3.1.3, &#8220;gem5 host to guest networking&#8221;</a></p>
</li>
<li>
<p>cannot see the start of the <code>init</code> process easily</p>
</li>
<li>
<p><code>gdbserver</code> alters the working of the kernel, and makes your run less representative</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Known limitations of direct userland debugging:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the kernel might switch context to another process or to the kernel itself e.g. on a system call, and then TODO confirm the PIC would go to weird places and source code would be missing.</p>
<div class="paragraph">
<p>Solutions to this are being researched at: <a href="#lx-ps">Section 2.10.1, &#8220;lx-ps&#8221;</a>.</p>
</div>
</li>
<li>
<p>TODO step into shared libraries. If I attempt to load them explicitly:</p>
<div class="literalblock">
<div class="content">
<pre>(gdb) sharedlibrary ../../staging/lib/libc.so.0
No loaded shared libraries match the pattern `../../staging/lib/libc.so.0'.</pre>
</div>
</div>
<div class="paragraph">
<p>since GDB does not know that libc is loaded.</p>
</div>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="gdb-step-debug-userland-custom-init"><a class="anchor" href="#gdb-step-debug-userland-custom-init"></a><a class="link" href="#gdb-step-debug-userland-custom-init">2.6.1. GDB step debug userland custom init</a></h4>
<div class="paragraph">
<p>This is the userland debug setup most likely to work, since at init time there is only one userland executable running.</p>
</div>
<div class="paragraph">
<p>For executables from the <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/">userland/</a> directory such as <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/count.c">userland/posix/count.c</a>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Shell 1:</p>
<div class="literalblock">
<div class="content">
<pre>./run --gdb-wait --kernel-cli 'init=/lkmc/posix/count.out'</pre>
</div>
</div>
</li>
<li>
<p>Shell 2:</p>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --userland userland/posix/count.c main</pre>
</div>
</div>
<div class="paragraph">
<p>Alternatively, we could also pass the full path to the executable:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --userland "$(./getvar userland_build_dir)/posix/count.out" main</pre>
</div>
</div>
<div class="paragraph">
<p>Path resolution is analogous to <a href="#baremetal-setup-getting-started">that of <code>./run --baremetal</code></a>.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Then, as soon as boot ends, we are left inside a debug session that looks just like what <code>gdbserver</code> would produce.</p>
</div>
</div>
<div class="sect3">
<h4 id="gdb-step-debug-userland-busybox-init"><a class="anchor" href="#gdb-step-debug-userland-busybox-init"></a><a class="link" href="#gdb-step-debug-userland-busybox-init">2.6.2. GDB step debug userland BusyBox init</a></h4>
<div class="paragraph">
<p>BusyBox custom init process:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Shell 1:</p>
<div class="literalblock">
<div class="content">
<pre>./run --gdb-wait --kernel-cli 'init=/bin/ls'</pre>
</div>
</div>
</li>
<li>
<p>Shell 2:</p>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --userland "$(./getvar buildroot_build_build_dir)"/busybox-*/busybox ls_main</pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>This follows BusyBox' convention of calling the main for each executable as <code>&lt;exec&gt;_main</code> since the <code>busybox</code> executable has many "mains".</p>
</div>
<div class="paragraph">
<p>BusyBox default init process:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Shell 1:</p>
<div class="literalblock">
<div class="content">
<pre>./run --gdb-wait</pre>
</div>
</div>
</li>
<li>
<p>Shell 2:</p>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --userland "$(./getvar buildroot_build_build_dir)"/busybox-*/busybox init_main</pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p><code>init</code> cannot be debugged with <a href="#gdbserver">gdbserver</a> without modifying the source, or else <code>/sbin/init</code> exits early with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>"must be run as PID 1"</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="gdb-step-debug-userland-non-init"><a class="anchor" href="#gdb-step-debug-userland-non-init"></a><a class="link" href="#gdb-step-debug-userland-non-init">2.6.3. GDB step debug userland non-init</a></h4>
<div class="paragraph">
<p>Non-init process:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Shell 1:</p>
<div class="literalblock">
<div class="content">
<pre>./run --gdb-wait</pre>
</div>
</div>
</li>
<li>
<p>Shell 2:</p>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --userland userland/linux/rand_check.c main</pre>
</div>
</div>
</li>
<li>
<p>Shell 1 after the boot finishes:</p>
<div class="literalblock">
<div class="content">
<pre>./linux/rand_check.out</pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>This is the least reliable setup as there might be other processes that use the given virtual address.</p>
</div>
<div class="sect4">
<h5 id="gdb-step-debug-userland-non-init-without-gdb-wait"><a class="anchor" href="#gdb-step-debug-userland-non-init-without-gdb-wait"></a><a class="link" href="#gdb-step-debug-userland-non-init-without-gdb-wait">2.6.3.1. GDB step debug userland non-init without --gdb-wait</a></h5>
<div class="paragraph">
<p>TODO: if I try <a href="#gdb-step-debug-userland-non-init">GDB step debug userland non-init</a> without <code>--gdb-wait</code> and the <code>break main</code> that we do inside <code>./run-gdb</code> says:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Cannot access memory at address 0x10604</pre>
</div>
</div>
<div class="paragraph">
<p>and then GDB never breaks. Tested at ac8663a44a450c3eadafe14031186813f90c21e4 + 1.</p>
</div>
<div class="paragraph">
<p>The exact behaviour seems to depend on the architecture:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>arm</code>: happens always</p>
</li>
<li>
<p><code>x86_64</code>: appears to happen only if you try to connect GDB as fast as possible, before init has been reached.</p>
</li>
<li>
<p><code>aarch64</code>: could not observe the problem</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We have also double checked the address with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain --arch arm readelf -- \
-s "$(./getvar --arch arm userland_build_dir)/linux/myinsmod.out" | \
grep main</pre>
</div>
</div>
<div class="paragraph">
<p>and from GDB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>info line main</pre>
</div>
</div>
<div class="paragraph">
<p>and both give:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>000105fc</pre>
</div>
</div>
<div class="paragraph">
<p>which is just 8 bytes before <code>0x10604</code>.</p>
</div>
<div class="paragraph">
<p><code>gdbserver</code> also says <code>0x10604</code>.</p>
</div>
<div class="paragraph">
<p>However, if do a <code>Ctrl-C</code> in GDB, and then a direct:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>b *0x000105fc</pre>
</div>
</div>
<div class="paragraph">
<p>it works. Why?!</p>
</div>
<div class="paragraph">
<p>On GEM5, x86 can also give the <code>Cannot access memory at address</code>, so maybe it is also unreliable on QEMU, and works just by coincidence.</p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gdb-call"><a class="anchor" href="#gdb-call"></a><a class="link" href="#gdb-call">2.7. GDB call</a></h3>
<div class="paragraph">
<p>GDB can call functions as explained at: <a href="https://stackoverflow.com/questions/1354731/how-to-evaluate-functions-in-gdb" class="bare">https://stackoverflow.com/questions/1354731/how-to-evaluate-functions-in-gdb</a></p>
</div>
<div class="paragraph">
<p>However this is failing for us:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>some symbols are not visible to <code>call</code> even though <code>b</code> sees them</p>
</li>
<li>
<p>for those that are, <code>call</code> fails with an E14 error</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>E.g.: if we break on <code>__x64_sys_write</code> on <code>count.sh</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&gt;&gt;&gt; call printk(0, "asdf")
Could not fetch register "orig_rax"; remote failure reply 'E14'
&gt;&gt;&gt; b printk
Breakpoint 2 at 0xffffffff81091bca: file kernel/printk/printk.c, line 1824.
&gt;&gt;&gt; call fdget_pos(fd)
No symbol "fdget_pos" in current context.
&gt;&gt;&gt; b fdget_pos
Breakpoint 3 at 0xffffffff811615e3: fdget_pos. (9 locations)
&gt;&gt;&gt;</pre>
</div>
</div>
<div class="paragraph">
<p>even though <code>fdget_pos</code> is the first thing <code>__x64_sys_write</code> does:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>581 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
582 size_t, count)
583 {
584 struct fd f = fdget_pos(fd);</pre>
</div>
</div>
<div class="paragraph">
<p>I also noticed that I get the same error:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Could not fetch register "orig_rax"; remote failure reply 'E14'</pre>
</div>
</div>
<div class="paragraph">
<p>when trying to use:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>fin</pre>
</div>
</div>
<div class="paragraph">
<p>on many (all?) functions.</p>
</div>
<div class="paragraph">
<p>See also: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/issues/19" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/issues/19</a></p>
</div>
</div>
<div class="sect2">
<h3 id="gdb-view-arm-system-registers"><a class="anchor" href="#gdb-view-arm-system-registers"></a><a class="link" href="#gdb-view-arm-system-registers">2.8. GDB view ARM system registers</a></h3>
<div class="paragraph">
<p><code>info all-registers</code> shows some of them.</p>
</div>
<div class="paragraph">
<p>The implementation is described at: <a href="https://stackoverflow.com/questions/46415059/how-to-observe-aarch64-system-registers-in-qemu/53043044#53043044" class="bare">https://stackoverflow.com/questions/46415059/how-to-observe-aarch64-system-registers-in-qemu/53043044#53043044</a></p>
</div>
</div>
<div class="sect2">
<h3 id="gdb-step-debug-multicore-userland"><a class="anchor" href="#gdb-step-debug-multicore-userland"></a><a class="link" href="#gdb-step-debug-multicore-userland">2.9. GDB step debug multicore userland</a></h3>
<div class="paragraph">
<p>For a more minimal baremetal multicore setup, see: <a href="#arm-multicore">Section 27.8.3, &#8220;ARM multicore&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>We can set and get which cores the Linux kernel allows a program to run on with <code>sched_getaffinity</code> and <code>sched_setaffinity</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --cpus 2 --eval-after './linux/sched_getaffinity.out'</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/sched_getaffinity.c">userland/linux/sched_getaffinity.c</a></p>
</div>
<div class="paragraph">
<p>Sample output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sched_getaffinity = 1 1
sched_getcpu = 1
sched_getaffinity = 1 0
sched_getcpu = 0</pre>
</div>
</div>
<div class="paragraph">
<p>Which shows us that:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>initially:</p>
<div class="ulist">
<ul>
<li>
<p>all 2 cores were enabled as shown by <code>sched_getaffinity = 1 1</code></p>
</li>
<li>
<p>the process was randomly assigned to run on core 1 (the second one) as shown by <code>sched_getcpu = 1</code>. If we run this several times, it will also run on core 0 sometimes.</p>
</li>
</ul>
</div>
</li>
<li>
<p>then we restrict the affinity to just core 0, and we see that the program was actually moved to core 0</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The number of cores is modified as explained at: <a href="#number-of-cores">Section 19.2.2.1, &#8220;Number of cores&#8221;</a></p>
</div>
<div class="paragraph">
<p><code>taskset</code> from the util-linux package sets the initial core affinity of a program:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot \
--config 'BR2_PACKAGE_UTIL_LINUX=y' \
--config 'BR2_PACKAGE_UTIL_LINUX_SCHEDUTILS=y' \
;
./run --eval-after 'taskset -c 1,1 ./linux/sched_getaffinity.out'</pre>
</div>
</div>
<div class="paragraph">
<p>output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sched_getaffinity = 0 1
sched_getcpu = 1
sched_getaffinity = 1 0
sched_getcpu = 0</pre>
</div>
</div>
<div class="paragraph">
<p>so we see that the affinity was restricted to the second core from the start.</p>
</div>
<div class="paragraph">
<p>Let&#8217;s do a QEMU observation to justify this example being in the repository with <a href="#gdb-step-debug-userland-non-init">userland breakpoints</a>.</p>
</div>
<div class="paragraph">
<p>We will run our <code>./linux/sched_getaffinity.out</code> infinitely many times, on core 0 and core 1 alternatively:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--cpus 2 \
--eval-after 'i=0; while true; do taskset -c $i,$i ./linux/sched_getaffinity.out; i=$((! $i)); done' \
--gdb-wait \
;</pre>
</div>
</div>
<div class="paragraph">
<p>on another shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --userland "$(./getvar userland_build_dir)/linux/sched_getaffinity.out" main</pre>
</div>
</div>
<div class="paragraph">
<p>Then, inside GDB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>(gdb) info threads
Id Target Id Frame
* 1 Thread 1 (CPU#0 [running]) main () at sched_getaffinity.c:30
2 Thread 2 (CPU#1 [halted ]) native_safe_halt () at ./arch/x86/include/asm/irqflags.h:55
(gdb) c
(gdb) info threads
Id Target Id Frame
1 Thread 1 (CPU#0 [halted ]) native_safe_halt () at ./arch/x86/include/asm/irqflags.h:55
* 2 Thread 2 (CPU#1 [running]) main () at sched_getaffinity.c:30
(gdb) c</pre>
</div>
</div>
<div class="paragraph">
<p>and we observe that <code>info threads</code> shows the actual correct core on which the process was restricted to run by <code>taskset</code>!</p>
</div>
<div class="paragraph">
<p>We should also try it out with kernel modules: <a href="https://stackoverflow.com/questions/28347876/set-cpu-affinity-on-a-loadable-linux-kernel-module" class="bare">https://stackoverflow.com/questions/28347876/set-cpu-affinity-on-a-loadable-linux-kernel-module</a></p>
</div>
<div class="paragraph">
<p>TODO we then tried:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --cpus 2 --eval-after './linux/sched_getaffinity_threads.out'</pre>
</div>
</div>
<div class="paragraph">
<p>and:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --userland "$(./getvar userland_build_dir)/linux/sched_getaffinity_threads.out"</pre>
</div>
</div>
<div class="paragraph">
<p>to switch between two simultaneous live threads with different affinities, it just didn&#8217;t break on our threads:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>b main_thread_0</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/10490756/how-to-use-sched-getaffinity-and-sched-setaffinity-in-linux-from-c/50117787#50117787" class="bare">https://stackoverflow.com/questions/10490756/how-to-use-sched-getaffinity-and-sched-setaffinity-in-linux-from-c/50117787#50117787</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/42800801/how-to-use-gdb-to-debug-qemu-with-smp-symmetric-multiple-processors" class="bare">https://stackoverflow.com/questions/42800801/how-to-use-gdb-to-debug-qemu-with-smp-symmetric-multiple-processors</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="linux-kernel-gdb-scripts"><a class="anchor" href="#linux-kernel-gdb-scripts"></a><a class="link" href="#linux-kernel-gdb-scripts">2.10. Linux kernel GDB scripts</a></h3>
<div class="paragraph">
<p>We source the Linux kernel GDB scripts by default for <code>lx-symbols</code>, but they also contains some other goodies worth looking into.</p>
</div>
<div class="paragraph">
<p>Those scripts basically parse some in-kernel data structures to offer greater visibility with GDB.</p>
</div>
<div class="paragraph">
<p>All defined commands are prefixed by <code>lx-</code>, so to get a full list just try to tab complete that.</p>
</div>
<div class="paragraph">
<p>There aren&#8217;t as many as I&#8217;d like, and the ones that do exist are pretty self explanatory, but let&#8217;s give a few examples.</p>
</div>
<div class="paragraph">
<p>Show dmesg:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lx-dmesg</pre>
</div>
</div>
<div class="paragraph">
<p>Show the <a href="#kernel-command-line-parameters">Kernel command line parameters</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lx-cmdline</pre>
</div>
</div>
<div class="paragraph">
<p>Dump the device tree to a <code>fdtdump.dtb</code> file in the current directory:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lx-fdtdump
pwd</pre>
</div>
</div>
<div class="paragraph">
<p>List inserted kernel modules:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lx-lsmod</pre>
</div>
</div>
<div class="paragraph">
<p>Sample output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Address Module Size Used by
0xffffff80006d0000 hello 16384 0</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://events.static.linuxfound.org/sites/events/files/slides/Debugging%20the%20Linux%20Kernel%20with%20GDB.pdf" class="bare">https://events.static.linuxfound.org/sites/events/files/slides/Debugging%20the%20Linux%20Kernel%20with%20GDB.pdf</a></p>
</li>
<li>
<p><a href="https://wiki.linaro.org/LandingTeams/ST/GDB" class="bare">https://wiki.linaro.org/LandingTeams/ST/GDB</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="lx-ps"><a class="anchor" href="#lx-ps"></a><a class="link" href="#lx-ps">2.10.1. lx-ps</a></h4>
<div class="paragraph">
<p>List all processes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lx-ps</pre>
</div>
</div>
<div class="paragraph">
<p>Sample output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0xffff88000ed08000 1 init
0xffff88000ed08ac0 2 kthreadd</pre>
</div>
</div>
<div class="paragraph">
<p>The second and third fields are obviously PID and process name.</p>
</div>
<div class="paragraph">
<p>The first one is more interesting, and contains the address of the <code>task_struct</code> in memory.</p>
</div>
<div class="paragraph">
<p>This can be confirmed with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>p ((struct task_struct)*0xffff88000ed08000</pre>
</div>
</div>
<div class="paragraph">
<p>which contains the correct PID for all threads I&#8217;ve tried:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>pid = 1,</pre>
</div>
</div>
<div class="paragraph">
<p>TODO get the PC of the kthreads: <a href="https://stackoverflow.com/questions/26030910/find-program-counter-of-process-in-kernel" class="bare">https://stackoverflow.com/questions/26030910/find-program-counter-of-process-in-kernel</a> Then we would be able to see where the threads are stopped in the code!</p>
</div>
<div class="paragraph">
<p>On ARM, I tried:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>task_pt_regs((struct thread_info *)((struct task_struct)*0xffffffc00e8f8000))-&gt;uregs[ARM_pc]</pre>
</div>
</div>
<div class="paragraph">
<p>but <code>task_pt_regs</code> is a <code>#define</code> and GDB cannot see defines without <code>-ggdb3</code>: <a href="https://stackoverflow.com/questions/2934006/how-do-i-print-a-defined-constant-in-gdb" class="bare">https://stackoverflow.com/questions/2934006/how-do-i-print-a-defined-constant-in-gdb</a> which are apparently not set?</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/9561546/thread-aware-gdb-for-kernel" class="bare">https://stackoverflow.com/questions/9561546/thread-aware-gdb-for-kernel</a></p>
</li>
<li>
<p><a href="https://wiki.linaro.org/LandingTeams/ST/GDB" class="bare">https://wiki.linaro.org/LandingTeams/ST/GDB</a></p>
</li>
<li>
<p><a href="https://events.static.linuxfound.org/sites/events/files/slides/Debugging%20the%20Linux%20Kernel%20with%20GDB.pdf" class="bare">https://events.static.linuxfound.org/sites/events/files/slides/Debugging%20the%20Linux%20Kernel%20with%20GDB.pdf</a> presentation: <a href="https://www.youtube.com/watch?v=pqn5hIrz3A8" class="bare">https://www.youtube.com/watch?v=pqn5hIrz3A8</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="debug-the-gdb-remote-protocol"><a class="anchor" href="#debug-the-gdb-remote-protocol"></a><a class="link" href="#debug-the-gdb-remote-protocol">2.11. Debug the GDB remote protocol</a></h3>
<div class="paragraph">
<p>For when it breaks again, or you want to add a new feature!</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --debug
./run-gdb --before '-ex "set remotetimeout 99999" -ex "set debug remote 1"' start_kernel</pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="https://stackoverflow.com/questions/13496389/gdb-remote-protocol-how-to-analyse-packets" class="bare">https://stackoverflow.com/questions/13496389/gdb-remote-protocol-how-to-analyse-packets</a></p>
</div>
<div class="sect3">
<h4 id="remote-g-packet"><a class="anchor" href="#remote-g-packet"></a><a class="link" href="#remote-g-packet">2.11.1. Remote 'g' packet reply is too long</a></h4>
<div class="paragraph">
<p>This error means that the GDB server, e.g. in QEMU, sent more registers than the GDB client expected.</p>
</div>
<div class="paragraph">
<p>This can happen for the following reasons:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>you set the architecture of the client wrong, often 32 vs 64 bit as mentioned at: <a href="https://stackoverflow.com/questions/4896316/gdb-remote-cross-debugging-fails-with-remote-g-packet-reply-is-too-long" class="bare">https://stackoverflow.com/questions/4896316/gdb-remote-cross-debugging-fails-with-remote-g-packet-reply-is-too-long</a></p>
</li>
<li>
<p>there is a bug in the GDB server and the XML description does not match the number of registers actually sent</p>
</li>
<li>
<p>the GDB server does not send XML target descriptions and your GDB expects a different number of registers by default. E.g., gem5 d4b3e064adeeace3c3e7d106801f95c14637c12f does not send the XML files</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The XML target description format is described a bit further at: <a href="https://stackoverflow.com/questions/46415059/how-to-observe-aarch64-system-registers-in-qemu/53043044#53043044" class="bare">https://stackoverflow.com/questions/46415059/how-to-observe-aarch64-system-registers-in-qemu/53043044#53043044</a></p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="kgdb"><a class="anchor" href="#kgdb"></a><a class="link" href="#kgdb">3. KGDB</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>KGDB is kernel dark magic that allows you to GDB the kernel on real hardware without any extra hardware support.</p>
</div>
<div class="paragraph">
<p>It is useless with QEMU since we already have full system visibility with <code>-gdb</code>. So the goal of this setup is just to prepare you for what to expect when you will be in the treches of real hardware.</p>
</div>
<div class="paragraph">
<p>KGDB is cheaper than JTAG (free) and easier to setup (all you need is serial), but with less visibility as it depends on the kernel working, so e.g.: dies on panic, does not see boot sequence.</p>
</div>
<div class="paragraph">
<p>First run the kernel with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kgdb</pre>
</div>
</div>
<div class="paragraph">
<p>this passes the following options on the kernel CLI:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>kgdbwait kgdboc=ttyS1,115200</pre>
</div>
</div>
<div class="paragraph">
<p><code>kgdbwait</code> tells the kernel to wait for KGDB to connect.</p>
</div>
<div class="paragraph">
<p>So the kernel sets things up enough for KGDB to start working, and then boot pauses waiting for connection:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;6&gt;[ 4.866050] Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled
&lt;6&gt;[ 4.893205] 00:05: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A
&lt;6&gt;[ 4.916271] 00:06: ttyS1 at I/O 0x2f8 (irq = 3, base_baud = 115200) is a 16550A
&lt;6&gt;[ 4.987771] KGDB: Registered I/O driver kgdboc
&lt;2&gt;[ 4.996053] KGDB: Waiting for connection from remote gdb...
Entering kdb (current=0x(____ptrval____), pid 1) on processor 0 due to Keyboard Entry
[0]kdb&gt;</pre>
</div>
</div>
<div class="paragraph">
<p>KGDB expects the connection at <code>ttyS1</code>, our second serial port after <code>ttyS0</code> which contains the terminal.</p>
</div>
<div class="paragraph">
<p>The last line is the KDB prompt, and is covered at: <a href="#kdb">Section 3.3, &#8220;KDB&#8221;</a>. Typing now shows nothing because that prompt is expecting input from <code>ttyS1</code>.</p>
</div>
<div class="paragraph">
<p>Instead, we connect to the serial port <code>ttyS1</code> with GDB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --kgdb --no-continue</pre>
</div>
</div>
<div class="paragraph">
<p>Once GDB connects, it is left inside the function <code>kgdb_breakpoint</code>.</p>
</div>
<div class="paragraph">
<p>So now we can set breakpoints and continue as usual.</p>
</div>
<div class="paragraph">
<p>For example, in GDB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>continue</pre>
</div>
</div>
<div class="paragraph">
<p>Then in QEMU:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./count.sh &amp;
./kgdb.sh</pre>
</div>
</div>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/kgdb.sh">rootfs_overlay/lkmc/kgdb.sh</a> pauses the kernel for KGDB, and gives control back to GDB.</p>
</div>
<div class="paragraph">
<p>And now in GDB we do the usual:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>break __x64_sys_write
continue
continue
continue
continue</pre>
</div>
</div>
<div class="paragraph">
<p>And now you can count from KGDB!</p>
</div>
<div class="paragraph">
<p>If you do: <code>break __x64_sys_write</code> immediately after <code>./run-gdb --kgdb</code>, it fails with <code>KGDB: BP remove failed: &lt;address&gt;</code>. I think this is because it would break too early on the boot sequence, and KGDB is not yet ready.</p>
</div>
<div class="paragraph">
<p>See also:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/torvalds/linux/blob/v4.9/Documentation/DocBook/kgdb.tmpl" class="bare">https://github.com/torvalds/linux/blob/v4.9/Documentation/DocBook/kgdb.tmpl</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/22004616/qemu-kernel-debugging-with-kgdb/44197715#44197715" class="bare">https://stackoverflow.com/questions/22004616/qemu-kernel-debugging-with-kgdb/44197715#44197715</a></p>
</li>
</ul>
</div>
<div class="sect2">
<h3 id="kgdb-arm"><a class="anchor" href="#kgdb-arm"></a><a class="link" href="#kgdb-arm">3.1. KGDB ARM</a></h3>
<div class="paragraph">
<p>TODO: we would need a second serial for KGDB to work, but it is not currently supported on <code>arm</code> and <code>aarch64</code> with <code>-M virt</code> that we use: <a href="https://unix.stackexchange.com/questions/479085/can-qemu-m-virt-on-arm-aarch64-have-multiple-serial-ttys-like-such-as-pl011-t/479340#479340" class="bare">https://unix.stackexchange.com/questions/479085/can-qemu-m-virt-on-arm-aarch64-have-multiple-serial-ttys-like-such-as-pl011-t/479340#479340</a></p>
</div>
<div class="paragraph">
<p>One possible workaround for this would be to use <a href="#kdb-arm">KDB ARM</a>.</p>
</div>
<div class="paragraph">
<p>Main more generic question: <a href="https://stackoverflow.com/questions/14155577/how-to-use-kgdb-on-arm" class="bare">https://stackoverflow.com/questions/14155577/how-to-use-kgdb-on-arm</a></p>
</div>
</div>
<div class="sect2">
<h3 id="kgdb-kernel-modules"><a class="anchor" href="#kgdb-kernel-modules"></a><a class="link" href="#kgdb-kernel-modules">3.2. KGDB kernel modules</a></h3>
<div class="paragraph">
<p>Just works as you would expect:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod timer.ko
./kgdb.sh</pre>
</div>
</div>
<div class="paragraph">
<p>In GDB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>break lkmc_timer_callback
continue
continue
continue</pre>
</div>
</div>
<div class="paragraph">
<p>and you now control the count.</p>
</div>
</div>
<div class="sect2">
<h3 id="kdb"><a class="anchor" href="#kdb"></a><a class="link" href="#kdb">3.3. KDB</a></h3>
<div class="paragraph">
<p>KDB is a way to use KDB directly in your main console, without GDB.</p>
</div>
<div class="paragraph">
<p>Advantage over KGDB: you can do everything in one serial. This can actually be important if you only have one serial for both shell and .</p>
</div>
<div class="paragraph">
<p>Disadvantage: not as much functionality as GDB, especially when you use Python scripts. Notably, TODO confirm you can&#8217;t see the the kernel source code and line step as from GDB, since the kernel source is not available on guest (ah, if only debugging information supported full source, or if the kernel had a crazy mechanism to embed it).</p>
</div>
<div class="paragraph">
<p>Run QEMU as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kdb</pre>
</div>
</div>
<div class="paragraph">
<p>This passes <code>kgdboc=ttyS0</code> to the Linux CLI, therefore using our main console. Then QEMU:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[0]kdb&gt; go</pre>
</div>
</div>
<div class="paragraph">
<p>And now the <code>kdb&gt;</code> prompt is responsive because it is listening to the main console.</p>
</div>
<div class="paragraph">
<p>After boot finishes, run the usual:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./count.sh &amp;
./kgdb.sh</pre>
</div>
</div>
<div class="paragraph">
<p>And you are back in KDB. Now you can count with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[0]kdb&gt; bp __x64_sys_write
[0]kdb&gt; go
[0]kdb&gt; go
[0]kdb&gt; go
[0]kdb&gt; go</pre>
</div>
</div>
<div class="paragraph">
<p>And you will break whenever <code>__x64_sys_write</code> is hit.</p>
</div>
<div class="paragraph">
<p>You can get see further commands with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[0]kdb&gt; help</pre>
</div>
</div>
<div class="paragraph">
<p>The other KDB commands allow you to step instructions, view memory, registers and some higher level kernel runtime data similar to the superior GDB Python scripts.</p>
</div>
<div class="sect3">
<h4 id="kdb-graphic"><a class="anchor" href="#kdb-graphic"></a><a class="link" href="#kdb-graphic">3.3.1. KDB graphic</a></h4>
<div class="paragraph">
<p>You can also use KDB directly from the <a href="#graphics">graphic</a> window with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --graphic --kdb</pre>
</div>
</div>
<div class="paragraph">
<p>This setup could be used to debug the kernel on machines without serial, such as modern desktops.</p>
</div>
<div class="paragraph">
<p>This works because <code>--graphics</code> adds <code>kbd</code> (which stands for <code>KeyBoarD</code>!) to <code>kgdboc</code>.</p>
</div>
</div>
<div class="sect3">
<h4 id="kdb-arm"><a class="anchor" href="#kdb-arm"></a><a class="link" href="#kdb-arm">3.3.2. KDB ARM</a></h4>
<div class="paragraph">
<p>TODO neither <code>arm</code> and <code>aarch64</code> are working as of 1cd1e58b023791606498ca509256cc48e95e4f5b + 1.</p>
</div>
<div class="paragraph">
<p><code>arm</code> seems to place and hit the breakpoint correctly, but no matter how many <code>go</code> commands I do, the <code>count.sh</code> stdout simply does not show.</p>
</div>
<div class="paragraph">
<p><code>aarch64</code> seems to place the breakpoint correctly, but after the first <code>go</code> the kernel oopses with warning:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>WARNING: CPU: 0 PID: 46 at /root/linux-kernel-module-cheat/submodules/linux/kernel/smp.c:416 smp_call_function_many+0xdc/0x358</pre>
</div>
</div>
<div class="paragraph">
<p>and stack trace:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>smp_call_function_many+0xdc/0x358
kick_all_cpus_sync+0x30/0x38
kgdb_flush_swbreak_addr+0x3c/0x48
dbg_deactivate_sw_breakpoints+0x7c/0xb8
kgdb_cpu_enter+0x284/0x6a8
kgdb_handle_exception+0x138/0x240
kgdb_brk_fn+0x2c/0x40
brk_handler+0x7c/0xc8
do_debug_exception+0xa4/0x1c0
el1_dbg+0x18/0x78
__arm64_sys_write+0x0/0x30
el0_svc_handler+0x74/0x90
el0_svc+0x8/0xc</pre>
</div>
</div>
<div class="paragraph">
<p>My theory is that every serious ARM developer has JTAG, and no one ever tests this, and the kernel code is just broken.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="gdbserver"><a class="anchor" href="#gdbserver"></a><a class="link" href="#gdbserver">4. gdbserver</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Step debug userland processes to understand how they are talking to the kernel.</p>
</div>
<div class="paragraph">
<p>First build <code>gdbserver</code> into the root filesystem:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_GDB=y'</pre>
</div>
</div>
<div class="paragraph">
<p>Then on guest, to debug <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/rand_check.c">userland/linux/rand_check.c</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gdbserver.sh ./c/print_argv.out asdf qwer</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/gdbserver.sh">rootfs_overlay/lkmc/gdbserver.sh</a>.</p>
</div>
<div class="paragraph">
<p>And on host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --gdbserver --userland userland/c/command_line_arguments.c main</pre>
</div>
</div>
<div class="paragraph">
<p>or alternatively with the path to the executable itself:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --gdbserver --userland "$(./getvar userland_build_dir)/c/print_argv.out"</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://reverseengineering.stackexchange.com/questions/8829/cross-debugging-for-arm-mips-elf-with-qemu-toolchain/16214#16214" class="bare">https://reverseengineering.stackexchange.com/questions/8829/cross-debugging-for-arm-mips-elf-with-qemu-toolchain/16214#16214</a></p>
</div>
<div class="sect2">
<h3 id="gdbserver-busybox"><a class="anchor" href="#gdbserver-busybox"></a><a class="link" href="#gdbserver-busybox">4.1. gdbserver BusyBox</a></h3>
<div class="paragraph">
<p>Analogous to <a href="#gdb-step-debug-userland-processes">GDB step debug userland processes</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gdbserver.sh ls</pre>
</div>
</div>
<div class="paragraph">
<p>on host you need:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --gdbserver --userland "$(./getvar buildroot_build_build_dir)"/busybox-*/busybox ls_main</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gdbserver-libc"><a class="anchor" href="#gdbserver-libc"></a><a class="link" href="#gdbserver-libc">4.2. gdbserver libc</a></h3>
<div class="paragraph">
<p>Our setup gives you the rare opportunity to step debug libc and other system libraries.</p>
</div>
<div class="paragraph">
<p>For example in the guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gdbserver.sh ./posix/count.out</pre>
</div>
</div>
<div class="paragraph">
<p>Then on host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --gdbserver --userland userland/posix/count.c main</pre>
</div>
</div>
<div class="paragraph">
<p>and inside GDB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>break sleep
continue</pre>
</div>
</div>
<div class="paragraph">
<p>And you are now left inside the <code>sleep</code> function of our default libc implementation uclibc <a href="https://cgit.uclibc-ng.org/cgi/cgit/uclibc-ng.git/tree/libc/unistd/sleep.c?h=v1.0.30#n91"><code>libc/unistd/sleep.c</code></a>!</p>
</div>
<div class="paragraph">
<p>You can also step into the <code>sleep</code> call:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>step</pre>
</div>
</div>
<div class="paragraph">
<p>This is made possible by the GDB command that we use by default:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>set sysroot ${common_buildroot_build_dir}/staging</pre>
</div>
</div>
<div class="paragraph">
<p>which automatically finds unstripped shared libraries on the host for us.</p>
</div>
<div class="paragraph">
<p>See also: <a href="https://stackoverflow.com/questions/8611194/debugging-shared-libraries-with-gdbserver/45252113#45252113" class="bare">https://stackoverflow.com/questions/8611194/debugging-shared-libraries-with-gdbserver/45252113#45252113</a></p>
</div>
</div>
<div class="sect2">
<h3 id="gdbserver-dynamic-loader"><a class="anchor" href="#gdbserver-dynamic-loader"></a><a class="link" href="#gdbserver-dynamic-loader">4.3. gdbserver dynamic loader</a></h3>
<div class="paragraph">
<p>TODO: try to step debug the dynamic loader. Would be even easier if <code>starti</code> is available: <a href="https://stackoverflow.com/questions/10483544/stopping-at-the-first-machine-code-instruction-in-gdb" class="bare">https://stackoverflow.com/questions/10483544/stopping-at-the-first-machine-code-instruction-in-gdb</a></p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/20114565/gdb-step-into-dynamic-linkerld-so-code" class="bare">https://stackoverflow.com/questions/20114565/gdb-step-into-dynamic-linkerld-so-code</a></p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="cpu-architecture"><a class="anchor" href="#cpu-architecture"></a><a class="link" href="#cpu-architecture">5. CPU architecture</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>The portability of the kernel and toolchains is amazing: change an option and most things magically work on completely different hardware.</p>
</div>
<div class="paragraph">
<p>To use <code>arm</code> instead of x86 for example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --arch arm
./run --arch arm</pre>
</div>
</div>
<div class="paragraph">
<p>Debug:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --gdb-wait
# On another terminal.
./run-gdb --arch arm</pre>
</div>
</div>
<div class="paragraph">
<p>We also have one letter shorthand names for the architectures and <code>--arch</code> option:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># aarch64
./run -a A
# arm
./run -a a
# x86_64
./run -a x</pre>
</div>
</div>
<div class="paragraph">
<p>Known quirks of the supported architectures are documented in this section.</p>
</div>
<div class="sect2">
<h3 id="x86_64"><a class="anchor" href="#x86_64"></a><a class="link" href="#x86_64">5.1. x86_64</a></h3>
<div class="sect3">
<h4 id="ring0"><a class="anchor" href="#ring0"></a><a class="link" href="#ring0">5.1.1. ring0</a></h4>
<div class="paragraph">
<p>This example illustrates how reading from the x86 control registers with <code>mov crX, rax</code> can only be done from kernel land on ring0.</p>
</div>
<div class="paragraph">
<p>From kernel land:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod ring0.ko</pre>
</div>
</div>
<div class="paragraph">
<p>works and output the registers, for example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cr0 = 0xFFFF880080050033
cr2 = 0xFFFFFFFF006A0008
cr3 = 0xFFFFF0DCDC000</pre>
</div>
</div>
<div class="paragraph">
<p>However if we try to do it from userland:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./ring0.out</pre>
</div>
</div>
<div class="paragraph">
<p>stdout gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Segmentation fault</pre>
</div>
</div>
<div class="paragraph">
<p>and dmesg outputs:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>traps: ring0.out[55] general protection ip:40054c sp:7fffffffec20 error:0 in ring0.out[400000+1000]</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/ring0.c">kernel_modules/ring0.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/ring0.h">lkmc/ring0.h</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/ring0.c">userland/arch/x86_64/ring0.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In both cases, we attempt to run the exact same code which is shared on the <code>ring0.h</code> header file.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/7415515/how-to-access-the-control-registers-cr0-cr2-cr3-from-a-program-getting-segmenta/7419306#7419306" class="bare">https://stackoverflow.com/questions/7415515/how-to-access-the-control-registers-cr0-cr2-cr3-from-a-program-getting-segmenta/7419306#7419306</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/18717016/what-are-ring-0-and-ring-3-in-the-context-of-operating-systems/44483439#44483439" class="bare">https://stackoverflow.com/questions/18717016/what-are-ring-0-and-ring-3-in-the-context-of-operating-systems/44483439#44483439</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="arm"><a class="anchor" href="#arm"></a><a class="link" href="#arm">5.2. arm</a></h3>
<div class="sect3">
<h4 id="run-arm-executable-in-aarch64"><a class="anchor" href="#run-arm-executable-in-aarch64"></a><a class="link" href="#run-arm-executable-in-aarch64">5.2.1. Run arm executable in aarch64</a></h4>
<div class="paragraph">
<p>TODO Can you run arm executables in the aarch64 guest? <a href="https://stackoverflow.com/questions/22460589/armv8-running-legacy-32-bit-applications-on-64-bit-os/51466709#51466709" class="bare">https://stackoverflow.com/questions/22460589/armv8-running-legacy-32-bit-applications-on-64-bit-os/51466709#51466709</a></p>
</div>
<div class="paragraph">
<p>I&#8217;ve tried:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain --arch aarch64 gcc -- -static ~/test/hello_world.c -o "$(./getvar p9_dir)/a.out"
./run --arch aarch64 --eval-after '/mnt/9p/data/a.out'</pre>
</div>
</div>
<div class="paragraph">
<p>but it fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>a.out: line 1: syntax error: unexpected word (expecting ")")</pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="mips"><a class="anchor" href="#mips"></a><a class="link" href="#mips">5.3. MIPS</a></h3>
<div class="paragraph">
<p>We used to "support" it until f8c0502bb2680f2dbe7c1f3d7958f60265347005 (it booted) but dropped since one was testing it often.</p>
</div>
<div class="paragraph">
<p>If you want to revive and maintain it, send a pull request.</p>
</div>
</div>
<div class="sect2">
<h3 id="other-architectures"><a class="anchor" href="#other-architectures"></a><a class="link" href="#other-architectures">5.4. Other architectures</a></h3>
<div class="paragraph">
<p>It should not be too hard to port this repository to any architecture that Buildroot supports. Pull requests are welcome.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="init"><a class="anchor" href="#init"></a><a class="link" href="#init">6. init</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>When the Linux kernel finishes booting, it runs an executable as the first and only userland process. This executable is called the <code>init</code> program.</p>
</div>
<div class="paragraph">
<p>The init process is then responsible for setting up the entire userland (or destroying everything when you want to have fun).</p>
</div>
<div class="paragraph">
<p>This typically means reading some configuration files (e.g. <code>/etc/initrc</code>) and forking a bunch of userland executables based on those files, including the very interactive shell that we end up on.</p>
</div>
<div class="paragraph">
<p>systemd provides a "popular" init implementation for desktop distros as of 2017.</p>
</div>
<div class="paragraph">
<p>BusyBox provides its own minimalistic init implementation which Buildroot, and therefore this repo, uses by default.</p>
</div>
<div class="paragraph">
<p>The <code>init</code> program can be either an executable shell text file, or a compiled ELF file. It becomes easy to accept this once you see that the <code>exec</code> system call handles both cases equally: <a href="https://unix.stackexchange.com/questions/174062/can-the-init-process-be-a-shell-script-in-linux/395375#395375" class="bare">https://unix.stackexchange.com/questions/174062/can-the-init-process-be-a-shell-script-in-linux/395375#395375</a></p>
</div>
<div class="paragraph">
<p>The <code>init</code> executable is searched for in a list of paths in the root filesystem, including <code>/init</code>, <code>/sbin/init</code> and a few others. For more details see: <a href="#path-to-init">Section 6.3, &#8220;Path to init&#8221;</a></p>
</div>
<div class="sect2">
<h3 id="replace-init"><a class="anchor" href="#replace-init"></a><a class="link" href="#replace-init">6.1. Replace init</a></h3>
<div class="paragraph">
<p>To have more control over the system, you can replace BusyBox&#8217;s init with your own.</p>
</div>
<div class="paragraph">
<p>The most direct way to replace <code>init</code> with our own is to just use the <code>init=</code> <a href="#kernel-command-line-parameters">command line parameter</a> directly:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'init=/lkmc/count.sh'</pre>
</div>
</div>
<div class="paragraph">
<p>This just counts every second forever and does not give you a shell.</p>
</div>
<div class="paragraph">
<p>This method is not very flexible however, as it is hard to reliably pass multiple commands and command line arguments to the init with it, as explained at: <a href="#init-environment">Section 6.4, &#8220;Init environment&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>For this reason, we have created a more robust helper method with the <code>--eval</code> option:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval 'echo "asdf qwer";insmod hello.ko;./linux/poweroff.out'</pre>
</div>
</div>
<div class="paragraph">
<p>It is basically a shortcut for:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'init=/lkmc/eval_base64.sh - lkmc_eval="insmod hello.ko;./linux/poweroff.out"'</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/eval_base64.sh">rootfs_overlay/lkmc/eval_base64.sh</a>.</p>
</div>
<div class="paragraph">
<p>This allows quoting and newlines by base64 encoding on host, and decoding on guest, see: <a href="#kernel-command-line-parameters-escaping">Section 15.3.1, &#8220;Kernel command line parameters escaping&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>It also automatically chooses between <code>init=</code> and <code>rcinit=</code> for you, see: <a href="#path-to-init">Section 6.3, &#8220;Path to init&#8221;</a></p>
</div>
<div class="paragraph">
<p><code>--eval</code> replaces BusyBox' init completely, which makes things more minimal, but also has has the following consequences:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>/etc/fstab</code> mounts are not done, notably <code>/proc</code> and <code>/sys</code>, test it out with:</p>
<div class="literalblock">
<div class="content">
<pre>./run --eval 'echo asdf;ls /proc;ls /sys;echo qwer'</pre>
</div>
</div>
</li>
<li>
<p>no shell is launched at the end of boot for you to interact with the system. You could explicitly add a <code>sh</code> at the end of your commands however:</p>
<div class="literalblock">
<div class="content">
<pre>./run --eval 'echo hello;sh'</pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>The best way to overcome those limitations is to use: <a href="#init-busybox">Section 6.2, &#8220;Run command at the end of BusyBox init&#8221;</a></p>
</div>
<div class="paragraph">
<p>If the script is large, you can add it to a gitignored file and pass that to <code>--eval</code> as in:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo '
cd /lkmc
insmod hello.ko
./linux/poweroff.out
' &gt; data/gitignore.sh
./run --eval "$(cat data/gitignore.sh)"</pre>
</div>
</div>
<div class="paragraph">
<p>or add it to a file to the root filesystem guest and rebuild:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo '#!/bin/sh
cd /lkmc
insmod hello.ko
./linux/poweroff.out
' &gt; rootfs_overlay/lkmc/gitignore.sh
chmod +x rootfs_overlay/lkmc/gitignore.sh
./build-buildroot
./run --kernel-cli 'init=/lkmc/gitignore.sh'</pre>
</div>
</div>
<div class="paragraph">
<p>Remember that if your init returns, the kernel will panic, there are just two non-panic possibilities:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>run forever in a loop or long sleep</p>
</li>
<li>
<p><code>poweroff</code> the machine</p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="poweroff-out"><a class="anchor" href="#poweroff-out"></a><a class="link" href="#poweroff-out">6.1.1. poweroff.out</a></h4>
<div class="paragraph">
<p>Just using BusyBox' <code>poweroff</code> at the end of the <code>init</code> does not work and the kernel panics:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval poweroff</pre>
</div>
</div>
<div class="paragraph">
<p>because BusyBox' <code>poweroff</code> tries to do some fancy stuff like killing init, likely to allow userland to shutdown nicely.</p>
</div>
<div class="paragraph">
<p>But this fails when we are <code>init</code> itself!</p>
</div>
<div class="paragraph">
<p>BusyBox' <code>poweroff</code> works more brutally and effectively if you add <code>-f</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval 'poweroff -f'</pre>
</div>
</div>
<div class="paragraph">
<p>but why not just use our minimal <code>./linux/poweroff.out</code> and be done with it?</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval './linux/poweroff.out'</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/poweroff.c">userland/linux/poweroff.c</a></p>
</div>
<div class="paragraph">
<p>This also illustrates how to shutdown the computer from C: <a href="https://stackoverflow.com/questions/28812514/how-to-shutdown-linux-using-c-or-qt-without-call-to-system" class="bare">https://stackoverflow.com/questions/28812514/how-to-shutdown-linux-using-c-or-qt-without-call-to-system</a></p>
</div>
</div>
<div class="sect3">
<h4 id="sleep_forever-out"><a class="anchor" href="#sleep_forever-out"></a><a class="link" href="#sleep_forever-out">6.1.2. sleep_forever.out</a></h4>
<div class="paragraph">
<p>I dare you to guess what this does:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval './posix/sleep_forever.out'</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/sleep_forever.c">userland/posix/sleep_forever.c</a></p>
</div>
<div class="paragraph">
<p>This executable is a convenient simple init that does not panic and sleeps instead.</p>
</div>
</div>
<div class="sect3">
<h4 id="time_boot-out"><a class="anchor" href="#time_boot-out"></a><a class="link" href="#time_boot-out">6.1.3. time_boot.out</a></h4>
<div class="paragraph">
<p>Get a reasonable answer to "how long does boot take in guest time?":</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after './linux/time_boot.c'</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/time_boot.c">userland/linux/time_boot.c</a></p>
</div>
<div class="paragraph">
<p>That executable writes to <code>dmesg</code> directly through <code>/dev/kmsg</code> a message of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[ 2.188242] /path/to/linux-kernel-module-cheat/userland/linux/time_boot.c</pre>
</div>
</div>
<div class="paragraph">
<p>which tells us that boot took <code>2.188242</code> seconds based on the dmesg timestamp.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/12683169/measure-time-taken-for-linux-kernel-from-bootup-to-userpace/46517014#46517014" class="bare">https://stackoverflow.com/questions/12683169/measure-time-taken-for-linux-kernel-from-bootup-to-userpace/46517014#46517014</a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="init-busybox"><a class="anchor" href="#init-busybox"></a><a class="link" href="#init-busybox">6.2. Run command at the end of BusyBox init</a></h3>
<div class="paragraph">
<p>Use the <code>--eval-after</code> option is for you rely on something that BusyBox' init set up for you like <code>/etc/fstab</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after 'echo asdf;ls /proc;ls /sys;echo qwer'</pre>
</div>
</div>
<div class="paragraph">
<p>After the commands run, you are left on an interactive shell.</p>
</div>
<div class="paragraph">
<p>The above command is basically equivalent to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli-after-dash 'lkmc_eval="insmod hello.ko;./linux/poweroff.out;"'</pre>
</div>
</div>
<div class="paragraph">
<p>where the <code>lkmc_eval</code> option gets evaled by our default <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/etc/init.d/S98">rootfs_overlay/etc/init.d/S98</a> startup script.</p>
</div>
<div class="paragraph">
<p>Except that <code>--eval-after</code> is smarter and uses <code>base64</code> encoding.</p>
</div>
<div class="paragraph">
<p>Alternatively, you can also add the comamdns to run to a new <code>init.d</code> entry to run at the end o the BusyBox init:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cp rootfs_overlay/etc/init.d/S98 rootfs_overlay/etc/init.d/S99.gitignore
vim rootfs_overlay/etc/init.d/S99.gitignore
./build-buildroot
./run</pre>
</div>
</div>
<div class="paragraph">
<p>and they will be run automatically before the login prompt.</p>
</div>
<div class="paragraph">
<p>Scripts under <code>/etc/init.d</code> are run by <code>/etc/init.d/rcS</code>, which gets called by the line <code>::sysinit:/etc/init.d/rcS</code> in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/etc/inittab"><code>/etc/inittab</code></a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="path-to-init"><a class="anchor" href="#path-to-init"></a><a class="link" href="#path-to-init">6.3. Path to init</a></h3>
<div class="paragraph">
<p>The init is selected at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>initrd or initramfs system: <code>/init</code>, a custom one can be set with the <code>rdinit=</code> <a href="#kernel-command-line-parameters">kernel command line parameter</a></p>
</li>
<li>
<p>otherwise: default is <code>/sbin/init</code>, followed by some other paths, a custom one can be set with <code>init=</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>More details: <a href="https://unix.stackexchange.com/questions/30414/what-can-make-passing-init-path-to-program-to-the-kernel-not-start-program-as-i/430614#430614" class="bare">https://unix.stackexchange.com/questions/30414/what-can-make-passing-init-path-to-program-to-the-kernel-not-start-program-as-i/430614#430614</a></p>
</div>
</div>
<div class="sect2">
<h3 id="init-environment"><a class="anchor" href="#init-environment"></a><a class="link" href="#init-environment">6.4. Init environment</a></h3>
<div class="paragraph">
<p>Documented at <a href="https://www.kernel.org/doc/html/v4.14/admin-guide/kernel-parameters.html" class="bare">https://www.kernel.org/doc/html/v4.14/admin-guide/kernel-parameters.html</a>:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>The kernel parses parameters from the kernel command line up to "-"; if it doesn&#8217;t recognize a parameter and it doesn&#8217;t contain a '.', the parameter gets passed to init: parameters with '=' go into init&#8217;s environment, others are passed as command line arguments to init. Everything after "-" is passed as an argument to init.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>And you can try it out with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'init=/lkmc/linux/init_env_poweroff.out - asdf=qwer zxcv'</pre>
</div>
</div>
<div class="paragraph">
<p>Output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>args:
/lkmc/linux/init_env_poweroff.out
-
zxcv
env:
HOME=/
TERM=linux
asdf=qwer</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/init_env_poweroff.c">userland/linux/init_env_poweroff.c</a>.</p>
</div>
<div class="sect3">
<h4 id="init-arguments"><a class="anchor" href="#init-arguments"></a><a class="link" href="#init-arguments">6.4.1. init arguments</a></h4>
<div class="paragraph">
<p>The annoying dash <code>-</code> gets passed as a parameter to <code>init</code>, which makes it impossible to use this method for most non custom executables.</p>
</div>
<div class="paragraph">
<p>Arguments with dots that come after <code>-</code> are still treated specially (of the form <code>subsystem.somevalue</code>) and disappear, from args, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'init=/lkmc/linux/init_env_poweroff.out - /lkmc/linux/poweroff.out'</pre>
</div>
</div>
<div class="paragraph">
<p>outputs:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>args
/lkmc/linux/init_env_poweroff.out
-
ab</pre>
</div>
</div>
<div class="paragraph">
<p>so see how <code>a.b</code> is gone.</p>
</div>
<div class="paragraph">
<p>The simple workaround is to just create a shell script that does it, e.g. as we&#8217;ve done at: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/gem5_exit.sh">rootfs_overlay/lkmc/gem5_exit.sh</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="init-environment-env"><a class="anchor" href="#init-environment-env"></a><a class="link" href="#init-environment-env">6.4.2. init environment env</a></h4>
<div class="paragraph">
<p>Wait, where do <code>HOME</code> and <code>TERM</code> come from? (greps the kernel). Ah, OK, the kernel sets those by default: <a href="https://github.com/torvalds/linux/blob/94710cac0ef4ee177a63b5227664b38c95bbf703/init/main.c#L173" class="bare">https://github.com/torvalds/linux/blob/94710cac0ef4ee177a63b5227664b38c95bbf703/init/main.c#L173</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre>const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="busybox-shell-init-environment"><a class="anchor" href="#busybox-shell-init-environment"></a><a class="link" href="#busybox-shell-init-environment">6.4.3. BusyBox shell init environment</a></h4>
<div class="paragraph">
<p>On top of the Linux kernel, the BusyBox <code>/bin/sh</code> shell will also define other variables.</p>
</div>
<div class="paragraph">
<p>We can explore the shenanigans that the shell adds on top of the Linux kernel with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'init=/bin/sh'</pre>
</div>
</div>
<div class="paragraph">
<p>From there we observe that:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>env</pre>
</div>
</div>
<div class="paragraph">
<p>gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>SHLVL=1
HOME=/
TERM=linux
PWD=/</pre>
</div>
</div>
<div class="paragraph">
<p>therefore adding <code>SHLVL</code> and <code>PWD</code> to the default kernel exported variables.</p>
</div>
<div class="paragraph">
<p>Furthermore, to increase confusion, if you list all non-exported shell variables <a href="https://askubuntu.com/questions/275965/how-to-list-all-variables-names-and-their-current-values" class="bare">https://askubuntu.com/questions/275965/how-to-list-all-variables-names-and-their-current-values</a> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>set</pre>
</div>
</div>
<div class="paragraph">
<p>then it shows more variables, notably:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>PATH='/sbin:/usr/sbin:/bin:/usr/bin'</pre>
</div>
</div>
<div class="sect4">
<h5 id="busybox-shell-initrc-files"><a class="anchor" href="#busybox-shell-initrc-files"></a><a class="link" href="#busybox-shell-initrc-files">6.4.3.1. BusyBox shell initrc files</a></h5>
<div class="paragraph">
<p>Login shells source some default files, notably:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/etc/profile
$HOME/.profile</pre>
</div>
</div>
<div class="paragraph">
<p>In our case, <code>HOME</code> is set to <code>/</code> presumably by <code>init</code> at: <a href="https://git.busybox.net/busybox/tree/init/init.c?id=5059653882dbd86e3bbf48389f9f81b0fac8cd0a#n1114" class="bare">https://git.busybox.net/busybox/tree/init/init.c?id=5059653882dbd86e3bbf48389f9f81b0fac8cd0a#n1114</a></p>
</div>
<div class="paragraph">
<p>We provide <code>/.profile</code> from <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/.profile">rootfs_overlay/.profile</a>, and use the default BusyBox <code>/etc/profile</code>.</p>
</div>
<div class="paragraph">
<p>The shell knows that it is a login shell if the first character of <code>argv[0]</code> is <code>-</code>, see also: <a href="https://stackoverflow.com/questions/2050961/is-argv0-name-of-executable-an-accepted-standard-or-just-a-common-conventi/42291142#42291142" class="bare">https://stackoverflow.com/questions/2050961/is-argv0-name-of-executable-an-accepted-standard-or-just-a-common-conventi/42291142#42291142</a></p>
</div>
<div class="paragraph">
<p>When we use just <code>init=/bin/sh</code>, the Linux kernel sets <code>argv[0]</code> to <code>/bin/sh</code>, which does not start with <code>-</code>.</p>
</div>
<div class="paragraph">
<p>However, if you use <code>::respawn:-/bin/sh</code> on inttab described at <a href="#tty">TTY</a>, BusyBox' init sets <code>argv[0][0]</code> to <code>-</code>, and so does <code>getty</code>. This can be observed with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /proc/$$/cmdline</pre>
</div>
</div>
<div class="paragraph">
<p>where <code>$$</code> is the PID of the shell itself: <a href="https://stackoverflow.com/questions/21063765/get-pid-in-shell-bash" class="bare">https://stackoverflow.com/questions/21063765/get-pid-in-shell-bash</a></p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://unix.stackexchange.com/questions/176027/ash-profile-configuration-file" class="bare">https://unix.stackexchange.com/questions/176027/ash-profile-configuration-file</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="initrd"><a class="anchor" href="#initrd"></a><a class="link" href="#initrd">7. initrd</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>The kernel can boot from an CPIO file, which is a directory serialization format much like tar: <a href="https://superuser.com/questions/343915/tar-vs-cpio-what-is-the-difference" class="bare">https://superuser.com/questions/343915/tar-vs-cpio-what-is-the-difference</a></p>
</div>
<div class="paragraph">
<p>The bootloader, which for us is provided by QEMU itself, is then configured to put that CPIO into memory, and tell the kernel that it is there.</p>
</div>
<div class="paragraph">
<p>This is very similar to the kernel image itself, which already gets put into memory by the QEMU <code>-kernel</code> option.</p>
</div>
<div class="paragraph">
<p>With this setup, you don&#8217;t even need to give a root filesystem to the kernel: it just does everything in memory in a ramfs.</p>
</div>
<div class="paragraph">
<p>To enable initrd instead of the default ext2 disk image, do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --initrd
./run --initrd</pre>
</div>
</div>
<div class="paragraph">
<p>By looking at the QEMU run command generated, you can see that we didn&#8217;t give the <code>-drive</code> option at all:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat "$(./getvar run_dir)/run.sh"</pre>
</div>
</div>
<div class="paragraph">
<p>Instead, we used the QEMU <code>-initrd</code> option to point to the <code>.cpio</code> filesystem that Buildroot generated for us.</p>
</div>
<div class="paragraph">
<p>Try removing that <code>-initrd</code> option to watch the kernel panic without rootfs at the end of boot.</p>
</div>
<div class="paragraph">
<p>When using <code>.cpio</code>, there can be no <a href="#disk-persistency">filesystem persistency</a> across boots, since all file operations happen in memory in a tmpfs:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>date &gt;f
poweroff
cat f
# can't open 'f': No such file or directory</pre>
</div>
</div>
<div class="paragraph">
<p>which can be good for automated tests, as it ensures that you are using a pristine unmodified system image every time.</p>
</div>
<div class="paragraph">
<p>Not however that we already disable disk persistency by default on ext2 filesystems even without <code>--initrd</code>: <a href="#disk-persistency">Section 18.2, &#8220;Disk persistency&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>One downside of this method is that it has to put the entire filesystem into memory, and could lead to a panic:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>end Kernel panic - not syncing: Out of memory and no killable processes...</pre>
</div>
</div>
<div class="paragraph">
<p>This can be solved by increasing the memory as explained at <a href="#memory-size">Memory size</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --initrd --memory 256M</pre>
</div>
</div>
<div class="paragraph">
<p>The main ingredients to get initrd working are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>BR2_TARGET_ROOTFS_CPIO=y</code>: make Buildroot generate <code>images/rootfs.cpio</code> in addition to the other images.</p>
<div class="paragraph">
<p>It is also possible to compress that image with other options.</p>
</div>
</li>
<li>
<p><code>qemu -initrd</code>: make QEMU put the image into memory and tell the kernel about it.</p>
</li>
<li>
<p><code>CONFIG_BLK_DEV_INITRD=y</code>: Compile the kernel with initrd support, see also: <a href="https://unix.stackexchange.com/questions/67462/linux-kernel-is-not-finding-the-initrd-correctly/424496#424496" class="bare">https://unix.stackexchange.com/questions/67462/linux-kernel-is-not-finding-the-initrd-correctly/424496#424496</a></p>
<div class="paragraph">
<p>Buildroot forces that option when <code>BR2_TARGET_ROOTFS_CPIO=y</code> is given</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>TODO: how does the bootloader inform the kernel where to find initrd? <a href="https://unix.stackexchange.com/questions/89923/how-does-linux-load-the-initrd-image" class="bare">https://unix.stackexchange.com/questions/89923/how-does-linux-load-the-initrd-image</a></p>
</div>
<div class="sect2">
<h3 id="initrd-in-desktop-distros"><a class="anchor" href="#initrd-in-desktop-distros"></a><a class="link" href="#initrd-in-desktop-distros">7.1. initrd in desktop distros</a></h3>
<div class="paragraph">
<p>Most modern desktop distributions have an initrd in their root disk to do early setup.</p>
</div>
<div class="paragraph">
<p>The rationale for this is described at: <a href="https://en.wikipedia.org/wiki/Initial_ramdisk" class="bare">https://en.wikipedia.org/wiki/Initial_ramdisk</a></p>
</div>
<div class="paragraph">
<p>One obvious use case is having an encrypted root filesystem: you keep the initrd in an unencrypted partition, and then setup decryption from there.</p>
</div>
<div class="paragraph">
<p>I think GRUB then knows read common disk formats, and then loads that initrd to memory with a <code>/boot/grub/grub.cfg</code> directive of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>initrd /initrd.img-4.4.0-108-generic</pre>
</div>
</div>
<div class="paragraph">
<p>Related: <a href="https://stackoverflow.com/questions/6405083/initrd-and-booting-the-linux-kernel" class="bare">https://stackoverflow.com/questions/6405083/initrd-and-booting-the-linux-kernel</a></p>
</div>
</div>
<div class="sect2">
<h3 id="initramfs"><a class="anchor" href="#initramfs"></a><a class="link" href="#initramfs">7.2. initramfs</a></h3>
<div class="paragraph">
<p>initramfs is just like <a href="#initrd">initrd</a>, but you also glue the image directly to the kernel image itself using the kernel&#8217;s build system.</p>
</div>
<div class="paragraph">
<p>Try it out with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --initramfs
./build-linux --initramfs
./run --initramfs</pre>
</div>
</div>
<div class="paragraph">
<p>Notice how we had to rebuild the Linux kernel this time around as well after Buildroot, since in that build we will be gluing the CPIO to the kernel image.</p>
</div>
<div class="paragraph">
<p>Now, once again, if we look at the QEMU run command generated, we see all that QEMU needs is the <code>-kernel</code> option, no <code>-drive</code> not even <code>-initrd</code>! Pretty cool:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat "$(./getvar run_dir)/run.sh"</pre>
</div>
</div>
<div class="paragraph">
<p>It is also interesting to observe how this increases the size of the kernel image if you do a:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ls -lh "$(./getvar linux_image)"</pre>
</div>
</div>
<div class="paragraph">
<p>before and after using initramfs, since the <code>.cpio</code> is now glued to the kernel image.</p>
</div>
<div class="paragraph">
<p>Don&#8217;t forget that to stop using initramfs, you must rebuild the kernel without <code>--initramfs</code> to get rid of the attached CPIO image:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux
./run</pre>
</div>
</div>
<div class="paragraph">
<p>Alternatively, consider using <a href="#linux-kernel-build-variants">Linux kernel build variants</a> if you need to switch between initramfs and non initramfs often:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --initramfs
./build-linux --initramfs --linux-build-id initramfs
./run --initramfs --linux-build-id</pre>
</div>
</div>
<div class="paragraph">
<p>Setting up initramfs is very easy: our scripts just set <code>CONFIG_INITRAMFS_SOURCE</code> to point to the CPIO path.</p>
</div>
<div class="paragraph">
<p><a href="http://nairobi-embedded.org/initramfs_tutorial.html" class="bare">http://nairobi-embedded.org/initramfs_tutorial.html</a> shows a full manual setup.</p>
</div>
</div>
<div class="sect2">
<h3 id="rootfs"><a class="anchor" href="#rootfs"></a><a class="link" href="#rootfs">7.3. rootfs</a></h3>
<div class="paragraph">
<p>This is how <code>/proc/mounts</code> shows the root filesystem:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>hard disk: <code>/dev/root on / type ext2 (rw,relatime,block_validity,barrier,user_xattr)</code>. That file does not exist however.</p>
</li>
<li>
<p>initrd: <code>rootfs on / type rootfs (rw)</code></p>
</li>
<li>
<p>initramfs: <code>rootfs on / type rootfs (rw)</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>TODO: understand <code>/dev/root</code> better:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://unix.stackexchange.com/questions/295060/why-on-some-linux-systems-does-the-root-filesystem-appear-as-dev-root-instead" class="bare">https://unix.stackexchange.com/questions/295060/why-on-some-linux-systems-does-the-root-filesystem-appear-as-dev-root-instead</a></p>
</li>
<li>
<p><a href="https://superuser.com/questions/1213770/how-do-you-determine-the-root-device-if-dev-root-is-missing" class="bare">https://superuser.com/questions/1213770/how-do-you-determine-the-root-device-if-dev-root-is-missing</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="devroot"><a class="anchor" href="#devroot"></a><a class="link" href="#devroot">7.3.1. /dev/root</a></h4>
<div class="paragraph">
<p>See: <a href="#rootfs">Section 7.3, &#8220;rootfs&#8221;</a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-initrd"><a class="anchor" href="#gem5-initrd"></a><a class="link" href="#gem5-initrd">7.4. gem5 initrd</a></h3>
<div class="paragraph">
<p>TODO we were not able to get it working yet: <a href="https://stackoverflow.com/questions/49261801/how-to-boot-the-linux-kernel-with-initrd-or-initramfs-with-gem5" class="bare">https://stackoverflow.com/questions/49261801/how-to-boot-the-linux-kernel-with-initrd-or-initramfs-with-gem5</a></p>
</div>
<div class="paragraph">
<p>This would require gem5 to load the CPIO into memory, just like QEMU. Grepping <code>initrd</code> shows some ARM hits under:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>src/arch/arm/linux/atag.hh</pre>
</div>
</div>
<div class="paragraph">
<p>but they are commented out.</p>
</div>
</div>
<div class="sect2">
<h3 id="gem5-initramfs"><a class="anchor" href="#gem5-initramfs"></a><a class="link" href="#gem5-initramfs">7.5. gem5 initramfs</a></h3>
<div class="paragraph">
<p>This could in theory be easier to make work than initrd since the emulator does not have to do anything special.</p>
</div>
<div class="paragraph">
<p>However, it didn&#8217;t: boot fails at the end because it does not see the initramfs, but rather tries to open our dummy root filesystem, which unsurprisingly does not have a format in a way that the kernel understands:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>VFS: Cannot open root device "sda" or unknown-block(8,0): error -5</pre>
</div>
</div>
<div class="paragraph">
<p>We think that this might be because gem5 boots directly <code>vmlinux</code>, and not from the final compressed images that contain the attached rootfs such as <code>bzImage</code>, which is what QEMU does, see also: <a href="#vmlinux-vs-bzimage-vs-zimage-vs-image">Section 15.21.1, &#8220;vmlinux vs bzImage vs zImage vs Image&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>To do this failed test, we automatically pass a dummy disk image as of gem5 7fa4c946386e7207ad5859e8ade0bbfc14000d91 since the scripts don&#8217;t handle a missing <code>--disk-image</code> well, much like is currently done for <a href="#baremetal">Baremetal</a>.</p>
</div>
<div class="paragraph">
<p>Interestingly, using initramfs significantly slows down the gem5 boot, even though it did not work. For example, we&#8217;ve observed a 4x slowdown of as 17062a2e8b6e7888a14c3506e9415989362c58bf for aarch64. This must be because expanding the large attached CPIO must be expensive. We can clearly see from the kernel logs that the kernel just hangs at a point after the message <code>PCI: CLS 0 bytes, default 64</code> for a long time before proceeding further.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="device-tree"><a class="anchor" href="#device-tree"></a><a class="link" href="#device-tree">8. Device tree</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>The device tree is a Linux kernel defined data structure that serves to inform the kernel how the hardware is setup.</p>
</div>
<div class="paragraph">
<p><a href="#platform_device">platform_device</a> contains a minimal runnable example of device tree manipulation.</p>
</div>
<div class="paragraph">
<p>Device trees serve to reduce the need for hardware vendors to patch the kernel: they just provide a device tree file instead, which is much simpler.</p>
</div>
<div class="paragraph">
<p>x86 does not use it device trees, but many other archs to, notably ARM.</p>
</div>
<div class="paragraph">
<p>This is notably because ARM boards:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>typically don&#8217;t have discoverable hardware extensions like PCI, but rather just put everything on an SoC with magic register addresses</p>
</li>
<li>
<p>are made by a wide variety of vendors due to ARM&#8217;s licensing business model, which increases variability</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The Linux kernel itself has several device trees under <code>./arch/&lt;arch&gt;/boot/dts</code>, see also: <a href="https://stackoverflow.com/questions/21670967/how-to-compile-dts-linux-device-tree-source-files-to-dtb/42839737#42839737" class="bare">https://stackoverflow.com/questions/21670967/how-to-compile-dts-linux-device-tree-source-files-to-dtb/42839737#42839737</a></p>
</div>
<div class="sect2">
<h3 id="dtb-files"><a class="anchor" href="#dtb-files"></a><a class="link" href="#dtb-files">8.1. DTB files</a></h3>
<div class="paragraph">
<p>Files that contain device trees have the <code>.dtb</code> extension when compiled, and <code>.dts</code> when in text form.</p>
</div>
<div class="paragraph">
<p>You can convert between those formats with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>"$(./getvar buildroot_host_dir)"/bin/dtc -I dtb -O dts -o a.dts a.dtb
"$(./getvar buildroot_host_dir)"/bin/dtc -I dts -O dtb -o a.dtb a.dts</pre>
</div>
</div>
<div class="paragraph">
<p>Buildroot builds the tool due to <code>BR2_PACKAGE_HOST_DTC=y</code>.</p>
</div>
<div class="paragraph">
<p>On Ubuntu 18.04, the package is named:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get install device-tree-compiler</pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="https://stackoverflow.com/questions/14000736/tool-to-visualize-the-device-tree-file-dtb-used-by-the-linux-kernel/39931834#39931834" class="bare">https://stackoverflow.com/questions/14000736/tool-to-visualize-the-device-tree-file-dtb-used-by-the-linux-kernel/39931834#39931834</a></p>
</div>
<div class="paragraph">
<p>Device tree files are provided to the emulator just like the root filesystem and the Linux kernel image.</p>
</div>
<div class="paragraph">
<p>In real hardware, those components are also often provided separately. For example, on the Raspberry Pi 2, the SD card must contain two partitions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the first contains all magic files, including the Linux kernel and the device tree</p>
</li>
<li>
<p>the second contains the root filesystem</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>See also: <a href="https://stackoverflow.com/questions/29837892/how-to-run-a-c-program-with-no-os-on-the-raspberry-pi/40063032#40063032" class="bare">https://stackoverflow.com/questions/29837892/how-to-run-a-c-program-with-no-os-on-the-raspberry-pi/40063032#40063032</a></p>
</div>
</div>
<div class="sect2">
<h3 id="device-tree-syntax"><a class="anchor" href="#device-tree-syntax"></a><a class="link" href="#device-tree-syntax">8.2. Device tree syntax</a></h3>
<div class="paragraph">
<p>Good format descriptions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://www.raspberrypi.org/documentation/configuration/device-tree.md" class="bare">https://www.raspberrypi.org/documentation/configuration/device-tree.md</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Minimal example</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/dts-v1/;
/ {
a;
};</pre>
</div>
</div>
<div class="paragraph">
<p>Check correctness with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>dtc a.dts</pre>
</div>
</div>
<div class="paragraph">
<p>Separate nodes are simply merged by node path, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/dts-v1/;
/ {
a;
};
/ {
b;
};</pre>
</div>
</div>
<div class="paragraph">
<p>then <code>dtc a.dts</code> gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/dts-v1/;
/ {
a;
b;
};</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="get-device-tree-from-a-running-kernel"><a class="anchor" href="#get-device-tree-from-a-running-kernel"></a><a class="link" href="#get-device-tree-from-a-running-kernel">8.3. Get device tree from a running kernel</a></h3>
<div class="paragraph">
<p><a href="https://unix.stackexchange.com/questions/265890/is-it-possible-to-get-the-information-for-a-device-tree-using-sys-of-a-running/330926#330926" class="bare">https://unix.stackexchange.com/questions/265890/is-it-possible-to-get-the-information-for-a-device-tree-using-sys-of-a-running/330926#330926</a></p>
</div>
<div class="paragraph">
<p>This is specially interesting because QEMU and gem5 are capable of generating DTBs that match the selected machine depending on dynamic command line parameters for some types of machines.</p>
</div>
<div class="paragraph">
<p>So observing the device tree from the guest allows to easily see what the emulator has generated.</p>
</div>
<div class="paragraph">
<p>Compile the <code>dtc</code> tool into the root filesystem:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot \
--arch aarch64 \
--config 'BR2_PACKAGE_DTC=y' \
--config 'BR2_PACKAGE_DTC_PROGRAMS=y' \
;</pre>
</div>
</div>
<div class="paragraph">
<p><code>-M virt</code> for example, which we use by default for <code>aarch64</code>, boots just fine without the <code>-dtb</code> option:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64</pre>
</div>
</div>
<div class="paragraph">
<p>Then, from inside the guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>dtc -I fs -O dts /sys/firmware/devicetree/base</pre>
</div>
</div>
<div class="paragraph">
<p>contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> cpus {
#address-cells = &lt;0x1&gt;;
#size-cells = &lt;0x0&gt;;
cpu@0 {
compatible = "arm,cortex-a57";
device_type = "cpu";
reg = &lt;0x0&gt;;
};
};</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="device-tree-emulator-generation"><a class="anchor" href="#device-tree-emulator-generation"></a><a class="link" href="#device-tree-emulator-generation">8.4. Device tree emulator generation</a></h3>
<div class="paragraph">
<p>Since emulators know everything about the hardware, they can automatically generate device trees for us, which is very convenient.</p>
</div>
<div class="paragraph">
<p>This is the case for both QEMU and gem5.</p>
</div>
<div class="paragraph">
<p>For example, if we increase the <a href="#number-of-cores">number of cores</a> to 2:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --cpus 2</pre>
</div>
</div>
<div class="paragraph">
<p>QEMU automatically adds a second CPU to the DTB!</p>
</div>
<div class="literalblock">
<div class="content">
<pre> cpu@0 {
cpu@1 {</pre>
</div>
</div>
<div class="paragraph">
<p>The action seems to be happening at: <code>hw/arm/virt.c</code>.</p>
</div>
<div class="paragraph">
<p>You can dump the DTB QEMU generated with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 -- -machine dumpdtb=dtb.dtb</pre>
</div>
</div>
<div class="paragraph">
<p>as mentioned at: <a href="https://lists.gnu.org/archive/html/qemu-discuss/2017-02/msg00051.html" class="bare">https://lists.gnu.org/archive/html/qemu-discuss/2017-02/msg00051.html</a></p>
</div>
<div class="paragraph">
<p><a href="#gem5-fs_biglittle">gem5 fs_bigLITTLE</a> 2a9573f5942b5416fb0570cf5cb6cdecba733392 can also generate its own DTB.</p>
</div>
<div class="paragraph">
<p>gem5 can generate DTBs on ARM with <code>--generate-dtb</code>. The generated DTB is placed in the <a href="#m5out-directory">m5out directory</a> named as <code>system.dtb</code>.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="kvm"><a class="anchor" href="#kvm"></a><a class="link" href="#kvm">9. KVM</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/Kernel-based_Virtual_Machine">KVM</a> is Linux kernel interface that <a href="#benchmark-linux-kernel-boot">greatly speeds up</a> execution of virtual machines.</p>
</div>
<div class="paragraph">
<p>You can make QEMU or <a href="#gem5-kvm">gem5</a> by passing enabling KVM with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kvm</pre>
</div>
</div>
<div class="paragraph">
<p>KVM works by running userland instructions natively directly on the real hardware instead of running a software simulation of those instructions.</p>
</div>
<div class="paragraph">
<p>Therefore, KVM only works if you the host architecture is the same as the guest architecture. This means that this will likely only work for x86 guests since almost all development machines are x86 nowadays. Unless you are <a href="https://www.youtube.com/watch?v=8ItXpmLsINs">running an ARM desktop for some weird reason</a> :-)</p>
</div>
<div class="paragraph">
<p>We don&#8217;t enable KVM by default because:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>it limits visibility, since more things are running natively:</p>
<div class="ulist">
<ul>
<li>
<p>can&#8217;t use <a href="#gdb">GDB</a></p>
</li>
<li>
<p>can&#8217;t do <a href="#tracing">instruction tracing</a></p>
</li>
<li>
<p>on gem5, you lose <a href="#gem5-run-benchmark">cycle counts</a> and therefor any notion of performance</p>
</li>
</ul>
</div>
</li>
<li>
<p>QEMU kernel boots are already <a href="#benchmark-linux-kernel-boot">fast enough</a> for most purposes without it</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>One important use case for KVM is to fast forward gem5 execution, often to skip boot, take a <a href="#gem5-checkpoint">gem5 checkpoint</a>, and then move on to a more detailed and slow simulation</p>
</div>
<div class="sect2">
<h3 id="kvm-arm"><a class="anchor" href="#kvm-arm"></a><a class="link" href="#kvm-arm">9.1. KVM arm</a></h3>
<div class="paragraph">
<p>TODO: we haven&#8217;t gotten it to work yet, but it should be doable, and this is an outline of how to do it. Just don&#8217;t expect this to tested very often for now.</p>
</div>
<div class="paragraph">
<p>We can test KVM on arm by running this repository inside an Ubuntu arm QEMU VM.</p>
</div>
<div class="paragraph">
<p>This produces no speedup of course, since the VM is already slow since it cannot use KVM on the x86 host.</p>
</div>
<div class="paragraph">
<p>First, obtain an Ubuntu arm64 virtual machine as explained at: <a href="https://askubuntu.com/questions/281763/is-there-any-prebuilt-qemu-ubuntu-image32bit-online/1081171#1081171" class="bare">https://askubuntu.com/questions/281763/is-there-any-prebuilt-qemu-ubuntu-image32bit-online/1081171#1081171</a></p>
</div>
<div class="paragraph">
<p>Then, from inside that image:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get install git
git clone https://github.com/cirosantilli/linux-kernel-module-cheat
cd linux-kernel-module-cheat
sudo ./setup -y</pre>
</div>
</div>
<div class="paragraph">
<p>and then proceed exactly as in <a href="#prebuilt">Prebuilt setup</a>.</p>
</div>
<div class="paragraph">
<p>We don&#8217;t want to build the full Buildroot image inside the VM as that would be way too slow, thus the recommendation for the prebuilt setup.</p>
</div>
<div class="paragraph">
<p>TODO: do the right thing and cross compile QEMU and gem5. gem5&#8217;s Python parts might be a pain. QEMU should be easy: <a href="https://stackoverflow.com/questions/26514252/cross-compile-qemu-for-arm" class="bare">https://stackoverflow.com/questions/26514252/cross-compile-qemu-for-arm</a></p>
</div>
</div>
<div class="sect2">
<h3 id="gem5-kvm"><a class="anchor" href="#gem5-kvm"></a><a class="link" href="#gem5-kvm">9.2. gem5 KVM</a></h3>
<div class="paragraph">
<p>While gem5 does have KVM, as of 2019 its support has not been very good, because debugging it is harder and people haven&#8217;t focused intensively on it.</p>
</div>
<div class="paragraph">
<p>X86 was broken with pending patches: <a href="https://www.mail-archive.com/gem5-users@gem5.org/msg15046.html" class="bare">https://www.mail-archive.com/gem5-users@gem5.org/msg15046.html</a> It failed immediately on:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>panic: KVM: Failed to enter virtualized mode (hw reason: 0x80000021)</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>ARM thread: <a href="https://stackoverflow.com/questions/53523087/how-to-run-gem5-on-kvm-on-arm-with-multiple-cores" class="bare">https://stackoverflow.com/questions/53523087/how-to-run-gem5-on-kvm-on-arm-with-multiple-cores</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="user-mode-simulation"><a class="anchor" href="#user-mode-simulation"></a><a class="link" href="#user-mode-simulation">10. User mode simulation</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Both QEMU and gem5 have an user mode simulation mode in addition to full system simulation that we consider elsewhere in this project.</p>
</div>
<div class="paragraph">
<p>In QEMU, it is called just <a href="#qemu-user-mode-getting-started">"user mode"</a>, and in gem5 it is called <a href="#gem5-syscall-emulation-mode">syscall emulation mode</a>.</p>
</div>
<div class="paragraph">
<p>In both, the basic idea is the same.</p>
</div>
<div class="paragraph">
<p>User mode simulation takes regular userland executables of any arch as input and executes them directly, without booting a kernel.</p>
</div>
<div class="paragraph">
<p>Instead of simulating the full system, it translates normal instructions like in full system mode, but magically forwards system calls to the host OS.</p>
</div>
<div class="paragraph">
<p>Advantages over full system simulation:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the simulation may <a href="#user-mode-vs-full-system-benchmark">run faster</a> since you don&#8217;t have to simulate the Linux kernel and several device models</p>
</li>
<li>
<p>you don&#8217;t need to build your own kernel or root filesystem, which saves time. You still need a toolchain however, but the pre-packaged ones may work fine.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Disadvantages:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>lower guest to host portability:</p>
<div class="ulist">
<ul>
<li>
<p>TODO confirm: host OS == guest OS?</p>
</li>
<li>
<p>TODO confirm: the host Linux kernel should be newer than the kernel the executable was built for.</p>
<div class="paragraph">
<p>It may still work even if that is not the case, but could fail is a missing system call is reached.</p>
</div>
<div class="paragraph">
<p>The target Linux kernel of the executable is a GCC toolchain build-time configuration.</p>
</div>
</li>
<li>
<p>emulator implementers have to keep up with libc changes, some of which break even a C hello world due setup code executed before main.</p>
<div class="paragraph">
<p>See also: <a href="#user-mode-simulation-with-glibc">Section 10.4, &#8220;User mode simulation with glibc&#8221;</a></p>
</div>
</li>
</ul>
</div>
</li>
<li>
<p>cannot be used to test the Linux kernel or any devices, and results are less representative of a real system since we are faking more</p>
</li>
</ul>
</div>
<div class="sect2">
<h3 id="qemu-user-mode-getting-started"><a class="anchor" href="#qemu-user-mode-getting-started"></a><a class="link" href="#qemu-user-mode-getting-started">10.1. QEMU user mode getting started</a></h3>
<div class="paragraph">
<p>Let&#8217;s run <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/command_line_arguments.c">userland/c/command_line_arguments.c</a> built with the Buildroot toolchain on QEMU user mode:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build user-mode-qemu
./run \
--userland userland/c/command_line_arguments.c \
--userland-args='asdf "qw er"' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/path/to/linux-kernel-module-cheat/out/userland/default/x86_64/c/print_argv.out
asdf
qw er</pre>
</div>
</div>
<div class="paragraph">
<p><code>./run --userland</code> path resolution is analogous to <a href="#baremetal-setup-getting-started">that of <code>./run --baremetal</code></a>.</p>
</div>
<div class="paragraph">
<p><code>./build user-mode-qemu</code> first builds Buildroot, and then runs <code>./build-userland</code>, which is further documented at: <a href="#userland-setup">Section 1.6, &#8220;Userland setup&#8221;</a>. It also builds QEMU. If you ahve already done a <a href="#qemu-buildroot-setup">QEMU Buildroot setup</a> previously, this will be very fast.</p>
</div>
<div class="paragraph">
<p>If you modify the userland programs, rebuild simply with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland</pre>
</div>
</div>
<div class="sect3">
<h4 id="user-mode-gdb"><a class="anchor" href="#user-mode-gdb"></a><a class="link" href="#user-mode-gdb">10.1.1. User mode GDB</a></h4>
<div class="paragraph">
<p>It&#8217;s nice when <a href="#gdb">the obvious</a> just works, right?</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--gdb-wait \
--userland userland/c/command_line_arguments.c \
--userland-args 'asdf "qw er"' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>and on another shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb \
--arch aarch64 \
--userland userland/c/command_line_arguments.c \
main \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Or alternatively, if you are using <a href="#tmux">tmux</a>, do everything in one go with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--gdb \
--userland userland/c/command_line_arguments.c \
--userland-args 'asdf "qw er"' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>To stop at the very first instruction of a freestanding program, just use <code>--no-continue</code>. A good example of this is shown at: <a href="#freestanding-programs">Section 22.5.1, &#8220;Freestanding programs&#8221;</a>.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="user-mode-tests"><a class="anchor" href="#user-mode-tests"></a><a class="link" href="#user-mode-tests">10.2. User mode tests</a></h3>
<div class="paragraph">
<p>Automatically run all userland tests that can be run in user mode simulation, and check that they exit with status 0:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --all-archs test-executables-userland
./test-executables --all-archs --all-emulators</pre>
</div>
</div>
<div class="paragraph">
<p>Or just for QEMU:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --all-archs test-executables-userland-qemu
./test-executables --all-archs --emulator qemu</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/test-executables">test-executables</a></p>
</div>
<div class="paragraph">
<p>This script skips a manually configured list of tests, notably:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>tests that depend on a full running kernel and cannot be run in user mode simulation, e.g. those that rely on kernel modules</p>
</li>
<li>
<p>tests that require user interaction</p>
</li>
<li>
<p>tests that take perceptible ammounts of time</p>
</li>
<li>
<p>known bugs we didn&#8217;t have time to fix ;-)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Tests under <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/libs/">userland/libs/</a> depend on certain libraries being available on the target, e.g. <a href="#blas">BLAS</a> for <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/libs/openblas">userland/libs/openblas</a>. They are not run by default, but can be enabled with <code>--package</code> and <code>--package-all</code>.</p>
</div>
<div class="paragraph">
<p>The gem5 tests require building statically with build id <code>static</code>, see also: <a href="#gem5-syscall-emulation-mode">Section 10.6, &#8220;gem5 syscall emulation mode&#8221;</a>. TODO automate this better.</p>
</div>
<div class="paragraph">
<p>See: <a href="#test-this-repo">Section 31.13, &#8220;Test this repo&#8221;</a> for more useful testing tips.</p>
</div>
</div>
<div class="sect2">
<h3 id="user-mode-buildroot-executables"><a class="anchor" href="#user-mode-buildroot-executables"></a><a class="link" href="#user-mode-buildroot-executables">10.3. User mode Buildroot executables</a></h3>
<div class="paragraph">
<p>If you followed <a href="#qemu-buildroot-setup">QEMU Buildroot setup</a>, you can now run the executables created by Buildroot directly as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--userland "$(./getvar buildroot_target_dir)/bin/echo" \
--userland-args='asdf' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>To easily explore the userland executable environment interactively, you can do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--userland "$(./getvar --arch aarch64 buildroot_target_dir)/bin/sh" \
--terminal \
;</pre>
</div>
</div>
<div class="paragraph">
<p>or:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--userland "$(./getvar --arch aarch64 buildroot_target_dir)/bin/sh" \
--userland-args='-c "uname -a &amp;&amp; pwd"' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Here is an interesting examples of this: <a href="#linux-test-project">Section 15.20.1, &#8220;Linux Test Project&#8221;</a></p>
</div>
</div>
<div class="sect2">
<h3 id="user-mode-simulation-with-glibc"><a class="anchor" href="#user-mode-simulation-with-glibc"></a><a class="link" href="#user-mode-simulation-with-glibc">10.4. User mode simulation with glibc</a></h3>
<div class="paragraph">
<p>At 125d14805f769104f93c510bedaa685a52ec025d we <a href="#libc-choice">moved Buildroot from uClibc to glibc</a>, and caused some user mode pain, which we document here.</p>
</div>
<div class="sect3">
<h4 id="fatal-kernel-too-old"><a class="anchor" href="#fatal-kernel-too-old"></a><a class="link" href="#fatal-kernel-too-old">10.4.1. FATAL: kernel too old</a></h4>
<div class="paragraph">
<p>glibc has a check for kernel version, likely obtained from the <code>uname</code> syscall, and if the kernel is not new enough, it quits.</p>
</div>
<div class="paragraph">
<p>Both gem5 and QEMU however allow setting the reported <code>uname</code> version from the command line, which we do to always match our toolchain.</p>
</div>
<div class="paragraph">
<p>QEMU by default copies the host <code>uname</code> value, but we always override it in our scripts.</p>
</div>
<div class="paragraph">
<p>Determining the right number to use for the kernel version is of course highly non-trivial and would require an extensive userland test suite, which most emulator don&#8217;t have.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --kernel-version 4.18 --userland userland/posix/uname.c</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/uname.c">userland/posix/uname.c</a>.</p>
</div>
<div class="paragraph">
<p>The QEMU source that does this is at: <a href="https://github.com/qemu/qemu/blob/v3.1.0/linux-user/syscall.c#L8931" class="bare">https://github.com/qemu/qemu/blob/v3.1.0/linux-user/syscall.c#L8931</a></p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/48959349/how-to-solve-fatal-kernel-too-old-when-running-gem5-in-syscall-emulation-se-m" class="bare">https://stackoverflow.com/questions/48959349/how-to-solve-fatal-kernel-too-old-when-running-gem5-in-syscall-emulation-se-m</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/53085048/how-to-compile-and-run-an-executable-in-gem5-syscall-emulation-mode-with-se-py/53085049#53085049" class="bare">https://stackoverflow.com/questions/53085048/how-to-compile-and-run-an-executable-in-gem5-syscall-emulation-mode-with-se-py/53085049#53085049</a></p>
</li>
<li>
<p><a href="https://gem5-review.googlesource.com/c/public/gem5/+/15855" class="bare">https://gem5-review.googlesource.com/c/public/gem5/+/15855</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The ID is just hardcoded on the source:</p>
</div>
</div>
<div class="sect3">
<h4 id="stack-smashing-detected"><a class="anchor" href="#stack-smashing-detected"></a><a class="link" href="#stack-smashing-detected">10.4.2. stack smashing detected</a></h4>
<div class="paragraph">
<p>For some reason QEMU / glibc x86_64 picks up the host libc, which breaks things.</p>
</div>
<div class="paragraph">
<p>Other archs work as they different host libc is skipped. <a href="#user-mode-static-executables">User mode static executables</a> also work.</p>
</div>
<div class="paragraph">
<p>We have worked around this with with <a href="https://bugs.launchpad.net/qemu/+bug/1701798/comments/12" class="bare">https://bugs.launchpad.net/qemu/+bug/1701798/comments/12</a> from the thread: <a href="https://bugs.launchpad.net/qemu/+bug/1701798" class="bare">https://bugs.launchpad.net/qemu/+bug/1701798</a> by creating the file: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/etc/ld.so.cache">rootfs_overlay/etc/ld.so.cache</a> which is a symlink to a file that cannot exist: <code>/dev/null/nonexistent</code>.</p>
</div>
<div class="paragraph">
<p>Reproduction:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rm -f "$(./getvar buildroot_target_dir)/etc/ld.so.cache"
./run --userland userland/c/hello.c
./run --userland userland/c/hello.c --qemu-which host</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>*** stack smashing detected ***: &lt;unknown&gt; terminated
qemu: uncaught target signal 6 (Aborted) - core dumped</pre>
</div>
</div>
<div class="paragraph">
<p>To get things working again, restore <code>ld.so.cache</code> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot</pre>
</div>
</div>
<div class="paragraph">
<p>I&#8217;ve also tested on an Ubuntu 16.04 guest and the failure is different one:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>qemu: uncaught target signal 4 (Illegal instruction) - core dumped</pre>
</div>
</div>
<div class="paragraph">
<p>A non-QEMU-specific example of stack smashing is shown at: <a href="https://stackoverflow.com/questions/1345670/stack-smashing-detected/51897264#51897264" class="bare">https://stackoverflow.com/questions/1345670/stack-smashing-detected/51897264#51897264</a></p>
</div>
<div class="paragraph">
<p>Tested at: 2e32389ebf1bedd89c682aa7b8fe42c3c0cf96e5 + 1.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="user-mode-static-executables"><a class="anchor" href="#user-mode-static-executables"></a><a class="link" href="#user-mode-static-executables">10.5. User mode static executables</a></h3>
<div class="paragraph">
<p>Example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland \
--arch aarch64 \
--static \
;
./run \
--arch aarch64 \
--static \
--userland userland/c/command_line_arguments.c \
--userland-args 'asdf "qw er"' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Running dynamically linked executables in QEMU requires pointing it to the root filesystem with the <code>-L</code> option so that it can find the dynamic linker and shared libraries.</p>
</div>
<div class="paragraph">
<p>We pass <code>-L</code> by default, so everything just works.</p>
</div>
<div class="paragraph">
<p>However, in case something goes wrong, you can also try statically linked executables, since this mechanism tends to be a bit more stable, for example:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>gem5 user mode currently only supports static executables as mentioned at: <a href="#gem5-syscall-emulation-mode">Section 10.6, &#8220;gem5 syscall emulation mode&#8221;</a></p>
</li>
<li>
<p>QEMU x86_64 guest on x86_64 host was failing with <a href="#stack-smashing-detected">stack smashing detected</a>, but we found a workaround</p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="user-mode-static-executables-with-dynamic-libraries"><a class="anchor" href="#user-mode-static-executables-with-dynamic-libraries"></a><a class="link" href="#user-mode-static-executables-with-dynamic-libraries">10.5.1. User mode static executables with dynamic libraries</a></h4>
<div class="paragraph">
<p>One limitation of static executables is that Buildroot mostly only builds dynamic versions of libraries (the libc is an exception).</p>
</div>
<div class="paragraph">
<p>So programs that rely on those libraries might not compile as GCC can&#8217;t find the <code>.a</code> version of the library.</p>
</div>
<div class="paragraph">
<p>For example, if we try to build <a href="#blas">BLAS</a> statically:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland --package openblas --static -- userland/libs/openblas/hello.c</pre>
</div>
</div>
<div class="paragraph">
<p>it fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ld: cannot find -lopenblas</pre>
</div>
</div>
<div class="paragraph">
<p><code>g++</code> and pthreads also causes issues: <a href="https://stackoverflow.com/questions/35116327/when-g-static-link-pthread-cause-segmentation-fault-why" class="bare">https://stackoverflow.com/questions/35116327/when-g-static-link-pthread-cause-segmentation-fault-why</a></p>
</div>
<div class="paragraph">
<p>As a consequence, the following fails:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --userland userland/cpp/atomic.cpp --static</pre>
</div>
</div>
<div class="paragraph">
<p>with error:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>qemu-x86_64: /path/to/linux-kernel-module-cheat/submodules/qemu/accel/tcg/cpu-exec.c:700: cpu_exec: Assertion `!have_mmap_lock()' failed.
qemu-x86_64: /path/to/linux-kernel-module-cheat/submodules/qemu/accel/tcg/cpu-exec.c:700: cpu_exec: Assertion `!have_mmap_lock()' failed.</pre>
</div>
</div>
<div class="paragraph">
<p>and if we manually build and run natively on host it segfaults.</p>
</div>
<div class="paragraph">
<p>If we hack the compilation command to do instead:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>-pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive</pre>
</div>
</div>
<div class="paragraph">
<p>then it works. We should automate that at some point.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-syscall-emulation-mode"><a class="anchor" href="#gem5-syscall-emulation-mode"></a><a class="link" href="#gem5-syscall-emulation-mode">10.6. gem5 syscall emulation mode</a></h3>
<div class="paragraph">
<p>Less robust than QEMU&#8217;s, but still usable:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/48986597/when-should-you-use-full-system-fs-vs-syscall-emulation-se-with-userland-program" class="bare">https://stackoverflow.com/questions/48986597/when-should-you-use-full-system-fs-vs-syscall-emulation-se-with-userland-program</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>There are much more unimplemented syscalls in gem5 than in QEMU. Many of those are trivial to implement however.</p>
</div>
<div class="paragraph">
<p>As of 185c2730cc78d5adda683d76c0e3b35e7cb534f0, dynamically linked executables only work on x86, and they can only use the host libraries, which is ugly:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/50542222/how-to-run-a-dynamically-linked-executable-syscall-emulation-mode-se-py-in-gem5" class="bare">https://stackoverflow.com/questions/50542222/how-to-run-a-dynamically-linked-executable-syscall-emulation-mode-se-py-in-gem5</a></p>
</li>
<li>
<p><a href="https://www.mail-archive.com/gem5-users@gem5.org/msg15585.html" class="bare">https://www.mail-archive.com/gem5-users@gem5.org/msg15585.html</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>If you try dynamically linked executables on ARM, they fail with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>fatal: Unable to open dynamic executable's interpreter.</pre>
</div>
</div>
<div class="paragraph">
<p>So let&#8217;s just play with some static ones:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland \
--arch aarch64 \
--static \
;
./run \
--arch aarch64 \
--emulator gem5 \
--userland userland/c/command_line_arguments.c \
--userland-args 'asdf "qw er"' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: how to escape spaces on the command line arguments?</p>
</div>
<div class="paragraph">
<p><a href="#user-mode-gdb">GDB step debug</a> also works normally on gem5:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--emulator gem5 \
--gdb-wait \
--static \
--userland userland/c/command_line_arguments.c \
--userland-args 'asdf "qw er"' \
;
./run-gdb \
--arch aarch64 \
--emulator gem5 \
--static \
--userland userland/c/command_line_arguments.c \
main \
;</pre>
</div>
</div>
<div class="sect3">
<h4 id="gem5-syscall-emulation-exit-status"><a class="anchor" href="#gem5-syscall-emulation-exit-status"></a><a class="link" href="#gem5-syscall-emulation-exit-status">10.6.1. gem5 syscall emulation exit status</a></h4>
<div class="paragraph">
<p>As of gem5 7fa4c946386e7207ad5859e8ade0bbfc14000d91, the crappy <code>se.py</code> script does not forward the exit status of syscall emulation mode, you can test it with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --dry-run --emulator gem5 --static --userland userland/c/false.c</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/false.c">userland/c/false.c</a>.</p>
</div>
<div class="paragraph">
<p>Then manually run the generated gem5 CLI, and do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>and the output is always <code>0</code>.</p>
</div>
<div class="paragraph">
<p>Instead, it just outputs a message to stdout just like for <a href="#m5-fail">m5 fail</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Simulated exit code not 0! Exit code is 1</pre>
</div>
</div>
<div class="paragraph">
<p>which we parse in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/run">run</a> and then exit with the correct result ourselves&#8230;&#8203;</p>
</div>
<div class="paragraph">
<p>Related thread: <a href="https://stackoverflow.com/questions/56032347/is-there-a-way-to-identify-if-gem5-run-got-over-successfully" class="bare">https://stackoverflow.com/questions/56032347/is-there-a-way-to-identify-if-gem5-run-got-over-successfully</a></p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-syscall-emulation-mode-program-stdin"><a class="anchor" href="#gem5-syscall-emulation-mode-program-stdin"></a><a class="link" href="#gem5-syscall-emulation-mode-program-stdin">10.6.2. gem5 syscall emulation mode program stdin</a></h4>
<div class="paragraph">
<p>gem5 shows its own stdout to terminal, and does not allow you to type stdin to programs.</p>
</div>
<div class="paragraph">
<p>Instead, you must pass stdin non-interactively with the through a file with the <code>--se.py --input</code> option, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>printf a &gt; f
./run --emulator gem5 --userland userland/c/getchar.c --static -- --input f</pre>
</div>
</div>
<div class="paragraph">
<p>leads to gem5 output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>enter a character: you entered: a</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/getchar.c">userland/c/getchar.c</a></p>
</div>
</div>
<div class="sect3">
<h4 id="user-mode-vs-full-system-benchmark"><a class="anchor" href="#user-mode-vs-full-system-benchmark"></a><a class="link" href="#user-mode-vs-full-system-benchmark">10.6.3. User mode vs full system benchmark</a></h4>
<div class="paragraph">
<p>Let&#8217;s see if user mode runs considerably faster than full system or not.</p>
</div>
<div class="paragraph">
<p>First we build <a href="#dhrystone">Dhrystone</a> manually statically since dynamic linking is broken in gem5 as explained at: <a href="#gem5-syscall-emulation-mode">Section 10.6, &#8220;gem5 syscall emulation mode&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>TODO: move this section to our new custom dhrystone setup: <a href="#dhrystone">Section 19.2.3.1, &#8220;Dhrystone&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>gem5 user mode:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --arch arm --config 'BR2_PACKAGE_DHRYSTONE=y'
make \
-B \
-C "$(./getvar --arch arm buildroot_build_build_dir)/dhrystone-2" \
CC="$(./run-toolchain --arch arm --print-tool gcc)" \
CFLAGS=-static \
;
time \
./run \
--arch arm \
--emulator gem5 \
--userland "$(./getvar --arch arm buildroot_build_build_dir)/dhrystone-2/dhrystone" \
--userland-args 'asdf qwer' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>gem5 full system:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>time \
./run \
--arch arm \
--eval-after './gem5.sh' \
--emulator gem5
--gem5-readfile 'dhrystone 100000' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>QEMU user mode:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>time qemu-arm "$(./getvar --arch arm buildroot_build_build_dir)/dhrystone-2/dhrystone" 100000000</pre>
</div>
</div>
<div class="paragraph">
<p>QEMU full system:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>time \
./run \
--arch arm \
--eval-after 'time dhrystone 100000000;./linux/poweroff.out' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Result on <a href="#p51">P51</a> at bad30f513c46c1b0995d3a10c0d9bc2a33dc4fa0:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>gem5 user: 33 seconds</p>
</li>
<li>
<p>gem5 full system: 51 seconds</p>
</li>
<li>
<p>QEMU user: 45 seconds</p>
</li>
<li>
<p>QEMU full system: 223 seconds</p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="gem5-syscall-emulation-mode-syscall-tracing"><a class="anchor" href="#gem5-syscall-emulation-mode-syscall-tracing"></a><a class="link" href="#gem5-syscall-emulation-mode-syscall-tracing">10.6.4. gem5 syscall emulation mode syscall tracing</a></h4>
<div class="paragraph">
<p>Since gem5 has to implement syscalls itself in syscall emulation mode, it can of course clearly see which syscalls are being made, and we can log them for debug purposes with <a href="#gem5-tracing">gem5 tracing</a>, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--emulator gem5 \
--static userland/arch/x86_64/freestanding/linux/hello.S \
--userland \
--trace-stdout \
--trace ExecAll,SyscallBase,SyscallVerbose \
;</pre>
</div>
</div>
<div class="paragraph">
<p>the trace as of f2eeceb1cde13a5ff740727526bf916b356cee38 + 1 contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> 0: system.cpu A0 T0 : @asm_main_after_prologue : mov rdi, 0x1
0: system.cpu A0 T0 : @asm_main_after_prologue.0 : MOV_R_I : limm rax, 0x1 : IntAlu : D=0x0000000000000001 flags=(IsInteger|IsMicroop|IsLastMicroop|IsFirstMicroop)
1000: system.cpu A0 T0 : @asm_main_after_prologue+7 : mov rdi, 0x1
1000: system.cpu A0 T0 : @asm_main_after_prologue+7.0 : MOV_R_I : limm rdi, 0x1 : IntAlu : D=0x0000000000000001 flags=(IsInteger|IsMicroop|IsLastMicroop|IsFirstMicroop)
2000: system.cpu A0 T0 : @asm_main_after_prologue+14 : lea rsi, DS:[rip + 0x19]
2000: system.cpu A0 T0 : @asm_main_after_prologue+14.0 : LEA_R_P : rdip t7, %ctrl153, : IntAlu : D=0x000000000040008d flags=(IsInteger|IsMicroop|IsDelayedCommit|IsFirstMicroop)
2500: system.cpu A0 T0 : @asm_main_after_prologue+14.1 : LEA_R_P : lea rsi, DS:[t7 + 0x19] : IntAlu : D=0x00000000004000a6 flags=(IsInteger|IsMicroop|IsLastMicroop)
3500: system.cpu A0 T0 : @asm_main_after_prologue+21 : mov rdi, 0x6
3500: system.cpu A0 T0 : @asm_main_after_prologue+21.0 : MOV_R_I : limm rdx, 0x6 : IntAlu : D=0x0000000000000006 flags=(IsInteger|IsMicroop|IsLastMicroop|IsFirstMicroop)
4000: system.cpu: T0 : syscall write called w/arguments 1, 4194470, 6, 0, 0, 0
hello
4000: system.cpu: T0 : syscall write returns 6
4000: system.cpu A0 T0 : @asm_main_after_prologue+28 : syscall eax : IntAlu : flags=(IsInteger|IsSerializeAfter|IsNonSpeculative|IsSyscall)
5000: system.cpu A0 T0 : @asm_main_after_prologue+30 : mov rdi, 0x3c
5000: system.cpu A0 T0 : @asm_main_after_prologue+30.0 : MOV_R_I : limm rax, 0x3c : IntAlu : D=0x000000000000003c flags=(IsInteger|IsMicroop|IsLastMicroop|IsFirstMicroop)
6000: system.cpu A0 T0 : @asm_main_after_prologue+37 : mov rdi, 0
6000: system.cpu A0 T0 : @asm_main_after_prologue+37.0 : MOV_R_I : limm rdi, 0 : IntAlu : D=0x0000000000000000 flags=(IsInteger|IsMicroop|IsLastMicroop|IsFirstMicroop)
6500: system.cpu: T0 : syscall exit called w/arguments 0, 4194470, 6, 0, 0, 0
6500: system.cpu: T0 : syscall exit returns 0
6500: system.cpu A0 T0 : @asm_main_after_prologue+44 : syscall eax : IntAlu : flags=(IsInteger|IsSerializeAfter|IsNonSpeculative|IsSyscall)</pre>
</div>
</div>
<div class="paragraph">
<p>so we see that two syscall lines were added for each syscall, showing the syscall inputs and exit status, just like a mini <code>strace</code>!</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="qemu-user-mode-quirks"><a class="anchor" href="#qemu-user-mode-quirks"></a><a class="link" href="#qemu-user-mode-quirks">10.7. QEMU user mode quirks</a></h3>
<div class="sect3">
<h4 id="qemu-user-mode-does-not-show-stdout-immediately"><a class="anchor" href="#qemu-user-mode-does-not-show-stdout-immediately"></a><a class="link" href="#qemu-user-mode-does-not-show-stdout-immediately">10.7.1. QEMU user mode does not show stdout immediately</a></h4>
<div class="paragraph">
<p>At 8d8307ac0710164701f6e14c99a69ee172ccbb70 + 1, I noticed that if you run <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/count.c">userland/posix/count.c</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --userland userland/posix/count_to.c --userland-args 3</pre>
</div>
</div>
<div class="paragraph">
<p>it first waits for 3 seconds, then the program exits, and then it dumps all the stdout at once, instead of counting once every second as expected.</p>
</div>
<div class="paragraph">
<p>The same can be reproduced by copying the raw QEMU command and piping it through <code>tee</code>, so I don&#8217;t think it is a bug in our setup:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/path/to/linux-kernel-module-cheat/out/qemu/default/x86_64-linux-user/qemu-x86_64 \
-L /path/to/linux-kernel-module-cheat/out/buildroot/build/default/x86_64/target \
/path/to/linux-kernel-module-cheat/out/userland/default/x86_64/posix/count.out \
3 \
| tee</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: investigate further and then possibly post on QEMU mailing list.</p>
</div>
<div class="sect4">
<h5 id="qemu-user-mode-does-not-show-errors"><a class="anchor" href="#qemu-user-mode-does-not-show-errors"></a><a class="link" href="#qemu-user-mode-does-not-show-errors">10.7.1.1. QEMU user mode does not show errors</a></h5>
<div class="paragraph">
<p>Similarly to <a href="#qemu-user-mode-does-not-show-stdout-immediately">QEMU user mode does not show stdout immediately</a>, QEMU error messages do not show at all through pipes.</p>
</div>
<div class="paragraph">
<p>In particular, it does not say anything if you pass it a non-existing executable:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>qemu-x86_64 asdf | cat</pre>
</div>
</div>
<div class="paragraph">
<p>So we just check ourselves manually</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="kernel-module-utilities"><a class="anchor" href="#kernel-module-utilities"></a><a class="link" href="#kernel-module-utilities">11. Kernel module utilities</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="insmod"><a class="anchor" href="#insmod"></a><a class="link" href="#insmod">11.1. insmod</a></h3>
<div class="paragraph">
<p><a href="https://git.busybox.net/busybox/tree/modutils/insmod.c?h=1_29_3">Provided by BusyBox</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after 'insmod hello.ko'</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="myinsmod"><a class="anchor" href="#myinsmod"></a><a class="link" href="#myinsmod">11.2. myinsmod</a></h3>
<div class="paragraph">
<p>If you are feeling raw, you can insert and remove modules with our own minimal module inserter and remover!</p>
</div>
<div class="literalblock">
<div class="content">
<pre># init_module
./linux/myinsmod.out hello.ko
# finit_module
./linux/myinsmod.out hello.ko "" 1
./linux/myrmmod.out hello</pre>
</div>
</div>
<div class="paragraph">
<p>which teaches you how it is done from C code.</p>
</div>
<div class="paragraph">
<p>Source:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/myinsmod.c">userland/linux/myinsmod.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/myrmmod.c">userland/linux/myrmmod.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The Linux kernel offers two system calls for module insertion:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>init_module</code></p>
</li>
<li>
<p><code>finit_module</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>and:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>man init_module</pre>
</div>
</div>
<div class="paragraph">
<p>documents that:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>The finit_module() system call is like init_module(), but reads the module to be loaded from the file descriptor fd. It is useful when the authenticity of a kernel module can be determined from its location in the filesystem; in cases where that is possible, the overhead of using cryptographically signed modules to determine the authenticity of a module can be avoided. The param_values argument is as for init_module().</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p><code>finit</code> is newer and was added only in v3.8. More rationale: <a href="https://lwn.net/Articles/519010/" class="bare">https://lwn.net/Articles/519010/</a></p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/5947286/how-to-load-linux-kernel-modules-from-c-code" class="bare">https://stackoverflow.com/questions/5947286/how-to-load-linux-kernel-modules-from-c-code</a></p>
</div>
</div>
<div class="sect2">
<h3 id="modprobe"><a class="anchor" href="#modprobe"></a><a class="link" href="#modprobe">11.3. modprobe</a></h3>
<div class="paragraph">
<p>Implemented as a BusyBox applet by default: <a href="https://git.busybox.net/busybox/tree/modutils/modprobe.c?h=1_29_stable" class="bare">https://git.busybox.net/busybox/tree/modutils/modprobe.c?h=1_29_stable</a></p>
</div>
<div class="paragraph">
<p><code>modprobe</code> searches for modules installed under:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ls /lib/modules/&lt;kernel_version&gt;</pre>
</div>
</div>
<div class="paragraph">
<p>and specified in the <code>modules.order</code> file.</p>
</div>
<div class="paragraph">
<p>This is the default install path for <code>CONFIG_SOME_MOD=m</code> modules built with <code>make modules_install</code> in the Linux kernel tree, with root path given by <code>INSTALL_MOD_PATH</code>, and therefore canonical in that sense.</p>
</div>
<div class="paragraph">
<p>Currently, there are only two kinds of kernel modules that you can try out with <code>modprobe</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>modules built with Buildroot, see: <a href="#kernel_modules-buildroot-package">Section 31.12.2.1, &#8220;kernel_modules buildroot package&#8221;</a></p>
</li>
<li>
<p>modules built from the kernel tree itself, see: <a href="#dummy-irq">Section 15.12.2, &#8220;dummy-irq&#8221;</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We are not installing out custom <code>./build-modules</code> modules there, because:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>we don&#8217;t know the right way. Why is there no <code>install</code> or <code>install_modules</code> target for kernel modules?</p>
<div class="paragraph">
<p>This can of course be solved by running Buildroot in verbose mode, and copying whatever it is doing, initial exploration at: <a href="https://stackoverflow.com/questions/22783793/how-to-install-kernel-modules-from-source-code-error-while-make-process/53169078#53169078" class="bare">https://stackoverflow.com/questions/22783793/how-to-install-kernel-modules-from-source-code-error-while-make-process/53169078#53169078</a></p>
</div>
</li>
<li>
<p>we would have to think how to not have to include the kernel modules twice in the root filesystem, but still have <a href="#9p">9P</a> working for fast development as described at: <a href="#your-first-kernel-module-hack">Section 1.1.2.2, &#8220;Your first kernel module hack&#8221;</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="kmod"><a class="anchor" href="#kmod"></a><a class="link" href="#kmod">11.4. kmod</a></h3>
<div class="paragraph">
<p>The more "reference" kernel.org implementation of <code>lsmod</code>, <code>insmod</code>, <code>rmmod</code>, etc.: <a href="https://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git" class="bare">https://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git</a></p>
</div>
<div class="paragraph">
<p>Default implementation on desktop distros such as Ubuntu 16.04, where e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ls -l /bin/lsmod</pre>
</div>
</div>
<div class="paragraph">
<p>gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lrwxrwxrwx 1 root root 4 Jul 25 15:35 /bin/lsmod -&gt; kmod</pre>
</div>
</div>
<div class="paragraph">
<p>and:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>dpkg -l | grep -Ei</pre>
</div>
</div>
<div class="paragraph">
<p>contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ii kmod 22-1ubuntu5 amd64 tools for managing Linux kernel modules</pre>
</div>
</div>
<div class="paragraph">
<p>BusyBox also implements its own version of those executables, see e.g. <a href="#modprobe">modprobe</a>. Here we will only describe features that differ from kmod to the BusyBox implementation.</p>
</div>
<div class="sect3">
<h4 id="module-init-tools"><a class="anchor" href="#module-init-tools"></a><a class="link" href="#module-init-tools">11.4.1. module-init-tools</a></h4>
<div class="paragraph">
<p>Name of a predecessor set of tools.</p>
</div>
</div>
<div class="sect3">
<h4 id="kmod-modprobe"><a class="anchor" href="#kmod-modprobe"></a><a class="link" href="#kmod-modprobe">11.4.2. kmod modprobe</a></h4>
<div class="paragraph">
<p>kmod&#8217;s <code>modprobe</code> can also load modules under different names to avoid conflicts, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo modprobe vmhgfs -o vm_hgfs</pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="filesystems"><a class="anchor" href="#filesystems"></a><a class="link" href="#filesystems">12. Filesystems</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="overlayfs"><a class="anchor" href="#overlayfs"></a><a class="link" href="#overlayfs">12.1. OverlayFS</a></h3>
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/OverlayFS">OverlayFS</a> is a filesystem merged in the Linux kernel in 3.18.</p>
</div>
<div class="paragraph">
<p>As the name suggests, OverlayFS allows you to merge multiple directories into one. The following minimal runnable examples should give you an intuition on how it works:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://askubuntu.com/questions/109413/how-do-i-use-overlayfs/1075564#1075564" class="bare">https://askubuntu.com/questions/109413/how-do-i-use-overlayfs/1075564#1075564</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/31044982/how-to-use-multiple-lower-layers-in-overlayfs/52792397#52792397" class="bare">https://stackoverflow.com/questions/31044982/how-to-use-multiple-lower-layers-in-overlayfs/52792397#52792397</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We are very interested in this filesystem because we are looking for a way to make host cross compiled executables appear on the guest root <code>/</code> without reboot.</p>
</div>
<div class="paragraph">
<p>This would have several advantages:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>makes it faster to test modified guest programs</p>
<div class="ulist">
<ul>
<li>
<p>not rebooting is fundamental for <a href="#gem5">gem5</a>, where the reboot is very costly.</p>
</li>
<li>
<p>no need to regenerate the root filesystem at all and reboot</p>
</li>
<li>
<p>overcomes the <code>check_bin_arch</code> problem as shown at: <a href="#rpath">Section 20.8, &#8220;Buildroot rebuild is slow when the root filesystem is large&#8221;</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>we could keep the base root filesystem very small, which implies:</p>
<div class="ulist">
<ul>
<li>
<p>less host disk usage, no need to copy the entire <code>./getvar out_rootfs_overlay_dir</code> to the image again</p>
</li>
<li>
<p>no need to worry about <a href="#br2_target_rootfs_ext2_size">BR2_TARGET_ROOTFS_EXT2_SIZE</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>We can already make host files appear on the guest with <a href="#9p">9P</a>, but they appear on a subdirectory instead of the root.</p>
</div>
<div class="paragraph">
<p>If they would appear on the root instead, that would be even more awesome, because you would just use the exact same paths relative to the root transparently.</p>
</div>
<div class="paragraph">
<p>For example, we wouldn&#8217;t have to mess around with variables such as <code>PATH</code> and <code>LD_LIBRARY_PATH</code>.</p>
</div>
<div class="paragraph">
<p>The idea is to:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>9P mount our overlay directory <code>./getvar out_rootfs_overlay_dir</code> on the guest, which we already do at <code>/mnt/9p/out_rootfs_overlay</code></p>
</li>
<li>
<p>then create an overlay with that directory and the root, and <code>chroot</code> into it.</p>
<div class="paragraph">
<p>I was unable to mount directly to <code>/</code> avoid the <code>chroot</code>:
<strong> <a href="https://stackoverflow.com/questions/41119656/how-can-i-overlayfs-the-root-filesystem-on-linux" class="bare">https://stackoverflow.com/questions/41119656/how-can-i-overlayfs-the-root-filesystem-on-linux</a>
</strong> <a href="https://unix.stackexchange.com/questions/316018/how-to-use-overlayfs-to-protect-the-root-filesystem" class="bare">https://unix.stackexchange.com/questions/316018/how-to-use-overlayfs-to-protect-the-root-filesystem</a>
** <a href="https://unix.stackexchange.com/questions/420646/mount-root-as-overlayfs" class="bare">https://unix.stackexchange.com/questions/420646/mount-root-as-overlayfs</a></p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>We already have a prototype of this running from <code>fstab</code> on guest at <code>/mnt/overlay</code>, but it has the following shortcomings:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>changes to underlying filesystems are not visible on the overlay unless you remount with <code>mount -r remount /mnt/overlay</code>, as mentioned <a href="https://github.com/torvalds/linux/blob/v4.18/Documentation/filesystems/overlayfs.txt#L332">on the kernel docs</a>:</p>
<div class="literalblock">
<div class="content">
<pre>Changes to the underlying filesystems while part of a mounted overlay
filesystem are not allowed. If the underlying filesystem is changed,
the behavior of the overlay is undefined, though it will not result in
a crash or deadlock.</pre>
</div>
</div>
<div class="paragraph">
<p>This makes everything very inconvenient if you are inside <code>chroot</code> action. You would have to leave <code>chroot</code>, remount, then come back.</p>
</div>
</li>
<li>
<p>the overlay does not contain sub-filesystems, e.g. <code>/proc</code>. We would have to re-mount them. But should be doable with some automation.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Even more awesome than <code>chroot</code> would be to <code>pivot_root</code>, but I couldn&#8217;t get that working either:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/28015688/pivot-root-device-or-resource-busy" class="bare">https://stackoverflow.com/questions/28015688/pivot-root-device-or-resource-busy</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/179788/pivot-root-device-or-resource-busy" class="bare">https://unix.stackexchange.com/questions/179788/pivot-root-device-or-resource-busy</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="secondary-disk"><a class="anchor" href="#secondary-disk"></a><a class="link" href="#secondary-disk">12.2. Secondary disk</a></h3>
<div class="paragraph">
<p>A simpler and possibly less overhead alternative to <a href="#9p">9P</a> would be to generate a secondary disk image with the benchmark you want to rebuild.</p>
</div>
<div class="paragraph">
<p>Then you can <code>umount</code> and re-mount on guest without reboot.</p>
</div>
<div class="paragraph">
<p>We don&#8217;t support this yet, but it should not be too hard to hack it up, maybe by hooking into <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs-post-build-script">rootfs-post-build-script</a>.</p>
</div>
<div class="paragraph">
<p>This was not possible from gem5 <code>fs.py</code> as of 60600f09c25255b3c8f72da7fb49100e2682093a: <a href="https://stackoverflow.com/questions/50862906/how-to-attach-multiple-disk-images-in-a-simulation-with-gem5-fs-py/51037661#51037661" class="bare">https://stackoverflow.com/questions/50862906/how-to-attach-multiple-disk-images-in-a-simulation-with-gem5-fs-py/51037661#51037661</a></p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="graphics"><a class="anchor" href="#graphics"></a><a class="link" href="#graphics">13. Graphics</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Both QEMU and gem5 are capable of outputting graphics to the screen, and taking mouse and keyboard input.</p>
</div>
<div class="paragraph">
<p><a href="https://unix.stackexchange.com/questions/307390/what-is-the-difference-between-ttys0-ttyusb0-and-ttyama0-in-linux" class="bare">https://unix.stackexchange.com/questions/307390/what-is-the-difference-between-ttys0-ttyusb0-and-ttyama0-in-linux</a></p>
</div>
<div class="sect2">
<h3 id="qemu-text-mode"><a class="anchor" href="#qemu-text-mode"></a><a class="link" href="#qemu-text-mode">13.1. QEMU text mode</a></h3>
<div class="paragraph">
<p>Text mode is the default mode for QEMU.</p>
</div>
<div class="paragraph">
<p>The opposite of text mode is <a href="#qemu-graphic-mode">QEMU graphic mode</a></p>
</div>
<div class="paragraph">
<p>In text mode, we just show the serial console directly on the current terminal, without opening a QEMU GUI window.</p>
</div>
<div class="paragraph">
<p>You cannot see any graphics from text mode, but text operations in this mode, including:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>scrolling up: <a href="#scroll-up-in-graphic-mode">Section 13.2.1, &#8220;Scroll up in graphic mode&#8221;</a></p>
</li>
<li>
<p>copy paste to and from the terminal</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>making this a good default, unless you really need to use with graphics.</p>
</div>
<div class="paragraph">
<p>Text mode works by sending the terminal character by character to a serial device.</p>
</div>
<div class="paragraph">
<p>This is different from a display screen, where each character is a bunch of pixels, and it would be much harder to convert that into actual terminal text.</p>
</div>
<div class="paragraph">
<p>For more details, see:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://unix.stackexchange.com/questions/307390/what-is-the-difference-between-ttys0-ttyusb0-and-ttyama0-in-linux" class="bare">https://unix.stackexchange.com/questions/307390/what-is-the-difference-between-ttys0-ttyusb0-and-ttyama0-in-linux</a></p>
</li>
<li>
<p><a href="#tty">TTY</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Note that you can still see an image even in text mode with the VNC:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --vnc</pre>
</div>
</div>
<div class="paragraph">
<p>and on another terminal:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./vnc</pre>
</div>
</div>
<div class="paragraph">
<p>but there is not terminal on the VNC window, just the <a href="#config_logo">CONFIG_LOGO</a> penguin.</p>
</div>
<div class="sect3">
<h4 id="quit-qemu-from-text-mode"><a class="anchor" href="#quit-qemu-from-text-mode"></a><a class="link" href="#quit-qemu-from-text-mode">13.1.1. Quit QEMU from text mode</a></h4>
<div class="paragraph">
<p><a href="https://superuser.com/questions/1087859/how-to-quit-the-qemu-monitor-when-not-using-a-gui" class="bare">https://superuser.com/questions/1087859/how-to-quit-the-qemu-monitor-when-not-using-a-gui</a></p>
</div>
<div class="paragraph">
<p>However, our QEMU setup captures Ctrl + C and other common signals and sends them to the guest, which makes it hard to quit QEMU for the first time since there is no GUI either.</p>
</div>
<div class="paragraph">
<p>The simplest way to quit QEMU, is to do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Ctrl-A X</pre>
</div>
</div>
<div class="paragraph">
<p>Alternative methods include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>quit</code> command on the <a href="#qemu-monitor">QEMU monitor</a></p>
</li>
<li>
<p><code>pkill qemu</code></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="qemu-graphic-mode"><a class="anchor" href="#qemu-graphic-mode"></a><a class="link" href="#qemu-graphic-mode">13.2. QEMU graphic mode</a></h3>
<div class="paragraph">
<p>Enable graphic mode with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --graphic</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: you see a penguin due to <a href="#config_logo">CONFIG_LOGO</a>.</p>
</div>
<div class="paragraph">
<p>For a more exciting GUI experience, see: <a href="#x11">Section 13.4, &#8220;X11 Buildroot&#8221;</a></p>
</div>
<div class="paragraph">
<p>Text mode is the default due to the following considerable advantages:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>copy and paste commands and stdout output to / from host</p>
</li>
<li>
<p>get full panic traces when you start making the kernel crash :-) See also: <a href="https://unix.stackexchange.com/questions/208260/how-to-scroll-up-after-a-kernel-panic" class="bare">https://unix.stackexchange.com/questions/208260/how-to-scroll-up-after-a-kernel-panic</a></p>
</li>
<li>
<p>have a large scroll buffer, and be able to search it, e.g. by using tmux on host</p>
</li>
<li>
<p>one less window floating around to think about in addition to your shell :-)</p>
</li>
<li>
<p>graphics mode has only been properly tested on <code>x86_64</code>.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Text mode has the following limitations over graphics mode:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>you can&#8217;t see graphics such as those produced by <a href="#x11">X11 Buildroot</a></p>
</li>
<li>
<p>very early kernel messages such as <code>early console in extract_kernel</code> only show on the GUI, since at such early stages, not even the serial has been setup.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p><code>x86_64</code> has a VGA device enabled by default, as can be seen as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor info qtree</pre>
</div>
</div>
<div class="paragraph">
<p>and the Linux kernel picks it up through the <a href="https://en.wikipedia.org/wiki/Linux_framebuffer">fbdev</a> graphics system as can be seen from:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /dev/urandom &gt; /dev/fb0</pre>
</div>
</div>
<div class="paragraph">
<p>flooding the screen with colors. See also: <a href="https://superuser.com/questions/223094/how-do-i-know-if-i-have-kms-enabled" class="bare">https://superuser.com/questions/223094/how-do-i-know-if-i-have-kms-enabled</a></p>
</div>
<div class="sect3">
<h4 id="scroll-up-in-graphic-mode"><a class="anchor" href="#scroll-up-in-graphic-mode"></a><a class="link" href="#scroll-up-in-graphic-mode">13.2.1. Scroll up in graphic mode</a></h4>
<div class="paragraph">
<p>Scroll up in <a href="#qemu-graphic-mode">QEMU graphic mode</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Shift-PgUp</pre>
</div>
</div>
<div class="paragraph">
<p>but I never managed to increase that buffer:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://askubuntu.com/questions/709697/how-to-increase-scrollback-lines-in-ubuntu14-04-2-server-edition" class="bare">https://askubuntu.com/questions/709697/how-to-increase-scrollback-lines-in-ubuntu14-04-2-server-edition</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/346018/how-to-increase-the-scrollback-buffer-size-for-tty" class="bare">https://unix.stackexchange.com/questions/346018/how-to-increase-the-scrollback-buffer-size-for-tty</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The superior alternative is to use text mode and GNU screen or <a href="#tmux">tmux</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="qemu-graphic-mode-arm"><a class="anchor" href="#qemu-graphic-mode-arm"></a><a class="link" href="#qemu-graphic-mode-arm">13.2.2. QEMU Graphic mode arm</a></h4>
<div class="sect4">
<h5 id="qemu-graphic-mode-arm-terminal"><a class="anchor" href="#qemu-graphic-mode-arm-terminal"></a><a class="link" href="#qemu-graphic-mode-arm-terminal">13.2.2.1. QEMU graphic mode arm terminal</a></h5>
<div class="paragraph">
<p>TODO: on arm, we see the penguin and some boot messages, but don&#8217;t get a shell at then end:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --graphic</pre>
</div>
</div>
<div class="paragraph">
<p>I think it does not work because the graphic window is <a href="#drm">DRM</a> only, i.e.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /dev/urandom &gt; /dev/fb0</pre>
</div>
</div>
<div class="paragraph">
<p>fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat: write error: No space left on device</pre>
</div>
</div>
<div class="paragraph">
<p>and has no effect, and the Linux kernel does not appear to have a built-in DRM console as it does for fbdev with <a href="#fbcon">fbcon</a>.</p>
</div>
<div class="paragraph">
<p>There is however one out-of-tree implementation: <a href="#kmscon">kmscon</a>.</p>
</div>
</div>
<div class="sect4">
<h5 id="qemu-graphic-mode-arm-terminal-implementation"><a class="anchor" href="#qemu-graphic-mode-arm-terminal-implementation"></a><a class="link" href="#qemu-graphic-mode-arm-terminal-implementation">13.2.2.2. QEMU graphic mode arm terminal implementation</a></h5>
<div class="paragraph">
<p><code>arm</code> and <code>aarch64</code> rely on the QEMU CLI option:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>-device virtio-gpu-pci</pre>
</div>
</div>
<div class="paragraph">
<p>and the kernel config options:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>CONFIG_DRM=y
CONFIG_DRM_VIRTIO_GPU=y</pre>
</div>
</div>
<div class="paragraph">
<p>Unlike x86, <code>arm</code> and <code>aarch64</code> don&#8217;t have a display device attached by default, thus the need for <code>virtio-gpu-pci</code>.</p>
</div>
<div class="paragraph">
<p>See also <a href="https://wiki.qemu.org/Documentation/Platforms/ARM" class="bare">https://wiki.qemu.org/Documentation/Platforms/ARM</a> (recently edited and corrected by yours truly&#8230;&#8203; :-)).</p>
</div>
</div>
<div class="sect4">
<h5 id="qemu-graphic-mode-arm-vga"><a class="anchor" href="#qemu-graphic-mode-arm-vga"></a><a class="link" href="#qemu-graphic-mode-arm-vga">13.2.2.3. QEMU graphic mode arm VGA</a></h5>
<div class="paragraph">
<p>TODO: how to use VGA on ARM? <a href="https://stackoverflow.com/questions/20811203/how-can-i-output-to-vga-through-qemu-arm" class="bare">https://stackoverflow.com/questions/20811203/how-can-i-output-to-vga-through-qemu-arm</a> Tried:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>-device VGA</pre>
</div>
</div>
<div class="paragraph">
<p>But <a href="https://github.com/qemu/qemu/blob/v2.12.0/docs/config/mach-virt-graphical.cfg#L264" class="bare">https://github.com/qemu/qemu/blob/v2.12.0/docs/config/mach-virt-graphical.cfg#L264</a> says:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># We use virtio-gpu because the legacy VGA framebuffer is
# very troublesome on aarch64, and virtio-gpu is the only
# video device that doesn't implement it.</pre>
</div>
</div>
<div class="paragraph">
<p>so maybe it is not possible?</p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-graphic-mode"><a class="anchor" href="#gem5-graphic-mode"></a><a class="link" href="#gem5-graphic-mode">13.3. gem5 graphic mode</a></h3>
<div class="paragraph">
<p>gem5 does not have a "text mode", since it cannot redirect the Linux terminal to same host terminal where the executable is running: you are always forced to connect to the terminal with <code>gem-shell</code>.</p>
</div>
<div class="paragraph">
<p>TODO could not get it working on <code>x86_64</code>, only ARM.</p>
</div>
<div class="paragraph">
<p>Overview: <a href="https://stackoverflow.com/questions/50364863/how-to-get-graphical-gui-output-and-user-touch-keyboard-mouse-input-in-a-ful/50364864#50364864" class="bare">https://stackoverflow.com/questions/50364863/how-to-get-graphical-gui-output-and-user-touch-keyboard-mouse-input-in-a-ful/50364864#50364864</a></p>
</div>
<div class="paragraph">
<p>More concretely, first build the kernel with the <a href="#gem5-arm-linux-kernel-patches">gem5 arm Linux kernel patches</a>, and then run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux \
--arch arm \
--custom-config-file-gem5 \
--linux-build-id gem5-v4.15 \
;
./run --arch arm --emulator gem5 --linux-build-id gem5-v4.15</pre>
</div>
</div>
<div class="paragraph">
<p>and then on another shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vinagre localhost:5900</pre>
</div>
</div>
<div class="paragraph">
<p>The <a href="#config_logo">CONFIG_LOGO</a> penguin only appears after several seconds, together with kernel messages of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[ 0.152755] [drm] found ARM HDLCD version r0p0
[ 0.152790] hdlcd 2b000000.hdlcd: bound virt-encoder (ops 0x80935f94)
[ 0.152795] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
[ 0.152799] [drm] No driver support for vblank timestamp query.
[ 0.215179] Console: switching to colour frame buffer device 240x67
[ 0.230389] hdlcd 2b000000.hdlcd: fb0: frame buffer device
[ 0.230509] [drm] Initialized hdlcd 1.0.0 20151021 for 2b000000.hdlcd on minor 0</pre>
</div>
</div>
<div class="paragraph">
<p>The port <code>5900</code> is incremented by one if you already have something running on that port, <code>gem5</code> stdout tells us the right port on stdout as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>system.vncserver: Listening for connections on port 5900</pre>
</div>
</div>
<div class="paragraph">
<p>and when we connect it shows a message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>info: VNC client attached</pre>
</div>
</div>
<div class="paragraph">
<p>Alternatively, you can also dump each new frame to an image file with <code>--frame-capture</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch arm \
--emulator gem5 \
--linux-build-id gem5-v4.15 \
-- --frame-capture \
;</pre>
</div>
</div>
<div class="paragraph">
<p>This creates on compressed PNG whenever the screen image changes inside the <a href="#m5out-directory">m5out directory</a> with filename of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>frames_system.vncserver/fb.&lt;frame-index&gt;.&lt;timestamp&gt;.png.gz</pre>
</div>
</div>
<div class="paragraph">
<p>It is fun to see how we get one new frame whenever the white underscore cursor appears and reappears under the penguin!</p>
</div>
<div class="paragraph">
<p>The last frame is always available uncompressed at: <code>system.framebuffer.png</code>.</p>
</div>
<div class="paragraph">
<p>TODO <a href="#kmscube">kmscube</a> failed on <code>aarch64</code> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>kmscube[706]: unhandled level 2 translation fault (11) at 0x00000000, esr 0x92000006, in libgbm.so.1.0.0[7fbf6a6000+e000]</pre>
</div>
</div>
<div class="paragraph">
<p>Tested on: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/commit/38fd6153d965ba20145f53dc1bb3ba34b336bde9">38fd6153d965ba20145f53dc1bb3ba34b336bde9</a></p>
</div>
<div class="sect3">
<h4 id="graphic-mode-gem5-aarch64"><a class="anchor" href="#graphic-mode-gem5-aarch64"></a><a class="link" href="#graphic-mode-gem5-aarch64">13.3.1. Graphic mode gem5 aarch64</a></h4>
<div class="paragraph">
<p>For <code>aarch64</code> we also need to configure the kernel with <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/linux_config/display">linux_config/display</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git -C "$(./getvar linux_source_dir)" fetch https://gem5.googlesource.com/arm/linux gem5/v4.15:gem5/v4.15
git -C "$(./getvar linux_source_dir)" checkout gem5/v4.15
./build-linux \
--arch aarch64 \
--config-fragment linux_config/display \
--custom-config-file-gem5 \
--linux-build-id gem5-v4.15 \
;
git -C "$(./getvar linux_source_dir)" checkout -
./run --arch aarch64 --emulator gem5 --linux-build-id gem5-v4.15</pre>
</div>
</div>
<div class="paragraph">
<p>This is because the gem5 <code>aarch64</code> defconfig does not enable HDLCD like the 32 bit one <code>arm</code> one for some reason.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-graphic-mode-dp650"><a class="anchor" href="#gem5-graphic-mode-dp650"></a><a class="link" href="#gem5-graphic-mode-dp650">13.3.2. gem5 graphic mode DP650</a></h4>
<div class="paragraph">
<p>TODO get working. There is an unmerged patchset at: <a href="https://gem5-review.googlesource.com/c/public/gem5/+/11036/1" class="bare">https://gem5-review.googlesource.com/c/public/gem5/+/11036/1</a></p>
</div>
<div class="paragraph">
<p>The DP650 is a newer display hardware than HDLCD. TODO is its interface publicly documented anywhere? Since it has a gem5 model and <a href="https://github.com/torvalds/linux/blob/v4.19/drivers/gpu/drm/arm/Kconfig#L39">in-tree Linux kernel support</a>, that information cannot be secret?</p>
</div>
<div class="paragraph">
<p>The key option to enable support in Linux is <code>DRM_MALI_DISPLAY=y</code> which we enable at <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/linux_config/display">linux_config/display</a>.</p>
</div>
<div class="paragraph">
<p>Build the kernel exactly as for <a href="#graphic-mode-gem5-aarch64">Graphic mode gem5 aarch64</a> and then run with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --dp650 --emulator gem5 --linux-build-id gem5-v4.15</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="gem5-graphic-mode-internals"><a class="anchor" href="#gem5-graphic-mode-internals"></a><a class="link" href="#gem5-graphic-mode-internals">13.3.3. gem5 graphic mode internals</a></h4>
<div class="paragraph">
<p>We cannot use mainline Linux because the <a href="#gem5-arm-linux-kernel-patches">gem5 arm Linux kernel patches</a> are required at least to provide the <code>CONFIG_DRM_VIRT_ENCODER</code> option.</p>
</div>
<div class="paragraph">
<p>gem5 emulates the <a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0541c/CHDBAIDI.html">HDLCD</a> ARM Holdings hardware for <code>arm</code> and <code>aarch64</code>.</p>
</div>
<div class="paragraph">
<p>The kernel uses HDLCD to implement the <a href="#drm">DRM</a> interface, the required kernel config options are present at: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/linux_config/display">linux_config/display</a>.</p>
</div>
<div class="paragraph">
<p>TODO: minimize out the <code>--custom-config-file</code>. If we just remove it on <code>arm</code>: it does not work with a failing dmesg:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[ 0.066208] [drm] found ARM HDLCD version r0p0
[ 0.066241] hdlcd 2b000000.hdlcd: bound virt-encoder (ops drm_vencoder_ops)
[ 0.066247] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
[ 0.066252] [drm] No driver support for vblank timestamp query.
[ 0.066276] hdlcd 2b000000.hdlcd: Cannot do DMA to address 0x0000000000000000
[ 0.066281] swiotlb: coherent allocation failed for device 2b000000.hdlcd size=8294400
[ 0.066288] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.15.0 #1
[ 0.066293] Hardware name: V2P-AARCH64 (DT)
[ 0.066296] Call trace:
[ 0.066301] dump_backtrace+0x0/0x1b0
[ 0.066306] show_stack+0x24/0x30
[ 0.066311] dump_stack+0xb8/0xf0
[ 0.066316] swiotlb_alloc_coherent+0x17c/0x190
[ 0.066321] __dma_alloc+0x68/0x160
[ 0.066325] drm_gem_cma_create+0x98/0x120
[ 0.066330] drm_fbdev_cma_create+0x74/0x2e0
[ 0.066335] __drm_fb_helper_initial_config_and_unlock+0x1d8/0x3a0
[ 0.066341] drm_fb_helper_initial_config+0x4c/0x58
[ 0.066347] drm_fbdev_cma_init_with_funcs+0x98/0x148
[ 0.066352] drm_fbdev_cma_init+0x40/0x50
[ 0.066357] hdlcd_drm_bind+0x220/0x428
[ 0.066362] try_to_bring_up_master+0x21c/0x2b8
[ 0.066367] component_master_add_with_match+0xa8/0xf0
[ 0.066372] hdlcd_probe+0x60/0x78
[ 0.066377] platform_drv_probe+0x60/0xc8
[ 0.066382] driver_probe_device+0x30c/0x478
[ 0.066388] __driver_attach+0x10c/0x128
[ 0.066393] bus_for_each_dev+0x70/0xb0
[ 0.066398] driver_attach+0x30/0x40
[ 0.066402] bus_add_driver+0x1d0/0x298
[ 0.066408] driver_register+0x68/0x100
[ 0.066413] __platform_driver_register+0x54/0x60
[ 0.066418] hdlcd_platform_driver_init+0x20/0x28
[ 0.066424] do_one_initcall+0x44/0x130
[ 0.066428] kernel_init_freeable+0x13c/0x1d8
[ 0.066433] kernel_init+0x18/0x108
[ 0.066438] ret_from_fork+0x10/0x1c
[ 0.066444] hdlcd 2b000000.hdlcd: Failed to set initial hw configuration.
[ 0.066470] hdlcd 2b000000.hdlcd: master bind failed: -12
[ 0.066477] hdlcd: probe of 2b000000.hdlcd failed with error -12</pre>
</div>
</div>
<div class="paragraph">
<p>So what other options are missing from <code>gem5_defconfig</code>? It would be cool to minimize it out to better understand the options.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="x11"><a class="anchor" href="#x11"></a><a class="link" href="#x11">13.4. X11 Buildroot</a></h3>
<div class="paragraph">
<p>Once you&#8217;ve seen the <code>CONFIG_LOGO</code> penguin as a sanity check, you can try to go for a cooler X11 Buildroot setup.</p>
</div>
<div class="paragraph">
<p>Build and run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config-fragment buildroot_config/x11
./run --graphic</pre>
</div>
</div>
<div class="paragraph">
<p>Inside QEMU:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>startx</pre>
</div>
</div>
<div class="paragraph">
<p>And then from the GUI you can start exciting graphical programs such as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>xcalc
xeyes</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: <a href="#image-x11">Figure 1, &#8220;X11 Buildroot graphical user interface screenshot&#8221;</a></p>
</div>
<div id="image-x11" class="imageblock">
<div class="content">
<a class="image" href="x11.png"><img src="x11.png" alt="x11"></a>
</div>
<div class="title">Figure 1. X11 Buildroot graphical user interface screenshot</div>
</div>
<div class="paragraph">
<p>We don&#8217;t build X11 by default because it takes a considerable amount of time (about 20%), and is not expected to be used by most users: you need to pass the <code>-x</code> flag to enable it.</p>
</div>
<div class="paragraph">
<p>More details: <a href="https://unix.stackexchange.com/questions/70931/how-to-install-x11-on-my-own-linux-buildroot-system/306116#306116" class="bare">https://unix.stackexchange.com/questions/70931/how-to-install-x11-on-my-own-linux-buildroot-system/306116#306116</a></p>
</div>
<div class="paragraph">
<p>Not sure how well that graphics stack represents real systems, but if it does it would be a good way to understand how it works.</p>
</div>
<div class="paragraph">
<p>To x11 packages have an <code>xserver</code> prefix as in:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config-fragment buildroot_config/x11 -- xserver_xorg-server-reconfigure</pre>
</div>
</div>
<div class="paragraph">
<p>the easiest way to find them out is to just list <code>"$(./getvar buildroot_build_build_dir)/x*</code>.</p>
</div>
<div class="paragraph">
<p>TODO as of: c2696c978d6ca88e8b8599c92b1beeda80eb62b2 I noticed that <code>startx</code> leads to a <a href="#bug_on">BUG_ON</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[ 2.809104] WARNING: CPU: 0 PID: 51 at drivers/gpu/drm/ttm/ttm_bo_vm.c:304 ttm_bo_vm_open+0x37/0x40</pre>
</div>
</div>
<div class="sect3">
<h4 id="x11-buildroot-mouse-not-moving"><a class="anchor" href="#x11-buildroot-mouse-not-moving"></a><a class="link" href="#x11-buildroot-mouse-not-moving">13.4.1. X11 Buildroot mouse not moving</a></h4>
<div class="paragraph">
<p>TODO 9076c1d9bcc13b6efdb8ef502274f846d8d4e6a1 I&#8217;m 100% sure that it was working before, but I didn&#8217;t run it forever, and it stopped working at some point. Needs bisection, on whatever commit last touched x11 stuff.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://askubuntu.com/questions/730891/how-can-i-get-a-mouse-cursor-in-qemu" class="bare">https://askubuntu.com/questions/730891/how-can-i-get-a-mouse-cursor-in-qemu</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/19665412/mouse-and-keyboard-not-working-in-qemu-emulator" class="bare">https://stackoverflow.com/questions/19665412/mouse-and-keyboard-not-working-in-qemu-emulator</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><code>-show-cursor</code> did not help, I just get to see the host cursor, but the guest cursor still does not move.</p>
</div>
<div class="paragraph">
<p>Doing:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>watch -n 1 grep i8042 /proc/interrupts</pre>
</div>
</div>
<div class="paragraph">
<p>shows that interrupts do happen when mouse and keyboard presses are done, so I expect that it is some wrong either with:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>QEMU. Same behaviour if I try the host&#8217;s QEMU 2.10.1 however.</p>
</li>
<li>
<p>X11 configuration. We do have <code>BR2_PACKAGE_XDRIVER_XF86_INPUT_MOUSE=y</code>.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p><code>/var/log/Xorg.0.log</code> contains the following interesting lines:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[ 27.549] (II) LoadModule: "mouse"
[ 27.549] (II) Loading /usr/lib/xorg/modules/input/mouse_drv.so
[ 27.590] (EE) &lt;default pointer&gt;: Cannot find which device to use.
[ 27.590] (EE) &lt;default pointer&gt;: cannot open input device
[ 27.590] (EE) PreInit returned 2 for "&lt;default pointer&gt;"
[ 27.590] (II) UnloadModule: "mouse"</pre>
</div>
</div>
<div class="paragraph">
<p>The file <code>/dev/inputs/mice</code> does not exist.</p>
</div>
<div class="paragraph">
<p>Note that our current link:kernel_confi_fragment sets:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># CONFIG_INPUT_MOUSE is not set
# CONFIG_INPUT_MOUSEDEV_PSAUX is not set</pre>
</div>
</div>
<div class="paragraph">
<p>for gem5, so you might want to remove those lines to debug this.</p>
</div>
</div>
<div class="sect3">
<h4 id="x11-buildroot-arm"><a class="anchor" href="#x11-buildroot-arm"></a><a class="link" href="#x11-buildroot-arm">13.4.2. X11 Buildroot ARM</a></h4>
<div class="paragraph">
<p>On ARM, <code>startx</code> hangs at a message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vgaarb: this pci device is not a vga device</pre>
</div>
</div>
<div class="paragraph">
<p>and nothing shows on the screen, and:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>grep EE /var/log/Xorg.0.log</pre>
</div>
</div>
<div class="paragraph">
<p>says:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>(EE) Failed to load module "modesetting" (module does not exist, 0)</pre>
</div>
</div>
<div class="paragraph">
<p>A friend told me this but I haven&#8217;t tried it yet:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>xf86-video-modesetting</code> is likely the missing ingredient, but it does not seem possible to activate it from Buildroot currently without patching things.</p>
</li>
<li>
<p><code>xf86-video-fbdev</code> should work as well, but we need to make sure fbdev is enabled, and maybe add some line to the <code>Xorg.conf</code></p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="networking"><a class="anchor" href="#networking"></a><a class="link" href="#networking">14. Networking</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="enable-networking"><a class="anchor" href="#enable-networking"></a><a class="link" href="#enable-networking">14.1. Enable networking</a></h3>
<div class="paragraph">
<p>We disable networking by default because it starts an userland process, and we want to keep the number of userland processes to a minimum to make the system more understandable as explained at: <a href="#resource-tradeoff-guidelines">Section 31.18.3, &#8220;Resource tradeoff guidelines&#8221;</a></p>
</div>
<div class="paragraph">
<p>To enable networking on Buildroot, simply run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ifup -a</pre>
</div>
</div>
<div class="paragraph">
<p>That command goes over all (<code>-a</code>) the interfaces in <code>/etc/network/interfaces</code> and brings them up.</p>
</div>
<div class="paragraph">
<p>Then test it with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>wget google.com
cat index.html</pre>
</div>
</div>
<div class="paragraph">
<p>Disable networking with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ifdown -a</pre>
</div>
</div>
<div class="paragraph">
<p>To enable networking by default after boot, use the methods documented at <a href="#init-busybox">Run command at the end of BusyBox init</a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="ping"><a class="anchor" href="#ping"></a><a class="link" href="#ping">14.2. ping</a></h3>
<div class="paragraph">
<p><code>ping</code> does not work within QEMU by default, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ping google.com</pre>
</div>
</div>
<div class="paragraph">
<p>hangs after printing the header:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>PING google.com (216.58.204.46): 56 data bytes</pre>
</div>
</div>
<div class="paragraph">
<p>Here Ciro describes how to get it working: <a href="https://unix.stackexchange.com/questions/473448/how-to-ping-from-the-qemu-guest-to-an-external-url" class="bare">https://unix.stackexchange.com/questions/473448/how-to-ping-from-the-qemu-guest-to-an-external-url</a></p>
</div>
<div class="paragraph">
<p>Further bibliography: <a href="https://superuser.com/questions/787400/qemu-user-mode-networking-doesnt-work" class="bare">https://superuser.com/questions/787400/qemu-user-mode-networking-doesnt-work</a></p>
</div>
</div>
<div class="sect2">
<h3 id="guest-host-networking"><a class="anchor" href="#guest-host-networking"></a><a class="link" href="#guest-host-networking">14.3. Guest host networking</a></h3>
<div class="paragraph">
<p>In this section we discuss how to interact between the guest and the host through networking.</p>
</div>
<div class="paragraph">
<p>First ensure that you can access the external network since that is easier to get working, see: <a href="#networking">Section 14, &#8220;Networking&#8221;</a>.</p>
</div>
<div class="sect3">
<h4 id="host-to-guest-networking"><a class="anchor" href="#host-to-guest-networking"></a><a class="link" href="#host-to-guest-networking">14.3.1. Host to guest networking</a></h4>
<div class="sect4">
<h5 id="nc-host-to-guest"><a class="anchor" href="#nc-host-to-guest"></a><a class="link" href="#nc-host-to-guest">14.3.1.1. nc host to guest</a></h5>
<div class="paragraph">
<p>With <code>nc</code> we can create the most minimal example possible as a sanity check.</p>
</div>
<div class="paragraph">
<p>On guest run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>nc -l -p 45455</pre>
</div>
</div>
<div class="paragraph">
<p>Then on host run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo asdf | nc localhost 45455</pre>
</div>
</div>
<div class="paragraph">
<p><code>asdf</code> appears on the guest.</p>
</div>
<div class="paragraph">
<p>This uses:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>BusyBox' <code>nc</code> utility, which is enabled with <code>CONFIG_NC=y</code></p>
</li>
<li>
<p><code>nc</code> from the <code>netcat-openbsd</code> package on an Ubuntu 18.04 host</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Only this specific port works by default since we have forwarded it on the QEMU command line.</p>
</div>
<div class="paragraph">
<p>We us this exact procedure to connect to <a href="#gdbserver">gdbserver</a>.</p>
</div>
</div>
<div class="sect4">
<h5 id="ssh-into-guest"><a class="anchor" href="#ssh-into-guest"></a><a class="link" href="#ssh-into-guest">14.3.1.2. ssh into guest</a></h5>
<div class="paragraph">
<p>Not enabled by default due to the build / runtime overhead. To enable, build with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_OPENSSH=y'</pre>
</div>
</div>
<div class="paragraph">
<p>Then inside the guest turn on sshd:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./sshd.sh</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/sshd.sh">rootfs_overlay/lkmc/sshd.sh</a></p>
</div>
<div class="paragraph">
<p>And finally on host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ssh root@localhost -p 45456</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://unix.stackexchange.com/questions/124681/how-to-ssh-from-host-to-guest-using-qemu/307557#307557" class="bare">https://unix.stackexchange.com/questions/124681/how-to-ssh-from-host-to-guest-using-qemu/307557#307557</a></p>
</div>
</div>
<div class="sect4">
<h5 id="gem5-host-to-guest-networking"><a class="anchor" href="#gem5-host-to-guest-networking"></a><a class="link" href="#gem5-host-to-guest-networking">14.3.1.3. gem5 host to guest networking</a></h5>
<div class="paragraph">
<p>Could not do port forwarding from host to guest, and therefore could not use <code>gdbserver</code>: <a href="https://stackoverflow.com/questions/48941494/how-to-do-port-forwarding-from-guest-to-host-in-gem5" class="bare">https://stackoverflow.com/questions/48941494/how-to-do-port-forwarding-from-guest-to-host-in-gem5</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="guest-to-host-networking"><a class="anchor" href="#guest-to-host-networking"></a><a class="link" href="#guest-to-host-networking">14.3.2. Guest to host networking</a></h4>
<div class="paragraph">
<p>First <a href="#enable-networking">Enable networking</a>.</p>
</div>
<div class="paragraph">
<p>Then in the host, start a server:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>python -m SimpleHTTPServer 8000</pre>
</div>
</div>
<div class="paragraph">
<p>And then in the guest, find the IP we need to hit with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ip rounte</pre>
</div>
</div>
<div class="paragraph">
<p>which gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>default via 10.0.2.2 dev eth0
10.0.2.0/24 dev eth0 scope link src 10.0.2.15</pre>
</div>
</div>
<div class="paragraph">
<p>so we use in the guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>wget 10.0.2.2:8000</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://serverfault.com/questions/769874/how-to-forward-a-port-from-guest-to-host-in-qemu-kvm/951835#951835" class="bare">https://serverfault.com/questions/769874/how-to-forward-a-port-from-guest-to-host-in-qemu-kvm/951835#951835</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/78953/qemu-how-to-ping-host-network/547698#547698" class="bare">https://unix.stackexchange.com/questions/78953/qemu-how-to-ping-host-network/547698#547698</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="9p"><a class="anchor" href="#9p"></a><a class="link" href="#9p">14.4. 9P</a></h3>
<div class="paragraph">
<p>The <a href="https://en.wikipedia.org/wiki/9P_(protocol)">9p protocol</a> allows the guest to mount a host directory.</p>
</div>
<div class="paragraph">
<p>Both QEMU and <a href="#9p-gem5">9P gem5</a> support 9P.</p>
</div>
<div class="sect3">
<h4 id="9p-vs-nfs"><a class="anchor" href="#9p-vs-nfs"></a><a class="link" href="#9p-vs-nfs">14.4.1. 9P vs NFS</a></h4>
<div class="paragraph">
<p>All of 9P and NFS (and sshfs) allow sharing directories between guest and host.</p>
</div>
<div class="paragraph">
<p>Advantages of 9P</p>
</div>
<div class="ulist">
<ul>
<li>
<p>requires <code>sudo</code> on the host to mount</p>
</li>
<li>
<p>we could share a guest directory to the host, but this would require running a server on the guest, which adds <a href="#resource-tradeoff-guidelines">simulation overhead</a></p>
<div class="paragraph">
<p>Furthermore, this would be inconvenient, since what we usually want to do is to share host cross built files with the guest, and to do that we would have to copy the files over after the guest starts the server.</p>
</div>
</li>
<li>
<p>QEMU implements 9P natively, which makes it very stable and convenient, and must mean it is a simpler protocol than NFS as one would expect.</p>
<div class="paragraph">
<p>This is not the case for gem5 7bfb7f3a43f382eb49853f47b140bfd6caad0fb8 unfortunately, which relies on the <a href="https://github.com/chaos/diod">diod</a> host daemon, although it is not unfeasible that future versions could implement it natively as well.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Advantages of NFS:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>way more widely used and therefore stable and available, not to mention that it also works on real hardware.</p>
</li>
<li>
<p>the name does not start with a digit, which is an invalid identifier in all programming languages known to man. Who in their right mind would call a software project as such? It does not even match the natural order of Plan 9; Plan then 9: P9!</p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="9p-getting-started"><a class="anchor" href="#9p-getting-started"></a><a class="link" href="#9p-getting-started">14.4.2. 9P getting started</a></h4>
<div class="paragraph">
<p>As usual, we have already set everything up for you. On host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd "$(./getvar p9_dir)"
uname -a &gt; host</pre>
</div>
</div>
<div class="paragraph">
<p>Guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd /mnt/9p/data
cat host
uname -a &gt; guest</pre>
</div>
</div>
<div class="paragraph">
<p>Host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat guest</pre>
</div>
</div>
<div class="paragraph">
<p>The main ingredients for this are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>9P</code> settings in our <a href="#kernel-configs-about">kernel configs</a></p>
</li>
<li>
<p><code>9p</code> entry on our <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/etc/fstab">rootfs_overlay/etc/fstab</a></p>
<div class="paragraph">
<p>Alternatively, you could also mount your own with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mkdir /mnt/my9p
mount -t 9p -o trans=virtio,version=9p2000.L host0 /mnt/my9p</pre>
</div>
</div>
<div class="paragraph">
<p>where mount tag <code>host0</code> is set by the emulator (<code>mount_tag</code> flag on QEMU CLI), and can be found in the guest with: <code>cat /sys/bus/virtio/drivers/9pnet_virtio/virtio0/mount_tag</code> as documented at: <a href="https://www.kernel.org/doc/Documentation/filesystems/9p.txt" class="bare">https://www.kernel.org/doc/Documentation/filesystems/9p.txt</a>.</p>
</div>
</li>
<li>
<p>Launch QEMU with <code>-virtfs</code> as in your <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/run">run</a> script</p>
<div class="paragraph">
<p>When we tried:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>security_model=mapped</pre>
</div>
</div>
<div class="paragraph">
<p>writes from guest failed due to user mismatch problems: <a href="https://serverfault.com/questions/342801/read-write-access-for-passthrough-9p-filesystems-with-libvirt-qemu" class="bare">https://serverfault.com/questions/342801/read-write-access-for-passthrough-9p-filesystems-with-libvirt-qemu</a></p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://superuser.com/questions/628169/how-to-share-a-directory-with-the-host-without-networking-in-qemu" class="bare">https://superuser.com/questions/628169/how-to-share-a-directory-with-the-host-without-networking-in-qemu</a></p>
</li>
<li>
<p><a href="https://wiki.qemu.org/Documentation/9psetup" class="bare">https://wiki.qemu.org/Documentation/9psetup</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="9p-gem5"><a class="anchor" href="#9p-gem5"></a><a class="link" href="#9p-gem5">14.4.3. 9P gem5</a></h4>
<div class="paragraph">
<p>TODO seems possible! Lets do it:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="http://gem5.org/wiki/images/b/b8/Summit2017_wa_devlib.pdf" class="bare">http://gem5.org/wiki/images/b/b8/Summit2017_wa_devlib.pdf</a></p>
</li>
<li>
<p><a href="http://gem5.org/WA-gem5" class="bare">http://gem5.org/WA-gem5</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>From the source, there is just one exported tag named <code>gem5</code>, so we could try on the guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mkdir -p /mnt/9p/gem5
mount -t 9p -o trans=virtio,version=9p2000.L gem5 /mnt/9p/data</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="nfs"><a class="anchor" href="#nfs"></a><a class="link" href="#nfs">14.4.4. NFS</a></h4>
<div class="paragraph">
<p>TODO: get working.</p>
</div>
<div class="paragraph">
<p><a href="#9p">9P</a> is better with emulation, but let&#8217;s just get this working for fun.</p>
</div>
<div class="paragraph">
<p>First make sure that this works: <a href="#guest-to-host-networking">Section 14.3.2, &#8220;Guest to host networking&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Then, build the kernel with NFS support:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux --config-fragment linux_config/nfs</pre>
</div>
</div>
<div class="paragraph">
<p>Now on host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get install nfs-kernel-server</pre>
</div>
</div>
<div class="paragraph">
<p>Now edit <code>/etc/exports</code> to contain:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/tmp *(rw,sync,no_root_squash,no_subtree_check)</pre>
</div>
</div>
<div class="paragraph">
<p>and restart the server:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo systemctl restart nfs-kernel-server</pre>
</div>
</div>
<div class="paragraph">
<p>Now on guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mkdir /mnt/nfs
mount -t nfs 10.0.2.2:/tmp /mnt/nfs</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: failing with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mount: mounting 10.0.2.2:/tmp on /mnt/nfs failed: No such device</pre>
</div>
</div>
<div class="paragraph">
<p>And now the <code>/tmp</code> directory from host is not mounted on guest!</p>
</div>
<div class="paragraph">
<p>If you don&#8217;t want to start the NFS server after the next boot automatically so save resources, <a href="https://askubuntu.com/questions/19320/how-to-enable-or-disable-services">do</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>systemctl disable nfs-kernel-server</pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="linux-kernel"><a class="anchor" href="#linux-kernel"></a><a class="link" href="#linux-kernel">15. Linux kernel</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="linux-kernel-configuration"><a class="anchor" href="#linux-kernel-configuration"></a><a class="link" href="#linux-kernel-configuration">15.1. Linux kernel configuration</a></h3>
<div class="sect3">
<h4 id="modify-kernel-config"><a class="anchor" href="#modify-kernel-config"></a><a class="link" href="#modify-kernel-config">15.1.1. Modify kernel config</a></h4>
<div class="paragraph">
<p>To modify a single option on top of our <a href="#kernel-configs-about">default kernel configs</a>, do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux --config 'CONFIG_FORTIFY_SOURCE=y'</pre>
</div>
</div>
<div class="paragraph">
<p>Kernel modules depend on certain kernel configs, and therefore in general you might have to clean and rebuild the kernel modules after changing the kernel config:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-modules --clean
./build-modules</pre>
</div>
</div>
<div class="paragraph">
<p>and then proceed as in <a href="#your-first-kernel-module-hack">Your first kernel module hack</a>.</p>
</div>
<div class="paragraph">
<p>You might often get way without rebuilding the kernel modules however.</p>
</div>
<div class="paragraph">
<p>To use an extra kernel config fragment file on top of our defaults, do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>printf '
CONFIG_IKCONFIG=y
CONFIG_IKCONFIG_PROC=y
' &gt; data/myconfig
./build-linux --config-fragment 'data/myconfig'</pre>
</div>
</div>
<div class="paragraph">
<p>To use just your own exact <code>.config</code> instead of our defaults ones, use:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux --custom-config-file data/myconfig</pre>
</div>
</div>
<div class="paragraph">
<p>There is also a shortcut <code>--custom-config-file</code> to use the <a href="#gem5-arm-linux-kernel-patches">gem5 arm Linux kernel patches</a>.</p>
</div>
<div class="paragraph">
<p>The following options can all be used together, sorted by decreasing config setting power precedence:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>--config</code></p>
</li>
<li>
<p><code>--config-fragment</code></p>
</li>
<li>
<p><code>--custom-config-file</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>To do a clean menu config yourself and use that for the build, do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux --clean
./build-linux --custom-config-target menuconfig</pre>
</div>
</div>
<div class="paragraph">
<p>But remember that every new build re-configures the kernel by default, so to keep your configs you will need to use on further builds:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux --no-configure</pre>
</div>
</div>
<div class="paragraph">
<p>So what you likely want to do instead is to save that as a new <code>defconfig</code> and use it later as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux --no-configure --no-modules-install savedefconfig
cp "$(./getvar linux_build_dir)/defconfig" data/myconfig
./build-linux --custom-config-file data/myconfig</pre>
</div>
</div>
<div class="paragraph">
<p>You can also use other config generating targets such as <code>defconfig</code> with the same method as shown at: <a href="#linux-kernel-defconfig">Section 15.1.3.1.1, &#8220;Linux kernel defconfig&#8221;</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="find-the-kernel-config"><a class="anchor" href="#find-the-kernel-config"></a><a class="link" href="#find-the-kernel-config">15.1.2. Find the kernel config</a></h4>
<div class="paragraph">
<p>Get the build config in guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>zcat /proc/config.gz</pre>
</div>
</div>
<div class="paragraph">
<p>or with our shortcut:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./conf.sh</pre>
</div>
</div>
<div class="paragraph">
<p>or to conveniently grep for a specific option case insensitively:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./conf.sh ikconfig</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/conf.sh">rootfs_overlay/lkmc/conf.sh</a>.</p>
</div>
<div class="paragraph">
<p>This is enabled by:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>CONFIG_IKCONFIG=y
CONFIG_IKCONFIG_PROC=y</pre>
</div>
</div>
<div class="paragraph">
<p>From host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat "$(./getvar linux_config)"</pre>
</div>
</div>
<div class="paragraph">
<p>Just for fun <a href="https://stackoverflow.com/questions/14958192/how-to-get-the-config-from-a-linux-kernel-image/14958263#14958263" class="bare">https://stackoverflow.com/questions/14958192/how-to-get-the-config-from-a-linux-kernel-image/14958263#14958263</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./linux/scripts/extract-ikconfig "$(./getvar vmlinux)"</pre>
</div>
</div>
<div class="paragraph">
<p>although this can be useful when someone gives you a random image.</p>
</div>
</div>
<div class="sect3">
<h4 id="kernel-configs-about"><a class="anchor" href="#kernel-configs-about"></a><a class="link" href="#kernel-configs-about">15.1.3. About our Linux kernel configs</a></h4>
<div class="paragraph">
<p>By default, <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-linux">build-linux</a> generates a <code>.config</code> that is a mixture of:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>a base config extracted from Buildroot&#8217;s minimal per machine <code>.config</code>, which has the minimal options needed to boot as explained at: <a href="#buildroot-kernel-config">Section 15.1.3.1, &#8220;About Buildroot&#8217;s kernel configs&#8221;</a>.</p>
</li>
<li>
<p>small overlays put top of that</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>To find out which kernel configs are being used exactly, simply run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux --dry-run</pre>
</div>
</div>
<div class="paragraph">
<p>and look for the <code>merge_config.sh</code> call. This script from the Linux kernel tree, as the name suggests, merges multiple configuration files into one as explained at: <a href="https://unix.stackexchange.com/questions/224887/how-to-script-make-menuconfig-to-automate-linux-kernel-build-configuration/450407#450407" class="bare">https://unix.stackexchange.com/questions/224887/how-to-script-make-menuconfig-to-automate-linux-kernel-build-configuration/450407#450407</a></p>
</div>
<div class="paragraph">
<p>For each arch, the base of our configs are named as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>linux_config/buildroot-&lt;arch&gt;</pre>
</div>
</div>
<div class="paragraph">
<p>e.g.: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/linux_config/buildroot-x86_64">linux_config/buildroot-x86_64</a>.</p>
</div>
<div class="paragraph">
<p>These configs are extracted directly from a Buildroot build with <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/update-buildroot-kernel-configs">update-buildroot-kernel-configs</a>.</p>
</div>
<div class="paragraph">
<p>Note that Buildroot can <code>sed</code> override some of the configurations, e.g. it forces <code>CONFIG_BLK_DEV_INITRD=y</code> when <code>BR2_TARGET_ROOTFS_CPIO</code> is on. For this reason, those configs are not simply copy pasted from Buildroot files, but rather from a Buildroot kernel build, and then minimized with <code>make savedefconfig</code>: <a href="https://stackoverflow.com/questions/27899104/how-to-create-a-defconfig-file-from-a-config" class="bare">https://stackoverflow.com/questions/27899104/how-to-create-a-defconfig-file-from-a-config</a></p>
</div>
<div class="paragraph">
<p>On top of those, we add the following by default:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/linux_config/min">linux_config/min</a>: see: <a href="#linux-kernel-min-config">Section 15.1.3.1.2, &#8220;Linux kernel min config&#8221;</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/linux_config/default">linux_config/default</a>: other optional configs that we enable by default because they increase visibility, or expose some cool feature, and don&#8217;t significantly increase build time nor add significant runtime overhead</p>
<div class="paragraph">
<p>We have since observed that the kernel size itself is very bloated compared to <code>defconfig</code> as shown at: <a href="#linux-kernel-defconfig">Section 15.1.3.1.1, &#8220;Linux kernel defconfig&#8221;</a>.</p>
</div>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="buildroot-kernel-config"><a class="anchor" href="#buildroot-kernel-config"></a><a class="link" href="#buildroot-kernel-config">15.1.3.1. About Buildroot&#8217;s kernel configs</a></h5>
<div class="paragraph">
<p>To see Buildroot&#8217;s base configs, start from <a href="https://github.com/buildroot/buildroot/blob/2018.05/configs/qemu_x86_64_defconfig"><code>buildroot/configs/qemu_x86_64_defconfig</code></a>.</p>
</div>
<div class="paragraph">
<p>That file contains <code>BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/x86_64/linux-4.15.config"</code>, which points to the base config file used: <a href="https://github.com/buildroot/buildroot/blob/2018.05/board/qemu/x86_64/linux-4.15.config">board/qemu/x86_64/linux-4.15.config</a>.</p>
</div>
<div class="paragraph">
<p><code>arm</code>, on the other hand, uses <a href="https://github.com/buildroot/buildroot/blob/2018.05/configs/qemu_arm_vexpress_defconfig"><code>buildroot/configs/qemu_arm_vexpress_defconfig</code></a>, which contains <code>BR2_LINUX_KERNEL_DEFCONFIG="vexpress"</code>, and therefore just does a <code>make vexpress_defconfig</code>, and gets its config from the Linux kernel tree itself.</p>
</div>
<div class="sect5">
<h6 id="linux-kernel-defconfig"><a class="anchor" href="#linux-kernel-defconfig"></a><a class="link" href="#linux-kernel-defconfig">15.1.3.1.1. Linux kernel defconfig</a></h6>
<div class="paragraph">
<p>To boot <a href="https://stackoverflow.com/questions/41885015/what-exactly-does-linux-kernels-make-defconfig-do">defconfig</a> from disk on Linux and see a shell, all we need is these missing virtio options:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux \
--linux-build-id defconfig \
--custom-config-target defconfig \
--config CONFIG_VIRTIO_PCI=y \
--config CONFIG_VIRTIO_BLK=y \
;
./run --linux-build-id defconfig</pre>
</div>
</div>
<div class="paragraph">
<p>Oh, and check this out:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>du -h \
"$(./getvar vmlinux)" \
"$(./getvar --linux-build-id defconfig vmlinux)" \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>360M /path/to/linux-kernel-module-cheat/out/linux/default/x86_64/vmlinux
47M /path/to/linux-kernel-module-cheat/out/linux/defconfig/x86_64/vmlinux</pre>
</div>
</div>
<div class="paragraph">
<p>Brutal. Where did we go wrong?</p>
</div>
<div class="paragraph">
<p>The extra virtio options are not needed if we use <a href="#initrd">initrd</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux \
--linux-build-id defconfig \
--custom-config-target defconfig \
;
./run --initrd --linux-build-id defconfig</pre>
</div>
</div>
<div class="paragraph">
<p>On aarch64, we can boot from initrd with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux \
--arch aarch64 \
--linux-build-id defconfig \
--custom-config-target defconfig \
;
./run \
--arch aarch64 \
--initrd \
--linux-build-id defconfig \
--memory 2G \
;</pre>
</div>
</div>
<div class="paragraph">
<p>We need the 2G of memory because the CPIO is 600MiB due to a humongous amount of loadable kernel modules!</p>
</div>
<div class="paragraph">
<p>In aarch64, the size situation is inverted from x86_64, and this can be seen on the vmlinux size as well:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>118M /path/to/linux-kernel-module-cheat/out/linux/default/aarch64/vmlinux
240M /path/to/linux-kernel-module-cheat/out/linux/defconfig/aarch64/vmlinux</pre>
</div>
</div>
<div class="paragraph">
<p>So it seems that the ARM devs decided rather than creating a minimal config that boots QEMU, to try and make a single config that boots every board in existence. Terrible!</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://unix.stackexchange.com/questions/29439/compiling-the-kernel-with-default-configurations/204512#204512" class="bare">https://unix.stackexchange.com/questions/29439/compiling-the-kernel-with-default-configurations/204512#204512</a></p>
</div>
<div class="paragraph">
<p>Tested on 1e2b7f1e5e9e3073863dc17e25b2455c8ebdeadd + 1.</p>
</div>
</div>
<div class="sect5">
<h6 id="linux-kernel-min-config"><a class="anchor" href="#linux-kernel-min-config"></a><a class="link" href="#linux-kernel-min-config">15.1.3.1.2. Linux kernel min config</a></h6>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/linux_config/min">linux_config/min</a> contains minimal tweaks required to boot gem5 or for using our slightly different QEMU command line options than Buildroot on all archs.</p>
</div>
<div class="paragraph">
<p>It is one of the default config fragments we use, as explained at: <a href="#kernel-configs-about">Section 15.1.3, &#8220;About our Linux kernel configs&#8221;</a>&gt;.</p>
</div>
<div class="paragraph">
<p>Having the same config working for both QEMU and gem5 (oh, the hours of bisection) means that you can deal with functional matters in QEMU, which runs much faster, and switch to gem5 only for performance issues.</p>
</div>
<div class="paragraph">
<p>We can build just with <code>min</code> on top of the base config with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux \
--arch aarch64 \
--config-fragment linux_config/min \
--custom-config-file linux_config/buildroot-aarch64 \
--linux-build-id min \
;</pre>
</div>
</div>
<div class="paragraph">
<p>vmlinux had a very similar size to the default. It seems that <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/linux_config/buildroot-aarch64">linux_config/buildroot-aarch64</a> contains or implies most <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/linux_config/default">linux_config/default</a> options already? TODO: that seems odd, really?</p>
</div>
<div class="paragraph">
<p>Tested on 649d06d6758cefd080d04dc47fd6a5a26a620874 + 1.</p>
</div>
</div>
</div>
<div class="sect4">
<h5 id="notable-alternate-gem5-kernel-configs"><a class="anchor" href="#notable-alternate-gem5-kernel-configs"></a><a class="link" href="#notable-alternate-gem5-kernel-configs">15.1.3.2. Notable alternate gem5 kernel configs</a></h5>
<div class="paragraph">
<p>Other configs which we had previously tested at 4e0d9af81fcce2ce4e777cb82a1990d7c2ca7c1e are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>arm</code> and <code>aarch64</code> configs present in the official ARM gem5 Linux kernel fork as described at: <a href="#gem5-arm-linux-kernel-patches">Section 19.8, &#8220;gem5 arm Linux kernel patches&#8221;</a>. Some of the configs present there are added by the patches.</p>
</li>
<li>
<p>Jason&#8217;s magic <code>x86_64</code> config: <a href="http://web.archive.org/web/20171229121642/http://www.lowepower.com/jason/files/config" class="bare">http://web.archive.org/web/20171229121642/http://www.lowepower.com/jason/files/config</a> which is referenced at: <a href="http://web.archive.org/web/20171229121525/http://www.lowepower.com/jason/setting-up-gem5-full-system.html" class="bare">http://web.archive.org/web/20171229121525/http://www.lowepower.com/jason/setting-up-gem5-full-system.html</a>. QEMU boots with that by removing <code># CONFIG_VIRTIO_PCI is not set</code>.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="kernel-version"><a class="anchor" href="#kernel-version"></a><a class="link" href="#kernel-version">15.2. Kernel version</a></h3>
<div class="sect3">
<h4 id="find-the-kernel-version"><a class="anchor" href="#find-the-kernel-version"></a><a class="link" href="#find-the-kernel-version">15.2.1. Find the kernel version</a></h4>
<div class="paragraph">
<p>We try to use the latest possible kernel major release version.</p>
</div>
<div class="paragraph">
<p>In QEMU:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /proc/version</pre>
</div>
</div>
<div class="paragraph">
<p>or in the source:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd "$(./getvar linux_source_dir)"
git log | grep -E ' Linux [0-9]+\.' | head</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="update-the-linux-kernel"><a class="anchor" href="#update-the-linux-kernel"></a><a class="link" href="#update-the-linux-kernel">15.2.2. Update the Linux kernel</a></h4>
<div class="paragraph">
<p>During update all you kernel modules may break since the kernel API is not stable.</p>
</div>
<div class="paragraph">
<p>They are usually trivial breaks of things moving around headers or to sub-structs.</p>
</div>
<div class="paragraph">
<p>The userland, however, should simply not break, as Linus enforces strict backwards compatibility of userland interfaces.</p>
</div>
<div class="paragraph">
<p>This backwards compatibility is just awesome, it makes getting and running the latest master painless.</p>
</div>
<div class="paragraph">
<p>This also makes this repo the perfect setup to develop the Linux kernel.</p>
</div>
<div class="paragraph">
<p>In case something breaks while updating the Linux kernel, you can try to bisect it to understand the root cause, see: <a href="#bisection">Section 31.14, &#8220;Bisection&#8221;</a>.</p>
</div>
<div class="sect4">
<h5 id="update-the-linux-kernel-lkmc-procedure"><a class="anchor" href="#update-the-linux-kernel-lkmc-procedure"></a><a class="link" href="#update-the-linux-kernel-lkmc-procedure">15.2.2.1. Update the Linux kernel LKMC procedure</a></h5>
<div class="paragraph">
<p>First, use use the branching procedure described at: <a href="#update-a-forked-submodule">Section 31.16, &#8220;Update a forked submodule&#8221;</a></p>
</div>
<div class="paragraph">
<p>Because the kernel is so central to this repository, almost all tests must be re-run, so basically just follow the full testing procedure described at: <a href="#test-this-repo">Section 31.13, &#8220;Test this repo&#8221;</a>. The only tests that can be skipped are essentially the <a href="#baremetal">Baremetal</a> tests.</p>
</div>
<div class="paragraph">
<p>Before comitting, don&#8217;t forget to update:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the <code>linux_kernel_version</code> constant in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/common.py">common.py</a></p>
</li>
<li>
<p>the tagline of this repository on:</p>
<div class="ulist">
<ul>
<li>
<p>this README</p>
</li>
<li>
<p>the GitHub project description</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="sect3">
<h4 id="downgrade-the-linux-kernel"><a class="anchor" href="#downgrade-the-linux-kernel"></a><a class="link" href="#downgrade-the-linux-kernel">15.2.3. Downgrade the Linux kernel</a></h4>
<div class="paragraph">
<p>The kernel is not forward compatible, however, so downgrading the Linux kernel requires downgrading the userland too to the latest Buildroot branch that supports it.</p>
</div>
<div class="paragraph">
<p>The default Linux kernel version is bumped in Buildroot with commit messages of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>linux: bump default to version 4.9.6</pre>
</div>
</div>
<div class="paragraph">
<p>So you can try:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git log --grep 'linux: bump default to version'</pre>
</div>
</div>
<div class="paragraph">
<p>Those commits change <code>BR2_LINUX_KERNEL_LATEST_VERSION</code> in <code>/linux/Config.in</code>.</p>
</div>
<div class="paragraph">
<p>You should then look up if there is a branch that supports that kernel. Staying on branches is a good idea as they will get backports, in particular ones that fix the build as newer host versions come out.</p>
</div>
<div class="paragraph">
<p>Finally, after downgrading Buildroot, if something does not work, you might also have to make some changes to how this repo uses Buildroot, as the Buildroot configuration options might have changed.</p>
</div>
<div class="paragraph">
<p>We don&#8217;t expect those changes to be very difficult. A good way to approach the task is to:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>do a dry run build to get the equivalent Bash commands used:</p>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --dry-run</pre>
</div>
</div>
</li>
<li>
<p>build the Buildroot documentation for the version you are going to use, and check if all Buildroot build commands make sense there</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Then, if you spot an option that is wrong, some grepping in this repo should quickly point you to the code you need to modify.</p>
</div>
<div class="paragraph">
<p>It also possible that you will need to apply some patches from newer Buildroot versions for it to build, due to incompatibilities with the host Ubuntu packages and that Buildroot version. Just read the error message, and try:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>git log master&#8201;&#8212;&#8201;packages/&lt;pkg&gt;</code></p>
</li>
<li>
<p>Google the error message for mailing list hits</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Successful port reports:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>v3.18: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/issues/39#issuecomment-438525481" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/issues/39#issuecomment-438525481</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="kernel-command-line-parameters"><a class="anchor" href="#kernel-command-line-parameters"></a><a class="link" href="#kernel-command-line-parameters">15.3. Kernel command line parameters</a></h3>
<div class="paragraph">
<p>Bootloaders can pass a string as input to the Linux kernel when it is booting to control its behaviour, much like the <code>execve</code> system call does to userland processes.</p>
</div>
<div class="paragraph">
<p>This allows us to control the behaviour of the kernel without rebuilding anything.</p>
</div>
<div class="paragraph">
<p>With QEMU, QEMU itself acts as the bootloader, and provides the <code>-append</code> option and we expose it through <code>./run --kernel-cli</code>, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'foo bar'</pre>
</div>
</div>
<div class="paragraph">
<p>Then inside the host, you can check which options were given with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /proc/cmdline</pre>
</div>
</div>
<div class="paragraph">
<p>They are also printed at the beginning of the boot message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>dmesg | grep "Command line"</pre>
</div>
</div>
<div class="paragraph">
<p>See also:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://unix.stackexchange.com/questions/48601/how-to-display-the-linux-kernel-command-line-parameters-given-for-the-current-bo" class="bare">https://unix.stackexchange.com/questions/48601/how-to-display-the-linux-kernel-command-line-parameters-given-for-the-current-bo</a></p>
</li>
<li>
<p><a href="https://askubuntu.com/questions/32654/how-do-i-find-the-boot-parameters-used-by-the-running-kernel" class="bare">https://askubuntu.com/questions/32654/how-do-i-find-the-boot-parameters-used-by-the-running-kernel</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The arguments are documented in the kernel documentation: <a href="https://www.kernel.org/doc/html/v4.14/admin-guide/kernel-parameters.html" class="bare">https://www.kernel.org/doc/html/v4.14/admin-guide/kernel-parameters.html</a></p>
</div>
<div class="paragraph">
<p>When dealing with real boards, extra command line options are provided on some magic bootloader configuration file, e.g.:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>GRUB configuration files: <a href="https://askubuntu.com/questions/19486/how-do-i-add-a-kernel-boot-parameter" class="bare">https://askubuntu.com/questions/19486/how-do-i-add-a-kernel-boot-parameter</a></p>
</li>
<li>
<p>Raspberry pi <code>/boot/cmdline.txt</code> on a magic partition: <a href="https://raspberrypi.stackexchange.com/questions/14839/how-to-change-the-kernel-commandline-for-archlinuxarm-on-raspberry-pi-effectly" class="bare">https://raspberrypi.stackexchange.com/questions/14839/how-to-change-the-kernel-commandline-for-archlinuxarm-on-raspberry-pi-effectly</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="kernel-command-line-parameters-escaping"><a class="anchor" href="#kernel-command-line-parameters-escaping"></a><a class="link" href="#kernel-command-line-parameters-escaping">15.3.1. Kernel command line parameters escaping</a></h4>
<div class="paragraph">
<p>Double quotes can be used to escape spaces as in <code>opt="a b"</code>, but double quotes themselves cannot be escaped, e.g. <code>opt"a\"b"</code></p>
</div>
<div class="paragraph">
<p>This even lead us to use base64 encoding with <code>--eval</code>!</p>
</div>
</div>
<div class="sect3">
<h4 id="kernel-command-line-parameters-definition-points"><a class="anchor" href="#kernel-command-line-parameters-definition-points"></a><a class="link" href="#kernel-command-line-parameters-definition-points">15.3.2. Kernel command line parameters definition points</a></h4>
<div class="paragraph">
<p>There are two methods:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>__setup</code> as in:</p>
<div class="literalblock">
<div class="content">
<pre>__setup("console=", console_setup);</pre>
</div>
</div>
</li>
<li>
<p><code>core_param</code> as in:</p>
<div class="literalblock">
<div class="content">
<pre>core_param(panic, panic_timeout, int, 0644);</pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p><code>core_param</code> suggests how they are different:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/**
* core_param - define a historical core kernel parameter.
...
* core_param is just like module_param(), but cannot be modular and
* doesn't add a prefix (such as "printk."). This is for compatibility
* with __setup(), and it makes sense as truly core parameters aren't
* tied to the particular file they're in.
*/</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="rw"><a class="anchor" href="#rw"></a><a class="link" href="#rw">15.3.3. rw</a></h4>
<div class="paragraph">
<p>By default, the Linux kernel mounts the root filesystem as readonly. TODO rationale?</p>
</div>
<div class="paragraph">
<p>This cannot be observed in the default BusyBox init, because by default our <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/etc/inittab">rootfs_overlay/etc/inittab</a> does:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/bin/mount -o remount,rw /</pre>
</div>
</div>
<div class="paragraph">
<p>Analogously, Ubuntu 18.04 does in its fstab something like:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>UUID=/dev/sda1 / ext4 errors=remount-ro 0 1</pre>
</div>
</div>
<div class="paragraph">
<p>which uses default mount <code>rw</code> flags.</p>
</div>
<div class="paragraph">
<p>We have however removed those setups init setups to keep things more minimal, and replaced them with the <code>rw</code> kernel boot parameter makes the root mounted as writable.</p>
</div>
<div class="paragraph">
<p>To observe the default readonly behaviour, hack the <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/run">run</a> script to remove <a href="#replace-init">replace init</a>, and then run on a raw shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'init=/bin/sh'</pre>
</div>
</div>
<div class="paragraph">
<p>Now try to do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>touch a</pre>
</div>
</div>
<div class="paragraph">
<p>which fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>touch: a: Read-only file system</pre>
</div>
</div>
<div class="paragraph">
<p>We can also observe the read-onlyness with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mount -t proc /proc
mount</pre>
</div>
</div>
<div class="paragraph">
<p>which contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/dev/root on / type ext2 (ro,relatime,block_validity,barrier,user_xattr)</pre>
</div>
</div>
<div class="paragraph">
<p>and so it is Read Only as shown by <code>ro</code>.</p>
</div>
</div>
<div class="sect3">
<h4 id="norandmaps"><a class="anchor" href="#norandmaps"></a><a class="link" href="#norandmaps">15.3.4. norandmaps</a></h4>
<div class="paragraph">
<p>Disable userland address space randomization. Test it out by running <a href="#rand_check-out">rand_check.out</a> twice:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after './linux/rand_check.out;./linux/poweroff.out'
./run --eval-after './linux/rand_check.out;./linux/poweroff.out'</pre>
</div>
</div>
<div class="paragraph">
<p>If we remove it from our <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/run">run</a> script by hacking it up, the addresses shown by <code>linux/rand_check.out</code> vary across boots.</p>
</div>
<div class="paragraph">
<p>Equivalent to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 0 &gt; /proc/sys/kernel/randomize_va_space</pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="printk"><a class="anchor" href="#printk"></a><a class="link" href="#printk">15.4. printk</a></h3>
<div class="paragraph">
<p><code>printk</code> is the most simple and widely used way of getting information from the kernel, so you should familiarize yourself with its basic configuration.</p>
</div>
<div class="paragraph">
<p>We use <code>printk</code> a lot in our kernel modules, and it shows on the terminal by default, along with stdout and what you type.</p>
</div>
<div class="paragraph">
<p>Hide all <code>printk</code> messages:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>dmesg -n 1</pre>
</div>
</div>
<div class="paragraph">
<p>or equivalently:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 1 &gt; /proc/sys/kernel/printk</pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="https://superuser.com/questions/351387/how-to-stop-kernel-messages-from-flooding-my-console" class="bare">https://superuser.com/questions/351387/how-to-stop-kernel-messages-from-flooding-my-console</a></p>
</div>
<div class="paragraph">
<p>Do it with a <a href="#kernel-command-line-parameters">Kernel command line parameters</a> to affect the boot itself:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'loglevel=5'</pre>
</div>
</div>
<div class="paragraph">
<p>and now only boot warning messages or worse show, which is useful to identify problems.</p>
</div>
<div class="paragraph">
<p>Our default <code>printk</code> format is:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;LEVEL&gt;[TIMESTAMP] MESSAGE</pre>
</div>
</div>
<div class="paragraph">
<p>e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;6&gt;[ 2.979121] Freeing unused kernel memory: 2024K</pre>
</div>
</div>
<div class="paragraph">
<p>where:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>LEVEL</code>: higher means less serious</p>
</li>
<li>
<p><code>TIMESTAMP</code>: seconds since boot</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This format is selected by the following boot options:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>console_msg_format=syslog</code>: add the <code>&lt;LEVEL&gt;</code> part. Added in v4.16.</p>
</li>
<li>
<p><code>printk.time=y</code>: add the <code>[TIMESTAMP]</code> part</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The debug highest level is a bit more magic, see: <a href="#pr_debug">Section 15.4.3, &#8220;pr_debug&#8221;</a> for more info.</p>
</div>
<div class="sect3">
<h4 id="procsyskernelprintk"><a class="anchor" href="#procsyskernelprintk"></a><a class="link" href="#procsyskernelprintk">15.4.1. /proc/sys/kernel/printk</a></h4>
<div class="paragraph">
<p>The current printk level can be obtained with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /proc/sys/kernel/printk</pre>
</div>
</div>
<div class="paragraph">
<p>As of <code>87e846fc1f9c57840e143513ebd69c638bd37aa8</code> this prints:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>7 4 1 7</pre>
</div>
</div>
<div class="paragraph">
<p>which contains:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>7</code>: current log level, modifiable by previously mentioned methods</p>
</li>
<li>
<p><code>4</code>: documented as: "printk&#8217;s without a loglevel use this": TODO what does that mean, how to call <code>printk</code> without a log level?</p>
</li>
<li>
<p><code>1</code>: minimum log level that still prints something (<code>0</code> prints nothing)</p>
</li>
<li>
<p><code>7</code>: default log level</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We start at the boot time default after boot by default, as can be seen from:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod myprintk.ko</pre>
</div>
</div>
<div class="paragraph">
<p>which outputs something like:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;1&gt;[ 12.494429] pr_alert
&lt;2&gt;[ 12.494666] pr_crit
&lt;3&gt;[ 12.494823] pr_err
&lt;4&gt;[ 12.494911] pr_warning
&lt;5&gt;[ 12.495170] pr_notice
&lt;6&gt;[ 12.495327] pr_info</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/myprintk.c">kernel_modules/myprintk.c</a></p>
</div>
<div class="paragraph">
<p>This proc entry is defined at: <a href="https://github.com/torvalds/linux/blob/v5.1/kernel/sysctl.c#L839" class="bare">https://github.com/torvalds/linux/blob/v5.1/kernel/sysctl.c#L839</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre>#if defined CONFIG_PRINTK
{
.procname = "printk",
.data = &amp;console_loglevel,
.maxlen = 4*sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec,
},</pre>
</div>
</div>
<div class="paragraph">
<p>which teaches us that printk can be completely disabled at compile time:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>config PRINTK
default y
bool "Enable support for printk" if EXPERT
select IRQ_WORK
help
This option enables normal printk support. Removing it
eliminates most of the message strings from the kernel image
and makes the kernel more or less silent. As this makes it
very difficult to diagnose system problems, saying N here is
strongly discouraged.</pre>
</div>
</div>
<div class="paragraph">
<p><code>console_loglevel</code> is defined at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>#define console_loglevel (console_printk[0])</pre>
</div>
</div>
<div class="paragraph">
<p>and <code>console_printk</code> is an array with 4 ints:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>int console_printk[4] = {
CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */
MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */
CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */
CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */
};</pre>
</div>
</div>
<div class="paragraph">
<p>and then we see that the default is configurable with <code>CONFIG_CONSOLE_LOGLEVEL_DEFAULT</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/*
* Default used to be hard-coded at 7, quiet used to be hardcoded at 4,
* we're now allowing both to be set from kernel config.
*/
#define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
#define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET</pre>
</div>
</div>
<div class="paragraph">
<p>The message loglevel default is explained at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/* printk's without a loglevel use this.. */
#define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT</pre>
</div>
</div>
<div class="paragraph">
<p>The min is just hardcoded to one as you would expect, with some amazing kernel comedy around it:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/* We show everything that is MORE important than this.. */
#define CONSOLE_LOGLEVEL_SILENT 0 /* Mum's the word */
#define CONSOLE_LOGLEVEL_MIN 1 /* Minimum loglevel we let people use */
#define CONSOLE_LOGLEVEL_DEBUG 10 /* issue debug messages */
#define CONSOLE_LOGLEVEL_MOTORMOUTH 15 /* You can't shut this one up */</pre>
</div>
</div>
<div class="paragraph">
<p>We then also learn about the useless <code>quiet</code> and <code>debug</code> kernel parameters at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>config CONSOLE_LOGLEVEL_QUIET
int "quiet console loglevel (1-15)"
range 1 15
default "4"
help
loglevel to use when "quiet" is passed on the kernel commandline.
When "quiet" is passed on the kernel commandline this loglevel
will be used as the loglevel. IOW passing "quiet" will be the
equivalent of passing "loglevel=&lt;CONSOLE_LOGLEVEL_QUIET&gt;"</pre>
</div>
</div>
<div class="paragraph">
<p>which explains the useless reason why that number is special. This is implemented at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>static int __init debug_kernel(char *str)
{
console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
return 0;
}
static int __init quiet_kernel(char *str)
{
console_loglevel = CONSOLE_LOGLEVEL_QUIET;
return 0;
}
early_param("debug", debug_kernel);
early_param("quiet", quiet_kernel);</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="ignore_loglevel"><a class="anchor" href="#ignore_loglevel"></a><a class="link" href="#ignore_loglevel">15.4.2. ignore_loglevel</a></h4>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'ignore_loglevel'</pre>
</div>
</div>
<div class="paragraph">
<p>enables all log levels, and is basically the same as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'loglevel=8'</pre>
</div>
</div>
<div class="paragraph">
<p>except that you don&#8217;t need to know what is the maximum level.</p>
</div>
</div>
<div class="sect3">
<h4 id="pr_debug"><a class="anchor" href="#pr_debug"></a><a class="link" href="#pr_debug">15.4.3. pr_debug</a></h4>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/28936199/why-is-pr-debug-of-the-linux-kernel-not-giving-any-output/49835405#49835405" class="bare">https://stackoverflow.com/questions/28936199/why-is-pr-debug-of-the-linux-kernel-not-giving-any-output/49835405#49835405</a></p>
</div>
<div class="paragraph">
<p>Debug messages are not printable by default without recompiling.</p>
</div>
<div class="paragraph">
<p>But the awesome <code>CONFIG_DYNAMIC_DEBUG=y</code> option which we enable by default allows us to do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 8 &gt; /proc/sys/kernel/printk
echo 'file kernel/module.c +p' &gt; /sys/kernel/debug/dynamic_debug/control
./linux/myinsmod.out hello.ko</pre>
</div>
</div>
<div class="paragraph">
<p>and we have a shortcut at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./pr_debug.sh</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/pr_debug.sh">rootfs_overlay/lkmc/pr_debug.sh</a>.</p>
</div>
<div class="paragraph">
<p>Syntax: <a href="https://www.kernel.org/doc/html/v4.11/admin-guide/dynamic-debug-howto.html" class="bare">https://www.kernel.org/doc/html/v4.11/admin-guide/dynamic-debug-howto.html</a></p>
</div>
<div class="paragraph">
<p>Wildcards are also accepted, e.g. enable all messages from all files:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 'file * +p' &gt; /sys/kernel/debug/dynamic_debug/control</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: why is this not working:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 'func sys_init_module +p' &gt; /sys/kernel/debug/dynamic_debug/control</pre>
</div>
</div>
<div class="paragraph">
<p>Enable messages in specific modules:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 8 &gt; /proc/sys/kernel/printk
echo 'module myprintk +p' &gt; /sys/kernel/debug/dynamic_debug/control
insmod myprintk.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/myprintk.c">kernel_modules/myprintk.c</a></p>
</div>
<div class="paragraph">
<p>This outputs the <code>pr_debug</code> message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>printk debug</pre>
</div>
</div>
<div class="paragraph">
<p>but TODO: it also shows debug messages even without enabling them explicitly:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 8 &gt; /proc/sys/kernel/printk
insmod myprintk.ko</pre>
</div>
</div>
<div class="paragraph">
<p>and it shows as enabled:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># grep myprintk /sys/kernel/debug/dynamic_debug/control
/root/linux-kernel-module-cheat/out/kernel_modules/x86_64/kernel_modules/panic.c:12 [myprintk]myinit =p "pr_debug\012"</pre>
</div>
</div>
<div class="paragraph">
<p>Enable <code>pr_debug</code> for boot messages as well, before we can reach userland and write to <code>/proc</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'dyndbg="file * +p" loglevel=8'</pre>
</div>
</div>
<div class="paragraph">
<p>Get ready for the noisiest boot ever, I think it overflows the <code>printk</code> buffer and funny things happen.</p>
</div>
<div class="sect4">
<h5 id="pr_debug-printkkern_debug"><a class="anchor" href="#pr_debug-printkkern_debug"></a><a class="link" href="#pr_debug-printkkern_debug">15.4.3.1. pr_debug != printk(KERN_DEBUG</a></h5>
<div class="paragraph">
<p>When <code>CONFIG_DYNAMIC_DEBUG</code> is set, <code>printk(KERN_DEBUG</code> is not the exact same as <code>pr_debug(</code> since <code>printk(KERN_DEBUG</code> messages are visible with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'initcall_debug logleve=8'</pre>
</div>
</div>
<div class="paragraph">
<p>which outputs lines of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;7&gt;[ 1.756680] calling clk_disable_unused+0x0/0x130 @ 1
&lt;7&gt;[ 1.757003] initcall clk_disable_unused+0x0/0x130 returned 0 after 111 usecs</pre>
</div>
</div>
<div class="paragraph">
<p>which are <code>printk(KERN_DEBUG</code> inside <code>init/main.c</code> in v4.16.</p>
</div>
<div class="paragraph">
<p>Mentioned at: <a href="https://stackoverflow.com/questions/37272109/how-to-get-details-of-all-modules-drivers-got-initialized-probed-during-kernel-b" class="bare">https://stackoverflow.com/questions/37272109/how-to-get-details-of-all-modules-drivers-got-initialized-probed-during-kernel-b</a></p>
</div>
<div class="paragraph">
<p>This likely comes from the ifdef split at <code>init/main.c</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/* If you are writing a driver, please use dev_dbg instead */
#if defined(CONFIG_DYNAMIC_DEBUG)
#include &lt;linux/dynamic_debug.h&gt;
/* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
#define pr_debug(fmt, ...) \
dynamic_pr_debug(fmt, ##__VA_ARGS__)
#elif defined(DEBUG)
#define pr_debug(fmt, ...) \
printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_debug(fmt, ...) \
no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#endif</pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="linux-kernel-entry-point"><a class="anchor" href="#linux-kernel-entry-point"></a><a class="link" href="#linux-kernel-entry-point">15.5. Linux kernel entry point</a></h3>
<div class="paragraph">
<p><code>start_kernel</code> is a good definition of it: <a href="https://stackoverflow.com/questions/18266063/does-kernel-have-main-function/33422401#33422401" class="bare">https://stackoverflow.com/questions/18266063/does-kernel-have-main-function/33422401#33422401</a></p>
</div>
</div>
<div class="sect2">
<h3 id="kernel-module-apis"><a class="anchor" href="#kernel-module-apis"></a><a class="link" href="#kernel-module-apis">15.6. Kernel module APIs</a></h3>
<div class="sect3">
<h4 id="kernel-module-parameters"><a class="anchor" href="#kernel-module-parameters"></a><a class="link" href="#kernel-module-parameters">15.6.1. Kernel module parameters</a></h4>
<div class="paragraph">
<p>The Linux kernel allows passing module parameters at insertion time <a href="#myinsmod">through the <code>init_module</code> and <code>finit_module</code> system calls</a>.</p>
</div>
<div class="paragraph">
<p>The <code>insmod</code> tool exposes that as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod params.ko i=3 j=4</pre>
</div>
</div>
<div class="paragraph">
<p>Parameters are declared in the module as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>static u32 i = 0;
module_param(i, int, S_IRUSR | S_IWUSR);
MODULE_PARM_DESC(i, "my favorite int");</pre>
</div>
</div>
<div class="paragraph">
<p>Automated test:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./params.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/params.c">kernel_modules/params.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/params.sh">rootfs_overlay/lkmc/params.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>As shown in the example, module parameters can also be read and modified at runtime from <a href="#sysfs">sysfs</a>.</p>
</div>
<div class="paragraph">
<p>We can obtain the help text of the parameters with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>modinfo params.ko</pre>
</div>
</div>
<div class="paragraph">
<p>The output contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>parm: j:my second favorite int
parm: i:my favorite int</pre>
</div>
</div>
<div class="sect4">
<h5 id="modprobe-conf"><a class="anchor" href="#modprobe-conf"></a><a class="link" href="#modprobe-conf">15.6.1.1. modprobe.conf</a></h5>
<div class="paragraph">
<p><a href="#modprobe">modprobe</a> insertion can also set default parameters via the <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/etc/modprobe.conf"><code>/etc/modprobe.conf</code></a> file:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>modprobe params
cat /sys/kernel/debug/lkmc_params</pre>
</div>
</div>
<div class="paragraph">
<p>Output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>12 34</pre>
</div>
</div>
<div class="paragraph">
<p>This is specially important when loading modules with <a href="#kernel-module-dependencies">Kernel module dependencies</a> or else we would have no opportunity of passing those.</p>
</div>
<div class="paragraph">
<p><code>modprobe.conf</code> doesn&#8217;t actually insmod anything for us: <a href="https://superuser.com/questions/397842/automatically-load-kernel-module-at-boot-angstrom/1267464#1267464" class="bare">https://superuser.com/questions/397842/automatically-load-kernel-module-at-boot-angstrom/1267464#1267464</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="kernel-module-dependencies"><a class="anchor" href="#kernel-module-dependencies"></a><a class="link" href="#kernel-module-dependencies">15.6.2. Kernel module dependencies</a></h4>
<div class="paragraph">
<p>One module can depend on symbols of another module that are exported with <code>EXPORT_SYMBOL</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./dep.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/dep.c">kernel_modules/dep.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/dep2.c">kernel_modules/dep2.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/dep.sh">rootfs_overlay/lkmc/dep.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The kernel deduces dependencies based on the <code>EXPORT_SYMBOL</code> that each module uses.</p>
</div>
<div class="paragraph">
<p>Symbols exported by <code>EXPORT_SYMBOL</code> can be seen with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod dep.ko
grep lkmc_dep /proc/kallsyms</pre>
</div>
</div>
<div class="paragraph">
<p>sample output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ffffffffc0001030 r __ksymtab_lkmc_dep [dep]
ffffffffc000104d r __kstrtab_lkmc_dep [dep]
ffffffffc0002300 B lkmc_dep [dep]</pre>
</div>
</div>
<div class="paragraph">
<p>This requires <code>CONFIG_KALLSYMS_ALL=y</code>.</p>
</div>
<div class="paragraph">
<p>Dependency information is stored by the kernel module build system in the <code>.ko</code> files' <a href="#module_info">MODULE_INFO</a>, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>modinfo dep2.ko</pre>
</div>
</div>
<div class="paragraph">
<p>contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>depends: dep</pre>
</div>
</div>
<div class="paragraph">
<p>We can double check with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>strings 3 dep2.ko | grep -E 'depends'</pre>
</div>
</div>
<div class="paragraph">
<p>The output contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>depends=dep</pre>
</div>
</div>
<div class="paragraph">
<p>Module dependencies are also stored at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd /lib/module/*
grep dep modules.dep</pre>
</div>
</div>
<div class="paragraph">
<p>Output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>extra/dep2.ko: extra/dep.ko
extra/dep.ko:</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: what for, and at which point point does Buildroot / BusyBox generate that file?</p>
</div>
<div class="sect4">
<h5 id="kernel-module-dependencies-with-modprobe"><a class="anchor" href="#kernel-module-dependencies-with-modprobe"></a><a class="link" href="#kernel-module-dependencies-with-modprobe">15.6.2.1. Kernel module dependencies with modprobe</a></h5>
<div class="paragraph">
<p>Unlike <code>insmod</code>, <a href="#modprobe">modprobe</a> deals with kernel module dependencies for us.</p>
</div>
<div class="paragraph">
<p>First get <a href="#kernel_modules-buildroot-package">kernel_modules buildroot package</a> working.</p>
</div>
<div class="paragraph">
<p>Then, for example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>modprobe buildroot_dep2</pre>
</div>
</div>
<div class="paragraph">
<p>outputs to dmesg:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>42</pre>
</div>
</div>
<div class="paragraph">
<p>and then:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lsmod</pre>
</div>
</div>
<div class="paragraph">
<p>outputs:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Module Size Used by Tainted: G
buildroot_dep2 16384 0
buildroot_dep 16384 1 buildroot_dep2</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_packages/kernel_modules/buildroot_dep.c">buildroot_packages/kernel_modules/buildroot_dep.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_packages/kernel_modules/buildroot_dep2.c">buildroot_packages/kernel_modules/buildroot_dep2.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Removal also removes required modules that have zero usage count:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>modprobe -r buildroot_dep2</pre>
</div>
</div>
<div class="paragraph">
<p><code>modprobe</code> uses information from the <code>modules.dep</code> file to decide the required dependencies. That file contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>extra/buildroot_dep2.ko: extra/buildroot_dep.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://askubuntu.com/questions/20070/whats-the-difference-between-insmod-and-modprobe" class="bare">https://askubuntu.com/questions/20070/whats-the-difference-between-insmod-and-modprobe</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/22891705/whats-the-difference-between-insmod-and-modprobe" class="bare">https://stackoverflow.com/questions/22891705/whats-the-difference-between-insmod-and-modprobe</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect3">
<h4 id="module_info"><a class="anchor" href="#module_info"></a><a class="link" href="#module_info">15.6.3. MODULE_INFO</a></h4>
<div class="paragraph">
<p>Module metadata is stored on module files at compile time. Some of the fields can be retrieved through the <code>THIS_MODULE</code> <code>struct module</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod module_info.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Dmesg output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>name = module_info
version = 1.0</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/module_info.c">kernel_modules/module_info.c</a></p>
</div>
<div class="paragraph">
<p>Some of those are also present on sysfs:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /sys/module/module_info/version</pre>
</div>
</div>
<div class="paragraph">
<p>Output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>1.0</pre>
</div>
</div>
<div class="paragraph">
<p>And we can also observe them with the <code>modinfo</code> command line utility:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>modinfo module_info.ko</pre>
</div>
</div>
<div class="paragraph">
<p>sample output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>filename: module_info.ko
license: GPL
version: 1.0
srcversion: AF3DE8A8CFCDEB6B00E35B6
depends:
vermagic: 4.17.0 SMP mod_unload modversions</pre>
</div>
</div>
<div class="paragraph">
<p>Module information is stored in a special <code>.modinfo</code> section of the ELF file:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain readelf -- -SW "$(./getvar kernel_modules_build_subdir)/module_info.ko"</pre>
</div>
</div>
<div class="paragraph">
<p>contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> [ 5] .modinfo PROGBITS 0000000000000000 0000d8 000096 00 A 0 0 8</pre>
</div>
</div>
<div class="paragraph">
<p>and:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain readelf -- -x .modinfo "$(./getvar kernel_modules_build_subdir)/module_info.ko"</pre>
</div>
</div>
<div class="paragraph">
<p>gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> 0x00000000 6c696365 6e73653d 47504c00 76657273 license=GPL.vers
0x00000010 696f6e3d 312e3000 61736466 3d717765 ion=1.0.asdf=qwe
0x00000020 72000000 00000000 73726376 65727369 r.......srcversi
0x00000030 6f6e3d41 46334445 38413843 46434445 on=AF3DE8A8CFCDE
0x00000040 42364230 30453335 42360000 00000000 B6B00E35B6......
0x00000050 64657065 6e64733d 006e616d 653d6d6f depends=.name=mo
0x00000060 64756c65 5f696e66 6f007665 726d6167 dule_info.vermag
0x00000070 69633d34 2e31372e 3020534d 50206d6f ic=4.17.0 SMP mo
0x00000080 645f756e 6c6f6164 206d6f64 76657273 d_unload modvers
0x00000090 696f6e73 2000 ions .</pre>
</div>
</div>
<div class="paragraph">
<p>I think a dedicated section is used to allow the Linux kernel and command line tools to easily parse that information from the ELF file as we&#8217;ve done with <code>readelf</code>.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/19467150/significance-of-this-module-in-linux-driver/49812248#49812248" class="bare">https://stackoverflow.com/questions/19467150/significance-of-this-module-in-linux-driver/49812248#49812248</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/4839024/how-to-find-the-version-of-a-compiled-kernel-module/42556565#42556565" class="bare">https://stackoverflow.com/questions/4839024/how-to-find-the-version-of-a-compiled-kernel-module/42556565#42556565</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/238167/how-to-understand-the-modinfo-output" class="bare">https://unix.stackexchange.com/questions/238167/how-to-understand-the-modinfo-output</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="vermagic"><a class="anchor" href="#vermagic"></a><a class="link" href="#vermagic">15.6.4. vermagic</a></h4>
<div class="paragraph">
<p>Vermagic is a magic string present in the kernel and on <a href="#module_info">MODULE_INFO</a> of kernel modules. It is used to verify that the kernel module was compiled against a compatible kernel version and relevant configuration:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod vermagic.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Possible dmesg output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>VERMAGIC_STRING = 4.17.0 SMP mod_unload modversions</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/vermagic.c">kernel_modules/vermagic.c</a></p>
</div>
<div class="paragraph">
<p>If we artificially create a mismatch with <code>MODULE_INFO(vermagic</code>, the insmod fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod: can't insert 'vermagic_fail.ko': invalid module format</pre>
</div>
</div>
<div class="paragraph">
<p>and <code>dmesg</code> says the expected and found vermagic found:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vermagic_fail: version magic 'asdfqwer' should be '4.17.0 SMP mod_unload modversions '</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/vermagic_fail.c">kernel_modules/vermagic_fail.c</a></p>
</div>
<div class="paragraph">
<p>The kernel&#8217;s vermagic is defined based on compile time configurations at <a href="https://github.com/torvalds/linux/blob/v4.17/include/linux/vermagic.h#L35">include/linux/vermagic.h</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>#define VERMAGIC_STRING \
UTS_RELEASE " " \
MODULE_VERMAGIC_SMP MODULE_VERMAGIC_PREEMPT \
MODULE_VERMAGIC_MODULE_UNLOAD MODULE_VERMAGIC_MODVERSIONS \
MODULE_ARCH_VERMAGIC \
MODULE_RANDSTRUCT_PLUGIN</pre>
</div>
</div>
<div class="paragraph">
<p>The <code>SMP</code> part of the string for example is defined on the same file based on the value of <code>CONFIG_SMP</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>#ifdef CONFIG_SMP
#define MODULE_VERMAGIC_SMP "SMP "
#else
#define MODULE_VERMAGIC_SMP ""</pre>
</div>
</div>
<div class="paragraph">
<p>TODO how to get the vermagic from running kernel from userland? <a href="https://lists.kernelnewbies.org/pipermail/kernelnewbies/2012-October/006306.html" class="bare">https://lists.kernelnewbies.org/pipermail/kernelnewbies/2012-October/006306.html</a></p>
</div>
<div class="paragraph">
<p><a href="#kmod-modprobe">kmod modprobe</a> has a flag to skip the vermagic check:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>--force-modversion</pre>
</div>
</div>
<div class="paragraph">
<p>This option just strips <code>modversion</code> information from the module before loading, so it is not a kernel feature.</p>
</div>
</div>
<div class="sect3">
<h4 id="init_module"><a class="anchor" href="#init_module"></a><a class="link" href="#init_module">15.6.5. init_module</a></h4>
<div class="paragraph">
<p><code>init_module</code> and <code>cleanup_module</code> are an older alternative to the <code>module_init</code> and <code>module_exit</code> macros:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod init_module.ko
rmmod init_module</pre>
</div>
</div>
<div class="paragraph">
<p>Dmesg output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>init_module
cleanup_module</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/init_module.c">kernel_modules/init_module.c</a></p>
</div>
<div class="paragraph">
<p>TODO why were <code>module_init</code> and <code>module_exit</code> created? <a href="https://stackoverflow.com/questions/3218320/what-is-the-difference-between-module-init-and-init-module-in-a-linux-kernel-mod" class="bare">https://stackoverflow.com/questions/3218320/what-is-the-difference-between-module-init-and-init-module-in-a-linux-kernel-mod</a></p>
</div>
</div>
<div class="sect3">
<h4 id="floating-point-in-kernel-modules"><a class="anchor" href="#floating-point-in-kernel-modules"></a><a class="link" href="#floating-point-in-kernel-modules">15.6.6. Floating point in kernel modules</a></h4>
<div class="paragraph">
<p>It is generally hard / impossible to use floating point operations in the kernel. TODO understand details.</p>
</div>
<div class="paragraph">
<p>A quick (x86-only for now because lazy) example is shown at: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/float.c">kernel_modules/float.c</a></p>
</div>
<div class="paragraph">
<p>Usage:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod float.ko myfloat=1 enable_fpu=1</pre>
</div>
</div>
<div class="paragraph">
<p>We have to call: <code>kernel_fpu_begin()</code> before starting FPU operations, and <code>kernel_fpu_end()</code> when we are done. This particular example however did not blow up without it at lkmc 7f917af66b17373505f6c21d75af9331d624b3a9 + 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod float.ko myfloat=1 enable_fpu=0</pre>
</div>
</div>
<div class="paragraph">
<p>The v5.1 documentation under <a href="https://github.com/cirosantilli/linux/blob/v5.1/arch/x86/include/asm/fpu/api.h#L15">arch/x86/include/asm/fpu/api.h</a> reads:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> * Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It
* disables preemption so be careful if you intend to use it for long periods
* of time.</pre>
</div>
</div>
<div class="paragraph">
<p>The example sets in the <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/Makefile">kernel_modules/Makefile</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>CFLAGS_REMOVE_float.o += -mno-sse -mno-sse2</pre>
</div>
</div>
<div class="paragraph">
<p>to avoid:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>error: SSE register return with SSE disabled</pre>
</div>
</div>
<div class="paragraph">
<p>We found those flags with <code>./build-modules --verbose</code>.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/13886338/use-of-floating-point-in-the-linux-kernel" class="bare">https://stackoverflow.com/questions/13886338/use-of-floating-point-in-the-linux-kernel</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/15883947/why-am-i-able-to-perform-floating-point-operations-inside-a-linux-kernel-module/47056242" class="bare">https://stackoverflow.com/questions/15883947/why-am-i-able-to-perform-floating-point-operations-inside-a-linux-kernel-module/47056242</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/1556142/sse-register-return-with-sse-disabled" class="bare">https://stackoverflow.com/questions/1556142/sse-register-return-with-sse-disabled</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="kernel-panic-and-oops"><a class="anchor" href="#kernel-panic-and-oops"></a><a class="link" href="#kernel-panic-and-oops">15.7. Kernel panic and oops</a></h3>
<div class="paragraph">
<p>To test out kernel panics and oops in controlled circumstances, try out the modules:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod panic.ko
insmod oops.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Source:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/panic.c">kernel_modules/panic.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/oops.c">kernel_modules/oops.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>A panic can also be generated with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo c &gt; /proc/sysrq-trigger</pre>
</div>
</div>
<div class="paragraph">
<p>Panic vs oops: <a href="https://unix.stackexchange.com/questions/91854/whats-the-difference-between-a-kernel-oops-and-a-kernel-panic" class="bare">https://unix.stackexchange.com/questions/91854/whats-the-difference-between-a-kernel-oops-and-a-kernel-panic</a></p>
</div>
<div class="paragraph">
<p>How to generate them:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://unix.stackexchange.com/questions/66197/how-to-cause-kernel-panic-with-a-single-command" class="bare">https://unix.stackexchange.com/questions/66197/how-to-cause-kernel-panic-with-a-single-command</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/23484147/generate-kernel-oops-or-crash-in-the-code" class="bare">https://stackoverflow.com/questions/23484147/generate-kernel-oops-or-crash-in-the-code</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>When a panic happens, <a href="#linux-kernel-magic-keys"><code>Shift-PgUp</code></a> does not work as it normally does, and it is hard to get the logs if on are on <a href="#qemu-graphic-mode">QEMU graphic mode</a>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://superuser.com/questions/848412/scrolling-up-the-failed-screen-with-kernel-panic" class="bare">https://superuser.com/questions/848412/scrolling-up-the-failed-screen-with-kernel-panic</a></p>
</li>
<li>
<p><a href="https://superuser.com/questions/269228/write-qemu-booting-virtual-machine-output-to-a-file" class="bare">https://superuser.com/questions/269228/write-qemu-booting-virtual-machine-output-to-a-file</a></p>
</li>
<li>
<p><a href="http://www.reactos.org/wiki/QEMU#Redirect_to_a_file" class="bare">http://www.reactos.org/wiki/QEMU#Redirect_to_a_file</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="kernel-panic"><a class="anchor" href="#kernel-panic"></a><a class="link" href="#kernel-panic">15.7.1. Kernel panic</a></h4>
<div class="paragraph">
<p>On panic, the kernel dies, and so does our terminal.</p>
</div>
<div class="paragraph">
<p>The panic trace looks like:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>panic: loading out-of-tree module taints kernel.
panic myinit
Kernel panic - not syncing: hello panic
CPU: 0 PID: 53 Comm: insmod Tainted: G O 4.16.0 #6
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.11.0-0-g63451fca13-prebuilt.qemu-project.org 04/01/2014
Call Trace:
dump_stack+0x7d/0xba
? 0xffffffffc0000000
panic+0xda/0x213
? printk+0x43/0x4b
? 0xffffffffc0000000
myinit+0x1d/0x20 [panic]
do_one_initcall+0x3e/0x170
do_init_module+0x5b/0x210
load_module+0x2035/0x29d0
? kernel_read_file+0x7d/0x140
? SyS_finit_module+0xa8/0xb0
SyS_finit_module+0xa8/0xb0
do_syscall_64+0x6f/0x310
? trace_hardirqs_off_thunk+0x1a/0x32
entry_SYSCALL_64_after_hwframe+0x42/0xb7
RIP: 0033:0x7ffff7b36206
RSP: 002b:00007fffffffeb78 EFLAGS: 00000206 ORIG_RAX: 0000000000000139
RAX: ffffffffffffffda RBX: 000000000000005c RCX: 00007ffff7b36206
RDX: 0000000000000000 RSI: 000000000069e010 RDI: 0000000000000003
RBP: 000000000069e010 R08: 00007ffff7ddd320 R09: 0000000000000000
R10: 00007ffff7ddd320 R11: 0000000000000206 R12: 0000000000000003
R13: 00007fffffffef4a R14: 0000000000000000 R15: 0000000000000000
Kernel Offset: disabled
---[ end Kernel panic - not syncing: hello panic</pre>
</div>
</div>
<div class="paragraph">
<p>Notice how our panic message <code>hello panic</code> is visible at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Kernel panic - not syncing: hello panic</pre>
</div>
</div>
<div class="sect4">
<h5 id="kernel-module-stack-trace-to-source-line"><a class="anchor" href="#kernel-module-stack-trace-to-source-line"></a><a class="link" href="#kernel-module-stack-trace-to-source-line">15.7.1.1. Kernel module stack trace to source line</a></h5>
<div class="paragraph">
<p>The log shows which module each symbol belongs to if any, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>myinit+0x1d/0x20 [panic]</pre>
</div>
</div>
<div class="paragraph">
<p>says that the function <code>myinit</code> is in the module <code>panic</code>.</p>
</div>
<div class="paragraph">
<p>To find the line that panicked, do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb</pre>
</div>
</div>
<div class="paragraph">
<p>and then:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>info line *(myinit+0x1d)</pre>
</div>
</div>
<div class="paragraph">
<p>which gives us the correct line:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Line 7 of "/root/linux-kernel-module-cheat/out/kernel_modules/x86_64/kernel_modules/panic.c" starts at address 0xbf00001c &lt;myinit+28&gt; and ends at 0xbf00002c &lt;myexit&gt;.</pre>
</div>
</div>
<div class="paragraph">
<p>as explained at: <a href="https://stackoverflow.com/questions/8545931/using-gdb-to-convert-addresses-to-lines/27576029#27576029" class="bare">https://stackoverflow.com/questions/8545931/using-gdb-to-convert-addresses-to-lines/27576029#27576029</a></p>
</div>
<div class="paragraph">
<p>The exact same thing can be done post mortem with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain gdb -- \
-batch \
-ex 'info line *(myinit+0x1d)' \
"$(./getvar kernel_modules_build_subdir)/panic.ko" \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Related:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/6151538/addr2line-on-kernel-module" class="bare">https://stackoverflow.com/questions/6151538/addr2line-on-kernel-module</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/13468286/how-to-read-understand-analyze-and-debug-a-linux-kernel-panic" class="bare">https://stackoverflow.com/questions/13468286/how-to-read-understand-analyze-and-debug-a-linux-kernel-panic</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="bug_on"><a class="anchor" href="#bug_on"></a><a class="link" href="#bug_on">15.7.1.2. BUG_ON</a></h5>
<div class="paragraph">
<p>Basically just calls <code>panic("BUG!")</code> for most archs.</p>
</div>
</div>
<div class="sect4">
<h5 id="exit-emulator-on-panic"><a class="anchor" href="#exit-emulator-on-panic"></a><a class="link" href="#exit-emulator-on-panic">15.7.1.3. Exit emulator on panic</a></h5>
<div class="paragraph">
<p>For testing purposes, it is very useful to quit the emulator automatically with exit status non zero in case of kernel panic, instead of just hanging forever.</p>
</div>
<div class="sect5">
<h6 id="exit-qemu-on-panic"><a class="anchor" href="#exit-qemu-on-panic"></a><a class="link" href="#exit-qemu-on-panic">15.7.1.3.1. Exit QEMU on panic</a></h6>
<div class="paragraph">
<p>Enabled by default with:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>panic=-1</code> command line option which reboots the kernel immediately on panic, see: <a href="#reboot-on-panic">Section 15.7.1.4, &#8220;Reboot on panic&#8221;</a></p>
</li>
<li>
<p>QEMU <code>-no-reboot</code>, which makes QEMU exit when the guest tries to reboot</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Also asked at <a href="https://unix.stackexchange.com/questions/443017/can-i-make-qemu-exit-with-failure-on-kernel-panic" class="bare">https://unix.stackexchange.com/questions/443017/can-i-make-qemu-exit-with-failure-on-kernel-panic</a> which also mentions the x86_64 <code>-device pvpanic</code>, but I don&#8217;t see much advantage to it.</p>
</div>
<div class="paragraph">
<p>TODO neither method exits with exit status different from 0, so for now we are just grepping the logs for panic messages, which sucks.</p>
</div>
<div class="paragraph">
<p>One possibility that gets close would be to use <a href="#gdb">GDB step debug</a> to break at the <code>panic</code> function, and then send a <a href="#qemu-monitor-from-gdb">QEMU monitor from GDB</a> <code>quit</code> command if that happens, but I don&#8217;t see a way to exit with non-zero status to indicate error.</p>
</div>
</div>
<div class="sect5">
<h6 id="exit-gem5-on-panic"><a class="anchor" href="#exit-gem5-on-panic"></a><a class="link" href="#exit-gem5-on-panic">15.7.1.3.2. Exit gem5 on panic</a></h6>
<div class="paragraph">
<p>gem5 9048ef0ffbf21bedb803b785fb68f83e95c04db8 (January 2019) can detect panics automatically if the option <code>system.panic_on_panic</code> is on.</p>
</div>
<div class="paragraph">
<p>It parses kernel symbols and detecting when the PC reaches the address of the <code>panic</code> function. gem5 then prints to stdout:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Kernel panic in simulated kernel</pre>
</div>
</div>
<div class="paragraph">
<p>and exits with status -6.</p>
</div>
<div class="paragraph">
<p>At gem5 ff52563a214c71fcd1e21e9f00ad839612032e3b (July 2018) behaviour was different, and just exited 0: <a href="https://www.mail-archive.com/gem5-users@gem5.org/msg15870.html" class="bare">https://www.mail-archive.com/gem5-users@gem5.org/msg15870.html</a> TODO find fixing commit.</p>
</div>
<div class="paragraph">
<p>We enable the <code>system.panic_on_panic</code> option by default on <code>arm</code> and <code>aarch64</code>, which makes gem5 exit immediately in case of panic, which is awesome!</p>
</div>
<div class="paragraph">
<p>If we don&#8217;t set <code>system.panic_on_panic</code>, then gem5 just hangs on an infinite guest loop.</p>
</div>
<div class="paragraph">
<p>TODO: why doesn&#8217;t gem5 x86 ff52563a214c71fcd1e21e9f00ad839612032e3b support <code>system.panic_on_panic</code> as well? Trying to set <code>system.panic_on_panic</code> there fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>tried to set or access non-existentobject parameter: panic_on_panic</pre>
</div>
</div>
<div class="paragraph">
<p>However, at that commit panic on x86 makes gem5 crash with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>panic: i8042 "System reset" command not implemented.</pre>
</div>
</div>
<div class="paragraph">
<p>which is a good side effect of an unimplemented hardware feature, since the simulation actually stops.</p>
</div>
<div class="paragraph">
<p>The implementation of panic detection happens at: <a href="https://github.com/gem5/gem5/blob/1da285dfcc31b904afc27e440544d006aae25b38/src/arch/arm/linux/system.cc#L73" class="bare">https://github.com/gem5/gem5/blob/1da285dfcc31b904afc27e440544d006aae25b38/src/arch/arm/linux/system.cc#L73</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre> kernelPanicEvent = addKernelFuncEventOrPanic&lt;Linux::KernelPanicEvent&gt;(
"panic", "Kernel panic in simulated kernel", dmesg_output);</pre>
</div>
</div>
<div class="paragraph">
<p>Here we see that the symbol <code>"panic"</code> for the <code>panic()</code> function is the one being tracked.</p>
</div>
<div class="paragraph">
<p>Related thread: <a href="https://stackoverflow.com/questions/56032347/is-there-a-way-to-identify-if-gem5-run-got-over-successfully" class="bare">https://stackoverflow.com/questions/56032347/is-there-a-way-to-identify-if-gem5-run-got-over-successfully</a></p>
</div>
</div>
</div>
<div class="sect4">
<h5 id="reboot-on-panic"><a class="anchor" href="#reboot-on-panic"></a><a class="link" href="#reboot-on-panic">15.7.1.4. Reboot on panic</a></h5>
<div class="paragraph">
<p>Make the kernel reboot after n seconds after panic:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 1 &gt; /proc/sys/kernel/panic</pre>
</div>
</div>
<div class="paragraph">
<p>Can also be controlled with the <code>panic=</code> kernel boot parameter.</p>
</div>
<div class="paragraph">
<p><code>0</code> to disable, <code>-1</code> to reboot immediately.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/torvalds/linux/blob/v4.17/Documentation/admin-guide/kernel-parameters.txt#L2931" class="bare">https://github.com/torvalds/linux/blob/v4.17/Documentation/admin-guide/kernel-parameters.txt#L2931</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/29567/how-to-configure-the-linux-kernel-to-reboot-on-panic/29569#29569" class="bare">https://unix.stackexchange.com/questions/29567/how-to-configure-the-linux-kernel-to-reboot-on-panic/29569#29569</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="panic-trace-show-addresses-instead-of-symbols"><a class="anchor" href="#panic-trace-show-addresses-instead-of-symbols"></a><a class="link" href="#panic-trace-show-addresses-instead-of-symbols">15.7.1.5. Panic trace show addresses instead of symbols</a></h5>
<div class="paragraph">
<p>If <code>CONFIG_KALLSYMS=n</code>, then addresses are shown on traces instead of symbol plus offset.</p>
</div>
<div class="paragraph">
<p>In v4.16 it does not seem possible to configure that at runtime. GDB step debugging with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after 'insmod dump_stack.ko' --gdb-wait --tmux-args dump_stack</pre>
</div>
</div>
<div class="paragraph">
<p>shows that traces are printed at <code>arch/x86/kernel/dumpstack.c</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>static void printk_stack_address(unsigned long address, int reliable,
char *log_lvl)
{
touch_nmi_watchdog();
printk("%s %s%pB\n", log_lvl, reliable ? "" : "? ", (void *)address);
}</pre>
</div>
</div>
<div class="paragraph">
<p>and <code>%pB</code> is documented at <code>Documentation/core-api/printk-formats.rst</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>If KALLSYMS are disabled then the symbol address is printed instead.</pre>
</div>
</div>
<div class="paragraph">
<p>I wasn&#8217;t able do disable <code>CONFIG_KALLSYMS</code> to test this this out however, it is being selected by some other option? But I then used <code>make menuconfig</code> to see which options select it, and they were all off&#8230;&#8203;</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="oops"><a class="anchor" href="#oops"></a><a class="link" href="#oops">15.7.2. Kernel oops</a></h4>
<div class="paragraph">
<p>On oops, the shell still lives after.</p>
</div>
<div class="paragraph">
<p>However we:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>leave the normal control flow, and <code>oops after</code> never gets printed: an interrupt is serviced</p>
</li>
<li>
<p>cannot <code>rmmod oops</code> afterwards</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>It is possible to make <code>oops</code> lead to panics always with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 1 &gt; /proc/sys/kernel/panic_on_oops
insmod oops.ko</pre>
</div>
</div>
<div class="paragraph">
<p>An oops stack trace looks like:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>BUG: unable to handle kernel NULL pointer dereference at 0000000000000000
IP: myinit+0x18/0x30 [oops]
PGD dccf067 P4D dccf067 PUD dcc1067 PMD 0
Oops: 0002 [#1] SMP NOPTI
Modules linked in: oops(O+)
CPU: 0 PID: 53 Comm: insmod Tainted: G O 4.16.0 #6
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.11.0-0-g63451fca13-prebuilt.qemu-project.org 04/01/2014
RIP: 0010:myinit+0x18/0x30 [oops]
RSP: 0018:ffffc900000d3cb0 EFLAGS: 00000282
RAX: 000000000000000b RBX: ffffffffc0000000 RCX: ffffffff81e3e3a8
RDX: 0000000000000001 RSI: 0000000000000086 RDI: ffffffffc0001033
RBP: ffffc900000d3e30 R08: 69796d2073706f6f R09: 000000000000013b
R10: ffffea0000373280 R11: ffffffff822d8b2d R12: 0000000000000000
R13: ffffffffc0002050 R14: ffffffffc0002000 R15: ffff88000dc934c8
FS: 00007ffff7ff66a0(0000) GS:ffff88000fc00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000000000000 CR3: 000000000dcd2000 CR4: 00000000000006f0
Call Trace:
do_one_initcall+0x3e/0x170
do_init_module+0x5b/0x210
load_module+0x2035/0x29d0
? SyS_finit_module+0xa8/0xb0
SyS_finit_module+0xa8/0xb0
do_syscall_64+0x6f/0x310
? trace_hardirqs_off_thunk+0x1a/0x32
entry_SYSCALL_64_after_hwframe+0x42/0xb7
RIP: 0033:0x7ffff7b36206
RSP: 002b:00007fffffffeb78 EFLAGS: 00000206 ORIG_RAX: 0000000000000139
RAX: ffffffffffffffda RBX: 000000000000005c RCX: 00007ffff7b36206
RDX: 0000000000000000 RSI: 000000000069e010 RDI: 0000000000000003
RBP: 000000000069e010 R08: 00007ffff7ddd320 R09: 0000000000000000
R10: 00007ffff7ddd320 R11: 0000000000000206 R12: 0000000000000003
R13: 00007fffffffef4b R14: 0000000000000000 R15: 0000000000000000
Code: &lt;c7&gt; 04 25 00 00 00 00 00 00 00 00 e8 b2 33 09 c1 31 c0 c3 0f 1f 44
RIP: myinit+0x18/0x30 [oops] RSP: ffffc900000d3cb0
CR2: 0000000000000000
---[ end trace 3cdb4e9d9842b503 ]---</pre>
</div>
</div>
<div class="paragraph">
<p>To find the line that oopsed, look at the <code>RIP</code> register:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>RIP: 0010:myinit+0x18/0x30 [oops]</pre>
</div>
</div>
<div class="paragraph">
<p>and then on GDB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb</pre>
</div>
</div>
<div class="paragraph">
<p>run</p>
</div>
<div class="literalblock">
<div class="content">
<pre>info line *(myinit+0x18)</pre>
</div>
</div>
<div class="paragraph">
<p>which gives us the correct line:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Line 7 of "/root/linux-kernel-module-cheat/out/kernel_modules/x86_64/kernel_modules/panic.c" starts at address 0xbf00001c &lt;myinit+28&gt; and ends at 0xbf00002c &lt;myexit&gt;.</pre>
</div>
</div>
<div class="paragraph">
<p>This-did not work on <code>arm</code> due to <a href="#gdb-step-debug-kernel-module-arm">GDB step debug kernel module insmodded by init on ARM</a> so we need to either:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#gdb-module_init">GDB module_init</a></p>
</li>
<li>
<p><a href="#kernel-module-stack-trace-to-source-line">Kernel module stack trace to source line</a> post-mortem method</p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="dump_stack"><a class="anchor" href="#dump_stack"></a><a class="link" href="#dump_stack">15.7.3. dump_stack</a></h4>
<div class="paragraph">
<p>The <code>dump_stack</code> function produces a stack trace much like panic and oops, but causes no problems and we return to the normal control flow, and can cleanly remove the module afterwards:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod dump_stack.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/dump_stack.c">kernel_modules/dump_stack.c</a></p>
</div>
</div>
<div class="sect3">
<h4 id="warn_on"><a class="anchor" href="#warn_on"></a><a class="link" href="#warn_on">15.7.4. WARN_ON</a></h4>
<div class="paragraph">
<p>The <code>WARN_ON</code> macro basically just calls <a href="#dump_stack">dump_stack</a>.</p>
</div>
<div class="paragraph">
<p>One extra side effect is that we can make it also panic with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 1 &gt; /proc/sys/kernel/panic_on_warn
insmod warn_on.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/warn_on.c">kernel_modules/warn_on.c</a></p>
</div>
<div class="paragraph">
<p>Can also be activated with the <code>panic_on_warn</code> boot parameter.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="pseudo-filesystems"><a class="anchor" href="#pseudo-filesystems"></a><a class="link" href="#pseudo-filesystems">15.8. Pseudo filesystems</a></h3>
<div class="paragraph">
<p>Pseudo filesystems are filesystems that don&#8217;t represent actual files in a hard disk, but rather allow us to do special operations on filesystem-related system calls.</p>
</div>
<div class="paragraph">
<p>What each pseudo-file does for each related system call does is defined by its <a href="#file-operations">File operations</a>.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://superuser.com/questions/1198292/what-is-a-pseudo-file-system-in-linux" class="bare">https://superuser.com/questions/1198292/what-is-a-pseudo-file-system-in-linux</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/Synthetic_file_system" class="bare">https://en.wikipedia.org/wiki/Synthetic_file_system</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="debugfs"><a class="anchor" href="#debugfs"></a><a class="link" href="#debugfs">15.8.1. debugfs</a></h4>
<div class="paragraph">
<p>Debugfs is the simplest pseudo filesystem to play around with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./debugfs.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/debugfs.c">kernel_modules/debugfs.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/debugfs.sh">rootfs_overlay/lkmc/debugfs.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Debugfs is made specifically to help test kernel stuff. Just mount, set <a href="#file-operations">File operations</a>, and we are done.</p>
</div>
<div class="paragraph">
<p>For this reason, it is the filesystem that we use whenever possible in our tests.</p>
</div>
<div class="paragraph">
<p><code>debugfs.sh</code> explicitly mounts a debugfs at a custom location, but the most common mount point is <code>/sys/kernel/debug</code>.</p>
</div>
<div class="paragraph">
<p>This mount not done automatically by the kernel however: we, like most distros, do it from userland with our <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/etc/fstab">fstab</a>.</p>
</div>
<div class="paragraph">
<p>Debugfs support requires the kernel to be compiled with <code>CONFIG_DEBUG_FS=y</code>.</p>
</div>
<div class="paragraph">
<p>Only the more basic file operations can be implemented in debugfs, e.g. <code>mmap</code> never gets called:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://patchwork.kernel.org/patch/9252557/" class="bare">https://patchwork.kernel.org/patch/9252557/</a></p>
</li>
<li>
<p><a href="https://github.com/torvalds/linux/blob/v4.9/fs/debugfs/file.c#L212" class="bare">https://github.com/torvalds/linux/blob/v4.9/fs/debugfs/file.c#L212</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://github.com/chadversary/debugfs-tutorial" class="bare">https://github.com/chadversary/debugfs-tutorial</a></p>
</div>
</div>
<div class="sect3">
<h4 id="procfs"><a class="anchor" href="#procfs"></a><a class="link" href="#procfs">15.8.2. procfs</a></h4>
<div class="paragraph">
<p>Procfs is just another fops entry point:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./procfs.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Procfs is a little less convenient than <a href="#debugfs">debugfs</a>, but is more used in serious applications.</p>
</div>
<div class="paragraph">
<p>Procfs can run all system calls, including ones that debugfs can&#8217;t, e.g. <a href="#mmap">mmap</a>.</p>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/procfs.c">kernel_modules/procfs.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/procfs.sh">rootfs_overlay/lkmc/procfs.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://superuser.com/questions/619955/how-does-proc-work/1442571#1442571" class="bare">https://superuser.com/questions/619955/how-does-proc-work/1442571#1442571</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/8516021/proc-create-example-for-kernel-module/18924359#18924359" class="bare">https://stackoverflow.com/questions/8516021/proc-create-example-for-kernel-module/18924359#18924359</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="proc-version"><a class="anchor" href="#proc-version"></a><a class="link" href="#proc-version">15.8.2.1. /proc/version</a></h5>
<div class="paragraph">
<p>Its data is shared with <code>uname()</code>, which is a <a href="#posix">POSIX C</a> function and has a Linux syscall to back it up.</p>
</div>
<div class="paragraph">
<p>Where the data comes from and how to modify it:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://unix.stackexchange.com/questions/136959/where-does-uname-get-its-information-from/485962#485962" class="bare">https://unix.stackexchange.com/questions/136959/where-does-uname-get-its-information-from/485962#485962</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/23424174/how-to-customize-or-remove-extra-linux-kernel-version-details-shown-at-boot" class="bare">https://stackoverflow.com/questions/23424174/how-to-customize-or-remove-extra-linux-kernel-version-details-shown-at-boot</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In this repo, leaking host information, and to make builds more reproducible, we are setting:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>user and date to dummy values with <code>KBUILD_BUILD_USER</code> and <code>KBUILD_BUILD_TIMESTAMP</code></p>
</li>
<li>
<p>hostname to the kernel git commit with <code>KBUILD_BUILD_HOST</code> and <code>KBUILD_BUILD_VERSION</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>A sample result is:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Linux version 4.19.0-dirty (lkmc@84df9525b0c27f3ebc2ebb1864fa62a97fdedb7d) (gcc version 6.4.0 (Buildroot 2018.05-00002-gbc60382b8f)) #1 SMP Thu Jan 1 00:00:00 UTC 1970</pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="sysfs"><a class="anchor" href="#sysfs"></a><a class="link" href="#sysfs">15.8.3. sysfs</a></h4>
<div class="paragraph">
<p>Sysfs is more restricted than <a href="#procfs">procfs</a>, as it does not take an arbitrary <code>file_operations</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./sysfs.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/sysfs.c">kernel_modules/sysfs.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/sysfs.sh">rootfs_overlay/lkmc/sysfs.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Vs procfs:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://unix.stackexchange.com/questions/4884/what-is-the-difference-between-procfs-and-sysfs/382315#382315" class="bare">https://unix.stackexchange.com/questions/4884/what-is-the-difference-between-procfs-and-sysfs/382315#382315</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/37237835/how-to-attach-file-operations-to-sysfs-attribute-in-platform-driver" class="bare">https://stackoverflow.com/questions/37237835/how-to-attach-file-operations-to-sysfs-attribute-in-platform-driver</a></p>
</li>
<li>
<p><a href="https://serverfault.com/questions/65261/linux-proc-sys-kernel-vs-sys-kernel" class="bare">https://serverfault.com/questions/65261/linux-proc-sys-kernel-vs-sys-kernel</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>You basically can only do <code>open</code>, <code>close</code>, <code>read</code>, <code>write</code>, and <code>lseek</code> on sysfs files.</p>
</div>
<div class="paragraph">
<p>It is similar to a <a href="#seq_file">seq_file</a> file operation, except that write is also implemented.</p>
</div>
<div class="paragraph">
<p>TODO: what are those <code>kobject</code> structs? Make a more complex example that shows what they can do.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/t3rm1n4l/kern-dev-tutorial/blob/1f036ef40fc4378f5c8d2842e55bcea7c6f8894a/05-sysfs/sysfs.c" class="bare">https://github.com/t3rm1n4l/kern-dev-tutorial/blob/1f036ef40fc4378f5c8d2842e55bcea7c6f8894a/05-sysfs/sysfs.c</a></p>
</li>
<li>
<p><a href="https://www.kernel.org/doc/Documentation/kobject.txt" class="bare">https://www.kernel.org/doc/Documentation/kobject.txt</a></p>
</li>
<li>
<p><a href="https://www.quora.com/What-are-kernel-objects-Kobj" class="bare">https://www.quora.com/What-are-kernel-objects-Kobj</a></p>
</li>
<li>
<p><a href="http://www.makelinux.net/ldd3/chp-14-sect-1" class="bare">http://www.makelinux.net/ldd3/chp-14-sect-1</a></p>
</li>
<li>
<p><a href="https://www.win.tue.nl/~aeb/linux/lk/lk-13.html" class="bare">https://www.win.tue.nl/~aeb/linux/lk/lk-13.html</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="character-devices"><a class="anchor" href="#character-devices"></a><a class="link" href="#character-devices">15.8.4. Character devices</a></h4>
<div class="paragraph">
<p>Character devices can have arbitrary <a href="#file-operations">File operations</a> associated to them:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./character_device.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/character_device.sh">rootfs_overlay/lkmc/character_device.sh</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/mknoddev.sh">rootfs_overlay/lkmc/mknoddev.sh</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/character_device.c">kernel_modules/character_device.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Unlike <a href="#procfs">procfs</a> entires, character device files are created with userland <code>mknod</code> or <code>mknodat</code> syscalls:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mknod &lt;/dev/path_to_dev&gt; c &lt;major&gt; &lt;minor&gt;</pre>
</div>
</div>
<div class="paragraph">
<p>Intuitively, for physical devices like keyboards, the major number maps to which driver, and the minor number maps to which device it is.</p>
</div>
<div class="paragraph">
<p>A single driver can drive multiple compatible devices.</p>
</div>
<div class="paragraph">
<p>The major and minor numbers can be observed with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ls -l /dev/urandom</pre>
</div>
</div>
<div class="paragraph">
<p>Output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>crw-rw-rw- 1 root root 1, 9 Jun 29 05:45 /dev/urandom</pre>
</div>
</div>
<div class="paragraph">
<p>which means:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>c</code> (first letter): this is a character device. Would be <code>b</code> for a block device.</p>
</li>
<li>
<p><code>1, 9</code>: the major number is <code>1</code>, and the minor <code>9</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>To avoid device number conflicts when registering the driver we:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>ask the kernel to allocate a free major number for us with: <code>register_chrdev(0</code></p>
</li>
<li>
<p>find ouf which number was assigned by grepping <code>/proc/devices</code> for the kernel module name</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://unix.stackexchange.com/questions/37829/understanding-character-device-or-character-special-files/371758#371758" class="bare">https://unix.stackexchange.com/questions/37829/understanding-character-device-or-character-special-files/371758#371758</a></p>
</div>
<div class="sect4">
<h5 id="automatically-create-character-device-file-on-insmod"><a class="anchor" href="#automatically-create-character-device-file-on-insmod"></a><a class="link" href="#automatically-create-character-device-file-on-insmod">15.8.4.1. Automatically create character device file on insmod</a></h5>
<div class="paragraph">
<p>And also destroy it on <code>rmmod</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./character_device_create.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/character_device_create.c">kernel_modules/character_device_create.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/character_device_create.sh">rootfs_overlay/lkmc/character_device_create.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867" class="bare">https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867</a></p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="pseudo-files"><a class="anchor" href="#pseudo-files"></a><a class="link" href="#pseudo-files">15.9. Pseudo files</a></h3>
<div class="sect3">
<h4 id="file-operations"><a class="anchor" href="#file-operations"></a><a class="link" href="#file-operations">15.9.1. File operations</a></h4>
<div class="paragraph">
<p>File operations are the main method of userland driver communication. <code>struct file_operations</code> determines what the kernel will do on filesystem system calls of <a href="#pseudo-filesystems">Pseudo filesystems</a>.</p>
</div>
<div class="paragraph">
<p>This example illustrates the most basic system calls: <code>open</code>, <code>read</code>, <code>write</code>, <code>close</code> and <code>lseek</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./fops.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/fops.c">kernel_modules/fops.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/fops.sh">rootfs_overlay/lkmc/fops.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Then give this a try:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sh -x ./fops.sh</pre>
</div>
</div>
<div class="paragraph">
<p>We have put printks on each fop, so this allows you to see which system calls are being made for each command.</p>
</div>
<div class="paragraph">
<p>No, there no official documentation: <a href="https://stackoverflow.com/questions/15213932/what-are-the-struct-file-operations-arguments" class="bare">https://stackoverflow.com/questions/15213932/what-are-the-struct-file-operations-arguments</a></p>
</div>
</div>
<div class="sect3">
<h4 id="seq_file"><a class="anchor" href="#seq_file"></a><a class="link" href="#seq_file">15.9.2. seq_file</a></h4>
<div class="paragraph">
<p>Writing trivial read <a href="#file-operations">File operations</a> is repetitive and error prone. The <code>seq_file</code> API makes the process much easier for those trivial cases:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./seq_file.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/seq_file.c">kernel_modules/seq_file.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/seq_file.sh">rootfs_overlay/lkmc/seq_file.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In this example we create a debugfs file that behaves just like a file that contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0
1
2</pre>
</div>
</div>
<div class="paragraph">
<p>However, we only store a single integer in memory and calculate the file on the fly in an iterator fashion.</p>
</div>
<div class="paragraph">
<p><code>seq_file</code> does not provide <code>write</code>: <a href="https://stackoverflow.com/questions/30710517/how-to-implement-a-writable-proc-file-by-using-seq-file-in-a-driver-module" class="bare">https://stackoverflow.com/questions/30710517/how-to-implement-a-writable-proc-file-by-using-seq-file-in-a-driver-module</a></p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/torvalds/linux/blob/v4.17/Documentation/filesystems/seq_file.txt">Documentation/filesystems/seq_file.txt</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/25399112/how-to-use-a-seq-file-in-linux-modules" class="bare">https://stackoverflow.com/questions/25399112/how-to-use-a-seq-file-in-linux-modules</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="seq_file-single_open"><a class="anchor" href="#seq_file-single_open"></a><a class="link" href="#seq_file-single_open">15.9.2.1. seq_file single_open</a></h5>
<div class="paragraph">
<p>If you have the entire read output upfront, <code>single_open</code> is an even more convenient version of <a href="#seq_file">seq_file</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./seq_file.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/seq_file_single_open.c">kernel_modules/seq_file_single_open.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/seq_file_single_open.sh">rootfs_overlay/lkmc/seq_file_single_open.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This example produces a debugfs file that behaves like a file that contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ab
cd</pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="poll"><a class="anchor" href="#poll"></a><a class="link" href="#poll">15.9.3. poll</a></h4>
<div class="paragraph">
<p>The poll system call allows an user process to do a non-busy wait on a kernel event:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./poll.sh</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: <code>jiffies</code> gets printed to stdout every second from userland.</p>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/poll.c">kernel_modules/poll.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/poll.sh">rootfs_overlay/lkmc/poll.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Typically, we are waiting for some hardware to make some piece of data available available to the kernel.</p>
</div>
<div class="paragraph">
<p>The hardware notifies the kernel that the data is ready with an interrupt.</p>
</div>
<div class="paragraph">
<p>To simplify this example, we just fake the hardware interrupts with a <a href="#kthread">kthread</a> that sleeps for a second in an infinite loop.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/30035776/how-to-add-poll-function-to-the-kernel-module-code/44645336#44645336" class="bare">https://stackoverflow.com/questions/30035776/how-to-add-poll-function-to-the-kernel-module-code/44645336#44645336</a></p>
</div>
</div>
<div class="sect3">
<h4 id="ioctl"><a class="anchor" href="#ioctl"></a><a class="link" href="#ioctl">15.9.4. ioctl</a></h4>
<div class="paragraph">
<p>The <code>ioctl</code> system call is the best way to pass an arbitrary number of parameters to the kernel in a single go:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./ioctl.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/ioctl.c">kernel_modules/ioctl.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/ioctl.h">lkmc/ioctl.h</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/kernel_modules/ioctl.c">userland/kernel_modules/ioctl.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/ioctl.sh">rootfs_overlay/lkmc/ioctl.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><code>ioctl</code> is one of the most important methods of communication with real device drivers, which often take several fields as input.</p>
</div>
<div class="paragraph">
<p><code>ioctl</code> takes as input:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>an integer <code>request</code> : it usually identifies what type of operation we want to do on this call</p>
</li>
<li>
<p>an untyped pointer to memory: can be anything, but is typically a pointer to a <code>struct</code></p>
<div class="paragraph">
<p>The type of the <code>struct</code> often depends on the <code>request</code> input</p>
</div>
<div class="paragraph">
<p>This <code>struct</code> is defined on a uapi-style C header that is used both to compile the kernel module and the userland executable.</p>
</div>
<div class="paragraph">
<p>The fields of this <code>struct</code> can be thought of as arbitrary input parameters.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>And the output is:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>an integer return value. <code>man ioctl</code> documents:</p>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>Usually, on success zero is returned. A few <code>ioctl()</code> requests use the return value as an output parameter and return a nonnegative value on success. On error, -1 is returned, and errno is set appropriately.</p>
</div>
</blockquote>
</div>
</li>
<li>
<p>the input pointer data may be overwritten to contain arbitrary output</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/2264384/how-do-i-use-ioctl-to-manipulate-my-kernel-module/44613896#44613896" class="bare">https://stackoverflow.com/questions/2264384/how-do-i-use-ioctl-to-manipulate-my-kernel-module/44613896#44613896</a></p>
</li>
<li>
<p><a href="https://askubuntu.com/questions/54239/problem-with-ioctl-in-a-simple-kernel-module/926675#926675" class="bare">https://askubuntu.com/questions/54239/problem-with-ioctl-in-a-simple-kernel-module/926675#926675</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="mmap"><a class="anchor" href="#mmap"></a><a class="link" href="#mmap">15.9.5. mmap</a></h4>
<div class="paragraph">
<p>The <code>mmap</code> system call allows us to share memory between user and kernel space without copying:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./mmap.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/mmap.c">kernel_modules/mmap.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/kernel_modules/mmap.c">userland/kernel_modules/mmap.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/mmap.sh">rootfs_overlay/lkmc/mmap.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In this example, we make a tiny 4 byte kernel buffer available to user-space, and we then modify it on userspace, and check that the kernel can see the modification.</p>
</div>
<div class="paragraph">
<p><code>mmap</code>, like most more complex <a href="#file-operations">File operations</a>, does not work with <a href="#debugfs">debugfs</a> as of 4.9, so we use a <a href="#procfs">procfs</a> file for it.</p>
</div>
<div class="paragraph">
<p>Example adapted from: <a href="https://coherentmusings.wordpress.com/2014/06/10/implementing-mmap-for-transferring-data-from-user-space-to-kernel-space/" class="bare">https://coherentmusings.wordpress.com/2014/06/10/implementing-mmap-for-transferring-data-from-user-space-to-kernel-space/</a></p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/10760479/mmap-kernel-buffer-to-user-space/10770582#10770582" class="bare">https://stackoverflow.com/questions/10760479/mmap-kernel-buffer-to-user-space/10770582#10770582</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/1623008/allocating-memory-for-user-space-from-kernel-thread" class="bare">https://stackoverflow.com/questions/1623008/allocating-memory-for-user-space-from-kernel-thread</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/6967933/mmap-mapping-in-user-space-a-kernel-buffer-allocated-with-kmalloc" class="bare">https://stackoverflow.com/questions/6967933/mmap-mapping-in-user-space-a-kernel-buffer-allocated-with-kmalloc</a></p>
</li>
<li>
<p><a href="https://github.com/jeremytrimble/ezdma" class="bare">https://github.com/jeremytrimble/ezdma</a></p>
</li>
<li>
<p><a href="https://github.com/simonjhall/dma" class="bare">https://github.com/simonjhall/dma</a></p>
</li>
<li>
<p><a href="https://github.com/ikwzm/udmabuf" class="bare">https://github.com/ikwzm/udmabuf</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="anonymous-inode"><a class="anchor" href="#anonymous-inode"></a><a class="link" href="#anonymous-inode">15.9.6. Anonymous inode</a></h4>
<div class="paragraph">
<p>Anonymous inodes allow getting multiple file descriptors from a single filesystem entry, which reduces namespace pollution compared to creating multiple device files:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./anonymous_inode.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/anonymous_inode.c">kernel_modules/anonymous_inode.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/anonymous_inode.h">lkmc/anonymous_inode.h</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/kernel_modules/anonymous_inode.c">userland/kernel_modules/anonymous_inode.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/anonymous_inode.sh">rootfs_overlay/lkmc/anonymous_inode.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This example gets an anonymous inode via <a href="#ioctl">ioctl</a> from a debugfs entry by using <code>anon_inode_getfd</code>.</p>
</div>
<div class="paragraph">
<p>Reads to that inode return the sequence: <code>1</code>, <code>10</code>, <code>100</code>, &#8230;&#8203; <code>10000000</code>, <code>1</code>, <code>100</code>, &#8230;&#8203;</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/4508998/what-is-an-anonymous-inode-in-linux/44388030#44388030" class="bare">https://stackoverflow.com/questions/4508998/what-is-an-anonymous-inode-in-linux/44388030#44388030</a></p>
</div>
</div>
<div class="sect3">
<h4 id="netlink-sockets"><a class="anchor" href="#netlink-sockets"></a><a class="link" href="#netlink-sockets">15.9.7. netlink sockets</a></h4>
<div class="paragraph">
<p>Netlink sockets offer a socket API for kernel / userland communication:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./netlink.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/netlink.c">kernel_modules/netlink.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/netlink.h">lkmc/netlink.h</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/kernel_modules/netlink.c">userland/kernel_modules/netlink.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/netlink.sh">rootfs_overlay/lkmc/netlink.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Launch multiple user requests in parallel to stress our socket:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod netlink.ko sleep=1
for i in `seq 16`; do ./netlink.out &amp; done</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: what is the advantage over <code>read</code>, <code>write</code> and <code>poll</code>? <a href="https://stackoverflow.com/questions/16727212/how-netlink-socket-in-linux-kernel-is-different-from-normal-polling-done-by-appl" class="bare">https://stackoverflow.com/questions/16727212/how-netlink-socket-in-linux-kernel-is-different-from-normal-polling-done-by-appl</a></p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module" class="bare">https://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/Netlink" class="bare">https://en.wikipedia.org/wiki/Netlink</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="kthread"><a class="anchor" href="#kthread"></a><a class="link" href="#kthread">15.10. kthread</a></h3>
<div class="paragraph">
<p>Kernel threads are managed exactly like userland threads; they also have a backing <code>task_struct</code>, and are scheduled with the same mechanism:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod kthread.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/kthread.c">kernel_modules/kthread.c</a></p>
</div>
<div class="paragraph">
<p>Outcome: dmesg counts from <code>0</code> to <code>9</code> once every second infinitely many times:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0
1
2
...
8
9
0
1
2
...</pre>
</div>
</div>
<div class="paragraph">
<p>The count stops when we <code>rmmod</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rmmod kthread</pre>
</div>
</div>
<div class="paragraph">
<p>The sleep is done with <code>usleep_range</code>, see: <a href="#sleep">Section 15.10.2, &#8220;sleep&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/10177641/proper-way-of-handling-threads-in-kernel" class="bare">https://stackoverflow.com/questions/10177641/proper-way-of-handling-threads-in-kernel</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/4084708/how-to-wait-for-a-linux-kernel-thread-kthreadto-exit" class="bare">https://stackoverflow.com/questions/4084708/how-to-wait-for-a-linux-kernel-thread-kthreadto-exit</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="kthreads"><a class="anchor" href="#kthreads"></a><a class="link" href="#kthreads">15.10.1. kthreads</a></h4>
<div class="paragraph">
<p>Let&#8217;s launch two threads and see if they actually run in parallel:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod kthreads.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/kthreads.c">kernel_modules/kthreads.c</a></p>
</div>
<div class="paragraph">
<p>Outcome: two threads count to dmesg from <code>0</code> to <code>9</code> in parallel.</p>
</div>
<div class="paragraph">
<p>Each line has output of form:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;thread_id&gt; &lt;count&gt;</pre>
</div>
</div>
<div class="paragraph">
<p>Possible very likely outcome:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>1 0
2 0
1 1
2 1
1 2
2 2
1 3
2 3</pre>
</div>
</div>
<div class="paragraph">
<p>The threads almost always interleaved nicely, thus confirming that they are actually running in parallel.</p>
</div>
</div>
<div class="sect3">
<h4 id="sleep"><a class="anchor" href="#sleep"></a><a class="link" href="#sleep">15.10.2. sleep</a></h4>
<div class="paragraph">
<p>Count to dmesg every one second from <code>0</code> up to <code>n - 1</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod sleep.ko n=5</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/sleep.c">kernel_modules/sleep.c</a></p>
</div>
<div class="paragraph">
<p>The sleep is done with a call to <a href="https://github.com/torvalds/linux/blob/v4.17/kernel/time/timer.c#L1984"><code>usleep_range</code></a> directly inside <code>module_init</code> for simplicity.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/15994603/how-to-sleep-in-the-linux-kernel/44153288#44153288" class="bare">https://stackoverflow.com/questions/15994603/how-to-sleep-in-the-linux-kernel/44153288#44153288</a></p>
</li>
<li>
<p><a href="https://github.com/torvalds/linux/blob/v4.17/Documentation/timers/timers-howto.txt" class="bare">https://github.com/torvalds/linux/blob/v4.17/Documentation/timers/timers-howto.txt</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="workqueues"><a class="anchor" href="#workqueues"></a><a class="link" href="#workqueues">15.10.3. Workqueues</a></h4>
<div class="paragraph">
<p>A more convenient front-end for <a href="#kthread">kthread</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod workqueue_cheat.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: count from <code>0</code> to <code>9</code> infinitely many times</p>
</div>
<div class="paragraph">
<p>Stop counting:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rmmod workqueue_cheat</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/workqueue_cheat.c">kernel_modules/workqueue_cheat.c</a></p>
</div>
<div class="paragraph">
<p>The workqueue thread is killed after the worker function returns.</p>
</div>
<div class="paragraph">
<p>We can&#8217;t call the module just <code>workqueue.c</code> because there is already a built-in with that name: <a href="https://unix.stackexchange.com/questions/364956/how-can-insmod-fail-with-kernel-module-is-already-loaded-even-is-lsmod-does-not" class="bare">https://unix.stackexchange.com/questions/364956/how-can-insmod-fail-with-kernel-module-is-already-loaded-even-is-lsmod-does-not</a></p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://github.com/torvalds/linux/blob/v4.17/Documentation/core-api/workqueue.rst" class="bare">https://github.com/torvalds/linux/blob/v4.17/Documentation/core-api/workqueue.rst</a></p>
</div>
<div class="sect4">
<h5 id="workqueue-from-workqueue"><a class="anchor" href="#workqueue-from-workqueue"></a><a class="link" href="#workqueue-from-workqueue">15.10.3.1. Workqueue from workqueue</a></h5>
<div class="paragraph">
<p>Count from <code>0</code> to <code>9</code> every second infinitely many times by scheduling a new work item from a work item:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod work_from_work.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Stop:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rmmod work_from_work</pre>
</div>
</div>
<div class="paragraph">
<p>The sleep is done indirectly through: <a href="https://github.com/torvalds/linux/blob/v4.17/include/linux/workqueue.h#L522"><code>queue_delayed_work</code></a>, which waits the specified time before scheduling the work.</p>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/work_from_work.c">kernel_modules/work_from_work.c</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="schedule"><a class="anchor" href="#schedule"></a><a class="link" href="#schedule">15.10.4. schedule</a></h4>
<div class="paragraph">
<p>Let&#8217;s block the entire kernel! Yay:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after 'dmesg -n 1;insmod schedule.ko schedule=0'</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the system hangs, the only way out is to kill the VM.</p>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/schedule.c">kernel_modules/schedule.c</a></p>
</div>
<div class="paragraph">
<p>kthreads only allow interrupting if you call <code>schedule()</code>, and the <code>schedule=0</code> <a href="#kernel-module-parameters">kernel module parameter</a> turns it off.</p>
</div>
<div class="paragraph">
<p>Sleep functions like <code>usleep_range</code> also end up calling schedule.</p>
</div>
<div class="paragraph">
<p>If we allow <code>schedule()</code> to be called, then the system becomes responsive:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after 'dmesg -n 1;insmod schedule.ko schedule=1'</pre>
</div>
</div>
<div class="paragraph">
<p>and we can observe the counting with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>dmesg -w</pre>
</div>
</div>
<div class="paragraph">
<p>The system also responds if we <a href="#number-of-cores">add another core</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --cpus 2 --eval-after 'dmesg -n 1;insmod schedule.ko schedule=0'</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="wait-queues"><a class="anchor" href="#wait-queues"></a><a class="link" href="#wait-queues">15.10.5. Wait queues</a></h4>
<div class="paragraph">
<p>Wait queues are a way to make a thread sleep until an event happens on the queue:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod wait_queue.c</pre>
</div>
</div>
<div class="paragraph">
<p>Dmesg output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0 0
1 0
2 0
# Wait one second.
0 1
1 1
2 1
# Wait one second.
0 2
1 2
2 2
...</pre>
</div>
</div>
<div class="paragraph">
<p>Stop the count:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rmmod wait_queue</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/wait_queue.c">kernel_modules/wait_queue.c</a></p>
</div>
<div class="paragraph">
<p>This example launches three threads:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>one thread generates events every with <a href="https://github.com/torvalds/linux/blob/v4.17/include/linux/wait.h#L195"><code>wake_up</code></a></p>
</li>
<li>
<p>the other two threads wait for that with <a href="https://github.com/torvalds/linux/blob/v4.17/include/linux/wait.h#L286"><code>wait_event</code></a>, and print a dmesg when it happens.</p>
<div class="paragraph">
<p>The <code>wait_event</code> macro works a bit like:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>while (!cond)
sleep_until_event</pre>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="timers"><a class="anchor" href="#timers"></a><a class="link" href="#timers">15.11. Timers</a></h3>
<div class="paragraph">
<p>Count from <code>0</code> to <code>9</code> infinitely many times in 1 second intervals using timers:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod timer.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Stop counting:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rmmod timer</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/timer.c">kernel_modules/timer.c</a></p>
</div>
<div class="paragraph">
<p>Timers are callbacks that run when an interrupt happens, from the interrupt context itself.</p>
</div>
<div class="paragraph">
<p>Therefore they produce more accurate timing than thread scheduling, which is more complex, but you can&#8217;t do too much work inside of them.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/10812858/timers-in-linux-device-drivers" class="bare">https://stackoverflow.com/questions/10812858/timers-in-linux-device-drivers</a></p>
</li>
<li>
<p><a href="https://gist.github.com/yagihiro/310149" class="bare">https://gist.github.com/yagihiro/310149</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="irq"><a class="anchor" href="#irq"></a><a class="link" href="#irq">15.12. IRQ</a></h3>
<div class="sect3">
<h4 id="irq-ko"><a class="anchor" href="#irq-ko"></a><a class="link" href="#irq-ko">15.12.1. irq.ko</a></h4>
<div class="paragraph">
<p>Brute force monitor every shared interrupt that will accept us:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after 'insmod irq.ko' --graphic</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/irq.c">kernel_modules/irq.c</a>.</p>
</div>
<div class="paragraph">
<p>Now try the following:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>press a keyboard key and then release it after a few seconds</p>
</li>
<li>
<p>press a mouse key, and release it after a few seconds</p>
</li>
<li>
<p>move the mouse around</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Outcome: dmesg shows which IRQ was fired for each action through messages of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>handler irq = 1 dev = 250</pre>
</div>
</div>
<div class="paragraph">
<p><code>dev</code> is the character device for the module and never changes, as can be confirmed by:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>grep lkmc_irq /proc/devices</pre>
</div>
</div>
<div class="paragraph">
<p>The IRQs that we observe are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>1</code> for keyboard press and release.</p>
<div class="paragraph">
<p>If you hold the key down for a while, it starts firing at a constant rate. So this happens at the hardware level!</p>
</div>
</li>
<li>
<p><code>12</code> mouse actions</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This only works if for IRQs for which the other handlers are registered as <code>IRQF_SHARED</code>.</p>
</div>
<div class="paragraph">
<p>We can see which ones are those, either via dmesg messages of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>genirq: Flags mismatch irq 0. 00000080 (myirqhandler0) vs. 00015a00 (timer)
request_irq irq = 0 ret = -16
request_irq irq = 1 ret = 0</pre>
</div>
</div>
<div class="paragraph">
<p>which indicate that <code>0</code> is not, but <code>1</code> is, or with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /proc/interrupts</pre>
</div>
</div>
<div class="paragraph">
<p>which shows:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> 0: 31 IO-APIC 2-edge timer
1: 9 IO-APIC 1-edge i8042, myirqhandler0</pre>
</div>
</div>
<div class="paragraph">
<p>so only <code>1</code> has <code>myirqhandler0</code> attached but not <code>0</code>.</p>
</div>
<div class="paragraph">
<p>The <a href="#qemu-monitor">QEMU monitor</a> also has some interrupt statistics for x86_64:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor info irq</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: properly understand how each IRQ maps to what number.</p>
</div>
</div>
<div class="sect3">
<h4 id="dummy-irq"><a class="anchor" href="#dummy-irq"></a><a class="link" href="#dummy-irq">15.12.2. dummy-irq</a></h4>
<div class="paragraph">
<p>The Linux kernel v4.16 mainline also has a <code>dummy-irq</code> module at <code>drivers/misc/dummy-irq.c</code> for monitoring a single IRQ.</p>
</div>
<div class="paragraph">
<p>We build it by default with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>CONFIG_DUMMY_IRQ=m</pre>
</div>
</div>
<div class="paragraph">
<p>And then you can do</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --graphic</pre>
</div>
</div>
<div class="paragraph">
<p>and in guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>modprobe dummy-irq irq=1</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: when you click a key on the keyboard, dmesg shows:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>dummy-irq: interrupt occurred on IRQ 1</pre>
</div>
</div>
<div class="paragraph">
<p>However, this module is intended to fire only once as can be seen from its source:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> static int count = 0;
if (count == 0) {
printk(KERN_INFO "dummy-irq: interrupt occurred on IRQ %d\n",
irq);
count++;
}</pre>
</div>
</div>
<div class="paragraph">
<p>and furthermore interrupt <code>1</code> and <code>12</code> happen immediately TODO why, were they somehow pending?</p>
</div>
<div class="paragraph">
<p>So so see something interesting, you need to monitor an interrupt that is more rare than the keyboard, e.g. <a href="#platform_device">platform_device</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="procinterrupts"><a class="anchor" href="#procinterrupts"></a><a class="link" href="#procinterrupts">15.12.3. /proc/interrupts</a></h4>
<div class="paragraph">
<p>In the guest with <a href="#qemu-graphic-mode">QEMU graphic mode</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>watch -n 1 cat /proc/interrupts</pre>
</div>
</div>
<div class="paragraph">
<p>Then see how clicking the mouse and keyboard affect the interrupt counts.</p>
</div>
<div class="paragraph">
<p>This confirms that:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>1: keyboard</p>
</li>
<li>
<p>12: mouse click and drags</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The module also shows which handlers are registered for each IRQ, as we have observed at <a href="#irq-ko">irq.ko</a></p>
</div>
<div class="paragraph">
<p>When in text mode, we can also observe interrupt line 4 with handler <code>ttyS0</code> increase continuously as IO goes through the UART.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="kernel-utility-functions"><a class="anchor" href="#kernel-utility-functions"></a><a class="link" href="#kernel-utility-functions">15.13. Kernel utility functions</a></h3>
<div class="paragraph">
<p><a href="https://github.com/torvalds/linux/blob/v4.17/Documentation/core-api/kernel-api.rst" class="bare">https://github.com/torvalds/linux/blob/v4.17/Documentation/core-api/kernel-api.rst</a></p>
</div>
<div class="sect3">
<h4 id="kstrto"><a class="anchor" href="#kstrto"></a><a class="link" href="#kstrto">15.13.1. kstrto</a></h4>
<div class="paragraph">
<p>Convert a string to an integer:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./kstrto.sh
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/kstrto.c">kernel_modules/kstrto.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/kstrto.sh">rootfs_overlay/lkmc/kstrto.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658" class="bare">https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658</a></p>
</div>
</div>
<div class="sect3">
<h4 id="virt_to_phys"><a class="anchor" href="#virt_to_phys"></a><a class="link" href="#virt_to_phys">15.13.2. virt_to_phys</a></h4>
<div class="paragraph">
<p>Convert a virtual address to physical:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod virt_to_phys.ko
cat /sys/kernel/debug/lkmc_virt_to_phys</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/virt_to_phys.c">kernel_modules/virt_to_phys.c</a></p>
</div>
<div class="paragraph">
<p>Sample output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>*kmalloc_ptr = 0x12345678
kmalloc_ptr = ffff88000e169ae8
virt_to_phys(kmalloc_ptr) = 0xe169ae8
static_var = 0x12345678
&amp;static_var = ffffffffc0002308
virt_to_phys(&amp;static_var) = 0x40002308</pre>
</div>
</div>
<div class="paragraph">
<p>We can confirm that the <code>kmalloc_ptr</code> translation worked with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor 'xp 0xe169ae8'</pre>
</div>
</div>
<div class="paragraph">
<p>which reads four bytes from a given physical address, and gives the expected:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>000000000e169ae8: 0x12345678</pre>
</div>
</div>
<div class="paragraph">
<p>TODO it only works for kmalloc however, for the static variable:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor 'xp 0x40002308'</pre>
</div>
</div>
<div class="paragraph">
<p>it gave a wrong value of <code>00000000</code>.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/5748492/is-there-any-api-for-determining-the-physical-address-from-virtual-address-in-li/45128487#45128487" class="bare">https://stackoverflow.com/questions/5748492/is-there-any-api-for-determining-the-physical-address-from-virtual-address-in-li/45128487#45128487</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/39134990/mmap-of-dev-mem-fails-with-invalid-argument-for-virt-to-phys-address-but-addre/45127582#45127582" class="bare">https://stackoverflow.com/questions/39134990/mmap-of-dev-mem-fails-with-invalid-argument-for-virt-to-phys-address-but-addre/45127582#45127582</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/43325205/can-we-use-virt-to-phys-for-user-space-memory-in-kernel-module" class="bare">https://stackoverflow.com/questions/43325205/can-we-use-virt-to-phys-for-user-space-memory-in-kernel-module</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="userland-physical-address-experiments"><a class="anchor" href="#userland-physical-address-experiments"></a><a class="link" href="#userland-physical-address-experiments">15.13.2.1. Userland physical address experiments</a></h5>
<div class="paragraph">
<p>Only tested in x86_64.</p>
</div>
<div class="paragraph">
<p>The Linux kernel exposes physical addresses to userland through:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>/proc/&lt;pid&gt;/maps</code></p>
</li>
<li>
<p><code>/proc/&lt;pid&gt;/pagemap</code></p>
</li>
<li>
<p><code>/dev/mem</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In this section we will play with them.</p>
</div>
<div class="paragraph">
<p>First get a virtual address to play with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./posix/virt_to_phys_test.out &amp;</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/virt_to_phys_test.c">userland/posix/virt_to_phys_test.c</a></p>
</div>
<div class="paragraph">
<p>Sample output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vaddr 0x600800
pid 110</pre>
</div>
</div>
<div class="paragraph">
<p>The program:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>allocates a <code>volatile</code> variable and sets is value to <code>0x12345678</code></p>
</li>
<li>
<p>prints the virtual address of the variable, and the program PID</p>
</li>
<li>
<p>runs a while loop until until the value of the variable gets mysteriously changed somehow, e.g. by nasty tinkerers like us</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Then, translate the virtual address to physical using <code>/proc/&lt;pid&gt;/maps</code> and <code>/proc/&lt;pid&gt;/pagemap</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./linux/virt_to_phys_user.out 110 0x600800</pre>
</div>
</div>
<div class="paragraph">
<p>Sample output physical address:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0x7c7b800</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/virt_to_phys_user.c">userland/linux/virt_to_phys_user.c</a></p>
</div>
<div class="paragraph">
<p>Now we can verify that <code>linux/virt_to_phys_user.out</code> gave the correct physical address in the following ways:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#qemu-xp">QEMU xp</a></p>
</li>
<li>
<p><a href="#dev-mem">/dev/mem</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/17021214/decode-proc-pid-pagemap-entry/45126141#45126141" class="bare">https://stackoverflow.com/questions/17021214/decode-proc-pid-pagemap-entry/45126141#45126141</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/6284810/proc-pid-pagemaps-and-proc-pid-maps-linux/45500208#45500208" class="bare">https://stackoverflow.com/questions/6284810/proc-pid-pagemaps-and-proc-pid-maps-linux/45500208#45500208</a></p>
</li>
</ul>
</div>
<div class="sect5">
<h6 id="qemu-xp"><a class="anchor" href="#qemu-xp"></a><a class="link" href="#qemu-xp">15.13.2.1.1. QEMU xp</a></h6>
<div class="paragraph">
<p>The <code>xp</code> <a href="#qemu-monitor">QEMU monitor</a> command reads memory at a given physical address.</p>
</div>
<div class="paragraph">
<p>First launch <code>linux/virt_to_phys_user.out</code> as described at <a href="#userland-physical-address-experiments">Userland physical address experiments</a>.</p>
</div>
<div class="paragraph">
<p>On a second terminal, use QEMU to read the physical address:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor 'xp 0x7c7b800'</pre>
</div>
</div>
<div class="paragraph">
<p>Output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0000000007c7b800: 0x12345678</pre>
</div>
</div>
<div class="paragraph">
<p>Yes!!! We read the correct value from the physical address.</p>
</div>
<div class="paragraph">
<p>We could not find however to write to memory from the QEMU monitor, boring.</p>
</div>
</div>
<div class="sect5">
<h6 id="dev-mem"><a class="anchor" href="#dev-mem"></a><a class="link" href="#dev-mem">15.13.2.1.2. /dev/mem</a></h6>
<div class="paragraph">
<p><code>/dev/mem</code> exposes access to physical addresses, and we use it through the convenient <code>devmem</code> BusyBox utility.</p>
</div>
<div class="paragraph">
<p>First launch <code>linux/virt_to_phys_user.out</code> as described at <a href="#userland-physical-address-experiments">Userland physical address experiments</a>.</p>
</div>
<div class="paragraph">
<p>Next, read from the physical address:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>devmem 0x7c7b800</pre>
</div>
</div>
<div class="paragraph">
<p>Possible output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Memory mapped at address 0x7ff7dbe01000.
Value at address 0X7C7B800 (0x7ff7dbe01800): 0x12345678</pre>
</div>
</div>
<div class="paragraph">
<p>which shows that the physical memory contains the expected value <code>0x12345678</code>.</p>
</div>
<div class="paragraph">
<p><code>0x7ff7dbe01000</code> is a new virtual address that <code>devmem</code> maps to the physical address to be able to read from it.</p>
</div>
<div class="paragraph">
<p>Modify the physical memory:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>devmem 0x7c7b800 w 0x9abcdef0</pre>
</div>
</div>
<div class="paragraph">
<p>After one second, we see on the screen:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>i 9abcdef0
[1]+ Done ./posix/virt_to_phys_test.out</pre>
</div>
</div>
<div class="paragraph">
<p>so the value changed, and the <code>while</code> loop exited!</p>
</div>
<div class="paragraph">
<p>This example requires:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>CONFIG_STRICT_DEVMEM=n</code>, otherwise <code>devmem</code> fails with:</p>
<div class="literalblock">
<div class="content">
<pre>devmem: mmap: Operation not permitted</pre>
</div>
</div>
</li>
<li>
<p><code>nopat</code> kernel parameter</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>which we set by default.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/11891979/how-to-access-mmaped-dev-mem-without-crashing-the-linux-kernel" class="bare">https://stackoverflow.com/questions/11891979/how-to-access-mmaped-dev-mem-without-crashing-the-linux-kernel</a></p>
</div>
</div>
<div class="sect5">
<h6 id="pagemap_dump-out"><a class="anchor" href="#pagemap_dump-out"></a><a class="link" href="#pagemap_dump-out">15.13.2.1.3. pagemap_dump.out</a></h6>
<div class="paragraph">
<p>Dump the physical address of all pages mapped to a given process using <code>/proc/&lt;pid&gt;/maps</code> and <code>/proc/&lt;pid&gt;/pagemap</code>.</p>
</div>
<div class="paragraph">
<p>First launch <code>linux/virt_to_phys_user.out</code> as described at <a href="#userland-physical-address-experiments">Userland physical address experiments</a>. Suppose that the output was:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># ./posix/virt_to_phys_test.out &amp;
vaddr 0x601048
pid 63
# ./linux/virt_to_phys_user.out 63 0x601048
0x1a61048</pre>
</div>
</div>
<div class="paragraph">
<p>Now obtain the page map for the process:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./linux/pagemap_dump.out 63</pre>
</div>
</div>
<div class="paragraph">
<p>Sample output excerpt:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vaddr pfn soft-dirty file/shared swapped present library
400000 1ede 0 1 0 1 ./posix/virt_to_phys_test.out
600000 1a6f 0 0 0 1 ./posix/virt_to_phys_test.out
601000 1a61 0 0 0 1 ./posix/virt_to_phys_test.out
602000 2208 0 0 0 1 [heap]
603000 220b 0 0 0 1 [heap]
7ffff78ec000 1fd4 0 1 0 1 /lib/libuClibc-1.0.30.so</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/pagemap_dump.c">userland/linux/pagemap_dump.c</a></p>
</div>
<div class="paragraph">
<p>Adapted from: <a href="https://github.com/dwks/pagemap/blob/8a25747bc79d6080c8b94eac80807a4dceeda57a/pagemap2.c" class="bare">https://github.com/dwks/pagemap/blob/8a25747bc79d6080c8b94eac80807a4dceeda57a/pagemap2.c</a></p>
</div>
<div class="paragraph">
<p>Meaning of the flags:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>vaddr</code>: first virtual address of a page the belongs to the process. Notably:</p>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain readelf -- -l "$(./getvar userland_build_dir)/posix/virt_to_phys_test.out"</pre>
</div>
</div>
<div class="paragraph">
<p>contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
...
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x000000000000075c 0x000000000000075c R E 0x200000
LOAD 0x0000000000000e98 0x0000000000600e98 0x0000000000600e98
0x00000000000001b4 0x0000000000000218 RW 0x200000
Section to Segment mapping:
Segment Sections...
...
02 .interp .hash .dynsym .dynstr .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame
03 .ctors .dtors .jcr .dynamic .got.plt .data .bss</pre>
</div>
</div>
<div class="paragraph">
<p>from which we deduce that:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>400000</code> is the text segment</p>
</li>
<li>
<p><code>600000</code> is the data segment</p>
</li>
</ul>
</div>
</li>
<li>
<p><code>pfn</code>: add three zeroes to it, and you have the physical address.</p>
<div class="paragraph">
<p>Three zeroes is 12 bits which is 4kB, which is the size of a page.</p>
</div>
<div class="paragraph">
<p>For example, the virtual address <code>0x601000</code> has <code>pfn</code> of <code>0x1a61</code>, which means that its physical address is <code>0x1a61000</code></p>
</div>
<div class="paragraph">
<p>This is consistent with what <code>linux/virt_to_phys_user.out</code> told us: the virtual address <code>0x601048</code> has physical address <code>0x1a61048</code>.</p>
</div>
<div class="paragraph">
<p><code>048</code> corresponds to the three last zeroes, and is the offset within the page.</p>
</div>
<div class="paragraph">
<p>Also, this value falls inside <code>0x601000</code>, which as previously analyzed is the data section, which is the normal location for global variables such as ours.</p>
</div>
</li>
<li>
<p><code>soft-dirty</code>: TODO</p>
</li>
<li>
<p><code>file/shared</code>: TODO. <code>1</code> seems to indicate that the page can be shared across processes, possibly for read-only pages? E.g. the text segment has <code>1</code>, but the data has <code>0</code>.</p>
</li>
<li>
<p><code>swapped</code>: TODO swapped to disk?</p>
</li>
<li>
<p><code>present</code>: TODO vs swapped?</p>
</li>
<li>
<p><code>library</code>: which executable owns that page</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This program works in two steps:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>parse the human readable lines lines from <code>/proc/&lt;pid&gt;/maps</code>. This files contains lines of form:</p>
<div class="literalblock">
<div class="content">
<pre>7ffff7b6d000-7ffff7bdd000 r-xp 00000000 fe:00 658 /lib/libuClibc-1.0.22.so</pre>
</div>
</div>
<div class="paragraph">
<p>which tells us that:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>7f8af99f8000-7f8af99ff000</code> is a virtual address range that belong to the process, possibly containing multiple pages.</p>
</li>
<li>
<p><code>/lib/libuClibc-1.0.22.so</code> is the name of the library that owns that memory</p>
</li>
</ul>
</div>
</li>
<li>
<p>loop over each page of each address range, and ask <code>/proc/&lt;pid&gt;/pagemap</code> for more information about that page, including the physical address</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="linux-kernel-tracing"><a class="anchor" href="#linux-kernel-tracing"></a><a class="link" href="#linux-kernel-tracing">15.14. Linux kernel tracing</a></h3>
<div class="paragraph">
<p>Good overviews:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="http://www.brendangregg.com/blog/2015-07-08/choosing-a-linux-tracer.html" class="bare">http://www.brendangregg.com/blog/2015-07-08/choosing-a-linux-tracer.html</a> by Brendan Greg, AKA the master of tracing. Also: <a href="https://github.com/brendangregg/perf-tools" class="bare">https://github.com/brendangregg/perf-tools</a></p>
</li>
<li>
<p><a href="https://jvns.ca/blog/2017/07/05/linux-tracing-systems/" class="bare">https://jvns.ca/blog/2017/07/05/linux-tracing-systems/</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>I hope to have examples of all methods some day, since I&#8217;m obsessed with visibility.</p>
</div>
<div class="sect3">
<h4 id="config_proc_events"><a class="anchor" href="#config_proc_events"></a><a class="link" href="#config_proc_events">15.14.1. CONFIG_PROC_EVENTS</a></h4>
<div class="paragraph">
<p>Logs proc events such as process creation to a <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/netlink.c">netlink socket</a>.</p>
</div>
<div class="paragraph">
<p>We then have a userland program that listens to the events and prints them out:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># ./linux/proc_events.out &amp;
# set mcast listen ok
# sleep 2 &amp; sleep 1
fork: parent tid=48 pid=48 -&gt; child tid=79 pid=79
fork: parent tid=48 pid=48 -&gt; child tid=80 pid=80
exec: tid=80 pid=80
exec: tid=79 pid=79
# exit: tid=80 pid=80 exit_code=0
exit: tid=79 pid=79 exit_code=0
echo a
a
#</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/proc_events.c">userland/linux/proc_events.c</a></p>
</div>
<div class="paragraph">
<p>TODO: why <code>exit: tid=79</code> shows after <code>exit: tid=80</code>?</p>
</div>
<div class="paragraph">
<p>Note how <code>echo a</code> is a Bash built-in, and therefore does not spawn a new process.</p>
</div>
<div class="paragraph">
<p>TODO: why does this produce no output?</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./linux/proc_events.out &gt;f &amp;</pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/6075013/detect-launching-of-programs-on-linux-platform/8255487#8255487" class="bare">https://stackoverflow.com/questions/6075013/detect-launching-of-programs-on-linux-platform/8255487#8255487</a></p>
</li>
<li>
<p><a href="https://serverfault.com/questions/199654/does-anyone-know-a-simple-way-to-monitor-root-process-spawn" class="bare">https://serverfault.com/questions/199654/does-anyone-know-a-simple-way-to-monitor-root-process-spawn</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/260162/how-to-track-newly-created-processes" class="bare">https://unix.stackexchange.com/questions/260162/how-to-track-newly-created-processes</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>TODO can you get process data such as UID and process arguments? It seems not since <code>exec_proc_event</code> contains so little data: <a href="https://github.com/torvalds/linux/blob/v4.16/include/uapi/linux/cn_proc.h#L80" class="bare">https://github.com/torvalds/linux/blob/v4.16/include/uapi/linux/cn_proc.h#L80</a> We could try to immediately read it from <code>/proc</code>, but there is a risk that the process finished and another one took its PID, so it wouldn&#8217;t be reliable.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://unix.stackexchange.com/questions/163681/print-pids-and-names-of-processes-as-they-are-created/163689" class="bare">https://unix.stackexchange.com/questions/163681/print-pids-and-names-of-processes-as-they-are-created/163689</a> requests process name</p>
</li>
<li>
<p><a href="https://serverfault.com/questions/199654/does-anyone-know-a-simple-way-to-monitor-root-process-spawn" class="bare">https://serverfault.com/questions/199654/does-anyone-know-a-simple-way-to-monitor-root-process-spawn</a> requests UID</p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="config_proc_events-aarch64"><a class="anchor" href="#config_proc_events-aarch64"></a><a class="link" href="#config_proc_events-aarch64">15.14.1.1. CONFIG_PROC_EVENTS aarch64</a></h5>
<div class="paragraph">
<p>0111ca406bdfa6fd65a2605d353583b4c4051781 was failing with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&gt;&gt;&gt; kernel_modules 1.0 Building
/usr/bin/make -j8 -C '/linux-kernel-module-cheat//out/aarch64/buildroot/build/kernel_modules-1.0/user' BR2_PACKAGE_OPENBLAS="" CC="/linux-kernel-module-cheat//out/aarch64/buildroot/host/bin/aarch64-buildroot-linux-uclibc-gcc" LD="/linux-kernel-module-cheat//out/aarch64/buildroot/host/bin/aarch64-buildroot-linux-uclibc-ld"
/linux-kernel-module-cheat//out/aarch64/buildroot/host/bin/aarch64-buildroot-linux-uclibc-gcc -ggdb3 -fopenmp -O0 -std=c99 -Wall -Werror -Wextra -o 'proc_events.out' 'proc_events.c'
In file included from /linux-kernel-module-cheat//out/aarch64/buildroot/host/aarch64-buildroot-linux-uclibc/sysroot/usr/include/signal.h:329:0,
from proc_events.c:12:
/linux-kernel-module-cheat//out/aarch64/buildroot/host/aarch64-buildroot-linux-uclibc/sysroot/usr/include/sys/ucontext.h:50:16: error: field uc_mcontext has incomplete type
mcontext_t uc_mcontext;
^~~~~~~~~~~</pre>
</div>
</div>
<div class="paragraph">
<p>so we commented it out.</p>
</div>
<div class="paragraph">
<p>Related threads:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://mailman.uclibc-ng.org/pipermail/devel/2018-January/001624.html" class="bare">https://mailman.uclibc-ng.org/pipermail/devel/2018-January/001624.html</a></p>
</li>
<li>
<p><a href="https://github.com/DynamoRIO/dynamorio/issues/2356" class="bare">https://github.com/DynamoRIO/dynamorio/issues/2356</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>If we try to naively update uclibc to 1.0.29 with <code>buildroot_override</code>, which contains the above mentioned patch, clean <code>aarch64</code> test build fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>../utils/ldd.c: In function 'elf_find_dynamic':
../utils/ldd.c:238:12: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
return (void *)byteswap_to_host(dynp-&gt;d_un.d_val);
^
/tmp/user/20321/cciGScKB.o: In function `process_line_callback':
msgmerge.c:(.text+0x22): undefined reference to `escape'
/tmp/user/20321/cciGScKB.o: In function `process':
msgmerge.c:(.text+0xf6): undefined reference to `poparser_init'
msgmerge.c:(.text+0x11e): undefined reference to `poparser_feed_line'
msgmerge.c:(.text+0x128): undefined reference to `poparser_finish'
collect2: error: ld returned 1 exit status
Makefile.in:120: recipe for target '../utils/msgmerge.host' failed
make[2]: *** [../utils/msgmerge.host] Error 1
make[2]: *** Waiting for unfinished jobs....
/tmp/user/20321/ccF8V8jF.o: In function `process':
msgfmt.c:(.text+0xbf3): undefined reference to `poparser_init'
msgfmt.c:(.text+0xc1f): undefined reference to `poparser_feed_line'
msgfmt.c:(.text+0xc2b): undefined reference to `poparser_finish'
collect2: error: ld returned 1 exit status
Makefile.in:120: recipe for target '../utils/msgfmt.host' failed
make[2]: *** [../utils/msgfmt.host] Error 1
package/pkg-generic.mk:227: recipe for target '/data/git/linux-kernel-module-cheat/out/aarch64/buildroot/build/uclibc-custom/.stamp_built' failed
make[1]: *** [/data/git/linux-kernel-module-cheat/out/aarch64/buildroot/build/uclibc-custom/.stamp_built] Error 2
Makefile:79: recipe for target '_all' failed
make: *** [_all] Error 2</pre>
</div>
</div>
<div class="paragraph">
<p>Buildroot master has already moved to uclibc 1.0.29 at f8546e836784c17aa26970f6345db9d515411700, but it is not yet in any tag&#8230;&#8203; so I&#8217;m not tempted to update it yet just for this.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="ftrace"><a class="anchor" href="#ftrace"></a><a class="link" href="#ftrace">15.14.2. ftrace</a></h4>
<div class="paragraph">
<p>Trace a single function:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd /sys/kernel/debug/tracing/
# Stop tracing.
echo 0 &gt; tracing_on
# Clear previous trace.
echo &gt; trace
# List the available tracers, and pick one.
cat available_tracers
echo function &gt; current_tracer
# List all functions that can be traced
# cat available_filter_functions
# Choose one.
echo __kmalloc &gt; set_ftrace_filter
# Confirm that only __kmalloc is enabled.
cat enabled_functions
echo 1 &gt; tracing_on
# Latest events.
head trace
# Observe trace continuously, and drain seen events out.
cat trace_pipe &amp;</pre>
</div>
</div>
<div class="paragraph">
<p>Sample output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># tracer: function
#
# entries-in-buffer/entries-written: 97/97 #P:1
#
# _-----=&gt; irqs-off
# / _----=&gt; need-resched
# | / _---=&gt; hardirq/softirq
# || / _--=&gt; preempt-depth
# ||| / delay
# TASK-PID CPU# |||| TIMESTAMP FUNCTION
# | | | |||| | |
head-228 [000] .... 825.534637: __kmalloc &lt;-load_elf_phdrs
head-228 [000] .... 825.534692: __kmalloc &lt;-load_elf_binary
head-228 [000] .... 825.534815: __kmalloc &lt;-load_elf_phdrs
head-228 [000] .... 825.550917: __kmalloc &lt;-__seq_open_private
head-228 [000] .... 825.550953: __kmalloc &lt;-tracing_open
head-229 [000] .... 826.756585: __kmalloc &lt;-load_elf_phdrs
head-229 [000] .... 826.756627: __kmalloc &lt;-load_elf_binary
head-229 [000] .... 826.756719: __kmalloc &lt;-load_elf_phdrs
head-229 [000] .... 826.773796: __kmalloc &lt;-__seq_open_private
head-229 [000] .... 826.773835: __kmalloc &lt;-tracing_open
head-230 [000] .... 827.174988: __kmalloc &lt;-load_elf_phdrs
head-230 [000] .... 827.175046: __kmalloc &lt;-load_elf_binary
head-230 [000] .... 827.175171: __kmalloc &lt;-load_elf_phdrs</pre>
</div>
</div>
<div class="paragraph">
<p>Trace all possible functions, and draw a call graph:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 1 &gt; max_graph_depth
echo 1 &gt; events/enable
echo function_graph &gt; current_tracer</pre>
</div>
</div>
<div class="paragraph">
<p>Sample output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># CPU DURATION FUNCTION CALLS
# | | | | | | |
0) 2.173 us | } /* ntp_tick_length */
0) | timekeeping_update() {
0) 4.176 us | ntp_get_next_leap();
0) 5.016 us | update_vsyscall();
0) | raw_notifier_call_chain() {
0) 2.241 us | notifier_call_chain();
0) + 19.879 us | }
0) 3.144 us | update_fast_timekeeper();
0) 2.738 us | update_fast_timekeeper();
0) ! 117.147 us | }
0) | _raw_spin_unlock_irqrestore() {
0) 4.045 us | _raw_write_unlock_irqrestore();
0) + 22.066 us | }
0) ! 265.278 us | } /* update_wall_time */</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: what do <code>+</code> and <code>!</code> mean?</p>
</div>
<div class="paragraph">
<p>Each <code>enable</code> under the <code>events/</code> tree enables a certain set of functions, the higher the <code>enable</code> more functions are enabled.</p>
</div>
<div class="paragraph">
<p>TODO: can you get function arguments? <a href="https://stackoverflow.com/questions/27608752/does-ftrace-allow-capture-of-system-call-arguments-to-the-linux-kernel-or-only" class="bare">https://stackoverflow.com/questions/27608752/does-ftrace-allow-capture-of-system-call-arguments-to-the-linux-kernel-or-only</a></p>
</div>
<div class="sect4">
<h5 id="ftrace-system-calls"><a class="anchor" href="#ftrace-system-calls"></a><a class="link" href="#ftrace-system-calls">15.14.2.1. ftrace system calls</a></h5>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/29840213/how-do-i-trace-a-system-call-in-linux/51856306#51856306" class="bare">https://stackoverflow.com/questions/29840213/how-do-i-trace-a-system-call-in-linux/51856306#51856306</a></p>
</div>
</div>
<div class="sect4">
<h5 id="trace-cmd"><a class="anchor" href="#trace-cmd"></a><a class="link" href="#trace-cmd">15.14.2.2. trace-cmd</a></h5>
<div class="paragraph">
<p>TODO example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_TRACE_CMD=y'</pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="kprobes"><a class="anchor" href="#kprobes"></a><a class="link" href="#kprobes">15.14.3. Kprobes</a></h4>
<div class="paragraph">
<p>kprobes is an instrumentation mechanism that injects arbitrary code at a given address in a trap instruction, much like GDB. Oh, the good old kernel. :-)</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux --config 'CONFIG_KPROBES=y'</pre>
</div>
</div>
<div class="paragraph">
<p>Then on guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod kprobe_example.ko
sleep 4 &amp; sleep 4 &amp;'</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: dmesg outputs on every fork:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;_do_fork&gt; pre_handler: p-&gt;addr = 0x00000000e1360063, ip = ffffffff810531d1, flags = 0x246
&lt;_do_fork&gt; post_handler: p-&gt;addr = 0x00000000e1360063, flags = 0x246
&lt;_do_fork&gt; pre_handler: p-&gt;addr = 0x00000000e1360063, ip = ffffffff810531d1, flags = 0x246
&lt;_do_fork&gt; post_handler: p-&gt;addr = 0x00000000e1360063, flags = 0x246</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/kprobe_example.c">kernel_modules/kprobe_example.c</a></p>
</div>
<div class="paragraph">
<p>TODO: it does not work if I try to immediately launch <code>sleep</code>, why?</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod kprobe_example.ko
sleep 4 &amp; sleep 4 &amp;</pre>
</div>
</div>
<div class="paragraph">
<p>I don&#8217;t think your code can refer to the surrounding kernel code however: the only visible thing is the value of the registers.</p>
</div>
<div class="paragraph">
<p>You can then hack it up to read the stack and read argument values, but do you really want to?</p>
</div>
<div class="paragraph">
<p>There is also a kprobes + ftrace based mechanism with <code>CONFIG_KPROBE_EVENTS=y</code> which does read the memory for us based on format strings that indicate type&#8230;&#8203; <a href="https://github.com/torvalds/linux/blob/v4.16/Documentation/trace/kprobetrace.txt" class="bare">https://github.com/torvalds/linux/blob/v4.16/Documentation/trace/kprobetrace.txt</a> Horrendous. Used by: <a href="https://github.com/brendangregg/perf-tools/blob/98d42a2a1493d2d1c651a5c396e015d4f082eb20/execsnoop" class="bare">https://github.com/brendangregg/perf-tools/blob/98d42a2a1493d2d1c651a5c396e015d4f082eb20/execsnoop</a></p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/torvalds/linux/blob/v4.16/Documentation/kprobes.txt" class="bare">https://github.com/torvalds/linux/blob/v4.16/Documentation/kprobes.txt</a></p>
</li>
<li>
<p><a href="https://github.com/torvalds/linux/blob/v4.17/samples/kprobes/kprobe_example.c" class="bare">https://github.com/torvalds/linux/blob/v4.17/samples/kprobes/kprobe_example.c</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="count-boot-instructions"><a class="anchor" href="#count-boot-instructions"></a><a class="link" href="#count-boot-instructions">15.14.4. Count boot instructions</a></h4>
<div class="paragraph">
<p>TODO: didn&#8217;t port during refactor after 3b0a343647bed577586989fb702b760bd280844a. Reimplementing should not be hard.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://www.quora.com/How-many-instructions-does-a-typical-Linux-kernel-boot-take" class="bare">https://www.quora.com/How-many-instructions-does-a-typical-Linux-kernel-boot-take</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/chat/issues/31" class="bare">https://github.com/cirosantilli/chat/issues/31</a></p>
</li>
<li>
<p><a href="https://rwmj.wordpress.com/2016/03/17/tracing-qemu-guest-execution/" class="bare">https://rwmj.wordpress.com/2016/03/17/tracing-qemu-guest-execution/</a></p>
</li>
<li>
<p><code>qemu/docs/tracing.txt</code> and <code>qemu/docs/replay.txt</code></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/39149446/how-to-use-qemus-simple-trace-backend/46497873#46497873" class="bare">https://stackoverflow.com/questions/39149446/how-to-use-qemus-simple-trace-backend/46497873#46497873</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Results (boot not excluded) are shown at: <a href="#table-boot-instruction-counts">Table 1, &#8220;Boot instruction counts for various setups&#8221;</a></p>
</div>
<table id="table-boot-instruction-counts" class="tableblock frame-all grid-all stretch">
<caption class="title">Table 1. Boot instruction counts for various setups</caption>
<colgroup>
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Commit</th>
<th class="tableblock halign-left valign-top">Arch</th>
<th class="tableblock halign-left valign-top">Simulator</th>
<th class="tableblock halign-left valign-top">Instruction count</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">7228f75ac74c896417fb8c5ba3d375a14ed4d36b</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">arm</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">QEMU</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">680k</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">7228f75ac74c896417fb8c5ba3d375a14ed4d36b</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">arm</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">gem5 AtomicSimpleCPU</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">160M</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">7228f75ac74c896417fb8c5ba3d375a14ed4d36b</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">arm</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">gem5 HPI</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">155M</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">7228f75ac74c896417fb8c5ba3d375a14ed4d36b</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">x86_64</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">QEMU</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">3M</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">7228f75ac74c896417fb8c5ba3d375a14ed4d36b</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">x86_64</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">gem5 AtomicSimpleCPU</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">528M</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>QEMU:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./trace-boot --arch x86_64</pre>
</div>
</div>
<div class="paragraph">
<p>sample output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>instructions 1833863
entry_address 0x1000000
instructions_firmware 20708</pre>
</div>
</div>
<div class="paragraph">
<p>gem5:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --emulator gem5 --eval 'm5 exit'
# Or:
# ./run --arch aarch64 --emulator gem5 --eval 'm5 exit' -- --cpu-type=HPI --caches
./gem5-stat --arch aarch64 sim_insts</pre>
</div>
</div>
<div class="paragraph">
<p>Notes:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>0x1000000</code> is the address where QEMU puts the Linux kernel at with <code>-kernel</code> in x86.</p>
<div class="paragraph">
<p>It can be found from:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain readelf -- -e "$(./getvar vmlinux)" | grep Entry</pre>
</div>
</div>
<div class="paragraph">
<p>TODO confirm further. If I try to break there with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb *0x1000000</pre>
</div>
</div>
<div class="paragraph">
<p>but I have no corresponding source line. Also note that this line is not actually the first line, since the kernel messages such as <code>early console in extract_kernel</code> have already shown on screen at that point. This does not break at all:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb extract_kernel</pre>
</div>
</div>
<div class="paragraph">
<p>It only appears once on every log I&#8217;ve seen so far, checked with <code>grep 0x1000000 trace.txt</code></p>
</div>
<div class="paragraph">
<p>Then when we count the instructions that run before the kernel entry point, there is only about 100k instructions, which is insignificant compared to the kernel boot itself.</p>
</div>
<div class="paragraph">
<p>TODO <code>--arch arm</code> and <code>--arch aarch64</code> does not count firmware instructions properly because the entry point address of the ELF file (<code>ffffff8008080000</code> for <code>aarch64</code>) does not show up on the trace at all. Tested on <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/commit/f8c0502bb2680f2dbe7c1f3d7958f60265347005">f8c0502bb2680f2dbe7c1f3d7958f60265347005</a>.</p>
</div>
</li>
<li>
<p>We can also discount the instructions after <code>init</code> runs by using <code>readelf</code> to get the initial address of <code>init</code>. One easy way to do that now is to just run:</p>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --userland "$(./getvar userland_build_dir)/linux/poweroff.out" main</pre>
</div>
</div>
<div class="paragraph">
<p>And get that from the traces, e.g. if the address is <code>4003a0</code>, then we search:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>grep -n 4003a0 trace.txt</pre>
</div>
</div>
<div class="paragraph">
<p>I have observed a single match for that instruction, so it must be the init, and there were only 20k instructions after it, so the impact is negligible.</p>
</div>
</li>
<li>
<p>to disable networking. Is replacing <code>init</code> enough?</p>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p><a href="https://superuser.com/questions/181254/how-do-you-boot-linux-with-networking-disabled" class="bare">https://superuser.com/questions/181254/how-do-you-boot-linux-with-networking-disabled</a></p>
</li>
<li>
<p><a href="https://superuser.com/questions/684005/how-does-one-permanently-disable-gnu-linux-networking/1255015#1255015" class="bare">https://superuser.com/questions/684005/how-does-one-permanently-disable-gnu-linux-networking/1255015#1255015</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p><code>CONFIG_NET=n</code> did not significantly reduce instruction counts, so maybe replacing <code>init</code> is enough.</p>
</div>
</li>
<li>
<p>gem5 simulates memory latencies. So I think that the CPU loops idle while waiting for memory, and counts will be higher.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="linux-kernel-hardening"><a class="anchor" href="#linux-kernel-hardening"></a><a class="link" href="#linux-kernel-hardening">15.15. Linux kernel hardening</a></h3>
<div class="paragraph">
<p>Make it harder to get hacked and easier to notice that you were, at the cost of some (small?) runtime overhead.</p>
</div>
<div class="sect3">
<h4 id="config_fortify_source"><a class="anchor" href="#config_fortify_source"></a><a class="link" href="#config_fortify_source">15.15.1. CONFIG_FORTIFY_SOURCE</a></h4>
<div class="paragraph">
<p>Detects buffer overflows for us:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux --config 'CONFIG_FORTIFY_SOURCE=y' --linux-build-id fortify
./build-modules --clean
./build-modules
./build-buildroot
./run --eval-after 'insmod strlen_overflow.ko' --linux-build-id fortify</pre>
</div>
</div>
<div class="paragraph">
<p>Possible dmesg output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>strlen_overflow: loading out-of-tree module taints kernel.
detected buffer overflow in strlen
------------[ cut here ]------------</pre>
</div>
</div>
<div class="paragraph">
<p>followed by a trace.</p>
</div>
<div class="paragraph">
<p>You may not get this error because this depends on <code>strlen</code> overflowing at least until the next page: if a random <code>\0</code> appears soon enough, it won&#8217;t blow up as desired.</p>
</div>
<div class="paragraph">
<p>TODO not always reproducible. Find a more reproducible failure. I could not observe it on:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod memcpy_overflow.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/strlen_overflow.c">kernel_modules/strlen_overflow.c</a></p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://www.reddit.com/r/hacking/comments/8h4qxk/what_a_buffer_overflow_in_the_linux_kernel_looks/" class="bare">https://www.reddit.com/r/hacking/comments/8h4qxk/what_a_buffer_overflow_in_the_linux_kernel_looks/</a></p>
</div>
</div>
<div class="sect3">
<h4 id="linux-security-modules"><a class="anchor" href="#linux-security-modules"></a><a class="link" href="#linux-security-modules">15.15.2. Linux security modules</a></h4>
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/Linux_Security_Modules" class="bare">https://en.wikipedia.org/wiki/Linux_Security_Modules</a></p>
</div>
<div class="sect4">
<h5 id="selinux"><a class="anchor" href="#selinux"></a><a class="link" href="#selinux">15.15.2.1. SELinux</a></h5>
<div class="paragraph">
<p>TODO get a hello world permission control working:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-linux \
--config-fragment linux_config/selinux \
--linux-build-id selinux \
;
./build-buildroot --config 'BR2_PACKAGE_REFPOLICY=y'
./run --enable-kvm --linux-build-id selinux</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/linux_config/selinux">linux_config/selinux</a></p>
</div>
<div class="paragraph">
<p>This builds:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>BR2_PACKAGE_REFPOLICY</code>, which includes a reference <code>/etc/selinux/config</code> policy: <a href="https://github.com/SELinuxProject/refpolicy" class="bare">https://github.com/SELinuxProject/refpolicy</a></p>
<div class="paragraph">
<p>refpolicy in turn depends on:</p>
</div>
</li>
<li>
<p><code>BR2_PACKAGE_SETOOLS</code>, which contains tools such as <code>getenforced</code>: <a href="https://github.com/SELinuxProject/setools" class="bare">https://github.com/SELinuxProject/setools</a></p>
<div class="paragraph">
<p>setools depends on:</p>
</div>
</li>
<li>
<p><code>BR2_PACKAGE_LIBSELINUX</code>, which is the backing userland library</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>After boot finishes, we see:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Starting auditd: mkdir: invalid option -- 'Z'</pre>
</div>
</div>
<div class="paragraph">
<p>which comes from <code>/etc/init.d/S01auditd</code>, because BusyBox' <code>mkdir</code> does not have the crazy <code>-Z</code> option like Ubuntu. That&#8217;s amazing!</p>
</div>
<div class="paragraph">
<p>The kernel logs contain:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>SELinux: Initializing.</pre>
</div>
</div>
<div class="paragraph">
<p>Inside the guest we now have:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>getenforce</pre>
</div>
</div>
<div class="paragraph">
<p>which initially says:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Disabled</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: if we try to enforce:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>setenforce 1</pre>
</div>
</div>
<div class="paragraph">
<p>it does not work and outputs:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>setenforce: SELinux is disabled</pre>
</div>
</div>
<div class="paragraph">
<p>SELinux requires glibc as mentioned at: <a href="#libc-choice">Section 20.10, &#8220;libc choice&#8221;</a>.</p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="user-mode-linux"><a class="anchor" href="#user-mode-linux"></a><a class="link" href="#user-mode-linux">15.16. User mode Linux</a></h3>
<div class="paragraph">
<p>I once got <a href="https://en.wikipedia.org/wiki/User-mode_Linux">UML</a> running on a minimal Buildroot setup at: <a href="https://unix.stackexchange.com/questions/73203/how-to-create-rootfs-for-user-mode-linux-on-fedora-18/372207#372207" class="bare">https://unix.stackexchange.com/questions/73203/how-to-create-rootfs-for-user-mode-linux-on-fedora-18/372207#372207</a></p>
</div>
<div class="paragraph">
<p>But in part because it is dying, I didn&#8217;t spend much effort to integrate it into this repo, although it would be a good fit in principle, since it is essentially a virtualization method.</p>
</div>
<div class="paragraph">
<p>Maybe some brave soul will send a pull request one day.</p>
</div>
</div>
<div class="sect2">
<h3 id="uio"><a class="anchor" href="#uio"></a><a class="link" href="#uio">15.17. UIO</a></h3>
<div class="paragraph">
<p>UIO is a kernel subsystem that allows to do certain types of driver operations from userland.</p>
</div>
<div class="paragraph">
<p>This would be awesome to improve debuggability and safety of kernel modules.</p>
</div>
<div class="paragraph">
<p>VFIO looks like a newer and better UIO replacement, but there do not exist any examples of how to use it: <a href="https://stackoverflow.com/questions/49309162/interfacing-with-qemu-edu-device-via-userspace-i-o-uio-linux-driver" class="bare">https://stackoverflow.com/questions/49309162/interfacing-with-qemu-edu-device-via-userspace-i-o-uio-linux-driver</a></p>
</div>
<div class="paragraph">
<p>TODO get something interesting working. I currently don&#8217;t understand the behaviour very well.</p>
</div>
<div class="paragraph">
<p>TODO how to ACK interrupts? How to ensure that every interrupt gets handled separately?</p>
</div>
<div class="paragraph">
<p>TODO how to write to registers. Currently using <code>/dev/mem</code> and <code>lspci</code>.</p>
</div>
<div class="paragraph">
<p>This example should handle interrupts from userland and print a message to stdout:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./uio_read.sh</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: what is the expected behaviour? I should have documented this when I wrote this stuff, and I&#8217;m that lazy right now that I&#8217;m in the middle of a refactor :-)</p>
</div>
<div class="paragraph">
<p>UIO interface in a nutshell:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>blocking read / poll: waits until interrupts</p>
</li>
<li>
<p><code>write</code>: call <code>irqcontrol</code> callback. Default: 0 or 1 to enable / disable interrupts.</p>
</li>
<li>
<p><code>mmap</code>: access device memory</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/kernel_modules/uio_read.c">userland/kernel_modules/uio_read.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/uio_read.sh">rootfs_overlay/lkmc/uio_read.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/15286772/userspace-vs-kernel-space-driver" class="bare">https://stackoverflow.com/questions/15286772/userspace-vs-kernel-space-driver</a></p>
</li>
<li>
<p><a href="https://01.org/linuxgraphics/gfx-docs/drm/driver-api/uio-howto.html" class="bare">https://01.org/linuxgraphics/gfx-docs/drm/driver-api/uio-howto.html</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/7986260/linux-interrupt-handling-in-user-space" class="bare">https://stackoverflow.com/questions/7986260/linux-interrupt-handling-in-user-space</a></p>
</li>
<li>
<p><a href="https://yurovsky.github.io/2014/10/10/linux-uio-gpio-interrupt/" class="bare">https://yurovsky.github.io/2014/10/10/linux-uio-gpio-interrupt/</a></p>
</li>
<li>
<p><a href="https://github.com/bmartini/zynq-axis/blob/65a3a448fda1f0ea4977adfba899eb487201853d/dev/axis.c" class="bare">https://github.com/bmartini/zynq-axis/blob/65a3a448fda1f0ea4977adfba899eb487201853d/dev/axis.c</a></p>
</li>
<li>
<p><a href="https://yurovsky.github.io/2014/10/10/linux-uio-gpio-interrupt/" class="bare">https://yurovsky.github.io/2014/10/10/linux-uio-gpio-interrupt/</a></p>
</li>
<li>
<p><a href="http://nairobi-embedded.org/uio_example.html" class="bare">http://nairobi-embedded.org/uio_example.html</a> that website has QEMU examples for everything as usual. The example has a kernel-side which creates the memory mappings and is used by the user.</p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/49309162/interfacing-with-qemu-edu-device-via-userspace-i-o-uio-linux-driver" class="bare">https://stackoverflow.com/questions/49309162/interfacing-with-qemu-edu-device-via-userspace-i-o-uio-linux-driver</a></p>
</li>
<li>
<p>userland driver stability questions:</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/8030758/getting-kernel-version-from-linux-kernel-module-at-runtime/45430233#45430233" class="bare">https://stackoverflow.com/questions/8030758/getting-kernel-version-from-linux-kernel-module-at-runtime/45430233#45430233</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/37098482/how-to-build-a-linux-kernel-module-so-that-it-is-compatible-with-all-kernel-rele/45429681#45429681" class="bare">https://stackoverflow.com/questions/37098482/how-to-build-a-linux-kernel-module-so-that-it-is-compatible-with-all-kernel-rele/45429681#45429681</a></p>
</li>
<li>
<p><a href="https://liquidat.wordpress.com/2007/07/21/linux-kernel-2623-to-have-stable-userspace-driver-api/" class="bare">https://liquidat.wordpress.com/2007/07/21/linux-kernel-2623-to-have-stable-userspace-driver-api/</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="linux-kernel-interactive-stuff"><a class="anchor" href="#linux-kernel-interactive-stuff"></a><a class="link" href="#linux-kernel-interactive-stuff">15.18. Linux kernel interactive stuff</a></h3>
<div class="sect3">
<h4 id="fbcon"><a class="anchor" href="#fbcon"></a><a class="link" href="#fbcon">15.18.1. Linux kernel console fun</a></h4>
<div class="paragraph">
<p>Requires <a href="#graphics">Graphics</a>.</p>
</div>
<div class="paragraph">
<p>You can also try those on the <code>Ctrl-Alt-F3</code> of your Ubuntu host, but it is much more fun inside a VM!</p>
</div>
<div class="paragraph">
<p>Stop the cursor from blinking:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 0 &gt; /sys/class/graphics/fbcon/cursor_blink</pre>
</div>
</div>
<div class="paragraph">
<p>Rotate the console 90 degrees! <a href="https://askubuntu.com/questions/237963/how-do-i-rotate-my-display-when-not-using-an-x-server" class="bare">https://askubuntu.com/questions/237963/how-do-i-rotate-my-display-when-not-using-an-x-server</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 1 &gt; /sys/class/graphics/fbcon/rotate</pre>
</div>
</div>
<div class="paragraph">
<p>Relies on: <code>CONFIG_FRAMEBUFFER_CONSOLE_ROTATION=y</code>.</p>
</div>
<div class="paragraph">
<p>Documented under: <code>Documentation/fb/</code>.</p>
</div>
<div class="paragraph">
<p>TODO: font and keymap. Mentioned at: <a href="https://cmcenroe.me/2017/05/05/linux-console.html" class="bare">https://cmcenroe.me/2017/05/05/linux-console.html</a> and I think can be done with BusyBox <code>loadkmap</code> and <code>loadfont</code>, we just have to understand their formats, related:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://unix.stackexchange.com/questions/177024/remap-keyboard-on-the-linux-console" class="bare">https://unix.stackexchange.com/questions/177024/remap-keyboard-on-the-linux-console</a></p>
</li>
<li>
<p><a href="https://superuser.com/questions/194202/remapping-keys-system-wide-in-linux-not-just-in-x" class="bare">https://superuser.com/questions/194202/remapping-keys-system-wide-in-linux-not-just-in-x</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="linux-kernel-magic-keys"><a class="anchor" href="#linux-kernel-magic-keys"></a><a class="link" href="#linux-kernel-magic-keys">15.18.2. Linux kernel magic keys</a></h4>
<div class="paragraph">
<p>Requires <a href="#graphics">Graphics</a>.</p>
</div>
<div class="paragraph">
<p>Let&#8217;s have some fun.</p>
</div>
<div class="paragraph">
<p>I think most are implemented under:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>drivers/tty</pre>
</div>
</div>
<div class="paragraph">
<p>TODO find all.</p>
</div>
<div class="paragraph">
<p>Scroll up / down the terminal:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Shift-PgDown
Shift-PgUp</pre>
</div>
</div>
<div class="paragraph">
<p>Or inside <code>./qemu-monitor</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sendkey shift-pgup
sendkey shift-pgdown</pre>
</div>
</div>
<div class="sect4">
<h5 id="ctrl-alt-del"><a class="anchor" href="#ctrl-alt-del"></a><a class="link" href="#ctrl-alt-del">15.18.2.1. Ctrl Alt Del</a></h5>
<div class="paragraph">
<p>If you run in <a href="#qemu-graphic-mode">QEMU graphic mode</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --graphic</pre>
</div>
</div>
<div class="paragraph">
<p>and then from the graphic window you enter the keys:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Ctrl-Alt-Del</pre>
</div>
</div>
<div class="paragraph">
<p>then this runs the following command on the guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/sbin/reboot</pre>
</div>
</div>
<div class="paragraph">
<p>This is enabled from our <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/etc/inittab">rootfs_overlay/etc/inittab</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>::ctrlaltdel:/sbin/reboot</pre>
</div>
</div>
<div class="paragraph">
<p>This leads Linux to try to reboot, and QEMU shutdowns due to the <code>-no-reboot</code> option which we set by default for, see: <a href="#exit-emulator-on-panic">Section 15.7.1.3, &#8220;Exit emulator on panic&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Here is a minimal example of Ctrl Alt Del:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'init=/lkmc/linux/ctrl_alt_del.out' --graphic</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/ctrl_alt_del.c">userland/linux/ctrl_alt_del.c</a></p>
</div>
<div class="paragraph">
<p>When you hit <code>Ctrl-Alt-Del</code> in the guest, our tiny init handles a <code>SIGINT</code> sent by the kernel and outputs to stdout:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cad</pre>
</div>
</div>
<div class="paragraph">
<p>To map between <code>man 2 reboot</code> and the uClibc <code>RB_*</code> magic constants see:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>less "$(./getvar buildroot_build_build_dir)"/uclibc-*/include/sys/reboot.h"</pre>
</div>
</div>
<div class="paragraph">
<p>The procfs mechanism is documented at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>less linux/Documentation/sysctl/kernel.txt</pre>
</div>
</div>
<div class="paragraph">
<p>which says:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>When the value in this file is 0, ctrl-alt-del is trapped and
sent to the init(1) program to handle a graceful restart.
When, however, the value is &gt; 0, Linux's reaction to a Vulcan
Nerve Pinch (tm) will be an immediate reboot, without even
syncing its dirty buffers.
Note: when a program (like dosemu) has the keyboard in 'raw'
mode, the ctrl-alt-del is intercepted by the program before it
ever reaches the kernel tty layer, and it's up to the program
to decide what to do with it.</pre>
</div>
</div>
<div class="paragraph">
<p>Under the hood, behaviour is controlled by the <code>reboot</code> syscall:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>man 2 reboot</pre>
</div>
</div>
<div class="paragraph">
<p><code>reboot</code> system calls can set either of the these behaviours for <code>Ctrl-Alt-Del</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>do a hard shutdown syscall. Set in uClibc C code with:</p>
<div class="literalblock">
<div class="content">
<pre>reboot(RB_ENABLE_CAD)</pre>
</div>
</div>
<div class="paragraph">
<p>or from procfs with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 1 &gt; /proc/sys/kernel/ctrl-alt-del</pre>
</div>
</div>
<div class="paragraph">
<p>Done by BusyBox' <code>reboot -f</code>.</p>
</div>
</li>
<li>
<p>send a SIGINT to the init process. This is what BusyBox' init does, and it then execs the string set in <code>inittab</code>.</p>
<div class="paragraph">
<p>Set in uclibc C code with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>reboot(RB_DISABLE_CAD)</pre>
</div>
</div>
<div class="paragraph">
<p>or from procfs with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 0 &gt; /proc/sys/kernel/ctrl-alt-del</pre>
</div>
</div>
<div class="paragraph">
<p>Done by BusyBox' <code>reboot</code>.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>When a BusyBox init is with the signal, it prints the following lines:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>The system is going down NOW!
Sent SIGTERM to all processes
Sent SIGKILL to all processes
Requesting system reboot</pre>
</div>
</div>
<div class="paragraph">
<p>On busybox-1.29.2&#8217;s init at init/init.c we see how the kill signals are sent:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>static void run_shutdown_and_kill_processes(void)
{
/* Run everything to be run at "shutdown". This is done _prior_
* to killing everything, in case people wish to use scripts to
* shut things down gracefully... */
run_actions(SHUTDOWN);
message(L_CONSOLE | L_LOG, "The system is going down NOW!");
/* Send signals to every process _except_ pid 1 */
kill(-1, SIGTERM);
message(L_CONSOLE, "Sent SIG%s to all processes", "TERM");
sync();
sleep(1);
kill(-1, SIGKILL);
message(L_CONSOLE, "Sent SIG%s to all processes", "KILL");
sync();
/*sleep(1); - callers take care about making a pause */
}</pre>
</div>
</div>
<div class="paragraph">
<p>and <code>run_shutdown_and_kill_processes</code> is called from:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/* The SIGPWR/SIGUSR[12]/SIGTERM handler */
static void halt_reboot_pwoff(int sig) NORETURN;
static void halt_reboot_pwoff(int sig)</pre>
</div>
</div>
<div class="paragraph">
<p>which also prints the final line:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> message(L_CONSOLE, "Requesting system %s", m);</pre>
</div>
</div>
<div class="paragraph">
<p>which is set as the signal handler via TODO.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://superuser.com/questions/193652/does-linux-have-a-ctrlaltdel-equivalent/1324415#1324415" class="bare">https://superuser.com/questions/193652/does-linux-have-a-ctrlaltdel-equivalent/1324415#1324415</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/42573/meaning-and-commands-for-ctrlaltdel/444969#444969" class="bare">https://unix.stackexchange.com/questions/42573/meaning-and-commands-for-ctrlaltdel/444969#444969</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="sysrq"><a class="anchor" href="#sysrq"></a><a class="link" href="#sysrq">15.18.2.2. SysRq</a></h5>
<div class="paragraph">
<p>We cannot test these actual shortcuts on QEMU since the host captures them at a lower level, but from:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor</pre>
</div>
</div>
<div class="paragraph">
<p>we can for example crash the system with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sendkey alt-sysrq-c</pre>
</div>
</div>
<div class="paragraph">
<p>Same but boring because no magic key:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo c &gt; /proc/sysrq-trigger</pre>
</div>
</div>
<div class="paragraph">
<p>Implemented in:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>drivers/tty/sysrq.c</pre>
</div>
</div>
<div class="paragraph">
<p>On your host, on modern systems that don&#8217;t have the <code>SysRq</code> key you can do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Alt-PrtSc-space</pre>
</div>
</div>
<div class="paragraph">
<p>which prints a message to <code>dmesg</code> of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sysrq: SysRq : HELP : loglevel(0-9) reboot(b) crash(c) terminate-all-tasks(e) memory-full-oom-kill(f) kill-all-tasks(i) thaw-filesystems(j) sak(k) show-backtrace-all-active-cpus(l) show-memory-usage(m) nice-all-RT-tasks(n) poweroff(o) show-registers(p) show-all-timers(q) unraw(r) sync(s) show-task-states(t) unmount(u) show-blocked-tasks(w) dump-ftrace-buffer(z)</pre>
</div>
</div>
<div class="paragraph">
<p>Individual SysRq can be enabled or disabled with the bitmask:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/proc/sys/kernel/sysrq</pre>
</div>
</div>
<div class="paragraph">
<p>The bitmask is documented at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>less linux/Documentation/admin-guide/sysrq.rst</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://en.wikipedia.org/wiki/Magic_SysRq_key" class="bare">https://en.wikipedia.org/wiki/Magic_SysRq_key</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="tty"><a class="anchor" href="#tty"></a><a class="link" href="#tty">15.18.3. TTY</a></h4>
<div class="paragraph">
<p>In order to play with TTYs, do this:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>printf '
tty2::respawn:/sbin/getty -n -L -l /lkmc/loginroot.sh tty2 0 vt100
tty3::respawn:-/bin/sh
tty4::respawn:/sbin/getty 0 tty4
tty63::respawn:-/bin/sh
::respawn:/sbin/getty -L ttyS0 0 vt100
::respawn:/sbin/getty -L ttyS1 0 vt100
::respawn:/sbin/getty -L ttyS2 0 vt100
# Leave one serial empty.
#::respawn:/sbin/getty -L ttyS3 0 vt100
' &gt;&gt; rootfs_overlay/etc/inittab
./build-buildroot
./run --graphic -- \
-serial telnet::1235,server,nowait \
-serial vc:800x600 \
-serial telnet::1236,server,nowait \
;</pre>
</div>
</div>
<div class="paragraph">
<p>and on a second shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>telnet localhost 1235</pre>
</div>
</div>
<div class="paragraph">
<p>We don&#8217;t add more TTYs by default because it would spawn more processes, even if we use <code>askfirst</code> instead of <code>respawn</code>.</p>
</div>
<div class="paragraph">
<p>On the GUI, switch TTYs with:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>Alt-Left</code> or <code>Alt-Right:</code> go to previous / next populated <code>/dev/ttyN</code> TTY. Skips over empty TTYs.</p>
</li>
<li>
<p><code>Alt-Fn</code>: go to the nth TTY. If it is not populated, don&#8217;t go there.</p>
</li>
<li>
<p><code>chvt &lt;n&gt;</code>: go to the n-th virtual TTY, even if it is empty: <a href="https://superuser.com/questions/33065/console-commands-to-change-virtual-ttys-in-linux-and-openbsd" class="bare">https://superuser.com/questions/33065/console-commands-to-change-virtual-ttys-in-linux-and-openbsd</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>You can also test this on most hosts such as Ubuntu 18.04, except that when in the GUI, you must use <code>Ctrl-Alt-Fx</code> to switch to another terminal.</p>
</div>
<div class="paragraph">
<p>Next, we also have the following shells running on the serial ports, hit enter to activate them:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>/dev/ttyS0</code>: first shell that was used to run QEMU, corresponds to QEMU&#8217;s <code>-serial mon:stdio</code>.</p>
<div class="paragraph">
<p>It would also work if we used <code>-serial stdio</code>, but:</p>
</div>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p><code>Ctrl-C</code> would kill QEMU instead of going to the guest</p>
</li>
<li>
<p><code>Ctrl-A C</code> wouldn&#8217;t open the QEMU console there</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>see also: <a href="https://stackoverflow.com/questions/49716931/how-to-run-qemu-with-nographic-and-monitor-but-still-be-able-to-send-ctrlc-to" class="bare">https://stackoverflow.com/questions/49716931/how-to-run-qemu-with-nographic-and-monitor-but-still-be-able-to-send-ctrlc-to</a></p>
</div>
</li>
<li>
<p><code>/dev/ttyS1</code>: second shell running <code>telnet</code></p>
</li>
<li>
<p><code>/dev/ttyS2</code>: go on the GUI and enter <code>Ctrl-Alt-2</code>, corresponds to QEMU&#8217;s <code>-serial vc</code>. Go back to the main console with <code>Ctrl-Alt-1</code>.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>although we cannot change between terminals from there.</p>
</div>
<div class="paragraph">
<p>Each populated TTY contains a "shell":</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>-/bin/sh</code>: goes directly into an <code>sh</code> without a login prompt.</p>
<div class="paragraph">
<p>The trailing dash <code>-</code> can be used on any command. It makes the command that follows take over the TTY, which is what we typically want for interactive shells: <a href="https://askubuntu.com/questions/902998/how-to-check-which-tty-am-i-using" class="bare">https://askubuntu.com/questions/902998/how-to-check-which-tty-am-i-using</a></p>
</div>
<div class="paragraph">
<p>The <code>getty</code> executable however also does this operation and therefore dispenses the <code>-</code>.</p>
</div>
</li>
<li>
<p><code>/sbin/getty</code> asks for password, and then gives you an <code>sh</code></p>
<div class="paragraph">
<p>We can overcome the password prompt with the <code>-l /lkmc/loginroot.sh</code> technique explained at: <a href="https://askubuntu.com/questions/902998/how-to-check-which-tty-am-i-using" class="bare">https://askubuntu.com/questions/902998/how-to-check-which-tty-am-i-using</a> but I don&#8217;t see any advantage over <code>-/bin/sh</code> currently.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Identify the current TTY with the command:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>tty</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://unix.stackexchange.com/questions/270272/how-to-get-the-tty-in-which-bash-is-running/270372" class="bare">https://unix.stackexchange.com/questions/270272/how-to-get-the-tty-in-which-bash-is-running/270372</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/187319/how-to-get-the-real-name-of-the-controlling-terminal" class="bare">https://unix.stackexchange.com/questions/187319/how-to-get-the-real-name-of-the-controlling-terminal</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/77796/how-to-get-the-current-terminal-name" class="bare">https://unix.stackexchange.com/questions/77796/how-to-get-the-current-terminal-name</a></p>
</li>
<li>
<p><a href="https://askubuntu.com/questions/902998/how-to-check-which-tty-am-i-using" class="bare">https://askubuntu.com/questions/902998/how-to-check-which-tty-am-i-using</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This outputs:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>/dev/console</code> for the initial GUI terminal. But I think it is the same as <code>/dev/tty1</code>, because if I try to do</p>
<div class="literalblock">
<div class="content">
<pre>tty1::respawn:-/bin/sh</pre>
</div>
</div>
<div class="paragraph">
<p>it makes the terminal go crazy, as if multiple processes are randomly eating up the characters.</p>
</div>
</li>
<li>
<p><code>/dev/ttyN</code> for the other graphic TTYs. Note that there are only 63 available ones, from <code>/dev/tty1</code> to <code>/dev/tty63</code> (<code>/dev/tty0</code> is the current one): <a href="https://superuser.com/questions/449781/why-is-there-so-many-linux-dev-tty" class="bare">https://superuser.com/questions/449781/why-is-there-so-many-linux-dev-tty</a>. I think this is determined by:</p>
<div class="literalblock">
<div class="content">
<pre>#define MAX_NR_CONSOLES 63</pre>
</div>
</div>
<div class="paragraph">
<p>in <code>linux/include/uapi/linux/vt.h</code>.</p>
</div>
</li>
<li>
<p><code>/dev/ttySN</code> for the text shells.</p>
<div class="paragraph">
<p>These are Serial ports, see this to understand what those represent physically: <a href="https://unix.stackexchange.com/questions/307390/what-is-the-difference-between-ttys0-ttyusb0-and-ttyama0-in-linux/367882#367882" class="bare">https://unix.stackexchange.com/questions/307390/what-is-the-difference-between-ttys0-ttyusb0-and-ttyama0-in-linux/367882#367882</a></p>
</div>
<div class="paragraph">
<p>There are only 4 serial ports, I think this is determined by QEMU. TODO check.</p>
</div>
<div class="paragraph">
<p>See also: <a href="https://stackoverflow.com/questions/16706423/two-instances-of-busybox-on-separate-serial-lines-ttysn" class="bare">https://stackoverflow.com/questions/16706423/two-instances-of-busybox-on-separate-serial-lines-ttysn</a></p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Get the TTY in bulk for all processes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./psa.sh</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/psa.sh">rootfs_overlay/lkmc/psa.sh</a>.</p>
</div>
<div class="paragraph">
<p>The TTY appears under the <code>TT</code> section, which is enabled by <code>-o tty</code>. This shows the TTY device number, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>4,1</pre>
</div>
</div>
<div class="paragraph">
<p>and we can then confirm it with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ls -l /dev/tty1</pre>
</div>
</div>
<div class="paragraph">
<p>Next try:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod kthread.ko</pre>
</div>
</div>
<div class="paragraph">
<p>and switch between virtual terminals, to understand that the dmesg goes to whatever current virtual terminal you are on, but not the others, and not to the serial terminals.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://serverfault.com/questions/119736/how-to-enable-multiple-virtual-consoles-on-linux" class="bare">https://serverfault.com/questions/119736/how-to-enable-multiple-virtual-consoles-on-linux</a></p>
</li>
<li>
<p><a href="https://github.com/mirror/busybox/blob/1_28_3/examples/inittab#L60" class="bare">https://github.com/mirror/busybox/blob/1_28_3/examples/inittab#L60</a></p>
</li>
<li>
<p><a href="http://web.archive.org/web/20180117124612/http://nairobi-embedded.org/qemu_serial_port_system_console.html" class="bare">http://web.archive.org/web/20180117124612/http://nairobi-embedded.org/qemu_serial_port_system_console.html</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="start-a-getty-from-outside-of-init"><a class="anchor" href="#start-a-getty-from-outside-of-init"></a><a class="link" href="#start-a-getty-from-outside-of-init">15.18.3.1. Start a getty from outside of init</a></h5>
<div class="paragraph">
<p>TODO: <a href="https://unix.stackexchange.com/questions/196704/getty-start-from-command-line" class="bare">https://unix.stackexchange.com/questions/196704/getty-start-from-command-line</a></p>
</div>
<div class="paragraph">
<p>TODO: how to place an <code>sh</code> directly on a TTY as well without <code>getty</code>?</p>
</div>
<div class="paragraph">
<p>If I try the exact same command that the <code>inittab</code> is doing from a regular shell after boot:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/sbin/getty 0 tty1</pre>
</div>
</div>
<div class="paragraph">
<p>it fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>getty: setsid: Operation not permitted</pre>
</div>
</div>
<div class="paragraph">
<p>The following however works:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval 'getty 0 tty1 &amp; getty 0 tty2 &amp; getty 0 tty3 &amp; sleep 99999999' --graphic</pre>
</div>
</div>
<div class="paragraph">
<p>presumably because it is being called from <code>init</code> directly?</p>
</div>
<div class="paragraph">
<p>Outcome: <code>Alt-Right</code> cycles between three TTYs, <code>tty1</code> being the default one that appears under the boot messages.</p>
</div>
<div class="paragraph">
<p><code>man 2 setsid</code> says that there is only one failure possibility:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>EPERM The process group ID of any process equals the PID of the calling process. Thus, in particular, setsid() fails if the calling process is already a process group leader.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>We can get some visibility into it to try and solve the problem with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./psa.sh</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="console-kernel-boot-parameter"><a class="anchor" href="#console-kernel-boot-parameter"></a><a class="link" href="#console-kernel-boot-parameter">15.18.3.2. console kernel boot parameter</a></h5>
<div class="paragraph">
<p>Take the command described at <a href="#tty">TTY</a> and try adding the following:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>-e 'console=tty7'</code>: boot messages still show on <code>/dev/tty1</code> (TODO how to change that?), but we don&#8217;t get a shell at the end of boot there.</p>
<div class="paragraph">
<p>Instead, the shell appears on <code>/dev/tty7</code>.</p>
</div>
</li>
<li>
<p><code>-e 'console=tty2'</code> like <code>/dev/tty7</code>, but <code>/dev/tty2</code> is broken, because we have two shells there:</p>
<div class="ulist">
<ul>
<li>
<p>one due to the <code>::respawn:-/bin/sh</code> entry which uses whatever <code>console</code> points to</p>
</li>
<li>
<p>another one due to the <code>tty2::respawn:/sbin/getty</code> entry we added</p>
</li>
</ul>
</div>
</li>
<li>
<p><code>-e 'console=ttyS0'</code> much like <code>tty2</code>, but messages show only on serial, and the terminal is broken due to having multiple shells on it</p>
</li>
<li>
<p><code>-e 'console=tty1 console=ttyS0'</code>: boot messages show on both <code>tty1</code> and <code>ttyS0</code>, but only <code>S0</code> gets a shell because it came last</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect3">
<h4 id="config_logo"><a class="anchor" href="#config_logo"></a><a class="link" href="#config_logo">15.18.4. CONFIG_LOGO</a></h4>
<div class="paragraph">
<p>If you run in <a href="#graphics">Graphics</a>, then you get a Penguin image for <a href="#number-of-cores">every core</a> above the console! <a href="https://askubuntu.com/questions/80938/is-it-possible-to-get-the-tux-logo-on-the-text-based-boot" class="bare">https://askubuntu.com/questions/80938/is-it-possible-to-get-the-tux-logo-on-the-text-based-boot</a></p>
</div>
<div class="paragraph">
<p>This is due to the <a href="https://github.com/torvalds/linux/blob/v4.17/drivers/video/logo/Kconfig#L5"><code>CONFIG_LOGO=y</code></a> option which we enable by default.</p>
</div>
<div class="paragraph">
<p><code>reset</code> on the terminal then kills the poor penguins.</p>
</div>
<div class="paragraph">
<p>When <code>CONFIG_LOGO=y</code> is set, the logo can be disabled at boot with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --kernel-cli 'logo.nologo'</pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/39872463/how-can-i-disable-the-startup-penguins-and-boot-text-on-linaro-ubuntu" class="bare">https://stackoverflow.com/questions/39872463/how-can-i-disable-the-startup-penguins-and-boot-text-on-linaro-ubuntu</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/332198/centos-remove-penguin-logo-at-startup" class="bare">https://unix.stackexchange.com/questions/332198/centos-remove-penguin-logo-at-startup</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Looks like a recompile is needed to modify the image&#8230;&#8203;</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://superuser.com/questions/736423/changing-kernel-bootsplash-image" class="bare">https://superuser.com/questions/736423/changing-kernel-bootsplash-image</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/153975/how-to-change-boot-logo-in-linux-mint" class="bare">https://unix.stackexchange.com/questions/153975/how-to-change-boot-logo-in-linux-mint</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="drm"><a class="anchor" href="#drm"></a><a class="link" href="#drm">15.19. DRM</a></h3>
<div class="paragraph">
<p>DRM / DRI is the new interface that supersedes <code>fbdev</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_LIBDRM=y'
./build-userland --package libdrm -- userland/libs/libdrm/modeset.c
./run --eval-after './libs/libdrm/modeset.out' --graphic</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/libs/libdrm/modeset.c">userland/libs/libdrm/modeset.c</a></p>
</div>
<div class="paragraph">
<p>Outcome: for a few seconds, the screen that contains the terminal gets taken over by changing colors of the rainbow.</p>
</div>
<div class="paragraph">
<p>TODO not working for <code>aarch64</code>, it takes over the screen for a few seconds and the kernel messages disappear, but the screen stays black all the time.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_LIBDRM=y'
./build-userland --package libdrm
./build-buildroot
./run --eval-after './libs/libdrm/modeset.out' --graphic</pre>
</div>
</div>
<div class="paragraph">
<p><a href="#kmscube">kmscube</a> however worked, which means that it must be a bug with this demo?</p>
</div>
<div class="paragraph">
<p>We set <code>CONFIG_DRM=y</code> on our default kernel configuration, and it creates one device file for each display:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># ls -l /dev/dri
total 0
crw------- 1 root root 226, 0 May 28 09:41 card0
# grep 226 /proc/devices
226 drm
# ls /sys/module/drm /sys/module/drm_kms_helper/</pre>
</div>
</div>
<div class="paragraph">
<p>Try creating new displays:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --graphic -- -device virtio-gpu-pci</pre>
</div>
</div>
<div class="paragraph">
<p>to see multiple <code>/dev/dri/cardN</code>, and then use a different display with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after './libs/libdrm/modeset.out' --graphic</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://dri.freedesktop.org/wiki/DRM/" class="bare">https://dri.freedesktop.org/wiki/DRM/</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/Direct_Rendering_Infrastructure" class="bare">https://en.wikipedia.org/wiki/Direct_Rendering_Infrastructure</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/Direct_Rendering_Manager" class="bare">https://en.wikipedia.org/wiki/Direct_Rendering_Manager</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/Mode_setting" class="bare">https://en.wikipedia.org/wiki/Mode_setting</a> KMS</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Tested on: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/commit/93e383902ebcc03d8a7ac0d65961c0e62af9612b">93e383902ebcc03d8a7ac0d65961c0e62af9612b</a></p>
</div>
<div class="sect3">
<h4 id="kmscube"><a class="anchor" href="#kmscube"></a><a class="link" href="#kmscube">15.19.1. kmscube</a></h4>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config-fragment buildroot_config/kmscube</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: a colored spinning cube coded in OpenGL + EGL takes over your display and spins forever: <a href="https://www.youtube.com/watch?v=CqgJMgfxjsk" class="bare">https://www.youtube.com/watch?v=CqgJMgfxjsk</a></p>
</div>
<div class="paragraph">
<p>It is a bit amusing to see OpenGL running outside of a window manager window like that: <a href="https://stackoverflow.com/questions/3804065/using-opengl-without-a-window-manager-in-linux/50669152#50669152" class="bare">https://stackoverflow.com/questions/3804065/using-opengl-without-a-window-manager-in-linux/50669152#50669152</a></p>
</div>
<div class="paragraph">
<p>TODO: it is very slow, about 1FPS. I tried Buildroot master ad684c20d146b220dd04a85dbf2533c69ec8ee52 with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>make qemu_x86_64_defconfig
printf "
BR2_CCACHE=y
BR2_PACKAGE_HOST_QEMU=y
BR2_PACKAGE_HOST_QEMU_LINUX_USER_MODE=n
BR2_PACKAGE_HOST_QEMU_SYSTEM_MODE=y
BR2_PACKAGE_HOST_QEMU_VDE2=y
BR2_PACKAGE_KMSCUBE=y
BR2_PACKAGE_MESA3D=y
BR2_PACKAGE_MESA3D_DRI_DRIVER_SWRAST=y
BR2_PACKAGE_MESA3D_OPENGL_EGL=y
BR2_PACKAGE_MESA3D_OPENGL_ES=y
BR2_TOOLCHAIN_BUILDROOT_CXX=y
" &gt;&gt; .config</pre>
</div>
</div>
<div class="paragraph">
<p>and the FPS was much better, I estimate something like 15FPS.</p>
</div>
<div class="paragraph">
<p>On Ubuntu 18.04 with NVIDIA proprietary drivers:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get instll kmscube
kmscube</pre>
</div>
</div>
<div class="paragraph">
<p>fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>drmModeGetResources failed: Invalid argument
failed to initialize legacy DRM</pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="https://github.com/robclark/kmscube/issues/12" class="bare">https://github.com/robclark/kmscube/issues/12</a> and <a href="https://stackoverflow.com/questions/26920835/can-egl-application-run-in-console-mode/26921287#26921287" class="bare">https://stackoverflow.com/questions/26920835/can-egl-application-run-in-console-mode/26921287#26921287</a></p>
</div>
<div class="paragraph">
<p>Tested on: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/commit/2903771275372ccfecc2b025edbb0d04c4016930">2903771275372ccfecc2b025edbb0d04c4016930</a></p>
</div>
</div>
<div class="sect3">
<h4 id="kmscon"><a class="anchor" href="#kmscon"></a><a class="link" href="#kmscon">15.19.2. kmscon</a></h4>
<div class="paragraph">
<p>TODO get working.</p>
</div>
<div class="paragraph">
<p>Implements a console for <a href="#drm">DRM</a>.</p>
</div>
<div class="paragraph">
<p>The Linux kernel has a built-in fbdev console called <a href="#fbcon">Linux kernel console fun</a> but not for <a href="#drm">DRM</a> it seems.</p>
</div>
<div class="paragraph">
<p>The upstream project seems dead with last commit in 2014: <a href="https://www.freedesktop.org/wiki/Software/kmscon/" class="bare">https://www.freedesktop.org/wiki/Software/kmscon/</a></p>
</div>
<div class="paragraph">
<p>Build failed in Ubuntu 18.04 with: <a href="https://github.com/dvdhrm/kmscon/issues/131" class="bare">https://github.com/dvdhrm/kmscon/issues/131</a> but this fork compiled but didn&#8217;t run on host: <a href="https://github.com/Aetf/kmscon/issues/2#issuecomment-392484043" class="bare">https://github.com/Aetf/kmscon/issues/2#issuecomment-392484043</a></p>
</div>
<div class="paragraph">
<p>Haven&#8217;t tested the fork on QEMU too much insanity.</p>
</div>
</div>
<div class="sect3">
<h4 id="libdri2"><a class="anchor" href="#libdri2"></a><a class="link" href="#libdri2">15.19.3. libdri2</a></h4>
<div class="paragraph">
<p>TODO get working.</p>
</div>
<div class="paragraph">
<p>Looks like a more raw alternative to libdrm:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKABE_LIBDRI2=y'
wget \
-O "$(./getvar userland_source_dir)/dri2test.c" \
https://raw.githubusercontent.com/robclark/libdri2/master/test/dri2test.c \
;
./build-userland</pre>
</div>
</div>
<div class="paragraph">
<p>but then I noticed that that example requires multiple files, and I don&#8217;t feel like integrating it into our build.</p>
</div>
<div class="paragraph">
<p>When I build it on Ubuntu 18.04 host, it does not generate any executable, so I&#8217;m confused.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="linux-kernel-testing"><a class="anchor" href="#linux-kernel-testing"></a><a class="link" href="#linux-kernel-testing">15.20. Linux kernel testing</a></h3>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/3177338/how-is-the-linux-kernel-tested" class="bare">https://stackoverflow.com/questions/3177338/how-is-the-linux-kernel-tested</a></p>
</div>
<div class="sect3">
<h4 id="linux-test-project"><a class="anchor" href="#linux-test-project"></a><a class="link" href="#linux-test-project">15.20.1. Linux Test Project</a></h4>
<div class="paragraph">
<p><a href="https://github.com/linux-test-project/ltp" class="bare">https://github.com/linux-test-project/ltp</a></p>
</div>
<div class="paragraph">
<p>Tests a lot of Linux and POSIX userland visible interfaces.</p>
</div>
<div class="paragraph">
<p>Buildroot already has a package, so it is trivial to build it:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_LTP_TESTSUITE=y'</pre>
</div>
</div>
<div class="paragraph">
<p>So now let&#8217;s try and see if the <code>exit</code> system call is working:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/usr/lib/ltp-testsuite/testcases/bin/exit01</pre>
</div>
</div>
<div class="paragraph">
<p>which gives successful output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>exit01 1 TPASS : exit() test PASSED</pre>
</div>
</div>
<div class="paragraph">
<p>and has source code at: <a href="https://github.com/linux-test-project/ltp/blob/20190115/testcases/kernel/syscalls/exit/exit01.c" class="bare">https://github.com/linux-test-project/ltp/blob/20190115/testcases/kernel/syscalls/exit/exit01.c</a></p>
</div>
<div class="paragraph">
<p>Besides testing any kernel modifications you make, LTP can also be used to the system call implementation of <a href="#user-mode-simulation">User mode simulation</a> as shown at <a href="#user-mode-buildroot-executables">User mode Buildroot executables</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --userland "$(./getvar buildroot_target_dir)/usr/lib/ltp-testsuite/testcases/bin/exit01"</pre>
</div>
</div>
<div class="paragraph">
<p>Tested at: 287c83f3f99db8c1ff9bbc85a79576da6a78e986 + 1.</p>
</div>
</div>
<div class="sect3">
<h4 id="stress"><a class="anchor" href="#stress"></a><a class="link" href="#stress">15.20.2. stress</a></h4>
<div class="paragraph">
<p><a href="#posix">POSIX</a> userland stress. Two versions:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot \
--config 'BR2_PACKAGE_STRESS=y' \
--config 'BR2_PACKAGE_STRESS_NG=y' \
;</pre>
</div>
</div>
<div class="paragraph">
<p><code>STRESS_NG</code> is likely the best, but it requires glibc, see: <a href="#libc-choice">Section 20.10, &#8220;libc choice&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Websites:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://people.seas.harvard.edu/~apw/stress/" class="bare">https://people.seas.harvard.edu/~apw/stress/</a></p>
</li>
<li>
<p><a href="https://github.com/ColinIanKing/stress-ng" class="bare">https://github.com/ColinIanKing/stress-ng</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><code>stress</code> usage:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>stress --help
stress -c 16 &amp;
ps</pre>
</div>
</div>
<div class="paragraph">
<p>and notice how 16 threads were created in addition to a parent worker thread.</p>
</div>
<div class="paragraph">
<p>It just runs forever, so kill it when you get tired:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>kill %1</pre>
</div>
</div>
<div class="paragraph">
<p><code>stress -c 1 -t 1</code> makes gem5 irresponsive for a very long time.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="linux-kernel-build-system"><a class="anchor" href="#linux-kernel-build-system"></a><a class="link" href="#linux-kernel-build-system">15.21. Linux kernel build system</a></h3>
<div class="sect3">
<h4 id="vmlinux-vs-bzimage-vs-zimage-vs-image"><a class="anchor" href="#vmlinux-vs-bzimage-vs-zimage-vs-image"></a><a class="link" href="#vmlinux-vs-bzimage-vs-zimage-vs-image">15.21.1. vmlinux vs bzImage vs zImage vs Image</a></h4>
<div class="paragraph">
<p>Between all archs on QEMU and gem5 we touch all of those kernel built output files.</p>
</div>
<div class="paragraph">
<p>We are trying to maintain a description of each at: <a href="https://unix.stackexchange.com/questions/5518/what-is-the-difference-between-the-following-kernel-makefile-terms-vmlinux-vml/482978#482978" class="bare">https://unix.stackexchange.com/questions/5518/what-is-the-difference-between-the-following-kernel-makefile-terms-vmlinux-vml/482978#482978</a></p>
</div>
<div class="paragraph">
<p>QEMU does not seem able to boot ELF files like <code>vmlinux</code>: <a href="https://superuser.com/questions/1376944/can-qemu-boot-linux-from-vmlinux-instead-of-bzimage" class="bare">https://superuser.com/questions/1376944/can-qemu-boot-linux-from-vmlinux-instead-of-bzimage</a></p>
</div>
<div class="paragraph">
<p>Converting <code>arch/*</code> images to <code>vmlinux</code> is possible in theory x86 with <a href="https://github.com/torvalds/linux/blob/v5.1/scripts/extract-vmlinux"><code>extract-vmlinux</code></a> but we didn&#8217;t get any gem5 boots working from images generated like that for some reason, see: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/issues/79" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/issues/79</a></p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="xen"><a class="anchor" href="#xen"></a><a class="link" href="#xen">16. Xen</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>TODO: get prototype working and then properly integrate:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-xen</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-xen">build-xen</a></p>
</div>
<div class="paragraph">
<p>This script attempts to build Xen for aarch64 and feed it into QEMU through <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/submodules/boot-wrapper-aarch64">submodules/boot-wrapper-aarch64</a></p>
</div>
<div class="paragraph">
<p>TODO: other archs not yet attempted.</p>
</div>
<div class="paragraph">
<p>The current bad behaviour is that it prints just:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Boot-wrapper v0.2</pre>
</div>
</div>
<div class="paragraph">
<p>and nothing else.</p>
</div>
<div class="paragraph">
<p>We will also need <code>CONFIG_XEN=y</code> on the Linux kernel, but first Xen should print some Xen messages before the kernel is ever reached.</p>
</div>
<div class="paragraph">
<p>If we pass to QEMU the xen image directly instead of the boot wrapper one:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>-kernel ../xen/xen/xen</pre>
</div>
</div>
<div class="paragraph">
<p>then Xen messages do show up! So it seems that the configuration failure lies in the boot wrapper itself rather than Xen.</p>
</div>
<div class="paragraph">
<p>Maybe it is also possible to run Xen directly like this: QEMU can already load multiple images at different memory locations with the generic loader: <a href="https://github.com/qemu/qemu/blob/master/docs/generic-loader.txt" class="bare">https://github.com/qemu/qemu/blob/master/docs/generic-loader.txt</a> which looks something along:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>-kernel file1.elf -device loader,file=file2.elf</pre>
</div>
</div>
<div class="paragraph">
<p>so as long as we craft the correct DTB and feed it into Xen so that it can see the kernel, it should work. TODO does QEMU support patching the auto-generated DTB with pre-generated options? In the worst case we can just dump it hand hack it up though with <code>-machine dumpdtb</code>, see: <a href="#device-tree-emulator-generation">Section 8.4, &#8220;Device tree emulator generation&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>this attempt was based on: <a href="https://wiki.xenproject.org/wiki/Xen_ARM_with_Virtualization_Extensions/FastModels" class="bare">https://wiki.xenproject.org/wiki/Xen_ARM_with_Virtualization_Extensions/FastModels</a> which is the documentation for the ARM Fast Models closed source simulators.</p>
</li>
<li>
<p><a href="https://wiki.xenproject.org/wiki/Xen_ARM_with_Virtualization_Extensions/qemu-system-aarch64" class="bare">https://wiki.xenproject.org/wiki/Xen_ARM_with_Virtualization_Extensions/qemu-system-aarch64</a> this is the only QEMU aarch64 Xen page on the web. It uses the Ubuntu aarc64 image, which has EDK2.</p>
<div class="paragraph">
<p>I however see no joy on blobs. Buildroot does not seem to support EDK 2.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Link on readme <a href="https://stackoverflow.com/questions/49348453/xen-on-qemu-with-arm64-architecture" class="bare">https://stackoverflow.com/questions/49348453/xen-on-qemu-with-arm64-architecture</a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="u-boot"><a class="anchor" href="#u-boot"></a><a class="link" href="#u-boot">17. U-Boot</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/Das_U-Boot" class="bare">https://en.wikipedia.org/wiki/Das_U-Boot</a></p>
</div>
<div class="paragraph">
<p>U-Boot is a popular bootloader.</p>
</div>
<div class="paragraph">
<p>It can read disk filesystems, and Buildroot supports it, so we could in theory put it into memory, and let it find a kernel image from the root filesystem and boot that, but I didn&#8217;t manage to get it working yet: <a href="https://stackoverflow.com/questions/58028789/how-to-boot-linux-aarch64-with-u-boot-with-buildroot-on-qemu" class="bare">https://stackoverflow.com/questions/58028789/how-to-boot-linux-aarch64-with-u-boot-with-buildroot-on-qemu</a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="qemu"><a class="anchor" href="#qemu"></a><a class="link" href="#qemu">18. QEMU</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="introduction-to-qemu"><a class="anchor" href="#introduction-to-qemu"></a><a class="link" href="#introduction-to-qemu">18.1. Introduction to QEMU</a></h3>
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/QEMU">QEMU</a> is a system simulator: it simulates a CPU and devices such as interrupt handlers, timers, UART, screen, keyboard, etc.</p>
</div>
<div class="paragraph">
<p>If you are familiar with <a href="https://en.wikipedia.org/wiki/VirtualBox">VirtualBox</a>, then QEMU then basically does the same thing: it opens a "window" inside your desktop that can run an operating system inside your operating system.</p>
</div>
<div class="paragraph">
<p>Also both can use very similar techniques: either <a href="https://en.wikipedia.org/wiki/Binary_translation">binary translation</a> or <a href="#kvm">KVM</a>. VirtualBox' binary translator is / was based on QEMU&#8217;s it seems: <a href="https://en.wikipedia.org/wiki/VirtualBox#Software-based_virtualization" class="bare">https://en.wikipedia.org/wiki/VirtualBox#Software-based_virtualization</a></p>
</div>
<div class="paragraph">
<p>The huge advantage of QEMU over VirtualBox is that is supports cross arch simulation, e.g. simulate an ARM guest on an x86 host.</p>
</div>
<div class="paragraph">
<p>QEMU is likely the leading cross arch system simulator as of 2018. It is even the default <a href="#android">Android</a> simulator that developers get with Android Studio 3 to develop apps without real hardware.</p>
</div>
<div class="paragraph">
<p>Another advantage of QEMU over virtual box is that it doesn&#8217;t have Oracle' hands all all over it, more like RedHat + ARM.</p>
</div>
<div class="paragraph">
<p>Another advantage of QEMU is that is has no nice configuration GUI. Because who needs GUIs when you have 50 million semi-documented CLI options? Android Studio adds a custom GUI configuration tool on top of it.</p>
</div>
<div class="paragraph">
<p>QEMU is also supported by Buildroot in-tree, see e.g.: <a href="https://github.com/buildroot/buildroot/blob/2018.05/configs/qemu_aarch64_virt_defconfig" class="bare">https://github.com/buildroot/buildroot/blob/2018.05/configs/qemu_aarch64_virt_defconfig</a> We however just build our own manually with <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-qemu">build-qemu</a>, as it gives more flexibility, and building QEMU is very easy!</p>
</div>
<div class="paragraph">
<p>All of this makes QEMU the natural choice of reference system simulator for this repo.</p>
</div>
</div>
<div class="sect2">
<h3 id="disk-persistency"><a class="anchor" href="#disk-persistency"></a><a class="link" href="#disk-persistency">18.2. Disk persistency</a></h3>
<div class="paragraph">
<p>We disable disk persistency for both QEMU and gem5 by default, to prevent the emulator from putting the image in an unknown state.</p>
</div>
<div class="paragraph">
<p>For QEMU, this is done by passing the <code>snapshot</code> option to <code>-drive</code>, and for gem5 it is the default behaviour.</p>
</div>
<div class="paragraph">
<p>If you hack up our <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/run">run</a> script to remove that option, then:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after 'date &gt;f;poweroff'</pre>
</div>
</div>
<div class="paragraph">
<p>followed by:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after 'cat f'</pre>
</div>
</div>
<div class="paragraph">
<p>gives the date, because <code>poweroff</code> without <code>-n</code> syncs before shutdown.</p>
</div>
<div class="paragraph">
<p>The <code>sync</code> command also saves the disk:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sync</pre>
</div>
</div>
<div class="paragraph">
<p>When you do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot</pre>
</div>
</div>
<div class="paragraph">
<p>the disk image gets overwritten by a fresh filesystem and you lose all changes.</p>
</div>
<div class="paragraph">
<p>Remember that if you forcibly turn QEMU off without <code>sync</code> or <code>poweroff</code> from inside the VM, e.g. by closing the QEMU window, disk changes may not be saved.</p>
</div>
<div class="paragraph">
<p>Persistency is also turned off when booting from <a href="#initrd">initrd</a> with a CPIO instead of with a disk.</p>
</div>
<div class="paragraph">
<p>Disk persistency is useful to re-run shell commands from the history of a previous session with <code>Ctrl-R</code>, but we felt that the loss of determinism was not worth it.</p>
</div>
<div class="sect3">
<h4 id="gem5-disk-persistency"><a class="anchor" href="#gem5-disk-persistency"></a><a class="link" href="#gem5-disk-persistency">18.2.1. gem5 disk persistency</a></h4>
<div class="paragraph">
<p>TODO how to make gem5 disk writes persistent?</p>
</div>
<div class="paragraph">
<p>As of cadb92f2df916dbb47f428fd1ec4932a2e1f0f48 there are some <code>read_only</code> entries in the <a href="#gem5-config-ini">gem5 config.ini</a> under cow sections, but hacking them to true did not work:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>diff --git a/configs/common/FSConfig.py b/configs/common/FSConfig.py
index 17498c42b..76b8b351d 100644
--- a/configs/common/FSConfig.py
+++ b/configs/common/FSConfig.py
@@ -60,7 +60,7 @@ os_types = { 'alpha' : [ 'linux' ],
}
class CowIdeDisk(IdeDisk):
- image = CowDiskImage(child=RawDiskImage(read_only=True),
+ image = CowDiskImage(child=RawDiskImage(read_only=False),
read_only=False)
def childImage(self, ci):</pre>
</div>
</div>
<div class="paragraph">
<p>The directory of interest is <code>src/dev/storage</code>.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-qcow2"><a class="anchor" href="#gem5-qcow2"></a><a class="link" href="#gem5-qcow2">18.3. gem5 qcow2</a></h3>
<div class="paragraph">
<p>qcow2 does not appear supported, there are not hits in the source tree, and there is a mention on Nate&#8217;s 2009 wishlist: <a href="http://gem5.org/Nate%27s_Wish_List" class="bare">http://gem5.org/Nate%27s_Wish_List</a></p>
</div>
<div class="paragraph">
<p>This would be good to allow storing smaller sparse ext2 images locally on disk.</p>
</div>
</div>
<div class="sect2">
<h3 id="snapshot"><a class="anchor" href="#snapshot"></a><a class="link" href="#snapshot">18.4. Snapshot</a></h3>
<div class="paragraph">
<p>QEMU allows us to take snapshots at any time through the monitor.</p>
</div>
<div class="paragraph">
<p>You can then restore CPU, memory and disk state back at any time.</p>
</div>
<div class="paragraph">
<p>qcow2 filesystems must be used for that to work.</p>
</div>
<div class="paragraph">
<p>To test it out, login into the VM with and run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after 'umount /mnt/9p/*;./count.sh'</pre>
</div>
</div>
<div class="paragraph">
<p>On another shell, take a snapshot:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor savevm my_snap_id</pre>
</div>
</div>
<div class="paragraph">
<p>The counting continues.</p>
</div>
<div class="paragraph">
<p>Restore the snapshot:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor loadvm my_snap_id</pre>
</div>
</div>
<div class="paragraph">
<p>and the counting goes back to where we saved. This shows that CPU and memory states were reverted.</p>
</div>
<div class="paragraph">
<p>The <code>umount</code> is needed because snapshotting conflicts with <a href="#9p">9P</a>, which we felt is a more valuable default. If you forget to unmount, the following error appears on the QEMU monitor:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Migration is disabled when VirtFS export path '/linux-kernel-module-cheat/out/x86_64/buildroot/build' is mounted in the guest using mount_tag 'host_out'</pre>
</div>
</div>
<div class="paragraph">
<p>We can also verify that the disk state is also reversed. Guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 0 &gt;f</pre>
</div>
</div>
<div class="paragraph">
<p>Monitor:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor savevm my_snap_id</pre>
</div>
</div>
<div class="paragraph">
<p>Guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 1 &gt;f</pre>
</div>
</div>
<div class="paragraph">
<p>Monitor:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor loadvm my_snap_id</pre>
</div>
</div>
<div class="paragraph">
<p>Guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat f</pre>
</div>
</div>
<div class="paragraph">
<p>And the output is <code>0</code>.</p>
</div>
<div class="paragraph">
<p>Our setup does not allow for snapshotting while using <a href="#initrd">initrd</a>.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/40227651/does-qemu-emulator-have-checkpoint-function/48724371#48724371" class="bare">https://stackoverflow.com/questions/40227651/does-qemu-emulator-have-checkpoint-function/48724371#48724371</a></p>
</div>
<div class="sect3">
<h4 id="snapshot-internals"><a class="anchor" href="#snapshot-internals"></a><a class="link" href="#snapshot-internals">18.4.1. Snapshot internals</a></h4>
<div class="paragraph">
<p>Snapshots are stored inside the <code>.qcow2</code> images themselves.</p>
</div>
<div class="paragraph">
<p>They can be observed with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>"$(./getvar buildroot_host_dir)/bin/qemu-img" info "$(./getvar qcow2_file)"</pre>
</div>
</div>
<div class="paragraph">
<p>which after <code>savevm my_snap_id</code> and <code>savevm asdf</code> contains an output of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>image: out/x86_64/buildroot/images/rootfs.ext2.qcow2
file format: qcow2
virtual size: 512M (536870912 bytes)
disk size: 180M
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 my_snap_id 47M 2018-04-27 21:17:50 00:00:15.251
2 asdf 47M 2018-04-27 21:20:39 00:00:18.583
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false</pre>
</div>
</div>
<div class="paragraph">
<p>As a consequence:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>it is possible to restore snapshots across boots, since they stay on the same image the entire time</p>
</li>
<li>
<p>it is not possible to use snapshots with <a href="#initrd">initrd</a> in our setup, since we don&#8217;t pass <code>-drive</code> at all when initrd is enabled</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="device-models"><a class="anchor" href="#device-models"></a><a class="link" href="#device-models">18.5. Device models</a></h3>
<div class="paragraph">
<p>This section documents:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>how to interact with peripheral hardware device models through device drivers</p>
</li>
<li>
<p>how to write your own hardware device models for our emulators, see also: <a href="https://stackoverflow.com/questions/28315265/how-to-add-a-new-device-in-qemu-source-code" class="bare">https://stackoverflow.com/questions/28315265/how-to-add-a-new-device-in-qemu-source-code</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>For the more complex interfaces, we focus on simplified educational devices, either:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>present in the QEMU upstream:</p>
<div class="ulist">
<ul>
<li>
<p><a href="#qemu-edu">QEMU edu PCI device</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>added in <a href="https://github.com/cirosantilli/qemu">our fork of QEMU</a>:</p>
<div class="ulist">
<ul>
<li>
<p><a href="#pci_min">pci_min</a></p>
</li>
<li>
<p><a href="#platform_device">platform_device</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="pci"><a class="anchor" href="#pci"></a><a class="link" href="#pci">18.5.1. PCI</a></h4>
<div class="paragraph">
<p>Only tested in x86.</p>
</div>
<div class="sect4">
<h5 id="pci_min"><a class="anchor" href="#pci_min"></a><a class="link" href="#pci_min">18.5.1.1. pci_min</a></h5>
<div class="paragraph">
<p>PCI driver for our minimal <code>pci_min.c</code> QEMU fork device:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run -- -device lkmc_pci_min</pre>
</div>
</div>
<div class="paragraph">
<p>then:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod pci_min.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Kernel module: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/pci_min.c">kernel_modules/pci_min.c</a>.</p>
</li>
<li>
<p>QEMU device: <a href="https://github.com/cirosantilli/qemu/blob/lkmc/hw/misc/lkmc_pci_min.c" class="bare">https://github.com/cirosantilli/qemu/blob/lkmc/hw/misc/lkmc_pci_min.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Outcome:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;4&gt;[ 10.608241] pci_min: loading out-of-tree module taints kernel.
&lt;6&gt;[ 10.609935] probe
&lt;6&gt;[ 10.651881] dev-&gt;irq = 11
lkmc_pci_min mmio_write addr = 0 val = 12345678 size = 4
&lt;6&gt;[ 10.668515] irq_handler irq = 11 dev = 251
lkmc_pci_min mmio_write addr = 4 val = 0 size = 4</pre>
</div>
</div>
<div class="paragraph">
<p>What happened:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>right at probe time, we write to a register</p>
</li>
<li>
<p>our hardware model is coded such that it generates an interrupt when written to</p>
</li>
<li>
<p>the Linux kernel interrupt handler write to another register, which tells the hardware to stop sending interrupts</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Kernel messages and printks from inside QEMU are shown all together, to see that more clearly, run in <a href="#qemu-graphic-mode">QEMU graphic mode</a> instead.</p>
</div>
<div class="paragraph">
<p>We don&#8217;t enable the device by default because it does not work for vanilla QEMU, which we often want to test with this repository.</p>
</div>
<div class="paragraph">
<p>Probe already does a MMIO write, which generates an IRQ and tests everything.</p>
</div>
</div>
<div class="sect4">
<h5 id="qemu-edu"><a class="anchor" href="#qemu-edu"></a><a class="link" href="#qemu-edu">18.5.1.2. QEMU edu PCI device</a></h5>
<div class="paragraph">
<p>Small upstream educational PCI device:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu_edu.sh</pre>
</div>
</div>
<div class="paragraph">
<p>This tests a lot of features of the edu device, to understand the results, compare the inputs with the documentation of the hardware: <a href="https://github.com/qemu/qemu/blob/v2.12.0/docs/specs/edu.txt" class="bare">https://github.com/qemu/qemu/blob/v2.12.0/docs/specs/edu.txt</a></p>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>kernel module: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/qemu_edu.c">kernel_modules/qemu_edu.c</a></p>
</li>
<li>
<p>QEMU device: <a href="https://github.com/qemu/qemu/blob/v2.12.0/hw/misc/edu.c" class="bare">https://github.com/qemu/qemu/blob/v2.12.0/hw/misc/edu.c</a></p>
</li>
<li>
<p>test script: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/qemu_edu.sh">rootfs_overlay/lkmc/qemu_edu.sh</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Works because we add to our default QEMU CLI:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>-device edu</pre>
</div>
</div>
<div class="paragraph">
<p>This example uses:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the QEMU <code>edu</code> educational device, which is a minimal educational in-tree PCI example</p>
</li>
<li>
<p>the <code>pci.ko</code> kernel module, which exercises the <code>edu</code> hardware.</p>
<div class="paragraph">
<p>I&#8217;ve contacted the awesome original author author of <code>edu</code> <a href="https://github.com/jirislaby">Jiri Slaby</a>, and he told there is no official kernel module example because this was created for a kernel module university course that he gives, and he didn&#8217;t want to give away answers. <a href="https://github.com/cirosantilli/how-to-teach-efficiently">I don&#8217;t agree with that philosophy</a>, so students, cheat away with this repo and go make startups instead.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>TODO exercise DMA on the kernel module. The <code>edu</code> hardware model has that feature:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/32592734/are-there-any-dma-driver-example-pcie-and-fpga/44716747#44716747" class="bare">https://stackoverflow.com/questions/32592734/are-there-any-dma-driver-example-pcie-and-fpga/44716747#44716747</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/17913679/how-to-instantiate-and-use-a-dma-driver-linux-module" class="bare">https://stackoverflow.com/questions/17913679/how-to-instantiate-and-use-a-dma-driver-linux-module</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="manipulate-pci-registers-directly"><a class="anchor" href="#manipulate-pci-registers-directly"></a><a class="link" href="#manipulate-pci-registers-directly">18.5.1.3. Manipulate PCI registers directly</a></h5>
<div class="paragraph">
<p>In this section we will try to interact with PCI devices directly from userland without kernel modules.</p>
</div>
<div class="paragraph">
<p>First identify the PCI device with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lspci</pre>
</div>
</div>
<div class="paragraph">
<p>In our case for example, we see:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>00:06.0 Unclassified device [00ff]: Device 1234:11e8 (rev 10)
00:07.0 Unclassified device [00ff]: Device 1234:11e9</pre>
</div>
</div>
<div class="paragraph">
<p>which we identify as being <code>edu</code> and <code>pci_min</code> respectively by the magic numbers: <code>1234:11e?</code></p>
</div>
<div class="paragraph">
<p>Alternatively, we can also do use the QEMU monitor:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor info qtree</pre>
</div>
</div>
<div class="paragraph">
<p>which gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> dev: lkmc_pci_min, id ""
addr = 07.0
romfile = ""
rombar = 1 (0x1)
multifunction = false
command_serr_enable = true
x-pcie-lnksta-dllla = true
x-pcie-extcap-init = true
class Class 00ff, addr 00:07.0, pci id 1234:11e9 (sub 1af4:1100)
bar 0: mem at 0xfeb54000 [0xfeb54007]
dev: edu, id ""
addr = 06.0
romfile = ""
rombar = 1 (0x1)
multifunction = false
command_serr_enable = true
x-pcie-lnksta-dllla = true
x-pcie-extcap-init = true
class Class 00ff, addr 00:06.0, pci id 1234:11e8 (sub 1af4:1100)
bar 0: mem at 0xfea00000 [0xfeafffff]</pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="https://serverfault.com/questions/587189/list-all-devices-emulated-for-a-vm/913622#913622" class="bare">https://serverfault.com/questions/587189/list-all-devices-emulated-for-a-vm/913622#913622</a></p>
</div>
<div class="paragraph">
<p>Read the configuration registers as binary:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>hexdump /sys/bus/pci/devices/0000:00:06.0/config</pre>
</div>
</div>
<div class="paragraph">
<p>Get nice human readable names and offsets of the registers and some enums:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>setpci --dumpregs</pre>
</div>
</div>
<div class="paragraph">
<p>Get the values of a given config register from its human readable name, either with either bus or device id:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>setpci -s 0000:00:06.0 BASE_ADDRESS_0
setpci -d 1234:11e9 BASE_ADDRESS_0</pre>
</div>
</div>
<div class="paragraph">
<p>Note however that <code>BASE_ADDRESS_0</code> also appears when you do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lspci -v</pre>
</div>
</div>
<div class="paragraph">
<p>as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Memory at feb54000</pre>
</div>
</div>
<div class="paragraph">
<p>Then you can try messing with that address with <a href="#dev-mem">/dev/mem</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>devmem 0xfeb54000 w 0x12345678</pre>
</div>
</div>
<div class="paragraph">
<p>which writes to the first register of our <a href="#pci_min">pci_min</a> device.</p>
</div>
<div class="paragraph">
<p>The device then fires an interrupt at irq 11, which is unhandled, which leads the kernel to say you are a bad boy:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lkmc_pci_min mmio_write addr = 0 val = 12345678 size = 4
&lt;5&gt;[ 1064.042435] random: crng init done
&lt;3&gt;[ 1065.567742] irq 11: nobody cared (try booting with the "irqpoll" option)</pre>
</div>
</div>
<div class="paragraph">
<p>followed by a trace.</p>
</div>
<div class="paragraph">
<p>Next, also try using our <a href="#irq-ko">irq.ko</a> IRQ monitoring module before triggering the interrupt:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod irq.ko
devmem 0xfeb54000 w 0x12345678</pre>
</div>
</div>
<div class="paragraph">
<p>Our kernel module handles the interrupt, but does not acknowledge it like our proper <a href="#pci_min">pci_min</a> kernel module, and so it keeps firing, which leads to infinitely many messages being printed:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>handler irq = 11 dev = 251</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="pciutils"><a class="anchor" href="#pciutils"></a><a class="link" href="#pciutils">18.5.1.4. pciutils</a></h5>
<div class="paragraph">
<p>There are two versions of <code>setpci</code> and <code>lspci</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>a simple one from BusyBox</p>
</li>
<li>
<p>a more complete one from <a href="https://github.com/pciutils/pciutils">pciutils</a> which Buildroot has a package for, and is the default on Ubuntu 18.04 host. This is the one we enable by default.</p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="introduction-to-pci"><a class="anchor" href="#introduction-to-pci"></a><a class="link" href="#introduction-to-pci">18.5.1.5. Introduction to PCI</a></h5>
<div class="paragraph">
<p>The PCI standard is non-free, obviously like everything in low level: <a href="https://pcisig.com/specifications" class="bare">https://pcisig.com/specifications</a> but Google gives several illegal PDF hits :-)</p>
</div>
<div class="paragraph">
<p>And of course, the best documentation available is: <a href="http://wiki.osdev.org/PCI" class="bare">http://wiki.osdev.org/PCI</a></p>
</div>
<div class="paragraph">
<p>Like every other hardware, we could interact with PCI on x86 using only IO instructions and memory operations.</p>
</div>
<div class="paragraph">
<p>But PCI is a complex communication protocol that the Linux kernel implements beautifully for us, so let&#8217;s use the kernel API.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>edu device source and spec in QEMU tree:</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/qemu/qemu/blob/v2.7.0/hw/misc/edu.c" class="bare">https://github.com/qemu/qemu/blob/v2.7.0/hw/misc/edu.c</a></p>
</li>
<li>
<p><a href="https://github.com/qemu/qemu/blob/v2.7.0/docs/specs/edu.txt" class="bare">https://github.com/qemu/qemu/blob/v2.7.0/docs/specs/edu.txt</a></p>
</li>
</ul>
</div>
</li>
<li>
<p><a href="http://www.zarb.org/~trem/kernel/pci/pci-driver.c" class="bare">http://www.zarb.org/~trem/kernel/pci/pci-driver.c</a> inb outb runnable example (no device)</p>
</li>
<li>
<p>LDD3 PCI chapter</p>
</li>
<li>
<p>another QEMU device + module, but using a custom QEMU device:</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/levex/kernel-qemu-pci/blob/31fc9355161b87cea8946b49857447ddd34c7aa6/module/levpci.c" class="bare">https://github.com/levex/kernel-qemu-pci/blob/31fc9355161b87cea8946b49857447ddd34c7aa6/module/levpci.c</a></p>
</li>
<li>
<p><a href="https://github.com/levex/kernel-qemu-pci/blob/31fc9355161b87cea8946b49857447ddd34c7aa6/qemu/hw/char/lev-pci.c" class="bare">https://github.com/levex/kernel-qemu-pci/blob/31fc9355161b87cea8946b49857447ddd34c7aa6/qemu/hw/char/lev-pci.c</a></p>
</li>
</ul>
</div>
</li>
<li>
<p><a href="https://is.muni.cz/el/1433/podzim2016/PB173/um/65218991/" class="bare">https://is.muni.cz/el/1433/podzim2016/PB173/um/65218991/</a> course given by the creator of the edu device. In Czech, and only describes API</p>
</li>
<li>
<p><a href="http://nairobi-embedded.org/linux_pci_device_driver.html" class="bare">http://nairobi-embedded.org/linux_pci_device_driver.html</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="pci-bfd"><a class="anchor" href="#pci-bfd"></a><a class="link" href="#pci-bfd">18.5.1.6. PCI BFD</a></h5>
<div class="paragraph">
<p><code>lspci -k</code> shows something like:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>00:04.0 Class 00ff: 1234:11e8 lkmc_pci</pre>
</div>
</div>
<div class="paragraph">
<p>Meaning of the first numbers:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;8:bus&gt;:&lt;5:device&gt;.&lt;3:function&gt;</pre>
</div>
</div>
<div class="paragraph">
<p>Often abbreviated to BDF.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>bus: groups PCI slots</p>
</li>
<li>
<p>device: maps to one slot</p>
</li>
<li>
<p>function: <a href="https://stackoverflow.com/questions/19223394/what-is-the-function-number-in-pci/44735372#44735372" class="bare">https://stackoverflow.com/questions/19223394/what-is-the-function-number-in-pci/44735372#44735372</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Sometimes a fourth number is also added, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0000:00:04.0</pre>
</div>
</div>
<div class="paragraph">
<p>TODO is that the domain?</p>
</div>
<div class="paragraph">
<p>Class: pure magic: <a href="https://www-s.acm.illinois.edu/sigops/2007/roll_your_own/7.c.1.html" class="bare">https://www-s.acm.illinois.edu/sigops/2007/roll_your_own/7.c.1.html</a> TODO: does it have any side effects? Set in the edu device at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>k-&gt;class_id = PCI_CLASS_OTHERS</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="pci-bar"><a class="anchor" href="#pci-bar"></a><a class="link" href="#pci-bar">18.5.1.7. PCI BAR</a></h5>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/30190050/what-is-base-address-register-bar-in-pcie/44716618#44716618" class="bare">https://stackoverflow.com/questions/30190050/what-is-base-address-register-bar-in-pcie/44716618#44716618</a></p>
</div>
<div class="paragraph">
<p>Each PCI device has 6 BAR IOs (base address register) as per the PCI spec.</p>
</div>
<div class="paragraph">
<p>Each BAR corresponds to an address range that can be used to communicate with the PCI.</p>
</div>
<div class="paragraph">
<p>Each BAR is of one of the two types:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>IORESOURCE_IO</code>: must be accessed with <code>inX</code> and <code>outX</code></p>
</li>
<li>
<p><code>IORESOURCE_MEM</code>: must be accessed with <code>ioreadX</code> and <code>iowriteX</code>. This is the saner method apparently, and what the edu device uses.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The length of each region is defined by the hardware, and communicated to software via the configuration registers.</p>
</div>
<div class="paragraph">
<p>The Linux kernel automatically parses the 64 bytes of standardized configuration registers for us.</p>
</div>
<div class="paragraph">
<p>QEMU devices register those regions with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>memory_region_init_io(&amp;edu-&gt;mmio, OBJECT(edu), &amp;edu_mmio_ops, edu,
"edu-mmio", 1 &lt;&lt; 20);
pci_register_bar(pdev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &amp;edu-&gt;mmio);</pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="gpio"><a class="anchor" href="#gpio"></a><a class="link" href="#gpio">18.5.2. GPIO</a></h4>
<div class="paragraph">
<p>TODO: broken. Was working before we moved <code>arm</code> from <code>-M versatilepb</code> to <code>-M virt</code> around af210a76711b7fa4554dcc2abd0ddacfc810dfd4. Either make it work on <code>-M virt</code> if that is possible, or document precisely how to make it work with <code>versatilepb</code>, or hopefully <code>vexpress</code> which is newer.</p>
</div>
<div class="paragraph">
<p>QEMU does not have a very nice mechanism to observe GPIO activity: <a href="https://raspberrypi.stackexchange.com/questions/56373/is-it-possible-to-get-the-state-of-the-leds-and-gpios-in-a-qemu-emulation-like-t/69267#69267" class="bare">https://raspberrypi.stackexchange.com/questions/56373/is-it-possible-to-get-the-state-of-the-leds-and-gpios-in-a-qemu-emulation-like-t/69267#69267</a></p>
</div>
<div class="paragraph">
<p>The best you can do is to hack our <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build">build</a> script to add:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>HOST_QEMU_OPTS='--extra-cflags=-DDEBUG_PL061=1'</pre>
</div>
</div>
<div class="paragraph">
<p>where <a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0190b/index.html">PL061</a> is the dominating ARM Holdings hardware that handles GPIO.</p>
</div>
<div class="paragraph">
<p>Then compile with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --arch arm --config-fragment buildroot_config/gpio
./build-linux --config-fragment linux_config/gpio</pre>
</div>
</div>
<div class="paragraph">
<p>then test it out with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gpio.sh</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/gpio.sh">rootfs_overlay/lkmc/gpio.sh</a></p>
</div>
<div class="paragraph">
<p>Buildroot&#8217;s Linux tools package provides some GPIO CLI tools: <code>lsgpio</code>, <code>gpio-event-mon</code>, <code>gpio-hammer</code>, TODO document them here.</p>
</div>
</div>
<div class="sect3">
<h4 id="leds"><a class="anchor" href="#leds"></a><a class="link" href="#leds">18.5.3. LEDs</a></h4>
<div class="paragraph">
<p>TODO: broken when <code>arm</code> moved to <code>-M virt</code>, same as <a href="#gpio">GPIO</a>.</p>
</div>
<div class="paragraph">
<p>Hack QEMU&#8217;s <code>hw/misc/arm_sysctl.c</code> with a printf:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>static void arm_sysctl_write(void *opaque, hwaddr offset,
uint64_t val, unsigned size)
{
arm_sysctl_state *s = (arm_sysctl_state *)opaque;
switch (offset) {
case 0x08: /* LED */
printf("LED val = %llx\n", (unsigned long long)val);</pre>
</div>
</div>
<div class="paragraph">
<p>and then rebuild with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-qemu --arch arm
./build-linux --arch arm --config-fragment linux_config/leds</pre>
</div>
</div>
<div class="paragraph">
<p>But beware that one of the LEDs has a heartbeat trigger by default (specified on dts), so it will produce a lot of output.</p>
</div>
<div class="paragraph">
<p>And then activate it with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd /sys/class/leds/versatile:0
cat max_brightness
echo 255 &gt;brightness</pre>
</div>
</div>
<div class="paragraph">
<p>Relevant QEMU files:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>hw/arm/versatilepb.c</code></p>
</li>
<li>
<p><code>hw/misc/arm_sysctl.c</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Relevant kernel files:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>arch/arm/boot/dts/versatile-pb.dts</code></p>
</li>
<li>
<p><code>drivers/leds/led-class.c</code></p>
</li>
<li>
<p><code>drivers/leds/leds-sysctl.c</code></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="platform_device"><a class="anchor" href="#platform_device"></a><a class="link" href="#platform_device">18.5.4. platform_device</a></h4>
<div class="paragraph">
<p>Minimal platform device example coded into the <code>-M versatilepb</code> SoC of our QEMU fork.</p>
</div>
<div class="paragraph">
<p>Using this device now requires checking out to the branch:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git checkout platform-device
git submodule sync</pre>
</div>
</div>
<div class="paragraph">
<p>before building, it does not work on master.</p>
</div>
<div class="paragraph">
<p>Rationale: we found out that the kernels that build for <code>qemu -M versatilepb</code> don&#8217;t work on gem5 because <code>versatilepb</code> is an old pre-v7 platform, and gem5 requires armv7. So we migrated over to <code>-M virt</code> to have a single kernel for both gem5 and QEMU, and broke this since the single kernel was more important. TODO port to <code>-M virt</code>.</p>
</div>
<div class="paragraph">
<p>The module itself can be found at: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/platform-device/kernel_modules/platform_device.c" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/blob/platform-device/kernel_modules/platform_device.c</a></p>
</div>
<div class="paragraph">
<p>Uses:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>hw/misc/lkmc_platform_device.c</code> minimal device added in our QEMU fork to <code>-M versatilepb</code></p>
</li>
<li>
<p>the device tree entry we added to our Linux kernel fork: <a href="https://github.com/cirosantilli/linux/blob/361bb623671a52a36a077a6dd45843389a687a33/arch/arm/boot/dts/versatile-pb.dts#L42" class="bare">https://github.com/cirosantilli/linux/blob/361bb623671a52a36a077a6dd45843389a687a33/arch/arm/boot/dts/versatile-pb.dts#L42</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Expected outcome after insmod:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>QEMU reports MMIO with printfs</p>
</li>
<li>
<p>IRQs are generated and handled by this module, which logs to dmesg</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Without insmoding this module, try writing to the register with <a href="#dev-mem">/dev/mem</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>devmem 0x101e9000 w 0x12345678</pre>
</div>
</div>
<div class="paragraph">
<p>We can also observe the interrupt with <a href="#dummy-irq">dummy-irq</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>modprobe dummy-irq irq=34
insmod platform_device.ko</pre>
</div>
</div>
<div class="paragraph">
<p>The IRQ number <code>34</code> was found by on the dmesg after:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>insmod platform_device.ko</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/28315265/how-to-add-a-new-device-in-qemu-source-code/44612957#44612957" class="bare">https://stackoverflow.com/questions/28315265/how-to-add-a-new-device-in-qemu-source-code/44612957#44612957</a></p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-educational-hardware-models"><a class="anchor" href="#gem5-educational-hardware-models"></a><a class="link" href="#gem5-educational-hardware-models">18.5.5. gem5 educational hardware models</a></h4>
<div class="paragraph">
<p>TODO get some working!</p>
</div>
<div class="paragraph">
<p><a href="http://gedare-csphd.blogspot.co.uk/2013/02/adding-simple-io-device-to-gem5.html" class="bare">http://gedare-csphd.blogspot.co.uk/2013/02/adding-simple-io-device-to-gem5.html</a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="qemu-monitor"><a class="anchor" href="#qemu-monitor"></a><a class="link" href="#qemu-monitor">18.6. QEMU monitor</a></h3>
<div class="paragraph">
<p>The QEMU monitor is a magic terminal that allows you to send text commands to the QEMU VM itself: <a href="https://en.wikibooks.org/wiki/QEMU/Monitor" class="bare">https://en.wikibooks.org/wiki/QEMU/Monitor</a></p>
</div>
<div class="paragraph">
<p>While QEMU is running, on another terminal, run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor</pre>
</div>
</div>
<div class="paragraph">
<p>or send one command such as <code>info qtree</code> and quit the monitor:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-monitor info qtree</pre>
</div>
</div>
<div class="paragraph">
<p>or equivalently:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 'info qtree' | ./qemu-monitor</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/qemu-monitor">qemu-monitor</a></p>
</div>
<div class="paragraph">
<p><code>qemu-monitor</code> uses the <code>-monitor</code> QEMU command line option, which makes the monitor listen from a socket.</p>
</div>
<div class="paragraph">
<p>Alternatively, we can also enter the QEMU monitor from inside <code>-nographics</code> <a href="#qemu-text-mode">QEMU text mode</a> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Ctrl-A C</pre>
</div>
</div>
<div class="paragraph">
<p>and go back to the terminal with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Ctrl-A C</pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/14165158/how-to-switch-to-qemu-monitor-console-when-running-with-curses" class="bare">https://stackoverflow.com/questions/14165158/how-to-switch-to-qemu-monitor-console-when-running-with-curses</a></p>
</li>
<li>
<p><a href="https://superuser.com/questions/488263/how-to-switch-to-the-qemu-control-panel-with-nographics" class="bare">https://superuser.com/questions/488263/how-to-switch-to-the-qemu-control-panel-with-nographics</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>When in graphic mode, we can do it from the GUI:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Ctrl-Alt ?</pre>
</div>
</div>
<div class="paragraph">
<p>where <code>?</code> is a digit <code>1</code>, or <code>2</code>, or, <code>3</code>, etc. depending on what else is available on the GUI: serial, parallel and frame buffer.</p>
</div>
<div class="paragraph">
<p>Finally, we can also access QEMU monitor commands directly from <a href="#gdb">GDB step debug</a> with the <code>monitor</code> command:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb</pre>
</div>
</div>
<div class="paragraph">
<p>then inside that shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>monitor info qtree</pre>
</div>
</div>
<div class="paragraph">
<p>This way you can use both QEMU monitor and GDB commands to inspect the guest from inside a single shell! Pretty awesome.</p>
</div>
<div class="paragraph">
<p>In general, <code>./qemu-monitor</code> is the best option, as it:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>works on both modes</p>
</li>
<li>
<p>allows to use the host Bash history to re-run one off commands</p>
</li>
<li>
<p>allows you to search the output of commands on your host shell even when in graphic mode</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Getting everything to work required careful choice of QEMU command line options:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/49716931/how-to-run-qemu-with-nographic-and-monitor-but-still-be-able-to-send-ctrlc-to/49751144#49751144" class="bare">https://stackoverflow.com/questions/49716931/how-to-run-qemu-with-nographic-and-monitor-but-still-be-able-to-send-ctrlc-to/49751144#49751144</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/167165/how-to-pass-ctrl-c-to-the-guest-when-running-qemu-with-nographic/436321#436321" class="bare">https://unix.stackexchange.com/questions/167165/how-to-pass-ctrl-c-to-the-guest-when-running-qemu-with-nographic/436321#436321</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="qemu-monitor-from-guest"><a class="anchor" href="#qemu-monitor-from-guest"></a><a class="link" href="#qemu-monitor-from-guest">18.6.1. QEMU monitor from guest</a></h4>
<div class="paragraph">
<p>Peter Maydell said potentially not possible nicely as of August 2018: <a href="https://stackoverflow.com/questions/51747744/how-to-run-a-qemu-monitor-command-from-inside-the-guest/51764110#51764110" class="bare">https://stackoverflow.com/questions/51747744/how-to-run-a-qemu-monitor-command-from-inside-the-guest/51764110#51764110</a></p>
</div>
<div class="paragraph">
<p>It is also worth looking into the QEMU Guest Agent tool <code>qemu-gq</code> that can be enabled with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_QEMU=y'</pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="https://superuser.com/questions/930588/how-to-pass-commands-noninteractively-to-running-qemu-from-the-guest-qmp-via-te" class="bare">https://superuser.com/questions/930588/how-to-pass-commands-noninteractively-to-running-qemu-from-the-guest-qmp-via-te</a></p>
</div>
</div>
<div class="sect3">
<h4 id="qemu-monitor-from-gdb"><a class="anchor" href="#qemu-monitor-from-gdb"></a><a class="link" href="#qemu-monitor-from-gdb">18.6.2. QEMU monitor from GDB</a></h4>
<div class="paragraph">
<p>When doing <a href="#gdb">GDB step debug</a> it is possible to send QEMU monitor commands through the GDB <code>monitor</code> command, which saves you the trouble of opening yet another shell.</p>
</div>
<div class="paragraph">
<p>Try for example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>monitor help
monitor info qtree</pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="debug-the-emulator"><a class="anchor" href="#debug-the-emulator"></a><a class="link" href="#debug-the-emulator">18.7. Debug the emulator</a></h3>
<div class="paragraph">
<p>When you start hacking QEMU or gem5, it is useful to see what is going on inside the emulator themselves.</p>
</div>
<div class="paragraph">
<p>This is of course trivial since they are just regular userland programs on the host, but we make it a bit easier with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --debug-vm</pre>
</div>
</div>
<div class="paragraph">
<p>Or for a faster development loop:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --debug-vm-args '-ex "break qemu_add_opts" -ex "run"'</pre>
</div>
</div>
<div class="paragraph">
<p>Our default emulator builds are optimized with <code>gcc -O2 -g</code>. To use <code>-O0</code> instead, build and run with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-qemu --qemu-build-type debug --verbose
./run --debug-vm
./build-gem5 --gem5-build-type debug --verbose
./run --debug-vm --emulator-gem5</pre>
</div>
</div>
<div class="paragraph">
<p>The <code>--verbose</code> is optional, but shows clearly each GCC build command so that you can confirm what <code>--*-build-type</code> is doing.</p>
</div>
<div class="paragraph">
<p>The build outputs are automatically stored in a different directories for optimized and debug builds, which prevents <code>debug</code> files from overwriting <code>opt</code> ones. Therefore, <code>--gem5-build-id</code> is not required:</p>
</div>
<div class="paragraph">
<p>The price to pay for debuggability is high however: a Linux kernel boot was about 3x slower in QEMU and 14 times slower in gem5 debug compared to opt, see benchmarks at: <a href="#benchmark-linux-kernel-boot">Section 29.2.1, &#8220;Benchmark Linux kernel boot&#8221;</a></p>
</div>
<div class="paragraph">
<p>When in <a href="#qemu-text-mode">QEMU text mode</a>, using <code>--debug-vm</code> makes Ctrl-C not get passed to the QEMU guest anymore: it is instead captured by GDB itself, so allow breaking. So e.g. you won&#8217;t be able to easily quit from a guest program like:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sleep 10</pre>
</div>
</div>
<div class="paragraph">
<p>In graphic mode, make sure that you never click inside the QEMU graphic while debugging, otherwise you mouse gets captured forever, and the only solution I can find is to go to a TTY with <code>Ctrl-Alt-F1</code> and <code>kill</code> QEMU.</p>
</div>
<div class="paragraph">
<p>You can still send key presses to QEMU however even without the mouse capture, just either click on the title bar, or alt tab to give it focus.</p>
</div>
<div class="sect3">
<h4 id="reverse-debug-the-emulator"><a class="anchor" href="#reverse-debug-the-emulator"></a><a class="link" href="#reverse-debug-the-emulator">18.7.1. Reverse debug the emulator</a></h4>
<div class="paragraph">
<p>While step debugging any complext program, you always end up feeling the need to step in reverse to reach the last call to some function that was called before the failure point, in order to trace back the problem to the actual bug source.</p>
</div>
<div class="paragraph">
<p>While GDB "has" this feature, it is just too broken to be usable, and so we expose the amazing Mozilla RR tool conveniently in this repo: <a href="https://stackoverflow.com/questions/1470434/how-does-reverse-debugging-work/53063242#53063242" class="bare">https://stackoverflow.com/questions/1470434/how-does-reverse-debugging-work/53063242#53063242</a></p>
</div>
<div class="paragraph">
<p>Before the first usage:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 'kernel.perf_event_paranoid=1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p</pre>
</div>
</div>
<div class="paragraph">
<p>Then use it with your content of interest, for example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --debug-vm-rr --userland userland/c/hello.c</pre>
</div>
</div>
<div class="paragraph">
<p>This will first run the program once until completion, and then restart the program at the very first instruction at <code>_start</code> and leave you in a GDB shell.</p>
</div>
<div class="paragraph">
<p>From there, run the program until your point of interest, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>break qemu_add_opts
continue</pre>
</div>
</div>
<div class="paragraph">
<p>and you can now reiably use reverse debugging commands such as <code>reverse-continue</code>, <code>reverse-finish</code> and <code>reverse-next</code>!</p>
</div>
<div class="paragraph">
<p>To restart debugging again after quitting <code>rr</code>, simlpy run on your host terminal:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rr replay</pre>
</div>
</div>
<div class="paragraph">
<p>Programs often tend to blow up in very low frames that use values passed in from higher frames. In those cases, remember that just like with forward debugging, you can&#8217;t just go:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>up
up
up
reverse-next</pre>
</div>
</div>
<div class="paragraph">
<p>but rather, you must:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>reverse-finish
reverse-finish
reverse-finish
reverse-next</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="debug-gem5-python-scripts"><a class="anchor" href="#debug-gem5-python-scripts"></a><a class="link" href="#debug-gem5-python-scripts">18.7.2. Debug gem5 Python scripts</a></h4>
<div class="paragraph">
<p>Start pdb at the first instruction:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --emulator gem5 --gem5-exe-args='--pdb' --terminal</pre>
</div>
</div>
<div class="paragraph">
<p>Requires <code>--terminal</code> as we must be on foreground.</p>
</div>
<div class="paragraph">
<p>Alternatively, you can add to the point of the code where you want to break the usual:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>import ipdb; ipdb.set_trace()</pre>
</div>
</div>
<div class="paragraph">
<p>and then run with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --emulator gem5 --terminal</pre>
</div>
</div>
<div class="paragraph">
<p>TODO test PyCharm: <a href="https://stackoverflow.com/questions/51982735/writing-gem5-configuration-scripts-with-pycharm" class="bare">https://stackoverflow.com/questions/51982735/writing-gem5-configuration-scripts-with-pycharm</a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="tracing"><a class="anchor" href="#tracing"></a><a class="link" href="#tracing">18.8. Tracing</a></h3>
<div class="paragraph">
<p>QEMU can log several different events.</p>
</div>
<div class="paragraph">
<p>The most interesting are events which show instructions that QEMU ran, for which we have a helper:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./trace-boot --arch x86_64</pre>
</div>
</div>
<div class="paragraph">
<p>Under the hood, this uses QEMU&#8217;s <code>-trace</code> option.</p>
</div>
<div class="paragraph">
<p>You can then inspect the address of each instruction run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>less "$(./getvar --arch x86_64 run_dir)/trace.txt"</pre>
</div>
</div>
<div class="paragraph">
<p>Sample output excerpt:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>exec_tb 0.000 pid=10692 tb=0x7fb4f8000040 pc=0xfffffff0
exec_tb 35.391 pid=10692 tb=0x7fb4f8000180 pc=0xfe05b
exec_tb 21.047 pid=10692 tb=0x7fb4f8000340 pc=0xfe066
exec_tb 12.197 pid=10692 tb=0x7fb4f8000480 pc=0xfe06a</pre>
</div>
</div>
<div class="paragraph">
<p>Get the list of available trace events:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --trace help</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: any way to show the actualy disassembled instruction executed directly from there? Possible with <a href="#qemu-d-tracing">QEMU -d tracing</a>.</p>
</div>
<div class="paragraph">
<p>Enable other specific trace events:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --trace trace1,trace2
./qemu-trace2txt -a "$arch"
less "$(./getvar -a "$arch" run_dir)/trace.txt"</pre>
</div>
</div>
<div class="paragraph">
<p>This functionality relies on the following setup:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>./configure --enable-trace-backends=simple</code>. This logs in a binary format to the trace file.</p>
<div class="paragraph">
<p>It makes 3x execution faster than the default trace backend which logs human readable data to stdout.</p>
</div>
<div class="paragraph">
<p>Logging with the default backend <code>log</code> greatly slows down the CPU, and in particular leads to this boot message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>All QSes seen, last rcu_sched kthread activity 5252 (4294901421-4294896169), jiffies_till_next_fqs=1, root -&gt;qsmask 0x0
swapper/0 R running task 0 1 0 0x00000008
ffff880007c03ef8 ffffffff8107aa5d ffff880007c16b40 ffffffff81a3b100
ffff880007c03f60 ffffffff810a41d1 0000000000000000 0000000007c03f20
fffffffffffffedc 0000000000000004 fffffffffffffedc ffffffff00000000
Call Trace:
&lt;IRQ&gt; [&lt;ffffffff8107aa5d&gt;] sched_show_task+0xcd/0x130
[&lt;ffffffff810a41d1&gt;] rcu_check_callbacks+0x871/0x880
[&lt;ffffffff810a799f&gt;] update_process_times+0x2f/0x60</pre>
</div>
</div>
<div class="paragraph">
<p>in which the boot appears to hang for a considerable time.</p>
</div>
</li>
<li>
<p>patch QEMU source to remove the <code>disable</code> from <code>exec_tb</code> in the <code>trace-events</code> file. See also: <a href="https://rwmj.wordpress.com/2016/03/17/tracing-qemu-guest-execution/" class="bare">https://rwmj.wordpress.com/2016/03/17/tracing-qemu-guest-execution/</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="qemu-d-tracing"><a class="anchor" href="#qemu-d-tracing"></a><a class="link" href="#qemu-d-tracing">18.8.1. QEMU -d tracing</a></h4>
<div class="paragraph">
<p>QEMU also has a second trace mechanism in addition to <code>-trace</code>, find out the events with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run -- -d help</pre>
</div>
</div>
<div class="paragraph">
<p>Let&#8217;s pick the one that dumps executed instructions, <code>in_asm</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval './linux/poweroff.out' -- -D out/trace.txt -d in_asm
less out/trace.txt</pre>
</div>
</div>
<div class="paragraph">
<p>Sample output excerpt:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>----------------
IN:
0xfffffff0: ea 5b e0 00 f0 ljmpw $0xf000:$0xe05b
----------------
IN:
0x000fe05b: 2e 66 83 3e 88 61 00 cmpl $0, %cs:0x6188
0x000fe062: 0f 85 7b f0 jne 0xd0e1</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: after <code>IN:</code>, symbol names are meant to show, which is awesome, but I don&#8217;t get any. I do see them however when running a bare metal example from: <a href="https://github.com/cirosantilli/newlib-examples/tree/900a9725947b1f375323c7da54f69e8049158881" class="bare">https://github.com/cirosantilli/newlib-examples/tree/900a9725947b1f375323c7da54f69e8049158881</a></p>
</div>
<div class="paragraph">
<p>TODO: what is the point of having two mechanisms, <code>-trace</code> and <code>-d</code>? <code>-d</code> tracing is cool because it does not require a messy recompile, and it can also show symbols.</p>
</div>
</div>
<div class="sect3">
<h4 id="qemu-trace-register-values"><a class="anchor" href="#qemu-trace-register-values"></a><a class="link" href="#qemu-trace-register-values">18.8.2. QEMU trace register values</a></h4>
<div class="paragraph">
<p>TODO: is it possible to show the register values for each instruction?</p>
</div>
<div class="paragraph">
<p>This would include the memory values read into the registers.</p>
</div>
<div class="paragraph">
<p>Asked at: <a href="https://superuser.com/questions/1377764/how-to-trace-the-register-values-of-executed-instructions-in-qemu" class="bare">https://superuser.com/questions/1377764/how-to-trace-the-register-values-of-executed-instructions-in-qemu</a></p>
</div>
<div class="paragraph">
<p>Seems impossible due to optimizations that QEMU does:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07479.html" class="bare">https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07479.html</a></p>
</li>
<li>
<p><a href="https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg02856.html" class="bare">https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg02856.html</a></p>
</li>
<li>
<p><a href="https://lists.gnu.org/archive/html/qemu-devel/2012-08/msg03057.html" class="bare">https://lists.gnu.org/archive/html/qemu-devel/2012-08/msg03057.html</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>PANDA can list memory addresses, so I bet it can also decode the instructions: <a href="https://github.com/panda-re/panda/blob/883c85fa35f35e84a323ed3d464ff40030f06bd6/panda/docs/LINE_Censorship.md" class="bare">https://github.com/panda-re/panda/blob/883c85fa35f35e84a323ed3d464ff40030f06bd6/panda/docs/LINE_Censorship.md</a> I wonder why they don&#8217;t just upstream those things to QEMU&#8217;s tracing: <a href="https://github.com/panda-re/panda/issues/290" class="bare">https://github.com/panda-re/panda/issues/290</a></p>
</div>
<div class="paragraph">
<p>gem5 can do it as shown at: <a href="#gem5-tracing">Section 18.8.7, &#8220;gem5 tracing&#8221;</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="qemu-trace-memory-accesses"><a class="anchor" href="#qemu-trace-memory-accesses"></a><a class="link" href="#qemu-trace-memory-accesses">18.8.3. QEMU trace memory accesses</a></h4>
<div class="paragraph">
<p>Not possible apparently, not even with the <code>memory_region_ops_read</code> and <code>memory_region_ops_write</code> trace events, Peter comments <a href="https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07482.html" class="bare">https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07482.html</a></p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>No. You will miss all the fast-path memory accesses, which are
done with custom generated assembly in the TCG backend. In
general QEMU is not designed to support this kind of monitoring
of guest operations.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>Related question: <a href="https://reverseengineering.stackexchange.com/questions/12260/how-to-log-all-memory-accesses-read-and-write-including-the-memory-content-in" class="bare">https://reverseengineering.stackexchange.com/questions/12260/how-to-log-all-memory-accesses-read-and-write-including-the-memory-content-in</a></p>
</div>
</div>
<div class="sect3">
<h4 id="trace-source-lines"><a class="anchor" href="#trace-source-lines"></a><a class="link" href="#trace-source-lines">18.8.4. Trace source lines</a></h4>
<div class="paragraph">
<p>We can further use Binutils' <code>addr2line</code> to get the line that corresponds to each address:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./trace-boot --arch x86_64
./trace2line --arch x86_64
less "$(./getvar --arch x86_64 run_dir)/trace-lines.txt"</pre>
</div>
</div>
<div class="paragraph">
<p>The last commands takes several seconds.</p>
</div>
<div class="paragraph">
<p>The format is as follows:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>39368 _static_cpu_has arch/x86/include/asm/cpufeature.h:148</pre>
</div>
</div>
<div class="paragraph">
<p>Where:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>39368</code>: number of consecutive times that a line ran. Makes the output much shorter and more meaningful</p>
</li>
<li>
<p><code>_static_cpu_has</code>: name of the function that contains the line</p>
</li>
<li>
<p><code>arch/x86/include/asm/cpufeature.h:148</code>: file and line</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This could of course all be done with GDB, but it would likely be too slow to be practical.</p>
</div>
<div class="paragraph">
<p>TODO do even more awesome offline post-mortem analysis things, such as:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>detect if we are in userspace or kernelspace. Should be a simple matter of reading the</p>
</li>
<li>
<p>read kernel data structures, and determine the current thread. Maybe we can reuse / extend the kernel&#8217;s GDB Python scripts??</p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="qemu-record-and-replay"><a class="anchor" href="#qemu-record-and-replay"></a><a class="link" href="#qemu-record-and-replay">18.8.5. QEMU record and replay</a></h4>
<div class="paragraph">
<p>QEMU runs, unlike gem5, are not deterministic by default, however it does support a record and replay mechanism that allows you to replay a previous run deterministically.</p>
</div>
<div class="paragraph">
<p>This awesome feature allows you to examine a single run as many times as you would like until you understand everything:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># Record a run.
./run --eval-after './linux/rand_check.out;./linux/poweroff.out;' --record
# Replay the run.
./run --eval-after './linux/rand_check.out;./linux/poweroff.out;' --replay</pre>
</div>
</div>
<div class="paragraph">
<p>A convenient shortcut to do both at once to test the feature is:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-rr --eval-after './linux/rand_check.out;./linux/poweroff.out;'</pre>
</div>
</div>
<div class="paragraph">
<p>By comparing the terminal output of both runs, we can see that they are the exact same, including things which normally differ across runs:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>timestamps of dmesg output</p>
</li>
<li>
<p><a href="#rand_check-out">rand_check.out</a> output</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The record and replay feature was revived around QEMU v3.0.0. It existed earlier but it rot completely. As of v3.0.0 it is still flaky: sometimes we get deadlocks, and only a limited number of command line arguments are supported.</p>
</div>
<div class="paragraph">
<p>Documented at: <a href="https://github.com/qemu/qemu/blob/v2.12.0/docs/replay.txt" class="bare">https://github.com/qemu/qemu/blob/v2.12.0/docs/replay.txt</a></p>
</div>
<div class="paragraph">
<p>TODO: using <code>-r</code> as above leads to a kernel warning:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rcu_sched detected stalls on CPUs/tasks</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: replay deadlocks intermittently at disk operations, last kernel message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>EXT4-fs (sda): re-mounted. Opts: block_validity,barrier,user_xattr</pre>
</div>
</div>
<div class="paragraph">
<p>TODO replay with network gets stuck:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./qemu-rr --eval-after 'ifup -a;wget -S google.com;./linux/poweroff.out;'</pre>
</div>
</div>
<div class="paragraph">
<p>after the message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>adding dns 10.0.2.3</pre>
</div>
</div>
<div class="paragraph">
<p>There is explicit network support on the QEMU patches, but either it is buggy or we are not using the correct magic options.</p>
</div>
<div class="paragraph">
<p>Solved on unmerged c42634d8e3428cfa60672c3ba89cabefc720cde9 from <a href="https://github.com/ispras/qemu/tree/rr-180725" class="bare">https://github.com/ispras/qemu/tree/rr-180725</a></p>
</div>
<div class="paragraph">
<p>TODO <code>arm</code> and <code>aarch64</code> only seem to work with initrd since I cannot plug a working IDE disk device? See also: <a href="https://lists.gnu.org/archive/html/qemu-devel/2018-02/msg05245.html" class="bare">https://lists.gnu.org/archive/html/qemu-devel/2018-02/msg05245.html</a></p>
</div>
<div class="paragraph">
<p>Then, when I tried with <a href="#initrd">initrd</a> and no disk:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --arch aarch64 --initrd
./qemu-rr --arch aarch64 --eval-after './linux/rand_check.out;./linux/poweroff.out;' --initrd</pre>
</div>
</div>
<div class="paragraph">
<p>QEMU crashes with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ERROR:replay/replay-time.c:49:replay_read_clock: assertion failed: (replay_file &amp;&amp; replay_mutex_locked())</pre>
</div>
</div>
<div class="paragraph">
<p>I had the same error previously on x86-64, but it was fixed: <a href="https://bugs.launchpad.net/qemu/+bug/1762179" class="bare">https://bugs.launchpad.net/qemu/+bug/1762179</a> so maybe the forgot to fix it for <code>aarch64</code>?</p>
</div>
<div class="paragraph">
<p>Solved on unmerged c42634d8e3428cfa60672c3ba89cabefc720cde9 from <a href="https://github.com/ispras/qemu/tree/rr-180725" class="bare">https://github.com/ispras/qemu/tree/rr-180725</a></p>
</div>
<div class="sect4">
<h5 id="qemu-reverse-debugging"><a class="anchor" href="#qemu-reverse-debugging"></a><a class="link" href="#qemu-reverse-debugging">18.8.5.1. QEMU reverse debugging</a></h5>
<div class="paragraph">
<p>TODO get working.</p>
</div>
<div class="paragraph">
<p>QEMU replays support checkpointing, and this allows for a simplistic "reverse debugging" implementation proposed at <a href="https://lists.gnu.org/archive/html/qemu-devel/2018-06/msg00478.html" class="bare">https://lists.gnu.org/archive/html/qemu-devel/2018-06/msg00478.html</a> on the unmerged <a href="https://github.com/ispras/qemu/tree/rr-180725" class="bare">https://github.com/ispras/qemu/tree/rr-180725</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after './linux/rand_check.out;./linux/poweroff.out;' --record
./run --eval-after './linux/rand_check.out;./linux/poweroff.out;' --replay --gdb-wait</pre>
</div>
</div>
<div class="paragraph">
<p>On another shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb start_kernel</pre>
</div>
</div>
<div class="paragraph">
<p>In GDB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>n
n
n
n
reverse-continue</pre>
</div>
</div>
<div class="paragraph">
<p>and we are back at <code>start_kernel</code></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="qemu-trace-multicore"><a class="anchor" href="#qemu-trace-multicore"></a><a class="link" href="#qemu-trace-multicore">18.8.6. QEMU trace multicore</a></h4>
<div class="paragraph">
<p>TODO: is there any way to distinguish which instruction runs on each core? Doing:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch x86_64 --cpus 2 --eval './linux/poweroff.out' --trace exec_tb
./qemu-trace2txt</pre>
</div>
</div>
<div class="paragraph">
<p>just appears to output both cores intertwined without any clear differentiation.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-tracing"><a class="anchor" href="#gem5-tracing"></a><a class="link" href="#gem5-tracing">18.8.7. gem5 tracing</a></h4>
<div class="paragraph">
<p>gem5 provides also provides a tracing mechanism documented at: <a href="http://www.gem5.org/Trace_Based_Debugging" class="bare">http://www.gem5.org/Trace_Based_Debugging</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --eval 'm5 exit' --emulator gem5 --trace ExecAll
less "$(./getvar --arch aarch64 run_dir)/trace.txt"</pre>
</div>
</div>
<div class="paragraph">
<p>Our wrapper just forwards the options to the <code>--debug-flags</code> gem5 option.</p>
</div>
<div class="paragraph">
<p>Keep in mind however that the disassembly is very broken in several places as of 2019q2, so you can&#8217;t always trust it.</p>
</div>
<div class="paragraph">
<p>Output the trace to stdout instead of a file:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--emulator gem5 \
--eval 'm5 exit' \
--trace ExecAll \
--trace-stdout \
;</pre>
</div>
</div>
<div class="paragraph">
<p>We also have a shortcut for <code>--trace ExecAll -trace-stdout</code> with <code>--trace-insts-stdout</code></p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--emulator gem5 \
--eval 'm5 exit' \
--trace-insts-stdout \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Be warned, the trace is humongous, at 16Gb.</p>
</div>
<div class="paragraph">
<p>This would produce a lot of output however, so you will likely not want that when tracing a Linux kernel boot instructions. But it can be very convenient for smaller traces such as <a href="#baremetal">Baremetal</a>.</p>
</div>
<div class="paragraph">
<p>List all available debug flags:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --gem5-exe-args='--debug-help' --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>but to understand most of them you have to look at the source code:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>less "$(./getvar gem5_source_dir)/src/cpu/SConscript"
less "$(./getvar gem5_source_dir)/src/cpu/exetrace.cc"</pre>
</div>
</div>
<div class="paragraph">
<p>The most important trace flags to know about are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#gem5-execall-trace-format"><code>ExecAll</code></a></p>
</li>
<li>
<p><code>Faults</code>: CPU exceptions / interrupts, see an example at: <a href="#arm-svc-instruction">ARM SVC instruction</a></p>
</li>
<li>
<p><a href="#gem5-registers-trace-format"><code>Registers</code></a></p>
</li>
<li>
<p><a href="#gem5-syscall-emulation-mode-syscall-tracing"><code>SyscallBase</code>, <code>SyscallVerbose</code></a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Trace internals are discussed at: <a href="#gem5-trace-internals">gem5 trace internals</a>.</p>
</div>
<div class="paragraph">
<p>As can be seen on the <code>Sconstruct</code>, <code>Exec</code> is just an alias that enables a set of flags.</p>
</div>
<div class="paragraph">
<p>We can make the trace smaller by naming the trace file as <code>trace.txt.gz</code>, which enables GZIP compression, but that is not currently exposed on our scripts, since you usually just need something human readable to work on.</p>
</div>
<div class="paragraph">
<p>Enabling tracing made the runtime about 4x slower on the <a href="#p51">P51</a>, with or without <code>.gz</code> compression.</p>
</div>
<div class="paragraph">
<p>Trace the source lines just like <a href="#trace-source-lines">for QEMU</a> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./trace-boot --arch aarch64 --emulator gem5
./trace2line --arch aarch64 --emulator gem5
less "$(./getvar --arch aarch64 run_dir)/trace-lines.txt"</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: 7452d399290c9c1fc6366cdad129ef442f323564 <code>./trace2line</code> this is too slow and takes hours. QEMU&#8217;s processing of 170k events takes 7 seconds. gem5&#8217;s processing is analogous, but there are 140M events, so it should take 7000 seconds ~ 2 hours which seems consistent with what I observe, so maybe there is no way to speed this up&#8230;&#8203; The workaround is to just use gem5&#8217;s <code>ExecSymbol</code> to get function granularity, and then GDB individually if line detail is needed?</p>
</div>
<div class="sect4">
<h5 id="gem5-trace-internals"><a class="anchor" href="#gem5-trace-internals"></a><a class="link" href="#gem5-trace-internals">18.8.7.1. gem5 trace internals</a></h5>
<div class="paragraph">
<p>gem5 traces are generated from <code>DPRINTF(&lt;trace-id&gt;</code> calls scattered throughout the code, except for <code>ExecAll</code> instruction traces, which uses <code>Debug::ExecEnable</code> directly..</p>
</div>
<div class="paragraph">
<p>The trace IDs are themselves encoded in <code>SConscript</code> files, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>DebugFlag('Event'</pre>
</div>
</div>
<div class="paragraph">
<p>in <code>src/cpu/SConscript</code>.</p>
</div>
<div class="paragraph">
<p>The build system then automatically adds the options to the <code>--debug-flags</code>.</p>
</div>
<div class="paragraph">
<p>For this entry, the build system then generates a file <code>build/ARM/debug/ExecEnable.hh</code>, which contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>namespace Debug {
class SimpleFlag;
extern SimpleFlag ExecEnable;
}</pre>
</div>
</div>
<div class="paragraph">
<p>and must be included in from callers of <code>DPRINTF(</code> as <code>&lt;debug/ExecEnable.hh&gt;</code>.</p>
</div>
<div class="paragraph">
<p>Tested in b4879ae5b0b6644e6836b0881e4da05c64a6550d.</p>
</div>
</div>
<div class="sect4">
<h5 id="gem5-execall-trace-format"><a class="anchor" href="#gem5-execall-trace-format"></a><a class="link" href="#gem5-execall-trace-format">18.8.7.2. gem5 ExecAll trace format</a></h5>
<div class="paragraph">
<p>This debug flag traces all instructions.</p>
</div>
<div class="paragraph">
<p>The output format is of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>25007000: system.cpu T0 : @start_kernel : stp
25007000: system.cpu T0 : @start_kernel.0 : addxi_uop ureg0, sp, #-112 : IntAlu : D=0xffffff8008913f90
25007500: system.cpu T0 : @start_kernel.1 : strxi_uop x29, [ureg0] : MemWrite : D=0x0000000000000000 A=0xffffff8008913f90
25008000: system.cpu T0 : @start_kernel.2 : strxi_uop x30, [ureg0, #8] : MemWrite : D=0x0000000000000000 A=0xffffff8008913f98
25008500: system.cpu T0 : @start_kernel.3 : addxi_uop sp, ureg0, #0 : IntAlu : D=0xffffff8008913f90</pre>
</div>
</div>
<div class="paragraph">
<p>There are two types of lines:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>full instructions, as the first line. Only shown if the <code>ExecMacro</code> flag is given.</p>
</li>
<li>
<p>micro ops that constitute the instruction, the lines that follow. Yes, <code>aarch64</code> also has microops: <a href="https://superuser.com/questions/934752/do-arm-processors-like-cortex-a9-use-microcode/934755#934755" class="bare">https://superuser.com/questions/934752/do-arm-processors-like-cortex-a9-use-microcode/934755#934755</a>. Only shown if the <code>ExecMicro</code> flag is given.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Breakdown:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>25007500</code>: time count in some unit. Note how the microops execute at further timestamps.</p>
</li>
<li>
<p><code>system.cpu</code>: distinguishes between CPUs when there are more than one. For example, running <a href="#arm-multicore">Section 27.8.3, &#8220;ARM multicore&#8221;</a> with two cores produces <code>system.cpu0</code> and <code>system.cpu1</code></p>
</li>
<li>
<p><code>T0</code>: thread number. TODO: <a href="https://superuser.com/questions/133082/hyper-threading-and-dual-core-whats-the-difference/995858#995858">hyperthread</a>? How to play with it?</p>
<div class="paragraph">
<p><code>config</code>.ini has <code>--param 'system.multi_thread = True' --param 'system.cpu[0].numThreads = 2'</code>, but in <a href="#arm-multicore">ARM multicore</a> the first one alone does not produce <code>T1</code>, and with the second one simulation blows up with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>fatal: fatal condition interrupts.size() != numThreads occurred: CPU system.cpu has 1 interrupt controllers, but is expecting one per thread (2)</pre>
</div>
</div>
</li>
<li>
<p><code>@start_kernel</code>: we are in the <code>start_kernel</code> function. Awesome feature! Implemented with libelf <a href="https://sourceforge.net/projects/elftoolchain/" class="bare">https://sourceforge.net/projects/elftoolchain/</a> copy pasted in-tree <code>ext/libelf</code>. To get raw addresses, remove the <code>ExecSymbol</code>, which is enabled by <code>Exec</code>. This can be done with <code>Exec,-ExecSymbol</code>.</p>
</li>
<li>
<p><code>.1</code> as in <code>@start_kernel.1</code>: index of the microop</p>
</li>
<li>
<p><code>stp</code>: instruction disassembly. Note however that the disassembly of many instructions are very broken as of 2019q2, and you can&#8217;t just trust them blindly.</p>
</li>
<li>
<p><code>strxi_uop x29, [ureg0]</code>: microop disassembly.</p>
</li>
<li>
<p><code>MemWrite : D=0x0000000000000000 A=0xffffff8008913f90</code>: a memory write microop:</p>
<div class="ulist">
<ul>
<li>
<p><code>D</code> stands for data, and represents the value that was written to memory or to a register</p>
</li>
<li>
<p><code>A</code> stands for address, and represents the address to which the value was written. It only shows when data is being written to memory, but not to registers.</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>The best way to verify all of this is to write some <a href="#baremetal">baremetal code</a></p>
</div>
</div>
<div class="sect4">
<h5 id="gem5-registers-trace-format"><a class="anchor" href="#gem5-registers-trace-format"></a><a class="link" href="#gem5-registers-trace-format">18.8.7.3. gem5 Registers trace format</a></h5>
<div class="paragraph">
<p>This flag shows a more detailed register usage than <a href="#gem5-execall-trace-format">gem5 ExecAll trace format</a>.</p>
</div>
<div class="paragraph">
<p>For example, if we run in LKMC 0323e81bff1d55b978a4b36b9701570b59b981eb:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --baremetal userland/arch/aarch64/add.S --emulator gem5 --trace ExecAll,Registers --trace-stdout</pre>
</div>
</div>
<div class="paragraph">
<p>then the stdout contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> 31000: system.cpu A0 T0 : @main_after_prologue : movz x0, #1, #0 : IntAlu : D=0x0000000000000001 flags=(IsInteger)
31500: system.cpu.[tid:0]: Setting int reg 34 (34) to 0.
31500: system.cpu.[tid:0]: Reading int reg 0 (0) as 0x1.
31500: system.cpu.[tid:0]: Setting int reg 1 (1) to 0x3.
31500: system.cpu A0 T0 : @main_after_prologue+4 : add x1, x0, #2 : IntAlu : D=0x0000000000000003 flags=(IsInteger)
32000: system.cpu.[tid:0]: Setting int reg 34 (34) to 0.
32000: system.cpu.[tid:0]: Reading int reg 1 (1) as 0x3.
32000: system.cpu.[tid:0]: Reading int reg 31 (34) as 0.
32000: system.cpu.[tid:0]: Setting int reg 0 (0) to 0x3.</pre>
</div>
</div>
<div class="paragraph">
<p>which corresponds to the two following instructions:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mov x0, 1
add x1, x0, 2</pre>
</div>
</div>
<div class="paragraph">
<p>TODO that format is either buggy or is very difficult to understand:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>what is <code>34</code>? Presumably some flags register?</p>
</li>
<li>
<p>what do the numbers in parenthesis mean at <code>31 (34)</code>? Presumably some flags register?</p>
</li>
<li>
<p>why is the first instruction setting <code>reg 1</code> and the second one <code>reg 0</code>, given that the first sets <code>x0</code> and the second <code>x1</code>?</p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="gem5-tarmac-traces"><a class="anchor" href="#gem5-tarmac-traces"></a><a class="link" href="#gem5-tarmac-traces">18.8.7.4. gem5 TARMAC traces</a></h5>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/54882466/how-to-use-the-tarmac-tracer-with-gem5" class="bare">https://stackoverflow.com/questions/54882466/how-to-use-the-tarmac-tracer-with-gem5</a></p>
</div>
</div>
<div class="sect4">
<h5 id="gem5-tracing-internals"><a class="anchor" href="#gem5-tracing-internals"></a><a class="link" href="#gem5-tracing-internals">18.8.7.5. gem5 tracing internals</a></h5>
<div class="paragraph">
<p>As of gem5 16eeee5356585441a49d05c78abc328ef09f7ace the default tracer is <code>ExeTracer</code>. It is set at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>src/cpu/BaseCPU.py:63:default_tracer = ExeTracer()</pre>
</div>
</div>
<div class="paragraph">
<p>which then gets used at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>class BaseCPU(ClockedObject):
[...]
tracer = Param.InstTracer(default_tracer, "Instruction tracer")</pre>
</div>
</div>
<div class="paragraph">
<p>All tracers derive from the common <code>InstTracer</code> base class:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git grep ': InstTracer'</pre>
</div>
</div>
<div class="paragraph">
<p>gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>src/arch/arm/tracers/tarmac_parser.hh:218: TarmacParser(const Params *p) : InstTracer(p), startPc(p-&gt;start_pc),
src/arch/arm/tracers/tarmac_tracer.cc:57: : InstTracer(p),
src/cpu/exetrace.hh:67: ExeTracer(const Params *params) : InstTracer(params)
src/cpu/inst_pb_trace.cc:72: : InstTracer(p), buf(nullptr), bufSize(0), curMsg(nullptr)
src/cpu/inteltrace.hh:63: IntelTrace(const IntelTraceParams *p) : InstTracer(p)</pre>
</div>
</div>
<div class="paragraph">
<p>As mentioned at <a href="#gem5-tarmac-traces">gem5 TARMAC traces</a>, there appears to be no way to select those currently without hacking the config scripts.</p>
</div>
<div class="paragraph">
<p>TARMAC is described at: <a href="#gem5-tarmac-traces">gem5 TARMAC traces</a>.</p>
</div>
<div class="paragraph">
<p>TODO: are <code>IntelTrace</code> and <code>TarmacParser</code> useful for anything or just relics?</p>
</div>
<div class="paragraph">
<p>Then there is also the <code>NativeTrace</code> class:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>src/cpu/nativetrace.hh:68:class NativeTrace : public ExeTracer</pre>
</div>
</div>
<div class="paragraph">
<p>which gets implemented in a few different ISAs, but not all:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>src/arch/arm/nativetrace.hh:40:class ArmNativeTrace : public NativeTrace
src/arch/sparc/nativetrace.hh:41:class SparcNativeTrace : public NativeTrace
src/arch/x86/nativetrace.hh:41:class X86NativeTrace : public NativeTrace</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: I can&#8217;t find any usages of those classes from in-tree configs.</p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="qemu-gui-is-unresponsive"><a class="anchor" href="#qemu-gui-is-unresponsive"></a><a class="link" href="#qemu-gui-is-unresponsive">18.9. QEMU GUI is unresponsive</a></h3>
<div class="paragraph">
<p>Sometimes in Ubuntu 14.04, after the QEMU SDL GUI starts, it does not get updated after keyboard strokes, and there are artifacts like disappearing text.</p>
</div>
<div class="paragraph">
<p>We have not managed to track this problem down yet, but the following workaround always works:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Ctrl-Shift-U
Ctrl-C
root</pre>
</div>
</div>
<div class="paragraph">
<p>This started happening when we switched to building QEMU through Buildroot, and has not been observed on later Ubuntu.</p>
</div>
<div class="paragraph">
<p>Using text mode is another workaround if you don&#8217;t need GUI features.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="gem5"><a class="anchor" href="#gem5"></a><a class="link" href="#gem5">19. gem5</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Getting started at: <a href="#gem5-buildroot-setup">Section 1.2, &#8220;gem5 Buildroot setup&#8221;</a>.</p>
</div>
<div class="sect2">
<h3 id="gem5-vs-qemu"><a class="anchor" href="#gem5-vs-qemu"></a><a class="link" href="#gem5-vs-qemu">19.1. gem5 vs QEMU</a></h3>
<div class="ulist">
<ul>
<li>
<p>advantages of gem5:</p>
<div class="ulist">
<ul>
<li>
<p>simulates a generic more realistic <a href="#gem5-cpu-types">optionally pipelined and out-of-order</a> CPU cycle by cycle, including a realistic DRAM memory access model with latencies, caches and page table manipulations. This allows us to:</p>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>do much more realistic performance benchmarking with it, which makes absolutely no sense in QEMU, which is purely functional</p>
</li>
<li>
<p>make certain functional observations that are not possible in QEMU, e.g.:</p>
<div class="ulist">
<ul>
<li>
<p>use Linux kernel APIs that flush cache memory like DMA, which are crucial for driver development. In QEMU, the driver would still work even if we forget to flush caches.</p>
</li>
<li>
<p>spectre / meltdown:</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://www.mail-archive.com/gem5-users@gem5.org/msg15319.html" class="bare">https://www.mail-archive.com/gem5-users@gem5.org/msg15319.html</a></p>
</li>
<li>
<p><a href="https://github.com/jlpresearch/gem5/tree/spectre-test" class="bare">https://github.com/jlpresearch/gem5/tree/spectre-test</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>It is not of course truly cycle accurate, as that:</p>
</div>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>would require exposing proprietary information of the CPU designs: <a href="https://stackoverflow.com/questions/17454955/can-you-check-performance-of-a-program-running-with-qemu-simulator/33580850#33580850" class="bare">https://stackoverflow.com/questions/17454955/can-you-check-performance-of-a-program-running-with-qemu-simulator/33580850#33580850</a></p>
</li>
<li>
<p>would make the simulation even slower TODO confirm, by how much</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>but the approximation is reasonable.</p>
</div>
<div class="paragraph">
<p>It is used mostly for microarchitecture research purposes: when you are making a new chip technology, you don&#8217;t really need to specialize enormously to an existing microarchitecture, but rather develop something that will work with a wide range of future architectures.</p>
</div>
</li>
<li>
<p>runs are deterministic by default, unlike QEMU which has a special <a href="#qemu-record-and-replay">QEMU record and replay</a> mode, that requires first playing the content once and then replaying</p>
</li>
<li>
<p>gem5 ARM at least appears to implement more low level CPU functionality than QEMU, e.g. QEMU only added EL2 in 2018: <a href="https://stackoverflow.com/questions/42824706/qemu-system-aarch64-entering-el1-when-emulating-a53-power-up" class="bare">https://stackoverflow.com/questions/42824706/qemu-system-aarch64-entering-el1-when-emulating-a53-power-up</a> See also: <a href="#arm-exception-levels">Section 27.8.1, &#8220;ARM exception levels&#8221;</a></p>
</li>
<li>
<p>gem5 offers more advanced logging, even for non micro architectural things which QEMU models in some way, e.g. <a href="#qemu-trace-memory-accesses">QEMU trace memory accesses</a>, because QEMU&#8217;s binary translation optimizations reduce visibility</p>
</li>
</ul>
</div>
</li>
<li>
<p>disadvantages of gem5:</p>
<div class="ulist">
<ul>
<li>
<p>slower than QEMU, see: <a href="#benchmark-linux-kernel-boot">Section 29.2.1, &#8220;Benchmark Linux kernel boot&#8221;</a></p>
<div class="paragraph">
<p>This implies that the user base is much smaller, since no Android devs.</p>
</div>
<div class="paragraph">
<p>Instead, we have only chip makers, who keep everything that really works closed, and researchers, who can&#8217;t version track or document code properly &gt;:-) And this implies that:</p>
</div>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>the documentation is more scarce</p>
</li>
<li>
<p>it takes longer to support new hardware features</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>Well, not that AOSP is that much better anyways.</p>
</div>
</li>
<li>
<p>not sure: gem5 has BSD license while QEMU has GPL</p>
<div class="paragraph">
<p>This suits chip makers that want to distribute forks with secret IP to their customers.</p>
</div>
<div class="paragraph">
<p>On the other hand, the chip makers tend to upstream less, and the project becomes more crappy in average :-)</p>
</div>
</li>
<li>
<p>gem5 is way more complex and harder to modify and maintain</p>
<div class="paragraph">
<p>The only hairy thing in QEMU is the binary code generation.</p>
</div>
<div class="paragraph">
<p>gem5 however has tended towards horrendous intensive <a href="#gem5-code-generation">code generation</a> in order to support all its different hardware types</p>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="gem5-run-benchmark"><a class="anchor" href="#gem5-run-benchmark"></a><a class="link" href="#gem5-run-benchmark">19.2. gem5 run benchmark</a></h3>
<div class="paragraph">
<p>OK, this is why we used gem5 in the first place, performance measurements!</p>
</div>
<div class="paragraph">
<p>Let&#8217;s see how many cycles <a href="#dhrystone">Dhrystone</a>, which Buildroot provides, takes for a few different input parameters.</p>
</div>
<div class="paragraph">
<p>We will do that for various input parameters on full system by taking a checkpoint after the boot finishes a fast atomic CPU boot, and then we will restore in a more detailed mode and run the benchmark:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_DHRYSTONE=y'
# Boot fast, take checkpoint, and exit.
./run --arch aarch64 --emulator gem5 --eval-after './gem5.sh'
# Restore the checkpoint after boot, and benchmark with input 1000.
./run \
--arch aarch64 \
--emulator gem5 \
--eval-after './gem5.sh' \
--gem5-readfile 'm5 resetstats;dhrystone 1000;m5 dumpstats' \
--gem5-restore 1 \
-- \
--cpu-type=HPI \
--restore-with-cpu=HPI \
--caches \
--l2cache \
--l1d_size=64kB \
--l1i_size=64kB \
--l2_size=256kB \
;
# Get the value for number of cycles.
# head because there are two lines: our dumpstats and the
# automatic dumpstats at the end which we don't care about.
./gem5-stat --arch aarch64 | head -n 1
# Now for input 10000.
./run \
--arch aarch64 \
--emulator gem5 \
--eval-after './gem5.sh' \
--gem5-readfile 'm5 resetstats;dhrystone 10000;m5 dumpstats' \
--gem5-restore 1 \
-- \
--cpu-type=HPI \
--restore-with-cpu=HPI \
--caches \
--l2cache \
--l1d_size=64kB \
--l1i_size=64kB \
--l2_size=256kB \
;
./gem5-stat --arch aarch64 | head -n 1</pre>
</div>
</div>
<div class="paragraph">
<p>If you ever need a shell to quickly inspect the system state after boot, you can just use:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--emulator gem5 \
--eval-after './gem5.sh' \
--gem5-readfile 'sh' \
--gem5-restore 1 \</pre>
</div>
</div>
<div class="paragraph">
<p>This procedure is further automated and DRYed up at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gem5-bench-dhrystone
cat out/gem5-bench-dhrystone.txt</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/gem5-bench-dhrystone">gem5-bench-dhrystone</a></p>
</div>
<div class="paragraph">
<p>Output at 2438410c25e200d9766c8c65773ee7469b599e4a + 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>n cycles
1000 13665219
10000 20559002
100000 85977065</pre>
</div>
</div>
<div class="paragraph">
<p>so as expected, the Dhrystone run with a larger input parameter <code>100000</code> took more cycles than the ones with smaller input parameters.</p>
</div>
<div class="paragraph">
<p>The <code>gem5-stats</code> commands output the approximate number of CPU cycles it took Dhrystone to run.</p>
</div>
<div class="paragraph">
<p>A more naive and simpler to understand approach would be a direct:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --emulator gem5 --eval 'm5 checkpoint;m5 resetstats;dhrystone 10000;m5 exit'</pre>
</div>
</div>
<div class="paragraph">
<p>but the problem is that this method does not allow to easily run a different script without running the boot again. The <code>./gem5.sh</code> script works around that by using <a href="#m5-readfile">m5 readfile</a> as explained further at: <a href="#gem5-restore-new-script">Section 19.5.2, &#8220;gem5 checkpoint restore and run a different script&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Now you can play a fun little game with your friends:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>pick a computational problem</p>
</li>
<li>
<p>make a program that solves the computation problem, and outputs output to stdout</p>
</li>
<li>
<p>write the code that runs the correct computation in the smallest number of cycles possible</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>To find out why your program is slow, a good first step is to have a look at the <a href="#gem5-m5out-stats-txt-file">gem5 m5out/stats.txt file</a>.</p>
</div>
<div class="sect3">
<h4 id="skip-extra-benchmark-instructions"><a class="anchor" href="#skip-extra-benchmark-instructions"></a><a class="link" href="#skip-extra-benchmark-instructions">19.2.1. Skip extra benchmark instructions</a></h4>
<div class="paragraph">
<p>A few imperfections of our <a href="#gem5-run-benchmark">benchmarking method</a> are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>when we do <code>m5 resetstats</code> and <code>m5 exit</code>, there is some time passed before the <code>exec</code> system call returns and the actual benchmark starts and ends</p>
</li>
<li>
<p>the benchmark outputs to stdout, which means so extra cycles in addition to the actual computation. But TODO: how to get the output to check that it is correct without such IO cycles?</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Solutions to these problems include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>modify benchmark code with instrumentation directly, see <a href="#m5ops-instructions">m5ops instructions</a> for an example.</p>
</li>
<li>
<p>monitor known addresses TODO possible? Create an example.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Discussion at: <a href="https://stackoverflow.com/questions/48944587/how-to-count-the-number-of-cpu-clock-cycles-between-the-start-and-end-of-a-bench/48944588#48944588" class="bare">https://stackoverflow.com/questions/48944587/how-to-count-the-number-of-cpu-clock-cycles-between-the-start-and-end-of-a-bench/48944588#48944588</a></p>
</div>
<div class="paragraph">
<p>Those problems should be insignificant if the benchmark runs for long enough however.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-system-parameters"><a class="anchor" href="#gem5-system-parameters"></a><a class="link" href="#gem5-system-parameters">19.2.2. gem5 system parameters</a></h4>
<div class="paragraph">
<p>Besides optimizing a program for a given CPU setup, chip developers can also do the inverse, and optimize the chip for a given benchmark!</p>
</div>
<div class="paragraph">
<p>The rabbit hole is likely deep, but let&#8217;s scratch a bit of the surface.</p>
</div>
<div class="sect4">
<h5 id="number-of-cores"><a class="anchor" href="#number-of-cores"></a><a class="link" href="#number-of-cores">19.2.2.1. Number of cores</a></h5>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --cpus 2 --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>Check with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /proc/cpuinfo
getconf _NPROCESSORS_CONF</pre>
</div>
</div>
<div class="sect5">
<h6 id="qemu-user-mode-multithreading"><a class="anchor" href="#qemu-user-mode-multithreading"></a><a class="link" href="#qemu-user-mode-multithreading">19.2.2.1.1. QEMU user mode multithreading</a></h6>
<div class="paragraph">
<p>TODO why in <a href="#user-mode-simulation">User mode simulation</a> QEMU always shows the number of cores of the host. E.g., both of the following output the same as <code>nproc</code> on the host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>nproc
./run --userland userland/cpp/thread_hardware_concurrency.cpp
./run --cpus 2 --userland userland/cpp/thread_hardware_concurrency.cpp</pre>
</div>
</div>
<div class="paragraph">
<p>This random page suggests that QEMU splits one host thread thread per guest thread, and thus presumably delegates context switching to the host kernel: <a href="https://qemu.weilnetz.de/w64/2012/2012-12-04/qemu-tech.html#User-emulation-specific-details" class="bare">https://qemu.weilnetz.de/w64/2012/2012-12-04/qemu-tech.html#User-emulation-specific-details</a></p>
</div>
<div class="paragraph">
<p>We can confirm that with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --userland userland/posix/pthread_count.c --userland-args 4
ps Haux | grep qemu | wc</pre>
</div>
</div>
<div class="paragraph">
<p>Remember <a href="#qemu-user-mode-does-not-show-stdout-immediately">QEMU user mode does not show stdout immediately</a> though.</p>
</div>
<div class="paragraph">
<p>At 369a47fc6e5c2f4a7f911c1c058b6088f8824463 + 1 QEMU appears to spawn 3 host threads plus one for every new guest thread created. Remember that <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/pthread_count.c">userland/posix/pthread_count.c</a> spawns N + 1 total threads if you count the <code>main</code> thread.</p>
</div>
</div>
<div class="sect5">
<h6 id="gem5-syscall-emulation-multithreading"><a class="anchor" href="#gem5-syscall-emulation-multithreading"></a><a class="link" href="#gem5-syscall-emulation-multithreading">19.2.2.1.2. gem5 syscall emulation multithreading</a></h6>
<div class="paragraph">
<p>gem5 user mode multithreading has been particularly flaky compared <a href="#qemu-user-mode-multithreading">to QEMU&#8217;s</a>.</p>
</div>
<div class="paragraph">
<p>You have the limitation that you must have at least one core per guest thread, otherwise <code>pthread_create</code> fails. For example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --cpus 1 --emulator gem5 --static --userland userland/posix/pthread_self.c --userland-args 1</pre>
</div>
</div>
<div class="paragraph">
<p>fails because that process has a total of 2 threads: one for <code>main</code> and one extra thread spawned: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/pthread_self.c">userland/posix/pthread_self.c</a> The error message is:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>pthread_create: Resource temporarily unavailable</pre>
</div>
</div>
<div class="paragraph">
<p>It works however if we add on extra CPU:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --cpus 2 --emulator gem5 --static --userland userland/posix/pthread_self.c --userland-args 1</pre>
</div>
</div>
<div class="paragraph">
<p>This has to do with the fact that gem5 has a more simplistic thread implementation that does not spawn one host thread per guest thread CPU. Maybe this is required to achieve reproducible runs? What is the task switch algorithm then?</p>
</div>
<div class="paragraph">
<p>gem5 threading does however show the expected number of cores, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --cpus 1 --userland userland/cpp/thread_hardware_concurrency.cpp --emulator gem5 --static
./run --cpus 2 --userland userland/cpp/thread_hardware_concurrency.cpp --emulator gem5 --static</pre>
</div>
</div>
<div class="paragraph">
<p>outputs <code>1</code> and <code>2</code> respectively.</p>
</div>
<div class="paragraph">
<p>TODO: aarch64 seems to failing to spawn more than 2 threads at 369a47fc6e5c2f4a7f911c1c058b6088f8824463 + 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --cpus 3 --emulator gem5 --static --userland userland/posix/pthread_self.c --userland-args 2</pre>
</div>
</div>
<div class="paragraph">
<p>fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Exiting @ tick 18446744073709551615 because simulate() limit reached</pre>
</div>
</div>
</div>
<div class="sect5">
<h6 id="gem5-se-py-user-mode-with-2-or-more-pthreads-fails-with-because-simulate-limit-reached"><a class="anchor" href="#gem5-se-py-user-mode-with-2-or-more-pthreads-fails-with-because-simulate-limit-reached"></a><a class="link" href="#gem5-se-py-user-mode-with-2-or-more-pthreads-fails-with-because-simulate-limit-reached">19.2.2.1.3. gem5 se.py user mode with 2 or more pthreads fails with because simulate() limit reached</a></h6>
<div class="paragraph">
<p>See bug report at: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/issues/81" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/issues/81</a></p>
</div>
<div class="paragraph">
<p>Related: <a href="#gem5-simulate-limit-reached">gem5 simulate() limit reached</a>.</p>
</div>
</div>
<div class="sect5">
<h6 id="gem5-arm-full-system-with-more-than-8-cores"><a class="anchor" href="#gem5-arm-full-system-with-more-than-8-cores"></a><a class="link" href="#gem5-arm-full-system-with-more-than-8-cores">19.2.2.1.4. gem5 ARM full system with more than 8 cores</a></h6>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/50248067/how-to-run-a-gem5-arm-aarch64-full-system-simulation-with-fs-py-with-more-than-8" class="bare">https://stackoverflow.com/questions/50248067/how-to-run-a-gem5-arm-aarch64-full-system-simulation-with-fs-py-with-more-than-8</a></p>
</div>
<div class="paragraph">
<p>Build the kernel with the <a href="#gem5-arm-linux-kernel-patches">gem5 arm Linux kernel patches</a>, and then run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--linux-build-id gem5-v4.15 \
--emulator gem5 \
--cpus 16 \
-- \
--param 'system.realview.gic.gem5_extensions = True' \
;</pre>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="gem5-cache-size"><a class="anchor" href="#gem5-cache-size"></a><a class="link" href="#gem5-cache-size">19.2.2.2. gem5 cache size</a></h5>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/49624061/how-to-run-gem5-simulator-in-fs-mode-without-cache/49634544#49634544" class="bare">https://stackoverflow.com/questions/49624061/how-to-run-gem5-simulator-in-fs-mode-without-cache/49634544#49634544</a></p>
</div>
<div class="paragraph">
<p>A quick <code>./run --emulator gem5 -- -h</code> leads us to the options:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>--caches
--l1d_size=1024
--l1i_size=1024
--l2cache
--l2_size=1024
--l3_size=1024</pre>
</div>
</div>
<div class="paragraph">
<p>But keep in mind that it only affects benchmark performance of the most detailed CPU types as shown at: <a href="#table-gem5-cache-cpu-type">Table 2, &#8220;gem5 cache support in function of CPU type&#8221;</a>.</p>
</div>
<table id="table-gem5-cache-cpu-type" class="tableblock frame-all grid-all stretch">
<caption class="title">Table 2. gem5 cache support in function of CPU type</caption>
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">arch</th>
<th class="tableblock halign-left valign-top">CPU type</th>
<th class="tableblock halign-left valign-top">caches used</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">X86</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>AtomicSimpleCPU</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">X86</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DerivO3CPU</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">?*</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">ARM</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>AtomicSimpleCPU</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">ARM</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>HPI</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">yes</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>*: couldn&#8217;t test because of:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/49011096/how-to-switch-cpu-models-in-gem5-after-restoring-a-checkpoint-and-then-observe-t" class="bare">https://stackoverflow.com/questions/49011096/how-to-switch-cpu-models-in-gem5-after-restoring-a-checkpoint-and-then-observe-t</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Cache sizes can in theory be checked with the methods described at: <a href="https://superuser.com/questions/55776/finding-l2-cache-size-in-linux" class="bare">https://superuser.com/questions/55776/finding-l2-cache-size-in-linux</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>getconf -a | grep CACHE
lscpu
cat /sys/devices/system/cpu/cpu0/cache/index2/size</pre>
</div>
</div>
<div class="paragraph">
<p>but for some reason the Linux kernel is not seeing the cache sizes:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/49008792/why-doesnt-the-linux-kernel-see-the-cache-sizes-in-the-gem5-emulator-in-full-sy" class="bare">https://stackoverflow.com/questions/49008792/why-doesnt-the-linux-kernel-see-the-cache-sizes-in-the-gem5-emulator-in-full-sy</a></p>
</li>
<li>
<p><a href="http://gem5-users.gem5.narkive.com/4xVBlf3c/verify-cache-configuration" class="bare">http://gem5-users.gem5.narkive.com/4xVBlf3c/verify-cache-configuration</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Behaviour breakdown:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>arm QEMU and gem5 (both <code>AtomicSimpleCPU</code> or <code>HPI</code>), x86 gem5: <code>/sys</code> files don&#8217;t exist, and <code>getconf</code> and <code>lscpu</code> value empty</p>
</li>
<li>
<p>x86 QEMU: <code>/sys</code> files exist, but <code>getconf</code> and <code>lscpu</code> values still empty</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>So we take a performance measurement approach instead:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gem5-bench-cache -- --arch aarch64
cat "$(./getvar --arch aarch64 run_dir)/bench-cache.txt"</pre>
</div>
</div>
<div class="paragraph">
<p>which gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cmd ./run --emulator gem5 --arch aarch64 --gem5-readfile "dhrystone 1000" --gem5-restore 1 -- --caches --l2cache --l1d_size=1024 --l1i_size=1024 --l2_size=1024 --l3_size=1024 --cpu-type=HPI --restore-with-cpu=HPI
time 23.82
exit_status 0
cycles 93284622
instructions 4393457
cmd ./run --emulator gem5 --arch aarch64 --gem5-readfile "dhrystone 1000" --gem5-restore 1 -- --caches --l2cache --l1d_size=1024kB --l1i_size=1024kB --l2_size=1024kB --l3_size=1024kB --cpu-type=HPI --restore-with-cpu=HPI
time 14.91
exit_status 0
cycles 10128985
instructions 4211458
cmd ./run --emulator gem5 --arch aarch64 --gem5-readfile "dhrystone 10000" --gem5-restore 1 -- --caches --l2cache --l1d_size=1024 --l1i_size=1024 --l2_size=1024 --l3_size=1024 --cpu-type=HPI --restore-with-cpu=HPI
time 51.87
exit_status 0
cycles 188803630
instructions 12401336
cmd ./run --emulator gem5 --arch aarch64 --gem5-readfile "dhrystone 10000" --gem5-restore 1 -- --caches --l2cache --l1d_size=1024kB --l1i_size=1024kB --l2_size=1024kB --l3_size=1024kB --cpu-type=HPI --restore-with-cpu=HPI
time 35.35
exit_status 0
cycles 20715757
instructions 12192527
cmd ./run --emulator gem5 --arch aarch64 --gem5-readfile "dhrystone 100000" --gem5-restore 1 -- --caches --l2cache --l1d_size=1024 --l1i_size=1024 --l2_size=1024 --l3_size=1024 --cpu-type=HPI --restore-with-cpu=HPI
time 339.07
exit_status 0
cycles 1176559936
instructions 94222791
cmd ./run --emulator gem5 --arch aarch64 --gem5-readfile "dhrystone 100000" --gem5-restore 1 -- --caches --l2cache --l1d_size=1024kB --l1i_size=1024kB --l2_size=1024kB --l3_size=1024kB --cpu-type=HPI --restore-with-cpu=HPI
time 240.37
exit_status 0
cycles 125666679
instructions 91738770</pre>
</div>
</div>
<div class="paragraph">
<p>We make the following conclusions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the number of instructions almost does not change: the CPU is waiting for memory all the extra time. TODO: why does it change at all?</p>
</li>
<li>
<p>the wall clock execution time is not directionally proportional to the number of cycles: here we had a 10x cycle increase, but only 2x time increase. This suggests that the simulation of cycles in which the CPU is waiting for memory to come back is faster.</p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="gem5-memory-latency"><a class="anchor" href="#gem5-memory-latency"></a><a class="link" href="#gem5-memory-latency">19.2.2.3. gem5 memory latency</a></h5>
<div class="paragraph">
<p>TODO These look promising:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>--list-mem-types
--mem-type=MEM_TYPE
--mem-channels=MEM_CHANNELS
--mem-ranks=MEM_RANKS
--mem-size=MEM_SIZE</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: now to verify this with the Linux kernel? Besides raw performance benchmarks.</p>
</div>
</div>
<div class="sect4">
<h5 id="memory-size"><a class="anchor" href="#memory-size"></a><a class="link" href="#memory-size">19.2.2.4. Memory size</a></h5>
<div class="literalblock">
<div class="content">
<pre>./run --memory 512M</pre>
</div>
</div>
<div class="paragraph">
<p>We can verify this on the guest directly from the kernel with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /proc/meminfo</pre>
</div>
</div>
<div class="paragraph">
<p>as of LKMC 1e969e832f66cb5a72d12d57c53fb09e9721d589 this output contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>MemTotal: 498472 kB</pre>
</div>
</div>
<div class="paragraph">
<p>which we expand with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>printf '0x%X\n' $((498472 * 1024))</pre>
</div>
</div>
<div class="paragraph">
<p>to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0x1E6CA000</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: why is this value a bit smaller than 512M?</p>
</div>
<div class="paragraph">
<p><code>free</code> also gives the same result:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>free -b</pre>
</div>
</div>
<div class="paragraph">
<p>contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> total used free shared buffers cached
Mem: 510435328 20385792 490049536 0 503808 2760704
-/+ buffers/cache: 17121280 493314048
Swap: 0 0 0</pre>
</div>
</div>
<div class="paragraph">
<p>which we expand with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>printf '0x%X\n' 510435328$((498472 * 1024)</pre>
</div>
</div>
<div class="paragraph">
<p><code>man free</code> from Ubuntu&#8217;s procps 3.3.15 tells us that <code>free</code> obtains this information from <code>/proc/meminfo</code> as well.</p>
</div>
<div class="paragraph">
<p>From C, we can get this information with <code>sysconf(_SC_PHYS_PAGES)</code> or <code>get_phys_pages()</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./linux/total_memory.out</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/total_memory.c">userland/linux/total_memory.c</a></p>
</div>
<div class="paragraph">
<p>Output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) = 0x1E6CA000
sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE) = 0x1D178000
get_phys_pages() * sysconf(_SC_PAGESIZE) = 0x1E6CA000
get_avphys_pages() * sysconf(_SC_PAGESIZE) = 0x1D178000</pre>
</div>
</div>
<div class="paragraph">
<p>This is mentioned at: <a href="https://stackoverflow.com/questions/22670257/getting-ram-size-in-c-linux-non-precise-result/22670407#22670407" class="bare">https://stackoverflow.com/questions/22670257/getting-ram-size-in-c-linux-non-precise-result/22670407#22670407</a></p>
</div>
<div class="paragraph">
<p>AV means available and gives the free memory: <a href="https://stackoverflow.com/questions/14386856/c-check-available-ram/57659190#57659190" class="bare">https://stackoverflow.com/questions/14386856/c-check-available-ram/57659190#57659190</a></p>
</div>
</div>
<div class="sect4">
<h5 id="gem5-disk-and-network-latency"><a class="anchor" href="#gem5-disk-and-network-latency"></a><a class="link" href="#gem5-disk-and-network-latency">19.2.2.5. gem5 disk and network latency</a></h5>
<div class="paragraph">
<p>TODO These look promising:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>--ethernet-linkspeed
--ethernet-linkdelay</pre>
</div>
</div>
<div class="paragraph">
<p>and also: <code>gem5-dist</code>: <a href="https://publish.illinois.edu/icsl-pdgem5/" class="bare">https://publish.illinois.edu/icsl-pdgem5/</a></p>
</div>
</div>
<div class="sect4">
<h5 id="gem5-clock-frequency"><a class="anchor" href="#gem5-clock-frequency"></a><a class="link" href="#gem5-clock-frequency">19.2.2.6. gem5 clock frequency</a></h5>
<div class="paragraph">
<p>Clock frequency: TODO how does it affect performance in benchmarks?</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --emulator gem5 -- --cpu-clock 10000000</pre>
</div>
</div>
<div class="paragraph">
<p>Check with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>m5 resetstats
sleep 10
m5 dumpstats</pre>
</div>
</div>
<div class="paragraph">
<p>and then:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gem5-stat --arch aarch64</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: why doesn&#8217;t this exist:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ls /sys/devices/system/cpu/cpu0/cpufreq</pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="interesting-benchmarks"><a class="anchor" href="#interesting-benchmarks"></a><a class="link" href="#interesting-benchmarks">19.2.3. Interesting benchmarks</a></h4>
<div class="paragraph">
<p>Buildroot built-in libraries, mostly under Libraries &gt; Other:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Armadillo <code>C++</code>: linear algebra</p>
</li>
<li>
<p>fftw: Fourier transform</p>
</li>
<li>
<p>Flann</p>
</li>
<li>
<p>GSL: various</p>
</li>
<li>
<p>liblinear</p>
</li>
<li>
<p>libspacialindex</p>
</li>
<li>
<p>libtommath</p>
</li>
<li>
<p>qhull</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>There are not yet enabled, but it should be easy to so, see: <a href="#add-new-buildroot-packages">Section 20.5, &#8220;Add new Buildroot packages&#8221;</a></p>
</div>
<div class="sect4">
<h5 id="dhrystone"><a class="anchor" href="#dhrystone"></a><a class="link" href="#dhrystone">19.2.3.1. Dhrystone</a></h5>
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/Dhrystone" class="bare">https://en.wikipedia.org/wiki/Dhrystone</a></p>
</div>
<div class="paragraph">
<p>Created in the 80&#8217;s, it is not a representative measure of performance in modern computers anymore. It has mostly been replaced by <a href="https://en.wikipedia.org/wiki/SPECint">SPEC</a>, which is&#8230;&#8203; closed source! Unbelievable.</p>
</div>
<div class="paragraph">
<p><a href="#buildroot">Buildroot</a> has a <code>dhrystone</code> package, but because it is so interesting to us, we decided to also build it ourselves, which allows things like static and baremetal compilation more easily.</p>
</div>
<div class="paragraph">
<p>Build and run on QEMU <a href="#user-mode-simulation">User mode simulation</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git submodule update --init submodules/dhrystone
./build-dhrystone --mode userland
./run --userland "$(./getvar userland_build_dir)/submodules/dhrystone/dhrystone"</pre>
</div>
</div>
<div class="paragraph">
<p>Build and run on gem5 use mode:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-dhrystone --mode userland --static --force-rebuild
./run --emulator gem5 --userland "$(./getvar userland_build_dir)/submodules/dhrystone/dhrystone"</pre>
</div>
</div>
<div class="paragraph">
<p>TODO automate run more nicely.</p>
</div>
<div class="paragraph">
<p>Build for <a href="#baremetal">Baremetal</a> execution and run it in baremetal QEMU:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-dhrystone --arch aarch64 --mode baremetal
./run --arch aarch64 --baremetal "$(./getvar baremetal_build_dir)/submodules/dhrystone/dhrystone"</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: fix the build, just need to factor out all run arguments from <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-baremetal">build-baremetal</a> into <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/common.py">common.py</a> and it should just work, no missing syscalls.</p>
</div>
<div class="paragraph">
<p>If you really want the Buildroot package for some reason, build it with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_DHRYSTONE=y'</pre>
</div>
</div>
<div class="paragraph">
<p>and run inside the guest from <code>PATH</code> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>dhrystone</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="bst-vs-heap-vs-hashmap"><a class="anchor" href="#bst-vs-heap-vs-hashmap"></a><a class="link" href="#bst-vs-heap-vs-hashmap">19.2.3.2. BST vs heap vs hashmap</a></h5>
<div class="paragraph">
<p>The following benchmark setup works both:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>on host through timers + <a href="https://stackoverflow.com/questions/51952471/why-do-i-get-a-constant-instead-of-logarithmic-curve-for-an-insert-time-benchmar/51953081#51953081">granule</a></p>
</li>
<li>
<p>gem5 with <a href="#m5ops-instructions">dumpstats</a>, which can get more precise results with <code>granule == 1</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>It has been used to answer:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>BST vs heap: <a href="https://stackoverflow.com/questions/6147243/heap-vs-binary-search-tree-bst/29548834#29548834" class="bare">https://stackoverflow.com/questions/6147243/heap-vs-binary-search-tree-bst/29548834#29548834</a></p>
</li>
<li>
<p><code>std::set</code>: <a href="https://stackoverflow.com/questions/2558153/what-is-the-underlying-data-structure-of-a-stl-set-in-c/51944661#51944661" class="bare">https://stackoverflow.com/questions/2558153/what-is-the-underlying-data-structure-of-a-stl-set-in-c/51944661#51944661</a></p>
</li>
<li>
<p><code>std::map</code>: <a href="https://stackoverflow.com/questions/18414579/what-data-structure-is-inside-stdmap-in-c/51945119#51945119" class="bare">https://stackoverflow.com/questions/18414579/what-data-structure-is-inside-stdmap-in-c/51945119#51945119</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>To benchmark on the host, we do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland-in-tree --force-rebuild --optimization-level 3 ./userland/cpp/bst_vs_heap_vs_hashmap.cpp
./userland/cpp/bst_vs_heap_vs_hashmap.out 10000000 10000 | tee bst_vs_heap_vs_hashmap.dat
gnuplot \
-e 'input_noext="bst_vs_heap_vs_hashmap"' \
-e 'heap_zoom_max=50' \
-e 'hashmap_zoom_max=400' \
./bst-vs-heap-vs-hashmap.gnuplot \
;
xdg-open bst_vs_heap_vs_hashmap.tmp.png</pre>
</div>
</div>
<div class="paragraph">
<p>The parameters <code>heap_zoom_max</code> and <code>hashmap_zoom_max</code> are chosen manually interactively to best showcase the regions of interest in those plots.</p>
</div>
<div class="paragraph">
<p>First we build the benchmark with <a href="#m5ops-instructions">m5ops instructions</a> enabled, and then we run it and extract the stats:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland \
--arch x86_64 \
--ccflags='-DLKMC_M5OPS_ENABLE=1' \
--force-rebuild userland/cpp/bst_vs_heap_vs_hashmap.cpp \
--static \
--optimization-level 3 \
;
./run \
--arch x86_64 \
--emulator gem5 \
--static \
--userland userland/cpp/bst_vs_heap_vs_hashmap.cpp \
--userland-args='100000' \
-- \
--cpu-type=DerivO3CPU \
--caches \
--l2cache \
--l1d_size=32kB \
--l1i_size=32kB \
--l2_size=256kB \
--l3_size=20MB \
;
./bst-vs-heap-vs-hashmap-gem5-stats --arch x86_64 | tee bst_vs_heap_vs_hashmap_gem5.dat
gnuplot \
-e 'input_noext="bst_vs_heap_vs_hashmap_gem5"' \
-e 'heap_zoom_max=500' \
-e 'hashmap_zoom_max=400' \
./bst-vs-heap-vs-hashmap.gnuplot \
;
xdg-open bst_vs_heap_vs_hashmap_gem5.tmp.png</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: the gem5 simulation blows up on a tcmalloc allocation somewhere near 25k elements as of 3fdd83c2c58327d9714fa2347c724b78d7c05e2b + 1, likely linked to the extreme inefficiency of the stats collection?</p>
</div>
<div class="paragraph">
<p>The cache sizes were chosen to match the host <a href="#p51">P51</a> to improve the comparison. Ideally we should also use the same standard library.</p>
</div>
<div class="paragraph">
<p>Note that this will take a long time, and will produce a humongous ~40Gb stats file as explained at: <a href="#gem5-only-dump-selected-stats">Section 19.9.2.1, &#8220;gem5 only dump selected stats&#8221;</a></p>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/bst_vs_heap_vs_hashmap.cpp">userland/cpp/bst_vs_heap_vs_hashmap.cpp</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/bst-vs-heap-vs-hashmap-gem5-stats">bst-vs-heap-vs-hashmap-gem5-stats</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/bst-vs-heap-vs-hashmap.gnuplot">bst-vs-heap-vs-hashmap.gnuplot</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="blas"><a class="anchor" href="#blas"></a><a class="link" href="#blas">19.2.3.3. BLAS</a></h5>
<div class="paragraph">
<p>Buildroot supports it, which makes everything just trivial:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_OPENBLAS=y'
./build-userland --package openblas -- userland/libs/openblas/hello.c
./run --eval-after './libs/openblas/hello.out; echo $?'</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: the test passes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/libs/openblas/hello.c">userland/libs/openblas/hello.c</a></p>
</div>
<div class="paragraph">
<p>The test performs a general matrix multiplication:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> | 1.0 -3.0 | | 1.0 2.0 1.0 | | 0.5 0.5 0.5 | | 11.0 - 9.0 5.0 |
1 * | 2.0 4.0 | * | -3.0 4.0 -1.0 | + 2 * | 0.5 0.5 0.5 | = | - 9.0 21.0 -1.0 |
| 1.0 -1.0 | | 0.5 0.5 0.5 | | 5.0 - 1.0 3.0 |</pre>
</div>
</div>
<div class="paragraph">
<p>This can be deduced from the Fortran interfaces at</p>
</div>
<div class="literalblock">
<div class="content">
<pre>less "$(./getvar buildroot_build_build_dir)"/openblas-*/reference/dgemmf.f</pre>
</div>
</div>
<div class="paragraph">
<p>which we can map to our call as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>C := alpha*op( A )*op( B ) + beta*C,
SUBROUTINE DGEMMF( TRANA, TRANB, M,N,K, ALPHA,A,LDA,B,LDB,BETA,C,LDC)
cblas_dgemm( CblasColMajor, CblasNoTrans, CblasTrans,3,3,2 ,1, A,3, B,3, 2 ,C,3 );</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="eigen"><a class="anchor" href="#eigen"></a><a class="link" href="#eigen">19.2.3.4. Eigen</a></h5>
<div class="paragraph">
<p>Header only linear algebra library with a mainline Buildroot package:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_EIGEN=y'
./build-userland --package eigen -- userland/libs/eigen/hello.cpp</pre>
</div>
</div>
<div class="paragraph">
<p>Just create an array and print it:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after './libs/eigen/hello.out'</pre>
</div>
</div>
<div class="paragraph">
<p>Output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> 3 -1
2.5 1.5</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/libs/eigen/hello.cpp">userland/libs/eigen/hello.cpp</a></p>
</div>
<div class="paragraph">
<p>This example just creates a matrix and prints it out.</p>
</div>
<div class="paragraph">
<p>Tested on: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/commit/a4bdcf102c068762bb1ef26c591fcf71e5907525">a4bdcf102c068762bb1ef26c591fcf71e5907525</a></p>
</div>
</div>
<div class="sect4">
<h5 id="parsec-benchmark"><a class="anchor" href="#parsec-benchmark"></a><a class="link" href="#parsec-benchmark">19.2.3.5. PARSEC benchmark</a></h5>
<div class="paragraph">
<p>We have ported parts of the <a href="http://parsec.cs.princeton.edu">PARSEC benchmark</a> for cross compilation at: <a href="https://github.com/cirosantilli/parsec-benchmark" class="bare">https://github.com/cirosantilli/parsec-benchmark</a> See the documentation on that repo to find out which benchmarks have been ported. Some of the benchmarks were are segfaulting, they are documented in that repo.</p>
</div>
<div class="paragraph">
<p>There are two ways to run PARSEC with this repo:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#parsec-benchmark-without-parsecmgmt">without <code>pasecmgmt</code></a>, most likely what you want</p>
</li>
<li>
<p><a href="#parsec-benchmark-with-parsecmgmt">with <code>pasecmgmt</code></a></p>
</li>
</ul>
</div>
<div class="sect5">
<h6 id="parsec-benchmark-without-parsecmgmt"><a class="anchor" href="#parsec-benchmark-without-parsecmgmt"></a><a class="link" href="#parsec-benchmark-without-parsecmgmt">19.2.3.5.1. PARSEC benchmark without parsecmgmt</a></h6>
<div class="literalblock">
<div class="content">
<pre>./build --arch arm --download-dependencies gem5-buildroot parsec-benchmark
./build-buildroot --arch arm --config 'BR2_PACKAGE_PARSEC_BENCHMARK=y'
./run --arch arm --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>Once inside the guest, launch one of the <code>test</code> input sized benchmarks manually as in:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd /parsec/ext/splash2x/apps/fmm/run
../inst/arm-linux.gcc/bin/fmm 1 &lt; input_1</pre>
</div>
</div>
<div class="paragraph">
<p>To find run out how to run many of the benchmarks, have a look at the <code>test.sh</code> script of the <code>parse-benchmark</code> repo.</p>
</div>
<div class="paragraph">
<p>From the guest, you can also run it as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd /parsec
./test.sh</pre>
</div>
</div>
<div class="paragraph">
<p>but this might be a bit time consuming in gem5.</p>
</div>
</div>
<div class="sect5">
<h6 id="parsec-change-the-input-size"><a class="anchor" href="#parsec-change-the-input-size"></a><a class="link" href="#parsec-change-the-input-size">19.2.3.5.2. PARSEC change the input size</a></h6>
<div class="paragraph">
<p>Running a benchmark of a size different than <code>test</code>, e.g. <code>simsmall</code>, requires a rebuild with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot \
--arch arm \
--config 'BR2_PACKAGE_PARSEC_BENCHMARK=y' \
--config 'BR2_PACKAGE_PARSEC_BENCHMARK_INPUT_SIZE="simsmall"' \
-- parsec_benchmark-reconfigure \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Large input may also require tweaking:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#br2_target_rootfs_ext2_size">BR2_TARGET_ROOTFS_EXT2_SIZE</a> if the unpacked inputs are large</p>
</li>
<li>
<p><a href="#memory-size">Memory size</a>, unless you want to meet the OOM killer, which is admittedly kind of fun</p>
</li>
</ul>
</div>
<div class="paragraph">
<p><code>test.sh</code> only contains the run commands for the <code>test</code> size, and cannot be used for <code>simsmall</code>.</p>
</div>
<div class="paragraph">
<p>The easiest thing to do, is to <a href="https://superuser.com/questions/231002/how-can-i-search-within-the-output-buffer-of-a-tmux-shell/1253137#1253137">scroll up on the host shell</a> after the build, and look for a line of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Running /root/linux-kernel-module-cheat/out/aarch64/buildroot/build/parsec-benchmark-custom/ext/splash2x/apps/ocean_ncp/inst/aarch64-linux.gcc/bin/ocean_ncp -n2050 -p1 -e1e-07 -r20000 -t28800</pre>
</div>
</div>
<div class="paragraph">
<p>and then tweak the command found in <code>test.sh</code> accordingly.</p>
</div>
<div class="paragraph">
<p>Yes, we do run the benchmarks on host just to unpack / generate inputs. They are expected fail to run since they were build for the guest instead of host, including for x86_64 guest which has a different interpreter than the host&#8217;s (see <code>file myexecutable</code>).</p>
</div>
<div class="paragraph">
<p>The rebuild is required because we unpack input files on the host.</p>
</div>
<div class="paragraph">
<p>Separating input sizes also allows to create smaller images when only running the smaller benchmarks.</p>
</div>
<div class="paragraph">
<p>This limitation exists because <code>parsecmgmt</code> generates the input files just before running via the Bash scripts, but we can&#8217;t run <code>parsecmgmt</code> on gem5 as it is too slow!</p>
</div>
<div class="paragraph">
<p>One option would be to do that inside the guest with QEMU.</p>
</div>
<div class="paragraph">
<p>Also, we can&#8217;t generate all input sizes at once, because many of them have the same name and would overwrite one another&#8230;&#8203;</p>
</div>
<div class="paragraph">
<p>PARSEC simply wasn&#8217;t designed with non native machines in mind&#8230;&#8203;</p>
</div>
</div>
<div class="sect5">
<h6 id="parsec-benchmark-with-parsecmgmt"><a class="anchor" href="#parsec-benchmark-with-parsecmgmt"></a><a class="link" href="#parsec-benchmark-with-parsecmgmt">19.2.3.5.3. PARSEC benchmark with parsecmgmt</a></h6>
<div class="paragraph">
<p>Most users won&#8217;t want to use this method because:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>running the <code>parsecmgmt</code> Bash scripts takes forever before it ever starts running the actual benchmarks on gem5</p>
<div class="paragraph">
<p>Running on QEMU is feasible, but not the main use case, since QEMU cannot be used for performance measurements</p>
</div>
</li>
<li>
<p>it requires putting the full <code>.tar</code> inputs on the guest, which makes the image twice as large (1x for the <code>.tar</code>, 1x for the unpacked input files)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>It would be awesome if it were possible to use this method, since this is what Parsec supports officially, and so:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>you don&#8217;t have to dig into what raw command to run</p>
</li>
<li>
<p>there is an easy way to run all the benchmarks in one go to test them out</p>
</li>
<li>
<p>you can just run any of the benchmarks that you want</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>but it simply is not feasible in gem5 because it takes too long.</p>
</div>
<div class="paragraph">
<p>If you still want to run this, try it out with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot \
--arch aarch64 \
--config 'BR2_PACKAGE_PARSEC_BENCHMARK=y' \
--config 'BR2_PACKAGE_PARSEC_BENCHMARK_PARSECMGMT=y' \
--config 'BR2_TARGET_ROOTFS_EXT2_SIZE="3G"' \
-- parsec_benchmark-reconfigure \
;</pre>
</div>
</div>
<div class="paragraph">
<p>And then you can run it just as you would on the host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd /parsec/
bash
. env.sh
parsecmgmt -a run -p splash2x.fmm -i test</pre>
</div>
</div>
</div>
<div class="sect5">
<h6 id="parsec-uninstall"><a class="anchor" href="#parsec-uninstall"></a><a class="link" href="#parsec-uninstall">19.2.3.5.4. PARSEC uninstall</a></h6>
<div class="paragraph">
<p>If you want to remove PARSEC later, Buildroot doesn&#8217;t provide an automated package removal mechanism as mentioned at: <a href="#remove-buildroot-packages">Section 20.6, &#8220;Remove Buildroot packages&#8221;</a>, but the following procedure should be satisfactory:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rm -rf \
"$(./getvar buildroot_download_dir)"/parsec-* \
"$(./getvar buildroot_build_dir)"/build/parsec-* \
"$(./getvar buildroot_build_dir)"/build/packages-file-list.txt \
"$(./getvar buildroot_build_dir)"/images/rootfs.* \
"$(./getvar buildroot_build_dir)"/target/parsec-* \
;
./build-buildroot --arch arm</pre>
</div>
</div>
</div>
<div class="sect5">
<h6 id="parsec-benchmark-hacking"><a class="anchor" href="#parsec-benchmark-hacking"></a><a class="link" href="#parsec-benchmark-hacking">19.2.3.5.5. PARSEC benchmark hacking</a></h6>
<div class="paragraph">
<p>If you end up going inside <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/submodules/parsec-benchmark">submodules/parsec-benchmark</a> to hack up the benchmark (you will!), these tips will be helpful.</p>
</div>
<div class="paragraph">
<p>Buildroot was not designed to deal with large images, and currently cross rebuilds are a bit slow, due to some image generation and validation steps.</p>
</div>
<div class="paragraph">
<p>A few workarounds are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>develop in host first as much as you can. Our PARSEC fork supports it.</p>
<div class="paragraph">
<p>If you do this, don&#8217;t forget to do a:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd "$(./getvar parsec_source_dir)"
git clean -xdf .</pre>
</div>
</div>
<div class="paragraph">
<p>before going for the cross compile build.</p>
</div>
</li>
<li>
<p>patch Buildroot to work well, and keep cross compiling all the way. This should be totally viable, and we should do it.</p>
<div class="paragraph">
<p>Don&#8217;t forget to explicitly rebuild PARSEC with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot \
--arch arm \
--config 'BR2_PACKAGE_PARSEC_BENCHMARK=y' \
-- parsec_benchmark-reconfigure \
;</pre>
</div>
</div>
<div class="paragraph">
<p>You may also want to test if your patches are still functionally correct inside of QEMU first, which is a faster emulator.</p>
</div>
</li>
<li>
<p>sell your soul, and compile natively inside the guest. We won&#8217;t do this, not only because it is evil, but also because Buildroot explicitly does not support it: <a href="https://buildroot.org/downloads/manual/manual.html#faq-no-compiler-on-target" class="bare">https://buildroot.org/downloads/manual/manual.html#faq-no-compiler-on-target</a> ARM employees have been known to do this: <a href="https://github.com/arm-university/arm-gem5-rsk/blob/aa3b51b175a0f3b6e75c9c856092ae0c8f2a7cdc/parsec_patches/qemu-patch.diff" class="bare">https://github.com/arm-university/arm-gem5-rsk/blob/aa3b51b175a0f3b6e75c9c856092ae0c8f2a7cdc/parsec_patches/qemu-patch.diff</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-kernel-command-line-parameters"><a class="anchor" href="#gem5-kernel-command-line-parameters"></a><a class="link" href="#gem5-kernel-command-line-parameters">19.3. gem5 kernel command line parameters</a></h3>
<div class="paragraph">
<p>Analogous <a href="#kernel-command-line-parameters">to QEMU</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --kernel-cli 'init=/lkmc/linux/poweroff.out' --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>Internals: when we give <code>--command-line=</code> to gem5, it overrides default command lines, including some mandatory ones which are required to boot properly.</p>
</div>
<div class="paragraph">
<p>Our run script hardcodes the require options in the default <code>--command-line</code> and appends extra options given by <code>-e</code>.</p>
</div>
<div class="paragraph">
<p>To find the default options in the first place, we removed <code>--command-line</code> and ran:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>and then looked at the line of the Linux kernel that starts with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Kernel command line:</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-gdb"><a class="anchor" href="#gem5-gdb"></a><a class="link" href="#gem5-gdb">19.4. gem5 GDB step debug</a></h3>
<div class="sect3">
<h4 id="gem5-gdb-step-debug-kernel"><a class="anchor" href="#gem5-gdb-step-debug-kernel"></a><a class="link" href="#gem5-gdb-step-debug-kernel">19.4.1. gem5 GDB step debug kernel</a></h4>
<div class="paragraph">
<p>Analogous <a href="#gdb">to QEMU</a>, on the first shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --emulator gem5 --gdb-wait</pre>
</div>
</div>
<div class="paragraph">
<p>On the second shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --arch arm --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>On a third shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gem5-shell</pre>
</div>
</div>
<div class="paragraph">
<p>When you want to break, just do a <code>Ctrl-C</code> on GDB shell, and then <code>continue</code>.</p>
</div>
<div class="paragraph">
<p>And we now see the boot messages, and then get a shell. Now try the <code>./count.sh</code> procedure described for QEMU at: <a href="#gdb-step-debug-kernel-post-boot">Section 2.2, &#8220;GDB step debug kernel post-boot&#8221;</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-gdb-step-debug-userland-process"><a class="anchor" href="#gem5-gdb-step-debug-userland-process"></a><a class="link" href="#gem5-gdb-step-debug-userland-process">19.4.2. gem5 GDB step debug userland process</a></h4>
<div class="paragraph">
<p>We are unable to use <code>gdbserver</code> because of networking as mentioned at: <a href="#gem5-host-to-guest-networking">Section 14.3.1.3, &#8220;gem5 host to guest networking&#8221;</a></p>
</div>
<div class="paragraph">
<p>The alternative is to do as in <a href="#gdb-step-debug-userland-processes">GDB step debug userland processes</a>.</p>
</div>
<div class="paragraph">
<p>Next, follow the exact same steps explained at <a href="#gdb-step-debug-userland-non-init-without-gdb-wait">GDB step debug userland non-init without --gdb-wait</a>, but passing <code>--emulator gem5</code> to every command as usual.</p>
</div>
<div class="paragraph">
<p>But then TODO (I&#8217;ll still go crazy one of those days): for <code>arm</code>, while debugging <code>./linux/myinsmod.out hello.ko</code>, after then line:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>23 if (argc &lt; 3) {
24 params = "";</pre>
</div>
</div>
<div class="paragraph">
<p>I press <code>n</code>, it just runs the program until the end, instead of stopping on the next line of execution. The module does get inserted normally.</p>
</div>
<div class="paragraph">
<p>TODO:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --arch arm --emulator gem5 --userland gem5-1.0/gem5/util/m5/m5 main</pre>
</div>
</div>
<div class="paragraph">
<p>breaks when <code>m5</code> is run on guest, but does not show the source code.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-checkpoint"><a class="anchor" href="#gem5-checkpoint"></a><a class="link" href="#gem5-checkpoint">19.5. gem5 checkpoint</a></h3>
<div class="paragraph">
<p>Analogous to QEMU&#8217;s <a href="#snapshot">Snapshot</a>, but better since it can be started from inside the guest, so we can easily checkpoint after a specific guest event, e.g. just before <code>init</code> is done.</p>
</div>
<div class="paragraph">
<p>Documentation: <a href="http://gem5.org/Checkpoints" class="bare">http://gem5.org/Checkpoints</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>In the guest, wait for the boot to end and run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>m5 checkpoint</pre>
</div>
</div>
<div class="paragraph">
<p>where <a href="#m5">m5</a> is a guest utility present inside the gem5 tree which we cross-compiled and installed into the guest.</p>
</div>
<div class="paragraph">
<p>To restore the checkpoint, kill the VM and run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --emulator gem5 --gem5-restore 1</pre>
</div>
</div>
<div class="paragraph">
<p>The <code>--gem5-restore</code> option restores the checkpoint that was created most recently.</p>
</div>
<div class="paragraph">
<p>Let&#8217;s create a second checkpoint to see how it works, in guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>date &gt;f
m5 checkpoint</pre>
</div>
</div>
<div class="paragraph">
<p>Kill the VM, and try it out:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --emulator gem5 --gem5-restore 1</pre>
</div>
</div>
<div class="paragraph">
<p>Here we use <code>--gem5-restore 1</code> again, since the second snapshot we took is now the most recent one</p>
</div>
<div class="paragraph">
<p>Now in the guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat f</pre>
</div>
</div>
<div class="paragraph">
<p>contains the <code>date</code>. The file <code>f</code> wouldn&#8217;t exist had we used the first checkpoint with <code>--gem5-restore 2</code>, which is the second most recent snapshot taken.</p>
</div>
<div class="paragraph">
<p>If you automate things with <a href="#kernel-command-line-parameters">Kernel command line parameters</a> as in:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --eval 'm5 checkpoint;m5 resetstats;dhrystone 1000;m5 exit' --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>Then there is no need to pass the kernel command line again to gem5 for replay:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --emulator gem5 --gem5-restore 1</pre>
</div>
</div>
<div class="paragraph">
<p>since boot has already happened, and the parameters are already in the RAM of the snapshot.</p>
</div>
<div class="sect3">
<h4 id="gem5-checkpoint-internals"><a class="anchor" href="#gem5-checkpoint-internals"></a><a class="link" href="#gem5-checkpoint-internals">19.5.1. gem5 checkpoint internals</a></h4>
<div class="paragraph">
<p>Checkpoints are stored inside the <a href="#m5out-directory">m5out directory</a> at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>"$(./getvar --emulator gem5 m5out_dir)/cpt.&lt;checkpoint-time&gt;"</pre>
</div>
</div>
<div class="paragraph">
<p>where <code>&lt;checkpoint-time&gt;</code> is the cycle number at which the checkpoint was taken.</p>
</div>
<div class="paragraph">
<p><code>fs.py</code> exposes the <code>-r N</code> flag to restore checkpoints, which N-th checkpoint with the largest <code>&lt;checkpoint-time&gt;</code>: <a href="https://github.com/gem5/gem5/blob/e02ec0c24d56bce4a0d8636a340e15cd223d1930/configs/common/Simulation.py#L118" class="bare">https://github.com/gem5/gem5/blob/e02ec0c24d56bce4a0d8636a340e15cd223d1930/configs/common/Simulation.py#L118</a></p>
</div>
<div class="paragraph">
<p>However, that interface is bad because if you had taken previous checkpoints, you have no idea what <code>N</code> to use, unless you memorize which checkpoint was taken at which cycle.</p>
</div>
<div class="paragraph">
<p>Therefore, just use our superior <code>--gem5-restore</code> flag, which uses directory timestamps to determine which checkpoint you created most recently.</p>
</div>
<div class="paragraph">
<p>The <code>-r N</code> integer value is just pure <code>fs.py</code> sugar, the backend at <code>m5.instantiate</code> just takes the actual tracepoint directory path as input.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-restore-new-script"><a class="anchor" href="#gem5-restore-new-script"></a><a class="link" href="#gem5-restore-new-script">19.5.2. gem5 checkpoint restore and run a different script</a></h4>
<div class="paragraph">
<p>You want to automate running several tests from a single pristine post-boot state.</p>
</div>
<div class="paragraph">
<p>The problem is that boot takes forever, and after the checkpoint, the memory and disk states are fixed, so you can&#8217;t for example:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>hack up an existing rc script, since the disk is fixed</p>
</li>
<li>
<p>inject new kernel boot command line options, since those have already been put into memory by the bootloader</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>There is however a few loopholes, <a href="#m5-readfile">m5 readfile</a> being the simplest, as it reads whatever is present on the host.</p>
</div>
<div class="paragraph">
<p>So we can do it like:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># Boot, checkpoint and exit.
printf 'echo "setup run";m5 exit' &gt; "$(./getvar gem5_readfile_file)"
./run --emulator gem5 --eval 'm5 checkpoint;m5 readfile &gt; a.sh;sh a.sh'
# Restore and run the first benchmark.
printf 'echo "first benchmark";m5 exit' &gt; "$(./getvar gem5_readfile_file)"
./run --emulator gem5 --gem5-restore 1
# Restore and run the second benchmark.
printf 'echo "second benchmark";m5 exit' &gt; "$(./getvar gem5_readfile_file)"
./run --emulator gem5 --gem5-restore 1
# If something weird happened, create an interactive shell to examine the system.
printf 'sh' &gt; "$(./getvar gem5_readfile_file)"
./run --emulator gem5 --gem5-restore 1</pre>
</div>
</div>
<div class="paragraph">
<p>Since this is such a common setup, we provide the following helpers for this operation:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/gem5.sh">rootfs_overlay/lkmc/gem5.sh</a>. This script is analogous to gem5&#8217;s in-tree <a href="https://github.com/gem5/gem5/blob/2b4b94d0556c2d03172ebff63f7fc502c3c26ff8/configs/boot/hack_back_ckpt.rcS">hack_back_ckpt.rcS</a>, but with less noise.</p>
</li>
<li>
<p><code>./run --gem5-readfile</code> is a convenient way to set the <code>m5 readfile</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Their usage us exemplified at <a href="#gem5-run-benchmark">gem5 run benchmark</a>.</p>
</div>
<div class="paragraph">
<p>Other loophole possibilities include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#9p">9P</a></p>
</li>
<li>
<p><a href="#secondary-disk">Secondary disk</a></p>
</li>
<li>
<p><code>expect</code> as mentioned at: <a href="https://stackoverflow.com/questions/7013137/automating-telnet-session-using-bash-scripts" class="bare">https://stackoverflow.com/questions/7013137/automating-telnet-session-using-bash-scripts</a></p>
<div class="literalblock">
<div class="content">
<pre>#!/usr/bin/expect
spawn telnet localhost 3456
expect "# $"
send "pwd\r"
send "ls /\r"
send "m5 exit\r"
expect eof</pre>
</div>
</div>
<div class="paragraph">
<p>This is ugly however as it is not deterministic.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p><a href="https://www.mail-archive.com/gem5-users@gem5.org/msg15233.html" class="bare">https://www.mail-archive.com/gem5-users@gem5.org/msg15233.html</a></p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-restore-checkpoint-with-a-different-cpu"><a class="anchor" href="#gem5-restore-checkpoint-with-a-different-cpu"></a><a class="link" href="#gem5-restore-checkpoint-with-a-different-cpu">19.5.3. gem5 restore checkpoint with a different CPU</a></h4>
<div class="paragraph">
<p>gem5 can switch to a different CPU model when restoring a checkpoint.</p>
</div>
<div class="paragraph">
<p>A common combo is to boot Linux with a fast CPU, make a checkpoint and then replay the benchmark of interest with a slower CPU.</p>
</div>
<div class="paragraph">
<p>An illustrative interactive run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>In guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>m5 checkpoint</pre>
</div>
</div>
<div class="paragraph">
<p>And then restore the checkpoint with a different CPU:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --emulator gem5 --gem5-restore 1 -- --caches --restore-with-cpu=HPI</pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="pass-extra-options-to-gem5"><a class="anchor" href="#pass-extra-options-to-gem5"></a><a class="link" href="#pass-extra-options-to-gem5">19.6. Pass extra options to gem5</a></h3>
<div class="paragraph">
<p>Remember that in the gem5 command line, we can either pass options to the script being run as in:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>build/X86/gem5.opt configs/examples/fs.py --some-option</pre>
</div>
</div>
<div class="paragraph">
<p>or to the gem5 executable itself:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>build/X86/gem5.opt --some-option configs/examples/fs.py</pre>
</div>
</div>
<div class="paragraph">
<p>Pass options to the script in our setup use:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>get help:</p>
<div class="literalblock">
<div class="content">
<pre>./run --emulator gem5 -- -h</pre>
</div>
</div>
</li>
<li>
<p>boot with the more detailed and slow <code>HPI</code> CPU model:</p>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --emulator gem5 -- --caches --cpu-type=HPI</pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>To pass options to the <code>gem5</code> executable we expose the <code>--gem5-exe-args</code> option:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>get help:</p>
<div class="literalblock">
<div class="content">
<pre>./run --gem5-exe-args='-h' --emulator gem5</pre>
</div>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="m5ops"><a class="anchor" href="#m5ops"></a><a class="link" href="#m5ops">19.7. m5ops</a></h3>
<div class="paragraph">
<p>m5ops are magic instructions which lead gem5 to do magic things, like quitting or dumping stats.</p>
</div>
<div class="paragraph">
<p>Documentation: <a href="http://gem5.org/M5ops" class="bare">http://gem5.org/M5ops</a></p>
</div>
<div class="paragraph">
<p>There are two main ways to use m5ops:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#m5">m5</a></p>
</li>
<li>
<p><a href="#m5ops-instructions">m5ops instructions</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><code>m5</code> is convenient if you only want to take snapshots before or after the benchmark, without altering its source code. It uses the <a href="#m5ops-instructions">m5ops instructions</a> as its backend.</p>
</div>
<div class="paragraph">
<p><code>m5</code> cannot should / should not be used however:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>in bare metal setups</p>
</li>
<li>
<p>when you want to call the instructions from inside interest points of your benchmark. Otherwise you add the syscall overhead to the benchmark, which is more intrusive and might affect results.</p>
<div class="paragraph">
<p>Why not just hardcode some <a href="#m5ops-instructions">m5ops instructions</a> as in our example instead, since you are going to modify the source of the benchmark anyways?</p>
</div>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="m5"><a class="anchor" href="#m5"></a><a class="link" href="#m5">19.7.1. m5</a></h4>
<div class="paragraph">
<p><code>m5</code> is a guest command line utility that is installed and run on the guest, that serves as a CLI front-end for the <a href="#m5ops">m5ops</a></p>
</div>
<div class="paragraph">
<p>Its source is present in the gem5 tree: <a href="https://github.com/gem5/gem5/blob/6925bf55005c118dc2580ba83e0fa10b31839ef9/util/m5/m5.c" class="bare">https://github.com/gem5/gem5/blob/6925bf55005c118dc2580ba83e0fa10b31839ef9/util/m5/m5.c</a></p>
</div>
<div class="paragraph">
<p>It is possible to guess what most tools do from the corresponding <a href="#m5ops">m5ops</a>, but let&#8217;s at least document the less obvious ones here.</p>
</div>
<div class="sect4">
<h5 id="m5-exit"><a class="anchor" href="#m5-exit"></a><a class="link" href="#m5-exit">19.7.1.1. m5 exit</a></h5>
<div class="paragraph">
<p>End the simulation.</p>
</div>
<div class="paragraph">
<p>Sane Python scripts will exit gem5 with status 0, which is what <code>fs.py</code> does.</p>
</div>
</div>
<div class="sect4">
<h5 id="m5-fail"><a class="anchor" href="#m5-fail"></a><a class="link" href="#m5-fail">19.7.1.2. m5 fail</a></h5>
<div class="paragraph">
<p>End the simulation with a failure exit event:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>m5 fail 1</pre>
</div>
</div>
<div class="paragraph">
<p>Sane Python scripts would use that as the exit status of gem5, which would be useful for testing purposes, but <code>fs.py</code> at 200281b08ca21f0d2678e23063f088960d3c0819 just prints an error message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Simulated exit code not 0! Exit code is 1</pre>
</div>
</div>
<div class="paragraph">
<p>and exits with status 0.</p>
</div>
<div class="paragraph">
<p>We then parse that string ourselves in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/run">run</a> and exit with the correct status&#8230;&#8203;</p>
</div>
<div class="paragraph">
<p>TODO: it used to be like that, but it actually got changed to just print the message. Why? <a href="https://gem5-review.googlesource.com/c/public/gem5/+/4880" class="bare">https://gem5-review.googlesource.com/c/public/gem5/+/4880</a></p>
</div>
<div class="paragraph">
<p><code>m5 fail</code> is just a superset of <code>m5 exit</code>, which is just:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>m5 fail 0</pre>
</div>
</div>
<div class="paragraph">
<p>as can be seen from the source: <a href="https://github.com/gem5/gem5/blob/50a57c0376c02c912a978c4443dd58caebe0f173/src/sim/pseudo_inst.cc#L303" class="bare">https://github.com/gem5/gem5/blob/50a57c0376c02c912a978c4443dd58caebe0f173/src/sim/pseudo_inst.cc#L303</a></p>
</div>
</div>
<div class="sect4">
<h5 id="m5-writefile"><a class="anchor" href="#m5-writefile"></a><a class="link" href="#m5-writefile">19.7.1.3. m5 writefile</a></h5>
<div class="paragraph">
<p>Send a guest file to the host. <a href="#9p">9P</a> is a more advanced alternative.</p>
</div>
<div class="paragraph">
<p>Guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo mycontent &gt; myfileguest
m5 writefile myfileguest myfilehost</pre>
</div>
</div>
<div class="paragraph">
<p>Host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat "$(./getvar --arch aarch64 --emulator gem5 m5out_dir)/myfilehost"</pre>
</div>
</div>
<div class="paragraph">
<p>Does not work for subdirectories, gem5 crashes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>m5 writefile myfileguest mydirhost/myfilehost</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="m5-readfile"><a class="anchor" href="#m5-readfile"></a><a class="link" href="#m5-readfile">19.7.1.4. m5 readfile</a></h5>
<div class="paragraph">
<p>Read a host file pointed to by the <code>fs.py --script</code> option to stdout.</p>
</div>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/49516399/how-to-use-m5-readfile-and-m5-execfile-in-gem5/49538051#49538051" class="bare">https://stackoverflow.com/questions/49516399/how-to-use-m5-readfile-and-m5-execfile-in-gem5/49538051#49538051</a></p>
</div>
<div class="paragraph">
<p>Host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>date &gt; "$(./getvar gem5_readfile_file)"</pre>
</div>
</div>
<div class="paragraph">
<p>Guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>m5 readfile</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome: date shows on guest.</p>
</div>
</div>
<div class="sect4">
<h5 id="m5-initparam"><a class="anchor" href="#m5-initparam"></a><a class="link" href="#m5-initparam">19.7.1.5. m5 initparam</a></h5>
<div class="paragraph">
<p>Ermm, just another <a href="#m5-readfile">m5 readfile</a> that only takes integers and only from CLI options? Is this software so redundant?</p>
</div>
<div class="paragraph">
<p>Host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --emulator gem5 --gem5-restore 1 -- --initparam 13
./run --emulator gem5 --gem5-restore 1 -- --initparam 42</pre>
</div>
</div>
<div class="paragraph">
<p>Guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>m5 initparm</pre>
</div>
</div>
<div class="paragraph">
<p>Outputs the given paramter.</p>
</div>
</div>
<div class="sect4">
<h5 id="m5-execfile"><a class="anchor" href="#m5-execfile"></a><a class="link" href="#m5-execfile">19.7.1.6. m5 execfile</a></h5>
<div class="paragraph">
<p>Trivial combination of <code>m5 readfile</code> + execute the script.</p>
</div>
<div class="paragraph">
<p>Host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>printf '#!/bin/sh
echo asdf
' &gt; "$(./getvar gem5_readfile_file)"</pre>
</div>
</div>
<div class="paragraph">
<p>Guest:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>touch /tmp/execfile
chmod +x /tmp/execfile
m5 execfile</pre>
</div>
</div>
<div class="paragraph">
<p>Outcome:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>adsf</pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="m5ops-instructions"><a class="anchor" href="#m5ops-instructions"></a><a class="link" href="#m5ops-instructions">19.7.2. m5ops instructions</a></h4>
<div class="paragraph">
<p>gem5 allocates some magic instructions on unused instruction encodings for convenient guest instrumentation.</p>
</div>
<div class="paragraph">
<p>Those instructions are exposed through the <a href="#m5">m5</a> in tree executable.</p>
</div>
<div class="paragraph">
<p>To make things simpler to understand, you can play around with our own minimized educational <code>m5</code> subset <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/m5ops.c">userland/c/m5ops.c</a>.</p>
</div>
<div class="paragraph">
<p>The instructions used by <code>./c/m5ops.out</code> are present in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/m5ops.h">lkmc/m5ops.h</a> in a very simple to understand and reuse inline assembly form.</p>
</div>
<div class="paragraph">
<p>To use that file, first rebuild <code>m5ops.out</code> with the m5ops instructions enabled and install it on the root filesystem:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland \
--arch aarch64 \
--ccflags='-DLKMC_M5OPS_ENABLE=1' \
--force-rebuild \
--static \
userland/c/m5ops.c \
;
./build-buildroot --arch aarch64</pre>
</div>
</div>
<div class="paragraph">
<p>We don&#8217;t enable <code>-DLKMC_M5OPS_ENABLE=1</code> by default on userland executables because we try to use a single image for both gem5, QEMU and <a href="#userland-setup-getting-started-natively">native</a>, and those instructions would break the latter two. We enable it in the <a href="#baremetal-setup">Baremetal setup</a> by default since we already have different images for QEMU and gem5 there.</p>
</div>
<div class="paragraph">
<p>Then, from inside <a href="#gem5-buildroot-setup">gem5 Buildroot setup</a>, test it out with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># checkpoint
./c/m5ops.out c
# dumpstats
./c/m5ops.out d
# exit
./c/m5ops.out e
# dump resetstats
./c/m5ops.out r</pre>
</div>
</div>
<div class="paragraph">
<p>In theory, the cleanest way to add m5ops to your benchmarks would be to do exactly what the <code>m5</code> tool does:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>include <a href="https://github.com/gem5/gem5/blob/05c4c2b566ce351ab217b2bd7035562aa7a76570/include/gem5/asm/generic/m5ops.h"><code>include/gem5/asm/generic/m5ops.h</code></a></p>
</li>
<li>
<p>link with the <code>.o</code> file under <code>util/m5</code> for the correct arch, e.g. <code>m5op_arm_A64.o</code> for aarch64.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>However, I think it is usually not worth the trouble of hacking up the build system of the benchmark to do this, and I recommend just hardcoding in a few raw instructions here and there, and managing it with version control + <code>sed</code>.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/56506154/how-to-analyze-only-interest-area-in-source-code-by-using-gem5/56506419#56506419" class="bare">https://stackoverflow.com/questions/56506154/how-to-analyze-only-interest-area-in-source-code-by-using-gem5/56506419#56506419</a></p>
</li>
<li>
<p><a href="https://www.mail-archive.com/gem5-users@gem5.org/msg15418.html" class="bare">https://www.mail-archive.com/gem5-users@gem5.org/msg15418.html</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="m5ops-instructions-interface"><a class="anchor" href="#m5ops-instructions-interface"></a><a class="link" href="#m5ops-instructions-interface">19.7.2.1. m5ops instructions interface</a></h5>
<div class="paragraph">
<p>Let&#8217;s study how <a href="#m5">m5</a> uses them:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/gem5/gem5/blob/05c4c2b566ce351ab217b2bd7035562aa7a76570/include/gem5/asm/generic/m5ops.h"><code>include/gem5/asm/generic/m5ops.h</code></a>: defines the magic constants that represent the instructions</p>
</li>
<li>
<p><a href="https://github.com/gem5/gem5/blob/05c4c2b566ce351ab217b2bd7035562aa7a76570/util/m5/m5op_arm_A64.S"><code>util/m5/m5op_arm_A64.S</code></a>: use the magic constants that represent the instructions using C preprocessor magic</p>
</li>
<li>
<p><a href="https://github.com/gem5/gem5/blob/05c4c2b566ce351ab217b2bd7035562aa7a76570/util/m5/m5.c"><code>util/m5/m5.c</code></a>: the actual executable. Gets linked to <code>m5op_arm_A64.S</code> which defines a function for each m5op.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We notice that there are two different implementations for each arch:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>magic instructions, which don&#8217;t exist in the corresponding arch</p>
</li>
<li>
<p>magic memory addresses on a given page</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>TODO: what is the advantage of magic memory addresses? Because you have to do more setup work by telling the kernel never to touch the magic page. For the magic instructions, the only thing that could go wrong is if you run some crazy kind of fuzzing workload that generates random instructions.</p>
</div>
<div class="paragraph">
<p>Then, in aarch64 magic instructions for example, the lines:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>.macro m5op_func, name, func, subfunc
.globl \name
\name:
.long 0xff000110 | (\func &lt;&lt; 16) | (\subfunc &lt;&lt; 12)
ret</pre>
</div>
</div>
<div class="paragraph">
<p>define a simple function function for each m5op. Here we see that:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>0xff000110</code> is a base mask for the magic non-existing instruction</p>
</li>
<li>
<p><code>\func</code> and <code>\subfunc</code> are OR-applied on top of the base mask, and define m5op this is.</p>
<div class="paragraph">
<p>Those values will loop over the magic constants defined in <code>m5ops.h</code> with the deferred preprocessor idiom.</p>
</div>
<div class="paragraph">
<p>For example, <code>exit</code> is <code>0x21</code> due to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>#define M5OP_EXIT 0x21</pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Finally, <code>m5.c</code> calls the defined functions as in:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>m5_exit(ints[0]);</pre>
</div>
</div>
<div class="paragraph">
<p>Therefore, the runtime "argument" that gets passed to the instruction, e.g. the delay in ticks until the exit for <code>m5 exit</code>, gets passed directly through the <a href="https://en.wikipedia.org/wiki/Calling_convention#ARM_(A64)">aarch64 calling convention</a>.</p>
</div>
<div class="paragraph">
<p>Keep in mind that for all archs, <code>m5.c</code> does the calls with 64-bit integers:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>uint64_t ints[2] = {0,0};
parse_int_args(argc, argv, ints, argc);
m5_fail(ints[1], ints[0]);</pre>
</div>
</div>
<div class="paragraph">
<p>Therefore, for example:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>aarch64 uses <code>x0</code> for the first argument and <code>x1</code> for the second, since each is 64 bits log already</p>
</li>
<li>
<p>arm uses <code>r0</code> and <code>r1</code> for the first argument, and <code>r2</code> and <code>r3</code> for the second, since each register is only 32 bits long</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>That convention specifies that <code>x0</code> to <code>x7</code> contain the function arguments, so <code>x0</code> contains the first argument, and <code>x1</code> the second.</p>
</div>
<div class="paragraph">
<p>In our <code>m5ops</code> example, we just hardcode everything in the assembly one-liners we are producing.</p>
</div>
<div class="paragraph">
<p>We ignore the <code>\subfunc</code> since it is always 0 on the ops that interest us.</p>
</div>
</div>
<div class="sect4">
<h5 id="m5op-annotations"><a class="anchor" href="#m5op-annotations"></a><a class="link" href="#m5op-annotations">19.7.2.2. m5op annotations</a></h5>
<div class="paragraph">
<p><code>include/gem5/asm/generic/m5ops.h</code> also describes some annotation instructions.</p>
</div>
<div class="paragraph">
<p>What they mean: <a href="https://stackoverflow.com/questions/50583962/what-are-the-gem5-annotations-mops-magic-instructions-and-how-to-use-them" class="bare">https://stackoverflow.com/questions/50583962/what-are-the-gem5-annotations-mops-magic-instructions-and-how-to-use-them</a></p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-arm-linux-kernel-patches"><a class="anchor" href="#gem5-arm-linux-kernel-patches"></a><a class="link" href="#gem5-arm-linux-kernel-patches">19.8. gem5 arm Linux kernel patches</a></h3>
<div class="paragraph">
<p><a href="https://gem5.googlesource.com/arm/linux/" class="bare">https://gem5.googlesource.com/arm/linux/</a> contains an ARM Linux kernel forks with a few gem5 specific Linux kernel patches on top of mainline created by ARM Holdings on top of a few upstream kernel releases.</p>
</div>
<div class="paragraph">
<p>The patches are optional: the vanilla kernel does boot. But they add some interesting gem5-specific optimizations, instrumentations and device support.</p>
</div>
<div class="paragraph">
<p>The patches also <a href="#notable-alternate-gem5-kernel-configs">add defconfigs</a> that are known to work well with gem5.</p>
</div>
<div class="paragraph">
<p>E.g. for arm v4.9 there is: <a href="https://gem5.googlesource.com/arm/linux/+/917e007a4150d26a0aa95e4f5353ba72753669c7/arch/arm/configs/gem5_defconfig" class="bare">https://gem5.googlesource.com/arm/linux/+/917e007a4150d26a0aa95e4f5353ba72753669c7/arch/arm/configs/gem5_defconfig</a>.</p>
</div>
<div class="paragraph">
<p>In order to use those patches and their associated configs, and, we recommend using <a href="#linux-kernel-build-variants">Linux kernel build variants</a> as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git -C "$(./getvar linux_source_dir)" fetch https://gem5.googlesource.com/arm/linux gem5/v4.15:gem5/v4.15
git -C "$(./getvar linux_source_dir)" checkout gem5/v4.15
./build-linux \
--arch aarch64 \
--custom-config-file-gem5 \
--linux-build-id gem5-v4.15 \
;
git -C "$(./getvar linux_source_dir)" checkout -
./run \
--arch aarch64 \
--emulator gem5 \
--linux-build-id gem5-v4.15 \
;</pre>
</div>
</div>
<div class="paragraph">
<p>QEMU also boots that kernel successfully:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--linux-build-id gem5-v4.15 \
;</pre>
</div>
</div>
<div class="paragraph">
<p>but glibc kernel version checks make init fail with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>FATAL: kernel too old</pre>
</div>
</div>
<div class="paragraph">
<p>because glibc was built to expect a newer Linux kernel as shown at: <a href="#fatal-kernel-too-old">Section 10.4.1, &#8220;FATAL: kernel too old&#8221;</a>. Your choices to sole this are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>see if there is a more recent gem5 kernel available, or port your patch of interest to the newest kernel</p>
</li>
<li>
<p>modify this repo to use <a href="#libc-choice">uClibc</a>, which is not hard because of Buildroot</p>
</li>
<li>
<p>patch glibc to remove that check, which is easy because glibc is in a submodule of this repo</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>It is obviously not possible to understand what they actually do from their commit message, so let&#8217;s explain them one by one here as we understand them:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>drm: Add component-aware simple encoder</code> allows you to see images through VNC, see: <a href="#gem5-graphic-mode">Section 13.3, &#8220;gem5 graphic mode&#8221;</a></p>
</li>
<li>
<p><code>gem5: Add support for gem5&#8217;s extended GIC mode</code> adds support for more than 8 cores, see: <a href="#gem5-arm-full-system-with-more-than-8-cores">Section 19.2.2.1.4, &#8220;gem5 ARM full system with more than 8 cores&#8221;</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Tested on 649d06d6758cefd080d04dc47fd6a5a26a620874 + 1.</p>
</div>
<div class="sect3">
<h4 id="gem5-arm-linux-kernel-patches-boot-speedup"><a class="anchor" href="#gem5-arm-linux-kernel-patches-boot-speedup"></a><a class="link" href="#gem5-arm-linux-kernel-patches-boot-speedup">19.8.1. gem5 arm Linux kernel patches boot speedup</a></h4>
<div class="paragraph">
<p>We have observed that with the kernel patches, boot is 2x faster, falling from 1m40s to 50s.</p>
</div>
<div class="paragraph">
<p>With <a href="https://stackoverflow.com/questions/49797246/how-to-monitor-for-how-much-time-each-line-of-stdout-was-the-last-output-line-in/49797547#49797547"><code>ts</code></a>, we see that a large part of the difference is at the message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>clocksource: Switched to clocksource arch_sys_counter</pre>
</div>
</div>
<div class="paragraph">
<p>which takes 4s on the patched kernel, and 30s on the unpatched one! TODO understand why, especially if it is a config difference, or if it actually comes from a patch.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="m5out-directory"><a class="anchor" href="#m5out-directory"></a><a class="link" href="#m5out-directory">19.9. m5out directory</a></h3>
<div class="paragraph">
<p>When you run gem5, it generates an <code>m5out</code> directory at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo $(./getvar --arch arm --emulator gem5 m5out_dir)"</pre>
</div>
</div>
<div class="paragraph">
<p>The location of that directory can be set with <code>./gem5.opt -d</code>, and defaults to <code>./m5out</code>.</p>
</div>
<div class="paragraph">
<p>The files in that directory contains some very important information about the run, and you should become familiar with every one of them.</p>
</div>
<div class="sect3">
<h4 id="gem5-m5out-system-terminal-file"><a class="anchor" href="#gem5-m5out-system-terminal-file"></a><a class="link" href="#gem5-m5out-system-terminal-file">19.9.1. gem5 m5out/system.terminal file</a></h4>
<div class="paragraph">
<p>Contains UART output, both from the Linux kernel or from the baremetal system.</p>
</div>
<div class="paragraph">
<p>Can also be seen live on <a href="#m5term">m5term</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-m5out-stats-txt-file"><a class="anchor" href="#gem5-m5out-stats-txt-file"></a><a class="link" href="#gem5-m5out-stats-txt-file">19.9.2. gem5 m5out/stats.txt file</a></h4>
<div class="paragraph">
<p>This file contains important statistics about the run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat "$(./getvar --arch aarch64 m5out_dir)/stats.txt"</pre>
</div>
</div>
<div class="paragraph">
<p>Whenever we run <code>m5 dumpstats</code> or <code>m5 exit</code>, a section with the following format is added to that file:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>---------- Begin Simulation Statistics ----------
[the stats]
---------- End Simulation Statistics ----------</pre>
</div>
</div>
<div class="paragraph">
<p>That file contains several important execution metrics, e.g. number of cycles and several types of cache misses:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>system.cpu.numCycles
system.cpu.dtb.inst_misses
system.cpu.dtb.inst_hits</pre>
</div>
</div>
<div class="paragraph">
<p>For x86, it is interesting to try and correlate <code>numCycles</code> with:</p>
</div>
<div class="sect4">
<h5 id="gem5-only-dump-selected-stats"><a class="anchor" href="#gem5-only-dump-selected-stats"></a><a class="link" href="#gem5-only-dump-selected-stats">19.9.2.1. gem5 only dump selected stats</a></h5>
<div class="paragraph">
<p>TODO</p>
</div>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/52014953/how-to-dump-only-a-single-or-certain-selected-stats-in-gem5" class="bare">https://stackoverflow.com/questions/52014953/how-to-dump-only-a-single-or-certain-selected-stats-in-gem5</a></p>
</div>
<div class="paragraph">
<p>To prevent the stats file from becoming humongous.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="gem5-config-ini"><a class="anchor" href="#gem5-config-ini"></a><a class="link" href="#gem5-config-ini">19.9.3. gem5 config.ini</a></h4>
<div class="paragraph">
<p>The <code>m5out/config.ini</code> file, contains a very good high level description of the system:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>less $(./getvar --arch arm --emulator gem5 m5out_dir)"</pre>
</div>
</div>
<div class="paragraph">
<p>That file contains a tree representation of the system, sample excerpt:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[root]
type=Root
children=system
full_system=true
[system]
type=ArmSystem
children=cpu cpu_clk_domain
auto_reset_addr_64=false
semihosting=Null
[system.cpu]
type=AtomicSimpleCPU
children=dstage2_mmu dtb interrupts isa istage2_mmu itb tracer
branchPred=Null
[system.cpu_clk_domain]
type=SrcClockDomain
clock=500</pre>
</div>
</div>
<div class="paragraph">
<p>Each node has:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>a list of child nodes, e.g. <code>system</code> is a child of <code>root</code>, and both <code>cpu</code> and <code>cpu_clk_domain</code> are children of <code>system</code></p>
</li>
<li>
<p>a list of parameters, e.g. <code>system.semihosting</code> is <code>Null</code>, which means that <a href="#semihosting">Semihosting</a> was turned off</p>
<div class="ulist">
<ul>
<li>
<p>the <code>type</code> parameter shows is present on every node, and it maps to a <code>Python</code> object that inherits from <code>SimObject</code>.</p>
<div class="paragraph">
<p>For example, <code>AtomicSimpleCPU</code> maps is defined at <a href="https://github.com/gem5/gem5/blob/05c4c2b566ce351ab217b2bd7035562aa7a76570/src/cpu/simple/AtomicSimpleCPU.py#L45">src/cpu/simple/AtomicSimpleCPU.py</a>.</p>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>You can also get a simplified graphical view of the tree with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>xdg-open "$(./getvar --arch arm --emulator gem5 m5out_dir)/config.dot.pdf"</pre>
</div>
</div>
<div class="paragraph">
<p>Modifying the <code>config.ini</code> file manually does nothing since it gets overwritten every time.</p>
</div>
<div class="paragraph">
<p>Set custom configs with the <code>--param</code> option of <code>fs.py</code>, e.g. we can make gem5 wait for GDB to connect with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>fs.py --param 'system.cpu[0].wait_for_remote_gdb = True'</pre>
</div>
</div>
<div class="paragraph">
<p>More complex settings involving new classes however require patching the config files, although it is easy to hack this up. See for example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/patches/manual/gem5-semihost.patch">patches/manual/gem5-semihost.patch</a>.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="m5term"><a class="anchor" href="#m5term"></a><a class="link" href="#m5term">19.10. m5term</a></h3>
<div class="paragraph">
<p>We use the <code>m5term</code> in-tree executable to connect to the terminal instead of a direct <code>telnet</code>.</p>
</div>
<div class="paragraph">
<p>If you use <code>telnet</code> directly, it mostly works, but certain interactive features don&#8217;t, e.g.:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>up and down arrows for history navigation</p>
</li>
<li>
<p>tab to complete paths</p>
</li>
<li>
<p><code>Ctrl-C</code> to kill processes</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>TODO understand in detail what <code>m5term</code> does differently than <code>telnet</code>.</p>
</div>
</div>
<div class="sect2">
<h3 id="gem5-python-scripts-without-rebuild"><a class="anchor" href="#gem5-python-scripts-without-rebuild"></a><a class="link" href="#gem5-python-scripts-without-rebuild">19.11. gem5 Python scripts without rebuild</a></h3>
<div class="paragraph">
<p>We have made a crazy setup that allows you to just <code>cd</code> into <code>submodules/gem5</code>, and edit Python scripts directly there.</p>
</div>
<div class="paragraph">
<p>This is not normally possible with Buildroot, since normal Buildroot packages first copy files to the output directory (<code>$(./getvar -a &lt;arch&gt; buildroot_build_build_dir)/&lt;pkg&gt;</code>), and then build there.</p>
</div>
<div class="paragraph">
<p>So if you modified the Python scripts with this setup, you would still need to <code>./build</code> to copy the modified files over.</p>
</div>
<div class="paragraph">
<p>For gem5 specifically however, we have hacked up the build so that we <code>cd</code> into the <code>submodules/gem5</code> tree, and then do an <a href="https://stackoverflow.com/questions/54343515/how-to-build-gem5-out-of-tree/54343516#54343516">out of tree</a> build to <code>out/common/gem5</code>.</p>
</div>
<div class="paragraph">
<p>Another advantage of this method is the we factor out the <code>arm</code> and <code>aarch64</code> gem5 builds which are identical and large, as well as the smaller arch generic pieces.</p>
</div>
<div class="paragraph">
<p>Using Buildroot for gem5 is still convenient because we use it to:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>to cross build <code>m5</code> for us</p>
</li>
<li>
<p>check timestamps and skip the gem5 build when it is not requested</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The out of build tree is required, because otherwise Buildroot would copy the output build of all archs to each arch directory, resulting in <code>arch^2</code> build copies, which is significant.</p>
</div>
</div>
<div class="sect2">
<h3 id="gem5-fs_biglittle"><a class="anchor" href="#gem5-fs_biglittle"></a><a class="link" href="#gem5-fs_biglittle">19.12. gem5 fs_bigLITTLE</a></h3>
<div class="paragraph">
<p>By default, we use <code>configs/example/fs.py</code> script.</p>
</div>
<div class="paragraph">
<p>The <code>--gem5-script biglittle</code> option enables the alternative <code>configs/example/arm/fs_bigLITTLE.py</code> script instead.</p>
</div>
<div class="paragraph">
<p>First apply:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>patch -d "$(./getvar gem5_source_dir)" -p 1 &lt; patches/manual/gem5-biglittle.patch</pre>
</div>
</div>
<div class="paragraph">
<p>then:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --emulator gem5 --gem5-script biglittle</pre>
</div>
</div>
<div class="paragraph">
<p>Advantages over <code>fs.py</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>more representative of mobile ARM SoCs, which almost always have big little cluster</p>
</li>
<li>
<p>simpler than <code>fs.py</code>, and therefore easier to understand and modify</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Disadvantages over <code>fs.py</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>only works for ARM, not other archs</p>
</li>
<li>
<p>not as many configuration options as <code>fs.py</code>, many things are hardcoded</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We setup 2 big and 2 small CPUs, but <code>cat /proc/cpuinfo</code> shows 4 identical CPUs instead of 2 of two different types, likely because gem5 does not expose some informational register much like the caches: <a href="https://www.mail-archive.com/gem5-users@gem5.org/msg15426.html" class="bare">https://www.mail-archive.com/gem5-users@gem5.org/msg15426.html</a> <a href="#gem5-config-ini">gem5 config.ini</a> does show that the two big ones are <code>DerivO3CPU</code> and the small ones are <code>MinorCPU</code>.</p>
</div>
<div class="paragraph">
<p>TODO: why is the <code>--dtb</code> required despite <code>fs_bigLITTLE.py</code> having a DTB generation capability? Without it, nothing shows on terminal, and the simulation terminates with <code>simulate() limit reached @ 18446744073709551615</code>. The magic <code>vmlinux.vexpress_gem5_v1.20170616</code> works however without a DTB.</p>
</div>
<div class="paragraph">
<p>Tested on: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/commit/18c1c823feda65f8b54cd38e261c282eee01ed9f">18c1c823feda65f8b54cd38e261c282eee01ed9f</a></p>
</div>
</div>
<div class="sect2">
<h3 id="gem5-unit-tests"><a class="anchor" href="#gem5-unit-tests"></a><a class="link" href="#gem5-unit-tests">19.13. gem5 unit tests</a></h3>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/52279971/how-to-run-the-gem5-unit-tests" class="bare">https://stackoverflow.com/questions/52279971/how-to-run-the-gem5-unit-tests</a></p>
</div>
<div class="paragraph">
<p>These are just very small GTest tests that test a single class in isolation, they don&#8217;t run any executables.</p>
</div>
<div class="paragraph">
<p>Build the unit tests and run them:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-gem5 --unit-tests</pre>
</div>
</div>
<div class="paragraph">
<p>Running individual unit tests is not yet exposed, but it is easy to do: while running the full tests, GTest prints each test command being run, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/path/to/build/ARM/base/circlebuf.test.opt --gtest_output=xml:/path/to/build/ARM/unittests.opt/base/circlebuf.test.xml
[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from CircleBufTest
[ RUN ] CircleBufTest.BasicReadWriteNoOverflow
[ OK ] CircleBufTest.BasicReadWriteNoOverflow (0 ms)
[ RUN ] CircleBufTest.SingleWriteOverflow
[ OK ] CircleBufTest.SingleWriteOverflow (0 ms)
[ RUN ] CircleBufTest.MultiWriteOverflow
[ OK ] CircleBufTest.MultiWriteOverflow (0 ms)
[ RUN ] CircleBufTest.PointerWrapAround
[ OK ] CircleBufTest.PointerWrapAround (0 ms)
[----------] 4 tests from CircleBufTest (0 ms total)
[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (0 ms total)
[ PASSED ] 4 tests.</pre>
</div>
</div>
<div class="paragraph">
<p>so you can just copy paste the command.</p>
</div>
<div class="paragraph">
<p>Building individual tests is possible with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-gem5 --unit-test base/circlebuf.test</pre>
</div>
</div>
<div class="paragraph">
<p>This does not run the test however.</p>
</div>
<div class="paragraph">
<p>Note that the command and it&#8217;s corresponding results don&#8217;t need to show consecutively on stdout because tests are run in parallel. You just have to match them based on the class name <code>CircleBufTest</code> to the file <code>circlebuf.test.cpp</code>.</p>
</div>
</div>
<div class="sect2">
<h3 id="gem5-regression-tests"><a class="anchor" href="#gem5-regression-tests"></a><a class="link" href="#gem5-regression-tests">19.14. gem5 regression tests</a></h3>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/52279971/how-to-run-the-gem5-unit-tests" class="bare">https://stackoverflow.com/questions/52279971/how-to-run-the-gem5-unit-tests</a></p>
</div>
<div class="paragraph">
<p>Running the larger 2019 regression tests is exposed for example with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./gem5-regression --arch aarch64 -- --length quick</pre>
</div>
</div>
<div class="paragraph">
<p>TODO skip the build by default with <code>--skip-build</code> since we already manage it with <code>./build-gem5</code>. But we can&#8217;t do this because it is the build step that downloads the test binaries. We need to find a way to either download the binaries without building, or to pass the exact same scons build options through <code>test/main.py</code>.</p>
</div>
</div>
<div class="sect2">
<h3 id="gem5-simulate-limit-reached"><a class="anchor" href="#gem5-simulate-limit-reached"></a><a class="link" href="#gem5-simulate-limit-reached">19.15. gem5 simulate() limit reached</a></h3>
<div class="paragraph">
<p>This error happens when the following instruction limits are reached:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>system.cpu[0].max_insts_all_threads
system.cpu[0].max_insts_any_thread</pre>
</div>
</div>
<div class="paragraph">
<p>If the parameter is not set, it defaults to <code>0</code>, which is magic and means the huge maximum value of <code>uint64_t</code>: 0xFFFFFFFFFFFFFFFF, which in practice would require a very long simulation if at least one CPU were live.</p>
</div>
<div class="paragraph">
<p>So this usually means all CPUs are in a sleep state, and no events are scheduled in the future, which usually indicates a bug in either gem5 or guest code, leading gem5 to blow up.</p>
</div>
<div class="paragraph">
<p>Still, fs.py at gem5 08c79a194d1a3430801c04f37d13216cc9ec1da3 does not exit with non-zero status due to this&#8230;&#8203; and so we just parse it out just as for <a href="#m5-fail">m5 fail</a>&#8230;&#8203;</p>
</div>
<div class="paragraph">
<p>A trivial and very direct way to see message would be:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--emulator gem5 \
--static \
--userland \userland/arch/x86_64/freestanding/linux/hello.S \
--trace-insts-stdout \
-- \
--param 'system.cpu[0].max_insts_all_threads = 3' \
;</pre>
</div>
</div>
<div class="paragraph">
<p>which as of lkmc 402059ed22432bb351d42eb10900e5a8e06aa623 runs only the first three instructions and quits!</p>
</div>
<div class="literalblock">
<div class="content">
<pre>info: Entering event queue @ 0. Starting simulation...
0: system.cpu A0 T0 : @asm_main_after_prologue : mov rdi, 0x1
0: system.cpu A0 T0 : @asm_main_after_prologue.0 : MOV_R_I : limm rax, 0x1 : IntAlu : D=0x0000000000000001 flags=(IsInteger|IsMicroop|IsLastMicroop|IsFirstMicroop)
1000: system.cpu A0 T0 : @asm_main_after_prologue+7 : mov rdi, 0x1
1000: system.cpu A0 T0 : @asm_main_after_prologue+7.0 : MOV_R_I : limm rdi, 0x1 : IntAlu : D=0x0000000000000001 flags=(IsInteger|IsMicroop|IsLastMicroop|IsFirstMicroop)
2000: system.cpu A0 T0 : @asm_main_after_prologue+14 : lea rsi, DS:[rip + 0x19]
2000: system.cpu A0 T0 : @asm_main_after_prologue+14.0 : LEA_R_P : rdip t7, %ctrl153, : IntAlu : D=0x000000000040008d flags=(IsInteger|IsMicroop|IsDelayedCommit|IsFirstMicroop)
2500: system.cpu A0 T0 : @asm_main_after_prologue+14.1 : LEA_R_P : lea rsi, DS:[t7 + 0x19] : IntAlu : D=0x00000000004000a6 flags=(IsInteger|IsMicroop|IsLastMicroop)
Exiting @ tick 3000 because all threads reached the max instruction count</pre>
</div>
</div>
<div class="paragraph">
<p>The exact same can be achieved with the older hardcoded <code>--maxinsts</code> mechanism present in <code>se.py</code> and <code>fs.py</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--emulator gem5 \
--static \
--userland \userland/arch/x86_64/freestanding/linux/hello.S \
--trace-insts-stdout \
-- \
--maxinsts 3
;</pre>
</div>
</div>
<div class="paragraph">
<p>The message also shows on <a href="#user-mode-simulation">User mode simulation</a> deadlocks, for example in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/pthread_deadlock.c">userland/posix/pthread_deadlock.c</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--emulator gem5 \
--static \
--userland userland/posix/pthread_deadlock.c \
--userland-args 1 \
;</pre>
</div>
</div>
<div class="paragraph">
<p>ends in:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Exiting @ tick 18446744073709551615 because simulate() limit reached</pre>
</div>
</div>
<div class="paragraph">
<p>where 18446744073709551615 is 0xFFFFFFFFFFFFFFFF in decimal.</p>
</div>
<div class="paragraph">
<p>And there is a <a href="#baremetal">Baremetal</a> example at <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/no_bootloader/wfe_loop.S">baremetal/arch/aarch64/no_bootloader/wfe_loop.S</a> that dies on <a href="#arm-wfe-and-sev-instructions">WFE</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--baremetal baremetal/arch/aarch64/no_bootloader/wfe_loop.S \
--emulator gem5 \
--trace-insts-stdout \
;</pre>
</div>
</div>
<div class="paragraph">
<p>which gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>info: Entering event queue @ 0. Starting simulation...
0: system.cpu A0 T0 : @lkmc_start : wfe : IntAlu : D=0x0000000000000000 flags=(IsSerializeAfter|IsNonSpeculative|IsQuiesce|IsUnverifiable)
1000: system.cpu A0 T0 : @lkmc_start+4 : b &lt;lkmc_start&gt; : IntAlu : flags=(IsControl|IsDirectControl|IsUncondControl)
1500: system.cpu A0 T0 : @lkmc_start : wfe : IntAlu : D=0x0000000000000000 flags=(IsSerializeAfter|IsNonSpeculative|IsQuiesce|IsUnverifiable)
Exiting @ tick 18446744073709551615 because simulate() limit reached</pre>
</div>
</div>
<div class="paragraph">
<p>Other examples of the message:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#arm-multicore">ARM multicore</a> with a single CPU stays stopped at an WFE sleep instruction</p>
</li>
<li>
<p>this sample bug on se.py multithreading: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/issues/81" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/issues/81</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="gem5-build-options"><a class="anchor" href="#gem5-build-options"></a><a class="link" href="#gem5-build-options">19.16. gem5 build options</a></h3>
<div class="paragraph">
<p>In order to use different build options, you might also want to use <a href="#gem5-build-variants">gem5 build variants</a> to keep the build outputs separate from one another.</p>
</div>
<div class="sect3">
<h4 id="gem5-debug-build"><a class="anchor" href="#gem5-debug-build"></a><a class="link" href="#gem5-debug-build">19.16.1. gem5 debug build</a></h4>
<div class="paragraph">
<p>Explained at: <a href="#debug-the-emulator">Section 18.7, &#8220;Debug the emulator&#8221;</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-clang-build"><a class="anchor" href="#gem5-clang-build"></a><a class="link" href="#gem5-clang-build">19.16.2. gem5 clang build</a></h4>
<div class="paragraph">
<p>TODO test properly, benchmark vs GCC.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get install clang
./build-gem5 --clang
./run --clang --emulator gem5</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="gem5-sanitation-build"><a class="anchor" href="#gem5-sanitation-build"></a><a class="link" href="#gem5-sanitation-build">19.16.3. gem5 sanitation build</a></h4>
<div class="paragraph">
<p>If there gem5 appears to have a C++ undefined behaviour bug, which is often very difficult to track down, you can try to build it with the following extra SCons options:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-gem5 --gem5-build-id san --verbose -- --with-ubsan --without-tcmalloc</pre>
</div>
</div>
<div class="paragraph">
<p>This will make GCC do a lot of extra sanitation checks at compile and run time.</p>
</div>
<div class="paragraph">
<p>As a result, the build and runtime will be way slower than normal, but that still might be the fastest way to solve undefined behaviour problems.</p>
</div>
<div class="paragraph">
<p>Ideally, we should also be able to run it with asan with <code>--with-asan</code>, but if we try then the build fails at gem5 16eeee5356585441a49d05c78abc328ef09f7ace (with two ubsan trivial fixes I&#8217;ll push soon):</p>
</div>
<div class="literalblock">
<div class="content">
<pre>=================================================================
==9621==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 371712 byte(s) in 107 object(s) allocated from:
#0 0x7ff039804448 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c448)
#1 0x7ff03950d065 in dictresize ../Objects/dictobject.c:643
Direct leak of 23728 byte(s) in 26 object(s) allocated from:
#0 0x7ff039804448 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c448)
#1 0x7ff03945e40d in _PyObject_GC_Malloc ../Modules/gcmodule.c:1499
#2 0x7ff03945e40d in _PyObject_GC_Malloc ../Modules/gcmodule.c:1493
Direct leak of 2928 byte(s) in 43 object(s) allocated from:
#0 0x7ff03980487e in __interceptor_realloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c87e)
#1 0x7ff03951d763 in list_resize ../Objects/listobject.c:62
#2 0x7ff03951d763 in app1 ../Objects/listobject.c:277
#3 0x7ff03951d763 in PyList_Append ../Objects/listobject.c:289
Direct leak of 2002 byte(s) in 3 object(s) allocated from:
#0 0x7ff039804448 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c448)
#1 0x7ff0394fd813 in PyString_FromStringAndSize ../Objects/stringobject.c:88
#2 0x7ff0394fd813 in PyString_FromStringAndSize ../Objects/stringobject.c:57 Direct leak of 40 byte(s) in 2 object(s) allocated from: #0 0x7ff039804448 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c448)
#1 0x7ff03951ea4b in PyList_New ../Objects/listobject.c:152
Indirect leak of 10384 byte(s) in 11 object(s) allocated from: #0 0x7ff039804448 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c448) #1 0x7ff03945e40d in _PyObject_GC_Malloc ../Modules/gcmodule.c:1499 #2 0x7ff03945e40d in _PyObject_GC_Malloc ../Modules/gcmodule.c:1493
Indirect leak of 4089 byte(s) in 6 object(s) allocated from:
#0 0x7ff039804448 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c448)
#1 0x7ff0394fd648 in PyString_FromString ../Objects/stringobject.c:143
Indirect leak of 2090 byte(s) in 3 object(s) allocated from:
#0 0x7ff039804448 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c448) #1 0x7ff0394eb36f in type_new ../Objects/typeobject.c:2421 #2 0x7ff0394eb36f in type_new ../Objects/typeobject.c:2094
Indirect leak of 1346 byte(s) in 2 object(s) allocated from:
#0 0x7ff039804448 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c448)
#1 0x7ff0394fd813 in PyString_FromStringAndSize ../Objects/stringobject.c:88 #2 0x7ff0394fd813 in PyString_FromStringAndSize ../Objects/stringobject.c:57 SUMMARY: AddressSanitizer: 418319 byte(s) leaked in 203 allocation(s).</pre>
</div>
</div>
<div class="paragraph">
<p>From the message, this appears however to be a Python / pyenv11 bug however and not in gem5 specifically. I think it worked when I tried it in the past in an older gem5 / Ubuntu.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-ruby-build"><a class="anchor" href="#gem5-ruby-build"></a><a class="link" href="#gem5-ruby-build">19.16.4. gem5 Ruby build</a></h4>
<div class="paragraph">
<p>Ruby is a system that includes the SLICC domain specific language to describe memory systems: <a href="http://gem5.org/Ruby" class="bare">http://gem5.org/Ruby</a></p>
</div>
<div class="paragraph">
<p>It seems to have usage outside of gem5, but the naming overload with the <a href="https://en.wikipedia.org/wiki/Ruby_(programming_language)">Ruby programming language</a>, which also has <a href="https://thoughtbot.com/blog/writing-a-domain-specific-language-in-ruby">domain specific languages</a> as a concept, makes it impossible to google anything about it!</p>
</div>
<div class="paragraph">
<p>Ruby is activated at compile time with the <code>PROTOCOL</code> flag, which specifies the desired memory system time.</p>
</div>
<div class="paragraph">
<p>For example, to use a two level <a href="https://en.wikipedia.org/wiki/MESI_protocol">MESI</a> <a href="https://en.wikipedia.org/wiki/Cache_coherence">cache coherence protocol</a>, we can do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-gem5 --arch aarch64 --gem5-build-id ruby -- PROTOCOL=MESI_Two_Level
./run --arch aarch64 --emulator -gem5 --gem5-build-id ruby -- --ruby</pre>
</div>
</div>
<div class="paragraph">
<p>and during build we see a humongous line of type:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>[ SLICC] src/mem/protocol/MESI_Two_Level.slicc -&gt; ARM/mem/protocol/AccessPermission.cc, ARM/mem/protocol/AccessPermission.hh, ...</pre>
</div>
</div>
<div class="paragraph">
<p>which shows that dozens of C++ files are being generated from Ruby SLICC.</p>
</div>
<div class="paragraph">
<p>TODO observe it doing something during a run.</p>
</div>
<div class="paragraph">
<p>The relevant source files live in the source tree under:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>src/mem/protocol/MESI_Two_Level*</pre>
</div>
</div>
<div class="paragraph">
<p>We already pass the <code>SLICC_HTML</code> flag by default to the build, which generates an HTML summary of each memory protocol under:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>xdg-open "$(./getvar --arch aarch64 --gem5-build-id ruby gem5_build_build_dir)/ARM/mem/protocol/html/index.html"</pre>
</div>
</div>
<div class="paragraph">
<p>A minimized ruby config which was not merged upstream can be found for study at: <a href="https://gem5-review.googlesource.com/c/public/gem5/+/13599/1" class="bare">https://gem5-review.googlesource.com/c/public/gem5/+/13599/1</a></p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-python-3-build"><a class="anchor" href="#gem5-python-3-build"></a><a class="link" href="#gem5-python-3-build">19.16.5. gem5 Python 3 build</a></h4>
<div class="paragraph">
<p>Python 3 support was mostly added in 2019 Q3 at arounda347a1a68b8a6e370334be3a1d2d66675891e0f1 but remained buggy for some time afterwards.</p>
</div>
<div class="paragraph">
<p>In an Ubuntu 18.04 host where <code>python</code> is <code>python2</code> by default, build with Python 3 instead with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-gem5 --gem5-build-id python3 -- PYTHON_CONFIG=python3-config</pre>
</div>
</div>
<div class="paragraph">
<p>Python 3 is then automatically used when running if you use that build.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-cpu-types"><a class="anchor" href="#gem5-cpu-types"></a><a class="link" href="#gem5-cpu-types">19.17. gem5 CPU types</a></h3>
<div class="paragraph">
<p>gem5 has a few in tree CPU models for different purposes.</p>
</div>
<div class="paragraph">
<p>In fs.py and se.py, those are selectable with the <code>--cpu-type</code> option.</p>
</div>
<div class="paragraph">
<p>TODO are there any public performance correlations between those models and real cores? The information to make accurate models isn&#8217;t generally public for non-free CPUs, so either you must either rely vendor provided models or on experiments/reverse engineering.</p>
</div>
<div class="sect3">
<h4 id="gem5-basesimplecpu"><a class="anchor" href="#gem5-basesimplecpu"></a><a class="link" href="#gem5-basesimplecpu">19.17.1. gem5 BaseSimpleCPU</a></h4>
<div class="paragraph">
<p>Simple abstract CPU without a pipeline.</p>
</div>
<div class="paragraph">
<p>They are therefore completely unrealistic. But they also run much faster.</p>
</div>
<div class="paragraph">
<p>Implementations:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>AtomicSimpleCPU</code>: the default one. Memory accesses happen instantaneously. The fastest simulation except for KVM, but not realistic at all.</p>
<div class="paragraph">
<p>Useful to <a href="#gem5-restore-checkpoint-with-a-different-cpu">boot Linux fast and then checkpoint and switch to a more detailed CPU</a>.</p>
</div>
</li>
<li>
<p><code>TimingSimpleCPU</code>: memory accesses are realistic, but the CPU has no pipeline. The simulation is faster than detailed models, but slower than <code>AtomicSimpleCPU</code>. TODO: application?</p>
</li>
</ul>
</div>
<div class="paragraph">
<p><a href="#gem5-kvm">KVM CPUs</a> are an alternative way of fast forwarding boot when they work.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-minorcpu"><a class="anchor" href="#gem5-minorcpu"></a><a class="link" href="#gem5-minorcpu">19.17.2. gem5 MinorCPU</a></h4>
<div class="paragraph">
<p>Generic in-order core that does not model any specific CPU.</p>
</div>
<div class="paragraph">
<p>Its C++ implementation that can be parametrized to more closely match real cores.</p>
</div>
<div class="paragraph">
<p>Note that since gem5 is highly parametrizable, the parametrization could even change which instructions a CPU can execute by altering its available <a href="https://en.wikipedia.org/wiki/Execution_unit">functional units</a>, which are used to model performance.</p>
</div>
<div class="paragraph">
<p>For example, <code>MinorCPU</code> allows all implemented instructions, including <a href="#arm-sve">ARM SVE</a> instructions, but a derived class modelling, say, an <a href="https://en.wikipedia.org/wiki/ARM_Cortex-A7">ARM Cortex A7 core</a>, might not, since SVE is a newer feature and the A7 core does not have SVE.</p>
</div>
<div class="paragraph">
<p>The weird name "Minor" stands for "M (TODO what is M) IN ONder".</p>
</div>
<div class="paragraph">
<p>Its 4 stage pipeline is described at the "MinorCPU" section of <a href="#gem5-arm-rsk">gem5 ARM RSK</a>.</p>
</div>
<div class="paragraph">
<p>As of 2019, in-order cores are mostly present in low power / cost contexts, for example little cores of <a href="https://en.wikipedia.org/wiki/ARM_big.LITTLE">ARM bigLITTLE</a>.</p>
</div>
<div class="paragraph">
<p>The following models extend the <code>MinorCPU</code> class by parametrization to make it match existing CPUs more closely:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>HPI</code>: derived from <code>MinorCPU</code>.</p>
<div class="paragraph">
<p>Created by Ashkan Tousi in 2017 while working at ARM.</p>
</div>
<div class="paragraph">
<p>According to <a href="#gem5-arm-rsk">gem5 ARM RSK</a>:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>The HPI CPU timing model is tuned to be representative of a modern in-order Armv8-A implementation.</p>
</div>
</blockquote>
</div>
</li>
<li>
<p><code>ex5_LITTLE</code>: derived from <code>MinorCPU</code>. Description reads:</p>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>ex5 LITTLE core (based on the ARM Cortex-A7)</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>Implemented by Pierre-Yves Péneau from LIRMM, which is a research lab in Montpellier, France, in 2017.</p>
</div>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="gem5-deriveo3cpu"><a class="anchor" href="#gem5-deriveo3cpu"></a><a class="link" href="#gem5-deriveo3cpu">19.17.3. gem5 DeriveO3CPU</a></h4>
<div class="paragraph">
<p>Generic out-of-order core. "O3" Stands for "Out Of Order"!</p>
</div>
<div class="paragraph">
<p>Analogous to <a href="#gem5-minorcpu">MinorCPU</a>, but modelling an out of order core instead of in order.</p>
</div>
<div class="paragraph">
<p>Existing parametrizations:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>ex5_big</code>: big corresponding to <code>ex5_LITTLE</code>, by same author at same time. It description reads:</p>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>ex5 big core (based on the ARM Cortex-A15)</p>
</div>
</blockquote>
</div>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="gem5-arm-rsk"><a class="anchor" href="#gem5-arm-rsk"></a><a class="link" href="#gem5-arm-rsk">19.17.4. gem5 ARM RSK</a></h4>
<div class="paragraph">
<p><a href="https://github.com/arm-university/arm-gem5-rsk/blob/aa3b51b175a0f3b6e75c9c856092ae0c8f2a7cdc/gem5_rsk.pdf" class="bare">https://github.com/arm-university/arm-gem5-rsk/blob/aa3b51b175a0f3b6e75c9c856092ae0c8f2a7cdc/gem5_rsk.pdf</a></p>
</div>
<div class="paragraph">
<p>Dated 2017, it contains a good overview of gem5 CPUs.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-arm-platforms"><a class="anchor" href="#gem5-arm-platforms"></a><a class="link" href="#gem5-arm-platforms">19.18. gem5 ARM platforms</a></h3>
<div class="paragraph">
<p>The gem5 platform is selectable with the <code>--machine</code> option, which is named after the analogous QEMU <code>-machine</code> option, and which sets the <code>--machine-type</code>.</p>
</div>
<div class="paragraph">
<p>Each platform represents a different system with different devices, memory and interrupt setup.</p>
</div>
<div class="paragraph">
<p>TODO: describe the main characteristics of each platform, as of gem5 5e83d703522a71ec4f3eb61a01acd8c53f6f3860:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>VExpress_GEM5_V1</code>: good sane base platform</p>
</li>
<li>
<p><code>VExpress_GEM5_V1_DPU</code>: <code>VExpress_GEM5_V1</code> with DP650 instead of HDLCD, selected automatically by <code>./run --dp650</code>, see also: <a href="#gem5-graphic-mode-dp650">gem5 graphic mode DP650</a></p>
</li>
<li>
<p><code>VExpress_GEM5_V2</code>: VExpress_GEM5_V1 with GICv3, uses a different bootloader <code>arm/aarch64_bootloader/boot_emm_v2.arm64</code> TODO is it because of GICv3?</p>
</li>
<li>
<p>anything that does not start with: <code>VExpress_GEM5_</code>: old and bad, don&#8217;t use them</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="gem5-internals"><a class="anchor" href="#gem5-internals"></a><a class="link" href="#gem5-internals">19.19. gem5 internals</a></h3>
<div class="paragraph">
<p>Internals under other sections:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#gem5-trace-internals">gem5 trace internals</a></p>
</li>
<li>
<p><a href="#gem5-checkpoint-internals">gem5 checkpoint internals</a></p>
</li>
<li>
<p><a href="#gem5-graphic-mode-internals">gem5 graphic mode internals</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="gem5-eclipse-configuration"><a class="anchor" href="#gem5-eclipse-configuration"></a><a class="link" href="#gem5-eclipse-configuration">19.19.1. gem5 Eclipse configuration</a></h4>
<div class="paragraph">
<p>In order to develop complex C++ software such as gem5, a good IDE setup is fundamental.</p>
</div>
<div class="paragraph">
<p>The best setup I&#8217;ve reached is with Eclipse. It is not perfect, and there is a learning curve, but is worth it.</p>
</div>
<div class="paragraph">
<p>I recommend the following settings, tested in Eclipse 2019.09, Ubuntu 18.04:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>fix all missing stdlib headers: <a href="https://stackoverflow.com/questions/10373788/how-to-solve-unresolved-inclusion-iostream-in-a-c-file-in-eclipse-cdt/51099533#51099533" class="bare">https://stackoverflow.com/questions/10373788/how-to-solve-unresolved-inclusion-iostream-in-a-c-file-in-eclipse-cdt/51099533#51099533</a></p>
</li>
<li>
<p>use spaces instead of tabs: Window, Preferences, Code Style, C/C++, Formatter, New, Edit, Tab Policy, Spaces Only</p>
</li>
<li>
<p>add to the include search path:</p>
<div class="ulist">
<ul>
<li>
<p>./src/ in the source tree</p>
</li>
<li>
<p>the ISA specific build directory which contains some self-generated stuff, e.g.: out/gem5/default/build/ARM</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>To run and GDB step debug the executable, just copy the full command line from the output <code>./run</code>, and configure it into Eclipse.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-python-c-interaction"><a class="anchor" href="#gem5-python-c-interaction"></a><a class="link" href="#gem5-python-c-interaction">19.19.2. gem5 Python C++ interaction</a></h4>
<div class="paragraph">
<p>The interaction uses the Python C extension interface <a href="https://docs.python.org/2/extending/extending.html" class="bare">https://docs.python.org/2/extending/extending.html</a> interface through the pybind11 helper library: <a href="https://github.com/pybind/pybind11" class="bare">https://github.com/pybind/pybind11</a></p>
</div>
<div class="paragraph">
<p>The C++ executable both:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>starts running the Python executable</p>
</li>
<li>
<p>provides Python classes written in C++ for that Python code to use</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>An example of this can be found at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://docs.python.org/2/extending/embedding.html#extending-embedded-python" class="bare">https://docs.python.org/2/extending/embedding.html#extending-embedded-python</a></p>
</li>
<li>
<p><a href="https://github.com/pybind/pybind11/tree/v2.2.3/tests/test_embed" class="bare">https://github.com/pybind/pybind11/tree/v2.2.3/tests/test_embed</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>then gem5 magic <code>simobject</code> class adds some crazy stuff on top of it further&#8230;&#8203; is is a mess. in particular, it auto generates <code>params/</code> headers. TODO: why is this mess needed at all? pybind11 seems to handle constructor arguments just fine:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/pybind/pybind11/blob/v2.2.3/tests/test_class.py#L77" class="bare">https://github.com/pybind/pybind11/blob/v2.2.3/tests/test_class.py#L77</a></p>
</li>
<li>
<p><a href="https://github.com/pybind/pybind11/blob/v2.2.3/tests/test_class.cpp#L41" class="bare">https://github.com/pybind/pybind11/blob/v2.2.3/tests/test_class.cpp#L41</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Let&#8217;s study <code>BadDevice</code> for example:</p>
</div>
<div class="paragraph">
<p><code>src/dev/BadDevice.py</code> defines <code>devicename</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>class BadDevice(BasicPioDevice):
type = 'BadDevice'
cxx_header = "dev/baddev.hh"
devicename = Param.String("Name of device to error on")</pre>
</div>
</div>
<div class="paragraph">
<p>The object is created in Python for example from <code>src/dev/alpha/Tsunami.py</code> as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> fb = BadDevice(pio_addr=0x801fc0003d0, devicename='FrameBuffer')</pre>
</div>
</div>
<div class="paragraph">
<p>Since <code>BadDevice</code> has no <code>__init__</code> method, and neither <code>BasicPioDevice</code>, it all just falls through until the SimObject.<em>init</em> constructor.</p>
</div>
<div class="paragraph">
<p>This constructor will loop through the inheritance chain and give the Python parameters to the C++ BadDeviceParams class as follows.</p>
</div>
<div class="paragraph">
<p>The auto-generated <code>build/ARM/params/BadDevice.hh</code> file defines BadDeviceParams in C++:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>#ifndef __PARAMS__BadDevice__
#define __PARAMS__BadDevice__
class BadDevice;
#include &lt;cstddef&gt;
#include &lt;string&gt;
#include "params/BasicPioDevice.hh"
struct BadDeviceParams
: public BasicPioDeviceParams
{
BadDevice * create();
std::string devicename;
};
#endif // __PARAMS__BadDevice__</pre>
</div>
</div>
<div class="paragraph">
<p>and <code>./python/_m5/param_BadDevice.cc</code> defines the param Python from C++ with pybind11:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>namespace py = pybind11;
static void
module_init(py::module &amp;m_internal)
{
py::module m = m_internal.def_submodule("param_BadDevice");
py::class_&lt;BadDeviceParams, BasicPioDeviceParams, std::unique_ptr&lt;BadDeviceParams, py::nodelete&gt;&gt;(m, "BadDeviceParams")
.def(py::init&lt;&gt;())
.def("create", &amp;BadDeviceParams::create)
.def_readwrite("devicename", &amp;BadDeviceParams::devicename)
;
py::class_&lt;BadDevice, BasicPioDevice, std::unique_ptr&lt;BadDevice, py::nodelete&gt;&gt;(m, "BadDevice")
;
}
static EmbeddedPyBind embed_obj("BadDevice", module_init, "BasicPioDevice");</pre>
</div>
</div>
<div class="paragraph">
<p><code>src/dev/baddev.hh</code> then uses the parameters on the constructor:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>class BadDevice : public BasicPioDevice
{
private:
std::string devname;
public:
typedef BadDeviceParams Params;
protected:
const Params *
params() const
{
return dynamic_cast&lt;const Params *&gt;(_params);
}
public:
/**
* Constructor for the Baddev Class.
* @param p object parameters
* @param a base address of the write
*/
BadDevice(Params *p);</pre>
</div>
</div>
<div class="paragraph">
<p><code>src/dev/baddev.cc</code> then uses the parameter:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>BadDevice::BadDevice(Params *p)
: BasicPioDevice(p, 0x10), devname(p-&gt;devicename)
{
}</pre>
</div>
</div>
<div class="paragraph">
<p>Tested on gem5 08c79a194d1a3430801c04f37d13216cc9ec1da3.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-entry-point"><a class="anchor" href="#gem5-entry-point"></a><a class="link" href="#gem5-entry-point">19.19.3. gem5 entry point</a></h4>
<div class="paragraph">
<p>The main is at: <code>src/sim/main.cc</code>. It calls:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ret = initM5Python();</pre>
</div>
</div>
<div class="paragraph">
<p>src/sim/init.cc:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>230 int
231 initM5Python()
232 {
233 EmbeddedPyBind::initAll();
234 return EmbeddedPython::initAll();
235 }</pre>
</div>
</div>
<div class="paragraph">
<p><code>initAll</code> basically just initializes the <code>_m5</code> Python object, which is used across multiple <code>.py</code>.</p>
</div>
<div class="paragraph">
<p>Back on <code>main</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ret = m5Main(argc, argv);</pre>
</div>
</div>
<div class="paragraph">
<p>which goes to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>result = PyRun_String(*command, Py_file_input, dict, dict);</pre>
</div>
</div>
<div class="paragraph">
<p>with commands looping over:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>import m5
m5.main()</pre>
</div>
</div>
<div class="paragraph">
<p>which leads into:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>src/python/m5/main.py#main</pre>
</div>
</div>
<div class="paragraph">
<p>which finally calls your config file like <code>fs.py</code> with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>filename = sys.argv[0]
filedata = file(filename, 'r').read()
filecode = compile(filedata, filename, 'exec')
[...]
exec filecode in scope</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: the file path name appears to be passed as a command line argument to the Python script, but I didn&#8217;t have the patience to fully understand the details.</p>
</div>
<div class="paragraph">
<p>Tested at gem5 b4879ae5b0b6644e6836b0881e4da05c64a6550d.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-event-queue"><a class="anchor" href="#gem5-event-queue"></a><a class="link" href="#gem5-event-queue">19.19.4. gem5 event queue</a></h4>
<div class="paragraph">
<p>gem5 is an event based simulator, and as such the event queue is of of the crucial elements in the system.</p>
</div>
<div class="paragraph">
<p>The gem5 event queue stores one callback event for each future point in time.</p>
</div>
<div class="paragraph">
<p>The event queue is implemented in the class <code>EventQueue</code> in the file <code>src/sim/eventq.hh</code>.</p>
</div>
<div class="paragraph">
<p>Not all times need to have an associated event: if a given time has no events, gem5 just skips it and jumps to the next event: the queue is basically a linked list of events.</p>
</div>
<div class="paragraph">
<p>Important examples of events include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>CPU ticks</p>
</li>
<li>
<p>TODO peripherals and memory</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>At the beginning of simulation, gem5 sets up exactly two events:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the first CPU cycle</p>
</li>
<li>
<p>one exit event at the end of time which triggers <a href="#gem5-simulate-limit-reached">gem5 simulate() limit reached</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Tick events then get triggered one by one as simulation progresses, in addition to any other system events.</p>
</div>
<div class="paragraph">
<p>The <code>EventQueue</code> class has one awesome <code>dump()</code> function that prints a human friendly representation of the queue, and can be easily called from GDB. TODO example.</p>
</div>
<div class="paragraph">
<p>We can also observe what is going on in the event queue with the <code>Event</code> <a href="#gem5-tracing">debug flag</a>.</p>
</div>
<div class="paragraph">
<p>Event execution is done at <code>EventQueue::serviceOne()</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Event *exit_event = eventq-&gt;serviceOne();</pre>
</div>
</div>
<div class="paragraph">
<p>This calls the <code>Event::process</code> method of the event.</p>
</div>
<div class="sect4">
<h5 id="gem5-event-queue-atomicsimplecpu-syscall-emulation-freestanding-example-analysis"><a class="anchor" href="#gem5-event-queue-atomicsimplecpu-syscall-emulation-freestanding-example-analysis"></a><a class="link" href="#gem5-event-queue-atomicsimplecpu-syscall-emulation-freestanding-example-analysis">19.19.4.1. gem5 event queue AtomicSimpleCPU syscall emulation freestanding example analysis</a></h5>
<div class="paragraph">
<p>Let&#8217;s now analyze every single event on a minimal <a href="#gem5-syscall-emulation-mode">gem5 syscall emulation mode</a> in the <a href="#gem5-cpu-types">simplest CPU that we have</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--emulator gem5 \
--userland userland/arch/aarch64/freestanding/linux/hello.S \
--trace Event \
--trace-stdout \
;</pre>
</div>
</div>
<div class="paragraph">
<p>At LKMC a0ea29835b9bacc6aa1cceb24c79d895315991d4 + 1 this outputs:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> 0: AtomicSimpleCPU tick.wrapped_function_event: EventFunctionWrapped event scheduled @ 0
**** REAL SIMULATION ****
0: Event_70: generic event scheduled @ 0
info: Entering event queue @ 0. Starting simulation...
0: Event_70: generic event rescheduled @ 18446744073709551615
0: AtomicSimpleCPU tick.wrapped_function_event: EventFunctionWrapped event rescheduled @ 500
500: AtomicSimpleCPU tick.wrapped_function_event: EventFunctionWrapped event rescheduled @ 1000
1000: AtomicSimpleCPU tick.wrapped_function_event: EventFunctionWrapped event rescheduled @ 1500
1500: AtomicSimpleCPU tick.wrapped_function_event: EventFunctionWrapped event rescheduled @ 2000
hello
2000: AtomicSimpleCPU tick.wrapped_function_event: EventFunctionWrapped event rescheduled @ 2500
2500: AtomicSimpleCPU tick.wrapped_function_event: EventFunctionWrapped event rescheduled @ 3000
3000: AtomicSimpleCPU tick.wrapped_function_event: EventFunctionWrapped event rescheduled @ 3500
3500: Event_71: generic event scheduled @ 3500</pre>
</div>
</div>
<div class="paragraph">
<p>which we immediately guess means that there is one event per tick when comparing to the <code>ExecAll</code> trace:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> 0: system.cpu A0 T0 : @asm_main_after_prologue : movz x0, #1, #0 : IntAlu : D=0x0000000000000001 flags=(IsInteger)
500: system.cpu A0 T0 : @asm_main_after_prologue+4 : adr x1, #28 : IntAlu : D=0x0000000000400098 flags=(IsInteger)
1000: system.cpu A0 T0 : @asm_main_after_prologue+8 : ldr w2, #4194464 : MemRead : D=0x0000000000000006 A=0x4000a0 flags=(IsInteger|IsMemRef|IsLoad)
1500: system.cpu A0 T0 : @asm_main_after_prologue+12 : movz x8, #64, #0 : IntAlu : D=0x0000000000000040 flags=(IsInteger)
2000: system.cpu A0 T0 : @asm_main_after_prologue+16 : svc #0x0 : IntAlu : flags=(IsSerializeAfter|IsNonSpeculative|IsSyscall)
hello
2500: system.cpu A0 T0 : @asm_main_after_prologue+20 : movz x0, #0, #0 : IntAlu : D=0x0000000000000000 flags=(IsInteger)
3000: system.cpu A0 T0 : @asm_main_after_prologue+24 : movz x8, #93, #0 : IntAlu : D=0x000000000000005d flags=(IsInteger)
3500: system.cpu A0 T0 : @asm_main_after_prologue+28 : svc #0x0 : IntAlu : flags=(IsSerializeAfter|IsNonSpeculative|IsSyscall)
Exiting @ tick 3500 because exiting with last active thread context</pre>
</div>
</div>
<div class="paragraph">
<p>On the event trace, we can see:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>AtomicSimpleCPU tick.wrapped_function_event: EventFunctionWrapped event scheduled @ 0</code> schedules a tick event for time <code>0</code>, and this leads to the first clock tick</p>
</li>
<li>
<p><code>0: Event_70: generic event scheduled @ 0</code>: schedules the end of time event for time <code>0</code>, which is later rescheduled to the actual end of time: <code>0: Event_70: generic event rescheduled @ 18446744073709551615</code></p>
</li>
<li>
<p>at <code>0: AtomicSimpleCPU tick.wrapped_function_event: EventFunctionWrapped event rescheduled @ 500</code> the first clock tick must have finished running, and so to represent the next one, it was simply rescheduled for the next time <code>500</code>! This is done at the end of <code>AtomicSimpleCPU::tick()</code>:</p>
<div class="literalblock">
<div class="content">
<pre>if (_status != Idle)
reschedule(tickEvent, curTick() + latency, true);</pre>
</div>
</div>
</li>
<li>
<p>at <code>3500: Event_71: generic event scheduled @ 3500</code> the exit system call is called and the simulation ends.</p>
<div class="paragraph">
<p>A new event is scheduled for the current time itself. TODO what is this event?</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Let&#8217;s study the first event. From GDB, it&#8217;s stack trace is:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Trace::OstreamLogger::logMessage() at trace.cc:149 0x5555593b3b1e
void Trace::Logger::dprintf_flag&lt;char const*, char const*, unsigned long&gt;() at 0x55555949e603
void Trace::Logger::dprintf&lt;char const*, char const*, unsigned long&gt;() at 0x55555949de58
Event::trace() at eventq.cc:395 0x55555946d109
EventQueue::schedule() at eventq_impl.hh:65 0x555557195441
EventManager::schedule() at eventq.hh:746 0x555557194aa2
AtomicSimpleCPU::activateContext() at atomic.cc:239 0x555559075531
SimpleThread::activate() at simple_thread.cc:177 0x555559545a63
Process::initState() at process.cc:283 0x555559484011
ArmProcess64::initState() at process.cc:126 0x55555730827a
ArmLinuxProcess64::initState() at process.cc:1,777 0x5555572d5e5e</pre>
</div>
</div>
<div class="paragraph">
<p>The interesting call is at <code>AtomicSimpleCPU::activateContext</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>schedule(tickEvent, clockEdge(Cycles(0)));</pre>
</div>
</div>
<div class="paragraph">
<p>which calls <code>EventManager::schedule</code>.</p>
</div>
<div class="paragraph">
<p><code>AtomicSimpleCPU</code> is an <code>EventManager</code> because <code>SimObject</code> inherits from it.</p>
</div>
<div class="paragraph">
<p><code>tickEvent</code> is an <code>EventFunctionWrapper</code> which contains a <code>std::function&lt;void(void)&gt; callback;</code>, and is initialized in the constructor as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>tickEvent([this]{ tick(); }, "AtomicSimpleCPU tick",
false, Event::CPU_Tick_Pri),</pre>
</div>
</div>
<div class="paragraph">
<p>So that&#8217;s how the main atomic tick loop works, fully understood!</p>
</div>
<div class="paragraph">
<p>The second event has backtrace:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Trace::OstreamLogger::logMessage() at trace.cc:149 0x5555593b3b1e
void Trace::Logger::dprintf_flag&lt;char const*, char const*, unsigned long&gt;() at 0x55555949e603
void Trace::Logger::dprintf&lt;char const*, char const*, unsigned long&gt;() at 0x55555949de58
Event::trace() at eventq.cc:395 0x55555946d109
EventQueue::schedule() at eventq_impl.hh:65 0x555557195441
BaseGlobalEvent::schedule() at global_event.cc:78 0x55555946d6f1
GlobalEvent::GlobalEvent() at 0x55555949d177
GlobalSimLoopExitEvent::GlobalSimLoopExitEvent() at sim_events.cc:61 0x555559474470
simulate() at simulate.cc:104 0x555559476d6f</pre>
</div>
</div>
<div class="paragraph">
<p>so gets scheduled automatically at object creation <code>simulate()</code> through the <code>GlobalEvent()</code> constructor:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>simulate_limit_event =
new GlobalSimLoopExitEvent(mainEventQueue[0]-&gt;getCurTick(),
"simulate() limit reached", 0);</pre>
</div>
</div>
<div class="paragraph">
<p>This event indicates that the simulation should finish by overriding <code>bool isExitEvent()</code> which gets checked in the main simulation at <code>EventQueue::serviceOne</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>if (event-&gt;isExitEvent()) {
assert(!event-&gt;flags.isSet(Event::Managed) ||
!event-&gt;flags.isSet(Event::IsMainQueue)); // would be silly
return event;</pre>
</div>
</div>
<div class="paragraph">
<p>And at long, we can guess without reading the code that <code>Event_71</code> is comes from the SE implementation of the exit syscall, so let&#8217;s just confirm, the trace contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>exitSimLoop() at sim_events.cc:97 0x5555594746e0
exitImpl() at syscall_emul.cc:215 0x55555948c046
exitFunc() at syscall_emul.cc:225 0x55555948c147
SyscallDesc::doSyscall() at syscall_desc.cc:72 0x5555594949b6
Process::syscall() at process.cc:401 0x555559484717
SimpleThread::syscall() at 0x555559558059
ArmISA::SupervisorCall::invoke() at faults.cc:856 0x5555572950d7
BaseSimpleCPU::advancePC() at base.cc:681 0x555559083133
AtomicSimpleCPU::tick() at atomic.cc:757 0x55555907834c</pre>
</div>
</div>
<div class="paragraph">
<p>and <code>exitSimLoop()</code> does:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>new GlobalSimLoopExitEvent(when + simQuantum, message, exit_code, repeat);</pre>
</div>
</div>
<div class="paragraph">
<p>Tested at b4879ae5b0b6644e6836b0881e4da05c64a6550d.</p>
</div>
</div>
<div class="sect4">
<h5 id="gem5-event-queue-minorcpu-syscall-emulation-freestanding-example-analysis"><a class="anchor" href="#gem5-event-queue-minorcpu-syscall-emulation-freestanding-example-analysis"></a><a class="link" href="#gem5-event-queue-minorcpu-syscall-emulation-freestanding-example-analysis">19.19.4.2. gem5 event queue MinorCPU syscall emulation freestanding example analysis</a></h5>
<div class="paragraph">
<p>The events <a href="#gem5-event-queue-atomicsimplecpu-syscall-emulation-freestanding-example-analysis">for the Atomic CPU</a> were pretty simple: basically just ticks.</p>
</div>
<div class="paragraph">
<p>But as we venture into <a href="#gem5-cpu-types">more complex CPU models</a> such as <code>MinorCPU</code>, the events get much more complex and interesting.</p>
</div>
<div class="paragraph">
<p>TODO: analyze the trace for:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--emulator gem5 \
--userland userland/arch/aarch64/freestanding/linux/hello.S \
--trace Event \
--trace-stdout \
-- \
--cpu-type MinorCPU \
--caches \
;</pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="gem5-stats-internals"><a class="anchor" href="#gem5-stats-internals"></a><a class="link" href="#gem5-stats-internals">19.19.5. gem5 stats internals</a></h4>
<div class="paragraph">
<p>This describes the internals of the <a href="#gem5-m5out-stats-txt-file">gem5 m5out/stats.txt file</a>.</p>
</div>
<div class="paragraph">
<p>GDB call stack to <code>dumpstats</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Stats::pythonDump () at build/ARM/python/pybind11/stats.cc:58
Stats::StatEvent::process() ()
GlobalEvent::BarrierEvent::process (this=0x555559fa6a80) at build/ARM/sim/global_event.cc:131
EventQueue::serviceOne (this=this@entry=0x555558c36080) at build/ARM/sim/eventq.cc:228
doSimLoop (eventq=0x555558c36080) at build/ARM/sim/simulate.cc:219
simulate (num_cycles=&lt;optimized out&gt;) at build/ARM/sim/simulate.cc:132</pre>
</div>
</div>
<div class="paragraph">
<p><code>Stats::pythonDump</code> does:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>void
pythonDump()
{
py::module m = py::module::import("m5.stats");
m.attr("dump")();
}</pre>
</div>
</div>
<div class="paragraph">
<p>This calls <code>src/python/m5/stats/<em>init</em>.py</code> in <code>def dump</code> does the main dumping</p>
</div>
<div class="paragraph">
<p>That function does notably:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> for output in outputList:
if output.valid():
output.begin()
for stat in stats_list:
stat.visit(output)
output.end()</pre>
</div>
</div>
<div class="paragraph">
<p><code>begin</code> and <code>end</code> are defined in C++ and output the header and tail respectively</p>
</div>
<div class="literalblock">
<div class="content">
<pre>void
Text::begin()
{
ccprintf(*stream, "\n---------- Begin Simulation Statistics ----------\n");
}
void
Text::end()
{
ccprintf(*stream, "\n---------- End Simulation Statistics ----------\n");
stream-&gt;flush();
}</pre>
</div>
</div>
<div class="paragraph">
<p><code>stats_list</code> contains the stats, and <code>stat.visit</code> prints them, <code>outputList</code> contains by default just the text output. I don&#8217;t see any other types of output in gem5, but likely JSON / binary formats could be envisioned.</p>
</div>
<div class="paragraph">
<p>Tested in gem5 b4879ae5b0b6644e6836b0881e4da05c64a6550d.</p>
</div>
</div>
<div class="sect3">
<h4 id="gem5-code-generation"><a class="anchor" href="#gem5-code-generation"></a><a class="link" href="#gem5-code-generation">19.19.6. gem5 code generation</a></h4>
<div class="paragraph">
<p>gem5 uses a ton of code generation, which makes the project horrendous:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>lots of magic happen on top of pybind11, which is already magic, to more automatically glue the C++ and Python worlds: <a href="#gem5-python-c-interaction">gem5 Python C++ interaction</a></p>
</li>
<li>
<p>.isa code which describes most of the instructions</p>
</li>
<li>
<p><a href="#gem5-ruby-build">Ruby</a> for memory systems</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>To find the definition of generated code, do a:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>grep -I -r build/ 'code of interest'</pre>
</div>
</div>
<div class="paragraph">
<p>where:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>-I</code>: ignore binray file matches on built objects</p>
</li>
<li>
<p><code>-r</code>: ignore symlinks due to <a href="#why-are-all-c-symlinked-into-the-gem5-build-dir">Why are all C++ symlinked into the gem5 build dir?</a> as explained at <a href="https://stackoverflow.com/questions/21738574/how-do-you-exclude-symlinks-in-a-grep" class="bare">https://stackoverflow.com/questions/21738574/how-do-you-exclude-symlinks-in-a-grep</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The code generation exists partly to support insanely generic cross ISA instructions mapping to one compute model, where it might be reasonable.</p>
</div>
<div class="paragraph">
<p>But it has been widely overused to insanity. It likely also exists partly because when the project started in 2003 C++ compilers weren&#8217;t that good, so you couldn&#8217;t rely on features like templates that much.</p>
</div>
<div class="sect4">
<h5 id="gem5-the_isa"><a class="anchor" href="#gem5-the_isa"></a><a class="link" href="#gem5-the_isa">19.19.6.1. gem5 THE_ISA</a></h5>
<div class="paragraph">
<p>Generated code at: <code>build/&lt;ISA&gt;/config/the_isa.hh</code> which contains amongst other lines:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>#define X86_ISA 8
enum class Arch {
X86ISA = X86_ISA
};
#define THE_ISA X86_ISA</pre>
</div>
</div>
<div class="paragraph">
<p>Generation code: <code>src/SConscript</code> at <code>def makeTheISA</code>.</p>
</div>
<div class="paragraph">
<p>Tested on gem5 211869ea950f3cc3116655f06b1d46d3fa39fb3a.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://www.mail-archive.com/gem5-users@gem5.org/msg16989.html" class="bare">https://www.mail-archive.com/gem5-users@gem5.org/msg16989.html</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="gem5-build-system"><a class="anchor" href="#gem5-build-system"></a><a class="link" href="#gem5-build-system">19.19.7. gem5 build system</a></h4>
<div class="sect4">
<h5 id="gem5-polymorphic-isa-includes"><a class="anchor" href="#gem5-polymorphic-isa-includes"></a><a class="link" href="#gem5-polymorphic-isa-includes">19.19.7.1. gem5 polymorphic ISA includes</a></h5>
<div class="paragraph">
<p>E.g. <code>src/cpu/decode_cache.hh</code> includes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>#include "arch/isa_traits.hh"</pre>
</div>
</div>
<div class="paragraph">
<p>which in turn is meant to refer to files of form:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>src/arch/&lt;isa&gt;/isa_traits.hh</pre>
</div>
</div>
<div class="paragraph">
<p>What happens is that the build system creates a file:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>build/ARM/arch/isa_traits.hh</pre>
</div>
</div>
<div class="paragraph">
<p>which contains just:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>#include "arch/arm/isa_traits.hh"</pre>
</div>
</div>
<div class="paragraph">
<p>and puts that in the <code>-I</code> include path during build.</p>
</div>
<div class="paragraph">
<p>It appears to be possible to deal with it using preprocessor macros, but it is ugly: <a href="https://stackoverflow.com/questions/3178946/using-define-to-include-another-file-in-c-c/3179218#3179218" class="bare">https://stackoverflow.com/questions/3178946/using-define-to-include-another-file-in-c-c/3179218#3179218</a></p>
</div>
<div class="paragraph">
<p>In addition to the header polymorphism, gem5 also namespaces classes with <code>TheISA::</code>, e.g. in <code>src/cpu/decode_cache.hh</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Value items[TheISA::PageBytes];</pre>
</div>
</div>
<div class="paragraph">
<p>which is defined at:</p>
</div>
<div class="paragraph">
<p>&#8230;&#8203;
build/ARM/config/the_isa.hh
&#8230;&#8203;</p>
</div>
<div class="paragraph">
<p>as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>#define TheISA ArmISA</pre>
</div>
</div>
<div class="paragraph">
<p>and forces already <code>arm/</code> specific headers to define their symbols under:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>namespace ArmISA</pre>
</div>
</div>
<div class="paragraph">
<p>so I don&#8217;t see the point of this pattern, why not just us <code>PageBytes</code> directly? Looks like a documentation mechanism to indicate that a certain symbol is ISA specific.</p>
</div>
<div class="paragraph">
<p>Tested in gem5 2a242c5f59a54bc6b8953f82486f7e6fe0aa9b3d.</p>
</div>
</div>
<div class="sect4">
<h5 id="why-are-all-c-symlinked-into-the-gem5-build-dir"><a class="anchor" href="#why-are-all-c-symlinked-into-the-gem5-build-dir"></a><a class="link" href="#why-are-all-c-symlinked-into-the-gem5-build-dir">19.19.7.2. Why are all C++ symlinked into the gem5 build dir?</a></h5>
<div class="paragraph">
<p>Some scons madness.</p>
</div>
<div class="paragraph">
<p><a href="https://scons.org/doc/2.4.1/HTML/scons-user.html#idp1378838508" class="bare">https://scons.org/doc/2.4.1/HTML/scons-user.html#idp1378838508</a> generates hard links by default.</p>
</div>
<div class="paragraph">
<p>Then the a5bc2291391b0497fdc60fdc960e07bcecebfb8f SConstruct use symlinks in a futile attempt to make things better for editors or build systems from the past century.</p>
</div>
<div class="paragraph">
<p>It was not possible to disable the symlinks automatically for the entire project when I last asked: <a href="https://stackoverflow.com/questions/53656787/how-to-set-disable-duplicate-0-for-all-scons-build-variants-without-repeating-th" class="bare">https://stackoverflow.com/questions/53656787/how-to-set-disable-duplicate-0-for-all-scons-build-variants-without-repeating-th</a></p>
</div>
<div class="paragraph">
<p>The horrendous downsides of this are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>when <a href="#debug-the-emulator">debugging the emulator</a>, it shows you directories inside the build directory rather than in the source tree</p>
</li>
<li>
<p>it is harder to separate which files are <a href="#gem5-code-generation">generated</a> and which are in-tree when grepping for code generated definitions</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="buildroot"><a class="anchor" href="#buildroot"></a><a class="link" href="#buildroot">20. Buildroot</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="introduction-to-buildroot"><a class="anchor" href="#introduction-to-buildroot"></a><a class="link" href="#introduction-to-buildroot">20.1. Introduction to Buildroot</a></h3>
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/Buildroot">Buildroot</a> is a set of Make scripts that download and compile from source compatible versions of:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>GCC</p>
</li>
<li>
<p>Linux kernel</p>
</li>
<li>
<p>C standard library: Buildroot supports several implementations, see: <a href="#libc-choice">Section 20.10, &#8220;libc choice&#8221;</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/BusyBox">BusyBox</a>: provides the shell and basic command line utilities</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>It therefore produces a pristine, blob-less, debuggable setup, where all moving parts are configured to work perfectly together.</p>
</div>
<div class="paragraph">
<p>Perhaps the awesomeness of Buildroot only sinks in once you notice that all it takes is 4 commands as explained at <a href="#buildroot-hello-world">Section 20.11, &#8220;Buildroot hello world&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>This repo basically wraps around that, and tries to make everything even more awesome for kernel developers.</p>
</div>
<div class="paragraph">
<p>The downsides of Buildroot are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the first build takes a while, but it is well worth it</p>
</li>
<li>
<p>the selection of software packages is relatively limited if compared to Debian, e.g. no Java or Python package in guest out of the box.</p>
<div class="paragraph">
<p>In theory, any software can be packaged, and the Buildroot side is easy.</p>
</div>
<div class="paragraph">
<p>The hard part is dealing with crappy third party build systems and huge dependency chains.</p>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="custom-buildroot-configs"><a class="anchor" href="#custom-buildroot-configs"></a><a class="link" href="#custom-buildroot-configs">20.2. Custom Buildroot configs</a></h3>
<div class="paragraph">
<p>We provide the following mechanisms:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>./build-buildroot --config-fragment data/br2</code>: append the Buildroot configuration file <code>data/br2</code> to a single build. Must be passed every time you run <code>./build</code>. The format is the same as <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_config/default">buildroot_config/default</a>.</p>
</li>
<li>
<p><code>./build-buildroot --config 'BR2_SOME_OPTION="myval"'</code>: append a single option to a single build.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>For example, if you decide to <a href="#enable-buildroot-compiler-optimizations">Enable Buildroot compiler optimizations</a> after an initial build is finished, you must <a href="#clean-the-build">Clean the build</a> and rebuild:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot \
--config 'BR2_OPTIMIZE_3=y' \
--config 'BR2_PACKAGE_SAMPLE_PACKAGE=y' \
--
sample_package-dirclean \
sample_package-reconfigure \
;</pre>
</div>
</div>
<div class="paragraph">
<p>as explained at: <a href="https://buildroot.org/downloads/manual/manual.html#rebuild-pkg" class="bare">https://buildroot.org/downloads/manual/manual.html#rebuild-pkg</a></p>
</div>
<div class="paragraph">
<p>The clean is necessary because the source files didn&#8217;t change, so <code>make</code> would just check the timestamps and not build anything.</p>
</div>
<div class="paragraph">
<p>You will then likely want to make those more permanent as explained at: <a href="#default-command-line-arguments">Section 31.4, &#8220;Default command line arguments&#8221;</a>.</p>
</div>
<div class="sect3">
<h4 id="enable-buildroot-compiler-optimizations"><a class="anchor" href="#enable-buildroot-compiler-optimizations"></a><a class="link" href="#enable-buildroot-compiler-optimizations">20.2.1. Enable Buildroot compiler optimizations</a></h4>
<div class="paragraph">
<p>If you are benchmarking compiled programs instead of hand written assembly, remember that we configure Buildroot to disable optimizations by default with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>BR2_OPTIMIZE_0=y</pre>
</div>
</div>
<div class="paragraph">
<p>to improve the debugging experience.</p>
</div>
<div class="paragraph">
<p>You will likely want to change that to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>BR2_OPTIMIZE_3=y</pre>
</div>
</div>
<div class="paragraph">
<p>Our <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_packages/sample_package">buildroot_packages/sample_package</a> package correctly forwards the Buildroot options to the build with <code>$(TARGET_CONFIGURE_OPTS)</code>, so you don&#8217;t have to do any extra work.</p>
</div>
<div class="paragraph">
<p>Don&#8217;t forget to do that if you are <a href="#add-new-buildroot-packages">adding a new package</a> with your own build system.</p>
</div>
<div class="paragraph">
<p>Then, you have two choices:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>if you already have a full <code>-O0</code> build, you can choose to rebuild just your package of interest to save some time as described at: <a href="#custom-buildroot-configs">Section 20.2, &#8220;Custom Buildroot configs&#8221;</a></p>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot \
--config 'BR2_OPTIMIZE_3=y' \
--config 'BR2_PACKAGE_SAMPLE_PACKAGE=y' \
-- \
sample_package-dirclean \
sample_package-reconfigure \
;</pre>
</div>
</div>
<div class="paragraph">
<p>However, this approach might not be representative since calls to an unoptimized libc and other libraries will have a negative performance impact.</p>
</div>
<div class="paragraph">
<p>Maybe you can get away with rebuilding libc, but I&#8217;m not sure that it will work properly.</p>
</div>
<div class="paragraph">
<p>Kernel-wise it should be fine though as mentioned at: <a href="#kernel-o0">Section 2.1.2, &#8220;Disable kernel compiler optimizations&#8221;</a></p>
</div>
</li>
<li>
<p><a href="#clean-the-build">clean the build</a> and rebuild from scratch:</p>
<div class="literalblock">
<div class="content">
<pre>mv out out~
./build-buildroot --config 'BR2_OPTIMIZE_3=y'</pre>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="find-buildroot-options-with-make-menuconfig"><a class="anchor" href="#find-buildroot-options-with-make-menuconfig"></a><a class="link" href="#find-buildroot-options-with-make-menuconfig">20.3. Find Buildroot options with make menuconfig</a></h3>
<div class="paragraph">
<p><code>make menuconfig</code> is a convenient way to find Buildroot configurations:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd "$(./getvar buildroot_build_dir)"
make menuconfig</pre>
</div>
</div>
<div class="paragraph">
<p>Hit <code>/</code> and search for the settings.</p>
</div>
<div class="paragraph">
<p>Save and quit.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>diff -u .config.olg .config</pre>
</div>
</div>
<div class="paragraph">
<p>Then copy and paste the diff additions to <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_config/default">buildroot_config/default</a> to make them permanent.</p>
</div>
</div>
<div class="sect2">
<h3 id="change-user"><a class="anchor" href="#change-user"></a><a class="link" href="#change-user">20.4. Change user</a></h3>
<div class="paragraph">
<p>At startup, we login automatically as the <code>root</code> user.</p>
</div>
<div class="paragraph">
<p>If you want to switch to another user to test some permissions, we have already created an <code>user0</code> user through the <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/user_table">user_table</a> file, and you can just login as that user with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>login user0</pre>
</div>
</div>
<div class="paragraph">
<p>and password:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>a</pre>
</div>
</div>
<div class="paragraph">
<p>Then test that the user changed with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>id</pre>
</div>
</div>
<div class="paragraph">
<p>which gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>uid=1000(user0) gid=1000(user0) groups=1000(user0)</pre>
</div>
</div>
<div class="sect3">
<h4 id="login-as-a-non-root-user-without-password"><a class="anchor" href="#login-as-a-non-root-user-without-password"></a><a class="link" href="#login-as-a-non-root-user-without-password">20.4.1. Login as a non-root user without password</a></h4>
<div class="paragraph">
<p>Replace on <code>inittab</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>::respawn:-/bin/sh</pre>
</div>
</div>
<div class="paragraph">
<p>with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>::respawn:-/bin/login -f user0</pre>
</div>
</div>
<div class="paragraph">
<p><code>-f</code> forces login without asking for the password.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="add-new-buildroot-packages"><a class="anchor" href="#add-new-buildroot-packages"></a><a class="link" href="#add-new-buildroot-packages">20.5. Add new Buildroot packages</a></h3>
<div class="paragraph">
<p>First, see if you can&#8217;t get away without actually adding a new package, for example:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>if you have a standalone C file with no dependencies besides the C standard library to be compiled with GCC, just add a new file under <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_packages/sample_package">buildroot_packages/sample_package</a> and you are done</p>
</li>
<li>
<p>if you have a dependency on a library, first check if Buildroot doesn&#8217;t have a package for it already with <code>ls buildroot/package</code>. If yes, just enable that package as explained at: <a href="#custom-buildroot-configs">Section 20.2, &#8220;Custom Buildroot configs&#8221;</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>If none of those methods are flexible enough for you, you can just fork or hack up <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_packages/sample_package">buildroot_packages/sample_package</a> the sample package to do what you want.</p>
</div>
<div class="paragraph">
<p>For how to use that package, see: <a href="#buildroot_packages-directory">Section 31.12.2, &#8220;buildroot_packages directory&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Then iterate trying to do what you want and reading the manual until it works: <a href="https://buildroot.org/downloads/manual/manual.html" class="bare">https://buildroot.org/downloads/manual/manual.html</a></p>
</div>
</div>
<div class="sect2">
<h3 id="remove-buildroot-packages"><a class="anchor" href="#remove-buildroot-packages"></a><a class="link" href="#remove-buildroot-packages">20.6. Remove Buildroot packages</a></h3>
<div class="paragraph">
<p>Once you&#8217;ve built a package in to the image, there is no easy way to remove it.</p>
</div>
<div class="paragraph">
<p>Documented at: <a href="https://github.com/buildroot/buildroot/blob/2017.08/docs/manual/rebuilding-packages.txt#L90" class="bare">https://github.com/buildroot/buildroot/blob/2017.08/docs/manual/rebuilding-packages.txt#L90</a></p>
</div>
<div class="paragraph">
<p>Also mentioned at: <a href="https://stackoverflow.com/questions/47320800/how-to-clean-only-target-in-buildroot" class="bare">https://stackoverflow.com/questions/47320800/how-to-clean-only-target-in-buildroot</a></p>
</div>
<div class="paragraph">
<p>See this for a sample manual workaround: <a href="#parsec-uninstall">Section 19.2.3.5.4, &#8220;PARSEC uninstall&#8221;</a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="br2_target_rootfs_ext2_size"><a class="anchor" href="#br2_target_rootfs_ext2_size"></a><a class="link" href="#br2_target_rootfs_ext2_size">20.7. BR2_TARGET_ROOTFS_EXT2_SIZE</a></h3>
<div class="paragraph">
<p>When adding new large package to the Buildroot root filesystem, it may fail with the message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Maybe you need to increase the filesystem size (BR2_TARGET_ROOTFS_EXT2_SIZE)</pre>
</div>
</div>
<div class="paragraph">
<p>The solution is to simply add:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_TARGET_ROOTFS_EXT2_SIZE="512M"'</pre>
</div>
</div>
<div class="paragraph">
<p>where 512Mb is "large enough".</p>
</div>
<div class="paragraph">
<p>Note that dots cannot be used as in <code>1.5G</code>, so just use Megs as in <code>1500M</code> instead.</p>
</div>
<div class="paragraph">
<p>Unfortunately, TODO we don&#8217;t have a perfect way to find the right value for <code>BR2_TARGET_ROOTFS_EXT2_SIZE</code>. One good heuristic is:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>du -hsx "$(./getvar --arch arm buildroot_target_dir)"</pre>
</div>
</div>
<div class="paragraph">
<p>Some promising ways to overcome this problem include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#squashfs">SquashFS</a>
TODO benchmark: would gem5 suffer a considerable disk read performance hit due to decompressing SquashFS?</p>
</li>
<li>
<p>libguestfs: <a href="https://serverfault.com/questions/246835/convert-directory-to-qemu-kvm-virtual-disk-image/916697#916697" class="bare">https://serverfault.com/questions/246835/convert-directory-to-qemu-kvm-virtual-disk-image/916697#916697</a>, in particular <a href="http://libguestfs.org/guestfish.1.html#vfs-minimum-size"><code>vfs-minimum-size</code></a></p>
</li>
<li>
<p>use methods described at: <a href="#gem5-restore-new-script">Section 19.5.2, &#8220;gem5 checkpoint restore and run a different script&#8221;</a> instead of putting builds on the root filesystem</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/49211241/is-there-a-way-to-automatically-detect-the-minimum-required-br2-target-rootfs-ex" class="bare">https://stackoverflow.com/questions/49211241/is-there-a-way-to-automatically-detect-the-minimum-required-br2-target-rootfs-ex</a></p>
</div>
<div class="sect3">
<h4 id="squashfs"><a class="anchor" href="#squashfs"></a><a class="link" href="#squashfs">20.7.1. SquashFS</a></h4>
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/SquashFS">SquashFS</a> creation with <code>mksquashfs</code> does not take fixed sizes, and I have successfully booted from it, but it is readonly, which is unacceptable.</p>
</div>
<div class="paragraph">
<p>But then we could mount <a href="https://wiki.debian.org/ramfs">ramfs</a> on top of it with <a href="#overlayfs">OverlayFS</a> to make it writable, but my attempts failed exactly as mentioned at <a href="#overlayfs">OverlayFS</a>.</p>
</div>
<div class="paragraph">
<p>This is the exact unanswered question: <a href="https://unix.stackexchange.com/questions/343484/mounting-squashfs-image-with-read-write-overlay-for-rootfs" class="bare">https://unix.stackexchange.com/questions/343484/mounting-squashfs-image-with-read-write-overlay-for-rootfs</a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="rpath"><a class="anchor" href="#rpath"></a><a class="link" href="#rpath">20.8. Buildroot rebuild is slow when the root filesystem is large</a></h3>
<div class="paragraph">
<p>Buildroot is not designed for large root filesystem images, and the rebuild becomes very slow when we add a large package to it.</p>
</div>
<div class="paragraph">
<p>This is due mainly to the <code>pkg-generic</code> <code>GLOBAL_INSTRUMENTATION_HOOKS</code> sanitation which go over the entire tree doing complex operations&#8230;&#8203; I no like, in particular <code>check_bin_arch</code> and <code>check_host_rpath</code></p>
</div>
<div class="paragraph">
<p>We have applied <a href="https://github.com/cirosantilli/buildroot/commit/983fe7910a73923a4331e7d576a1e93841d53812">983fe7910a73923a4331e7d576a1e93841d53812</a> to out Buildroot fork which removes part of the pain by not running:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&gt;&gt;&gt; Sanitizing RPATH in target tree</pre>
</div>
</div>
<div class="paragraph">
<p>which contributed to a large part of the slowness.</p>
</div>
<div class="paragraph">
<p>Test how Buildroot deals with many files with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot \
--config 'BR2_PACKAGE_LKMC_MANY_FILES=y' \
-- \
lkmc_many_files-reconfigure \
|&amp; \
ts -i '%.s' \
;
./build-buildroot |&amp; ts -i '%.s'</pre>
</div>
</div>
<div class="paragraph">
<p>and notice how the second build, which does not rebuilt the package at all, still gets stuck in the <code>RPATH</code> check forever without our Buildroot patch.</p>
</div>
</div>
<div class="sect2">
<h3 id="report-upstream-bugs"><a class="anchor" href="#report-upstream-bugs"></a><a class="link" href="#report-upstream-bugs">20.9. Report upstream bugs</a></h3>
<div class="paragraph">
<p>When asking for help on upstream repositories outside of this repository, you will need to provide the commands that you are running in detail without referencing our scripts.</p>
</div>
<div class="paragraph">
<p>For example, QEMU developers will only want to see the final QEMU command that you are running.</p>
</div>
<div class="paragraph">
<p>For the configure and build, search for the <code>Building</code> and <code>Configuring</code> parts of the build log, then try to strip down all Buildroot related paths, to keep only options that seem to matter.</p>
</div>
<div class="paragraph">
<p>We make that easy by building commands as strings, and then echoing them before evaling.</p>
</div>
<div class="paragraph">
<p>So for example when you run:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm</pre>
</div>
</div>
<div class="paragraph">
<p>the very first stdout output of that script is the actual QEMU command that is being run.</p>
</div>
<div class="paragraph">
<p>The command is also saved to a file for convenience:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat "$(./getvar --arch arm run_cmd_file)"</pre>
</div>
</div>
<div class="paragraph">
<p>which you can manually modify and execute during your experiments later:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vim "$(./getvar --arch arm run_cmd_file)"
./"$(./getvar --arch arm run_cmd_file)"</pre>
</div>
</div>
<div class="paragraph">
<p>If you are not already on the master of the given component, you can do that neatly with <a href="#build-variants">Build variants</a>.</p>
</div>
<div class="paragraph">
<p>E.g., to check if a QEMU bug is still present on <code>master</code>, you can do as explained at <a href="#qemu-build-variants">QEMU build variants</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git -C "$(./getvar qemu_source_dir)" checkout master
./build-qemu --clean --qemu-build-id master
./build-qemu --qemu-build-id master
git -C "$(./getvar qemu_source_dir)" checkout -
./run --qemu-build-id master</pre>
</div>
</div>
<div class="paragraph">
<p>Then, you will also want to do a <a href="#bisection">Bisection</a> to pinpoint the exact commit to blame, and CC that developer.</p>
</div>
<div class="paragraph">
<p>Finally, give the images you used save upstream developers' time as shown at: <a href="#release-zip">Section 31.17.2, &#8220;release-zip&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>For Buildroot problems, you should wither provide the config you have:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./getvar buildroot_config_file</pre>
</div>
</div>
<div class="paragraph">
<p>or try to reproduce with a minimal config, see: <a href="https://github.com/cirosantilli/buildroot/tree/in-tree-package-master" class="bare">https://github.com/cirosantilli/buildroot/tree/in-tree-package-master</a></p>
</div>
</div>
<div class="sect2">
<h3 id="libc-choice"><a class="anchor" href="#libc-choice"></a><a class="link" href="#libc-choice">20.10. libc choice</a></h3>
<div class="paragraph">
<p>Buildroot supports several libc implementations, including:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://en.wikipedia.org/wiki/GNU_C_Library">glibc</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/UClibc">uClibc</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We currently use glibc, which is selected by:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>BR2_TOOLCHAIN_BUILDROOT_GLIBC=y</pre>
</div>
</div>
<div class="paragraph">
<p>Ideally we would like to use uClibc, as it is more minimal and easier to understand, but unfortunately there are some very few packages that use some weird glibc extension that uClibc hasn&#8217;t implemented yet, e.g.:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#selinux">SELinux</a>. Trivial unmerged fix at: <a href="http://lists.busybox.net/pipermail/buildroot/2017-July/197793.html" class="bare">http://lists.busybox.net/pipermail/buildroot/2017-July/197793.html</a> just missing the uClibc option to expose <code>fts.h</code>&#8230;&#8203;</p>
</li>
<li>
<p><a href="#stress">stress</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The full list of unsupported packages can be found by grepping the Buildroot source:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git -C "$(./getvar buildroot_source_dir)" grep 'depends on BR2_TOOLCHAIN_USES_GLIBC'</pre>
</div>
</div>
<div class="paragraph">
<p>One "downside" of glibc is that it exercises much more kernel functionality on its more bloated pre-main init, which breaks user mode C hello worlds more often, see: <a href="#user-mode-simulation-with-glibc">Section 10.4, &#8220;User mode simulation with glibc&#8221;</a>. I quote "downside" because glibc is actually exposing emulator bugs which we should actually go and fix.</p>
</div>
</div>
<div class="sect2">
<h3 id="buildroot-hello-world"><a class="anchor" href="#buildroot-hello-world"></a><a class="link" href="#buildroot-hello-world">20.11. Buildroot hello world</a></h3>
<div class="paragraph">
<p>This repo doesn&#8217;t do much more other than setting a bunch of Buildroot configurations and building it.</p>
</div>
<div class="paragraph">
<p>The minimal work you have to do to get QEMU to boot Buildroot from scratch is tiny if, about 4 commands!</p>
</div>
<div class="paragraph">
<p>Here are some good working commands for several ISAs:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>x86_64 <a href="https://unix.stackexchange.com/questions/44062/how-to-use-qemu-to-run-build-root-linux-images/543075#543075" class="bare">https://unix.stackexchange.com/questions/44062/how-to-use-qemu-to-run-build-root-linux-images/543075#543075</a></p>
<div class="ulist">
<ul>
<li>
<p>x86_64 X11 <a href="https://unix.stackexchange.com/questions/70931/how-to-install-x11-on-my-own-linux-buildroot-system/306116#306116" class="bare">https://unix.stackexchange.com/questions/70931/how-to-install-x11-on-my-own-linux-buildroot-system/306116#306116</a> Also mentioned at: <a href="#x11">Section 13.4, &#8220;X11 Buildroot&#8221;</a>.</p>
</li>
</ul>
</div>
</li>
<li>
<p>aarch64 <a href="https://stackoverflow.com/questions/47557262/how-to-download-the-torvalds-linux-kernel-master-recompile-it-and-boot-it-wi/49349237#49349237" class="bare">https://stackoverflow.com/questions/47557262/how-to-download-the-torvalds-linux-kernel-master-recompile-it-and-boot-it-wi/49349237#49349237</a></p>
<div class="ulist">
<ul>
<li>
<p>aarch64 U-Boot: <a href="https://stackoverflow.com/questions/58028789/how-to-boot-linux-aarch64-with-u-boot-with-buildroot-on-qemu" class="bare">https://stackoverflow.com/questions/58028789/how-to-boot-linux-aarch64-with-u-boot-with-buildroot-on-qemu</a> Also mentioned at: <a href="#u-boot">U-Boot</a>.</p>
</li>
</ul>
</div>
</li>
<li>
<p>arm <a href="https://stackoverflow.com/questions/38320066/how-to-run-linux-on-a-qemu-arm-versatile-machine/44099299#44099299" class="bare">https://stackoverflow.com/questions/38320066/how-to-run-linux-on-a-qemu-arm-versatile-machine/44099299#44099299</a></p>
</li>
<li>
<p>PPC <a href="https://stackoverflow.com/questions/48021127/build-powerpc-kernel-and-boot-powerpc-kernel-on-qemu/49349262#49349262" class="bare">https://stackoverflow.com/questions/48021127/build-powerpc-kernel-and-boot-powerpc-kernel-on-qemu/49349262#49349262</a> just work commands for PPC, comment on how to replace kernel</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>These can come in handy if you want to debug something in Buildroot itself and possibly report an upstream bug.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="userland-content"><a class="anchor" href="#userland-content"></a><a class="link" href="#userland-content">21. Userland content</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>This section contains userland content, such as <a href="#c">C</a>, <a href="#cpp">C++</a> and <a href="#posix">POSIX</a> examples.</p>
</div>
<div class="paragraph">
<p>Getting started at: <a href="#userland-setup">Section 1.6, &#8220;Userland setup&#8221;</a></p>
</div>
<div class="paragraph">
<p>Userland assembly content is located at: <a href="#userland-assembly">Section 22, &#8220;Userland assembly&#8221;</a>. It was split from this section basically because we were hitting the HTML <code>h6</code> limit, stupid web :-)</p>
</div>
<div class="paragraph">
<p>This content makes up the bulk of the <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/">userland/</a> directory.</p>
</div>
<div class="paragraph">
<p>The quickest way to run the arch agnostic examples, which comprise the majority of the examples, is natively as shown at: <a href="#userland-setup-getting-started-natively">Section 1.6.2.1, &#8220;Userland setup getting started natively&#8221;</a></p>
</div>
<div class="paragraph">
<p>This section was originally moved in here from: <a href="https://github.com/cirosantilli/cpp-cheat" class="bare">https://github.com/cirosantilli/cpp-cheat</a></p>
</div>
<div class="sect2">
<h3 id="c"><a class="anchor" href="#c"></a><a class="link" href="#c">21.1. C</a></h3>
<div class="paragraph">
<p>Programs under <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/">userland/c/</a> are examples of <a href="https://en.wikipedia.org/wiki/ANSI_C">ANSI C</a> programming:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/hello.c">userland/c/hello.c</a></p>
</li>
<li>
<p><code>main</code> and environment</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/return0.c">userland/c/return0.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/return1.c">userland/c/return1.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/return2.c">userland/c/return2.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/exit0.c">userland/c/exit0.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/exit1.c">userland/c/exit1.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/exit2.c">userland/c/exit2.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/command_line_arguments.c">userland/c/command_line_arguments.c</a>: print one command line argument per line using <code>argc</code> and <code>argv</code>.</p>
<div class="paragraph">
<p>Good sanity check for user mode: <a href="#qemu-user-mode-getting-started">QEMU user mode getting started</a></p>
</div>
</li>
</ul>
</div>
</li>
<li>
<p>Standard library</p>
<div class="ulist">
<ul>
<li>
<p><code>assert.h</code></p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/assert_fail.c">userland/c/assert_fail.c</a></p>
</li>
</ul>
</div>
</li>
<li>
<p><code>stdlib.h</code></p>
<div class="ulist">
<ul>
<li>
<p>exit</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/abort.c">userland/c/abort.c</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li>
<p><code>stdio.h</code></p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/getchar.c">userland/c/getchar.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/snprintf.c">userland/c/snprintf.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/stderr.c">userland/c/stderr.c</a></p>
</li>
<li>
<p>File IO</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/file_write_read.c">userland/c/file_write_read.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/open_o_tmpfile.c">userland/linux/open_o_tmpfile.c</a>: <a href="https://stackoverflow.com/questions/4508998/what-is-an-anonymous-inode-in-linux/44388030#44388030" class="bare">https://stackoverflow.com/questions/4508998/what-is-an-anonymous-inode-in-linux/44388030#44388030</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li>
<p>Fun</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/loop.c">userland/c/loop.c</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="malloc"><a class="anchor" href="#malloc"></a><a class="link" href="#malloc">21.1.1. malloc</a></h4>
<div class="paragraph">
<p>Allocate memory! Vs using the stack: <a href="https://stackoverflow.com/questions/4584089/what-is-the-function-of-the-push-pop-instructions-used-on-registers-in-x86-ass/33583134#33583134" class="bare">https://stackoverflow.com/questions/4584089/what-is-the-function-of-the-push-pop-instructions-used-on-registers-in-x86-ass/33583134#33583134</a></p>
</div>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/malloc.c">userland/c/malloc.c</a>: <code>malloc</code> hello world: allocate two ints and use them.</p>
</div>
<div class="paragraph">
<p>Linux 5.1 / glibc 2.29 implements it with the <a href="#mmap"><code>mmap</code> system call</a>.</p>
</div>
<div class="paragraph">
<p><code>malloc</code> leads to the infinite joys of <a href="#memory-leaks">Memory leaks</a>.</p>
</div>
<div class="sect4">
<h5 id="malloc-implementation"><a class="anchor" href="#malloc-implementation"></a><a class="link" href="#malloc-implementation">21.1.1.1. malloc implementation</a></h5>
<div class="paragraph">
<p>TODO: the exact answer is going to be hard.</p>
</div>
<div class="paragraph">
<p>But at least let&#8217;s verify that large <code>malloc</code> calls use the <code>mmap</code> syscall with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>strace -x ./c/malloc_size.out 0x100000 2&gt;&amp;1 | grep mmap | tail -n 1
strace -x ./c/malloc_size.out 0x200000 2&gt;&amp;1 | grep mmap | tail -n 1
strace -x ./c/malloc_size.out 0x400000 2&gt;&amp;1 | grep mmap | tail -n 1</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/malloc_size.c">userland/c/malloc_size.c</a>.</p>
</div>
<div class="paragraph">
<p>From this we sese that the last <code>mmap</code> calls are:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffff7ef2000
mmap(NULL, 2101248, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffff7271000
mmap(NULL, 4198400, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffff7071000</pre>
</div>
</div>
<div class="paragraph">
<p>which in hex are:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>printf '%x\n' 1052672
# 101000
printf '%x\n' 2101248
# 201000
printf '%x\n' 4198400
# 401000</pre>
</div>
</div>
<div class="paragraph">
<p>so we figured out the pattern: those 1, 2, and 4 MiB mallocs are mmaping N + 0x1000 bytes.</p>
</div>
</div>
<div class="sect4">
<h5 id="malloc-maximum-size"><a class="anchor" href="#malloc-maximum-size"></a><a class="link" href="#malloc-maximum-size">21.1.1.2. malloc maximum size</a></h5>
<div class="paragraph">
<p>General overview at: <a href="https://stackoverflow.com/questions/2798330/maximum-memory-which-malloc-can-allocate" class="bare">https://stackoverflow.com/questions/2798330/maximum-memory-which-malloc-can-allocate</a></p>
</div>
<div class="paragraph">
<p>See also:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/13127855/what-is-the-size-limit-for-mmap" class="bare">https://stackoverflow.com/questions/13127855/what-is-the-size-limit-for-mmap</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/7504139/malloc-allocates-memory-more-than-ram" class="bare">https://stackoverflow.com/questions/7504139/malloc-allocates-memory-more-than-ram</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>From <a href="#memory-size">Memory size</a> and <code>./run --help</code>, we see that at we set the emulator memory by default to 256MB. Let&#8217;s see how much Linux allows us to malloc.</p>
</div>
<div class="paragraph">
<p>Then from <a href="#malloc-implementation">malloc implementation</a> we see that <code>malloc</code> is implemented with <code>mmap</code>. Therefore, let&#8217;s simplify the problam and try to understand what is the larges mmap we can do first. This way we can ignore how glibc implements malloc for now.</p>
</div>
<div class="paragraph">
<p>In Linux, the maximum <code>mmap</code> value in controlled by:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cat /proc/sys/vm/overcommit_memory</pre>
</div>
</div>
<div class="paragraph">
<p>which is documented in <code>man proc</code>.</p>
</div>
<div class="paragraph">
<p>The default value is <code>0</code>, which I can&#8217;t find a precise documentation for. <code>2</code> is precisly documented but I&#8217;m lazy to do all calculations. So let&#8217;s just verify <code>0</code> vs <code>1</code> by trying to <code>mmap</code> 1GiB of memory:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 0 &gt; /proc/sys/vm/overcommit_memory
./linux/mmap_anonymous.out 0x40000000
echo 1 &gt; /proc/sys/vm/overcommit_memory
./linux/mmap_anonymous.out 0x40000000</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/mmap_anonymous.c">userland/linux/mmap_anonymous.c</a></p>
</div>
<div class="paragraph">
<p>With <code>0</code>, we get a failure:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mmap: Cannot allocate memory</pre>
</div>
</div>
<div class="paragraph">
<p>but with <code>1</code> the allocation works.</p>
</div>
<div class="paragraph">
<p>We are allowed to allocate more than the actual memory + swap because the memory is only virtual, as explained at: <a href="https://stackoverflow.com/questions/7880784/what-is-rss-and-vsz-in-linux-memory-management/57453334#57453334" class="bare">https://stackoverflow.com/questions/7880784/what-is-rss-and-vsz-in-linux-memory-management/57453334#57453334</a></p>
</div>
<div class="paragraph">
<p>If we start using the pages, the OOM killer would sooner or later step in and kill our process: <a href="#linux-out-of-memory-killer">Linux out-of-memory killer</a>.</p>
</div>
<div class="sect5">
<h6 id="linux-out-of-memory-killer"><a class="anchor" href="#linux-out-of-memory-killer"></a><a class="link" href="#linux-out-of-memory-killer">21.1.1.2.1. Linux out-of-memory killer</a></h6>
<div class="paragraph">
<p>We can observe the OOM in LKMC 1e969e832f66cb5a72d12d57c53fb09e9721d589 which defaults to 256MiB of memory with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo 1 &gt; /proc/sys/vm/overcommit_memory
./linux/mmap_anonymous_touch.out 0x40000000 0x8000000</pre>
</div>
</div>
<div class="paragraph">
<p>This first allows memory overcommit so to that the program can mmap 1GiB, 4x more than total RAM without failing as mentioned at <a href="#malloc-maximum-size">malloc maximum size</a>.</p>
</div>
<div class="paragraph">
<p>It then walks over every page and writes a value in it to ensure that it is used.</p>
</div>
<div class="paragraph">
<p>Algorithm used by the OOM: <a href="https://unix.stackexchange.com/questions/153585/how-does-the-oom-killer-decide-which-process-to-kill-first" class="bare">https://unix.stackexchange.com/questions/153585/how-does-the-oom-killer-decide-which-process-to-kill-first</a></p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="c-multithreading"><a class="anchor" href="#c-multithreading"></a><a class="link" href="#c-multithreading">21.1.2. C multithreading</a></h4>
<div class="paragraph">
<p>Added in C11!</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/atomic.c">userland/c/atomic.c</a>: <code>atomic_int</code> and <code>thrd_create</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/3908031/how-to-multithread-c-code/52453354#52453354" class="bare">https://stackoverflow.com/questions/3908031/how-to-multithread-c-code/52453354#52453354</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="gcc-c-extensions"><a class="anchor" href="#gcc-c-extensions"></a><a class="link" href="#gcc-c-extensions">21.1.3. GCC C extensions</a></h4>
<div class="sect4">
<h5 id="c-empty-struct"><a class="anchor" href="#c-empty-struct"></a><a class="link" href="#c-empty-struct">21.1.3.1. C empty struct</a></h5>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/gcc/empty_struct.c">userland/gcc/empty_struct.c</a></p>
</div>
<div class="paragraph">
<p>Documentation: <a href="https://gcc.gnu.org/onlinedocs/gcc-8.2.0/gcc/Empty-Structures.html#Empty-Structures" class="bare">https://gcc.gnu.org/onlinedocs/gcc-8.2.0/gcc/Empty-Structures.html#Empty-Structures</a></p>
</div>
<div class="paragraph">
<p>Question: <a href="https://stackoverflow.com/questions/24685399/c-empty-struct-what-does-this-mean-do" class="bare">https://stackoverflow.com/questions/24685399/c-empty-struct-what-does-this-mean-do</a></p>
</div>
</div>
<div class="sect4">
<h5 id="openmp"><a class="anchor" href="#openmp"></a><a class="link" href="#openmp">21.1.3.2. OpenMP</a></h5>
<div class="paragraph">
<p>GCC implements the <a href="#openmp">OpenMP</a> threading implementation: <a href="https://stackoverflow.com/questions/3949901/pthreads-vs-openmp" class="bare">https://stackoverflow.com/questions/3949901/pthreads-vs-openmp</a></p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/gcc/openmp.c">userland/gcc/openmp.c</a></p>
</div>
<div class="paragraph">
<p>The implementation is built into GCC itself. It is enabled at GCC compile time by <code>BR2_GCC_ENABLE_OPENMP=y</code> on Buildroot, and at program compile time by <code>-fopenmp</code>.</p>
</div>
<div class="paragraph">
<p>It seems to be easier to use for compute parallelism and more language agnostic than POSIX threads.</p>
</div>
<div class="paragraph">
<p>pthreads are more versatile though and allow for a superset of OpenMP.</p>
</div>
<div class="paragraph">
<p>The implementation lives under <code>libgomp</code> in the GCC tree, and is documented at: <a href="https://gcc.gnu.org/onlinedocs/libgomp/" class="bare">https://gcc.gnu.org/onlinedocs/libgomp/</a></p>
</div>
<div class="paragraph">
<p><code>strace</code> shows that OpenMP makes <code>clone()</code> syscalls in Linux. TODO: does it actually call <code>pthread_</code> functions, or does it make syscalls directly? Or in other words, can it work on <a href="#freestanding-programs">Freestanding programs</a>? A quick grep shows many references to pthreads.</p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="cpp"><a class="anchor" href="#cpp"></a><a class="link" href="#cpp">21.2. C++</a></h3>
<div class="paragraph">
<p>Programs under <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/">userland/cpp/</a> are examples of <a href="https://en.wikipedia.org/wiki/C%2B%2B#Standardization">ISO C</a> programming.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/empty.cpp">userland/cpp/empty.cpp</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/hello.cpp">userland/cpp/hello.cpp</a></p>
</li>
<li>
<p>templates</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/template.cpp">userland/cpp/template.cpp</a>: basic example</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/template_class_with_static_member.cpp">userland/cpp/template_class_with_static_member.cpp</a>: <a href="https://stackoverflow.com/questions/3229883/static-member-initialization-in-a-class-template" class="bare">https://stackoverflow.com/questions/3229883/static-member-initialization-in-a-class-template</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/if_constexpr.cpp">userland/cpp/if_constexpr.cpp</a>: C++17 <code>if constexpr</code></p>
</li>
</ul>
</div>
</li>
<li>
<p>fstream</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/file_write_read.cpp">userland/cpp/file_write_read.cpp</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/temporary_directory.cpp">userland/cpp/temporary_directory.cpp</a>: illustrates <code>std::filesystem::temp_directory_path</code> and answers <a href="https://stackoverflow.com/questions/3379956/how-to-create-a-temporary-directory-in-c/58454949#58454949" class="bare">https://stackoverflow.com/questions/3379956/how-to-create-a-temporary-directory-in-c/58454949#58454949</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>random</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/random.cpp">userland/cpp/random.cpp</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="cpp-multithreading"><a class="anchor" href="#cpp-multithreading"></a><a class="link" href="#cpp-multithreading">21.2.1. C++ multithreading</a></h4>
<div class="ulist">
<ul>
<li>
<p><a href="https://en.cppreference.com/w/cpp/header/thread"><code>&lt;thread&gt;</code></a></p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/count.cpp">userland/cpp/count.cpp</a> Exemplifies: <code>std::this_thread::sleep_for</code></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/thread_hardware_concurrency.cpp">userland/cpp/thread_hardware_concurrency.cpp</a> <code>std::thread::hardware_concurrency</code></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/thread_get_id.cpp">userland/cpp/thread_get_id.cpp</a> <code>std::thread::get_id</code></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/thread_return_value.cpp">userland/cpp/thread_return_value.cpp</a>: how to return a value from a thread</p>
</li>
</ul>
</div>
</li>
<li>
<p><a href="https://en.cppreference.com/w/cpp/header/atomic"><code>&lt;atomic&gt;</code></a>: <a href="#cpp17">C++17 N4659 standards draft</a> 32 "Atomic operations library"</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/atomic.cpp">userland/cpp/atomic.cpp</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="cpp-standards"><a class="anchor" href="#cpp-standards"></a><a class="link" href="#cpp-standards">21.2.2. C++ standards</a></h4>
<div class="paragraph">
<p>Like for C, you have to pay for the standards&#8230;&#8203; insane. So we just use the closest free drafts instead.</p>
</div>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents" class="bare">https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents</a></p>
</div>
<div class="sect4">
<h5 id="cpp17"><a class="anchor" href="#cpp17"></a><a class="link" href="#cpp17">21.2.2.1. C++17 N4659 standards draft</a></h5>
<div class="paragraph">
<p><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf" class="bare">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf</a></p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="posix"><a class="anchor" href="#posix"></a><a class="link" href="#posix">21.3. POSIX</a></h3>
<div class="paragraph">
<p>Programs under <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/">userland/posix/</a> are examples of POSIX C programming.</p>
</div>
<div class="paragraph">
<p>These links provide a clear overview of what POSIX is:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/1780599/what-is-the-meaning-of-posix/31865755#31865755" class="bare">https://stackoverflow.com/questions/1780599/what-is-the-meaning-of-posix/31865755#31865755</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/11983/what-exactly-is-posix/220877#220877" class="bare">https://unix.stackexchange.com/questions/11983/what-exactly-is-posix/220877#220877</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="unistd-h"><a class="anchor" href="#unistd-h"></a><a class="link" href="#unistd-h">21.3.1. unistd.h</a></h4>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/count.c">userland/posix/count.c</a> illustrates <code>sleep()</code></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/count_to.c">userland/posix/count_to.c</a> minor variation of <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/count.c">userland/posix/count.c</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="pthreads"><a class="anchor" href="#pthreads"></a><a class="link" href="#pthreads">21.3.2. pthreads</a></h4>
<div class="paragraph">
<p>POSIX' multithreading API. This was for a looong time the only "portable" multithreading alternative, until <a href="#cpp-multithreading">C++11 finally added threads</a>, thus also extending the portability to Windows.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/pthread_count.c">userland/posix/pthread_count.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/pthread_deadlock.c">userland/posix/pthread_deadlock.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/pthread_self.c">userland/posix/pthread_self.c</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="sysconf"><a class="anchor" href="#sysconf"></a><a class="link" href="#sysconf">21.3.3. sysconf</a></h4>
<div class="paragraph">
<p><a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html" class="bare">https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html</a></p>
</div>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/sysconf.c">userland/posix/sysconf.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/sysconf.c">userland/linux/sysconf.c</a> showcases Linux extensions to POSIX</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Get lots of info on the system configuration.</p>
</div>
<div class="paragraph">
<p>The constants can also be viewed accessed on my Ubuntu 18.04 host with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>getconf -a</pre>
</div>
</div>
<div class="paragraph">
<p><code>getconf</code> is also specified by POSIX at: <a href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/getconf.html" class="bare">https://pubs.opengroup.org/onlinepubs/9699919799/utilities/getconf.html</a> but not the <code>-a</code> option which shows all configurations.</p>
</div>
</div>
<div class="sect3">
<h4 id="mmap-2"><a class="anchor" href="#mmap-2"></a><a class="link" href="#mmap-2">21.3.4. mmap</a></h4>
<div class="paragraph">
<p>The mmap system call allows advanced memory operations.</p>
</div>
<div class="paragraph">
<p>mmap is notably used to implement the <a href="#malloc">malloc ANSI C</a> function, replacing the previously used break system call.</p>
</div>
<div class="paragraph">
<p>Linux adds has several POSIX extension flags to it.</p>
</div>
<div class="sect4">
<h5 id="mmap-map-anonymous"><a class="anchor" href="#mmap-map-anonymous"></a><a class="link" href="#mmap-map-anonymous">21.3.4.1. mmap MAP_ANONYMOUS</a></h5>
<div class="paragraph">
<p>Basic <code>mmap</code> example, do the same as <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/malloc.c">userland/c/malloc.c</a>, but with <code>mmap</code>.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/mmap_anonymous.c">userland/linux/mmap_anonymous.c</a></p>
</div>
<div class="paragraph">
<p>In POSIX 7 mmap always maps to a file.</p>
</div>
<div class="paragraph">
<p>If we add the MAP_ANONYMOUS Linux extension however, this is not required, and mmap can be used to allocate memory like malloc.</p>
</div>
<div class="paragraph">
<p>Answers: <a href="https://stackoverflow.com/questions/4779188/how-to-use-mmap-to-allocate-a-memory-in-heap" class="bare">https://stackoverflow.com/questions/4779188/how-to-use-mmap-to-allocate-a-memory-in-heap</a></p>
</div>
</div>
<div class="sect4">
<h5 id="mmap-file"><a class="anchor" href="#mmap-file"></a><a class="link" href="#mmap-file">21.3.4.2. mmap file</a></h5>
<div class="paragraph">
<p>Memory mapped file example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/mmap_file.c">userland/posix/mmap_file.c</a></p>
</div>
<div class="paragraph">
<p>The example creates a file, mmaps to it, writes to maped memory, and then closes the file.</p>
</div>
<div class="paragraph">
<p>We then read the file and confirm it was written to.</p>
</div>
</div>
<div class="sect4">
<h5 id="brk"><a class="anchor" href="#brk"></a><a class="link" href="#brk">21.3.4.3. brk</a></h5>
<div class="paragraph">
<p>Previously <a href="#posix">POSIX</a>, but was deprecated in favor of <a href="#malloc">malloc</a></p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/brk.c">userland/linux/brk.c</a></p>
</div>
<div class="paragraph">
<p>The example allocates two ints and uses them, and then deallocates back.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/6988487/what-does-the-brk-system-call-do/31082353#31082353" class="bare">https://stackoverflow.com/questions/6988487/what-does-the-brk-system-call-do/31082353#31082353</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="socket"><a class="anchor" href="#socket"></a><a class="link" href="#socket">21.3.5. socket</a></h4>
<div class="paragraph">
<p>A bit like <code>read</code> and <code>write</code>, but from / to the Internet!</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/wget.c">userland/posix/wget.c</a> tiny <code>wget</code> re-implementation. See: <a href="https://stackoverflow.com/questions/11208299/how-to-make-an-http-get-request-in-c-without-libcurl/35680609#35680609" class="bare">https://stackoverflow.com/questions/11208299/how-to-make-an-http-get-request-in-c-without-libcurl/35680609#35680609</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="userland-multithreading"><a class="anchor" href="#userland-multithreading"></a><a class="link" href="#userland-multithreading">21.4. Userland multithreading</a></h3>
<div class="paragraph">
<p>The following sections are related to multithreading in userland:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>language topics:</p>
<div class="ulist">
<ul>
<li>
<p><a href="#c-multithreading">C multithreading</a></p>
</li>
<li>
<p><a href="#cpp-multithreading">C++ multithreading</a></p>
</li>
<li>
<p><a href="#pthreads">pthreads</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>ISA topics:</p>
<div class="ulist">
<ul>
<li>
<p><a href="#x86-thread-synchronization-primitives">x86 thread synchronization primitives</a></p>
</li>
<li>
<p><a href="#arm-lse">ARM Large System Extensions (LSE)</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>emulator topics:</p>
<div class="ulist">
<ul>
<li>
<p><a href="#qemu-user-mode-multithreading">QEMU user mode multithreading</a></p>
</li>
<li>
<p><a href="#gem5-syscall-emulation-multithreading">gem5 syscall emulation multithreading</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="c-debugging"><a class="anchor" href="#c-debugging"></a><a class="link" href="#c-debugging">21.5. C debugging</a></h3>
<div class="paragraph">
<p>Let&#8217;s group the hard-to-debug undefined-behaviour-like stuff found in C / C+ here and how to tackle those problems.</p>
</div>
<div class="sect3">
<h4 id="stack-smashing"><a class="anchor" href="#stack-smashing"></a><a class="link" href="#stack-smashing">21.5.1. Stack smashing</a></h4>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/1345670/stack-smashing-detected/51897264#51897264" class="bare">https://stackoverflow.com/questions/1345670/stack-smashing-detected/51897264#51897264</a></p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">Example</dt>
<dd>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/smash_stack.c">userland/c/smash_stack.c</a></p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>Leads to the dreadful "Stack smashing detected" message. Which is infinitely better than a silent break in any case.</p>
</div>
</div>
<div class="sect3">
<h4 id="memory-leaks"><a class="anchor" href="#memory-leaks"></a><a class="link" href="#memory-leaks">21.5.2. Memory leaks</a></h4>
<div class="paragraph">
<p>How to debug: <a href="https://stackoverflow.com/questions/6261201/how-to-find-memory-leak-in-a-c-code-project/57877190#57877190" class="bare">https://stackoverflow.com/questions/6261201/how-to-find-memory-leak-in-a-c-code-project/57877190#57877190</a></p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/memory_leak.c">userland/c/memory_leak.c</a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="interpreted-languages"><a class="anchor" href="#interpreted-languages"></a><a class="link" href="#interpreted-languages">21.6. Interpreted languages</a></h3>
<div class="paragraph">
<p>Maybe some day someone will use this setup to study the performance of interpreters:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#node-js">Node.js</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="node-js"><a class="anchor" href="#node-js"></a><a class="link" href="#node-js">21.7. Node.js</a></h3>
<div class="paragraph">
<p>Parent section: <a href="#interpreted-languages">Interpreted languages</a>.</p>
</div>
<div class="paragraph">
<p>Install the interpreter with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_NODEJS=y'</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: broken as of 3c3deb14dc8d6511680595dc42cb627d5781746d + 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ERROR: package host-nodejs installs executables without proper RPATH</pre>
</div>
</div>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>String</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/nodejs/alphanumeric.js">rootfs_overlay/lkmc/nodejs/alphanumeric.js</a>: <a href="https://stackoverflow.com/questions/4444477/how-to-tell-if-a-string-contains-a-certain-character-in-javascript/58359106#58359106" class="bare">https://stackoverflow.com/questions/4444477/how-to-tell-if-a-string-contains-a-certain-character-in-javascript/58359106#58359106</a></p>
</li>
</ul>
</div>
</li>
<li>
<p><code>process</code></p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/nodejs/command_line_arguments.js">rootfs_overlay/lkmc/nodejs/command_line_arguments.js</a></p>
</li>
</ul>
</div>
</li>
<li>
<p><code>fs</code></p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/nodejs/file_write_read.js">rootfs_overlay/lkmc/nodejs/file_write_read.js</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/nodejs/read_stdin_to_string.js">rootfs_overlay/lkmc/nodejs/read_stdin_to_string.js</a> Question: <a href="https://stackoverflow.com/questions/30441025/read-all-text-from-stdin-to-a-string" class="bare">https://stackoverflow.com/questions/30441025/read-all-text-from-stdin-to-a-string</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="userland-content-bibliography"><a class="anchor" href="#userland-content-bibliography"></a><a class="link" href="#userland-content-bibliography">21.8. Userland content bibliography</a></h3>
<div class="ulist">
<ul>
<li>
<p>The Linux Programming Interface by Michael Kerrisk <a href="https://www.amazon.co.uk/Linux-Programming-Interface-System-Handbook/dp/1593272200" class="bare">https://www.amazon.co.uk/Linux-Programming-Interface-System-Handbook/dp/1593272200</a> Lots of open source POSIX examples: <a href="https://github.com/cirosantilli/linux-programming-interface-kerrisk" class="bare">https://github.com/cirosantilli/linux-programming-interface-kerrisk</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="userland-assembly"><a class="anchor" href="#userland-assembly"></a><a class="link" href="#userland-assembly">22. Userland assembly</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Programs under <code>userland/arch/&lt;arch&gt;/</code> are examples of userland assembly programming.</p>
</div>
<div class="paragraph">
<p>This section will document ISA agnostic concepts, and you should read it first.</p>
</div>
<div class="paragraph">
<p>ISA specifics are covered at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#x86-userland-assembly">x86 userland assembly</a> under <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/">userland/arch/x86_64/</a>, originally migrated from: <a href="https://github.com/cirosantilli/x86-assembly-cheat" class="bare">https://github.com/cirosantilli/x86-assembly-cheat</a></p>
</li>
<li>
<p><a href="#arm-userland-assembly">ARM userland assembly</a> originally migrated from <a href="https://github.com/cirosantilli/arm-assembly-cheat" class="bare">https://github.com/cirosantilli/arm-assembly-cheat</a> under:</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/">userland/arch/arm/</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/">userland/arch/aarch64/</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Like other userland programs, these programs can be run as explained at: <a href="#userland-setup">Section 1.6, &#8220;Userland setup&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>As a quick reminder, the fastest setups to get started are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#userland-setup-getting-started-natively">Userland setup getting started natively</a> if your host can run the examples, e.g. x86 example on an x86 host:</p>
</li>
<li>
<p><a href="#userland-setup-getting-started-with-prebuilt-toolchain-and-qemu-user-mode">Userland setup getting started with prebuilt toolchain and QEMU user mode</a> otherwise</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>However, as usual, it is saner to build your toolchain as explained at: <a href="#qemu-user-mode-getting-started">Section 10.1, &#8220;QEMU user mode getting started&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>The first examples you should look into are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>add</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/add.S">userland/arch/x86_64/add.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/add.S">userland/arch/arm/add.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/add.S">userland/arch/aarch64/add.S</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>mov between register and memory</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/mov.S">userland/arch/x86_64/mov.S</a></p>
</li>
<li>
<p><a href="#arm-mov-instruction">ARM MOV instruction</a></p>
</li>
<li>
<p><a href="#arm-load-and-store-instructions">ARM load and store instructions</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>addressing modes</p>
<div class="ulist">
<ul>
<li>
<p><a href="#x86-addressing-modes">x86 addressing modes</a></p>
</li>
<li>
<p><a href="#arm-addressing-modes">ARM addressing modes</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>registers, see: <a href="#assembly-registers">Section 22.1, &#8220;Assembly registers&#8221;</a></p>
</li>
<li>
<p>jumping:</p>
<div class="ulist">
<ul>
<li>
<p><a href="#x86-control-transfer-instructions">x86 control transfer instructions</a></p>
</li>
<li>
<p><a href="#arm-branch-instructions">ARM branch instructions</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>SIMD</p>
<div class="ulist">
<ul>
<li>
<p><a href="#x86-simd">x86 SIMD</a></p>
</li>
<li>
<p><a href="#arm-simd">ARM SIMD</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>The add examples in particular:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>introduce the basics of how a given assembly works: how many inputs / outputs, who is input and output, can it use memory or just registers, etc.</p>
<div class="paragraph">
<p>It is then a big copy paste for most other data instructions.</p>
</div>
</li>
<li>
<p>verify that the venerable ADD instruction and our assertions are working</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Now try to modify modify the x86_64 add program to see the assertion fail:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>LKMC_ASSERT_EQ(%rax, $4)</pre>
</div>
</div>
<div class="paragraph">
<p>because 1 + 2 tends to equal 3 instead of 4.</p>
</div>
<div class="paragraph">
<p>And then watch the assertion fail:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland
./run --userland userland/arch/x86_64/add.S</pre>
</div>
</div>
<div class="paragraph">
<p>with error message:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>assert_eq_64 failed
val1 0x3
val2 0x4
error: asm_main returned 1 at line 8</pre>
</div>
</div>
<div class="paragraph">
<p>and notice how the error message gives both:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the actual assembly source line number where the failing assert was</p>
</li>
<li>
<p>the actual and expected values</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Other infrastructure sanity checks that you might want to look into include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/empty.S">userland/arch/empty.S</a></p>
</li>
<li>
<p><code>LKMC_FAIL</code> tests</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/lkmc_assert_fail.S">userland/arch/lkmc_assert_fail.S</a></p>
</li>
</ul>
</div>
</li>
<li>
<p><code>LKMC_ASSERT_EQ</code> tests</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/lkmc_assert_eq_fail.S">userland/arch/x86_64/lkmc_assert_eq_fail.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/lkmc_assert_eq_fail.S">userland/arch/arm/lkmc_assert_eq_fail.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/lkmc_assert_eq_fail.S">userland/arch/aarch64/lkmc_assert_eq_fail.S</a></p>
</li>
</ul>
</div>
</li>
<li>
<p><code>LKMC_ASSERT_MEMCMP</code> tests</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/lkmc_assert_memcmp_fail.S">userland/arch/x86_64/lkmc_assert_memcmp_fail.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/lkmc_assert_memcmp_fail.S">userland/arch/arm/lkmc_assert_memcmp_fail.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/lkmc_assert_memcmp_fail.S">userland/arch/aarch64/lkmc_assert_memcmp_fail.S</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="sect2">
<h3 id="assembly-registers"><a class="anchor" href="#assembly-registers"></a><a class="link" href="#assembly-registers">22.1. Assembly registers</a></h3>
<div class="paragraph">
<p>After seeing an <a href="#userland-assembly">ADD hello world</a>, you need to learn the general registers:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>x86, see: <a href="#x86-registers">Section 23.1, &#8220;x86 registers&#8221;</a></p>
</li>
<li>
<p>arm</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/registers.S">userland/arch/arm/registers.S</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>aarch64</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/registers.S">userland/arch/aarch64/registers.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/pc.S">userland/arch/aarch64/pc.S</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography: <a href="#armarm7">ARMv7 architecture reference manual</a> A2.3 "ARM core registers".</p>
</div>
<div class="sect3">
<h4 id="armv8-aarch64-x31-register"><a class="anchor" href="#armv8-aarch64-x31-register"></a><a class="link" href="#armv8-aarch64-x31-register">22.1.1. ARMv8 aarch64 x31 register</a></h4>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/x31.S">userland/arch/aarch64/x31.S</a></p>
</div>
<div class="paragraph">
<p>There is no X31 name, and the encoding can have two different names depending on the instruction:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>XZR: zero register:</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/42788696/why-might-one-use-the-xzr-register-instead-of-the-literal-0-on-armv8" class="bare">https://stackoverflow.com/questions/42788696/why-might-one-use-the-xzr-register-instead-of-the-literal-0-on-armv8</a></p>
</li>
<li>
<p><a href="https://community.arm.com/processors/f/discussions/3185/wzr-xzr-register-s-purpose" class="bare">https://community.arm.com/processors/f/discussions/3185/wzr-xzr-register-s-purpose</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>SP: stack pointer</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>To make things more confusing, some aliases can take either name, which makes them alias to different things, e.g. MOV accepts both:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mov x0, sp
mov x0, xzr</pre>
</div>
</div>
<div class="paragraph">
<p>and the first one is an alias to ADD while the second an alias to <a href="#arm-bitwise-instructions">ORR</a>.</p>
</div>
<div class="paragraph">
<p>The difference is documented on a per instruction basis. Instructions that encode 31 as SP say:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>if d == 31 then
SP[] = result;
else
X[d] = result;</pre>
</div>
</div>
<div class="paragraph">
<p>And then those that don&#8217;t say that, B1.2.1 "Registers in AArch64 state" implies the zero register:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>In instruction encodings, the value 0b11111 (31) is used to indicate the ZR (zero register). This
indicates that the argument takes the value zero, but does not indicate that the ZR is implemented
as a physical register.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>This is also described on <a href="#armarm8">ARMv8 architecture reference manual</a> C1.2.5 "Register names":</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>There is no register named W31 or X31.</p>
</div>
<div class="paragraph">
<p>The name SP represents the stack pointer for 64-bit operands where an encoding of the value 31 in the
corresponding register field is interpreted as a read or write of the current stack pointer. When instructions
do not interpret this operand encoding as the stack pointer, use of the name SP is an error.</p>
</div>
<div class="paragraph">
<p>The name XZR represents the zero register for 64-bit operands where an encoding of the value 31 in the
corresponding register field is interpreted as returning zero when read or discarding the result when written.
When instructions do not interpret this operand encoding as the zero register, use of the name XZR is an error</p>
</div>
</blockquote>
</div>
</div>
</div>
<div class="sect2">
<h3 id="floating-point-assembly"><a class="anchor" href="#floating-point-assembly"></a><a class="link" href="#floating-point-assembly">22.2. Floating point assembly</a></h3>
<div class="paragraph">
<p>Keep in mind that many ISAs started floating point as an optional thing, and it later got better integrated into the main CPU, side by side with SIMD.</p>
</div>
<div class="paragraph">
<p>For this reason, there are sometimes multiple ways to do floating point operations in each ISA.</p>
</div>
<div class="paragraph">
<p>Let&#8217;s start as usual with floating point addition + register file:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>arm</p>
<div class="ulist">
<ul>
<li>
<p><a href="#arm-vadd-instruction">ARM VADD instruction</a></p>
</li>
<li>
<p><a href="#arm-vfp-registers">ARM VFP registers</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>aarch64</p>
<div class="ulist">
<ul>
<li>
<p><a href="#armv8-aarch64-fadd-instruction">ARMv8 aarch64 FADD instruction</a></p>
</li>
<li>
<p><a href="#armv8-aarch64-floating-point-registers">ARMv8 AArch64 floating point registers</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="simd-assembly"><a class="anchor" href="#simd-assembly"></a><a class="link" href="#simd-assembly">22.3. SIMD assembly</a></h3>
<div class="paragraph">
<p>Much like ADD for non-SIMD, start learning SIMD instructions by looking at the integer and floating point SIMD ADD instructions of each ISA:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>x86</p>
<div class="ulist">
<ul>
<li>
<p><a href="#x86-sse-data-transfer-instructions">ADDPD</a></p>
</li>
<li>
<p><a href="#x86-paddq-instruction">x86 PADDQ instruction</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>arm</p>
<div class="ulist">
<ul>
<li>
<p><a href="#arm-vadd-instruction">ARM VADD instruction</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>aarch64</p>
<div class="ulist">
<ul>
<li>
<p><a href="#armv8-aarch64-add-vector-instruction">ARMv8 aarch64 add vector instruction</a></p>
</li>
<li>
<p><a href="#armv8-aarch64-fadd-instruction">ARMv8 aarch64 FADD instruction</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Then it is just a huge copy paste of infinite boring details:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#x86-simd">x86 SIMD</a></p>
</li>
<li>
<p><a href="#arm-simd">ARM SIMD</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>To debug these instructoins, you can see the register values in GDB with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>info registers float</pre>
</div>
</div>
<div class="paragraph">
<p>or alternatively with register names (here the ARMv8 V0 register):</p>
</div>
<div class="literalblock">
<div class="content">
<pre>print $v0</pre>
</div>
</div>
<div class="paragraph">
<p>as mentioned at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/5429137/how-to-print-register-values-in-gdb/38036152#38036152" class="bare">https://stackoverflow.com/questions/5429137/how-to-print-register-values-in-gdb/38036152#38036152</a></p>
</li>
<li>
<p><a href="https://reverseengineering.stackexchange.com/questions/8992/floating-point-registers-on-arm/20623#20623" class="bare">https://reverseengineering.stackexchange.com/questions/8992/floating-point-registers-on-arm/20623#20623</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/1389712/getting-started-with-intel-x86-sse-simd-instructions/56409539#56409539" class="bare">https://stackoverflow.com/questions/1389712/getting-started-with-intel-x86-sse-simd-instructions/56409539#56409539</a></p>
</div>
<div class="sect3">
<h4 id="fma-instruction"><a class="anchor" href="#fma-instruction"></a><a class="link" href="#fma-instruction">22.3.1. FMA instruction</a></h4>
<div class="paragraph">
<p>Fused multiply add:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>x86: <a href="#x86-fma">Section 23.12.3, &#8220;x86 fused multiply add (FMA)&#8221;</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://en.wikipedia.org/wiki/Multiplyaccumulate_operation" class="bare">https://en.wikipedia.org/wiki/Multiplyaccumulate_operation</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/FMA_instruction_set" class="bare">https://en.wikipedia.org/wiki/FMA_instruction_set</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Particularly important numerical analysis instruction, that is used in particular for;</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Dot product</p>
</li>
<li>
<p>Matrix multiplication</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>FMA is so important that <a href="#ieee-754">IEEE 754</a> specifies it with single precision drop compared to a separate add and multiply!</p>
</div>
<div class="paragraph">
<p>Micro-op fun: <a href="https://stackoverflow.com/questions/28630864/how-is-fma-implemented" class="bare">https://stackoverflow.com/questions/28630864/how-is-fma-implemented</a></p>
</div>
<div class="paragraph">
<p>Historically, FMA instructions have been added relatively late to instruction sets.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="user-vs-system-assembly"><a class="anchor" href="#user-vs-system-assembly"></a><a class="link" href="#user-vs-system-assembly">22.4. User vs system assembly</a></h3>
<div class="paragraph">
<p>By "userland assembly", we mean "the parts of the ISA which can be freely used from userland".</p>
</div>
<div class="paragraph">
<p>Most ISAs are divided into a system and userland part, and to running the system part requires elevated privileges such as <a href="#ring0">ring0</a> in x86.</p>
</div>
<div class="paragraph">
<p>One big difference between both is that we can run userland assembly on <a href="#userland-setup">Userland setup</a>, which is easier to get running and debug.</p>
</div>
<div class="paragraph">
<p>In particular, most userland assembly examples link to the C standard library, see: <a href="#userland-assembly-c-standard-library">Section 22.5, &#8220;Userland assembly C standard library&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Userland assembly is generally simpler, and a pre-requisite for <a href="#baremetal-setup">Baremetal setup</a>.</p>
</div>
<div class="paragraph">
<p>System-land assembly cheats will be put under: <a href="#baremetal-setup">Section 1.7, &#8220;Baremetal setup&#8221;</a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="userland-assembly-c-standard-library"><a class="anchor" href="#userland-assembly-c-standard-library"></a><a class="link" href="#userland-assembly-c-standard-library">22.5. Userland assembly C standard library</a></h3>
<div class="paragraph">
<p>All examples except the <a href="#freestanding-programs">Freestanding programs</a> link to the C standard library.</p>
</div>
<div class="paragraph">
<p>This allows using the C standard library for IO, which is very convenient and portable across host OSes.</p>
</div>
<div class="paragraph">
<p>It also exposes other non-IO functionality that is very convenient such as <code>memcmp</code>.</p>
</div>
<div class="paragraph">
<p>The C standard library infrastructure is implemented in the common userland / baremetal source files:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc.c">lkmc.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc.h">lkmc.h</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/aarch64.h">lkmc/aarch64.h</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/arm.h">lkmc/arm.h</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/x86_64.h">lkmc/x86_64.h</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="freestanding-programs"><a class="anchor" href="#freestanding-programs"></a><a class="link" href="#freestanding-programs">22.5.1. Freestanding programs</a></h4>
<div class="paragraph">
<p>Unlike most our other assembly examples, which use the C standard library for portability, examples under <code>freestanding/</code> directories don&#8217;t link to the C standard library:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/freestanding/">userland/arch/x86_64/freestanding/</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/freestanding/">userland/arch/arm/freestanding/</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/freestanding/">userland/arch/aarch64/freestanding/</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>As a result, those examples cannot do IO portably, and so they make raw system calls and only be run on one given OS, e.g. <a href="#linux-system-calls">Linux system calls</a>.</p>
</div>
<div class="paragraph">
<p>Such executables are called freestanding because they don&#8217;t execute the glibc initialization code, but rather start directly on our custom hand written assembly.</p>
</div>
<div class="paragraph">
<p>In order to GDB step debug those executables, you will want to use <code>--no-continue</code>, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --userland userland/arch/aarch64/freestanding/linux/hello.S --gdb-wait
./run-gdb --arch aarch64 --no-continue --userland userland/arch/aarch64/freestanding/linux/hello.S</pre>
</div>
</div>
<div class="paragraph">
<p>or in one go with <a href="#tmux">tmux</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--gdb-wait \
--tmux-args=--no-continue \
--userland userland/arch/aarch64/freestanding/linux/hello.S \
;</pre>
</div>
</div>
<div class="paragraph">
<p>You are now left on the very first instruction of our tiny executable!</p>
</div>
<div class="paragraph">
<p>This is analogous to <a href="#baremetal-gdb-step-debug">step debugging baremetal examples</a>.</p>
</div>
<div class="sect4">
<h5 id="nostartfiles-programs"><a class="anchor" href="#nostartfiles-programs"></a><a class="link" href="#nostartfiles-programs">22.5.1.1. nostartfiles programs</a></h5>
<div class="paragraph">
<p>Assembly examples under <code>nostartfiles</code> directories can use the standard library, but they don&#8217;t use the pre-<code>main</code> boilerplate and start directly at our explicitly given <code>_start</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/freestanding/">userland/arch/aarch64/freestanding/</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>I&#8217;m not sure how much stdlib functionality is supposed to work without the pre-main stuff, but I guess we&#8217;ll just have to find out!</p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gcc-inline-assembly"><a class="anchor" href="#gcc-inline-assembly"></a><a class="link" href="#gcc-inline-assembly">22.6. GCC inline assembly</a></h3>
<div class="paragraph">
<p>Examples under <code>arch/&lt;arch&gt;/c/</code> directories show to how use inline assembly from higher level languages such as C:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>x86_64</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/inline_asm/inc.c">userland/arch/x86_64/inline_asm/inc.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/inline_asm/add.c">userland/arch/x86_64/inline_asm/add.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/inline_asm/sqrt_x87.c">userland/arch/x86_64/inline_asm/sqrt_x87.c</a> Shows how to use the <a href="#x86-x87-fpu-instructions">x86 x87 FPU instructions</a> from inline assembly. Bibliography: <a href="https://stackoverflow.com/questions/6514537/how-do-i-specify-immediate-floating-point-numbers-with-inline-assembly/52906126#52906126" class="bare">https://stackoverflow.com/questions/6514537/how-do-i-specify-immediate-floating-point-numbers-with-inline-assembly/52906126#52906126</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>arm</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/inline_asm/inc.c">userland/arch/arm/inline_asm/inc.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/inline_asm/inc_memory.c">userland/arch/arm/inline_asm/inc_memory.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/inline_asm/inc_memory_global.c">userland/arch/arm/inline_asm/inc_memory_global.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/inline_asm/add.c">userland/arch/arm/inline_asm/add.c</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>aarch64</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/inline_asm/earlyclobber.c">userland/arch/aarch64/inline_asm/earlyclobber.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/inline_asm/inc.c">userland/arch/aarch64/inline_asm/inc.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/inline_asm/multiline.cpp">userland/arch/aarch64/inline_asm/multiline.cpp</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="gcc-inline-assembly-register-variables"><a class="anchor" href="#gcc-inline-assembly-register-variables"></a><a class="link" href="#gcc-inline-assembly-register-variables">22.6.1. GCC inline assembly register variables</a></h4>
<div class="paragraph">
<p>Used notably in some of the <a href="#linux-system-calls">Linux system calls</a> setups:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/inline_asm/reg_var.c">userland/arch/arm/inline_asm/reg_var.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/inline_asm/reg_var.c">userland/arch/aarch64/inline_asm/reg_var.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/inline_asm/reg_var_float.c">userland/arch/aarch64/inline_asm/reg_var_float.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In x86, makes it possible to access variables not exposed with the one letter register constraints.</p>
</div>
<div class="paragraph">
<p>In arm, it is the only way to achieve this effect: <a href="https://stackoverflow.com/questions/10831792/how-to-use-specific-register-in-arm-inline-assembler" class="bare">https://stackoverflow.com/questions/10831792/how-to-use-specific-register-in-arm-inline-assembler</a></p>
</div>
<div class="paragraph">
<p>This feature notably useful for making system calls from C, see: <a href="#linux-system-calls">Section 22.7, &#8220;Linux system calls&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Documentation: <a href="https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Explicit-Reg-Vars.html" class="bare">https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Explicit-Reg-Vars.html</a></p>
</div>
</div>
<div class="sect3">
<h4 id="gcc-inline-assembly-scratch-registers"><a class="anchor" href="#gcc-inline-assembly-scratch-registers"></a><a class="link" href="#gcc-inline-assembly-scratch-registers">22.6.2. GCC inline assembly scratch registers</a></h4>
<div class="paragraph">
<p>How to use temporary registers in inline assembly:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>x86_64</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/inline_asm/scratch.c">userland/arch/x86_64/inline_asm/scratch.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/inline_asm/scratch_hardcode.c">userland/arch/x86_64/inline_asm/scratch_hardcode.c</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/6682733/gcc-prohibit-use-of-some-registers/54963829#54963829" class="bare">https://stackoverflow.com/questions/6682733/gcc-prohibit-use-of-some-registers/54963829#54963829</a></p>
</div>
</div>
<div class="sect3">
<h4 id="gcc-inline-assembly-early-clobbers"><a class="anchor" href="#gcc-inline-assembly-early-clobbers"></a><a class="link" href="#gcc-inline-assembly-early-clobbers">22.6.3. GCC inline assembly early-clobbers</a></h4>
<div class="paragraph">
<p>An example of using the <code>&amp;</code> early-clobber modifier: link:userland/arch/aarch64/earlyclobber.c</p>
</div>
<div class="paragraph">
<p>More details at: <a href="https://stackoverflow.com/questions/15819794/when-to-use-earlyclobber-constraint-in-extended-gcc-inline-assembly/54853663#54853663" class="bare">https://stackoverflow.com/questions/15819794/when-to-use-earlyclobber-constraint-in-extended-gcc-inline-assembly/54853663#54853663</a></p>
</div>
<div class="paragraph">
<p>The assertion may fail without it. It actually does fail in GCC 8.2.0.</p>
</div>
</div>
<div class="sect3">
<h4 id="gcc-inline-assembly-floating-point-arm"><a class="anchor" href="#gcc-inline-assembly-floating-point-arm"></a><a class="link" href="#gcc-inline-assembly-floating-point-arm">22.6.4. GCC inline assembly floating point ARM</a></h4>
<div class="paragraph">
<p>Not documented as of GCC 8.2, but possible: <a href="https://stackoverflow.com/questions/53960240/armv8-floating-point-output-inline-assembly" class="bare">https://stackoverflow.com/questions/53960240/armv8-floating-point-output-inline-assembly</a></p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/inline_asm/inc_float.c">userland/arch/arm/inline_asm/inc_float.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/inline_asm/inc_float.c">userland/arch/aarch64/inline_asm/inc_float.c</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="gcc-intrinsics"><a class="anchor" href="#gcc-intrinsics"></a><a class="link" href="#gcc-intrinsics">22.6.5. GCC intrinsics</a></h4>
<div class="paragraph">
<p>Pre-existing C wrappers using inline assembly, this is what production programs should use instead of inline assembly for SIMD:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>x86_64</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/intrinsics/paddq.c">userland/arch/x86_64/intrinsics/paddq.c</a>. Intrinsics version of <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/paddq.S">userland/arch/x86_64/paddq.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/intrinsics/addpd.c">userland/arch/x86_64/intrinsics/addpd.c</a>. Intrinsics version of <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/addpd.S">userland/arch/x86_64/addpd.S</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="gcc-x86-intrinsics"><a class="anchor" href="#gcc-x86-intrinsics"></a><a class="link" href="#gcc-x86-intrinsics">22.6.5.1. GCC x86 intrinsics</a></h5>
<div class="paragraph">
<p>Good official cheatsheet with all intrinsics and what they expand to: <a href="https://software.intel.com/sites/landingpage/IntrinsicsGuide" class="bare">https://software.intel.com/sites/landingpage/IntrinsicsGuide</a></p>
</div>
<div class="paragraph">
<p>The functions use the the following naming convention:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;vector_size&gt;_&lt;intrin_op&gt;_&lt;suffix&gt;</pre>
</div>
</div>
<div class="paragraph">
<p>where:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>&lt;vector_size&gt;</code>:</p>
<div class="ulist">
<ul>
<li>
<p><code>mm</code>: 128-bit vectors (SSE)</p>
</li>
<li>
<p><code>mm256</code>: 256-bit vectors (AVX and AVX2)</p>
</li>
<li>
<p><code>mm512</code>: 512-bit vectors (AVX512)</p>
</li>
</ul>
</div>
</li>
<li>
<p><code>&lt;intrin_op&gt;</code>: operation of the intrinsic function, e.g. add, sub, mul, etc.</p>
</li>
<li>
<p><code>&lt;suffix&gt;</code>: data type:</p>
<div class="ulist">
<ul>
<li>
<p><code>ps</code>: 4 floats (Packed Single)</p>
</li>
<li>
<p><code>pd</code>: 2 doubles (Packed Double)</p>
</li>
<li>
<p><code>ss</code>: 1 float (Single Single), often the lowest order one</p>
</li>
<li>
<p><code>sd</code>: 1 double (Single Double)</p>
</li>
<li>
<p><code>si128</code>: 128-bits of integers of any size</p>
</li>
<li>
<p><code>ep&lt;int_type&gt;</code> integer types, e.g.:</p>
<div class="ulist">
<ul>
<li>
<p><code>epi32</code>: 32 bit signed integers</p>
</li>
<li>
<p><code>epu16</code>: 16 bit unsigned integers</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Data types:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>__m128</code>: four floats</p>
</li>
<li>
<p><code>__m128d</code>: two doubles</p>
</li>
<li>
<p><code>__m128i</code>: integers: 8 x 16-bit, 4 x 32-bit, 2 x 64-bit</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The headers to include are clarified at: <a href="https://stackoverflow.com/questions/11228855/header-files-for-x86-simd-intrinsics" class="bare">https://stackoverflow.com/questions/11228855/header-files-for-x86-simd-intrinsics</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre>x86intrin.h everything
mmintrin.h MMX
xmmintrin.h SSE
emmintrin.h SSE2
pmmintrin.h SSE3
tmmintrin.h SSSE3
smmintrin.h SSE4.1
nmmintrin.h SSE4.2
ammintrin.h SSE4A
wmmintrin.h AES
immintrin.h AVX
zmmintrin.h AVX512</pre>
</div>
</div>
<div class="paragraph">
<p>Present in <code>gcc-7_3_0-release</code> tree at: <code>gcc/config/i386/x86intrin.h</code>.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://www.cs.virginia.edu/~cr4bd/3330/S2018/simdref.html" class="bare">https://www.cs.virginia.edu/~cr4bd/3330/S2018/simdref.html</a></p>
</li>
<li>
<p><a href="https://software.intel.com/en-us/articles/how-to-use-intrinsics" class="bare">https://software.intel.com/en-us/articles/how-to-use-intrinsics</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="linux-system-calls"><a class="anchor" href="#linux-system-calls"></a><a class="link" href="#linux-system-calls">22.7. Linux system calls</a></h3>
<div class="paragraph">
<p>The following <a href="#userland-setup">Userland setup</a> programs illustrate how to make system calls:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>x86_64</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/freestanding/linux/hello.S">userland/arch/x86_64/freestanding/linux/hello.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/freestanding/linux/int_system_call.S">userland/arch/x86_64/freestanding/linux/int_system_call.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/inline_asm/freestanding/linux/hello.c">userland/arch/x86_64/inline_asm/freestanding/linux/hello.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/inline_asm/freestanding/linux/hello_regvar.c">userland/arch/x86_64/inline_asm/freestanding/linux/hello_regvar.c</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>arm</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/freestanding/linux/hello.S">userland/arch/arm/freestanding/linux/hello.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/inline_asm/freestanding/linux/hello.c">userland/arch/arm/inline_asm/freestanding/linux/hello.c</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>aarch64</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/freestanding/linux/hello.S">userland/arch/aarch64/freestanding/linux/hello.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/inline_asm/freestanding/linux/hello.c">userland/arch/aarch64/inline_asm/freestanding/linux/hello.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/inline_asm/freestanding/linux/hello_clobbers.c">userland/arch/aarch64/inline_asm/freestanding/linux/hello_clobbers.c</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Determining the ARM syscall numbers:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://reverseengineering.stackexchange.com/questions/16917/arm64-syscalls-table" class="bare">https://reverseengineering.stackexchange.com/questions/16917/arm64-syscalls-table</a></p>
</li>
<li>
<p>arm: <a href="https://github.com/torvalds/linux/blob/v4.17/arch/arm/tools/syscall.tbl" class="bare">https://github.com/torvalds/linux/blob/v4.17/arch/arm/tools/syscall.tbl</a></p>
</li>
<li>
<p>aarch64: <a href="https://github.com/torvalds/linux/blob/v4.17/include/uapi/asm-generic/unistd.h" class="bare">https://github.com/torvalds/linux/blob/v4.17/include/uapi/asm-generic/unistd.h</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Determining the ARM syscall interface:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/12946958/what-is-the-interface-for-arm-system-calls-and-where-is-it-defined-in-the-linux" class="bare">https://stackoverflow.com/questions/12946958/what-is-the-interface-for-arm-system-calls-and-where-is-it-defined-in-the-linux</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/45742869/linux-syscall-conventions-for-armv8" class="bare">https://stackoverflow.com/questions/45742869/linux-syscall-conventions-for-armv8</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Questions about the C inline assembly examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>x86_64: <a href="https://stackoverflow.com/questions/9506353/how-to-invoke-a-system-call-via-sysenter-in-inline-assembly/54956854#54956854" class="bare">https://stackoverflow.com/questions/9506353/how-to-invoke-a-system-call-via-sysenter-in-inline-assembly/54956854#54956854</a></p>
</li>
<li>
<p>ARM: <a href="https://stackoverflow.com/questions/21729497/doing-a-syscall-without-libc-using-arm-inline-assembly" class="bare">https://stackoverflow.com/questions/21729497/doing-a-syscall-without-libc-using-arm-inline-assembly</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="linux-calling-conventions"><a class="anchor" href="#linux-calling-conventions"></a><a class="link" href="#linux-calling-conventions">22.8. Linux calling conventions</a></h3>
<div class="paragraph">
<p>A summary of results is shown at: <a href="#table-linux-calling-conventions">Table 3, &#8220;Summary of Linux calling conventions for several architectures&#8221;</a>.</p>
</div>
<table id="table-linux-calling-conventions" class="tableblock frame-all grid-all stretch">
<caption class="title">Table 3. Summary of Linux calling conventions for several architectures</caption>
<colgroup>
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">arch</th>
<th class="tableblock halign-left valign-top">arguments</th>
<th class="tableblock halign-left valign-top">return value</th>
<th class="tableblock halign-left valign-top">callee saved registers</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">x86_64</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">rdi, rsi, rdx, rcx, r8, r9, xmm07</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">rax, rdx</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">rbx, rbp, r12r15</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">arm</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">r0-r3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">r0-r3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">r4-r11</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">aarch64</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">x0-x7</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">x0-x7</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">x19-x29</p></td>
</tr>
</tbody>
</table>
<div class="sect3">
<h4 id="x86_64-calling-convention"><a class="anchor" href="#x86_64-calling-convention"></a><a class="link" href="#x86_64-calling-convention">22.8.1. x86_64 calling convention</a></h4>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/x86_64.h">lkmc/x86_64.h</a> <code>ENTRY</code> and <code>EXIT</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>One important catch is that the stack must always be aligned to 16-bits before making calls: <a href="https://stackoverflow.com/questions/56324948/why-does-calling-the-c-abort-function-from-an-x86-64-assembly-function-lead-to" class="bare">https://stackoverflow.com/questions/56324948/why-does-calling-the-c-abort-function-from-an-x86-64-assembly-function-lead-to</a></p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI" class="bare">https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/18024672/what-registers-are-preserved-through-a-linux-x86-64-function-call/55207335#55207335" class="bare">https://stackoverflow.com/questions/18024672/what-registers-are-preserved-through-a-linux-x86-64-function-call/55207335#55207335</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="arm-calling-convention"><a class="anchor" href="#arm-calling-convention"></a><a class="link" href="#arm-calling-convention">22.8.2. ARM calling convention</a></h4>
<div class="paragraph">
<p>Call C standard library functions from assembly and vice versa.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>arm</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/arm.h">lkmc/arm.h</a> <code>ENTRY</code> and <code>EXIT</code></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/linux/c_from_asm.S">userland/arch/arm/linux/c_from_asm.S</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>aarch64</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/aarch64.h">lkmc/aarch64.h</a> <code>ENTRY</code> and <code>EXIT</code></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/inline_asm/linux/asm_from_c.c">userland/arch/aarch64/inline_asm/linux/asm_from_c.c</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>ARM Architecture Procedure Call Standard (AAPCS) is the name that ARM Holdings gives to the calling convention.</p>
</div>
<div class="paragraph">
<p>Official specification: <a href="http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042f/IHI0042F_aapcs.pdf" class="bare">http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042f/IHI0042F_aapcs.pdf</a></p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://en.wikipedia.org/wiki/Calling_convention#ARM_(A32" class="bare">https://en.wikipedia.org/wiki/Calling_convention#ARM_(A32</a>) Wiki contains the master list as usual.</p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/8422287/calling-c-functions-from-arm-assembly" class="bare">https://stackoverflow.com/questions/8422287/calling-c-functions-from-arm-assembly</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/261419/arm-to-c-calling-convention-registers-to-save" class="bare">https://stackoverflow.com/questions/261419/arm-to-c-calling-convention-registers-to-save</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/10494848/arm-whats-the-difference-between-apcs-and-aapcs-abi" class="bare">https://stackoverflow.com/questions/10494848/arm-whats-the-difference-between-apcs-and-aapcs-abi</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gnu-gas-assembler"><a class="anchor" href="#gnu-gas-assembler"></a><a class="link" href="#gnu-gas-assembler">22.9. GNU GAS assembler</a></h3>
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/GNU_Assembler">GNU GAS</a> is the default assembler used by GDB, and therefore it completely dominates in Linux.</p>
</div>
<div class="paragraph">
<p>The Linux kernel in particular uses GNU GAS assembly extensively for the arch specific parts under <code>arch/</code>.</p>
</div>
<div class="sect3">
<h4 id="gnu-gas-assembler-comments"><a class="anchor" href="#gnu-gas-assembler-comments"></a><a class="link" href="#gnu-gas-assembler-comments">22.9.1. GNU GAS assembler comments</a></h4>
<div class="paragraph">
<p>In this tutorial, we use exclusively C Preprocessor <code>/**/</code> comments because:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>they are the same for all archs</p>
</li>
<li>
<p>we are already stuck to the C Preprocessor because GNU GAS macros are unusable so we need <code>#define</code></p>
</li>
<li>
<p>mixing <code>#</code> GNU GAS comments and <code>#define</code> is a bad idea ;-)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>But just in case you want to suffer, see this full explanation of GNU GAS comments: <a href="https://stackoverflow.com/questions/15663280/how-to-make-the-gnu-assembler-use-a-slash-for-comments/51991349#51991349" class="bare">https://stackoverflow.com/questions/15663280/how-to-make-the-gnu-assembler-use-a-slash-for-comments/51991349#51991349</a></p>
</div>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/comments.S">userland/arch/arm/comments.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/comments.S">userland/arch/aarch64/comments.S</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="gnu-gas-assembler-immediates"><a class="anchor" href="#gnu-gas-assembler-immediates"></a><a class="link" href="#gnu-gas-assembler-immediates">22.9.2. GNU GAS assembler immediates</a></h4>
<div class="paragraph">
<p>Summary:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>x86 always dollar <code>$</code> everywhere.</p>
</li>
<li>
<p>ARM: can use either <code>#</code>, <code>$</code> or nothing depending on v7 vs v8 and <a href="#gnu-gas-assembler-arm-unified-syntax"><code>.syntax unified</code></a>.</p>
<div class="paragraph">
<p>Fuller explanation at: <a href="https://stackoverflow.com/questions/21652884/is-the-hash-required-for-immediate-values-in-arm-assembly/51987780#51987780" class="bare">https://stackoverflow.com/questions/21652884/is-the-hash-required-for-immediate-values-in-arm-assembly/51987780#51987780</a></p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/immediates.S">userland/arch/arm/immediates.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/immediates.S">userland/arch/aarch64/immediates.S</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="gnu-gas-assembler-data-sizes"><a class="anchor" href="#gnu-gas-assembler-data-sizes"></a><a class="link" href="#gnu-gas-assembler-data-sizes">22.9.3. GNU GAS assembler data sizes</a></h4>
<div class="paragraph">
<p>Let&#8217;s see how many bytes go into each data type:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/gas_data_sizes.S">userland/arch/x86_64/gas_data_sizes.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/gas_data_sizes.S">userland/arch/arm/gas_data_sizes.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/gas_data_sizes.S">userland/arch/aarch64/gas_data_sizes.S</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The results are shown at: <a href="#table-gas-data-sizes">Table 4, &#8220;Summary of GNU GAS assembler data sizes&#8221;</a>.</p>
</div>
<table id="table-gas-data-sizes" class="tableblock frame-all grid-all stretch">
<caption class="title">Table 4. Summary of GNU GAS assembler data sizes</caption>
<colgroup>
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">.byte</th>
<th class="tableblock halign-left valign-top">.word</th>
<th class="tableblock halign-left valign-top">.long</th>
<th class="tableblock halign-left valign-top">.quad</th>
<th class="tableblock halign-left valign-top">.octa</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">x86</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">4</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">8</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">16</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">arm</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">4</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">4</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">8</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">16</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">aarch64</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">4</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>and also keep in mind that according to the manual:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>.int</code> is the same as <code>.long</code></p>
</li>
<li>
<p><code>.hword</code> is the same as <code>.short</code> which is usually the same as <code>.word</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://sourceware.org/binutils/docs-2.32/as/Pseudo-Ops.html#Pseudo-Ops" class="bare">https://sourceware.org/binutils/docs-2.32/as/Pseudo-Ops.html#Pseudo-Ops</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/43005411/how-does-the-quad-directive-work-in-assembly/43006616" class="bare">https://stackoverflow.com/questions/43005411/how-does-the-quad-directive-work-in-assembly/43006616</a></p>
</li>
<li>
<p><a href="https://gist.github.com/steakknife/d47d0b19a24817f48027" class="bare">https://gist.github.com/steakknife/d47d0b19a24817f48027</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="gnu-gas-assembler-arm-specifics"><a class="anchor" href="#gnu-gas-assembler-arm-specifics"></a><a class="link" href="#gnu-gas-assembler-arm-specifics">22.9.3.1. GNU GAS assembler ARM specifics</a></h5>
<div class="sect5">
<h6 id="gnu-gas-assembler-arm-unified-syntax"><a class="anchor" href="#gnu-gas-assembler-arm-unified-syntax"></a><a class="link" href="#gnu-gas-assembler-arm-unified-syntax">22.9.3.1.1. GNU GAS assembler ARM unified syntax</a></h6>
<div class="paragraph">
<p>There are two types of ARMv7 assemblies:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>.syntax divided</code></p>
</li>
<li>
<p><code>.syntax unified</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>They are very similar, but unified is the new and better one, which we use in this tutorial.</p>
</div>
<div class="paragraph">
<p>Unfortunately, for backwards compatibility, GNU AS 2.31.1 and GCC 8.2.0 still use <code>.syntax divided</code> by default.</p>
</div>
<div class="paragraph">
<p>The concept of unified assembly is mentioned in ARM&#8217;s official assembler documentation: <a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0473c/BABJIHGJ.html" class="bare">http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0473c/BABJIHGJ.html</a> and is often called Unified Assembly Language (UAL).</p>
</div>
<div class="paragraph">
<p>Some of the differences include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>#</code> is optional in unified syntax int literals, see <a href="#gnu-gas-assembler-immediates">GNU GAS assembler immediates</a></p>
</li>
<li>
<p>many mnemonics changed:</p>
<div class="ulist">
<ul>
<li>
<p>most of them are condition code position changes, e.g. ANDSEQ vs ANDEQS: <a href="https://stackoverflow.com/questions/51184921/wierd-gcc-behaviour-with-arm-assembler-andseq-instruction" class="bare">https://stackoverflow.com/questions/51184921/wierd-gcc-behaviour-with-arm-assembler-andseq-instruction</a></p>
</li>
<li>
<p>but there are some more drastic ones, e.g. SWI vs <a href="#arm-svc-instruction">SVC</a>: <a href="https://stackoverflow.com/questions/8459279/are-arm-instructuons-swi-and-svc-exactly-same-thing/54078731#54078731" class="bare">https://stackoverflow.com/questions/8459279/are-arm-instructuons-swi-and-svc-exactly-same-thing/54078731#54078731</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>cannot have implicit destination with shift, see: <a href="#arm-shift-suffixes">Section 24.4.4.1, &#8220;ARM shift suffixes&#8221;</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect4">
<h5 id="gnu-gas-assembler-arm-n-and-w-suffixes"><a class="anchor" href="#gnu-gas-assembler-arm-n-and-w-suffixes"></a><a class="link" href="#gnu-gas-assembler-arm-n-and-w-suffixes">22.9.3.2. GNU GAS assembler ARM .n and .w suffixes</a></h5>
<div class="paragraph">
<p>When reading disassembly, many instructions have either a <code>.n</code> or <code>.w</code> suffix.</p>
</div>
<div class="paragraph">
<p><code>.n</code> means narrow, and stands for the <a href="#arm-instruction-encodings">Thumb encoding</a> of an instructions, while <code>.w</code> means wide and stands for the ARM encoding.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/27147043/n-suffix-to-branch-instruction" class="bare">https://stackoverflow.com/questions/27147043/n-suffix-to-branch-instruction</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="gnu-gas-assembler-char-literals"><a class="anchor" href="#gnu-gas-assembler-char-literals"></a><a class="link" href="#gnu-gas-assembler-char-literals">22.9.4. GNU GAS assembler char literals</a></h4>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/char_literals.S">userland/arch/x86_64/char_literals.S</a></p>
</div>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/33246811/how-to-use-character-literals-in-gnu-gas-to-replace-numbers" class="bare">https://stackoverflow.com/questions/33246811/how-to-use-character-literals-in-gnu-gas-to-replace-numbers</a></p>
</div>
<div class="paragraph">
<p>This syntax plays horribly with the C preprocessor:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>MACRO($'a)</pre>
</div>
</div>
<div class="paragraph">
<p>fails because cpp treats string and char literals magically.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="nop-instructions"><a class="anchor" href="#nop-instructions"></a><a class="link" href="#nop-instructions">22.10. NOP instructions</a></h3>
<div class="ulist">
<ul>
<li>
<p>x86: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/nop.S">NOP</a></p>
</li>
<li>
<p>ARM: <a href="#arm-nop-instruction">Section 24.5.1, &#8220;ARM NOP instruction&#8221;</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>No OPeration.</p>
</div>
<div class="paragraph">
<p>Does nothing except take up one processor cycle and occupy some instruction memory.</p>
</div>
<div class="paragraph">
<p>Applications: <a href="https://stackoverflow.com/questions/234906/whats-the-purpose-of-the-nop-opcode" class="bare">https://stackoverflow.com/questions/234906/whats-the-purpose-of-the-nop-opcode</a></p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="x86-userland-assembly"><a class="anchor" href="#x86-userland-assembly"></a><a class="link" href="#x86-userland-assembly">23. x86 userland assembly</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Arch agnostic infrastructure getting started at: <a href="#userland-assembly">Section 22, &#8220;Userland assembly&#8221;</a>.</p>
</div>
<div class="sect2">
<h3 id="x86-registers"><a class="anchor" href="#x86-registers"></a><a class="link" href="#x86-registers">23.1. x86 registers</a></h3>
<div class="paragraph">
<p>link:userland/arch/x86_64/registers.S</p>
</div>
<div class="literalblock">
<div class="content">
<pre>|-----------------------------------------------|
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|-----------------------------------------------|
| | | AH | AL |
|-----------------------------------------------|
| | | AX |
|-----------------------------------------------|
| | EAX |
|-----------------------------------------------|
| RAX |
|-----------------------------------------------|</pre>
</div>
</div>
<div class="paragraph">
<p>For the newer x86_64 registers, the naming convention is somewhat saner:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>|-----------------------------------------------|
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|-----------------------------------------------|
| | |R12H |R12L |
|-----------------------------------------------|
| | | R12W |
|-----------------------------------------------|
| | R12D |
|-----------------------------------------------|
| R12 |
|-----------------------------------------------|</pre>
</div>
</div>
<div class="paragraph">
<p>Most of the 8 older x86 general purpose registers are not "really" general purpose in the sense that a few instructions magically use them without an explicit encoding. This is reflected in their names:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>RAX: Accumulator. The general place where you add, subtract and otherwise manipulate results in-place. Magic for example for <a href="#x86-binary-arithmetic-instructions">MUL</a>.</p>
</li>
<li>
<p>RCX, RSI, RDI: Counter, Source and Destination. Used in <a href="#x86-string-instructions">x86 string instructions</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="x86-flags-registers"><a class="anchor" href="#x86-flags-registers"></a><a class="link" href="#x86-flags-registers">23.1.1. x86 FLAGS registers</a></h4>
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/FLAGS_register" class="bare">https://en.wikipedia.org/wiki/FLAGS_register</a></p>
</div>
<div class="paragraph">
<p>TODO: add some more info here. Just need a link placeholder for now.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="x86-addressing-modes"><a class="anchor" href="#x86-addressing-modes"></a><a class="link" href="#x86-addressing-modes">23.2. x86 addressing modes</a></h3>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/address_modes.S">userland/arch/x86_64/address_modes.S</a></p>
</div>
<div class="paragraph">
<p>Several x86 instructions can calculate addresses of a complex form:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>s:a(b, c, d)</pre>
</div>
</div>
<div class="paragraph">
<p>which expands to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>a + b + c * d</pre>
</div>
</div>
<div class="paragraph">
<p>Where the instruction encoding allows for:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>a</code>: any 8 or 32-bit general purpose register</p>
</li>
<li>
<p><code>b</code>: any 32-bit general purpose register except ESP</p>
</li>
<li>
<p><code>c</code>: 1, 2, 4 or 8 (encoded in 2 SIB bits)</p>
</li>
<li>
<p><code>d</code>: immediate constant</p>
</li>
<li>
<p><code>s</code>: a segment register. Cannot be tested simply from userland, so we won&#8217;t talk about them here. See: <a href="https://github.com/cirosantilli/x86-bare-metal-examples/blob/6606a2647d44bc14e6fd695c0ea2b6b7a5f04ca3/segment_registers_real_mode.S" class="bare">https://github.com/cirosantilli/x86-bare-metal-examples/blob/6606a2647d44bc14e6fd695c0ea2b6b7a5f04ca3/segment_registers_real_mode.S</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The common compiler usage is:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>a</code>: base pointer</p>
</li>
<li>
<p><code>b</code>: array offset</p>
</li>
<li>
<p><code>c</code> and <code>d</code>: struct offset</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 3.7.5 "Specifying an Offset"</p>
</li>
<li>
<p><a href="https://sourceware.org/binutils/docs-2.18/as/i386_002dMemory.html" class="bare">https://sourceware.org/binutils/docs-2.18/as/i386_002dMemory.html</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="x86-data-transfer-instructions"><a class="anchor" href="#x86-data-transfer-instructions"></a><a class="link" href="#x86-data-transfer-instructions">23.3. x86 data transfer instructions</a></h3>
<div class="paragraph">
<p>5.1.1 "Data Transfer Instructions"</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/lea.S">userland/arch/x86_64/lea.S</a>: LEA</p>
</li>
<li>
<p>Integer typecasts</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/movzx.S">userland/arch/x86_64/movzx.S</a>: MOVZX</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/movsx.S">userland/arch/x86_64/movsx.S</a>: MOVSX</p>
</li>
</ul>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/bswap.S">userland/arch/x86_64/bswap.S</a>: BSWAP: convert between little endian and big endian</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/pushf.S">userland/arch/x86_64/pushf.S</a> PUSHF: <a href="#x86-push-and-pop-instructions">push and pop</a> the <a href="#x86-flags-registers">x86 FLAGS registers</a> to / from the stack</p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="x86-exchange-instructions"><a class="anchor" href="#x86-exchange-instructions"></a><a class="link" href="#x86-exchange-instructions">23.3.1. x86 exchange instructions</a></h4>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 7.3.1.2 "Exchange Instructions":</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/xadd.S">userland/arch/x86_64/xadd.S</a> XADD: exchange and add. This is how C `&lt;atomic&gt;`'s' `` is implemented in GCC 5.1. TODO: why is the exchange part needed?</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/xchg.S">userland/arch/x86_64/xchg.S</a> XCHG: exchange two values</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>TODO: concrete multi-thread <a href="#gcc-inline-assembly">GCC inline assembly</a> examples of how all those instructions are normally used as synchronization primitives.</p>
</div>
<div class="sect4">
<h5 id="x86-cmpxchg-instruction"><a class="anchor" href="#x86-cmpxchg-instruction"></a><a class="link" href="#x86-cmpxchg-instruction">23.3.1.1. x86 CMPXCHG instruction</a></h5>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/cmpxchg.S">userland/arch/x86_64/cmpxchg.S</a></p>
</div>
<div class="paragraph">
<p>CMPXCHG: compare and exchange. <code>cmpxchg a, b</code> does:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>if (RAX == b) {
ZF = 1
b = a
} else {
ZF = 0
RAX = b
}</pre>
</div>
</div>
<div class="paragraph">
<p>TODO application: <a href="https://stackoverflow.com/questions/6935442/x86-spinlock-using-cmpxchg" class="bare">https://stackoverflow.com/questions/6935442/x86-spinlock-using-cmpxchg</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="x86-push-and-pop-instructions"><a class="anchor" href="#x86-push-and-pop-instructions"></a><a class="link" href="#x86-push-and-pop-instructions">23.3.2. x86 PUSH and POP instructions</a></h4>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/push.S">userland/arch/x86_64/push.S</a></p>
</div>
<div class="paragraph">
<p><code>push %rax</code> is basically equivalent to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sub $8, %rsp
mov %rax, (%rsp)</pre>
</div>
</div>
<div class="paragraph">
<p>and <code>pop %rax</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mov (%rsp), %rax
add $8, %rsp</pre>
</div>
</div>
<div class="paragraph">
<p>Why do those instructions exist at all vs MOV / ADD / SUB: <a href="https://stackoverflow.com/questions/4584089/what-is-the-function-of-push-pop-registers-in-x86-assembly/33583134#33583134" class="bare">https://stackoverflow.com/questions/4584089/what-is-the-function-of-push-pop-registers-in-x86-assembly/33583134#33583134</a></p>
</div>
</div>
<div class="sect3">
<h4 id="x86-cqto-and-cltq-instructions"><a class="anchor" href="#x86-cqto-and-cltq-instructions"></a><a class="link" href="#x86-cqto-and-cltq-instructions">23.3.3. x86 CQTO and CLTQ instructions</a></h4>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/cqto.S">userland/arch/x86_64/cqto.S</a> CQTO</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/cltq.S">userland/arch/x86_64/cltq.S</a> CLTQ</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Instructions without E suffix: sign extend RAX into RDX:RAX.</p>
</div>
<div class="paragraph">
<p>Instructions E suffix: sign extend withing RAX itself.</p>
</div>
<div class="paragraph">
<p>Common combo with IDIV 32-bit, which takes the input from EDX:EAX: so you need to set up EDX before calling it.</p>
</div>
<div class="paragraph">
<p>Has some Intel vs AT&amp;T name overload hell:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/6555094/what-does-cltq-do-in-assembly/45386217#45386217" class="bare">https://stackoverflow.com/questions/6555094/what-does-cltq-do-in-assembly/45386217#45386217</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/17170388/trying-to-understand-the-assembly-instruction-cltd-on-x86/50315201#50315201" class="bare">https://stackoverflow.com/questions/17170388/trying-to-understand-the-assembly-instruction-cltd-on-x86/50315201#50315201</a></p>
</li>
<li>
<p><a href="https://sourceware.org/binutils/docs/as/i386_002dMnemonics.html" class="bare">https://sourceware.org/binutils/docs/as/i386_002dMnemonics.html</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>GNU GAS accepts both syntaxes, see: <a href="#table-cqto-cltq">Table 5, &#8220;CQTO and CLTQ family Intel vs AT&amp;T&#8221;</a>.</p>
</div>
<table id="table-cqto-cltq" class="tableblock frame-all grid-all stretch">
<caption class="title">Table 5. CQTO and CLTQ family Intel vs AT&amp;T</caption>
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Intel</th>
<th class="tableblock halign-left valign-top">AT&amp;T</th>
<th class="tableblock halign-left valign-top">From</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">To</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">CBW</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">CBTW</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">AL</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">AX</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">CWDE</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">CWTL</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">AX</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">EAX</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">CWD</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">CWTD</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">AX</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">DX:AX</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">CDQ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">CLTD</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">EAX</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">EDX:EAX</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">CDQE</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">CLTQ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">EAX</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">RAX</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">CQO</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">CQTO</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">RAX</p></td>
</tr>
</tbody>
</table>
</div>
<div class="sect3">
<h4 id="x86-cmovcc-instructions"><a class="anchor" href="#x86-cmovcc-instructions"></a><a class="link" href="#x86-cmovcc-instructions">23.3.4. x86 CMOVcc instructions</a></h4>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/cmovcc.S">userland/arch/x86_64/cmovcc.S</a>: CMOVcc</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>mov if a condition is met:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>CMOVcc a, b</pre>
</div>
</div>
<div class="paragraph">
<p>Equals:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>if(flag) a = b</pre>
</div>
</div>
<div class="paragraph">
<p>where <code>cc</code> are the same flags as Jcc.</p>
</div>
<div class="paragraph">
<p>Vs jmp:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/14131096/why-is-a-conditional-move-not-vulnerable-for-branch-prediction-failure" class="bare">https://stackoverflow.com/questions/14131096/why-is-a-conditional-move-not-vulnerable-for-branch-prediction-failure</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/27136961/what-is-it-about-cmov-which-improves-cpu-pipeline-performance" class="bare">https://stackoverflow.com/questions/27136961/what-is-it-about-cmov-which-improves-cpu-pipeline-performance</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/26154488/difference-between-conditional-instructions-cmov-and-jump-instructions" class="bare">https://stackoverflow.com/questions/26154488/difference-between-conditional-instructions-cmov-and-jump-instructions</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/6754454/speed-difference-between-if-else-and-ternary-operator-in-c?lq=1#comment8007791_6754495" class="bare">https://stackoverflow.com/questions/6754454/speed-difference-between-if-else-and-ternary-operator-in-c?lq=1#comment8007791_6754495</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Not necessarily faster because of branch prediction.</p>
</div>
<div class="paragraph">
<p>This is partly why the ternary <code>?</code> C operator exists: <a href="https://stackoverflow.com/questions/3565368/ternary-operator-vs-if-else" class="bare">https://stackoverflow.com/questions/3565368/ternary-operator-vs-if-else</a></p>
</div>
<div class="paragraph">
<p>It is interesting to compare this with ARMv7 conditional execution: which is available for all instructions, as shown at: <a href="#arm-conditional-execution">Section 24.2.5, &#8220;ARM conditional execution&#8221;</a>.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="x86-binary-arithmetic-instructions"><a class="anchor" href="#x86-binary-arithmetic-instructions"></a><a class="link" href="#x86-binary-arithmetic-instructions">23.4. x86 binary arithmetic instructions</a></h3>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.1.2 "Binary Arithmetic Instructions":</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/add.S">userland/arch/x86_64/add.S</a>: ADD</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/inc.S">userland/arch/x86_64/inc.S</a>: INC</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/adc.S">userland/arch/x86_64/adc.S</a>: ADC</p>
</li>
</ul>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/sub.S">userland/arch/x86_64/sub.S</a>: SUB</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/dec.S">userland/arch/x86_64/dec.S</a>: DEC</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/sbb.S">userland/arch/x86_64/sbb.S</a>: SBB</p>
</li>
</ul>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/mul.S">userland/arch/x86_64/mul.S</a>: MUL</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/neg.S">userland/arch/x86_64/neg.S</a>: NEG</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/imul.S">userland/arch/x86_64/imul.S</a>: IMUL</p>
</li>
</ul>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/div.S">userland/arch/x86_64/div.S</a>: DIV</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/div_overflow.S">userland/arch/x86_64/div_overflow.S</a>: DIV overflow</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/div_zero.S">userland/arch/x86_64/div_zero.S</a>: DIV zero</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/idiv.S">userland/arch/x86_64/idiv.S</a>: IDIV</p>
</li>
</ul>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/cmp.S">userland/arch/x86_64/cmp.S</a>: CMP</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="x86-logical-instructions"><a class="anchor" href="#x86-logical-instructions"></a><a class="link" href="#x86-logical-instructions">23.5. x86 logical instructions</a></h3>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.1.4 "Logical Instructions"</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/and.S">userland/arch/x86_64/and.S</a>: AND</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/not.S">userland/arch/x86_64/not.S</a>: NOT</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/or.S">userland/arch/x86_64/or.S</a>: OR</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/xor.S">userland/arch/x86_64/xor.S</a>: XOR</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="x86-shift-and-rotate-instructions"><a class="anchor" href="#x86-shift-and-rotate-instructions"></a><a class="link" href="#x86-shift-and-rotate-instructions">23.6. x86 shift and rotate instructions</a></h3>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.1.5 "Shift and Rotate Instructions"</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/shl.S">SHL and SHR</a></p>
<div class="paragraph">
<p>SHift left or Right and insert 0.</p>
</div>
<div class="paragraph">
<p>CF == the bit that got shifted out.</p>
</div>
<div class="paragraph">
<p>Application: quick unsigned multiply and divide by powers of 2.</p>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/sal.S">SAL and SAR</a></p>
<div class="paragraph">
<p>Application: signed multiply and divide by powers of 2.</p>
</div>
<div class="paragraph">
<p>Mnemonics: Shift Arithmetic Left and Right</p>
</div>
<div class="paragraph">
<p>Keeps the same sign on right shift.</p>
</div>
<div class="paragraph">
<p>Not directly exposed in C, for which signed shift is undetermined behavior, but does exist in Java via the <code>&gt;&gt;&gt;</code> operator. C compilers can omit it however.</p>
</div>
<div class="paragraph">
<p>SHL and SAL are exactly the same and have the same encoding: <a href="https://stackoverflow.com/questions/8373415/difference-between-shl-and-sal-in-80x86/56621271#56621271" class="bare">https://stackoverflow.com/questions/8373415/difference-between-shl-and-sal-in-80x86/56621271#56621271</a></p>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/rol.S">userland/arch/x86_64/rol.S</a>: ROL and ROR</p>
<div class="paragraph">
<p>Rotates the bit that is going out around to the other side.</p>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/rol.S">userland/arch/x86_64/rol.S</a>: RCL and RCR</p>
<div class="paragraph">
<p>Like ROL and ROR, but insert the carry bit instead, which effectively generates a rotation of 8 + 1 bits. TODO application.</p>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="x86-bit-and-byte-instructions"><a class="anchor" href="#x86-bit-and-byte-instructions"></a><a class="link" href="#x86-bit-and-byte-instructions">23.7. x86 bit and byte instructions</a></h3>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.1.6 "Bit and Byte Instructions"</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/bt.S">userland/arch/x86_64/bt.S</a>: BT</p>
<div class="paragraph">
<p>Bit test: test if the Nth bit a bit of a register is set and store the result in the CF FLAG.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>CF = reg[N]</pre>
</div>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/btr.S">userland/arch/x86_64/btr.S</a>: BTR</p>
<div class="paragraph">
<p>Do a BT and then set the bit to 0.</p>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/btc.S">userland/arch/x86_64/btc.S</a>: BTC</p>
<div class="paragraph">
<p>Do a BT and then swap the value of the tested bit.</p>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/setcc.S">userland/arch/x86_64/setcc.S</a>: SETcc</p>
<div class="paragraph">
<p>Set a byte of a register to 0 or 1 depending on the cc condition.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/1406783/how-to-read-and-write-x86-flags-registers-directly/30952577#30952577" class="bare">https://stackoverflow.com/questions/1406783/how-to-read-and-write-x86-flags-registers-directly/30952577#30952577</a></p>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/popcnt.S">userland/arch/x86_64/popcnt.S</a>: POPCNT</p>
<div class="paragraph">
<p>Count the number of 1 bits.</p>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/test.S">userland/arch/x86_64/test.S</a>: TEST</p>
<div class="paragraph">
<p>Like <a href="#x86-binary-arithmetic-instructions">CMP</a> but does AND instead of SUB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ZF = (!(X &amp;&amp; Y)) ? 1 : 0</pre>
</div>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="x86-control-transfer-instructions"><a class="anchor" href="#x86-control-transfer-instructions"></a><a class="link" href="#x86-control-transfer-instructions">23.8. x86 control transfer instructions</a></h3>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.1.7 "Control Transfer Instructions"</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/jmp.S">userland/arch/x86_64/jmp.S</a>: JMP</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/jmp_indirect.S">userland/arch/x86_64/jmp_indirect.S</a>: JMP indirect</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="x86-jcc-instructions"><a class="anchor" href="#x86-jcc-instructions"></a><a class="link" href="#x86-jcc-instructions">23.8.1. x86 Jcc instructions</a></h4>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/jcc.S">userland/arch/x86_64/jcc.S</a></p>
</div>
<div class="paragraph">
<p>Jump if certain conditions of the flags register are met.</p>
</div>
<div class="paragraph">
<p>Jcc includes the instructions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>JZ, JNZ</p>
<div class="ulist">
<ul>
<li>
<p>JE, JNE: same as JZ, with two separate manual entries that say almost the same thing, lol: <a href="https://stackoverflow.com/questions/14267081/difference-between-je-jne-and-jz-jnz/14267662#14267662" class="bare">https://stackoverflow.com/questions/14267081/difference-between-je-jne-and-jz-jnz/14267662#14267662</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>JG: greater than, signed</p>
<div class="ulist">
<ul>
<li>
<p>JA: Above: greater than, unsigned</p>
</li>
</ul>
</div>
</li>
<li>
<p>JL: less than, signed</p>
<div class="ulist">
<ul>
<li>
<p>JB below: less than, unsigned</p>
</li>
</ul>
</div>
</li>
<li>
<p>JC: carry</p>
</li>
<li>
<p>JO: overflow</p>
</li>
<li>
<p>JP: parity. Why it exists: <a href="https://stackoverflow.com/questions/25707130/what-is-the-purpose-of-the-parity-flag-on-a-cpu" class="bare">https://stackoverflow.com/questions/25707130/what-is-the-purpose-of-the-parity-flag-on-a-cpu</a></p>
</li>
<li>
<p>JPE: parity even</p>
</li>
<li>
<p>JPO: parity odd</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>JG vs JA and JL vs JB:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/9617877/assembly-jg-jnle-jl-jnge-after-cmp/56613928#56613928" class="bare">https://stackoverflow.com/questions/9617877/assembly-jg-jnle-jl-jnge-after-cmp/56613928#56613928</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/20906639/difference-between-ja-and-jg-in-assembly" class="bare">https://stackoverflow.com/questions/20906639/difference-between-ja-and-jg-in-assembly</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="x86-loop-instruction"><a class="anchor" href="#x86-loop-instruction"></a><a class="link" href="#x86-loop-instruction">23.8.2. x86 LOOP instruction</a></h4>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/loop.S">userland/arch/x86_64/loop.S</a></p>
</div>
<div class="paragraph">
<p>Vs <a href="#x86-jcc-instructions">Jcc</a>: <a href="https://stackoverflow.com/questions/6805692/x86-assembly-programming-loops-with-ecx-and-loop-instruction-versus-jmp-jcond" class="bare">https://stackoverflow.com/questions/6805692/x86-assembly-programming-loops-with-ecx-and-loop-instruction-versus-jmp-jcond</a> Holy CISC!</p>
</div>
</div>
<div class="sect3">
<h4 id="x86-string-instructions"><a class="anchor" href="#x86-string-instructions"></a><a class="link" href="#x86-string-instructions">23.8.3. x86 string instructions</a></h4>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.1.8 "String Instructions"</p>
</div>
<div class="paragraph">
<p>These instructions do some operation on an array item, and automatically update the index to the next item:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>First example explained in more detail</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/stos.S">userland/arch/x86_64/stos.S</a>: STOS: STOre String: store register to memory. STOSD is called STOSL in GNU GAS as usual: <a href="https://stackoverflow.com/questions/6211629/gcc-inline-assembly-error-no-such-instruction-stosd" class="bare">https://stackoverflow.com/questions/6211629/gcc-inline-assembly-error-no-such-instruction-stosd</a></p>
</li>
</ul>
</div>
</li>
<li>
<p>Further examples</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/cmps.S">userland/arch/x86_64/cmps.S</a>: CMPS: CoMPare Strings: compare two values in memory with addresses given by RSI and RDI. Could be used to implement <code>memcmp</code>. Store the result in JZ as usual.</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/lods.S">userland/arch/x86_64/lods.S</a>: LODS: LOaD String: load from memory to register.</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/movs.S">userland/arch/x86_64/movs.S</a>: MOVS: MOV String: move from one memory to another with addresses given by RSI and RDI. Could be used to implement <code>memmov</code>.</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/scas.S">userland/arch/x86_64/scas.S</a>: SCAS: SCan String: compare memory to the value in a register. Could be used to implement <code>strchr</code>.</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>The RSI and RDI registers are actually named after these intructions! S is the source of string instructions, D is the destination of string instructions: <a href="https://stackoverflow.com/questions/1856320/purpose-of-esi-edi-registers" class="bare">https://stackoverflow.com/questions/1856320/purpose-of-esi-edi-registers</a></p>
</div>
<div class="paragraph">
<p>The direction of the index increment depends on the direction flag of the FLAGS register: 0 means forward and 1 means backward: <a href="https://stackoverflow.com/questions/9636691/what-are-cld-and-std-for-in-x86-assembly-language-what-does-df-do" class="bare">https://stackoverflow.com/questions/9636691/what-are-cld-and-std-for-in-x86-assembly-language-what-does-df-do</a></p>
</div>
<div class="paragraph">
<p>These instructions were originally developed to speed up "string" operations such as those present in the <code>&lt;string.h&gt;</code> header of the C standard library.</p>
</div>
<div class="paragraph">
<p>However, as computer architecture evolved, those instructions might not offer considerable speedups anymore, and modern glibc such as 2.29 just uses <a href="#x86-simd">x86 SIMD</a> operations instead:, see also: <a href="https://stackoverflow.com/questions/33480999/how-can-the-rep-stosb-instruction-execute-faster-than-the-equivalent-loop" class="bare">https://stackoverflow.com/questions/33480999/how-can-the-rep-stosb-instruction-execute-faster-than-the-equivalent-loop</a></p>
</div>
<div class="sect4">
<h5 id="x86-rep-prefix"><a class="anchor" href="#x86-rep-prefix"></a><a class="link" href="#x86-rep-prefix">23.8.3.1. x86 REP prefix</a></h5>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/rep.S">userland/arch/x86_64/rep.S</a></p>
</div>
<div class="paragraph">
<p>Repeat a string instruction RCX times:</p>
</div>
<div class="paragraph">
<p>As the repetitions happen:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>RCX decreases, until it reaches 0</p>
</li>
<li>
<p>RDI and RSI increase</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The variants: REPZ, REPNZ (alias REPE, REPNE) repeat a given instruction until something happens.</p>
</div>
<div class="paragraph">
<p>REP and REPZ also additionally stop if the comparison operation they repeat fails.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>REP: INS, OUTS, MOVS, LODS, and STOS</p>
</li>
<li>
<p>REPZ: CMPS and SCAS</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect3">
<h4 id="x86-enter-and-leave-instructions"><a class="anchor" href="#x86-enter-and-leave-instructions"></a><a class="link" href="#x86-enter-and-leave-instructions">23.8.4. x86 ENTER and LEAVE instructions</a></h4>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/enter.S">userland/arch/x86_64/enter.S</a></p>
</div>
<div class="paragraph">
<p>These instructions were designed to allocate and deallocate function stack frames in the prologue and epilogue: <a href="https://stackoverflow.com/questions/5959890/enter-vs-push-ebp-mov-ebp-esp-sub-esp-imm-and-leave-vs-mov-esp-ebp" class="bare">https://stackoverflow.com/questions/5959890/enter-vs-push-ebp-mov-ebp-esp-sub-esp-imm-and-leave-vs-mov-esp-ebp</a></p>
</div>
<div class="paragraph">
<p>ENTER appears obsolete and is kept mostly for backwards compatibility. LEAVE is still emitted by some compilers.</p>
</div>
<div class="paragraph">
<p>ENTER A, B is basically equivalent to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>push %rbp
mov %rsp, %rbp
sub %rsp, A</pre>
</div>
</div>
<div class="paragraph">
<p>which implies an allocation of:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>one dword to remember EBP</p>
</li>
<li>
<p>A bytes for local function variables</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>I didn&#8217;t have the patience to study the B parameter, and it does not seem to be used often: <a href="https://stackoverflow.com/questions/26323215/do-any-languages-compilers-utilize-the-x86-enter-instruction-with-a-nonzero-ne" class="bare">https://stackoverflow.com/questions/26323215/do-any-languages-compilers-utilize-the-x86-enter-instruction-with-a-nonzero-ne</a></p>
</div>
<div class="paragraph">
<p>LEAVE is equivalent to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mov %rbp, %rsp
pop %rbp</pre>
</div>
</div>
<div class="paragraph">
<p>which restores RSP and RBP to the values they had before the prologue.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="x86-miscellaneous-instructions"><a class="anchor" href="#x86-miscellaneous-instructions"></a><a class="link" href="#x86-miscellaneous-instructions">23.9. x86 miscellaneous instructions</a></h3>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.1.13 "Miscellaneous Instructions"</p>
</div>
<div class="paragraph">
<p>NOP: <a href="#nop-instructions">Section 22.10, &#8220;NOP instructions&#8221;</a></p>
</div>
</div>
<div class="sect2">
<h3 id="x86-random-number-generator-instructions"><a class="anchor" href="#x86-random-number-generator-instructions"></a><a class="link" href="#x86-random-number-generator-instructions">23.10. x86 random number generator instructions</a></h3>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.1.15 Random Number Generator Instructions</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/rdrand.S">userland/arch/x86_64/rdrand.S</a>: RDRAND</p>
</div>
<div class="paragraph">
<p>If you run that executable multiple times, it prints a random number every time to stdout.</p>
</div>
<div class="paragraph">
<p>RDRAND is a true random number generator!</p>
</div>
<div class="paragraph">
<p>This Intel engineer says its based on quantum effects: <a href="https://stackoverflow.com/questions/17616960/true-random-numbers-with-c11-and-rdrand/18004959#18004959" class="bare">https://stackoverflow.com/questions/17616960/true-random-numbers-with-c11-and-rdrand/18004959#18004959</a></p>
</div>
<div class="paragraph">
<p>Generated some polemic when kernel devs wanted to use it as part of <code>/dev/random</code>, because it could be used as a cryptographic backdoor by Intel since it is a black box.</p>
</div>
<div class="paragraph">
<p>RDRAND sets the carry flag when data is ready so we must loop if the carry flag isn&#8217;t set.</p>
</div>
<div class="sect3">
<h4 id="x86-cpuid-instruction"><a class="anchor" href="#x86-cpuid-instruction"></a><a class="link" href="#x86-cpuid-instruction">23.10.1. x86 CPUID instruction</a></h4>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/cpuid.S">userland/arch/x86_64/cpuid.S</a></p>
</div>
<div class="paragraph">
<p>Fills EAX, EBX, ECX and EDX with CPU information.</p>
</div>
<div class="paragraph">
<p>The exact data to show depends on the value of EAX, and for a few cases instructions ECX. When it depends on ECX, it is called a sub-leaf. Out test program prints <code>eax == 0</code>.</p>
</div>
<div class="paragraph">
<p>On <a href="#p51">P51</a> for example the output EAX, EBX, ECX and EDX are:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0x00000016
0x756E6547
0x6C65746E
0x49656E69</pre>
</div>
</div>
<div class="paragraph">
<p>EBX and ECX are easy to interpret:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>EBX: 75 6e 65 47 == 'u', 'n', 'e', 'G' in ASCII</p>
</li>
<li>
<p>ECX: 6C 65 74 6E == 'l', 'e', 't', 'n'</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>so we see the string <code>Genu ntel</code> which is a shorthand for "Genuine Intel". Ha, I wonder if they had serious CPU pirating problems in the past? :-)</p>
</div>
<div class="paragraph">
<p>Information available includes:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>vendor</p>
</li>
<li>
<p>version</p>
</li>
<li>
<p>features (mmx, simd, rdrand, etc.) &lt;<a href="http://en.wikipedia.org/wiki/CPUID#" class="bare">http://en.wikipedia.org/wiki/CPUID#</a> EAX.3D1:_Processor_Info_and_Feature_Bits&gt;</p>
</li>
<li>
<p>caches</p>
</li>
<li>
<p>tlbs <a href="http://en.wikipedia.org/wiki/Translation_lookaside_buffer" class="bare">http://en.wikipedia.org/wiki/Translation_lookaside_buffer</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The cool thing about this instruction is that it allows you to check the CPU specs and take alternative actions based on that inside your program.</p>
</div>
<div class="paragraph">
<p>On Linux, the capacity part of this information is parsed and made available at <code>cat /proc/cpuinfo</code>. See: <a href="http://unix.stackexchange.com/questions/43539/what-do-the-flags-in-proc-cpuinfo-mean" class="bare">http://unix.stackexchange.com/questions/43539/what-do-the-flags-in-proc-cpuinfo-mean</a></p>
</div>
<div class="paragraph">
<p>There is also the <code>cpuinfo</code> command line tool that parses the CPUID instruction from the command line. Source: <a href="http://www.etallen.com/cpuid.html" class="bare">http://www.etallen.com/cpuid.html</a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="x86-x87-fpu-instructions"><a class="anchor" href="#x86-x87-fpu-instructions"></a><a class="link" href="#x86-x87-fpu-instructions">23.11. x86 x87 FPU instructions</a></h3>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.2 "X87 FPU INSTRUCTIONS"</p>
</div>
<div class="paragraph">
<p>Old floating point unit that you should likely not use anymore, prefer instead the newer <a href="#x86-simd">x86 SIMD</a> instructions.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>FPU basic examples, start here</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/fadd.S">userland/arch/x86_64/fadd.S</a> FADD. The x76 FPU works on a stack of floating point numbers.</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/faddp.S">userland/arch/x86_64/faddp.S</a> FADDP. Instructions with the P suffix also Pop the stack. This is often what you want for most computations, where the intermediate results don&#8217;t matter.</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/fldl_literal.S">userland/arch/x86_64/fldl_literal.S</a> FLDL literal. It does not seem possible to either <a href="https://stackoverflow.com/questions/6514537/how-do-i-specify-immediate-floating-point-numbers-with-inline-assembly" class="bare">https://stackoverflow.com/questions/6514537/how-do-i-specify-immediate-floating-point-numbers-with-inline-assembly</a></p>
<div class="ulist">
<ul>
<li>
<p>load floating point immediates into x86 x87 FPU registers</p>
</li>
<li>
<p>encode floating point literals in x86 instructions, including MOV</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li>
<p>Bulk instructions</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/fabs.S">userland/arch/x86_64/fabs.S</a> FABS: absolute value: <code>ST0 = |ST0|</code></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/fchs.S">userland/arch/x86_64/fchs.S</a> FCHS: change sign: <code>ST0 = -ST0</code></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/fild.S">userland/arch/x86_64/fild.S</a> FILD: Integer Load. Convert integer to float.</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/fld1.S">userland/arch/x86_64/fld1.S</a> FLD1: Push 1.0 to ST0. CISC!</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/fldz.S">userland/arch/x86_64/fldz.S</a> FLDZ: Push 0.0 to ST0.</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/fscale.S">userland/arch/x86_64/fscale.S</a> FSCALE: <code>ST0 = ST0 * 2 ^ RoundTowardZero(ST1)</code></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/fsqrt.S">userland/arch/x86_64/fsqrt.S</a> FSQRT: square root</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/fxch.S">userland/arch/x86_64/fxch.S</a> FXCH: swap ST0 and another register</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>The ST0-ST7 x87 FPU registers are actually 80-bits wide, this can be seen from GDB with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>i r st0 st1</pre>
</div>
</div>
<div class="paragraph">
<p>By counting the number of hex digits, we have 20 digits instead of 16!</p>
</div>
<div class="paragraph">
<p>Instructions such as FLDL convert standard <a href="#ieee-754">IEEE 754</a> 64-bit values from memory into this custom 80-bit format.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/3206101/extended-80-bit-double-floating-point-in-x87-not-sse2-we-dont-miss-it" class="bare">https://stackoverflow.com/questions/3206101/extended-80-bit-double-floating-point-in-x87-not-sse2-we-dont-miss-it</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/Extended_precision#x86_extended_precision_format" class="bare">https://en.wikipedia.org/wiki/Extended_precision#x86_extended_precision_format</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="x86-x87-fpu-vs-simd"><a class="anchor" href="#x86-x87-fpu-vs-simd"></a><a class="link" href="#x86-x87-fpu-vs-simd">23.11.1. x86 x87 FPU vs SIMD</a></h4>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/1844669/benefits-of-x87-over-sse" class="bare">https://stackoverflow.com/questions/1844669/benefits-of-x87-over-sse</a></p>
</div>
<div class="paragraph">
<p>Modern x86 has two main ways of doing floating point operations:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#x86-x87-fpu-instructions">x86 x87 FPU instructions</a></p>
</li>
<li>
<p><a href="#x86-simd">x86 SIMD</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Advantages of FPU:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>present in old CPUs, while SSE2 is only required in x86-64</p>
</li>
<li>
<p>contains some instructions no present in SSE, e.g. trigonometric</p>
</li>
<li>
<p>higher precision: FPU holds 80 bit Intel extension, while SSE2 only does up to 64 bit operations despite having the 128-bit register</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In GCC, you can choose between them with <code>-mfpmath=</code>.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="x86-simd"><a class="anchor" href="#x86-simd"></a><a class="link" href="#x86-simd">23.12. x86 SIMD</a></h3>
<div class="paragraph">
<p>Parent section: <a href="#simd-assembly">Section 22.3, &#8220;SIMD assembly&#8221;</a></p>
</div>
<div class="paragraph">
<p>History:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://en.wikipedia.org/wiki/MMX_(instruction_set)">MMX</a>: MultiMedia eXtension (unofficial name). 1997. MM0-MM7 64-bit registers.</p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/Streaming_SIMD_Extensions">SSE</a>: Streaming SIMD Extensions. 1999. XMM0-XMM7 128-bit registers, XMM0-XMM15 for AMD in 64-bit mode.</p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/SSE2">SSE2</a>: 2004</p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/SSE3">SSE3</a>: 2006</p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/SSE4">SSE4</a>: 2006</p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/Advanced_Vector_Extensions">AVX</a>: Advanced Vector Extensions. 2011. YMM0YMM15 256-bit registers in 64-bit mode. Extension of XMM.</p>
</li>
<li>
<p>AVX2:2013</p>
</li>
<li>
<p>AVX-512: 2016. 512-bit ZMM registers. Extension of YMM.</p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="x86-sse-instructions"><a class="anchor" href="#x86-sse-instructions"></a><a class="link" href="#x86-sse-instructions">23.12.1. x86 SSE instructions</a></h4>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.5 "SSE INSTRUCTIONS"</p>
</div>
<div class="sect4">
<h5 id="x86-sse-data-transfer-instructions"><a class="anchor" href="#x86-sse-data-transfer-instructions"></a><a class="link" href="#x86-sse-data-transfer-instructions">23.12.1.1. x86 SSE data transfer instructions</a></h5>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.5.1.1 "SSE Data Transfer Instructions"</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/movaps.S">userland/arch/x86_64/movaps.S</a>: MOVAPS: move 4 x 32-bits between two XMM registeres or XMM registers and 16-byte aligned memory</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/movaps.S">userland/arch/x86_64/movaps.S</a>: MOVUPS: like MOVAPS but also works for unaligned memory</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/movss.S">userland/arch/x86_64/movss.S</a>: MOVSS: move 32-bits between two XMM registeres or XMM registers and memory</p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="x86-sse-packed-arithmetic-instructions"><a class="anchor" href="#x86-sse-packed-arithmetic-instructions"></a><a class="link" href="#x86-sse-packed-arithmetic-instructions">23.12.1.2. x86 SSE packed arithmetic instructions</a></h5>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.5.1.2 "SSE Packed Arithmetic Instructions"</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/addpd.S">userland/arch/x86_64/addpd.S</a>: ADDPS, ADDPD: good first instruction to learn <a href="#simd-assembly">SIMD assembly</a>.</p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="x86-sse-conversion-instructions"><a class="anchor" href="#x86-sse-conversion-instructions"></a><a class="link" href="#x86-sse-conversion-instructions">23.12.1.3. x86 SSE conversion instructions</a></h5>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.5.1.6 "SSE Conversion Instructions"</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="x86-sse2-instructions"><a class="anchor" href="#x86-sse2-instructions"></a><a class="link" href="#x86-sse2-instructions">23.12.2. x86 SSE2 instructions</a></h4>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.6 "SSE2 INSTRUCTIONS"</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/cvttss2si.S">userland/arch/x86_64/cvttss2si.S</a>: CVTTSS2SI: convert 32-bit floating point to 32-bit integer, store the result in a general purpose register. Round towards 0.</p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="x86-paddq-instruction"><a class="anchor" href="#x86-paddq-instruction"></a><a class="link" href="#x86-paddq-instruction">23.12.2.1. x86 PADDQ instruction</a></h5>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/paddq.S">userland/arch/x86_64/paddq.S</a>: PADDQ, PADDL, PADDW, PADDB</p>
</div>
<div class="paragraph">
<p>Good first instruction to learn <a href="#simd-assembly">SIMD assembly</a>.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="x86-fma"><a class="anchor" href="#x86-fma"></a><a class="link" href="#x86-fma">23.12.3. x86 fused multiply add (FMA)</a></h4>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.15 "FUSED-MULTIPLY-ADD (FMA)"</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/vfmadd132pd.S">userland/arch/x86_64/vfmadd132pd.S</a>: VFMADD132PD: "Multiply packed double-precision floating-point values from xmm1 and xmm3/mem, add to xmm2 and put result in xmm1." TODO: but I don&#8217;t understand the manual, experimentally on <a href="#p51">P51</a> Ubuntu 19.04 host the result is stored in XMM2!</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>These instructions were not part of any SSEn set: they actually have a dedicated CPUID flag for it! It appears under <code>/proc/cpuinfo</code> as <code>fma</code>. They were introduced into AVX512F however.</p>
</div>
<div class="paragraph">
<p>They are also unusual for x86 instructions in that they take 3 operands, as you would intuitively expect from the definition of FMA.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="x86-system-instructions"><a class="anchor" href="#x86-system-instructions"></a><a class="link" href="#x86-system-instructions">23.13. x86 system instructions</a></h3>
<div class="paragraph">
<p><a href="#intel-manual-1">Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a> 5.20 "SYSTEM INSTRUCTIONS"</p>
</div>
<div class="sect3">
<h4 id="x86-rdtsc-instruction"><a class="anchor" href="#x86-rdtsc-instruction"></a><a class="link" href="#x86-rdtsc-instruction">23.13.1. x86 RDTSC instruction</a></h4>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/rdtsc.S">userland/arch/x86_64/rdtsc.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/intrinsics/rdtsc.c">userland/arch/x86_64/intrinsics/rdtsc.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Try running the programs multiple times, and watch the value increase, and then try to correlate it with <code>/proc/cpuinfo</code> frequency!</p>
</div>
<div class="literalblock">
<div class="content">
<pre>while true; do sleep 1 &amp;&amp; ./userland/arch/x86_64/rdtsc.out; done</pre>
</div>
</div>
<div class="paragraph">
<p>RDTSC stores its output to EDX:EAX, even in 64-bit mode, top bits are zeroed out.</p>
</div>
<div class="paragraph">
<p>TODO: review this section, make a more controlled userland experiment with <a href="#m5ops">m5ops</a> instrumentation.</p>
</div>
<div class="paragraph">
<p>Let&#8217;s have some fun and try to correlate the <a href="#gem5-m5out-stats-txt-file">gem5 m5out/stats.txt file</a> <code>system.cpu.numCycles</code> cycle count with the <a href="https://en.wikipedia.org/wiki/Time_Stamp_Counter">x86 RDTSC instruction</a> that is supposed to do the same thing:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-userland --static userland/arch/x86_64/inline_asm/rdtsc.S
./run --eval './arch/x86_64/rdtsc.out;m5 exit;' --emulator gem5
./gem5-stat</pre>
</div>
</div>
<div class="paragraph">
<p>RDTSC outputs a cycle count which we compare with gem5&#8217;s <code>gem5-stat</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>3828578153</code>: RDTSC</p>
</li>
<li>
<p><code>3830832635</code>: <code>gem5-stat</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>which gives pretty close results, and serve as a nice sanity check that the cycle counter is coherent.</p>
</div>
<div class="paragraph">
<p>It is also nice to see that RDTSC is a bit smaller than the <code>stats.txt</code> value, since the latter also includes the exec syscall for <code>m5</code>.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://en.wikipedia.org/wiki/Time_Stamp_Counter" class="bare">https://en.wikipedia.org/wiki/Time_Stamp_Counter</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/13772567/how-to-get-the-cpu-cycle-count-in-x86-64-from-c" class="bare">https://stackoverflow.com/questions/13772567/how-to-get-the-cpu-cycle-count-in-x86-64-from-c</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/9887839/clock-cycle-count-wth-gcc/9887979" class="bare">https://stackoverflow.com/questions/9887839/clock-cycle-count-wth-gcc/9887979</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="x86-rdtscp-instruction"><a class="anchor" href="#x86-rdtscp-instruction"></a><a class="link" href="#x86-rdtscp-instruction">23.13.1.1. x86 RDTSCP instruction</a></h5>
<div class="paragraph">
<p>RDTSCP is like RDTSP, but it also stores the CPU ID into ECX: this is convenient because the value of RDTSC depends on which core we are currently on, so you often also want the core ID when you want the RDTSC.</p>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/rdtscp.S">userland/arch/x86_64/rdtscp.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/intrinsics/rdtscp.c">userland/arch/x86_64/intrinsics/rdtscp.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We can observe its operation with the good and old <code>taskset</code>, for example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>taskset -c 0 ./userland/arch/x86_64/rdtscp.out | tail -n 1
taskset -c 1 ./userland/arch/x86_64/rdtscp.out | tail -n 1</pre>
</div>
</div>
<div class="paragraph">
<p>produces:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0x00000000
0x00000001</pre>
</div>
</div>
<div class="paragraph">
<p>There is also the RDPID instruction that reads just the processor ID, but it appears to be very new for QEMU 4.0.0 or <a href="#p51">P51</a>, as it fails with SIGILL on both.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/22310028/is-there-an-x86-instruction-to-tell-which-core-the-instruction-is-being-run-on/56622112#56622112" class="bare">https://stackoverflow.com/questions/22310028/is-there-an-x86-instruction-to-tell-which-core-the-instruction-is-being-run-on/56622112#56622112</a></p>
</div>
</div>
<div class="sect4">
<h5 id="arm-pmccntr-register"><a class="anchor" href="#arm-pmccntr-register"></a><a class="link" href="#arm-pmccntr-register">23.13.1.2. ARM PMCCNTR register</a></h5>
<div class="paragraph">
<p>TODO We didn&#8217;t manage to find a working ARM analogue to <a href="#x86-rdtsc-instruction">x86 RDTSC instruction</a>: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/kernel_modules/pmccntr.c">kernel_modules/pmccntr.c</a> is oopsing, and even it if weren&#8217;t, it likely won&#8217;t give the cycle count since boot since it needs to be activate before it starts counting anything:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/40454157/is-there-an-equivalent-instruction-to-rdtsc-in-arm" class="bare">https://stackoverflow.com/questions/40454157/is-there-an-equivalent-instruction-to-rdtsc-in-arm</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/31620375/arm-cortex-a7-returning-pmccntr-0-in-kernel-mode-and-illegal-instruction-in-u/31649809#31649809" class="bare">https://stackoverflow.com/questions/31620375/arm-cortex-a7-returning-pmccntr-0-in-kernel-mode-and-illegal-instruction-in-u/31649809#31649809</a></p>
</li>
<li>
<p><a href="https://blog.regehr.org/archives/794" class="bare">https://blog.regehr.org/archives/794</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="x86-thread-synchronization-primitives"><a class="anchor" href="#x86-thread-synchronization-primitives"></a><a class="link" href="#x86-thread-synchronization-primitives">23.14. x86 thread synchronization primitives</a></h3>
<div class="sect3">
<h4 id="x86-lock-prefix"><a class="anchor" href="#x86-lock-prefix"></a><a class="link" href="#x86-lock-prefix">23.14.1. x86 LOCK prefix</a></h4>
<div class="paragraph">
<p>Inline assembly example at: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/atomic.cpp">userland/cpp/atomic.cpp</a></p>
</div>
<div class="paragraph">
<p>Ensures that memory modifications are visible across all CPUs, which is fundamental for thread synchronization.</p>
</div>
<div class="paragraph">
<p>Apparently already automatically implied by some of the <a href="#x86-exchange-instructions">x86 exchange instructions</a></p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/8891067/what-does-the-lock-instruction-mean-in-x86-assembly/56803909#56803909" class="bare">https://stackoverflow.com/questions/8891067/what-does-the-lock-instruction-mean-in-x86-assembly/56803909#56803909</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/980999/what-does-multicore-assembly-language-look-like/33651438#33651438" class="bare">https://stackoverflow.com/questions/980999/what-does-multicore-assembly-language-look-like/33651438#33651438</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="x86-assembly-bibliography"><a class="anchor" href="#x86-assembly-bibliography"></a><a class="link" href="#x86-assembly-bibliography">23.15. x86 assembly bibliography</a></h3>
<div class="sect3">
<h4 id="x86-official-bibliography"><a class="anchor" href="#x86-official-bibliography"></a><a class="link" href="#x86-official-bibliography">23.15.1. x86 official bibliography</a></h4>
<div class="sect4">
<h5 id="intel-manual"><a class="anchor" href="#intel-manual"></a><a class="link" href="#intel-manual">23.15.1.1. Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals</a></h5>
<div class="paragraph">
<p>We are using the May 2019 version unless otherwise noted.</p>
</div>
<div class="paragraph">
<p>There are a few download forms at: <a href="https://software.intel.com/en-us/articles/intel-sdm" class="bare">https://software.intel.com/en-us/articles/intel-sdm</a></p>
</div>
<div class="paragraph">
<p>The single PDF one is useless however because it does not have a unified ToC nor inter Volume links, so I just download the 4-part one.</p>
</div>
<div class="paragraph">
<p>The Volumes are well split, so it is usually easy to guess where you should look into.</p>
</div>
<div class="paragraph">
<p>Also I can&#8217;t find older versions on the website easily, so I just web archive everything.</p>
</div>
<div class="sect5">
<h6 id="intel-manual-1"><a class="anchor" href="#intel-manual-1"></a><a class="link" href="#intel-manual-1">23.15.1.1.1. Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 1</a></h6>
<div class="paragraph">
<p>Userland basics: <a href="http://web.archive.org/web/20190606075544/https://software.intel.com/sites/default/files/managed/a4/60/253665-sdm-vol-1.pdf" class="bare">http://web.archive.org/web/20190606075544/https://software.intel.com/sites/default/files/managed/a4/60/253665-sdm-vol-1.pdf</a></p>
</div>
</div>
<div class="sect5">
<h6 id="intel-manual-2"><a class="anchor" href="#intel-manual-2"></a><a class="link" href="#intel-manual-2">23.15.1.1.2. Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 2</a></h6>
<div class="paragraph">
<p>Instruction list: <a href="http://web.archive.org/web/20190606075330/https://software.intel.com/sites/default/files/managed/a4/60/325383-sdm-vol-2abcd.pdf" class="bare">http://web.archive.org/web/20190606075330/https://software.intel.com/sites/default/files/managed/a4/60/325383-sdm-vol-2abcd.pdf</a></p>
</div>
</div>
<div class="sect5">
<h6 id="intel-manual-3"><a class="anchor" href="#intel-manual-3"></a><a class="link" href="#intel-manual-3">23.15.1.1.3. Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 3</a></h6>
<div class="paragraph">
<p>Kernel land: <a href="http://web.archive.org/web/20190606075534/https://software.intel.com/sites/default/files/managed/a4/60/325384-sdm-vol-3abcd.pdf" class="bare">http://web.archive.org/web/20190606075534/https://software.intel.com/sites/default/files/managed/a4/60/325384-sdm-vol-3abcd.pdf</a></p>
</div>
</div>
<div class="sect5">
<h6 id="intel-manual-4"><a class="anchor" href="#intel-manual-4"></a><a class="link" href="#intel-manual-4">23.15.1.1.4. Intel 64 and IA-32 Architectures Software Developer&#8217;s Manuals Volume 4</a></h6>
<div class="paragraph">
<p>Model specific extensions: <a href="http://web.archive.org/web/20190606075325/https://software.intel.com/sites/default/files/managed/22/0d/335592-sdm-vol-4.pdf" class="bare">http://web.archive.org/web/20190606075325/https://software.intel.com/sites/default/files/managed/22/0d/335592-sdm-vol-4.pdf</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="arm-userland-assembly"><a class="anchor" href="#arm-userland-assembly"></a><a class="link" href="#arm-userland-assembly">24. ARM userland assembly</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Arch general getting started at: <a href="#userland-assembly">Section 22, &#8220;Userland assembly&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Instructions here loosely grouped based on that of the <a href="#armarm7">ARMv7 architecture reference manual</a> Chapter A4 "The Instruction Sets".</p>
</div>
<div class="paragraph">
<p>We cover here mostly ARMv7, and then treat aarch64 differentially, since much of the ARMv7 userland is the same in aarch32.</p>
</div>
<div class="sect2">
<h3 id="introduction-to-the-arm-architecture"><a class="anchor" href="#introduction-to-the-arm-architecture"></a><a class="link" href="#introduction-to-the-arm-architecture">24.1. Introduction to the ARM architecture</a></h3>
<div class="paragraph">
<p>The <a href="https://en.wikipedia.org/wiki/ARM_architecture">ARM architecture</a> is has been used on the vast majority of mobile phones in the 2010&#8217;s, and on a large fraction of micro controllers.</p>
</div>
<div class="paragraph">
<p>It competes with <a href="#x86-userland-assembly">x86 userland assembly</a> because its implementations are designed for low power consumption, which is a major requirement of the cell phone market.</p>
</div>
<div class="paragraph">
<p>ARM is generally considered a RISC instruction set, although there are some more complex instructions which would not generally be classified as purely RISC.</p>
</div>
<div class="paragraph">
<p>ARM is developed by the British funded company ARM Holdings: <a href="https://en.wikipedia.org/wiki/Arm_Holdings" class="bare">https://en.wikipedia.org/wiki/Arm_Holdings</a> which originated as a joint venture between Acorn Computers, Apple and VLSI Technology in 1990.</p>
</div>
<div class="paragraph">
<p>ARM Holdings was bought by the Japanese giant SoftBank in 2016.</p>
</div>
<div class="sect3">
<h4 id="armv8-vs-armv7-vs-aarch64-vs-aarch32"><a class="anchor" href="#armv8-vs-armv7-vs-aarch64-vs-aarch32"></a><a class="link" href="#armv8-vs-armv7-vs-aarch64-vs-aarch32">24.1.1. ARMv8 vs ARMv7 vs AArch64 vs AArch32</a></h4>
<div class="paragraph">
<p>ARMv7 is the older architecture described at: <a href="#armarm7">ARMv7 architecture reference manual</a>.</p>
</div>
<div class="paragraph">
<p>ARMv8 is the newer architecture ISA <a href="https://developer.arm.com/docs/den0024/latest/preface">released in 2013</a> and described at: <a href="#armarm8">ARMv8 architecture reference manual</a>. It can be in either of two states:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#aarch32">AArch32</a></p>
</li>
<li>
<p>aarch64</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In the lose terminology of this repository:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>arm</code> means basically AArch32</p>
</li>
<li>
<p><code>aarch64</code> means ARMv8 AArch64</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>ARMv8 has <a href="https://en.wikipedia.org/wiki/ARM_architecture#ARMv8-A">had several updates</a> since its release:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>v8.1: 2014</p>
</li>
<li>
<p>v8.2: 2016</p>
</li>
<li>
<p>v8.3: 2016</p>
</li>
<li>
<p>v8.4: TODO</p>
</li>
<li>
<p>v8.5: 2018</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>They are described at: <a href="#armarm8">ARMv8 architecture reference manual</a> A1.7 "ARMv8 architecture extensions".</p>
</div>
<div class="sect4">
<h5 id="aarch32"><a class="anchor" href="#aarch32"></a><a class="link" href="#aarch32">24.1.1.1. AArch32</a></h5>
<div class="paragraph">
<p>32-bit mode of operation of ARMv8.</p>
</div>
<div class="paragraph">
<p>Userland is highly / fully backwards compatible with ARMv7:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/42972096/armv8-backward-compatibility-with-armv7-snapdragon-820-vs-cortex-a15" class="bare">https://stackoverflow.com/questions/42972096/armv8-backward-compatibility-with-armv7-snapdragon-820-vs-cortex-a15</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/31848185/does-armv8-aarch32-mode-has-backward-compatible-with-armv4-armv5-or-armv6" class="bare">https://stackoverflow.com/questions/31848185/does-armv8-aarch32-mode-has-backward-compatible-with-armv4-armv5-or-armv6</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>For this reason, QEMU and GAS seems to enable both AArch32 and ARMv7 under <code>arm</code> rather than <code>aarch64</code>.</p>
</div>
<div class="paragraph">
<p>There are however some extensions over ARMv7, many of them are functionality that ARMv8 has and that designers decided to backport on AArch32 as well, e.g.:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#armv8-aarch32-vcvta-instruction">ARMv8 AArch32 VCVTA instruction</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="aarch32-vs-aarch64"><a class="anchor" href="#aarch32-vs-aarch64"></a><a class="link" href="#aarch32-vs-aarch64">24.1.1.2. AArch32 vs AArch64</a></h5>
<div class="paragraph">
<p>A great summary of differences can be found at: <a href="https://en.wikipedia.org/wiki/ARM_architecture#AArch64_features" class="bare">https://en.wikipedia.org/wiki/ARM_architecture#AArch64_features</a></p>
</div>
<div class="paragraph">
<p>Some random ones:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>aarch32 has two encodings: Thumb and ARM: <a href="#arm-instruction-encodings">Section 24.1.3, &#8220;ARM instruction encodings&#8221;</a></p>
</li>
<li>
<p>in ARMv8, the stack can be enforced to 16-byte alignment: <a href="#armv8-aarch64-stack-alignment">Section 24.3.2.2.1, &#8220;ARMV8 aarch64 stack alignment&#8221;</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect3">
<h4 id="free-arm-implementations"><a class="anchor" href="#free-arm-implementations"></a><a class="link" href="#free-arm-implementations">24.1.2. Free ARM implementations</a></h4>
<div class="paragraph">
<p>The ARM instruction set is itself protected by patents / copyright / whatever, and you have to pay ARM Holdings a licence to implement it, even if you are creating your own custom Verilog code.</p>
</div>
<div class="paragraph">
<p>ARM has already sued people in the past for implementing ARM ISA: <a href="http://www.eetimes.com/author.asp?section_id=36&amp;doc_id=1287452" class="bare">http://www.eetimes.com/author.asp?section_id=36&amp;doc_id=1287452</a></p>
</div>
<div class="paragraph">
<p><a href="http://semiengineering.com/an-alternative-to-x86-arm-architectures/" class="bare">http://semiengineering.com/an-alternative-to-x86-arm-architectures/</a> mentions that:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>Asanovic joked that the shortest unit of time is not the moment between a traffic light turning green in New York City and the cab driver behind the first vehicle blowing the horn; its someone announcing that they have created an open-source, ARM-compatible core and receiving a “cease and desist” letter from a law firm representing ARM.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>This licensing however does have the following fairness to it: ARM Holdings invents a lot of money in making a great open source software environment for the ARM ISA, so it is only natural that it should be able to get some money from hardware manufacturers for using their ISA.</p>
</div>
<div class="paragraph">
<p>Patents for very old ISAs however have expired, Amber is one implementation of those: <a href="https://en.wikipedia.org/wiki/Amber_%28processor_core%29" class="bare">https://en.wikipedia.org/wiki/Amber_%28processor_core%29</a> TODO does it have any application?</p>
</div>
<div class="paragraph">
<p>Generally, it is mostly large companies that implement the CPUs themselves. For example, the <a href="https://en.wikipedia.org/wiki/Apple_A12">Apple A12 chip</a>, which is used in iPhones, has verilog designs:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>The A12 features an Apple-designed 64-bit ARMv8.3-A six-core CPU, with two high-performance cores running at 2.49 GHz called Vortex and four energy-efficient cores called Tempest.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>ARM designed CPUs however are mostly called <code>Coretx-A&lt;id&gt;</code>: <a href="https://en.wikipedia.org/wiki/List_of_applications_of_ARM_cores" class="bare">https://en.wikipedia.org/wiki/List_of_applications_of_ARM_cores</a> Vortex and Tempest are Apple designed ones.
Bibliography: <a href="https://www.quora.com/Why-is-it-that-you-need-a-license-from-ARM-to-design-an-ARM-CPU-How-are-the-instruction-sets-protected" class="bare">https://www.quora.com/Why-is-it-that-you-need-a-license-from-ARM-to-design-an-ARM-CPU-How-are-the-instruction-sets-protected</a></p>
</div>
</div>
<div class="sect3">
<h4 id="arm-instruction-encodings"><a class="anchor" href="#arm-instruction-encodings"></a><a class="link" href="#arm-instruction-encodings">24.1.3. ARM instruction encodings</a></h4>
<div class="paragraph">
<p>Understanding the basics of instruction encodings is fundamental to help you to remember what instructions do and why some things are possible or not, notably the <a href="#arm-ldr-pseudo-instruction">ARM LDR pseudo-instruction</a> and the <a href="#arm-adr-instruction">ADRP instruction</a>.</p>
</div>
<div class="paragraph">
<p>aarch32 has two "instruction sets", which to look just like encodings.</p>
</div>
<div class="paragraph">
<p>The encodings are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>A32: every instruction is 4 bytes long. Can encode every instruction.</p>
</li>
<li>
<p>T32: most common instructions are 2 bytes long. Many others less common ones are 4 bytes long.</p>
<div class="paragraph">
<p>T stands for "Thumb", which is the original name for the technology, <a href="#armarm8">ARMv8 architecture reference manual</a> A1.3.2 "The ARM instruction sets" says:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>In previous documentation, these instruction sets were called the ARM and Thumb instruction sets</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>See also: <a href="#armarm8">ARMv8 architecture reference manual</a> F2.1.3 "Instruction encodings".</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Within each instruction set, there can be multiple encodings for a given function, and they are noted simply as:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>A1, A2, &#8230;&#8203;: A32 encodings</p>
</li>
<li>
<p>T1, T2, ..m: T32 encodings</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The state bit <code>PSTATE.T</code> determines if the processor is in thumb mode or not. <a href="#armarm8">ARMv8 architecture reference manual</a> says that this bit it can only be read from <a href="#arm-bx-instruction">ARM BX instruction</a></p>
</div>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/22660025/how-can-i-tell-if-i-am-in-arm-mode-or-thumb-mode-in-gdb" class="bare">https://stackoverflow.com/questions/22660025/how-can-i-tell-if-i-am-in-arm-mode-or-thumb-mode-in-gdb</a></p>
</div>
<div class="paragraph">
<p>TODO: details: <a href="https://stackoverflow.com/questions/22660025/how-can-i-tell-if-i-am-in-arm-mode-or-thumb-mode-in-gdb" class="bare">https://stackoverflow.com/questions/22660025/how-can-i-tell-if-i-am-in-arm-mode-or-thumb-mode-in-gdb</a> says it is <code>0x20 &amp; CPSR</code>.</p>
</div>
<div class="paragraph">
<p>This RISC-y mostly fixed instruction length design likely makes processor design easier and allows for certain optimizations, at the cost of slightly more complex assembly, as you can&#8217;t encode 4 / 8 byte addresses in a single instruction. Totally worth it IMHO.</p>
</div>
<div class="paragraph">
<p>This design can be contrasted with x86, which has widely variable instruction length.</p>
</div>
<div class="paragraph">
<p>We can swap between A32 and T32 with the BX and BLX instructions: <a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.kui0100a/armasm_cihfddaf.htm" class="bare">http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.kui0100a/armasm_cihfddaf.htm</a> puts it really nicely:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="ulist">
<ul>
<li>
<p>The BL and BLX instructions copy the address of the next instruction into lr (r14, the link register).</p>
</li>
<li>
<p>The BX and BLX instructions can change the processor state from ARM to Thumb, or from Thumb to ARM.</p>
<div class="ulist">
<ul>
<li>
<p>BLX label always changes the state.</p>
</li>
<li>
<p>BX Rm and BLX Rm derive the target state from bit[0] of Rm:</p>
<div class="ulist">
<ul>
<li>
<p>if bit[0] of Rm is 0, the processor changes to, or remains in, ARM state</p>
</li>
<li>
<p>if bit[0] of Rm is 1, the processor changes to, or remains in, Thumb state.</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>The BXJ instruction changes the processor state to Jazelle.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/28669905/what-is-the-difference-between-the-arm-thumb-and-thumb-2-instruction-encodings" class="bare">https://stackoverflow.com/questions/28669905/what-is-the-difference-between-the-arm-thumb-and-thumb-2-instruction-encodings</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="arm-thumb-encoding"><a class="anchor" href="#arm-thumb-encoding"></a><a class="link" href="#arm-thumb-encoding">24.1.3.1. ARM Thumb encoding</a></h5>
<div class="paragraph">
<p>Thumb examples are available at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/thumb.S">userland/arch/arm/thumb.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/freestanding/linux/hello_thumb.S">userland/arch/arm/freestanding/linux/hello_thumb.S</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>For both of them, we can check that we are in thumb from inside GDB with:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>disassemble</code>, and observe that some of the instructions are only 2 bytes long instead of always 4 as in ARM</p>
</li>
<li>
<p><code>print $cpsr &amp; 0x20</code> which is <code>1</code> on thumb and <code>0</code> otherwise</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>You should contrast those examples with similar non-thumb ones of course.</p>
</div>
<div class="paragraph">
<p>We also note that thumbness of those sources is determined solely by the <code>.thumb_func</code> directive, which implies that there must be some metadata to allow the linker to decide how that code should be called:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>for the freestanding example, this is determined by the first bit of the entry address ELF header as mentioned at: <a href="https://stackoverflow.com/questions/20369440/can-start-be-the-thumb-function/20374451#20374451" class="bare">https://stackoverflow.com/questions/20369440/can-start-be-the-thumb-function/20374451#20374451</a></p>
<div class="paragraph">
<p>We verify that with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain --arch arm readelf -- -h "$(./getvar --arch arm userland_build_dir)/arch/arm/freestanding/linux/hello_thumb.out"</pre>
</div>
</div>
<div class="paragraph">
<p>The Linux kernel must use that to decide put the CPU in thumb mode: that could be done simply with a regular BX.</p>
</div>
</li>
<li>
<p>on the non-freestanding one, the linker uses some ELF metadata to decide that <code>main</code> is thumb and jumps to it appropriately: <a href="https://reverseengineering.stackexchange.com/questions/6080/how-to-detect-thumb-mode-in-arm-disassembly" class="bare">https://reverseengineering.stackexchange.com/questions/6080/how-to-detect-thumb-mode-in-arm-disassembly</a></p>
<div class="paragraph">
<p>TODO details. Does the linker then resolve thumbness with address relocation? Doesn&#8217;t this imply that the compiler cannot generate BL (never changes) or BLX (always changes) across object files, only BX (target state controlled by lower bit)?</p>
</div>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="arm-big-endian-mode"><a class="anchor" href="#arm-big-endian-mode"></a><a class="link" href="#arm-big-endian-mode">24.1.3.2. ARM big endian mode</a></h5>
<div class="paragraph">
<p>ARM can switch between big and little endian mode on the fly!</p>
</div>
<div class="paragraph">
<p>However, everyone only uses little endian, so the big endian ecosystem is not as supported.</p>
</div>
<div class="paragraph">
<p>TODO is there any advantage of using big endian?</p>
</div>
<div class="paragraph">
<p>Here Peter mentions that QEMU does "support" big endian in theory, but that there are no machines for it not sure what that implies: <a href="https://stackoverflow.com/questions/41571643/emulatin-big-endian-arm-system-with-qemu" class="bare">https://stackoverflow.com/questions/41571643/emulatin-big-endian-arm-system-with-qemu</a></p>
</div>
<div class="paragraph">
<p>We can try it out quickly in user mode with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>touch userland/arch/aarch64/freestanding/linux/hello.S
./build-userland --arch aarch64 --ccflags=-mbig-endian userland/arch/aarch64/freestanding/linux/hello.S
./run --arch aarch64 --userland userland/arch/aarch64/freestanding/linux/hello.S</pre>
</div>
</div>
<div class="paragraph">
<p>and it fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Invalid ELF image for this architecture</pre>
</div>
</div>
<div class="paragraph">
<p>From this we can guess that the big endian metadata is actually stored in the <a href="#elf">ELF</a> file, and confirm that with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-toolchain \
--arch aarch64 \
readelf \
-- \
--file-header "$(./getvar --arch aarch64 userland_build_dir)/arch/aarch64/freestanding/linux/hello.out" \
;</pre>
</div>
</div>
<div class="paragraph">
<p>which contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Data: 2's complement, big endian</pre>
</div>
</div>
<div class="paragraph">
<p>instead of the default:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Data: 2's complement, little endian</pre>
</div>
</div>
<div class="paragraph">
<p>TODO does the Linux kernel support running big endian executables? I tried after building the big endian executable:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --arch aarch64
./run --arch aarch64 --eval-after ./arch/aarch64/freestanding/linux/hello.out</pre>
</div>
</div>
<div class="paragraph">
<p>but that failed with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/lkmc/arch/aarch64/freestanding/linux/hello.out: line 1: ELF@x@0@8@: not found
/lkmc/arch/aarch64/freestanding/linux/hello.out: line 2: @@: not found
/lkmc/arch/aarch64/freestanding/linux/hello.out: line 3: syntax error: unexpected ")"</pre>
</div>
</div>
<div class="paragraph">
<p>TODO:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>can you compile the Linux kernel itself as big endian? Looks like yes since there is a <a href="https://github.com/torvalds/linux/blob/v5.1/arch/arm64/Kconfig#L791"><code>config CPU_BIG_ENDIAN</code></a> See also: <a href="https://unix.stackexchange.com/questions/378829/getting-big-endian-linux-build-to-boot-on-arm-with-u-boot" class="bare">https://unix.stackexchange.com/questions/378829/getting-big-endian-linux-build-to-boot-on-arm-with-u-boot</a></p>
</li>
<li>
<p>how can be is the endianess be checked and modified in the CPU?</p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="arm-branch-instructions"><a class="anchor" href="#arm-branch-instructions"></a><a class="link" href="#arm-branch-instructions">24.2. ARM branch instructions</a></h3>
<div class="sect3">
<h4 id="arm-b-instruction"><a class="anchor" href="#arm-b-instruction"></a><a class="link" href="#arm-b-instruction">24.2.1. ARM B instruction</a></h4>
<div class="paragraph">
<p>Unconditional branch.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/b.S">userland/arch/arm/b.S</a></p>
</div>
<div class="paragraph">
<p>The encoding stores PC offsets in 24 bits. The destination must be a multiple of 4, which is easy since all instructions are 4 bytes.</p>
</div>
<div class="paragraph">
<p>This allows for 26 bit long jumps, which is 64 MiB.</p>
</div>
<div class="paragraph">
<p>TODO: what to do if we want to jump longer than that?</p>
</div>
</div>
<div class="sect3">
<h4 id="arm-beq-instruction"><a class="anchor" href="#arm-beq-instruction"></a><a class="link" href="#arm-beq-instruction">24.2.2. ARM BEQ instruction</a></h4>
<div class="paragraph">
<p>Branch if equal based on the status registers.</p>
</div>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/beq.S">userland/arch/arm/beq.S</a>.</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/beq.S">userland/arch/aarch64/beq.S</a>.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The family of instructions includes:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>BEQ: branch if equal</p>
</li>
<li>
<p>BNE: branch if not equal</p>
</li>
<li>
<p>BLE: less or equal</p>
</li>
<li>
<p>BGE: greater or equal</p>
</li>
<li>
<p>BLT: less than</p>
</li>
<li>
<p>BGT: greater than</p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="arm-bl-instruction"><a class="anchor" href="#arm-bl-instruction"></a><a class="link" href="#arm-bl-instruction">24.2.3. ARM BL instruction</a></h4>
<div class="paragraph">
<p>Branch with link, i.e. branch and store the return address on the RL register.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/bl.S">userland/arch/arm/bl.S</a></p>
</div>
<div class="paragraph">
<p>This is the major way to make function calls.</p>
</div>
<div class="paragraph">
<p>The current ARM / Thumb mode is encoded in the least significant bit of lr.</p>
</div>
<div class="sect4">
<h5 id="arm-bx-instruction"><a class="anchor" href="#arm-bx-instruction"></a><a class="link" href="#arm-bx-instruction">24.2.3.1. ARM BX instruction</a></h5>
<div class="paragraph">
<p>See: <a href="#arm-thumb-encoding">Section 24.1.3.1, &#8220;ARM Thumb encoding&#8221;</a></p>
</div>
</div>
<div class="sect4">
<h5 id="armv8-aarch64-ret-instruction"><a class="anchor" href="#armv8-aarch64-ret-instruction"></a><a class="link" href="#armv8-aarch64-ret-instruction">24.2.3.2. ARMv8 aarch64 ret instruction</a></h5>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/ret.S">userland/arch/aarch64/ret.S</a></p>
</div>
<div class="paragraph">
<p>ARMv8 AArch64 only:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>there is no BX in AArch64 since no Thumb to worry about, so it is called just BR</p>
</li>
<li>
<p>the RET instruction was added in addition to BR, with the following differences:</p>
<div class="ulist">
<ul>
<li>
<p>provides a hint that this is a function call return</p>
</li>
<li>
<p>has a default argument X30 if none is given. This is where BL puts the return value.</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>See also: <a href="https://stackoverflow.com/questions/32304646/arm-assembly-branch-to-address-inside-register-or-memory/54145818#54145818" class="bare">https://stackoverflow.com/questions/32304646/arm-assembly-branch-to-address-inside-register-or-memory/54145818#54145818</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="arm-cbz-instruction"><a class="anchor" href="#arm-cbz-instruction"></a><a class="link" href="#arm-cbz-instruction">24.2.4. ARM CBZ instruction</a></h4>
<div class="paragraph">
<p>Compare and branch if zero.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/cbz.S">userland/arch/aarch64/cbz.S</a></p>
</div>
<div class="paragraph">
<p>Only in ARMv8 and ARMv7 Thumb mode, not in armv7 ARM mode.</p>
</div>
<div class="paragraph">
<p>Very handy!</p>
</div>
</div>
<div class="sect3">
<h4 id="arm-conditional-execution"><a class="anchor" href="#arm-conditional-execution"></a><a class="link" href="#arm-conditional-execution">24.2.5. ARM conditional execution</a></h4>
<div class="paragraph">
<p>Weirdly, <a href="#arm-b-instruction">ARM B instruction</a> and family are not the only instructions that can execute conditionally on the flags: the same also applies to most instructions, e.g. ADD.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/cond.S">userland/arch/arm/cond.S</a></p>
</div>
<div class="paragraph">
<p>Just add the usual <code>eq</code>, <code>ne</code>, etc. suffixes just as for B.</p>
</div>
<div class="paragraph">
<p>The list of all extensions is documented at <a href="#armarm7">ARMv7 architecture reference manual</a> "A8.3 Conditional execution".</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="arm-load-and-store-instructions"><a class="anchor" href="#arm-load-and-store-instructions"></a><a class="link" href="#arm-load-and-store-instructions">24.3. ARM load and store instructions</a></h3>
<div class="paragraph">
<p>In ARM, there are only two instruction families that do memory access:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#arm-ldr-instruction">ARM LDR instruction</a> to load from memory to registers</p>
</li>
<li>
<p><a href="#arm-str-instruction">ARM STR instruction</a> to store from registers to memory</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Everything else works on register and immediates.</p>
</div>
<div class="paragraph">
<p>This is part of the RISC-y beauty of the ARM instruction set, unlike x86 in which several operations can read from memory, and helps to predict how to optimize for a given CPU pipeline.</p>
</div>
<div class="paragraph">
<p>This kind of architecture is called a <a href="https://en.wikipedia.org/wiki/Load/store_architecture">Load/store architecture</a>.</p>
</div>
<div class="sect3">
<h4 id="arm-ldr-instruction"><a class="anchor" href="#arm-ldr-instruction"></a><a class="link" href="#arm-ldr-instruction">24.3.1. ARM LDR instruction</a></h4>
<div class="sect4">
<h5 id="arm-ldr-pseudo-instruction"><a class="anchor" href="#arm-ldr-pseudo-instruction"></a><a class="link" href="#arm-ldr-pseudo-instruction">24.3.1.1. ARM LDR pseudo-instruction</a></h5>
<div class="paragraph">
<p>LDR can be either a regular instruction that loads stuff into memory, or also a pseudo-instruction (assembler magic): <a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0041c/Babbfdih.html" class="bare">http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0041c/Babbfdih.html</a></p>
</div>
<div class="paragraph">
<p>The pseudo instruction version is when an equal sign appears on one of the operators.</p>
</div>
<div class="paragraph">
<p>The LDR pseudo instruction can automatically create hidden variables in a place called the "literal pool", and load them from memory with PC relative loads.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/ldr_pseudo.S">userland/arch/arm/ldr_pseudo.S</a></p>
</div>
<div class="paragraph">
<p>This is done basically because all instructions are 32-bit wide, and there is not enough space to encode 32-bit addresses in them.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/37840754/what-does-an-equals-sign-on-the-right-side-of-a-ldr-instruction-in-arm-mean" class="bare">https://stackoverflow.com/questions/37840754/what-does-an-equals-sign-on-the-right-side-of-a-ldr-instruction-in-arm-mean</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/17214962/what-is-the-difference-between-label-equals-sign-and-label-brackets-in-ar" class="bare">https://stackoverflow.com/questions/17214962/what-is-the-difference-between-label-equals-sign-and-label-brackets-in-ar</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/14046686/why-use-ldr-over-mov-or-vice-versa-in-arm-assembly" class="bare">https://stackoverflow.com/questions/14046686/why-use-ldr-over-mov-or-vice-versa-in-arm-assembly</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="arm-addressing-modes"><a class="anchor" href="#arm-addressing-modes"></a><a class="link" href="#arm-addressing-modes">24.3.1.2. ARM addressing modes</a></h5>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/address_modes.S">userland/arch/arm/address_modes.S</a></p>
</div>
<div class="paragraph">
<p>Load and store instructions can update the source register with the following modes:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>offset: add an offset, don&#8217;t change the address register. Notation:</p>
<div class="literalblock">
<div class="content">
<pre>ldr r1, [r0, 4]</pre>
</div>
</div>
</li>
<li>
<p>pre-indexed: change the address register, and then use it modified. Notation:</p>
<div class="literalblock">
<div class="content">
<pre>ldr r1, [r0, 4]!</pre>
</div>
</div>
</li>
<li>
<p>post-indexed: use the address register unmodified, and then modify it. Notation:</p>
<div class="literalblock">
<div class="content">
<pre>ldr r1, [r0], 4</pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>The offset itself can come from the following sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>immediate</p>
</li>
<li>
<p>register</p>
</li>
<li>
<p>scaled register: left shift the register and use that as an offset</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The indexed modes are convenient to loop over arrays.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="#armarm7">ARMv7 architecture reference manual</a>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>A4.6.5 "Addressing modes"</p>
</li>
<li>
<p>A8.5 "Memory accesses"</p>
</li>
</ul>
</div>
<div class="paragraph">
<p><a href="#armarm8">ARMv8 architecture reference manual</a>: C1.3.3 "Load/Store addressing modes"</p>
</div>
<div class="sect5">
<h6 id="arm-loop-over-array"><a class="anchor" href="#arm-loop-over-array"></a><a class="link" href="#arm-loop-over-array">24.3.1.2.1. ARM loop over array</a></h6>
<div class="paragraph">
<p>As an application of the post-indexed addressing mode, let&#8217;s increment an array.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/inc_array.S">userland/arch/arm/inc_array.S</a></p>
</div>
</div>
</div>
<div class="sect4">
<h5 id="arm-ldrh-and-ldrb-instructions"><a class="anchor" href="#arm-ldrh-and-ldrb-instructions"></a><a class="link" href="#arm-ldrh-and-ldrb-instructions">24.3.1.3. ARM LDRH and LDRB instructions</a></h5>
<div class="paragraph">
<p>There are LDR variants that load less than full 4 bytes:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/ldrb.S">userland/arch/arm/ldrb.S</a>: load byte</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/ldrh.S">userland/arch/arm/ldrh.S</a>: load half word</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>These also have signed and unsigned versions to either zero or one extend the result:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/ldrsw.S">userland/arch/aarch64/ldrsw.S</a>: load byte and sign extend</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect3">
<h4 id="arm-str-instruction"><a class="anchor" href="#arm-str-instruction"></a><a class="link" href="#arm-str-instruction">24.3.2. ARM STR instruction</a></h4>
<div class="paragraph">
<p>Store from memory into registers.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/str.S">userland/arch/arm/str.S</a></p>
</div>
<div class="paragraph">
<p>Basically everything that applies to <a href="#arm-ldr-instruction">ARM LDR instruction</a> also applies here so we won&#8217;t go into much detail.</p>
</div>
<div class="sect4">
<h5 id="armv8-aarch64-str-instruction"><a class="anchor" href="#armv8-aarch64-str-instruction"></a><a class="link" href="#armv8-aarch64-str-instruction">24.3.2.1. ARMv8 aarch64 STR instruction</a></h5>
<div class="paragraph">
<p>PC-relative STR is not possible in aarch64.</p>
</div>
<div class="paragraph">
<p>For LDR it works <a href="#arm-ldr-instruction">as in aarch32</a>.</p>
</div>
<div class="paragraph">
<p>As a result, it is not possible to load from the literal pool for STR.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/str.S">userland/arch/aarch64/str.S</a></p>
</div>
<div class="paragraph">
<p>This can be seen from <a href="#armarm8">ARMv8 architecture reference manual</a> C3.2.1 "Load/Store register": LDR simply has on extra PC encoding that STR does not.</p>
</div>
</div>
<div class="sect4">
<h5 id="armv8-aarch64-ldp-and-stp-instructions"><a class="anchor" href="#armv8-aarch64-ldp-and-stp-instructions"></a><a class="link" href="#armv8-aarch64-ldp-and-stp-instructions">24.3.2.2. ARMv8 aarch64 LDP and STP instructions</a></h5>
<div class="paragraph">
<p>Push a pair of registers to the stack.</p>
</div>
<div class="paragraph">
<p>TODO minimal example. Currently used in <code>LKMC_PROLOGUE</code> at <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/aarch64.h">lkmc/aarch64.h</a> since it is the main way to restore register state.</p>
</div>
<div class="sect5">
<h6 id="armv8-aarch64-stack-alignment"><a class="anchor" href="#armv8-aarch64-stack-alignment"></a><a class="link" href="#armv8-aarch64-stack-alignment">24.3.2.2.1. ARMV8 aarch64 stack alignment</a></h6>
<div class="paragraph">
<p>In ARMv8, the stack can be enforced to 16-byte alignment.</p>
</div>
<div class="paragraph">
<p>This is why the main way to push things to stack is with 8-byte pair pushes with the <a href="#armv8-aarch64-ldp-and-stp-instructions">ARMv8 aarch64 LDP and STP instructions</a>.</p>
</div>
<div class="paragraph">
<p><a href="#armarm8-db">ARMv8 architecture reference manual db</a> C1.3.3 "Load/Store addressing modes" says:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>When stack alignment checking is enabled by system software and the base register is the SP, the current stack pointer must be initially quadword aligned, that is aligned to 16 bytes. Misalignment generates a Stack Alignment fault. The offset does not have to be a multiple of 16 bytes unless the specific Load/Store instruction requires this. SP cannot be used as a register offset.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p><a href="#armarm8-db">ARMv8 architecture reference manual db</a> C3.2 "Loads and stores" says:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>The additional control bits SCTLR_ELx.SA and SCTLR_EL1.SA0 control whether the stack pointer must be quadword aligned when used as a base register. See SP alignment checking on page D1-2164. Using a misaligned stack pointer generates an SP alignment fault exception.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p><a href="#armarm8-db">ARMv8 architecture reference manual db</a> D1.8.2 "SP alignment checking" is then the main section.</p>
</div>
<div class="paragraph">
<p>TODO: what does the ABI say on this? Why don&#8217;t I observe faults on QEMU as mentioned at: <a href="https://stackoverflow.com/questions/212466/what-is-a-bus-error/31877230#31877230" class="bare">https://stackoverflow.com/questions/212466/what-is-a-bus-error/31877230#31877230</a></p>
</div>
<div class="paragraph">
<p>See also:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/38535738/does-aarch64-support-unaligned-access" class="bare">https://stackoverflow.com/questions/38535738/does-aarch64-support-unaligned-access</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="arm-ldmia-instruction"><a class="anchor" href="#arm-ldmia-instruction"></a><a class="link" href="#arm-ldmia-instruction">24.3.3. ARM LDMIA instruction</a></h4>
<div class="paragraph">
<p>Pop values form stack into the register and optionally update the address register.</p>
</div>
<div class="paragraph">
<p>STMDB is the push version.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/ldmia.S">userland/arch/arm/ldmia.S</a></p>
</div>
<div class="paragraph">
<p>The mnemonics stand for:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>STMDB: STore Multiple Decrement Before</p>
</li>
<li>
<p>LDMIA: LoaD Multiple Increment After</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/push.S">userland/arch/arm/push.S</a></p>
</div>
<div class="paragraph">
<p>PUSH and POP are just mnemonics STDMDB and LDMIA using the stack pointer SP as address register:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>stmdb sp!, reglist
ldmia sp!, reglist</pre>
</div>
</div>
<div class="paragraph">
<p>The <code>!</code> indicates that we want to update the register.</p>
</div>
<div class="paragraph">
<p>The registers are encoded as single bits inside the instruction: each bit represents one register.</p>
</div>
<div class="paragraph">
<p>As a consequence, the push order is fixed no matter how you write the assembly instruction: there is just not enough space to encode ordering.</p>
</div>
<div class="paragraph">
<p>AArch64 loses those instructions, likely because it was not possible anymore to encode all registers: <a href="https://stackoverflow.com/questions/27941220/push-lr-and-pop-lr-in-arm-arch64" class="bare">https://stackoverflow.com/questions/27941220/push-lr-and-pop-lr-in-arm-arch64</a> and replaces them with the <a href="#armv8-aarch64-ldp-and-stp-instructions">ARMv8 aarch64 LDP and STP instructions</a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="arm-data-processing-instructions"><a class="anchor" href="#arm-data-processing-instructions"></a><a class="link" href="#arm-data-processing-instructions">24.4. ARM data processing instructions</a></h3>
<div class="paragraph">
<p>Arithmetic:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/mul.S">userland/arch/arm/mul.S</a>: multiply</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/sub.S">userland/arch/arm/sub.S</a>: subtract</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/rbit.S">userland/arch/arm/rbit.S</a>: reverse bit order</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/rev.S">userland/arch/arm/rev.S</a>: reverse byte order</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/tst.S">userland/arch/arm/tst.S</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="arm-cset-instruction"><a class="anchor" href="#arm-cset-instruction"></a><a class="link" href="#arm-cset-instruction">24.4.1. ARM CSET instruction</a></h4>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/cset.S">userland/arch/aarch64/cset.S</a></p>
</div>
<div class="paragraph">
<p>Set a register conditionally depending on the condition flags:</p>
</div>
<div class="paragraph">
<p>ARMv8-only, likely because in ARMv8 you can&#8217;t have conditional suffixes for every instruction.</p>
</div>
</div>
<div class="sect3">
<h4 id="arm-bitwise-instructions"><a class="anchor" href="#arm-bitwise-instructions"></a><a class="link" href="#arm-bitwise-instructions">24.4.2. ARM bitwise instructions</a></h4>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/and.S">userland/arch/arm/and.S</a> AND</p>
</li>
<li>
<p>EOR: exclusive OR</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/orr.S">userland/arch/arm/orr.S</a>: OR</p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/clz.S">userland/arch/arm/clz.S</a>: count leading zeroes</p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="arm-bic-instruction"><a class="anchor" href="#arm-bic-instruction"></a><a class="link" href="#arm-bic-instruction">24.4.2.1. ARM BIC instruction</a></h5>
<div class="paragraph">
<p>Bitwise Bit Clear: clear some bits.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>dest = left &amp; ~right</pre>
</div>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/bic.S">userland/arch/arm/bic.S</a></p>
</div>
</div>
<div class="sect4">
<h5 id="arm-ubfm-instruction"><a class="anchor" href="#arm-ubfm-instruction"></a><a class="link" href="#arm-ubfm-instruction">24.4.2.2. ARM UBFM instruction</a></h5>
<div class="paragraph">
<p>Unsigned Bitfield Move.</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>copies any number of low-order bits from a source register into the same number of adjacent bits at any position in the destination register, with zeros in the upper and lower bits.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/ubfm.S">userland/arch/aarch64/ubfm.S</a></p>
</div>
<div class="paragraph">
<p>TODO: explain full behaviour. Very complicated. Has several simpler to understand aliases.</p>
</div>
<div class="sect5">
<h6 id="arm-ubfx-instruction"><a class="anchor" href="#arm-ubfx-instruction"></a><a class="link" href="#arm-ubfx-instruction">24.4.2.2.1. ARM UBFX instruction</a></h6>
<div class="paragraph">
<p>Alias for:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>UBFM &lt;Wd&gt;, &lt;Wn&gt;, #&lt;lsb&gt;, #(&lt;lsb&gt;+&lt;width&gt;-1)</pre>
</div>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/ubfx.S">userland/arch/aarch64/ubfx.S</a></p>
</div>
<div class="paragraph">
<p>The operation:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>UBFX dest, src, lsb, width</pre>
</div>
</div>
<div class="paragraph">
<p>does:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>dest = (src &amp; ((1 &lt;&lt; width) - 1)) &gt;&gt; lsb;</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/8366625/arm-bit-field-extract" class="bare">https://stackoverflow.com/questions/8366625/arm-bit-field-extract</a></p>
</div>
</div>
</div>
<div class="sect4">
<h5 id="arm-bfm-instruction"><a class="anchor" href="#arm-bfm-instruction"></a><a class="link" href="#arm-bfm-instruction">24.4.2.3. ARM BFM instruction</a></h5>
<div class="paragraph">
<p>TODO: explain. Similar to <a href="#arm-ubfm-instruction">UBFM</a> but leave untouched bits unmodified.</p>
</div>
<div class="sect5">
<h6 id="arm-bfi-instruction"><a class="anchor" href="#arm-bfi-instruction"></a><a class="link" href="#arm-bfi-instruction">24.4.2.3.1. ARM BFI instruction</a></h6>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/bfi.S">userland/arch/arm/bfi.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/bfi.S">userland/arch/aarch64/bfi.S</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Move the lower bits of source register into any position in the destination:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>ARMv8: an alias for <a href="#arm-bfm-instruction">ARM BFM instruction</a></p>
</li>
<li>
<p>ARMv7: a real instruction</p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="arm-mov-instruction"><a class="anchor" href="#arm-mov-instruction"></a><a class="link" href="#arm-mov-instruction">24.4.3. ARM MOV instruction</a></h4>
<div class="paragraph">
<p>Move an immediate to a register, or a register to another register.</p>
</div>
<div class="paragraph">
<p>Cannot load from or to memory, since only the LDR and STR instruction families can do that in ARM as mentioned at: <a href="#arm-load-and-store-instructions">Section 24.3, &#8220;ARM load and store instructions&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/mov.S">userland/arch/arm/mov.S</a></p>
</div>
<div class="paragraph">
<p>Since every instruction <a href="#arm-instruction-encodings">has a fixed 4 byte size</a>, there is not enough space to encode arbitrary 32-bit immediates in a single instruction, since some of the bits are needed to actually encode the instruction itself.</p>
</div>
<div class="paragraph">
<p>The solutions to this problem are mentioned at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/38689886/loading-32-bit-values-to-a-register-in-arm-assembly" class="bare">https://stackoverflow.com/questions/38689886/loading-32-bit-values-to-a-register-in-arm-assembly</a></p>
</li>
<li>
<p><a href="https://community.arm.com/processors/b/blog/posts/how-to-load-constants-in-assembly-for-arm-architecture" class="bare">https://community.arm.com/processors/b/blog/posts/how-to-load-constants-in-assembly-for-arm-architecture</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Summary of solutions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#arm-movw-and-movt-instructions">ARM movw and movt instructions</a></p>
</li>
<li>
<p>place it in memory. But then how to load the address, which is also a 32-bit value?</p>
<div class="ulist">
<ul>
<li>
<p>use pc-relative addressing if the memory is close enough</p>
</li>
<li>
<p>use <a href="#arm-bitwise-instructions">ORR</a> encodable shifted immediates</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>The blog article summarizes nicely which immediates can be encoded and the design rationale:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>An Operand 2 immediate must obey the following rule to fit in the instruction: an 8-bit value rotated right by an even number of bits between 0 and 30 (inclusive). This allows for constants such as 0xFF (0xFF rotated right by 0), 0xFF00 (0xFF rotated right by 24) or 0xF000000F (0xFF rotated right by 4).</p>
</div>
<div class="paragraph">
<p>In software - especially in languages like C - constants tend to be small. When they are not small they tend to be bit masks. Operand 2 immediates provide a reasonable compromise between constant coverage and encoding space; most common constants can be encoded directly.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>Assemblers however support magic memory allocations which may hide what is truly going on: <a href="https://stackoverflow.com/questions/14046686/why-use-ldr-over-mov-or-vice-versa-in-arm-assembly" class="bare">https://stackoverflow.com/questions/14046686/why-use-ldr-over-mov-or-vice-versa-in-arm-assembly</a> Always ask your friendly disassembly for a good confirmation.</p>
</div>
<div class="sect4">
<h5 id="arm-movw-and-movt-instructions"><a class="anchor" href="#arm-movw-and-movt-instructions"></a><a class="link" href="#arm-movw-and-movt-instructions">24.4.3.1. ARM movw and movt instructions</a></h5>
<div class="paragraph">
<p>Set the higher or lower 16 bits of a register to an immediate in one go.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/movw.S">userland/arch/arm/movw.S</a></p>
</div>
<div class="paragraph">
<p>The armv8 version analogue is <a href="#armv8-aarch64-movk-instruction">ARMv8 aarch64 movk instruction</a>.</p>
</div>
</div>
<div class="sect4">
<h5 id="armv8-aarch64-movk-instruction"><a class="anchor" href="#armv8-aarch64-movk-instruction"></a><a class="link" href="#armv8-aarch64-movk-instruction">24.4.3.2. ARMv8 aarch64 movk instruction</a></h5>
<div class="paragraph">
<p>Fill a 64 bit register with 4 16-bit instructions one at a time.</p>
</div>
<div class="paragraph">
<p>Similar to <a href="#arm-movw-and-movt-instructions">ARM movw and movt instructions</a> in v7.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/movk.S">userland/arch/aarch64/movk.S</a></p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/27938768/moving-a-32-bit-constant-in-arm-arch64-register" class="bare">https://stackoverflow.com/questions/27938768/moving-a-32-bit-constant-in-arm-arch64-register</a></p>
</div>
</div>
<div class="sect4">
<h5 id="armv8-aarch64-movn-instruction"><a class="anchor" href="#armv8-aarch64-movn-instruction"></a><a class="link" href="#armv8-aarch64-movn-instruction">24.4.3.3. ARMv8 aarch64 movn instruction</a></h5>
<div class="paragraph">
<p>Set 16-bits negated and the rest to <code>1</code>.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/movn.S">userland/arch/aarch64/movn.S</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="arm-data-processing-instruction-suffixes"><a class="anchor" href="#arm-data-processing-instruction-suffixes"></a><a class="link" href="#arm-data-processing-instruction-suffixes">24.4.4. ARM data processing instruction suffixes</a></h4>
<div class="sect4">
<h5 id="arm-shift-suffixes"><a class="anchor" href="#arm-shift-suffixes"></a><a class="link" href="#arm-shift-suffixes">24.4.4.1. ARM shift suffixes</a></h5>
<div class="paragraph">
<p>Most data processing instructions can also optionally shift the second register operand.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/shift.S">userland/arch/arm/shift.S</a></p>
</div>
<div class="paragraph">
<p>The shift types are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>LSR and LFL: Logical Shift Right / Left. Insert zeroes.</p>
</li>
<li>
<p>ROR: Rotate Right / Left. Wrap bits around.</p>
</li>
<li>
<p>ASR: Arithmetic Shift Right. Keep sign.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Documented at: <a href="#armarm7">ARMv7 architecture reference manual</a> "A4.4.1 Standard data-processing instructions"</p>
</div>
</div>
<div class="sect4">
<h5 id="arm-s-suffix"><a class="anchor" href="#arm-s-suffix"></a><a class="link" href="#arm-s-suffix">24.4.4.2. ARM S suffix</a></h5>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/s_suffix.S">userland/arch/arm/s_suffix.S</a></p>
</div>
<div class="paragraph">
<p>The <code>S</code> suffix, present on most <a href="#arm-data-processing-instructions">ARM data processing instructions</a>, makes the instruction also set the Status register flags that control conditional jumps.</p>
</div>
<div class="paragraph">
<p>If the result of the operation is <code>0</code>, then it triggers BEQ, since comparison is a subtraction, with success on 0.</p>
</div>
<div class="paragraph">
<p>CMP sets the flags by default of course.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="arm-adr-instruction"><a class="anchor" href="#arm-adr-instruction"></a><a class="link" href="#arm-adr-instruction">24.4.5. ARM ADR instruction</a></h4>
<div class="paragraph">
<p>Similar rationale to the <a href="#arm-ldr-pseudo-instruction">ARM LDR pseudo-instruction</a>, allowing to easily store a PC-relative reachable address into a register in one go, to overcome the 4-byte fixed instruction size.</p>
</div>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/adr.S">userland/arch/arm/adr.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/adr.S">userland/arch/aarch64/adr.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/adrp.S">userland/arch/aarch64/adrp.S</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>More details: <a href="https://stackoverflow.com/questions/41906688/what-are-the-semantics-of-adrp-and-adrl-instructions-in-arm-assembly/54042899#54042899" class="bare">https://stackoverflow.com/questions/41906688/what-are-the-semantics-of-adrp-and-adrl-instructions-in-arm-assembly/54042899#54042899</a></p>
</div>
<div class="sect4">
<h5 id="arm-adrl-instruction"><a class="anchor" href="#arm-adrl-instruction"></a><a class="link" href="#arm-adrl-instruction">24.4.5.1. ARM ADRL instruction</a></h5>
<div class="paragraph">
<p>See: <a href="#arm-adr-instruction">Section 24.4.5, &#8220;ARM ADR instruction&#8221;</a>.</p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="arm-miscellaneous-instructions"><a class="anchor" href="#arm-miscellaneous-instructions"></a><a class="link" href="#arm-miscellaneous-instructions">24.5. ARM miscellaneous instructions</a></h3>
<div class="sect3">
<h4 id="arm-nop-instruction"><a class="anchor" href="#arm-nop-instruction"></a><a class="link" href="#arm-nop-instruction">24.5.1. ARM NOP instruction</a></h4>
<div class="paragraph">
<p>Parent section: <a href="#nop-instructions">Section 22.10, &#8220;NOP instructions&#8221;</a></p>
</div>
<div class="paragraph">
<p>There are a few different ways to encode NOP, notably MOV a register into itself, and a dedicated miscellaneous instruction.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/nop.S">userland/arch/arm/nop.S</a></p>
</div>
<div class="paragraph">
<p>Try disassembling the executable to see what the assembler is emitting:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>gdb-multiarch -batch -ex 'arch arm' -ex "file v7/nop.out" -ex "disassemble/rs asm_main_after_prologue"</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/1875491/nop-for-iphone-binaries" class="bare">https://stackoverflow.com/questions/1875491/nop-for-iphone-binaries</a></p>
</div>
</div>
<div class="sect3">
<h4 id="arm-udf-instruction"><a class="anchor" href="#arm-udf-instruction"></a><a class="link" href="#arm-udf-instruction">24.5.2. ARM UDF instruction</a></h4>
<div class="paragraph">
<p>Guaranteed undefined! Therefore raise illegal instruction signal. Used by GCC <code>__builtin_trap</code> apparently: <a href="https://stackoverflow.com/questions/16081618/programmatically-cause-undefined-instruction-exception" class="bare">https://stackoverflow.com/questions/16081618/programmatically-cause-undefined-instruction-exception</a></p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/udf.S">userland/arch/arm/udf.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/udf.S">userland/arch/aarch64/udf.S</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Why GNU GAS 2.29 does not have a mnemonic for it in A64 because it is very recent: shows in <a href="#armarm8-db">ARMv8 architecture reference manual db</a> but not <code>ca</code>.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="arm-simd"><a class="anchor" href="#arm-simd"></a><a class="link" href="#arm-simd">24.6. ARM SIMD</a></h3>
<div class="paragraph">
<p>Parent section: <a href="#simd-assembly">Section 22.3, &#8220;SIMD assembly&#8221;</a></p>
</div>
<div class="sect3">
<h4 id="arm-vfp"><a class="anchor" href="#arm-vfp"></a><a class="link" href="#arm-vfp">24.6.1. ARM VFP</a></h4>
<div class="paragraph">
<p>The name for the ARMv7 and AArch32 floating point and SIMD instructions / registers.</p>
</div>
<div class="paragraph">
<p>Vector Floating Point extension.</p>
</div>
<div class="paragraph">
<p>TODO I think it was optional in ARMv7, find quote.</p>
</div>
<div class="paragraph">
<p>VFP has several revisions, named as VFPv1, VFPv2, etc. TODO: announcement dates.</p>
</div>
<div class="paragraph">
<p>As mentioned at: <a href="https://stackoverflow.com/questions/37790029/what-is-difference-between-arm64-and-armhf/48954012#48954012" class="bare">https://stackoverflow.com/questions/37790029/what-is-difference-between-arm64-and-armhf/48954012#48954012</a> the Linux kernel shows those capabilities in <code>/proc/cpuinfo</code> with flags such as <code>vfp</code>, <code>vfpv3</code> and others, see:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/torvalds/linux/blob/v4.18/arch/arm/kernel/setup.c#L1199" class="bare">https://github.com/torvalds/linux/blob/v4.18/arch/arm/kernel/setup.c#L1199</a></p>
</li>
<li>
<p><a href="https://github.com/torvalds/linux/blob/v4.18/arch/arm64/kernel/cpuinfo.c#L95" class="bare">https://github.com/torvalds/linux/blob/v4.18/arch/arm64/kernel/cpuinfo.c#L95</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>When a certain version of VFP is present on a CPU, the compiler prefix typically contains the <code>hf</code> characters which stands for Hard Float, e.g.: <code>arm-linux-gnueabihf</code>. This means that the compiler will emit VFP instructions instead of just using software implementations.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#armarm7">ARMv7 architecture reference manual</a> Appendix D6 "Common VFP Subarchitecture Specification". It is not part of the ISA, but just an extension. TODO: that spec does not seem to have the instructions documented, and instruction like VMOV just live with the main instructions. Is VMOV part of VFP?</p>
</li>
<li>
<p><a href="https://mindplusplus.wordpress.com/2013/06/25/arm-vfp-vector-programming-part-1-introduction/" class="bare">https://mindplusplus.wordpress.com/2013/06/25/arm-vfp-vector-programming-part-1-introduction/</a></p>
</li>
<li>
<p><a href="https://en.wikipedia.org/wiki/ARM_architecture#Floating-point_(VFP" class="bare">https://en.wikipedia.org/wiki/ARM_architecture#Floating-point_(VFP</a>)</p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="arm-vfp-registers"><a class="anchor" href="#arm-vfp-registers"></a><a class="link" href="#arm-vfp-registers">24.6.1.1. ARM VFP registers</a></h5>
<div class="paragraph">
<p>TODO example</p>
</div>
<div class="paragraph">
<p><a href="#armarm8">ARMv8 architecture reference manual</a> E1.3.1 "The SIMD and floating-point register file" Figure E1-1 "SIMD and floating-point register file, AArch32 operation":</p>
</div>
<div class="literalblock">
<div class="content">
<pre>+-----+-----+-----+
| S0 | | |
+-----+ D0 + |
| S1 | | |
+-----+-----+ Q0 |
| S2 | | |
+-----+ D1 + |
| S3 | | |
+-----+-----+-----+
| S4 | | |
+-----+ D2 + |
| S5 | | |
+-----+-----+ Q1 |
| S6 | | |
+-----+ D3 + |
| S7 | | |
+-----+-----+-----+</pre>
</div>
</div>
<div class="paragraph">
<p>Note how Sn is weirdly packed inside Dn, and Dn weirdly packed inside Qn, likely for historical reasons.</p>
</div>
<div class="paragraph">
<p>And you can&#8217;t access the higher bytes at D16 or greater with Sn.</p>
</div>
</div>
<div class="sect4">
<h5 id="arm-vadd-instruction"><a class="anchor" href="#arm-vadd-instruction"></a><a class="link" href="#arm-vadd-instruction">24.6.1.2. ARM VADD instruction</a></h5>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/vadd_scalar.S">userland/arch/arm/vadd_scalar.S</a>: see also: <a href="#floating-point-assembly">Section 22.2, &#8220;Floating point assembly&#8221;</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/vadd_vector.S">userland/arch/arm/vadd_vector.S</a>: see also: <a href="#simd-assembly">Section 22.3, &#8220;SIMD assembly&#8221;</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="arm-vcvt-instruction"><a class="anchor" href="#arm-vcvt-instruction"></a><a class="link" href="#arm-vcvt-instruction">24.6.1.3. ARM VCVT instruction</a></h5>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/vcvt.S">userland/arch/arm/vcvt.S</a></p>
</div>
<div class="paragraph">
<p>Convert between integers and floating point.</p>
</div>
<div class="paragraph">
<p><a href="#armarm7">ARMv7 architecture reference manual</a> on rounding:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>The floating-point to fixed-point operation uses the Round towards Zero rounding mode. The fixed-point to floating-point operation uses the Round to Nearest rounding mode.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>Notice how the opcode takes two types.</p>
</div>
<div class="paragraph">
<p>E.g., in our 32-bit float to 32-bit unsigned example we use:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vld1.32.f32</pre>
</div>
</div>
<div class="sect5">
<h6 id="arm-vcvtr-instruction"><a class="anchor" href="#arm-vcvtr-instruction"></a><a class="link" href="#arm-vcvtr-instruction">24.6.1.3.1. ARM VCVTR instruction</a></h6>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/vcvtr.S">userland/arch/arm/vcvtr.S</a></p>
</div>
<div class="paragraph">
<p>Like <a href="#arm-vcvt-instruction">ARM VCVT instruction</a>, but the rounding mode is selected by the FPSCR.RMode field.</p>
</div>
<div class="paragraph">
<p>Selecting rounding mode explicitly per instruction was apparently not possible in ARMv7, but was made possible in <a href="#aarch32">AArch32</a> e.g. with <a href="#armv8-aarch32-vcvta-instruction">ARMv8 AArch32 VCVTA instruction</a>.</p>
</div>
<div class="paragraph">
<p>Rounding mode selection is exposed in the ANSI C standard through <a href="https://en.cppreference.com/w/c/numeric/fenv/feround"><code>fesetround</code></a>.</p>
</div>
<div class="paragraph">
<p>TODO: is the initial rounding mode specified by the ELF standard? Could not find a reference.</p>
</div>
</div>
<div class="sect5">
<h6 id="armv8-aarch32-vcvta-instruction"><a class="anchor" href="#armv8-aarch32-vcvta-instruction"></a><a class="link" href="#armv8-aarch32-vcvta-instruction">24.6.1.3.2. ARMv8 AArch32 VCVTA instruction</a></h6>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/vcvt.S">userland/arch/arm/vcvt.S</a></p>
</div>
<div class="paragraph">
<p>Added in ARMv8 <a href="#aarch32">AArch32</a> only, not present in ARMv7.</p>
</div>
<div class="paragraph">
<p>In ARMv7, to use a non-round-to-zero rounding mode, you had to set the rounding mode with FPSCR and use the R version of the instruction e.g. <a href="#arm-vcvtr-instruction">ARM VCVTR instruction</a>.</p>
</div>
<div class="paragraph">
<p>Now in AArch32 it is possible to do it explicitly per-instruction.</p>
</div>
<div class="paragraph">
<p>Also there was no ties to away mode in ARMv7. This mode does not exist in C99 either.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="armv8-advanced-simd-and-floating-point-support"><a class="anchor" href="#armv8-advanced-simd-and-floating-point-support"></a><a class="link" href="#armv8-advanced-simd-and-floating-point-support">24.6.2. ARMv8 Advanced SIMD and floating-point support</a></h4>
<div class="paragraph">
<p>The <a href="#armarm8">ARMv8 architecture reference manual</a> specifies floating point and SIMD support in the main architecture at A1.5 "Advanced SIMD and floating-point support".</p>
</div>
<div class="paragraph">
<p>The feature is often refered to simply as "SIMD&amp;FP" throughout the manual.</p>
</div>
<div class="paragraph">
<p>The Linux kernel shows <code>/proc/cpuinfo</code> compatibility as <code>neon</code>, which is yet another intermediate name that came up at some point, see: <a href="#arm-neon">Section 24.6.2.2, &#8220;ARM NEON&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Vs <a href="#arm-vfp">ARM VFP</a>: <a href="https://stackoverflow.com/questions/4097034/arm-cortex-a8-whats-the-difference-between-vfp-and-neon" class="bare">https://stackoverflow.com/questions/4097034/arm-cortex-a8-whats-the-difference-between-vfp-and-neon</a></p>
</div>
<div class="sect4">
<h5 id="armv8-floating-point-availability"><a class="anchor" href="#armv8-floating-point-availability"></a><a class="link" href="#armv8-floating-point-availability">24.6.2.1. ARMv8 floating point availability</a></h5>
<div class="paragraph">
<p>Support is semi-mandatory. <a href="#armarm8">ARMv8 architecture reference manual</a> A1.5 "Advanced SIMD and floating-point support":</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>ARMv8 can support the following levels of support for Advanced SIMD and floating-point instructions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Full SIMD and floating-point support without exception trapping.</p>
</li>
<li>
<p>Full SIMD and floating-point support with exception trapping.</p>
</li>
<li>
<p>No floating-point or SIMD support. This option is licensed only for implementations targeting specialized markets.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Note: All systems that support standard operating systems with rich application environments provide hardware
support for Advanced SIMD and floating-point. It is a requirement of the ARM Procedure Call Standard for
AArch64, see Procedure Call Standard for the ARM 64-bit Architecture.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>Therefore it is in theory optional, but highly available.</p>
</div>
<div class="paragraph">
<p>This is unlike ARMv7, where floating point is completely optional through <a href="#arm-vfp">ARM VFP</a>.</p>
</div>
</div>
<div class="sect4">
<h5 id="arm-neon"><a class="anchor" href="#arm-neon"></a><a class="link" href="#arm-neon">24.6.2.2. ARM NEON</a></h5>
<div class="paragraph">
<p>Just an informal name for the "Advanced SIMD instructions"? Very confusing.</p>
</div>
<div class="paragraph">
<p><a href="#armarm8">ARMv8 architecture reference manual</a> F2.9 "Additional information about Advanced SIMD and floating-point instructions" says:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>The Advanced SIMD architecture, its associated implementations, and supporting software, are commonly referred to as NEON technology.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p><a href="https://developer.arm.com/technologies/neon" class="bare">https://developer.arm.com/technologies/neon</a> mentions that is is present on both ARMv7 and ARMv8:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>NEON technology was introduced to the Armv7-A and Armv7-R profiles. It is also now an extension to the Armv8-A and Armv8-R profiles.</p>
</div>
</blockquote>
</div>
</div>
</div>
<div class="sect3">
<h4 id="armv8-aarch64-floating-point-registers"><a class="anchor" href="#armv8-aarch64-floating-point-registers"></a><a class="link" href="#armv8-aarch64-floating-point-registers">24.6.3. ARMv8 AArch64 floating point registers</a></h4>
<div class="paragraph">
<p>TODO example.</p>
</div>
<div class="paragraph">
<p><a href="#armarm8">ARMv8 architecture reference manual</a> B1.2.1 "Registers in AArch64 state" describes the registers:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>32 SIMD&amp;FP registers, V0 to V31. Each register can be accessed as:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>A 128-bit register named Q0 to Q31.</p>
</li>
<li>
<p>A 64-bit register named D0 to D31.</p>
</li>
<li>
<p>A 32-bit register named S0 to S31.</p>
</li>
<li>
<p>A 16-bit register named H0 to H31.</p>
</li>
<li>
<p>An 8-bit register named B0 to B31.</p>
</li>
</ul>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>Notice how Sn is very different between v7 <a href="#arm-vfp-registers">ARM VFP registers</a> and v8! In v7 it goes across Dn, and in v8 inside each Dn:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>128 64 32 16 8 0
+---------------------------+-------------------+-------+---+---+
| Vn |
+---------------------------------------------------------------+
| Qn |
+---------------------------+-----------------------------------+
| Dn |
+-----------------------------------+
| Sn |
+---------------+
| Hn |
+-------+
|Bn |
+---+</pre>
</div>
</div>
<div class="sect4">
<h5 id="armv8-aarch64-add-vector-instruction"><a class="anchor" href="#armv8-aarch64-add-vector-instruction"></a><a class="link" href="#armv8-aarch64-add-vector-instruction">24.6.3.1. ARMv8 aarch64 add vector instruction</a></h5>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/add_vector.S">userland/arch/aarch64/add_vector.S</a></p>
</div>
<div class="paragraph">
<p>Good first instruction to learn SIMD: <a href="#simd-assembly">SIMD assembly</a>.</p>
</div>
</div>
<div class="sect4">
<h5 id="armv8-aarch64-fadd-instruction"><a class="anchor" href="#armv8-aarch64-fadd-instruction"></a><a class="link" href="#armv8-aarch64-fadd-instruction">24.6.3.2. ARMv8 aarch64 FADD instruction</a></h5>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/fadd_vector.S">userland/arch/aarch64/fadd_vector.S</a>: see also: <a href="#simd-assembly">Section 22.3, &#8220;SIMD assembly&#8221;</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/fadd_scalar.S">userland/arch/aarch64/fadd_scalar.S</a>: see also: <a href="#floating-point-assembly">Section 22.2, &#8220;Floating point assembly&#8221;</a></p>
</li>
</ul>
</div>
<div class="sect5">
<h6 id="arm-fadd-vs-vadd"><a class="anchor" href="#arm-fadd-vs-vadd"></a><a class="link" href="#arm-fadd-vs-vadd">24.6.3.2.1. ARM FADD vs VADD</a></h6>
<div class="paragraph">
<p>It is very confusing, but FADDS and FADDD in Aarch32 are <a href="#gnu-gas-assembler-arm-unified-syntax">pre-UAL</a> for <code>vadd.f32</code> and <code>vadd.f64</code> which we use in this tutorial, see: <a href="#arm-vadd-instruction">Section 24.6.1.2, &#8220;ARM VADD instruction&#8221;</a></p>
</div>
<div class="paragraph">
<p>The same goes for most ARMv7 mnemonics: <code>f*</code> is old, and <code>v*</code> is the newer better syntax.</p>
</div>
<div class="paragraph">
<p>But then, in ARMv8, they decided to use <a href="#armv8-aarch64-fadd-instruction">ARMv8 aarch64 FADD instruction</a> as the main floating point add name, and get rid of VADD!</p>
</div>
<div class="paragraph">
<p>Also keep in mind that fused multiply add is FMADD.</p>
</div>
<div class="paragraph">
<p>Examples at: <a href="#simd-assembly">Section 22.3, &#8220;SIMD assembly&#8221;</a></p>
</div>
</div>
</div>
<div class="sect4">
<h5 id="armv8-aarch64-ld2-instruction"><a class="anchor" href="#armv8-aarch64-ld2-instruction"></a><a class="link" href="#armv8-aarch64-ld2-instruction">24.6.3.3. ARMv8 aarch64 ld2 instruction</a></h5>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/ld2.S">userland/arch/aarch64/ld2.S</a></p>
</div>
<div class="paragraph">
<p>We can load multiple vectors interleaved from memory in one single instruction!</p>
</div>
<div class="paragraph">
<p>This is why the <code>ldN</code> instructions take an argument list denoted by <code>{}</code> for the registers, much like armv7 <a href="#arm-ldmia-instruction">ARM LDMIA instruction</a>.</p>
</div>
<div class="paragraph">
<p>There are analogous LD3 and LD4 instruction.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="arm-simd-bibliography"><a class="anchor" href="#arm-simd-bibliography"></a><a class="link" href="#arm-simd-bibliography">24.6.4. ARM SIMD bibliography</a></h4>
<div class="ulist">
<ul>
<li>
<p>GNU GAS tests under <a href="https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=tree;f=gas/testsuite/gas/aarch64;hb=00f223631fa9803b783515a2f667f86997e2cdbe"><code>gas/testsuite/gas/aarch64</code></a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/2851421/is-there-a-good-reference-for-arm-neon-intrinsics" class="bare">https://stackoverflow.com/questions/2851421/is-there-a-good-reference-for-arm-neon-intrinsics</a></p>
</li>
<li>
<p>assembly optimized libraries:</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/projectNe10/Ne10" class="bare">https://github.com/projectNe10/Ne10</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="arm-sve"><a class="anchor" href="#arm-sve"></a><a class="link" href="#arm-sve">24.6.5. ARM SVE</a></h4>
<div class="paragraph">
<p>Scalable Vector Extension.</p>
</div>
<div class="paragraph">
<p>Example: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/sve.S">userland/arch/aarch64/sve.S</a></p>
</div>
<div class="paragraph">
<p>To understand it, the first thing you have to look at is the execution example at Fig 1 of: <a href="https://alastairreid.github.io/papers/sve-ieee-micro-2017.pdf" class="bare">https://alastairreid.github.io/papers/sve-ieee-micro-2017.pdf</a></p>
</div>
<div class="paragraph">
<p>aarch64 only, newer than <a href="#arm-neon">ARM NEON</a>.</p>
</div>
<div class="paragraph">
<p>It is called Scalable because it does not specify the vector width! Therefore we don&#8217;t have to worry about new vector width instructions every few years! Hurray!</p>
</div>
<div class="paragraph">
<p>The instructions then allow:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>incrementing loop index by the vector length without explicitly hardcoding it</p>
</li>
<li>
<p>when the last loop is reached, extra bytes that are not multiples of the vector length get automatically masked out by the predicate register, and have no effect</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Added to QEMU in 3.0.0 and gem5 in 2019 Q3.</p>
</div>
<div class="paragraph">
<p>TODO announcement date. Possibly 2017: <a href="https://alastairreid.github.io/papers/sve-ieee-micro-2017.pdf" class="bare">https://alastairreid.github.io/papers/sve-ieee-micro-2017.pdf</a> There is also a 2016 mention: <a href="https://community.arm.com/tools/hpc/b/hpc/posts/technology-update-the-scalable-vector-extension-sve-for-the-armv8-a-architecture" class="bare">https://community.arm.com/tools/hpc/b/hpc/posts/technology-update-the-scalable-vector-extension-sve-for-the-armv8-a-architecture</a></p>
</div>
<div class="paragraph">
<p>The Linux kernel shows <code>/proc/cpuinfo</code> compatibility as <code>sve</code>.</p>
</div>
<div class="paragraph">
<p>Official spec: <a href="https://developer.arm.com/docs/100891/latest/sve-overview/introducing-sve" class="bare">https://developer.arm.com/docs/100891/latest/sve-overview/introducing-sve</a></p>
</div>
<div class="paragraph">
<p>SVE support is indicated by <code>ID_AA64PFR0_EL1.SVE</code> which is dumped from <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/dump_regs.c">baremetal/arch/aarch64/dump_regs.c</a>.</p>
</div>
<div class="paragraph">
<p>Using SVE normally requires setting the CPACR_EL1.FPEN and ZEN bits, which as as of lkmc 29fd625f3fda79f5e0ee6cac43517ba74340d513 + 1 we also enable in our <a href="#baremetal-bootloaders">Baremetal bootloaders</a>, see also: <a href="#aarch64-baremetal-neon-setup">aarch64 baremetal NEON setup</a>.</p>
</div>
<div class="sect4">
<h5 id="sve-bibliography"><a class="anchor" href="#sve-bibliography"></a><a class="link" href="#sve-bibliography">24.6.5.1. SVE bibliography</a></h5>
<div class="ulist">
<ul>
<li>
<p><a href="https://www.rico.cat/files/ICS18-gem5-sve-tutorial.pdf" class="bare">https://www.rico.cat/files/ICS18-gem5-sve-tutorial.pdf</a> step by step of a complete code execution examples, the best initial tutorial so far</p>
</li>
<li>
<p><a href="https://static.docs.arm.com/dui0965/c/DUI0965C_scalable_vector_extension_guide.pdf" class="bare">https://static.docs.arm.com/dui0965/c/DUI0965C_scalable_vector_extension_guide.pdf</a></p>
</li>
<li>
<p><a href="https://developer.arm.com/products/software-development-tools/hpc/documentation/writing-inline-sve-assembly" class="bare">https://developer.arm.com/products/software-development-tools/hpc/documentation/writing-inline-sve-assembly</a> quick inlining guide</p>
</li>
</ul>
</div>
<div class="sect5">
<h6 id="sve-spec"><a class="anchor" href="#sve-spec"></a><a class="link" href="#sve-spec">24.6.5.1.1. SVE spec</a></h6>
<div class="paragraph">
<p><a href="#armarm8">ARMv8 architecture reference manual</a> A1.7 "ARMv8 architecture extensions" says:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>SVE is an optional extension to ARMv8.2. That is, SVE requires the implementation of ARMv8.2.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>A1.7.8 "The Scalable Vector Extension (SVE)": then says that only changes to the existing registers are described in that manual, and that you should look instead at the "ARM Architecture Reference Manual Supplement, The Scalable Vector Extension (SVE), for ARMv8-A."</p>
</div>
<div class="paragraph">
<p>We then download the zip from: <a href="https://developer.arm.com/docs/ddi0584/latest/arm-architecture-reference-manual-supplement-the-scalable-vector-extension-sve-for-armv8-a" class="bare">https://developer.arm.com/docs/ddi0584/latest/arm-architecture-reference-manual-supplement-the-scalable-vector-extension-sve-for-armv8-a</a> and it contains the PDF: <code>DDI0584A_d_SVE_supp_armv8A.pdf</code> which we use here.</p>
</div>
<div class="paragraph">
<p>That document then describes the SVE instructions and registers.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="armv8-architecture-extensions"><a class="anchor" href="#armv8-architecture-extensions"></a><a class="link" href="#armv8-architecture-extensions">24.7. ARMv8 architecture extensions</a></h3>
<div class="sect3">
<h4 id="armv8-1-architecture-extension"><a class="anchor" href="#armv8-1-architecture-extension"></a><a class="link" href="#armv8-1-architecture-extension">24.7.1. ARMv8.1 architecture extension</a></h4>
<div class="paragraph">
<p><a href="#armarm8-db">ARMv8 architecture reference manual db</a> A1.7.3 "The ARMv8.1 architecture extension"</p>
</div>
<div class="sect4">
<h5 id="arm-lse"><a class="anchor" href="#arm-lse"></a><a class="link" href="#arm-lse">24.7.1.1. ARM Large System Extensions (LSE)</a></h5>
<div class="paragraph">
<p><a href="#armarm8-db">ARMv8 architecture reference manual db</a> "ARMv8.1-LSE, ARMv8.1 Large System Extensions"</p>
</div>
<div class="ulist">
<ul>
<li>
<p>LDADD: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/cpp/atomic.cpp">userland/cpp/atomic.cpp</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/21535058/arm64-ldxr-stxr-vs-ldaxr-stlxr" class="bare">https://stackoverflow.com/questions/21535058/arm64-ldxr-stxr-vs-ldaxr-stlxr</a></p>
</li>
<li>
<p><a href="https://preshing.com/20120710/memory-barriers-are-like-source-control-operations/" class="bare">https://preshing.com/20120710/memory-barriers-are-like-source-control-operations/</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="arm-assembly-bibliography"><a class="anchor" href="#arm-assembly-bibliography"></a><a class="link" href="#arm-assembly-bibliography">24.8. ARM assembly bibliography</a></h3>
<div class="sect3">
<h4 id="arm-non-official-bibliography"><a class="anchor" href="#arm-non-official-bibliography"></a><a class="link" href="#arm-non-official-bibliography">24.8.1. ARM non-official bibliography</a></h4>
<div class="paragraph">
<p>Good getting started tutorials:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="http://www.davespace.co.uk/arm/introduction-to-arm/" class="bare">http://www.davespace.co.uk/arm/introduction-to-arm/</a></p>
</li>
<li>
<p><a href="https://azeria-labs.com/writing-arm-assembly-part-1/" class="bare">https://azeria-labs.com/writing-arm-assembly-part-1/</a></p>
</li>
<li>
<p><a href="https://thinkingeek.com/arm-assembler-raspberry-pi/" class="bare">https://thinkingeek.com/arm-assembler-raspberry-pi/</a></p>
</li>
<li>
<p><a href="http://bob.cs.sonoma.edu/IntroCompOrg-RPi/app-make.html" class="bare">http://bob.cs.sonoma.edu/IntroCompOrg-RPi/app-make.html</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="arm-official-bibliography"><a class="anchor" href="#arm-official-bibliography"></a><a class="link" href="#arm-official-bibliography">24.8.2. ARM official bibliography</a></h4>
<div class="paragraph">
<p>The official manuals were stored in <a href="http://infocenter.arm.com" class="bare">http://infocenter.arm.com</a> but as of 2017 they started to slowly move to <a href="https://developer.arm.com" class="bare">https://developer.arm.com</a>.</p>
</div>
<div class="paragraph">
<p>Each revision of a document has a "ARM DDI" unique document identifier.</p>
</div>
<div class="paragraph">
<p>The "ARM Architecture Reference Manuals" are the official canonical ISA documentation document. In this repository, we always reference the following revisions:</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://www.quora.com/Where-can-I-find-the-official-documentation-of-ARM-instruction-set-architectures-ISAs" class="bare">https://www.quora.com/Where-can-I-find-the-official-documentation-of-ARM-instruction-set-architectures-ISAs</a></p>
</div>
<div class="sect4">
<h5 id="armarm7"><a class="anchor" href="#armarm7"></a><a class="link" href="#armarm7">24.8.2.1. ARMv7 architecture reference manual</a></h5>
<div class="paragraph">
<p><a href="https://developer.arm.com/products/architecture/a-profile/docs/ddi0406/latest/arm-architecture-reference-manual-armv7-a-and-armv7-r-edition" class="bare">https://developer.arm.com/products/architecture/a-profile/docs/ddi0406/latest/arm-architecture-reference-manual-armv7-a-and-armv7-r-edition</a></p>
</div>
<div class="paragraph">
<p>The official comprehensive ARMv7 reference.</p>
</div>
<div class="paragraph">
<p>We use by default: DDI 0406C.d: <a href="https://static.docs.arm.com/ddi0406/cd/DDI0406C_d_armv7ar_arm.pdf" class="bare">https://static.docs.arm.com/ddi0406/cd/DDI0406C_d_armv7ar_arm.pdf</a></p>
</div>
</div>
<div class="sect4">
<h5 id="armarm8"><a class="anchor" href="#armarm8"></a><a class="link" href="#armarm8">24.8.2.2. ARMv8 architecture reference manual</a></h5>
<div class="paragraph">
<p><a href="https://static.docs.arm.com/ddi0487/ca/DDI0487C_a_armv8_arm.pdf" class="bare">https://static.docs.arm.com/ddi0487/ca/DDI0487C_a_armv8_arm.pdf</a></p>
</div>
<div class="paragraph">
<p>Latest version: <a href="https://developer.arm.com/docs/ddi0487/latest/arm-architecture-reference-manual-armv8-for-armv8-a-architecture-profile" class="bare">https://developer.arm.com/docs/ddi0487/latest/arm-architecture-reference-manual-armv8-for-armv8-a-architecture-profile</a></p>
</div>
<div class="paragraph">
<p>Versions are determined by two letteres in lexicographical order, e.g.:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>a</p>
</li>
<li>
<p>af</p>
</li>
<li>
<p>aj</p>
</li>
<li>
<p>aj</p>
</li>
<li>
<p>b</p>
</li>
<li>
<p>ba</p>
</li>
<li>
<p>bb</p>
</li>
<li>
<p>ca</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The link: <a href="https://static.docs.arm.com/ddi0487/ca/DDI0487C_a_armv8_arm.pdf" class="bare">https://static.docs.arm.com/ddi0487/ca/DDI0487C_a_armv8_arm.pdf</a> is the <code>ca</code> version for example.</p>
</div>
<div class="paragraph">
<p>The official comprehensive ARMv8 reference.</p>
</div>
<div class="paragraph">
<p>ISA quick references can be found in some places:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://web.archive.org/web/20161009122630/http://infocenter.arm.com/help/topic/com.arm.doc.qrc0001m/QRC0001_UAL.pdf" class="bare">https://web.archive.org/web/20161009122630/http://infocenter.arm.com/help/topic/com.arm.doc.qrc0001m/QRC0001_UAL.pdf</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="armarm8-db"><a class="anchor" href="#armarm8-db"></a><a class="link" href="#armarm8-db">24.8.2.3. ARMv8 architecture reference manual db</a></h5>
<div class="paragraph">
<p><a href="https://static.docs.arm.com/ddi0487/db/DDI0487D_b_armv8_arm.pdf" class="bare">https://static.docs.arm.com/ddi0487/db/DDI0487D_b_armv8_arm.pdf</a></p>
</div>
</div>
<div class="sect4">
<h5 id="armv8-programmers-guide"><a class="anchor" href="#armv8-programmers-guide"></a><a class="link" href="#armv8-programmers-guide">24.8.2.4. Programmer&#8217;s Guide for ARMv8-A</a></h5>
<div class="paragraph">
<p><a href="https://static.docs.arm.com/den0024/a/DEN0024A_v8_architecture_PG.pdf" class="bare">https://static.docs.arm.com/den0024/a/DEN0024A_v8_architecture_PG.pdf</a></p>
</div>
<div class="paragraph">
<p>A more terse human readable introduction to the ARM architecture than the reference manuals.</p>
</div>
<div class="paragraph">
<p>Does not have as many assembly code examples as you&#8217;d hope however&#8230;&#8203;</p>
</div>
<div class="paragraph">
<p>Latest version at: <a href="https://developer.arm.com/docs/den0024/latest/preface" class="bare">https://developer.arm.com/docs/den0024/latest/preface</a></p>
</div>
</div>
<div class="sect4">
<h5 id="arm-a64-instruction-set-architecture-future-architecture-technologies-in-the-a-architecture-profile-documentation"><a class="anchor" href="#arm-a64-instruction-set-architecture-future-architecture-technologies-in-the-a-architecture-profile-documentation"></a><a class="link" href="#arm-a64-instruction-set-architecture-future-architecture-technologies-in-the-a-architecture-profile-documentation">24.8.2.5. Arm A64 Instruction Set Architecture: Future Architecture Technologies in the A architecture profile Documentation</a></h5>
<div class="paragraph">
<p><a href="https://developer.arm.com/docs/ddi0602/b" class="bare">https://developer.arm.com/docs/ddi0602/b</a></p>
</div>
<div class="paragraph">
<p>This page contains the documentation of architecture features that were publicly announced but haven&#8217;t been merged into the main spec yet.</p>
</div>
</div>
<div class="sect4">
<h5 id="arm-processor-documentation"><a class="anchor" href="#arm-processor-documentation"></a><a class="link" href="#arm-processor-documentation">24.8.2.6. ARM processor documentation</a></h5>
<div class="paragraph">
<p>ARM also releases documentation specific to each given processor.</p>
</div>
<div class="paragraph">
<p>This adds extra details to the more portable <a href="#armarm8">ARMv8 architecture reference manual</a> ISA documentation.</p>
</div>
<div class="sect5">
<h6 id="arm-cortex15-trm"><a class="anchor" href="#arm-cortex15-trm"></a><a class="link" href="#arm-cortex15-trm">24.8.2.6.1. ARM Cortex-A15 MPCore Processor Technical Reference Manual r4p0</a></h6>
<div class="paragraph">
<p><a href="http://infocenter.arm.com/help/topic/com.arm.doc.ddi0438i/DDI0438I_cortex_a15_r4p0_trm.pdf" class="bare">http://infocenter.arm.com/help/topic/com.arm.doc.ddi0438i/DDI0438I_cortex_a15_r4p0_trm.pdf</a></p>
</div>
<div class="paragraph">
<p>2013.</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="elf"><a class="anchor" href="#elf"></a><a class="link" href="#elf">25. ELF</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/Executable_and_Linkable_Format" class="bare">https://en.wikipedia.org/wiki/Executable_and_Linkable_Format</a></p>
</div>
<div class="paragraph">
<p>This is the main format for executables, object files (<code>.o</code>) and shared libraries (<code>.so</code>) in Linux.</p>
</div>
<div class="paragraph">
<p>An introduction to the format can be found at: <a href="https://cirosantilli.com/elf-hello-world" class="bare">https://cirosantilli.com/elf-hello-world</a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="ieee-754"><a class="anchor" href="#ieee-754"></a><a class="link" href="#ieee-754">26. IEEE 754</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/IEEE_754" class="bare">https://en.wikipedia.org/wiki/IEEE_754</a></p>
</div>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/x86_64/ieee754.S">userland/arch/x86_64/ieee754.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/float.h">lkmc/float.h</a>. Bibliography: <a href="https://stackoverflow.com/questions/52905648/how-to-use-hexadecimal-floating-point-literals-in-gnu-gas/56818851#56818851" class="bare">https://stackoverflow.com/questions/52905648/how-to-use-hexadecimal-floating-point-literals-in-gnu-gas/56818851#56818851</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/8341395/what-is-a-subnormal-floating-point-number/53203428#53203428" class="bare">https://stackoverflow.com/questions/8341395/what-is-a-subnormal-floating-point-number/53203428#53203428</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/18118408/what-is-difference-between-quiet-nan-and-signaling-nan/55648118#55648118" class="bare">https://stackoverflow.com/questions/18118408/what-is-difference-between-quiet-nan-and-signaling-nan/55648118#55648118</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/2618059/in-java-what-does-nan-mean/55673220#55673220" class="bare">https://stackoverflow.com/questions/2618059/in-java-what-does-nan-mean/55673220#55673220</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="baremetal"><a class="anchor" href="#baremetal"></a><a class="link" href="#baremetal">27. Baremetal</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Getting started at: <a href="#baremetal-setup">Section 1.7, &#8220;Baremetal setup&#8221;</a></p>
</div>
<div class="sect2">
<h3 id="baremetal-gdb-step-debug"><a class="anchor" href="#baremetal-gdb-step-debug"></a><a class="link" href="#baremetal-gdb-step-debug">27.1. Baremetal GDB step debug</a></h3>
<div class="paragraph">
<p>GDB step debug works on baremetal exactly as it does on the Linux kernel, which is described at: <a href="#gdb">Section 2, &#8220;GDB step debug&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Except that is is even cooler here since we can easily control and understand every single instruction that is being run!</p>
</div>
<div class="paragraph">
<p>For example, on the first shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --baremetal userland/c/hello.c --gdb-wait</pre>
</div>
</div>
<div class="paragraph">
<p>then on the second shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --arch arm --baremetal userland/c/hello.c -- main</pre>
</div>
</div>
<div class="paragraph">
<p>Or if you are a <a href="#tmux">tmux pro</a>, do everything in one go with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --baremetal userland/c/hello.c --gdb</pre>
</div>
</div>
<div class="paragraph">
<p>Alternatively, to start from the very first executed instruction of our tiny <a href="#baremetal-bootloaders">Baremetal bootloaders</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch arm \
--baremetal userland/c/hello.c \
--gdb-wait \
--tmux-args=--no-continue \
;</pre>
</div>
</div>
<div class="paragraph">
<p>analogously to what is done for <a href="#freestanding-programs">Freestanding programs</a>.</p>
</div>
<div class="paragraph">
<p>Now you can just <code>stepi</code> to when jumping into main to go to the C code in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/hello.c">userland/c/hello.c</a>.</p>
</div>
<div class="paragraph">
<p>This is specially interesting for the executables that don&#8217;t use the bootloader from under <code>baremetal/arch/&lt;arch&gt;/no_bootloader/*.S</code>, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch arm \
--baremetal baremetal/arch/arm/no_bootloader/semihost_exit.S \
--gdb-wait \
--tmux-args=--no-continue \
;</pre>
</div>
</div>
<div class="paragraph">
<p>The cool thing about those examples is that you start at the very first instruction of your program, which gives more control.</p>
</div>
<div class="paragraph">
<p>Examples without bootloader are somewhat analogous to user mode <a href="#freestanding-programs">Freestanding programs</a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="baremetal-bootloaders"><a class="anchor" href="#baremetal-bootloaders"></a><a class="link" href="#baremetal-bootloaders">27.2. Baremetal bootloaders</a></h3>
<div class="paragraph">
<p>As can be seen from <a href="#baremetal-gdb-step-debug">Baremetal GDB step debug</a>, all examples under <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/">baremetal/</a>, with the exception of <code>baremetal/arch/&lt;arch&gt;/no_bootloader</code>, start from our tiny bootloaders:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/lib/arm.S">baremetal/lib/arm.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/lib/aarch64.S">baremetal/lib/aarch64.S</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Out simplistic bootloaders basically setup up just enough system state to allow calling:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>C functions such as <code>exit</code> from the assembly examples</p>
</li>
<li>
<p>the <code>main</code> of C examples itself</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The most important things that we setup in the bootloaders are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the stack pointer</p>
</li>
<li>
<p>NEON: <a href="#aarch64-baremetal-neon-setup">Section 27.9.2, &#8220;aarch64 baremetal NEON setup&#8221;</a></p>
</li>
<li>
<p>TODO: we don&#8217;t do this currently but maybe we should setup BSS</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The C functions that become available as a result are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Newlib functions implemented at <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/lib/syscalls.c">baremetal/lib/syscalls.c</a></p>
</li>
<li>
<p><code>lkmc_</code> non-Newlib functions implemented at <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc.c">lkmc.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>It is not possible to call those C functions from the examples that don&#8217;t use a bootloader.</p>
</div>
<div class="paragraph">
<p>For this reason, we tend to create examples with bootloaders, as it is easier to write them portably.</p>
</div>
</div>
<div class="sect2">
<h3 id="semihosting"><a class="anchor" href="#semihosting"></a><a class="link" href="#semihosting">27.3. Semihosting</a></h3>
<div class="paragraph">
<p>Semihosting is a publicly documented interface specified by ARM Holdings that allows us to do some magic operations very useful in development.</p>
</div>
<div class="paragraph">
<p>Semihosting is implemented both on some real devices and on simulators such as QEMU and <a href="#gem5-semihosting">gem5 semihosting</a>.</p>
</div>
<div class="paragraph">
<p>It is documented at: <a href="https://developer.arm.com/docs/100863/latest/introduction" class="bare">https://developer.arm.com/docs/100863/latest/introduction</a></p>
</div>
<div class="paragraph">
<p>For example, all the following code make QEMU exit:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --baremetal baremetal/arch/arm/semihost_exit.S
./run --arch arm --baremetal baremetal/arch/arm/no_bootloader/semihost_exit.S
./run --arch aarch64 --baremetal baremetal/arch/aarch64/semihost_exit.S
./run --arch aarch64 --baremetal baremetal/arch/aarch64/no_bootloader/semihost_exit.S</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/arm/semihost_exit.S">baremetal/arch/arm/semihost_exit.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/arm/no_bootloader/semihost_exit.S">baremetal/arch/arm/no_bootloader/semihost_exit.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/semihost_exit.S">baremetal/arch/aarch64/semihost_exit.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/no_bootloader/semihost_exit.S">baremetal/arch/aarch64/no_bootloader/semihost_exit.S</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>That <code>arm</code> program program contains the code:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mov r0, #0x18
ldr r1, =#0x20026
svc 0x00123456</pre>
</div>
</div>
<div class="paragraph">
<p>and we can see from the docs that <code>0x18</code> stands for the <code>SYS_EXIT</code> command.</p>
</div>
<div class="paragraph">
<p>This is also how we implement the <code>exit(0)</code> system call in C for QEMU, which is used for example at <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/exit0.c">userland/c/exit0.c</a> through the Newlib via the <code>_exit</code> function at <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/lib/syscalls.c">baremetal/lib/syscalls.c</a>.</p>
</div>
<div class="paragraph">
<p>Other magic operations we can do with semihosting besides exiting the on the host include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>read and write to host stdin and stdout</p>
</li>
<li>
<p>read and write to host files</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Alternatives exist for some semihosting operations, e.g.:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>UART IO for host stdin and stdout in both emulators and real hardware</p>
</li>
<li>
<p><a href="#m5ops">m5ops</a> for <a href="#gem5">gem5</a>, e.g. <code>m5 exit</code> makes the emulator quit</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The big advantage of semihosting is that it is standardized across all ARM boards, and therefore allows you to make a single image that does those magic operations instead of having to compile multiple images with different magic addresses.</p>
</div>
<div class="paragraph">
<p>The downside of semihosting is that it is ARM specific. TODO is it an open standard that other vendors can implement?</p>
</div>
<div class="paragraph">
<p>In QEMU, we enable semihosting with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>-semihosting</pre>
</div>
</div>
<div class="paragraph">
<p>Newlib 9c84bfd47922aad4881f80243320422b621c95dc already has a semi-hosting implementation at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>newlib/libc/sys/arm/syscalls.c</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: how to use it? Possible through crosstool-NG? In the worst case we could just copy it.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/31990487/how-to-cleanly-exit-qemu-after-executing-bare-metal-program-without-user-interve/40957928#40957928" class="bare">https://stackoverflow.com/questions/31990487/how-to-cleanly-exit-qemu-after-executing-bare-metal-program-without-user-interve/40957928#40957928</a></p>
</li>
<li>
<p><a href="https://balau82.wordpress.com/2010/11/04/qemu-arm-semihosting/" class="bare">https://balau82.wordpress.com/2010/11/04/qemu-arm-semihosting/</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="gem5-semihosting"><a class="anchor" href="#gem5-semihosting"></a><a class="link" href="#gem5-semihosting">27.3.1. gem5 semihosting</a></h4>
<div class="paragraph">
<p>For gem5, you need:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>patch -d "$(./getvar gem5_source_dir)" -p 1 &lt; patches/manual/gem5-semihost.patch</pre>
</div>
</div>
<div class="paragraph">
<p><a href="https://stackoverflow.com/questions/52475268/how-to-enable-arm-semihosting-in-gem5/52475269#52475269" class="bare">https://stackoverflow.com/questions/52475268/how-to-enable-arm-semihosting-in-gem5/52475269#52475269</a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gem5-baremetal-carriage-return"><a class="anchor" href="#gem5-baremetal-carriage-return"></a><a class="link" href="#gem5-baremetal-carriage-return">27.4. gem5 baremetal carriage return</a></h3>
<div class="paragraph">
<p>TODO: our example is printing newlines without automatic carriage return <code>\r</code> as in:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>enter a character
got: a</pre>
</div>
</div>
<div class="paragraph">
<p>We use <code>m5term</code> by default, and if we try <code>telnet</code> instead:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>telnet localhost 3456</pre>
</div>
</div>
<div class="paragraph">
<p>it does add the carriage returns automatically.</p>
</div>
</div>
<div class="sect2">
<h3 id="baremetal-host-packaged-toolchain"><a class="anchor" href="#baremetal-host-packaged-toolchain"></a><a class="link" href="#baremetal-host-packaged-toolchain">27.5. Baremetal host packaged toolchain</a></h3>
<div class="paragraph">
<p>For <code>arm</code>, some baremetal examples compile fine with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>sudo apt-get install gcc-arm-none-eabi qemu-system-arm
./build-baremetal --arch arm --gcc-which host-baremetal
./run --arch arm --baremetal userland/c/hello.c --qemu-which host</pre>
</div>
</div>
<div class="paragraph">
<p>However, there are as usual limitations to using prebuilts:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>certain examples fail to build with the Ubuntu packaged toolchain. E.g.: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/exit0.c">userland/c/exit0.c</a> fails with:</p>
<div class="literalblock">
<div class="content">
<pre>/usr/lib/gcc/arm-none-eabi/6.3.1/../../../arm-none-eabi/lib/libg.a(lib_a-fini.o): In function `__libc_fini_array':
/build/newlib-8gJlYR/newlib-2.4.0.20160527/build/arm-none-eabi/newlib/libc/misc/../../../../../newlib/libc/misc/fini.c:33: undefined reference to `_fini'
collect2: error: ld returned 1 exit status</pre>
</div>
</div>
<div class="paragraph">
<p>with the prebuilt toolchain, and I&#8217;m lazy to debug.</p>
</div>
</li>
<li>
<p>there seems to to be no analogous <code>aarch64</code> Ubuntu package to <code>gcc-arm-none-eabi</code>: <a href="https://askubuntu.com/questions/1049249/is-there-a-package-with-the-aarch64-version-of-gcc-arm-none-eabi-for-bare-metal" class="bare">https://askubuntu.com/questions/1049249/is-there-a-package-with-the-aarch64-version-of-gcc-arm-none-eabi-for-bare-metal</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="baremetal-cpp"><a class="anchor" href="#baremetal-cpp"></a><a class="link" href="#baremetal-cpp">27.6. Baremetal C++</a></h3>
<div class="paragraph">
<p>TODO not working as of 8825222579767f2ee7e46ffd8204b9e509440759 + 1. Not yet properly researched / reported upstream yet.</p>
</div>
<div class="paragraph">
<p>Should not be hard in theory since <code>libstdc++</code> is just part of GCC, as shown at: <a href="https://stackoverflow.com/questions/21872229/how-to-edit-and-re-build-the-gcc-libstdc-c-standard-library-source/51946224#51946224" class="bare">https://stackoverflow.com/questions/21872229/how-to-edit-and-re-build-the-gcc-libstdc-c-standard-library-source/51946224#51946224</a></p>
</div>
<div class="paragraph">
<p>To test it out, I first hack <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/common.py">common.py</a> to enable <code>C++</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>consts['baremetal_build_in_exts'] = consts['build_in_exts']</pre>
</div>
</div>
<div class="paragraph">
<p>and then I hack <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/inline_asm/multiline.cpp">userland/arch/aarch64/inline_asm/multiline.cpp</a> to consist only of an empty main:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>int main() {}</pre>
</div>
</div>
<div class="paragraph">
<p>then for example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-baremetal --arch aarch64
./run --arch aarch64 --baremetal userland/arch/aarch64/inline_asm/multiline.cpp</pre>
</div>
</div>
<div class="paragraph">
<p>fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rom: requested regions overlap (rom dtb. free=0x00000000000000a0, addr=0x0000000000000000)
qemu-system-aarch64: rom check and register reset failed</pre>
</div>
</div>
<div class="paragraph">
<p>and the gem5 build fails completely:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-baremetal --arch aarch64 --emulator gem5 userland/arch/aarch64/inline_asm/multiline.cpp</pre>
</div>
</div>
<div class="paragraph">
<p>fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/tmp/ccFd2YIB.o:(.eh_frame+0x1c): relocation truncated to fit: R_AARCH64_PREL32 against `.text'
collect2: error: ld returned 1 exit status</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="gdb-builtin-cpu-simulator"><a class="anchor" href="#gdb-builtin-cpu-simulator"></a><a class="link" href="#gdb-builtin-cpu-simulator">27.7. GDB builtin CPU simulator</a></h3>
<div class="paragraph">
<p>It is incredible, but GDB also has a CPU simulator inside of it as documented at: <a href="https://sourceware.org/gdb/onlinedocs/gdb/Target-Commands.html" class="bare">https://sourceware.org/gdb/onlinedocs/gdb/Target-Commands.html</a></p>
</div>
<div class="paragraph">
<p>TODO: any advantage over QEMU? I doubt it, mostly using it as as toy for now:</p>
</div>
<div class="paragraph">
<p>Without running <code>./run</code>, do directly:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --arch arm --baremetal userland/c/hello.c --sim</pre>
</div>
</div>
<div class="paragraph">
<p>Then inside GDB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>load
starti</pre>
</div>
</div>
<div class="paragraph">
<p>and now you can debug normally.</p>
</div>
<div class="paragraph">
<p>Enabled with the crosstool-NG configuration:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>CT_GDB_CROSS_SIM=y</pre>
</div>
</div>
<div class="paragraph">
<p>which by grepping crosstool-NG we can see does on GDB:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./configure --enable-sim</pre>
</div>
</div>
<div class="paragraph">
<p>Those are not set by default on <code>gdb-multiarch</code> in Ubuntu 16.04.</p>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/49470659/arm-none-eabi-gdb-undefined-target-command-sim" class="bare">https://stackoverflow.com/questions/49470659/arm-none-eabi-gdb-undefined-target-command-sim</a></p>
</li>
<li>
<p><a href="http://cs107e.github.io/guides/gdb/" class="bare">http://cs107e.github.io/guides/gdb/</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="gdb-builtin-cpu-simulator-userland"><a class="anchor" href="#gdb-builtin-cpu-simulator-userland"></a><a class="link" href="#gdb-builtin-cpu-simulator-userland">27.7.1. GDB builtin CPU simulator userland</a></h4>
<div class="paragraph">
<p>Since I had this compiled, I also decided to try it out on userland.</p>
</div>
<div class="paragraph">
<p>I was also able to run a freestanding Linux userland example on it: <a href="https://github.com/cirosantilli/arm-assembly-cheat/blob/cd232dcaf32c0ba6399b407e0b143d19b6ec15f4/v7/linux/hello.S" class="bare">https://github.com/cirosantilli/arm-assembly-cheat/blob/cd232dcaf32c0ba6399b407e0b143d19b6ec15f4/v7/linux/hello.S</a></p>
</div>
<div class="paragraph">
<p>It just ignores the <a href="#arm-svc-instruction">ARM SVC instruction</a> however, and does not forward syscalls to the host like QEMU does.</p>
</div>
<div class="paragraph">
<p>Then I tried a glibc example: <a href="https://github.com/cirosantilli/arm-assembly-cheat/blob/cd232dcaf32c0ba6399b407e0b143d19b6ec15f4/v7/mov.S" class="bare">https://github.com/cirosantilli/arm-assembly-cheat/blob/cd232dcaf32c0ba6399b407e0b143d19b6ec15f4/v7/mov.S</a></p>
</div>
<div class="paragraph">
<p>First it wouldn&#8217;t break, so I added <code>-static</code> to the <code>Makefile</code>, and then it started failing with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Unhandled v6 thumb insn</pre>
</div>
</div>
<div class="paragraph">
<p>Doing:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>help architecture</pre>
</div>
</div>
<div class="paragraph">
<p>shows ARM version up to <code>armv6</code>, so maybe <code>armv6</code> is not implemented?</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="arm-baremetal"><a class="anchor" href="#arm-baremetal"></a><a class="link" href="#arm-baremetal">27.8. ARM baremetal</a></h3>
<div class="paragraph">
<p>In this section we will focus on learning ARM architecture concepts that can only learnt on baremetal setups.</p>
</div>
<div class="paragraph">
<p>Userland information can be found at: <a href="https://github.com/cirosantilli/arm-assembly-cheat" class="bare">https://github.com/cirosantilli/arm-assembly-cheat</a></p>
</div>
<div class="sect3">
<h4 id="arm-exception-levels"><a class="anchor" href="#arm-exception-levels"></a><a class="link" href="#arm-exception-levels">27.8.1. ARM exception levels</a></h4>
<div class="paragraph">
<p>ARM exception levels are analogous to x86 <a href="#ring0">rings</a>.</p>
</div>
<div class="paragraph">
<p>The current EL can be determined by reading from certain registers, which we do with bit disassembly at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --baremetal userland/arch/arm/dump_regs.c
./run --arch aarch64 --baremetal baremetal/arch/aarch64/dump_regs.c</pre>
</div>
</div>
<div class="paragraph">
<p>The relevant bits are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>arm: <code>CPSR.M</code></p>
</li>
<li>
<p>aarch64: <code>CurrentEl.EL</code>. This register is not accessible from EL0 for some weird reason however.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/arm/dump_regs.c">baremetal/arch/arm/dump_regs.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/dump_regs.c">baremetal/arch/aarch64/dump_regs.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The instructions that find the ARM EL are explained at: <a href="https://stackoverflow.com/questions/31787617/what-is-the-current-execution-mode-exception-level-etc" class="bare">https://stackoverflow.com/questions/31787617/what-is-the-current-execution-mode-exception-level-etc</a></p>
</div>
<div class="paragraph">
<p>The lower ELs are not mandated by the architecture, and can be controlled through command line options in QEMU and gem5.</p>
</div>
<div class="paragraph">
<p>In QEMU, you can configure the lowest EL as explained at <a href="https://stackoverflow.com/questions/42824706/qemu-system-aarch64-entering-el1-when-emulating-a53-power-up" class="bare">https://stackoverflow.com/questions/42824706/qemu-system-aarch64-entering-el1-when-emulating-a53-power-up</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --baremetal userland/arch/arm/dump_regs.c | grep CPSR.M
./run --arch arm --baremetal userland/arch/arm/dump_regs.c -- -machine virtualization=on | grep CPSR.M
./run --arch arm --baremetal userland/arch/arm/dump_regs.c -- -machine secure=on | grep CPSR.M
./run --arch aarch64 --baremetal baremetal/arch/aarch64/dump_regs.c | grep CurrentEL.EL
./run --arch aarch64 --baremetal baremetal/arch/aarch64/dump_regs.c -- -machine virtualization=on | grep CurrentEL.EL
./run --arch aarch64 --baremetal baremetal/arch/aarch64/dump_regs.c -- -machine secure=on | grep CurrentEL.EL</pre>
</div>
</div>
<div class="paragraph">
<p>outputs respectively:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>CPSR.M 0x3
CPSR.M 0x3
CPSR.M 0x3
CurrentEL.EL 0x1
CurrentEL.EL 0x2
CurrentEL.EL 0x3</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: why is arm <code>CPSR.M</code> stuck at <code>0x3</code> which equals Supervisor mode?</p>
</div>
<div class="paragraph">
<p>In gem5, you can configure the lowest EL with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --baremetal userland/arch/arm/dump_regs.c --emulator gem5
grep CPSR.M "$(./getvar --arch arm --emulator gem5 gem5_guest_terminal_file)"
./run --arch arm --baremetal userland/arch/arm/dump_regs.c --emulator gem5 -- --param 'system.have_virtualization = True'
grep CPSR.M "$(./getvar --arch arm --emulator gem5 gem5_guest_terminal_file)"
./run --arch arm --baremetal userland/arch/arm/dump_regs.c --emulator gem5 -- --param 'system.have_security = True'
grep CPSR.M "$(./getvar --arch arm --emulator gem5 gem5_guest_terminal_file)"
./run --arch aarch64 --baremetal baremetal/arch/aarch64/dump_regs.c --emulator gem5
grep CurrentEL.EL "$(./getvar --arch aarch64 --emulator gem5 gem5_guest_terminal_file)"
./run --arch aarch64 --baremetal baremetal/arch/aarch64/dump_regs.c --emulator gem5 -- --param 'system.have_virtualization = True'
grep CurrentEL.EL "$(./getvar --arch aarch64 --emulator gem5 gem5_guest_terminal_file)"
./run --arch aarch64 --baremetal baremetal/arch/aarch64/dump_regs.c --emulator gem5 -- --param 'system.have_security = True'
grep CurrentEL.EL "$(./getvar --arch aarch64 --emulator gem5 gem5_guest_terminal_file)"</pre>
</div>
</div>
<div class="paragraph">
<p>output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>CPSR.M 0x3
CPSR.M 0xA
CPSR.M 0x3
CurrentEL.EL 0x1
CurrentEL.EL 0x2
CurrentEL.EL 0x3</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: the call:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --baremetal userland/arch/arm/dump_regs.c --emulator gem5 -- --param 'system.have_virtualization = True'</pre>
</div>
</div>
<div class="paragraph">
<p>started failing with an exception since <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/commit/add6eedb76636b8f443b815c6b2dd160afdb7ff4" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/commit/add6eedb76636b8f443b815c6b2dd160afdb7ff4</a> at the instruction:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vmsr fpexc, r0</pre>
</div>
</div>
<div class="paragraph">
<p>in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/lib/arm.S">baremetal/lib/arm.S</a>. That patch however enables SIMD in baremetal, which I feel is more important.</p>
</div>
<div class="paragraph">
<p>According to <a href="#armarm7">ARMv7 architecture reference manual</a>, access to that register is controlled by other registers <code>NSACR.{CP11, CP10}</code> and <code>HCPTR</code> so those must be turned off, but I&#8217;m lazy to investigate now, even just trying to dump those registers in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/dump_regs.c">userland/arch/arm/dump_regs.c</a> also leads to exceptions&#8230;&#8203;</p>
</div>
<div class="sect4">
<h5 id="arm-change-exception-level"><a class="anchor" href="#arm-change-exception-level"></a><a class="link" href="#arm-change-exception-level">27.8.1.1. ARM change exception level</a></h5>
<div class="paragraph">
<p>TODO. Create a minimal runnable example of going into EL0 and jumping to EL1.</p>
</div>
</div>
<div class="sect4">
<h5 id="arm-sp0-vs-spx"><a class="anchor" href="#arm-sp0-vs-spx"></a><a class="link" href="#arm-sp0-vs-spx">27.8.1.2. ARM SP0 vs SPx</a></h5>
<div class="paragraph">
<p>See <a href="#armarm8-db">ARMv8 architecture reference manual db</a> D1.6.2 "The stack pointer registers".</p>
</div>
<div class="paragraph">
<p>TODO create a minimal runnable example.</p>
</div>
<div class="paragraph">
<p>TODO: how to select to use SP0 in an exception handler?</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="arm-svc-instruction"><a class="anchor" href="#arm-svc-instruction"></a><a class="link" href="#arm-svc-instruction">27.8.2. ARM SVC instruction</a></h4>
<div class="paragraph">
<p>This is the most basic example of exception handling we have.</p>
</div>
<div class="paragraph">
<p>We a handler for SVC, do an SVC, and observe that the handler got called and returned from C and assembly:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --baremetal baremetal/arch/aarch64/svc.c
./run --arch aarch64 --baremetal baremetal/arch/aarch64/svc_asm.S</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/svc.c">baremetal/arch/aarch64/svc.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/svc_asm.S">baremetal/arch/aarch64/svc_asm.S</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Sample output for the C one:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>DAIF 0x3C0
SPSEL 0x1
VBAR_EL1 0x40000800
after_svc 0x4000209c
lkmc_vector_trap_handler
exc_type 0x11
exc_type is LKMC_VECTOR_SYNC_SPX
ESR 0x5600ABCD
ESR.EC 0x15
ESR.EC.ISS.imm16 0xABCD
SP 0x4200C510
ELR 0x4000209C
SPSR 0x600003C5
x0 0x0
x1 0x1
x2 0x15
x3 0x15
x4 0x4000A178
x5 0xFFFFFFF6
x6 0x4200C390
x7 0x78
x8 0x1
x9 0x14
x10 0x0
x11 0x0
x12 0x0
x13 0x0
x14 0x0
x15 0x0
x16 0x0
x17 0x0
x18 0x0
x19 0x0
x20 0x0
x21 0x0
x22 0x0
x23 0x0
x24 0x0
x25 0x0
x26 0x0
x27 0x0
x28 0x0
x29 0x4200C510
x30 0x40002064</pre>
</div>
</div>
<div class="paragraph">
<p>The C code does an:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>svc 0xABCD</pre>
</div>
</div>
<div class="paragraph">
<p>and the value 0xABCD appears at the bottom of <a href="#arm-esr-register">ARM ESR register</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ESR 0x5600ABCD
ESR.EC 0x15
ESR.EC.ISS.imm16 0xABCD</pre>
</div>
</div>
<div class="paragraph">
<p>The other important register is the <a href="#arm-elr-register">ARM ELR register</a>, which contains the return address after the exception.</p>
</div>
<div class="paragraph">
<p>From the output, we can see that it matches the value as obtained by taking the address of a label placed just after the SVC:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>after_svc 0x4000209c
ELR 0x4000209C</pre>
</div>
</div>
<div class="paragraph">
<p>Both QEMU and gem5 are able to trace interrupts in addition to instructions, and it is instructive to enable both and have a look at the traces.</p>
</div>
<div class="paragraph">
<p>With <a href="#qemu-d-tracing">QEMU -d tracing</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--baremetal baremetal/arch/aarch64/svc.c \
-- -d in_asm,int \
;</pre>
</div>
</div>
<div class="paragraph">
<p>the output at 8f73910dd1fc1fa6dc6904ae406b7598cdcd96d7 contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>----------------
IN: main
0x40002098: d41579a1 svc #0xabcd
Taking exception 2 [SVC]
...from EL1 to EL1
...with ESR 0x15/0x5600abcd
...with ELR 0x4000209c
...to EL1 PC 0x40000a00 PSTATE 0x3c5
----------------
IN:
0x40000a00: 14000225 b #0x40001294
----------------
IN:
0x40001294: a9bf7bfd stp x29, x30, [sp, #-0x10]!
0x40001298: a9bf73fb stp x27, x28, [sp, #-0x10]!
0x4000129c: a9bf6bf9 stp x25, x26, [sp, #-0x10]!
0x400012a0: a9bf63f7 stp x23, x24, [sp, #-0x10]!
0x400012a4: a9bf5bf5 stp x21, x22, [sp, #-0x10]!
0x400012a8: a9bf53f3 stp x19, x20, [sp, #-0x10]!
0x400012ac: a9bf4bf1 stp x17, x18, [sp, #-0x10]!
0x400012b0: a9bf43ef stp x15, x16, [sp, #-0x10]!
0x400012b4: a9bf3bed stp x13, x14, [sp, #-0x10]!
0x400012b8: a9bf33eb stp x11, x12, [sp, #-0x10]!
0x400012bc: a9bf2be9 stp x9, x10, [sp, #-0x10]!
0x400012c0: a9bf23e7 stp x7, x8, [sp, #-0x10]!
0x400012c4: a9bf1be5 stp x5, x6, [sp, #-0x10]!
0x400012c8: a9bf13e3 stp x3, x4, [sp, #-0x10]!
0x400012cc: a9bf0be1 stp x1, x2, [sp, #-0x10]!
0x400012d0: d5384015 mrs x21, spsr_el1
0x400012d4: a9bf03f5 stp x21, x0, [sp, #-0x10]!
0x400012d8: d5384035 mrs x21, elr_el1
0x400012dc: a9bf57ff stp xzr, x21, [sp, #-0x10]!
0x400012e0: d2800235 movz x21, #0x11
0x400012e4: d5385216 mrs x22, esr_el1
0x400012e8: a9bf5bf5 stp x21, x22, [sp, #-0x10]!
0x400012ec: 910003f5 mov x21, sp
0x400012f0: 910482b5 add x21, x21, #0x120
0x400012f4: f9000bf5 str x21, [sp, #0x10]
0x400012f8: 910003e0 mov x0, sp
0x400012fc: 9400023f bl #0x40001bf8
----------------
IN: lkmc_vector_trap_handler
0x40001bf8: a9bd7bfd stp x29, x30, [sp, #-0x30]!</pre>
</div>
</div>
<div class="paragraph">
<p>And with <a href="#gem5-tracing">gem5 tracing</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch aarch64 \
--baremetal baremetal/arch/aarch64/svc_asm.S \
--trace ExecAll,Faults \
--trace-stdout \
;</pre>
</div>
</div>
<div class="paragraph">
<p>the output contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> 4000: system.cpu A0 T0 : @main+8 : svc #0x0 : IntAlu : flags=(IsSerializeAfter|IsNonSpeculative|IsSyscall)
4000: Supervisor Call: Invoking Fault (AArch64 target EL):Supervisor Call cpsr:0x3c5 PC:0x80000808 elr:0x8000080c newVec: 0x80001200
4500: system.cpu A0 T0 : @vector_table+512 : b &lt;_curr_el_spx_sync&gt; : IntAlu : flags=(IsControl|IsDirectControl|IsUncondControl)</pre>
</div>
</div>
<div class="paragraph">
<p>So we see in both cases that the:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>SVC is done</p>
</li>
<li>
<p>an exception happens, and the PC jumps to address 0x40000a00. From our custom terminal prints further on, we see that this equals <code>VBAR_EL1 + 0x200</code>.</p>
<div class="paragraph">
<p>According to the format of the <a href="#armv8-exception-vector-table-format">ARMv8 exception vector table format</a>, we see that the <code>+ 0x200</code> means that we are jumping in the Current EL with SPx.</p>
</div>
<div class="paragraph">
<p>This can also be deduced from the message <code>exc_type is LKMC_VECTOR_SYNC_SPX</code>: we just manually store a different integer for every exception vector type in our handler code to be able to tell what happened.</p>
</div>
<div class="paragraph">
<p>This is the one used because we are jumping <a href="#arm-exception-levels">from EL1 to EL1</a>.</p>
</div>
<div class="paragraph">
<p>We set VBAR_EL1 to that address ourselves <a href="#baremetal-bootloaders">in the bootloader</a>.</p>
</div>
</li>
<li>
<p>at 0x40000a00 a <code>b #0x40001294</code> is done and then at 0x40001294 boilerplate preparation is done for lkmc_vector_trap_handler starting with several STP instructions.</p>
<div class="paragraph">
<p>We have coded both of those in our vector table macro madness. As of LKMC 8f73910dd1fc1fa6dc6904ae406b7598cdcd96d7, both come from <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/aarch64.h">lkmc/aarch64.h</a>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>b #0x40001294</code> comes from: <code>LKMC_VECTOR_ENTRY</code></p>
</li>
<li>
<p>the STP come from: <code>LKMC_VECTOR_BUILD_TRAPFRAME</code></p>
<div class="paragraph">
<p>We jump immediately from inside <code>LKMC_VECTOR_ENTRY</code> to <code>LKMC_VECTOR_BUILD_TRAPFRAME</code> because we can only use 0x80 bytes of instructions for each one before reaching the next handler, so we might as well get it over with by jumping into a memory region without those constraints.</p>
</div>
<div class="paragraph">
<p>TODO: why doesn&#8217;t QEMU show our nice symbol names? gem5 shows them fine, and <code>nm</code> says they are there!</p>
</div>
<div class="literalblock">
<div class="content">
<pre>0000000040000800 T lkmc_vector_table
0000000040001294 T lkmc_vector_build_trapframe_curr_el_spx_sync</pre>
</div>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>The exception return happens at the end of <code>lkmc_vector_trap_handler</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>----------------
IN: lkmc_vector_trap_handler
0x40002000: d503201f nop
0x40002004: a8c37bfd ldp x29, x30, [sp], #0x30
0x40002008: d65f03c0 ret
----------------
IN:
0x40001300: 910043ff add sp, sp, #0x10
0x40001304: a8c15bf5 ldp x21, x22, [sp], #0x10
0x40001308: d5184036 msr elr_el1, x22
----------------
IN:
0x4000130c: a8c103f5 ldp x21, x0, [sp], #0x10
0x40001310: d5184015 msr spsr_el1, x21
----------------
IN:
0x40001314: a8c10be1 ldp x1, x2, [sp], #0x10
0x40001318: a8c113e3 ldp x3, x4, [sp], #0x10
0x4000131c: a8c11be5 ldp x5, x6, [sp], #0x10
0x40001320: a8c123e7 ldp x7, x8, [sp], #0x10
0x40001324: a8c12be9 ldp x9, x10, [sp], #0x10
0x40001328: a8c133eb ldp x11, x12, [sp], #0x10
0x4000132c: a8c13bed ldp x13, x14, [sp], #0x10
0x40001330: a8c143ef ldp x15, x16, [sp], #0x10
0x40001334: a8c14bf1 ldp x17, x18, [sp], #0x10
0x40001338: a8c153f3 ldp x19, x20, [sp], #0x10
0x4000133c: a8c15bf5 ldp x21, x22, [sp], #0x10
0x40001340: a8c163f7 ldp x23, x24, [sp], #0x10
0x40001344: a8c16bf9 ldp x25, x26, [sp], #0x10
0x40001348: a8c173fb ldp x27, x28, [sp], #0x10
0x4000134c: a8c17bfd ldp x29, x30, [sp], #0x10
0x40001350: d69f03e0 eret
Exception return from AArch64 EL1 to AArch64 EL1 PC 0x4000209c
----------------
IN: main
0x4000209c: d0000040 adrp x0, #0x4000c000</pre>
</div>
</div>
<div class="paragraph">
<p>which does an <code>eret</code> and jumps back to 0x4000209c, which is 4 bytes and therefore one instruction after where SVC was taken at 0x40002098.</p>
</div>
<div class="paragraph">
<p>In QEMU, and then we just continue running from the exception handler address.</p>
</div>
<div class="paragraph">
<p>On the terminal output, we observe the initial values of:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>DAIF: 0x3c0, i.e. 4 bits (6 to 9) set to 1, which means that exceptions are masked for each exception type: Synchronous, System error, IRQ and FIQ.</p>
<div class="paragraph">
<p>This reset value is defined by <a href="#armarm8">ARMv8 architecture reference manual</a> C5.2.2 "DAIF, Interrupt Mask Bits".</p>
</div>
</li>
<li>
<p>SPSel: 0x1, which means: use SPx instead of SP0.</p>
<div class="paragraph">
<p>This reset value is defined by <a href="#armarm8">ARMv8 architecture reference manual</a> C5.2.16 "SPSel, Stack Pointer Select".</p>
</div>
</li>
<li>
<p>VBAR_EL1: 0x0 holds the base address of the vector table</p>
<div class="paragraph">
<p>This reset value is defined UNKNOWN by <a href="#armarm8">ARMv8 architecture reference manual</a> D10.2.116 "VBAR_EL1, Vector Base Address Register (EL1)", so we must set it to something ourselves to have greater portability.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/torvalds/linux/blob/v4.20/arch/arm64/kernel/entry.S#L430" class="bare">https://github.com/torvalds/linux/blob/v4.20/arch/arm64/kernel/entry.S#L430</a> this is where the kernel defines the vector table</p>
</li>
<li>
<p><a href="https://github.com/dwelch67/qemu_arm_samples/tree/07162ba087111e0df3f44fd857d1b4e82458a56d/swi01" class="bare">https://github.com/dwelch67/qemu_arm_samples/tree/07162ba087111e0df3f44fd857d1b4e82458a56d/swi01</a></p>
</li>
<li>
<p><a href="https://github.com/NienfengYao/armv8-bare-metal/blob/572c6f95880e70aa92fe9fed4b8ad7697082a764/vector.S#L168" class="bare">https://github.com/NienfengYao/armv8-bare-metal/blob/572c6f95880e70aa92fe9fed4b8ad7697082a764/vector.S#L168</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/24162109/arm-assembly-code-and-svc-numbering/57064062#57064062" class="bare">https://stackoverflow.com/questions/24162109/arm-assembly-code-and-svc-numbering/57064062#57064062</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/44991264/armv8-exception-vectors-and-handling" class="bare">https://stackoverflow.com/questions/44991264/armv8-exception-vectors-and-handling</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="armv8-exception-vector-table-format"><a class="anchor" href="#armv8-exception-vector-table-format"></a><a class="link" href="#armv8-exception-vector-table-format">27.8.2.1. ARMv8 exception vector table format</a></h5>
<div class="paragraph">
<p>The vector table format is described on <a href="#armarm8">ARMv8 architecture reference manual</a> Table D1-7 "Vector offsets from vector table base address".</p>
</div>
<div class="paragraph">
<p>A good representation of the format of the vector table can also be found at <a href="#armv8-programmers-guide">Programmer&#8217;s Guide for ARMv8-A</a> Table 10-2 "Vector table offsets from vector table base address".</p>
</div>
<div class="paragraph">
<p>The first part of the table contains: <a href="#table-armv8-vector-handlers">Table 6, &#8220;Summary of ARMv8 vector handlers&#8221;</a>.</p>
</div>
<table id="table-armv8-vector-handlers" class="tableblock frame-all grid-all stretch">
<caption class="title">Table 6. Summary of ARMv8 vector handlers</caption>
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Address</th>
<th class="tableblock halign-left valign-top">Exception type</th>
<th class="tableblock halign-left valign-top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x000</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Synchronous</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Current EL with SP0</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x080</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">IRQ/vIRQ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Current EL with SP0</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x100</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">FIQ/vFIQ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Current EL with SP0</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x180</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">SError/vSError</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Current EL with SP0</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x200</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Synchronous</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Current EL with SPx</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x280</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">IRQ/vIRQ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Current EL with SPx</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x300</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">FIQ/vFIQ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Current EL with SPx</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x380</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">SError/vSError</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Lower EL using AArch64</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x400</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Synchronous</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Lower EL using AArch64</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x480</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">IRQ/vIRQ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Lower EL using AArch64</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x500</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">FIQ/vFIQ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Lower EL using AArch64</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x580</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">SError/vSError</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Lower EL using AArch64</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x600</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Synchronous</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Lower EL using AArch32</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x680</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">IRQ/vIRQ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Lower EL using AArch32</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x700</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">FIQ/vFIQ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Lower EL using AArch32</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">VBAR_ELn + 0x780</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">SError/vSError</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Lower EL using AArch32</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>and the following other parts are analogous, but referring to SPx and lower ELs.</p>
</div>
<div class="paragraph">
<p>Now, to fully understand this table, we need the following concepts:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Synchronous: what happens for example when we do an <a href="#arm-svc-instruction">ARM SVC instruction</a>.</p>
<div class="paragraph">
<p>It is called synchronous because the CPU is generating it itself from an instruction, unlike an interrupt generated by a device like a keyboard, which ends up in an IRQ or FIQ</p>
</div>
</li>
<li>
<p>IRQ: an example can be found at: <a href="#arm-timer">ARM timer</a></p>
</li>
<li>
<p>TODO FIQ vs IRQ</p>
</li>
<li>
<p>TODO SError</p>
</li>
<li>
<p>EL changes: <a href="#arm-change-exception-level">ARM change exception level</a></p>
</li>
<li>
<p>SP0 vs SPx: <a href="#arm-sp0-vs-spx">ARM SP0 vs SPx</a>.</p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="arm-esr-register"><a class="anchor" href="#arm-esr-register"></a><a class="link" href="#arm-esr-register">27.8.2.2. ARM ESR register</a></h5>
<div class="paragraph">
<p>Exception Syndrome Register.</p>
</div>
<div class="paragraph">
<p>See example at: <a href="#arm-svc-instruction">Section 27.8.2, &#8220;ARM SVC instruction&#8221;</a></p>
</div>
<div class="paragraph">
<p>Documentation: <a href="#armarm8-db">ARMv8 architecture reference manual db</a> D12.2.36 "ESR_EL1, Exception Syndrome Register (EL1)".</p>
</div>
</div>
<div class="sect4">
<h5 id="arm-elr-register"><a class="anchor" href="#arm-elr-register"></a><a class="link" href="#arm-elr-register">27.8.2.3. ARM ELR register</a></h5>
<div class="paragraph">
<p>Exception Link Register.</p>
</div>
<div class="paragraph">
<p>See the example at: <a href="#arm-svc-instruction">Section 27.8.2, &#8220;ARM SVC instruction&#8221;</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="arm-multicore"><a class="anchor" href="#arm-multicore"></a><a class="link" href="#arm-multicore">27.8.3. ARM multicore</a></h4>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --baremetal baremetal/arch/aarch64/no_bootloader/multicore_asm.S --cpus 2
./run --arch aarch64 --baremetal baremetal/arch/aarch64/no_bootloader/multicore_asm.S --cpus 2 --emulator gem5
./run --arch aarch64 --baremetal baremetal/arch/aarch64/multicore.c --cpus 2
./run --arch aarch64 --baremetal baremetal/arch/aarch64/multicore.c --cpus 2 --emulator gem5
./run --arch arm --baremetal baremetal/arch/arm/no_bootloader/multicore_asm.S --cpus 2
./run --arch arm --baremetal baremetal/arch/arm/no_bootloader/multicore_asm.S --cpus 2 --emulator gem5
# TODO not working, hangs.
# ./run --arch arm --baremetal baremetal/arch/arm/multicore.c --cpus 2
./run --arch arm --baremetal baremetal/arch/arm/multicore.c --cpus 2 --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/no_bootloader/multicore_asm.S">baremetal/arch/aarch64/no_bootloader/multicore_asm.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/multicore.c">baremetal/arch/aarch64/multicore.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/arm/no_bootloader/multicore_asm.S">baremetal/arch/arm/no_bootloader/multicore_asm.S</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/arm/multicore.c">baremetal/arch/arm/multicore.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>CPU 0 of this program enters a spinlock loop: it repeatedly checks if a given memory address is 1.</p>
</div>
<div class="paragraph">
<p>So, we need CPU 1 to come to the rescue and set that memory address to 1, otherwise CPU 0 will be stuck there forever!</p>
</div>
<div class="paragraph">
<p>Don&#8217;t believe me? Then try:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --baremetal baremetal/arch/aarch64/multicore.c --cpus 1</pre>
</div>
</div>
<div class="paragraph">
<p>and watch it hang forever.</p>
</div>
<div class="paragraph">
<p>Note that if you try the same thing on gem5:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --baremetal baremetal/arch/aarch64/multicore.c --cpus 1 --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>then the gem5 actually exits with <a href="#gem5-simulate-limit-reached">gem5 simulate() limit reached</a> as opposed to the expected:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Exiting @ tick 36500 because m5_exit instruction encountered</pre>
</div>
</div>
<div class="paragraph">
<p>since gem5 is able to detect when nothing will ever happen, and exits.</p>
</div>
<div class="paragraph">
<p>When GDB step debugging, switch between cores with the usual <code>thread</code> commands, see also: <a href="#gdb-step-debug-multicore-userland">Section 2.9, &#8220;GDB step debug multicore userland&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/980999/what-does-multicore-assembly-language-look-like/33651438#33651438" class="bare">https://stackoverflow.com/questions/980999/what-does-multicore-assembly-language-look-like/33651438#33651438</a></p>
</div>
<div class="sect4">
<h5 id="arm-wfe-and-sev-instructions"><a class="anchor" href="#arm-wfe-and-sev-instructions"></a><a class="link" href="#arm-wfe-and-sev-instructions">27.8.3.1. ARM WFE and SEV instructions</a></h5>
<div class="paragraph">
<p>The WFE and SEV instructions are just hints: a compliant implementation can treat them as NOPs.</p>
</div>
<div class="paragraph">
<p>However, likely no implementation likely does (TODO confirm), since:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>WFE puts the core in a low power mode</p>
</li>
<li>
<p>SEV wakes up cores from a low power mode</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>and power consumption is key in ARM applications.</p>
</div>
<div class="paragraph">
<p>In QEMU 3.0.0, SEV is a NOPs, and WFE might be, but I&#8217;m not sure, see: <a href="https://github.com/qemu/qemu/blob/v3.0.0/target/arm/translate-a64.c#L1423" class="bare">https://github.com/qemu/qemu/blob/v3.0.0/target/arm/translate-a64.c#L1423</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre> case 2: /* WFE */
if (!(tb_cflags(s-&gt;base.tb) &amp; CF_PARALLEL)) {
s-&gt;base.is_jmp = DISAS_WFE;
}
return;
case 4: /* SEV */
case 5: /* SEVL */
/* we treat all as NOP at least for now */
return;</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: what does the WFE code do? How can it not be a NOP if SEV is a NOP? <a href="https://github.com/qemu/qemu/blob/v3.0.0/target/arm/translate.c#L4609" class="bare">https://github.com/qemu/qemu/blob/v3.0.0/target/arm/translate.c#L4609</a> might explain why, but it is Chinese to me (I only understand 30% ;-)):</p>
</div>
<div class="literalblock">
<div class="content">
<pre> * For WFI we will halt the vCPU until an IRQ. For WFE and YIELD we
* only call the helper when running single threaded TCG code to ensure
* the next round-robin scheduled vCPU gets a crack. In MTTCG mode we
* just skip this instruction. Currently the SEV/SEVL instructions
* which are *one* of many ways to wake the CPU from WFE are not
* implemented so we can't sleep like WFI does.
*/</pre>
</div>
</div>
<div class="paragraph">
<p>For gem5 however, if we comment out the SVE instruction, then it actually exits with <code>simulate() limit reached</code>, so the CPU truly never wakes up, which is a more realistic behaviour.</p>
</div>
<div class="paragraph">
<p>The following Raspberry Pi bibliography helped us get this sample up and running:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/bztsrc/raspi3-tutorial/tree/a3f069b794aeebef633dbe1af3610784d55a0efa/02_multicorec" class="bare">https://github.com/bztsrc/raspi3-tutorial/tree/a3f069b794aeebef633dbe1af3610784d55a0efa/02_multicorec</a></p>
</li>
<li>
<p><a href="https://github.com/dwelch67/raspberrypi/tree/a09771a1d5a0b53d8e7a461948dc226c5467aeec/multi00" class="bare">https://github.com/dwelch67/raspberrypi/tree/a09771a1d5a0b53d8e7a461948dc226c5467aeec/multi00</a></p>
</li>
<li>
<p><a href="https://github.com/LdB-ECM/Raspberry-Pi/blob/3b628a2c113b3997ffdb408db03093b2953e4961/Multicore/SmartStart64.S" class="bare">https://github.com/LdB-ECM/Raspberry-Pi/blob/3b628a2c113b3997ffdb408db03093b2953e4961/Multicore/SmartStart64.S</a></p>
</li>
<li>
<p><a href="https://github.com/LdB-ECM/Raspberry-Pi/blob/3b628a2c113b3997ffdb408db03093b2953e4961/Multicore/SmartStart32.S" class="bare">https://github.com/LdB-ECM/Raspberry-Pi/blob/3b628a2c113b3997ffdb408db03093b2953e4961/Multicore/SmartStart32.S</a></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="arm-psci"><a class="anchor" href="#arm-psci"></a><a class="link" href="#arm-psci">27.8.3.2. ARM PSCI</a></h5>
<div class="paragraph">
<p>In QEMU, CPU 1 starts in a halted state. This can be observed from GDB, where:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>info threads</pre>
</div>
</div>
<div class="paragraph">
<p>shows something like:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>* 1 Thread 1 (CPU#0 [running]) lkmc_start
2 Thread 2 (CPU#1 [halted ]) lkmc_start</pre>
</div>
</div>
<div class="paragraph">
<p>To wake up CPU 1 on QEMU, we must use the Power State Coordination Interface (PSCI) which is documented at: <a href="https://developer.arm.com/docs/den0022/latest/arm-power-state-coordination-interface-platform-design-document" class="bare">https://developer.arm.com/docs/den0022/latest/arm-power-state-coordination-interface-platform-design-document</a>.</p>
</div>
<div class="paragraph">
<p>This interface uses HVC calls, and the calling convention is documented at "SMC CALLING CONVENTION" <a href="https://developer.arm.com/docs/den0028/latest" class="bare">https://developer.arm.com/docs/den0028/latest</a>.</p>
</div>
<div class="paragraph">
<p>If we boot the Linux kernel on QEMU and <a href="#get-device-tree-from-a-running-kernel">dump the auto-generated device tree</a>, we observe that it contains the address of the PSCI CPU_ON call:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> psci {
method = "hvc";
compatible = "arm,psci-0.2", "arm,psci";
cpu_on = &lt;0xc4000003&gt;;
migrate = &lt;0xc4000005&gt;;
cpu_suspend = &lt;0xc4000001&gt;;
cpu_off = &lt;0x84000002&gt;;
};</pre>
</div>
</div>
<div class="paragraph">
<p>The Linux kernel wakes up the secondary cores in this exact same way at: <a href="https://github.com/torvalds/linux/blob/v4.19/drivers/firmware/psci.c#L122" class="bare">https://github.com/torvalds/linux/blob/v4.19/drivers/firmware/psci.c#L122</a> We first actually got it working here by grepping the kernel and step debugging that call :-)</p>
</div>
<div class="paragraph">
<p>In gem5, CPU 1 starts woken up from the start, so PSCI is not needed. TODO gem5 actually blows up if we try to do the HVC call, understand why.</p>
</div>
<div class="paragraph">
<p>Bibliography: <a href="https://stackoverflow.com/questions/20055754/arm-start-wakeup-bringup-the-other-cpu-cores-aps-and-pass-execution-start-addre/53473447#53473447" class="bare">https://stackoverflow.com/questions/20055754/arm-start-wakeup-bringup-the-other-cpu-cores-aps-and-pass-execution-start-addre/53473447#53473447</a></p>
</div>
</div>
<div class="sect4">
<h5 id="arm-dmb-instruction"><a class="anchor" href="#arm-dmb-instruction"></a><a class="link" href="#arm-dmb-instruction">27.8.3.3. ARM DMB instruction</a></h5>
<div class="paragraph">
<p>TODO: create and study a minimal examples in gem5 where the DMB instruction leads to less cycles: <a href="https://stackoverflow.com/questions/15491751/real-life-use-cases-of-barriers-dsb-dmb-isb-in-arm" class="bare">https://stackoverflow.com/questions/15491751/real-life-use-cases-of-barriers-dsb-dmb-isb-in-arm</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="arm-timer"><a class="anchor" href="#arm-timer"></a><a class="link" href="#arm-timer">27.8.4. ARM timer</a></h4>
<div class="paragraph">
<p>The ARM timer is the simplest way to generate hardware interrupts periodically, and therefore serves as the simples example of <a href="#arm-gic">ARM GIC</a> usage.</p>
</div>
<div class="paragraph">
<p>Working on QEMU: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/arch/aarch64/timer.c">baremetal/arch/aarch64/timer.c</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --baremetal baremetal/arch/aarch64/timer.c</pre>
</div>
</div>
<div class="paragraph">
<p>Output at lkmc d8dae268c0a3e4e361002aca3b382fedd77f2567 + 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cntv_ctl_el0 0x0
cntfrq_el0 0x3B9ACA0
cntv_cval_el0 0x0
cntvct_el0 0x105113
cntvct_el0 0x1080BC
cntvct_el0 0x10A118
IRQ number 0x1B
cntvct_el0 0x14D25B
cntv_cval_el0 0x3CE9CD6
IRQ number 0x1B
cntvct_el0 0x3CF516F
cntv_cval_el0 0x7893217
IRQ number 0x1B
cntvct_el0 0x789B733
cntv_cval_el0 0xB439642</pre>
</div>
</div>
<div class="paragraph">
<p>and new <code>IRQ number</code> section appears every second, when a clock interrupt is raised!</p>
</div>
<div class="paragraph">
<p>TODO make work on gem5. Fails with <a href="#gem5-simulate-limit-reached">gem5 simulate() limit reached</a> at the first WFI done in main, which means that the interrupt is never raised.</p>
</div>
<div class="paragraph">
<p>Once an interrupt is raised, the interrupt itself sets up a new interrupt to happen in one second in the future after <code>cntv_cval_el0</code> is reached by the counter.</p>
</div>
<div class="paragraph">
<p>The timer is part of the aarch64 specification itself and is documented at: <a href="#armarm8-db">ARMv8 architecture reference manual db</a> Chapter D10 "The Generic Timer in AArch64 state". The key registers to keep in mind are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>CNTVCT_EL0</code>: "Counter-timer Virtual Count register". The increasing current counter value.</p>
</li>
<li>
<p><code>CNTFRQ_EL0</code>: "Counter-timer Frequency register". "Indicates the system counter clock frequency, in Hz."</p>
</li>
<li>
<p><code>CNTV_CTL_EL0</code>: "Counter-timer Virtual Timer Control register". This control register is very simple and only has three fields:</p>
<div class="ulist">
<ul>
<li>
<p><code>CNTV_CTL_EL0.ISTATUS</code> bit: set to 1 when the timer condition is met</p>
</li>
<li>
<p><code>CNTV_CTL_EL0.IMASK</code> bit: if 1, the interrupt does not happen when <code>ISTATUS</code> becomes one</p>
</li>
<li>
<p><code>CNTV_CTL_EL0.ENABLE</code> bit: if 0, the counter is turned off, interrupts don&#8217;t happen</p>
</li>
</ul>
</div>
</li>
<li>
<p><code>CNTV_CVAL_EL0</code>: "Counter-timer Virtual Timer CompareValue register". The interrupt happens when <code>CNTVCT_EL0</code> reaches the value in this register.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Due to <a href="#gem5-vs-qemu">QEMU&#8217;s non-determinism</a>, each consecutive run has slightly different output values.</p>
</div>
<div class="paragraph">
<p>From the terminal output, we can see that the initial clock frequency is 0x3B9ACA0 == 62500000 Hz == 62.5MHz. Grepping QEMU source for that string leads us to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/* Scale factor for generic timers, ie number of ns per tick.
* This gives a 62.5MHz timer.
*/
#define GTIMER_SCALE 16</pre>
</div>
</div>
<div class="paragraph">
<p>which in turn is used to set the initial reset value of the clock:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
.opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
.access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
.fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
.resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,</pre>
</div>
</div>
<div class="paragraph">
<p>where <code>(1000 * 1000 * 1000) / 16 == 62500000</code>.</p>
</div>
<div class="paragraph">
<p>Trying to set the frequency on QEMU by writing to the CNTFRQ register does change the value of future reads, but has no effect on the actual clock frequency as commented on the QEMU source code <a href="https://github.com/qemu/qemu/blob/v4.0.0/target/arm/helper.c#L2647" class="bare">https://github.com/qemu/qemu/blob/v4.0.0/target/arm/helper.c#L2647</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre>static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
/* Note that CNTFRQ is purely reads-as-written for the benefit
* of software; writing it doesn't actually change the timer frequency.
* Our reset value matches the fixed frequency we implement the timer at.
*/
{ .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
.type = ARM_CP_ALIAS,
.access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
.fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
},</pre>
</div>
</div>
<div class="paragraph">
<p>At each interrupt, we increase the compare value <code>CVAL</code> by about 1x the clock frequency 0x3B9ACA0 so that it will fire again in one second, e.g. <code>0x3CE9CD6 - 0x14D25B == 3B9CA7B</code>. The increment is not perfect because the counter keeps ticking even while our register read and print instructions are running inside the interrupt handler!</p>
</div>
<div class="paragraph">
<p>We then observe that the next interrupt happens soon after CNTV_CVAL_EL0 is reached by CNTVCT_EL0:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cntv_cval_el0 0x3CE9CD6
IRQ number 0x1B
cntvct_el0 0x3CF516F</pre>
</div>
</div>
<div class="paragraph">
<p>Bibliography:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/51094092/how-to-make-timer-irq-work-on-qemu-machine-virt-cpu-cortex-a57" class="bare">https://stackoverflow.com/questions/51094092/how-to-make-timer-irq-work-on-qemu-machine-virt-cpu-cortex-a57</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/44198483/arm-timers-and-interrupts" class="bare">https://stackoverflow.com/questions/44198483/arm-timers-and-interrupts</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="arm-gic"><a class="anchor" href="#arm-gic"></a><a class="link" href="#arm-gic">27.8.5. ARM GIC</a></h4>
<div class="paragraph">
<p>Generic Interrupt Controller.</p>
</div>
<div class="paragraph">
<p>Examples:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#arm-timer">ARM timer</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>ARM publishes both a GIC standard architecture specification, and specific implementations of these specifications.</p>
</div>
<div class="paragraph">
<p>The specification can be found at: <a href="https://developer.arm.com/docs/ihi0069/latest" class="bare">https://developer.arm.com/docs/ihi0069/latest</a></p>
</div>
<div class="paragraph">
<p>As of 2019Q2 the latest version if v4.0, often called GICv4: <a href="https://static.docs.arm.com/ihi0069/e/Q1-IHI0069E_gic_architecture_specification_v3.1_19_01_21.pdf" class="bare">https://static.docs.arm.com/ihi0069/e/Q1-IHI0069E_gic_architecture_specification_v3.1_19_01_21.pdf</a></p>
</div>
<div class="paragraph">
<p>That document clarifies that GICv2 is a legacy specification only:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Version 2.0 (GICv2) is only described in terms of the GICv3 optional support for legacy operation</pre>
</div>
</div>
<div class="paragraph">
<p>The specific models have names of type GIC-600, GIC-500, etc.</p>
</div>
<div class="paragraph">
<p>In QEMU v4.0.0, the GICv3 can be selected with an extra <code>-machine gic_version=3</code> option.</p>
</div>
<div class="paragraph">
<p>In gem5 3126e84db773f64e46b1d02a9a27892bf6612d30, the GIC is determined by selecting the platform as explained at: <a href="#gem5-arm-platforms">gem5 ARM platforms</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="arm-paging"><a class="anchor" href="#arm-paging"></a><a class="link" href="#arm-paging">27.8.6. ARM paging</a></h4>
<div class="paragraph">
<p>TODO create a minimal working aarch64 example analogous to the x86 one at: <a href="https://github.com/cirosantilli/x86-bare-metal-examples/blob/6dc9a73830fc05358d8d66128f740ef9906f7677/paging.S" class="bare">https://github.com/cirosantilli/x86-bare-metal-examples/blob/6dc9a73830fc05358d8d66128f740ef9906f7677/paging.S</a></p>
</div>
<div class="paragraph">
<p>A general introduction to paging with x86 examples can be found at: <a href="https://cirosantilli.com/x86-paging" class="bare">https://cirosantilli.com/x86-paging</a>.</p>
</div>
<div class="paragraph">
<p>ARM paging is documented at <a href="#armarm8-db">ARMv8 architecture reference manual db</a> Chapter D5 and is mostly called VMSAv8 in the ARMv8 manual (Virtual Memory System Architecture).</p>
</div>
<div class="paragraph">
<p>Paging is enabled by the <code>SCTLR_EL1.M</code> bit.</p>
</div>
<div class="paragraph">
<p>The base table address is selected by the register documented at <a href="#armarm8-db">ARMv8 architecture reference manual db</a> D12.2.111 "TTBR0_EL1, Translation Table Base Register 0 (EL1)".</p>
</div>
<div class="paragraph">
<p>There is also a <code>TTBR1_EL1</code> register, which is for the second translation stage to speed up virtualization: <a href="https://en.wikipedia.org/wiki/Second_Level_Address_Translation" class="bare">https://en.wikipedia.org/wiki/Second_Level_Address_Translation</a> and will not be used in this section.</p>
</div>
<div class="paragraph">
<p>The translation types are described at: <a href="#armarm8-db">ARMv8 architecture reference manual db</a> D5.2.4 "Memory translation granule size".</p>
</div>
<div class="paragraph">
<p>From this we can see that the translation scheme uses up to 4 levels (0 to 3) and has possible granule sizes 4KiB, 16KiB and 64KiB.</p>
</div>
<div class="paragraph">
<p>Page table formats are described at <a href="#armarm8-db">ARMv8 architecture reference manual db</a> D5.3.1 "VMSAv8-64 translation table level 0, level 1, and level 2 descriptor formats".</p>
</div>
</div>
<div class="sect3">
<h4 id="arm-baremetal-bibliography"><a class="anchor" href="#arm-baremetal-bibliography"></a><a class="link" href="#arm-baremetal-bibliography">27.8.7. ARM baremetal bibliography</a></h4>
<div class="paragraph">
<p>First, also consider the userland bibliography: <a href="#arm-assembly-bibliography">Section 24.8, &#8220;ARM assembly bibliography&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>The most useful ARM baremetal example sets we&#8217;ve seen so far are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/dwelch67/raspberrypi" class="bare">https://github.com/dwelch67/raspberrypi</a> real hardware</p>
</li>
<li>
<p><a href="https://github.com/dwelch67/qemu_arm_samples" class="bare">https://github.com/dwelch67/qemu_arm_samples</a> QEMU <code>-m vexpress</code></p>
</li>
<li>
<p><a href="https://github.com/bztsrc/raspi3-tutorial" class="bare">https://github.com/bztsrc/raspi3-tutorial</a> real hardware + QEMU <code>-m raspi</code></p>
</li>
<li>
<p><a href="https://github.com/LdB-ECM/Raspberry-Pi" class="bare">https://github.com/LdB-ECM/Raspberry-Pi</a> real hardware</p>
</li>
<li>
<p><a href="https://github.com/BrianSidebotham/arm-tutorial-rpi" class="bare">https://github.com/BrianSidebotham/arm-tutorial-rpi</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="nienfengyaoarmv8-bare-metal"><a class="anchor" href="#nienfengyaoarmv8-bare-metal"></a><a class="link" href="#nienfengyaoarmv8-bare-metal">27.8.7.1. NienfengYao/armv8-bare-metal</a></h5>
<div class="paragraph">
<p><a href="https://github.com/NienfengYao/armv8-bare-metal" class="bare">https://github.com/NienfengYao/armv8-bare-metal</a></p>
</div>
<div class="paragraph">
<p>The only QEMU <code>-m virt</code> aarch64 example set that I can find on the web. Awesome.</p>
</div>
<div class="paragraph">
<p>A large part of the code is taken from the awesome educational OS under 2-clause BSD as can be seen from file headers: <a href="https://github.com/takeharukato/sample-tsk-sw/tree/ce7973aa5d46c9eedb58309de43df3b09d4f8d8d/hal/aarch64" class="bare">https://github.com/takeharukato/sample-tsk-sw/tree/ce7973aa5d46c9eedb58309de43df3b09d4f8d8d/hal/aarch64</a> but Nienfeng largely minimized it.</p>
</div>
<div class="paragraph">
<p>I needed the following minor patches: <a href="https://github.com/NienfengYao/armv8-bare-metal/pull/1" class="bare">https://github.com/NienfengYao/armv8-bare-metal/pull/1</a></p>
</div>
<div class="paragraph">
<p>Handles an SVC and setups and handles the timer about once per second.</p>
</div>
<div class="paragraph">
<p>The source claims GICv3, however if I try to add <code>-machine gic_version=3</code> on their command line with our QEMU v4.0.0, then it blows up at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>static void init_gicc(void)
{
uint32_t pending_irq;
/* Disable CPU interface */
*REG_GIC_GICC_CTLR = GICC_CTLR_DISABLE;</pre>
</div>
</div>
<div class="paragraph">
<p>which tries to write to 0x8010000 according to GDB.</p>
</div>
<div class="paragraph">
<p>Without <code>-machine</code>, QEMU&#8217;s DTB clearly states GICv2, so I&#8217;m starting to wonder if Nienfeng just made a mistake there? The QEMU GICv3 DTB contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>reg = &lt;0x0 0x8000000 0x0 0x10000 0x0 0x80a0000 0x0 0xf60000&gt;;</pre>
</div>
</div>
<div class="paragraph">
<p>and the GICv2 one:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>reg = &lt;0x0 0x8000000 0x0 0x10000 0x0 0x8010000 0x0 0x10000&gt;;</pre>
</div>
</div>
<div class="paragraph">
<p>which further confirms that the exception is correct: v2 has a register range at 0x8010000 while in v3 it moved to 0x80a0000 and 0x8010000 is empty.</p>
</div>
<div class="paragraph">
<p>The original source does not mention GICv3 anywhere, only <a href="https://github.com/takeharukato/sample-tsk-sw/blob/c7bbc9dce6b14660bcce8d20735f8c6ebb09396b/hal/aarch64/gic-pl390.c">pl390</a>, which is a specific GIC model that predates the GICv2 spec I believe.</p>
</div>
<div class="paragraph">
<p>TODO if I hack <code>#define GIC_GICC_BASE (GIC_BASE + 0xa0000)</code>, then it goes a bit further, but the next loop never ends.</p>
</div>
</div>
<div class="sect4">
<h5 id="tukl-msdgem5-bare-metal"><a class="anchor" href="#tukl-msdgem5-bare-metal"></a><a class="link" href="#tukl-msdgem5-bare-metal">27.8.7.2. tukl-msd/gem5.bare-metal</a></h5>
<div class="paragraph">
<p><a href="https://github.com/tukl-msd/gem5.bare-metal" class="bare">https://github.com/tukl-msd/gem5.bare-metal</a></p>
</div>
<div class="paragraph">
<p>Reiterated at: <a href="https://stackoverflow.com/questions/43682311/uart-communication-in-gem5-with-arm-bare-metal" class="bare">https://stackoverflow.com/questions/43682311/uart-communication-in-gem5-with-arm-bare-metal</a></p>
</div>
<div class="paragraph">
<p>Basic gem5 aarch64 baremetal setup that just works. Does serial IO and timer through GICv2. Usage:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># Build gem5.
git clone https://gem5.googlesource.com/public/gem5
cd gem5
git checkout 60600f09c25255b3c8f72da7fb49100e2682093a
scons --ignore-style -j`nproc` build/ARM/gem5.opt
cd ..
# Build example.
sudo apt-get install gcc-arm-none-eabi
git clone https://github.com/tukl-msd/gem5.bare-metal
cd gem5.bare-metal
git checkout 6ad1069d4299b775b5491e9252739166bfac9bfe
cd Simple
make CROSS_COMPILE_DIR=/usr/bin
# Run example.
../../gem5/default/build/ARM/gem5.opt' \
../../gem5/configs/example/fs.py' \
--bare-metal \
--disk-image="$(pwd)/../common/fake.iso" \
--kernel="$(pwd)/main.elf" \
--machine-type=RealView_PBX \
--mem-size=256MB \
;</pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="how-we-got-some-baremetal-stuff-to-work"><a class="anchor" href="#how-we-got-some-baremetal-stuff-to-work"></a><a class="link" href="#how-we-got-some-baremetal-stuff-to-work">27.9. How we got some baremetal stuff to work</a></h3>
<div class="paragraph">
<p>It is nice when thing just work.</p>
</div>
<div class="paragraph">
<p>But you can also learn a thing or two from how I actually made them work in the first place.</p>
</div>
<div class="sect3">
<h4 id="find-the-uart-address"><a class="anchor" href="#find-the-uart-address"></a><a class="link" href="#find-the-uart-address">27.9.1. Find the UART address</a></h4>
<div class="paragraph">
<p>Enter the QEMU console:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>Ctrl-X C</pre>
</div>
</div>
<div class="paragraph">
<p>Then do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>info mtree</pre>
</div>
</div>
<div class="paragraph">
<p>And look for <code>pl011</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre> 0000000009000000-0000000009000fff (prio 0, i/o): pl011</pre>
</div>
</div>
<div class="paragraph">
<p>On gem5, it is easy to find it on the source. We are using the machine <code>RealView_PBX</code>, and a quick grep leads us to: <a href="https://github.com/gem5/gem5/blob/a27ce59a39ec8fa20a3c4e9fa53e9b3db1199e91/src/dev/arm/RealView.py#L615" class="bare">https://github.com/gem5/gem5/blob/a27ce59a39ec8fa20a3c4e9fa53e9b3db1199e91/src/dev/arm/RealView.py#L615</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre>class RealViewPBX(RealView):
uart = Pl011(pio_addr=0x10009000, int_num=44)</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="aarch64-baremetal-neon-setup"><a class="anchor" href="#aarch64-baremetal-neon-setup"></a><a class="link" href="#aarch64-baremetal-neon-setup">27.9.2. aarch64 baremetal NEON setup</a></h4>
<div class="paragraph">
<p>Inside <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/baremetal/lib/aarch64.S">baremetal/lib/aarch64.S</a> there is a chunk of code that enables floating point operations:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mov x1, 0x3 &lt;&lt; 20
msr cpacr_el1, x1
isb</pre>
</div>
</div>
<div class="paragraph">
<p>CPACR_EL1 is documented at <a href="#armarm8">ARMv8 architecture reference manual</a> D10.2.29 "CPACR_EL1, Architectural Feature Access Control Register".</p>
</div>
<div class="paragraph">
<p>Here we touch the CPACR_EL1.FPEN bits to 3, which enable floating point operations:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>11 This control does not cause any instructions to be trapped.</p>
</div>
</blockquote>
</div>
<div class="paragraph">
<p>We later also added an enable for the CPACR_EL1.ZEN bits, which are needed for <a href="#arm-sve">ARM SVE</a>.</p>
</div>
<div class="paragraph">
<p>Without CPACR_EL1.FPEN, the <code>printf</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>printf("got: %c\n", c);</pre>
</div>
</div>
<div class="paragraph">
<p>compiled to a:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>str q0, [sp, #80]</pre>
</div>
</div>
<div class="paragraph">
<p>which uses NEON registers, and goes into an exception loop.</p>
</div>
<div class="paragraph">
<p>It was a bit confusing because there was a previous <code>printf</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>printf("enter a character\n");</pre>
</div>
</div>
<div class="paragraph">
<p>which did not blow up because GCC compiles it into <code>puts</code> directly since it has no arguments, and that does not generate NEON instructions.</p>
</div>
<div class="paragraph">
<p>The last instructions ran was found with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>while(1)
stepi
end</pre>
</div>
</div>
<div class="paragraph">
<p>or by hacking the QEMU CLI to contain:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>-D log.log -d in_asm</pre>
</div>
</div>
<div class="paragraph">
<p>I could not find any previous NEON instruction executed so this led me to suspect that some NEON initialization was required:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="http://infocenter.arm.com/help/topic/com.arm.doc.dai0527a/DAI0527A_baremetal_boot_code_for_ARMv8_A_processors.pdf" class="bare">http://infocenter.arm.com/help/topic/com.arm.doc.dai0527a/DAI0527A_baremetal_boot_code_for_ARMv8_A_processors.pdf</a> "Bare-metal Boot Code for ARMv8-A Processors"</p>
</li>
<li>
<p><a href="https://community.arm.com/processors/f/discussions/5409/how-to-enable-neon-in-cortex-a8" class="bare">https://community.arm.com/processors/f/discussions/5409/how-to-enable-neon-in-cortex-a8</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/19231197/enable-neon-on-arm-cortex-a-series" class="bare">https://stackoverflow.com/questions/19231197/enable-neon-on-arm-cortex-a-series</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We then tried to copy the code from the "Bare-metal Boot Code for ARMv8-A Processors" document:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>// Disable trapping of accessing in EL3 and EL2.
MSR CPTR_EL3, XZR
MSR CPTR_EL3, XZR
// Disable access trapping in EL1 and EL0.
MOV X1, #(0x3 &lt;&lt; 20) // FPEN disables trapping to EL1.
MSR CPACR_EL1, X1
ISB</pre>
</div>
</div>
<div class="paragraph">
<p>but it entered an exception loop at <code>MSR CPTR_EL3, XZR</code>.</p>
</div>
<div class="paragraph">
<p>We then found out that QEMU <a href="#arm-exception-levels">starts in EL1</a>, and so we kept just the EL1 part, and it worked. Related:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/42824706/qemu-system-aarch64-entering-el1-when-emulating-a53-power-up" class="bare">https://stackoverflow.com/questions/42824706/qemu-system-aarch64-entering-el1-when-emulating-a53-power-up</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/37299524/neon-support-in-armv8-system-mode-qemu" class="bare">https://stackoverflow.com/questions/37299524/neon-support-in-armv8-system-mode-qemu</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="baremetal-tests"><a class="anchor" href="#baremetal-tests"></a><a class="link" href="#baremetal-tests">27.10. Baremetal tests</a></h3>
<div class="paragraph">
<p>Baremetal tests work exactly like <a href="#user-mode-tests">User mode tests</a>, except that you have to add the <code>--mode baremetal</code> option, for example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./test-executables --mode baremetal --arch aarch64</pre>
</div>
</div>
<div class="paragraph">
<p>In baremetal, we detect if tests failed by parsing logs for the <a href="#magic-failure-string">Magic failure string</a>.</p>
</div>
<div class="paragraph">
<p>See: <a href="#test-this-repo">Section 31.13, &#8220;Test this repo&#8221;</a> for more useful testing tips.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="android"><a class="anchor" href="#android"></a><a class="link" href="#android">28. Android</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Remember: Android AOSP is a huge undocumented piece of bloatware. It&#8217;s integration into this repo will likely never be super good.</p>
</div>
<div class="paragraph">
<p>Verbose setup description: <a href="https://stackoverflow.com/questions/1809774/how-to-compile-the-android-aosp-kernel-and-test-it-with-the-android-emulator/48310014#48310014" class="bare">https://stackoverflow.com/questions/1809774/how-to-compile-the-android-aosp-kernel-and-test-it-with-the-android-emulator/48310014#48310014</a></p>
</div>
<div class="paragraph">
<p>Download, build and run with the prebuilt AOSP QEMU emulator and the AOSP kernel:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-android \
--android-base-dir /path/to/your/hd \
--android-version 8.1.0_r60 \
download \
build \
;
./run-android \
--android-base-dir /path/to/your/hd \
--android-version 8.1.0_r60 \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-android">build-android</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/run-android">run-android</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>TODO how to hack the AOSP kernel, userland and emulator?</p>
</div>
<div class="paragraph">
<p>Other archs work as well as usual with <code>--arch</code> parameter. However, running in non-x86 is very slow due to the lack of KVM.</p>
</div>
<div class="paragraph">
<p>Tested on: <code>8.1.0_r60</code>.</p>
</div>
<div class="sect2">
<h3 id="android-image-structure"><a class="anchor" href="#android-image-structure"></a><a class="link" href="#android-image-structure">28.1. Android image structure</a></h3>
<div class="paragraph">
<p><a href="https://source.android.com/devices/bootloader/partitions-images" class="bare">https://source.android.com/devices/bootloader/partitions-images</a></p>
</div>
<div class="paragraph">
<p>The messy AOSP generates a ton of images instead of just one.</p>
</div>
<div class="paragraph">
<p>When the emulator launches, we can see them through QEMU <code>-drive</code> arguments:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>emulator: argv[21] = "-initrd"
emulator: argv[22] = "/data/aosp/8.1.0_r60/out/target/product/generic_x86_64/ramdisk.img"
emulator: argv[23] = "-drive"
emulator: argv[24] = "if=none,index=0,id=system,file=/path/to/aosp/8.1.0_r60/out/target/product/generic_x86_64/system-qemu.img,read-only"
emulator: argv[25] = "-device"
emulator: argv[26] = "virtio-blk-pci,drive=system,iothread=disk-iothread,modern-pio-notify"
emulator: argv[27] = "-drive"
emulator: argv[28] = "if=none,index=1,id=cache,file=/path/to/aosp/8.1.0_r60/out/target/product/generic_x86_64/cache.img.qcow2,overlap-check=none,cache=unsafe,l2-cache-size=1048576"
emulator: argv[29] = "-device"
emulator: argv[30] = "virtio-blk-pci,drive=cache,iothread=disk-iothread,modern-pio-notify"
emulator: argv[31] = "-drive"
emulator: argv[32] = "if=none,index=2,id=userdata,file=/path/to/aosp/8.1.0_r60/out/target/product/generic_x86_64/userdata-qemu.img.qcow2,overlap-check=none,cache=unsafe,l2-cache-size=1048576"
emulator: argv[33] = "-device"
emulator: argv[34] = "virtio-blk-pci,drive=userdata,iothread=disk-iothread,modern-pio-notify"
emulator: argv[35] = "-drive"
emulator: argv[36] = "if=none,index=3,id=encrypt,file=/path/to/aosp/8.1.0_r60/out/target/product/generic_x86_64/encryptionkey.img.qcow2,overlap-check=none,cache=unsafe,l2-cache-size=1048576"
emulator: argv[37] = "-device"
emulator: argv[38] = "virtio-blk-pci,drive=encrypt,iothread=disk-iothread,modern-pio-notify"
emulator: argv[39] = "-drive"
emulator: argv[40] = "if=none,index=4,id=vendor,file=/path/to/aosp/8.1.0_r60/out/target/product/generic_x86_64/vendor-qemu.img,read-only"
emulator: argv[41] = "-device"
emulator: argv[42] = "virtio-blk-pci,drive=vendor,iothread=disk-iothread,modern-pio-notify"</pre>
</div>
</div>
<div class="paragraph">
<p>The root directory is the <a href="#initrd">initrd</a> given on the QEMU CLI, which <code>/proc/mounts</code> reports at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rootfs on / type rootfs (ro,seclabel,size=886392k,nr_inodes=221598)</pre>
</div>
</div>
<div class="paragraph">
<p>This contains the <a href="#android-init">Android init</a>, which through <code>.rc</code> must be mounting mounts the drives int o the right places TODO find exact point.</p>
</div>
<div class="paragraph">
<p>The drive order is:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>system
cache
userdata
encryptionkey
vendor-qemu</pre>
</div>
</div>
<div class="paragraph">
<p>Then, on the terminal:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mount | grep vd</pre>
</div>
</div>
<div class="paragraph">
<p>gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/dev/block/vda1 on /system type ext4 (ro,seclabel,relatime,data=ordered)
/dev/block/vde1 on /vendor type ext4 (ro,seclabel,relatime,data=ordered)
/dev/block/vdb on /cache type ext4 (rw,seclabel,nosuid,nodev,noatime,errors=panic,data=ordered)</pre>
</div>
</div>
<div class="paragraph">
<p>and we see that the order of <code>vda</code>, <code>vdb</code>, etc. matches that in which <code>-drive</code> were given to QEMU.</p>
</div>
<div class="paragraph">
<p>Tested on: <code>8.1.0_r60</code>.</p>
</div>
<div class="sect3">
<h4 id="android-images-read-only"><a class="anchor" href="#android-images-read-only"></a><a class="link" href="#android-images-read-only">28.1.1. Android images read-only</a></h4>
<div class="paragraph">
<p>From <code>mount</code>, we can see that some of the mounted images are <code>ro</code>.</p>
</div>
<div class="paragraph">
<p>Basically, every image that was given to QEMU as qcow2 is writable, and that qcow2 is an overlay over the actual original image.</p>
</div>
<div class="paragraph">
<p>In order to make <code>/system</code> and <code>/vendor</code> writable by using qcow2 for them as well, we must use the <code>-writable-system</code> option:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-android -- -writable-system</pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://android.stackexchange.com/questions/110927/how-to-mount-system-rewritable-or-read-only-rw-ro/207200#207200" class="bare">https://android.stackexchange.com/questions/110927/how-to-mount-system-rewritable-or-read-only-rw-ro/207200#207200</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/13089694/adb-remount-permission-denied-but-able-to-access-super-user-in-shell-android/43163693#43163693" class="bare">https://stackoverflow.com/questions/13089694/adb-remount-permission-denied-but-able-to-access-super-user-in-shell-android/43163693#43163693</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>then:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>su
mount -o rw,remount /system
date &gt;/system/a</pre>
</div>
</div>
<div class="paragraph">
<p>Now reboot, and relaunch with <code>-writable-system</code> once again to pick up the modified qcow2 images:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-android -- -writable-system</pre>
</div>
</div>
<div class="paragraph">
<p>and the newly created file is still there:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>date &gt;/system/a</pre>
</div>
</div>
<div class="paragraph">
<p><code>/system</code> and <code>/vendor</code> can be nuked quickly with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-android --extra-args snod
./build-android --extra-args vnod</pre>
</div>
</div>
<div class="paragraph">
<p>as mentioned at: <a href="https://stackoverflow.com/questions/29023406/how-to-just-build-android-system-image" class="bare">https://stackoverflow.com/questions/29023406/how-to-just-build-android-system-image</a> and on:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-android --extra-args help</pre>
</div>
</div>
<div class="paragraph">
<p>Tested on: <code>8.1.0_r60</code>.</p>
</div>
</div>
<div class="sect3">
<h4 id="android-data-partition"><a class="anchor" href="#android-data-partition"></a><a class="link" href="#android-data-partition">28.1.2. Android /data partition</a></h4>
<div class="paragraph">
<p>When I install an app like F-Droid, it goes under <code>/data</code> according to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>find / -iname '*fdroid*'</pre>
</div>
</div>
<div class="paragraph">
<p>and it <a href="#disk-persistency">persists across boots</a>.</p>
</div>
<div class="paragraph">
<p><code>/data</code> is behind a RW LVM device:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>/dev/block/dm-0 on /data type ext4 (rw,seclabel,nosuid,nodev,noatime,errors=panic,data=ordered)</pre>
</div>
</div>
<div class="paragraph">
<p>but TODO I can&#8217;t find where it comes from since I don&#8217;t have the CLI tools mentioned at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://superuser.com/questions/131519/what-is-this-dm-0-device" class="bare">https://superuser.com/questions/131519/what-is-this-dm-0-device</a></p>
</li>
<li>
<p><a href="https://unix.stackexchange.com/questions/185057/where-does-lvm-store-its-configuration" class="bare">https://unix.stackexchange.com/questions/185057/where-does-lvm-store-its-configuration</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>However, by looking at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-android -- -help</pre>
</div>
</div>
<div class="paragraph">
<p>we see:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>-data &lt;file&gt; data image (default &lt;datadir&gt;/userdata-qemu.img</pre>
</div>
</div>
<div class="paragraph">
<p>which confirms the suspicion that this data goes in <code>userdata-qemu.img</code>.</p>
</div>
<div class="paragraph">
<p>To reset images to their original state, just remove the qcow2 overlay and regenerate it: <a href="https://stackoverflow.com/questions/54446680/how-to-reset-the-userdata-image-when-building-android-aosp-and-running-it-on-the" class="bare">https://stackoverflow.com/questions/54446680/how-to-reset-the-userdata-image-when-building-android-aosp-and-running-it-on-the</a></p>
</div>
<div class="paragraph">
<p>Tested on: <code>8.1.0_r60</code>.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="install-android-apps"><a class="anchor" href="#install-android-apps"></a><a class="link" href="#install-android-apps">28.2. Install Android apps</a></h3>
<div class="paragraph">
<p>I don&#8217;t know how to download files from the web on Vanilla android, the default browser does not download anything, and there is no <code>wget</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://android.stackexchange.com/questions/6984/how-to-download-files-from-the-web-in-the-android-browser" class="bare">https://android.stackexchange.com/questions/6984/how-to-download-files-from-the-web-in-the-android-browser</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/26775079/wget-in-android-terminal" class="bare">https://stackoverflow.com/questions/26775079/wget-in-android-terminal</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Installing with <code>adb install</code> does however work: <a href="https://stackoverflow.com/questions/7076240/install-an-apk-file-from-command-prompt" class="bare">https://stackoverflow.com/questions/7076240/install-an-apk-file-from-command-prompt</a></p>
</div>
<div class="paragraph">
<p><a href="https://f-droid.org">F-Droid</a> installed fine like that, however it does not have permission to install apps: <a href="https://www.maketecheasier.com/install-apps-from-unknown-sources-android/" class="bare">https://www.maketecheasier.com/install-apps-from-unknown-sources-android/</a></p>
</div>
<div class="paragraph">
<p>And the <code>Settings</code> app crashes so I can&#8217;t change it, logcat contains:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>No service published for: wifip2p</pre>
</div>
</div>
<div class="paragraph">
<p>which is mentioned at: <a href="https://stackoverflow.com/questions/47839955/android-8-settings-app-crashes-on-emulator-with-clean-aosp-build" class="bare">https://stackoverflow.com/questions/47839955/android-8-settings-app-crashes-on-emulator-with-clean-aosp-build</a></p>
</div>
<div class="paragraph">
<p>We also tried to enable it from the command line with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>settings put secure install_non_market_apps 1</pre>
</div>
</div>
<div class="paragraph">
<p>as mentioned at: <a href="https://android.stackexchange.com/questions/77280/allow-unknown-sources-from-terminal-without-going-to-settings-app" class="bare">https://android.stackexchange.com/questions/77280/allow-unknown-sources-from-terminal-without-going-to-settings-app</a> but it didn&#8217;t work either.</p>
</div>
<div class="paragraph">
<p>No person alive seems to know how to pre-install apps on AOSP: <a href="https://stackoverflow.com/questions/6249458/pre-installing-android-application" class="bare">https://stackoverflow.com/questions/6249458/pre-installing-android-application</a></p>
</div>
<div class="paragraph">
<p>Tested on: <code>8.1.0_r60</code>.</p>
</div>
</div>
<div class="sect2">
<h3 id="android-init"><a class="anchor" href="#android-init"></a><a class="link" href="#android-init">28.3. Android init</a></h3>
<div class="paragraph">
<p>For Linux in general, see: <a href="#init">Section 6, &#8220;init&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>The <code>/init</code> executable interprets the <code>/init.rc</code> files, which is in a custom Android init system language: <a href="https://android.googlesource.com/platform/system/core/+/ee0e63f71d90537bb0570e77aa8a699cc222cfaf/init/README.md" class="bare">https://android.googlesource.com/platform/system/core/+/ee0e63f71d90537bb0570e77aa8a699cc222cfaf/init/README.md</a></p>
</div>
<div class="paragraph">
<p>The top of that file then sources other <code>.rc</code> files present on the root directory:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>import /init.environ.rc
import /init.usb.rc
import /init.${ro.hardware}.rc
import /vendor/etc/init/hw/init.${ro.hardware}.rc
import /init.usb.configfs.rc
import /init.${ro.zygote}.rc</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: how is <code>ro.hardware</code> determined? <a href="https://stackoverflow.com/questions/20572781/android-boot-where-is-the-init-hardware-rc-read-in-init-c-where-are-servic" class="bare">https://stackoverflow.com/questions/20572781/android-boot-where-is-the-init-hardware-rc-read-in-init-c-where-are-servic</a> It is a system property and can be obtained with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>getprop ro.hardware</pre>
</div>
</div>
<div class="paragraph">
<p>This gives:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ranchu</pre>
</div>
</div>
<div class="paragraph">
<p>which is the codename for the QEMU virtual platform we are running on: <a href="https://www.oreilly.com/library/view/android-system-programming/9781787125360/9736a97c-cd09-40c3-b14d-955717648302.xhtml" class="bare">https://www.oreilly.com/library/view/android-system-programming/9781787125360/9736a97c-cd09-40c3-b14d-955717648302.xhtml</a></p>
</div>
<div class="paragraph">
<p>TODO: is it possible to add a custom <code>.rc</code> file without modifying the initrd that <a href="#android-image-structure">gets mounted on root</a>? <a href="https://stackoverflow.com/questions/9768103/make-persistent-changes-to-init-rc" class="bare">https://stackoverflow.com/questions/9768103/make-persistent-changes-to-init-rc</a></p>
</div>
<div class="paragraph">
<p>Tested on: <code>8.1.0_r60</code>.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="benchmark-this-repo"><a class="anchor" href="#benchmark-this-repo"></a><a class="link" href="#benchmark-this-repo">29. Benchmark this repo</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>TODO: didn&#8217;t fully port during refactor after 3b0a343647bed577586989fb702b760bd280844a. Reimplementing should not be hard.</p>
</div>
<div class="paragraph">
<p>In this section document how benchmark builds and runs of this repo, and how to investigate what the bottleneck is.</p>
</div>
<div class="paragraph">
<p>Ideally, we should setup an automated build server that benchmarks those things continuously for us, but our <a href="#travis">Travis</a> attempt failed.</p>
</div>
<div class="paragraph">
<p>So currently, we are running benchmarks manually when it seems reasonable and uploading them to: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat-regression" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat-regression</a></p>
</div>
<div class="paragraph">
<p>All benchmarks were run on the <a href="#p51">P51</a> machine, unless stated otherwise.</p>
</div>
<div class="paragraph">
<p>Run all benchmarks and upload the results:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd ..
git clone https://github.com/cirosantilli/linux-kernel-module-cheat-regression
cd -
./bench-all -A</pre>
</div>
</div>
<div class="sect2">
<h3 id="continuous-integraion"><a class="anchor" href="#continuous-integraion"></a><a class="link" href="#continuous-integraion">29.1. Continuous integraion</a></h3>
<div class="paragraph">
<p>We have exploreed a few Continuous integration solutions.</p>
</div>
<div class="paragraph">
<p>We haven&#8217;t setup any of them yet.</p>
</div>
<div class="sect3">
<h4 id="travis"><a class="anchor" href="#travis"></a><a class="link" href="#travis">29.1.1. Travis</a></h4>
<div class="paragraph">
<p>We tried to automate it on Travis with <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/.travis.yml">.travis.yml</a> but it hits the current 50 minute job timeout: <a href="https://travis-ci.org/cirosantilli/linux-kernel-module-cheat/builds/296454523" class="bare">https://travis-ci.org/cirosantilli/linux-kernel-module-cheat/builds/296454523</a> And I bet it would likely hit a disk maxout either way if it went on.</p>
</div>
</div>
<div class="sect3">
<h4 id="circleci"><a class="anchor" href="#circleci"></a><a class="link" href="#circleci">29.1.2. CircleCI</a></h4>
<div class="paragraph">
<p>This setup sucessfully built gem5 on every commit: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/.circleci/config.yml">.circleci/config.yml</a></p>
</div>
<div class="paragraph">
<p>Enabling it is however blocked on: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/issues/79" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/issues/79</a> so we disabled the builds on the web UI.</p>
</div>
<div class="paragraph">
<p>If that ever gets done, we will also need to:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>convert this to a nightly with a workflow, to save server resources: <a href="https://circleci.com/docs/2.0/configuration-reference/#triggers" class="bare">https://circleci.com/docs/2.0/configuration-reference/#triggers</a></p>
</li>
<li>
<p>download the prebuilt disk images and enable caches to save the images across runs</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>A build took about 1 hour of a core, and the free tier allows for 1000 minutes per month: <a href="https://circleci.com/pricing/" class="bare">https://circleci.com/pricing/</a> so about 17 hours. The cheapest non-free setup seems to be 50 dollars per month gets us infinite build minutes per month and 2 containers, so we could scale things to run in under 24 hours.</p>
</div>
<div class="paragraph">
<p>There is no result reporting web UI however&#8230;&#8203; but neither does GitLab CI: <a href="https://gitlab.com/gitlab-org/gitlab-ce/issues/17081" class="bare">https://gitlab.com/gitlab-org/gitlab-ce/issues/17081</a></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="benchmark-this-repo-benchmarks"><a class="anchor" href="#benchmark-this-repo-benchmarks"></a><a class="link" href="#benchmark-this-repo-benchmarks">29.2. Benchmark this repo benchmarks</a></h3>
<div class="sect3">
<h4 id="benchmark-linux-kernel-boot"><a class="anchor" href="#benchmark-linux-kernel-boot"></a><a class="link" href="#benchmark-linux-kernel-boot">29.2.1. Benchmark Linux kernel boot</a></h4>
<div class="paragraph">
<p>Run all kernel boot benchmarks for one arch:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-test-boot --size 3 &amp;&amp; ./test-boot --all-archs --all-emulators --size 3
cat "$(./getvar test_boot_benchmark_file)"</pre>
</div>
</div>
<div class="paragraph">
<p>Sample results at 8fb9db39316d43a6dbd571e04dd46ae73915027f:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cmd ./run --arch x86_64 --eval './linux/poweroff.out'
time 8.25
exit_status 0
cmd ./run --arch x86_64 --eval './linux/poweroff.out' --kvm
time 1.22
exit_status 0
cmd ./run --arch x86_64 --eval './linux/poweroff.out' --trace exec_tb
time 8.83
exit_status 0
instructions 2244297
cmd ./run --arch x86_64 --eval 'm5 exit' --emulator gem5
time 213.39
exit_status 0
instructions 318486337
cmd ./run --arch arm --eval './linux/poweroff.out'
time 6.62
exit_status 0
cmd ./run --arch arm --eval './linux/poweroff.out' --trace exec_tb
time 6.90
exit_status 0
instructions 776374
cmd ./run --arch arm --eval 'm5 exit' --emulator gem5
time 118.46
exit_status 0
instructions 153023392
cmd ./run --arch arm --eval 'm5 exit' --emulator gem5 -- --cpu-type=HPI --caches --l2cache --l1d_size=1024kB --l1i_size=1024kB --l2_size=1024kB --l3_size=1024kB
time 2250.40
exit_status 0
instructions 151981914
cmd ./run --arch aarch64 --eval './linux/poweroff.out'
time 4.94
exit_status 0
cmd ./run --arch aarch64 --eval './linux/poweroff.out' --trace exec_tb
time 5.04
exit_status 0
instructions 233162
cmd ./run --arch aarch64 --eval 'm5 exit' --emulator gem5
time 70.89
exit_status 0
instructions 124346081
cmd ./run --arch aarch64 --eval 'm5 exit' --emulator gem5 -- --cpu-type=HPI --caches --l2cache --l1d_size=1024kB --l1i_size=1024kB --l2_size=1024kB --l3_size=1024kB
time 381.86
exit_status 0
instructions 124564620
cmd ./run --arch aarch64 --eval 'm5 exit' --emulator gem5 --gem5-build-type fast
time 58.00
exit_status 0
instructions 124346081
cmd ./run --arch aarch64 --eval 'm5 exit' --emulator gem5 --gem5-build-type debug
time 1022.03
exit_status 0
instructions 124346081</pre>
</div>
</div>
<div class="paragraph">
<p>TODO: aarch64 gem5 and QEMU use the same kernel, so why is the gem5 instruction count so much much higher?</p>
</div>
<div class="sect4">
<h5 id="gem5-arm-hpi-boot-takes-much-longer-than-aarch64"><a class="anchor" href="#gem5-arm-hpi-boot-takes-much-longer-than-aarch64"></a><a class="link" href="#gem5-arm-hpi-boot-takes-much-longer-than-aarch64">29.2.1.1. gem5 arm HPI boot takes much longer than aarch64</a></h5>
<div class="paragraph">
<p>TODO 62f6870e4e0b384c4bd2d514116247e81b241251 takes 33 minutes to finish at 62f6870e4e0b384c4bd2d514116247e81b241251:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cmd ./run --arch arm --eval 'm5 exit' --emulator gem5 -- --caches --cpu-type=HPI</pre>
</div>
</div>
<div class="paragraph">
<p>while aarch64 only 7 minutes.</p>
</div>
<div class="paragraph">
<p>I had previously documented on README 10 minutes at: 2eff007f7c3458be240c673c32bb33892a45d3a0 found with <code>git log</code> search for <code>10 minutes</code>. But then I checked out there, run it, and kernel panics before any messages come out. Lol?</p>
</div>
<div class="paragraph">
<p>Logs of the runs can be found at: <a href="https://github.com/cirosantilli-work/gem5-issues/tree/0df13e862b50ae20fcd10bae1a9a53e55d01caac/arm-hpi-slow" class="bare">https://github.com/cirosantilli-work/gem5-issues/tree/0df13e862b50ae20fcd10bae1a9a53e55d01caac/arm-hpi-slow</a></p>
</div>
<div class="paragraph">
<p>The cycle count is higher for <code>arm</code>, 350M vs 250M for <code>aarch64</code>, not nowhere near the 5x runtime time increase.</p>
</div>
<div class="paragraph">
<p>A quick look at the boot logs show that they are basically identical in structure: the same operations appear more ore less on both, and there isn&#8217;t one specific huge time pit in arm: it is just that every individual operation seems to be taking a lot longer.</p>
</div>
</div>
<div class="sect4">
<h5 id="gem5-x86_64-derivo3cpu-boot-panics"><a class="anchor" href="#gem5-x86_64-derivo3cpu-boot-panics"></a><a class="link" href="#gem5-x86_64-derivo3cpu-boot-panics">29.2.1.2. gem5 x86_64 DerivO3CPU boot panics</a></h5>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli-work/gem5-issues/issues/2" class="bare">https://github.com/cirosantilli-work/gem5-issues/issues/2</a></p>
</div>
<div class="literalblock">
<div class="content">
<pre>Kernel panic - not syncing: Attempted to kill the idle task!</pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="benchmark-builds"><a class="anchor" href="#benchmark-builds"></a><a class="link" href="#benchmark-builds">29.2.2. Benchmark builds</a></h4>
<div class="paragraph">
<p>The build times are calculated after doing <code>./configure</code> and <a href="https://buildroot.org/downloads/manual/manual.html#_offline_builds"><code>make source</code></a>, which downloads the sources, and basically benchmarks the <a href="#benchmark-internets">Internet</a>.</p>
</div>
<div class="paragraph">
<p>Sample build time at 2c12b21b304178a81c9912817b782ead0286d282: 28 minutes, 15 with full ccache hits. Breakdown: 19% GCC, 13% Linux kernel, 7% uclibc, 6% host-python, 5% host-qemu, 5% host-gdb, 2% host-binutils</p>
</div>
<div class="paragraph">
<p>Buildroot automatically stores build timestamps as milliseconds since Epoch. Convert to minutes:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>awk -F: 'NR==1{start=$1}; END{print ($1 - start)/(60000.0)}' "$(./getvar buildroot_build_build_dir)/build-time.log"</pre>
</div>
</div>
<div class="paragraph">
<p>Or to conveniently do a clean build without affecting your current one:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./bench-all -b
cat ../linux-kernel-module-cheat-regression/*/build-time.log</pre>
</div>
</div>
<div class="sect4">
<h5 id="find-which-buildroot-packages-are-making-the-build-slow-and-big"><a class="anchor" href="#find-which-buildroot-packages-are-making-the-build-slow-and-big"></a><a class="link" href="#find-which-buildroot-packages-are-making-the-build-slow-and-big">29.2.2.1. Find which Buildroot packages are making the build slow and big</a></h5>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot -- graph-build graph-size graph-depends
cd "$(./getvar buildroot_build_dir)/graphs"
xdg-open build.pie-packages.pdf
xdg-open graph-depends.pdf
xdg-open graph-size.pdf</pre>
</div>
</div>
<div class="sect5">
<h6 id="prebuilt-toolchain"><a class="anchor" href="#prebuilt-toolchain"></a><a class="link" href="#prebuilt-toolchain">29.2.2.1.1. Buildroot use prebuilt host toolchain</a></h6>
<div class="paragraph">
<p>The biggest build time hog is always GCC, and it does not look like we can use a precompiled one: <a href="https://stackoverflow.com/questions/10833672/buildroot-environment-with-host-toolchain" class="bare">https://stackoverflow.com/questions/10833672/buildroot-environment-with-host-toolchain</a></p>
</div>
</div>
</div>
<div class="sect4">
<h5 id="benchmark-buildroot-build-baseline"><a class="anchor" href="#benchmark-buildroot-build-baseline"></a><a class="link" href="#benchmark-buildroot-build-baseline">29.2.2.2. Benchmark Buildroot build baseline</a></h5>
<div class="paragraph">
<p>This is the minimal build we could expect to get away with.</p>
</div>
<div class="paragraph">
<p>We will run this whenever the Buildroot submodule is updated.</p>
</div>
<div class="paragraph">
<p>On the upstream Buildroot repo at :</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./bench-all -B</pre>
</div>
</div>
<div class="paragraph">
<p>Sample time on 2017.08: 11 minutes, 7 with full ccache hits. Breakdown: 47% GCC, 15% Linux kernel, 9% uclibc, 5% host-binutils. Conclusions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>we have bloated our kernel build 3x with all those delicious features :-)</p>
</li>
<li>
<p>GCC time increased 1.5x by our bloat, but its percentage of the total was greatly reduced, due to new packages being introduced.</p>
<div class="paragraph">
<p><code>make graph-depends</code> shows that most new dependencies come from QEMU and GDB, which we can&#8217;t get rid of anyways.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>A quick look at the system monitor reveals that the build switches between times when:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>CPUs are at a max, memory is fine. So we must be CPU / memory speed bound. I bet that this happens during heavy compilation.</p>
</li>
<li>
<p>CPUs are not at a max, and memory is fine. So we are likely disk bound. I bet that this happens during configuration steps.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This is consistent with the fact that ccache reduces the build time only partially, since ccache should only overcome the CPU bound compilation steps, but not the disk bound ones.</p>
</div>
<div class="paragraph">
<p>The instructions counts varied very little between the baseline and LKMC, so runtime overhead is not a big deal apparently.</p>
</div>
<div class="paragraph">
<p>Size:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>bzImage</code>: 4.4M</p>
</li>
<li>
<p><code>rootfs.cpio</code>: 1.6M</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Zipped: 4.9M, <code>rootfs.cpio</code> deflates 50%, <code>bzImage</code> almost nothing.</p>
</div>
</div>
<div class="sect4">
<h5 id="benchmark-gem5-build"><a class="anchor" href="#benchmark-gem5-build"></a><a class="link" href="#benchmark-gem5-build">29.2.2.3. Benchmark gem5 build</a></h5>
<div class="paragraph">
<p>How long it takes to build gem5 itself.</p>
</div>
<div class="paragraph">
<p>We will update this whenever the gem5 submodule is updated.</p>
</div>
<div class="paragraph">
<p>Sample results at gem5 2a9573f5942b5416fb0570cf5cb6cdecba733392: 10 to 12 minutes.</p>
</div>
<div class="paragraph">
<p>Get results with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./bench-all --emulator gem5
tail -n+1 ../linux-kernel-module-cheat-regression/*/gem5-bench-build-*.txt</pre>
</div>
</div>
<div class="sect5">
<h6 id="benchmark-gem5-single-file-change-rebuild-time"><a class="anchor" href="#benchmark-gem5-single-file-change-rebuild-time"></a><a class="link" href="#benchmark-gem5-single-file-change-rebuild-time">29.2.2.3.1. Benchmark gem5 single file change rebuild time</a></h6>
<div class="paragraph">
<p>This is the critical development parameter, and is dominated by the link time of huge binaries.</p>
</div>
<div class="paragraph">
<p>In order to benchmark it better, make a comment only change to:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>vim submodules/gem5/src/sim/main.cc</pre>
</div>
</div>
<div class="paragraph">
<p>then rebuild with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-gem5 --arch aarch64 --verbose</pre>
</div>
</div>
<div class="paragraph">
<p>and then copy the link command to a separate Bash file. Then you can time and modify it easily.</p>
</div>
<div class="paragraph">
<p>Some approximate reference values on <a href="#p51">P51</a>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>opt</code></p>
<div class="ulist">
<ul>
<li>
<p>unmodified: 10 seconds</p>
</li>
<li>
<p>hack with <code>-fuse-ld=gold</code>: 6 seconds. Huge improvement!</p>
</li>
</ul>
</div>
</li>
<li>
<p><code>debug</code></p>
<div class="ulist">
<ul>
<li>
<p>unmodified: 14 seconds. Why two times slower than unmodified?</p>
</li>
<li>
<p>hack with <code>-fuse-ld=gold</code>: <code>internal error in read_cie, at ../../gold/ehframe.cc:919</code> on Ubuntu 18.04 all GCC. TODO report.</p>
</li>
</ul>
</div>
</li>
<li>
<p><code>fast</code></p>
<div class="ulist">
<ul>
<li>
<p><code>--force-lto</code>: 1 minute. Slower as expected, since more optimizations are done at link time. <code>--force-lto</code> is only used for <code>fast</code>, and it adds <code>-flto</code> to the build.</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>ramfs made no difference, the kernel must be caching files in memory very efficiently already.</p>
</div>
<div class="paragraph">
<p>Tested at: d4b3e064adeeace3c3e7d106801f95c14637c12f + 1.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="benchmark-machines"><a class="anchor" href="#benchmark-machines"></a><a class="link" href="#benchmark-machines">29.3. Benchmark machines</a></h3>
<div class="sect3">
<h4 id="p51"><a class="anchor" href="#p51"></a><a class="link" href="#p51">29.3.1. P51</a></h4>
<div class="paragraph">
<p>Lenovo ThinkPad <a href="https://www3.lenovo.com/gb/en/laptops/thinkpad/p-series/P51/p/22TP2WPWP51">P51 laptop</a>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>2500 USD in 2018 (high end)</p>
</li>
<li>
<p>Intel Core i7-7820HQ Processor (8MB Cache, up to 3.90GHz) (4 cores 8 threads)</p>
</li>
<li>
<p>32GB(16+16) DDR4 2400MHz SODIMM</p>
</li>
<li>
<p>512GB SSD PCIe TLC OPAL2</p>
</li>
<li>
<p>NVIDIA Quadro M1200 Mobile, latest Ubuntu supported proprietary driver</p>
</li>
<li>
<p>Latest Ubuntu</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="benchmark-internets"><a class="anchor" href="#benchmark-internets"></a><a class="link" href="#benchmark-internets">29.4. Benchmark Internets</a></h3>
<div class="sect3">
<h4 id="38mbps-internet"><a class="anchor" href="#38mbps-internet"></a><a class="link" href="#38mbps-internet">29.4.1. 38Mbps internet</a></h4>
<div class="paragraph">
<p>2c12b21b304178a81c9912817b782ead0286d282:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>shallow clone of all submodules: 4 minutes.</p>
</li>
<li>
<p><code>make source</code>: 2 minutes</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Google M-lab speed test: 36.4Mbps</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="benchmark-this-repo-bibliography"><a class="anchor" href="#benchmark-this-repo-bibliography"></a><a class="link" href="#benchmark-this-repo-bibliography">29.5. Benchmark this repo bibliography</a></h3>
<div class="paragraph">
<p>gem5:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://www.mail-archive.com/gem5-users@gem5.org/msg15262.html" class="bare">https://www.mail-archive.com/gem5-users@gem5.org/msg15262.html</a> which parts of the gem5 code make it slow</p>
</li>
<li>
<p>what are the minimum system requirements:</p>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/47997565/gem5-system-requirements-for-decent-performance/48941793#48941793" class="bare">https://stackoverflow.com/questions/47997565/gem5-system-requirements-for-decent-performance/48941793#48941793</a></p>
</li>
<li>
<p><a href="https://github.com/gem5/gem5/issues/25" class="bare">https://github.com/gem5/gem5/issues/25</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="xephyr"><a class="anchor" href="#xephyr"></a><a class="link" href="#xephyr">30. Xephyr</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Xephyr is an RTOS that has <a href="#posix">POSIX</a> support. I think it works much like our <a href="#baremetal-setup">Baremetal setup</a> which uses Newlib and generates individual ELF files that contain both our C program&#8217;s code, and the Xephyr libraries.</p>
</div>
<div class="paragraph">
<p>TODO get a hello world working, and then consider further integration in this repo, e.g. being able to run all C userland content on it.</p>
</div>
<div class="paragraph">
<p>TODO: Cortex-A CPUs are not currently supported, there are some <code>qemu_cortex_m0</code> boards, but can&#8217;t find a QEMU Cortex-A. There is an x86_64 qemu board, but we don&#8217;t currently have an <a href="#about-the-baremetal-setup">x86 baremetal toolchain</a>. For this reason, we won&#8217;t touch this further for now.</p>
</div>
<div class="paragraph">
<p>However, unlike Newlib, Xephyr must be setting up a simple pre-main runtime to be able to handle threads.</p>
</div>
<div class="paragraph">
<p>Failed attempt:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># https://askubuntu.com/questions/952429/is-there-a-good-ppa-for-cmake-backports
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2&gt;/dev/null | sudo apt-key add -
sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic-rc main'
sudo apt-get update
sudo apt-get install cmake
git clone https://github.com/zephyrproject-rtos/zephyr
pip3 install --user -U west packaging
cd zephyr
git checkout v1.14.1
west init zephyrproject
west update
export ZEPHYR_TOOLCHAIN_VARIANT=xtools
export XTOOLS_TOOLCHAIN_PATH="$(pwd)/out/crosstool-ng/build/default/install/aarch64/bin/"
source zephyr-env.sh
west build -b qemu_aarch64 samples/hello_world</pre>
</div>
</div>
<div class="paragraph">
<p>The build system of that project is a bit excessive / wonky. You need an edge CMake not present in Ubuntu 18.04, which I don&#8217;t want to install right now, and it uses the weird custom <code>west</code> build tool frontend.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="about-this-repo"><a class="anchor" href="#about-this-repo"></a><a class="link" href="#about-this-repo">31. About this repo</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="supported-hosts"><a class="anchor" href="#supported-hosts"></a><a class="link" href="#supported-hosts">31.1. Supported hosts</a></h3>
<div class="paragraph">
<p>The host requirements depend a lot on which examples you want to run.</p>
</div>
<div class="paragraph">
<p>Some setups of this repository are very portable, notably setups under <a href="#userland-setup">Userland setup</a>, e.g. <a href="#c">C</a>, and will likely work on any host system with minimal modification.</p>
</div>
<div class="paragraph">
<p>The least portable setups are those that require Buildroot and crosstool-NG.</p>
</div>
<div class="paragraph">
<p>We tend to test this repo the most on the latest Ubuntu and on the latest <a href="https://askubuntu.com/questions/16366/whats-the-difference-between-a-long-term-support-release-and-a-normal-release">Ubuntu LTS</a>.</p>
</div>
<div class="paragraph">
<p>For other Linux distros, everything will likely also just work if you install the analogous required packages for your distro.</p>
</div>
<div class="paragraph">
<p>Find out the packages that we install with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --download-dependencies --dry-run &lt;some-target&gt; | less</pre>
</div>
</div>
<div class="paragraph">
<p>and then just look for the <code>apt-get</code> commands shown on the log.</p>
</div>
<div class="paragraph">
<p>After installing the missing packages for your distro, do the build with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --download-dependencies --no-apt &lt;some-target&gt;</pre>
</div>
</div>
<div class="paragraph">
<p>which does everything as normal, except that it skips any <code>apt</code> commands.</p>
</div>
<div class="paragraph">
<p>If something does not work however, <a href="#docker">Docker host setup</a> should just work on any Linux distro.</p>
</div>
<div class="paragraph">
<p>Native Windows is unlikely feasible for Buildroot setups because Buildroot is a huge set of GNU Make scripts + host tools, just do everything from inside an Ubuntu in VirtualBox instance in that case.</p>
</div>
<div class="paragraph">
<p>Pull requests with ports to new host systems and reports on issues that things work or don&#8217;t work on your host are welcome.</p>
</div>
</div>
<div class="sect2">
<h3 id="common-build-issues"><a class="anchor" href="#common-build-issues"></a><a class="link" href="#common-build-issues">31.2. Common build issues</a></h3>
<div class="sect3">
<h4 id="put-source-uris-in-sources"><a class="anchor" href="#put-source-uris-in-sources"></a><a class="link" href="#put-source-uris-in-sources">31.2.1. You must put some 'source' URIs in your sources.list</a></h4>
<div class="paragraph">
<p>If <code>./build --download-dependencies</code> fails with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>E: You must put some 'source' URIs in your sources.list</pre>
</div>
</div>
<div class="paragraph">
<p>see this: <a href="https://askubuntu.com/questions/496549/error-you-must-put-some-source-uris-in-your-sources-list/857433#857433" class="bare">https://askubuntu.com/questions/496549/error-you-must-put-some-source-uris-in-your-sources-list/857433#857433</a> I don&#8217;t know how to automate this step. Why, Ubuntu, why.</p>
</div>
</div>
<div class="sect3">
<h4 id="build-from-downloaded-source-zip-files"><a class="anchor" href="#build-from-downloaded-source-zip-files"></a><a class="link" href="#build-from-downloaded-source-zip-files">31.2.2. Build from downloaded source zip files</a></h4>
<div class="paragraph">
<p>It does not work if you just download the <code>.zip</code> with the sources for this repository from GitHub because we use <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/.gitmodules">Git submodules</a>, you must clone this repo.</p>
</div>
<div class="paragraph">
<p><code>./build --download-dependencies</code> then fetches only the required submodules for you.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="run-command-after-boot"><a class="anchor" href="#run-command-after-boot"></a><a class="link" href="#run-command-after-boot">31.3. Run command after boot</a></h3>
<div class="paragraph">
<p>If you just want to run a command after boot ends without thinking much about it, just use the <code>--eval-after</code> option, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after 'echo hello'</pre>
</div>
</div>
<div class="paragraph">
<p>This option passes the command to our init scripts through <a href="#kernel-command-line-parameters">Kernel command line parameters</a>, and uses a few clever tricks along the way to make it just work.</p>
</div>
<div class="paragraph">
<p>See <a href="#init">init</a> for the gory details.</p>
</div>
</div>
<div class="sect2">
<h3 id="default-command-line-arguments"><a class="anchor" href="#default-command-line-arguments"></a><a class="link" href="#default-command-line-arguments">31.4. Default command line arguments</a></h3>
<div class="paragraph">
<p>It gets annoying to retype <code>--arch aarch64</code> for every single command, or to remember <code>--config</code> setups.</p>
</div>
<div class="paragraph">
<p>So simplify that, do:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cp config.py data/</pre>
</div>
</div>
<div class="paragraph">
<p>and then edit the <code>data/config</code> file to your needs.</p>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/config.py">config.py</a></p>
</div>
<div class="paragraph">
<p>You can also choose a different configuration file explicitly with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --config data/config2.py</pre>
</div>
</div>
<div class="paragraph">
<p>Almost all options names are automatically deduced from their command line <code>--help</code> name: just replace <code>-</code> with <code>_</code>.</p>
</div>
<div class="paragraph">
<p>More precisely, we use the <code>dest=</code> value of Python&#8217;s <a href="https://docs.python.org/3/library/argparse.html">argparse module</a>.</p>
</div>
<div class="paragraph">
<p>To get a list of all global options that you can use, try:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./getvar --type input</pre>
</div>
</div>
<div class="paragraph">
<p>but note that this does not include script specific options.</p>
</div>
</div>
<div class="sect2">
<h3 id="documentation"><a class="anchor" href="#documentation"></a><a class="link" href="#documentation">31.5. Documentation</a></h3>
<div class="paragraph">
<p>To learn how to build the documentation see: <a href="#build-the-documentation">Section 1.8, &#8220;Build the documentation&#8221;</a>.</p>
</div>
<div class="sect3">
<h4 id="documentation-verification"><a class="anchor" href="#documentation-verification"></a><a class="link" href="#documentation-verification">31.5.1. Documentation verification</a></h4>
<div class="paragraph">
<p>When running <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-doc">build-doc</a>, we do the following checks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>&lt;&lt;&gt;&gt;</code> inner links are not broken</p>
</li>
<li>
<p><code>link:somefile[]</code> links point to paths that exist via <a href="#asciidoctor-extract-link-targets">asciidoctor/extract-link-targets</a>. Upstream wontfix at: <a href="https://github.com/asciidoctor/asciidoctor/issues/3210" class="bare">https://github.com/asciidoctor/asciidoctor/issues/3210</a></p>
</li>
<li>
<p>all links in non-README files to README IDs exist via <code>git grep</code> + <a href="#asciidoctor-extract-header-ids">asciidoctor/extract-header-ids</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The scripts prints what you have to fix and exits with an error status if there are any errors.</p>
</div>
<div class="sect4">
<h5 id="asciidoctor-extract-link-targets"><a class="anchor" href="#asciidoctor-extract-link-targets"></a><a class="link" href="#asciidoctor-extract-link-targets">31.5.1.1. asciidoctor/extract-link-targets</a></h5>
<div class="paragraph">
<p>Documentation for <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/asciidoctor/extract-link-targets">asciidoctor/extract-link-targets</a></p>
</div>
<div class="paragraph">
<p>Extract link targets from Asciidoctor document.</p>
</div>
<div class="paragraph">
<p>Usage:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./asciidoctor/extract-link-targets README.adoc</pre>
</div>
</div>
<div class="paragraph">
<p>Output: one link target per line.</p>
</div>
<div class="paragraph">
<p>Hastily hacked from: <a href="https://asciidoctor.org/docs/user-manual/#inline-macro-processor-example" class="bare">https://asciidoctor.org/docs/user-manual/#inline-macro-processor-example</a></p>
</div>
</div>
<div class="sect4">
<h5 id="asciidoctor-extract-header-ids"><a class="anchor" href="#asciidoctor-extract-header-ids"></a><a class="link" href="#asciidoctor-extract-header-ids">31.5.1.2. asciidoctor/extract-header-ids</a></h5>
<div class="paragraph">
<p>Documentation for <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/asciidoctor/extract-header-ids">asciidoctor/extract-header-ids</a></p>
</div>
<div class="paragraph">
<p>Extract header IDs, both auto-generated and manually given.</p>
</div>
<div class="paragraph">
<p>E.g., for the document <code>test.adoc</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>= Auto generated
[[explicitly-given]]
== La la</pre>
</div>
</div>
<div class="paragraph">
<p>the script:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./asciidoctor/extract-header-ids test.adoc</pre>
</div>
</div>
<div class="paragraph">
<p>produces:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>auto-generated
explicitly-given</pre>
</div>
</div>
<div class="paragraph">
<p>One application we have in mind for this is that as of 2.0.10 Asciidoctor does not warn on header ID collisions between auto-generated IDs: <a href="https://github.com/asciidoctor/asciidoctor/issues/3147" class="bare">https://github.com/asciidoctor/asciidoctor/issues/3147</a> But this script doesn&#8217;t solve that yet as it would require generating the section IDs without the <code>-N</code> suffix. Section generation happens at <code>Section.generate_id</code> in Asciidoctor code.</p>
</div>
<div class="paragraph">
<p>Hastily hacked from: <a href="https://asciidoctor.org/docs/user-manual/#https://asciidoctor.org/docs/user-manual/#tree-processor-example" class="bare">https://asciidoctor.org/docs/user-manual/#https://asciidoctor.org/docs/user-manual/#tree-processor-example</a> until I noticed that that example had a bug at the time and so fixed it here: <a href="https://github.com/asciidoctor/asciidoctor/issues/3363" class="bare">https://github.com/asciidoctor/asciidoctor/issues/3363</a></p>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="asciidoctor-link-target-up-rb"><a class="anchor" href="#asciidoctor-link-target-up-rb"></a><a class="link" href="#asciidoctor-link-target-up-rb">31.6. asciidoctor/link-target-up.rb</a></h3>
<div class="paragraph">
<p>The Asciidoctor extension scripts:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>link:asciidoctor-link-up.rb</p>
</li>
<li>
<p>link:asciidoctor-link-github.rb</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>hack the README <code>link:</code> targets to make them work from:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>inside the <code>out/</code> directory with <code>../</code></p>
</li>
<li>
<p><a href="#github-pages">GitHub pages</a>, with explicit GitHub blob URLs</p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="github-pages"><a class="anchor" href="#github-pages"></a><a class="link" href="#github-pages">31.6.1. GitHub pages</a></h4>
<div class="paragraph">
<p>As mentioned before the TOC, we have to push this README to GitHub pages due to: <a href="https://github.com/isaacs/github/issues/1610" class="bare">https://github.com/isaacs/github/issues/1610</a></p>
</div>
<div class="paragraph">
<p>For now, instead of pushing with <code>git push</code>, I just remember to always push with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./publish-gh-pages</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/publish-gh-pages">publish-gh-pages</a></p>
</div>
<div class="paragraph">
<p>I&#8217;m going this way for now because:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>the Jekyll Asciidoctor plugin is not enabled by default on GitHub: <a href="https://webapps.stackexchange.com/questions/114606/can-github-pages-render-asciidoc" class="bare">https://webapps.stackexchange.com/questions/114606/can-github-pages-render-asciidoc</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/1797074/local-executing-hook-after-a-git-push">post-push hooks don&#8217;t exist</a></p>
</li>
<li>
<p>I&#8217;m lazy to setup a proper Travis CI push</p>
</li>
<li>
<p>I&#8217;m the only contributor essentially, so no problems with pull requests</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The only files used by the GitHub pages are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/Gemfile">Gemfile</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/_config.yml">_config.yml</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="clean-the-build"><a class="anchor" href="#clean-the-build"></a><a class="link" href="#clean-the-build">31.7. Clean the build</a></h3>
<div class="paragraph">
<p>You did something crazy, and nothing seems to work anymore?</p>
</div>
<div class="paragraph">
<p>All our build outputs are stored under <code>out/</code>, so the coarsest and most effective thing you can do is:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>rm -rf out</pre>
</div>
</div>
<div class="paragraph">
<p>This implies a full rebuild for all archs however, so you might first want to explore finer grained cleans first.</p>
</div>
<div class="paragraph">
<p>All our individual <code>build-*</code> scripts have a <code>--clean</code> option to completely nuke their builds:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-gem5 --clean
./build-qemu --clean
./build-buildroot --clean</pre>
</div>
</div>
<div class="paragraph">
<p>Verify with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ls "$(./getvar qemu_build_dir)"
ls "$(./getvar gem5_build_dir)"
ls "$(./getvar buildroot_build_dir)"</pre>
</div>
</div>
<div class="paragraph">
<p>Note that host tools like QEMU and gem5 store all archs in a single directory to factor out build objects, so cleaning one arch will clean all of them.</p>
</div>
<div class="paragraph">
<p>To only nuke only one Buildroot package, we can use the <a href="https://buildroot.org/downloads/manual/manual.html#pkg-build-steps"><code>-dirclean</code></a> Buildroot target:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --no-all -- &lt;package-name&gt;-dirclean</pre>
</div>
</div>
<div class="paragraph">
<p>e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --no-all -- sample_package-dirclean</pre>
</div>
</div>
<div class="paragraph">
<p>Verify with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ls "$(./getvar buildroot_build_build_dir)"</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="ccache"><a class="anchor" href="#ccache"></a><a class="link" href="#ccache">31.8. ccache</a></h3>
<div class="paragraph">
<p><a href="https://en.wikipedia.org/wiki/Ccache">ccache</a> <a href="#benchmark-builds">might</a> save you a lot of re-build when you decide to <a href="#clean-the-build">Clean the build</a> or create a new <a href="#build-variants">build variant</a>.</p>
</div>
<div class="paragraph">
<p>We have ccache enabled for everything we build by default.</p>
</div>
<div class="paragraph">
<p>However, you likely want to add the following to your <code>.bashrc</code> to take better advantage of <code>ccache</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>export CCACHE_DIR=~/.ccache
export CCACHE_MAXSIZE="20G"</pre>
</div>
</div>
<div class="paragraph">
<p>We cannot automate this because you have to decide:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>should I store my cache on my HD or SSD?</p>
</li>
<li>
<p>how big is my build, and how many build configurations do I need to keep around at a time?</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>If you don&#8217;t those variables it, the default is to use <code>~/.buildroot-ccache</code> with <code>5G</code>, which is a bit small for us.</p>
</div>
<div class="paragraph">
<p>To check if <code>ccache</code> is working, run this command while a build is running on another shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>watch -n1 'make -C "$(./getvar buildroot_build_dir)" ccache-stats'</pre>
</div>
</div>
<div class="paragraph">
<p>or if you have it installed on host and the environment variables exported simply with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>watch -n1 'ccache -s'</pre>
</div>
</div>
<div class="paragraph">
<p>and then watch the miss or hit counts go up.</p>
</div>
<div class="paragraph">
<p>We have <a href="https://buildroot.org/downloads/manual/manual.html#ccache">enabled ccached</a> builds by default.</p>
</div>
<div class="paragraph">
<p><code>BR2_CCACHE_USE_BASEDIR=n</code> is used for Buildroot, which means that:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>absolute paths are used and GDB can find source files</p>
</li>
<li>
<p>but builds are not reused across separated LKMC directories</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="rebuild-buildroot-while-running"><a class="anchor" href="#rebuild-buildroot-while-running"></a><a class="link" href="#rebuild-buildroot-while-running">31.9. Rebuild Buildroot while running</a></h3>
<div class="paragraph">
<p>It is not possible to rebuild the root filesystem while running QEMU because QEMU holds the file qcow2 file:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>error while converting qcow2: Failed to get "write" lock</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="simultaneous-runs"><a class="anchor" href="#simultaneous-runs"></a><a class="link" href="#simultaneous-runs">31.10. Simultaneous runs</a></h3>
<div class="paragraph">
<p>When doing long simulations sweeping across multiple system parameters, it becomes fundamental to do multiple simulations in parallel.</p>
</div>
<div class="paragraph">
<p>This is specially true for gem5, which runs much slower than QEMU, and cannot use multiple host cores to speed up the simulation: <a href="https://github.com/cirosantilli-work/gem5-issues/issues/15" class="bare">https://github.com/cirosantilli-work/gem5-issues/issues/15</a>, so the only way to parallelize is to run multiple instances in parallel.</p>
</div>
<div class="paragraph">
<p>This also has a good synergy with <a href="#build-variants">Build variants</a>.</p>
</div>
<div class="paragraph">
<p>First shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run</pre>
</div>
</div>
<div class="paragraph">
<p>Another shell:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --run-id 1</pre>
</div>
</div>
<div class="paragraph">
<p>and now you have two QEMU instances running in parallel.</p>
</div>
<div class="paragraph">
<p>The default run id is <code>0</code>.</p>
</div>
<div class="paragraph">
<p>Our scripts solve two difficulties with simultaneous runs:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>port conflicts, e.g. GDB and <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/gem5-shell">gem5-shell</a></p>
</li>
<li>
<p>output directory conflicts, e.g. traces and gem5 stats overwriting one another</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Each run gets a separate output directory. For example:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --emulator gem5 --run-id 0 &amp;&gt;/dev/null &amp;
./run --arch aarch64 --emulator gem5 --run-id 1 &amp;&gt;/dev/null &amp;</pre>
</div>
</div>
<div class="paragraph">
<p>produces two separate <a href="#m5out-directory"><code>m5out</code> directories</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo "$(./getvar --arch aarch64 --emulator gem5 --run-id 0 m5out_dir)"
echo "$(./getvar --arch aarch64 --emulator gem5 --run-id 1 m5out_dir)"</pre>
</div>
</div>
<div class="paragraph">
<p>and the gem5 host executable stdout and stderr can be found at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>less "$(./getvar --arch aarch64 --emulator gem5 --run-id 0 termout_file)"
less "$(./getvar --arch aarch64 --emulator gem5 --run-id 1 termout_file)"</pre>
</div>
</div>
<div class="paragraph">
<p>Each line is prepended with the timestamp in seconds since the start of the program when it appeared.</p>
</div>
<div class="paragraph">
<p>To have more semantic output directories names for later inspection, you can use a non numeric string for the run ID, and indicate the port offset explicitly:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 --emulator gem5 --run-id some-experiment --port-offset 1</pre>
</div>
</div>
<div class="paragraph">
<p><code>--port-offset</code> defaults to the run ID when that is a number.</p>
</div>
<div class="paragraph">
<p>Like <a href="#cpu-architecture">CPU architecture</a>, you will need to pass the <code>-n</code> option to anything that needs to know runtime information, e.g. <a href="#gdb">GDB step debug</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --run-id 1
./run-gdb --run-id 1</pre>
</div>
</div>
<div class="paragraph">
<p>To run multiple gem5 checkouts, see: <a href="#gem5-worktree">Section 31.11.3.1, &#8220;gem5 worktree&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>Implementation note: we create multiple namespaces for two things:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>run output directory</p>
</li>
<li>
<p>ports</p>
<div class="ulist">
<ul>
<li>
<p>QEMU allows setting all ports explicitly.</p>
<div class="paragraph">
<p>If a port is not free, it just crashes.</p>
</div>
<div class="paragraph">
<p>We assign a contiguous port range for each run ID.</p>
</div>
</li>
<li>
<p>gem5 automatically increments ports until it finds a free one.</p>
<div class="paragraph">
<p>gem5 60600f09c25255b3c8f72da7fb49100e2682093a does not seem to expose a way to set the terminal and VNC ports from <code>fs.py</code>, so we just let gem5 assign the ports itself, and use <code>-n</code> only to match what it assigned. Those ports both appear on <a href="#gem5-config-ini">gem5 config.ini</a>.</p>
</div>
<div class="paragraph">
<p>The GDB port can be assigned on <code>gem5.opt --remote-gdb-port</code>, but it does not appear on <code>config.ini</code>.</p>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="build-variants"><a class="anchor" href="#build-variants"></a><a class="link" href="#build-variants">31.11. Build variants</a></h3>
<div class="paragraph">
<p>It often happens that you are comparing two versions of the build, a good and a bad one, and trying to figure out why the bad one is bad.</p>
</div>
<div class="paragraph">
<p>Our build variants system allows you to keep multiple built versions of all major components, so that you can easily switching between running one or the other.</p>
</div>
<div class="sect3">
<h4 id="linux-kernel-build-variants"><a class="anchor" href="#linux-kernel-build-variants"></a><a class="link" href="#linux-kernel-build-variants">31.11.1. Linux kernel build variants</a></h4>
<div class="paragraph">
<p>If you want to keep two builds around, one for the latest Linux version, and the other for Linux <code>v4.16</code>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># Build master.
./build-linux
# Build another branch.
git -C "$(./getvar linux_source_dir)" fetch --tags --unshallow
git -C "$(./getvar linux_source_dir)" checkout v4.16
./build-linux --linux-build-id v4.16
# Restore master.
git -C "$(./getvar linux_source_dir)" checkout -
# Run master.
./run
# Run another branch.
./run --linux-build-id v4.16</pre>
</div>
</div>
<div class="paragraph">
<p>The <code>git fetch --unshallow</code> is needed the first time because <code>./build --download-dependencies</code> only does a shallow clone of the Linux kernel to save space and time, see also: <a href="https://stackoverflow.com/questions/6802145/how-to-convert-a-git-shallow-clone-to-a-full-clone" class="bare">https://stackoverflow.com/questions/6802145/how-to-convert-a-git-shallow-clone-to-a-full-clone</a></p>
</div>
<div class="paragraph">
<p>The <code>--linux-build-id</code> option should be passed to all scripts that support it, much like <code>--arch</code> for the <a href="#cpu-architecture">CPU architecture</a>, e.g. to step debug:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --linux-build-id v4.16</pre>
</div>
</div>
<div class="paragraph">
<p>To run both kernels simultaneously, one on each QEMU instance, see: <a href="#simultaneous-runs">Section 31.10, &#8220;Simultaneous runs&#8221;</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="qemu-build-variants"><a class="anchor" href="#qemu-build-variants"></a><a class="link" href="#qemu-build-variants">31.11.2. QEMU build variants</a></h4>
<div class="paragraph">
<p>Analogous to the <a href="#linux-kernel-build-variants">Linux kernel build variants</a> but with the <code>--qemu-build-id</code> option instead:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-qemu
git -C "$(./getvar qemu_source_dir)" checkout v2.12.0
./build-qemu --qemu-build-id v2.12.0
git -C "$(./getvar qemu_source_dir)" checkout -
./run
./run --qemu-build-id v2.12.0</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="gem5-build-variants"><a class="anchor" href="#gem5-build-variants"></a><a class="link" href="#gem5-build-variants">31.11.3. gem5 build variants</a></h4>
<div class="paragraph">
<p>Analogous to the <a href="#linux-kernel-build-variants">Linux kernel build variants</a> but with the <code>--gem5-build-id</code> option instead:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># Build master.
./build-gem5
# Build another branch.
git -C "$(./getvar gem5_source_dir)" checkout some-branch
./build-gem5 --gem5-build-id some-branch
# Restore master.
git -C "$(./getvar gem5_source_dir)" checkout -
# Run master.
./run --emulator gem5
# Run another branch.
git -C "$(./getvar gem5_source_dir)" checkout some-branch
./run --gem5-build-id some-branch --emulator gem5</pre>
</div>
</div>
<div class="paragraph">
<p>Don&#8217;t forget however that gem5 has Python scripts in its source code tree, and that those must match the source code of a given build.</p>
</div>
<div class="paragraph">
<p>Therefore, you can&#8217;t forget to checkout to the sources to that of the corresponding build before running, unless you explicitly tell gem5 to use a non-default source tree with <a href="#gem5-worktree">gem5 worktree</a>. This becomes inevitable when you want to launch multiple simultaneous runs at different checkouts.</p>
</div>
<div class="sect4">
<h5 id="gem5-worktree"><a class="anchor" href="#gem5-worktree"></a><a class="link" href="#gem5-worktree">31.11.3.1. gem5 worktree</a></h5>
<div class="paragraph">
<p><a href="#gem5-build-variants"><code>--gem5-build-id</code></a> goes a long way, but if you want to seamlessly switch between two gem5 tress without checking out multiple times, then <code>--gem5-worktree</code> is for you.</p>
</div>
<div class="literalblock">
<div class="content">
<pre># Build gem5 at the revision in the gem5 submodule.
./build-gem5
# Create a branch at the same revision as the gem5 submodule.
./build-gem5 --gem5-worktree my-new-feature
cd "$(./getvar --gem5-worktree my-new-feature)"
vim create-bugs
git add .
git commit -m 'Created a bug'
cd -
./build-gem5 --gem5-worktree my-new-feature
# Run the submodule.
./run --emulator gem5 --run-id 0 &amp;&gt;/dev/null &amp;
# Run the branch the need to check out anything.
# With --gem5-worktree, we can do both runs at the same time!
./run --emulator gem5 --gem5-worktree my-new-feature --run-id 1 &amp;&gt;/dev/null &amp;</pre>
</div>
</div>
<div class="paragraph">
<p><code>--gem5-worktree &lt;worktree-id&gt;</code> automatically creates:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>a <a href="https://git-scm.com/docs/git-worktree">Git worktree</a> of gem5 if one didn&#8217;t exit yet for <code>&lt;worktree-id&gt;</code></p>
</li>
<li>
<p>a separate build directory, exactly like <code>--gem5-build-id my-new-feature</code> would</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We promise that the scripts sill never touch that worktree again once it has been created: it is now up to you to manage the code manually.</p>
</div>
<div class="paragraph">
<p><code>--gem5-worktree</code> is required if you want to do multiple simultaneous runs of different gem5 versions, because each gem5 build needs to use the matching Python scripts inside the source tree.</p>
</div>
<div class="paragraph">
<p>The difference between <code>--gem5-build-id</code> and <code>--gem5-worktree</code> is that <code>--gem5-build-id</code> specifies only the gem5 build output directory, while <code>--gem5-worktree</code> specifies the source input directory.</p>
</div>
<div class="paragraph">
<p>Each Git worktree needs a branch name, and we append the <code>wt/</code> prefix to the <code>--gem5-worktree</code> value, where <code>wt</code> stands for <code>WorkTree</code>. This is done to allow us to checkout to a test <code>some-branch</code> branch under <code>submodules/gem5</code> and still use <code>--gem5-worktree some-branch</code>, without conflict for the worktree branch, which can only be checked out once.</p>
</div>
</div>
<div class="sect4">
<h5 id="gem5-private-source-trees"><a class="anchor" href="#gem5-private-source-trees"></a><a class="link" href="#gem5-private-source-trees">31.11.3.2. gem5 private source trees</a></h5>
<div class="paragraph">
<p>Suppose that you are working on a private fork of gem5, but you want to use this repository to develop it as well.</p>
</div>
<div class="paragraph">
<p>Simply adding your private repository as a remote to <code>submodules/gem5</code> is dangerous, as you might forget and push your private work by mistake one day.</p>
</div>
<div class="paragraph">
<p>Even removing remotes is not safe enough, since <code>git submodule update</code> and other submodule commands can restore the old public remote.</p>
</div>
<div class="paragraph">
<p>Instead, we provide the following safer process.</p>
</div>
<div class="paragraph">
<p>First do a separate private clone of you private repository outside of this repository:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git clone https://my.private.repo.com/my-fork/gem5.git gem5-internal
gem5_internal="$(pwd)/gem5-internal"</pre>
</div>
</div>
<div class="paragraph">
<p>Next, when you want to build with the private repository, use the <code>--gem5-build-dir</code> and <code>--gem5-source-dir</code> argument to override our default gem5 source and build locations:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd linux-kernel-module-cheat
./build-gem5 \
--gem5-build-dir "${gem5_internal}/build" \
--gem5-source-dir "$gem5_internal" \
;
./run-gem5 \
--gem5-build-dir "${gem5_internal}/build" \
--gem5-source-dir "$gem5_internal" \
;</pre>
</div>
</div>
<div class="paragraph">
<p>With this setup, both your private gem5 source and build are safely kept outside of this public repository.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="buildroot-build-variants"><a class="anchor" href="#buildroot-build-variants"></a><a class="link" href="#buildroot-build-variants">31.11.4. Buildroot build variants</a></h4>
<div class="paragraph">
<p>Allows you to have multiple versions of the GCC toolchain or root filesystem.</p>
</div>
<div class="paragraph">
<p>Analogous to the <a href="#linux-kernel-build-variants">Linux kernel build variants</a> but with the <code>--build-id</code> option instead:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot
git -C "$(./getvar buildroot_source_dir)" checkout 2018.05
./build-buildroot --buildroot-build-id 2018.05
git -C "$(./getvar buildroot_source_dir)" checkout -
./run
./run --buildroot-build-id 2018.05</pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="directory-structure"><a class="anchor" href="#directory-structure"></a><a class="link" href="#directory-structure">31.12. Directory structure</a></h3>
<div class="sect3">
<h4 id="lkmc-directory"><a class="anchor" href="#lkmc-directory"></a><a class="link" href="#lkmc-directory">31.12.1. lkmc directory</a></h4>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc/">lkmc/</a> contains sources and headers that are shared across kernel modules, userland and baremetal examples.</p>
</div>
<div class="paragraph">
<p>We chose this awkward name so that our includes will have an <code>lkmc/</code> prefix.</p>
</div>
<div class="paragraph">
<p>Another option would have been to name it as <code>includes/lkmc</code>, but that would make paths longer, and we might want to store source code in that directory as well in the future.</p>
</div>
<div class="sect4">
<h5 id="userland-objects-vs-header-only"><a class="anchor" href="#userland-objects-vs-header-only"></a><a class="link" href="#userland-objects-vs-header-only">31.12.1.1. Userland objects vs header-only</a></h5>
<div class="paragraph">
<p>When factoring out functionality across userland examples, there are two main options:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>use header-only implementations</p>
</li>
<li>
<p>use separate C files and link to separate objects.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The downsides of the header-only implementation are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>slower compilation time, especially for C++</p>
</li>
<li>
<p>cannot call C implementations from assembly files</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The advantages of header-only implementations are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>easier to use, just <code>#include</code> and you are done, no need to modify build metadata.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>As a result, we are currently using the following rule:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>if something is only going to be used from C and not assembly, define it in a header which is easier to use</p>
<div class="paragraph">
<p>The slower compilation should be OK as long as split functionality amongst different headers and only include the required ones.</p>
</div>
<div class="paragraph">
<p>Also we don&#8217;t have a choice in the case of C++ template, which must stay in headers.</p>
</div>
</li>
<li>
<p>if the functionality will be called from assembly, then we don&#8217;t have a choice, and must add it to a separate source file and link against it.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect3">
<h4 id="buildroot_packages-directory"><a class="anchor" href="#buildroot_packages-directory"></a><a class="link" href="#buildroot_packages-directory">31.12.2. buildroot_packages directory</a></h4>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_packages/">buildroot_packages/</a></p>
</div>
<div class="paragraph">
<p>Every directory inside it is a Buildroot package.</p>
</div>
<div class="paragraph">
<p>Those packages get automatically added to Buildroot&#8217;s <code>BR2_EXTERNAL</code>, so all you need to do is to turn them on during build, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_SAMPLE_PACKAGE=y'</pre>
</div>
</div>
<div class="paragraph">
<p>then test it out with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after '/sample_package.out'</pre>
</div>
</div>
<div class="paragraph">
<p>and you should see:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>hello sample_package</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_packages/sample_package/sample_package.c">buildroot_packages/sample_package/sample_package.c</a></p>
</div>
<div class="paragraph">
<p>You can force a rebuild with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot --config 'BR2_PACKAGE_SAMPLE_PACKAGE=y' -- sample_package-reconfigure</pre>
</div>
</div>
<div class="paragraph">
<p>Buildroot packages are convenient, but in general, if a package if very important to you, but not really mergeable back to Buildroot, you might want to just use a custom build script for it, and point it to the Buildroot toolchain, and then use <code>BR2_ROOTFS_OVERLAY</code>, much like we do for <a href="#userland-setup">Userland setup</a>.</p>
</div>
<div class="paragraph">
<p>A custom build script can give you more flexibility: e.g. the package can be made work with other root filesystems more easily, have better <a href="#9p">9P</a> support, and rebuild faster as it evades some Buildroot boilerplate.</p>
</div>
<div class="sect4">
<h5 id="kernel_modules-buildroot-package"><a class="anchor" href="#kernel_modules-buildroot-package"></a><a class="link" href="#kernel_modules-buildroot-package">31.12.2.1. kernel_modules buildroot package</a></h5>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_packages/kernel_modules/">buildroot_packages/kernel_modules/</a></p>
</div>
<div class="paragraph">
<p>An example of how to use kernel modules in Buildroot.</p>
</div>
<div class="paragraph">
<p>Usage:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-buildroot \
--build-linux \
--config 'BR2_PACKAGE_KERNEL_MODULES=y' \
--no-overlay \
-- \
kernel_modules-reconfigure \
;</pre>
</div>
</div>
<div class="paragraph">
<p>Then test one of the modules with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --buildroot-linux --eval-after 'modprobe buildroot_hello'</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/buildroot_packages/kernel_modules/buildroot_hello.c">buildroot_packages/kernel_modules/buildroot_hello.c</a></p>
</div>
<div class="paragraph">
<p>As you have just seen, this sets up everything so that <a href="#modprobe">modprobe</a> can correctly find the module.</p>
</div>
<div class="paragraph">
<p><code>./build-buildroot --build-linux</code> and <code>./run --buildroot-linux</code> are needed because the Buildroot kernel modules must use the Buildroot Linux kernel at build and run time.</p>
</div>
<div class="paragraph">
<p>The <code>--no-overlay</code> is required otherwise our <code>modules.order</code> generated by <code>./build-linux</code> and installed with <code>BR2_ROOTFS_OVERLAY</code> overwrites the Buildroot generated one.</p>
</div>
<div class="paragraph">
<p>Implementattion described at: <a href="https://stackoverflow.com/questions/40307328/how-to-add-a-linux-kernel-driver-module-as-a-buildroot-package/43874273#43874273" class="bare">https://stackoverflow.com/questions/40307328/how-to-add-a-linux-kernel-driver-module-as-a-buildroot-package/43874273#43874273</a></p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="patches-directory"><a class="anchor" href="#patches-directory"></a><a class="link" href="#patches-directory">31.12.3. patches directory</a></h4>
<div class="sect4">
<h5 id="patches-global-directory"><a class="anchor" href="#patches-global-directory"></a><a class="link" href="#patches-global-directory">31.12.3.1. patches/global directory</a></h5>
<div class="paragraph">
<p>Has the following structure:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>package-name/00001-do-something.patch</pre>
</div>
</div>
<div class="paragraph">
<p>The patches are then applied to the corresponding packages before build.</p>
</div>
<div class="paragraph">
<p>Uses <code>BR2_GLOBAL_PATCH_DIR</code>.</p>
</div>
</div>
<div class="sect4">
<h5 id="patches-manual-directory"><a class="anchor" href="#patches-manual-directory"></a><a class="link" href="#patches-manual-directory">31.12.3.2. patches/manual directory</a></h5>
<div class="paragraph">
<p>Patches in this directory are never applied automatically: it is up to users to manually apply them before usage following the instructions in this documentation.</p>
</div>
<div class="paragraph">
<p>These are typically patches that don&#8217;t contain fundamental functionality, so we don&#8217;t feel like forking the target repos.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="rootfs_overlay"><a class="anchor" href="#rootfs_overlay"></a><a class="link" href="#rootfs_overlay">31.12.4. rootfs_overlay</a></h4>
<div class="paragraph">
<p>We use this directory for:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>customized configuration files</p>
</li>
<li>
<p>userland module test scripts that don&#8217;t need to be compiled.</p>
<div class="paragraph">
<p>Contrast this with <a href="#userland-content">C examples</a> that need compilation.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>This directory is copied into the target filesystem by:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./copy-overlay
./build-buildroot</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/copy-overlay">copy-overlay</a></p>
</div>
<div class="paragraph">
<p>Build Buildroot is required for the same reason as described at: <a href="#your-first-kernel-module-hack">Section 1.1.2.2, &#8220;Your first kernel module hack&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>However, since the <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay">rootfs_overlay</a> directory does not require compilation, unlike say <a href="#your-first-kernel-module-hack">kernel modules</a>, we also make it <a href="#9p">9P</a> available to the guest directly even without <code>./copy-overlay</code> at:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>ls /mnt/9p/rootfs_overlay</pre>
</div>
</div>
<div class="paragraph">
<p>This way you can just hack away the scripts and try them out immediately without any further operations.</p>
</div>
</div>
<div class="sect3">
<h4 id="lkmc-c"><a class="anchor" href="#lkmc-c"></a><a class="link" href="#lkmc-c">31.12.5. lkmc.c</a></h4>
<div class="paragraph">
<p>The files:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc.c">lkmc.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/lkmc.h">lkmc.h</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>contain common C function helpers that can be used both in userland and baremetal. Oh, the infinite <a href="#about-the-baremetal-setup">joys of Newlib</a>.</p>
</div>
<div class="paragraph">
<p>Those files also contain arch specific helpers under ifdefs like:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>#if defined(__aarch64__)</pre>
</div>
</div>
<div class="paragraph">
<p>We try to keep as much as possible in those files. It bloats builds a little, but just makes everything simpler to understand.</p>
</div>
</div>
<div class="sect3">
<h4 id="rand_check-out"><a class="anchor" href="#rand_check-out"></a><a class="link" href="#rand_check-out">31.12.6. rand_check.out</a></h4>
<div class="paragraph">
<p>Print out several parameters that normally change randomly from boot to boot:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --eval-after './linux/rand_check.out;./linux/poweroff.out'</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/linux/rand_check.c">userland/linux/rand_check.c</a></p>
</div>
<div class="paragraph">
<p>This can be used to check the determinism of:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#norandmaps">norandmaps</a></p>
</li>
<li>
<p><a href="#qemu-record-and-replay">QEMU record and replay</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="lkmc_home"><a class="anchor" href="#lkmc_home"></a><a class="link" href="#lkmc_home">31.12.7. lkmc_home</a></h4>
<div class="paragraph">
<p><code>lkmc_home</code> refers to the target base directory in which we put all our custom built stuff, such as <a href="#userland-setup">userland executables</a> and <a href="#your-first-kernel-module-hack">kernel modules</a>.</p>
</div>
<div class="paragraph">
<p>The current value can be found with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./getvar guest_lkmc_home</pre>
</div>
</div>
<div class="paragraph">
<p>In the past, we used to dump everything into the root filesystem, but as the userland structure got more complex with subfolders, we decided that the risk of conflicting with important root files was becoming too great.</p>
</div>
<div class="paragraph">
<p>To save you from typing that path every time, we have made our most common commands <code>cd</code> into that directory by default for you, e.g.:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>interactive shells <code>cd</code> there through <a href="#busybox-shell-initrc-files">BusyBox shell initrc files</a></p>
</li>
<li>
<p><code>--eval</code> and <code>--eval-after</code> through <a href="#replace-init">Replace init</a> and <a href="#init-busybox">Run command at the end of BusyBox init</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Whenever a relative path is used inside a guest sample command, e.g. <code>insmod hello.ko</code> or <code>./hello.out</code>, it means that the path lives in <code>lkmc_home</code> unless stated otherwise.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="test-this-repo"><a class="anchor" href="#test-this-repo"></a><a class="link" href="#test-this-repo">31.13. Test this repo</a></h3>
<div class="sect3">
<h4 id="automated-tests"><a class="anchor" href="#automated-tests"></a><a class="link" href="#automated-tests">31.13.1. Automated tests</a></h4>
<div class="paragraph">
<p>Run almost all tests:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build-test --all-archs --all-emulators --size 3 &amp;&amp; \
./test --size 3
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>should output 0.</p>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/build-test">build-test</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/test">test</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/test">test</a> script runs several different types of tests, which can also be run separately as explained at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/test-boot">test-boot</a></p>
</li>
<li>
<p><a href="#test-userland-in-full-system">Test userland in full system</a></p>
</li>
<li>
<p><a href="#user-mode-tests">User mode tests</a></p>
</li>
<li>
<p><a href="#baremetal-tests">Baremetal tests</a></p>
</li>
<li>
<p><a href="#gdb-tests">GDB tests</a></p>
</li>
<li>
<p><a href="#gem5-unit-tests">gem5 unit tests</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/test">test</a> does not all possible tests, because there are too many possible variations and that would take forever. The rationale is the same as for <code>./build all</code> and is explained in <code>./build --help</code>.</p>
</div>
<div class="sect4">
<h5 id="test-arch-and-emulator-selection"><a class="anchor" href="#test-arch-and-emulator-selection"></a><a class="link" href="#test-arch-and-emulator-selection">31.13.1.1. Test arch and emulator selection</a></h5>
<div class="paragraph">
<p>You can select multiple archs and emulators of interest, as for an other command, with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./test-executables \
--arch x86_64 \
--arch aarch64 \
--emulator gem5 \
--emulator qemu \
;</pre>
</div>
</div>
<div class="paragraph">
<p>You can also test all supported archs and emulators with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./test-executables \
--all-archs \
--all-emulators \
;</pre>
</div>
</div>
<div class="paragraph">
<p>This command would run the test four times, using <code>x86_64</code> and <code>aarch64</code> with both gem5 and QEMU.</p>
</div>
<div class="paragraph">
<p>Without those flags, it defaults to just running the default arch and emulator once: <code>x86_64</code> and <code>qemu</code>.</p>
</div>
</div>
<div class="sect4">
<h5 id="quit-on-fail"><a class="anchor" href="#quit-on-fail"></a><a class="link" href="#quit-on-fail">31.13.1.2. Quit on fail</a></h5>
<div class="paragraph">
<p>By default, continue running even after the first failure happens, and they show a summary at the end.</p>
</div>
<div class="paragraph">
<p>You can make them exit immediately with the <code>--no-quit-on-fail</code> option, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./test-executables --quit-on-fail</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="test-userland-in-full-system"><a class="anchor" href="#test-userland-in-full-system"></a><a class="link" href="#test-userland-in-full-system">31.13.1.3. Test userland in full system</a></h5>
<div class="paragraph">
<p>TODO: we really need a mechanism to automatically generate the test list automatically e.g. based on <a href="#path-properties">path_properties</a>, currently there are many tests missing, and we have to add everything manually which is very annoying.</p>
</div>
<div class="paragraph">
<p>We could just generate it on the fly on the host, and forward it to guest through CLI arguments.</p>
</div>
<div class="paragraph">
<p>Run all userland tests from inside full system simulation (i.e. not <a href="#user-mode-simulation">User mode simulation</a>):</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./test-userland-full-system</pre>
</div>
</div>
<div class="paragraph">
<p>This includes, in particular, userland programs that test the kernel modules, which cannot be tested in user mode simulation.</p>
</div>
<div class="paragraph">
<p>Basically just boots and runs: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/test_all.sh">rootfs_overlay/lkmc/test_all.sh</a></p>
</div>
<div class="paragraph">
<p>Failure is detected by looking for the <a href="#magic-failure-string">Magic failure string</a></p>
</div>
<div class="paragraph">
<p>Most userland programs that don&#8217;t rely on kernel modules can also be tested in user mode simulation as explained at: <a href="#user-mode-tests">Section 10.2, &#8220;User mode tests&#8221;</a>.</p>
</div>
</div>
<div class="sect4">
<h5 id="gdb-tests"><a class="anchor" href="#gdb-tests"></a><a class="link" href="#gdb-tests">31.13.1.4. GDB tests</a></h5>
<div class="paragraph">
<p>We have some <a href="https://github.com/pexpect/pexpect">pexpect</a> automated tests for GDB for both userland and baremetal programs!</p>
</div>
<div class="paragraph">
<p>Run the userland tests:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --all-archs test-gdb &amp;&amp; \
./test-gdb --all-archs --all-emulators</pre>
</div>
</div>
<div class="paragraph">
<p>Run the baremetal tests instead:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./test-gdb --all-archs --all-emulators --mode baremetal</pre>
</div>
</div>
<div class="paragraph">
<p>Sources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/test-gdb">test-gdb</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/gdb_tests/">userland/gdb_tests/</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/arm/gdb_tests/">userland/arch/arm/gdb_tests/</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/arch/aarch64/gdb_tests/">userland/arch/aarch64/gdb_tests/</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>If a test fails, re-run the test commands manually and use <code>--verbose</code> to understand what happened:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch arm --background --baremetal baremetal/c/add.c --gdb-wait &amp;
./run-gdb --arch arm --baremetal baremetal/c/add.c --verbose -- main</pre>
</div>
</div>
<div class="paragraph">
<p>and possibly repeat the GDB steps manually with the usual:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb --arch arm --baremetal baremetal/c/add.c --no-continue --verbose</pre>
</div>
</div>
<div class="paragraph">
<p>To debug GDB problems on gem5, you might want to enable the following <a href="#gem5-tracing">tracing</a> options:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run \
--arch arm \
--baremetal baremetal/c/add.c \
--gdb-wait \
--trace GDBRecv,GDBSend \
--trace-stdout \
;</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="magic-failure-string"><a class="anchor" href="#magic-failure-string"></a><a class="link" href="#magic-failure-string">31.13.1.5. Magic failure string</a></h5>
<div class="paragraph">
<p>We do not know of any way to set the emulator exit status in QEMU arm full system.</p>
</div>
<div class="paragraph">
<p>For other arch / emulator combinations, we know how to do it:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>aarch64: aarch64 semihosting supports exit status</p>
</li>
<li>
<p>gem5: <a href="#m5-fail">m5 fail</a> works on all archs</p>
</li>
<li>
<p>user mode: QEMU forwards exit status, for gem5 we do some log parsing as described at: <a href="#gem5-syscall-emulation-exit-status">Section 10.6.1, &#8220;gem5 syscall emulation exit status&#8221;</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Since we can&#8217;t do it for QEMU arm, the only reliable solution is to just parse the guest serial output for a magic failure string to check if tests failed.</p>
</div>
<div class="paragraph">
<p>Our run scripts parse the serial output looking for a line line containing only exactly the magic regular expression:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>lkmc_exit_status_(\d+)</pre>
</div>
</div>
<div class="paragraph">
<p>and then exit with the given regular expression, e.g.:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch aarch64 baremetal/return2.c
echo $?</pre>
</div>
</div>
<div class="paragraph">
<p>should output:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>2</pre>
</div>
</div>
<div class="paragraph">
<p>This magic output string is notably generated by:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/lkmc/test_fail.sh">rootfs_overlay/lkmc/test_fail.sh</a>, which is used by <a href="#test-userland-in-full-system">Test userland in full system</a></p>
</li>
<li>
<p>the <code>exit()</code> baremetal function when <code>status != 1</code>.</p>
<div class="paragraph">
<p>Unfortunately the only way we found to set this up was with <code>on_exit</code>: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/issues/59" class="bare">https://github.com/cirosantilli/linux-kernel-module-cheat/issues/59</a>.</p>
</div>
<div class="paragraph">
<p>Trying to patch <code>_exit</code> directly fails since at that point some de-initialization has already happened which prevents the print.</p>
</div>
<div class="paragraph">
<p>So setup this <code>on_exit</code> automatically from all our <a href="#baremetal-bootloaders">Baremetal bootloaders</a>, so it just works automatically for the examples that use the bootloaders: <a href="https://stackoverflow.com/questions/44097610/pass-parameter-to-atexit/49659697#49659697" class="bare">https://stackoverflow.com/questions/44097610/pass-parameter-to-atexit/49659697#49659697</a></p>
</div>
<div class="paragraph">
<p>The following examples end up testing that our setup is working:</p>
</div>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/assert_fail.c">userland/c/assert_fail.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/return0.c">userland/c/return0.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/return1.c">userland/c/return1.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/return2.c">userland/c/return2.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/exit0.c">userland/c/exit0.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/exit1.c">userland/c/exit1.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/c/exit2.c">userland/c/exit2.c</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/userland/posix/kill.c">userland/posix/kill.c</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Beware that on Linux kernel simulations, you cannot even echo that string from userland, since userland stdout shows up on the serial.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="non-automated-tests"><a class="anchor" href="#non-automated-tests"></a><a class="link" href="#non-automated-tests">31.13.2. Non-automated tests</a></h4>
<div class="sect4">
<h5 id="test-gdb-linux-kernel"><a class="anchor" href="#test-gdb-linux-kernel"></a><a class="link" href="#test-gdb-linux-kernel">31.13.2.1. Test GDB Linux kernel</a></h5>
<div class="paragraph">
<p>For the Linux kernel, do the following manual tests for now.</p>
</div>
<div class="paragraph">
<p>Shell 1:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --gdb-wait</pre>
</div>
</div>
<div class="paragraph">
<p>Shell 2:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run-gdb start_kernel</pre>
</div>
</div>
<div class="paragraph">
<p>Should break GDB at <code>start_kernel</code>.</p>
</div>
<div class="paragraph">
<p>Then proceed to do the following tests:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>./count.sh</code> and <code>break __x64_sys_write</code></p>
</li>
<li>
<p><code>insmod timer.ko</code> and <code>break lkmc_timer_callback</code></p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="test-the-internet"><a class="anchor" href="#test-the-internet"></a><a class="link" href="#test-the-internet">31.13.2.2. Test the Internet</a></h5>
<div class="paragraph">
<p>You should also test that the Internet works:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./run --arch x86_64 --kernel-cli '- lkmc_eval="ifup -a;wget -S google.com;poweroff;"'</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="cli-script-tests"><a class="anchor" href="#cli-script-tests"></a><a class="link" href="#cli-script-tests">31.13.2.3. CLI script tests</a></h5>
<div class="paragraph">
<p><code>build-userland</code> and <code>test-executables</code> have a wide variety of target selection modes, and it was hard to keep them all working without some tests:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/test-build-userland">test-build-userland</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/test-test-executables">test-test-executables</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="bisection"><a class="anchor" href="#bisection"></a><a class="link" href="#bisection">31.14. Bisection</a></h3>
<div class="paragraph">
<p>When updating the Linux kernel, QEMU and gem5, things sometimes break.</p>
</div>
<div class="paragraph">
<p>However, for many types of crashes, it is trivial to bisect down to the offending commit, in particular because we can make QEMU and gem5 exit with status 1 on kernel panic as mentioned at: <a href="#exit-emulator-on-panic">Section 15.7.1.3, &#8220;Exit emulator on panic&#8221;</a>.</p>
</div>
<div class="paragraph">
<p>For example, when updating from QEMU <code>v2.12.0</code> to <code>v3.0.0-rc3</code>, the Linux kernel boot started to panic for <code>arm</code>.</p>
</div>
<div class="paragraph">
<p>We then bisected it as explained at: <a href="https://stackoverflow.com/questions/4713088/how-to-use-git-bisect/22592593#22592593" class="bare">https://stackoverflow.com/questions/4713088/how-to-use-git-bisect/22592593#22592593</a> with the <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/bisect-qemu-linux-boot">bisect-qemu-linux-boot</a> script:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>root_dir="$(pwd)"
cd "$(./getvar qemu_source_dir)"
git bisect start
# Check that our test script fails on v3.0.0-rc3 as expected, and mark it as bad.
"${root_dir}/bisect-qemu-linux-boot"
# Should output 1.
echo #?
git bisect bad
# Same for the good end.
git checkout v2.12.0
"${root_dir}/bisect-qemu-linux-boot"
# Should output 0.
echo #?
git bisect good
# This leaves us at the offending commit.
git bisect run "${root_dir}/bisect-qemu-linux-boot"
# Clean up after the bisection.
git bisect reset
git submodule update
"${root_dir}/build-qemu" --clean --qemu-build-id bisect</pre>
</div>
</div>
<div class="paragraph">
<p>Other bisection helpers include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/bisect-linux-boot-gem5">bisect-linux-boot-gem5</a></p>
</li>
<li>
<p><a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/bisect-gem5-linux-boot">bisect-gem5-linux-boot</a></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="path-properties"><a class="anchor" href="#path-properties"></a><a class="link" href="#path-properties">31.15. path_properties</a></h3>
<div class="paragraph">
<p>In order to build and run each userland and <a href="#baremetal-setup">baremetal</a> example properly, we need per-file metadata such as compiler flags and required number of cores.</p>
</div>
<div class="paragraph">
<p>This data is stored is stored in <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/path_properties.py">path_properties.py</a> at <code>path_properties_tuples</code>.</p>
</div>
<div class="paragraph">
<p>Maybe we should embed it magically into source files directories to make it easier to see? But one big Python dict was easier to implement so we started like this. And it allows factoring chunks out easily.</p>
</div>
<div class="paragraph">
<p>The format is as follows:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>'path_component': (
{'property': value},
{
'child_path_component':
{
{'child_property': },
{}
}
}
)</pre>
</div>
</div>
<div class="paragraph">
<p>and as a shortcut, paths that don&#8217;t have any children can be written directly as:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>'path_component': {'property': value}</pre>
</div>
</div>
<div class="paragraph">
<p>Properties of parent directories apply to all children.</p>
</div>
<div class="paragraph">
<p>Lists coming from parent directories are extended instead of overwritten by children, this is especially useful for C compiler flags.</p>
</div>
</div>
<div class="sect2">
<h3 id="update-a-forked-submodule"><a class="anchor" href="#update-a-forked-submodule"></a><a class="link" href="#update-a-forked-submodule">31.16. Update a forked submodule</a></h3>
<div class="paragraph">
<p>This is a template update procedure for submodules for which we have some patches on on top of mainline.</p>
</div>
<div class="paragraph">
<p>This example is based on the Linux kernel, for which we used to have patches, but have since moved to mainline:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># Last point before out patches.
last_mainline_revision=v4.15
next_mainline_revision=v4.16
cd "$(./getvar linux_source_dir)"
# Create a branch before the rebase in case things go wrong.
git checkout -b "lkmc-${last_mainline_revision}"
git remote set-url origin git@github.com:cirosantilli/linux.git
git push
git checkout master
git remote add up git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
git fetch up
git rebase --onto "$next_mainline_revision" "$last_mainline_revision"
# And update the README to show off.
git commit -m "linux: update to ${next_mainline_revision}"</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="release"><a class="anchor" href="#release"></a><a class="link" href="#release">31.17. Release</a></h3>
<div class="sect3">
<h4 id="release-procedure"><a class="anchor" href="#release-procedure"></a><a class="link" href="#release-procedure">31.17.1. Release procedure</a></h4>
<div class="paragraph">
<p>Ensure that the <a href="#automated-tests">Automated tests</a> are passing on a clean build:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>mv out out.bak
./build-test --size 3 &amp;&amp; ./test --size 3</pre>
</div>
</div>
<div class="paragraph">
<p>The <code>./build-test</code> command builds a superset of what will be downloaded which also tests other things we would like to be working on the release. For the minimal build to generate the files to be uploaded, see: <a href="#release-zip">Section 31.17.2, &#8220;release-zip&#8221;</a></p>
</div>
<div class="paragraph">
<p>The clean build is necessary as it generates clean images since <a href="#remove-buildroot-packages">it is not possible to remove Buildroot packages</a></p>
</div>
<div class="paragraph">
<p>Run all tests in <a href="#non-automated-tests">Non-automated tests</a> just QEMU x86_64 and QEMU aarch64.</p>
</div>
<div class="paragraph">
<p>TODO: not working currently, so skipped: Ensure that the <a href="#benchmark-this-repo">benchmarks</a> look fine:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./bench-all -A</pre>
</div>
</div>
<div class="paragraph">
<p>Create a release candidate and upload it:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git tag -a -m '' v3.0-rc1
git push --follow-tags
./release-zip --all-archs
# export LKMC_GITHUB_TOKEN=&lt;your-token&gt;
./release-upload</pre>
</div>
</div>
<div class="paragraph">
<p>Now let&#8217;s do an out-of-box testing for the release candidate:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd ..
git clone https://github.com/cirosantilli/linux-kernel-module-cheat linux-kernel-module-cheat-release
cd linux-kernel-module-cheat-release</pre>
</div>
</div>
<div class="paragraph">
<p>Test <a href="#prebuilt">Prebuilt setup</a>.</p>
</div>
<div class="paragraph">
<p>Clean up, and re-start from scratch:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>cd ..
rm -rf linux-kernel-module-cheat-release
git clone https://github.com/cirosantilli/linux-kernel-module-cheat linux-kernel-module-cheat-release
cd linux-kernel-module-cheat-release</pre>
</div>
</div>
<div class="paragraph">
<p>Go through all the other <a href="#getting-started">Getting started</a> sections in order.</p>
</div>
<div class="paragraph">
<p>Once everything looks fine, publish the release with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>git tag -a v3.0
# Describe the release int the tag message.
git push --follow-tags
./release-zip --all-archs
# export LKMC_GITHUB_TOKEN=&lt;your-token&gt;
./release-upload</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="release-zip"><a class="anchor" href="#release-zip"></a><a class="link" href="#release-zip">31.17.2. release-zip</a></h4>
<div class="paragraph">
<p>Create a zip containing all files required for <a href="#prebuilt">Prebuilt setup</a>:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>./build --all-archs release &amp;&amp; ./release-zip --all-archs</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/release-zip">release-zip</a></p>
</div>
<div class="paragraph">
<p>This generates a zip file:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>echo "$(./getvar release_zip_file)"</pre>
</div>
</div>
<div class="paragraph">
<p>which you can then upload somewhere.</p>
</div>
</div>
<div class="sect3">
<h4 id="release-upload"><a class="anchor" href="#release-upload"></a><a class="link" href="#release-upload">31.17.3. release-upload</a></h4>
<div class="paragraph">
<p>After:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>running <a href="#release-zip">release-zip</a></p>
</li>
<li>
<p>creating and pushing a tag to GitHub</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>you can upload the release to GitHub automatically with:</p>
</div>
<div class="literalblock">
<div class="content">
<pre># export LKMC_GITHUB_TOKEN=&lt;your-token&gt;
./release-upload</pre>
</div>
</div>
<div class="paragraph">
<p>Source: <a href="https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/release-upload">release-upload</a></p>
</div>
<div class="paragraph">
<p>The HEAD of the local repository must be on top of a tag that has been pushed for this to work.</p>
</div>
<div class="paragraph">
<p>Create <code>LKMC_GITHUB_TOKEN</code> under: <a href="https://github.com/settings/tokens/new" class="bare">https://github.com/settings/tokens/new</a> and save it to your <code>.bashrc</code>.</p>
</div>
<div class="paragraph">
<p>The implementation of this script is described at:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://stackoverflow.com/questions/5207269/how-to-release-a-build-artifact-asset-on-github-with-a-script/52354732#52354732" class="bare">https://stackoverflow.com/questions/5207269/how-to-release-a-build-artifact-asset-on-github-with-a-script/52354732#52354732</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/38153418/can-someone-give-a-python-requests-example-of-uploading-a-release-asset-in-githu/52354681#52354681" class="bare">https://stackoverflow.com/questions/38153418/can-someone-give-a-python-requests-example-of-uploading-a-release-asset-in-githu/52354681#52354681</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="design-rationale"><a class="anchor" href="#design-rationale"></a><a class="link" href="#design-rationale">31.18. Design rationale</a></h3>
<div class="sect3">
<h4 id="design-goals"><a class="anchor" href="#design-goals"></a><a class="link" href="#design-goals">31.18.1. Design goals</a></h4>
<div class="paragraph">
<p>This project was created to help me understand, modify and test low level system components by using system simulators.</p>
</div>
<div class="paragraph">
<p>System simulators are cool compared to real hardware because they are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>free</p>
</li>
<li>
<p>make experiments highly reproducible</p>
</li>
<li>
<p>give full visibility to the system: you can inspect any byte in memory, or the state of any hardware register. The laws of physics sometimes get in the way when doing that for real hardware.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The current components we focus the most on are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#linux-kernel">Linux kernel</a> and Linux kernel modules</p>
</li>
<li>
<p>full systems emulators, currently <a href="#qemu-buildroot-setup">qemu</a> and <a href="#gem5-buildroot-setup">gem5</a></p>
</li>
<li>
<p><a href="#buildroot">Buildroot</a>. We use and therefore document, a large part of its feature set.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The following components are not covered, but they would also benefit from this setup, and it shouldn&#8217;t be hard to add them:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>C standard libraries</p>
</li>
<li>
<p>compilers. Project idea: add a new instruction to x86, then hack up GCC to actually use it, and make a C program that generates it.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The design goals are to provide setups that are:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>highly automated: "just works"</p>
</li>
<li>
<p>thoroughly documented: you know what "just works" means</p>
</li>
<li>
<p>can be fully built from source: to give visibility and allow modifications</p>
</li>
<li>
<p>can also use <a href="#prebuilt">prebuilt binaries</a> as much as possible: in case you are lazy or unable to build from source</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We aim to make a documentation that contains a very high runnable example / theory bullshit ratio.</p>
</div>
<div class="paragraph">
<p>Having at least one example per section is ideal, and it should be the very first thing in the section if possible.</p>
</div>
</div>
<div class="sect3">
<h4 id="setup-trade-offs"><a class="anchor" href="#setup-trade-offs"></a><a class="link" href="#setup-trade-offs">31.18.2. Setup trade-offs</a></h4>
<div class="paragraph">
<p>The trade-offs between the different <a href="#getting-started">setups</a> are basically a balance between:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>speed ans size: how long and how much disk space do the build and run take?</p>
</li>
<li>
<p>visibility: can you GDB step debug everything and read source code?</p>
</li>
<li>
<p>modifiability: can you modify the source code and rebuild a modified version?</p>
</li>
<li>
<p>portability: does it work on a Windows host? Could it ever?</p>
</li>
<li>
<p>accuracy: how accurate does the simulation represent real hardware?</p>
</li>
<li>
<p>compatibility: how likely is is that all the components will work well together: emulator, compiler, kernel, standard library, &#8230;&#8203;</p>
</li>
<li>
<p>guest software availability: how wide is your choice of easily installed guest software packages? See also: <a href="#linux-distro-choice">Section 31.18.4, &#8220;Linux distro choice&#8221;</a></p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="resource-tradeoff-guidelines"><a class="anchor" href="#resource-tradeoff-guidelines"></a><a class="link" href="#resource-tradeoff-guidelines">31.18.3. Resource tradeoff guidelines</a></h4>
<div class="paragraph">
<p>Choosing which features go into our default builds means making tradeoffs, here are our guidelines:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>keep the root filesystem as tiny as possible to make <a href="#prebuilt">Prebuilt setup</a> small: only add BusyBox to have a small interactive system.</p>
<div class="paragraph">
<p>It is easy to add new packages once you have the toolchain, and if you don&#8217;t there are infinitely many packages to cover and we can&#8217;t cover them all.</p>
</div>
</li>
<li>
<p>enable every feature possible on the toolchain (GCC, Binutils), because changes imply Buildroot rebuilds</p>
</li>
<li>
<p>runtime is sacred. Faster systems are:</p>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>easier to understand</p>
</li>
<li>
<p>run faster, which is specially for <a href="#gem5">gem5</a> which is slow</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>Runtime basically just comes down to how we configure the Linux kernel, since in the root filesystem all that matters is <code>init=</code>, and that is easy to control.</p>
</div>
<div class="paragraph">
<p>One possibility we could play with is to build loadable modules instead of built-in modules to reduce runtime, but make it easier to get started with the modules.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>In order to learn how to measure some of those aspects, see: <a href="#benchmark-this-repo">Section 29, &#8220;Benchmark this repo&#8221;</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="linux-distro-choice"><a class="anchor" href="#linux-distro-choice"></a><a class="link" href="#linux-distro-choice">31.18.4. Linux distro choice</a></h4>
<div class="paragraph">
<p>We haven&#8217;t found the ultimate distro yet, here is a summary table of trade-offs that we care about: <a href="#table-lkmc-linux-distro-comparison">Table 7, &#8220;Comparison of Linux distros for usage in this repository&#8221;</a>.</p>
</div>
<table id="table-lkmc-linux-distro-comparison" class="tableblock frame-all grid-all stretch">
<caption class="title">Table 7. Comparison of Linux distros for usage in this repository</caption>
<colgroup>
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.667%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Distro</th>
<th class="tableblock halign-left valign-top">Packages in single Git tree</th>
<th class="tableblock halign-left valign-top">Git tracked docs</th>
<th class="tableblock halign-left valign-top">Cross build without QEMU</th>
<th class="tableblock halign-left valign-top">Prebuilt downloads</th>
<th class="tableblock halign-left valign-top">Number of packages</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Buildroot 2018.05</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">y</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">y</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">y</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">n</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">2k (1)</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Ubuntu 18.04</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">n</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">n</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">n</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">y</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">50k (3)</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Yocto 2.5 (8)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">?</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">y (5)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">?</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">y (6)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">400 (7)</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Alpine Linux 3.8.0</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">y</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">n (1)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">?</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">y</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">2000 (4)</p></td>
</tr>
</tbody>
</table>
<div class="ulist">
<ul>
<li>
<p>(1): Wiki&#8230;&#8203; <a href="https://wiki.alpinelinux.org/wiki/Main_Page" class="bare">https://wiki.alpinelinux.org/wiki/Main_Page</a></p>
</li>
<li>
<p>(2): <code>ls packages | wc</code></p>
</li>
<li>
<p>(3): <a href="https://askubuntu.com/questions/120630/how-many-packages-are-in-the-main-repository" class="bare">https://askubuntu.com/questions/120630/how-many-packages-are-in-the-main-repository</a></p>
</li>
<li>
<p>(4): <code>ls main community non-free | wc</code></p>
</li>
<li>
<p>(5): yes, but on a separate Git tree&#8230;&#8203; <a href="https://git.yoctoproject.org/cgit/cgit.cgi/yocto-docs/" class="bare">https://git.yoctoproject.org/cgit/cgit.cgi/yocto-docs/</a></p>
</li>
<li>
<p>(6): yes, but the initial Poky build / download still took 5 hours on <a href="#38mbps-internet">38Mbps internet</a>, and QEMU failed to boot at the end&#8230;&#8203; <a href="https://bugzilla.yoctoproject.org/show_bug.cgi?id=12953" class="bare">https://bugzilla.yoctoproject.org/show_bug.cgi?id=12953</a></p>
</li>
<li>
<p>(7): <code>ls recipes-* | wc</code></p>
</li>
<li>
<p>(8): Poky reference system: <a href="http://git.yoctoproject.org/cgit/cgit.cgi/poky" class="bare">http://git.yoctoproject.org/cgit/cgit.cgi/poky</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Other interesting possibilities that I haven&#8217;t evaluated well:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>NixOS <a href="https://nixos.org/" class="bare">https://nixos.org/</a> Seems to support full build from source well. Not much cross compilation information however.</p>
</li>
<li>
<p>Gentoo <a href="https://en.wikipedia.org/wiki/Gentoo_Linux" class="bare">https://en.wikipedia.org/wiki/Gentoo_Linux</a> Seems to support full build from source well.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="soft-topics"><a class="anchor" href="#soft-topics"></a><a class="link" href="#soft-topics">31.19. Soft topics</a></h3>
<div class="sect3">
<h4 id="fairy-tale"><a class="anchor" href="#fairy-tale"></a><a class="link" href="#fairy-tale">31.19.1. Fairy tale</a></h4>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>Once upon a time, there was a boy called Linus.</p>
</div>
<div class="paragraph">
<p>Linus made a super fun toy, and since he was not very humble, decided to call it Linux.</p>
</div>
<div class="paragraph">
<p>Linux was an awesome toy, but it had one big problem: it was very difficult to learn how to play with it!</p>
</div>
<div class="paragraph">
<p>As a result, only some weird kids who were very bored ended up playing with Linux, and everyone thought those kids were very cool, in their own weird way.</p>
</div>
<div class="paragraph">
<p>One day, a mysterious new kid called Ciro tried to play with Linux, and like many before him, got very frustrated, and gave up.</p>
</div>
<div class="paragraph">
<p>A few years later, Ciro had grown up a bit, and by chance came across a very cool toy made by the boy Petazzoni and his gang: it was called Buildroot.</p>
</div>
<div class="paragraph">
<p>Ciro noticed that if you used Buildroot together with Linux, and Linux suddenly became very fun to play with!</p>
</div>
<div class="paragraph">
<p>So Ciro decided to explain to as many kids as possible how to use Buildroot to play with Linux.</p>
</div>
<div class="paragraph">
<p>And so everyone was happy. Except some of the old weird kernel hackers who wanted to keep their mystique, but so be it.</p>
</div>
<div class="paragraph">
<p>THE END</p>
</div>
</blockquote>
</div>
</div>
<div class="sect3">
<h4 id="should-you-waste-your-life-with-systems-programming"><a class="anchor" href="#should-you-waste-your-life-with-systems-programming"></a><a class="link" href="#should-you-waste-your-life-with-systems-programming">31.19.2. Should you waste your life with systems programming?</a></h4>
<div class="paragraph">
<p>Being the hardcore person who fully understands an important complex system such as a computer, it does have a nice ring to it doesn&#8217;t it?</p>
</div>
<div class="paragraph">
<p>But before you dedicate your life to this nonsense, do consider the following points:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>almost all contributions to the kernel are done by large companies, and if you are not an employee in one of them, you are likely not going to be able to do much.</p>
<div class="paragraph">
<p>This can be inferred by the fact that the <code>devices/</code> directory is by far the largest in the kernel.</p>
</div>
<div class="paragraph">
<p>The kernel is of course just an interface to hardware, and the hardware developers start developing their kernel stuff even before specs are publicly released, both to help with hardware development and to have things working when the announcement is made.</p>
</div>
<div class="paragraph">
<p>Furthermore, I believe that there are in-tree devices which have never been properly publicly documented. Linus is of course fine with this, since code == documentation for him, but it is not as easy for mere mortals.</p>
</div>
<div class="paragraph">
<p>There are some less hardware bound higher level layers in the kernel which might not require being in a hardware company, and a few people must be living off it.</p>
</div>
<div class="paragraph">
<p>But of course, those are heavily motivated by the underlying hardware characteristics, and it is very likely that most of the people working there were previously at a hardware company.</p>
</div>
<div class="paragraph">
<p>In that sense, therefore, the kernel is not as open as one might want to believe.</p>
</div>
</li>
<li>
<p>it is impossible to become rich with this knowledge.</p>
<div class="paragraph">
<p>This is partly implied by the fact that you need to be in a big company to make useful low level things, and therefore you will only be a tiny cog in the engine.</p>
</div>
<div class="paragraph">
<p>The key problem is that the entry cost of hardware design is just too insanely high for startups in general.</p>
</div>
</li>
<li>
<p>Is learning this the most useful thing that you think can do for society?</p>
<div class="paragraph">
<p>Or are you just learning it for job security and having a nice sounding title?</p>
</div>
<div class="paragraph">
<p>I&#8217;m not a huge fan of the person, but I think Jobs said it right: <a href="https://www.youtube.com/watch?v=FF-tKLISfPE" class="bare">https://www.youtube.com/watch?v=FF-tKLISfPE</a></p>
</div>
<div class="paragraph">
<p>First determine the useful goal, and then backtrack down to the most efficient thing you can do to reach it.</p>
</div>
</li>
<li>
<p>there are two things that sadden me compared to physics-based engineering:</p>
<div class="openblock">
<div class="content">
<div class="ulist">
<ul>
<li>
<p>you will never become eternally famous. All tech disappears sooner or later, while laws of nature, at least as useful approximations, stay unchanged.</p>
</li>
<li>
<p>every problem that you face is caused by imperfections introduced by other humans.</p>
<div class="paragraph">
<p>It is much easier to accept limitations of physics, and even natural selection in biology, which is are produced by a sentient being (?).</p>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>Physics-based engineering, just like low level hardware, is of course completely closed source however, since wrestling against the laws of physics is about the most expensive thing humans can do.</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Are you fine with those points, and ready to continue wasting your life?</p>
</div>
<div class="paragraph">
<p>Good. In that case, read on, and let&#8217;s have some fun together ;-)</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="bibliography"><a class="anchor" href="#bibliography"></a><a class="link" href="#bibliography">31.20. Bibliography</a></h3>
<div class="paragraph">
<p>Runnable stuff:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://lwn.net/Kernel/LDD3/" class="bare">https://lwn.net/Kernel/LDD3/</a> the best book, but outdated. Updated source: <a href="https://github.com/martinezjavier/ldd3" class="bare">https://github.com/martinezjavier/ldd3</a> But examples non-minimal and take too much brain power to understand.</p>
</li>
<li>
<p><a href="https://github.com/satoru-takeuchi/elkdat" class="bare">https://github.com/satoru-takeuchi/elkdat</a> manual build process without Buildroot, very few and simple kernel modules. But it seem ktest + QEMU working, which is awesome. <code>./test</code> there patches ktest config dynamically based on CLI! Maybe we should just steal it since GPL licensed.</p>
</li>
<li>
<p><a href="https://github.com/tinyclub/linux-lab" class="bare">https://github.com/tinyclub/linux-lab</a> Buildroot based, no kernel modules?</p>
</li>
<li>
<p><a href="https://github.com/agelastic/eudyptula" class="bare">https://github.com/agelastic/eudyptula</a></p>
</li>
<li>
<p><a href="https://github.com/linux-kernel-labs" class="bare">https://github.com/linux-kernel-labs</a> Yocto based, source inside a kernel fork subdir: <a href="https://github.com/linux-kernel-labs/linux/tree/f08b9e4238dfc612a9d019e3705bd906930057fc/tools/labs" class="bare">https://github.com/linux-kernel-labs/linux/tree/f08b9e4238dfc612a9d019e3705bd906930057fc/tools/labs</a> which the author would like to upstream <a href="https://www.reddit.com/r/programming/comments/79w2q9/linux_device_driver_labs_the_linux_kernel/dp6of43/" class="bare">https://www.reddit.com/r/programming/comments/79w2q9/linux_device_driver_labs_the_linux_kernel/dp6of43/</a></p>
</li>
<li>
<p>Android AOSP: <a href="https://stackoverflow.com/questions/1809774/how-to-compile-the-android-aosp-kernel-and-test-it-with-the-android-emulator/48310014#48310014" class="bare">https://stackoverflow.com/questions/1809774/how-to-compile-the-android-aosp-kernel-and-test-it-with-the-android-emulator/48310014#48310014</a> AOSP is basically a uber bloated Buildroot (2 hours build vs 30 minutes), Android is Linux based, and QEMU is the emulator backend. These instructions might work for debugging the kernel: <a href="https://github.com/Fuzion24/AndroidKernelExploitationPlayground" class="bare">https://github.com/Fuzion24/AndroidKernelExploitationPlayground</a></p>
</li>
<li>
<p><a href="https://github.com/s-matyukevich/raspberry-pi-os" class="bare">https://github.com/s-matyukevich/raspberry-pi-os</a> Does both an OS from scratch, and annotates the corresponding kernel source code. For RPI3, no QEMU support: <a href="https://github.com/s-matyukevich/raspberry-pi-os/issues/8" class="bare">https://github.com/s-matyukevich/raspberry-pi-os/issues/8</a></p>
</li>
<li>
<p><a href="https://github.com/pw4ever/linux-kernel-hacking-helper" class="bare">https://github.com/pw4ever/linux-kernel-hacking-helper</a> as of bd9952127e7eda643cbb6cb4c51ad7b5b224f438, Bash, Arch Linux rootfs</p>
</li>
<li>
<p><a href="https://github.com/MichielDerhaeg/build-linux" class="bare">https://github.com/MichielDerhaeg/build-linux</a> untested. Manually builds musl and BusyBox, no Buildroot. Seems to use host packaged toolchain and tested on x86_64 only. Might contain a minimized kernel config.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Theory:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="http://nairobi-embedded.org" class="bare">http://nairobi-embedded.org</a> you will fall here a lot when you start popping the hard QEMU Google queries. They have covered everything we do here basically, but with a more manual approach, while this repo automates everything.</p>
<div class="paragraph">
<p>I couldn&#8217;t find the markup source code for the tutorials, and as a result when the domain went down in May 2018, you have to use <a href="http://web.archive.org/" class="bare">http://web.archive.org/</a> to see the pages&#8230;&#8203;</p>
</div>
</li>
<li>
<p><a href="https://balau82.wordpress.com" class="bare">https://balau82.wordpress.com</a> awesome low level resource</p>
</li>
<li>
<p><a href="https://rwmj.wordpress.com/" class="bare">https://rwmj.wordpress.com/</a> awesome red hatter</p>
</li>
<li>
<p><a href="https://lwn.net" class="bare">https://lwn.net</a></p>
</li>
<li>
<p><a href="http://www.makelinux.net" class="bare">http://www.makelinux.net</a></p>
</li>
<li>
<p><a href="https://notes.shichao.io/lkd/" class="bare">https://notes.shichao.io/lkd/</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Awesome lists:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/gurugio/lowlevelprogramming-university" class="bare">https://github.com/gurugio/lowlevelprogramming-university</a></p>
</li>
<li>
<p><a href="https://github.com/uhub/awesome-c" class="bare">https://github.com/uhub/awesome-c</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</body>
</html>