Эх сурвалжийг харах

docs: add doc about memory management and multi-arch support

shao 3 сар өмнө
parent
commit
833e7f9c75

+ 4 - 4
README.md

@@ -23,17 +23,17 @@
 
 项目经测试可使用 *busybox* 中大多数功能,并且该内核使用 *busybox* 搭配 init 脚本启动,运行 *busybox* 提供的 *ash* 来执行 shell 命令。
 
-![测试](doc/image-0.png)
-![test pipes](doc/image-1.png)
+![测试](doc/images/image-0.png)
+![test pipes](doc/images/image-1.png)
 
 ## 初赛文档
 
 - [启动加载](https://gitlab.eduxiji.net/T202410247994622/project2608132-275906/-/blob/master/doc/boot.md)
-- [内存管理]()
+- [内存管理](https://gitlab.eduxiji.net/T202410247994622/project2608132-275906/-/blob/master/doc/memory.md)
 - [进程管理](https://gitlab.eduxiji.net/T202410247994622/project2608132-275906/-/blob/master/doc/task.md)
 - [文件系统](https://gitlab.eduxiji.net/T202410247994622/project2608132-275906/-/blob/master/doc/filesystem.md)
 - [设备驱动](https://gitlab.eduxiji.net/T202410247994622/project2608132-275906/-/blob/master/doc/device_driver.md)
-- [多平台支持]()
+- [多平台支持](https://gitlab.eduxiji.net/T202410247994622/project2608132-275906/-/blob/master/doc/multi_arch.md)
 
 ## 代码树结构
 

+ 0 - 0
doc/image-0.png → doc/images/image-0.png


+ 0 - 0
doc/image-1.png → doc/images/image-1.png


+ 260 - 0
doc/images/mm.drawio

@@ -0,0 +1,260 @@
+<mxfile host="65bd71144e">
+    <diagram id="4VEP11HeOnmaw6osTsnV" name="第 1 页">
+        <mxGraphModel dx="929" dy="1067" grid="0" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="0" pageScale="1" pageWidth="827" pageHeight="1169" background="#FFF6E8" math="0" shadow="0">
+            <root>
+                <mxCell id="0"/>
+                <mxCell id="1" parent="0"/>
+                <mxCell id="4" style="edgeStyle=none;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" parent="1" source="2" edge="1">
+                    <mxGeometry relative="1" as="geometry">
+                        <mxPoint x="132.18" y="83.66668701171875" as="targetPoint"/>
+                    </mxGeometry>
+                </mxCell>
+                <mxCell id="2" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#666666;fontColor=#333333;" parent="1" vertex="1">
+                    <mxGeometry x="76.18" y="-61" width="111" height="364" as="geometry"/>
+                </mxCell>
+                <mxCell id="3" value="&lt;font color=&quot;#252323&quot;&gt;&lt;b&gt;代码段&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;" parent="1" vertex="1">
+                    <mxGeometry x="76.18" y="252" width="111" height="34" as="geometry"/>
+                </mxCell>
+                <mxCell id="5" value="&lt;font color=&quot;#252323&quot;&gt;&lt;b&gt;数据段&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" parent="1" vertex="1">
+                    <mxGeometry x="76.18" y="214" width="111" height="34" as="geometry"/>
+                </mxCell>
+                <mxCell id="6" value="&lt;font color=&quot;#252323&quot;&gt;&lt;b&gt;BSS段&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;" parent="1" vertex="1">
+                    <mxGeometry x="76.18" y="187" width="111" height="23" as="geometry"/>
+                </mxCell>
+                <mxCell id="7" value="&lt;font color=&quot;#252323&quot;&gt;&lt;b&gt;用户堆&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;" parent="1" vertex="1">
+                    <mxGeometry x="76.18" y="138" width="111" height="45" as="geometry"/>
+                </mxCell>
+                <mxCell id="8" value="&lt;font color=&quot;#252323&quot;&gt;&lt;b&gt;用户栈&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#bac8d3;strokeColor=#23445d;" parent="1" vertex="1">
+                    <mxGeometry x="76.18" y="-59" width="111" height="93" as="geometry"/>
+                </mxCell>
+                <mxCell id="9" value="&lt;font color=&quot;#252323&quot;&gt;&lt;b&gt;MMAP区&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#b0e3e6;strokeColor=#0e8088;" parent="1" vertex="1">
+                    <mxGeometry x="76.18" y="67" width="111" height="45" as="geometry"/>
+                </mxCell>
+                <mxCell id="18" style="edgeStyle=none;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" parent="1" source="19" edge="1">
+                    <mxGeometry relative="1" as="geometry">
+                        <mxPoint x="461.05" y="80.66668701171875" as="targetPoint"/>
+                    </mxGeometry>
+                </mxCell>
+                <mxCell id="19" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#666666;fontColor=#333333;" parent="1" vertex="1">
+                    <mxGeometry x="405.05" y="-64" width="111" height="364" as="geometry"/>
+                </mxCell>
+                <mxCell id="31" value="&lt;b&gt;&lt;font style=&quot;font-size: 16px;&quot;&gt;进程虚拟内存空间&lt;/font&gt;&lt;/b&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="66.68" y="328" width="135.5" height="30" as="geometry"/>
+                </mxCell>
+                <mxCell id="34" value="&lt;b style=&quot;color: rgb(240, 240, 240); font-size: 12px;&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;0x0000 7FFF FFFF F000&lt;/font&gt;&lt;/b&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Helvetica;fontSize=16;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="197.18" y="-78" width="145" height="30" as="geometry"/>
+                </mxCell>
+                <mxCell id="35" value="&lt;b style=&quot;color: rgb(51, 51, 51); font-size: 12px;&quot;&gt;0x0000 0000 0000 0000&lt;/b&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Helvetica;fontSize=16;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="197.18" y="286" width="140" height="30" as="geometry"/>
+                </mxCell>
+                <mxCell id="36" value="&lt;b&gt;&lt;font style=&quot;font-size: 16px;&quot;&gt;内核虚拟内存空间&lt;/font&gt;&lt;/b&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="398.18" y="325" width="135.5" height="30" as="geometry"/>
+                </mxCell>
+                <mxCell id="37" value="&lt;b style=&quot;color: rgb(51, 51, 51); font-size: 12px;&quot;&gt;0xFFFF 8000 0000 0000&lt;/b&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Helvetica;fontSize=16;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="536.05" y="283" width="140" height="30" as="geometry"/>
+                </mxCell>
+                <mxCell id="38" value="&lt;b style=&quot;color: rgb(51, 51, 51); font-size: 12px;&quot;&gt;0xFFFF FFFF FFFF FFFF&lt;/b&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Helvetica;fontSize=16;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="532.68" y="-78" width="146.37" height="30" as="geometry"/>
+                </mxCell>
+                <mxCell id="39" value="&lt;font color=&quot;#252323&quot;&gt;&lt;b&gt;直接映射区&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#bac8d3;strokeColor=#23445d;" vertex="1" parent="1">
+                    <mxGeometry x="405.05" y="201" width="111" height="61" as="geometry"/>
+                </mxCell>
+                <mxCell id="40" value="&lt;b style=&quot;color: rgb(51, 51, 51); font-size: 12px;&quot;&gt;0xFFFF FF00 0000 0000&lt;/b&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Helvetica;fontSize=16;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="537.05" y="245" width="140" height="30" as="geometry"/>
+                </mxCell>
+                <mxCell id="41" value="&lt;b&gt;&lt;font style=&quot;font-size: 12px;&quot;&gt;0xFFFF FF80 0000 0000&lt;/font&gt;&lt;/b&gt;" style="text;whiteSpace=wrap;html=1;fontSize=16;fontFamily=Helvetica;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="536.05" y="185" width="141.5" height="32" as="geometry"/>
+                </mxCell>
+                <mxCell id="43" value="&lt;font color=&quot;#252323&quot;&gt;&lt;b&gt;内核堆&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
+                    <mxGeometry x="405.05" y="45" width="111" height="45" as="geometry"/>
+                </mxCell>
+                <mxCell id="45" value="&lt;font color=&quot;#252323&quot;&gt;&lt;b&gt;Kernel Image&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
+                    <mxGeometry x="405.05" y="-32" width="111" height="51" as="geometry"/>
+                </mxCell>
+                <mxCell id="46" value="&lt;font color=&quot;#252323&quot;&gt;&lt;b&gt;Memory Map&lt;br&gt;(Page Array)区&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;gradientColor=#97d077;" vertex="1" parent="1">
+                    <mxGeometry x="405.05" y="123" width="111" height="45" as="geometry"/>
+                </mxCell>
+                <mxCell id="47" value="&lt;b&gt;0xFFFF FF81 8000 0000&lt;/b&gt;" style="text;whiteSpace=wrap;html=1;fontSize=12;fontFamily=Helvetica;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="534.68" y="76" width="140" height="23" as="geometry"/>
+                </mxCell>
+                <mxCell id="48" value="&lt;b&gt;0xFFFF&amp;nbsp;FFFF FFC0 0000&lt;/b&gt;" style="text;whiteSpace=wrap;html=1;fontSize=12;fontFamily=Helvetica;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="533.68" y="2" width="149.37" height="25" as="geometry"/>
+                </mxCell>
+                <mxCell id="49" value="&lt;b&gt;0xFFFF FFFF FFE0 0000&lt;/b&gt;" style="text;whiteSpace=wrap;html=1;fontSize=12;fontFamily=Helvetica;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="533.68" y="-47" width="145.2" height="25" as="geometry"/>
+                </mxCell>
+                <mxCell id="50" value="&lt;b&gt;0xFFFF FFC0 0000 0000&lt;/b&gt;" style="text;whiteSpace=wrap;html=1;fontSize=12;fontFamily=Helvetica;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="533.68" y="34" width="139" height="25.5" as="geometry"/>
+                </mxCell>
+                <mxCell id="51" value="&lt;b&gt;0xFFFF FF80 4000 0000&lt;/b&gt;" style="text;whiteSpace=wrap;html=1;fontSize=12;fontFamily=Helvetica;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="535.68" y="155" width="140" height="25" as="geometry"/>
+                </mxCell>
+                <mxCell id="52" value="&lt;b&gt;0xFFFF FF81 4000 0000&lt;/b&gt;" style="text;whiteSpace=wrap;html=1;fontSize=12;fontFamily=Helvetica;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="534.68" y="116" width="140" height="27" as="geometry"/>
+                </mxCell>
+                <mxCell id="53" value="" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
+                    <mxGeometry x="70.68" y="-355" width="131" height="199" as="geometry"/>
+                </mxCell>
+                <mxCell id="54" value="&lt;b&gt;Zone&lt;/b&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="108.68" y="-389" width="60" height="30" as="geometry"/>
+                </mxCell>
+                <mxCell id="55" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
+                    <mxGeometry x="146.55" y="-319" width="41.13" height="23" as="geometry"/>
+                </mxCell>
+                <mxCell id="97" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="66" target="83">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="66" value="&lt;b&gt;&lt;font color=&quot;#000000&quot;&gt;1&lt;/font&gt;&lt;/b&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
+                    <mxGeometry x="146.55" y="-296" width="41.13" height="23" as="geometry"/>
+                </mxCell>
+                <mxCell id="108" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="69" target="85">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="69" value="&lt;b&gt;&lt;font color=&quot;#000000&quot;&gt;2&lt;/font&gt;&lt;/b&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
+                    <mxGeometry x="146.55" y="-273" width="41.13" height="23" as="geometry"/>
+                </mxCell>
+                <mxCell id="126" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="76" target="124">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="76" value="&lt;b&gt;&lt;font color=&quot;#000000&quot;&gt;10&lt;/font&gt;&lt;/b&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
+                    <mxGeometry x="146.55" y="-189" width="41.13" height="23" as="geometry"/>
+                </mxCell>
+                <mxCell id="77" value="&lt;b&gt;FreeArea&lt;br&gt;[MAX_PAGE_ORDER]&lt;/b&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" vertex="1" parent="1">
+                    <mxGeometry x="66.68" y="-351" width="141" height="30" as="geometry"/>
+                </mxCell>
+                <mxCell id="90" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;fontFamily=Helvetica;fontSize=12;fontColor=#000000;strokeColor=#000000;" edge="1" parent="1" source="79" target="55">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="92" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="79" target="91">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="79" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="219.68" y="-318.5" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="82" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;......&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
+                    <mxGeometry x="146.55" y="-250" width="41.13" height="62" as="geometry"/>
+                </mxCell>
+                <mxCell id="98" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="83" target="66">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="83" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="219.68" y="-294" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="101" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="84" target="99">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="84" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="251.68" y="-294" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="109" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="85" target="69">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="85" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="219.68" y="-270" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="86" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="251.68" y="-270" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="87" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="283.68" y="-270" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="114" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="88" target="110">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="88" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="315.68" y="-270" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="89" value="" style="endArrow=classic;html=1;fontFamily=Helvetica;fontSize=12;fontColor=#000000;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;strokeColor=#000000;" edge="1" parent="1" source="55" target="79">
+                    <mxGeometry width="50" height="50" relative="1" as="geometry">
+                        <mxPoint x="335.68" y="-121" as="sourcePoint"/>
+                        <mxPoint x="385.68" y="-171" as="targetPoint"/>
+                    </mxGeometry>
+                </mxCell>
+                <mxCell id="93" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="91" target="79">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="95" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="91" target="94">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="91" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="278.68" y="-318.5" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="96" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="94" target="91">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="94" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="337.68" y="-318.5" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="102" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="99" target="84">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="99" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="303.55" y="-294" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="105" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="100" target="103">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="100" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="335.55" y="-294" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="106" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="103">
+                    <mxGeometry relative="1" as="geometry">
+                        <mxPoint x="370.0133740234375" y="-279" as="targetPoint"/>
+                    </mxGeometry>
+                </mxCell>
+                <mxCell id="103" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="388.68" y="-294" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="122" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="104">
+                    <mxGeometry relative="1" as="geometry">
+                        <mxPoint x="475.34668701171876" y="-289" as="targetPoint"/>
+                    </mxGeometry>
+                </mxCell>
+                <mxCell id="104" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="420.68" y="-294" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="115" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="110" target="88">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="110" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="372.68" y="-270" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="111" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="404.68" y="-270" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="112" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="436.68" y="-270" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="113" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="468.68" y="-270" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="123" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="120" target="104">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="120" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="475.68" y="-294" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="121" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="507.68" y="-294" width="32" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="127" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="124" target="76">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="129" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="124" target="128">
+                    <mxGeometry relative="1" as="geometry"/>
+                </mxCell>
+                <mxCell id="124" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;1024个Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="215.68" y="-189" width="228" height="20" as="geometry"/>
+                </mxCell>
+                <mxCell id="130" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;strokeColor=#000000;fontFamily=Helvetica;fontSize=12;fontColor=#000000;" edge="1" parent="1" source="128">
+                    <mxGeometry relative="1" as="geometry">
+                        <mxPoint x="446.0133740234375" y="-176" as="targetPoint"/>
+                    </mxGeometry>
+                </mxCell>
+                <mxCell id="128" value="&lt;font color=&quot;#000000&quot;&gt;&lt;b&gt;1024个Page&lt;/b&gt;&lt;/font&gt;" style="rounded=0;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=12;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
+                    <mxGeometry x="471.68" y="-191" width="228" height="20" as="geometry"/>
+                </mxCell>
+            </root>
+        </mxGraphModel>
+    </diagram>
+</mxfile>

BIN
doc/images/mm_layout.png


BIN
doc/images/mm_zone.png


+ 97 - 0
doc/memory.md

@@ -0,0 +1,97 @@
+# 内存管理
+
+## 物理内存组织
+ROS 采用 **FLATMEM 平坦内存模型**以及 **UMA 架构**,把可用于分配的物理内存统一纳入 Zone 进行管理。Zone 所管理的物理内存页会被进一步划分成不同阶数的 FreeArea。其中,每一个 FreeArea 均由大小为 2^i^ 的物理内存块所构成,这些物理块作为内存分配与管理的基础单元,在整个物理内存的组织架构中起着关键作用,用于实现高效的内存管理操作。
+![物理内存组织](images/mm_zone.png)
+
+## 物理内存分配
+物理内存分配可根据所需内存大小分为**页分配**和**字节块分配**,整个物理内存分配由**伙伴系统**和**slab分配器**合作完成。
+
+### 页分配
+ROS 使用伙伴系统管理内存页的分配,其工作原理是将内存块按照 2 的幂次方大小分为多个层级,并通过合并和分解有效减少外部碎片。**分配物理页时**,伙伴系统从最小适合该请求的层级开始查找。通过检查和计算伙伴块的地址,可以快速地进行内存分割与合并。当需要分配的内存块小于当前可用最小块时,将当前块一分为二,直到找到合适大小的块为止。**回收物理页时**,若其伙伴块也空闲,则将两个块合并为一个更大的块,迭代进行直到不能再合并为止。伙伴分配器的优点在于分配和释放内存块的操作非常快速,且通过内存块大小的选择和合并操作,有效减少了外部碎片。
+
+### 字节块分配
+ROS 采用 slab 分配器来实现字节块分配。slab 分配器针对不同大小的常用内存对象预先分配一系列内存块,这些内存块组织成**缓存池**。当有特定字节大小的内存块分配请求时,首先在对应的 slab 缓存池中查找空闲内存块。若找到,则直接分配;若未找到,slab 分配器依据既定策略向伙伴系统申请一定数量页面,将这些页面划分成所需大小的字节块并加入对应的 slab 缓存池,然后从中取出字节块分配。slab 分配器的优势显著,尤其在频繁申请和释放相同大小字节块的场景下,它能极大减少内存碎片的产生,大幅提升内存分配与释放效率。这对于内核中诸如进程控制块、文件描述符等众多小内存对象频繁的分配回收需求极为关键,有力保障了系统整体稳定高效运行。
+
+## 地址空间布局
+ROS 地址空间布局借鉴了 Linux 的设计,采用**用户空间和内核空间统一布局**方式。
+![地址空间](images/mm_layout.png)
+**Kernel Image**
+Kernel Image 位于内核虚拟内存空间的最上方,其中包含了内核的可执行代码和内核数据结构,是内核运行的核心部分。
+**Memory Map 区**
+Memory Map 区用于存储描述物理页的数据结构 PageArray,此结构对于物理内存管理至关重要。
+**直接映射区**
+直接映射区将物理内存直接映射到内核虚拟地址空间,使得内核可以方便地访问物理内存。例如,物理地址加上固定的一个偏移量 PHYS_OFFSET 即可得到其在内核中的虚拟地址,
+
+## 缺页异常处理
+ROS 能够有效利用缺页异常处理来实现**写时复制(Copy on write)**、**懒分配(Lazy page allocation)** 以及**高效用户态内存拷贝**技术。
+**写时复制**
+在PageFault处理中,内核会根据COW标志位该页是否进行写时复制,并根据 Page 结构体中的原子引用计数判断是否为最后一个持有者,如果不是最后一个持有者,会新分配一个页并复制原始页的数据并恢复写标志位重新映射,如果是最后一个持有者,直接恢复读写标志位。
+``` rust
+if attributes & PA_COW as usize != 0 {
+            attributes &= !PA_COW as usize;
+            if area.permission.write {
+                attributes |= PA_RW as usize;
+            } else {
+                attributes &= !PA_RW as usize;
+            }
+
+            let page = unsafe { Page::take_pfn(pfn, 0) };
+            if page.load_refcount() == 1 {
+                // SAFETY: This is actually safe. If we read `1` here and we have `MMList` lock
+                // held, there couldn't be neither other processes sharing the page, nor other
+                // threads making the page COW at the same time.
+                pte.set_attributes(attributes);
+                core::mem::forget(page);
+                return Ok(());
+            }
+
+            let new_page = Page::alloc_one();
+            if attributes & PA_ANON as usize != 0 {
+                new_page.zero();
+            } else {
+                new_page
+                    .as_cached()
+                    .as_mut_slice::<u8>(0x1000)
+                    .copy_from_slice(CachedPP::new(pfn).as_slice(0x1000));
+            }
+
+            attributes &= !(PA_A | PA_ANON) as usize;
+
+            pfn = new_page.into_pfn();
+            pte.set(pfn, attributes);
+        }
+```
+
+**懒分配**
+懒分配技术主要用于堆栈分配以及 mmap 匿名映射或文件映射。在传统的内存分配方法中,操作系统在进程请求内存时会立即为其分配实际的物理内存。然而,这种方法在某些情况下可能导致资源的浪费,因为进程可能并不会立即使用全部分配的内存。
+懒分配技术的核心思想是推迟实际物理内存的分配,直到进程真正访问到该内存区域。这样可以优化内存使用,提高系统性能。对于内存的懒分配,比如堆栈分配,mmap 匿名内存分配,ROS 将许可分配的范围记录下来,但并不进行实际分配操作,当用户访问到许诺分配但未分配的页面时会触发缺页异常,缺页异常处理函数会进行实际的分配操作。
+
+**高效用户态内存拷贝**
+在系统调用过程中,内核需要频繁与用户态指针指向的数据进行交互,例如 read、write 系统调用。在 ROS 中,用户和内核共享地址空间,因此在访问用户态的内存时不需要同 xv6/rcore 那样通过软件查询页表,而是可以直接解引用用户态指针。
+然而,用户态指针(例如,read 系统调用参数中指向的用户态buf的指针)并不总是有效的,有可能指向非法内存,在这种情况下,仅通过类似 memcpy 简单拷贝操作可能导致内核的panic。OS 内核的用户态内存拷贝参考了 Linux copy_from_user 的实现思路,利用 PageFault 实现内核访问用户态内存的高效拷贝和错误处理。具体实现为当内核访问用户态指针出现 PageFault,会在 PageFault 处理函数中调用 try_page_fault_fix,此函数会修改进程中断上下文的 pc 值,使 PageFault 处理完毕后跳转到拷贝结束处,并可以根据拷贝字节数判断拷贝过程中是否出现异常状况。该处理方法在用户指针正常时速度极快,同时还能完全复用用户缺页异常处理的代码来处理用户指针懒加载/写时复制的情况。并且为了进一步方便内核使用用户态指针,ROS 定义了 UserBuffer、UserString、UserPointer 结构体。
+``` rust
+/// Try to fix the page fault by jumping to the `error` address.
+///
+/// Panic if we can't find the `ip` in the fix list.
+fn try_page_fault_fix(int_stack: &mut InterruptContext, addr: VAddr) {
+    let ip = int_stack.rip as u64;
+
+    // SAFETY: `FIX_START` and `FIX_END` are defined in the linker script in `.rodata` section.
+    let entries = unsafe {
+        core::slice::from_raw_parts(
+            FIX_START,
+            (FIX_END as usize - FIX_START as usize) / size_of::<FixEntry>(),
+        )
+    };
+
+    for entry in entries.iter() {
+        if ip >= entry.start && ip < entry.start + entry.length {
+            int_stack.rip = entry.jump_address as u64;
+            return;
+        }
+    }
+
+    kernel_page_fault_die(addr, ip as usize)
+}
+```

+ 90 - 0
doc/multi_arch.md

@@ -0,0 +1,90 @@
+# 多架构支持
+ROS 目前仅支持x86-64架构,但为了后续多架构的支持,在 ROS 开发过程中尽可能的将架构相关的操作进行抽象,并充分利用 rust 包管理机制,将架构相关代码统一置于 arch crate 中,便于后续多架构扩展。ROS 通过 cfg-if 实现不同架构的条件编译。
+``` rust
+cfg_if::cfg_if! {
+    if #[cfg(target_arch = "x86_64")] {
+        mod x86_64;
+        pub use self::x86_64::*;
+    } else if #[cfg(target_arch = "riscv64")] {
+        mod riscv64;
+        pub use self::riscv64::*;
+    } else if #[cfg(target_arch = "aarch64")]{
+        mod aarch64;
+        pub use self::aarch64::*;
+    }
+}
+```
+## 进程相关抽象
+### 进程上下文抽象
+进程上下文是内核调度的关键结构,但由于不同的体系结构在进程上下文的保存上存在差异,例如,x86-64 只能将上下文存储在内核栈上,而 riscv64 可以定义相关结构体存储上下文内容,所以我们将进程上下文及其相关操作,如上下文初始化,上下文切换等统一进行抽象,这样内核处理上下文时只需要 TaskContext 提供的统一接口。
+``` rust
+// x86-64
+pub struct TaskContext {
+    /// The kernel stack pointer
+    pub rsp: u64,
+    // Extended states, i.e., FP/SIMD states to do!
+}
+
+impl TaskContext {
+    pub const fn new() -> Self {
+        Self { rsp: 0 }
+    }
+
+    pub fn init(&mut self, entry: usize, kstack_top: usize) {
+        unsafe {
+            let frame_ptr = (kstack_top as *mut ContextSwitchFrame).sub(1);
+            core::ptr::write(
+                frame_ptr,
+                ContextSwitchFrame {
+                    rip: entry as u64,
+                    eflags: 0x200,
+                    ..Default::default()
+                },
+            );
+            self.rsp = frame_ptr as u64;
+        }
+    }
+
+    #[inline(always)]
+    pub fn switch_to(&mut self, next_task: &mut Self) {
+        unsafe { _switch_to(&mut self.rsp, &mut next_task.rsp) }
+    }
+}
+```
+## 内存相关抽象
+页表根目录的修改和获取,tlb的刷新等都是内核必须的内存操作,提供有关抽象很有必要。
+
+## IO相关抽象
+### 中断上下文抽象
+中断上下文需要保存中断前CPU所有信息,为了多架构的统一,将中断上下文抽象为InterruptContext,这样内核再进行中断相关操作时无需考虑架构上的差异。
+``` rust
+// x86-64
+pub struct InterruptContext {
+    pub rax: u64,
+    pub rbx: u64,
+    pub rcx: u64,
+    pub rdx: u64,
+    pub rdi: u64,
+    pub rsi: u64,
+    pub r8: u64,
+    pub r9: u64,
+    pub r10: u64,
+    pub r11: u64,
+    pub r12: u64,
+    pub r13: u64,
+    pub r14: u64,
+    pub r15: u64,
+    pub rbp: u64,
+
+    pub int_no: u64,
+    pub error_code: u64,
+
+    // Pushed by CPU
+    pub rip: u64,
+    pub cs: u64,
+    pub eflags: u64,
+    pub rsp: u64,
+    pub ss: u64,
+}
+```
+## 杂项