162306a36Sopenharmony_ciSPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ciChinese translated version of Documentation/arch/arm64/booting.rst
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciIf you have any comment or update to the content, please contact the
662306a36Sopenharmony_cioriginal document maintainer directly.  However, if you have a problem
762306a36Sopenharmony_cicommunicating in English you can also ask the Chinese maintainer for
862306a36Sopenharmony_cihelp.  Contact the Chinese maintainer if this translation is outdated
962306a36Sopenharmony_cior if there is a problem with the translation.
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciM:	Will Deacon <will.deacon@arm.com>
1262306a36Sopenharmony_cizh_CN:	Fu Wei <wefu@redhat.com>
1362306a36Sopenharmony_cizh_TW:	Hu Haowen <src.res.211@gmail.com>
1462306a36Sopenharmony_ciC:	55f058e7574c3615dea4615573a19bdb258696c6
1562306a36Sopenharmony_ci---------------------------------------------------------------------
1662306a36Sopenharmony_ciDocumentation/arch/arm64/booting.rst 的中文翻譯
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci如果想評論或更新本文的內容,請直接聯繫原文檔的維護者。如果你使用英文
1962306a36Sopenharmony_ci交流有困難的話,也可以向中文版維護者求助。如果本翻譯更新不及時或者翻
2062306a36Sopenharmony_ci譯存在問題,請聯繫中文版維護者。
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci英文版維護者: Will Deacon <will.deacon@arm.com>
2362306a36Sopenharmony_ci中文版維護者: 傅煒  Fu Wei <wefu@redhat.com>
2462306a36Sopenharmony_ci中文版翻譯者: 傅煒  Fu Wei <wefu@redhat.com>
2562306a36Sopenharmony_ci中文版校譯者: 傅煒  Fu Wei <wefu@redhat.com>
2662306a36Sopenharmony_ci繁體中文版校譯者: 胡皓文  Hu Haowen <src.res.211@gmail.com>
2762306a36Sopenharmony_ci本文翻譯提交時的 Git 檢出點爲: 55f058e7574c3615dea4615573a19bdb258696c6
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci以下爲正文
3062306a36Sopenharmony_ci---------------------------------------------------------------------
3162306a36Sopenharmony_ci			啓動 AArch64 Linux
3262306a36Sopenharmony_ci			==================
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci作者: Will Deacon <will.deacon@arm.com>
3562306a36Sopenharmony_ci日期: 2012 年 09 月 07 日
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci本文檔基於 Russell King 的 ARM 啓動文檔,且適用於所有公開發布的
3862306a36Sopenharmony_ciAArch64 Linux 內核代碼。
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciAArch64 異常模型由多個異常級(EL0 - EL3)組成,對於 EL0 和 EL1 異常級
4162306a36Sopenharmony_ci有對應的安全和非安全模式。EL2 是系統管理級,且僅存在於非安全模式下。
4262306a36Sopenharmony_ciEL3 是最高特權級,且僅存在於安全模式下。
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci基於本文檔的目的,我們將簡單地使用『引導裝載程序』(『boot loader』)
4562306a36Sopenharmony_ci這個術語來定義在將控制權交給 Linux 內核前 CPU 上執行的所有軟體。
4662306a36Sopenharmony_ci這可能包含安全監控和系統管理代碼,或者它可能只是一些用於準備最小啓動
4762306a36Sopenharmony_ci環境的指令。
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci基本上,引導裝載程序(至少)應實現以下操作:
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci1、設置和初始化 RAM
5262306a36Sopenharmony_ci2、設置設備樹數據
5362306a36Sopenharmony_ci3、解壓內核映像
5462306a36Sopenharmony_ci4、調用內核映像
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci1、設置和初始化 RAM
5862306a36Sopenharmony_ci-----------------
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci必要性: 強制
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci引導裝載程序應該找到並初始化系統中所有內核用於保持系統變量數據的 RAM。
6362306a36Sopenharmony_ci這個操作的執行方式因設備而異。(它可能使用內部算法來自動定位和計算所有
6462306a36Sopenharmony_ciRAM,或可能使用對這個設備已知的 RAM 信息,還可能是引導裝載程序設計者
6562306a36Sopenharmony_ci想到的任何合適的方法。)
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci2、設置設備樹數據
6962306a36Sopenharmony_ci---------------
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci必要性: 強制
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci設備樹數據塊(dtb)必須 8 字節對齊,且大小不能超過 2MB。由於設備樹
7462306a36Sopenharmony_ci數據塊將在使能緩存的情況下以 2MB 粒度被映射,故其不能被置於必須以特定
7562306a36Sopenharmony_ci屬性映射的2M區域內。
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci註: v4.2 之前的版本同時要求設備樹數據塊被置於從內核映像以下
7862306a36Sopenharmony_citext_offset 字節處算起第一個 512MB 內。
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci3、解壓內核映像
8162306a36Sopenharmony_ci-------------
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci必要性: 可選
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ciAArch64 內核當前沒有提供自解壓代碼,因此如果使用了壓縮內核映像文件
8662306a36Sopenharmony_ci(比如 Image.gz),則需要通過引導裝載程序(使用 gzip 等)來進行解壓。
8762306a36Sopenharmony_ci若引導裝載程序沒有實現這個功能,就要使用非壓縮內核映像文件。
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci4、調用內核映像
9162306a36Sopenharmony_ci-------------
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci必要性: 強制
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci已解壓的內核映像包含一個 64 字節的頭,內容如下:
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci  u32 code0;			/* 可執行代碼 */
9862306a36Sopenharmony_ci  u32 code1;			/* 可執行代碼 */
9962306a36Sopenharmony_ci  u64 text_offset;		/* 映像裝載偏移,小端模式 */
10062306a36Sopenharmony_ci  u64 image_size;		/* 映像實際大小, 小端模式 */
10162306a36Sopenharmony_ci  u64 flags;			/* 內核旗標, 小端模式 *
10262306a36Sopenharmony_ci  u64 res2	= 0;		/* 保留 */
10362306a36Sopenharmony_ci  u64 res3	= 0;		/* 保留 */
10462306a36Sopenharmony_ci  u64 res4	= 0;		/* 保留 */
10562306a36Sopenharmony_ci  u32 magic	= 0x644d5241;	/* 魔數, 小端, "ARM\x64" */
10662306a36Sopenharmony_ci  u32 res5;			/* 保留 (用於 PE COFF 偏移) */
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci映像頭注釋:
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci- 自 v3.17 起,除非另有說明,所有域都是小端模式。
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci- code0/code1 負責跳轉到 stext.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci- 當通過 EFI 啓動時, 最初 code0/code1 被跳過。
11662306a36Sopenharmony_ci  res5 是到 PE 文件頭的偏移,而 PE 文件頭含有 EFI 的啓動入口點
11762306a36Sopenharmony_ci  (efi_stub_entry)。當 stub 代碼完成了它的使命,它會跳轉到 code0
11862306a36Sopenharmony_ci  繼續正常的啓動流程。
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci- v3.17 之前,未明確指定 text_offset 的字節序。此時,image_size 爲零,
12162306a36Sopenharmony_ci  且 text_offset 依照內核字節序爲 0x80000。
12262306a36Sopenharmony_ci  當 image_size 非零,text_offset 爲小端模式且是有效值,應被引導加載
12362306a36Sopenharmony_ci  程序使用。當 image_size 爲零,text_offset 可假定爲 0x80000。
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci- flags 域 (v3.17 引入) 爲 64 位小端模式,其編碼如下:
12662306a36Sopenharmony_ci  位 0: 	內核字節序。 1 表示大端模式,0 表示小端模式。
12762306a36Sopenharmony_ci  位 1-2:	內核頁大小。
12862306a36Sopenharmony_ci			0 - 未指定。
12962306a36Sopenharmony_ci			1 - 4K
13062306a36Sopenharmony_ci			2 - 16K
13162306a36Sopenharmony_ci			3 - 64K
13262306a36Sopenharmony_ci  位 3:		內核物理位置
13362306a36Sopenharmony_ci			0 - 2MB 對齊基址應儘量靠近內存起始處,因爲
13462306a36Sopenharmony_ci			    其基址以下的內存無法通過線性映射訪問
13562306a36Sopenharmony_ci			1 - 2MB 對齊基址可以在物理內存的任意位置
13662306a36Sopenharmony_ci  位 4-63:	保留。
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci- 當 image_size 爲零時,引導裝載程序應試圖在內核映像末尾之後儘可能
13962306a36Sopenharmony_ci  多地保留空閒內存供內核直接使用。對內存空間的需求量因所選定的內核
14062306a36Sopenharmony_ci  特性而異, 並無實際限制。
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci內核映像必須被放置在任意一個可用系統內存 2MB 對齊基址的 text_offset
14362306a36Sopenharmony_ci字節處,並從該處被調用。2MB 對齊基址和內核映像起始地址之間的區域對於
14462306a36Sopenharmony_ci內核來說沒有特殊意義,且可能被用於其他目的。
14562306a36Sopenharmony_ci從映像起始地址算起,最少必須準備 image_size 字節的空閒內存供內核使用。
14662306a36Sopenharmony_ci註: v4.6 之前的版本無法使用內核映像物理偏移以下的內存,所以當時建議
14762306a36Sopenharmony_ci將映像儘量放置在靠近系統內存起始的地方。
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci任何提供給內核的內存(甚至在映像起始地址之前),若未從內核中標記爲保留
15062306a36Sopenharmony_ci(如在設備樹(dtb)的 memreserve 區域),都將被認爲對內核是可用。
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci在跳轉入內核前,必須符合以下狀態:
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci- 停止所有 DMA 設備,這樣內存數據就不會因爲虛假網絡包或磁碟數據而
15562306a36Sopenharmony_ci  被破壞。這可能可以節省你許多的調試時間。
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci- 主 CPU 通用寄存器設置
15862306a36Sopenharmony_ci  x0 = 系統 RAM 中設備樹數據塊(dtb)的物理地址。
15962306a36Sopenharmony_ci  x1 = 0 (保留,將來可能使用)
16062306a36Sopenharmony_ci  x2 = 0 (保留,將來可能使用)
16162306a36Sopenharmony_ci  x3 = 0 (保留,將來可能使用)
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci- CPU 模式
16462306a36Sopenharmony_ci  所有形式的中斷必須在 PSTATE.DAIF 中被屏蔽(Debug、SError、IRQ
16562306a36Sopenharmony_ci  和 FIQ)。
16662306a36Sopenharmony_ci  CPU 必須處於 EL2(推薦,可訪問虛擬化擴展)或非安全 EL1 模式下。
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci- 高速緩存、MMU
16962306a36Sopenharmony_ci  MMU 必須關閉。
17062306a36Sopenharmony_ci  指令緩存開啓或關閉皆可。
17162306a36Sopenharmony_ci  已載入的內核映像的相應內存區必須被清理,以達到緩存一致性點(PoC)。
17262306a36Sopenharmony_ci  當存在系統緩存或其他使能緩存的一致性主控器時,通常需使用虛擬地址
17362306a36Sopenharmony_ci  維護其緩存,而非 set/way 操作。
17462306a36Sopenharmony_ci  遵從通過虛擬地址操作維護構架緩存的系統緩存必須被配置,並可以被使能。
17562306a36Sopenharmony_ci  而不通過虛擬地址操作維護構架緩存的系統緩存(不推薦),必須被配置且
17662306a36Sopenharmony_ci  禁用。
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci  *譯者註:對於 PoC 以及緩存相關內容,請參考 ARMv8 構架參考手冊
17962306a36Sopenharmony_ci   ARM DDI 0487A
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci- 架構計時器
18262306a36Sopenharmony_ci  CNTFRQ 必須設定爲計時器的頻率,且 CNTVOFF 必須設定爲對所有 CPU
18362306a36Sopenharmony_ci  都一致的值。如果在 EL1 模式下進入內核,則 CNTHCTL_EL2 中的
18462306a36Sopenharmony_ci  EL1PCTEN (bit 0) 必須置位。
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci- 一致性
18762306a36Sopenharmony_ci  通過內核啓動的所有 CPU 在內核入口地址上必須處於相同的一致性域中。
18862306a36Sopenharmony_ci  這可能要根據具體實現來定義初始化過程,以使能每個CPU上對維護操作的
18962306a36Sopenharmony_ci  接收。
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci- 系統寄存器
19262306a36Sopenharmony_ci  在進入內核映像的異常級中,所有構架中可寫的系統寄存器必須通過軟體
19362306a36Sopenharmony_ci  在一個更高的異常級別下初始化,以防止在 未知 狀態下運行。
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci  對於擁有 GICv3 中斷控制器並以 v3 模式運行的系統:
19662306a36Sopenharmony_ci  - 如果 EL3 存在:
19762306a36Sopenharmony_ci    ICC_SRE_EL3.Enable (位 3) 必須初始化爲 0b1。
19862306a36Sopenharmony_ci    ICC_SRE_EL3.SRE (位 0) 必須初始化爲 0b1。
19962306a36Sopenharmony_ci  - 若內核運行在 EL1:
20062306a36Sopenharmony_ci    ICC_SRE_EL2.Enable (位 3) 必須初始化爲 0b1。
20162306a36Sopenharmony_ci    ICC_SRE_EL2.SRE (位 0) 必須初始化爲 0b1。
20262306a36Sopenharmony_ci  - 設備樹(DT)或 ACPI 表必須描述一個 GICv3 中斷控制器。
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci  對於擁有 GICv3 中斷控制器並以兼容(v2)模式運行的系統:
20562306a36Sopenharmony_ci  - 如果 EL3 存在:
20662306a36Sopenharmony_ci    ICC_SRE_EL3.SRE (位 0) 必須初始化爲 0b0。
20762306a36Sopenharmony_ci  - 若內核運行在 EL1:
20862306a36Sopenharmony_ci    ICC_SRE_EL2.SRE (位 0) 必須初始化爲 0b0。
20962306a36Sopenharmony_ci  - 設備樹(DT)或 ACPI 表必須描述一個 GICv2 中斷控制器。
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci以上對於 CPU 模式、高速緩存、MMU、架構計時器、一致性、系統寄存器的
21262306a36Sopenharmony_ci必要條件描述適用於所有 CPU。所有 CPU 必須在同一異常級別跳入內核。
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci引導裝載程序必須在每個 CPU 處於以下狀態時跳入內核入口:
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci- 主 CPU 必須直接跳入內核映像的第一條指令。通過此 CPU 傳遞的設備樹
21762306a36Sopenharmony_ci  數據塊必須在每個 CPU 節點中包含一個 『enable-method』 屬性,所
21862306a36Sopenharmony_ci  支持的 enable-method 請見下文。
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci  引導裝載程序必須生成這些設備樹屬性,並在跳入內核入口之前將其插入
22162306a36Sopenharmony_ci  數據塊。
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci- enable-method 爲 「spin-table」 的 CPU 必須在它們的 CPU
22462306a36Sopenharmony_ci  節點中包含一個 『cpu-release-addr』 屬性。這個屬性標識了一個
22562306a36Sopenharmony_ci  64 位自然對齊且初始化爲零的內存位置。
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci  這些 CPU 必須在內存保留區(通過設備樹中的 /memreserve/ 域傳遞
22862306a36Sopenharmony_ci  給內核)中自旋於內核之外,輪詢它們的 cpu-release-addr 位置(必須
22962306a36Sopenharmony_ci  包含在保留區中)。可通過插入 wfe 指令來降低忙循環開銷,而主 CPU 將
23062306a36Sopenharmony_ci  發出 sev 指令。當對 cpu-release-addr 所指位置的讀取操作返回非零值
23162306a36Sopenharmony_ci  時,CPU 必須跳入此值所指向的地址。此值爲一個單獨的 64 位小端值,
23262306a36Sopenharmony_ci  因此 CPU 須在跳轉前將所讀取的值轉換爲其本身的端模式。
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci- enable-method 爲 「psci」 的 CPU 保持在內核外(比如,在
23562306a36Sopenharmony_ci  memory 節點中描述爲內核空間的內存區外,或在通過設備樹 /memreserve/
23662306a36Sopenharmony_ci  域中描述爲內核保留區的空間中)。內核將會發起在 ARM 文檔(編號
23762306a36Sopenharmony_ci  ARM DEN 0022A:用於 ARM 上的電源狀態協調接口系統軟體)中描述的
23862306a36Sopenharmony_ci  CPU_ON 調用來將 CPU 帶入內核。
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci  *譯者注: ARM DEN 0022A 已更新到 ARM DEN 0022C。
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci  設備樹必須包含一個 『psci』 節點,請參考以下文檔:
24362306a36Sopenharmony_ci  Documentation/devicetree/bindings/arm/psci.yaml
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci- 輔助 CPU 通用寄存器設置
24762306a36Sopenharmony_ci  x0 = 0 (保留,將來可能使用)
24862306a36Sopenharmony_ci  x1 = 0 (保留,將來可能使用)
24962306a36Sopenharmony_ci  x2 = 0 (保留,將來可能使用)
25062306a36Sopenharmony_ci  x3 = 0 (保留,將來可能使用)
25162306a36Sopenharmony_ci
252