免費試閱

免費試閱:三科各看一點

數學 109 台大資工所(整份 11 題)

15%

Consider \(x_1+x_2+\cdots+x_n=r\), where \(a\le x_i\le b\) for \(1\le i\le n\). What is the generating function for the number of integer solutions to the above equation (where the desired count appears as the coefficient of \(x^r\), where \(r=0,1,\ldots\))?

每個 \(x_i\):\(x^a+x^{a+1}+\cdots+x^b=\dfrac{x^a-x^{b+1}}{1-x}\)

共 \(n\) 個變數 → \(n\) 次方

\(\left(\dfrac{x^a-x^{b+1}}{1-x}\right)^{n}\)

25%

What is the number of functions from \(\{1,2,\ldots,n\}^m\) to \(\{1,2,\ldots,i\}^j\)?

\(|\text{domain}|=n^m\),\(|\text{codomain}|=i^j\)

函數個數 \(=|\text{codomain}|^{|\text{domain}|}\)

\(\left(i^{j}\right)^{n^m}\)

310%

Consider \(x_1+x_2+\cdots+x_n\lt r\), where \(x_i\ge 0\) for \(1\le i\le n\). What is its number of nonnegative integer solutions when \(n=4\) and \(r=8\)?

\(x_1+x_2+x_3+x_4\lt 8 \iff x_1+x_2+x_3+x_4\le 7\)

加 slack \(y\ge 0\):\(x_1+x_2+x_3+x_4+y=7\)

\(H(5,7)=\dbinom{11}{7}=\dbinom{11}{4}=\dfrac{11\cdot10\cdot9\cdot8}{4\cdot3\cdot2\cdot1}\)

\(330\)

410%

Derive the solution for \(a_n\) that satisfies the recurrence equation \(a_n=3a_{n-1}+n\) with \(a_0=1\).

齊次:\(r=3\) \(a_n^{(h)}=A\cdot 3^n\)

特解:設 \(a_n^{(p)}=Cn+D\)

\(Cn+D-3\big(C(n-1)+D\big)=n\)

\(-2Cn+(3C-2D)=n\) → \(C=-\tfrac12,\ D=-\tfrac34\)

\(a_n=A\cdot3^n-\tfrac12 n-\tfrac34\),\(a_0=1\Rightarrow A=\tfrac74\)

驗 \(a_1\):\(\tfrac{21}{4}-\tfrac24-\tfrac34=4=3\cdot1+1\)

\(a_n=\tfrac74\cdot3^n-\tfrac12 n-\tfrac34\)

510%

The generating function in partial fraction decomposition for the above recurrence equation is ______. (Note that expressions like \(\dfrac{x-8}{(x-3)^2}-\dfrac{9}{x-1}\) are not partial fraction decompositions.)

\(A(x)=\sum_{n\ge0}a_nx^n\),遞迴式兩邊乘 \(x^n\)、對 \(n\ge1\) 加總:

\(\big(A(x)-1\big)-3xA(x)=\dfrac{x}{(1-x)^2}\)

\(A(x)=\dfrac{1-x+x^2}{(1-3x)(1-x)^2}=\dfrac{\alpha}{1-3x}+\dfrac{\beta}{1-x}+\dfrac{\gamma}{(1-x)^2}\)

\(1-x+x^2=\alpha(1-x)^2+\beta(1-3x)(1-x)+\gamma(1-3x)\)

\(x=1\):\(1=-2\gamma\) → \(\gamma=-\tfrac12\)

\(x=\tfrac13\):\(\tfrac79=\tfrac49\alpha\) → \(\alpha=\tfrac74\)

\(x^2\) 係數:\(1=\alpha+3\beta\) → \(\beta=-\tfrac14\)

對第 4 題:\([x^n]=\tfrac74 3^n-\tfrac14-\tfrac12(n+1)\),一致

\(\dfrac{7/4}{1-3x}-\dfrac{1/4}{1-x}-\dfrac{1/2}{(1-x)^2}\)

