Skip to content

Commit 106b88d

Browse files
committed
Merge tag 'x86-asm-2024-01-08' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 asm updates from Ingo Molnar: "Replace magic numbers in GDT descriptor definitions & handling: - Introduce symbolic names via macros for descriptor types/fields/flags, and then use these symbolic names. - Clean up definitions a bit, such as GDT_ENTRY_INIT() - Fix/clean up details that became visibly inconsistent after the symbol-based code was introduced: - Unify accessed flag handling - Set the D/B size flag consistently & according to the HW specification" * tag 'x86-asm-2024-01-08' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/asm: Add DB flag to 32-bit percpu GDT entry x86/asm: Always set A (accessed) flag in GDT descriptors x86/asm: Replace magic numbers in GDT descriptors, script-generated change x86/asm: Replace magic numbers in GDT descriptors, preparations x86/asm: Provide new infrastructure for GDT descriptors
2 parents 33034c4 + bc90aef commit 106b88d

10 files changed

Lines changed: 105 additions & 58 deletions

File tree

arch/x86/boot/pm.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
#include "boot.h"
14+
#include <asm/desc_defs.h>
1415
#include <asm/segment.h>
1516

1617
/*
@@ -67,13 +68,13 @@ static void setup_gdt(void)
6768
being 8-byte unaligned. Intel recommends 16 byte alignment. */
6869
static const u64 boot_gdt[] __attribute__((aligned(16))) = {
6970
/* CS: code, read/execute, 4 GB, base 0 */
70-
[GDT_ENTRY_BOOT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff),
71+
[GDT_ENTRY_BOOT_CS] = GDT_ENTRY(DESC_CODE32, 0, 0xfffff),
7172
/* DS: data, read/write, 4 GB, base 0 */
72-
[GDT_ENTRY_BOOT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff),
73+
[GDT_ENTRY_BOOT_DS] = GDT_ENTRY(DESC_DATA32, 0, 0xfffff),
7374
/* TSS: 32-bit tss, 104 bytes, base 4096 */
7475
/* We only have a TSS here to keep Intel VT happy;
7576
we don't actually use it for anything. */
76-
[GDT_ENTRY_BOOT_TSS] = GDT_ENTRY(0x0089, 4096, 103),
77+
[GDT_ENTRY_BOOT_TSS] = GDT_ENTRY(DESC_TSS32, 4096, 103),
7778
};
7879
/* Xen HVM incorrectly stores a pointer to the gdt_ptr, instead
7980
of the gdt_ptr contents. Thus, make it static so it will

arch/x86/include/asm/desc_defs.h

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,56 @@
88
* archs.
99
*/
1010

11+
/*
12+
* Low-level interface mapping flags/field names to bits
13+
*/
14+
15+
/* Flags for _DESC_S (non-system) descriptors */
16+
#define _DESC_ACCESSED 0x0001
17+
#define _DESC_DATA_WRITABLE 0x0002
18+
#define _DESC_CODE_READABLE 0x0002
19+
#define _DESC_DATA_EXPAND_DOWN 0x0004
20+
#define _DESC_CODE_CONFORMING 0x0004
21+
#define _DESC_CODE_EXECUTABLE 0x0008
22+
23+
/* Common flags */
24+
#define _DESC_S 0x0010
25+
#define _DESC_DPL(dpl) ((dpl) << 5)
26+
#define _DESC_PRESENT 0x0080
27+
28+
#define _DESC_LONG_CODE 0x2000
29+
#define _DESC_DB 0x4000
30+
#define _DESC_GRANULARITY_4K 0x8000
31+
32+
/* System descriptors have a numeric "type" field instead of flags */
33+
#define _DESC_SYSTEM(code) (code)
34+
35+
/*
36+
* High-level interface mapping intended usage to low-level combinations
37+
* of flags
38+
*/
39+
40+
#define _DESC_DATA (_DESC_S | _DESC_PRESENT | _DESC_ACCESSED | \
41+
_DESC_DATA_WRITABLE)
42+
#define _DESC_CODE (_DESC_S | _DESC_PRESENT | _DESC_ACCESSED | \
43+
_DESC_CODE_READABLE | _DESC_CODE_EXECUTABLE)
44+
45+
#define DESC_DATA16 (_DESC_DATA)
46+
#define DESC_CODE16 (_DESC_CODE)
47+
48+
#define DESC_DATA32 (_DESC_DATA | _DESC_GRANULARITY_4K | _DESC_DB)
49+
#define DESC_DATA32_BIOS (_DESC_DATA | _DESC_DB)
50+
51+
#define DESC_CODE32 (_DESC_CODE | _DESC_GRANULARITY_4K | _DESC_DB)
52+
#define DESC_CODE32_BIOS (_DESC_CODE | _DESC_DB)
53+
54+
#define DESC_TSS32 (_DESC_SYSTEM(9) | _DESC_PRESENT)
55+
56+
#define DESC_DATA64 (_DESC_DATA | _DESC_GRANULARITY_4K | _DESC_DB)
57+
#define DESC_CODE64 (_DESC_CODE | _DESC_GRANULARITY_4K | _DESC_LONG_CODE)
58+
59+
#define DESC_USER (_DESC_DPL(3))
60+
1161
#ifndef __ASSEMBLY__
1262

