templeos-info/public/Wb/Kernel/Mem/MemPhysical.HC.HTML

213 lines
19 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF1>Bool</span><span class=cF0> </span><span class=cFD>Mem32DevIns</span><span class=cF0>(</span><span class=cF9>CMemRange</span><span class=cF0> *tmpmr)
<a name="l2"></a>{
<a name="l3"></a> </span><span class=cF9>CMemRange</span><span class=cF0> *tmpmr1=</span><span class=cFB>dev</span><span class=cF0>.mem32_head.next,*tmpmr2;
<a name="l4"></a> </span><span class=cF1>while</span><span class=cF0> (tmpmr1!=&amp;</span><span class=cFB>dev</span><span class=cF0>.mem32_head) </span><span class=cF7>{</span><span class=cF0>
<a name="l5"></a> </span><span class=cF1>if</span><span class=cF0> (!tmpmr1-&gt;type &amp;&amp; tmpmr-&gt;base&gt;=tmpmr1-&gt;base &amp;&amp;
<a name="l6"></a> tmpmr-&gt;base+tmpmr-&gt;size&lt;=tmpmr1-&gt;base+tmpmr1-&gt;size) {
<a name="l7"></a> </span><span class=cF1>if</span><span class=cF0> (tmpmr-&gt;base&gt;tmpmr1-&gt;base) </span><span class=cF7>{</span><span class=cF0>
<a name="l8"></a> tmpmr2=</span><span class=cF5>AMAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CMemRange</span><span class=cF7>)</span><span class=cF0>);
<a name="l9"></a> tmpmr2-&gt;type=</span><span class=cF3>MRT_UNUSED</span><span class=cF0>;
<a name="l10"></a> tmpmr2-&gt;flags=0;
<a name="l11"></a> tmpmr2-&gt;base=tmpmr1-&gt;base;
<a name="l12"></a> tmpmr2-&gt;size=tmpmr-&gt;base-tmpmr1-&gt;base;
<a name="l13"></a> </span><span class=cF5>QueInsRev</span><span class=cF0>(tmpmr2,tmpmr1);
<a name="l14"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l15"></a> </span><span class=cF5>QueInsRev</span><span class=cF0>(tmpmr,tmpmr1);
<a name="l16"></a> tmpmr1-&gt;size=tmpmr1-&gt;base+tmpmr1-&gt;size-
<a name="l17"></a> (tmpmr-&gt;base+tmpmr-&gt;size);
<a name="l18"></a> tmpmr1-&gt;base=tmpmr-&gt;base+tmpmr-&gt;size;
<a name="l19"></a> </span><span class=cF1>if</span><span class=cF0> (!tmpmr1-&gt;size) </span><span class=cF7>{</span><span class=cF0>
<a name="l20"></a> </span><span class=cF5>QueRem</span><span class=cF0>(tmpmr1);
<a name="l21"></a> </span><span class=cF5>Free</span><span class=cF0>(tmpmr1);
<a name="l22"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l23"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l24"></a> }
<a name="l25"></a> tmpmr1=tmpmr1-&gt;next;
<a name="l26"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l27"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l28"></a>}
<a name="l29"></a>
<a name="l30"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>Mem32DevInit</span><span class=cF0>()
<a name="l31"></a>{
<a name="l32"></a> </span><span class=cF9>CMemRange</span><span class=cF0> *tmpmr;
<a name="l33"></a> </span><span class=cF9>CMemE820</span><span class=cF0> *m20=</span><span class=cFD>MEM_E820</span><span class=cF0>;
<a name="l34"></a>
<a name="l35"></a> </span><span class=cF5>QueInit</span><span class=cF0>(&amp;</span><span class=cFB>dev</span><span class=cF0>.mem32_head);
<a name="l36"></a> tmpmr=</span><span class=cF5>AMAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CMemRange</span><span class=cF7>)</span><span class=cF0>);
<a name="l37"></a> tmpmr-&gt;type=</span><span class=cF3>MRT_UNUSED</span><span class=cF0>;
<a name="l38"></a> tmpmr-&gt;flags=0;
<a name="l39"></a></span><span class=cF2>//Maybe !!! Change this to 0xF0000000 !!!</span><span class=cF0>
<a name="l40"></a> tmpmr-&gt;base=0xE0000000;
<a name="l41"></a> tmpmr-&gt;size=0x10000000;
<a name="l42"></a> </span><span class=cF5>QueIns</span><span class=cF0>(tmpmr,</span><span class=cFB>dev</span><span class=cF0>.mem32_head.last);
<a name="l43"></a>
<a name="l44"></a> </span><span class=cF1>if</span><span class=cF0> (m20-&gt;type) </span><span class=cF7>{</span><span class=cF0>
<a name="l45"></a> </span><span class=cF1>while</span><span class=cF0> (m20-&gt;type) {
<a name="l46"></a> tmpmr=</span><span class=cF5>AMAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CMemRange</span><span class=cF7>)</span><span class=cF0>);
<a name="l47"></a> tmpmr-&gt;type=m20-&gt;type;
<a name="l48"></a> tmpmr-&gt;flags=0;
<a name="l49"></a> tmpmr-&gt;base=m20-&gt;base;
<a name="l50"></a> tmpmr-&gt;size=m20-&gt;len;
<a name="l51"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cFD>Mem32DevIns</span><span class=cF7>(</span><span class=cF0>tmpmr</span><span class=cF7>)</span><span class=cF0>)
<a name="l52"></a> </span><span class=cF5>Free</span><span class=cF0>(tmpmr);
<a name="l53"></a> m20++;
<a name="l54"></a> }
<a name="l55"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l56"></a>}
<a name="l57"></a>
<a name="l58"></a></span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF5>Mem32DevAlloc</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> size,</span><span class=cF9>I64</span><span class=cF0> alignment)
<a name="l59"></a>{</span><span class=cF2>//Alloc 32-bit addr space for device. (Doesn't work.) Not used.</span><span class=cF0>
<a name="l60"></a></span><span class=cF2>//For this to work the BIOS E820 map must be searched for gaps in</span><span class=cF0>
<a name="l61"></a> </span><span class=cF2>//the 32-bit range and the pool initialized to the gaps.</span><span class=cF0>
<a name="l62"></a> </span><span class=cF1>U8</span><span class=cF0> *base,*limit;
<a name="l63"></a> </span><span class=cF9>CMemRange</span><span class=cF0> *tmpmr,*tmpmr1;
<a name="l64"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DEV_MEM</span><span class=cF0>],0</span><span class=cF7>)</span><span class=cF0>)
<a name="l65"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l66"></a> tmpmr1=</span><span class=cFB>dev</span><span class=cF0>.mem32_head.next;
<a name="l67"></a> </span><span class=cF1>while</span><span class=cF0> (tmpmr1!=&amp;</span><span class=cFB>dev</span><span class=cF0>.mem32_head) </span><span class=cF7>{</span><span class=cF0>
<a name="l68"></a> base=(tmpmr1-&gt;base+alignment-1)&amp;~(alignment-1);
<a name="l69"></a> limit=base+size-1;
<a name="l70"></a> </span><span class=cF1>if</span><span class=cF0> (!tmpmr1-&gt;type &amp;&amp;
<a name="l71"></a> limit&lt;tmpmr1-&gt;base+tmpmr1-&gt;size) {
<a name="l72"></a> tmpmr=</span><span class=cF5>AMAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CMemRange</span><span class=cF7>)</span><span class=cF0>);
<a name="l73"></a> tmpmr-&gt;type=</span><span class=cF3>MRT_DEV</span><span class=cF0>;
<a name="l74"></a> tmpmr-&gt;flags=0;
<a name="l75"></a> tmpmr-&gt;base=base;
<a name="l76"></a> tmpmr-&gt;size=size;
<a name="l77"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cFD>Mem32DevIns</span><span class=cF7>(</span><span class=cF0>tmpmr</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l78"></a> </span><span class=cF5>Free</span><span class=cF0>(tmpmr);
<a name="l79"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DEV_MEM</span><span class=cF0>],0);
<a name="l80"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l81"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l82"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DEV_MEM</span><span class=cF0>],0);
<a name="l83"></a> </span><span class=cF1>return</span><span class=cF0> tmpmr-&gt;base;
<a name="l84"></a> }
<a name="l85"></a> tmpmr1=tmpmr1-&gt;next;
<a name="l86"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l87"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DEV_MEM</span><span class=cF0>],0);
<a name="l88"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l89"></a>}
<a name="l90"></a>
<a name="l91"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>Mem32DevFree</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *base)
<a name="l92"></a>{</span><span class=cF2>//Free 32-bit device address space.</span><span class=cF0>
<a name="l93"></a> </span><span class=cF9>CMemRange</span><span class=cF0> *tmpmr;
<a name="l94"></a> </span><span class=cF1>if</span><span class=cF0> (!base) </span><span class=cF1>return</span><span class=cF0>;
<a name="l95"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DEV_MEM</span><span class=cF0>],0</span><span class=cF7>)</span><span class=cF0>)
<a name="l96"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l97"></a> tmpmr=</span><span class=cFB>dev</span><span class=cF0>.mem32_head.next;
<a name="l98"></a> </span><span class=cF1>while</span><span class=cF0> (tmpmr!=&amp;</span><span class=cFB>dev</span><span class=cF0>.mem32_head) </span><span class=cF7>{</span><span class=cF0>
<a name="l99"></a> </span><span class=cF1>if</span><span class=cF0> (tmpmr-&gt;base==base) {
<a name="l100"></a> tmpmr-&gt;type=</span><span class=cF3>MRT_UNUSED</span><span class=cF0>;
<a name="l101"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l102"></a> }
<a name="l103"></a> tmpmr=tmpmr-&gt;next;
<a name="l104"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l105"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DEV_MEM</span><span class=cF0>],0);
<a name="l106"></a>}
<a name="l107"></a>
<a name="l108"></a></span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF5>Mem64DevAlloc</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> *_pages1Gig)
<a name="l109"></a>{</span><span class=cF2>//Alloc 64-bit addr space for device.</span><span class=cF0>
<a name="l110"></a> </span><span class=cF1>U8</span><span class=cF0> *a;
<a name="l111"></a> </span><span class=cF9>I64</span><span class=cF0> i=*_pages1Gig,*pte;
<a name="l112"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DEV_MEM</span><span class=cF0>],0</span><span class=cF7>)</span><span class=cF0>)
<a name="l113"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l114"></a> </span><span class=cF1>while</span><span class=cF0> (i--) </span><span class=cF7>{</span><span class=cF0>
<a name="l115"></a> a=</span><span class=cFB>dev</span><span class=cF0>.mem64_ptr-=1&lt;&lt;30;
<a name="l116"></a> </span><span class=cF1>do</span><span class=cF0> {
<a name="l117"></a> pte=</span><span class=cF5>MemPageTable</span><span class=cF0>(a);
<a name="l118"></a> *pte=*pte&amp;~0x18 |0x11; </span><span class=cF2>//Uncached and present</span><span class=cF0>
<a name="l119"></a> </span><span class=cF5>InvlPg</span><span class=cF0>(</span><span class=cFB>dev</span><span class=cF0>.mem64_ptr);
<a name="l120"></a> a+=</span><span class=cFB>mem_page_size</span><span class=cF0>;
<a name="l121"></a> } </span><span class=cF1>while</span><span class=cF0> (a-</span><span class=cFB>dev</span><span class=cF0>.mem64_ptr&lt;1&lt;&lt;30);
<a name="l122"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l123"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DEV_MEM</span><span class=cF0>],0);
<a name="l124"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cFB>dev</span><span class=cF0>.mem64_ptr;
<a name="l125"></a>}
<a name="l126"></a>
<a name="l127"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>Mem64DevFree</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *base,</span><span class=cF9>I64</span><span class=cF0> pages1Gig)
<a name="l128"></a>{</span><span class=cF2>//Free 64-bit device address space.</span><span class=cF0>
<a name="l129"></a> </span><span class=cF1>if</span><span class=cF0> (!base) </span><span class=cF1>return</span><span class=cF0>;
<a name="l130"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DEV_MEM</span><span class=cF0>],0</span><span class=cF7>)</span><span class=cF0>)
<a name="l131"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l132"></a> </span><span class=cF1>if</span><span class=cF0> (base==</span><span class=cFB>dev</span><span class=cF0>.mem64_ptr)
<a name="l133"></a> </span><span class=cFB>dev</span><span class=cF0>.mem64_ptr+=pages1Gig*1&lt;&lt;30;
<a name="l134"></a></span><span class=cF2>//else not freed</span><span class=cF0>
<a name="l135"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DEV_MEM</span><span class=cF0>],0);
<a name="l136"></a>}
<a name="l137"></a>
<a name="l138"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>UncachedAliasAlloc</span><span class=cF0>() </span><span class=cF2>//Make uncached alias for 4 lowest Gig.</span><span class=cF0>
<a name="l139"></a>{
<a name="l140"></a> </span><span class=cF9>I64</span><span class=cF0> i=4,*pte;
<a name="l141"></a> </span><span class=cF1>U8</span><span class=cF0> *a;
<a name="l142"></a> a=</span><span class=cFB>dev</span><span class=cF0>.uncached_alias=</span><span class=cF5>Mem64DevAlloc</span><span class=cF0>(&amp;i);
<a name="l143"></a> </span><span class=cF1>do</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l144"></a> pte=</span><span class=cF5>MemPageTable</span><span class=cF0>(a);
<a name="l145"></a> *pte=0x197+a-</span><span class=cFB>dev</span><span class=cF0>.uncached_alias;
<a name="l146"></a> </span><span class=cF5>InvlPg</span><span class=cF0>(a);
<a name="l147"></a> a+=</span><span class=cFB>mem_page_size</span><span class=cF0>;
<a name="l148"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>while</span><span class=cF0> (a-</span><span class=cFB>dev</span><span class=cF0>.uncached_alias&lt;1&lt;&lt;32);
<a name="l149"></a>}
<a name="l150"></a>
<a name="l151"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cFD>BIOSTotalMem</span><span class=cF0>()
<a name="l152"></a>{
<a name="l153"></a> </span><span class=cF9>I64</span><span class=cF0> r01,r20;
<a name="l154"></a> </span><span class=cF9>U16</span><span class=cF0> *m01=</span><span class=cFD>MEM_E801</span><span class=cF0>;
<a name="l155"></a> </span><span class=cF9>CMemE820</span><span class=cF0> *m20=</span><span class=cFD>MEM_E820</span><span class=cF0>;
<a name="l156"></a>
<a name="l157"></a> r01=0x100000+m01[0]&lt;&lt;10+m01[1]&lt;&lt;16;
<a name="l158"></a> r20=0;
<a name="l159"></a> </span><span class=cF1>if</span><span class=cF0> (m20-&gt;type) </span><span class=cF7>{</span><span class=cF0>
<a name="l160"></a> </span><span class=cF1>while</span><span class=cF0> (m20-&gt;type) {
<a name="l161"></a> </span><span class=cF1>if</span><span class=cF0> (m20-&gt;type==1)
<a name="l162"></a> r20+=m20-&gt;len;
<a name="l163"></a> m20++;
<a name="l164"></a> }
<a name="l165"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l166"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF5>MaxI64</span><span class=cF0>(r01,r20);
<a name="l167"></a>}
<a name="l168"></a>
<a name="l169"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>Scale2Mem</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> min,</span><span class=cF9>I64</span><span class=cF0> max,</span><span class=cF9>I64</span><span class=cF0> limit=2*1024*1024*1024)
<a name="l170"></a>{</span><span class=cF2>//Helps pick DiskCache and RAMDisk sizes.</span><span class=cF0>
<a name="l171"></a></span><span class=cF2>//Can be used in </span><a href="/Wb/Adam/Opt/Boot/BootHDIns.HC.HTML#l18"><span class=cF4>BootHDIns</span></a><span class=cF2>() config scripts.</span><span class=cF0>
<a name="l172"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l173"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>sys_data_bp</span><span class=cF0>)
<a name="l174"></a> i=</span><span class=cFB>sys_data_bp</span><span class=cF0>-&gt;alloced_u8s;
<a name="l175"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l176"></a> i=</span><span class=cFB>sys_code_bp</span><span class=cF0>-&gt;alloced_u8s;
<a name="l177"></a> </span><span class=cF1>if</span><span class=cF0> (i&gt;=limit)
<a name="l178"></a> </span><span class=cF1>return</span><span class=cF0> max;
<a name="l179"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l180"></a> </span><span class=cF1>return</span><span class=cF0> min+(max-min)*i/limit;
<a name="l181"></a>}
</span></pre></body>
</html>