{"componentChunkName":"component---src-templates-post-template-js","path":"/unix-xv6-011-kernel-main-08-en","result":{"data":{"markdownRemark":{"id":"42c4389e-5c50-597a-8a8d-6e280b09304a","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/unix-xv6-011-kernel-main-08\">original page</a>.</p>\n</blockquote>\n<p>I’m reading <a href=\"https://github.com/mit-pdos/xv6-public\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">xv6 OS</a> inspired by <a href=\"https://amzn.to/3q8TU3K\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Your First OS Code Reading: Learning Kernel Internals with UNIX V6</a>.</p>\n<p>Because UNIX V6 itself does not run on x86 CPUs, I decided to read the source code 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 adapts UNIXv6 to run on the x86 architecture.</p>\n<p><a href=\"/unix-xv6-010-kernel-main-07-en\">Last time</a>, we looked at the behavior of the <code class=\"language-text\">consoleinit</code> function executed from <code class=\"language-text\">main</code>.</p>\n<p>This time, I’ll follow the behavior of the <code class=\"language-text\">uartinit</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-uartinit-function\">The uartinit function</a></p>\n<ul>\n<li><a href=\"#what-is-uart\">What is UART</a></li>\n<li><a href=\"#using-the-serial-port-for-communication\">Using the serial port for communication</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-uartinit-function\" style=\"position:relative;\"><a href=\"#the-uartinit-function\" aria-label=\"the uartinit 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 uartinit function</h2>\n<p>The <code class=\"language-text\">uartinit</code> function is defined in <code class=\"language-text\">uart.c</code>, and it performs initialization related to the serial port.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">COM1</span>    <span class=\"token expression\"><span class=\"token number\">0x3f8</span></span></span>\n<span class=\"token keyword\">static</span> <span class=\"token keyword\">int</span> uart<span class=\"token punctuation\">;</span>    <span class=\"token comment\">// is there a uart?</span>\n\n<span class=\"token keyword\">void</span> <span class=\"token function\">uartinit</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\">char</span> <span class=\"token operator\">*</span>p<span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Turn off the FIFO</span>\n  <span class=\"token function\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">2</span><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\">// 9600 baud, 8 data bits, 1 stop bit, parity off.</span>\n  <span class=\"token function\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x80</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>    <span class=\"token comment\">// Unlock divisor</span>\n  <span class=\"token function\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">115200</span><span class=\"token operator\">/</span><span class=\"token number\">9600</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token function\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">1</span><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\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x03</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>    <span class=\"token comment\">// Lock divisor, 8 data bits.</span>\n  <span class=\"token function\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">4</span><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\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x01</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>    <span class=\"token comment\">// Enable receive interrupts.</span>\n\n  <span class=\"token comment\">// If status is 0xFF, no serial port.</span>\n  <span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span><span class=\"token function\">inb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">5</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span> <span class=\"token number\">0xFF</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span><span class=\"token punctuation\">;</span>\n  uart <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// Acknowledge pre-existing interrupt conditions;</span>\n  <span class=\"token comment\">// enable interrupts.</span>\n  <span class=\"token function\">inb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token function\">inb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token function\">ioapicenable</span><span class=\"token punctuation\">(</span>IRQ_COM1<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\">// Announce that we're here.</span>\n  <span class=\"token keyword\">for</span><span class=\"token punctuation\">(</span>p<span class=\"token operator\">=</span><span class=\"token string\">\"xv6...\\n\"</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">*</span>p<span class=\"token punctuation\">;</span> p<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token function\">uartputc</span><span class=\"token punctuation\">(</span><span class=\"token operator\">*</span>p<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"what-is-uart\" style=\"position:relative;\"><a href=\"#what-is-uart\" aria-label=\"what is uart 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>What is UART</h3>\n<p>To begin with, UART stands for <code class=\"language-text\">**Universal Asynchronous Receiver/Transmitter**</code>.</p>\n<p>UART is a protocol for exchanging serial data between two devices.</p>\n<p>Reference: <a href=\"https://www.rohde-schwarz.com/jp/products/test-and-measurement/oscilloscopes/educational-content/understanding-uart_254524.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Overview of UART | Rohde &#x26; Schwarz</a></p>\n<p>Reference: <a href=\"https://en.wikipedia.org/wiki/Universal_asynchronous_receiver-transmitter\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Universal asynchronous receiver-transmitter - Wikipedia</a></p>\n<p>It does not seem to be used very much these days, but UART appears to have been the protocol used for communication over serial ports.</p>\n<p>The <code class=\"language-text\">uartinit</code> function seems to be setting up what is usually called a COM port.</p>\n<h3 id=\"using-the-serial-port-for-communication\" style=\"position:relative;\"><a href=\"#using-the-serial-port-for-communication\" aria-label=\"using the serial port for communication 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>Using the serial port for communication</h3>\n<p>If an OS uses a serial port for communication, it first needs to initialize the serial port.</p>\n<p>Reference: <a href=\"https://wiki.osdev.org/Serial_Ports\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Serial Ports - OSDev Wiki</a></p>\n<p>Let’s look through the source code in order.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">COM1</span>    <span class=\"token expression\"><span class=\"token number\">0x3f8</span></span></span>\n\n<span class=\"token comment\">// Turn off the FIFO</span>\n<span class=\"token function\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">2</span><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><code class=\"language-text\">COM1</code> is defined as <code class=\"language-text\">0x3f8</code>.</p>\n<p>This is the fixed I/O port address of the COM1 port.</p>\n<p>Each COM port register can be accessed as an offset from the I/O port address.</p>\n<p><code class=\"language-text\">COM1+2</code> points to the <code class=\"language-text\">Interrupt Identification</code> and <code class=\"language-text\">FIFO control registers</code>.</p>\n<p>Here, setting 0 disables FIFO operation inside the UART.</p>\n<p>When FIFO is disabled, received data is passed to the <code class=\"language-text\">Receiver buffer register</code>.</p>\n<p>Next, let’s look at the following code.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// 9600 baud, 8 data bits, 1 stop bit, parity off.</span>\n<span class=\"token function\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x80</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>    <span class=\"token comment\">// Unlock divisor</span>\n<span class=\"token function\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">115200</span><span class=\"token operator\">/</span><span class=\"token number\">9600</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">1</span><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\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x03</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>    <span class=\"token comment\">// Lock divisor, 8 data bits.</span>\n<span class=\"token function\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">4</span><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\">outb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x01</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>    <span class=\"token comment\">// Enable receive interrupts.</span></code></pre></div>\n<p><code class=\"language-text\">COM1+3</code> corresponds to the <code class=\"language-text\">Line control register</code>.</p>\n<p>This register changes the communication parameters according to the bits that are set.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 428px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/71dfa1e3f1bdd7ff90d7dd26a33318b1/47730/image-54.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: 102.08333333333333%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAD5UlEQVQ4y2VUW2/bZBjO30RCQ0jco0kIiRu4KFRC4gaBEFyAgNttQqDCurVdS8fSdlm7tEua2LFzsJ2D48RnO+c8PO+XjRU10asvtuP3e97n8BW2fj3C3W928OHXO/jgy9/w0Xe7+OynPby/fQ8ff/8Yn/zwCJ/+uIetX57g7rd/4d3P7+HO9gPc+eJtvbd9H+9sPcBXP++gMBwFMBs6vOEQSRSiaZqoVF5Ba9SRJjE0rYa6Voemy3WC6STDJM8wvVGTPMV8PsPZ+SUKozAFsOYXyLMMQza2LAtDz8NiPke366Db66I/6GGxWGK1Wt2q5XIJ+ZTKFRQcd4yESJI0Vc00TUO1ckWEDURRhFq9hur1tUIZhCHiOL5V8r80y3FSKqPgKYTAar1WCF13CMe2MBqPOd4EtmPDJkqH65yIBc3tWrxF2PdCxV/GZr4/VhxWqxWO2VfIDV6bpgGLDSeTKWtyq9I0Qc5npy/IoTuOEBPyZDpVpA/6fYUmCEL+OUen04FpNGDbDjcM4A5dUiPlYTwawfOGfHcGDoiziysUgmSCN5/5bIYxRxXEKTldLBZs2CZqA7quod22YNk2DMNU3Oq6rjj3KGBELs8vayh0ui503hTuItqm2+2qsce+D5/NDYojLwlCIT+OYoV0THqkkeeNFGI/CDYj94Y+hq6LmOPKyLXrqmogiibctdVqoU0buUQtiGecYrGYq98ihqxSYrvnF6/IoR9jvVqqm1EY4LJcRqNhYER+ojBCkw3NZhOtdhtT8ixKy3qzRJg5Pao47HuBcn7Om6KyNBSEYuwRCZeRTaPJe7raRGhIkhR5nitnvKkpkZ9KUryAPlyL21f0YY4GFRVlhYI8S+FYNnq0UH/QR4/8tolY7CMJ2Yy9mU758CV92JWkxJFS1SWXV1fMMRV0+PKIKEXJWq3Ojag0UWq8DknF/9MSEeXrpPhxTj95KikpH4rCfXpRbJBxE9NsEmGPlmmrTDdbwmdL8bbmOzez/KJcZZYHY3ZPyUtCDn2FSJoOiFb8WK/X2XTjuyYbabUaJ9BU9uW9VK2ZoqFYesnTJsqoZsid1qqhwdFsmtdnUlKSr+uGokLEGZDHDjm0HIcnUoeTDBS3DT7r08fnl9dUeRS+Ps/mmyw3TVgURRITcyNRVxo5dkdFUpLT6bTUKnZq8LwUg89mc6pM21T1Np6dXSi4x8USHh8+xd7hPzh69hzHJyXsHhzjydNTHBwXsf93EQ/3j/Bw71Ctf+we4Pc/H/H5CfaPiup/BZFcFMroqzyf0KgzdVCIL/+7VqfMVP3uUbAuRZKISoIs2qpJpJVKldGM8S+pabx/t0/rAgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/71dfa1e3f1bdd7ff90d7dd26a33318b1/8ac56/image-54.webp 240w,\n/static/71dfa1e3f1bdd7ff90d7dd26a33318b1/3b73a/image-54.webp 428w\"\n              sizes=\"(max-width: 428px) 100vw, 428px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/71dfa1e3f1bdd7ff90d7dd26a33318b1/8ff5a/image-54.png 240w,\n/static/71dfa1e3f1bdd7ff90d7dd26a33318b1/47730/image-54.png 428w\"\n            sizes=\"(max-width: 428px) 100vw, 428px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/71dfa1e3f1bdd7ff90d7dd26a33318b1/47730/image-54.png\"\n            alt=\"2022/02/image-54.png\"\n            title=\"2022/02/image-54.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=\"https://www.lammertbies.nl/comm/info/serial-uart\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Serial UART, an in depth tutorial - Lammert Bies</a></p>\n<p><code class=\"language-text\">outb(COM1+3, 0x80);</code> sets the eighth bit and enables <code class=\"language-text\">DLAB</code>.</p>\n<p>As a result, the following writes to <code class=\"language-text\">COM1+0</code> and <code class=\"language-text\">COM1+1</code> are redirected to the <code class=\"language-text\">Divisor latch registers (R/W)</code>.</p>\n<p>Writing <code class=\"language-text\">115200/9600</code> and <code class=\"language-text\">0</code> there sets the UART speed to 9,600 bps.</p>\n<p>The following <code class=\"language-text\">outb(COM1+3, 0x03);</code> clears <code class=\"language-text\">DLAB</code> in the LCR while also setting <code class=\"language-text\">8 data bits</code>.</p>\n<p>After that, it writes 0 to the <code class=\"language-text\">Modem control register</code> and enables interrupts.</p>\n<p>After going through the full initialization process, it checks the <code class=\"language-text\">Line status register</code> to confirm that the serial port is available.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// If status is 0xFF, no serial port.</span>\n<span class=\"token keyword\">if</span><span class=\"token punctuation\">(</span><span class=\"token function\">inb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">5</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span> <span class=\"token number\">0xFF</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">return</span><span class=\"token punctuation\">;</span>\nuart <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Finally, it checks the current interrupt state by reading the RBR and IIR information, enables interrupts, and finishes.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token comment\">// Acknowledge pre-existing interrupt conditions;</span>\n<span class=\"token comment\">// enable interrupts.</span>\n<span class=\"token function\">inb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">inb</span><span class=\"token punctuation\">(</span>COM1<span class=\"token operator\">+</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">ioapicenable</span><span class=\"token punctuation\">(</span>IRQ_COM1<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<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>This time, I traced through the serial-port initialization process.</p>\n<p>Next time, I’ll look at the <code class=\"language-text\">pinit</code> function.</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\">OS Development in 30 Days!</a></li>\n<li><a href=\"https://amzn.to/3qXYsZX\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Introduction to OS Development from Scratch</a></li>\n<li><a href=\"https://amzn.to/3q8TU3K\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Your First 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\">Understanding the Linux Kernel</a></li>\n<li><a href=\"https://amzn.to/3JRUdI2\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Build to Understand an OS: Theory and Implementation for Running x86 Computers</a></li>\n</ul>","fields":{"slug":"/unix-xv6-011-kernel-main-08-en","tagSlugs":["/tag/unix-en/","/tag/xv-6-en/","/tag/kernel-en/","/tag/os-en/","/tag/english/"]},"frontmatter":{"date":"2022-02-13","description":"I'm reading the source code of the educational OS xv6OS to learn how its kernel works. In this article, I trace through the behavior of the xv6OS kernel's main function.","tags":["Unix (en)","xv6 (en)","Kernel (en)","OS (en)","English"],"title":"Reading xv6OS Seriously to Fully Understand the Kernel - Serial Port","socialImage":{"publicURL":"/static/c61f0e90db47df1f83c37024f2eab7a7/unix-xv6-011-kernel-main-08.png"}}}},"pageContext":{"slug":"/unix-xv6-011-kernel-main-08-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}