<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Edgar Luque - kernel</title>
    <subtitle>Software Developer</subtitle>
    <link rel="self" type="application/atom+xml" href="https://edgl.dev/categories/kernel/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://edgl.dev"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2025-08-30T00:00:00+00:00</updated>
    <id>https://edgl.dev/categories/kernel/atom.xml</id>
    <entry xml:lang="en">
        <title>Creating an x86_64 kernel in Rust: Part 2</title>
        <published>2025-08-30T00:00:00+00:00</published>
        <updated>2025-08-30T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://edgl.dev/blog/creating-a-kernel-p2/"/>
        <id>https://edgl.dev/blog/creating-a-kernel-p2/</id>
        
        <content type="html" xml:base="https://edgl.dev/blog/creating-a-kernel-p2/">&lt;h2 id=&quot;adding-serial-output&quot;&gt;Adding Serial output&lt;&#x2F;h2&gt;
&lt;p&gt;QEMU provides serial output by specifying it in the cli options:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;-device&lt;&#x2F;span&gt;&lt;span&gt; isa-debug-exit,iobase=0xf4,iosize=0x04 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;-serial&lt;&#x2F;span&gt;&lt;span&gt; stdio &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The device isa-debug-exit allows us to exit QEMU easily.&lt;&#x2F;p&gt;
&lt;p&gt;The serial output is a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;16550_UART&quot;&gt;16550 UART&lt;&#x2F;a&gt; universal asynchronous receiver-transmitter.&lt;&#x2F;p&gt;
&lt;p&gt;For this, we will use the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;uart_16550&#x2F;latest&#x2F;uart_16550&#x2F;&quot;&gt;uart_16550&lt;&#x2F;a&gt; crate, which interfaces with the port-mapped I&#x2F;O (basically reading&#x2F;writing to specific I&#x2F;O port addresses to communicate with hardware).&lt;&#x2F;p&gt;
&lt;p&gt;First, let&#x27;s add the dependency to our &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;uart_16550 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;0.3.1&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s create a file named &lt;code&gt;serial.rs&lt;&#x2F;code&gt;, where we will add a serial_println macro, so we can have nice debug output.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;core&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#39bae6;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; Write}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;spin&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{Once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span&gt;mutex&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;Mutex}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;uart_16550&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;SerialPort&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;SERIAL_DBG&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;Once&amp;lt;Mutex&amp;lt;SerialPort&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;Once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;new()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;init&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;SERIAL_DBG&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;call_once&lt;&#x2F;span&gt;&lt;span&gt;(|| {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; port &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;unsafe &lt;&#x2F;span&gt;&lt;span&gt;{ uart_16550&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;SerialPort&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;0x3F8&lt;&#x2F;span&gt;&lt;span&gt;) }&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;        port&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;init&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;        Mutex&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;new(port)
&lt;&#x2F;span&gt;&lt;span&gt;    })&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;macro_export&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;serial_print &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;$arg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;tt&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;($crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;serial&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;_serial_print(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;format_args!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;($arg)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)))&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;macro_export&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;serial_println &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    () &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;($crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;serial_print&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#95e6cb;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;$arg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;tt&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;($crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;serial_print&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#95e6cb;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;format_args!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;($arg)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;)))&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;doc&lt;&#x2F;span&gt;&lt;span&gt;(hidden)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;_serial_print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;fmt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;Arguments) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;unsafe &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; This is safe because we only call this after init() has been called,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; and the UART is properly initialized
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;SERIAL_DBG
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;unwrap_unchecked&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;write_fmt&lt;&#x2F;span&gt;&lt;span&gt;(args)
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;format_args!&lt;&#x2F;code&gt; doesn&#x27;t heap allocate, so this works even before we have a heap allocator.&lt;&#x2F;p&gt;
&lt;p&gt;The port &lt;code&gt;0x3F8&lt;&#x2F;code&gt; is the COM1 port, which is the standard first serial port address on x86 systems. QEMU maps this to the serial output we configured. You can find out more in the &lt;a href=&quot;https:&#x2F;&#x2F;wiki.osdev.org&#x2F;Serial_Ports&quot;&gt;osdev&lt;&#x2F;a&gt; wiki.&lt;&#x2F;p&gt;
&lt;p&gt;Don&#x27;t forget to add the module to your main.rs:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;serial&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since we want to have serial output as early as possible (especially for debugging panics during early boot), I added the init call to the &lt;code&gt;boot.rs&lt;&#x2F;code&gt; file:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; boot.rs
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;unsafe&lt;&#x2F;span&gt;&lt;span&gt;(no_mangle)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;unsafe extern &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;C&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;kmain&lt;&#x2F;span&gt;&lt;span&gt;() &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;! &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; All limine requests must also be referenced in a called function, otherwise they may be
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; removed by the linker.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;assert!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;BASE_REVISION&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;is_supported&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; Early init serial in case we panic on expects.
&lt;&#x2F;span&gt;&lt;span&gt;    serial&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;init()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since we can print now, let&#x27;s modify the panic handler to print the panic info:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; main.rs
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;panic_handler&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;rust_panic&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;core&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;panic&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;PanicInfo) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;! &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;serial_println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;KERNEL PANIC:&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;serial_println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;{info:#?}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;hlt&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;testing-it-out&quot;&gt;Testing it out&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s add a simple test to the main function to see the serial output working:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;! &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;serial_println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;Hello from the kernel!&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;serial_println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;Serial output is working: {}&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;hlt&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When you run &lt;code&gt;make run&lt;&#x2F;code&gt;, you should see the output in your terminal since we configured QEMU with &lt;code&gt;-serial stdio&lt;&#x2F;code&gt;. If everything is working correctly, you&#x27;ll see your messages printed alongside any QEMU boot messages.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Creating an x86_64 kernel in Rust: Part 1</title>
        <published>2025-08-28T00:00:00+00:00</published>
        <updated>2025-08-28T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://edgl.dev/blog/creating-a-kernel/"/>
        <id>https://edgl.dev/blog/creating-a-kernel/</id>
        
        <content type="html" xml:base="https://edgl.dev/blog/creating-a-kernel/">&lt;p&gt;This is some kind of blog series about my journey as I learn and implement my own Rust kernel.&lt;&#x2F;p&gt;
&lt;p&gt;Any knowledge shared here may not be fully true, since I do this as a hobby and I&#x27;m not an experienced kernel developer.
I&#x27;m just doing this for fun, learning about the true low level bits that make running a computer with the x86_64 architecture possible.&lt;&#x2F;p&gt;
&lt;p&gt;Sometimes I will mention some names or mnemonics, and maybe I won&#x27;t explain them here, but I always try to put relevant links to more information.
After all, if you want to make a kernel, you will need to do research, &lt;strong&gt;a lot&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I also won&#x27;t be doing everything from scratch, for example I will use the &lt;code&gt;x86_64&lt;&#x2F;code&gt; crate for some interactions with registers, and the &lt;code&gt;acpi&lt;&#x2F;code&gt; crate to parse and use the ACPI tables.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-bootloader&quot;&gt;The bootloader&lt;&#x2F;h2&gt;
&lt;p&gt;I&#x27;ll be using &lt;a href=&quot;https:&#x2F;&#x2F;limine-bootloader.org&#x2F;&quot;&gt;limine&lt;&#x2F;a&gt;, for some reasons:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;I like it. Feels simple and modern.&lt;&#x2F;li&gt;
&lt;li&gt;It provides a really nice &lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;limine&quot;&gt;crate&lt;&#x2F;a&gt; with a &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;jasondyoungberg&#x2F;limine-rust-template&quot;&gt;template&lt;&#x2F;a&gt; to create a kernel.&lt;&#x2F;li&gt;
&lt;li&gt;We have bootloader support with UEFI out of the box, which is what most modern systems use (instead of the older BIOS).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;To start, go to the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;jasondyoungberg&#x2F;limine-rust-template&quot;&gt;template&lt;&#x2F;a&gt; and clone it or use the template button.&lt;&#x2F;p&gt;
&lt;p&gt;This template uses limine, and has a nice makefile to build and run the kernel under QEMU,
the makefile also downloads the UEFI firmware (&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tianocore&#x2F;tianocore.github.io&#x2F;wiki&#x2F;OVMF&quot;&gt;OVMF&lt;&#x2F;a&gt;) needed by QEMU to use UEFI.&lt;&#x2F;p&gt;
&lt;p&gt;I also recommend creating a &lt;code&gt;.cargo&lt;&#x2F;code&gt; file inside the kernel folder with the following &lt;code&gt;config.toml&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;build&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;target &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;x86_64-unknown-none&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;x86_64-unknown-none&lt;&#x2F;code&gt; is a baremetal target that fits perfectly to create a kernel.&lt;&#x2F;p&gt;
&lt;p&gt;If you are using vscode I also recommend to create a &lt;code&gt;settings.json&lt;&#x2F;code&gt; with the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;rust-analyzer.cargo.target&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;x86_64-unknown-none&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;rust-analyzer.cargo.allTargets&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;false
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You should look a bit around at the makefile to understand it.&lt;&#x2F;p&gt;
&lt;p&gt;I will only focus on x86_64 with UEFI, so I modified a bit the relevant runners (the one that uses cdrom and the one with hdd):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;make&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-make &quot;&gt;&lt;code class=&quot;language-make&quot; data-lang=&quot;make&quot;&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;.PHONY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;run-x86_64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;run-x86_64&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;ovmf&#x2F;ovmf-code-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;KARCH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;.fd ovmf&#x2F;ovmf-vars-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;KARCH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;.fd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;IMAGE_NAME&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;.iso
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;qemu-system-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;KARCH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-M&lt;&#x2F;span&gt;&lt;span&gt; q35 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-drive&lt;&#x2F;span&gt;&lt;span&gt; if=pflash,unit=0,format=raw,file=ovmf&#x2F;ovmf-code-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;KARCH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.fd,readonly=on &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-drive&lt;&#x2F;span&gt;&lt;span&gt; if=pflash,unit=1,format=raw,file=ovmf&#x2F;ovmf-vars-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;KARCH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.fd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-cdrom &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;IMAGE_NAME&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.iso &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-device&lt;&#x2F;span&gt;&lt;span&gt; isa-debug-exit,iobase=0xf4,iosize=0x04 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-serial&lt;&#x2F;span&gt;&lt;span&gt; stdio &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-no-reboot &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;QEMUFLAGS&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;.PHONY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;run-hdd-x86_64
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;run-hdd-x86_64&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;ovmf&#x2F;ovmf-code-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;KARCH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;.fd ovmf&#x2F;ovmf-vars-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;KARCH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;.fd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;IMAGE_NAME&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;.hdd
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;qemu-system-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;KARCH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-M&lt;&#x2F;span&gt;&lt;span&gt; q35 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-drive&lt;&#x2F;span&gt;&lt;span&gt; if=pflash,unit=0,format=raw,file=ovmf&#x2F;ovmf-code-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;KARCH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.fd,readonly=on &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-drive&lt;&#x2F;span&gt;&lt;span&gt; if=pflash,unit=1,format=raw,file=ovmf&#x2F;ovmf-vars-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;KARCH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.fd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-hda &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;IMAGE_NAME&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.hdd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-device&lt;&#x2F;span&gt;&lt;span&gt; isa-debug-exit,iobase=0xf4,iosize=0x04 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-serial&lt;&#x2F;span&gt;&lt;span&gt; stdio &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;		-no-reboot &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;\
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;QEMUFLAGS&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The main changes are adding a serial for debug output, and disabling automatic reboot.&lt;&#x2F;p&gt;
&lt;p&gt;Disabling reboot is handy in case the kernel &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Triple_fault&quot;&gt;triple faults&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;limine&quot;&gt;Limine&lt;&#x2F;h2&gt;
&lt;p&gt;With limine, you make &quot;requests&quot; to the bootloader to provide you with some information, for example to get the framebuffer, you request for it with:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;used&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;unsafe&lt;&#x2F;span&gt;&lt;span&gt;(link_section &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;.requests&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;pub static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;FRAMEBUFFER_REQUEST&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; FramebufferRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;FramebufferRequest&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;new()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This works alongside the support of a custom linker script, which comes with the template.&lt;&#x2F;p&gt;
&lt;p&gt;The linker script is needed because we need to tell the linker exactly where to put our requests in memory so that limine can find them. Without it, the linker might put our data anywhere, and limine wouldn&#x27;t know where to look:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#0f1419;color:#bfbab0;&quot;&gt;&lt;code&gt;&lt;span&gt;.data : {
&lt;&#x2F;span&gt;&lt;span&gt;    *(.data .data.*)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &#x2F;* Place the sections that contain the Limine requests as part of the .data *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    &#x2F;* output section. *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    KEEP(*(.requests_start_marker))
&lt;&#x2F;span&gt;&lt;span&gt;    KEEP(*(.requests))
&lt;&#x2F;span&gt;&lt;span&gt;    KEEP(*(.requests_end_marker))
&lt;&#x2F;span&gt;&lt;span&gt;} :data
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You should read the linker script and understand it.&lt;&#x2F;p&gt;
&lt;p&gt;See &lt;a href=&quot;https:&#x2F;&#x2F;wiki.osdev.org&#x2F;Linker_Scripts&quot;&gt;wiki.osdev.org&#x2F;Linker_Scripts&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;sourceware.org&#x2F;binutils&#x2F;docs&#x2F;ld&#x2F;Scripts.html#Scripts&quot;&gt;docs&#x2F;ld&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;On the script, you will also see a relevant entry:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#0f1419;color:#bfbab0;&quot;&gt;&lt;code&gt;&lt;span&gt;&#x2F;* We want to be placed in the topmost 2GiB of the address space, for optimisations *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;* and because that is what the Limine spec mandates. *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;* Any address in this region will do, but often 0xffffffff80000000 is chosen as *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;* that is the beginning of the region. *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;. = 0xffffffff80000000;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This puts our kernel in the higher half virtual address range, which can only be accessed in ring 0 (the highest privilege level where the kernel runs), it also puts the kernel at the very top 2gb range.&lt;&#x2F;p&gt;
&lt;p&gt;The higher half approach is pretty common for kernels because it gives us some nice benefits: it protects the kernel from user programs (they can&#x27;t access these addresses), and it makes memory management easier since we can map the kernel at the same virtual address in every process.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;small-intro-to-paging&quot;&gt;Small intro to paging&lt;&#x2F;h2&gt;
&lt;p&gt;Paging&#x27;s main use is to provide each process with its own &quot;virtual address space&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Virtual memory is divided into fixed-size blocks called pages, while physical memory is divided into equally sized page frames. Each page can be individually mapped to any frame, avoiding memory fragmentation.
This will be the job of our frame allocator alongside the memory mapper.&lt;&#x2F;p&gt;
&lt;p&gt;On x86_64, paging is achieved through the Memory Management Unit (MMU) using a series of tables:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;4-level page tables: PML4 -&amp;gt; PDPT -&amp;gt; PD -&amp;gt; PT&lt;&#x2F;li&gt;
&lt;li&gt;48-bit virtual addresses (using canonical addressing)&lt;&#x2F;li&gt;
&lt;li&gt;4 KiB page size (typically)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The 48-bit virtual addresses need to be canonical, that is, they have to be sign extended. Also there is a gap in virtual addresses that aren&#x27;t valid due to this.&lt;&#x2F;p&gt;
&lt;p&gt;Each page table has 512 entries of 8 bytes each, requiring 9 bits to address each entry.&lt;&#x2F;p&gt;
&lt;p&gt;When the CPU needs to translate a virtual address, it basically does this: takes the virtual address, splits it into chunks (9 bits for each level), and uses each chunk as an index to walk through the page tables until it finds the physical address. It&#x27;s like following a path through multiple directories to find a file.&lt;&#x2F;p&gt;
&lt;p&gt;To improve performance, x86_64 caches recent translations in the Translation Lookaside Buffer (TLB), allowing translations to skip the multi-level table walk when cached.
The TLB must be manually invalidated by the kernel when page tables change using the &lt;code&gt;invlpg&lt;&#x2F;code&gt; instruction&lt;&#x2F;p&gt;
&lt;p&gt;Check out specially the osdev wiki on paging to understand more.&lt;&#x2F;p&gt;
&lt;p&gt;See more:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;os.phil-opp.com&#x2F;paging-implementation&#x2F;&quot;&gt;os.phil-opp.com&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;wiki.osdev.org&#x2F;Paging&quot;&gt;wiki.osdev.org&#x2F;Paging&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;cs61.seas.harvard.edu&#x2F;wiki&#x2F;2016&#x2F;Kernel2X&#x2F;&quot;&gt;Introduction to Virtual Memory and the x86-64 MMU&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;read.seas.harvard.edu&#x2F;cs161&#x2F;2018&#x2F;doc&#x2F;memory-layout&#x2F;&quot;&gt;CS 161 - Memory layout&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;reorganizing&quot;&gt;Reorganizing&lt;&#x2F;h2&gt;
&lt;p&gt;I moved most of the main.rs code to a &lt;code&gt;boot.rs&lt;&#x2F;code&gt; file, where I&#x27;ll have most limine related stuff, the real entry point, which will call the main at &lt;code&gt;main.rs&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I did this because it&#x27;s good to separate the boot logic from the actual kernel logic. The boot stuff is pretty specific to limine and getting everything set up, while main.rs should focus on the actual kernel functionality. Makes things cleaner and easier to understand.&lt;&#x2F;p&gt;
&lt;p&gt;Also created a BootInfo struct alongside a global static &lt;code&gt;BOOT_INFO&lt;&#x2F;code&gt; to access it easily.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; boot.rs
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F;&#x2F; Sets the base revision to the latest revision supported by the crate.
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F;&#x2F; See specification for further info.
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F;&#x2F; Be sure to mark all limine requests with #[used], otherwise they may be removed by the compiler.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;used&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; The .requests section allows limine to find the requests faster and more safely.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;unsafe&lt;&#x2F;span&gt;&lt;span&gt;(link_section &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;.requests&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;pub static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;BASE_REVISION&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; BaseRevision &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;BaseRevision&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;new()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;used&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;unsafe&lt;&#x2F;span&gt;&lt;span&gt;(link_section &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;.requests&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;pub static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;FRAMEBUFFER_REQUEST&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; FramebufferRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;FramebufferRequest&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;new()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F;&#x2F; Define the start and end markers for Limine requests.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;used&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;unsafe&lt;&#x2F;span&gt;&lt;span&gt;(link_section &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;.requests_start_marker&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;_START_MARKER&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; RequestsStartMarker &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;RequestsStartMarker&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;new()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;used&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;unsafe&lt;&#x2F;span&gt;&lt;span&gt;(link_section &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;.requests_end_marker&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;_END_MARKER&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; RequestsEndMarker &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;RequestsEndMarker&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;new()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(unused)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;pub struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;BootInfo &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span&gt;framebuffer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;Framebuffer&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;&amp;#39;static&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;pub static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;BOOT_INFO&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;Once&amp;lt;BootInfo&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;Once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;new()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;boot_info&lt;&#x2F;span&gt;&lt;span&gt;() &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;&amp;#39;static&lt;&#x2F;span&gt;&lt;span&gt; BootInfo {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;unsafe &lt;&#x2F;span&gt;&lt;span&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;BOOT_INFO&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;unwrap_unchecked&lt;&#x2F;span&gt;&lt;span&gt;() }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;unsafe&lt;&#x2F;span&gt;&lt;span&gt;(no_mangle)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;unsafe extern &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;C&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;kmain&lt;&#x2F;span&gt;&lt;span&gt;() &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;! &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; All limine requests must also be referenced in a called function, otherwise they may be
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; removed by the linker.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;assert!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;BASE_REVISION&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;is_supported&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; framebuffer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;FRAMEBUFFER_REQUEST
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;get_response&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;need a framebuffer&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;framebuffers&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;need a framebuffer&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; boot_info &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; BootInfo {
&lt;&#x2F;span&gt;&lt;span&gt;        framebuffer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;BOOT_INFO&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;call_once&lt;&#x2F;span&gt;&lt;span&gt;(|| boot_info)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#5c6773;&quot;&gt;&#x2F;&#x2F; main.rs
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;x86_64&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;instructions&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;hlt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;boot&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;! &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;hlt&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;panic_handler&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffb454;&quot;&gt;rust_panic&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29718;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;core&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;panic&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;PanicInfo) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f29668;&quot;&gt;! &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff7733;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f07178;&quot;&gt;hlt&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bfbab0cc;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;Once&lt;&#x2F;code&gt; comes from the spin crate (it&#x27;s like a no_std OnceCell), and &lt;code&gt;hlt&lt;&#x2F;code&gt; from the &lt;code&gt;x86_64&lt;&#x2F;code&gt; crate.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;hlt&lt;&#x2F;code&gt; instruction is better than just doing &lt;code&gt;loop {}&lt;&#x2F;code&gt; because it actually puts the CPU to sleep until an interrupt happens, which saves power and doesn&#x27;t waste CPU cycles.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#0f1419;color:#bfbab0;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;spin &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;0.10.0&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#59c2ff;&quot;&gt;x86_64 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c2d94c;&quot;&gt;&amp;quot;0.15.2&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;what-s-next&quot;&gt;What&#x27;s next?&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;Setting up a frame allocator&lt;&#x2F;li&gt;
&lt;li&gt;Creating a memory mapper&lt;&#x2F;li&gt;
&lt;li&gt;Initializing the kernel heap&lt;&#x2F;li&gt;
&lt;li&gt;Creating the GDT.&lt;&#x2F;li&gt;
&lt;li&gt;Handling interrupts and exceptions&lt;&#x2F;li&gt;
&lt;li&gt;Parsing the ACPI tables.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
</feed>
