{"componentChunkName":"component---src-templates-post-template-js","path":"/unix-xv6-007-kernel-main-04-en","result":{"data":{"markdownRemark":{"id":"b0f87460-0d5b-5467-9bba-bcfb12604d0b","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/unix-xv6-007-kernel-main-04\">original page</a>.</p>\n</blockquote>\n<p>Inspired by <a href=\"https://amzn.to/3q8TU3K\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">An Introduction to OS Code Reading: Learning Kernel Internals with UNIX V6</a>, I’m reading <a href=\"https://github.com/mit-pdos/xv6-public\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">xv6 OS</a>.</p>\n<p>Because UNIX V6 itself does not run on x86 CPUs, I decided to read the source of <a href=\"https://github.com/kash1064/xv6-public\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">kash1064/xv6-public: xv6 OS</a>, a fork of the <a href=\"https://github.com/mit-pdos/xv6-public\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">xv6 OS</a> repository that makes UNIX V6 run on the x86 architecture.</p>\n<p>In the <a href=\"/unix-xv6-006-kernel-main-03-en\">previous article</a>, I looked at how the <code class=\"language-text\">mpinit</code> function retrieves CPU information in a multiprocessor configuration.</p>\n<p>This time, I will trace the behavior of the <code class=\"language-text\">lapicinit</code> function.</p>\n<!-- omit in toc -->\n<h2 id=\"table-of-contents\" style=\"position:relative;\"><a href=\"#table-of-contents\" aria-label=\"table of contents permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Table of Contents</h2>\n<ul>\n<li>\n<p><a href=\"#the-lapicinit-function\">The <code class=\"language-text\">lapicinit</code> function</a></p>\n<ul>\n<li><a href=\"#local-apic-registers\">Local APIC registers</a></li>\n<li><a href=\"#setting-the-spurious-interrupt-vector-and-enabling-the-local-apic\">Setting the Spurious Interrupt Vector and enabling the Local APIC</a></li>\n<li><a href=\"#timer-configuration\">Timer configuration</a></li>\n<li><a href=\"#disable-logical-interrupt-lines\">Disable logical interrupt lines</a></li>\n<li><a href=\"#disable-performance-counter-overflow-interrupts\">Disable performance counter overflow interrupts</a></li>\n<li><a href=\"#setting-the-error-register\">Setting the Error Register</a></li>\n<li><a href=\"#clearing-the-esr\">Clearing the ESR</a></li>\n<li><a href=\"#checking-the-eoi\">Checking the EOI</a></li>\n<li><a href=\"#setting-the-interrupt-command-register\">Setting the Interrupt Command Register</a></li>\n</ul>\n</li>\n<li><a href=\"#summary\">Summary</a></li>\n<li><a href=\"#reference-books\">Reference Books</a></li>\n</ul>\n<h2 id=\"the-lapicinit-function\" style=\"position:relative;\"><a href=\"#the-lapicinit-function\" aria-label=\"the lapicinit function permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The <code class=\"language-text\">lapicinit</code> function</h2>\n<p>This time I will start from <code class=\"language-text\">lapicinit</code>, the first function called in <code class=\"language-text\">main</code>.</p>\n<p>This function initializes the interrupt controller.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token function\">lapicinit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>     <span class=\"token comment\">// interrupt controller</span></code></pre></div>\n<p>The <code class=\"language-text\">lapicinit</code> function is defined in <code class=\"language-text\">lapic.c</code> as follows.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token keyword\">void</span> <span class=\"token function\">lapicinit</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">void</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>lapic<span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Enable local APIC; set spurious interrupt vector.</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>SVR<span class=\"token punctuation\">,</span> ENABLE <span class=\"token operator\">|</span> <span class=\"token punctuation\">(</span>T_IRQ0 <span class=\"token operator\">+</span> IRQ_SPURIOUS<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// The timer repeatedly counts down at bus frequency</span>\n  <span class=\"token comment\">// from lapic[TICR] and then issues an interrupt.</span>\n  <span class=\"token comment\">// If xv6 cared more about precise timekeeping,</span>\n  <span class=\"token comment\">// TICR would be calibrated using an external time source.</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>TDCR<span class=\"token punctuation\">,</span> X1<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>TIMER<span class=\"token punctuation\">,</span> PERIODIC <span class=\"token operator\">|</span> <span class=\"token punctuation\">(</span>T_IRQ0 <span class=\"token operator\">+</span> IRQ_TIMER<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>TICR<span class=\"token punctuation\">,</span> <span class=\"token number\">10000000</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Disable logical interrupt lines.</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>LINT0<span class=\"token punctuation\">,</span> MASKED<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>LINT1<span class=\"token punctuation\">,</span> MASKED<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Disable performance counter overflow interrupts</span>\n  <span class=\"token comment\">// on machines that provide that interrupt entry.</span>\n  <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span>lapic<span class=\"token punctuation\">[</span>VER<span class=\"token punctuation\">]</span><span class=\"token operator\">>></span><span class=\"token number\">16</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">&amp;</span> <span class=\"token number\">0xFF</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">>=</span> <span class=\"token number\">4</span><span class=\"token punctuation\">)</span> <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>PCINT<span class=\"token punctuation\">,</span> MASKED<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Map error interrupt to IRQ_ERROR.</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>ERROR<span class=\"token punctuation\">,</span> T_IRQ0 <span class=\"token operator\">+</span> IRQ_ERROR<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Clear error status register (requires back-to-back writes).</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>ESR<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>ESR<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Ack any outstanding interrupts.</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>EOI<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Send an Init Level De-Assert to synchronise arbitration ID's.</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>ICRHI<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>ICRLO<span class=\"token punctuation\">,</span> BCAST <span class=\"token operator\">|</span> INIT <span class=\"token operator\">|</span> LEVEL<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">while</span><span class=\"token punctuation\">(</span>lapic<span class=\"token punctuation\">[</span>ICRLO<span class=\"token punctuation\">]</span> <span class=\"token operator\">&amp;</span> DELIVS<span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Enable interrupts on the APIC (but not on the processor).</span>\n  <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>TPR<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The line <code class=\"language-text\">if(!lapic) return;</code> checks whether the global variable <code class=\"language-text\">lapic</code> holds a value.</p>\n<h3 id=\"local-apic-registers\" style=\"position:relative;\"><a href=\"#local-apic-registers\" aria-label=\"local apic registers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Local APIC registers</h3>\n<p>The variable <code class=\"language-text\">lapic</code> had the address from <code class=\"language-text\">lapicaddr</code> inside the MP Floating Pointer Structure stored into it during the MP table retrieval covered in the <a href=\"/unix-xv6-006-kernel-main-03-en\">previous article</a>.</p>\n<p>Let’s confirm the actual value stored in this global variable using a debugger.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ b *0x801027a0\n$ <span class=\"token builtin class-name\">continue</span></code></pre></div>\n<p>After inspecting the contents of <code class=\"language-text\">lapic</code>, the address <code class=\"language-text\">0xfee00000</code> was stored there.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ info variables lapic\nFile lapic.c:\n<span class=\"token number\">44</span>:volatile uint *lapic<span class=\"token punctuation\">;</span>\n\n$ p lapic\n<span class=\"token variable\">$1</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>volatile uint *<span class=\"token punctuation\">)</span> 0xfee00000</code></pre></div>\n<p>This <code class=\"language-text\">lapic</code> is the memory-mapped Local APIC register.</p>\n<p>The Local APIC register is 32-bit data that is memory-mapped at the address pointed to by the MP Configuration Table.</p>\n<p>Each 32-bit element at an offset aligned to a 16-byte boundary is set as a Local APIC register.</p>\n<p>The following page is helpful for further details.</p>\n<p>Reference: <a href=\"https://wiki.osdev.org/APIC\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">APIC - OSDev Wiki</a></p>\n<p>From here, we will configure the Local APIC registers.</p>\n<p>The following values defined in <code class=\"language-text\">lapic.c</code> are used for this purpose.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// Local APIC registers, divided by 4 for use as uint[] indices.</span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">ID</span>      <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0020</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// ID</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">VER</span>     <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0030</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Version</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">TPR</span>     <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0080</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Task Priority</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">EOI</span>     <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x00B0</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// EOI</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">SVR</span>     <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x00F0</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Spurious Interrupt Vector</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">ENABLE</span>     <span class=\"token expression\"><span class=\"token number\">0x00000100</span>   </span><span class=\"token comment\">// Unit Enable</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">ESR</span>     <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0280</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Error Status</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">ICRLO</span>   <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0300</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Interrupt Command</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">INIT</span>       <span class=\"token expression\"><span class=\"token number\">0x00000500</span>   </span><span class=\"token comment\">// INIT/RESET</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">STARTUP</span>    <span class=\"token expression\"><span class=\"token number\">0x00000600</span>   </span><span class=\"token comment\">// Startup IPI</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">DELIVS</span>     <span class=\"token expression\"><span class=\"token number\">0x00001000</span>   </span><span class=\"token comment\">// Delivery status</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">ASSERT</span>     <span class=\"token expression\"><span class=\"token number\">0x00004000</span>   </span><span class=\"token comment\">// Assert interrupt (vs deassert)</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">DEASSERT</span>   <span class=\"token expression\"><span class=\"token number\">0x00000000</span></span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">LEVEL</span>      <span class=\"token expression\"><span class=\"token number\">0x00008000</span>   </span><span class=\"token comment\">// Level triggered</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">BCAST</span>      <span class=\"token expression\"><span class=\"token number\">0x00080000</span>   </span><span class=\"token comment\">// Send to all APICs, including self.</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">BUSY</span>       <span class=\"token expression\"><span class=\"token number\">0x00001000</span></span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">FIXED</span>      <span class=\"token expression\"><span class=\"token number\">0x00000000</span></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">ICRHI</span>   <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0310</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Interrupt Command [63:32]</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">TIMER</span>   <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0320</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Local Vector Table 0 (TIMER)</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">X1</span>         <span class=\"token expression\"><span class=\"token number\">0x0000000B</span>   </span><span class=\"token comment\">// divide counts by 1</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">PERIODIC</span>   <span class=\"token expression\"><span class=\"token number\">0x00020000</span>   </span><span class=\"token comment\">// Periodic</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">PCINT</span>   <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0340</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Performance Counter LVT</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">LINT0</span>   <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0350</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Local Vector Table 1 (LINT0)</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">LINT1</span>   <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0360</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Local Vector Table 2 (LINT1)</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">ERROR</span>   <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0370</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Local Vector Table 3 (ERROR)</span></span>\n  <span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">MASKED</span>     <span class=\"token expression\"><span class=\"token number\">0x00010000</span>   </span><span class=\"token comment\">// Interrupt masked</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">TICR</span>    <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0380</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Timer Initial Count</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">TCCR</span>    <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x0390</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Timer Current Count</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">TDCR</span>    <span class=\"token expression\"><span class=\"token punctuation\">(</span><span class=\"token number\">0x03E0</span><span class=\"token operator\">/</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span>   </span><span class=\"token comment\">// Timer Divide Configuration</span></span></code></pre></div>\n<h3 id=\"setting-the-spurious-interrupt-vector-and-enabling-the-local-apic\" style=\"position:relative;\"><a href=\"#setting-the-spurious-interrupt-vector-and-enabling-the-local-apic\" aria-label=\"setting the spurious interrupt vector and enabling the local apic permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setting the Spurious Interrupt Vector and enabling the Local APIC</h3>\n<p>Moving on.</p>\n<p>This line uses the <code class=\"language-text\">lapicw</code> function to set the <code class=\"language-text\">Spurious Interrupt Vector</code> and enable the Local APIC.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// Enable local APIC; set spurious interrupt vector.</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>SVR<span class=\"token punctuation\">,</span> ENABLE <span class=\"token operator\">|</span> <span class=\"token punctuation\">(</span>T_IRQ0 <span class=\"token operator\">+</span> IRQ_SPURIOUS<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>The <code class=\"language-text\">lapicw</code> function, used frequently from here on, is defined as follows.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token keyword\">static</span> <span class=\"token keyword\">void</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">int</span> index<span class=\"token punctuation\">,</span> <span class=\"token keyword\">int</span> value<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n  lapic<span class=\"token punctuation\">[</span>index<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> value<span class=\"token punctuation\">;</span>\n  lapic<span class=\"token punctuation\">[</span>ID<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>  <span class=\"token comment\">// wait for write to finish, by reading</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>It takes <code class=\"language-text\">index</code> and <code class=\"language-text\">value</code> as arguments and overwrites the corresponding <code class=\"language-text\">lapic</code> value.</p>\n<p><code class=\"language-text\">ID</code> in <code class=\"language-text\">lapic[ID]</code> is defined as <code class=\"language-text\">(0x0020/4)</code> in <code class=\"language-text\">lapic.c</code>.</p>\n<p><code class=\"language-text\">lapic[ID];</code> does not change any settings; its purpose is to wait for the preceding <code class=\"language-text\">lapic</code> write to complete by reading this value.</p>\n<p>Let’s look at the line <code class=\"language-text\">lapicw(SVR, ENABLE | (T_IRQ0 + IRQ_SPURIOUS));</code>.</p>\n<p>The index is <code class=\"language-text\">SVR</code>.</p>\n<p>This refers to the offset <code class=\"language-text\">(0x00F0/4)</code> of the <code class=\"language-text\">Spurious Interrupt Vector Register</code>.</p>\n<p>As described in the following article, setting bit 8 (<code class=\"language-text\">0x100</code>) of the <code class=\"language-text\">Spurious Interrupt Vector Register</code> enables the APIC.</p>\n<p>Reference: <a href=\"https://wiki.osdev.org/APIC\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">APIC - OSDev Wiki</a></p>\n<p>Also, in order for the Local APIC to be able to receive interrupts, the <code class=\"language-text\">Spurious Interrupt Vector</code> must be set.</p>\n<p>The lower 8 bits of the <code class=\"language-text\">Spurious Interrupt Vector Register</code> are mapped to the IRQ number of the <code class=\"language-text\">Spurious Interrupt Vector</code>.</p>\n<p>Therefore, <code class=\"language-text\">0x13f</code>, the OR of <code class=\"language-text\">0x100</code> and <code class=\"language-text\">0x3f</code>, is set in the <code class=\"language-text\">Spurious Interrupt Vector Register</code>.</p>\n<p>This lower value <code class=\"language-text\">0x3f</code> comes from the expression <code class=\"language-text\">T_IRQ0 + IRQ_SPURIOUS</code>.</p>\n<p>The values such as <code class=\"language-text\">T_IRQ0</code> are defined in <code class=\"language-text\">traps.h</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// These are arbitrarily chosen, but with care not to overlap</span>\n<span class=\"token comment\">// processor defined exceptions or interrupt vectors.</span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">T_SYSCALL</span>       <span class=\"token expression\"><span class=\"token number\">64</span>      </span><span class=\"token comment\">// system call</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">T_DEFAULT</span>      <span class=\"token expression\"><span class=\"token number\">500</span>      </span><span class=\"token comment\">// catchall</span></span>\n\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">T_IRQ0</span>          <span class=\"token expression\"><span class=\"token number\">32</span>      </span><span class=\"token comment\">// IRQ 0 corresponds to int T_IRQ</span></span>\n\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">IRQ_TIMER</span>        <span class=\"token expression\"><span class=\"token number\">0</span></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">IRQ_KBD</span>          <span class=\"token expression\"><span class=\"token number\">1</span></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">IRQ_COM1</span>         <span class=\"token expression\"><span class=\"token number\">4</span></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">IRQ_IDE</span>         <span class=\"token expression\"><span class=\"token number\">14</span></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">IRQ_ERROR</span>       <span class=\"token expression\"><span class=\"token number\">19</span></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">IRQ_SPURIOUS</span>    <span class=\"token expression\"><span class=\"token number\">31</span></span></span></code></pre></div>\n<p>According to the <a href=\"https://wiki.osdev.org/APIC\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">OSDev Wiki</a>, the simplest value to set for the <code class=\"language-text\">Spurious Interrupt Vector</code> is <code class=\"language-text\">0xff</code>, but xv6OS uses <code class=\"language-text\">0x1f</code>.</p>\n<p>(I don’t really understand the reason for this…)</p>\n<h3 id=\"timer-configuration\" style=\"position:relative;\"><a href=\"#timer-configuration\" aria-label=\"timer configuration permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Timer configuration</h3>\n<p>Next are the following lines.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// The timer repeatedly counts down at bus frequency</span>\n<span class=\"token comment\">// from lapic[TICR] and then issues an interrupt.</span>\n<span class=\"token comment\">// If xv6 cared more about precise timekeeping,</span>\n<span class=\"token comment\">// TICR would be calibrated using an external time source.</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>TDCR<span class=\"token punctuation\">,</span> X1<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>TIMER<span class=\"token punctuation\">,</span> PERIODIC <span class=\"token operator\">|</span> <span class=\"token punctuation\">(</span>T_IRQ0 <span class=\"token operator\">+</span> IRQ_TIMER<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>TICR<span class=\"token punctuation\">,</span> <span class=\"token number\">10000000</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p><code class=\"language-text\">TDCR</code> is <code class=\"language-text\">(0x03E0/4)</code>, which is the offset of the <code class=\"language-text\">Divide Configuration Register (for Timer)</code>.</p>\n<p>This setting is used when configuring the Local APIC timer period.</p>\n<p>The Local APIC timer is a timer built into each processor’s Local APIC, and it generates interrupts only for that processor.</p>\n<p>The Local APIC timer starts from a value set by the kernel as the <code class=\"language-text\">Timer Initial Count</code>, decrements at a fixed interval, and fires an interrupt when it reaches 0.</p>\n<p>The decrement speed depends on the CPU’s bus frequency divided by the <code class=\"language-text\">Timer Divide Configuration</code> value.</p>\n<p>The Local APIC timer supports two modes: <code class=\"language-text\">Periodic mode</code> and <code class=\"language-text\">One-shot mode</code>.</p>\n<p>In <code class=\"language-text\">Periodic mode</code>, when the count reaches 0, it automatically resets to the initial value and begins counting down again.</p>\n<p>In <code class=\"language-text\">One-shot mode</code>, when the count reaches 0 and fires an interrupt, the count stays at 0 until the program explicitly sets a new initial value.</p>\n<p>Reference: <a href=\"https://wiki.osdev.org/APIC_timer\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">APIC timer - OSDev Wiki</a></p>\n<p>Reference: <a href=\"https://japan.zdnet.com/article/20365986/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Timer interrupt selected as Local APIC timer interrupt - ZDNet Japan</a></p>\n<p>In xv6OS, the <code class=\"language-text\">Timer Divide Configuration</code> value is set to <code class=\"language-text\">0x0000000B</code>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/d05d9531c380a9b7fae36b6a644a608f/0b533/image.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 39.583333333333336%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAARlAAAEZQAGA43XUAAABF0lEQVQoz32RCYuDQAyF+///mdTCFhGLth3B+6rifb+dhJ2ltN0NPJgkbz4z8dD3PZZlQZqmeDxKVKVUVaEoCmRZJmsPzktZL396Ko+iCHGcoG1bZgzDgAMdKJIkge/7CIKAjY7jQNd12LbNNSXyeJ7Hcl3BXhqKgliHeZ45iWWDLt9uN1iWJSfOoIKmVB8q8hxN06Cua4zjyOd1XdlHrF9g13VPz6u4ue8791wheFpN05DKNRCAYORRsDfgp1DAKArxdT4z9HnyVx8D1Q7/iySOcTqdoB+PEML908c7pIUSeZqmN/GzpCmXezNNE4ZhIJR7/OQlBrF4wmetUtfrlf9yEPhw5Jnyy+XCEuLO4CAMsW0bXu9/A9hYY3E3+dHEAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/d05d9531c380a9b7fae36b6a644a608f/8ac56/image.webp 240w,\n/static/d05d9531c380a9b7fae36b6a644a608f/d3be9/image.webp 480w,\n/static/d05d9531c380a9b7fae36b6a644a608f/b0a15/image.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/d05d9531c380a9b7fae36b6a644a608f/8ff5a/image.png 240w,\n/static/d05d9531c380a9b7fae36b6a644a608f/e85cb/image.png 480w,\n/static/d05d9531c380a9b7fae36b6a644a608f/0b533/image.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/d05d9531c380a9b7fae36b6a644a608f/0b533/image.png\"\n            alt=\"2022/02/image.png\"\n            title=\"2022/02/image.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Reference image: <a href=\"http://flint.cs.yale.edu/cs422/doc/24547212.pdf\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Intel SDM vol3</a></p>\n<p>Also, the <code class=\"language-text\">Timer Initial Count</code> is set to <code class=\"language-text\">10000000</code> by <code class=\"language-text\">lapicw(TICR, 10000000);</code>.</p>\n<p>Incidentally, the following line configures <code class=\"language-text\">Local Vector Table 0 (TIMER)</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>TIMER<span class=\"token punctuation\">,</span> PERIODIC <span class=\"token operator\">|</span> <span class=\"token punctuation\">(</span>T_IRQ0 <span class=\"token operator\">+</span> IRQ_TIMER<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>The <code class=\"language-text\">Local Vector Table (LVT)</code> itself is a table in the Local APIC that maps events to interrupt vectors.</p>\n<p>Through the LVT, software can specify how local interrupts are sent to the CPU.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/1fb117892fd2af6a004e0e16826524d0/0b533/image-1.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 119.16666666666667%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAYCAYAAAD6S912AAAACXBIWXMAARlAAAEZQAGA43XUAAADAklEQVQ4y32U2XLaQBBF+f9vyWOe8uiyg6GMXaHMvoPYQWKVMMh0+jQaAgR7qkYzmuXO7dtLKgxD2e/3cjgchPl6vbYxDCNZrVYyn/sym07Pe1OdbzYbWS6XNn58fNjI/m63kxRgtEgXBoOBtNttG+ndTsdGz/Ok1WpJt9s9j71eTxaLQIbDoQHS4jj+B3g8Ho0pLPp9T/oKApvxeGx9u93a3mw2k0CBRqORFIsFqVQqOhalWq1KFO2uAY9yam9/8vLj5y8Zjsa2jimubRT0kmkQBPYIsmD+GfCyLZYr6Q4nEkaRvhrZQYBp49FQstms1Ot1taR/dQ+s1F6dcfz8lFzuRWnX7LVP/b9tDjCKQnPIer0yGY4XeyfAhCF6NRoNMwET8do2iQAuuEdgvNAzUSIDjrhmeGMyb8Hg9TUnuZcXaXe6FlJRYj6Peb2uPDw86Jk3s+guoKPt9MKccrmsobE477MH88l4JOl0Wmq1+h0NbwBdkNM8jTXfD8xcQsZJMZ1M5Pn5WQqFokx07vv+94DMuVhRhoTHUjOGdUyGOZIM1MM9e9A35t+abGmoF9HR8/qWCaQhezAF+F6775QkYwBlzujylREwl//fAqITB3EChQHTuDyfzy31yFn0ch3vct7JwMP0M6DpooWgowWBlHK92WzaHh2zAQMctitd439zD3CTlCYAcIQbSTGEhykjYDgDLVmjEvHYFSBarRKGDsh1sofKwhz25C8lDlAAsQLA2AFutxtpKQgXOfgVIOsAAuRYoSOAi8UFQ4J1t4ukWChYTXMXenoQAEDRybGC4Uid5IIdlmDE/2moYYGHuQAgrPgPAv/kCJWDPeas43k0R6Zx8sAVoItBAE6hE8pXjaB/fHyUfD4v2UxG0zAjhzgB5EPOvr8XTC9KOqbXtFMcqvrPGowx8WTNWn4/PRlgRgEzWnBjZRjHiYYcLpVKBgQo80KiqevoBnsXCeQ5Z9krlcqyS7Io5VLssvFIo1FXp7RNH6KgmcRmpVI2Zx1vZKDQ0v8C6TUhDYjW/XoAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/1fb117892fd2af6a004e0e16826524d0/8ac56/image-1.webp 240w,\n/static/1fb117892fd2af6a004e0e16826524d0/d3be9/image-1.webp 480w,\n/static/1fb117892fd2af6a004e0e16826524d0/b0a15/image-1.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/1fb117892fd2af6a004e0e16826524d0/8ff5a/image-1.png 240w,\n/static/1fb117892fd2af6a004e0e16826524d0/e85cb/image-1.png 480w,\n/static/1fb117892fd2af6a004e0e16826524d0/0b533/image-1.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/1fb117892fd2af6a004e0e16826524d0/0b533/image-1.png\"\n            alt=\"2022/02/image-1.png\"\n            title=\"2022/02/image-1.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Reference image: <a href=\"http://flint.cs.yale.edu/cs422/doc/24547212.pdf\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Intel SDM vol3</a></p>\n<p>The LVT consists of the following 32-bit registers. Details will be examined when they are actually used.</p>\n<ul>\n<li>LVT Timer Register (FEE0 0320H)</li>\n<li>LVT Thermal Monitor Register (FEE0 0330H)</li>\n<li>LVT Performance Counter Register (FEE0 0340H)</li>\n<li>LVT LINT0 Register (FEE0 0350H)</li>\n<li>LVT LINT1 Register (FEE0 0360H)</li>\n<li>LVT Error Register (FEE0 0370H)</li>\n</ul>\n<p>Here, <code class=\"language-text\">TIMER</code> specifies the <code class=\"language-text\">LVT Timer Register</code> at <code class=\"language-text\">(0x0320/4)</code>.</p>\n<p>The <code class=\"language-text\">LVT Timer Register</code> specifies the interrupt to raise when the APIC timer fires.</p>\n<p>Here, <code class=\"language-text\">PERIODIC</code> sets bit 18 (<code class=\"language-text\">Timer Mode</code>) of the LVT to 1, switching the timer to <code class=\"language-text\">Periodic mode</code>.</p>\n<p>Also, <code class=\"language-text\">T_IRQ0 + IRQ_TIMER</code> sets the interrupt vector; the value being set appears to be <code class=\"language-text\">0x20</code>.\n(I couldn’t find a direct source for this, but it probably refers to a timer interrupt like <code class=\"language-text\">INT 0x20</code> — I’m not sure.)</p>\n<h3 id=\"disable-logical-interrupt-lines\" style=\"position:relative;\"><a href=\"#disable-logical-interrupt-lines\" aria-label=\"disable logical interrupt lines permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Disable logical interrupt lines</h3>\n<p>Moving on.</p>\n<p><code class=\"language-text\">0x00010000</code> is set at <code class=\"language-text\">(0x0350/4)</code> and <code class=\"language-text\">(0x0360/4)</code> respectively.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// Disable logical interrupt lines.</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>LINT0<span class=\"token punctuation\">,</span> MASKED<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>LINT1<span class=\"token punctuation\">,</span> MASKED<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>First, <code class=\"language-text\">(0x0350/4)</code> and <code class=\"language-text\">(0x0360/4)</code> are the <code class=\"language-text\">LVT LINT0 Register</code> and <code class=\"language-text\">LVT LINT1 Register</code>.</p>\n<p>Both are interrupt vectors that define interrupts from the LINT0 and LINT1 pins.</p>\n<p>Here, bit 17 is set to enable the mask.</p>\n<p>(I had no idea why it was necessary to disable these two — why?!)</p>\n<h3 id=\"disable-performance-counter-overflow-interrupts\" style=\"position:relative;\"><a href=\"#disable-performance-counter-overflow-interrupts\" aria-label=\"disable performance counter overflow interrupts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Disable performance counter overflow interrupts</h3>\n<p>Moving on.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// Disable performance counter overflow interrupts</span>\n<span class=\"token comment\">// on machines that provide that interrupt entry.</span>\n<span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span>lapic<span class=\"token punctuation\">[</span>VER<span class=\"token punctuation\">]</span><span class=\"token operator\">>></span><span class=\"token number\">16</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">&amp;</span> <span class=\"token number\">0xFF</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">>=</span> <span class=\"token number\">4</span><span class=\"token punctuation\">)</span> <span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>PCINT<span class=\"token punctuation\">,</span> MASKED<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p><code class=\"language-text\">PCINT</code> masks the <code class=\"language-text\">LVT Performance Counter Register</code>, which generates an interrupt on overflow, under a specific condition.</p>\n<p>That condition is that the value obtained by right-shifting the Local APIC <code class=\"language-text\">Version Register</code> by 16 bits and taking the lower 8 bits is 4 or greater.</p>\n<p>I had no idea what this means…</p>\n<p>In any case, when I ran through this in the debugger, this line was not executed, so I will proceed on the assumption that xv6OS does not mask the <code class=\"language-text\">LVT Performance Counter Register</code>.</p>\n<h3 id=\"setting-the-error-register\" style=\"position:relative;\"><a href=\"#setting-the-error-register\" aria-label=\"setting the error register permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setting the Error Register</h3>\n<p>The interrupt vector for the <code class=\"language-text\">LVT Error Register</code> is set here.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// Map error interrupt to IRQ_ERROR.</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>ERROR<span class=\"token punctuation\">,</span> T_IRQ0 <span class=\"token operator\">+</span> IRQ_ERROR<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h3 id=\"clearing-the-esr\" style=\"position:relative;\"><a href=\"#clearing-the-esr\" aria-label=\"clearing the esr permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Clearing the ESR</h3>\n<p>The ESR is cleared here.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// Clear error status register (requires back-to-back writes).</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>ESR<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>ESR<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>ESR stands for Error Status Register; bits are set when an error occurs.</p>\n<p>The corresponding bits are shown in the following diagram.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/a62689b6484885eda853c23b71f5eeee/0b533/image-2.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 55.00000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAARlAAAEZQAGA43XUAAABeUlEQVQoz4WS626CQBCFff8Ha39IKiDKTWMU5aaA0ihync4Zu8a0DSU5mWV359uzszO5Xq/UNA2laUpZllFRFJTnOR2PR4qiiE6nExX5Y+6hjMIwFEWsOI5JMW63G03quiZ8WAiCQDYmSUKbzYYsy6LdbidrCnI4HOQgCPux1ratMMB6AodhkNi2jTgVZ+wWTsuypKqqxAHi5XIRYf58Po8DK07SplN6f38jTdNosVhQylAk4gAlXB9z/wLv1Y1msw8yDJM8z6P9fi9OlKPXCCjgXdeNASuamybphs4yyPd8qSkK/1MoAcCjDpumJt/3xZ3ruhLL8vNZw1cpaN/3I4/Cz79er0WAbbdbatgBkuCk532IaBOVo+KfQMSQW2O5tAXq2LY8jOt6ZJoGOY4jzqMoFugv4P1+lwk4UAq49wyun82w+dxi+FLGAKM3Ta6xx2Vp+TGG7xwwwBKHGCh1fJ2EmxVJcLharQRoMUzXZwJFF6CV4PA1F/9fbf9C6fe4f28AAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/a62689b6484885eda853c23b71f5eeee/8ac56/image-2.webp 240w,\n/static/a62689b6484885eda853c23b71f5eeee/d3be9/image-2.webp 480w,\n/static/a62689b6484885eda853c23b71f5eeee/b0a15/image-2.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/a62689b6484885eda853c23b71f5eeee/8ff5a/image-2.png 240w,\n/static/a62689b6484885eda853c23b71f5eeee/e85cb/image-2.png 480w,\n/static/a62689b6484885eda853c23b71f5eeee/0b533/image-2.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/a62689b6484885eda853c23b71f5eeee/0b533/image-2.png\"\n            alt=\"2022/02/image-2.png\"\n            title=\"2022/02/image-2.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Reference image: <a href=\"http://flint.cs.yale.edu/cs422/doc/24547212.pdf\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Intel SDM vol3</a></p>\n<p>Why it is cleared twice is, once again, a mystery.</p>\n<h3 id=\"checking-the-eoi\" style=\"position:relative;\"><a href=\"#checking-the-eoi\" aria-label=\"checking the eoi permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Checking the EOI</h3>\n<p>EOI stands for End of Interrupt, a signal sent to the PIC to indicate that a specific interrupt handler has completed.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// Ack any outstanding interrupts.</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>EOI<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Reference: <a href=\"https://en.wikipedia.org/wiki/End_of_interrupt\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">End of interrupt - Wikipedia</a></p>\n<p>For compatibility reasons, 0 must be set in the EOI register.</p>\n<h3 id=\"setting-the-interrupt-command-register\" style=\"position:relative;\"><a href=\"#setting-the-interrupt-command-register\" aria-label=\"setting the interrupt command register permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setting the Interrupt Command Register</h3>\n<p>Both <code class=\"language-text\">ICRHI</code> and <code class=\"language-text\">ICRLO</code> are <code class=\"language-text\">Interrupt Command Registers</code>.</p>\n<p>These two registers are used to send interrupts to CPUs.</p>\n<p>Note that writing to <code class=\"language-text\">(0x0300/4)</code> triggers an interrupt, but writing to <code class=\"language-text\">(0x0310/4)</code> does not, so values are set in the order <code class=\"language-text\">ICRHI</code> then <code class=\"language-text\">ICRLO</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// Send an Init Level De-Assert to synchronise arbitration ID's.</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>ICRHI<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>ICRLO<span class=\"token punctuation\">,</span> BCAST <span class=\"token operator\">|</span> INIT <span class=\"token operator\">|</span> LEVEL<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">while</span><span class=\"token punctuation\">(</span>lapic<span class=\"token punctuation\">[</span>ICRLO<span class=\"token punctuation\">]</span> <span class=\"token operator\">&amp;</span> DELIVS<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Here, an <code class=\"language-text\">INIT Level De-assert</code> message is sent to all Local APICs in the system to synchronize and set their <code class=\"language-text\">arbitration IDs</code>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/192a2ec202fe9a196c88a8cd947bd723/0b533/image-3.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 86.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAARlAAAEZQAGA43XUAAACIklEQVQ4y3VUibKiMBD0/3/r7dv1HWpZaLkciiIegAqCKOAxOz2PUChsqsZgjk5Ppyed8/lMRZHT4RDSJcsoz/l7v6cgCChNU0qSROJ0OjUC82EYyvftdiNgdYqiIDTf98myLFosFmSaFs3nc1qtVhKbzYZ0/S/1+32a2Tat12taLpfkuq6sy5gIGsh08IOGQZzyeDykv16vFMcxMz/Q8XhkJhFFUVQxwz6QUfueAO/3u5w6m81oPB4Ly+12K73jOGQzKwRYAVRJgDXoFaknhgmz2e12kiLSh6ZgiM0AgVZRFMr/y+UiLDGfpmfJqgJU+Y80jX6/v5M2GtFkMiHP8yQdsMdCCC6pcpoF/x8Oh9T905WM4jhpAiLdj26XBoMBadqIJ38uC00xQnqPcgyHvr39kkuCxo2UwQTi5nlW2aANEOvQLMukz8+vil0DELfpsch79uC91ERp88SwHAsCX1hC81ZAbEIo3V4BlYaYh11slgi6G4ZBa/apsl4F+NoAVgcEOwCpMZct1Ov1+eaPTR/WAepAdUBEZWQ2PZhpJUPMNRi+Ar4yRLo/l8aFwD0M7zhLqXnI0crwfy0rHw2UIwKAqCJdN8TcrZeCk1DsJqcA01rTqdwgWNUBkTbGfN9jQF0K4AlQGRtl5zIAXhzbnku5KW3ammka9N3rsamjyhWiIfLHhxL9xLU6ZXZ4snA6vvFEIfBAwNDoTdMU7wJM2Q39PwL5F+pagDLJAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/192a2ec202fe9a196c88a8cd947bd723/8ac56/image-3.webp 240w,\n/static/192a2ec202fe9a196c88a8cd947bd723/d3be9/image-3.webp 480w,\n/static/192a2ec202fe9a196c88a8cd947bd723/b0a15/image-3.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/192a2ec202fe9a196c88a8cd947bd723/8ff5a/image-3.png 240w,\n/static/192a2ec202fe9a196c88a8cd947bd723/e85cb/image-3.png 480w,\n/static/192a2ec202fe9a196c88a8cd947bd723/0b533/image-3.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/192a2ec202fe9a196c88a8cd947bd723/0b533/image-3.png\"\n            alt=\"2022/02/image-3.png\"\n            title=\"2022/02/image-3.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Reference image: <a href=\"http://flint.cs.yale.edu/cs422/doc/24547212.pdf\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Intel SDM vol3</a></p>\n<p>Finally, setting the <code class=\"language-text\">TPR (Task Priority Register)</code> to 0 allows the CPU to handle all interrupts, enabling the interrupt functionality.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// Enable interrupts on the APIC (but not on the processor).</span>\n<span class=\"token function\">lapicw</span><span class=\"token punctuation\">(</span>TPR<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Incidentally, setting the TPR to 15 disables all interrupts.</p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>With this, all processing inside the <code class=\"language-text\">lapicinit</code> function called from <code class=\"language-text\">main</code> is complete.</p>\n<p>There were many parts I didn’t fully understand this time, so I plan to add notes as I figure things out.</p>\n<p>Next time: segment descriptors at last.</p>\n<p>It’s becoming more and more interesting as the inner workings of the kernel start to come into focus.</p>\n<h2 id=\"reference-books\" style=\"position:relative;\"><a href=\"#reference-books\" aria-label=\"reference books permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reference Books</h2>\n<ul>\n<li><a href=\"https://amzn.to/3qZSCY7\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Build an OS in 30 Days!</a></li>\n<li><a href=\"https://amzn.to/3qXYsZX\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Introduction to OS Development from Zero</a></li>\n<li><a href=\"https://amzn.to/3q8TU3K\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">An Introduction to OS Code Reading: Learning Kernel Internals with UNIX V6</a></li>\n<li><a href=\"https://amzn.to/3I6fkVt\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Detailed Linux Kernel</a></li>\n<li><a href=\"https://amzn.to/3JRUdI2\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Build and Understand an OS: Theory and Implementation for Running x86 Computers</a></li>\n</ul>","fields":{"slug":"/unix-xv6-007-kernel-main-04-en","tagSlugs":["/tag/unix-en/","/tag/xv-6-en/","/tag/kernel-en/","/tag/os-en/","/tag/english/"]},"frontmatter":{"date":"2022-02-02","description":"I am learning about kernels by reading the source code of the educational OS xv6OS. In this article, I walk through the behavior of the xv6OS kernel's main function.","tags":["Unix (en)","xv6 (en)","Kernel (en)","OS (en)","English"],"title":"Reading xv6OS Thoroughly to Fully Understand the Kernel - Local APIC Edition -","socialImage":{"publicURL":"/static/1333b4e4c7eb212c2c577464f891366f/unix-xv6-007-kernel-main-04.png"}}}},"pageContext":{"slug":"/unix-xv6-007-kernel-main-04-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}