610%

Prove the following inequality: \(\dbinom{n}{\lfloor n/2\rfloor}\ge\dfrac{2^n}{n}\), where \(2\le n\).

\(\sum_{k=0}^{n}\binom nk=2^n\)

把 \(\binom n0+\binom nn=2\) 併成一項,共 \(n\) 項:

\(2,\ \binom n1,\ \binom n2,\ \ldots,\ \binom n{n-1}\),總和 \(2^n\)

\(\binom n{\lfloor n/2\rfloor}\ge\binom nk\ \ \forall k\),且 \(\binom n{\lfloor n/2\rfloor}\ge\binom n1=n\ge2\)

⇒ \(\binom n{\lfloor n/2\rfloor}\) 是這 \(n\) 項中最大的

最大值 \(\ge\) 平均值:\(\dbinom n{\lfloor n/2\rfloor}\ge\dfrac{2^n}{n}\)

得證

710%

If the polynomial function \(f(x)=ax^4+bx^3+cx^2+dx+e\) satisfies

\(f(-2)=150,\ f(-1)=16,\ f(0)=2,\ f(1)=18,\ f(2)=166,\)

then \(a,b,c,d,e\) are ___, ___, ___, ___, ___, respectively.

\(f(0)=e=2\)

\(f(x)+f(-x)=2(ax^4+cx^2+e)\)

\(x=1\):\(34=2(a+c+2)\) → \(a+c=15\)

\(x=2\):\(316=2(16a+4c+2)\) → \(4a+c=39\)

→ \(a=8,\ c=7\)

\(f(x)-f(-x)=2(bx^3+dx)\)

\(x=1\):\(2=2(b+d)\) → \(b+d=1\)

\(x=2\):\(16=2(8b+2d)\) → \(4b+d=4\)

→ \(b=1,\ d=0\)

\(8,\ 1,\ 7,\ 0,\ 2\)

810%

The nullities of the matrices \(BB^T-\lambda I\) for \(\lambda=0,1,2,3,4\)

\(B=\begin{bmatrix}0&0&1&0\\-2&0&0&0\\0&0&0&0\\0&0&0&-1\\0&0&-1&0\end{bmatrix}\)

are ___, ___, ___, ___, ___, respectively.

\(B\) 的 column 兩兩正交,算 \(B^TB\)(4×4)比較快:

\(B^TB=\operatorname{diag}(4,\,0,\,2,\,1)\)

\(BB^T\) 與 \(B^TB\) 非零 eigenvalue 相同,\(BB^T\) 是 5×5

→ \(BB^T\) 的 eigenvalue:\(4,\,2,\,1,\,0,\,0\)

\(BB^T\) 對稱 ⇒ 可對角化,nullity \(=\) 重數

\(2,\ 1,\ 1,\ 0,\ 1\)

910%

Let \(A=\begin{bmatrix}2&0&0&2\\0&0&0&0\\0&0&0&0\\2&0&0&2\end{bmatrix}\). Let

\(U=\left\{a\begin{bmatrix}1\\0\\1\\0\end{bmatrix}+b\begin{bmatrix}0\\1\\0\\1\end{bmatrix}+c\begin{bmatrix}-1\\0\\1\\0\end{bmatrix}: a,b,c\in\mathbb R\right\}.\)

The numbers of elements \(-2,-1,0,1,2\) in a matrix \(B\) with \(Bx=\begin{cases}Ax & \text{if } x\in U\\ 0 & \text{if } x\in U^\perp\end{cases}\) are ___, ___, ___, ___, ___, respectively.

\(U=\operatorname{span}\{e_1,\ e_3,\ e_2+e_4\}\),\(U^\perp=\operatorname{span}\{e_2-e_4\}\)

\(Be_1=Ae_1=(2,0,0,2)^T\)

\(Be_3=Ae_3=0\)

\(B(e_2+e_4)=A(e_2+e_4)=(2,0,0,2)^T\)

\(B(e_2-e_4)=0\)

