<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>yu419</title><link>https://yuqu419.github.io/</link><description>yu419</description><generator>Hugo 0.169.0 &amp; FixIt v1.0.0-rc.1</generator><language>zh-CN</language><lastBuildDate>Thu, 26 Nov 2026 21:14:13 +0800</lastBuildDate><atom:link href="https://yuqu419.github.io/index.xml" rel="self" type="application/rss+xml"/><item><title>算法学习 贪心算法</title><link>https://yuqu419.github.io/posts/algorithm-greedy/</link><pubDate>Thu, 26 Nov 2026 21:14:13 +0800</pubDate><guid>https://yuqu419.github.io/posts/algorithm-greedy/</guid><category domain="https://yuqu419.github.io/categories/%E5%AD%A6%E4%B9%A0/">学习</category><description>&lt;div style="max-width: 620px; margin: 56px auto; font-family: 'Noto Serif SC','Songti SC',Georgia,serif; color: light-dark(#2b2b2b, #d1d5db); line-height: 2;"&gt;&#10;&lt;p style="font-size: 11px; letter-spacing: .5em; text-transform: uppercase; color: light-dark(#888, #9aa3b2); margin: 0 0 24px;"&gt;Preface&lt;/p&gt;&#10;&lt;div style="width: 36px; height: 1px; background: light-dark(#111, #e1e6ed); margin-bottom: 34px;"&gt;&lt;/div&gt;&#10;&lt;p style="font-size: 15px; text-align: justify; text-justify: inter-ideograph; margin: 0 0 18px;"&gt;&#10;&lt;p&gt;贪心是一种智慧&lt;/p&gt;&#10;&lt;/p&gt;&#10;&lt;p style="font-size: 13px; color: light-dark(#888, #9aa3b2); letter-spacing: .1em; text-align: right; margin: 40px 0 0;"&gt;— 2026/11/26&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;h2 class="heading-element" id="贪心算法"&gt;&lt;span&gt;贪心算法&lt;/span&gt;&#10; &lt;a href="#%e8%b4%aa%e5%bf%83%e7%ae%97%e6%b3%95" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;贪心算法的总体思想是每步局部最优，得到整体最优。&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;注意：局部最优未必整体最优，需要严格的数学证明，推荐阅读&lt;a href="https://www.luogu.me/article/47d78t3x#%E8%B4%AA%E5%BF%83%E7%AE%80%E4%BB%8B" target="_blank" rel="external nofollow noopener noreferrer"&gt;贪心&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h2 class="heading-element" id="相邻交换"&gt;&lt;span&gt;相邻交换&lt;/span&gt;&#10; &lt;a href="#%e7%9b%b8%e9%82%bb%e4%ba%a4%e6%8d%a2" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;相邻交换（&lt;strong&gt;Exchange Argument&lt;/strong&gt;）是一种贪心策略，一般在排队问题中使用&lt;/p&gt;&#10;&lt;p&gt;当考虑一个队伍如何排列时，可以先考虑局部，考虑相邻的两个元素交换后是否影响最终结果，当相邻两个元素取到最优时，整体就取到最优解。&lt;/p&gt;&#10;&lt;p&gt;例：&lt;/p&gt;&#10;&lt;p&gt;有$ n $个人排队接水，第$ i $ 个人接水需要 $ T_i $ 分钟。怎么排队，能让所有人的平均等待时间最短？&lt;/p&gt;&#10;&lt;p&gt;我们假设 A，B 两人相邻，A 接水时长为 $T_A$，B 排队接水为 $T_B$，在前面的人的排队时长为 $T_S$。那么，当 A 在 B 之前时，A、B 排队时长之和为 &lt;/p&gt;&#10;$$T_1 = T_S &amp;#43; T_S &amp;#43; T_A = 2 T_S &amp;#43; T_A$$&lt;p&gt; 同理，当 B 在 A 之前时，排队时长之和为 &lt;/p&gt;&#10;$$T_1 = T_S &amp;#43; T_S &amp;#43; T_B = 2 T_S &amp;#43; T_B$$&lt;p&gt; 假设 A 拍在 B 之前为最优解，那么 &lt;/p&gt;&#10;$$ T_1 &amp;lt; T_2 $$&lt;p&gt; 则 &lt;/p&gt;&#10;$$T_A &amp;lt; T_B$$&lt;p&gt; 所以，应该让接水时间短的人排在前面，这样就找到了最优解。&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;练习：&lt;a href="https://www.luogu.com.cn/problem/P1080" target="_blank" rel="external nofollow noopener noreferrer"&gt;P1080 国王游戏&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h2 class="heading-element" id="区间调度"&gt;&lt;span&gt;区间调度&lt;/span&gt;&#10; &lt;a href="#%e5%8c%ba%e9%97%b4%e8%b0%83%e5%ba%a6" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;给定若干区间（任务），每个区间有开始时间与结束时间，目标是选择&lt;strong&gt;最多数量的互不重叠区间&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;论证思路：&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;选择最早结束的区间能最大限度为后续区间留除空间&lt;/li&gt;&#10;&lt;li&gt;若最优解未选择该区间，也可将其替换为结束更早的区间而不减少可选数量&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;这里运用了交换论证的思想&lt;/p&gt;&#10;&lt;div style="padding:12px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-left:4px solid #1d6fd0;border-radius:10px;background:rgba(29,111,208,.05);"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:700;color:#1d6fd0;margin-bottom:6px;"&gt;ℹ 交换论证&lt;/div&gt;&lt;div style="font-size:14px;line-height:1.7;"&gt;&#10;&lt;p&gt;所谓交换论证，就是将贪心解与最优解的某一个步骤进行交换，证明交换后的最优解仍然成立。那么贪心解也是最优解&lt;/p&gt;&#10;&lt;div&gt;&#10;&lt;p&gt;比如有一个排队问题，我们得到贪心解 $G(g_1,g_2,...,g_n)$，最优解为$ O（o_1,o_2,..,o_n）$&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;将最优解的第一步替换，得到$ O&amp;#39;（g_1,o_2,..,o_n）$,证明替换后的$O&amp;#39;$不弱于$O$。如果证明每一步替换都不弱于$O$，则贪心解为最优解&lt;/p&gt;&#10;&lt;/div&gt;&lt;/div&gt;&#10;&lt;p&gt;如果区间带有权重，贪心策略会失效，需使用动态规划&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="johnson法则"&gt;&lt;span&gt;Johnson法则&lt;/span&gt;&#10; &lt;a href="#johnson%e6%b3%95%e5%88%99" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Johnson法则&lt;/strong&gt;用于解决两台机器流水作业问题&lt;/p&gt;&#10;&lt;p&gt;适用限制：适用于两台机器，且每个作业都必须先M1，后M2&lt;/p&gt;&#10;&lt;p&gt;结论：单个作业如果M1时间短于M2，M1上时间短的往前排，如果M1时间长于M2，M2上时间长的往前排&lt;/p&gt;&#10;&lt;h1 class="heading-element" id="例题"&gt;&lt;span&gt;例题&lt;/span&gt;&#10; &lt;a href="#%e4%be%8b%e9%a2%98" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;h2 class="heading-element" id="p1842-奶牛玩杂技"&gt;&lt;span&gt;P1842 &lt;a href="https://www.luogu.com.cn/problem/P1842" target="_blank" rel="external nofollow noopener noreferrer"&gt;奶牛玩杂技&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&#10; &lt;a href="#p1842-%e5%a5%b6%e7%89%9b%e7%8e%a9%e6%9d%82%e6%8a%80" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;div style="max-width:760px;margin:16px auto;padding:16px 20px;border-left:3px solid #4a6fa5;background: transparent;border-radius:0 8px 8px 0;font-family:-apple-system,'PingFang SC','Microsoft YaHei',sans-serif;line-height:1.75;color: #c5d3e1;"&gt;&#10;&lt;p&gt;每头牛都有自己的体重以及力量，编号为 $i $的奶牛的体重为 $W_i$，力量为 $S_i$&lt;/p&gt;&#10;&lt;p&gt;当某头牛身上站着另一些牛时它就会在一定程度上被压扁，我们不妨把它被压扁的程度叫做它的压扁指数。对于任意的牛，她的压扁指数等于摞在她上面的所有奶牛的总重（当然不包括她自己）减去她的力量。奶牛们按照一定的顺序摞在一起后，她们的总压扁指数就是被压得最扁的那头奶牛的压扁指数。&lt;/p&gt;&#10;&lt;p&gt;你的任务就是帮助奶牛们找出一个摞在一起的顺序，使得总压扁指数最小。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;假设有两只候选牛 A,B 最有可能被压得最扁，假设 A,B 两头牛把牛分成了三部分，最上面重量为 $W_u$，中间为 $W_m$，最下面为 $W_d$&lt;/p&gt;&#10;&lt;p&gt;当 A 在 B 上方时，总压扁指数为&lt;/p&gt;&#10;$$&#10;P_1 = max(W_u- S_A,W_u &amp;#43; W_m &amp;#43; W_A- S_B)&#10;$$&lt;p&gt;当 B 在 A 上方时，总压扁指数为&lt;/p&gt;&#10;$$&#10;P_2 = max(W_u- S_B,W_u &amp;#43; W_m &amp;#43; W_B- S_A)&#10;$$&lt;p&gt;假设 A 在上方时的总压扁指数最小，则&lt;/p&gt;&#10;$$&#10;max(W_u- S_A,W_u &amp;#43; W_m &amp;#43; W_A- S_B) &amp;lt; max(W_u- S_B,W_u &amp;#43; W_m &amp;#43; W_B- S_A) &#10;$$&lt;p&gt;然后分类讨论，可以得到&lt;/p&gt;&#10;$$&#10;W_A&amp;#43;S_A &amp;lt; W_B&amp;#43;S_B&#10;$$&lt;p&gt;但是分类讨论太麻烦了，我们可以假设 A，B 相邻，问题简化为&lt;/p&gt;&#10;$$&#10;max(W_s- S_A,W_s &amp;#43; W_A- S_B) &amp;lt; max(W_s- S_B,W_s &amp;#43; W_B- S_A) &#10;$$&lt;p&gt;然后再分类讨论，也能得到正确结果&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="二进制串构造"&gt;&lt;span&gt;二进制串构造&lt;/span&gt;&#10; &lt;a href="#%e4%ba%8c%e8%bf%9b%e5%88%b6%e4%b8%b2%e6%9e%84%e9%80%a0" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;div style="max-width:760px;margin:16px auto;padding:16px 20px;border-left:3px solid #4a6fa5;background: transparent;border-radius:0 8px 8px 0;font-family:-apple-system,'PingFang SC','Microsoft YaHei',sans-serif;line-height:1.75;color: #c5d3e1;"&gt;&#10;&lt;p&gt;给定一个长度为 n的01串$S=S_1S_2...S_n$。你需要在每两个相邻的数字$Si$ 与$Si&amp;#43;1$ 之间（共$n−1$个位置）各插入一个位运算符，使生成的表达式最终运算结果为0。&#10;可供选择的运算符有三种，其运算规则如下：&#10;• 按位与（&amp;amp;）：对于表达式a&amp;amp;b，此表达式的值为 1当且仅当a=b=1，否则为 0。&#10;• 按位异或（^）：对于表达式a^b，此表达式的值为 1当且仅当a=1,b=0或a=0,b=1，否&#10;则为 0。&#10;• 按位或（|）：对于表达式a|b，此表达式的值为 0当且仅当a=b=0，否则为 1。&#10;注意，本题中位运算的优先级与C语言一致，规则如下：&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;按位与（&amp;amp;）的优先级最高。&lt;/li&gt;&#10;&lt;li&gt;按位异或（^）的优先级低于按位与，但高于按位或。&lt;/li&gt;&#10;&lt;li&gt;按位或（|）的优先级最低。&lt;/li&gt;&#10;&lt;li&gt;对于优先级相同的运算，按照自左向右的顺序结合计算。&#10;你需要构造出一组合法的运算符序列，使得整个表达式最终的运算结果等于0。如果存在多种方案，输&#10;出任意一种即可。可以证明对于所有合法的输入总能找到至少一组合法的构造方案。&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;通过回溯来解题，我们需要的最终结果为0&#10;如何得到0呢&#10;&lt;/p&gt;&#10;$$&#10;\begin{align}&#10;0 {\ \ \ }\&amp;amp;{\ \ \ } 0 = 0 \\\\&#10;1 {\ \ }\oplus{\ \ } 1 = 0 \\\\&#10;1 {\ \ \ }\&amp;amp;{\ \ \ } 0 = 0 \\\\&#10;\end{align}&#10;$$&lt;p&gt;我们发现,0，1总有办法得到0，所以&#10;贪心策略为，假定前面的总能得到0&#10;比如：000，设前两个零得到的是0，很容易得到&#10;&lt;/p&gt;&#10;$$&#10;0 = 0 \&amp;amp; 0 \&amp;amp; 0&#10;$$</description></item><item><title>xv6 Lab Trap</title><link>https://yuqu419.github.io/posts/xv6-lab-trap/</link><pubDate>Mon, 23 Nov 2026 19:41:13 +0800</pubDate><guid>https://yuqu419.github.io/posts/xv6-lab-trap/</guid><category domain="https://yuqu419.github.io/categories/%E5%AD%A6%E4%B9%A0/">学习</category><description>&lt;div style="max-width: 620px; margin: 56px auto; font-family: 'Noto Serif SC','Songti SC',Georgia,serif; color: light-dark(#2b2b2b, #d1d5db); line-height: 2;"&gt;&#10;&lt;p style="font-size: 11px; letter-spacing: .5em; text-transform: uppercase; color: light-dark(#888, #9aa3b2); margin: 0 0 24px;"&gt;Preface&lt;/p&gt;&#10;&lt;div style="width: 36px; height: 1px; background: light-dark(#111, #e1e6ed); margin-bottom: 34px;"&gt;&lt;/div&gt;&#10;&lt;p style="font-size: 15px; text-align: justify; text-justify: inter-ideograph; margin: 0 0 18px;"&gt;&#10;&lt;p&gt;你掉进陷阱啦！&lt;/p&gt;&#10;&lt;/p&gt;&#10;&lt;p style="font-size: 13px; color: light-dark(#888, #9aa3b2); letter-spacing: .1em; text-align: right; margin: 40px 0 0;"&gt;— 2026/11/23&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;h2 class="heading-element" id="risc-v"&gt;&lt;span&gt;RISC-V&lt;/span&gt;&#10; &lt;a href="#risc-v" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;在学习 RISC-V，我会将其与 x86 进行对比学习&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="寄存器"&gt;&lt;span&gt;寄存器&lt;/span&gt;&#10; &lt;a href="#%e5%af%84%e5%ad%98%e5%99%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;RISC-V 有 32 个通用寄存器&#10;分别是&lt;/p&gt;&#10;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;zero&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;ra&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;sp&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;s0&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;t0~t6&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;a0 ~ a7&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;s1 ~ s11&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;硬接线为 0&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;返回地址&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;栈指针&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;帧指针&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;临时寄存器&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;存放参数和返回值&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;保存寄存器&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;调用者保存寄存器&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;被调用者保存寄存器&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;ra,t0~t6&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;sp,s0,s1~s11&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;p&gt;而 x86 中有 16 个通用寄存器&lt;/p&gt;&#10;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;rax&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;rbx&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;rdx&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;rcx&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;rsp&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;rbp&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;rdi&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;rsi&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;r8~r15&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;存放返回值&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;常用作基址&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;参数 3&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;参数 4&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;栈指针&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;帧指针&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;参数 1&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;参数 2&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;r8 和 r9 作为参数 5 和参数 6&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;调用者保存寄存器&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;被调用者保存寄存器&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;rax,rdx,rcx,rdi,rsi,r8,r9,r10,r11&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;rbx,rbp,r12~r15&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;h3 class="heading-element" id="函数调用"&gt;&lt;span&gt;函数调用&lt;/span&gt;&#10; &lt;a href="#%e5%87%bd%e6%95%b0%e8%b0%83%e7%94%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;函数调用使用 &lt;code&gt;call&lt;/code&gt; 伪指令，之所以是伪指令，是因为实际上 &lt;code&gt;call func&lt;/code&gt; 会展开为&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;auipc ra, %pcrel_hi(func)&#10;jalr ra, %pcrel_lo(func)(ra)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;实际上做的事情就是&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;将当前指令下一条指令的地址写入 &lt;code&gt;ra&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;将目标地址写入 &lt;code&gt;pc&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;这与 x86 的一个显著区别就是在 x86 中返回地址是要压栈的，而这里是把返回地址放入 &lt;code&gt;ra&lt;/code&gt; 寄存器中&lt;/p&gt;&#10;&lt;p&gt;进入被调用函数之后，也就是函数序言&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt; addi sp, sp, -16 &#10; sd ra, -8(sp) &#10; sd s0, -16(sp) &#10; addi s0, sp, 16 &lt;/code&gt;&lt;/pre&gt;&lt;p&gt;所做的事情也就是&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;分配栈空间，也就是栈指针向下移动（记得 16 字节对齐）&lt;/li&gt;&#10;&lt;li&gt;将返回地址压栈&lt;/li&gt;&#10;&lt;li&gt;保存帧指针&lt;/li&gt;&#10;&lt;li&gt;保存被调用寄存器在栈内&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;可以看出，帧指针是相对于返回地址是在低地址的，这一点与 x86 一致，不过 risc-v 中，返回地址压栈是手动的&lt;/p&gt;&#10;&lt;p&gt;在函数尾声中&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt; lw ra, 28(sp) &#10; lw s0, 24(sp) &#10; lw s1, 20(sp)&#10; lw s2, 16(sp)&#10; addi sp, sp, 32 &#10; ret &lt;/code&gt;&lt;/pre&gt;&lt;ol&gt;&#10;&lt;li&gt;恢复返回地址&lt;/li&gt;&#10;&lt;li&gt;恢复被调用保存寄存器&lt;/li&gt;&#10;&lt;li&gt;释放帧指针&lt;/li&gt;&#10;&lt;li&gt;释放栈帧&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 class="heading-element" id="backtrace"&gt;&lt;span&gt;Backtrace&lt;/span&gt;&#10; &lt;a href="#backtrace" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;div style="padding: 40px 20px; border-radius: 12px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; max-width: 800px; margin: 0 auto; color: #fff; box-sizing: border-box;"&gt;&#10; &lt;h3 style="text-align: center; margin-top: 0; margin-bottom: 35px; font-weight: 600; font-size: 22px; color: #fff; letter-spacing: 1px;"&gt;xv6 栈帧布局 (Stack Frame)&lt;/h3&gt;&#10; &lt;div style="display: flex; max-width: 650px; margin: 0 auto; gap: 15px;"&gt;&#10; &lt;!-- 左侧地址标签 --&gt;&#10; &lt;div style="display: flex; flex-direction: column; justify-content: space-between; text-align: right; color: #888; font-size: 13px; padding: 15px 0; width: 70px; line-height: 1.5;"&gt;&#10; &lt;div&gt;高地址&lt;br&gt;(High)&lt;/div&gt;&#10; &lt;div style="padding-top: 120px;"&gt;...&lt;/div&gt;&#10; &lt;div&gt;低地址&lt;br&gt;(sp)&lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;!-- 右侧栈帧结构 --&gt;&#10; &lt;div style="flex: 1; border: 1px solid #444; border-radius: 8px; overflow: hidden; display: flex; flex-direction: column;"&gt;&#10; &lt;!-- Caller's Frame --&gt;&#10; &lt;div style="background-color: #2c2d35; padding: 18px 20px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="color: #a0a0a0; font-weight: 500;"&gt;Caller's Frame&lt;/span&gt;&#10; &lt;span style="color: #777; font-size: 14px;"&gt;调用者栈帧&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;div style="background-color: #3a2228; padding: 18px 20px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="color: #f28b82; font-weight: 500;"&gt;ra (Return Address)&lt;/span&gt;&#10; &lt;span style="color: #d69ca0; font-size: 14px;"&gt;返回地址&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;!-- 前一个帧指针 (fp) --&gt;&#10; &lt;div style="background-color: #302a42; padding: 18px 20px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="color: #c58af9; font-weight: 500;"&gt;fp (Frame Pointer)&lt;/span&gt;&#10; &lt;span style="color: #a99cc9; font-size: 14px;"&gt;前一个帧指针&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;!-- 保存的寄存器 --&gt;&#10; &lt;div style="background-color: #33301f; padding: 18px 20px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="color: #fdd663; font-weight: 500;"&gt;s0-s11 (Saved Regs)&lt;/span&gt;&#10; &lt;span style="color: #d4b95e; font-size: 14px;"&gt;保存的寄存器&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;!-- 局部变量 --&gt;&#10; &lt;div style="background-color: #233622; padding: 18px 20px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="color: #81c995; font-weight: 500;"&gt;Local Variables&lt;/span&gt;&#10; &lt;span style="color: #6da07a; font-size: 14px;"&gt;局部变量&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;!-- Callee's Frame (未分配/向下增长) --&gt;&#10; &lt;div style="background: repeating-linear-gradient(45deg, #2c2d35, #2c2d35 10px, #3a3b45 10px, #3a3b45 20px); padding: 18px 20px; display: flex; justify-content: space-between; align-items: center;"&gt;&#10; &lt;span style="color: #aaa; font-weight: 500;"&gt;Callee's Frame&lt;/span&gt;&#10; &lt;span style="color: #888; font-size: 14px;"&gt;被调用者栈帧 (向下增长)&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;!-- 底部注释 --&gt;&#10; &lt;div style="color: #666; font-size: 13px; margin-top: 35px; text-align: center; border-top: 1px solid #333; padding-top: 15px;"&gt;&#10; * 各段高度仅为示意，实际依赖架构（如 RISC-V）的对齐要求。&#10; &lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;在函数序言之后，&lt;code&gt;s0&lt;/code&gt; 中存放就是函数栈帧底部的地址，往下 8 个字节就是返回地址，再往下 8 个字节就是上一个函数栈帧底部的地址，所以说，我们总可以通过当前函数帧指针找到上一个函数的帧指针，这就类似于链表。&lt;/p&gt;&#10;&lt;p&gt;那我们如何确定栈帧形成的链表已经结束了呢？&lt;/p&gt;&#10;&lt;p&gt;在内核分配内核栈时，应该遵循页对齐的，也就是说，如果在同一个内核栈的函数栈帧，它们帧指针应该是在同一页的&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;#define PGROUNDDOWN(a) (((a)) &amp;amp; ~(PGSIZE - 1))&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;riscv.h&lt;/code&gt; 提供了这样一个 &lt;code&gt;PGROUNDDOWN&lt;/code&gt; 宏，它的作用是截取地址的前 52 位，如果地址前 52 位相同，那么它们就在同一页&lt;/p&gt;&#10;&lt;div style="padding:12px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-left:4px solid #1d6fd0;border-radius:10px;background:rgba(29,111,208,.05);"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:700;color:#1d6fd0;margin-bottom:6px;"&gt;ℹ Note&lt;/div&gt;&lt;div style="font-size:14px;line-height:1.7;"&gt;&#10;可能有人会有疑问，如果内核栈大小超过1页了，怎么办呢？其实在xv6中，内核栈的大小就刚好是一页，所以不会出现这个问题&#10;&lt;div&gt;&#10;在Linux上，backtrace的实现并不是靠帧指针，而是会有一张专门的表来存放调用链的元数据&#10;&lt;/div&gt;&#10;&lt;/div&gt;&lt;/div&gt;&#10;&lt;p&gt;在实际写 backtrace 的过程中，打印地址可能会对结果一脸茫然，这里可以大致了解一下不同区域的范围&lt;/p&gt;&#10;&lt;p&gt;在内核虚拟地址空间中，最大地址为 &lt;strong&gt;0x4000000000&lt;/strong&gt; 最上方是 &lt;strong&gt;Trampoline&lt;/strong&gt;，然后是多个 &lt;strong&gt;kernel stack&lt;/strong&gt; 和 &lt;strong&gt;guard page&lt;/strong&gt;，这些都不是直接映射的&lt;/p&gt;&#10;&lt;p&gt;其次是在&lt;strong&gt;PHYSTOP&lt;/strong&gt;与&lt;strong&gt;KERNBASE&lt;/strong&gt;之间的区域，这部分内存用于保存内核数据（也就是全局变量和静态变量）和代码段。这些内存都是直接映射的&lt;/p&gt;&#10;&lt;div style="padding:12px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-left:4px solid #1d6fd0;border-radius:10px;background:rgba(29,111,208,.05);"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:700;color:#1d6fd0;margin-bottom:6px;"&gt;ℹ Note&lt;/div&gt;&lt;div style="font-size:14px;line-height:1.7;"&gt;&#10;&lt;code&gt;PHYSTOP&lt;/code&gt;是指的是Physical Stop，表示这是物理内存的结束地址，KERNBASE = 0x80000000 , PHYSTOP = 0x88000000,这中间夹着128MB的内存大小&#10;&lt;/div&gt;&lt;/div&gt;&#10;&lt;p&gt;最后一部分是 0x0~0x80000000，这一步分都是驱动，也是采取直接映射的&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="alarm"&gt;&lt;span&gt;alarm&lt;/span&gt;&#10; &lt;a href="#alarm" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;这题首先我们要弄懂我们要做什么&lt;/p&gt;&#10;&lt;p&gt;简单来说，需要完成两个系统调用 &lt;code&gt;sigalarm&lt;/code&gt; 和 &lt;code&gt;sigreturn&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;它们的用户层接口分别是&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int sigalarm(int interval, void (*handler)());&#10;int sigreturn(void);&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;sigalarm&lt;/code&gt; 接收两个参数，作用是每过 &lt;code&gt;interval&lt;/code&gt; 个 tick 后，就调用一次 &lt;code&gt;handler&lt;/code&gt;&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="时钟机制"&gt;&lt;span&gt;时钟机制&lt;/span&gt;&#10; &lt;a href="#%e6%97%b6%e9%92%9f%e6%9c%ba%e5%88%b6" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;在 risc-v 中，有一个 &lt;code&gt;time&lt;/code&gt; 寄存器，是一个由硬件管理不断自增的计时器，而 xv6 提供了 &lt;code&gt;r_time&lt;/code&gt; 接口来获取其值&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;time&lt;/code&gt; 寄存器自增的单位是&lt;strong&gt;cycle&lt;/strong&gt;，1s = 1000000 cycles,&lt;code&gt;time&lt;/code&gt; 的自增是由硬件控制的，软件无法干预，也不用干预&lt;/p&gt;&#10;&lt;p&gt;而本实验中的 &lt;code&gt;ticks&lt;/code&gt; 是一个全局变量，它有内核进行维护，其维护的主要函数是 &lt;code&gt;clockintr&lt;/code&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;void&#10;clockintr()&#10;{&#10; if (cpuid() == 0) {&#10; acquire(&amp;amp;tickslock);&#10; ticks&amp;#43;&amp;#43;;&#10; wakeup(&amp;amp;ticks);&#10; release(&amp;amp;tickslock);&#10; }&#10;&#10; w_stimecmp(r_time() &amp;#43; 1000000);&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;由于可能有多核 CPU，只让 &lt;code&gt;cpu0&lt;/code&gt; 对 &lt;code&gt;ticks&lt;/code&gt; 的值进行更新，这段代码的重点在于&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;w_stimecmp(r_time() &amp;#43; 1000000);&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;原理是 cpu 中还有个寄存器 &lt;code&gt;stimecmp&lt;/code&gt;，&lt;code&gt;w_stimecmp&lt;/code&gt; 表示将 &lt;code&gt;stimecmp&lt;/code&gt; 修改为 &lt;code&gt;当前cycles + 1000000&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;每次 &lt;code&gt;time&lt;/code&gt; 寄存器自增时，都会于 &lt;code&gt;stimecmp&lt;/code&gt; 寄存器内的值进行比较，如果相等，则会触发一次时钟中断，每次触发时钟中断都会调用 &lt;code&gt;clockintr&lt;/code&gt; 函数，更新 &lt;code&gt;ticks&lt;/code&gt; 和 &lt;code&gt;stimecmp&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;可以看出，每次更新 &lt;code&gt;ticks&lt;/code&gt; 的间隔为 &lt;code&gt;1000000cycles&lt;/code&gt;，也就是说&lt;/p&gt;&#10;&lt;p&gt;$$&lt;/p&gt;&#10;&lt;p&gt;0.1 s = 1 tick = 1000000 cycles&lt;/p&gt;&#10;&lt;p&gt;$$&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;回到 &lt;code&gt;sigalarm&lt;/code&gt;，每过 &lt;code&gt;interval&lt;/code&gt; 个 ticks，我们就需要调用 &lt;code&gt;handler&lt;/code&gt;。考虑到需要检测 &lt;code&gt;ticks&lt;/code&gt;，所以需要把检测和调用 &lt;code&gt;handler&lt;/code&gt; 的代码放在时钟中断的相关逻辑中。最重要也是最困难的是如何安全的调用 &lt;code&gt;handler&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;考虑相关需求&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;在调用 &lt;code&gt;handler&lt;/code&gt; 前后，我们需要保证在用户态的上下文不改变，也就是说在发生 alarm，调用 &lt;code&gt;handler&lt;/code&gt; 之后，原代码继续执行&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;在 &lt;code&gt;handler&lt;/code&gt; 结束之前，不能再次调用 &lt;code&gt;handler&lt;/code&gt;，所以我们需要一个变量维护 &lt;code&gt;handler&lt;/code&gt; 调用状态&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;每次 &lt;code&gt;handler&lt;/code&gt; 结束后，会调用 &lt;code&gt;sigreturn&lt;/code&gt; 为返回到原位置做准备&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;看起来有点困难，实际上一点也不简单，先梳理一下常见的坑&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;在获得 &lt;code&gt;handler&lt;/code&gt; 的用户虚拟地址后，通过 &lt;code&gt;walkaddr&lt;/code&gt; 得到物理地址进行调用&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;div style="padding:12px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-left:4px solid #1d6fd0;border-radius:10px;background:rgba(29,111,208,.05);"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:700;color:#1d6fd0;margin-bottom:6px;"&gt;解决方法&lt;/div&gt;&lt;div style="font-size:14px;line-height:1.7;"&gt;&#10;这是函数指针于变量指针一个很大的区别，变量我们获得其物理地址之后直接访问就可以了，但函数指令却不行，因为缺少函数执行的上下文。所以如果要执行&lt;code&gt;handler&lt;/code&gt;，我们还是需要回到用户虚拟空间进行执行，方法也是很简单的，那就是改变当前&lt;code&gt;proc&lt;/code&gt;的&lt;code&gt;trapframe&lt;/code&gt;的&lt;code&gt;epc&lt;/code&gt;字段为&lt;code&gt;handler&lt;/code&gt;的用户虚拟地址&#10;&lt;/div&gt;&lt;/div&gt;&#10;&lt;ol start="2"&gt;&#10;&lt;li&gt;忽略 &lt;code&gt;sigreturn&lt;/code&gt; 的返回值&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;div style="padding:12px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-left:4px solid #1d6fd0;border-radius:10px;background:rgba(29,111,208,.05);"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:700;color:#1d6fd0;margin-bottom:6px;"&gt;解决方法&lt;/div&gt;&lt;div style="font-size:14px;line-height:1.7;"&gt;&#10;这里就涉及到了一个很重要的点:系统调用在内核层面的封装函数一定有是以&lt;code&gt;uint64&lt;/code&gt;为返回值且不含参数的，在&lt;code&gt;syscall&lt;/code&gt;分发函数中，会将封装函数的返回值赋值给&lt;code&gt;trapframe&lt;/code&gt;的&lt;code&gt;a0&lt;/code&gt;寄存器，作为用户层系统调用的返回值，假设在实际实现为&lt;code&gt;void&lt;/code&gt;，那么最后在赋给a0的值就是垃圾值了，所以一定要返回原本存的a0&#10;&lt;/div&gt;&lt;/div&gt;&#10;&lt;p&gt;在实现的过程中我们需要在 &lt;code&gt;struct proc&lt;/code&gt; 中增加一些字段，并且记得在 &lt;code&gt;allocproc&lt;/code&gt; 中初始化。在对每个进程都需要做一些处理时，逻辑应写在 &lt;code&gt;allocproc&lt;/code&gt; 函数，而不是 &lt;code&gt;kfork&lt;/code&gt; 函数，因为第一个进程不是通过 &lt;code&gt;kfork&lt;/code&gt; 创建，但需要经过 &lt;code&gt;allocproc&lt;/code&gt; 函数&lt;/p&gt;&#10;</description></item><item><title>xv6 Lab Page Table</title><link>https://yuqu419.github.io/posts/xv6-lab-pagetable/</link><pubDate>Mon, 16 Nov 2026 15:30:13 +0800</pubDate><guid>https://yuqu419.github.io/posts/xv6-lab-pagetable/</guid><category domain="https://yuqu419.github.io/categories/%E5%AD%A6%E4%B9%A0/">学习</category><description>&lt;div style="max-width: 620px; margin: 56px auto; font-family: 'Noto Serif SC','Songti SC',Georgia,serif; color: light-dark(#2b2b2b, #d1d5db); line-height: 2;"&gt;&#10;&lt;p style="font-size: 11px; letter-spacing: .5em; text-transform: uppercase; color: light-dark(#888, #9aa3b2); margin: 0 0 24px;"&gt;Preface&lt;/p&gt;&#10;&lt;div style="width: 36px; height: 1px; background: light-dark(#111, #e1e6ed); margin-bottom: 34px;"&gt;&lt;/div&gt;&#10;&lt;p style="font-size: 15px; text-align: justify; text-justify: inter-ideograph; margin: 0 0 18px;"&gt;&#10;&lt;p&gt;虚拟地址如何找到物理地址呢？&lt;/p&gt;&#10;&lt;/p&gt;&#10;&lt;p style="font-size: 13px; color: light-dark(#888, #9aa3b2); letter-spacing: .1em; text-align: right; margin: 40px 0 0;"&gt;— 2026/11/16&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;h2 class="heading-element" id="三级索引机制"&gt;&lt;span&gt;三级索引机制&lt;/span&gt;&#10; &lt;a href="#%e4%b8%89%e7%ba%a7%e7%b4%a2%e5%bc%95%e6%9c%ba%e5%88%b6" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;xv6 中，64 位虚拟地址只有低 39 位被使用，低 39 位中的前 27 位被拆成 3 个 9 位的索引，也就是三级索引。之所以每级索引占 9 位，是因为这个索引索引的是页表项（PTE），每个 PTE 占 8 个字节，故每页有 512 个 PTE&lt;/p&gt;&#10;&lt;p&gt;后 12 位表示页内偏移量，因为一页大小为 4096bytes，故 offset 需要 12 位表示&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;satp&lt;/code&gt; 寄存器保存着根页表的&lt;strong&gt;物理地址&lt;/strong&gt;&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;在实际使用中，xv6 将虚拟地址限制在 38 位，在 &lt;code&gt;kernel/riscv.h&lt;/code&gt; 中定义了 &lt;code&gt;#define MAXVA (1L &amp;lt;&amp;lt; (9 + 9 + 9 + 12 - 1))&lt;/code&gt;。在 RISC-V 标准中，第 39 位作为符号位出现，标记位不同的区域，用户地址空间位于正半区，而内核地址空间位于负半区。但 xv6 为了简化设计，弃用了负半区，转而在 &lt;code&gt;kernel/memlayout.h&lt;/code&gt; 中设定了 &lt;code&gt;#define KERNBASE 0x80000000L&lt;/code&gt;，高于 &lt;code&gt;KERNBASE&lt;/code&gt; 的空间就是内核地址空间了&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;&lt;svg xmlns="http://www.w3.org/2000/svg" style="background: transparent; background-color: transparent; color-scheme: light dark; cursor:pointer;max-width:100%;max-height:578px;" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1802px" viewBox="-0.5 -0.5 1802 578" id="ge-svg-SVIfVKwqptDZyk70ip1z" content="&amp;lt;mxfile host=&amp;quot;app.diagrams.net&amp;quot;&amp;gt;&amp;#10; &amp;lt;diagram name=&amp;quot;第 1 页&amp;quot; id=&amp;quot;sHY3qtBKj73aOUg8780x&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGraphModel dx=&amp;quot;2082&amp;quot; dy=&amp;quot;851&amp;quot; grid=&amp;quot;1&amp;quot; gridSize=&amp;quot;10&amp;quot; guides=&amp;quot;1&amp;quot; tooltips=&amp;quot;1&amp;quot; connect=&amp;quot;1&amp;quot; arrows=&amp;quot;1&amp;quot; fold=&amp;quot;1&amp;quot; page=&amp;quot;1&amp;quot; pageScale=&amp;quot;1&amp;quot; pageWidth=&amp;quot;827&amp;quot; pageHeight=&amp;quot;1169&amp;quot; math=&amp;quot;0&amp;quot; shadow=&amp;quot;0&amp;quot;&amp;gt;&amp;#10; &amp;lt;root&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;0&amp;quot; /&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;1&amp;quot; parent=&amp;quot;0&amp;quot; /&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-31&amp;quot; edge=&amp;quot;1&amp;quot; parent=&amp;quot;1&amp;quot; source=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-1&amp;quot; style=&amp;quot;edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;defaultLabelBackgroundColor=page;&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; as=&amp;quot;geometry&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxPoint x=&amp;quot;260.00000000000045&amp;quot; y=&amp;quot;370&amp;quot; as=&amp;quot;targetPoint&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxGeometry&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-32&amp;quot; connectable=&amp;quot;0&amp;quot; parent=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-31&amp;quot; style=&amp;quot;edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];inheritText=1;defaultLabelBackgroundColor=page;&amp;quot; value=&amp;quot;index&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; x=&amp;quot;0.0769&amp;quot; y=&amp;quot;1&amp;quot; as=&amp;quot;geometry&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxPoint as=&amp;quot;offset&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxGeometry&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-1&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;rounded=0;whiteSpace=wrap;html=1;&amp;quot; value=&amp;quot;VPN[2]&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;60&amp;quot; width=&amp;quot;120&amp;quot; x=&amp;quot;200&amp;quot; y=&amp;quot;180&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-2&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;rounded=0;whiteSpace=wrap;html=1;&amp;quot; value=&amp;quot;VPN[1]&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;60&amp;quot; width=&amp;quot;120&amp;quot; x=&amp;quot;320&amp;quot; y=&amp;quot;180&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-54&amp;quot; edge=&amp;quot;1&amp;quot; parent=&amp;quot;1&amp;quot; source=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-4&amp;quot; style=&amp;quot;edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;defaultLabelBackgroundColor=page;&amp;quot; target=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-43&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; as=&amp;quot;geometry&amp;quot;&amp;gt;&amp;#10; &amp;lt;Array as=&amp;quot;points&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxPoint x=&amp;quot;500&amp;quot; y=&amp;quot;120&amp;quot; /&amp;gt;&amp;#10; &amp;lt;mxPoint x=&amp;quot;1000&amp;quot; y=&amp;quot;120&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/Array&amp;gt;&amp;#10; &amp;lt;/mxGeometry&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-4&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;rounded=0;whiteSpace=wrap;html=1;&amp;quot; value=&amp;quot;VPN[0]&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;60&amp;quot; width=&amp;quot;120&amp;quot; x=&amp;quot;440&amp;quot; y=&amp;quot;180&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-55&amp;quot; edge=&amp;quot;1&amp;quot; parent=&amp;quot;1&amp;quot; source=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-5&amp;quot; style=&amp;quot;edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;defaultLabelBackgroundColor=page;&amp;quot; target=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-56&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; as=&amp;quot;geometry&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxPoint x=&amp;quot;1430&amp;quot; y=&amp;quot;280&amp;quot; as=&amp;quot;targetPoint&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxGeometry&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-5&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;rounded=0;whiteSpace=wrap;html=1;&amp;quot; value=&amp;quot;offset&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;60&amp;quot; width=&amp;quot;140&amp;quot; x=&amp;quot;560&amp;quot; y=&amp;quot;180&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-11&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;text;html=1;whiteSpace=wrap;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;rounded=0;autosize=1;resizable=0;&amp;quot; value=&amp;quot;9 bits&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;30&amp;quot; width=&amp;quot;50&amp;quot; x=&amp;quot;235&amp;quot; y=&amp;quot;250&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-12&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;text;html=1;whiteSpace=wrap;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;rounded=0;autosize=1;resizable=0;&amp;quot; value=&amp;quot;9 bits&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;30&amp;quot; width=&amp;quot;50&amp;quot; x=&amp;quot;475&amp;quot; y=&amp;quot;250&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-40&amp;quot; edge=&amp;quot;1&amp;quot; parent=&amp;quot;1&amp;quot; source=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-2&amp;quot; style=&amp;quot;edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;defaultLabelBackgroundColor=page;exitX=0.5;exitY=1;exitDx=0;exitDy=0;&amp;quot; target=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-33&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-41&amp;quot; connectable=&amp;quot;0&amp;quot; parent=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-40&amp;quot; style=&amp;quot;edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];inheritText=1;defaultLabelBackgroundColor=page;&amp;quot; value=&amp;quot;index&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; x=&amp;quot;-0.1295&amp;quot; y=&amp;quot;-1&amp;quot; as=&amp;quot;geometry&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxPoint as=&amp;quot;offset&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxGeometry&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-13&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;text;html=1;whiteSpace=wrap;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;rounded=0;autosize=1;resizable=0;&amp;quot; value=&amp;quot;9 bits&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;30&amp;quot; width=&amp;quot;50&amp;quot; x=&amp;quot;355&amp;quot; y=&amp;quot;250&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-16&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;text;html=1;whiteSpace=wrap;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;rounded=0;autosize=1;resizable=0;&amp;quot; value=&amp;quot;12 bits&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;30&amp;quot; width=&amp;quot;60&amp;quot; x=&amp;quot;600&amp;quot; y=&amp;quot;250&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-17&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;rounded=0;whiteSpace=wrap;html=1;&amp;quot; value=&amp;quot;&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;60&amp;quot; width=&amp;quot;420&amp;quot; x=&amp;quot;-220&amp;quot; y=&amp;quot;180&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-18&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;text;strokeColor=none;fillColor=none;html=1;fontSize=22;fontStyle=1;verticalAlign=middle;align=center;autosizeText=1;&amp;quot; value=&amp;quot;Virtual Address&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;50&amp;quot; width=&amp;quot;170&amp;quot; x=&amp;quot;-220&amp;quot; y=&amp;quot;120&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-19&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;text;html=1;whiteSpace=wrap;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;rounded=0;autosize=1;resizable=0;&amp;quot; value=&amp;quot;25 bits&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;30&amp;quot; width=&amp;quot;60&amp;quot; x=&amp;quot;-55&amp;quot; y=&amp;quot;250&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-47&amp;quot; edge=&amp;quot;1&amp;quot; parent=&amp;quot;1&amp;quot; source=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-21&amp;quot; style=&amp;quot;edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;defaultLabelBackgroundColor=page;&amp;quot; target=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-22&amp;quot; value=&amp;quot;&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-21&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;rounded=0;whiteSpace=wrap;html=1;&amp;quot; value=&amp;quot;&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;130&amp;quot; width=&amp;quot;200&amp;quot; x=&amp;quot;-200&amp;quot; y=&amp;quot;490&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-42&amp;quot; edge=&amp;quot;1&amp;quot; parent=&amp;quot;1&amp;quot; source=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-22&amp;quot; style=&amp;quot;edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;defaultLabelBackgroundColor=page;&amp;quot; target=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-33&amp;quot; value=&amp;quot;&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-22&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;whiteSpace=wrap;html=1;aspect=fixed;fontFamily=Helvetica;fontSize=12;fontColor=default;fontStyle=0;align=center;verticalAlign=middle;&amp;quot; value=&amp;quot;&amp;amp;lt;font style=&amp;amp;quot;font-size: 25px;&amp;amp;quot;&amp;amp;gt;Page of PTE&amp;amp;lt;/font&amp;amp;gt;&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;300&amp;quot; width=&amp;quot;300&amp;quot; x=&amp;quot;80&amp;quot; y=&amp;quot;370&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-44&amp;quot; edge=&amp;quot;1&amp;quot; parent=&amp;quot;1&amp;quot; source=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-33&amp;quot; style=&amp;quot;edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;defaultLabelBackgroundColor=page;&amp;quot; target=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-43&amp;quot; value=&amp;quot;&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-33&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;whiteSpace=wrap;html=1;aspect=fixed;fontFamily=Helvetica;fontSize=12;fontColor=default;fontStyle=0;align=center;verticalAlign=middle;&amp;quot; value=&amp;quot;&amp;amp;lt;font style=&amp;amp;quot;font-size: 25px;&amp;amp;quot;&amp;amp;gt;Page of PTE&amp;amp;lt;/font&amp;amp;gt;&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;300&amp;quot; width=&amp;quot;300&amp;quot; x=&amp;quot;460&amp;quot; y=&amp;quot;370&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-52&amp;quot; edge=&amp;quot;1&amp;quot; parent=&amp;quot;1&amp;quot; source=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-43&amp;quot; style=&amp;quot;edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;defaultLabelBackgroundColor=page;&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; as=&amp;quot;geometry&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxPoint x=&amp;quot;1240&amp;quot; y=&amp;quot;520&amp;quot; as=&amp;quot;targetPoint&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxGeometry&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-43&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;whiteSpace=wrap;html=1;aspect=fixed;&amp;quot; value=&amp;quot;&amp;amp;lt;font style=&amp;amp;quot;font-size: 25px;&amp;amp;quot;&amp;amp;gt;Page of PTE&amp;amp;lt;/font&amp;amp;gt;&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;300&amp;quot; width=&amp;quot;300&amp;quot; x=&amp;quot;850&amp;quot; y=&amp;quot;370&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-49&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;text;strokeColor=none;fillColor=none;html=1;fontSize=27;fontStyle=1;verticalAlign=middle;align=center;autosizeText=1;&amp;quot; value=&amp;quot;satp&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;40&amp;quot; width=&amp;quot;160&amp;quot; x=&amp;quot;-180&amp;quot; y=&amp;quot;650&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-50&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;text;strokeColor=none;fillColor=none;html=1;fontSize=27;fontStyle=1;verticalAlign=middle;align=center;autosizeText=1;&amp;quot; value=&amp;quot;Pagetable&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;40&amp;quot; width=&amp;quot;160&amp;quot; x=&amp;quot;-180&amp;quot; y=&amp;quot;535&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-57&amp;quot; edge=&amp;quot;1&amp;quot; parent=&amp;quot;1&amp;quot; source=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-53&amp;quot; style=&amp;quot;edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;defaultLabelBackgroundColor=page;&amp;quot; target=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-56&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry relative=&amp;quot;1&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-53&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;rounded=0;whiteSpace=wrap;html=1;&amp;quot; value=&amp;quot;PNN&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;60&amp;quot; width=&amp;quot;120&amp;quot; x=&amp;quot;1240&amp;quot; y=&amp;quot;490&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;mxCell id=&amp;quot;4YYd-5ISIH3eC9KDUPZ9-56&amp;quot; parent=&amp;quot;1&amp;quot; style=&amp;quot;rounded=0;whiteSpace=wrap;html=1;&amp;quot; value=&amp;quot;Physical Address&amp;quot; vertex=&amp;quot;1&amp;quot;&amp;gt;&amp;#10; &amp;lt;mxGeometry height=&amp;quot;60&amp;quot; width=&amp;quot;120&amp;quot; x=&amp;quot;1460&amp;quot; y=&amp;quot;490&amp;quot; as=&amp;quot;geometry&amp;quot; /&amp;gt;&amp;#10; &amp;lt;/mxCell&amp;gt;&amp;#10; &amp;lt;/root&amp;gt;&amp;#10; &amp;lt;/mxGraphModel&amp;gt;&amp;#10; &amp;lt;/diagram&amp;gt;&amp;#10;&amp;lt;/mxfile&amp;gt;&amp;#10;" onclick="(function(svg){var src=window.event.target||window.event.srcElement;while (src!=null&amp;amp;&amp;amp;src.nodeName.toLowerCase()!='a'){src=src.parentNode;}if(src==null){if(svg.wnd!=null&amp;amp;&amp;amp;!svg.wnd.closed){svg.wnd.focus();}else{var r=function(evt){if(evt.data=='ready'&amp;amp;&amp;amp;evt.source==svg.wnd){svg.wnd.postMessage(decodeURIComponent(svg.getAttribute('content')),'*');window.removeEventListener('message',r);}};window.addEventListener('message',r);svg.wnd=window.open('https://viewer.diagrams.net/?client=1&amp;amp;edit=_blank');}}})(this);"&gt;&lt;style type="text/css"&gt;@supports (color: light-dark(#000, #fff)) { #ge-svg-SVIfVKwqptDZyk70ip1z { &amp;ndash;ge-adaptive-bg: light-dark(#ffffff, var(&amp;ndash;ge-dark-color, #121212)); &amp;ndash;ge-adaptive-fg: light-dark(#000000, var(&amp;ndash;ge-light-color, #ffffff)); } }&lt;/style&gt;&lt;defs&gt;&lt;clipPath id="mx-label-gap-gvtnz"&gt;&lt;path clip-rule="evenodd" d="M 222.85 -127 L 741.15 -127 L 741.15 511 L 222.85 511 Z M 468.85 191.41 L 495.15 191.41 L 495.15 204.6 L 468.85 204.6 Z"/&gt;&lt;/clipPath&gt;&lt;clipPath id="mx-label-gap-1v2ech"&gt;&lt;path clip-rule="evenodd" d="M 246 -227 L 1184 -227 L 1184 611 L 246 611 Z M 678.85 187.41 L 705.15 187.41 L 705.15 200.6 L 678.85 200.6 Z"/&gt;&lt;/clipPath&gt;&lt;/defs&gt;&lt;g&gt;&lt;g data-cell-id="0"&gt;&lt;g data-cell-id="1"&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-31"&gt;&lt;g transform="translate(0.5,0.5)" clip-path="url(#mx-label-gap-gvtnz)"&gt;&lt;path d="M 480 127 L 480 250.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke" style="stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;path d="M 480 255.88 L 476.5 248.88 L 480 250.63 L 483.5 248.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all" style="fill: var(--ge-adaptive-fg, #000000); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-32"&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 198px; margin-left: 482px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 11px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: nowrap; "&gt;index&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="482" y="201" fill="#000000" font-family="Helvetica" font-size="11px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;index&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-1"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="420" y="67" width="120" height="60" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 97px; margin-left: 421px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;VPN[2]&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="480" y="101" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;VPN[2]&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-2"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="540" y="67" width="120" height="60" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 97px; margin-left: 541px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;VPN[1]&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="600" y="101" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;VPN[1]&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-54"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;path d="M 720 67 L 720 7 L 1220 7 L 1220 250.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke" style="stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;path d="M 1220 255.88 L 1216.5 248.88 L 1220 250.63 L 1223.5 248.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all" style="fill: var(--ge-adaptive-fg, #000000); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-4"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="660" y="67" width="120" height="60" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 97px; margin-left: 661px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;VPN[0]&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="720" y="101" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;VPN[0]&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-55"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;path d="M 920 97 L 1740 97 L 1740 370.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke" style="stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;path d="M 1740 375.88 L 1736.5 368.88 L 1740 370.63 L 1743.5 368.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all" style="fill: var(--ge-adaptive-fg, #000000); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-5"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="780" y="67" width="140" height="60" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 138px; height: 1px; padding-top: 97px; margin-left: 781px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;offset&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="850" y="101" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;offset&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-11"&gt;&lt;g&gt;&lt;rect x="455" y="137" width="50" height="30" fill="none" stroke="none" pointer-events="all"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 152px; margin-left: 456px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;9 bits&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="480" y="156" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;9 bits&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-12"&gt;&lt;g&gt;&lt;rect x="695" y="137" width="50" height="30" fill="none" stroke="none" pointer-events="all"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 152px; margin-left: 696px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;9 bits&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="720" y="156" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;9 bits&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-40"&gt;&lt;g transform="translate(0.5,0.5)" clip-path="url(#mx-label-gap-1v2ech)"&gt;&lt;path d="M 600 127 L 600 192 L 830 192 L 830 250.63" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke" style="stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;path d="M 830 255.88 L 826.5 248.88 L 830 250.63 L 833.5 248.88 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all" style="fill: var(--ge-adaptive-fg, #000000); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-41"&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 194px; margin-left: 692px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 11px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: nowrap; "&gt;index&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="692" y="197" fill="#000000" font-family="Helvetica" font-size="11px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;index&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-13"&gt;&lt;g&gt;&lt;rect x="575" y="137" width="50" height="30" fill="none" stroke="none" pointer-events="all"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 152px; margin-left: 576px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;9 bits&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="600" y="156" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;9 bits&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-16"&gt;&lt;g&gt;&lt;rect x="820" y="137" width="60" height="30" fill="none" stroke="none" pointer-events="all"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 58px; height: 1px; padding-top: 152px; margin-left: 821px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;12 bits&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="850" y="156" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;12 bits&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-17"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="0" y="67" width="420" height="60" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-18"&gt;&lt;g&gt;&lt;rect x="0" y="7" width="170" height="50" fill="none" stroke="none" pointer-events="all"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 32px; margin-left: 85px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 22px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; font-weight: bold; white-space: nowrap; "&gt;Virtual Address&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="85" y="39" fill="#000000" font-family="Helvetica" font-size="22px" text-anchor="middle" font-weight="bold" style="fill: var(--ge-adaptive-fg, #000000);"&gt;Virtual Address&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-19"&gt;&lt;g&gt;&lt;rect x="165" y="137" width="60" height="30" fill="none" stroke="none" pointer-events="all"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 58px; height: 1px; padding-top: 152px; margin-left: 166px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;25 bits&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="195" y="156" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;25 bits&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-47"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;path d="M 220 442 L 260 442 L 260 407 L 293.63 407" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke" style="stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;path d="M 298.88 407 L 291.88 410.5 L 293.63 407 L 291.88 403.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all" style="fill: var(--ge-adaptive-fg, #000000); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-21"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="20" y="377" width="200" height="130" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-42"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;path d="M 600 407 L 673.63 407" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke" style="stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;path d="M 678.88 407 L 671.88 410.5 L 673.63 407 L 671.88 403.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all" style="fill: var(--ge-adaptive-fg, #000000); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-22"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="300" y="257" width="300" height="300" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 298px; height: 1px; padding-top: 407px; margin-left: 301px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;&lt;span style="font-size: 25px;"&gt;Page of PTE&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="450" y="411" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;Page of PTE&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-44"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;path d="M 980 407 L 1063.63 407" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke" style="stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;path d="M 1068.88 407 L 1061.88 410.5 L 1063.63 407 L 1061.88 403.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all" style="fill: var(--ge-adaptive-fg, #000000); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-33"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="680" y="257" width="300" height="300" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 298px; height: 1px; padding-top: 407px; margin-left: 681px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;&lt;span style="font-size: 25px;"&gt;Page of PTE&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="830" y="411" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;Page of PTE&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-52"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;path d="M 1370 407 L 1453.63 407" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke" style="stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;path d="M 1458.88 407 L 1451.88 410.5 L 1453.63 407 L 1451.88 403.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all" style="fill: var(--ge-adaptive-fg, #000000); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-43"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="1070" y="257" width="300" height="300" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 298px; height: 1px; padding-top: 407px; margin-left: 1071px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;&lt;span style="font-size: 25px;"&gt;Page of PTE&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="1220" y="411" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;Page of PTE&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-49"&gt;&lt;g&gt;&lt;rect x="40" y="537" width="160" height="40" fill="none" stroke="none" pointer-events="all"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 557px; margin-left: 120px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 27px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; font-weight: bold; white-space: nowrap; "&gt;satp&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="120" y="565" fill="#000000" font-family="Helvetica" font-size="27px" text-anchor="middle" font-weight="bold" style="fill: var(--ge-adaptive-fg, #000000);"&gt;satp&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-50"&gt;&lt;g&gt;&lt;rect x="40" y="422" width="160" height="40" fill="none" stroke="none" pointer-events="all"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 1px; height: 1px; padding-top: 442px; margin-left: 120px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 27px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; font-weight: bold; white-space: nowrap; "&gt;Pagetable&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="120" y="450" fill="#000000" font-family="Helvetica" font-size="27px" text-anchor="middle" font-weight="bold" style="fill: var(--ge-adaptive-fg, #000000);"&gt;Pagetable&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-57"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;path d="M 1580 407 L 1673.63 407" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke" style="stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;path d="M 1678.88 407 L 1671.88 410.5 L 1673.63 407 L 1671.88 403.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all" style="fill: var(--ge-adaptive-fg, #000000); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-53"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="1460" y="377" width="120" height="60" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 407px; margin-left: 1461px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;PNN&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="1520" y="411" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;PNN&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g data-cell-id="4YYd-5ISIH3eC9KDUPZ9-56"&gt;&lt;g transform="translate(0.5,0.5)"&gt;&lt;rect x="1680" y="377" width="120" height="60" fill="#ffffff" stroke="#000000" pointer-events="all" style="fill: var(--ge-adaptive-bg, #ffffff); stroke: var(--ge-adaptive-fg, #000000);"/&gt;&lt;/g&gt;&lt;g&gt;&lt;g&gt;&lt;switch&gt;&lt;foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 118px; height: 1px; padding-top: 407px; margin-left: 1681px;"&gt;&lt;div style="box-sizing: border-box; font-size: 0; text-align: center; color: #000000; "&gt;&lt;div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: var(--ge-adaptive-fg, #000000); line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "&gt;Physical Address&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;text x="1740" y="411" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" style="fill: var(--ge-adaptive-fg, #000000);"&gt;Physical Address&lt;/text&gt;&lt;/switch&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;switch&gt;&lt;g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/&gt;&lt;a transform="translate(0,-5)" xlink:href="https://www.drawio.com/doc/faq/svg-export-text-problems" target="_blank"&gt;&lt;text text-anchor="middle" font-size="10px" x="50%" y="100%"&gt;Text is not SVG - cannot display&lt;/text&gt;&lt;/a&gt;&lt;/switch&gt;&lt;/svg&gt;&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="pte"&gt;&lt;span&gt;PTE&lt;/span&gt;&#10; &lt;a href="#pte" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;PTE 即 Page Table Entry 译为页表项，每个 PTE 占 8 个字节。PTE 是页表的基本组成单元&lt;/p&gt;&#10;&lt;p&gt;PTE 是一个 64 位的整数，但 64 并非全部使用，部分留作未来扩展，有效的主要由两部分组成。&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;PPN(Physical Page Number)&#10;PPN 是物理页号，占 44 位，这是 PTE 的核心，存储着映射的物理页的起始地址&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Flags&#10;Flags 记着该页的访问权限和状态，其中被 xv6 系统使用的有：&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;V&lt;/strong&gt;&lt;/u&gt; 第零位，作为有效位&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;R&lt;/strong&gt;&lt;/u&gt; 第一位，作为可读权限位&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;W&lt;/strong&gt;&lt;/u&gt; 第二位，作为可写权限位&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;X&lt;/strong&gt;&lt;/u&gt; 第三位，作为可执行权限位&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;U&lt;/strong&gt;&lt;/u&gt; 第四位，作为用户态权限位&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;xv6 定义了一些宏于 &lt;code&gt;kernel/riscv.h&lt;/code&gt;，以方便访问&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;#define PTE_V (1L &amp;lt;&amp;lt; 0) // valid&#10;#define PTE_R (1L &amp;lt;&amp;lt; 1)&#10;#define PTE_W (1L &amp;lt;&amp;lt; 2)&#10;#define PTE_X (1L &amp;lt;&amp;lt; 3)&#10;#define PTE_U (1L &amp;lt;&amp;lt; 4) // user can access&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;另外还有 &lt;code&gt;PTE_G&lt;/code&gt;（标记全局映射），&lt;code&gt;PTE_A&lt;/code&gt;（访问位）和 &lt;code&gt;PTE_D&lt;/code&gt;（脏位），xv6 使用较少&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;当 &lt;code&gt;PTE&lt;/code&gt; 表示的下一级索引而非叶子时，标志位 &lt;code&gt;V&lt;/code&gt; 位为 1，&lt;code&gt;R/W/X&lt;/code&gt; 全为 0，这也是非叶子 PTE 的标志&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;当访问虚拟地址时，通过页表将虚拟地址映射为物理地址&lt;/p&gt;&#10;&lt;p&gt;$$&lt;/p&gt;&#10;&lt;p&gt;Physical Address(56bits) = PPN(44bits) + Offset(12bits)&lt;/p&gt;&#10;&lt;p&gt;$$&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="walk-函数"&gt;&lt;span&gt;&lt;code&gt;walk&lt;/code&gt; 函数&lt;/span&gt;&#10; &lt;a href="#walk-%e5%87%bd%e6%95%b0" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;&lt;code&gt;walk&lt;/code&gt; 函数位于 &lt;code&gt;kernel/vm.c&lt;/code&gt; 中，其主要作用就是按照三级索引下降，最终返回叶子 PTE 的指针&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;// Return the address of the PTE in page table pagetable&#10;// that corresponds to virtual address va. If alloc!=0,&#10;// create any required page-table pages.&#10;//&#10;// The risc-v Sv39 scheme has three levels of page-table&#10;// pages. A page-table page contains 512 64-bit PTEs.&#10;// A 64-bit virtual address is split into five fields:&#10;// 39..63 -- must be zero.&#10;// 30..38 -- 9 bits of level-2 index.&#10;// 21..29 -- 9 bits of level-1 index.&#10;// 12..20 -- 9 bits of level-0 index.&#10;// 0..11 -- 12 bits of byte offset within the page.&#10;pte_t * walk(pagetable_t pagetable, uint64 va, int alloc)&#10;{&#10; if (va &amp;gt;= MAXVA) //判断传入的虚拟地址是否超过最大虚拟地址&#10; panic(&amp;#34;walk&amp;#34;); //如果超过就触发恐慌&#10; //遍历三级索引&#10; for (int level = 2; level &amp;gt; 0; level--) {&#10; //提取索引&#10; pte_t *pte = &amp;amp;pagetable[PX(level, va)];&#10; //检查有效位&#10; if (*pte &amp;amp; PTE_V) {&#10; //将PTE转化为物理地址 &#10; pagetable = (pagetable_t)PTE2PA(*pte);&#10;&#10; #ifdef LAB_PGTBL&#10; if (PTE_LEAF(*pte)) {&#10; return pte;&#10; }&#10; #endif&#10; //有效位为0，判断是否分配新的物理页&#10; } else {&#10; //只查找，不分配&#10; if (!alloc || (pagetable = (pde_t *)kalloc()) == 0)&#10; return 0;&#10; //缺失是分配新的物理页&#10; memset(pagetable, 0, PGSIZE);&#10; *pte = PA2PTE(pagetable) | PTE_V;&#10; }&#10;&#10; }&#10; //返回物理页号&#10; return &amp;amp;pagetable[PX(0, va)];&#10; }&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;其中：&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;PX&lt;/code&gt; 宏定义在 &lt;code&gt;kernel/riscv.h&lt;/code&gt; 中，用于提取每一级 VPN（Virtual Page Number），即每一级索引&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;// extract the three 9-bit page table indices from a virtual address.&#10;#define PXMASK 0x1FF // 9 bits&#10;#define PXSHIFT(level) (PGSHIFT &amp;#43; (9 * (level)))&#10;#define PX(level, va) ((((uint64)(va)) &amp;gt;&amp;gt; PXSHIFT(level)) &amp;amp; PXMASK)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;PA2PTE&lt;/code&gt; 与 &lt;code&gt;PTE2PA&lt;/code&gt; 是 PTE 与物理地址互相转化的工具(这里的 &lt;code&gt;two&lt;/code&gt; 是 &lt;code&gt;to&lt;/code&gt;)，同样定义在 &lt;code&gt;kernel/riscv.h&lt;/code&gt; 中&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;// shift a physical address to the right place for a PTE.&#10;#define PA2PTE(pa) ((((uint64)pa) &amp;gt;&amp;gt; 12) &amp;lt;&amp;lt; 10)&#10;&#10;#define PTE2PA(pte) (((pte) &amp;gt;&amp;gt; 10) &amp;lt;&amp;lt; 12)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;转化原理&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;PTE&lt;/strong&gt;与&lt;strong&gt;PA&lt;/strong&gt;结构如下&lt;/p&gt;&#10;&lt;div style="font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; max-width: 800px; margin: 20px 0; line-height: 1.5; color: inherit;"&gt;&#10; &lt;!-- ================= 物理地址 PA ================= --&gt;&#10; &lt;div style="font-weight: 700; margin-bottom: 8px; font-size: 14px; opacity: 0.9;"&gt;&#10; 物理地址 PA（56 位）&#10; &lt;/div&gt;&#10; &lt;div style="display: grid; grid-template-columns: 44fr 12fr; border: 1px solid rgba(148, 163, 184, 0.5); border-radius: 6px; overflow: hidden; height: 42px; text-align: center; font-weight: 600;"&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(96, 165, 250, 0.15); color: #60a5fa; border-right: 1px solid rgba(148, 163, 184, 0.5);"&gt;&#10; PPN (44 位)&#10; &lt;/div&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(74, 222, 128, 0.15); color: #4ade80;"&gt;&#10; Offset (12 位)&#10; &lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;div style="display: grid; grid-template-columns: 44fr 12fr; text-align: center; font-size: 11px; opacity: 0.6; margin-top: 4px; margin-bottom: 28px;"&gt;&#10; &lt;div&gt;bit 55 … 12&lt;/div&gt;&#10; &lt;div&gt;bit 11 … 0&lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;!-- ================= 页表项 PTE ================= --&gt;&#10; &lt;div style="font-weight: 700; margin-bottom: 8px; font-size: 14px; opacity: 0.9;"&gt;&#10; 页表项 PTE（64 位）&#10; &lt;/div&gt;&#10; &lt;!-- 使用 11 列的 Grid，完美分配宽度比例：10, 44, 2, 1x8 --&gt;&#10; &lt;div style="display: grid; grid-template-columns: 10fr 44fr 2fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr; border: 1px solid rgba(148, 163, 184, 0.5); border-radius: 6px; overflow: hidden; height: 42px; text-align: center; font-weight: 600; font-size: 12px;"&gt;&#10; &lt;!-- Reserved (10位) --&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(148, 163, 184, 0.1); color: rgba(148, 163, 184, 1); border-right: 1px solid rgba(148, 163, 184, 0.3);"&gt;&#10; Resv&#10; &lt;/div&gt;&#10; &lt;!-- PPN (44位) --&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(96, 165, 250, 0.15); color: #60a5fa; border-right: 1px solid rgba(148, 163, 184, 0.3);"&gt;&#10; PPN (44 位)&#10; &lt;/div&gt;&#10; &lt;!-- RSW (2位) --&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(192, 132, 252, 0.15); color: #c084fc; border-right: 1px solid rgba(148, 163, 184, 0.3);"&gt;&#10; RSW&#10; &lt;/div&gt;&#10; &lt;!-- D A G U X W R V (各1位) --&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(239, 68, 68, 0.15); color: #ef4444; border-right: 1px solid rgba(148, 163, 184, 0.3);"&gt;D&lt;/div&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(249, 115, 22, 0.15); color: #f97316; border-right: 1px solid rgba(148, 163, 184, 0.3);"&gt;A&lt;/div&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(234, 179, 8, 0.15); color: #eab308; border-right: 1px solid rgba(148, 163, 184, 0.3);"&gt;G&lt;/div&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(132, 204, 22, 0.15); color: #84cc16; border-right: 1px solid rgba(148, 163, 184, 0.3);"&gt;U&lt;/div&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(20, 184, 166, 0.15); color: #14b8a6; border-right: 1px solid rgba(148, 163, 184, 0.3);"&gt;X&lt;/div&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(6, 182, 212, 0.15); color: #06b6d4; border-right: 1px solid rgba(148, 163, 184, 0.3);"&gt;W&lt;/div&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(59, 130, 246, 0.15); color: #3b82f6; border-right: 1px solid rgba(148, 163, 184, 0.3);"&gt;R&lt;/div&gt;&#10; &lt;div style="display: flex; align-items: center; justify-content: center; background: rgba(168, 85, 247, 0.15); color: #a855f7;"&gt;V&lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;!-- PTE 下方标尺 --&gt;&#10; &lt;div style="display: grid; grid-template-columns: 10fr 44fr 2fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr; text-align: center; font-size: 11px; opacity: 0.6; margin-top: 4px;"&gt;&#10; &lt;div&gt;63…54&lt;/div&gt;&#10; &lt;div&gt;53…10&lt;/div&gt;&#10; &lt;div&gt;9-8&lt;/div&gt;&#10; &lt;div style="grid-column: span 8; text-align: right; padding-right: 4px;"&gt;7 … 0&lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;div style="margin-top: 24px; font-size: 13px; padding: 12px 16px; background: rgba(59, 130, 246, 0.08); border-left: 4px solid #3b82f6; border-radius: 0 6px 6px 0;"&gt;&#10; &lt;span style="color: #60a5fa; font-weight: 700;"&gt;PPN&lt;/span&gt; 在 PA 里从 &lt;b&gt;bit 12&lt;/b&gt; 开始，在 PTE 里从 &lt;b&gt;bit 10&lt;/b&gt; 开始 —— 中间差 &lt;b&gt;2 位&lt;/b&gt;。这就是为什么提取时 `&gt;&gt;10`，放回时 `&lt;&lt;12`&#10; &lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h2 class="heading-element" id="mappages-函数"&gt;&lt;span&gt;&lt;code&gt;mappages&lt;/code&gt; 函数&lt;/span&gt;&#10; &lt;a href="#mappages-%e5%87%bd%e6%95%b0" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;&lt;code&gt;mappages&lt;/code&gt; 用于建立 物理内存 与虚拟地址之间的联系&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;// Create PTEs for virtual addresses starting at va that refer to&#10;// physical addresses starting at pa.&#10;// va and size MUST be page-aligned.&#10;// Returns 0 on success, -1 if walk() couldn&amp;#39;t&#10;// allocate a needed page-table page.&#10;int&#10;mappages(pagetable_t pagetable, uint64 va, &#10; uint64 size, uint64 pa, int perm)&#10;{&#10; uint64 a, last;&#10; pte_t *pte;&#10; //检查对齐&#10; if ((va % PGSIZE) != 0)&#10; panic(&amp;#34;mappages: va not aligned&amp;#34;);&#10;&#10; if ((size % PGSIZE) != 0)&#10; panic(&amp;#34;mappages: size not aligned&amp;#34;);&#10;&#10; if (size == 0)&#10; panic(&amp;#34;mappages: size&amp;#34;);&#10;&#10; a = va;&#10; last = va &amp;#43; size - PGSIZE;&#10; //for循环实现批量映射&#10; for (;;) {&#10; if ((pte = walk(pagetable, a, 1)) == 0)&#10; return -1;&#10; if (*pte &amp;amp; PTE_V)&#10; panic(&amp;#34;mappages: remap&amp;#34;);&#10; //将地址和权限写入pte&#10; *pte = PA2PTE(pa) | perm | PTE_V;&#10;&#10; if (a == last)&#10; break;&#10; //移动至下一页&#10; a &amp;#43;= PGSIZE;&#10; pa &amp;#43;= PGSIZE;&#10; }&#10; return 0;&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;h2 class="heading-element" id="用户地址空间与内核地址空间"&gt;&lt;span&gt;用户地址空间与内核地址空间&lt;/span&gt;&#10; &lt;a href="#%e7%94%a8%e6%88%b7%e5%9c%b0%e5%9d%80%e7%a9%ba%e9%97%b4%e4%b8%8e%e5%86%85%e6%a0%b8%e5%9c%b0%e5%9d%80%e7%a9%ba%e9%97%b4" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;用户地址空间&lt;/p&gt;&#10;&lt;div style="padding: 32px 24px; border-radius: 12px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif; color: #e0e0e0; max-width: 500px; margin: 24px auto; box-sizing: border-box;"&gt;&#10; &lt;div style="text-align: center; font-weight: 600; font-size: 18px; margin-bottom: 28px; color: #e0e0e0; letter-spacing: 0.5px;"&gt;&#10; xv6 用户地址空间布局&#10; &lt;/div&gt;&#10; &lt;div style="display: flex; flex-direction: column; font-size: 14px; position: relative;"&gt;&#10; &lt;div style="display: flex;"&gt;&#10; &lt;!-- 左侧地址标签栏 (绝对定位精准对齐右侧加高后的区块) --&gt;&#10; &lt;div style="width: 110px; flex: none; position: relative; padding-right: 16px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; color: #888; text-align: right;"&gt;&#10; &lt;div style="position: absolute; top: 0; right: 16px; transform: translateY(-50%);"&gt;MAXVA&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 75px; right: 16px; transform: translateY(-50%);"&gt;TRAMPOLINE&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 150px; right: 16px; transform: translateY(-50%);"&gt;TRAPFRAME&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 385px; right: 16px; transform: translateY(-50%);"&gt;p-&amp;gt;sz&lt;/div&gt;&#10; &lt;div style="position: absolute; bottom: 0; right: 16px; transform: translateY(50%);"&gt;0&lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;!-- 右侧内存区块 --&gt;&#10; &lt;div style="flex: 1; display: flex; flex-direction: column; border: 1px solid #444; border-radius: 8px; overflow: hidden; background-color: #2b2b36;"&gt;&#10; &lt;div style="height: 75px; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; background-color: #1a2d3d; border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="font-weight: 600; color: #6da3ef; font-size: 14px;"&gt;trampoline&lt;/span&gt;&#10; &lt;span style="font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; color: #6da3ef; opacity: 0.8;"&gt;内核跳板代码&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;div style="height: 75px; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; background-color: #2d1a22; border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="font-weight: 600; color: #e57e96; font-size: 14px;"&gt;trapframe&lt;/span&gt;&#10; &lt;span style="font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; color: #e57e96; opacity: 0.8;"&gt;保存用户上下文&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;div style="height: 75px; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; background-color: #261e35; border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="font-weight: 600; color: #b380c9; font-size: 14px;"&gt;用户栈 stack&lt;/span&gt;&#10; &lt;span style="font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; color: #b380c9; opacity: 0.8;"&gt;向低地址增长&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;div style="height: 40px; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; background: repeating-linear-gradient(45deg, #2a1818, #2a1818 6px, #201212 6px, #201212 12px); border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="font-weight: 600; color: #e55e5e; font-size: 13px;"&gt;guard page&lt;/span&gt;&#10; &lt;span style="font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 11px; color: #e55e5e; opacity: 0.8;"&gt;不可访问&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;div style="height: 120px; display: flex; flex-direction: column; justify-content: center; align-items: center; background-color: #2b2416; border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="font-weight: 600; color: #d9a358; margin-bottom: 6px; font-size: 14px;"&gt;heap (堆)&lt;/span&gt;&#10; &lt;span style="font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; color: #d9a358; opacity: 0.8;"&gt;↑ sbrk() 向上增长&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;div style="height: 75px; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; background-color: #182e1d; border-bottom: 1px solid #444;"&gt;&#10; &lt;span style="font-weight: 600; color: #86c984; font-size: 14px;"&gt;data (.data + .bss)&lt;/span&gt;&#10; &lt;span style="font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; color: #86c984; opacity: 0.8;"&gt;全局/静态变量&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;div style="height: 75px; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; background-color: #1a2436;"&gt;&#10; &lt;span style="font-weight: 600; color: #7b9ff2; font-size: 14px;"&gt;text (代码段)&lt;/span&gt;&#10; &lt;span style="font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; color: #7b9ff2; opacity: 0.8;"&gt;只读、可执行&lt;/span&gt;&#10; &lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;/div&gt;&#10; &lt;div style="margin-top: 24px; font-size: 12px; color: #888; line-height: 1.6; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; border-top: 1px solid #333; padding-top: 16px;"&gt;&#10; &lt;div style="margin-top: 10px; opacity: 0.7;"&gt;* 各段高度仅为示意，实际依赖页面（PGSIZE）对齐。&lt;/div&gt;&#10; &lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;h3 class="heading-element" id="区域功能详解"&gt;&lt;span&gt;区域功能详解&lt;/span&gt;&#10; &lt;a href="#%e5%8c%ba%e5%9f%9f%e5%8a%9f%e8%83%bd%e8%af%a6%e8%a7%a3" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;div style="max-width:860px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI','PingFang SC','Microsoft YaHei',sans-serif;font-size:14px;line-height:1.7;"&gt;&#10;&lt;div style="font-family:ui-monospace,Menlo,Consolas,monospace;font-size:11px;letter-spacing:.12em;opacity:.55;margin-bottom:12px;"&gt;进程地址空间布局 · 低地址 ↑ 高地址&lt;/div&gt;&#10;&lt;div style="display:flex;flex-wrap:wrap;gap:14px;padding:12px 16px;margin-bottom:10px;border:1px solid rgba(148,163,184,.3);border-left:4px solid #c0392b;border-radius:10px;"&gt;&lt;div style="flex:0 0 150px;"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:15px;font-weight:700;color:#c0392b;"&gt;text&lt;/div&gt;&lt;div style="font-size:12px;opacity:.55;"&gt;代码段 · R-X&lt;/div&gt;&lt;/div&gt;&lt;div style="flex:1 1 320px;"&gt;实际上是编译后的机器指令，&lt;strong&gt;只读、可执行&lt;/strong&gt;，&lt;code&gt;pc&lt;/code&gt; 所指向的常常就是这个区域。值得了解的是，在 RISC-V 中许多指令占 &lt;strong&gt;4 字节&lt;/strong&gt;，而在 x86 中许多指令占 &lt;strong&gt;2 字节&lt;/strong&gt;。&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;flex-wrap:wrap;gap:14px;padding:12px 16px;margin-bottom:10px;border:1px solid rgba(148,163,184,.3);border-left:4px solid #b8860b;border-radius:10px;"&gt;&lt;div style="flex:0 0 150px;"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:15px;font-weight:700;color:#b8860b;"&gt;rodata&lt;/div&gt;&lt;div style="font-size:12px;opacity:.55;"&gt;只读数据段 · R--&lt;/div&gt;&lt;/div&gt;&lt;div style="flex:1 1 320px;"&gt;存储着只读数据，夹在 &lt;code&gt;text&lt;/code&gt; 和 &lt;code&gt;data&lt;/code&gt; 之间（图中未画出）。&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;flex-wrap:wrap;gap:14px;padding:12px 16px;margin-bottom:10px;border:1px solid rgba(148,163,184,.3);border-left:4px solid #0e7490;border-radius:10px;"&gt;&lt;div style="flex:0 0 150px;"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:15px;font-weight:700;color:#0e7490;"&gt;data&lt;/div&gt;&lt;div style="font-size:12px;opacity:.55;"&gt;数据段 · RW-&lt;/div&gt;&lt;/div&gt;&lt;div style="flex:1 1 320px;"&gt;包括 &lt;code&gt;.data&lt;/code&gt; 和 &lt;code&gt;.bss&lt;/code&gt;：&lt;code&gt;.data&lt;/code&gt; 是&lt;strong&gt;已初始化&lt;/strong&gt;的数据，&lt;code&gt;.bss&lt;/code&gt; 是&lt;strong&gt;未初始化或初始化为 0&lt;/strong&gt; 的数据。&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;flex-wrap:wrap;gap:14px;padding:12px 16px;margin-bottom:10px;border:1px solid rgba(148,163,184,.3);border-left:4px solid #1d6fd0;border-radius:10px;"&gt;&lt;div style="flex:0 0 150px;"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:15px;font-weight:700;color:#1d6fd0;"&gt;heap&lt;/div&gt;&lt;div style="font-size:12px;opacity:.55;"&gt;堆区 · RW-&lt;/div&gt;&lt;/div&gt;&lt;div style="flex:1 1 320px;"&gt;向&lt;strong&gt;高地址&lt;/strong&gt;方向增长，使用 &lt;code&gt;.sbrk()&lt;/code&gt; 增大或缩小。&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;flex-wrap:wrap;gap:14px;padding:12px 16px;margin-bottom:10px;border:1px solid rgba(148,163,184,.3);border-left:4px solid #c62828;border-radius:10px;"&gt;&lt;div style="flex:0 0 150px;"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:15px;font-weight:700;color:#c62828;"&gt;guard page&lt;/div&gt;&lt;div style="font-size:12px;opacity:.55;"&gt;保护页 · ---&lt;/div&gt;&lt;/div&gt;&lt;div style="flex:1 1 320px;"&gt;&lt;strong&gt;不可访问&lt;/strong&gt;，访问此区域时触发缺页异常（Page Fault），内核杀死该进程。常用来检测栈溢出；当然，当堆区过高时也会访问到这个区域。&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;flex-wrap:wrap;gap:14px;padding:12px 16px;margin-bottom:10px;border:1px solid rgba(148,163,184,.3);border-left:4px solid #7c3aed;border-radius:10px;"&gt;&lt;div style="flex:0 0 150px;"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:15px;font-weight:700;color:#7c3aed;"&gt;stack&lt;/div&gt;&lt;div style="font-size:12px;opacity:.55;"&gt;栈 · RW-&lt;/div&gt;&lt;/div&gt;&lt;div style="flex:1 1 320px;"&gt;可以说是最重要且最容易出错的区域之一，向&lt;strong&gt;低地址&lt;/strong&gt;增长。值得一提的是，xv6 的栈相当小，可能申请 4096 个 bytes 就爆栈了。&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;flex-wrap:wrap;gap:14px;padding:12px 16px;margin-bottom:10px;border:1px solid rgba(148,163,184,.3);border-left:4px solid #c2570c;border-radius:10px;"&gt;&lt;div style="flex:0 0 150px;"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:15px;font-weight:700;color:#c2570c;"&gt;trapframe&lt;/div&gt;&lt;div style="font-size:12px;opacity:.55;"&gt;陷阱帧 · RW-&lt;/div&gt;&lt;/div&gt;&lt;div style="flex:1 1 320px;"&gt;保存系统调用时的上下文，在系统调用的 Lab 中已详细了解了该区域。每个 &lt;code&gt;struct proc&lt;/code&gt; 都有一个 &lt;code&gt;trapframe&lt;/code&gt; 字段，用于访问该进程上下文内容。&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;flex-wrap:wrap;gap:14px;padding:12px 16px;margin-bottom:10px;border:1px solid rgba(148,163,184,.3);border-left:4px solid #15803d;border-radius:10px;"&gt;&lt;div style="flex:0 0 150px;"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:15px;font-weight:700;color:#15803d;"&gt;trampoline&lt;/div&gt;&lt;div style="font-size:12px;opacity:.55;"&gt;跳板页 · R-X&lt;/div&gt;&lt;/div&gt;&lt;div style="flex:1 1 320px;"&gt;保存着系统调用时的内核跳转机器指令，也就是 &lt;code&gt;trampoline.S&lt;/code&gt; 中所呈现的汇编指令。它同时被映射在用户页表和内核页表的同一虚拟地址上，因为陷入内核态时也需要访问该区域&lt;/div&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;hr&gt;&#10;&lt;h3 class="heading-element" id="内核地址空间"&gt;&lt;span&gt;内核地址空间&lt;/span&gt;&#10; &lt;a href="#%e5%86%85%e6%a0%b8%e5%9c%b0%e5%9d%80%e7%a9%ba%e9%97%b4" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;div style="width: 100%; overflow-x: auto;"&gt;&#10; &lt;div style="width: 800px; height: 800px;"&gt;&#10; &lt;div style="padding: 32px 24px; border-radius: 12px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif; color: #e0e0e0; max-width: 560px; margin: 24px auto; box-sizing: border-box; position: relative;"&gt;&#10; &lt;div style="text-align: center; font-weight: 600; font-size: 18px; margin-bottom: 24px; color: #e0e0e0; letter-spacing: 0.5px;"&gt;&#10; xv6 内核地址空间 (虚拟 &amp;rarr; 物理映射)&#10; &lt;/div&gt;&#10; &lt;div style="position: relative; width: 100%; height: 780px; font-size: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;"&gt;&#10; &lt;!-- 顶部标题 --&gt;&#10; &lt;div style="position: absolute; top: 0; left: 0; width: 180px; text-align: center; font-weight: 600; font-size: 14px; color: #888;"&gt;Virtual Addresses&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 0; right: 0; width: 180px; text-align: center; font-weight: 600; font-size: 14px; color: #888;"&gt;Physical Addresses&lt;/div&gt;&#10; &lt;!-- 左侧地址标签 --&gt;&#10; &lt;div style="position: absolute; top: 20px; left: -80px; width: 70px; text-align: right; color: #888; transform: translateY(-50%);"&gt;MAXVA&lt;br&gt;(0x4000000000)&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 260px; left: -80px; width: 70px; text-align: right; color: #888; transform: translateY(-50%); line-height: 1.2;"&gt;PHYSTOP&lt;br&gt;(0x88000000)&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 440px; left: -80px; width: 70px; text-align: right; color: #888; transform: translateY(-50%); line-height: 1.2;"&gt;KERNBASE&lt;br&gt;(0x80000000)&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 540px; left: -80px; width: 70px; text-align: right; color: #888; transform: translateY(-50%);"&gt;0x10001000&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 570px; left: -80px; width: 70px; text-align: right; color: #888; transform: translateY(-50%);"&gt;0x10000000&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 600px; left: -80px; width: 70px; text-align: right; color: #888; transform: translateY(-50%);"&gt;0x0C000000&lt;/div&gt;&#10; &lt;div style="position: absolute; bottom: 0; left: -80px; width: 70px; text-align: right; color: #888;"&gt;0&lt;/div&gt;&#10; &lt;!-- 右侧地址标签 --&gt;&#10; &lt;div style="position: absolute; top: 20px; right: -80px; width: 70px; text-align: left; color: #888; transform: translateY(-50%);"&gt;2^56-1&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 260px; right: -80px; width: 70px; text-align: left; color: #888; transform: translateY(-50%);"&gt;0x88000000&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 440px; right: -80px; width: 70px; text-align: left; color: #888; transform: translateY(-50%);"&gt;0x80000000&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 635px; right: -80px; width: 70px; text-align: left; color: #888; transform: translateY(-50%);"&gt;0x02000000&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 715px; right: -80px; width: 70px; text-align: left; color: #888; transform: translateY(-50%);"&gt;0x1000&lt;/div&gt;&#10; &lt;div style="position: absolute; bottom: 0; right: -80px; width: 70px; text-align: left; color: #888;"&gt;0&lt;/div&gt;&#10; &lt;!-- 左侧虚拟区块 --&gt;&#10; &lt;div style="position: absolute; top: 20px; left: 0; width: 180px; height: 36px; background-color: #1a2d3d; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #6da3ef;"&gt;Trampoline&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 60px; left: 0; width: 180px; height: 20px; background: repeating-linear-gradient(45deg, #2a1818, #2a1818 6px, #201212 6px, #201212 12px); border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #e55e5e; font-size: 11px;"&gt;Guard page&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 84px; left: 0; width: 180px; height: 36px; background-color: #261e35; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #b380c9;"&gt;Kstack 0&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 124px; left: 0; width: 180px; height: 20px; background: repeating-linear-gradient(45deg, #2a1818, #2a1818 6px, #201212 6px, #201212 12px); border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #e55e5e; font-size: 11px;"&gt;Guard page&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 148px; left: 0; width: 180px; height: 36px; background-color: #261e35; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #b380c9;"&gt;Kstack 1&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 188px; left: 0; width: 180px; height: 20px; display: flex; align-items: center; justify-content: center; color: #888; font-size: 16px; letter-spacing: 2px;"&gt;...&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 260px; left: 0; width: 180px; height: 100px; background-color: #2b2416; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #d9a358;"&gt;Free memory&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 364px; left: 0; width: 180px; height: 40px; background-color: #182e1d; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #86c984;"&gt;Kernel data&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 408px; left: 0; width: 180px; height: 40px; background-color: #1a2436; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #7b9ff2;"&gt;Kernel text&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 540px; left: 0; width: 180px; height: 26px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #ccc;"&gt;VIRTIO disk&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 570px; left: 0; width: 180px; height: 26px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #ccc;"&gt;UART0&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 600px; left: 0; width: 180px; height: 26px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #ccc;"&gt;PLIC&lt;/div&gt;&#10; &lt;!-- 右侧物理区块 --&gt;&#10; &lt;div style="position: absolute; top: 20px; right: 0; width: 180px; height: 236px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #888;"&gt;Unused&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 260px; right: 0; width: 180px; height: 176px; background-color: #2b2416; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #d9a358;"&gt;Physical memory (RAM)&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 440px; right: 0; width: 180px; height: 96px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #888; text-align: center; line-height: 1.4;"&gt;Unused&lt;br&gt;and other I/O devices&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 540px; right: 0; width: 180px; height: 26px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #ccc;"&gt;VIRTIO disk&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 570px; right: 0; width: 180px; height: 26px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #ccc;"&gt;UART0&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 600px; right: 0; width: 180px; height: 26px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #ccc;"&gt;PLIC&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 635px; right: 0; width: 180px; height: 26px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #ccc;"&gt;CLINT&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 665px; right: 0; width: 180px; height: 46px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #888;"&gt;Unused&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 715px; right: 0; width: 180px; height: 26px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #ccc;"&gt;boot ROM&lt;/div&gt;&#10; &lt;div style="position: absolute; top: 745px; right: 0; width: 180px; height: 26px; background-color: #2b2b36; border: 1px solid #444; border-radius: 4px; display: flex; align-items: center; justify-content: center; color: #888;"&gt;Unused&lt;/div&gt;&#10; &lt;!-- 中间映射连线 (SVG) --&gt;&#10; &lt;svg style="position: absolute; top: 0; left: 190px; width: 150px; height: 780px; pointer-events: none;"&gt;&#10; &lt;defs&gt;&#10; &lt;marker id="arrow" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"&gt;&#10; &lt;path d="M 0 0 L 10 5 L 0 10 z" fill="#888" /&gt;&#10; &lt;/marker&gt;&#10; &lt;/defs&gt;&#10; &lt;!-- Trampoline -&gt; RAM Top --&gt;&#10; &lt;line x1="0" y1="38" x2="130" y2="280" stroke="#888" stroke-width="1.5" stroke-dasharray="3,3" marker-end="url(#arrow)" /&gt;&#10; &lt;!-- Kstack 0 -&gt; RAM --&gt;&#10; &lt;line x1="0" y1="102" x2="130" y2="320" stroke="#888" stroke-width="1.5" stroke-dasharray="3,3" marker-end="url(#arrow)" /&gt;&#10; &lt;!-- Kstack 1 -&gt; RAM --&gt;&#10; &lt;line x1="0" y1="166" x2="130" y2="350" stroke="#888" stroke-width="1.5" stroke-dasharray="3,3" marker-end="url(#arrow)" /&gt;&#10; &lt;!-- Free memory Top (PHYSTOP) -&gt; RAM Top --&gt;&#10; &lt;line x1="0" y1="260" x2="130" y2="260" stroke="#888" stroke-width="1.5" stroke-dasharray="3,3" marker-end="url(#arrow)" /&gt;&#10; &lt;!-- Free memory Bottom -&gt; RAM Mid --&gt;&#10; &lt;line x1="0" y1="360" x2="130" y2="360" stroke="#888" stroke-width="1.5" stroke-dasharray="3,3" marker-end="url(#arrow)" /&gt;&#10; &lt;!-- Kernel data -&gt; RAM Bottom --&gt;&#10; &lt;line x1="0" y1="384" x2="130" y2="384" stroke="#888" stroke-width="1.5" stroke-dasharray="3,3" marker-end="url(#arrow)" /&gt;&#10; &lt;!-- Kernel text -&gt; RAM Bottom edge --&gt;&#10; &lt;line x1="0" y1="428" x2="130" y2="428" stroke="#888" stroke-width="1.5" stroke-dasharray="3,3" marker-end="url(#arrow)" /&gt;&#10; &lt;!-- VIRTIO -&gt; VIRTIO --&gt;&#10; &lt;line x1="0" y1="553" x2="130" y2="553" stroke="#888" stroke-width="1.5" stroke-dasharray="3,3" marker-end="url(#arrow)" /&gt;&#10; &lt;!-- UART0 -&gt; UART0 --&gt;&#10; &lt;line x1="0" y1="583" x2="130" y2="583" stroke="#888" stroke-width="1.5" stroke-dasharray="3,3" marker-end="url(#arrow)" /&gt;&#10; &lt;!-- PLIC -&gt; PLIC --&gt;&#10; &lt;line x1="0" y1="613" x2="130" y2="613" stroke="#888" stroke-width="1.5" stroke-dasharray="3,3" marker-end="url(#arrow)" /&gt;&#10; &lt;/svg&gt;&#10; &lt;/div&gt;&#10; &lt;div style="margin-top: 10px; font-size: 12px; color: #888; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; text-align: center;"&gt;&#10; * 左右地址空间通过页表映射对应&#10; &lt;/div&gt;&#10;&lt;/div&gt;&#10; &lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;内核地址空间多采用直接映射，内核空间从 &lt;code&gt;KERNBASE&lt;/code&gt; 开始&lt;/p&gt;&#10;&lt;p&gt;值得一提的是，每个进程都有一个内核栈，但内核栈并不保存在进程的用户地址空间中，而是保存在内核地址空间中，内核地址空间为所有进程共享，但只有当 CPU 处于 Supervisor Mode 时才可以访问。内核栈之间也会插入&lt;strong&gt;guard page&lt;/strong&gt;，以防止栈溢出&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="物理页的分配"&gt;&lt;span&gt;物理页的分配&lt;/span&gt;&#10; &lt;a href="#%e7%89%a9%e7%90%86%e9%a1%b5%e7%9a%84%e5%88%86%e9%85%8d" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;在 &lt;code&gt;walk&lt;/code&gt; 函数中，若 &lt;code&gt;alloc&lt;/code&gt; ≠ 0 时，会通过 &lt;code&gt;kalloc()&lt;/code&gt; 函数新分配物理页，&lt;code&gt;kalloc&lt;/code&gt; 实现在 &lt;code&gt;kernel/kalloc.c&lt;/code&gt; 中&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;// Allocate one 4096-byte page of physical memory.&#10;// Returns a pointer that the kernel can use.&#10;// Returns 0 if the memory cannot be allocated.&#10;void *&#10;kalloc(void)&#10;{&#10; struct run *r;&#10;&#10; acquire(&amp;amp;kmem.lock);&#10; r = kmem.freelist;&#10; if (r)&#10; kmem.freelist = r-&amp;gt;next;&#10; release(&amp;amp;kmem.lock);&#10;&#10; if (r)&#10; memset((char *)r, 5, PGSIZE); // fill with junk&#10; return (void *)r;&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;显然，&lt;code&gt;kalloc&lt;/code&gt; 维护着一个链表，&lt;code&gt;freelist&lt;/code&gt; 始终指向一个空闲页，当拿到空闲页时，会用 0x5 填充空闲页垃圾数据&lt;/p&gt;&#10;&lt;div style="padding:12px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-left:4px solid #1d6fd0;border-radius:10px;background:rgba(29,111,208,.05);"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:700;color:#1d6fd0;margin-bottom:6px;"&gt;kalloc 功用&lt;/div&gt;&lt;div style="font-size:14px;line-height:1.7;"&gt;&#10;功能上，&lt;code&gt;kalloc&lt;/code&gt;返回一个物理地址，但从实现上看，它返回的是一个指针，似乎更像是虚拟地址。实际上，&lt;code&gt;kalloc&lt;/code&gt;只能由在内核态运行，由于内核采用直接映射，故虚拟地址等同于物理地址。当用户地址空间需要物理页时，由内核地址空间调用&lt;code&gt;kalloc&lt;/code&gt;申请，然后通过&lt;code&gt;mappages&lt;/code&gt;将物理页与虚拟地址产生联系&#10;&lt;/div&gt;&lt;/div&gt;&#10;&lt;h2 class="heading-element" id="从虚拟地址到物理地址"&gt;&lt;span&gt;从虚拟地址到物理地址&lt;/span&gt;&#10; &lt;a href="#%e4%bb%8e%e8%99%9a%e6%8b%9f%e5%9c%b0%e5%9d%80%e5%88%b0%e7%89%a9%e7%90%86%e5%9c%b0%e5%9d%80" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;我们以虚拟地址 &lt;u&gt;&lt;strong&gt;0x0000_0000_0000_1004&lt;/strong&gt;&lt;/u&gt; 为例串联起从虚拟地址到物理地址全过程&lt;/p&gt;&#10;&lt;div style="display:flex;gap:12px;padding:12px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-radius:10px;"&gt;&lt;div style="flex:0 0 26px;height:26px;border-radius:50%;background:rgba(29,111,208,.15);color:#1d6fd0;font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:700;display:flex;align-items:center;justify-content:center;"&gt;1&lt;/div&gt;&lt;div style="flex:1;"&gt;&lt;div style="font-size:14px;font-weight:600;margin-bottom:2px;"&gt;拆分地址&lt;/div&gt;&lt;div style="font-size:13.5px;line-height:1.7;opacity:.8;"&gt;&#10;将虚拟地址后39位拆分，得到&#10;&lt;div style="padding:4px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-radius:10px;"&gt;&#10;&lt;div style="display:flex;gap:12px;padding:8px 0;border-bottom:1px solid rgba(148,163,184,.15);"&gt;&lt;div style="flex:0 0 120px;font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:600;color:#1d6fd0;"&gt;VPN[0]&lt;/div&gt;&lt;div style="flex:1;font-size:13.5px;line-height:1.7;"&gt;1&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;gap:12px;padding:8px 0;"&gt;&lt;div style="flex:0 0 120px;font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:600;color:#1d6fd0;"&gt;VPN[1]&lt;/div&gt;&lt;div style="flex:1;font-size:13.5px;line-height:1.7;"&gt;0&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;gap:12px;padding:8px 0;"&gt;&lt;div style="flex:0 0 120px;font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:600;color:#1d6fd0;"&gt;VPN[2]&lt;/div&gt;&lt;div style="flex:1;font-size:13.5px;line-height:1.7;"&gt;0&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;gap:12px;padding:8px 0;"&gt;&lt;div style="flex:0 0 120px;font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:600;color:#1d6fd0;"&gt;Offset&lt;/div&gt;&lt;div style="flex:1;font-size:13.5px;line-height:1.7;"&gt;4&lt;/div&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;gap:12px;padding:12px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-radius:10px;"&gt;&#10;&lt;div style="flex:0 0 26px;height:26px;border-radius:50%;background:rgba(29,111,208,.15);color:#1d6fd0;font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:700;display:flex;align-items:center;justify-content:center;"&gt;2&lt;/div&gt;&lt;div style="flex:1;"&gt;&lt;div style="font-size:14px;font-weight:600;margin-bottom:2px;"&gt;用户态访问&lt;/div&gt;&lt;div style="font-size:13.5px;line-height:1.7;opacity:.8;"&gt;通过&lt;code&gt;*&lt;/code&gt;访问虚拟地址，此时CPU处于用户态&#10;&lt;div&gt;1. 先从 &lt;code&gt;satp&lt;/code&gt; 拿到根页表基址，找到第 &lt;b&gt;VPN[2] = 0 &lt;/b&gt; 个PTE，发现它是有效的，从这个PTE中提取出出中间级页表的物理地址&lt;/div&gt;&#10;&lt;div&gt;2. 再在前面的物理地址基址中，找到第 &lt;b&gt;VPN[1] = 0 &lt;/b&gt; 个PTE，发现它是有效的，从这个PTE中提取出叶子级页表的物理地址&lt;/div&gt;&#10;&lt;div&gt;3. 再在前面的物理地址基址，找到第 &lt;b&gt;VPN[0] = 1 &lt;/b&gt; 个PTE，发现它是有效的，从这个PTE中提取物理页号，再拼上12位的&lt;b&gt;offset&lt;/b&gt; , 得到物理地址&lt;/div&gt;&#10;&lt;div&gt;4. 最后去相应物理地址出读出数据&lt;/div&gt;&#10;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&#10;&lt;div style="display:flex;gap:12px;padding:12px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-radius:10px;"&gt;&lt;div style="flex:0 0 26px;height:26px;border-radius:50%;background:rgba(29,111,208,.15);color:#1d6fd0;font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:700;display:flex;align-items:center;justify-content:center;"&gt;3&lt;/div&gt;&lt;div style="flex:1;"&gt;&lt;div style="font-size:14px;font-weight:600;margin-bottom:2px;"&gt;分配新内存&lt;/div&gt;&lt;div style="font-size:13.5px;line-height:1.7;opacity:.8;"&gt;假设用户程序调用了&lt;code&gt;malloc&lt;/code&gt;函数申请堆内存&#10;&lt;div&gt;&#10;&lt;code&gt;malloc&lt;/code&gt; 会触发 &lt;code&gt;sbrk&lt;/code&gt;系统调用，然后进入内核态，内核调用&lt;code&gt;kalloc&lt;/code&gt;函数申请物理页，再调用&lt;code&gt;mappages&lt;/code&gt;将物理内存与用户的虚拟地址产生映射关系，最后返回物理页对应的虚拟地址&#10;&lt;/div&gt;&#10;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&#10;&lt;h2 class="heading-element" id="用户虚拟地址内核虚拟地址与物理地址"&gt;&lt;span&gt;用户虚拟地址，内核虚拟地址与物理地址&lt;/span&gt;&#10; &lt;a href="#%e7%94%a8%e6%88%b7%e8%99%9a%e6%8b%9f%e5%9c%b0%e5%9d%80%e5%86%85%e6%a0%b8%e8%99%9a%e6%8b%9f%e5%9c%b0%e5%9d%80%e4%b8%8e%e7%89%a9%e7%90%86%e5%9c%b0%e5%9d%80" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;三者极易弄混，在习惯上，能直接访问的地址用指针表示，不能直接访问的地址用 uint64 表示&lt;/p&gt;&#10;&lt;p&gt;我们不能在内核态访问用户虚拟地址，因为在访问虚拟地址时，实际上是&lt;strong&gt;MMU&lt;/strong&gt;根据 &lt;code&gt;satp&lt;/code&gt; 寄存器中存放的物理地址读取，每个进程都有自己的页表。而内核的页表是一个定义在 &lt;code&gt;kernel/vm.c&lt;/code&gt; 中的全局变量，在学习 syscall 时，我们已经知道，在汇编函数 &lt;code&gt;uservec&lt;/code&gt;，会将 &lt;code&gt;satp&lt;/code&gt; 从用户页表切换至内核页表，所以在系统调用时访问用户虚拟空间也是行不通的&lt;/p&gt;&#10;&lt;p&gt;因此，在编写系统调用实现函数，比如 &lt;code&gt;int open(char* file,int flags)&lt;/code&gt;，我们需要接收一个字符串指针，但显然，在 &lt;code&gt;sys_open&lt;/code&gt; 中，万万不能对拿到的指针进行解引用的，为了便于使用，xv6 实现了 &lt;code&gt;copyin&lt;/code&gt; 和 &lt;code&gt;copyout&lt;/code&gt; 来实现 kernel 和 user 数据复制，我们来看看 xv6 是怎么做的&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;// Copy from user to kernel.&#10;// Copy len bytes to dst from virtual address srcva in a given page table.&#10;// Return 0 on success, -1 on error.&#10;int&#10;copyin(pagetable_t pagetable, uint64 psz, char *dst, uint64 srcva, uint64 len)&#10;{&#10; uint64 n, va0, pa0;&#10;&#10; while (len &amp;gt; 0) {&#10; va0 = PGROUNDDOWN(srcva);&#10; pa0 = walkaddr(pagetable, va0);&#10; if (pa0 == 0) {&#10; if ((pa0 = vmfault(pagetable, psz, va0, 1)) == 0) {&#10; return -1;&#10; }&#10; }&#10; n = PGSIZE - (srcva - va0);&#10; if (n &amp;gt; len)&#10; n = len;&#10; memmove(dst, (void *)(pa0 &amp;#43; (srcva - va0)), n);&#10;&#10; len -= n;&#10; dst &amp;#43;= n;&#10; srcva = va0 &amp;#43; PGSIZE;&#10; }&#10; return 0;&#10;}&#10;&#10;// Copy from kernel to user.&#10;// Copy len bytes from src to virtual address dstva in a given page table.&#10;// Return 0 on success, -1 on error.&#10;int&#10;copyout(pagetable_t pagetable, uint64 psz, uint64 dstva, char *src, uint64 len)&#10;{&#10; uint64 n, va0, pa0;&#10; pte_t *pte;&#10;&#10; while (len &amp;gt; 0) {&#10; va0 = PGROUNDDOWN(dstva);&#10; if (va0 &amp;gt;= MAXVA)&#10; return -1;&#10;&#10; pa0 = walkaddr(pagetable, va0);&#10; if (pa0 == 0) {&#10; if ((pa0 = vmfault(pagetable, psz, va0, 0)) == 0) {&#10; return -1;&#10; }&#10; }&#10;&#10; if ((pte = walk(pagetable, va0, 0)) == 0) {&#10; // printk(&amp;#34;copyout: pte should exist %lx %ld\n&amp;#34;, dstva, len);&#10; return -1;&#10; }&#10;&#10; // forbid copyout over read-only user text pages.&#10; if ((*pte &amp;amp; PTE_W) == 0)&#10; return -1;&#10;&#10; n = PGSIZE - (dstva - va0);&#10; if (n &amp;gt; len)&#10; n = len;&#10; memmove((void *)(pa0 &amp;#43; (dstva - va0)), src, n);&#10;&#10; len -= n;&#10; src &amp;#43;= n;&#10; dstva = va0 &amp;#43; PGSIZE;&#10; }&#10; return 0;&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;其实核心就是通过 &lt;code&gt;walkaddr&lt;/code&gt; 函数手动地把 CPU 访问虚拟内存得到物理内存的工作做一遍，这里的&lt;strong&gt;in&lt;/strong&gt;和&lt;strong&gt;out&lt;/strong&gt;都是相较于内核而言&lt;/p&gt;&#10;&lt;div style="padding:12px 16px;margin:10px 0;border:1px solid rgba(148,163,184,.3);border-left:4px solid #1d6fd0;border-radius:10px;background:rgba(29,111,208,.05);"&gt;&lt;div style="font-family:ui-monospace,Menlo,monospace;font-size:13px;font-weight:700;color:#1d6fd0;margin-bottom:6px;"&gt;ℹ 提示&lt;/div&gt;&lt;div style="font-size:14px;line-height:1.7;"&gt;&#10;&lt;code&gt;walk&lt;/code&gt;和&lt;code&gt;walkaddr&lt;/code&gt;大差不差，区别在于&lt;code&gt;walk&lt;/code&gt;返回的是指向叶子PTE的内核虚拟地址，&lt;code&gt;walkaddr&lt;/code&gt;返回的是传入虚拟地址通过页表找到的物理地址(但不带偏移量)&#10;&lt;/div&gt;&lt;/div&gt;&#10;&lt;h2 class="heading-element" id="最后"&gt;&lt;span&gt;最后&lt;/span&gt;&#10; &lt;a href="#%e6%9c%80%e5%90%8e" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;虚拟内存机制是操作系统的重要机制，虚拟内存给进程提供连续、独立的虚拟地址空间，同时，物理页的申请由内核管理，保障了系统的安全。由于每个进程间虚拟内存的独立，保障了物理内存访问的安全性，也有利于实现进程间隔离。&lt;/p&gt;&#10;&lt;p&gt;当然，虚拟内存并不只是软件的设计，更有 CPU 硬件的约束。在 CPU 中，&#10;&lt;strong&gt;MMU（Memory Management Unit）&lt;/strong&gt; 作为硬件模块，遵循特定的索引访问机制。更确切地说，内核负责建立和维护页表、管理物理页框和映射策略；MMU 按硬件规定的页表格式和遍历规则完成地址转换与权限检查；缺页时 MMU 触发异常，由内核处理。&lt;/p&gt;&#10;</description></item><item><title>xv6 Lab System calls</title><link>https://yuqu419.github.io/posts/xv6-lab-syscalls/</link><pubDate>Fri, 13 Nov 2026 14:48:13 +0800</pubDate><guid>https://yuqu419.github.io/posts/xv6-lab-syscalls/</guid><category domain="https://yuqu419.github.io/categories/%E5%AD%A6%E4%B9%A0/">学习</category><description>&lt;div style="max-width: 620px; margin: 56px auto; font-family: 'Noto Serif SC','Songti SC',Georgia,serif; color: light-dark(#2b2b2b, #d1d5db); line-height: 2;"&gt;&#10;&lt;p style="font-size: 11px; letter-spacing: .5em; text-transform: uppercase; color: light-dark(#888, #9aa3b2); margin: 0 0 24px;"&gt;Preface&lt;/p&gt;&#10;&lt;div style="width: 36px; height: 1px; background: light-dark(#111, #e1e6ed); margin-bottom: 34px;"&gt;&lt;/div&gt;&#10;&lt;p style="font-size: 15px; text-align: justify; text-justify: inter-ideograph; margin: 0 0 18px;"&gt;&#10;&lt;p&gt;系统调用的流程到底是怎样的？&lt;/p&gt;&#10;&lt;/p&gt;&#10;&lt;p style="font-size: 13px; color: light-dark(#888, #9aa3b2); letter-spacing: .1em; text-align: right; margin: 40px 0 0;"&gt;— 2026/11/13&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;h1 class="heading-element" id="lab-system-calls"&gt;&lt;span&gt;Lab System calls&lt;/span&gt;&#10; &lt;a href="#lab-system-calls" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;本 Lab 主要以完成 &lt;code&gt;interpose&lt;/code&gt; 为例，让读者了解 xv6 中系统调用的全流程&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="用户接口"&gt;&lt;span&gt;用户接口&lt;/span&gt;&#10; &lt;a href="#%e7%94%a8%e6%88%b7%e6%8e%a5%e5%8f%a3" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;以 interpose 为例子&lt;/p&gt;&#10;&lt;p&gt;我们会在用户层提供接口 &lt;code&gt;interpose(int syscall,char* name)&lt;/code&gt;，&#10;这个接口的由汇编完成实现，汇编代码由 &lt;code&gt;usys.pl&lt;/code&gt; 自动生成，我们只需要在 &lt;code&gt;usys.pl&lt;/code&gt; 中添加 &lt;code&gt;entry(&amp;quot;interpose&amp;quot;)&lt;/code&gt; 即可&#10;生成的汇编如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;.global interpose # 外部链接interpose，表示对interpose函数的实现&#10;interpose:&#10; li a7, SYS_interpose # li表示load immediate，加载立即数到a7寄存器中&#10; ecall # 触发系统调用&#10; ret # 返回&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;需要说明的是，&lt;code&gt;ecall&lt;/code&gt; 就相当于 x86 中的 &lt;code&gt;syscall&lt;/code&gt;，而系统调用号是被放在 a7 中。&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="risc-v"&gt;&lt;span&gt;RISC-V&lt;/span&gt;&#10; &lt;a href="#risc-v" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;后续会涉及到 RISC-V 汇编知识，让我们简单了解一下相关知识&#10;寄存器&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;zero&lt;/strong&gt;&lt;/u&gt; 硬接线为 0，表示值恒为 0&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;ra&lt;/strong&gt;&lt;/u&gt; Return Address 用于存放返回地址&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;sp&lt;/strong&gt;&lt;/u&gt; Stack Pointer 栈指针&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;a0~a7&lt;/strong&gt;&lt;/u&gt; 存放整数参数，多余参数由调用者压栈，返回值保存在 a0&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;在 &lt;code&gt;ecall&lt;/code&gt; 时，a0~a5 用于从存放参数，a7 用于放系统调用号，返回值存在 a0&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;RISC-V 与 x86 一个显著的区别就是有个特殊的 ra 寄存器存放返回地址，当一个函数调用另一个函数时，会将 &lt;code&gt;ra&lt;/code&gt; 保存起来&lt;/p&gt;&#10;&lt;p&gt;本 lab 关于 RISC-V 学习到这儿就足够了&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="syscall"&gt;&lt;span&gt;syscall&lt;/span&gt;&#10; &lt;a href="#syscall" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;当使用 &lt;code&gt;ecall&lt;/code&gt; 之后会发生什么呢？&lt;/p&gt;&#10;&lt;p&gt;ecall 属于 trap 的一种，主要分为四个步骤&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="cpu-硬件执行原子操作"&gt;&lt;span&gt;CPU 硬件执行原子操作&lt;/span&gt;&#10; &lt;a href="#cpu-%e7%a1%ac%e4%bb%b6%e6%89%a7%e8%a1%8c%e5%8e%9f%e5%ad%90%e6%93%8d%e4%bd%9c" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;CPU 做的事情主要是更改 pc，改变硬件控制寄存器（Hardware control registers）和改变特权级，硬件控制寄存器有许多个&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;将 &lt;code&gt;pc&lt;/code&gt; 中的值保存到 &lt;code&gt;sepc&lt;/code&gt;（Supervisor Exception Program Counter），保存函数调用后能返回到正确的位置&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;记录 trap 的原因，在 &lt;code&gt;scause&lt;/code&gt;（Supervisor Cause）写入一个数字，表明 trap 原因，因为 trap 不仅限于系统调用&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;关闭中断，改变 &lt;code&gt;sstatus&lt;/code&gt;（Supervisor Status），禁用中断，防止处理系统调用时被打断&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;在 &lt;code&gt;sstatus&lt;/code&gt; 中写入当前特权级，以便返回时后续该回到哪个权级&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;改变特权级，将当前 CPU 的特权级设置为 Supervisor mode&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;跳转 将 pc 设置为 &lt;code&gt;stvec&lt;/code&gt; 寄存器中预先配置好的地址，这个地址式 Trap handler 的入口&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h3 class="heading-element" id="汇编层的软件处理"&gt;&lt;span&gt;汇编层的软件处理&lt;/span&gt;&#10; &lt;a href="#%e6%b1%87%e7%bc%96%e5%b1%82%e7%9a%84%e8%bd%af%e4%bb%b6%e5%a4%84%e7%90%86" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;&lt;code&gt;stvec&lt;/code&gt; 中的跳转地址已被内核修改为 &lt;code&gt;uservec&lt;/code&gt; 的地址，&lt;code&gt;uservec&lt;/code&gt; 位于 &lt;code&gt;trampoline.S&lt;/code&gt; 文件中&#10;实现如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;uservec: &#10;&#10; # trap.c sets stvec to point here, so&#10; # traps from user space start here,&#10; # in supervisor mode, but with a&#10; # user page table.&#10; #&#10;&#10; # save user a0 in sscratch so&#10; # a0 can be used to get at TRAPFRAME.&#10; csrw sscratch, a0&#10;&#10; # each process has a separate p-&amp;gt;trapframe memory area,&#10; # but it&amp;#39;s mapped to the same virtual address&#10; # (TRAPFRAME) in every process&amp;#39;s user page table.&#10; li a0, TRAPFRAME&#10; &#10; # save the user registers in TRAPFRAME&#10; sd ra, 40(a0)&#10; sd sp, 48(a0)&#10; sd gp, 56(a0)&#10; sd tp, 64(a0)&#10; sd t0, 72(a0)&#10; sd t1, 80(a0)&#10; sd t2, 88(a0)&#10; sd s0, 96(a0)&#10; sd s1, 104(a0)&#10; sd a1, 120(a0)&#10; sd a2, 128(a0)&#10; sd a3, 136(a0)&#10; sd a4, 144(a0)&#10; sd a5, 152(a0)&#10; sd a6, 160(a0)&#10; sd a7, 168(a0)&#10; sd s2, 176(a0)&#10; sd s3, 184(a0)&#10; sd s4, 192(a0)&#10; sd s5, 200(a0)&#10; sd s6, 208(a0)&#10; sd s7, 216(a0)&#10; sd s8, 224(a0)&#10; sd s9, 232(a0)&#10; sd s10, 240(a0)&#10; sd s11, 248(a0)&#10; sd t3, 256(a0)&#10; sd t4, 264(a0)&#10; sd t5, 272(a0)&#10; sd t6, 280(a0)&#10;&#10;&#9;# save the user a0 in p-&amp;gt;trapframe-&amp;gt;a0&#10; csrr t0, sscratch&#10; sd t0, 112(a0)&#10;&#10; # initialize kernel stack pointer, from p-&amp;gt;trapframe-&amp;gt;kernel_sp&#10; ld sp, 8(a0)&#10;&#10; # make tp hold the current hartid, from p-&amp;gt;trapframe-&amp;gt;kernel_hartid&#10; ld tp, 32(a0)&#10;&#10; # load the address of usertrap(), from p-&amp;gt;trapframe-&amp;gt;kernel_trap&#10; ld t0, 16(a0)&#10;&#10; # fetch the kernel page table address, from p-&amp;gt;trapframe-&amp;gt;kernel_satp.&#10; ld t1, 0(a0)&#10;&#10; # wait for any previous memory operations to complete, so that&#10; # they use the user page table.&#10; sfence.vma zero, zero&#10;&#10; # install the kernel page table.&#10; csrw satp, t1&#10;&#10; # flush now-stale user entries from the TLB.&#10; sfence.vma zero, zero&#10;&#10; # call usertrap()&#10; jalr t0&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;trapframe&lt;/code&gt; 实现&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;struct trapframe {&#10; /* 0 */ uint64 kernel_satp; // kernel page table&#10; /* 8 */ uint64 kernel_sp; // top of process&amp;#39;s kernel stack&#10; /* 16 */ uint64 kernel_trap; // usertrap()&#10; /* 24 */ uint64 epc; // saved user program counter&#10; /* 32 */ uint64 kernel_hartid; // saved kernel tp&#10; /* 40 */ uint64 ra;&#10; /* 48 */ uint64 sp;&#10; /* 56 */ uint64 gp;&#10; /* 64 */ uint64 tp;&#10; /* 72 */ uint64 t0;&#10; /* 80 */ uint64 t1;&#10; /* 88 */ uint64 t2;&#10; /* 96 */ uint64 s0;&#10; /* 104 */ uint64 s1;&#10; /* 112 */ uint64 a0;&#10; /* 120 */ uint64 a1;&#10; /* 128 */ uint64 a2;&#10; /* 136 */ uint64 a3;&#10; /* 144 */ uint64 a4;&#10; /* 152 */ uint64 a5;&#10; /* 160 */ uint64 a6;&#10; /* 168 */ uint64 a7;&#10; /* 176 */ uint64 s2;&#10; /* 184 */ uint64 s3;&#10; /* 192 */ uint64 s4;&#10; /* 200 */ uint64 s5;&#10; /* 208 */ uint64 s6;&#10; /* 216 */ uint64 s7;&#10; /* 224 */ uint64 s8;&#10; /* 232 */ uint64 s9;&#10; /* 240 */ uint64 s10;&#10; /* 248 */ uint64 s11;&#10; /* 256 */ uint64 t3;&#10; /* 264 */ uint64 t4;&#10; /* 272 */ uint64 t5;&#10; /* 280 */ uint64 t6;&#10;};&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;sd&lt;/code&gt;（Store Doubleword）表示将寄存器中值存到指定内存中去&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;uservec&lt;/code&gt; 所做的事情：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;保存用户寄存器到 &lt;code&gt;trapframe&lt;/code&gt; 中&lt;/strong&gt; &lt;code&gt;trapframe&lt;/code&gt; 是进程虚拟内存空间的一个特殊区域，专门保存 trap 时的用户寄存器&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;切换到内核栈&lt;/strong&gt; 将 &lt;code&gt;sp&lt;/code&gt; 设置为当前进程的内核栈&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;内核栈不同于用户栈，用户栈是在当前进程的虚拟地址空间当中。每个进程都有自己的内核栈，但内核栈属于内核虚拟地址空间，内核地址空间有所有进程共享。在 &lt;code&gt;proc&lt;/code&gt; 中 &lt;code&gt;kstack&lt;/code&gt; 字段专门存放内核栈的虚拟地址&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;切换内核页表&lt;/strong&gt; 将 &lt;code&gt;satp&lt;/code&gt; 设置为内核页表的地址&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;内核页表是全局变量，定义在 &lt;code&gt;vm.c&lt;/code&gt; 中&#10;&lt;code&gt;pagetable_t kernel_pagetable;&lt;/code&gt;&#10;这个页表映射内核代码，内核数据，每个进程的 &lt;code&gt;trapframe&lt;/code&gt; 等等。每个进程都有独立的内核栈，但都映射到同一个内核页表中&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;跳转到 C 代码&lt;/strong&gt; 从 &lt;code&gt;uservec&lt;/code&gt; 跳转到 &lt;code&gt;usertrap()&lt;/code&gt; 函数&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 class="heading-element" id="c-的软件处理"&gt;&lt;span&gt;C 的软件处理&lt;/span&gt;&#10; &lt;a href="#c-%e7%9a%84%e8%bd%af%e4%bb%b6%e5%a4%84%e7%90%86" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;&lt;code&gt;usertrap()&lt;/code&gt; 是内核中处理用户态 Trap 的核心 C 函数，位于 &lt;code&gt;kernel/trap.c&lt;/code&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;uint64 usertrap(void)&#10;{&#10; int which_dev = 0;&#10;&#10; if ((r_sstatus() &amp;amp; SSTATUS_SPP) != 0)&#10; panic(&amp;#34;usertrap: not from user mode&amp;#34;);&#10;&#10; // send interrupts and exceptions to kerneltrap(),&#10; // since we&amp;#39;re now in the kernel.&#10; w_stvec((uint64)kernelvec); //DOC: kernelvec&#10;&#10; struct proc *p = myproc();&#10;&#10; // save user program counter.&#10; p-&amp;gt;trapframe-&amp;gt;epc = r_sepc();&#10;&#10; if (r_scause() == 8) {&#10; // system call&#10;&#10; if (killed(p))&#10; kexit(-1);&#10;&#10; // sepc points to the ecall instruction,&#10; // but we want to return to the next instruction.&#10; p-&amp;gt;trapframe-&amp;gt;epc &amp;#43;= 4;&#10;&#10; // an interrupt will change sepc, scause, and sstatus,&#10; // so enable only now that we&amp;#39;re done with those registers.&#10; intr_on();&#10;&#10; syscall();&#10; } else if ((which_dev = devintr()) != 0) {&#10; // ok&#10; } else if ((r_scause() == 15 || r_scause() == 13) &amp;amp;&amp;amp;&#10; vmfault(p-&amp;gt;pagetable, p-&amp;gt;sz, r_stval(),&#10; (r_scause() == 13) ? 1 : 0) != 0) {&#10; // page fault on lazily-allocated page&#10; } else {&#10; printk(&amp;#34;usertrap(): unexpected scause 0x%lx pid=%d\n&amp;#34;, r_scause(), p-&amp;gt;pid);&#10; printk(&amp;#34; sepc=0x%lx stval=0x%lx\n&amp;#34;, r_sepc(), r_stval());&#10; setkilled(p);&#10; }&#10;&#10; if (killed(p))&#10; kexit(-1);&#10;&#10; // give up the CPU if this is a timer interrupt.&#10; if (which_dev == 2)&#10; yield();&#10;&#10; prepare_return();&#10;&#10; // the user page table to switch to, for trampoline.S&#10; uint64 satp = MAKE_SATP(p-&amp;gt;pagetable);&#10;&#10; // return to trampoline.S; satp value in a0.&#10; return satp;&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;usertrap&lt;/code&gt; 所做的事情&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;将 &lt;code&gt;sepc&lt;/code&gt; 中的返回地址保存到 &lt;code&gt;trapframe&lt;/code&gt;&lt;/strong&gt; 不依赖寄存器长期保存地址。此外若为系统调用，还要将 &lt;code&gt;epc += 4&lt;/code&gt;，这是因为，&lt;code&gt;sepc&lt;/code&gt; 存的是 &lt;code&gt;ecall&lt;/code&gt; 之前 &lt;code&gt;pc&lt;/code&gt; 中的值，&lt;code&gt;pc&lt;/code&gt; 保存下一条指令的地址，也就是 &lt;code&gt;ecall&lt;/code&gt;，但我们想要的是系统调用返回后执行 &lt;code&gt;ecall&lt;/code&gt; 的下一条指令，因此偏移 4 个字节&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;至于为什么是加 4，而不是加 8，这是因为 &lt;code&gt;ecall&lt;/code&gt; 指令占 4 个字节&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;重启系统中断&lt;/strong&gt; 调用 &lt;code&gt;intr_on()&lt;/code&gt; 函数重启中断&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;分发处理&lt;/strong&gt; 依据 &lt;code&gt;scause&lt;/code&gt; 中的值判断 trap 的原因，并进行相应处理，对于系统调用，并调用 &lt;code&gt;syscall&lt;/code&gt; 函数&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;除了系统调用，还可能是设备中断（Device Interrupt）或者异常（Exception），Expcetion 时，usertrap 会打印错误信息（包括 scause,sepc,stval 等）&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h3 class="heading-element" id="syscall-分发系统调用"&gt;&lt;span&gt;&lt;code&gt;syscall&lt;/code&gt; 分发系统调用&lt;/span&gt;&#10; &lt;a href="#syscall-%e5%88%86%e5%8f%91%e7%b3%bb%e7%bb%9f%e8%b0%83%e7%94%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;&lt;code&gt;syscall()&lt;/code&gt; 位于 &lt;code&gt;kernel/syscall.c&lt;/code&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;void syscall(void)&#10;{&#10; int num;&#10; struct proc *p = myproc();&#10;&#10; num = p-&amp;gt;trapframe-&amp;gt;a7;&#10; if (num &amp;gt; 0 &amp;amp;&amp;amp; num &amp;lt; NELEM(syscalls) &amp;amp;&amp;amp; syscalls[num]) {&#10; // Use num to lookup the system call function for num, call it,&#10; // and store its return value in p-&amp;gt;trapframe-&amp;gt;a0&#10; p-&amp;gt;trapframe-&amp;gt;a0 = syscalls[num]();&#10; } else {&#10; printk(&amp;#34;%d %s: unknown sys call %d\n&amp;#34;, p-&amp;gt;pid, p-&amp;gt;name, num);&#10; p-&amp;gt;trapframe-&amp;gt;a0 = -1;&#10; }&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;其功能显而易见，读取 &lt;code&gt;a7&lt;/code&gt; 中系统调用号，分发系统调用，并保存返回值到 &lt;code&gt;a0&lt;/code&gt; 寄存器中&lt;/p&gt;&#10;&lt;p&gt;那么系统调用处理函数怎么工作呢？&lt;/p&gt;&#10;&lt;p&gt;在开始是我们在用户层定义了 &lt;code&gt;interpose&lt;/code&gt; 函数接口&lt;/p&gt;&#10;&lt;p&gt;在调用 &lt;code&gt;interpose&lt;/code&gt; 接口函数时，有两个参数被传入，根据函数调用约定，这个两个参数被存放在寄存器 &lt;code&gt;a0&lt;/code&gt; 和 &lt;code&gt;a1&lt;/code&gt; 中，然后这两个参数在汇编处理阶段被存入 &lt;code&gt;trapframe&lt;/code&gt; 中，所以在具体实现中，我们需要去 &lt;code&gt;trapframe&lt;/code&gt; 中找这两个参数&lt;/p&gt;&#10;&lt;p&gt;好消息是，在 &lt;code&gt;kernel/syscall.c&lt;/code&gt; 为我们提供了相关接口获取参数 &lt;code&gt;argraw&lt;/code&gt; 极其衍生函数，如 &lt;code&gt;argint&lt;/code&gt; 和 &lt;code&gt;argstr&lt;/code&gt;&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="返回用户态"&gt;&lt;span&gt;返回用户态&lt;/span&gt;&#10; &lt;a href="#%e8%bf%94%e5%9b%9e%e7%94%a8%e6%88%b7%e6%80%81" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;在 &lt;code&gt;syscall&lt;/code&gt; 函数完毕后，进入 &lt;code&gt;prepare_return()&lt;/code&gt; 函数&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;void prepare_return(void)&#10;{&#10; struct proc *p = myproc();&#10;&#10; // we&amp;#39;re about to switch the destination of traps from&#10; // kerneltrap() to usertrap(). because a trap from kernel&#10; // code to usertrap would be a disaster, turn off interrupts.&#10; intr_off();&#10;&#10; // send syscalls, interrupts, and exceptions to uservec in trampoline.S&#10; uint64 trampoline_uservec = TRAMPOLINE &amp;#43; (uservec - trampoline);&#10; w_stvec(trampoline_uservec);&#10;&#10; // set up trapframe values that uservec will need when&#10; // the process next traps into the kernel.&#10; p-&amp;gt;trapframe-&amp;gt;kernel_satp = r_satp(); // kernel page table&#10; p-&amp;gt;trapframe-&amp;gt;kernel_sp = p-&amp;gt;kstack &amp;#43; PGSIZE; // process&amp;#39;s kernel stack&#10; p-&amp;gt;trapframe-&amp;gt;kernel_trap = (uint64)usertrap;&#10; p-&amp;gt;trapframe-&amp;gt;kernel_hartid = r_tp(); // hartid for cpuid()&#10;&#10; // set up the registers that trampoline.S&amp;#39;s sret will use&#10; // to get to user space.&#10;&#10; // set S Previous Privilege mode to User.&#10; unsigned long x = r_sstatus();&#10; x &amp;amp;= ~SSTATUS_SPP; // clear SPP to 0 for user mode&#10; x |= SSTATUS_SPIE; // enable interrupts in user mode&#10; w_sstatus(x);&#10;&#10; // set S Exception Program Counter to the saved user pc.&#10; w_sepc(p-&amp;gt;trapframe-&amp;gt;epc);&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;prepare&lt;/code&gt; 重新关闭中断；修改 &lt;code&gt;stvec&lt;/code&gt; 为 trap 入口，保证下次 trap 时正常进入 &lt;code&gt;uservec&lt;/code&gt;；调整 &lt;code&gt;trapframe&lt;/code&gt; 的内核信息；修改 &lt;code&gt;sstatus&lt;/code&gt;。最后在 &lt;code&gt;usertrap&lt;/code&gt; 中返回 &lt;code&gt;satp&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;usertrap&lt;/code&gt; 返回在 &lt;code&gt;userret&lt;/code&gt; 中&#10;&lt;code&gt;userret&lt;/code&gt; 实现如下&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;userret:&#10; # usertrap() returns here, with user satp in a0.&#10; # return from kernel to user.&#10;&#10; # flush icache, in case this is the first time&#10; # we&amp;#39;re running this proc on this hart.&#10; fence.i&#10;&#10; # switch to the user page table.&#10; sfence.vma zero, zero&#10; csrw satp, a0&#10; sfence.vma zero, zero&#10;&#10; li a0, TRAPFRAME&#10;&#10; # restore all but a0 from TRAPFRAME&#10; ld ra, 40(a0)&#10; ld sp, 48(a0)&#10; ld gp, 56(a0)&#10; ld tp, 64(a0)&#10; ld t0, 72(a0)&#10; ld t1, 80(a0)&#10; ld t2, 88(a0)&#10; ld s0, 96(a0)&#10; ld s1, 104(a0)&#10; ld a1, 120(a0)&#10; ld a2, 128(a0)&#10; ld a3, 136(a0)&#10; ld a4, 144(a0)&#10; ld a5, 152(a0)&#10; ld a6, 160(a0)&#10; ld a7, 168(a0)&#10; ld s2, 176(a0)&#10; ld s3, 184(a0)&#10; ld s4, 192(a0)&#10; ld s5, 200(a0)&#10; ld s6, 208(a0)&#10; ld s7, 216(a0)&#10; ld s8, 224(a0)&#10; ld s9, 232(a0)&#10; ld s10, 240(a0)&#10; ld s11, 248(a0)&#10; ld t3, 256(a0)&#10; ld t4, 264(a0)&#10; ld t5, 272(a0)&#10; ld t6, 280(a0)&#10;&#10;&#9;# restore user a0&#10; ld a0, 112(a0)&#10; &#10; # return to user mode and user pc.&#10; # prepare_return() sets up sstatus and sepc.&#10; sret&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;基本就是在将 trapframe 中的值重新写入寄存器中，最后 &lt;code&gt;sret&lt;/code&gt; 返回，硬件自动将特权级切换至 User Mode.&lt;/p&gt;&#10;&lt;p&gt;整体调用链：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;flowchart LR&#10; A[&amp;#34;CPU硬件处理&amp;lt;br/&amp;gt;ecall 触发 trap&amp;#34;] --&amp;gt; B[&amp;#34;uservec&amp;lt;br/&amp;gt;trampoline.S&amp;#34;]&#10; B --&amp;gt; C[&amp;#34;usertrap&amp;lt;br/&amp;gt;trap.c&amp;#34;]&#10; C --&amp;gt; D[&amp;#34;syscall&amp;lt;br/&amp;gt;syscall.c&amp;#34;]&#10; D --&amp;gt; E[&amp;#34;prepare_return&amp;lt;br/&amp;gt;trap.c&amp;#34;]&#10; E --&amp;gt; F[&amp;#34;userret&amp;lt;br/&amp;gt;trampoline.S&amp;#34;]&#10; F --&amp;gt; G[&amp;#34;返回用户态继续执行&amp;#34;]&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;这便是系统调用的全流程&lt;/p&gt;&#10;</description></item><item><title>xv6 chapter2</title><link>https://yuqu419.github.io/posts/xv6-chapter-2/</link><pubDate>Thu, 12 Nov 2026 15:00:13 +0800</pubDate><guid>https://yuqu419.github.io/posts/xv6-chapter-2/</guid><category domain="https://yuqu419.github.io/categories/%E5%AD%A6%E4%B9%A0/">学习</category><description>&lt;div style="max-width: 620px; margin: 56px auto; font-family: 'Noto Serif SC','Songti SC',Georgia,serif; color: light-dark(#2b2b2b, #d1d5db); line-height: 2;"&gt;&#10;&lt;p style="font-size: 11px; letter-spacing: .5em; text-transform: uppercase; color: light-dark(#888, #9aa3b2); margin: 0 0 24px;"&gt;Preface&lt;/p&gt;&#10;&lt;div style="width: 36px; height: 1px; background: light-dark(#111, #e1e6ed); margin-bottom: 34px;"&gt;&lt;/div&gt;&#10;&lt;p style="font-size: 15px; text-align: justify; text-justify: inter-ideograph; margin: 0 0 18px;"&gt;&#10;&lt;p&gt;The operating system will help you manage your programs.&lt;/p&gt;&#10;&lt;/p&gt;&#10;&lt;p style="font-size: 13px; color: light-dark(#888, #9aa3b2); letter-spacing: .1em; text-align: right; margin: 40px 0 0;"&gt;— 2026/11/11&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;h1 class="heading-element" id="operating-system-organization"&gt;&lt;span&gt;Operating system organization&lt;/span&gt;&#10; &lt;a href="#operating-system-organization" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;An operating system should satisfy three requirements:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;multiplexing&#10;The operating system must support several activities at once,sharing resources such as CUPs and memory among them.&lt;/li&gt;&#10;&lt;li&gt;isolation&#10;The operating system should ensure that when a process occurs bugs,it won&amp;rsquo;t affect other processes.&lt;/li&gt;&#10;&lt;li&gt;interaction&#10;Different processes need to interact,such as pipelines.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 class="heading-element" id="why-operating-systems"&gt;&lt;span&gt;Why operating systems?&lt;/span&gt;&#10; &lt;a href="#why-operating-systems" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ul&gt;&#10;&lt;li&gt;achieve strong ioslation.Avoid the influence among processes.&lt;/li&gt;&#10;&lt;li&gt;provide abstract resources and interface such as &lt;code&gt;open&lt;/code&gt;,&lt;code&gt;read&lt;/code&gt;and &lt;code&gt;write&lt;/code&gt;,so that operating system take the responsibility of manage the disk.&lt;/li&gt;&#10;&lt;li&gt;transparently switches CPUs among process.Save the state of registers.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="user-modesupervisor-mode-and-system-calls"&gt;&lt;span&gt;User mode,supervisor mode and system calls&lt;/span&gt;&#10; &lt;a href="#user-modesupervisor-mode-and-system-calls" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;To achieve string isolation,CPUs provide hardware support.RISC-V has three privilege levels which constrain what code can do: &lt;strong&gt;machine mode&lt;/strong&gt;, &lt;strong&gt;supervisor mode&lt;/strong&gt;, &lt;strong&gt;user mode&lt;/strong&gt;.&lt;/p&gt;&#10;&lt;p&gt;machine mode has full privilege,which is mostly intended for setting up the computer during boot.Xv6 executes briefly in machine mode and then changes to supervisor mode.&lt;/p&gt;&#10;&lt;p&gt;In supervisor mode,the CPU is allowed ti execute privileged instructions such as system calls.If in user mode,CPU doesn&amp;rsquo;t execute the priviledge instruction,but &amp;ldquo;traps&amp;rdquo; to special code in supervisor mode.&lt;/p&gt;&#10;&lt;p&gt;An application can execute only user-mode instructions and is said to be running in &lt;em&gt;user space&lt;/em&gt;,while the software in supervisor mode can also execute privileged instructions and is said to be running in &lt;em&gt;kernel space&lt;/em&gt;.The software running in kernel space is called the &lt;em&gt;kernel&lt;/em&gt;&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="xv6-organization"&gt;&lt;span&gt;xv6 organization&lt;/span&gt;&#10; &lt;a href="#xv6-organization" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;monolithic kernel&lt;/strong&gt;&lt;/u&gt; The entire operating system resides in the kernel space.&#10;&lt;u&gt;**microkernel&lt;/u&gt; Parts of operating system retains in the user space.&lt;/p&gt;&#10;&lt;p&gt;xv6 is implemented as a monolithic kernel like Linux and most Unix operating systems.&lt;/p&gt;&#10;&lt;p&gt;The xv6 kernel source is in the &lt;code&gt;kernel/&lt;/code&gt; sub-directory.Files are divided into major areas of kernel responsibility:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;starting the system(booting)&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;creating and controlling process&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;handling traps&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;allocating memory and configuring virtual addresses&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;controlling devices&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;managing the file-system&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;img loading="lazy" src='https://yuqu419.github.io/posts/image-2.png' alt="alt text" decoding="async"&gt;&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="process-overview"&gt;&lt;span&gt;Process overview&lt;/span&gt;&#10; &lt;a href="#process-overview" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;Process is regarded as an instance of program, which provides kits with program.&lt;/p&gt;&#10;&lt;p&gt;Xv6 use page tables(which are implemented by handware) to give each process its own address space.The RISC-V page table translate a &lt;em&gt;virtual address&lt;/em&gt; to &lt;em&gt;physical address&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;Xv6 maintains a seperate page tabe for each process that defines that process&amp;rsquo;s address space.&#10;The layout of virutal address space is like following:&lt;/p&gt;&#10;&lt;p&gt;&lt;img loading="lazy" src='https://yuqu419.github.io/posts/image-3.png' alt="alt text" decoding="async"&gt;&lt;/p&gt;&#10;&lt;p&gt;Instructions -&amp;gt; global variables -&amp;gt; stack -&amp;gt; heap&#10;When looking up virtual addresses in page tables,the handware uses only the low 39 bits;and xv6 uses only 38 of those 39 bits.&lt;/p&gt;&#10;&lt;p&gt;Thus,the maximum address is 0x3fffffffff,which is &lt;code&gt;MAXVA&lt;/code&gt;(maximum virtual address).&lt;/p&gt;&#10;&lt;p&gt;At the top of the address space, xv6 places a &lt;em&gt;trampoline page&lt;/em&gt; and a &lt;em&gt;trapframe page&lt;/em&gt;,which are used to transition into the kernel and back.&lt;/p&gt;&#10;&lt;p&gt;The page contains the code to transition in and out of the kernel, and the trapframe os where the kernel saves the process&amp;rsquo;s user registers.&lt;/p&gt;&#10;&lt;p&gt;The xv6 kernel maintains many pieces of state for each process which is gathered into a &lt;code&gt;struct proc&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;The significant pieces of the &lt;code&gt;proc&lt;/code&gt;:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;page table&lt;/li&gt;&#10;&lt;li&gt;kernel stack&lt;/li&gt;&#10;&lt;li&gt;run state&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Each process has a thread of control that holds the state needed to execute and process.Concretely,to switch a CPU among processes, the kernel suspends the thread currently running on that CPU and save its state and restore the state of another process&amp;rsquo;s prevously-suspended thread.&lt;/p&gt;&#10;&lt;p&gt;Each thread has two stacks: a user stack and a kernel stack.When the process is executing user instructions,only the user stack is in use while the kernel stack is empty;when the process enters the kernel,the kernel code executes on the process&amp;rsquo;s kernel stack;while a process is in the kernel,its user stack still contains saved data but not actively used.And the kernel stack is seperate from the user stack.&lt;/p&gt;&#10;&lt;p&gt;A process&amp;rsquo;s user code can make a system call by executing the RISC-V &lt;code&gt;ecall&lt;/code&gt; instruction.This instruction switches to supervisor mode and changes the program counter to a kernel-defined entry point.&#10;The code at the entry point switches to the kernel stack and executes the code the system call implements.&lt;/p&gt;&#10;&lt;p&gt;When system call completes,the kernel returns to user space by executing the &lt;code&gt;sret&lt;/code&gt; instruction.&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;typedef int pagetable_t&#10;&#10;enum procstate { &#10; UNUSED, &#10; USED, &#10; SLEEPING, &#10; RUNNABLE, &#10; RUNNING, &#10; ZOMBIE &#10;};&#10;&#10;struct proc{&#10; uint64 kstack;//the virtual address of the kernel stack&#10; pagetable_t pagetable;//user page table&#10; enum proctstate state; //the state of the process&#10; /*other fields*/&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;code&gt;state&lt;/code&gt;&lt;/u&gt; indicates the state of the process.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;code&gt;pagetable&lt;/code&gt;&lt;/u&gt; holds the process&amp;rsquo;s page table.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item><item><title>xv6 chapter1</title><link>https://yuqu419.github.io/posts/xv6-chapter-1/</link><pubDate>Wed, 11 Nov 2026 15:24:13 +0800</pubDate><guid>https://yuqu419.github.io/posts/xv6-chapter-1/</guid><category domain="https://yuqu419.github.io/categories/%E5%AD%A6%E4%B9%A0/">学习</category><description>&lt;div style="max-width: 620px; margin: 56px auto; font-family: 'Noto Serif SC','Songti SC',Georgia,serif; color: light-dark(#2b2b2b, #d1d5db); line-height: 2;"&gt;&#10;&lt;p style="font-size: 11px; letter-spacing: .5em; text-transform: uppercase; color: light-dark(#888, #9aa3b2); margin: 0 0 24px;"&gt;Preface&lt;/p&gt;&#10;&lt;div style="width: 36px; height: 1px; background: light-dark(#111, #e1e6ed); margin-bottom: 34px;"&gt;&lt;/div&gt;&#10;&lt;p style="font-size: 15px; text-align: justify; text-justify: inter-ideograph; margin: 0 0 18px;"&gt;&#10;&lt;p&gt;Let us go to the world of xv6.&lt;/p&gt;&#10;&lt;/p&gt;&#10;&lt;p style="font-size: 13px; color: light-dark(#888, #9aa3b2); letter-spacing: .1em; text-align: right; margin: 40px 0 0;"&gt;— 2026/11/11&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;h2 class="heading-element" id="operating-system"&gt;&lt;span&gt;Operating System&lt;/span&gt;&#10; &lt;a href="#operating-system" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;An operating system is designed to manages and abstracts the low-level hardware.&lt;/p&gt;&#10;&lt;p&gt;To keep each process isolated, the kernel use hardware protection mechanisms provided by CPU.When more permissions are needed(the function-like calling is known as system call),the user program invokes a system call,the hardware raises the privilege level&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="process-and-memory"&gt;&lt;span&gt;Process and memory&lt;/span&gt;&#10; &lt;a href="#process-and-memory" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;An xv6 process consists of user-space memory.&lt;/p&gt;&#10;&lt;p&gt;All C library are not allowed to use.Because it depends on the x86/AMD assembly while the xv6 operating system is based on risc-v.&lt;/p&gt;&#10;&lt;p&gt;Luckily,most of useful functions are provided.We can check all functions ,which is able to be called freely,in &lt;code&gt;user/user.h&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;All right.Let us gain an inspection on some of functions provided&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;fork&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;function signature&lt;/strong&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int fork()&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;fork&lt;/code&gt; is used to create a child process which possess a exact copy of the calling process&amp;rsquo;s memory.&lt;code&gt;fork&lt;/code&gt; copy everything of calling process into the new process&amp;rsquo;s memory.&lt;/p&gt;&#10;&lt;p&gt;Just like we know,&lt;code&gt;fork&lt;/code&gt; returns in both original and new processes.In the original process,it returns the new process&amp;rsquo;s PID,while in the new process,it returns zero.If receiving a negative number,it means that fork failed.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;exit&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;function signature&lt;/strong&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int exit(int status)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;exit&lt;/code&gt; system call causes the calling process to stop and release resource such as memory and file descriptor&lt;/p&gt;&#10;&lt;p&gt;When calling &lt;code&gt;exit&lt;/code&gt;,a status argument is reuquired.Convetionally,0 indicates success and 1 stands for failure&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;wait&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;function signature&lt;/strong&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int wait(int* status)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Unlike &lt;code&gt;wait&lt;/code&gt; in Linux,&lt;code&gt;wait&lt;/code&gt; in xv6 is similar to &lt;code&gt;waitpid(-1,&amp;amp;status,0)&lt;/code&gt; in Linux.&lt;/p&gt;&#10;&lt;p&gt;it returns the PID of an exited child process and copies the exit status of the child to the address passed to wait.&lt;/p&gt;&#10;&lt;p&gt;if none of the caller&amp;rsquo;s children has exited,&lt;code&gt;wait&lt;/code&gt; waits for one to do so.If the caller has no children,&lt;code&gt;wait&lt;/code&gt; returns -1 immediately.&lt;/p&gt;&#10;&lt;p&gt;Supposing the caller have no interest in the status of a child,passing 0(also NULL) is allowed.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;exec&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;function signature&lt;/strong&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int exec(char *file, char *argv[])&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Just like the &lt;code&gt;execve&lt;/code&gt; in Linux,&lt;code&gt;exec&lt;/code&gt; system call replaces the calling process&amp;rsquo;s memory with a new memory image loaded from a file stored in the file system.Xv6 operating system uses ELF format as executable program.&lt;/p&gt;&#10;&lt;p&gt;Considering &lt;code&gt;exec&lt;/code&gt; succeeds,it does not return.If it returns,we can ensure &lt;code&gt;exec&lt;/code&gt; has failed&lt;/p&gt;&#10;&lt;p&gt;When calling &lt;code&gt;exec&lt;/code&gt;,two arguments are passed:the path of the executable file and an array of string arguments(the path included).&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 class="heading-element" id="io-and-file-descriptors"&gt;&lt;span&gt;I/O and File descriptors&lt;/span&gt;&#10; &lt;a href="#io-and-file-descriptors" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;&lt;em&gt;file descriptor&lt;/em&gt; is a small integer representing a kernel-managed object that a process may read from or write to.&lt;/p&gt;&#10;&lt;p&gt;To clarify it,a file descriptor is obtained while opening a file,directory,device or by creating a pipe.&lt;/p&gt;&#10;&lt;p&gt;The xv6 kernel uses descriptor as an index into a per-process table.Every process has a private space of file descriptor starting at zero.&lt;/p&gt;&#10;&lt;p&gt;By convention,a process reads file descriptor 0(standard input),writes output to file descriptor 1(standard output),writes error messages to file desciptor 2(standard error).&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;read&lt;/code&gt; and &lt;code&gt;write&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;function signature&lt;/strong&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int read(int fd, char *buf, int n)&#10;int write(int fd, char *buf, int n)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;read&lt;/code&gt; is used to read n bytes at most from the file descriptor and store them into buf.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;NOTICE: &lt;code&gt;read&lt;/code&gt; always not ensure n bytes are read&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;&lt;code&gt;read&lt;/code&gt; returns the number of byte read as a matter of fact.&lt;/p&gt;&#10;&lt;p&gt;So,when calling &lt;code&gt;read&lt;/code&gt;,we always need to use &lt;em&gt;while&lt;/em&gt;,if continuing calling &lt;code&gt;read&lt;/code&gt;,&lt;code&gt;read&lt;/code&gt; reads data from the current file offset.(the offest is stored by &lt;code&gt;struct file&lt;/code&gt;)&lt;/p&gt;&#10;&lt;p&gt;In xv6,&lt;code&gt;read&lt;/code&gt; is not only used to read file but directory.When reading the directory,the files(including directory) are abstracted as a array of &lt;code&gt;dirent&lt;/code&gt;.&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;struct dirent {&#10;&#10; ushort inum;&#10; char name[DIRSIZ] __attribute__((nonstring));&#10;};&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Each &lt;code&gt;read&lt;/code&gt; just reads one single &lt;code&gt;dirent&lt;/code&gt;,so it is a good choice to use &lt;code&gt;while&lt;/code&gt; to read all files in current directory.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;So,what is file descriptor in fact?&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;We can regard a file descriptor as an index of the files kept in kernel.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;In fact, the profile of file is in &lt;code&gt;struct file&lt;/code&gt;,including reference count, flags ,file offset and so on.Check it in &lt;code&gt;linux/fs.h&lt;/code&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;open&lt;/code&gt; and &lt;code&gt;close&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;function signature&lt;/strong&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int open(char *file, int flags)&#10;int close(int fd)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The second parameter of &lt;code&gt;open&lt;/code&gt; is flags,which includes &lt;code&gt;O_RDOLNY&lt;/code&gt;,&lt;code&gt;O_WRONLY&lt;/code&gt;,&lt;code&gt;O_RDWR&lt;/code&gt;,&lt;code&gt;O_CREATE&lt;/code&gt;,&lt;code&gt;O_TRUNC&lt;/code&gt;.They are defined in &lt;code&gt;fcntl.h&lt;/code&gt; and obvious.&lt;/p&gt;&#10;&lt;p&gt;The kernel will choose a smallest usable number as file descriptor.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;close&lt;/code&gt; system call is used to close the file descriptor instead of the file.In a word,We just change the reference count of file.When the count of reference is zero,the &lt;code&gt;struct file&lt;/code&gt; is released.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;dup&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;function signature&lt;/strong&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int dup(int fd)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;dup&lt;/code&gt; is the short for &amp;ldquo;duplicate&amp;rdquo;,which works as duplicating the file descriptor pointing the same file and increasing the count of reference.&lt;/p&gt;&#10;&lt;p&gt;When calling &lt;code&gt;fork&lt;/code&gt;,everything is almost copied to the new process,and the &lt;code&gt;struct file&lt;/code&gt; included.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 class="heading-element" id="pipes"&gt;&lt;span&gt;Pipes&lt;/span&gt;&#10; &lt;a href="#pipes" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;&lt;em&gt;pipe&lt;/em&gt; is small kernel buffer exposed to processes as a pair of file descriptors,one for reading and one for writing.Pipes provide a way for interprocess communication.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;function signature&lt;/strong&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int pipe(int pipefd[2]);&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;pipe&lt;/code&gt; system call initialize a two-element array,one for reading and another for writing.&lt;/p&gt;&#10;&lt;p&gt;Let us have a look an example provided by Linux documentation.&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;&#10;#include &amp;lt;stdlib.h&amp;gt;&#10;#include &amp;lt;string.h&amp;gt;&#10;#include &amp;lt;sys/wait.h&amp;gt;&#10;#include &amp;lt;unistd.h&amp;gt;&#10;&#10;int main(int argc, char *argv[])&#10;{&#10; int pipefd[2];&#10; char buf;&#10; pid_t cpid;&#10;&#10; if (argc != 2) {&#10; fprintf(stderr, &amp;#34;Usage: %s &amp;lt;string&amp;gt;\n&amp;#34;, argv[0]);&#10; exit(EXIT_FAILURE);&#10; }&#10;&#10; if (pipe(pipefd) == -1) {&#10; perror(&amp;#34;pipe&amp;#34;);&#10; exit(EXIT_FAILURE);&#10; }&#10;&#10; cpid = fork();&#10; if (cpid == -1) {&#10; perror(&amp;#34;fork&amp;#34;);&#10; exit(EXIT_FAILURE);&#10; }&#10;&#10; if (cpid == 0) { /* Child reads from pipe */&#10; close(pipefd[1]); /* Close unused write end */&#10;&#10; while (read(pipefd[0], &amp;amp;buf, 1) &amp;gt; 0)&#10; write(STDOUT_FILENO, &amp;amp;buf, 1);&#10;&#10; write(STDOUT_FILENO, &amp;#34;\n&amp;#34;, 1);&#10; close(pipefd[0]);&#10; _exit(EXIT_SUCCESS);&#10;&#10; } else { /* Parent writes argv[1] to pipe */&#10; close(pipefd[0]); /* Close unused read end */&#10; write(pipefd[1], argv[1], strlen(argv[1]));&#10; close(pipefd[1]); /* Reader will see EOF */&#10; wait(NULL); /* Wait for child */&#10; exit(EXIT_SUCCESS);&#10; }&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;How does it work?&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;p&gt;We call &lt;code&gt;pipe&lt;/code&gt; to initialize two file descriptor,one for writing and another for reading.And when forking, the pair of file descriptors are copied.For safety,it is a must to close the useless end(1 indicates writing end ,0 indicates reading end).&lt;/p&gt;&#10;&lt;p&gt;When we call &lt;code&gt;write&lt;/code&gt; on write-end,the data is written into pipe blocked.&lt;/p&gt;&#10;&lt;p&gt;When we call &lt;code&gt;read&lt;/code&gt; on read-end,the data is read into buffer.However, if there is no data in the stream,read will wait for either data to be written or for all file descriptors referring to the write end to be closed.In the latter case,&lt;code&gt;read&lt;/code&gt; will return 0,just as if the EOF had been reached.&lt;/p&gt;&#10;&lt;p&gt;In the xv6 shell &lt;code&gt;|&lt;/code&gt; is regarded as pipelines because it works just like a pipe and pass the output current instruction to next instruction as the input.&lt;/p&gt;&#10;</description></item><item><title>深入理解计算机系统 第三章</title><link>https://yuqu419.github.io/posts/cssapp-chapter-3/</link><pubDate>Mon, 09 Nov 2026 08:35:13 +0800</pubDate><guid>https://yuqu419.github.io/posts/cssapp-chapter-3/</guid><category domain="https://yuqu419.github.io/categories/%E5%AD%A6%E4%B9%A0/">学习</category><description>&lt;div style="max-width: 620px; margin: 56px auto; font-family: 'Noto Serif SC','Songti SC',Georgia,serif; color: light-dark(#2b2b2b, #d1d5db); line-height: 2;"&gt;&#10;&lt;p style="font-size: 11px; letter-spacing: .5em; text-transform: uppercase; color: light-dark(#888, #9aa3b2); margin: 0 0 24px;"&gt;Preface&lt;/p&gt;&#10;&lt;div style="width: 36px; height: 1px; background: light-dark(#111, #e1e6ed); margin-bottom: 34px;"&gt;&lt;/div&gt;&#10;&lt;p style="font-size: 15px; text-align: justify; text-justify: inter-ideograph; margin: 0 0 18px;"&gt;这些笔记写于大一加入凌睿工作室招新期间，更多的是知识的回顾，我将这些笔记记录下来，纪念那段奋斗的时光&lt;/p&gt;&#10;&lt;p style="font-size: 13px; color: light-dark(#888, #9aa3b2); letter-spacing: .1em; text-align: right; margin: 40px 0 0;"&gt;— 2026/11/9&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;h2 class="heading-element" id="栈"&gt;&lt;span&gt;栈&lt;/span&gt;&#10; &lt;a href="#%e6%a0%88" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;h3 class="heading-element" id="栈基础知识"&gt;&lt;span&gt;栈基础知识&lt;/span&gt;&#10; &lt;a href="#%e6%a0%88%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;栈是一种后进先出的数据结构。理解栈帧，有助于跟踪汇编中的函数调用、局部变量与返回过程。&lt;/p&gt;&#10;&lt;p&gt;栈的特点：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;向下增长，也就是从高地址到低地址&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;栈的空间有限，一般只有几 MB，所以当声明过大数组或者深递归时可能会导致爆栈&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;栈顶是低地址，栈底是高地址&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;rbp&lt;/code&gt; 寄存器内存储指向函数栈帧栈底的指针（地址），&lt;code&gt;rsp&lt;/code&gt; 寄存器内存储指向栈顶的指针&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;当在栈中读取数据时，是从低地址向高地址读取&lt;/p&gt;&#10;&lt;p&gt;如 &lt;code&gt;movq (%rsp), %rax&lt;/code&gt;，读取地址 &lt;code&gt;0(%rsp)&lt;/code&gt; 至 &lt;code&gt;7(%rsp)&lt;/code&gt; 的八个字节，写入 &lt;code&gt;rax&lt;/code&gt; 寄存器&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;当调用函数时，一般会创建一个函数栈帧&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;pushq %rbp&#10;movq %rsp, %rbp&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;增长栈：&lt;code&gt;subq $Imme, %rsp&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;回收栈：&lt;code&gt;addq $Imme, %rsp&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;当函数结束，函数栈帧会被回收&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;leave&#10;ret&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;leave&lt;/code&gt; 所做的事情相当于&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;movq %rbp, %rsp&#10;popq %rbp&lt;/code&gt;&lt;/pre&gt;&lt;h3 class="heading-element" id="栈安全"&gt;&lt;span&gt;栈安全&lt;/span&gt;&#10; &lt;a href="#%e6%a0%88%e5%ae%89%e5%85%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;我们知道，每次产生一个函数栈帧时，会在这之前，压返回地址入栈，设想这种情况，假设在改动栈时，将返回地址修改了，那么当返回函数时，&lt;code&gt;rip&lt;/code&gt; 内会被修改为一个无法确定甚至是被恶意修改的地址。这就是缓冲区溢出所带来的巨大风险，为了避免这种风险，在编写代码时尽量不使用不检查边界的函数，如 &lt;code&gt;strcpy&lt;/code&gt;&lt;/p&gt;&#10;&lt;h4 class="heading-element" id="栈保护机制"&gt;&lt;span&gt;&lt;u&gt;&lt;strong&gt;栈保护机制&lt;/strong&gt;&lt;/u&gt;&lt;/span&gt;&#10; &lt;a href="#%e6%a0%88%e4%bf%9d%e6%8a%a4%e6%9c%ba%e5%88%b6" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h4&gt;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;栈随机化&lt;/strong&gt; 每次运行程序时，栈的起始地址随机变化&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Stack Canary（栈金丝雀）&lt;/strong&gt; 栈金丝雀是编译器放在缓冲区和返回地址之间的一个随机值（具体位置有编译器确定），在返回函数前，检查栈金丝雀是否被修改，若被修改，则调用 &lt;code&gt;__stack_chk_fail&lt;/code&gt; 函数，立即终止程序&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;不可执行栈&lt;/strong&gt; 栈被标记为不可执行的，避免攻击者将攻击代码放到栈上执行&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="函数调用"&gt;&lt;span&gt;函数调用&lt;/span&gt;&#10; &lt;a href="#%e5%87%bd%e6%95%b0%e8%b0%83%e7%94%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;函数调用使用 &lt;code&gt;call &amp;lt;func&amp;gt;&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;执行 &lt;code&gt;call&lt;/code&gt; 时发生了以下几件事&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;压栈&lt;/strong&gt; 将 &lt;code&gt;call&lt;/code&gt; 的接下来一条指令压入栈中，为函数执行完毕后继续运行做准备&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;准备进入函数&lt;/strong&gt; 将被调用函数的第一条指令的地址复制到 &lt;code&gt;rip&lt;/code&gt; 中&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;寄存器 &lt;code&gt;rip&lt;/code&gt; 是程序计数器，专门存储下一条指令的内存地址，在介绍寄存器时，详细介绍&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;另外，在使用 &lt;code&gt;call&lt;/code&gt; 指令时，需要对齐，即保证 &lt;code&gt;rsp&lt;/code&gt; 为 16 的整数倍&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;一般而言，函数需要创建自己的栈帧，但是&lt;strong&gt;叶子函数&lt;/strong&gt;则不需要，叶子函数是不调用其他函数的函数&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h3 class="heading-element" id="函数传参"&gt;&lt;span&gt;函数传参&lt;/span&gt;&#10; &lt;a href="#%e5%87%bd%e6%95%b0%e4%bc%a0%e5%8f%82" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;前六个参数分别存储在六个寄存器中：&lt;code&gt;rdi&lt;/code&gt;，&lt;code&gt;rsi&lt;/code&gt;，&lt;code&gt;rdx&lt;/code&gt;，&lt;code&gt;rcx&lt;/code&gt;，&lt;code&gt;r8&lt;/code&gt;，&lt;code&gt;r9&lt;/code&gt;，当然，这六个寄存器只能存储整数和内存地址。更多参数存储在栈中，规则是按照从右往左的顺序压栈，这样就可以从左往右出栈&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;除此以外，还有一些其他规则。当传浮点数时，使用 8 个 SSE 寄存器（xmm0~xmm7），这种寄存器可存储 16 字节&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;所以说，并不是说函数传参超过 6 个就会压栈，而是要更根据参数参数的具体类型决定&lt;/p&gt;&#10;&lt;h4 class="heading-element" id="可变参数"&gt;&lt;span&gt;可变参数&lt;/span&gt;&#10; &lt;a href="#%e5%8f%af%e5%8f%98%e5%8f%82%e6%95%b0" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h4&gt;&lt;p&gt;当使用可变参数时，还需设置 &lt;code&gt;al&lt;/code&gt; 寄存器来表明用了多少个浮点数以优化速度&lt;/p&gt;&#10;&lt;p&gt;可变参数在返回地址的上方，callee 函数只负责读取，而 caller 函数需要负责清理&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="函数返回"&gt;&lt;span&gt;函数返回&lt;/span&gt;&#10; &lt;a href="#%e5%87%bd%e6%95%b0%e8%bf%94%e5%9b%9e" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;函数的返回值大多数时候存储在 &lt;code&gt;rax&lt;/code&gt; 寄存器中，但并非没有意外&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;寄存器类型限制&lt;/strong&gt; 前面已经知道，&lt;code&gt;rax&lt;/code&gt; 这类寄存器只能存储整数和内存地址，那么显然，如果返回浮点数，就需要 SSE 寄存器了，这是会将返回值存储到 &lt;code&gt;xmm0&lt;/code&gt; 中&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;寄存器大小限制&lt;/strong&gt; 这个需要分为两类，基础类型和复合类型：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;u&gt;基础类型&lt;/u&gt; 当存储 128 位整数时没有寄存器能胜任这个任务，所以采取的方案时，拼装两个寄存器 &lt;code&gt;rdx&lt;/code&gt; 和 &lt;code&gt;rax&lt;/code&gt;，&lt;code&gt;rdx&lt;/code&gt; 存储高 64 位，&lt;code&gt;rax&lt;/code&gt; 存储低 64 位&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;复合类型&lt;/u&gt; 当存储结构体时，采取的做法是将结构体拆分到 &lt;code&gt;rax&lt;/code&gt;，&lt;code&gt;rdx&lt;/code&gt;，&lt;code&gt;xmm0&lt;/code&gt;，&lt;code&gt;xmm1&lt;/code&gt; 中，若结构过大，则会采取，由 caller 分配内存，&lt;code&gt;rax&lt;/code&gt; 存储其内存地址的方式&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;在函数的末尾，使用 &lt;code&gt;ret&lt;/code&gt; 指令，其作用是将存放栈中的返回地址弹出，写入 &lt;code&gt;rip&lt;/code&gt; 中&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="寄存器"&gt;&lt;span&gt;寄存器&lt;/span&gt;&#10; &lt;a href="#%e5%af%84%e5%ad%98%e5%99%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;寄存器是真实存在的物理实体，它是 CPU 的一种速度极快的存储器&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="通用寄存器"&gt;&lt;span&gt;通用寄存器&lt;/span&gt;&#10; &lt;a href="#%e9%80%9a%e7%94%a8%e5%af%84%e5%ad%98%e5%99%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;在 64 位系统中，前 8 个寄存器有自己的名字，它们分别是 &lt;code&gt;rax&lt;/code&gt;，&lt;code&gt;rbx&lt;/code&gt;，&lt;code&gt;rcx&lt;/code&gt;，&lt;code&gt;rdx&lt;/code&gt;，&lt;code&gt;rsp&lt;/code&gt;，&lt;code&gt;rbp&lt;/code&gt;，&lt;code&gt;rdi&lt;/code&gt;，&lt;code&gt;rsi&lt;/code&gt;，后 8 个寄存器就是 r8~r15，重点学习前 8 个寄存器&lt;/p&gt;&#10;&lt;p&gt;我们已经 &lt;code&gt;rax&lt;/code&gt;，&lt;code&gt;rcx&lt;/code&gt;，&lt;code&gt;rdx&lt;/code&gt;，&lt;code&gt;rsp&lt;/code&gt;，&lt;code&gt;rbp&lt;/code&gt;，&lt;code&gt;rdi&lt;/code&gt;，&lt;code&gt;rsi&lt;/code&gt; 各有 ABI 为他们分配的任务，唯独 &lt;code&gt;rbx&lt;/code&gt; 貌似没什么作用——事实上，它确实没有特殊作用。在习惯上，它常被用于数组的基址，但并非强制&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="调用者保存寄存器"&gt;&lt;span&gt;调用者保存寄存器&lt;/span&gt;&#10; &lt;a href="#%e8%b0%83%e7%94%a8%e8%80%85%e4%bf%9d%e5%ad%98%e5%af%84%e5%ad%98%e5%99%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;&lt;u&gt;调用者保存寄存器&lt;/u&gt;：即调用者负责保存的寄存器，所以被调用的函数可以随意对其进行修改，调用前后不保证寄存器内数据不变，有：&lt;code&gt;rax&lt;/code&gt;,&lt;code&gt;rdx&lt;/code&gt;,&lt;code&gt;rcx&lt;/code&gt;,&lt;code&gt;rsi&lt;/code&gt;,&lt;code&gt;rdi&lt;/code&gt;,&lt;code&gt;r8&lt;/code&gt;,&lt;code&gt;r9&lt;/code&gt;,&lt;code&gt;r10&lt;/code&gt;,&lt;code&gt;r11&lt;/code&gt;,&lt;code&gt;xmm0~xmm15&lt;/code&gt;&#10;可以发现，调用者保存寄存器包括了参数存放和函数返回的寄存器，这些量是在函数调用前后才有意义的，而r10和r11是专门留作临时工的，特殊记忆即可&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;一个典型的误区是调用者保存不意味着调用者就会去保存，只是当需要保存时有调用者负责而已，其实大多数时候不会特意保存，故是易失的&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h3 class="heading-element" id="被调用者保存寄存器"&gt;&lt;span&gt;被调用者保存寄存器&lt;/span&gt;&#10; &lt;a href="#%e8%a2%ab%e8%b0%83%e7%94%a8%e8%80%85%e4%bf%9d%e5%ad%98%e5%af%84%e5%ad%98%e5%99%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;&lt;u&gt;被调用者保存&lt;/u&gt;：即被调用者负责保存的寄存器，所以在函数调用前后可以保证其内数据不变，可以看作不易失的，包括&lt;code&gt;rbx&lt;/code&gt;,&lt;code&gt;rbp&lt;/code&gt;,&lt;code&gt;r12&lt;/code&gt;,&lt;code&gt;r13&lt;/code&gt;,&lt;code&gt;r14&lt;/code&gt;,&lt;code&gt;r15&lt;/code&gt;。从这里可以看出&lt;code&gt;rbx&lt;/code&gt;的特殊之处&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="特殊寄存器"&gt;&lt;span&gt;特殊寄存器&lt;/span&gt;&#10; &lt;a href="#%e7%89%b9%e6%ae%8a%e5%af%84%e5%ad%98%e5%99%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;rsp&lt;/code&gt;&lt;/strong&gt;：由调用者和被调用共同维护，并且作为栈的实际栈顶&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;虽然我们常常将 &lt;code&gt;rbp&lt;/code&gt; 寄存器认为是当前函数栈底，但这不是严格的，只是约定俗称。而 &lt;code&gt;rsp&lt;/code&gt; 确实严格为栈顶&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;&lt;code&gt;rip&lt;/code&gt;&lt;/strong&gt;：由 &lt;code&gt;call&lt;/code&gt;/&lt;code&gt;ret&lt;/code&gt; 隐式管理，无法显式地修改 &lt;code&gt;rip&lt;/code&gt;&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="寄存器命名"&gt;&lt;span&gt;寄存器命名&lt;/span&gt;&#10; &lt;a href="#%e5%af%84%e5%ad%98%e5%99%a8%e5%91%bd%e5%90%8d" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;在 64 位操作系统上，寄存器的命令法则有三套&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;code&gt;rax&lt;/code&gt;, &lt;code&gt;rbx&lt;/code&gt;, &lt;code&gt;rdx&lt;/code&gt;, &lt;code&gt;rcx&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;大小&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;64 位&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;32 位&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;16 位&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;高 8 位&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;低 8 位&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;&lt;strong&gt;rax 类&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;rax&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;eax&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;ax&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;ah&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;al&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;&lt;strong&gt;rbx 类&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;rbx&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;ebx&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;bx&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;bh&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;bl&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;&lt;strong&gt;rcx 类&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;rcx&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;ecx&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;cx&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;ch&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;cl&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;&lt;strong&gt;rdx 类&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;rdx&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;edx&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;dx&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;dh&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;dl&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;ol start="2"&gt;&#10;&lt;li&gt;&lt;code&gt;rdi&lt;/code&gt;, &lt;code&gt;rsi&lt;/code&gt;, &lt;code&gt;rsp&lt;/code&gt;, &lt;code&gt;rbp&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;大小&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;64 位&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;32 位&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;16 位&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;低 8 位&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;&lt;strong&gt;rdi 类&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;rdi&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;edi&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;di&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;dil&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;&lt;strong&gt;rsi 类&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;rsi&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;esi&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;si&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;sil&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;&lt;strong&gt;rsp 类&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;rsp&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;esp&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;sp&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;spl&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;&lt;strong&gt;rbp 类&lt;/strong&gt;&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;rbp&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;ebp&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;bp&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;bpl&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;ol start="3"&gt;&#10;&lt;li&gt;&lt;code&gt;r8&lt;/code&gt; ~ &lt;code&gt;r15&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;大小&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;64 位&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;32 位&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;16 位&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;低 8 位&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;名称&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;r8~r15&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;r8d~r15d&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;r8w~r15w&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;r8b~r15b&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;关键规则&lt;/strong&gt;&lt;/u&gt;：当写入32位寄存器时，会把高32位清零。但写入16位和8位并不会改变其他的数位&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="rflags-寄存器"&gt;&lt;span&gt;&lt;code&gt;rflags&lt;/code&gt; 寄存器&lt;/span&gt;&#10; &lt;a href="#rflags-%e5%af%84%e5%ad%98%e5%99%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;&lt;code&gt;rflags&lt;/code&gt; 寄存器是一个特殊的寄存器，它的每一位都都是特殊的标志位（高 32 位保留）&lt;/p&gt;&#10;&lt;p&gt;下面看常见的标志位：&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;ZF&lt;/strong&gt;（Zero Flag）：结果为 0 时为 1&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;SF&lt;/strong&gt;（Sign Flag）：最高位为 1 就为 1&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;OF&lt;/strong&gt;（Overflow Flag）：有符号溢出则为 1&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;CF&lt;/strong&gt;（Carry Flag）：无符号进位/借位则为 1&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 class="heading-element" id="常用指令"&gt;&lt;span&gt;常用指令&lt;/span&gt;&#10; &lt;a href="#%e5%b8%b8%e7%94%a8%e6%8c%87%e4%bb%a4" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;指令是汇编中的核心操作，主要是通过操作栈和寄存器进行的。在编写时牢记以下几点：&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;在 AT&amp;amp;T 语法中，一律为 &lt;code&gt;指令 源操作数, 目的操作数&lt;/code&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;每条指令最多访问一次内存，但可以多次访问寄存器&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;目的操作数不能是立即数&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;下面介绍以下常用指令：&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="数据传送指令"&gt;&lt;span&gt;数据传送指令&lt;/span&gt;&#10; &lt;a href="#%e6%95%b0%e6%8d%ae%e4%bc%a0%e9%80%81%e6%8c%87%e4%bb%a4" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;mov&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;mov&lt;/code&gt; 是最常用的指令之一，它的作用是把源操作数中的数据复制到目的操作数&lt;/p&gt;&#10;&lt;p&gt;如：&lt;code&gt;movq %rax, %rbx&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;表示把 &lt;code&gt;rax&lt;/code&gt; 中存储的数据移动到 &lt;code&gt;rbx&lt;/code&gt; 中，如果给 &lt;code&gt;rax&lt;/code&gt; 加一层括号，就类似于 C 中的解引用操作&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;movq (%rax), %rbx&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;表示以 &lt;code&gt;rax&lt;/code&gt; 中存储的数据为内存地址寻找数据，将找到的数据存储在 &lt;code&gt;rbx&lt;/code&gt; 中&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;lea&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;lea&lt;/code&gt; 非常容易与 &lt;code&gt;mov&lt;/code&gt; 搞混，它代表直接读取源操作数的地址到目的操作数中&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;leaq (%rax), %rbx&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;表示将 &lt;code&gt;rax&lt;/code&gt; 中存储的地址读取到 &lt;code&gt;rbx&lt;/code&gt; 中&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;值得注意的是，这里的 &lt;code&gt;(%rax)&lt;/code&gt; 并非代表与上面类似的解引用操作，而是表明 &lt;code&gt;rax&lt;/code&gt; 内存储着地址&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;&lt;code&gt;leaq %rax, %rbx&lt;/code&gt;是不合法的&lt;/p&gt;&#10;&lt;p&gt;正是以为 &lt;code&gt;leaq&lt;/code&gt; 的特性，常常被用作高效乘法，具体后面介绍&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;push 与 pop&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;push&lt;/code&gt; 的原理等同于 &lt;code&gt;sub&lt;/code&gt; + &lt;code&gt;mov&lt;/code&gt;，即先存储空间，再将目的操作数的数据复制到栈中&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;pop&lt;/code&gt; 的原理等于同 &lt;code&gt;mov&lt;/code&gt; + &lt;code&gt;add&lt;/code&gt;，即先将栈中内容复制到目的操作数中&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 class="heading-element" id="算术与逻辑指令"&gt;&lt;span&gt;算术与逻辑指令&lt;/span&gt;&#10; &lt;a href="#%e7%ae%97%e6%9c%af%e4%b8%8e%e9%80%bb%e8%be%91%e6%8c%87%e4%bb%a4" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;一元操作算术&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;inc&lt;/code&gt;，&lt;code&gt;dec&lt;/code&gt;，&lt;code&gt;neg&lt;/code&gt; 三者分别对应自增，自减，取负&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;二元操作算术&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;add&lt;/code&gt;，&lt;code&gt;sub&lt;/code&gt; 两者分别对应加，减&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;code&gt;lea&lt;/code&gt; 可以完成同等功能，但并非取代，在不同场景下有不同的最优解，一个比较明显的区别是 &lt;code&gt;lea&lt;/code&gt; 并不会修改 &lt;code&gt;rflags&lt;/code&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;逻辑操作算术&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;and&lt;/code&gt;，&lt;code&gt;or&lt;/code&gt;，&lt;code&gt;xor&lt;/code&gt; 分别表示与、或、异或&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;移位操作算术&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;sal&lt;/code&gt;, &lt;code&gt;shl&lt;/code&gt;, &lt;code&gt;sar&lt;/code&gt;, &lt;code&gt;shr&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;分别是&lt;strong&gt;算数左移&lt;/strong&gt;（Shift Arithmetic Left），&lt;strong&gt;逻辑左移&lt;/strong&gt;（Shift Logical Left），&lt;strong&gt;算数右移&lt;/strong&gt;（Shift Arithmetic Right），&lt;strong&gt;逻辑右移&lt;/strong&gt;（Shift Logical Right）&lt;/p&gt;&#10;&lt;p&gt;其中&lt;u&gt;逻辑&lt;/u&gt;表明直接执行位移操作而不进行额外判断&lt;/p&gt;&#10;&lt;p&gt;&lt;u&gt;算数&lt;/u&gt;表明执行位移操作时，会尽量使负数维持为负数，正数维持为正数。但左移必定破坏最高位符号位，所以逻辑左移等同算数左移&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;另外对于源操作数有特殊要求，必须是立即数，或是存储在 &lt;code&gt;cl&lt;/code&gt; 寄存器（rcx 的低 8 位）中&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;以上的指令都有显著特征：即会改变 &lt;code&gt;rflags&lt;/code&gt; 寄存器，且在算数逻辑单元中执行运算&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="控制流指令"&gt;&lt;span&gt;控制流指令&lt;/span&gt;&#10; &lt;a href="#%e6%8e%a7%e5%88%b6%e6%b5%81%e6%8c%87%e4%bb%a4" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;cmp 与 test&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;cmp&lt;/code&gt; 的原理是，进行运算：目的操作数 - 源操作数，类似于 &lt;code&gt;sub&lt;/code&gt;，但 &lt;code&gt;cmp&lt;/code&gt; 不会保存运算结果，仅设置条件码&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;test&lt;/code&gt; 与 &lt;code&gt;cmp&lt;/code&gt; 类似，执行 &lt;code&gt;and&lt;/code&gt; 操作，但不保留结果，仅设置条件码&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;jmp&lt;/strong&gt;&lt;/u&gt;&#10;&lt;code&gt;jmp&lt;/code&gt; 指令很简单 &lt;code&gt;jmp Label&lt;/code&gt; 将 &lt;code&gt;rip&lt;/code&gt; 内地址修改为 Label 地址&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;jmp *%rax&lt;/code&gt;：表示跳转到 &lt;code&gt;%rax&lt;/code&gt; 中存储的地址&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;jmp *(%rax)&lt;/code&gt;：表示跳转到以 &lt;code&gt;(%rax)&lt;/code&gt; 中存储的地址指向的数据为地址的内存地址&lt;/p&gt;&#10;&lt;p&gt;难点在&lt;strong&gt;jmp 类指令&lt;/strong&gt;，这类指令根据条件码的情况进行跳转操作，有以下几种&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;je（jump if equal）&lt;/strong&gt; ZF = 1 时跳转&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;jne（jump if not equal）&lt;/strong&gt; ZF = 0 时跳转&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;jg（jump if greater）&lt;/strong&gt; 用于有符号比较，需要综合考虑 &lt;code&gt;SF&lt;/code&gt;，&lt;code&gt;OF&lt;/code&gt;，&lt;code&gt;ZF&lt;/code&gt;，类似的还有 &lt;code&gt;jge&lt;/code&gt;，与 &lt;code&gt;jg/jge&lt;/code&gt; 对应的是 &lt;code&gt;jl/jle&lt;/code&gt;（jump if less/jump if less or equal）&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;ja（jump if above）&lt;/strong&gt; 用于无符号比较，需要综合考虑 &lt;code&gt;CF&lt;/code&gt;，&lt;code&gt;ZF&lt;/code&gt;，类似的还有 &lt;code&gt;jae&lt;/code&gt;，与 &lt;code&gt;ja/jae&lt;/code&gt; 对应的是 &lt;code&gt;jb/jbe&lt;/code&gt;（jump if less/jump if less or equal）&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;记忆这几个的方法是记住，&lt;strong&gt;所有的大或者小都是相对于目的操作数而言的&lt;/strong&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;流程控制的主要目的是模拟 C 中的 &lt;code&gt;for&lt;/code&gt;，&lt;code&gt;while&lt;/code&gt;，&lt;code&gt;do-while&lt;/code&gt;&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="函数调用与栈帧指令"&gt;&lt;span&gt;函数调用与栈帧指令&lt;/span&gt;&#10; &lt;a href="#%e5%87%bd%e6%95%b0%e8%b0%83%e7%94%a8%e4%b8%8e%e6%a0%88%e5%b8%a7%e6%8c%87%e4%bb%a4" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;&lt;code&gt;call&lt;/code&gt;，&lt;code&gt;ret&lt;/code&gt;，&lt;code&gt;leave&lt;/code&gt; 这三个指令在上面已经介绍得很清楚了，不加赘述&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="数据声明与访问"&gt;&lt;span&gt;数据声明与访问&lt;/span&gt;&#10; &lt;a href="#%e6%95%b0%e6%8d%ae%e5%a3%b0%e6%98%8e%e4%b8%8e%e8%ae%bf%e9%97%ae" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;h3 class="heading-element" id="内存布局"&gt;&lt;span&gt;内存布局&lt;/span&gt;&#10; &lt;a href="#%e5%86%85%e5%ad%98%e5%b8%83%e5%b1%80" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA4MDAgODAwIiB3aWR0aD0iMTAwJSIgc3R5bGU9Im1heC13aWR0aDo4MDBweDsgZm9udC1mYW1pbHk6c2Fucy1zZXJpZjsgYmFja2dyb3VuZC1jb2xvcjp3aGl0ZTsgZGlzcGxheTpibG9jazsgbWFyZ2luOjAgYXV0bzsiPjxyZWN0IHg9IjIyMCIgeT0iNTAiIHdpZHRoPSIzMDAiIGhlaWdodD0iNzAwIiBmaWxsPSIjZTZlNmU2IiBzdHJva2U9IiMyMjIiIHN0cm9rZS13aWR0aD0iMiIgLz48cmVjdCB4PSIyMjAiIHk9IjY5MCIgd2lkdGg9IjMwMCIgaGVpZ2h0PSI2MCIgZmlsbD0iIzljZDFmMCIgLz48cmVjdCB4PSIyMjAiIHk9IjQyMCIgd2lkdGg9IjMwMCIgaGVpZ2h0PSI4MCIgZmlsbD0iIzljZDFmMCIgLz48cmVjdCB4PSIyMjAiIHk9IjI1MCIgd2lkdGg9IjMwMCIgaGVpZ2h0PSIxMDAiIGZpbGw9IiM5Y2QxZjAiIC8+PGxpbmUgeDE9IjIyMCIgeTE9IjE4MCIgeDI9IjUyMCIgeTI9IjE4MCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PGxpbmUgeDE9IjIyMCIgeTE9IjI1MCIgeDI9IjUyMCIgeTI9IjI1MCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PGxpbmUgeDE9IjIyMCIgeTE9IjM1MCIgeDI9IjUyMCIgeTI9IjM1MCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PGxpbmUgeDE9IjIyMCIgeTE9IjQyMCIgeDI9IjUyMCIgeTI9IjQyMCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PGxpbmUgeDE9IjIyMCIgeTE9IjUwMCIgeDI9IjUyMCIgeTI9IjUwMCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PGxpbmUgeDE9IjIyMCIgeTE9IjU3MCIgeDI9IjUyMCIgeTI9IjU3MCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PGxpbmUgeDE9IjIyMCIgeTE9IjYzMCIgeDI9IjUyMCIgeTI9IjYzMCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PGxpbmUgeDE9IjIyMCIgeTE9IjY5MCIgeDI9IjUyMCIgeTI9IjY5MCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PGxpbmUgeDE9IjIyMCIgeTE9IjUwIiB4Mj0iNTIwIiB5Mj0iNTAiIHN0cm9rZT0iIzIyMiIgc3Ryb2tlLXdpZHRoPSI0IiAvPjxnIGZvbnQtc2l6ZT0iMTUiIGZpbGw9IiMyMjIiIHRleHQtYW5jaG9yPSJtaWRkbGUiPjx0ZXh0IHg9IjM3MCIgeT0iMTA1Ij5LZXJuZWwgdmlydHVhbCBtZW1vcnk8L3RleHQ+PHRleHQgeD0iMzcwIiB5PSIxMzAiPihjb2RlLCBkYXRhLCBoZWFwLCBzdGFjayk8L3RleHQ+PHRleHQgeD0iMzcwIiB5PSIyMTAiPlVzZXIgc3RhY2s8L3RleHQ+PHRleHQgeD0iMzcwIiB5PSIyMzUiPihjcmVhdGVkIGF0IHJ1biB0aW1lKTwvdGV4dD48dGV4dCB4PSIzNzAiIHk9IjM3NSI+TWVtb3J5LW1hcHBlZCByZWdpb24gZm9yPC90ZXh0Pjx0ZXh0IHg9IjM3MCIgeT0iNDAwIj5zaGFyZWQgbGlicmFyaWVzPC90ZXh0Pjx0ZXh0IHg9IjM3MCIgeT0iNTI1Ij5SdW4tdGltZSBoZWFwPC90ZXh0Pjx0ZXh0IHg9IjM3MCIgeT0iNTUwIj4oY3JlYXRlZCBieSA8dHNwYW4gZm9udC1mYW1pbHk9Im1vbm9zcGFjZSI+bWFsbG9jPC90c3Bhbj4pPC90ZXh0Pjx0ZXh0IHg9IjM3MCIgeT0iNTkwIj5SZWFkL3dyaXRlIHNlZ21lbnQ8L3RleHQ+PHRleHQgeD0iMzcwIiB5PSI2MTUiPig8dHNwYW4gZm9udC1mYW1pbHk9Im1vbm9zcGFjZSI+LmRhdGE8L3RzcGFuPiwgPHRzcGFuIGZvbnQtZmFtaWx5PSJtb25vc3BhY2UiPi5ic3M8L3RzcGFuPik8L3RleHQ+PHRleHQgeD0iMzcwIiB5PSI2NTAiPlJlYWQtb25seSBjb2RlIHNlZ21lbnQ8L3RleHQ+PHRleHQgeD0iMzcwIiB5PSI2NzUiPig8dHNwYW4gZm9udC1mYW1pbHk9Im1vbm9zcGFjZSI+LmluaXQ8L3RzcGFuPiwgPHRzcGFuIGZvbnQtZmFtaWx5PSJtb25vc3BhY2UiPi50ZXh0PC90c3Bhbj4sIDx0c3BhbiBmb250LWZhbWlseT0ibW9ub3NwYWNlIj4ucm9kYXRhPC90c3Bhbj4pPC90ZXh0PjwvZz48dGV4dCB4PSI1MzUiIHk9IjI1NSIgZm9udC1zaXplPSIxNSIgZmlsbD0iIzIyMiI+JWVzcCAoc3RhY2sgcG9pbnRlcik8L3RleHQ+PGxpbmUgeDE9IjUzMiIgeTE9IjI1MCIgeDI9IjUyMiIgeTI9IjI1MCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PHBvbHlnb24gcG9pbnRzPSI1MjIsMjUwIDUyOCwyNDcgNTI4LDI1MyIgZmlsbD0iIzIyMiIvPjx0ZXh0IHg9IjUzNSIgeT0iNTA1IiBmb250LXNpemU9IjE1IiBmaWxsPSIjMjIyIj5icms8L3RleHQ+PGxpbmUgeDE9IjUzMiIgeTE9IjUwMCIgeDI9IjUyMiIgeTI9IjUwMCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PHBvbHlnb24gcG9pbnRzPSI1MjIsNTAwIDUyOCw0OTcgNTI4LDUwMyIgZmlsbD0iIzIyMiIvPjxwYXRoIGQ9Ik0gNTMwIDU3MCBDIDU0MCA1NzAsIDU0MCA1ODAsIDU0MCA1ODAgTCA1NDAgNjgwIEMgNTQwIDY4MCwgNTQwIDY5MCwgNTMwIDY5MCIgZmlsbD0ibm9uZSIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PHRleHQgeD0iNTQ1IiB5PSI2MjAiIGZvbnQtc2l6ZT0iMTUiIGZpbGw9IiMyMjIiPkxvYWRlZCBmcm9tIHRoZTwvdGV4dD48dGV4dCB4PSI1NDUiIHk9IjY0NSIgZm9udC1zaXplPSIxNSIgZmlsbD0iIzIyMiI+ZXhlY3V0YWJsZSBmaWxlPC90ZXh0Pjx0ZXh0IHg9IjU0NSIgeT0iODAiIGZvbnQtc2l6ZT0iMTUiIGZpbGw9IiMyMjIiPk1lbW9yeTwvdGV4dD48dGV4dCB4PSI1NDUiIHk9IjEwNSIgZm9udC1zaXplPSIxNSIgZmlsbD0iIzIyMiI+aW52aXNpYmxlIHRvPC90ZXh0Pjx0ZXh0IHg9IjU0NSIgeT0iMTMwIiBmb250LXNpemU9IjE1IiBmaWxsPSIjMjIyIj51c2VyIGNvZGU8L3RleHQ+PGxpbmUgeDE9IjUzNSIgeTE9IjE1MCIgeDI9IjUzNSIgeTI9IjcwIiBzdHJva2U9IiMyMjIiIHN0cm9rZS13aWR0aD0iMiIgLz48cG9seWdvbiBwb2ludHM9IjUzNSw3MCA1MzIsNzYgNTM4LDc2IiBmaWxsPSIjMjIyIi8+PHRleHQgeD0iMjA1IiB5PSI1NSIgZm9udC1zaXplPSIxNSIgZmlsbD0iIzIyMiIgdGV4dC1hbmNob3I9ImVuZCI+PHRzcGFuPjI8L3RzcGFuPjx0c3BhbiBmb250LXNpemU9IjEwIiBkeT0iLTYiPjQ4PC90c3Bhbj48dHNwYW4gZHk9IjYiPiAtIDE8L3RzcGFuPjwvdGV4dD48bGluZSB4MT0iMjEyIiB5MT0iNTAiIHgyPSIyMTgiIHkyPSI1MCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PHBvbHlnb24gcG9pbnRzPSIyMTgsNTAgMjEyLDQ3IDIxMiw1MyIgZmlsbD0iIzIyMiIvPjx0ZXh0IHg9IjIwNSIgeT0iNjk1IiBmb250LXNpemU9IjE1IiBmaWxsPSIjMjIyIiB0ZXh0LWFuY2hvcj0iZW5kIj4weDQwMDAwMDwvdGV4dD48bGluZSB4MT0iMjEyIiB5MT0iNjkwIiB4Mj0iMjE4IiB5Mj0iNjkwIiBzdHJva2U9IiMyMjIiIHN0cm9rZS13aWR0aD0iMiIgLz48cG9seWdvbiBwb2ludHM9IjIxOCw2OTAgMjEyLDY4NyAyMTIsNjkzIiBmaWxsPSIjMjIyIi8+PHRleHQgeD0iMjA1IiB5PSI3NTUiIGZvbnQtc2l6ZT0iMTUiIGZpbGw9IiMyMjIiIHRleHQtYW5jaG9yPSJlbmQiPjA8L3RleHQ+PGxpbmUgeDE9IjM3MCIgeTE9IjI3MCIgeDI9IjM3MCIgeTI9IjI5MCIgc3Ryb2tlPSIjMjIyIiBzdHJva2Utd2lkdGg9IjIiIC8+PHBvbHlnb24gcG9pbnRzPSIzNzAsMjkwIDM2NywyODQgMzczLDI4NCIgZmlsbD0iIzIyMiIvPjxsaW5lIHgxPSIzNzAiIHkxPSIzNDAiIHgyPSIzNzAiIHkyPSIzMjAiIHN0cm9rZT0iIzIyMiIgc3Ryb2tlLXdpZHRoPSIyIiAvPjxwb2x5Z29uIHBvaW50cz0iMzcwLDMyMCAzNjcsMzI2IDM3MywzMjYiIGZpbGw9IiMyMjIiLz48bGluZSB4MT0iMzcwIiB5MT0iNDgwIiB4Mj0iMzcwIiB5Mj0iNDQwIiBzdHJva2U9IiMyMjIiIHN0cm9rZS13aWR0aD0iMiIgLz48cG9seWdvbiBwb2ludHM9IjM3MCw0NDAgMzY3LDQ0NiAzNzMsNDQ2IiBmaWxsPSIjMjIyIi8+PC9zdmc+" alt="Linux Virtual Memory Layout" /&gt;&#10;&lt;h2 class="heading-element" id="全局变量与静态变量"&gt;&lt;span&gt;全局变量与静态变量&lt;/span&gt;&#10; &lt;a href="#%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f%e4%b8%8e%e9%9d%99%e6%80%81%e5%8f%98%e9%87%8f" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;两者都存储在 &lt;code&gt;.bss&lt;/code&gt; 和 &lt;code&gt;.data&lt;/code&gt; 段内，不同的是普通全局变量是外部链接，通过伪指令 &lt;code&gt;.global&lt;/code&gt; 声明；而静态全局变量是内部链接，通过伪指令 &lt;code&gt;.local&lt;/code&gt; 声明&lt;/p&gt;&#10;&lt;p&gt;如&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int a = 10;&#10;static int b = 10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;对应的汇编如下&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;.section .data&#10;&#10;a: .long 10&#10;b: .long 10&#10;&#10;.local b&#10;.global a&lt;/code&gt;&lt;/pre&gt;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;code&gt;.bss&lt;/code&gt; 和 &lt;code&gt;.data&lt;/code&gt; 的区别在于，&lt;code&gt;.bss&lt;/code&gt; 中存储的是未初始化或初始化为 0 的变量，&lt;code&gt;.data&lt;/code&gt; 则不是这样的&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;在声明时，一般格式为 &lt;code&gt;名称: 伪指令&lt;/code&gt;&#10;常见的有 &lt;code&gt;.byte&lt;/code&gt;，&lt;code&gt;.word&lt;/code&gt;，&lt;code&gt;.long&lt;/code&gt;，&lt;code&gt;.quad&lt;/code&gt; 这四个分别对应 1 字节，2 字节，4 字节，8 字节&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;.string&lt;/code&gt; 表示自动加 &lt;code&gt;\0&lt;/code&gt; 的字符串，&lt;code&gt;.ascii&lt;/code&gt; 表示不会自动加 &lt;code&gt;\0&lt;/code&gt; 的字符串&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;.zero N&lt;/code&gt; 表示预留 N 字节空间，并初始化为 0，&lt;code&gt;.space N&lt;/code&gt; 表示预留 N 字节空间，不初始化&lt;/p&gt;&#10;&lt;p&gt;除了 &lt;code&gt;.bss&lt;/code&gt; 和 &lt;code&gt;.data&lt;/code&gt; 段，还有 &lt;code&gt;.rodata&lt;/code&gt; 段，表示 Read-only Data，一般存放字符串。一个典型的例子就是 &lt;code&gt;printf(&amp;quot;%d&amp;quot;,number);&lt;/code&gt;，其中这个 &lt;code&gt;%d&lt;/code&gt; 就会被存放到 &lt;code&gt;rodata&lt;/code&gt; 段&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;C 中的一个常见的坑就是 &lt;code&gt;char* str = &amp;quot;Hello&amp;quot;;&lt;/code&gt;，不能使用 &lt;code&gt;str[0]&lt;/code&gt; 来修改字符串，这是因为 str 指向的字符串是在 &lt;code&gt;rodata&lt;/code&gt; 段上的&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;因为每次程序加载的地址都不一样，所以使用 &lt;code&gt;.bss/.data/.rodata&lt;/code&gt; 时应使用 RIP 相对地址：&lt;code&gt;变量名(%rip)&lt;/code&gt;&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="数组"&gt;&lt;span&gt;数组&lt;/span&gt;&#10; &lt;a href="#%e6%95%b0%e7%bb%84" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;h4 class="heading-element" id="全局静态变量数组"&gt;&lt;span&gt;全局（静态）变量数组&lt;/span&gt;&#10; &lt;a href="#%e5%85%a8%e5%b1%80%e9%9d%99%e6%80%81%e5%8f%98%e9%87%8f%e6%95%b0%e7%bb%84" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h4&gt;&lt;p&gt;其声明方法与其他全局变量类似：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;nums: .long 1&#10; .long 2&#10; .long 3&#10; .space 308&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;等同于&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int nums[80] = {1,2,3};&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;访问方法也很简单&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;movl nums&amp;#43;4(%rip), %eax&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;等同于&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;int num = nums[1];&lt;/code&gt;&lt;/pre&gt;&lt;h4 class="heading-element" id="局部变量数组"&gt;&lt;span&gt;局部变量数组&lt;/span&gt;&#10; &lt;a href="#%e5%b1%80%e9%83%a8%e5%8f%98%e9%87%8f%e6%95%b0%e7%bb%84" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h4&gt;&lt;p&gt;相较于全部变量数组，局部变量数组更加常用，也更复杂&lt;/p&gt;&#10;&lt;p&gt;所谓局部变量，默认是存储在栈上的变量，其访问格式为：&lt;/p&gt;&#10;$$&#10; offset(base, index, scale) = base &amp;#43; index * scale &amp;#43; offset&#10;$$&lt;p&gt;&lt;strong&gt;其中 &lt;code&gt;base&lt;/code&gt; 必须为寄存器，&lt;code&gt;index&lt;/code&gt; 必须为通用寄存器（不能为 &lt;code&gt;%rsp&lt;/code&gt;）或为空，&lt;code&gt;scale&lt;/code&gt; 只能为 1,2,4,8，且 &lt;code&gt;base&lt;/code&gt; 和 &lt;code&gt;index&lt;/code&gt; 必须为相同大小的寄存器&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;正是因为在这个取址运算中用到了乘法，所以常常使用 &lt;code&gt;lea&lt;/code&gt; 来实现高效的乘法&lt;/p&gt;&#10;&lt;p&gt;结构体和联合体访问与数组类似。不过结构体是不同数据类型的集合，并且需要考虑内存对齐，联合体所占字节为最大成员变量所占字节数&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="gdb-使用技巧"&gt;&lt;span&gt;GDB 使用技巧&lt;/span&gt;&#10; &lt;a href="#gdb-%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;h3 class="heading-element" id="基本指令"&gt;&lt;span&gt;基本指令&lt;/span&gt;&#10; &lt;a href="#%e5%9f%ba%e6%9c%ac%e6%8c%87%e4%bb%a4" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;ol&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;break&lt;/strong&gt;&lt;/u&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;作用：打断点&#10;&lt;em&gt;usage&lt;/em&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;break 行号&#10;break 函数名&#10;break 文件名:行号&#10;break *地址&#10;break 对象 if 条件&#10;rbreak 正则表达式&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;用 &lt;code&gt;tbreak&lt;/code&gt; 替代 &lt;code&gt;break&lt;/code&gt; 代表临时断点&lt;/p&gt;&#10;&lt;ol start="2"&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;watch&lt;/strong&gt;&lt;/u&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;作用观察变量是否被修改&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;usage&lt;/em&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;watch 变量名 //当变量值发生改变时暂停&#10;rwatch 变量名 //当变量值被读取时暂停&#10;awatch 变量名 //当变量值被读取或写入时暂停&#10;watch 变量名 if 条件&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;ol start="3"&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;disas&lt;/strong&gt;&lt;/u&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;反汇编命令&lt;/p&gt;&#10;&lt;p&gt;&lt;em&gt;usage&lt;/em&gt;&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;disas //反汇编当前栈帧对应函数&#10;disas 函数名 //反汇编此函数&#10;disas $pc //反汇编$pc所在函数&#10;disas 地址 //反汇编地址所在函数&#10;disas 起始地址, 终止地址 //反汇编此地址区间&#10;disas 起始地址, 偏移量 //从起始地址开始反汇编指定偏移量&#10;disas /s //混合显示C源码和汇编代码&#10;disas /b //显示汇编源码和机器指令&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;ol start="3"&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;examine&lt;/strong&gt;&lt;/u&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;em&gt;usage&lt;/em&gt;&#10;标准语法：&lt;code&gt;x/&amp;lt;N&amp;gt;&amp;lt;F&amp;gt;&amp;lt;U&amp;gt; &amp;lt;ADDR&amp;gt;&lt;/code&gt;&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;N&lt;/strong&gt; 表示重复次数，连续往后查找&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;U&lt;/strong&gt; 表示单位大小 可选 &lt;code&gt;b(byte)&lt;/code&gt;，&lt;code&gt;h(halfword)&lt;/code&gt;，&lt;code&gt;w(word)&lt;/code&gt;，&lt;code&gt;g(giant word)&lt;/code&gt;，分别对应 1 字节，2 字节，4 字节，8 字节&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;需要注意的是，在汇编中，w（double word）代表 4 字节&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;F&lt;/strong&gt; 表示显示格式&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;x&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;d&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;u&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;o&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;t&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;f&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;c&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;s&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;a&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;十六进制数&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;有符号十进制&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;无符号十进制&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;八进制&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;二进制&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;浮点数&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;字符&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;字符串&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;地址&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;p&gt;例如：&lt;code&gt;x/8gx&lt;/code&gt; 代表 8 个 8 字节的 16 进制数（一般就是地址）&lt;/p&gt;</description></item><item><title>深入理解计算机系统 第六章</title><link>https://yuqu419.github.io/posts/csapp-chapter-6/</link><pubDate>Mon, 09 Nov 2026 08:35:13 +0800</pubDate><guid>https://yuqu419.github.io/posts/csapp-chapter-6/</guid><category domain="https://yuqu419.github.io/categories/%E5%AD%A6%E4%B9%A0/">学习</category><description>&lt;div style="max-width: 620px; margin: 56px auto; font-family: 'Noto Serif SC','Songti SC',Georgia,serif; color: light-dark(#2b2b2b, #d1d5db); line-height: 2;"&gt;&#10;&lt;p style="font-size: 11px; letter-spacing: .5em; text-transform: uppercase; color: light-dark(#888, #9aa3b2); margin: 0 0 24px;"&gt;Preface&lt;/p&gt;&#10;&lt;div style="width: 36px; height: 1px; background: light-dark(#111, #e1e6ed); margin-bottom: 34px;"&gt;&lt;/div&gt;&#10;&lt;p style="font-size: 15px; text-align: justify; text-justify: inter-ideograph; margin: 0 0 18px;"&gt;这些笔记写于大一加入凌睿工作室招新期间，更多的是知识的回顾，我将这些笔记记录下来，纪念那段奋斗的时光&lt;/p&gt;&#10;&lt;p style="font-size: 13px; color: light-dark(#888, #9aa3b2); letter-spacing: .1em; text-align: right; margin: 40px 0 0;"&gt;— 2026/11/9&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;h1 class="heading-element" id="进程"&gt;&lt;span&gt;进程&lt;/span&gt;&#10; &lt;a href="#%e8%bf%9b%e7%a8%8b" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;进程是程序的一次执行实例&lt;/p&gt;&#10;&lt;p&gt;每个进程都有独属于自己的内存区域（相互独立的堆栈等）&lt;/p&gt;&#10;&lt;p&gt;每个进程都有独属于自己的自己的&lt;u&gt;&lt;strong&gt;进程 ID（PID）&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="创建进程"&gt;&lt;span&gt;创建进程&lt;/span&gt;&#10; &lt;a href="#%e5%88%9b%e5%bb%ba%e8%bf%9b%e7%a8%8b" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;使用 &lt;code&gt;fork()&lt;/code&gt; 函数可以创建子进程，物理页先共享并设为只读，任一进程写入时触发缺页异常，内核再复制该页。这就是 写时复制 COW&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="覆盖进程"&gt;&lt;span&gt;覆盖进程&lt;/span&gt;&#10; &lt;a href="#%e8%a6%86%e7%9b%96%e8%bf%9b%e7%a8%8b" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;使用 &lt;code&gt;exec&lt;/code&gt; 类函数能够将硬盘中指定的可执行文件加载到当前进程中，覆盖掉几乎全部内容。当以下内容不变&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;PID，进程所在进程组和会话&lt;/li&gt;&#10;&lt;li&gt;打开的文件描述符&lt;/li&gt;&#10;&lt;li&gt;信号掩码&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;特别注意：信号处理函数会被重置&#10;系统调用：&lt;code&gt;execve&lt;/code&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;pre&gt;&lt;code&gt;int execve(const char *pathname, char *const argv[], char *const envp[]);&lt;/code&gt;&lt;/pre&gt;&lt;ul&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;pathname&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt;：为程序的绝对路径或相对路径，不回去找 PATH&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;argv&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt;：表示命名行参数数组，以 &lt;code&gt;NULL&lt;/code&gt; 结尾&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;envp&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt;：是环境变量数组，以 &lt;code&gt;NULL&lt;/code&gt; 结尾&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;返回值&lt;/strong&gt;&lt;/u&gt;：失败时返回-1，并设置&lt;code&gt;errno&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;剩余 5 个 &lt;code&gt;exec&lt;/code&gt; 的类函数都是由 &lt;code&gt;execve&lt;/code&gt; 函数包装而成&lt;/p&gt;&#10;&lt;p&gt;可以通过后缀字母窥见其含义：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;l(list)&lt;/code&gt;：参数以列表（可变参数）的形式传递&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;v(vector)&lt;/code&gt;：参数以字符串数组的形式传递&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;e(environment)&lt;/code&gt;：自定义环境变量&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;p(path)&lt;/code&gt;：当 &lt;code&gt;path&lt;/code&gt; 参数为以 &lt;code&gt;/&lt;/code&gt; 起始时，运行指定路径程序，当 &lt;code&gt;path&lt;/code&gt; 参数不以 &lt;code&gt;/&lt;/code&gt; 起始的，从 &lt;code&gt;PATH&lt;/code&gt; 中搜索&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;规则如下：&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;当指定环境变量时，使用自定义的环境变量，否则使用当前进程中的 &lt;code&gt;environ&lt;/code&gt;&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;每个进程中都有一个全局变量 &lt;code&gt;char** environ&lt;/code&gt;，当 &lt;code&gt;fork&lt;/code&gt; 时，创建的子进程会复制父进程的 &lt;code&gt;environ&lt;/code&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;PATH&lt;/code&gt; 是一个环境变量，其一般格式：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;当使用含 &lt;code&gt;p&lt;/code&gt; 后缀的函数时，且 &lt;code&gt;path&lt;/code&gt; 参数不以 &lt;code&gt;/&lt;/code&gt; 起始时(如 &lt;code&gt;ls&lt;/code&gt;)，会依次尝试&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;/usr/local/bin/ls&#10;/usr/bin/ls&#10;...&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;其余 5 个函数分别为 &lt;code&gt;execl&lt;/code&gt;，&lt;code&gt;execvp&lt;/code&gt;，&lt;code&gt;execlp&lt;/code&gt;，&lt;code&gt;execv&lt;/code&gt;，&lt;code&gt;execle&lt;/code&gt;&lt;/p&gt;&#10;&lt;h1 class="heading-element" id="进程组"&gt;&lt;span&gt;进程组&lt;/span&gt;&#10; &lt;a href="#%e8%bf%9b%e7%a8%8b%e7%bb%84" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;进程组是一个进程的集合，通常表示一个&lt;strong&gt;作业（job）&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;每个进程只属于一个进程组，在使用 &lt;code&gt;fork&lt;/code&gt; 创建子进程时，默认子进程属于父进程所在进程组&lt;/p&gt;&#10;&lt;p&gt;多个进程组组成一个&lt;strong&gt;Session（会话）&lt;/strong&gt;&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="前台进程组"&gt;&lt;span&gt;前台进程组&lt;/span&gt;&#10; &lt;a href="#%e5%89%8d%e5%8f%b0%e8%bf%9b%e7%a8%8b%e7%bb%84" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;前台进程组是一个重要的概念，它是当前 Session 中唯一被赋予控制终端并独占访问权的进程组&lt;/p&gt;&#10;&lt;p&gt;在同一时刻，只有一个进程组能被设为前台进程组，如果不是前台进程组，那就是后台进程组&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="特性"&gt;&lt;span&gt;特性&lt;/span&gt;&#10; &lt;a href="#%e7%89%b9%e6%80%a7" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;ul&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;接收键盘产生的信号&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt; 当在当前 Session 按下对应的键时，会把信号发送给&lt;strong&gt;前台进程组中的全体成员&lt;/strong&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;读取终端&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt; 前台进程组可以不受限制地从终端读取输入，而后台进程组一旦尝试读取终端，终端会向该进程组发送 &lt;code&gt;SIGTTIN&lt;/code&gt;，暂停该进程组&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;向终端写入时，终端驱动有一个 &lt;code&gt;TOSTOP&lt;/code&gt; 标志，默认关闭，当该标志关闭时，后台进程组可以向控制终端写入而不被阻止&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h2 class="heading-element" id="shell-进程组"&gt;&lt;span&gt;shell 进程组&lt;/span&gt;&#10; &lt;a href="#shell-%e8%bf%9b%e7%a8%8b%e7%bb%84" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;交互式 shell 本身就是一个进程，所在进程组默认时其就为前台进程组&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="特殊之处"&gt;&lt;span&gt;特殊之处&lt;/span&gt;&#10; &lt;a href="#%e7%89%b9%e6%ae%8a%e4%b9%8b%e5%a4%84" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;ul&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;信号处理特殊&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;会忽略信号：&lt;code&gt;SIGINT&lt;/code&gt;，&lt;code&gt;SIGQUIT&lt;/code&gt;，&lt;code&gt;SIGTSTP&lt;/code&gt;，&lt;code&gt;SIGTTIN&lt;/code&gt;，&lt;code&gt;SIGTTOU&lt;/code&gt;&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;作业控制特殊&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;不在 &lt;code&gt;jobs&lt;/code&gt; 输出中&lt;/li&gt;&#10;&lt;li&gt;不能被 &lt;code&gt;fg/bg&lt;/code&gt; 管理&lt;/li&gt;&#10;&lt;li&gt;不能被 &lt;code&gt;Ctrl-Z&lt;/code&gt; 观其&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 class="heading-element" id="前后台进程组切换"&gt;&lt;span&gt;前后台进程组切换&lt;/span&gt;&#10; &lt;a href="#%e5%89%8d%e5%90%8e%e5%8f%b0%e8%bf%9b%e7%a8%8b%e7%bb%84%e5%88%87%e6%8d%a2" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;挂起前台作业&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt; 在前台进程组使用 &lt;code&gt;Ctrl-Z&lt;/code&gt; 向其发送 &lt;code&gt;SIGTSTP&lt;/code&gt; 信号，使其挂起，该作业变为后台作业&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;后台/挂起作业变为前台作业&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt; 使用 &lt;code&gt;fg&lt;/code&gt; 会将指定作业设为前台作业，并向其发送 &lt;code&gt;SIGCONT&lt;/code&gt; 信号&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;fg 背后调用的是 &lt;code&gt;tcsetpgrp&lt;/code&gt;（terminal control set process group），其能够设置前台进程组，与之对应的函数还有 &lt;code&gt;tcgetpgrp&lt;/code&gt;（terminal control get process group），获取前台进程组&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;继续后台运行挂起作业&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt; 使用 &lt;code&gt;bg&lt;/code&gt; 向指定挂起作业发送 &lt;code&gt;SIGCONT&lt;/code&gt;，并让其在后台运行&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h1 class="heading-element" id="进程生命周期"&gt;&lt;span&gt;进程生命周期&lt;/span&gt;&#10; &lt;a href="#%e8%bf%9b%e7%a8%8b%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;每个进程都有一个（Process Control Block），其记录着进程的状态，标识信息等，可以说，PCB 是进程的档案袋&lt;/p&gt;&#10;&lt;p&gt;在 Linux 中，PCB 的具体实现是 &lt;code&gt;task_struct&lt;/code&gt;，这是一个复杂且庞大的结构体，后面在深入再学习&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="创建"&gt;&lt;span&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;创建&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt;&lt;/span&gt;&#10; &lt;a href="#%e5%88%9b%e5%bb%ba" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;使用 &lt;code&gt;fork&lt;/code&gt; 函数创建，一般呢，会调用 &lt;code&gt;execv&lt;/code&gt; 类函数&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="阻塞"&gt;&lt;span&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;阻塞&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt;&lt;/span&gt;&#10; &lt;a href="#%e9%98%bb%e5%a1%9e" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;当进程内调用 &lt;code&gt;wait&lt;/code&gt;，&lt;code&gt;sleep&lt;/code&gt;，&lt;code&gt;read&lt;/code&gt; 等函数时，会进入阻塞状态，待到指定事件完成后，会自动恢复为运行状态&lt;/p&gt;&#10;&lt;p&gt;阻塞分为两种：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;em&gt;Interruptible Sleep（S）&lt;/em&gt; 能被信号唤醒，大多数阻塞属于这一类型&lt;/li&gt;&#10;&lt;li&gt;&lt;em&gt;Disk Sleep（D）&lt;/em&gt; 不能被信号唤醒，一般是 I/O 操作&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 class="heading-element" id="暂停"&gt;&lt;span&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;暂停&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt;&lt;/span&gt;&#10; &lt;a href="#%e6%9a%82%e5%81%9c" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;当进程收到 &lt;code&gt;SIGTSTP&lt;/code&gt; 或 &lt;code&gt;SIGSTOP&lt;/code&gt; 信号时，进程暂停，当接收到 &lt;code&gt;SIGCONT&lt;/code&gt; 信号时，进程恢复到 R 态&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="终止"&gt;&lt;span&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;终止&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt;&lt;/span&gt;&#10; &lt;a href="#%e7%bb%88%e6%ad%a2" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;终止分为两类&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;em&gt;正常终止&lt;/em&gt; 通过 &lt;code&gt;main&lt;/code&gt; 函数正常返回或通过 &lt;code&gt;exit/_exit&lt;/code&gt; 终止，退出状态码通过 &lt;code&gt;wait&lt;/code&gt; 交给父进程&lt;/li&gt;&#10;&lt;li&gt;&lt;em&gt;异常终止&lt;/em&gt; 接收到触发异常终止的信号：&lt;code&gt;SIGINT&lt;/code&gt;，&lt;code&gt;SIGQUIT&lt;/code&gt;，&lt;code&gt;SIGSEGV&lt;/code&gt;，&lt;code&gt;SIGTERM&lt;/code&gt;，&lt;code&gt;SIGKILL&lt;/code&gt; 等，可以通过 &lt;code&gt;wait&lt;/code&gt; 获取 &lt;code&gt;status&lt;/code&gt; 查看终止信号&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;当进程终止后，会进入僵尸状态（Z），等待父进程收尸&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="回收"&gt;&lt;span&gt;&lt;u&gt;&lt;strong&gt;&lt;code&gt;回收&lt;/code&gt;&lt;/strong&gt;&lt;/u&gt;&lt;/span&gt;&#10; &lt;a href="#%e5%9b%9e%e6%94%b6" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;p&gt;当子进程变为僵尸进程后，需要父进程使用 &lt;code&gt;wait/waitpid&lt;/code&gt; 为子进程收尸&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;pid_t wait(int *_Nullable wstatus);&#10;pid_t waitpid(pid_t pid, int *_Nullable wstatus, int options);&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;waitpid&lt;/code&gt; 函数是 &lt;code&gt;wait&lt;/code&gt; 的上位替代，所以这里重点讲解 &lt;code&gt;waitpid&lt;/code&gt;&#10;参数：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;code&gt;pid&lt;/code&gt;&lt;/u&gt;：详细规则如下表&lt;/p&gt;&#10;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;&amp;lt;-1&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;-1&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;0&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;&amp;gt;0&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;等待与 &lt;code&gt;pid&lt;/code&gt; 绝对值相等的进程组内所有子进程&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;等待所有子进程&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;等待同进程组内所有子进程&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;等待 &lt;code&gt;pid&lt;/code&gt; 所对应的子进程&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;code&gt;options&lt;/code&gt;&lt;/u&gt;&lt;/p&gt;&#10;&lt;p&gt;使用掩码进行传参&#10;有三种选择&lt;/p&gt;&#10;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;WNOHANG&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;WUNTRACED&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;WCONTINUED&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;如果没有子进程为僵尸进程则立即返回&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;如果一个子进程在暂停状态也会返回，且会记录在 &lt;code&gt;status&lt;/code&gt; 中&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;如果子进程被 &lt;code&gt;SIGCONT&lt;/code&gt; 信号恢复了也会返回&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;p&gt;当不使用 &lt;code&gt;WNOHANG&lt;/code&gt; 时，&lt;code&gt;waitpid&lt;/code&gt; 会一直等待指定子进程终止并收尸才返回，使用后如果就把等待变成询问，问一下子进程是否终止，终止就收尸，否则立即返回&lt;/p&gt;&#10;&lt;p&gt;当不使用 &lt;code&gt;WUNTRACED&lt;/code&gt; 时，&lt;code&gt;waitpid&lt;/code&gt; 会对不关心进程是否暂停，使用后，如果暂停，waitpid 也会立即返回，而且可以通过 &lt;code&gt;status&lt;/code&gt; 得到是因为暂停返回还是终止返回&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;WCONTINUED&lt;/code&gt; 与 &lt;code&gt;WUNTRACED&lt;/code&gt; 类似&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;u&gt;&lt;code&gt;wstatus&lt;/code&gt;&lt;/u&gt; &lt;code&gt;wstatus&lt;/code&gt; 需要传一个 int 指针，&lt;code&gt;wait&lt;/code&gt; 函数会把僵尸进程的信息写入 &lt;code&gt;wstatus&lt;/code&gt; 中，并通过指定宏读取 &lt;code&gt;wstatus&lt;/code&gt; 以获取信息，下面是常见宏&lt;/p&gt;&#10;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;WIFEXITED&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;WEXITSTATUS&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;WIFSIGNALED&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;WTERMSIG&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;WIFSTOPPED&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;WSTOPSIG&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;WIFCONTINUED&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;判断进程是否正常终止&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;获取正常终止时的退出状态码&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;判断进程是否是由于信号终止&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;获取使进程终止的信号&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;判断进程是否是在暂停（&lt;code&gt;options&lt;/code&gt; 必须使用 &lt;code&gt;WUNTRACED&lt;/code&gt;）&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;获取让进程暂停的信号&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;判断进程是否被 &lt;code&gt;SIGCONT&lt;/code&gt; 恢复了&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;&lt;code&gt;wait(wstatus)&lt;/code&gt; == &lt;code&gt;waitpid(-1,wstatus,0)&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;waitpid&lt;/code&gt; 函数每次返回只会处理一个子进程&lt;/p&gt;&#10;&lt;p&gt;返回值：&lt;/p&gt;&#10;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;-1&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;0&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;&amp;gt;0&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;无法找到对应子进程或被信号中断&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;只有在 &lt;code&gt;WNOHANG&lt;/code&gt; 的条件下才可能返回 0，代表没有找到了子进程，但其不为可报告的进程&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;成功，返回对应处理的子进程的 &lt;code&gt;pid&lt;/code&gt;&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;p&gt;&lt;code&gt;waitpid&lt;/code&gt; 能够回收僵尸进程，但必须要父进程调用才行了，那么问题来了，如果一个进程没有父进程，该怎么回收呢？这种没有父进程（或者说父进程已经终止）的进程称为孤儿进程&lt;/p&gt;&#10;&lt;p&gt;孤儿进程不需要我们操心了，内核会给孤儿找养父，让养父来为孤儿收尸，所以孤儿一般不会长期僵尸&lt;/p&gt;&#10;&lt;h1 class="heading-element" id="信号"&gt;&lt;span&gt;信号&lt;/span&gt;&#10; &lt;a href="#%e4%bf%a1%e5%8f%b7" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;在上面我们已经知道信号与进程紧密相连的，能够实现进程间的简单通信&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="信号的生命周期"&gt;&lt;span&gt;信号的生命周期&lt;/span&gt;&#10; &lt;a href="#%e4%bf%a1%e5%8f%b7%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;h3 class="heading-element" id="产生"&gt;&lt;span&gt;产生&lt;/span&gt;&#10; &lt;a href="#%e4%ba%a7%e7%94%9f" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;可由硬件，终端和内核产生，也可通过 &lt;code&gt;kill&lt;/code&gt;，&lt;code&gt;raise&lt;/code&gt; 方法手动发送信号&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="阻塞-1"&gt;&lt;span&gt;阻塞&lt;/span&gt;&#10; &lt;a href="#%e9%98%bb%e5%a1%9e-1" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;进程设置信号掩码，表示该进程暂不接收某些信号&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;sigset_t new_mask, old_mask;&#10; &#10;// 初始化新信号集，只包含 SIGINT&#10;sigemptyset(&amp;amp;new_mask);&#10;sigaddset(&amp;amp;new_mask, SIGINT);&#10;&#10;// 设置新的信号屏蔽字，阻塞 SIGINT 信号&#10;sigprocmask(SIG_BLOCK, &amp;amp;new_mask, &amp;amp;old_mask);&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;解除信号屏蔽&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;sigprocmask(SIG_UNBLOCK, &amp;amp;new_mask, &amp;amp;old_mask);&lt;/code&gt;&lt;/pre&gt;&lt;h3 class="heading-element" id="处理"&gt;&lt;span&gt;处理&lt;/span&gt;&#10; &lt;a href="#%e5%a4%84%e7%90%86" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;p&gt;进程接收到信号后，调用对应的信号处理函数进行处理，如果没有注册则会调用默认的处理函数，否则调用用户注册的函数&lt;/p&gt;&#10;&lt;p&gt;注册信号处理函数一般使用 &lt;code&gt;sigaction&lt;/code&gt; 函数&#10;函数原型&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt; int sigaction(int signum,&#10; const struct sigaction *_Nullable restrict act,&#10; struct sigaction *_Nullable restrict oldact);&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;其中：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;code&gt;signum&lt;/code&gt;&lt;/u&gt;：表示需要注册函数的信号码（不可以是 &lt;code&gt;SIGKILL&lt;/code&gt; 或 &lt;code&gt;SIGSTOP&lt;/code&gt;，不能为这两个信号注册函数）&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;code&gt;act&lt;/code&gt;&lt;/u&gt;：新的信号处理指针，可为 &lt;code&gt;NULL&lt;/code&gt;，表示不修改&lt;/li&gt;&#10;&lt;li&gt;&lt;u&gt;&lt;code&gt;oldact&lt;/code&gt;&lt;/u&gt;：保存旧的信号处理指针，可为 &lt;code&gt;NULL&lt;/code&gt;，表示不保存&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;两个信号处理指针都是 sigaction 结构体指针&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;struct sigaction {&#10; void (*sa_handler)(int);&#10; void (*sa_sigaction)(int, siginfo_t *, void *);&#10; sigset_t sa_mask;&#10; int sa_flags;&#10; void (*sa_restorer)(void);&#10;};&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;其中 &lt;code&gt;sa_handler&lt;/code&gt; 和 &lt;code&gt;sa_sigaction&lt;/code&gt; 是二选一&lt;/p&gt;&#10;&lt;h2 class="heading-element" id="常见信号"&gt;&lt;span&gt;常见信号&lt;/span&gt;&#10; &lt;a href="#%e5%b8%b8%e8%a7%81%e4%bf%a1%e5%8f%b7" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h2&gt;&lt;div class="table-wrapper"&gt;&#10; &lt;table data-sortable&gt;&lt;thead&gt;&#10; &lt;tr&gt;&#10; &lt;th style="text-align: center"&gt;&lt;code&gt;SIGINT&lt;/code&gt;&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;&lt;code&gt;SIGKILL&lt;/code&gt;&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;&lt;code&gt;SIGSTOP&lt;/code&gt;&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;&lt;code&gt;SIGTSTP&lt;/code&gt;&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;&lt;code&gt;SIGCHLD&lt;/code&gt;&lt;/th&gt;&#10; &lt;th style="text-align: center"&gt;&lt;code&gt;SIGCONT&lt;/code&gt;&lt;/th&gt;&#10; &lt;/tr&gt;&#10; &lt;/thead&gt;&#10; &lt;tbody&gt;&#10; &lt;tr&gt;&#10; &lt;td style="text-align: center"&gt;Terminated&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;Terminated&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;Stopped&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;Stopped&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;Ignore&lt;/td&gt;&#10; &lt;td style="text-align: center"&gt;Continue&lt;/td&gt;&#10; &lt;/tr&gt;&#10; &lt;/tbody&gt;&#10; &lt;/table&gt;&#10;&lt;/div&gt;&lt;p&gt;重点在&lt;code&gt;SIGCHLD&lt;/code&gt;信号，在子进程终止，暂停和继续时，父进程都会收到&lt;code&gt;SIGCHLD&lt;/code&gt;信号，一般我们通过&lt;code&gt;waitpid&lt;/code&gt;方法获得子进程相关信息进行收尸处理&lt;/p&gt;&#10;&lt;p&gt;信号的一大特点是：&lt;strong&gt;信号不排队，也没有引用计数&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;这也就意味着，当多个子进程终止时，父进程只会收到一个&lt;code&gt;SIGCHLD&lt;/code&gt;信号，因此，在为子进程收尸时，我们会使用循环保证处理所有需要的子进程&lt;/p&gt;&#10;</description></item></channel></rss>