Apply patch (requested by jym in ticket #1380): netbsd-5
authorsnj <snj@NetBSD.org>
Thu, 22 Apr 2010 20:02:48 +0000
branchnetbsd-5
changeset 259668 7db1b1e4ce16
parent 259667 27a96bbe37ef
child 259669 3b416325a9c7
Apply patch (requested by jym in ticket #1380): Fix the NX regression issue observed on amd64 kernels, where per-page execution right was disabled (therefore leading to the inability of the kernel to detect fraudulent use of memory mappings marked as not being executable).
sys/arch/amd64/amd64/locore.S
sys/arch/amd64/amd64/machdep.c
sys/arch/amd64/amd64/mptramp.S
sys/arch/i386/i386/machdep.c
sys/arch/i386/isa/npx.c
sys/arch/x86/include/cpu.h
sys/arch/x86/include/cpuvar.h
sys/arch/x86/x86/cpu.c
sys/arch/x86/x86/cpu_topology.c
sys/arch/x86/x86/identcpu.c
sys/arch/x86/x86/pmap.c
sys/arch/xen/x86/cpu.c
--- a/sys/arch/amd64/amd64/locore.S	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/amd64/amd64/locore.S	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.47.8.3 2010/04/22 19:54:34 snj Exp $	*/
+/*	$NetBSD: locore.S,v 1.47.8.4 2010/04/22 20:02:48 snj Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -235,7 +235,7 @@
 #endif
 
 	.globl	_C_LABEL(cpu_id),_C_LABEL(cpu_vendorname), _C_LABEL(cpu_brand_id)
-	.globl	_C_LABEL(cpuid_level),_C_LABEL(cpu_feature),_C_LABEL(cpu_feature2)
+	.globl	_C_LABEL(cpuid_level)
 	.globl	_C_LABEL(esym),_C_LABEL(eblob),_C_LABEL(boothowto)
 	.globl	_C_LABEL(bootinfo),_C_LABEL(atdevbase)
 	.globl	_C_LABEL(proc0paddr),_C_LABEL(PDPpaddr)
@@ -245,10 +245,6 @@
 _C_LABEL(cpu):		.long	0	# are we 386, 386sx, or 486,
 					#   or Pentium, or..
 _C_LABEL(cpu_id):	.long	0	# saved from `cpuid' instruction
-_C_LABEL(cpu_feature):	.long	0	# feature flags from 'cpuid'
-					#   instruction
-_C_LABEL(cpu_feature2):	.long	0	# feature flags from 'cpuid'
-					#   instruction
 _C_LABEL(cpuid_level):	.long	-1	# max. level accepted by 'cpuid'
 					#   instruction
 _C_LABEL(cpu_vendorname):	.space	16	# vendor string returned by `cpuid'
@@ -300,7 +296,7 @@
 gdt64_end:
 
 farjmp64:
-	.long	longmode-KERNBASE
+	.long	_RELOC(longmode)
 	.word	GSEL(GCODE_SEL, SEL_KPL)
 	
 #endif	/* !XEN */
@@ -423,18 +419,11 @@
 	movl	$1,%eax
 	cpuid
 	movl	%eax,RELOC(cpu_id)
-	movl	%edx,RELOC(cpu_feature)
-	movl	%ecx,RELOC(cpu_feature2)
 
 	/* Brand ID is bits 0-7 of %ebx */
 	andl	$255,%ebx
 	movl	%ebx,RELOC(cpu_brand_id)
 
-	/* add AMD specific feature flags */
-	movl	$0x80000001,%eax
-	cpuid
-	orl	%edx,RELOC(cpu_feature)
-
 	/*
 	 * Finished with old stack; load new %esp now instead of later so we
 	 * can trace this code without having to worry about the trace trap
--- a/sys/arch/amd64/amd64/machdep.c	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/amd64/amd64/machdep.c	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.102.4.12 2009/12/01 19:29:54 snj Exp $	*/
+/*	$NetBSD: machdep.c,v 1.102.4.13 2010/04/22 20:02:48 snj Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008
@@ -112,7 +112,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.102.4.12 2009/12/01 19:29:54 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.102.4.13 2010/04/22 20:02:48 snj Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1392,9 +1392,9 @@
 	__PRINTK(("init_x86_64(0x%lx)\n", first_avail));
 	first_bt_vaddr = (vaddr_t) (first_avail + KERNBASE + PAGE_SIZE * 2);
 	__PRINTK(("first_bt_vaddr 0x%lx\n", first_bt_vaddr));
-	cpu_feature = cpu_info_primary.ci_feature_flags;
 	/* not on Xen... */
-	cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX);
+	cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR);
+	cpu_feature3 &= ~(CPUID_NOX);
 #endif /* XEN */
 
 	cpu_init_msrs(&cpu_info_primary, true);