→ \(Be_2=Be_4=(1,0,0,1)^T\)

\(B=\begin{bmatrix}2&1&0&1\\0&0&0&0\\0&0&0&0\\2&1&0&1\end{bmatrix}\)

\(0,\ 0,\ 10,\ 4,\ 2\)

1010%

If \(A=\begin{bmatrix}0&-0.5&0&0&0\\0&0&0&0&0\\0.5&0&0&0&-0.5\\0&0&0&-1&0\end{bmatrix}\),

then the numbers of elements \(-2,-1,0,1,2\) in a matrix \(B\) with

\(ABA=A,\quad BAB=B,\quad (AB)^T=AB,\quad (BA)^T=BA\)

are ___, ___, ___, ___, ___, respectively.

四個條件 ⇒ \(B=A^{+}\)(Moore–Penrose)

\(A\) 的 row:\(r_1,r_3,r_4\) 兩兩正交,\(r_2=0\)

\(AA^T=\operatorname{diag}\!\left(\tfrac14,\,0,\,\tfrac12,\,1\right)\)

\(A^{+}=A^T(AA^T)^{+}=A^T\operatorname{diag}(4,\,0,\,2,\,1)\)

\(B=\begin{bmatrix}4r_1^T & 0 & 2r_3^T & r_4^T\end{bmatrix}=\begin{bmatrix}0&0&1&0\\-2&0&0&0\\0&0&0&0\\0&0&0&-1\\0&0&-1&0\end{bmatrix}\)

跟第 8 題的 \(B\) 是同一個矩陣

\(1,\ 2,\ 16,\ 1,\ 0\)

1110%

The numbers of elements \(0,1,2,3,4\) in a Jordan normal form of the matrix

\(A=\begin{bmatrix}4&4&2&1\\0&0&-1&-1\\-1&-1&2&0\\1&1&-1&1\end{bmatrix}\)

are ___, ___, ___, ___, ___, respectively.

col 1 \(=\) col 2 ⇒ \(\lambda=0\)

\(A-I\):row 3 \(=-\)row 4 ⇒ \(\lambda=1\)

\(\operatorname{tr}A=7\) ⇒ \(\lambda_3+\lambda_4=6\)

2×2 主子式和:\(0+10+3-1+1+2=15=\lambda_3\lambda_4+(\lambda_3+\lambda_4)\)

⇒ \(\lambda_3\lambda_4=9\) ⇒ \(\lambda_3=\lambda_4=3\)

\(\operatorname{rank}(A-3I)=3\) ⇒ GM \(=1\lt\) AM \(=2\)

\(J=\begin{bmatrix}0&0&0&0\\0&1&0&0\\0&0&3&1\\0&0&0&3\end{bmatrix}\)

\(12,\ 2,\ 0,\ 2,\ 0\)

軟體 107 台大資工所 資料結構與演算法

8–108 分

(II) (8 points) In a min-heap stored in an array, please answer the following questions after these 11 numbers have been inserted: 12, 8, 23, 21, 6, 1, 7, 25, 24, 22, 26.

8. (2 points) What is the height of the heap? (A) 2 (B) 3 (C) 4 (D) 5

9. (3 points) What is the value of the node at index 3? (A) 1 (B) 6 (C) 7 (D) 8

10. (3 points) What is the value of the left child of the node with value 12? (A) 22 (B) 24 (C) 25 (D) 26

逐個插到最後一格,再往上換(索引從 1 起算,父節點 \(\lfloor i/2\rfloor\)):

12 → [12]

8 → 與 12 換 → [8, 12]

23 → [8, 12, 23]

21 → [8, 12, 23, 21](父節點 12,不換)

6 → 索引 5,與 12 換、再與 8 換 → [6, 8, 23, 21, 12]

1 → 索引 6,與 23 換、再與 6 換 → [1, 8, 6, 21, 12, 23]

7 → 索引 7,父節點 6,不換 → [1, 8, 6, 21, 12, 23, 7]

