요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
============
CPU Features
============
Hollis Blanchard <[email protected]>
5 Jun 2002
This document describes the system (including self-modifying code) used in the
PPC Linux kernel to support a variety of PowerPC CPUs without requiring
compile-time selection.
Early in the boot process the ppc32 kernel detects the current CPU type and
chooses a set of features accordingly. Some examples include Altivec support,
split instruction and data caches, and if the CPU supports the DOZE and NAP
sleep modes.
Detection of the feature set is simple. A list of processors can be found in
arch/powerpc/kernel/cputable.c. The PVR register is masked and compared with
each value in the list. If a match is found, the cpu_features of cur_cpu_spec
is assigned to the feature bitmask for this processor and a __setup_cpu
function is called.
C code may test 'cur_cpu_spec[smp_processor_id()]->cpu_features' for a
particular feature bit. This is done in quite a few places, for example
in ppc_setup_l2cr().
Implementing cpufeatures in assembly is a little more involved. There are
several paths that are performance-critical and would suffer if an array
index, structure dereference, and conditional branch were added. To avoid the
performance penalty but still allow for runtime (rather than compile-time) CPU
selection, unused code is replaced by 'nop' instructions. This nop'ing is
based on CPU 0's capabilities, so a multi-processor system with non-identical
processors will not work (but such a system would likely have other problems
anyways).
After detecting the processor type, the kernel patches out sections of code
that shouldn't be used by writing nop's over it. Using cpufeatures requires
just 2 macros (found in arch/powerpc/include/asm/cputable.h), as seen in 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 */
If CPU 0 supports Altivec, the code is left untouched. If it doesn't, both
instructions are replaced with nop's.
The END_FTR_SECTION macro has two simpler variations: END_FTR_SECTION_IFSET
and END_FTR_SECTION_IFCLR. These simply test if a flag is set (in
cur_cpu_spec[0]->cpu_features) or is cleared, respectively. These two macros
should be used in the majority of cases.
The END_FTR_SECTION macros are implemented by storing information about this
code in the '__ftr_fixup' ELF section. When do_cpu_ftr_fixups
(arch/powerpc/kernel/misc.S) is invoked, it will iterate over the records in
__ftr_fixup, and if the required feature is not present it will loop writing
nop's from each BEGIN_FTR_SECTION to END_FTR_SECTION.
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-25Boot 초기 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-35Assembly에서 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-49Processor 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`으로 교체합니다.
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`을 반복해서 기록합니다.
요약과 해설
cpu_features.rst:1-60C code는 feature bit를 직접 검사하지만 assembly hot path는 boot 때 `__ftr_fixup` record를 해석해 불필요한 instruction을 `nop`으로 덮습니다. CPU 0의 capability가 system 전체 기준입니다.