--- a/sys/arch/amd64/amd64/mptramp.S	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/amd64/amd64/mptramp.S	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: mptramp.S,v 1.9 2008/05/13 22:39:17 ad Exp $	*/
+/*	$NetBSD: mptramp.S,v 1.9.8.1 2010/04/22 20:02:48 snj Exp $	*/
 
 /*-
  * Copyright (c) 2000 The NetBSD Foundation, Inc.
@@ -223,7 +223,7 @@
 	testq	%rdi, %rdi
 	jz	1b
 
-	movl	_C_LABEL(cpu_feature),%eax
+	movl	_C_LABEL(cpu_feature3),%eax
 	andl	$CPUID_NOX,%eax
 	jz	1f
 	movl	$MSR_EFER,%ecx
--- a/sys/arch/i386/i386/machdep.c	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/i386/i386/machdep.c	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.644.4.11 2009/10/03 23:49:50 snj Exp $	*/
+/*	$NetBSD: machdep.c,v 1.644.4.12 2010/04/22 20:02:48 snj Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2004, 2006, 2008, 2009
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.644.4.11 2009/10/03 23:49:50 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.644.4.12 2010/04/22 20:02:48 snj Exp $");
 
 #include "opt_beep.h"
 #include "opt_compat_ibcs2.h"
@@ -248,9 +248,6 @@
 
 int	physmem;
 
-unsigned int cpu_feature;
-unsigned int cpu_feature2;
-unsigned int cpu_feature_padlock;
 int	cpu_class;
 int	i386_fpu_present;
 int	i386_fpu_exception;
@@ -1432,8 +1429,6 @@
 	cpu_info_primary.ci_vcpu = &HYPERVISOR_shared_info->vcpu_info[0];
 #endif
 	cpu_probe(&cpu_info_primary);
-	cpu_feature = cpu_info_primary.ci_feature_flags;
-	cpu_feature2 = cpu_info_primary.ci_feature2_flags;
 	cpu_feature_padlock = cpu_info_primary.ci_padlock_flags;
 
 	proc0paddr = UAREA_TO_USER(proc0uarea);
@@ -1441,7 +1436,8 @@
 
 #ifdef XEN
 	/* not on Xen... */
-	cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX);
+	cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR);
+	cpu_feature3 &= ~(CPUID_NOX);
 	lwp0.l_addr->u_pcb.pcb_cr3 = PDPpaddr - KERNBASE;
 	__PRINTK(("pcb_cr3 0x%lx cr3 0x%lx\n",
 	    PDPpaddr - KERNBASE, xpmap_ptom(PDPpaddr - KERNBASE)));
--- a/sys/arch/i386/isa/npx.c	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/i386/isa/npx.c	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: npx.c,v 1.129.10.5 2008/11/27 03:37:02 snj Exp $	*/
+/*	$NetBSD: npx.c,v 1.129.10.6 2010/04/22 20:02:48 snj Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -96,7 +96,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npx.c,v 1.129.10.5 2008/11/27 03:37:02 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npx.c,v 1.129.10.6 2010/04/22 20:02:48 snj Exp $");
 
 #if 0
 #define IPRINTF(x)	printf x
@@ -124,6 +124,7 @@
 #include <uvm/uvm_extern.h>
 
 #include <machine/cpufunc.h>
+#include <machine/cpuvar.h>
 #include <machine/pcb.h>
 #include <machine/trap.h>
 #include <machine/specialreg.h>
--- a/sys/arch/x86/include/cpu.h	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/x86/include/cpu.h	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.h,v 1.9.4.1 2009/06/16 02:19:44 snj Exp $	*/
+/*	$NetBSD: cpu.h,v 1.9.4.2 2010/04/22 20:02:48 snj Exp $	*/
 
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
@@ -304,9 +304,6 @@
 
 extern int biosbasemem;
 extern int biosextmem;