25、24、22、26 → 父節點分別是 21、21、12、12,都比它們小,不換

最後 \(A=[1,\,8,\,6,\,21,\,12,\,23,\,7,\,25,\,24,\,22,\,26]\)

252124822122612367

8 \(n=11\):高度 \(\lfloor\lg 11\rfloor=3\)(邊數) → (B)

9 \(A[3]=6\) → (B)

10 12 在索引 5,左子節點索引 \(2\cdot5=10\),\(A[10]=22\) → (A)

選項有 2、3、4、5:樹高照 CLRS 以邊數計(\(\lfloor\lg n\rfloor\)),若用層數會是 4

8 (B) 9 (B) 10 (A)

13–1610 分

(IV) (10 points) Suppose we want to use trees to store the following 10 elements, inserted in this exact sequence: 88, 93, 76, 50, 46, 3, 32, 85, 30, 95.

13. (2 points) What is the height of the tree if we insert these 10 elements in sequence into a Binary Search Tree? (A) 4 (B) 6 (C) 7 (D) 8.

14. (2 points) In the Binary Search Tree from the previous question, suppose we remove the node with value 76, what's the parent of the node with value 50? (A) 46 (B) 85 (C) 88 (D) 93.

15. (3 points) Suppose we insert these 10 elements in sequence into an AVL Tree, what's value of the parent of the node with value 76? (A) 46 (B) 85 (C) 88 (D) 93.

16. (3 points) In the AVL tree from the previous question, suppose we remove the node with value 32, what's the parent of the node with value 30? (A) 3 (B) 46 (C) 50 (D) 76.

13 BST 插入結果:

3303246507685889395

最深路徑 \(88\to76\to50\to46\to3\to32\to30\),6 條邊 → 高度 6 → (B)

高度照 CLRS 以邊數計;若算節點數是 7,選項 (C) 就是這個陷阱。第 8 題的 heap 高度同一個規則

14 刪 76(兩個子節點 50、85):用 successor(右子樹最小)85 取代 76

33032465085889395

50 的父節點變成 85 → (B)

若改用 predecessor(左子樹最大 = 50)取代,50 直接接在 88 底下,答案就是 (C);以 CLRS 的 TREE-DELETE 用 successor 為準

15 AVL 逐個插入:

88, 93, 76, 50 不失衡

46:76 失衡(左左)→ 右旋 76 → 50 (46, 76)

3:88 失衡(左左)→ 右旋 88 → 根變 50:50 (46 (3), 88 (76, 93))

32:46 失衡(左右,32 掛在 3 的右邊)→ 先左旋 3、再右旋 46 → 32 (3, 46)

85、30、95 都不失衡

3303246507685889395

76 的父節點是 88 → (C)

16 刪 32(兩個子節點 3、46):successor 46 取代 32,此時 46 左邊高 2、右邊 0 → 失衡(左右)

先左旋 3 → 30 (3),再右旋 46 → 30 (3, 46)

33046507685889395

30 的父節點是 50 → (C)

改用 predecessor(30)取代 32 也不會失衡,30 的父節點同樣是 50

13 (B) 14 (B) 15 (C) 16 (C)

V (a)15 分

(V) (35 points) In the data structure deque, elements can be added to one end and removed from the other. Different from queues, a deque allows elements to be added to and removed from both ends.

(a) (15 points) Given a deque with a permutation of the numbers from 1 to \(n\) (\(n\ge5\)), we want to remove all of the numbers to form a sequence \(\langle a_1,a_2,a_3,\ldots,a_n\rangle\) that maximizes the alternative sum (\(a_1-a_2+a_3-\cdots\pm a_n\)), where \(a_1\) is the first number removed, \(a_2\) is the second number, and so on. For example, suppose a deque contains 5 numbers as follows: (head) 2, 3, 1, 4, 5 (rear). One possible generated sequence is \(\langle5,2,4,1,3\rangle\) whose alternative sum is 9.

