All of lore.kernel.org
 help / color / mirror / Atom feed
* [kvm-unit-tests PATCH 0/3] x86: Use macros for selectors in asm
@ 2025-02-21 22:54 Sean Christopherson
  2025-02-21 22:54 ` [kvm-unit-tests PATCH 1/3] x86: Move descriptor table selector #defines to the top of desc.h Sean Christopherson
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Sean Christopherson @ 2025-02-21 22:54 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: kvm, Sean Christopherson, Hang SU

Use macros for segment selectors in assembly instead of open coded literals.

v2:
 - Commit to using __ASSEMBLER__.

v1: https://lore.kernel.org/all/20240621122640.2347541-1-darcy.sh@antgroup.com


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
-- 
2.48.1.601.g30ceb7b040-goog


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [kvm-unit-tests PATCH 1/3] x86: Move descriptor table selector #defines to the top of desc.h
  2025-02-21 22:54 [kvm-unit-tests PATCH 0/3] x86: Use macros for selectors in asm Sean Christopherson
@ 2025-02-21 22:54 ` Sean Christopherson
  2025-02-21 22:54 ` [kvm-unit-tests PATCH 2/3] x86: Commit to using __ASSEMBLER__ instead of __ASSEMBLY__ Sean Christopherson
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2025-02-21 22:54 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: kvm, Sean Christopherson, Hang SU

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.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 lib/x86/desc.h | 114 ++++++++++++++++++++++++-------------------------
 1 file changed, 57 insertions(+), 57 deletions(-)

diff --git a/lib/x86/desc.h b/lib/x86/desc.h
index 92c45a48..a4459127 100644
--- a/lib/x86/desc.h
+++ b/lib/x86/desc.h
@@ -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;
-- 
2.48.1.601.g30ceb7b040-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [kvm-unit-tests PATCH 2/3] x86: Commit to using __ASSEMBLER__ instead of __ASSEMBLY__
  2025-02-21 22:54 [kvm-unit-tests PATCH 0/3] x86: Use macros for selectors in asm Sean Christopherson
  2025-02-21 22:54 ` [kvm-unit-tests PATCH 1/3] x86: Move descriptor table selector #defines to the top of desc.h Sean Christopherson
@ 2025-02-21 22:54 ` Sean Christopherson
  2025-02-21 22:54 ` [kvm-unit-tests PATCH 3/3] x86: replace segment selector magic number with macro definition Sean Christopherson
  2025-02-24 17:24 ` [kvm-unit-tests PATCH 0/3] x86: Use macros for selectors in asm Sean Christopherson
  3 siblings, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2025-02-21 22:54 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: kvm, Sean Christopherson, Hang SU

Convert 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.

Link: https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html#Standard-Predefined-Macros
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 lib/x86/asm/page.h | 4 ++--
 lib/x86/desc.h     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/x86/asm/page.h b/lib/x86/asm/page.h
index 298e7e8e..bc0e78c7 100644
--- a/lib/x86/asm/page.h
+++ b/lib/x86/asm/page.h
@@ -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
diff --git a/lib/x86/desc.h b/lib/x86/desc.h
index a4459127..aa6213d1 100644
--- a/lib/x86/desc.h
+++ b/lib/x86/desc.h
@@ -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__) " "
-- 
2.48.1.601.g30ceb7b040-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [kvm-unit-tests PATCH 3/3] x86: replace segment selector magic number with macro definition
  2025-02-21 22:54 [kvm-unit-tests PATCH 0/3] x86: Use macros for selectors in asm Sean Christopherson
  2025-02-21 22:54 ` [kvm-unit-tests PATCH 1/3] x86: Move descriptor table selector #defines to the top of desc.h Sean Christopherson
  2025-02-21 22:54 ` [kvm-unit-tests PATCH 2/3] x86: Commit to using __ASSEMBLER__ instead of __ASSEMBLY__ Sean Christopherson
@ 2025-02-21 22:54 ` Sean Christopherson
  2025-02-21 23:34   ` Sean Christopherson
  2025-02-24 17:24 ` [kvm-unit-tests PATCH 0/3] x86: Use macros for selectors in asm Sean Christopherson
  3 siblings, 1 reply; 6+ messages in thread
From: Sean Christopherson @ 2025-02-21 22:54 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: kvm, Sean Christopherson, Hang SU

From: Hang SU <darcysail@gmail.com>

Add assembly check in desc.h, to replace segment selector
magic number with macro definition.

Signed-off-by: Hang SU <darcy.sh@antgroup.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 lib/x86/desc.h    | 7 ++++++-
 x86/cstart64.S    | 8 ++++----
 x86/trampolines.S | 8 +++++---
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/lib/x86/desc.h b/lib/x86/desc.h
index aa6213d1..5634de94 100644
--- a/lib/x86/desc.h
+++ b/lib/x86/desc.h
@@ -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_ */
diff --git a/x86/cstart64.S b/x86/cstart64.S
index 4dff1102..2b14c076 100644
--- a/x86/cstart64.S
+++ b/x86/cstart64.S
@@ -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
 
diff --git a/x86/trampolines.S b/x86/trampolines.S
index 6a3df9c1..f0b05ab5 100644
--- a/x86/trampolines.S
+++ b/x86/trampolines.S
@@ -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
-- 
2.48.1.601.g30ceb7b040-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [kvm-unit-tests PATCH 3/3] x86: replace segment selector magic number with macro definition
  2025-02-21 22:54 ` [kvm-unit-tests PATCH 3/3] x86: replace segment selector magic number with macro definition Sean Christopherson
@ 2025-02-21 23:34   ` Sean Christopherson
  0 siblings, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2025-02-21 23:34 UTC (permalink / raw)
  To: Paolo Bonzini, kvm, Hang SU

On Fri, Feb 21, 2025, Sean Christopherson wrote:
> @@ -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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [kvm-unit-tests PATCH 0/3] x86: Use macros for selectors in asm
  2025-02-21 22:54 [kvm-unit-tests PATCH 0/3] x86: Use macros for selectors in asm Sean Christopherson
                   ` (2 preceding siblings ...)
  2025-02-21 22:54 ` [kvm-unit-tests PATCH 3/3] x86: replace segment selector magic number with macro definition Sean Christopherson
@ 2025-02-24 17:24 ` Sean Christopherson
  3 siblings, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2025-02-24 17:24 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, Hang SU

On Fri, 21 Feb 2025 14:54:03 -0800, Sean Christopherson wrote:
> Use macros for segment selectors in assembly instead of open coded literals.
> 
> v2:
>  - Commit to using __ASSEMBLER__.
> 
> v1: https://lore.kernel.org/all/20240621122640.2347541-1-darcy.sh@antgroup.com
> 
> [...]

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

--
https://github.com/kvm-x86/kvm-unit-tests/tree/next

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-02-24 17:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-21 22:54 [kvm-unit-tests PATCH 0/3] x86: Use macros for selectors in asm Sean Christopherson
2025-02-21 22:54 ` [kvm-unit-tests PATCH 1/3] x86: Move descriptor table selector #defines to the top of desc.h Sean Christopherson
2025-02-21 22:54 ` [kvm-unit-tests PATCH 2/3] x86: Commit to using __ASSEMBLER__ instead of __ASSEMBLY__ Sean Christopherson
2025-02-21 22:54 ` [kvm-unit-tests PATCH 3/3] x86: replace segment selector magic number with macro definition Sean Christopherson
2025-02-21 23:34   ` Sean Christopherson
2025-02-24 17:24 ` [kvm-unit-tests PATCH 0/3] x86: Use macros for selectors in asm Sean Christopherson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.