TomAwezome 2021-09-21 02:55:53 -04:00
parent 3c3e7977a7
commit 033e169511
10 changed files with 1208 additions and 505 deletions

View file

@ -43,7 +43,7 @@ body {background-color:#fef1f0;}
<a name="l16"></a>kernel memory in other operating systems. See </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/Memory/MAllocFree.CC.html#l408"><span class=cF4>SysCAlloc</span></a><span class=cF0>(), </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/Memory/MAllocFree.CC.html#l394"><span class=cF4>SysMAlloc</span></a><span class=cF0>(), </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/Memory/MAllocFree.CC.html#l427"><span class=cF4>SysMAllocIdent</span></a><span class=cF0>() and </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/Memory/MAllocFree.CC.html#l503"><span class=cF4>SysStrNew</span></a><span class=cF0>().
<a name="l17"></a>
<a name="l18"></a>All of the regular page tables are marked, &quot;cached&quot;. When accessing hardware, however, you need uncached page table. The
<a name="l19"></a>lowest 4Gig addresses have an alias to access hardware located toward the top of mapped space, </span><span class=cF2>0x01C4A00000</span><span class=cF0>. See </span><span class=cF4>
<a name="l19"></a>lowest 4Gig addresses have an alias to access hardware located toward the top of mapped space, </span><span class=cF2>0x01D7800000</span><span class=cF0>. See </span><span class=cF4>
<a name="l20"></a></span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/MultiProc.CC.html#l147"><span class=cF4>dev.uncached_alias</span></a><span class=cF0>.
<a name="l21"></a>
<a name="l22"></a>During an extended powered-on session of ZealOS, in theory, memory will become fragmented, requiring a reboot. It has never
@ -71,13 +71,13 @@ body {background-color:#fef1f0;}
<a name="l44"></a> 0xF0000000. PCI devices are supported, so </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/Memory/MemPhysical.CC.html#l64"><span class=cF4>Mem32DevAlloc</span></a><span class=cF0>() flaws could become an issue.
<a name="l45"></a>
<a name="l46"></a></span><span class=cF2> 0x0080000000-~0x00DFFFFFFF</span><span class=cF0>
<a name="l47"></a></span><span class=cF2> 0x0100000000-~0x01C49FFFFF</span><span class=cF0>
<a name="l47"></a></span><span class=cF2> 0x0100000000-~0x01D77FFFFF</span><span class=cF0>
<a name="l48"></a> Data Heap mem. (The physical memory that exists in this range is data heap.)
<a name="l49"></a>
<a name="l50"></a></span><span class=cF2> 0x01C4A00000- 0x02C49FFFFF</span><span class=cF0>
<a name="l50"></a></span><span class=cF2> 0x01D7800000- 0x02D77FFFFF</span><span class=cF0>
<a name="l51"></a> Uncached alias of first 4Gig. (For 32-bit device access.)
<a name="l52"></a>
<a name="l53"></a></span><span class=cF2> - 0x02C49FFFFF</span><span class=cF0>
<a name="l53"></a></span><span class=cF2> - 0x02D77FFFFF</span><span class=cF0>
<a name="l54"></a> 64-bit devices are alloced with </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/Memory/MemPhysical.CC.html#l122"><span class=cF4>Mem64DevAlloc</span></a><span class=cF0>() counting backward.
<a name="l55"></a>
<a name="l56"></a>

View file

@ -34,46 +34,58 @@ body {background-color:#fef1f0;}
<a name="l7"></a>#</span><span class=cF1>define</span><span class=cF0> PCIV_E1000 </span><span class=cFE>0x8086</span><span class=cF0>
<a name="l8"></a>#</span><span class=cF1>define</span><span class=cF0> PCID_82545EM </span><span class=cFE>0x100F</span><span class=cF0>
<a name="l9"></a>
<a name="l10"></a></span><span class=cF1>U0</span><span class=cF0> NetDriverInclude(</span><span class=cF1>U8</span><span class=cF0> *driver)
<a name="l11"></a>{
<a name="l12"></a> </span><span class=cF1>U8</span><span class=cF0> *filename = </span><span class=cF5>MStrPrint</span><span class=cF0>(</span><span class=cF3>__DIR__</span><span class=cF0> </span><span class=cF6>&quot;/%s&quot;</span><span class=cF0>, driver);
<a name="l10"></a>
<a name="l11"></a>#</span><span class=cF1>define</span><span class=cF0> PCIV_VIRTIO </span><span class=cFE>0x1AF4</span><span class=cF0>
<a name="l12"></a></span><span class=cF2>//#define PCID_VIRTIO_NET 0x1000</span><span class=cF0>
<a name="l13"></a>
<a name="l14"></a> </span><span class=cF5>ExeFile</span><span class=cF0>(filename);
<a name="l15"></a> </span><span class=cF5>Free</span><span class=cF0>(filename);
<a name="l16"></a>}
<a name="l14"></a></span><span class=cF1>U0</span><span class=cF0> NetDriverInclude(</span><span class=cF1>U8</span><span class=cF0> *driver)
<a name="l15"></a>{
<a name="l16"></a> </span><span class=cF1>U8</span><span class=cF0> *filename = </span><span class=cF5>MStrPrint</span><span class=cF0>(</span><span class=cF3>__DIR__</span><span class=cF0> </span><span class=cF6>&quot;/%s&quot;</span><span class=cF0>, driver);
<a name="l17"></a>
<a name="l18"></a></span><span class=cF1>U0</span><span class=cF0> NetDriverInit()
<a name="l19"></a>{
<a name="l20"></a> </span><span class=cF9>CPCIDev</span><span class=cF0> *net_driver_pci = </span><span class=cF5>PCIDevFind</span><span class=cF0>(</span><span class=cF3>PCIC_NETWORK</span><span class=cF0>);
<a name="l21"></a> </span><span class=cF1>Bool</span><span class=cF0> found = </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l22"></a>
<a name="l23"></a> </span><span class=cF1>switch</span><span class=cF0> (net_driver_pci-&gt;vendor_id)
<a name="l24"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l25"></a> </span><span class=cF1>case</span><span class=cF0> PCIV_PCNET:
<a name="l26"></a> </span><span class=cF1>switch</span><span class=cF0> (net_driver_pci-&gt;device_id)
<a name="l27"></a> {
<a name="l28"></a> </span><span class=cF1>case</span><span class=cF0> PCID_PCNET:
<a name="l29"></a> NetDriverInclude(</span><span class=cF6>&quot;PCNet&quot;</span><span class=cF0>);
<a name="l30"></a> found = </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l31"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l32"></a> }
<a name="l33"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l34"></a>
<a name="l35"></a> </span><span class=cF1>case</span><span class=cF0> PCIV_E1000:
<a name="l36"></a> </span><span class=cF1>switch</span><span class=cF0> (net_driver_pci-&gt;device_id)
<a name="l37"></a> {
<a name="l38"></a> </span><span class=cF1>case</span><span class=cF0> PCID_82545EM:
<a name="l39"></a> NetDriverInclude(</span><span class=cF6>&quot;E1000&quot;</span><span class=cF0>);
<a name="l40"></a> found = </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l41"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l42"></a> }
<a name="l43"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l44"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l45"></a>
<a name="l46"></a> </span><span class=cF1>if</span><span class=cF0> (!found)
<a name="l47"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'NET'</span><span class=cF0>);
<a name="l48"></a>}
<a name="l49"></a>
<a name="l50"></a>NetDriverInit;
<a name="l18"></a> </span><span class=cF5>ExeFile</span><span class=cF0>(filename);
<a name="l19"></a> </span><span class=cF5>Free</span><span class=cF0>(filename);
<a name="l20"></a>}
<a name="l21"></a>
<a name="l22"></a></span><span class=cF1>U0</span><span class=cF0> NetDriverInit()
<a name="l23"></a>{
<a name="l24"></a> </span><span class=cF9>CPCIDev</span><span class=cF0> *net_driver_pci = </span><span class=cF5>PCIDevFind</span><span class=cF0>(</span><span class=cF3>PCIC_NETWORK</span><span class=cF0>);
<a name="l25"></a> </span><span class=cF1>Bool</span><span class=cF0> found = </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l26"></a>
<a name="l27"></a> </span><span class=cF1>switch</span><span class=cF0> (net_driver_pci-&gt;vendor_id)
<a name="l28"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l29"></a> </span><span class=cF1>case</span><span class=cF0> PCIV_PCNET:
<a name="l30"></a> </span><span class=cF1>switch</span><span class=cF0> (net_driver_pci-&gt;device_id)
<a name="l31"></a> {
<a name="l32"></a> </span><span class=cF1>case</span><span class=cF0> PCID_PCNET:
<a name="l33"></a> NetDriverInclude(</span><span class=cF6>&quot;PCNet&quot;</span><span class=cF0>);
<a name="l34"></a> found = </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l35"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l36"></a> }
<a name="l37"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l38"></a>
<a name="l39"></a> </span><span class=cF1>case</span><span class=cF0> PCIV_E1000:
<a name="l40"></a> </span><span class=cF1>switch</span><span class=cF0> (net_driver_pci-&gt;device_id)
<a name="l41"></a> {
<a name="l42"></a> </span><span class=cF1>case</span><span class=cF0> PCID_82545EM:
<a name="l43"></a> NetDriverInclude(</span><span class=cF6>&quot;E1000&quot;</span><span class=cF0>);
<a name="l44"></a> found = </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l45"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l46"></a> }
<a name="l47"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l48"></a>
<a name="l49"></a> </span><span class=cF1>case</span><span class=cF0> PCIV_VIRTIO:
<a name="l50"></a> NetDriverInclude(</span><span class=cF6>&quot;VirtIONet&quot;</span><span class=cF0>);
<a name="l51"></a> found = </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l52"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l53"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l54"></a>
<a name="l55"></a> </span><span class=cF1>if</span><span class=cF0> (!found)
<a name="l56"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l57"></a> </span><span class=cF5>ClassRep</span><span class=cF0>(net_driver_pci);
<a name="l58"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'NODRIVER'</span><span class=cF0>);
<a name="l59"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l60"></a>}
<a name="l61"></a>
<a name="l62"></a>NetDriverInit;
</span></pre></body>
</html>

View file

@ -0,0 +1,345 @@
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V1.00">
<style type="text/css">
body {background-color:#fef1f0;}
.cF0{color:#000000;background-color:#fef1f0;}
.cF1{color:#0148a4;background-color:#fef1f0;}
.cF2{color:#3b7901;background-color:#fef1f0;}
.cF3{color:#057c7e;background-color:#fef1f0;}
.cF4{color:#bb2020;background-color:#fef1f0;}
.cF5{color:#9e42ae;background-color:#fef1f0;}
.cF6{color:#b57901;background-color:#fef1f0;}
.cF7{color:#b2b6af;background-color:#fef1f0;}
.cF8{color:#555753;background-color:#fef1f0;}
.cF9{color:#678fbb;background-color:#fef1f0;}
.cFA{color:#82bc49;background-color:#fef1f0;}
.cFB{color:#0097a2;background-color:#fef1f0;}
.cFC{color:#e26a6a;background-color:#fef1f0;}
.cFD{color:#c671bc;background-color:#fef1f0;}
.cFE{color:#c7ab00;background-color:#fef1f0;}
.cFF{color:#fef1f0;background-color:#fef1f0;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF2>/*</span><span class=cF0>
<a name="l2"></a> </span><span class=cF2>VirtIO Net driver ported from alec's Erythros.</span><span class=cF0>
<a name="l3"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l4"></a>
<a name="l5"></a></span><span class=cF2>//</span><span class=cF0>
<a name="l6"></a></span><span class=cF2>// PCI virtio I/O registers.</span><span class=cF0>
<a name="l7"></a></span><span class=cF2>//</span><span class=cF0>
<a name="l8"></a>
<a name="l9"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_PCI_HOST_FEATURES </span><span class=cFE>0</span><span class=cF0> </span><span class=cF2>// Features supported by the host</span><span class=cF0>
<a name="l10"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_PCI_GUEST_FEATURES </span><span class=cFE>4</span><span class=cF0> </span><span class=cF2>// Features activated by the guest</span><span class=cF0>
<a name="l11"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_PCI_QUEUE_PFN </span><span class=cFE>8</span><span class=cF0> </span><span class=cF2>// PFN for the currently selected queue</span><span class=cF0>
<a name="l12"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_PCI_QUEUE_SIZE </span><span class=cFE>12</span><span class=cF0> </span><span class=cF2>// Queue size for the currently selected queue</span><span class=cF0>
<a name="l13"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_PCI_QUEUE_SEL </span><span class=cFE>14</span><span class=cF0> </span><span class=cF2>// Queue selector</span><span class=cF0>
<a name="l14"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_PCI_QUEUE_NOTIFY </span><span class=cFE>16</span><span class=cF0> </span><span class=cF2>// Queue notifier</span><span class=cF0>
<a name="l15"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_PCI_STATUS </span><span class=cFE>18</span><span class=cF0> </span><span class=cF2>// Device status register</span><span class=cF0>
<a name="l16"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_PCI_ISR </span><span class=cFE>19</span><span class=cF0> </span><span class=cF2>// Interrupt status register</span><span class=cF0>
<a name="l17"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_PCI_CONFIG </span><span class=cFE>20</span><span class=cF0> </span><span class=cF2>// Configuration data block</span><span class=cF0>
<a name="l18"></a>
<a name="l19"></a></span><span class=cF2>//</span><span class=cF0>
<a name="l20"></a></span><span class=cF2>// PCI virtio status register bits</span><span class=cF0>
<a name="l21"></a></span><span class=cF2>//</span><span class=cF0>
<a name="l22"></a>
<a name="l23"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_CONFIG_S_ACKNOWLEDGE </span><span class=cFE>1</span><span class=cF0>
<a name="l24"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_CONFIG_S_DRIVER </span><span class=cFE>2</span><span class=cF0>
<a name="l25"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_CONFIG_S_DRIVER_OK </span><span class=cFE>4</span><span class=cF0>
<a name="l26"></a>#</span><span class=cF1>define</span><span class=cF0> VIRTIO_CONFIG_S_FAILED </span><span class=cFE>0x80</span><span class=cF0>
<a name="l27"></a>
<a name="l28"></a></span><span class=cF2>//</span><span class=cF0>
<a name="l29"></a></span><span class=cF2>// Ring descriptor flags</span><span class=cF0>
<a name="l30"></a></span><span class=cF2>//</span><span class=cF0>
<a name="l31"></a>
<a name="l32"></a>#</span><span class=cF1>define</span><span class=cF0> VRING_DESC_F_NEXT </span><span class=cFE>1</span><span class=cF0> </span><span class=cF2>// Buffer continues via the next field</span><span class=cF0>
<a name="l33"></a>#</span><span class=cF1>define</span><span class=cF0> VRING_DESC_F_WRITE </span><span class=cFE>2</span><span class=cF0> </span><span class=cF2>// Buffer is write-only (otherwise read-only)</span><span class=cF0>
<a name="l34"></a>#</span><span class=cF1>define</span><span class=cF0> VRING_DESC_F_INDIRECT </span><span class=cFE>4</span><span class=cF0> </span><span class=cF2>// Buffer contains a list of buffer descriptors</span><span class=cF0>
<a name="l35"></a>
<a name="l36"></a></span><span class=cF1>class</span><span class=cF0> @virtio_queue_buf {
<a name="l37"></a> </span><span class=cF9>U64</span><span class=cF0> address;
<a name="l38"></a> </span><span class=cF9>U32</span><span class=cF0> length;
<a name="l39"></a> </span><span class=cF9>U16</span><span class=cF0> flags;
<a name="l40"></a> </span><span class=cF9>U16</span><span class=cF0> next;
<a name="l41"></a>};
<a name="l42"></a></span><span class=cF1>class</span><span class=cF0> @virtio_avail {
<a name="l43"></a> </span><span class=cF9>U16</span><span class=cF0> flags;
<a name="l44"></a> </span><span class=cF9>U16</span><span class=cF0> index;
<a name="l45"></a> </span><span class=cF9>U16</span><span class=cF0> ring[</span><span class=cFE>256</span><span class=cF0>];
<a name="l46"></a> </span><span class=cF9>U16</span><span class=cF0> int_index;
<a name="l47"></a>};
<a name="l48"></a></span><span class=cF1>class</span><span class=cF0> @virtio_used_item {
<a name="l49"></a> </span><span class=cF9>U32</span><span class=cF0> index;
<a name="l50"></a> </span><span class=cF9>U32</span><span class=cF0> length;
<a name="l51"></a>};
<a name="l52"></a></span><span class=cF1>class</span><span class=cF0> @virtio_used {
<a name="l53"></a> </span><span class=cF9>U16</span><span class=cF0> flags;
<a name="l54"></a> </span><span class=cF9>U16</span><span class=cF0> index;
<a name="l55"></a> @virtio_used_item ring[</span><span class=cFE>256</span><span class=cF0>];
<a name="l56"></a> </span><span class=cF9>U16</span><span class=cF0> int_index;
<a name="l57"></a>};
<a name="l58"></a></span><span class=cF1>class</span><span class=cF0> @virtio_queue {
<a name="l59"></a> @virtio_queue_buf buffers[</span><span class=cFE>256</span><span class=cF0>];
<a name="l60"></a> @virtio_avail available;
<a name="l61"></a> </span><span class=cF1>U8</span><span class=cF0> padding[</span><span class=cFE>3578</span><span class=cF0>];
<a name="l62"></a> @virtio_used used;
<a name="l63"></a>};
<a name="l64"></a>
<a name="l65"></a></span><span class=cF1>class</span><span class=cF0> @virtio_avail_buf {
<a name="l66"></a> </span><span class=cF9>U32</span><span class=cF0> index;
<a name="l67"></a> </span><span class=cF9>U64</span><span class=cF0> address;
<a name="l68"></a> </span><span class=cF9>U32</span><span class=cF0> length;
<a name="l69"></a>};
<a name="l70"></a>
<a name="l71"></a></span><span class=cF1>class</span><span class=cF0> @virtio_buf_info {
<a name="l72"></a> </span><span class=cF1>U8</span><span class=cF0> *buffer;
<a name="l73"></a> </span><span class=cF9>U64</span><span class=cF0> size;
<a name="l74"></a> </span><span class=cF1>U8</span><span class=cF0> flags;
<a name="l75"></a>
<a name="l76"></a> </span><span class=cF2>// If the user wants to keep same buffer as passed in this struct, use &quot;true&quot;.</span><span class=cF0>
<a name="l77"></a> </span><span class=cF2>// otherwise, the supplied buffer will be copied in the queues' buffer</span><span class=cF0>
<a name="l78"></a> </span><span class=cF1>Bool</span><span class=cF0> copy;
<a name="l79"></a>};
<a name="l80"></a>
<a name="l81"></a></span><span class=cF6>&quot;virtio &quot;</span><span class=cF0>;
<a name="l82"></a>
<a name="l83"></a></span><span class=cF2>//</span><span class=cF0>
<a name="l84"></a></span><span class=cF2>// PCI VirtIO Net</span><span class=cF0>
<a name="l85"></a></span><span class=cF2>//</span><span class=cF0>
<a name="l86"></a>
<a name="l87"></a></span><span class=cF9>I64</span><span class=cF0> rx_buffer_ptr = </span><span class=cFE>0</span><span class=cF0>;
<a name="l88"></a></span><span class=cF9>I64</span><span class=cF0> tx_buffer_ptr = </span><span class=cFE>0</span><span class=cF0>;
<a name="l89"></a></span><span class=cF9>I64</span><span class=cF0> rx_buffer_count = </span><span class=cFE>255</span><span class=cF0>;
<a name="l90"></a></span><span class=cF9>I64</span><span class=cF0> tx_buffer_count = </span><span class=cFE>256</span><span class=cF0>;
<a name="l91"></a></span><span class=cF9>U64</span><span class=cF0> rx_buffers = </span><span class=cF5>MAlloc</span><span class=cF0>(ETHERNET_FRAME_SIZE * rx_buffer_count);
<a name="l92"></a></span><span class=cF9>U64</span><span class=cF0> tx_buffers = </span><span class=cF5>MAlloc</span><span class=cF0>(ETHERNET_FRAME_SIZE * tx_buffer_count);
<a name="l93"></a>
<a name="l94"></a></span><span class=cF1>class</span><span class=cF0> @virtio_net {
<a name="l95"></a> </span><span class=cF9>U16</span><span class=cF0> port;
<a name="l96"></a> </span><span class=cF1>U8</span><span class=cF0> mac[</span><span class=cFE>6</span><span class=cF0>];
<a name="l97"></a> @virtio_queue *rq;
<a name="l98"></a> @virtio_queue *sq;
<a name="l99"></a> </span><span class=cF9>I64</span><span class=cF0> rq_size;
<a name="l100"></a> </span><span class=cF9>I64</span><span class=cF0> rq_index;
<a name="l101"></a> </span><span class=cF9>I64</span><span class=cF0> sq_size;
<a name="l102"></a> </span><span class=cF9>I64</span><span class=cF0> sq_index;
<a name="l103"></a> </span><span class=cF9>I64</span><span class=cF0> rx_packets;
<a name="l104"></a> </span><span class=cF9>I64</span><span class=cF0> rx_bytes;
<a name="l105"></a> </span><span class=cF9>I64</span><span class=cF0> tx_packets;
<a name="l106"></a> </span><span class=cF9>I64</span><span class=cF0> tx_bytes;
<a name="l107"></a>};
<a name="l108"></a>
<a name="l109"></a></span><span class=cF1>class</span><span class=cF0> @virtio_net_header {
<a name="l110"></a> </span><span class=cF1>U8</span><span class=cF0> flags;
<a name="l111"></a> </span><span class=cF1>U8</span><span class=cF0> gso_type;
<a name="l112"></a> </span><span class=cF9>U16</span><span class=cF0> header_length;
<a name="l113"></a> </span><span class=cF9>U16</span><span class=cF0> gso_size;
<a name="l114"></a> </span><span class=cF9>U16</span><span class=cF0> checksum_start;
<a name="l115"></a> </span><span class=cF9>U16</span><span class=cF0> checksum_offset;
<a name="l116"></a>};
<a name="l117"></a>
<a name="l118"></a>@virtio_net VirtioNet;
<a name="l119"></a></span><span class=cF5>MemSet</span><span class=cF0>(&amp;VirtioNet, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>@virtio_net</span><span class=cF7>)</span><span class=cF0>);
<a name="l120"></a>
<a name="l121"></a>@virtio_net_header *def_pkt_hdr = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>@virtio_net_header</span><span class=cF7>)</span><span class=cF0>);
<a name="l122"></a>
<a name="l123"></a></span><span class=cF1>static</span><span class=cF0> </span><span class=cF9>I64</span><span class=cF0> @virtio_net_alloc_tx_packet(</span><span class=cF1>U8</span><span class=cF0> **buffer_out, </span><span class=cF9>I64</span><span class=cF0> length</span><span class=cF2>/*, I64 flags*/</span><span class=cF0>) {
<a name="l124"></a> </span><span class=cF2>// FIXME: validate length</span><span class=cF0>
<a name="l125"></a></span><span class=cF2>// flags = flags;</span><span class=cF0>
<a name="l126"></a> </span><span class=cF9>I64</span><span class=cF0> sq_idx = VirtioNet.sq-&gt;available.index % </span><span class=cFE>256</span><span class=cF0>;
<a name="l127"></a> </span><span class=cF9>I64</span><span class=cF0> sq_idx2 = sq_idx % </span><span class=cFE>128</span><span class=cF0>;
<a name="l128"></a> </span><span class=cF9>I64</span><span class=cF0> index = tx_buffer_ptr;
<a name="l129"></a> tx_buffer_ptr = (tx_buffer_ptr + </span><span class=cFE>1</span><span class=cF0>) &amp; (tx_buffer_count - </span><span class=cFE>1</span><span class=cF0>);
<a name="l130"></a> *buffer_out = tx_buffers + index * ETHERNET_FRAME_SIZE;
<a name="l131"></a>
<a name="l132"></a> VirtioNet.sq-&gt;buffers[sq_idx2 * </span><span class=cFE>2</span><span class=cF0>].address = def_pkt_hdr;
<a name="l133"></a> VirtioNet.sq-&gt;buffers[sq_idx2 * </span><span class=cFE>2</span><span class=cF0>].length = </span><span class=cF1>sizeof</span><span class=cF0>(@virtio_net_header);
<a name="l134"></a> VirtioNet.sq-&gt;buffers[sq_idx2 * </span><span class=cFE>2</span><span class=cF0>].flags = VRING_DESC_F_NEXT;
<a name="l135"></a> VirtioNet.sq-&gt;buffers[sq_idx2 * </span><span class=cFE>2</span><span class=cF0>].next = (sq_idx2 * </span><span class=cFE>2</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>;
<a name="l136"></a> VirtioNet.sq-&gt;buffers[(sq_idx2 * </span><span class=cFE>2</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>].address = *buffer_out;
<a name="l137"></a> VirtioNet.sq-&gt;buffers[(sq_idx2 * </span><span class=cFE>2</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>].length = length;
<a name="l138"></a> VirtioNet.sq-&gt;buffers[(sq_idx2 * </span><span class=cFE>2</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>].flags = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l139"></a> VirtioNet.sq-&gt;buffers[(sq_idx2 * </span><span class=cFE>2</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>].next = </span><span class=cFE>0</span><span class=cF0>;
<a name="l140"></a> VirtioNet.sq-&gt;available.ring[sq_idx] = sq_idx2 * </span><span class=cFE>2</span><span class=cF0>;
<a name="l141"></a>
<a name="l142"></a> VirtioNet.sq-&gt;available.index++;
<a name="l143"></a>
<a name="l144"></a> VirtioNet.tx_packets++;
<a name="l145"></a> VirtioNet.tx_bytes += length;
<a name="l146"></a>
<a name="l147"></a> </span><span class=cF1>return</span><span class=cF0> index;
<a name="l148"></a>}
<a name="l149"></a>
<a name="l150"></a></span><span class=cF1>static</span><span class=cF0> </span><span class=cF9>I64</span><span class=cF0> @virtio_net_finish_tx_packet(</span><span class=cF9>I64</span><span class=cF0>) {
<a name="l151"></a> </span><span class=cF5>OutU16</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_QUEUE_NOTIFY, </span><span class=cFE>1</span><span class=cF0>);
<a name="l152"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cFE>0</span><span class=cF0>;
<a name="l153"></a>}
<a name="l154"></a>
<a name="l155"></a></span><span class=cF1>U8</span><span class=cF0> *loopback_frame = </span><span class=cF5>MAlloc</span><span class=cF0>(ETHERNET_FRAME_SIZE);
<a name="l156"></a></span><span class=cF9>I64</span><span class=cF0> loopback_length = </span><span class=cFE>0</span><span class=cF0>;
<a name="l157"></a>
<a name="l158"></a></span><span class=cF9>I64</span><span class=cF0> EthernetFrameAllocate(</span><span class=cF1>U8</span><span class=cF0> **buffer_out, </span><span class=cF1>U8</span><span class=cF0> *src_addr, </span><span class=cF1>U8</span><span class=cF0> *dst_addr,
<a name="l159"></a> </span><span class=cF9>U16</span><span class=cF0> ethertype, </span><span class=cF9>I64</span><span class=cF0> length</span><span class=cF2>/*, I64 flags*/</span><span class=cF0>) {
<a name="l160"></a>
<a name="l161"></a> </span><span class=cF1>U8</span><span class=cF0> *frame;
<a name="l162"></a>
<a name="l163"></a> </span><span class=cF2>// APAD_XMT doesn't seem to work in VirtualBox, so we have to pad the frame</span><span class=cF0>
<a name="l164"></a> </span><span class=cF2>// ourselves</span><span class=cF0>
<a name="l165"></a> </span><span class=cF1>if</span><span class=cF0> (length &lt; </span><span class=cFE>46</span><span class=cF0>)
<a name="l166"></a> length = </span><span class=cFE>46</span><span class=cF0>;
<a name="l167"></a>
<a name="l168"></a> </span><span class=cF9>I64</span><span class=cF0> index;
<a name="l169"></a>
<a name="l170"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>MemCompare</span><span class=cF7>(</span><span class=cF0>dst_addr, &amp;VirtioNet.mac, </span><span class=cFE>6</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l171"></a> frame = loopback_frame;
<a name="l172"></a> loopback_length = length;
<a name="l173"></a> index = </span><span class=cF3>I64_MAX</span><span class=cF0>;
<a name="l174"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l175"></a> index = @virtio_net_alloc_tx_packet(&amp;frame, </span><span class=cFE>14</span><span class=cF0> + length</span><span class=cF2>/*, flags*/</span><span class=cF0>);
<a name="l176"></a> </span><span class=cF1>if</span><span class=cF0> (index &lt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l177"></a> </span><span class=cF1>return</span><span class=cF0> index;
<a name="l178"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l179"></a>
<a name="l180"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(frame + </span><span class=cFE>0</span><span class=cF0>, dst_addr, </span><span class=cFE>6</span><span class=cF0>);
<a name="l181"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(frame + </span><span class=cFE>6</span><span class=cF0>, src_addr, </span><span class=cFE>6</span><span class=cF0>);
<a name="l182"></a> frame[</span><span class=cFE>12</span><span class=cF0>] = (ethertype &gt;&gt; </span><span class=cFE>8</span><span class=cF0>);
<a name="l183"></a> frame[</span><span class=cFE>13</span><span class=cF0>] = (ethertype &amp; </span><span class=cFE>0xff</span><span class=cF0>);
<a name="l184"></a>
<a name="l185"></a> *buffer_out = frame + </span><span class=cFE>14</span><span class=cF0>;
<a name="l186"></a> </span><span class=cF1>return</span><span class=cF0> index;
<a name="l187"></a>}
<a name="l188"></a>
<a name="l189"></a></span><span class=cF9>I64</span><span class=cF0> EthernetFrameFinish(</span><span class=cF9>I64</span><span class=cF0> index) {
<a name="l190"></a> </span><span class=cF1>if</span><span class=cF0> (index == </span><span class=cF3>I64_MAX</span><span class=cF0> &amp;&amp; loopback_frame &amp;&amp; loopback_length) </span><span class=cF7>{</span><span class=cF0>
<a name="l191"></a> NetQueuePush(loopback_frame, loopback_length);
<a name="l192"></a> loopback_length = </span><span class=cFE>0</span><span class=cF0>;
<a name="l193"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cFE>0</span><span class=cF0>;
<a name="l194"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l195"></a> </span><span class=cF1>return</span><span class=cF0> @virtio_net_finish_tx_packet(index);
<a name="l196"></a>}
<a name="l197"></a>
<a name="l198"></a></span><span class=cF1>U8</span><span class=cF0> *EthernetMACGet() { </span><span class=cF1>return</span><span class=cF0> &amp;VirtioNet.mac; }
<a name="l199"></a>
<a name="l200"></a></span><span class=cF9>I64</span><span class=cF0> @virtio_net_init() {
<a name="l201"></a> </span><span class=cF9>I64</span><span class=cF0> i, j;
<a name="l202"></a>
<a name="l203"></a> </span><span class=cF2>// Scan for device</span><span class=cF0>
<a name="l204"></a> j = </span><span class=cF5>PCIClassFind</span><span class=cF0>(</span><span class=cFE>0x020000</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF0>);
<a name="l205"></a> </span><span class=cF1>if</span><span class=cF0> (j &lt; </span><span class=cFE>0</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l206"></a> </span><span class=cF6>&quot;\nVirtio-net device not found.\n&quot;</span><span class=cF0>;
<a name="l207"></a> </span><span class=cF1>return</span><span class=cF0> -</span><span class=cFE>1</span><span class=cF0>;
<a name="l208"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l209"></a> VirtioNet.port = </span><span class=cF5>PCIReadU32</span><span class=cF0>(j.u8[</span><span class=cFE>2</span><span class=cF0>], j.u8[</span><span class=cFE>1</span><span class=cF0>], j.u8[</span><span class=cFE>0</span><span class=cF0>], </span><span class=cFE>0x10</span><span class=cF0>) &amp; </span><span class=cFE>0xFFFFFFFC</span><span class=cF0>;
<a name="l210"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; </span><span class=cFE>6</span><span class=cF0>; i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l211"></a> VirtioNet.mac[i] = </span><span class=cF5>InU8</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_CONFIG + i);
<a name="l212"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l213"></a>
<a name="l214"></a> </span><span class=cF2>// Reset Device</span><span class=cF0>
<a name="l215"></a> </span><span class=cF5>OutU8</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_STATUS, </span><span class=cFE>0</span><span class=cF0>);
<a name="l216"></a>
<a name="l217"></a> </span><span class=cF2>// Found Driver</span><span class=cF0>
<a name="l218"></a> </span><span class=cF5>OutU8</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_STATUS,
<a name="l219"></a> </span><span class=cF5>InU8</span><span class=cF7>(</span><span class=cF0>VirtioNet.port + VIRTIO_PCI_STATUS</span><span class=cF7>)</span><span class=cF0> | VIRTIO_CONFIG_S_ACKNOWLEDGE |
<a name="l220"></a> VIRTIO_CONFIG_S_DRIVER);
<a name="l221"></a>
<a name="l222"></a> </span><span class=cF2>// Set up receive queue</span><span class=cF0>
<a name="l223"></a> </span><span class=cF5>OutU16</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_QUEUE_SEL, </span><span class=cFE>0</span><span class=cF0>);
<a name="l224"></a> VirtioNet.rq_size = </span><span class=cF5>InU16</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_QUEUE_SIZE); </span><span class=cF2>// 256</span><span class=cF0>
<a name="l225"></a> VirtioNet.rq = </span><span class=cF5>CAllocAligned</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>@virtio_queue</span><span class=cF7>)</span><span class=cF0>, </span><span class=cFE>4096</span><span class=cF0>, </span><span class=cF5>Fs</span><span class=cF0>-&gt;code_heap);
<a name="l226"></a> </span><span class=cF5>OutU32</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_QUEUE_PFN, VirtioNet.rq / </span><span class=cFE>4096</span><span class=cF0>);
<a name="l227"></a>
<a name="l228"></a> </span><span class=cF2>// Set up send queue</span><span class=cF0>
<a name="l229"></a> </span><span class=cF5>OutU16</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_QUEUE_SEL, </span><span class=cFE>1</span><span class=cF0>);
<a name="l230"></a> VirtioNet.sq_size = </span><span class=cF5>InU16</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_QUEUE_SIZE); </span><span class=cF2>// 256</span><span class=cF0>
<a name="l231"></a> VirtioNet.sq = </span><span class=cF5>CAllocAligned</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>@virtio_queue</span><span class=cF7>)</span><span class=cF0>, </span><span class=cFE>4096</span><span class=cF0>, </span><span class=cF5>Fs</span><span class=cF0>-&gt;code_heap);
<a name="l232"></a> </span><span class=cF5>OutU32</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_QUEUE_PFN, VirtioNet.sq / </span><span class=cFE>4096</span><span class=cF0>);
<a name="l233"></a>
<a name="l234"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; </span><span class=cFE>128</span><span class=cF0>; i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l235"></a> VirtioNet.rq-&gt;buffers[i * </span><span class=cFE>2</span><span class=cF0>].address = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>@virtio_net_header</span><span class=cF7>)</span><span class=cF0>);
<a name="l236"></a> VirtioNet.rq-&gt;buffers[i * </span><span class=cFE>2</span><span class=cF0>].length = </span><span class=cF1>sizeof</span><span class=cF0>(@virtio_net_header);
<a name="l237"></a> VirtioNet.rq-&gt;buffers[i * </span><span class=cFE>2</span><span class=cF0>].flags = VRING_DESC_F_NEXT | VRING_DESC_F_WRITE;
<a name="l238"></a> VirtioNet.rq-&gt;buffers[i * </span><span class=cFE>2</span><span class=cF0>].next = (i * </span><span class=cFE>2</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>;
<a name="l239"></a> VirtioNet.rq-&gt;buffers[(i * </span><span class=cFE>2</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>].address = </span><span class=cF5>CAlloc</span><span class=cF0>(ETHERNET_FRAME_SIZE);
<a name="l240"></a> VirtioNet.rq-&gt;buffers[(i * </span><span class=cFE>2</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>].length = ETHERNET_FRAME_SIZE;
<a name="l241"></a> VirtioNet.rq-&gt;buffers[(i * </span><span class=cFE>2</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>].flags = VRING_DESC_F_WRITE;
<a name="l242"></a> VirtioNet.rq-&gt;buffers[(i * </span><span class=cFE>2</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>].next = </span><span class=cFE>0</span><span class=cF0>;
<a name="l243"></a> VirtioNet.rq-&gt;available.ring[i] = i * </span><span class=cFE>2</span><span class=cF0>;
<a name="l244"></a> VirtioNet.rq-&gt;available.ring[i + </span><span class=cFE>128</span><span class=cF0>] = i * </span><span class=cFE>2</span><span class=cF0>;
<a name="l245"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l246"></a> VirtioNet.rq-&gt;available.index = </span><span class=cFE>1</span><span class=cF0>;
<a name="l247"></a>
<a name="l248"></a> </span><span class=cF2>// Init OK</span><span class=cF0>
<a name="l249"></a> </span><span class=cF5>OutU8</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_STATUS,
<a name="l250"></a> </span><span class=cF5>InU8</span><span class=cF7>(</span><span class=cF0>VirtioNet.port + VIRTIO_PCI_STATUS</span><span class=cF7>)</span><span class=cF0> | VIRTIO_CONFIG_S_DRIVER_OK);
<a name="l251"></a> </span><span class=cF5>OutU16</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_QUEUE_NOTIFY, </span><span class=cFE>0</span><span class=cF0>);
<a name="l252"></a> </span><span class=cF6>&quot;\x1b[33mVirtio-net device detected, MAC address &quot;</span><span class=cF0>
<a name="l253"></a> </span><span class=cF6>&quot;%02x:%02x:%02x:%02x:%02x:%02x\x1b[0m\n&quot;</span><span class=cF0>,
<a name="l254"></a> VirtioNet.mac[</span><span class=cFE>0</span><span class=cF0>], VirtioNet.mac[</span><span class=cFE>1</span><span class=cF0>], VirtioNet.mac[</span><span class=cFE>2</span><span class=cF0>], VirtioNet.mac[</span><span class=cFE>3</span><span class=cF0>],
<a name="l255"></a> VirtioNet.mac[</span><span class=cFE>4</span><span class=cF0>], VirtioNet.mac[</span><span class=cFE>5</span><span class=cF0>];
<a name="l256"></a>}
<a name="l257"></a>
<a name="l258"></a></span><span class=cF6>&quot;virtio-net &quot;</span><span class=cF0>;
<a name="l259"></a>@virtio_net_init;
<a name="l260"></a>
<a name="l261"></a></span><span class=cF2>/*</span><span class=cF0>
<a name="l262"></a></span><span class=cF2>U0 @virtio_net_handle_net_fifo_entry(CNetFifoEntry *e) {</span><span class=cF0>
<a name="l263"></a></span><span class=cF2> CEthFrame l2_frame;</span><span class=cF0>
<a name="l264"></a>
<a name="l265"></a></span><span class=cF2> if (EthernetFrameParse(&amp;l2_frame, e-&gt;frame, e-&gt;length) &lt; 0)</span><span class=cF0>
<a name="l266"></a></span><span class=cF2> return;</span><span class=cF0>
<a name="l267"></a>
<a name="l268"></a></span><span class=cF2> CL3Protocol *l3 = l3_protocols;</span><span class=cF0>
<a name="l269"></a>
<a name="l270"></a></span><span class=cF2> while (l3) {</span><span class=cF0>
<a name="l271"></a></span><span class=cF2> if (l3-&gt;ethertype == l2_frame.ethertype) {</span><span class=cF0>
<a name="l272"></a></span><span class=cF2> l3-&gt;handler(&amp;l2_frame);</span><span class=cF0>
<a name="l273"></a></span><span class=cF2> break;</span><span class=cF0>
<a name="l274"></a></span><span class=cF2> }</span><span class=cF0>
<a name="l275"></a></span><span class=cF2> l3 = l3-&gt;next;</span><span class=cF0>
<a name="l276"></a></span><span class=cF2> }</span><span class=cF0>
<a name="l277"></a></span><span class=cF2>}</span><span class=cF0>
<a name="l278"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l279"></a></span><span class=cF1>U0</span><span class=cF0> @virtio_net_handler_task() {
<a name="l280"></a> </span><span class=cF9>I64</span><span class=cF0> idx_used, idx_rec;
<a name="l281"></a> </span><span class=cF9>I64</span><span class=cF0> i, j;
<a name="l282"></a> @virtio_used_item *item;
<a name="l283"></a> </span><span class=cF1>U8</span><span class=cF0> *buffer;
<a name="l284"></a> </span><span class=cF9>I64</span><span class=cF0> length;
<a name="l285"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l286"></a> idx_rec = VirtioNet.rq_index;
<a name="l287"></a> idx_used = VirtioNet.rq-&gt;used.index;
<a name="l288"></a>
<a name="l289"></a> </span><span class=cF1>if</span><span class=cF0> (idx_used &lt; idx_rec) {
<a name="l290"></a> idx_used += </span><span class=cFE>0x10000</span><span class=cF0>;
<a name="l291"></a> }
<a name="l292"></a>
<a name="l293"></a> </span><span class=cF1>if</span><span class=cF0> (idx_rec != idx_used &amp;&amp; idx_used) {
<a name="l294"></a>
<a name="l295"></a> j = </span><span class=cFE>0</span><span class=cF0>;
<a name="l296"></a> </span><span class=cF1>for</span><span class=cF0> (i = idx_rec; i &lt; idx_used; i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l297"></a> item = VirtioNet.rq-&gt;used.ring;
<a name="l298"></a> buffer = VirtioNet.rq-&gt;buffers[item[i % </span><span class=cFE>256</span><span class=cF0>].index + </span><span class=cFE>1</span><span class=cF0>];
<a name="l299"></a> length = item[i % </span><span class=cFE>256</span><span class=cF0>].length;
<a name="l300"></a> NetQueuePush(buffer, length - </span><span class=cFE>10</span><span class=cF0>);
<a name="l301"></a> j++;
<a name="l302"></a> VirtioNet.rx_packets++;
<a name="l303"></a> VirtioNet.rx_bytes += length - </span><span class=cFE>10</span><span class=cF0>;
<a name="l304"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l305"></a> VirtioNet.rq_index = idx_used % </span><span class=cFE>0x10000</span><span class=cF0>;
<a name="l306"></a> VirtioNet.rq-&gt;available.index += j;
<a name="l307"></a> </span><span class=cF5>OutU16</span><span class=cF0>(VirtioNet.port + VIRTIO_PCI_QUEUE_NOTIFY, </span><span class=cFE>0</span><span class=cF0>);
<a name="l308"></a> }
<a name="l309"></a> CNetQueueEntry *e = NetQueuePull;
<a name="l310"></a> </span><span class=cF1>if</span><span class=cF0> (e) {
<a name="l311"></a></span><span class=cF2>// @virtio_net_handle_net_fifo_entry(e);</span><span class=cF0>
<a name="l312"></a> }
<a name="l313"></a> </span><span class=cF5>Sleep</span><span class=cF0>(</span><span class=cFE>30</span><span class=cF0>);
<a name="l314"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l315"></a>}
<a name="l316"></a>
<a name="l317"></a></span><span class=cF5>Spawn</span><span class=cF0>(&amp;@virtio_net_handler_task,, </span><span class=cF6>&quot;Virtio-net&quot;</span><span class=cF0>);</span></pre></body>
</html>

View file

@ -31,8 +31,9 @@ body {background-color:#fef1f0;}
<a name="l3"></a></span><a href="https://zeal-operating-system.github.io/ZealOS/Home/Net/index.html#l1"><span class=cE4>../</span></a><span class=cF4>
<a name="l4"></a></span><a href="https://zeal-operating-system.github.io/ZealOS/Home/Net/Drivers/E1000.CC.html#l1"><span class=cF4>E1000.CC</span></a><span class=cF4>
<a name="l5"></a></span><a href="https://zeal-operating-system.github.io/ZealOS/Home/Net/Drivers/PCNet.CC.html#l1"><span class=cF4>PCNet.CC</span></a><span class=cF4>
<a name="l6"></a></span><a href="https://zeal-operating-system.github.io/ZealOS/Home/Net/Drivers/Run.CC.html#l1"><span class=cF4>Run.CC</span></a><span class=cF0>
<a name="l7"></a>
<a name="l6"></a></span><a href="https://zeal-operating-system.github.io/ZealOS/Home/Net/Drivers/Run.CC.html#l1"><span class=cF4>Run.CC</span></a><span class=cF4>
<a name="l7"></a></span><a href="https://zeal-operating-system.github.io/ZealOS/Home/Net/Drivers/VirtIONet.CC.html#l1"><span class=cF4>VirtIONet.CC</span></a><span class=cF0>
<a name="l8"></a>
<a name="l9"></a>
</span></pre></body>
</html>

View file

@ -34,7 +34,15 @@ body {background-color:#fef1f0;}
<a name="l7"></a>Host(</span><span class=cF6>&quot;bitchute.com&quot;</span><span class=cF0>);
<a name="l8"></a></span><span class=cF6>&quot;Host(\&quot;github.com\&quot;);\n&quot;</span><span class=cF0>;
<a name="l9"></a>Host(</span><span class=cF6>&quot;github.com&quot;</span><span class=cF0>);
<a name="l10"></a>
<a name="l11"></a>DNSRep;
<a name="l12"></a>ARPRep;</span></pre></body>
<a name="l10"></a></span><span class=cF6>&quot;Host(\&quot;soundcloud.com\&quot;);\n&quot;</span><span class=cF0>;
<a name="l11"></a>Host(</span><span class=cF6>&quot;soundcloud.com&quot;</span><span class=cF0>);
<a name="l12"></a></span><span class=cF6>&quot;Host(\&quot;wiki.osdev.org\&quot;);\n&quot;</span><span class=cF0>;
<a name="l13"></a>Host(</span><span class=cF6>&quot;wiki.osdev.org&quot;</span><span class=cF0>);
<a name="l14"></a></span><span class=cF6>&quot;Host(\&quot;archive.org\&quot;);\n&quot;</span><span class=cF0>;
<a name="l15"></a>Host(</span><span class=cF6>&quot;archive.org&quot;</span><span class=cF0>);
<a name="l16"></a></span><span class=cF6>&quot;Host(\&quot;git.checksum.fail\&quot;);\n&quot;</span><span class=cF0>;
<a name="l17"></a>Host(</span><span class=cF6>&quot;git.checksum.fail&quot;</span><span class=cF0>);
<a name="l18"></a>
<a name="l19"></a>DNSRep;
<a name="l20"></a>ARPRep;</span></pre></body>
</html>

File diff suppressed because it is too large Load diff

View file

@ -7,6 +7,10 @@
#define PCIV_E1000 0x8086
#define PCID_82545EM 0x100F
#define PCIV_VIRTIO 0x1AF4
//#define PCID_VIRTIO_NET 0x1000
U0 NetDriverInclude(U8 *driver)
{
U8 *filename = MStrPrint(__DIR__ "/%s", driver);
@ -41,10 +45,18 @@ U0 NetDriverInit()
break;
}
break;
case PCIV_VIRTIO:
NetDriverInclude("VirtIONet");
found = TRUE;
break;
}
if (!found)
throw('NET');
{
ClassRep(net_driver_pci);
throw('NODRIVER');
}
}
NetDriverInit;

317
src/Home/Net/Drivers/VirtIONet.CC Executable file
View file

@ -0,0 +1,317 @@
/*
VirtIO Net driver ported from alec's Erythros.
*/
//
// PCI virtio I/O registers.
//
#define VIRTIO_PCI_HOST_FEATURES 0 // Features supported by the host
#define VIRTIO_PCI_GUEST_FEATURES 4 // Features activated by the guest
#define VIRTIO_PCI_QUEUE_PFN 8 // PFN for the currently selected queue
#define VIRTIO_PCI_QUEUE_SIZE 12 // Queue size for the currently selected queue
#define VIRTIO_PCI_QUEUE_SEL 14 // Queue selector
#define VIRTIO_PCI_QUEUE_NOTIFY 16 // Queue notifier
#define VIRTIO_PCI_STATUS 18 // Device status register
#define VIRTIO_PCI_ISR 19 // Interrupt status register
#define VIRTIO_PCI_CONFIG 20 // Configuration data block
//
// PCI virtio status register bits
//
#define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
#define VIRTIO_CONFIG_S_DRIVER 2
#define VIRTIO_CONFIG_S_DRIVER_OK 4
#define VIRTIO_CONFIG_S_FAILED 0x80
//
// Ring descriptor flags
//
#define VRING_DESC_F_NEXT 1 // Buffer continues via the next field
#define VRING_DESC_F_WRITE 2 // Buffer is write-only (otherwise read-only)
#define VRING_DESC_F_INDIRECT 4 // Buffer contains a list of buffer descriptors
class @virtio_queue_buf {
U64 address;
U32 length;
U16 flags;
U16 next;
};
class @virtio_avail {
U16 flags;
U16 index;
U16 ring[256];
U16 int_index;
};
class @virtio_used_item {
U32 index;
U32 length;
};
class @virtio_used {
U16 flags;
U16 index;
@virtio_used_item ring[256];
U16 int_index;
};
class @virtio_queue {
@virtio_queue_buf buffers[256];
@virtio_avail available;
U8 padding[3578];
@virtio_used used;
};
class @virtio_avail_buf {
U32 index;
U64 address;
U32 length;
};
class @virtio_buf_info {
U8 *buffer;
U64 size;
U8 flags;
// If the user wants to keep same buffer as passed in this struct, use "true".
// otherwise, the supplied buffer will be copied in the queues' buffer
Bool copy;
};
"virtio ";
//
// PCI VirtIO Net
//
I64 rx_buffer_ptr = 0;
I64 tx_buffer_ptr = 0;
I64 rx_buffer_count = 255;
I64 tx_buffer_count = 256;
U64 rx_buffers = MAlloc(ETHERNET_FRAME_SIZE * rx_buffer_count);
U64 tx_buffers = MAlloc(ETHERNET_FRAME_SIZE * tx_buffer_count);
class @virtio_net {
U16 port;
U8 mac[6];
@virtio_queue *rq;
@virtio_queue *sq;
I64 rq_size;
I64 rq_index;
I64 sq_size;
I64 sq_index;
I64 rx_packets;
I64 rx_bytes;
I64 tx_packets;
I64 tx_bytes;
};
class @virtio_net_header {
U8 flags;
U8 gso_type;
U16 header_length;
U16 gso_size;
U16 checksum_start;
U16 checksum_offset;
};
@virtio_net VirtioNet;
MemSet(&VirtioNet, 0, sizeof(@virtio_net));
@virtio_net_header *def_pkt_hdr = CAlloc(sizeof(@virtio_net_header));
static I64 @virtio_net_alloc_tx_packet(U8 **buffer_out, I64 length/*, I64 flags*/) {
// FIXME: validate length
// flags = flags;
I64 sq_idx = VirtioNet.sq->available.index % 256;
I64 sq_idx2 = sq_idx % 128;
I64 index = tx_buffer_ptr;
tx_buffer_ptr = (tx_buffer_ptr + 1) & (tx_buffer_count - 1);
*buffer_out = tx_buffers + index * ETHERNET_FRAME_SIZE;
VirtioNet.sq->buffers[sq_idx2 * 2].address = def_pkt_hdr;
VirtioNet.sq->buffers[sq_idx2 * 2].length = sizeof(@virtio_net_header);
VirtioNet.sq->buffers[sq_idx2 * 2].flags = VRING_DESC_F_NEXT;
VirtioNet.sq->buffers[sq_idx2 * 2].next = (sq_idx2 * 2) + 1;
VirtioNet.sq->buffers[(sq_idx2 * 2) + 1].address = *buffer_out;
VirtioNet.sq->buffers[(sq_idx2 * 2) + 1].length = length;
VirtioNet.sq->buffers[(sq_idx2 * 2) + 1].flags = NULL;
VirtioNet.sq->buffers[(sq_idx2 * 2) + 1].next = 0;
VirtioNet.sq->available.ring[sq_idx] = sq_idx2 * 2;
VirtioNet.sq->available.index++;
VirtioNet.tx_packets++;
VirtioNet.tx_bytes += length;
return index;
}
static I64 @virtio_net_finish_tx_packet(I64) {
OutU16(VirtioNet.port + VIRTIO_PCI_QUEUE_NOTIFY, 1);
return 0;
}
U8 *loopback_frame = MAlloc(ETHERNET_FRAME_SIZE);
I64 loopback_length = 0;
I64 EthernetFrameAllocate(U8 **buffer_out, U8 *src_addr, U8 *dst_addr,
U16 ethertype, I64 length/*, I64 flags*/) {
U8 *frame;
// APAD_XMT doesn't seem to work in VirtualBox, so we have to pad the frame
// ourselves
if (length < 46)
length = 46;
I64 index;
if (!MemCompare(dst_addr, &VirtioNet.mac, 6)) {
frame = loopback_frame;
loopback_length = length;
index = I64_MAX;
} else {
index = @virtio_net_alloc_tx_packet(&frame, 14 + length/*, flags*/);
if (index < 0)
return index;
}
MemCopy(frame + 0, dst_addr, 6);
MemCopy(frame + 6, src_addr, 6);
frame[12] = (ethertype >> 8);
frame[13] = (ethertype & 0xff);
*buffer_out = frame + 14;
return index;
}
I64 EthernetFrameFinish(I64 index) {
if (index == I64_MAX && loopback_frame && loopback_length) {
NetQueuePush(loopback_frame, loopback_length);
loopback_length = 0;
return 0;
}
return @virtio_net_finish_tx_packet(index);
}
U8 *EthernetMACGet() { return &VirtioNet.mac; }
I64 @virtio_net_init() {
I64 i, j;
// Scan for device
j = PCIClassFind(0x020000, 0);
if (j < 0) {
"\nVirtio-net device not found.\n";
return -1;
}
VirtioNet.port = PCIReadU32(j.u8[2], j.u8[1], j.u8[0], 0x10) & 0xFFFFFFFC;
for (i = 0; i < 6; i++) {
VirtioNet.mac[i] = InU8(VirtioNet.port + VIRTIO_PCI_CONFIG + i);
}
// Reset Device
OutU8(VirtioNet.port + VIRTIO_PCI_STATUS, 0);
// Found Driver
OutU8(VirtioNet.port + VIRTIO_PCI_STATUS,
InU8(VirtioNet.port + VIRTIO_PCI_STATUS) | VIRTIO_CONFIG_S_ACKNOWLEDGE |
VIRTIO_CONFIG_S_DRIVER);
// Set up receive queue
OutU16(VirtioNet.port + VIRTIO_PCI_QUEUE_SEL, 0);
VirtioNet.rq_size = InU16(VirtioNet.port + VIRTIO_PCI_QUEUE_SIZE); // 256
VirtioNet.rq = CAllocAligned(sizeof(@virtio_queue), 4096, Fs->code_heap);
OutU32(VirtioNet.port + VIRTIO_PCI_QUEUE_PFN, VirtioNet.rq / 4096);
// Set up send queue
OutU16(VirtioNet.port + VIRTIO_PCI_QUEUE_SEL, 1);
VirtioNet.sq_size = InU16(VirtioNet.port + VIRTIO_PCI_QUEUE_SIZE); // 256
VirtioNet.sq = CAllocAligned(sizeof(@virtio_queue), 4096, Fs->code_heap);
OutU32(VirtioNet.port + VIRTIO_PCI_QUEUE_PFN, VirtioNet.sq / 4096);
for (i = 0; i < 128; i++) {
VirtioNet.rq->buffers[i * 2].address = CAlloc(sizeof(@virtio_net_header));
VirtioNet.rq->buffers[i * 2].length = sizeof(@virtio_net_header);
VirtioNet.rq->buffers[i * 2].flags = VRING_DESC_F_NEXT | VRING_DESC_F_WRITE;
VirtioNet.rq->buffers[i * 2].next = (i * 2) + 1;
VirtioNet.rq->buffers[(i * 2) + 1].address = CAlloc(ETHERNET_FRAME_SIZE);
VirtioNet.rq->buffers[(i * 2) + 1].length = ETHERNET_FRAME_SIZE;
VirtioNet.rq->buffers[(i * 2) + 1].flags = VRING_DESC_F_WRITE;
VirtioNet.rq->buffers[(i * 2) + 1].next = 0;
VirtioNet.rq->available.ring[i] = i * 2;
VirtioNet.rq->available.ring[i + 128] = i * 2;
}
VirtioNet.rq->available.index = 1;
// Init OK
OutU8(VirtioNet.port + VIRTIO_PCI_STATUS,
InU8(VirtioNet.port + VIRTIO_PCI_STATUS) | VIRTIO_CONFIG_S_DRIVER_OK);
OutU16(VirtioNet.port + VIRTIO_PCI_QUEUE_NOTIFY, 0);
"\x1b[33mVirtio-net device detected, MAC address "
"%02x:%02x:%02x:%02x:%02x:%02x\x1b[0m\n",
VirtioNet.mac[0], VirtioNet.mac[1], VirtioNet.mac[2], VirtioNet.mac[3],
VirtioNet.mac[4], VirtioNet.mac[5];
}
"virtio-net ";
@virtio_net_init;
/*
U0 @virtio_net_handle_net_fifo_entry(CNetFifoEntry *e) {
CEthFrame l2_frame;
if (EthernetFrameParse(&l2_frame, e->frame, e->length) < 0)
return;
CL3Protocol *l3 = l3_protocols;
while (l3) {
if (l3->ethertype == l2_frame.ethertype) {
l3->handler(&l2_frame);
break;
}
l3 = l3->next;
}
}
*/
U0 @virtio_net_handler_task() {
I64 idx_used, idx_rec;
I64 i, j;
@virtio_used_item *item;
U8 *buffer;
I64 length;
while (1) {
idx_rec = VirtioNet.rq_index;
idx_used = VirtioNet.rq->used.index;
if (idx_used < idx_rec) {
idx_used += 0x10000;
}
if (idx_rec != idx_used && idx_used) {
j = 0;
for (i = idx_rec; i < idx_used; i++) {
item = VirtioNet.rq->used.ring;
buffer = VirtioNet.rq->buffers[item[i % 256].index + 1];
length = item[i % 256].length;
NetQueuePush(buffer, length - 10);
j++;
VirtioNet.rx_packets++;
VirtioNet.rx_bytes += length - 10;
}
VirtioNet.rq_index = idx_used % 0x10000;
VirtioNet.rq->available.index += j;
OutU16(VirtioNet.port + VIRTIO_PCI_QUEUE_NOTIFY, 0);
}
CNetQueueEntry *e = NetQueuePull;
if (e) {
// @virtio_net_handle_net_fifo_entry(e);
}
Sleep(30);
}
}
Spawn(&@virtio_net_handler_task,, "Virtio-net");

View file

@ -7,6 +7,14 @@ Host("duckduckgo.com");
Host("bitchute.com");
"Host(\"github.com\");\n";
Host("github.com");
"Host(\"soundcloud.com\");\n";
Host("soundcloud.com");
"Host(\"wiki.osdev.org\");\n";
Host("wiki.osdev.org");
"Host(\"archive.org\");\n";
Host("archive.org");
"Host(\"git.checksum.fail\");\n";
Host("git.checksum.fail");
DNSRep;
ARPRep;