(1) (5 points) Give an example to demonstrate that the problem cannot be solved by a greedy algorithm, which first removes the larger one of the two ends, then removing in the order of the smaller one, the larger one, the smaller one, etc. Show your calculation.

(2) (10 points) Devise an \(O(n^2)\) algorithm to generate the optimal sequence for a given deque. Explain how your algorithm works on the example you give previously.

(1) 反例:head 3, 1, 2, 4, 5 rear

greedy:

兩端 (3, 5) 取大 → 5,剩 3, 1, 2, 4 → \(+5\)

兩端 (3, 4) 取小 → 3,剩 1, 2, 4 → \(-3\)

兩端 (1, 4) 取大 → 4,剩 1, 2 → \(+4\)

兩端 (1, 2) 取小 → 1,剩 2 → \(-1\)

最後 2 → \(+2\)

\(5-3+4-1+2=7\)

另一種取法 \(\langle3,1,5,2,4\rangle\):取 head 3、head 1、rear 5、head 2、剩 4

\(3-1+5-2+4=9\gt7\) → greedy 不是最佳

題目給的範例 2, 3, 1, 4, 5 用 greedy 剛好也是 9,不能當反例

(2) DP。第 \(k\) 個被取出的數符號是 \(+\)(\(k\) 奇數)或 \(-\)(\(k\) 偶數)

剩下區間 \(A[i..j]\),長度 \(\ell=j-i+1\),下一個是第 \(n-\ell+1\) 個,符號只由 \(\ell\) 決定:\(s_\ell=+1\)(\(n-\ell\) 為偶)、\(-1\)(\(n-\ell\) 為奇)

\(D[i][j]\)=取完 \(A[i..j]\) 能得到的最大(帶符號)總和

\(D[i][j]=\max\big(s_\ell A[i]+D[i+1][j],\ \ s_\ell A[j]+D[i][j-1]\big)\),\(D[i][i-1]=0\)

依長度 \(\ell=1,2,\ldots,n\) 填表,答案 \(D[1][n]\);每格記錄選頭或選尾,從 \(D[1][n]\) 往回追就得到序列

狀態 \(O(n^2)\) 個,每個 \(O(1)\) → \(O(n^2)\) 時間

範例 \(A=(3,1,2,4,5)\),\(n=5\):\(\ell=5,3,1\) 符號 \(+\);\(\ell=4,2\) 符號 \(-\)

\(\ell\)(符號)\(D[1][\cdot]\)\(D[2][\cdot]\)\(D[3][\cdot]\)\(D[4][\cdot]\)\(D[5][\cdot]\)
1 (+)31245
2 (−)\(D[1][2]=2\)\(D[2][3]=1\)\(D[3][4]=2\)\(D[4][5]=1\)
3 (+)\(D[1][3]=4\)\(D[2][4]=5\)\(D[3][5]=7\)
4 (−)\(D[1][4]=2\)\(D[2][5]=6\)
5 (+)\(D[1][5]=9\)

例:\(D[1][5]=\max(3+D[2][5],\ 5+D[1][4])=\max(3+6,\ 5+2)=9\) → 先取 head 3

\(D[2][5]=\max(-1+D[3][5],\ -5+D[2][4])=\max(6,\ 0)\) → 取 head 1

\(D[3][5]=\max(2+D[4][5],\ 5+D[3][4])=\max(3,\ 7)\) → 取 rear 5

\(D[3][4]=\max(-2+D[4][4],\ -4+D[3][3])=\max(2,\ -2)\) → 取 head 2,最後 4

序列 \(\langle3,1,5,2,4\rangle\),最大交替和 \(9\)

硬體 台大資工所 計算機結構與作業系統

106 年

15%

Part I: Architecture

For a 5-stage MIPS processor with separate L1 data/instruction caches (32KB/256KB), the latency for each stage is listed below:

PipelineIFIDEXEMEMWB
Latency150ps100ps50ps200ps100ps

