x86: Use macros for selectors in asm
Sean Christopherson
reviewercover x86: Use macros for selectors in asm
Use macros for segment selectors in assembly instead of open coded literals.
v2:
- Commit to using __ASSEMBLER__.
v1: https://lore.kernel.org/all/[email protected]
Hang SU (1):
x86: replace segment selector magic number with macro definition
Sean Christopherson (2):
x86: Move descriptor table selector #defines to the top of desc.h
x86: Commit to using __ASSEMBLER__ instead of __ASSEMBLY__
lib/x86/asm/page.h | 4 +- lib/x86/desc.h | 123 +++++++++++++++++++++++---------------------- x86/cstart64.S | 8 +-- x86/trampolines.S | 8 +-- 4 files changed, 75 insertions(+), 68 deletions(-)
base-commit: f77fb696cfd0e4a5562cdca189be557946bf522f
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Sean Christopherson
reviewerPATCH 1/3 x86: Move descriptor table selector #defines to the top of desc.h
Hoist the selector #defines in desc.h to the very top so that they can be exposed to assembly code with minimal #ifdefs.
No functional change intended.
--- lib/x86/desc.h | 114 ++++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 57 deletions(-)
lib/x86/desc.h +57 -57
@@ -1,7 +1,61 @@#ifndef _X86_DESC_H_#define _X86_DESC_H_ -#include <setjmp.h> +/* +* selector 32-bit 64-bit +* 0x00 NULL descriptor NULL descriptor +* 0x08 ring-0 code segment (32-bit) ring-0 code segment (64-bit) +* 0x10 ring-0 data segment (32-bit) ring-0 data segment (32/64-bit) +* 0x18 ring-0 code segment (P=0) ring-0 code segment (64-bit, P=0) +* 0x20 intr_alt_stack TSS ring-0 code segment (32-bit) +* 0x28 ring-0 code segment (16-bit) same +* 0x30 ring-0 data segment (16-bit) same +* 0x38 (0x3b) ring-3 code segment (32-bit) same +* 0x40 (0x43) ring-3 data segment (32-bit) ring-3 data segment (32/64-bit) +* 0x48 (0x4b) **unused** ring-3 code segment (64-bit) +* 0x50-0x78 free to use for test cases same +* 0x80-0x870 primary TSS (CPU 0..254) same +* 0x878-0x1068 percpu area (CPU 0..254) not used +* +* Note that the same segment can be used for 32-bit and 64-bit data segments +* (the L bit is only defined for code segments) +* +* Selectors 0x08-0x10 and 0x3b-0x4b are set up for use with the SYSCALL +* and SYSRET instructions. +*/ + +#define KERNEL_CS 0x08 +#define KERNEL_DS 0x10 +#define NP_SEL 0x18 +#ifdef __x86_64__ +#define KERNEL_CS32 0x20 +#else +#define TSS_INTR 0x20 +#endif +#define KERNEL_CS16 0x28 +#define KERNEL_DS16 0x30 +#define USER_CS32 0x3b +#define USER_DS 0x43 +#ifdef __x86_64__ +#define USER_CS64 0x4b +#endif + +/* Synonyms */ +#define KERNEL_DS32 KERNEL_DS +#define USER_DS32 USER_DS + +#ifdef __x86_64__ +#define KERNEL_CS64 KERNEL_CS +#define USER_CS USER_CS64 +#define KERNEL_DS64 KERNEL_DS +#define USER_DS64 USER_DS +#else +#define KERNEL_CS32 KERNEL_CS +#define USER_CS USER_CS32 +#endif + +#define FIRST_SPARE_SEL 0x50 +#define TSS_MAIN 0x80#ifdef __ASSEMBLY__#define __ASM_FORM (x, ...) x,## __VA_ARGS__
@@ -15,6 +69,8 @@#define __ASM_SEL (a,b) __ASM_FORM(b)#endif +#include <setjmp.h> +void setup_idt (void );void load_idt (void );void setup_alt_stack (void );
@@ -120,62 +176,6 @@ fep_unavailable:return false; } -/* -* selector 32-bit 64-bit -* 0x00 NULL descriptor NULL descriptor -* 0x08 ring-0 code segment (32-bit) ring-0 code segment (64-bit) -* 0x10 ring-0 data segment (32-bit) ring-0 data segment (32/64-bit) -* 0x18 ring-0 code segment (P=0) ring-0 code segment (64-bit, P=0) -* 0x20 intr_alt_stack TSS ring-0 code segment (32-bit) -* 0x28 ring-0 code segment (16-bit) same -* 0x30 ring-0 data segment (16-bit) same -* 0x38 (0x3b) ring-3 code segment (32-bit) same -* 0x40 (0x43) ring-3 data segment (32-bit) ring-3 data segment (32/64-bit) -* 0x48 (0x4b) **unused** ring-3 code segment (64-bit) -* 0x50-0x78 free to use for test cases same -* 0x80-0x870 primary TSS (CPU 0..254) same -* 0x878-0x1068 percpu area (CPU 0..254) not used -* -* Note that the same segment can be used for 32-bit and 64-bit data segments -* (the L bit is only defined for code segments) -* -* Selectors 0x08-0x10 and 0x3b-0x4b are set up for use with the SYSCALL -* and SYSRET instructions. -*/ - -#define KERNEL_CS 0x08 -#define KERNEL_DS 0x10 -#define NP_SEL 0x18 -#ifdef __x86_64__ -#define KERNEL_CS32 0x20 -#else -#define TSS_INTR 0x20 -#endif -#define KERNEL_CS16 0x28 -#define KERNEL_DS16 0x30 -#define USER_CS32 0x3b -#define USER_DS 0x43 -#ifdef __x86_64__ -#define USER_CS64 0x4b -#endif - -/* Synonyms */ -#define KERNEL_DS32 KERNEL_DS -#define USER_DS32 USER_DS - -#ifdef __x86_64__ -#define KERNEL_CS64 KERNEL_CS -#define USER_CS USER_CS64 -#define KERNEL_DS64 KERNEL_DS -#define USER_DS64 USER_DS -#else -#define KERNEL_CS32 KERNEL_CS -#define USER_CS USER_CS32 -#endif - -#define FIRST_SPARE_SEL 0x50 -#define TSS_MAIN 0x80 - typedef struct { unsigned short offset0; unsigned short selector;
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Sean Christopherson
reviewerPATCH 2/3 x86: Commit to using __ASSEMBLER__ instead of __ASSEMBLY__
In reply to Sean ChristophersonConvert all two of x86's anti-assembly #ifdefs from __ASSEMBLY__ to __ASSEMBLER__. Usage of __ASSEMBLY__ was inherited blindly from the Linux kernel, and must be manually defined, e.g. through build rules or with explicit #defines in assembly code. __ASSEMBLER__ on the other hand is automatically defined by the compiler when preprocessing assembly, i.e. doesn't require manually #defines for the code to function correctly.
Convert only x86 for the time being, as x86 doesn't actually rely on __ASSEMBLY__ (a clever observer will note that it's never #defined on x86). E.g. trying to include x86's page.h doesn't work as is. All other architectures actually rely on __ASSEMBLY__, and will be dealt with separately.
Note, while only gcc appears to officially document __ASSEMBLER__, clang has followed suit since at least clang 6.0, and clang 6.0 doesn't come remotely close to being able to comple KVM-Unit-Tests.
--- lib/x86/asm/page.h | 4 ++-- lib/x86/desc.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
lib/x86/asm/page.h +2 -2
@@ -15,7 +15,7 @@ typedef unsigned long pgd_t;#include <asm-generic/page.h> -#ifndef __ASSEMBLY__ +#ifndef __ASSEMBLER__#define PAGE_ALIGN (addr) ALIGN(addr, PAGE_SIZE)
@@ -79,5 +79,5 @@ extern unsigned long long get_amd_sev_addr_upperbound(void);#define PGDIR_BITS (lvl) (((lvl) - 1) * PGDIR_WIDTH + PAGE_SHIFT)#define PGDIR_OFFSET (va, lvl) (((va) >> PGDIR_BITS(lvl)) & PGDIR_MASK) -#endif /* !__ASSEMBLY__ */ +#endif /* !__ASSEMBLER__ */ #endif
lib/x86/desc.h +1 -1
@@ -57,7 +57,7 @@#define FIRST_SPARE_SEL 0x50#define TSS_MAIN 0x80 -#ifdef __ASSEMBLY__ +#ifdef __ASSEMBLER__#define __ASM_FORM (x, ...) x,## __VA_ARGS__#else #define __ASM_FORM (x, ...) " " xstr(x,##__VA_ARGS__) " "
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Sean Christopherson
reviewerPATCH 3/3 x86: replace segment selector magic number with macro definition
In reply to Sean ChristophersonFrom: Hang SU <[email protected]>
Add assembly check in desc.h, to replace segment selector magic number with macro definition.
--- lib/x86/desc.h | 7 ++++++- x86/cstart64.S | 8 ++++---- x86/trampolines.S | 8 +++++--- 3 files changed, 15 insertions(+), 8 deletions(-)
lib/x86/desc.h +6 -1
@@ -69,6 +69,8 @@#define __ASM_SEL (a,b) __ASM_FORM(b)#endif +#ifndef __ASSEMBLER__ +#include <setjmp.h> void setup_idt (void );
@@ -338,4 +340,7 @@ do { \#define asm_safe_report_ex __asm_safe_report -#endif + +#endif /* __ASSEMBLER__ */ + +#endif /* _X86_DESC_H_ */
x86/cstart64.S +4 -4
@@ -1,5 +1,5 @@ - #include "apic-defs.h" +#include "desc.h" ipi_vector = 0x20
@@ -66,7 +66,7 @@ start: mov $stacktop, %esp setup_percpu_area call prepare_64 - jmpl $8, $start64 + jmpl $KERNEL_CS, $start64 switch_to_5level: /* Disable CR4.PCIDE */
@@ -86,11 +86,11 @@ switch_to_5level: bts $12, %eax mov %eax, %cr4 - mov $0x10, %ax + mov $KERNEL_DS, %ax mov %ax, %ss call enter_long_mode - jmpl $8, $lvl5 + jmpl $KERNEL_CS, $lvl5 smp_stacktop: .long stacktop - 4096
x86/trampolines.S +5 -3
@@ -3,6 +3,8 @@ * transition from 32-bit to 64-bit code (x86-64 only) */ +#include "desc.h" + /* EFI provides it's own SIPI sequence to handle relocation. */ #ifndef CONFIG_EFI .code16
@@ -15,7 +17,7 @@ sipi_entry: or $1, %eax mov %eax, %cr0 lgdtl ap_rm_gdt_descr - sipi_entry - ljmpl $8, $ap_start32 + ljmpl $KERNEL_CS32, $ap_start32 sipi_end: .globl ap_rm_gdt_descr
@@ -66,7 +68,7 @@ MSR_GS_BASE = 0xc0000101 mov $MSR_GS_BASE, %ecx rdmsr - mov $0x10, %bx + mov $KERNEL_DS, %bx mov %bx, %ds mov %bx, %es mov %bx, %fs
@@ -123,7 +125,7 @@ ap_start32: call prepare_64 load_absolute_addr $ap_start64, %edx - pushl $0x08 + pushl $KERNEL_CS pushl %edx lretl #endif
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Sean Christopherson
reviewerRe: [kvm-unit-tests PATCH 3/3] x86: replace segment selector magic number with macro definition
x86/trampolines.S sipi_entry: · line 20
Quoted 6 lines · Sean Christopherson
x86/trampolines.S +1
-1@@ -15,7 +17,7 @@ sipi_entry: or $1, %eax mov %eax, %cr0 lgdtl ap_rm_gdt_descr - sipi_entry - ljmpl $8, $ap_start32 + ljmpl $KERNEL_CS32, $ap_start32
Gah, this is wrong, it should be KERNEL_CS, not KERNEL_CS32. It was wrong in the original posting as well. Not sure how I missed it the first time around.
I'll fixup when applying.
Quoted 8 lines · Sean Christopherson
Use macros for segment selectors in assembly instead of open coded literals.
v2:
- Commit to using__ASSEMBLER__.v1: https://lore.kernel.org/all/[email protected]
[...]
Applied to kvm-x86 next (and now pulled by Paolo), with the fixup to use KERNEL_CS instead of KERNEL_CS32.
[1/3] x86: Move descriptor table selector #defines to the top of desc.h
https://github.com/kvm-x86/kvm-unit-tests/commit/4c5d37137baf [2/3] x86: Commit to using __ASSEMBLER__ instead of __ASSEMBLY__ https://github.com/kvm-x86/kvm-unit-tests/commit/f372d35fb1be [3/3] x86: replace segment selector magic number with macro definition https://github.com/kvm-x86/kvm-unit-tests/commit/c8a8a35827d7