-extern unsigned int cpu_feature;
-extern unsigned int cpu_feature2;
-extern unsigned int cpu_feature_padlock;
 extern int cpu;
 extern int cpuid_level;
 extern int cpu_class;
--- a/sys/arch/x86/include/cpuvar.h	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/x86/include/cpuvar.h	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/* 	$NetBSD: cpuvar.h,v 1.27.8.2 2009/10/05 11:37:14 sborrill Exp $ */
+/* 	$NetBSD: cpuvar.h,v 1.27.8.3 2010/04/22 20:02:48 snj Exp $ */
 
 /*-
  * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
@@ -142,6 +142,11 @@
 extern int cpu_vendor;
 extern bool x86_mp_online;
 
+extern uint32_t cpu_feature;
+extern uint32_t cpu_feature2;
+extern uint32_t cpu_feature3;
+extern uint32_t cpu_feature4;
+extern uint32_t cpu_feature_padlock;
 #endif
 
 #endif /* !_X86_CPUVAR_H_ */
--- a/sys/arch/x86/x86/cpu.c	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/x86/x86/cpu.c	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.57.4.3 2009/02/02 20:10:16 snj Exp $	*/
+/*	$NetBSD: cpu.c,v 1.57.4.4 2010/04/22 20:02:48 snj Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.57.4.3 2009/02/02 20:10:16 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.57.4.4 2010/04/22 20:02:48 snj Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -167,6 +167,13 @@
 uint32_t cpus_attached = 0;
 uint32_t cpus_running = 0;
 
+/* CPUID feature flags */
+uint32_t cpu_feature;  /* %edx */
+uint32_t cpu_feature2; /* %ecx */
+uint32_t cpu_feature3; /* extended features - %edx */
+uint32_t cpu_feature4; /* extended features - %ecx */
+uint32_t cpu_feature_padlock; /* VIA PadLock feature flags */
+
 extern char x86_64_doubleflt_stack[];
 
 bool x86_mp_online;
@@ -969,7 +976,7 @@
 		wrmsr(MSR_KERNELGSBASE, 0);
 	}
 
-	if (cpu_feature & CPUID_NOX)
+	if (cpu_feature3 & CPUID_NOX)
 		wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE);
 }
 #endif	/* __x86_64__ */
--- a/sys/arch/x86/x86/cpu_topology.c	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/x86/x86/cpu_topology.c	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu_topology.c,v 1.2.2.3 2009/06/16 02:23:31 snj Exp $	*/
+/*	$NetBSD: cpu_topology.c,v 1.2.2.4 2010/04/22 20:02:48 snj Exp $	*/
 
 /*-
  * Copyright (c) 2009 Mindaugas Rasiukevicius <rmind at NetBSD org>,
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cpu_topology.c,v 1.2.2.3 2009/06/16 02:23:31 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu_topology.c,v 1.2.2.4 2010/04/22 20:02:48 snj Exp $");
 
 #include <sys/param.h>
 #include <sys/bitops.h>
@@ -82,7 +82,8 @@
 	lextmode = descs[0];
 	if (lextmode >= 0x80000001) {
 		x86_cpuid(0x80000001, descs);
-		ci->ci_feature3_flags |= descs[3]; /* edx */
+		ci->ci_feature4_flags = descs[2]; /* ecx */
+		ci->ci_feature3_flags = descs[3]; /* edx */
 	}
 
 	/* Check for HTT support.  See notes below regarding AMD. */
--- a/sys/arch/x86/x86/identcpu.c	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/x86/x86/identcpu.c	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.10.4.5 2010/04/22 19:56:44 snj Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.10.4.6 2010/04/22 20:02:48 snj Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.10.4.5 2010/04/22 19:56:44 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.10.4.6 2010/04/22 20:02:48 snj Exp $");
 
 #include "opt_enhanced_speedstep.h"
 #include "opt_intel_odcm.h"
@@ -287,8 +287,8 @@
 	x86_cpuid(0x80000000, descs);
 	if (descs[0] >= 0x80000001) {
 		x86_cpuid(0x80000001, descs);
-		ci->ci_feature3_flags |= descs[3]; /* %edx */
-		ci->ci_feature4_flags = descs[2];  /* %ecx */
+		ci->ci_feature4_flags = descs[2]; /* %ecx */
+		ci->ci_feature3_flags = descs[3]; /* %edx */
 	}
 
 	cpu_probe_amd_cache(ci);
