Skip to content

Commit 4763ed4

Browse files
author
H. Peter Anvin
committed
x86, mm: Clean up and simplify NX enablement
The 32- and 64-bit code used very different mechanisms for enabling NX, but even the 32-bit code was enabling NX in head_32.S if it is available. Furthermore, we had a bewildering collection of tests for the available of NX. This patch: a) merges the 32-bit set_nx() and the 64-bit check_efer() function into a single x86_configure_nx() function. EFER control is left to the head code. b) eliminates the nx_enabled variable entirely. Things that need to test for NX enablement can verify __supported_pte_mask directly, and cpu_has_nx gives the supported status of NX. Signed-off-by: H. Peter Anvin <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Brian Gerst <[email protected]> Cc: Yinghai Lu <[email protected]> Cc: Pekka Enberg <[email protected]> Cc: Vegard Nossum <[email protected]> Cc: Jeremy Fitzhardinge <[email protected]> Cc: Chris Wright <[email protected]> LKML-Reference: <[email protected]> Acked-by: Kees Cook <[email protected]>
1 parent 583140a commit 4763ed4

File tree

6 files changed

+13
-50
lines changed

6 files changed

+13
-50
lines changed

arch/x86/include/asm/proto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern void ia32_sysenter_target(void);
1616

1717
extern void syscall32_cpu_init(void);
1818

19-
extern void check_efer(void);
19+
extern void x86_configure_nx(void);
2020

2121
extern int reboot_force;
2222

arch/x86/kernel/cpu/common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ void __cpuinit cpu_init(void)
11361136
wrmsrl(MSR_KERNEL_GS_BASE, 0);
11371137
barrier();
11381138

1139-
check_efer();
1139+
x86_configure_nx();
11401140
if (cpu != 0)
11411141
enable_x2apic();
11421142

arch/x86/kernel/setup.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -787,21 +787,17 @@ void __init setup_arch(char **cmdline_p)
787787
strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
788788
*cmdline_p = command_line;
789789

790-
#ifdef CONFIG_X86_64
791790
/*
792791
* Must call this twice: Once just to detect whether hardware doesn't
793792
* support NX (so that the early EHCI debug console setup can safely
794793
* call set_fixmap(), and then again after parsing early parameters to
795794
* honor the respective command line option.
796795
*/
797-
check_efer();
798-
#endif
796+
x86_configure_nx();
799797

800798
parse_early_param();
801799

802-
#ifdef CONFIG_X86_64
803-
check_efer();
804-
#endif
800+
x86_configure_nx();
805801

806802
/* Must be before kernel pagetables are setup */
807803
vmi_activate();

arch/x86/mm/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ unsigned long __init_refok init_memory_mapping(unsigned long start,
146146
use_gbpages = direct_gbpages;
147147
#endif
148148

149-
set_nx();
150-
if (nx_enabled)
149+
/* XXX: replace this with Kees' improved messages */
150+
if (__supported_pte_mask & _PAGE_NX)
151151
printk(KERN_INFO "NX (Execute Disable) protection: active\n");
152152

153153
/* Enable PSE if available */

arch/x86/mm/setup_nx.c

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
#include <linux/init.h>
44

55
#include <asm/pgtable.h>
6+
#include <asm/proto.h>
67

7-
int nx_enabled;
8-
9-
#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
108
static int disable_nx __cpuinitdata;
119

1210
/*
@@ -22,48 +20,19 @@ static int __init noexec_setup(char *str)
2220
if (!str)
2321
return -EINVAL;
2422
if (!strncmp(str, "on", 2)) {
25-
__supported_pte_mask |= _PAGE_NX;
2623
disable_nx = 0;
2724
} else if (!strncmp(str, "off", 3)) {
2825
disable_nx = 1;
29-
__supported_pte_mask &= ~_PAGE_NX;
3026
}
27+
x86_configure_nx();
3128
return 0;
3229
}
3330
early_param("noexec", noexec_setup);
34-
#endif
35-
36-
#ifdef CONFIG_X86_PAE
37-
void __init set_nx(void)
38-
{
39-
unsigned int v[4], l, h;
40-
41-
if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
42-
cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
43-
44-
if ((v[3] & (1 << 20)) && !disable_nx) {
45-
rdmsr(MSR_EFER, l, h);
46-
l |= EFER_NX;
47-
wrmsr(MSR_EFER, l, h);
48-
nx_enabled = 1;
49-
__supported_pte_mask |= _PAGE_NX;
50-
}
51-
}
52-
}
53-
#else
54-
void set_nx(void)
55-
{
56-
}
57-
#endif
5831

59-
#ifdef CONFIG_X86_64
60-
void __cpuinit check_efer(void)
32+
void __cpuinit x86_configure_nx(void)
6133
{
62-
unsigned long efer;
63-
64-
rdmsrl(MSR_EFER, efer);
65-
if (!(efer & EFER_NX) || disable_nx)
34+
if (cpu_has_nx && !disable_nx)
35+
__supported_pte_mask |= _PAGE_NX;
36+
else
6637
__supported_pte_mask &= ~_PAGE_NX;
6738
}
68-
#endif
69-

arch/x86/xen/enlighten.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,10 +1082,8 @@ asmlinkage void __init xen_start_kernel(void)
10821082

10831083
__supported_pte_mask |= _PAGE_IOMAP;
10841084

1085-
#ifdef CONFIG_X86_64
10861085
/* Work out if we support NX */
1087-
check_efer();
1088-
#endif
1086+
x86_configure_nx();
10891087

10901088
xen_setup_features();
10911089

0 commit comments

Comments
 (0)