The CPI with a perfect cache is 1 and both I/D caches are blocking caches. The cache miss penalty is 200 CPU cycles. For the target benchmark suite, the I-Cache miss rate is 5%, D-cache miss rate is 10% and the frequency of the load/store instructions is 35%. The architecture team is considering to increase the data cache size to 512KB so the data miss rate could be reduced to 5% for the target benchmark. But it increases the MEM pipeline stage latency to 250ps. Please justify if increasing the data size is a good design decision or not.

cycle time = 最慢的 stage

原設計:\(T_c=\max(150,100,50,200,100)=200\) ps

\(\text{CPI}=1+0.05\times200+0.35\times0.10\times200=1+10+7=18\)

每條指令時間 \(=18\times200=3600\) ps

新設計:MEM 變 250 ps → \(T_c=250\) ps

\(\text{CPI}=1+0.05\times200+0.35\times0.05\times200=1+10+3.5=14.5\)

每條指令時間 \(=14.5\times250=3625\) ps

\(\dfrac{3600}{3625}\approx0.993\) → 新設計慢約 0.7%

CPI 降 19%,但 cycle time 拉長 25%;I-cache 的 10 cycles stall 兩邊都有,D-cache 省下的 3.5 cycles 不夠抵

不是好的設計(3625 ps > 3600 ps)

55%

For the MIPS instruction set (32-bit instruction/32 registers), if the register file size is increased to 128 registers, what is the total number of bits needed for a R-type format instruction (register instructions)? Could more registers decrease the size of a MIPS assembly program? Why?

128 個 register:\(\log_2128=7\) bits 一個欄位

R-type:opcode 6 | rs 7 | rt 7 | rd 7 | shamt 5 | funct 6

\(6+7+7+7+5+6=38\) bits

程式大小:可以變小

register 不夠時,compiler 要把變數 spill 到記憶體,多出 lw/sw;register 變多,變數可以留在 register,這些 load/store 就不用了 → 指令數下降

但每條指令變寬(38 bits > 32 bits,原本的 32-bit 格式放不下);總 byte 數是否變小要看省下的指令與變寬的代價誰多

38 bits;可以(減少 spill 的 load/store,指令數變少)

107 年

93 分

(3 points) Answer TRUE or FALSE: Consider the L1 Cache in Intel i7-6700 (Skylake) with 32 KB, 64 B/line and 8-WAY L1 cache, 4 KB pages mode. If Virtual Address Indexed, Physical Address Tagged cache (VIPT) is used, one RAM data may be repeated placed in different cache slots at the same time.

line 數 \(=32\text{K}/64=512\);set 數 \(=512/8=64\) → index \(=6\) bit,block offset \(=6\) bit

index + offset \(=12\) bit \(=\) 4 KB page 的 page offset(VA 與 PA 相同的位元)

→ 用 VA 取 index 和用 PA 取 index 結果一樣,同一份實體資料只會進同一個 set,沒有 synonym/aliasing

F

1025 分

Part II: Operating System. NOTE that in the question, it is intended to provide redundant or miss certain assumption to disguise you. Please make your own assumption if necessary to answer the questions.

10. (25 points) On Jan. 2, 2018, it was announced that the meltdown vulnerability of Intel processors may allow normal user programs – from database applications to JavaScript in web browsers – to discern to some extent the layout or contents of protected kernel memory areas. Please answer the following questions.

(a) (5 points) The meltdown vulnerability is caused by shared translation lookaside buffer (TLB) in order to reduce the latency for user/kernel mode switch. Please describe how TLB works while translating virtual address to physical address.

(b) (6 points) The page table shown below is for a system with 16-bit virtual and physical addresses and with 4,096-byte pages. Please translate the following 16-bit virtual addresses into their corresponding physical address in hexadecimal. 1. 0xA12C 2. 0x3A9D 3. 0xB2D0

Page #0123456789101112131415
Page Frame #45-2--912--15-13--
Reference Bit0001000000000000

(c) (4 points) On a 64-bit address computer paging physical memory into 4KB page frame, a simple page table can take too much space to map memory pages. How does the operating system do to avoid using huge page table? Please provide at least two different approaches. (It can be different on Intel and ARM processors.)