@@ -627,6 +627,8 @@
 		/* If first. */
 		cpu_feature = ci->ci_feature_flags;
 		cpu_feature2 = ci->ci_feature2_flags;
+		cpu_feature3 = ci->ci_feature3_flags;
+		cpu_feature4 = ci->ci_feature4_flags;
 		/* Early patch of text segment. */
 #ifndef XEN
 		x86_patch(true);
@@ -635,6 +637,8 @@
 		/* If not first. */
 		cpu_feature &= ci->ci_feature_flags;
 		cpu_feature2 &= ci->ci_feature2_flags;
+		cpu_feature3 &= ci->ci_feature3_flags;
+		cpu_feature4 &= ci->ci_feature4_flags;
 	}
 }
 
--- a/sys/arch/x86/x86/pmap.c	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/x86/x86/pmap.c	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.74.4.2 2010/02/14 13:35:44 bouyer Exp $	*/
+/*	$NetBSD: pmap.c,v 1.74.4.3 2010/04/22 20:02:48 snj Exp $	*/
 
 /*
  * Copyright (c) 2007 Manuel Bouyer.
@@ -154,7 +154,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.74.4.2 2010/02/14 13:35:44 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.74.4.3 2010/04/22 20:02:48 snj Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1074,7 +1074,7 @@
 	npte = ma | ((prot & VM_PROT_WRITE) ? PG_RW : PG_RO) |
 	     PG_V | PG_k;
 #ifndef XEN
-	if ((cpu_feature & CPUID_NOX) && !(prot & VM_PROT_EXECUTE))
+	if ((cpu_feature3 & CPUID_NOX) && !(prot & VM_PROT_EXECUTE))
 		npte |= PG_NX;
 #endif
 	opte = pmap_pte_testset (pte, npte); /* zap! */
@@ -1199,7 +1199,7 @@
 #else
 	unsigned long p1i;
 	vaddr_t kva_end;
-	pt_entry_t pg_nx = (cpu_feature & CPUID_NOX ? PG_NX : 0);
+	pt_entry_t pg_nx = (cpu_feature3 & CPUID_NOX ? PG_NX : 0);
 #endif
 
 	/*
--- a/sys/arch/xen/x86/cpu.c	Thu Apr 22 19:56:44 2010 +0000
+++ b/sys/arch/xen/x86/cpu.c	Thu Apr 22 20:02:48 2010 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.28.4.1 2008/11/13 00:04:07 snj Exp $	*/
+/*	$NetBSD: cpu.c,v 1.28.4.2 2010/04/22 20:02:49 snj Exp $	*/
 /* NetBSD: cpu.c,v 1.18 2004/02/20 17:35:01 yamt Exp  */
 
 /*-
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.28.4.1 2008/11/13 00:04:07 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.28.4.2 2010/04/22 20:02:49 snj Exp $");
 
 #include "opt_ddb.h"
 #include "opt_multiprocessor.h"
@@ -173,6 +173,13 @@
 uint32_t cpus_attached = 0;
 uint32_t cpus_running = 0;
 
+/* CPUID feature flags */
+uint32_t cpu_feature;  /* %edx */
+uint32_t cpu_feature2; /* %ecx */
+uint32_t cpu_feature3; /* extended features - %edx */
+uint32_t cpu_feature4; /* extended features - %ecx */
+uint32_t cpu_feature_padlock; /* VIA PadLock feature flags */
+
 bool x86_mp_online;
 paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
 
@@ -679,7 +686,6 @@
 {
 	struct cpu_info *ci = (struct cpu_info *)v;
 	int s, i;
-	uint32_t blacklist_features;
 
 #ifdef __x86_64__
         cpu_init_msrs(ci, true);
@@ -688,9 +694,8 @@
 	cpu_probe(ci);
 
 	/* not on Xen... */
-	blacklist_features = ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX); /* XXX add CPUID_SVM */
-
-	cpu_feature &= blacklist_features;
+	cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR); /* XXX add CPUID_SVM */
+	cpu_feature3 &= ~CPUID_NOX;
 
 	KDASSERT((ci->ci_flags & CPUF_PRESENT) == 0);
 	atomic_or_32(&ci->ci_flags, CPUF_PRESENT);