124 lines
11 KiB
HTML
124 lines
11 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `r0` crate."><meta name="keywords" content="rust, rustlang, rust-lang, r0"><title>r0 - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><p class='location'>Crate r0</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#macros">Macros</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'r0', ty: 'mod', relpath: '../'};</script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../src/r0/lib.rs.html#1-214' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>r0</a></span></h1><div class='docblock'><p>Initialization code ("crt0") written in Rust</p>
|
||
<p>This is for bare metal systems where there is no ELF loader or OS to take
|
||
care of initializing RAM for the program.</p>
|
||
<h1 id="initializing-ram" class="section-header"><a href="#initializing-ram">Initializing RAM</a></h1>
|
||
<p>On the linker script side, we must assign names (symbols) to the boundaries
|
||
of the <code>.bss</code> and <code>.data</code> sections.</p>
|
||
<pre><code class="language-text">.bss : ALIGN(4)
|
||
{
|
||
_sbss = .;
|
||
*(.bss.*);
|
||
_ebss = ALIGN(4);
|
||
} > RAM
|
||
|
||
.data : ALIGN(4)
|
||
{
|
||
_sdata = .;
|
||
*(.data.*);
|
||
_edata = ALIGN(4);
|
||
} > RAM AT > FLASH
|
||
|
||
_sidata = LOADADDR(.data);
|
||
</code></pre>
|
||
<p>On the Rust side, we must bind to those symbols using an <code>extern</code> block.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">unsafe</span> <span class="kw">fn</span> <span class="ident">before_main</span>() {
|
||
<span class="comment">// The type, `u32`, indicates that the memory is 4-byte aligned</span>
|
||
<span class="kw">extern</span> <span class="string">"C"</span> {
|
||
<span class="kw">static</span> <span class="kw-2">mut</span> <span class="ident">_sbss</span>: <span class="ident">u32</span>;
|
||
<span class="kw">static</span> <span class="kw-2">mut</span> <span class="ident">_ebss</span>: <span class="ident">u32</span>;
|
||
|
||
<span class="kw">static</span> <span class="kw-2">mut</span> <span class="ident">_sdata</span>: <span class="ident">u32</span>;
|
||
<span class="kw">static</span> <span class="kw-2">mut</span> <span class="ident">_edata</span>: <span class="ident">u32</span>;
|
||
|
||
<span class="kw">static</span> <span class="ident">_sidata</span>: <span class="ident">u32</span>;
|
||
}
|
||
|
||
<span class="ident">zero_bss</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">_sbss</span>, <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">_ebss</span>);
|
||
<span class="ident">init_data</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">_sdata</span>, <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">_edata</span>, <span class="kw-2">&</span><span class="ident">_sidata</span>);
|
||
}</pre>
|
||
<h1 id="init_array--pre_init_array" class="section-header"><a href="#init_array--pre_init_array"><code>.init_array</code> & <code>.pre_init_array</code></a></h1>
|
||
<p>This crate also provides an API to add "life before main" functionality to
|
||
bare metal systems.</p>
|
||
<p>On the linker script side, instruct the linker to keep the <code>.init_array</code>
|
||
sections from input object files. Store the start and end address of the
|
||
merged <code>.init_array</code> section.</p>
|
||
<pre><code class="language-text">.text :
|
||
{
|
||
/* .. */
|
||
_init_array_start = ALIGN(4);
|
||
KEEP(*(.init_array));
|
||
_init_array_end = ALIGN(4);
|
||
/* .. */
|
||
}
|
||
</code></pre>
|
||
<p>On the startup code, invoke the <code>run_init_array</code> function <em>before</em> you call
|
||
the user <code>main</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">unsafe</span> <span class="kw">fn</span> <span class="ident">start</span>() {
|
||
<span class="kw">extern</span> <span class="string">"C"</span> {
|
||
<span class="kw">static</span> <span class="ident">_init_array_start</span>: <span class="kw">extern</span> <span class="string">"C"</span> <span class="kw">fn</span>();
|
||
<span class="kw">static</span> <span class="ident">_init_array_end</span>: <span class="kw">extern</span> <span class="string">"C"</span> <span class="kw">fn</span>();
|
||
}
|
||
|
||
::<span class="ident">r0</span>::<span class="ident">run_init_array</span>(<span class="kw-2">&</span><span class="ident">_init_array_start</span>, <span class="kw-2">&</span><span class="ident">_init_array_end</span>);
|
||
|
||
<span class="kw">extern</span> <span class="string">"C"</span> {
|
||
<span class="kw">fn</span> <span class="ident">main</span>(<span class="ident">argc</span>: <span class="ident">isize</span>, <span class="ident">argv</span>: <span class="kw-2">*</span><span class="kw">const</span> <span class="kw-2">*</span><span class="kw">const</span> <span class="ident">u8</span>) <span class="op">-></span> <span class="ident">isize</span>;
|
||
}
|
||
|
||
<span class="ident">main</span>();
|
||
}</pre>
|
||
<p>Then the user application can use this crate <code>init_array!</code> macro to run code
|
||
before <code>main</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="macro">init_array</span><span class="macro">!</span>(<span class="ident">before_main</span>, {
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello"</span>);
|
||
});
|
||
|
||
<span class="kw">fn</span> <span class="ident">main</span>() {
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"World"</span>);
|
||
}</pre>
|
||
</div><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class="macro" href="macro.init_array.html"
|
||
title='macro r0::init_array'>init_array</a></td>
|
||
<td class='docblock-short'>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="macro" href="macro.pre_init_array.html"
|
||
title='macro r0::pre_init_array'>pre_init_array</a></td>
|
||
<td class='docblock-short'>
|
||
|
||
</td>
|
||
</tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class="fn" href="fn.init_data.html"
|
||
title='fn r0::init_data'>init_data</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td>
|
||
<td class='docblock-short'>
|
||
<p>Initializes the <code>.data</code> section</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="fn" href="fn.run_init_array.html"
|
||
title='fn r0::run_init_array'>run_init_array</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td>
|
||
<td class='docblock-short'>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="fn" href="fn.zero_bss.html"
|
||
title='fn r0::zero_bss'>zero_bss</a><a title='unsafe function' href='#'><sup>⚠</sup></a></td>
|
||
<td class='docblock-short'>
|
||
<p>Zeroes the <code>.bss</code> section</p>
|
||
|
||
</td>
|
||
</tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "r0";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html> |