← Documents Documentation/arch/powerpc/cpu_features.rst GitHub 원문 ↗

Linux 6.18.37 · Architecture

PowerPC CPU Features

PVR 기반 runtime CPU feature detection과 assembly section nop patching을 설명합니다.

Source pathDocumentation/arch/powerpc/cpu_features.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

cpu_features.rst:1-60

C code는 feature bit를 직접 검사하지만 assembly hot path는 boot 때 `__ftr_fixup` record를 해석해 불필요한 instruction을 `nop`으로 덮습니다. CPU 0의 capability가 system 전체 기준입니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 ============
2 CPU Features
3 ============
4
5 Hollis Blanchard <[email protected]>
6 5 Jun 2002
7
8 This document describes the system (including self-modifying code) used in the
9 PPC Linux kernel to support a variety of PowerPC CPUs without requiring
10 compile-time selection.
11
12 Early in the boot process the ppc32 kernel detects the current CPU type and
13 chooses a set of features accordingly. Some examples include Altivec support,
14 split instruction and data caches, and if the CPU supports the DOZE and NAP
15 sleep modes.
16
17 Detection of the feature set is simple. A list of processors can be found in
18 arch/powerpc/kernel/cputable.c. The PVR register is masked and compared with
19 each value in the list. If a match is found, the cpu_features of cur_cpu_spec
20 is assigned to the feature bitmask for this processor and a __setup_cpu
21 function is called.
22
23 C code may test 'cur_cpu_spec[smp_processor_id()]->cpu_features' for a
24 particular feature bit. This is done in quite a few places, for example
25 in ppc_setup_l2cr().
26
27 Implementing cpufeatures in assembly is a little more involved. There are
28 several paths that are performance-critical and would suffer if an array
29 index, structure dereference, and conditional branch were added. To avoid the
30 performance penalty but still allow for runtime (rather than compile-time) CPU
31 selection, unused code is replaced by 'nop' instructions. This nop'ing is
32 based on CPU 0's capabilities, so a multi-processor system with non-identical
33 processors will not work (but such a system would likely have other problems
34 anyways).
35
36 After detecting the processor type, the kernel patches out sections of code
37 that shouldn't be used by writing nop's over it. Using cpufeatures requires
38 just 2 macros (found in arch/powerpc/include/asm/cputable.h), as seen in head.S
39 transfer_to_handler::
40
41 #ifdef CONFIG_ALTIVEC
42 BEGIN_FTR_SECTION
43 mfspr r22,SPRN_VRSAVE /* if G4, save vrsave register value */
44 stw r22,THREAD_VRSAVE(r23)
45 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
46 #endif /* CONFIG_ALTIVEC */
47
48 If CPU 0 supports Altivec, the code is left untouched. If it doesn't, both
49 instructions are replaced with nop's.
50
51 The END_FTR_SECTION macro has two simpler variations: END_FTR_SECTION_IFSET
52 and END_FTR_SECTION_IFCLR. These simply test if a flag is set (in
53 cur_cpu_spec[0]->cpu_features) or is cleared, respectively. These two macros
54 should be used in the majority of cases.
55
56 The END_FTR_SECTION macros are implemented by storing information about this
57 code in the '__ftr_fixup' ELF section. When do_cpu_ftr_fixups
58 (arch/powerpc/kernel/misc.S) is invoked, it will iterate over the records in
59 __ftr_fixup, and if the required feature is not present it will loop writing
60 nop's from each BEGIN_FTR_SECTION to END_FTR_SECTION.
61

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Runtime CPU feature 선택

1-10

저자는 Hollis Blanchard이며 문서 날짜는 2002년 6월 5일입니다. 이 문서는 compile-time CPU 선택 없이 여러 PowerPC CPU를 지원하기 위해 PPC Linux kernel이 사용하는 system과 self-modifying code를 설명합니다.

PVR 기반 feature detection과 C 사용법

11-25

Boot 초기 ppc32 kernel은 현재 CPU type을 감지해 해당 feature 집합을 선택합니다. 예로 Altivec 지원, instruction/data cache 분리, DOZE와 NAP sleep mode 지원 여부가 있습니다.

Feature set 감지는 단순합니다. `arch/powerpc/kernel/cputable.c`의 processor 목록을 순회하며 PVR register를 mask한 값과 각 항목을 비교합니다. Match하면 `cur_cpu_spec`의 `cpu_features`를 해당 processor feature bitmask로 설정하고 `__setup_cpu` function을 호출합니다.

C code는 특정 feature bit를 검사할 때 `cur_cpu_spec[smp_processor_id()]->cpu_features`를 사용할 수 있습니다. 실제 예는 `ppc_setup_l2cr()`입니다.

Assembly hot path의 runtime patching

26-35

Assembly에서 cpufeature를 구현하는 일은 더 복잡합니다. Performance-critical path마다 array index, structure dereference, conditional branch를 추가하면 비용이 큽니다. Runtime CPU 선택은 유지하면서 이 비용을 피하기 위해 사용하지 않는 code를 `nop` instruction으로 교체합니다.

이 nop patching은 CPU 0의 capability를 기준으로 하므로 processor가 서로 다른 multiprocessor system은 동작하지 않습니다. 그런 구성은 다른 문제도 일으킬 가능성이 큽니다.

Feature section macro와 Altivec 예

36-49

Processor type을 감지한 뒤 kernel은 사용하면 안 되는 code section 위에 `nop`을 써서 제거합니다. Cpufeature 사용에는 `arch/powerpc/include/asm/cputable.h`의 macro 두 개가 필요하며, `head.S`의 `transfer_to_handler`가 예를 보여 줍니다.

#ifdef CONFIG_ALTIVEC
BEGIN_FTR_SECTION
        mfspr        r22,SPRN_VRSAVE                /* if G4, save vrsave register value */
        stw        r22,THREAD_VRSAVE(r23)
END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
#endif /* CONFIG_ALTIVEC */

CPU 0이 Altivec를 지원하면 code를 그대로 두고, 지원하지 않으면 두 instruction을 모두 `nop`으로 교체합니다.

CPU feature fixup 결정
PVR match`cur_cpu_spec[0]->cpu_features`Feature presentInstruction 유지
PVR match`cur_cpu_spec[0]->cpu_features`Feature absent`nop` patch

Boot-time detection 결과가 feature section을 유지할지 nop으로 지울지 결정합니다.

`__ftr_fixup` ELF section

50-60

`END_FTR_SECTION` macro에는 더 단순한 `END_FTR_SECTION_IFSET`과 `END_FTR_SECTION_IFCLR` variation이 있습니다. 각각 `cur_cpu_spec[0]->cpu_features`에서 flag가 set되었는지 또는 clear되었는지만 검사하며 대부분의 경우 이 두 macro를 사용해야 합니다.

`END_FTR_SECTION` macro는 해당 code 정보를 `__ftr_fixup` ELF section에 저장하는 방식으로 구현됩니다. `arch/powerpc/kernel/misc.S`의 `do_cpu_ftr_fixups`가 호출되면 record를 순회하고 필요한 feature가 없을 때 각 `BEGIN_FTR_SECTION`부터 `END_FTR_SECTION`까지 `nop`을 반복해서 기록합니다.