1363
#include <linux/types.h>
@@ -22,19 +72,19 @@ struct desc_struct {
2272

2373
#define GDT_ENTRY_INIT(flags, base, limit) \
2474
{ \
25-
.limit0 = (u16) (limit), \
26-
.limit1 = ((limit) >> 16) & 0x0F, \
27-
.base0 = (u16) (base), \
28-
.base1 = ((base) >> 16) & 0xFF, \
29-
.base2 = ((base) >> 24) & 0xFF, \
30-
.type = (flags & 0x0f), \
31-
.s = (flags >> 4) & 0x01, \
32-
.dpl = (flags >> 5) & 0x03, \
33-
.p = (flags >> 7) & 0x01, \
34-
.avl = (flags >> 12) & 0x01, \
35-
.l = (flags >> 13) & 0x01, \
36-
.d = (flags >> 14) & 0x01, \
37-
.g = (flags >> 15) & 0x01, \
75+
.limit0 = ((limit) >> 0) & 0xFFFF, \
76+
.limit1 = ((limit) >> 16) & 0x000F, \
77+
.base0 = ((base) >> 0) & 0xFFFF, \
78+
.base1 = ((base) >> 16) & 0x00FF, \
79+
.base2 = ((base) >> 24) & 0x00FF, \
80+
.type = ((flags) >> 0) & 0x000F, \
81+
.s = ((flags) >> 4) & 0x0001, \
82+
.dpl = ((flags) >> 5) & 0x0003, \
83+
.p = ((flags) >> 7) & 0x0001, \
84+
.avl = ((flags) >> 12) & 0x0001, \
85+
.l = ((flags) >> 13) & 0x0001, \
86+
.d = ((flags) >> 14) & 0x0001, \
87+
.g = ((flags) >> 15) & 0x0001, \
3888
}
3989

4090
enum {
@@ -94,6 +144,7 @@ struct gate_struct {
94144

95145
typedef struct gate_struct gate_desc;
96146

147+
#ifndef _SETUP
97148
static inline unsigned long gate_offset(const gate_desc *g)
98149
{
99150
#ifdef CONFIG_X86_64
@@ -108,6 +159,7 @@ static inline unsigned long gate_segment(const gate_desc *g)
108159
{
109160
return g->segment;
110161
}
162+
#endif
111163

112164
struct desc_ptr {
113165
unsigned short size;

arch/x86/kernel/apm_32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ static DEFINE_MUTEX(apm_mutex);
420420
* This is for buggy BIOS's that refer to (real mode) segment 0x40
421421
* even though they are called in protected mode.
422422
*/
423-
static struct desc_struct bad_bios_desc = GDT_ENTRY_INIT(0x4092,
423+
static struct desc_struct bad_bios_desc = GDT_ENTRY_INIT(DESC_DATA32_BIOS,
424424
(unsigned long)__va(0x400UL), PAGE_SIZE - 0x400 - 1);
425425

426426
static const char driver_version[] = "1.16ac"; /* no spaces */

arch/x86/kernel/cpu/common.c

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -188,45 +188,37 @@ DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
188188
* TLS descriptors are currently at a different place compared to i386.
189189
* Hopefully nobody expects them at a fixed place (Wine?)
190190
*/
191-
[GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
192-
[GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
193-
[GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
194-
[GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
195-
[GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
196-
[GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
191+
[GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff),
192+
[GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(DESC_CODE64, 0, 0xfffff),
193+
[GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(DESC_DATA64, 0, 0xfffff),
194+
[GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(DESC_CODE32 | DESC_USER, 0, 0xfffff),
195+
[GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(DESC_DATA64 | DESC_USER, 0, 0xfffff),
196+
[GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(DESC_CODE64 | DESC_USER, 0, 0xfffff),
197197
#else
198-
[GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
199-
[GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
200-
[GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
201-
[GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
198+
[GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff),
199+
[GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
200+
[GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(DESC_CODE32 | DESC_USER, 0, 0xfffff),
201+
[GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(DESC_DATA32 | DESC_USER, 0, 0xfffff),
202202
/*
203203
* Segments used for calling PnP BIOS have byte granularity.
204204
* They code segments and data segments have fixed 64k limits,
205205
* the transfer segment sizes are set at run time.
206206
*/
207-
/* 32-bit code */
208-
[GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
209-
/* 16-bit code */
210-
[GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
211-
/* 16-bit data */
212-
[GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
213-
/* 16-bit data */
214-
[GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
215-
/* 16-bit data */
216-
[GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
207+
[GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(DESC_CODE32_BIOS, 0, 0xffff),
208+
[GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(DESC_CODE16, 0, 0xffff),
209+
[GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(DESC_DATA16, 0, 0xffff),
210+
[GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(DESC_DATA16, 0, 0),
211+
[GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(DESC_DATA16, 0, 0),
217212
/*
218213
* The APM segments have byte granularity and their bases
219214
* are set at run time. All have 64k limits.
220215
*/
221-
/* 32-bit code */
222-
[GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
223-
/* 16-bit code */
224-
[GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
225-
/* data */
226-
[GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
227-
228-
[GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
229-
[GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
216+
[GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(DESC_CODE32_BIOS, 0, 0xffff),
217+
[GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(DESC_CODE16, 0, 0xffff),
218+
[GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(DESC_DATA32_BIOS, 0, 0xffff),
219+
220+
[GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
221+
[GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
230222
#endif
231223
} };
232224
EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);

arch/x86/kernel/head64.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ EXPORT_SYMBOL(vmemmap_base);
7171
* GDT used on the boot CPU before switching to virtual addresses.
7272
*/
7373
static struct desc_struct startup_gdt[GDT_ENTRIES] __initdata = {
74-
[GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
75-
[GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
76-
[GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
74+
[GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff),
75+
[GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(DESC_CODE64, 0, 0xfffff),
76+
[GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(DESC_DATA64, 0, 0xfffff),
7777
};
7878

7979
/*

arch/x86/kernel/setup_percpu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ void __init pcpu_populate_pte(unsigned long addr)
106106
static inline void setup_percpu_segment(int cpu)
107107
{
108108
#ifdef CONFIG_X86_32
109-
struct desc_struct d = GDT_ENTRY_INIT(0x8092, per_cpu_offset(cpu),
110-
0xFFFFF);
109+
struct desc_struct d = GDT_ENTRY_INIT(DESC_DATA32,
110+
per_cpu_offset(cpu), 0xFFFFF);
111111

112112
write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_PERCPU, &d, DESCTYPE_S);
113113
#endif

arch/x86/platform/pvh/head.S

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/elfnote.h>
1212
#include <linux/init.h>
1313
#include <linux/linkage.h>
14+
#include <asm/desc_defs.h>
1415
#include <asm/segment.h>
1516
#include <asm/asm.h>
1617
#include <asm/boot.h>
@@ -148,11 +149,11 @@ SYM_DATA_END(gdt)
148149
SYM_DATA_START_LOCAL(gdt_start)
149150
.quad 0x0000000000000000 /* NULL descriptor */
150151
#ifdef CONFIG_X86_64
151-
.quad GDT_ENTRY(0xa09a, 0, 0xfffff) /* PVH_CS_SEL */
152+
.quad GDT_ENTRY(DESC_CODE64, 0, 0xfffff) /* PVH_CS_SEL */
152153
#else
153-
.quad GDT_ENTRY(0xc09a, 0, 0xfffff) /* PVH_CS_SEL */
154+
.quad GDT_ENTRY(DESC_CODE32, 0, 0xfffff) /* PVH_CS_SEL */
154155
#endif
155-
.quad GDT_ENTRY(0xc092, 0, 0xfffff) /* PVH_DS_SEL */
156+
.quad GDT_ENTRY(DESC_DATA32, 0, 0xfffff) /* PVH_DS_SEL */
156157
SYM_DATA_END_LABEL(gdt_start, SYM_L_LOCAL, gdt_end)
157158

158159
.balign 16

arch/x86/realmode/rm/reboot.S

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#include <linux/linkage.h>
3+
#include <asm/desc_defs.h>
34
#include <asm/segment.h>
45
#include <asm/page_types.h>
56
#include <asm/processor-flags.h>
@@ -153,5 +154,5 @@ SYM_DATA_START(machine_real_restart_gdt)
153154
* base value 0x100; since this is consistent with real mode
154155
* semantics we don't have to reload the segments once CR0.PE = 0.
155156
*/
156-
.quad GDT_ENTRY(0x0093, 0x100, 0xffff)
157+
.quad GDT_ENTRY(DESC_DATA16, 0x100, 0xffff)
157158
SYM_DATA_END(machine_real_restart_gdt)

drivers/firmware/efi/libstub/x86-5lvl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ bool efi_no5lvl;
1313
static void (*la57_toggle)(void *cr3);
1414

1515
static const struct desc_struct gdt[] = {
16-
[GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
17-
[GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
16+
[GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff),
17+
[GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(DESC_CODE64, 0, 0xfffff),
1818
};
1919

2020
/*

drivers/pnp/pnpbios/bioscalls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ do { \
6060
set_desc_limit(&gdt[(selname) >> 3], (size) - 1); \
6161
} while(0)
6262

63-
static struct desc_struct bad_bios_desc = GDT_ENTRY_INIT(0x4092,
63+
static struct desc_struct bad_bios_desc = GDT_ENTRY_INIT(DESC_DATA32_BIOS,
6464
(unsigned long)__va(0x400UL), PAGE_SIZE - 0x400 - 1);
6565

6666
/*

0 commit comments

Comments
 (0)