(d) (5 points) Please describe the possible method to prevent the security vulnerability on TLB without replacing the processor and discuss the performance impacts to the operating systems.

(e) (5 points) The execution of one user process may switch between user mode and kernel model. Please describe the procedure of mode switch and answer if requesting for the PID of a process, i.e., getpid() in POSIX, requires a mode switch.

(a) TLB=MMU 裡存「VPN → frame + 權限位元」的小型 cache(常帶 ASID/PCID)

VA 拆成 VPN + offset,先用 VPN 查 TLB:

hit:直接拿到 frame number,PA \(=\) frame ‖ offset,順便檢查權限,不碰記憶體 → 約 1 cycle

miss:走 page table(hardware page walk 或由 OS 處理),到記憶體裡查;找到且 valid → 把 entry 填進 TLB(必要時替換舊的)→ 再轉換一次

查不到(invalid)→ page fault,交給 OS 把頁載入

題目說 Meltdown 由 shared TLB 造成,不精確:成因是 out-of-order/speculative 在權限檢查完成前先讀了 kernel 資料,再靠 cache timing 洩漏;kernel 映射留在 user 的 page table(切換 mode 不必 flush TLB)是讓它讀得到的前提,(d) 的 KPTI 就是拿掉這個前提

(b) 4096 B \(=2^{12}\):低 12 bit 是 offset,高 4 bit 是 page #

VApage #offsetframePA
0xA12C0xA = 100x12C15 = 0xF0xF12C
0x3A9D0x3 = 30xA9D20x2A9D
0xB2D00xB = 110x2D0—page fault

page 11 沒有 frame("-"),不在記憶體裡,不能轉成實體位址

(c) 不要為整個 \(2^{64}\) 的空間一次配一張大表:

① multi-level(hierarchical)page table:x86-64 是 4 層(現在可到 5 層)、ARMv8 也是多層;沒用到的區段不配下一層的表,只配實際用到的

② inverted page table(或 hashed page table):表的大小跟 physical memory 的 frame 數成正比,不是跟 virtual space 成正比,用 hash 找 entry

③ 搭配 huge page(2 MB/1 GB):一個 entry 蓋更大範圍,少一層、表更小,TLB 也更夠用

(d) 軟體方法:KPTI(Kernel Page Table Isolation,Linux 的 KAISER)

user mode 用的 page table 只映射使用者空間,加上進出 kernel 必需的一點點 trampoline;kernel 其餘位址不在裡面 → speculative 也讀不到

進 kernel(syscall/interrupt)時切到完整的 kernel page table(改 CR3),回 user 再切回來

效能代價:每次 syscall/interrupt 多兩次 CR3 切換;沒有 PCID 就要 flush TLB,之後一堆 TLB miss;syscall、中斷密集的 workload(I/O、資料庫)慢約 5–30%,計算為主的幾乎沒差

有 PCID(TLB entry 帶 tag)可以不必整個 flush,把代價壓小

(e) 程序(trap/syscall):

① user 執行 syscall/trap 指令(或發生 interrupt/exception)

② 硬體:mode bit 切成 kernel、存 PC 與 flags、換成 kernel stack,依 vector/syscall table 跳到 handler

③ kernel 存其餘 register,檢查參數,執行服務

④ 還原 register,sysret/iret(return-from-trap)把 mode 切回 user、PC 還原,繼續執行

getpid():PID 存在 kernel 的 PCB(task_struct),user 不能直接讀 → 需要 mode switch(它是 system call)

(b) 0xF12C 0x2A9D page fault  (e) getpid() 需要 mode switch

台大其他年度、其他學校也是這樣寫的。

國立臺灣大學 106–115 數學、軟體、硬體全年度完整詳解共 367 頁。

購買 · NT$ 850 看台大各年度考點分析

聯絡我們

題目有錯、詳解看不懂,都歡迎來信

想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。

william.chou.123123@gmail.com